Embedded System Design 2 - Project
ADXL362.c File Reference

All code for the ADXL362 accelerometer. More...

#include <stdint.h>
#include <stdbool.h>
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_usart.h"
#include "ADXL362.h"
#include "pin_mapping.h"
#include "debug_dbprint.h"
#include "delay.h"
#include "util.h"

Go to the source code of this file.

Macros

#define ADXL_REG_DEVID_AD   0x00 /* Reset: 0xAD */
 
#define ADXL_REG_DEVID_MST   0x01 /* Reset: 0x1D */
 
#define ADXL_REG_PARTID   0x02 /* Reset: 0xF2 */
 
#define ADXL_REG_REVID   0x03 /* Reset: 0x01 (can be incremented) */
 
#define ADXL_REG_XDATA   0x08
 
#define ADXL_REG_YDATA   0x09
 
#define ADXL_REG_ZDATA   0x0A
 
#define ADXL_REG_STATUS   0x0B
 
#define ADXL_REG_TEMP_L   0x14
 
#define ADXL_REG_TEMP_H   0x15
 
#define ADXL_REG_SOFT_RESET   0x1F /* Needs to be 0x52 ("R") written to for a soft reset */
 
#define ADXL_REG_THRESH_ACT_L   0x20 /* 7:0 bits used */
 
#define ADXL_REG_THRESH_ACT_H   0x21 /* 2:0 bits used */
 
#define ADXL_REG_ACT_INACT_CTL   0x27 /* Activity/Inactivity control register: XX - XX - LINKLOOP - LINKLOOP - INACT_REF - INACT_EN - ACT_REF - ACT_EN */
 
#define ADXL_REG_INTMAP1   0x2A /* INT_LOW -- AWAKE -- INACT -- ACT -- FIFO_OVERRUN -- FIFO_WATERMARK -- FIFO_READY -- DATA_READY */
 
#define ADXL_REG_INTMAP2   0x2B /* INT_LOW -- AWAKE -- INACT -- ACT -- FIFO_OVERRUN -- FIFO_WATERMARK -- FIFO_READY -- DATA_READY */
 
#define ADXL_REG_FILTER_CTL   0x2C /* Write FFxx xxxx (FF = 00 for +-2g, 01 for =-4g, 1x for +- 8g) for measurement range selection */
 
#define ADXL_REG_POWER_CTL   0x2D /* Write xxxx xxMM (MM = 10) to: measurement mode */
 

Functions

static void powerADXL (bool enabled)
 Enable or disable the power to the accelerometer. More...
 
static void initADXL_SPI (void)
 Initialize USARTx in SPI mode according to the settings required by the accelerometer. More...
 
static void softResetADXL (void)
 Soft reset accelerometer. More...
 
static void resetHandlerADXL (void)
 Soft reset accelerometer handler. More...
 
static uint8_t readADXL (uint8_t address)
 Read an SPI byte from the accelerometer (8 bits) using a given address. More...
 
static void writeADXL (uint8_t address, uint8_t data)
 Write an SPI byte to the accelerometer (8 bits) using a given address and specified data. More...
 
static void readADXL_XYZDATA (void)
 Read the X-Y-Z data registers in the XYZDATA[] field using burst reads. More...
 
static bool checkID_ADXL (void)
 Check if the ID is correct. More...
 
static int32_t convertGRangeToGValue (int8_t sensorValue)
 Convert sensor value in +-g range to mg value. More...
 
void initADXL (void)
 Initialize the accelerometer. More...
 
uint16_t ADXL_getCounter (void)
 Getter for the ADXL_triggercounter variable. More...
 
void ADXL_clearCounter (void)
 Method to set the ADXL_triggercounter variable back to zero. More...
 
void ADXL_setTriggered (bool triggered)
 Setter for the ADXL_triggered variable. More...
 
bool ADXL_getTriggered (void)
 Getter for the ADXL_triggered variable. More...
 
void ADXL_ackInterrupt (void)
 Acknowledge the interrupt from the accelerometer. More...
 
void ADXL_enableSPI (bool enabled)
 Enable or disable the SPI pins and USART0/1 clock and peripheral to the accelerometer. More...
 
void ADXL_enableMeasure (bool enabled)
 Enable or disable measurement mode. More...
 
void ADXL_configRange (ADXL_Range_t givenRange)
 Configure the measurement range and store the selected one in a global variable for later (internal) use. More...
 
void ADXL_configODR (ADXL_ODR_t givenODR)
 Configure the Output Data Rate (ODR). More...
 
void ADXL_configActivity (uint8_t gThreshold)
 Configure the accelerometer to work in (referenced) activity threshold mode. More...
 
void ADXL_readValues (void)
 Read and display "g" values forever with a 100ms interval. More...
 
void testADXL (void)
 This method goes through all of the ODR settings to see the influence they have on power usage. The measurement range is the default one (+-2g). More...
 

Variables

volatile bool ADXL_triggered = false
 
volatile uint16_t ADXL_triggercounter = 0
 
int8_t XYZDATA [3] = { 0x00, 0x00, 0x00 }
 
ADXL_Range_t range
 
bool ADXL_VDD_initialized = false
 

Detailed Description

All code for the ADXL362 accelerometer.

Version
3.1
Author
Brecht Van Eeckhoudt

Versions

  • v1.0: Started with the code from https://github.com/Fescron/Project-LabEmbeddedDesign1/tree/master/code/SLSTK3400A_ADXL362 Changed file name from accel.c to ADXL362.c.
  • v1.1: Changed PinModeSet out value to 0 in initADXL_VCC.
  • v1.2: Changed last argument in GPIO_PinModeSet in method initADXL_VCC to change the pin mode and enable the pin in one statement.
  • v1.3: Changed some methods and global variables to be static (~hidden).
  • v1.4: Changed delay method and cleaned up includes.
  • v1.5: Added get/set method for the static variable ADXL_triggered.
  • v1.6: Changed a lot of things...
  • v1.7: Updated documentation and chanced USART0 to ADXL_SPI.
  • v1.8: Updated code with new DEFINE checks.
  • v1.9: Started using custom enum for range & ODR configuration methods.
  • v2.0: Added testing method to go through all the settings, moved the register definitions.
  • v2.1: Disabled SPI pins on hard reset, cleaned up some code.
  • v2.2: Added functionality to check the number of interrupts.
  • v2.3: Updated documentation.
  • v2.4: Changed error numbering.
  • v2.5: Updated ODR enum and changed masking logic for register settings.
  • v3.0: Added functionality to exit methods after error call and updated version number.
  • v3.1: Removed static before the local variables (not necessary).

