Embedded System Design 2 - Project
|
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 |
All code for the ADXL362 accelerometer.
out
value to 0 in initADXL_VCC. ADXL_triggered
. USART0
to ADXL_SPI
. error
call and updated version number. static
before the local variables (not necessary).writeADXL(ADXL_REG_POWER_CTL, 0b00001000); // 5th bit
)writeADXL(ADXL_REG_ACT_INACT_CTL, 0b00110000);
)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.
#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_FILTER_CTL 0x2C /* Write FFxx xxxx (FF = 00 for +-2g, 01 for =-4g, 1x for +- 8g) for measurement range selection */ |
#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_POWER_CTL 0x2D /* Write xxxx xxMM (MM = 10) to: measurement mode */ |
#define ADXL_REG_REVID 0x03 /* Reset: 0x01 (can be incremented) */ |
#define ADXL_REG_SOFT_RESET 0x1F /* Needs to be 0x52 ("R") written to for a soft reset */ |
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.
void ADXL_clearCounter | ( | void | ) |
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
[in] | gThreshold | Threshold [g]. |
Definition at line 468 of file ADXL362.c.
void ADXL_configODR | ( | ADXL_ODR_t | givenODR | ) |
Configure the Output Data Rate (ODR).
[in] | givenODR | The selected ODR. |
Definition at line 411 of file ADXL362.c.
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".
[in] | givenRange | The selected range. |
Definition at line 358 of file ADXL362.c.
void ADXL_enableMeasure | ( | bool | enabled | ) |
Enable or disable measurement mode.
[in] | enabled |
|
Definition at line 310 of file ADXL362.c.
void ADXL_enableSPI | ( | bool | enabled | ) |
Enable or disable the SPI pins and USART0/1 clock and peripheral to the accelerometer.
[in] | enabled |
|
Definition at line 245 of file ADXL362.c.
uint16_t ADXL_getCounter | ( | void | ) |
bool ADXL_getTriggered | ( | void | ) |
void ADXL_readValues | ( | void | ) |
Read and display "g" values forever with a 100ms interval.
Definition at line 515 of file ADXL362.c.
void ADXL_setTriggered | ( | bool | triggered | ) |
|
static |
Check if the ID is correct.
true
- Correct ID returned. false
- Incorrect ID returned. Definition at line 918 of file ADXL362.c.
|
static |
Convert sensor value in +-g range to mg value.
[in] | sensorValue | Value in g-range returned by sensor. |
Definition at line 939 of file ADXL362.c.
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.
|
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.
Definition at line 647 of file ADXL362.c.
|
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.
[in] | enabled |
|
Definition at line 875 of file ADXL362.c.
|
static |
Read an SPI byte from the accelerometer (8 bits) using a given address.
[in] | address | The register address to read from. |
uint8_t
). Definition at line 782 of file ADXL362.c.
|
static |
|
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.
Definition at line 697 of file ADXL362.c.
|
static |
Soft reset accelerometer.
Definition at line 901 of file ADXL362.c.
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:
Definition at line 571 of file ADXL362.c.
|
static |
Write an SPI byte to the accelerometer (8 bits) using a given address and specified data.
[in] | address | The register address to write the data to (one byte, uint8_t ). |
[in] | data | The data to write to the address (one byte, uint8_t ). |
Definition at line 816 of file ADXL362.c.
ADXL_Range_t range |