Loading...
Searching...
No Matches
a3g4250d.c
1/***********************************************************************************
2 * @file A3G4250D.c *
3 * @author Matt Ricci *
4 * @addtogroup A3G4250D *
5 * *
6 * @todo Move private interface methods (read/write register) to static functions *
7 * with internal prototypes. *
8 * @{ *
9 ***********************************************************************************/
10
11#include "a3g4250d.h"
12
13#include "string.h"
14
15static uint8_t A3G4250D_readRegister(A3G4250D_t *, uint8_t);
16static void A3G4250D_writeRegister(A3G4250D_t *, uint8_t, uint8_t);
17
18/* =============================================================================== */
31 A3G4250D_t *gyro,
32 SPI_t *spi,
33 GPIOpin_t cs,
34 float sensitivity,
35 const uint8_t *axes,
36 const int8_t *sign
37) {
38 gyro->spi = spi;
39 gyro->cs = cs;
40 gyro->base.sensitivity = sensitivity;
41 gyro->base.dataSize = A3G4250D_DATA_TOTAL;
46 gyro->base.bias = gyro->bias;
47 gyro->base.axes = gyro->axes;
48 gyro->base.sign = gyro->sign;
49 gyro->base.gyroData = gyro->gyroData;
50 gyro->base.rawGyroData = gyro->rawGyroData;
51 memcpy(gyro->base.axes, axes, A3G4250D_DATA_COUNT);
52 memcpy(gyro->base.sign, sign, A3G4250D_DATA_COUNT);
53
54 // TODO:
55 // Make scale & sensitivity for gyroscopes configurable on init.
56 // See kx134_1211.c for example.
57
58 A3G4250D_writeRegister(
59 gyro,
60 A3G4250D_CTRL_REG1,
61 (A3G4250D_CTRL_REG1_ODR_800Hz // Set data rate to 800Hz
62 | A3G4250D_CTRL_REG1_AXIS_ENABLE // Enable axis measurement
63 | A3G4250D_CTRL_REG1_PD_ENABLE) // Set power mode to normal
64 );
65
66 return *gyro;
67}
68
69/******************************** DEVICE METHODS ********************************/
70
71/* =============================================================================== */
80void A3G4250D_readGyro(Gyro_t *gyro, float *out) {
81 uint8_t bytes[A3G4250D_DATA_TOTAL];
82 gyro->readRawBytes(gyro, bytes);
83 gyro->processRawBytes(gyro, bytes, out);
84}
85
86/* =============================================================================== */
95 gyro->readRawBytes(gyro, gyro->rawGyroData);
96 gyro->processRawBytes(gyro, gyro->rawGyroData, gyro->gyroData);
97}
98
99/* =============================================================================== */
109void A3G4250D_processRawBytes(Gyro_t *gyro, uint8_t *bytes, float *out) {
110 for (int i = 0; i < A3G4250D_DATA_COUNT; i++) {
111 out[i] = (int16_t)(((uint16_t)bytes[i * 2] << 8) | bytes[i * 2 + 1]); // Process bytes
112 out[i] = (out[i] * gyro->sign[i] * gyro->sensitivity) - gyro->bias[i]; // Scale and offset
113 }
114}
115
116/* =============================================================================== */
125void A3G4250D_readRawBytes(Gyro_t *gyro, uint8_t *out) {
126#define INDEX_AXES(index, byte) 2 * gyro->axes[index] + byte
127 out[INDEX_AXES(0, 0)] = A3G4250D_readRegister((A3G4250D_t *)gyro, A3G4250D_OUT_X_H); // gyro X high
128 out[INDEX_AXES(0, 1)] = A3G4250D_readRegister((A3G4250D_t *)gyro, A3G4250D_OUT_X_L); // gyro X low
129 out[INDEX_AXES(1, 0)] = A3G4250D_readRegister((A3G4250D_t *)gyro, A3G4250D_OUT_Y_H); // gyro Y high
130 out[INDEX_AXES(1, 1)] = A3G4250D_readRegister((A3G4250D_t *)gyro, A3G4250D_OUT_Y_L); // gyro Y low
131 out[INDEX_AXES(2, 0)] = A3G4250D_readRegister((A3G4250D_t *)gyro, A3G4250D_OUT_Z_H); // gyro Z high
132 out[INDEX_AXES(2, 1)] = A3G4250D_readRegister((A3G4250D_t *)gyro, A3G4250D_OUT_Z_L); // gyro Z low
133#undef INDEX_AXES
134}
135
136/******************************** INTERFACE METHODS ********************************/
137
138// @TODO: Implement burst read function readRegisters and update A3G4250D_readRawBytes
139
140void A3G4250D_writeRegister(A3G4250D_t *gyro, uint8_t address, uint8_t data) {
141 SPI_t *spi = gyro->spi;
142 GPIOpin_t cs = gyro->cs;
143
144 cs.reset(&cs);
145
146 // Send read command and address
147 uint8_t payload = address & 0x7F; // Load payload with address and read command
148 spi->transmit(spi, payload); // Transmit payload
149 spi->transmit(spi, data); // Transmit dummy data and read response data
150
151 cs.set(&cs);
152}
153
154uint8_t A3G4250D_readRegister(A3G4250D_t *gyro, uint8_t address) {
155 uint8_t response = 0;
156 SPI_t *spi = gyro->spi;
157 GPIOpin_t cs = gyro->cs;
158
159 cs.reset(&cs);
160
161 // Send read command and address
162 uint8_t payload = address | 0x80; // Load payload with address and read command
163 response = spi->transmit(spi, payload); // Transmit payload
164 response = spi->transmit(spi, 0xFF); // Transmit dummy data and read response data
165
166 cs.set(&cs);
167
168 return response;
169}
170
void A3G4250D_readRawBytes(Gyro_t *, uint8_t *)
Read raw 3-axis data.
Definition a3g4250d.c:125
void A3G4250D_update(Gyro_t *)
Updates internally stored gyro readings.
Definition a3g4250d.c:94
A3G4250D_t A3G4250D_init(A3G4250D_t *gyro, SPI_t *spi, GPIOpin_t cs, float sensitivity, const uint8_t *axes, const int8_t *sign)
Initialiser for a A3G4250D gyroscope.
Definition a3g4250d.c:30
void A3G4250D_readGyro(Gyro_t *, float *)
Read 3-axis floating point gyro rates.
Definition a3g4250d.c:80
void A3G4250D_processRawBytes(Gyro_t *, uint8_t *, float *)
Process raw 3-axis data to floating point gyro rates.
Definition a3g4250d.c:109
uint8_t * axes
Array defining axes of mounting.
Definition gyroscope.h:64
int8_t * sign
Array defining sign of axes.
Definition gyroscope.h:65
void(* readRawBytes)(struct Gyro *, uint8_t *)
Pointer to readRawBytes method.
Definition gyroscope.h:48
float * gyroData
Processed angular rates array.
Definition gyroscope.h:67
uint8_t * rawGyroData
Raw angular rates array.
Definition gyroscope.h:66
void(* update)(struct Gyro *gyro)
Pointer to update method.
Definition gyroscope.h:24
float * bias
Bias offset array.
Definition gyroscope.h:68
uint8_t dataSize
Total data size.
Definition gyroscope.h:63
void(* processRawBytes)(struct Gyro *, uint8_t *, float *)
Pointer to processRawBytes method.
Definition gyroscope.h:61
void(* readGyro)(struct Gyro *gyro, float *out)
Pointer to readGyro method.
Definition gyroscope.h:36
void(* set)(struct GPIOpin *)
Definition gpiopin.h:155
void(* reset)(struct GPIOpin *)
Definition gpiopin.h:156
Struct definition for a GPIO pin.
Definition gpiopin.h:151
uint16_t(* transmit)(struct SPI *, uint16_t)
SPI transmit method.
Definition spi.h:139
Struct definition for SPI interface. Provides the interface for API consumers to interact with the SP...
Definition spi.h:134
uint8_t rawGyroData[A3G4250D_DATA_TOTAL]
Raw rotation array.
Definition a3g4250d.h:47
GPIOpin_t cs
Chip select GPIO.
Definition a3g4250d.h:43
float bias[A3G4250D_DATA_COUNT]
Bias offset array.
Definition a3g4250d.h:49
int8_t sign[A3G4250D_DATA_COUNT]
Array defining sign of axes.
Definition a3g4250d.h:46
uint8_t axes[A3G4250D_DATA_COUNT]
Array defining axes of mounting.
Definition a3g4250d.h:45
Gyro_t base
Base gyroscope API.
Definition a3g4250d.h:41
SPI_t * spi
Parent SPI interface.
Definition a3g4250d.h:42
float gyroData[A3G4250D_DATA_COUNT]
Processed rotation array.
Definition a3g4250d.h:48