Todo:
Future improvements:
  • Check configurations by reading the registers again and return true/false when the registers have/don't have the correct values.
  • Enable wake-up mode (writeADXL(ADXL_REG_POWER_CTL, 0b00001000); // 5th bit)
  • Enable loop mode (with act/inact time registers?) so interrupts don't need to be acknowledged by host (writeADXL(ADXL_REG_ACT_INACT_CTL, 0b00110000);)

License

Copyright (C) 2019 - Brecht Van Eeckhoudt

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

A copy of the GNU General Public License can be found in the LICENSE file along with this source code.


Some methods use code obtained from examples from Silicon Labs' GitHub. These sections are licensed under the Silabs License Agreement. See the file "Silabs_License_Agreement.txt" for details. Before using this software for any purpose, you must agree to the terms of that agreement.

Definition in file ADXL362.c.

Macro Definition Documentation

◆ ADXL_REG_ACT_INACT_CTL

#define ADXL_REG_ACT_INACT_CTL   0x27 /* Activity/Inactivity control register: XX - XX - LINKLOOP - LINKLOOP - INACT_REF - INACT_EN - ACT_REF - ACT_EN */

Definition at line 96 of file ADXL362.c.

◆ ADXL_REG_DEVID_AD

#define ADXL_REG_DEVID_AD   0x00 /* Reset: 0xAD */

Definition at line 83 of file ADXL362.c.

◆ ADXL_REG_DEVID_MST

#define ADXL_REG_DEVID_MST   0x01 /* Reset: 0x1D */

Definition at line 84 of file ADXL362.c.

◆ ADXL_REG_FILTER_CTL

#define ADXL_REG_FILTER_CTL   0x2C /* Write FFxx xxxx (FF = 00 for +-2g, 01 for =-4g, 1x for +- 8g) for measurement range selection */

Definition at line 99 of file ADXL362.c.

◆ ADXL_REG_INTMAP1

#define ADXL_REG_INTMAP1   0x2A /* INT_LOW -- AWAKE -- INACT -- ACT -- FIFO_OVERRUN -- FIFO_WATERMARK -- FIFO_READY -- DATA_READY */

Definition at line 97 of file ADXL362.c.

◆ ADXL_REG_INTMAP2

#define ADXL_REG_INTMAP2   0x2B /* INT_LOW -- AWAKE -- INACT -- ACT -- FIFO_OVERRUN -- FIFO_WATERMARK -- FIFO_READY -- DATA_READY */

Definition at line 98 of file ADXL362.c.

◆ ADXL_REG_PARTID

#define ADXL_REG_PARTID   0x02 /* Reset: 0xF2 */

Definition at line 85 of file ADXL362.c.

◆ ADXL_REG_POWER_CTL

#define ADXL_REG_POWER_CTL   0x2D /* Write xxxx xxMM (MM = 10) to: measurement mode */

Definition at line 100 of file ADXL362.c.

◆ ADXL_REG_REVID

#define ADXL_REG_REVID   0x03 /* Reset: 0x01 (can be incremented) */

Definition at line 86 of file ADXL362.c.

◆ ADXL_REG_SOFT_RESET

#define ADXL_REG_SOFT_RESET   0x1F /* Needs to be 0x52 ("R") written to for a soft reset */

Definition at line 93 of file ADXL362.c.

◆ ADXL_REG_STATUS

#define ADXL_REG_STATUS   0x0B

Definition at line 90 of file ADXL362.c.

◆ ADXL_REG_TEMP_H

#define ADXL_REG_TEMP_H   0x15

Definition at line 92 of file ADXL362.c.

◆ ADXL_REG_TEMP_L

#define ADXL_REG_TEMP_L   0x14

Definition at line 91 of file ADXL362.c.

◆ ADXL_REG_THRESH_ACT_H

#define ADXL_REG_THRESH_ACT_H   0x21 /* 2:0 bits used */

Definition at line 95 of file ADXL362.c.

◆ ADXL_REG_THRESH_ACT_L

#define ADXL_REG_THRESH_ACT_L   0x20 /* 7:0 bits used */

Definition at line 94 of file ADXL362.c.

◆ ADXL_REG_XDATA

#define ADXL_REG_XDATA   0x08

Definition at line 87 of file ADXL362.c.

◆ ADXL_REG_YDATA

#define ADXL_REG_YDATA   0x09

Definition at line 88 of file ADXL362.c.

◆ ADXL_REG_ZDATA

#define ADXL_REG_ZDATA   0x0A

Definition at line 89 of file ADXL362.c.

Function Documentation

◆ ADXL_ackInterrupt()

void ADXL_ackInterrupt ( void  )

Acknowledge the interrupt from the accelerometer.

Read a certain register (necessary if the accelerometer is not in linked-loop mode) and clear the variable.

Definition at line 230 of file ADXL362.c.

231 {
233  ADXL_triggered = false;
234 }
#define ADXL_REG_STATUS
Definition: ADXL362.c:90
static uint8_t readADXL(uint8_t address)
Read an SPI byte from the accelerometer (8 bits) using a given address.
Definition: ADXL362.c:782
volatile bool ADXL_triggered
Definition: ADXL362.c:104

◆ ADXL_clearCounter()

void ADXL_clearCounter ( void  )

Method to set the ADXL_triggercounter variable back to zero.

Definition at line 188 of file ADXL362.c.

189 {
191 }
volatile uint16_t ADXL_triggercounter
Definition: ADXL362.c:105

◆ ADXL_configActivity()

void ADXL_configActivity ( uint8_t  gThreshold)

Configure the accelerometer to work in (referenced) activity threshold mode.

Route activity detector to INT1 pin using INTMAP1, isolate bits and write settings to both threshold registers.

Referenced means that during the initialization a reference acceleration gets measured (like for example 1 g on a certain axis) and stored internally. This value always gets internally subtracted from a measured acceleration value to calculate the final value and check if it exceeds the set threshold:

  • ABS(acceleration - reference) > threshold
Parameters
[in]gThresholdThreshold [g].

Definition at line 468 of file ADXL362.c.

469 {
470  /* Map activity detector to INT1 pin */
471  writeADXL(ADXL_REG_INTMAP1, 0b00010000); /* Bit 4 selects activity detector */
472 
473  /* Enable referenced activity threshold mode (last two bits) */
474  writeADXL(ADXL_REG_ACT_INACT_CTL, 0b00000011);
475 
476  /* Convert g value to "codes"
477  * THRESH_ACT [codes] = Threshold Value [g] × Scale Factor [LSB per g] */
478  uint8_t threshold;
479 
480  if (range == ADXL_RANGE_2G) threshold = gThreshold * 1000;
481  else if (range == ADXL_RANGE_4G) threshold = gThreshold * 500;
482  else if (range == ADXL_RANGE_8G) threshold = gThreshold * 250;
483  else
484  {
485 
486 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
487  dbcrit("Range wrong, can't set gThreshold!");
488 #endif /* DEBUG_DBPRINT */
489 
490  error(26);
491 
492  /* Exit function */
493  return;
494  }
495 
496  /* Isolate bits using masks and shifting */
497  uint8_t low = (threshold & 0b0011111111);
498  uint8_t high = (threshold & 0b1100000000) >> 8;
499 
500  /* Set threshold register values (total: 11bit unsigned) */
501  writeADXL(ADXL_REG_THRESH_ACT_L, low); /* 7:0 bits used */
502  writeADXL(ADXL_REG_THRESH_ACT_H, high); /* 2:0 bits used */
503 
504 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
505  dbinfoInt("ADXL362: Activity configured: ", gThreshold, "g");
506 #endif /* DEBUG_DBPRINT */
507 
508 }
static void writeADXL(uint8_t address, uint8_t data)
Write an SPI byte to the accelerometer (8 bits) using a given address and specified data...
Definition: ADXL362.c:816
void dbinfoInt(char *message1, int32_t value, char *message2)
Print an info value surrounded by two strings (char array) to USARTx.
Definition: dbprint.c:567
#define ADXL_REG_THRESH_ACT_L
Definition: ADXL362.c:94
#define ADXL_REG_THRESH_ACT_H
Definition: ADXL362.c:95
ADXL_Range_t range
Definition: ADXL362.c:107
void error(uint8_t number)
Error method.
Definition: util.c:131
#define ADXL_REG_INTMAP1
Definition: ADXL362.c:97
#define ADXL_REG_ACT_INACT_CTL
Definition: ADXL362.c:96
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:539

◆ ADXL_configODR()

void ADXL_configODR ( ADXL_ODR_t  givenODR)

Configure the Output Data Rate (ODR).

Parameters
[in]givenODRThe selected ODR.

Definition at line 411 of file ADXL362.c.

412 {
413  /* Get value in register */
414  uint8_t reg = readADXL(ADXL_REG_FILTER_CTL);
415 
416  /* AND with mask to keep the bits we don't want to change */
417  reg &= 0b11111000;
418 
419  /* Set ODR (OR with new setting bits, last three bits) */
420  if (givenODR == ADXL_ODR_12_5_HZ) writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000000));
421  else if (givenODR == ADXL_ODR_25_HZ) writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000001));
422  else if (givenODR == ADXL_ODR_50_HZ) writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000010));
423  else if (givenODR == ADXL_ODR_100_HZ) writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000011));
424  else if (givenODR == ADXL_ODR_200_HZ) writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000100));
425  else if (givenODR == ADXL_ODR_400_HZ) writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000101));
426  else
427  {
428 
429 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
430  dbcrit("Non-existing ODR selected!");
431 #endif /* DEBUG_DBPRINT */
432 
433  error(25);
434 
435  /* Exit function */
436  return;
437  }
438 
439 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
440  if (givenODR == ADXL_ODR_12_5_HZ) dbinfo("ADXL362: ODR set at 12.5 Hz");
441  else if (givenODR == ADXL_ODR_25_HZ) dbinfo("ADXL362: ODR set at 25 Hz");
442  else if (givenODR == ADXL_ODR_50_HZ) dbinfo("ADXL362: ODR set at 50 Hz");
443  else if (givenODR == ADXL_ODR_100_HZ) dbinfo("ADXL362: ODR set at 100 Hz");
444  else if (givenODR == ADXL_ODR_200_HZ) dbinfo("ADXL362: ODR set at 200 Hz");
445  else if (givenODR == ADXL_ODR_400_HZ) dbinfo("ADXL362: ODR set at 400 Hz");
446 #endif /* DEBUG_DBPRINT */
447 
448 }
static void writeADXL(uint8_t address, uint8_t data)
Write an SPI byte to the accelerometer (8 bits) using a given address and specified data...
Definition: ADXL362.c:816
#define ADXL_REG_FILTER_CTL
Definition: ADXL362.c:99
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
Definition: dbprint.c:503
void error(uint8_t number)
Error method.
Definition: util.c:131
static uint8_t readADXL(uint8_t address)
Read an SPI byte from the accelerometer (8 bits) using a given address.
Definition: ADXL362.c:782
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:539

◆ ADXL_configRange()

void ADXL_configRange ( ADXL_Range_t  givenRange)

Configure the measurement range and store the selected one in a global variable for later (internal) use.

When a range of, for example "2g" is selected, the real range is "+-2g".

Parameters
[in]givenRangeThe selected range.

Definition at line 358 of file ADXL362.c.

359 {
360  /* Get value in register */
361  uint8_t reg = readADXL(ADXL_REG_FILTER_CTL);
362 
363  /* AND with mask to keep the bits we don't want to change */
364  reg &= 0b00111111;
365 
366  /* Set measurement range (OR with new setting bits, first two bits) */
367  if (givenRange == ADXL_RANGE_2G)
368  {
369  writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b00000000));
371  }
372  else if (givenRange == ADXL_RANGE_4G)
373  {
374  writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b01000000));
376  }
377  else if (givenRange == ADXL_RANGE_8G)
378  {
379  writeADXL(ADXL_REG_FILTER_CTL, (reg | 0b10000000));
381  }
382  else
383  {
384 
385 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
386  dbcrit("Non-existing range selected!");
387 #endif /* DEBUG_DBPRINT */
388 
389  error(24);
390 
391  /* Exit function */
392  return;
393  }
394 
395 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
396  if (range == ADXL_RANGE_2G) dbinfo("ADXL362: Measurement mode +- 2g selected");
397  else if (range == ADXL_RANGE_4G) dbinfo("ADXL362: Measurement mode +- 4g selected");
398  else if (range == ADXL_RANGE_8G) dbinfo("ADXL362: Measurement mode +- 8g selected");
399 #endif /* DEBUG_DBPRINT */
400 
401 }
static void writeADXL(uint8_t address, uint8_t data)
Write an SPI byte to the accelerometer (8 bits) using a given address and specified data...
Definition: ADXL362.c:816
#define ADXL_REG_FILTER_CTL
Definition: ADXL362.c:99
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
Definition: dbprint.c:503
ADXL_Range_t range
Definition: ADXL362.c:107
void error(uint8_t number)
Error method.
Definition: util.c:131
static uint8_t readADXL(uint8_t address)
Read an SPI byte from the accelerometer (8 bits) using a given address.
Definition: ADXL362.c:782
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:539

◆ ADXL_enableMeasure()

void ADXL_enableMeasure ( bool  enabled)

Enable or disable measurement mode.

Parameters
[in]enabled
  • true - Enable measurement mode.
  • false - Disable measurement mode (standby).

Definition at line 310 of file ADXL362.c.

311 {
312  if (enabled)
313  {
314  /* Get value in register */
315  uint8_t reg = readADXL(ADXL_REG_POWER_CTL);
316 
317  /* AND with mask to keep the bits we don't want to change */
318  reg &= 0b11111100;
319 
320  /* Enable measurements (OR with new setting bits) */
321  writeADXL(ADXL_REG_POWER_CTL, reg | 0b00000010); /* Last 2 bits are measurement mode */
322 
323 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
324  dbinfo("ADXL362: Measurement enabled");
325 #endif /* DEBUG_DBPRINT */
326 
327  }
328  else
329  {
330  /* Get value in register */
331  uint8_t reg = readADXL(ADXL_REG_POWER_CTL);
332 
333  /* AND with mask to keep the bits we don't want to change */
334  reg &= 0b11111100;
335 
336  /* Disable measurements (OR with new setting bits) */
337  writeADXL(ADXL_REG_POWER_CTL, reg | 0b00000000); /* Last 2 bits are measurement mode */
338 
339 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
340  dbinfo("ADXL362: Measurement disabled (standby)");
341 #endif /* DEBUG_DBPRINT */
342 
343  }
344 }
static void writeADXL(uint8_t address, uint8_t data)
Write an SPI byte to the accelerometer (8 bits) using a given address and specified data...
Definition: ADXL362.c:816
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
Definition: dbprint.c:503
#define ADXL_REG_POWER_CTL
Definition: ADXL362.c:100
static uint8_t readADXL(uint8_t address)
Read an SPI byte from the accelerometer (8 bits) using a given address.
Definition: ADXL362.c:782

◆ ADXL_enableSPI()

void ADXL_enableSPI ( bool  enabled)

Enable or disable the SPI pins and USART0/1 clock and peripheral to the accelerometer.

Parameters
[in]enabled
  • true - Enable the SPI pins and USART0/1 clock and peripheral to the accelerometer.
  • false - Disable the SPI pins and USART0/1 clock and peripheral to the accelerometer.

Definition at line 245 of file ADXL362.c.

246 {
247  if (enabled)
248  {
249  /* Enable USART clock and peripheral */
250  if (ADXL_SPI == USART0) CMU_ClockEnable(cmuClock_USART0, true);
251  else if (ADXL_SPI == USART1) CMU_ClockEnable(cmuClock_USART1, true);
252  else
253  {
254 
255 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
256  dbcrit("Wrong peripheral selected!");
257 #endif /* DEBUG_DBPRINT */
258 
259  error(22);
260 
261  /* Exit function */
262  return;
263  }
264 
265  USART_Enable(ADXL_SPI, usartEnable);
266 
267  /* In the case of gpioModePushPull", the last argument directly sets the pin state */
268  GPIO_PinModeSet(ADXL_CLK_PORT, ADXL_CLK_PIN, gpioModePushPull, 0); /* US0_CLK is push pull */
269  GPIO_PinModeSet(ADXL_NCS_PORT, ADXL_NCS_PIN, gpioModePushPull, 1); /* US0_CS is push pull */
270  GPIO_PinModeSet(ADXL_MOSI_PORT, ADXL_MOSI_PIN, gpioModePushPull, 1); /* US0_TX (MOSI) is push pull */
271  GPIO_PinModeSet(ADXL_MISO_PORT, ADXL_MISO_PIN, gpioModeInput, 1); /* US0_RX (MISO) is input */
272  }
273  else
274  {
275  /* Disable USART clock and peripheral */
276  if (ADXL_SPI == USART0) CMU_ClockEnable(cmuClock_USART0, false);
277  else if (ADXL_SPI == USART1) CMU_ClockEnable(cmuClock_USART1, false);
278  else
279  {
280 
281 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
282  dbcrit("Wrong peripheral selected!");
283 #endif /* DEBUG_DBPRINT */
284 
285  error(23);
286 
287  /* Exit function */
288  return;
289  }
290 
291  USART_Enable(ADXL_SPI, usartDisable);
292 
293  /* gpioModeDisabled: Pull-up if DOUT is set. */
294  GPIO_PinModeSet(ADXL_CLK_PORT, ADXL_CLK_PIN, gpioModeDisabled, 0);
295  GPIO_PinModeSet(ADXL_NCS_PORT, ADXL_NCS_PIN, gpioModeDisabled, 1);
296  GPIO_PinModeSet(ADXL_MOSI_PORT, ADXL_MOSI_PIN, gpioModeDisabled, 1);
297  GPIO_PinModeSet(ADXL_MISO_PORT, ADXL_MISO_PIN, gpioModeDisabled, 1);
298  }
299 }
#define ADXL_CLK_PORT
Definition: pin_mapping.h:72
#define ADXL_NCS_PIN
Definition: pin_mapping.h:75
#define ADXL_SPI
Definition: pin_mapping.h:66
#define ADXL_CLK_PIN
Definition: pin_mapping.h:73
#define ADXL_MOSI_PORT
Definition: pin_mapping.h:68
#define ADXL_NCS_PORT
Definition: pin_mapping.h:74
#define ADXL_MISO_PIN
Definition: pin_mapping.h:71
void error(uint8_t number)
Error method.
Definition: util.c:131
#define ADXL_MOSI_PIN
Definition: pin_mapping.h:69
#define ADXL_MISO_PORT
Definition: pin_mapping.h:70
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:539

◆ ADXL_getCounter()

uint16_t ADXL_getCounter ( void  )

Getter for the ADXL_triggercounter variable.

Returns
The value of ADXL_triggercounter.

Definition at line 178 of file ADXL362.c.

179 {
180  return (ADXL_triggercounter);
181 }
volatile uint16_t ADXL_triggercounter
Definition: ADXL362.c:105

◆ ADXL_getTriggered()

bool ADXL_getTriggered ( void  )

Getter for the ADXL_triggered variable.

Returns
The value of ADXL_triggered.

Definition at line 216 of file ADXL362.c.

217 {
218  return (ADXL_triggered);
219 }
volatile bool ADXL_triggered
Definition: ADXL362.c:104

◆ ADXL_readValues()

void ADXL_readValues ( void  )

Read and display "g" values forever with a 100ms interval.

Definition at line 515 of file ADXL362.c.

516 {
517  uint32_t counter = 0;
518 
519  /* Enable measurement mode */
520  ADXL_enableMeasure(true);
521 
522  /* Infinite loop */
523  while (1)
524  {
525  led(true); /* Enable LED */
526 
527  readADXL_XYZDATA(); /* Read XYZ sensor data */
528 
529 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
530  /* Print XYZ sensor data */
531  dbprint("\r[");
532  dbprintInt(counter);
533  dbprint("] X: ");
535  dbprint(" mg | Y: ");
537  dbprint(" mg | Z: ");
539  dbprint(" mg "); /* Extra spacing is to overwrite other data if it's remaining (see \r) */
540 #endif /* DEBUG_DBPRINT */
541 
542  led(false); /* Disable LED */
543 
544  counter++;
545 
546  delay(100);
547  }
548 }
void dbprint(char *message)
Print a string (char array) to USARTx.
Definition: dbprint.c:373
void ADXL_enableMeasure(bool enabled)
Enable or disable measurement mode.
Definition: ADXL362.c:310
int8_t XYZDATA[3]
Definition: ADXL362.c:106
static void readADXL_XYZDATA(void)
Read the X-Y-Z data registers in the XYZDATA[] field using burst reads.
Definition: ADXL362.c:842
static int32_t convertGRangeToGValue(int8_t sensorValue)
Convert sensor value in +-g range to mg value.
Definition: ADXL362.c:939
void delay(uint32_t msDelay)
Wait for a certain amount of milliseconds in EM2/3.
Definition: delay.c:124
void dbprintInt(int32_t value)
Print a number in decimal notation to USARTx.
Definition: dbprint.c:738
void led(bool enabled)
Enable or disable the LED.
Definition: util.c:103

◆ ADXL_setTriggered()

void ADXL_setTriggered ( bool  triggered)

Setter for the ADXL_triggered variable.

Parameters
[in]triggered
  • true - Set ADXL_triggered to true.
  • false - Set ADXL_triggered to false.

Definition at line 202 of file ADXL362.c.

203 {
204  ADXL_triggered = triggered;
206 }
volatile uint16_t ADXL_triggercounter
Definition: ADXL362.c:105
volatile bool ADXL_triggered
Definition: ADXL362.c:104

◆ checkID_ADXL()

static bool checkID_ADXL ( void  )
static

Check if the ID is correct.

Note
This is a static method because it's only internally used in this file and called by other methods if necessary.
Returns
  • true - Correct ID returned.
  • false - Incorrect ID returned.

Definition at line 918 of file ADXL362.c.

919 {
920  return (readADXL(ADXL_REG_DEVID_AD) == 0xAD);
921 }
#define ADXL_REG_DEVID_AD
Definition: ADXL362.c:83
static uint8_t readADXL(uint8_t address)
Read an SPI byte from the accelerometer (8 bits) using a given address.
Definition: ADXL362.c:782

◆ convertGRangeToGValue()

static int32_t convertGRangeToGValue ( int8_t  sensorValue)
static

Convert sensor value in +-g range to mg value.

Note
Info found at http://ozzmaker.com/accelerometer-to-g/
This is a static method because it's only internally used in this file and called by other methods if necessary.
Parameters
[in]sensorValueValue in g-range returned by sensor.
Returns
The calculated mg value.

Definition at line 939 of file ADXL362.c.

940 {
941  /* Explanation of the used numbers:
942  * - 255 = (-) 128 + 127
943  * - 2 = "+" & "-"
944  * - 1000 = "m"g */
945 
946  if (range == ADXL_RANGE_2G) return ((2*2*1000 / 255) * sensorValue);
947  else if (range == ADXL_RANGE_4G) return ((2*4*1000 / 255) * sensorValue);
948  else if (range == ADXL_RANGE_8G) return ((2*8*1000 / 255) * sensorValue);
949  else
950  {
951 
952 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
953  dbcrit("Range wrong, can't calculate mg value!");
954 #endif /* DEBUG_DBPRINT */
955 
956  error(27);
957 
958  /* Exit function */
959  return (0);
960  }
961 }
ADXL_Range_t range
Definition: ADXL362.c:107
void error(uint8_t number)
Error method.
Definition: util.c:131
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:539

◆ initADXL()

void initADXL ( void  )

Initialize the accelerometer.

This method calls all the other internal necessary functions. Clock enable functionality is gathered here instead of in lower (static) functions.

Definition at line 132 of file ADXL362.c.

133 {
134  /* Enable necessary clocks (just in case) */
135  CMU_ClockEnable(cmuClock_HFPER, true); /* GPIO and USART0/1 are High Frequency Peripherals */
136  CMU_ClockEnable(cmuClock_GPIO, true);
137 
138  /* Initialize and power VDD pin */
139  powerADXL(true);
140 
141  /* Power-up delay of 40 ms */
142  delay(40);
143 
144  /* Enable necessary clock (just in case) */
145  if (ADXL_SPI == USART0) CMU_ClockEnable(cmuClock_USART0, true);
146  else if (ADXL_SPI == USART1) CMU_ClockEnable(cmuClock_USART1, true);
147  else
148  {
149 
150 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
151  dbcrit("Wrong peripheral selected!");
152 #endif /* DEBUG_DBPRINT */
153 
154  /* Disable power to the VDD pin */
155  powerADXL(false);
156 
157  error(21);
158 
159  /* Exit function */
160  return;
161  }
162 
163  /* Initialize USART0/1 as SPI slave (also initialize CS pin) */
164  initADXL_SPI();
165 
166  /* Soft reset ADXL handler */
168 }
#define ADXL_SPI
Definition: pin_mapping.h:66
static void resetHandlerADXL(void)
Soft reset accelerometer handler.
Definition: ADXL362.c:697
void error(uint8_t number)
Error method.
Definition: util.c:131
static void powerADXL(bool enabled)
Enable or disable the power to the accelerometer.
Definition: ADXL362.c:875
static void initADXL_SPI(void)
Initialize USARTx in SPI mode according to the settings required by the accelerometer.
Definition: ADXL362.c:647
void delay(uint32_t msDelay)
Wait for a certain amount of milliseconds in EM2/3.
Definition: delay.c:124
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:539

◆ initADXL_SPI()

static void initADXL_SPI ( void  )
static

Initialize USARTx in SPI mode according to the settings required by the accelerometer.

Configure pins, configure USARTx in SPI mode, route the pins, enable USARTx and set CS high. Necessary clocks are enabled in a previous method.

Note
This is a static method because it's only internally used in this file and called by other methods if necessary.

Definition at line 647 of file ADXL362.c.

648 {
649  /* Configure GPIO */
650  /* In the case of gpioModePushPull", the last argument directly sets the pin state */
651  GPIO_PinModeSet(ADXL_CLK_PORT, ADXL_CLK_PIN, gpioModePushPull, 0); /* US0_CLK is push pull */
652  GPIO_PinModeSet(ADXL_NCS_PORT, ADXL_NCS_PIN, gpioModePushPull, 1); /* US0_CS is push pull */
653  GPIO_PinModeSet(ADXL_MOSI_PORT, ADXL_MOSI_PIN, gpioModePushPull, 1); /* US0_TX (MOSI) is push pull */
654  GPIO_PinModeSet(ADXL_MISO_PORT, ADXL_MISO_PIN, gpioModeInput, 1); /* US0_RX (MISO) is input */
655 
656  /* Start with default config */
657  USART_InitSync_TypeDef config = USART_INITSYNC_DEFAULT;
658 
659  /* Modify some settings */
660  config.enable = false; /* making sure to keep USART disabled until we've set everything up */
661  config.refFreq = 0; /* USART/UART reference clock assumed when configuring baud rate setup. Set to 0 to use the currently configured reference clock. */
662  config.baudrate = 4000000; /* CLK freq is 1 MHz (1000000) */
663  config.databits = usartDatabits8; /* master mode */
664  config.master = true; /* master mode */
665  config.msbf = true; /* send MSB first */
666  config.clockMode = usartClockMode0; /* clock idle low, sample on rising/first edge (Clock polarity/phase mode = CPOL/CPHA) */
667  config.prsRxEnable = false; /* If enabled: Enable USART Rx via PRS. */
668  config.autoTx = false; /* If enabled: Enable AUTOTX mode. Transmits as long as RX is not full. Generates underflows if TX is empty. */
669  config.autoCsEnable = false; /* CS pin controlled by hardware, not firmware */
670 
671  /* Initialize USART0/1 with the configured parameters */
672  USART_InitSync(ADXL_SPI, &config);
673 
674  /* Set the pin location for USART0/1 (before: USART_ROUTE_LOCATION_LOC0) */
675  ADXL_SPI->ROUTE = USART_ROUTE_CLKPEN | USART_ROUTE_CSPEN | USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | ADXL_SPI_LOC;
676 
677  /* Enable USART0/1 */
678  USART_Enable(ADXL_SPI, usartEnable);
679 
680  /* Set CS high (active low!) */
681  GPIO_PinOutSet(gpioPortE, 13);
682 }
#define ADXL_CLK_PORT
Definition: pin_mapping.h:72
#define ADXL_NCS_PIN
Definition: pin_mapping.h:75
#define ADXL_SPI
Definition: pin_mapping.h:66
#define ADXL_CLK_PIN
Definition: pin_mapping.h:73
#define ADXL_MOSI_PORT
Definition: pin_mapping.h:68
#define ADXL_NCS_PORT
Definition: pin_mapping.h:74
#define ADXL_MISO_PIN
Definition: pin_mapping.h:71
#define ADXL_SPI_LOC
Definition: pin_mapping.h:67
#define ADXL_MOSI_PIN
Definition: pin_mapping.h:69
#define ADXL_MISO_PORT
Definition: pin_mapping.h:70

◆ powerADXL()

static void powerADXL ( bool  enabled)
static

Enable or disable the power to the accelerometer.

This method also initializes the pin-mode if necessary. Necessary clocks are enabled in a previous method.

Note
This is a static method because it's only internally used in this file and called by other methods if necessary.
Parameters
[in]enabled
  • true - Enable the GPIO pin connected to the VDD pin of the accelerometer.
  • false - Disable the GPIO pin connected to the VDD pin of the accelerometer.

Definition at line 875 of file ADXL362.c.

876 {
877  /* Initialize VDD pin if not already the case */
879  {
880  /* In the case of gpioModePushPull", the last argument directly sets the pin state */
881  GPIO_PinModeSet(ADXL_VDD_PORT, ADXL_VDD_PIN, gpioModePushPull, enabled);
882 
883  ADXL_VDD_initialized = true;
884  }
885  else
886  {
887  if (enabled) GPIO_PinOutSet(ADXL_VDD_PORT, ADXL_VDD_PIN); /* Enable VDD pin */
888  else GPIO_PinOutClear(ADXL_VDD_PORT, ADXL_VDD_PIN); /* Disable VDD pin */
889  }
890 }
#define ADXL_VDD_PIN
Definition: pin_mapping.h:82
#define ADXL_VDD_PORT
Definition: pin_mapping.h:81
bool ADXL_VDD_initialized
Definition: ADXL362.c:108

◆ readADXL()

static uint8_t readADXL ( uint8_t  address)
static

Read an SPI byte from the accelerometer (8 bits) using a given address.

Note
This is a static method because it's only internally used in this file and called by other methods if necessary.
Parameters
[in]addressThe register address to read from.
Returns
The response (one byte, uint8_t).

Definition at line 782 of file ADXL362.c.

783 {
784  uint8_t response;
785 
786  /* Set CS low (active low!) */
787  GPIO_PinOutClear(ADXL_NCS_PORT, ADXL_NCS_PIN);
788 
789  /* 3-byte operation according to datasheet */
790  USART_SpiTransfer(ADXL_SPI, 0x0B); /* "read" instruction */
791  USART_SpiTransfer(ADXL_SPI, address); /* Address */
792  response = USART_SpiTransfer(ADXL_SPI, 0x00); /* Read response */
793 
794  /* Set CS high */
795  GPIO_PinOutSet(ADXL_NCS_PORT, ADXL_NCS_PIN);
796 
797  return (response);
798 }
#define ADXL_NCS_PIN
Definition: pin_mapping.h:75
#define ADXL_SPI
Definition: pin_mapping.h:66
#define ADXL_NCS_PORT
Definition: pin_mapping.h:74

◆ readADXL_XYZDATA()

static void readADXL_XYZDATA ( void  )
static

Read the X-Y-Z data registers in the XYZDATA[] field using burst reads.

Response data gets put in XYZDATA array (global volatile variable).

Note
This is a static method because it's only internally used in this file and called by other methods if necessary.

Definition at line 842 of file ADXL362.c.

843 {
844  /* CS low (active low!) */
845  GPIO_PinOutClear(ADXL_NCS_PORT, ADXL_NCS_PIN);
846 
847  /* Burst read (address auto-increments) */
848  USART_SpiTransfer(ADXL_SPI, 0x0B); /* "read" instruction */
849  USART_SpiTransfer(ADXL_SPI, ADXL_REG_XDATA); /* Address */
850  XYZDATA[0] = USART_SpiTransfer(ADXL_SPI, 0x00); /* Read response */
851  XYZDATA[1] = USART_SpiTransfer(ADXL_SPI, 0x00); /* Read response */
852  XYZDATA[2] = USART_SpiTransfer(ADXL_SPI, 0x00); /* Read response */
853 
854  /* CS high */
855  GPIO_PinOutSet(ADXL_NCS_PORT, ADXL_NCS_PIN);
856 }
#define ADXL_NCS_PIN
Definition: pin_mapping.h:75
#define ADXL_SPI
Definition: pin_mapping.h:66
#define ADXL_NCS_PORT
Definition: pin_mapping.h:74
#define ADXL_REG_XDATA
Definition: ADXL362.c:87
int8_t XYZDATA[3]
Definition: ADXL362.c:106

◆ resetHandlerADXL()

static void resetHandlerADXL ( void  )
static

Soft reset accelerometer handler.

If the first ID check fails, the MCU is put on hold for one second and the ID gets checked again.

Note
This is a static method because it's only internally used in this file and called by other methods if necessary.

Definition at line 697 of file ADXL362.c.

698 {
699  uint8_t retries = 0;
700 
701  /* Soft reset ADXL */
702  softResetADXL();
703 
704  /* First try to get the correct ID failed */
705  if (!checkID_ADXL())
706  {
707  retries++;
708 
709  delay(1000);
710 
711  /* Soft reset */
712  softResetADXL();
713 
714  /* Second try to get the correct ID failed */
715  if (!checkID_ADXL())
716  {
717  retries++;
718 
719  delay(1000);
720 
721  /* Soft reset */
722  softResetADXL();
723 
724  /* Third try to get the correct ID failed
725  * resorting to "hard" reset! */
726  if (!checkID_ADXL())
727  {
728  retries++;
729 
730  powerADXL(false);
731  ADXL_enableSPI(false); /* Make sure the accelerometer doesn't get power through the SPI pins */
732 
733  delay(1000);
734 
735  powerADXL(true);
736  ADXL_enableSPI(true);
737 
738  delay(1000);
739 
740  /* Soft reset */
741  softResetADXL();
742 
743  /* Last try to get the correct ID failed */
744  if (!checkID_ADXL())
745  {
746 
747 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
748  dbcrit("ADXL362 initialization failed");
749 #endif /* DEBUG_DBPRINT */
750 
751  error(20);
752 
753  /* Exit function */
754  return;
755  }
756  }
757  }
758  }
759 
760 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
761  if (retries < 2) dbinfoInt("ADXL362 initialized (", retries, " soft reset retries)");
762  else dbwarnInt("ADXL362 initialized (had to \"hard reset\", ", retries, " soft reset retries)");
763 #endif /* DEBUG_DBPRINT */
764 
765 }
void dbinfoInt(char *message1, int32_t value, char *message2)
Print an info value surrounded by two strings (char array) to USARTx.
Definition: dbprint.c:567
void dbwarnInt(char *message1, int32_t value, char *message2)
Print a warning value surrounded by two strings (char array) to USARTx.
Definition: dbprint.c:598
void error(uint8_t number)
Error method.
Definition: util.c:131
static void powerADXL(bool enabled)
Enable or disable the power to the accelerometer.
Definition: ADXL362.c:875
void delay(uint32_t msDelay)
Wait for a certain amount of milliseconds in EM2/3.
Definition: delay.c:124
void ADXL_enableSPI(bool enabled)
Enable or disable the SPI pins and USART0/1 clock and peripheral to the accelerometer.
Definition: ADXL362.c:245
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:539
static bool checkID_ADXL(void)
Check if the ID is correct.
Definition: ADXL362.c:918
static void softResetADXL(void)
Soft reset accelerometer.
Definition: ADXL362.c:901

◆ softResetADXL()

static void softResetADXL ( void  )
static

Soft reset accelerometer.

Note
This is a static method because it's only internally used in this file and called by other methods if necessary.

Definition at line 901 of file ADXL362.c.

902 {
903  writeADXL(ADXL_REG_SOFT_RESET, 0x52); /* 0x52 = "R" */
904 }
static void writeADXL(uint8_t address, uint8_t data)
Write an SPI byte to the accelerometer (8 bits) using a given address and specified data...
Definition: ADXL362.c:816
#define ADXL_REG_SOFT_RESET
Definition: ADXL362.c:93

◆ testADXL()

void testADXL ( void  )

This method goes through all of the ODR settings to see the influence they have on power usage. The measurement range is the default one (+-2g).

To get the "correct" currents the delay method puts the MCU to EM2/3 sleep and the SPI lines are disabled. The order of the test is:

  • Wait 5 seconds
  • Soft reset the accelerometer
  • One second in "standby" mode
  • Enable "measurement" mode
  • One second in ODR 12.5 Hz
  • One second in ODR 25 Hz
  • One second in ODR 50 Hz
  • One second in ODR 100 Hz
  • One second in ODR 200 Hz
  • One second in ODR 400 Hz
  • Soft reset the accelerometer

Definition at line 571 of file ADXL362.c.

572 {
573  delay(5000);
574 
575  /* Soft reset ADXL */
576  softResetADXL();
577 
578  /* Standby */
579  ADXL_enableSPI(false); /* Disable SPI functionality */
580  delay(1000);
581  ADXL_enableSPI(true); /* Enable SPI functionality */
582 
583  /* ODR 12,5Hz */
584  writeADXL(ADXL_REG_FILTER_CTL, 0b00010000); /* last 3 bits are ODR */
585 
586  /* Enable measurements */
587  writeADXL(ADXL_REG_POWER_CTL, 0b00000010); /* last 2 bits are measurement mode */
588 
589  ADXL_enableSPI(false); /* Disable SPI functionality */
590  delay(1000);
591  ADXL_enableSPI(true); /* Enable SPI functionality */
592 
593  /* ODR 25Hz */
594  writeADXL(ADXL_REG_FILTER_CTL, 0b00010001); /* last 3 bits are ODR */
595 
596  ADXL_enableSPI(false); /* Disable SPI functionality */
597  delay(1000);
598  ADXL_enableSPI(true); /* Enable SPI functionality */
599 
600  /* ODR 50Hz */
601  writeADXL(ADXL_REG_FILTER_CTL, 0b00010010); /* last 3 bits are ODR */
602 
603  ADXL_enableSPI(false); /* Disable SPI functionality */
604  delay(1000);
605  ADXL_enableSPI(true); /* Enable SPI functionality */
606 
607  /* ODR 100Hz (default) */
608  writeADXL(ADXL_REG_FILTER_CTL, 0b00010011); /* last 3 bits are ODR */
609 
610  ADXL_enableSPI(false); /* Disable SPI functionality */
611  delay(1000);
612  ADXL_enableSPI(true); /* Enable SPI functionality */
613 
614  /* ODR 200Hz */
615  writeADXL(ADXL_REG_FILTER_CTL, 0b00010100); /* last 3 bits are ODR */
616 
617  ADXL_enableSPI(false); /* Disable SPI functionality */
618  delay(1000);
619  ADXL_enableSPI(true); /* Enable SPI functionality */
620 
621  /* ODR 400Hz */
622  writeADXL(ADXL_REG_FILTER_CTL, 0b00010101); /* last 3 bits are ODR */
623 
624  ADXL_enableSPI(false); /* Disable SPI functionality */
625  delay(1000);
626  ADXL_enableSPI(true); /* Enable SPI functionality */
627 
628  /* Soft reset ADXL */
629  softResetADXL();
630 }
static void writeADXL(uint8_t address, uint8_t data)
Write an SPI byte to the accelerometer (8 bits) using a given address and specified data...
Definition: ADXL362.c:816
#define ADXL_REG_FILTER_CTL
Definition: ADXL362.c:99
#define ADXL_REG_POWER_CTL
Definition: ADXL362.c:100
void delay(uint32_t msDelay)
Wait for a certain amount of milliseconds in EM2/3.
Definition: delay.c:124
void ADXL_enableSPI(bool enabled)
Enable or disable the SPI pins and USART0/1 clock and peripheral to the accelerometer.
Definition: ADXL362.c:245
static void softResetADXL(void)
Soft reset accelerometer.
Definition: ADXL362.c:901

◆ writeADXL()

static void writeADXL ( uint8_t  address,
uint8_t  data 
)
static

Write an SPI byte to the accelerometer (8 bits) using a given address and specified data.

Note
This is a static method because it's only internally used in this file and called by other methods if necessary.
Parameters
[in]addressThe register address to write the data to (one byte, uint8_t).
[in]dataThe data to write to the address (one byte, uint8_t).

Definition at line 816 of file ADXL362.c.

817 {
818  /* Set CS low (active low!) */
819  GPIO_PinOutClear(ADXL_NCS_PORT, ADXL_NCS_PIN);
820 
821  /* 3-byte operation according to datasheet */
822  USART_SpiTransfer(ADXL_SPI, 0x0A); /* "write" instruction */
823  USART_SpiTransfer(ADXL_SPI, address); /* Address */
824  USART_SpiTransfer(ADXL_SPI, data); /* Data */
825 
826  /* Set CS high */
827  GPIO_PinOutSet(ADXL_NCS_PORT, ADXL_NCS_PIN);
828 }
#define ADXL_NCS_PIN
Definition: pin_mapping.h:75
#define ADXL_SPI
Definition: pin_mapping.h:66
#define ADXL_NCS_PORT
Definition: pin_mapping.h:74
MeasurementData_t data
Definition: main.c:189

Variable Documentation

◆ ADXL_triggercounter

volatile uint16_t ADXL_triggercounter = 0

Definition at line 105 of file ADXL362.c.

◆ ADXL_triggered

volatile bool ADXL_triggered = false

Definition at line 104 of file ADXL362.c.

◆ ADXL_VDD_initialized

bool ADXL_VDD_initialized = false

Definition at line 108 of file ADXL362.c.

◆ range

ADXL_Range_t range

Definition at line 107 of file ADXL362.c.

◆ XYZDATA

int8_t XYZDATA[3] = { 0x00, 0x00, 0x00 }

Definition at line 106 of file ADXL362.c.