Loading...
Searching...
No Matches
a3g4250d.c
1/***********************************************************************************
2 *
3 * @file A3G4250D.c *
4 * @author Matt Ricci *
5 * @addtogroup A3G4250D *
6 * *
7 * @todo Move private interface methods (read/write register) to static functions *
8 * with internal prototypes. *
9 * @{ *
10 ***********************************************************************************/
11
12#include "a3g4250d.h"
13
14#include "string.h"
15
16/* =============================================================================== */
29 A3G4250D_t *gyro,
30 SPI_t *spi,
31 GPIOpin_t cs,
32 float sensitivity,
33 const uint8_t *axes,
34 const int8_t *sign
35) {
36 gyro->base = spi;
37 gyro->cs = cs;
38 gyro->sensitivity = sensitivity;
39 gyro->update = A3G4250D_update;
43 memcpy(gyro->axes, axes, A3G4250D_DATA_COUNT);
44 memcpy(gyro->sign, sign, A3G4250D_DATA_COUNT);
45
46 // Wait for the spefified period - need to wait for 2ms here.
47 for (uint32_t i = 0; i < 0xFFFF; i++);
48
49 A3G4250D_writeRegister(gyro, A3G4250D_CTRL_REG1, A3G4250D_CTRL_REG1_ODR_800Hz | A3G4250D_CTRL_REG1_AXIS_ENABLE | A3G4250D_CTRL_REG1_PD_ENABLE);
50
51 return *gyro;
52}
53
54/******************************** DEVICE METHODS ********************************/
55
56/* =============================================================================== */
65void A3G4250D_readGyro(A3G4250D_t *gyro, float *out) {
66 uint8_t bytes[A3G4250D_DATA_TOTAL];
67 gyro->readRawBytes(gyro, bytes);
68 gyro->processRawBytes(gyro, bytes, out);
69}
70
71/* =============================================================================== */
80 gyro->readRawBytes(gyro, gyro->rawGyroData);
81 gyro->processRawBytes(gyro, gyro->rawGyroData, gyro->gyroData);
82}
83
84/* =============================================================================== */
94void A3G4250D_processRawBytes(A3G4250D_t *gyro, uint8_t *bytes, float *out) {
95 out[0] = gyro->sign[0] * gyro->sensitivity * (int16_t)(((uint16_t)bytes[0] << 8) | bytes[1]); // gyro X
96 out[1] = gyro->sign[1] * gyro->sensitivity * (int16_t)(((uint16_t)bytes[2] << 8) | bytes[3]); // gyro Y
97 out[2] = gyro->sign[2] * gyro->sensitivity * (int16_t)(((uint16_t)bytes[4] << 8) | bytes[5]); // gyro Z
98}
99
100/* =============================================================================== */
109void A3G4250D_readRawBytes(A3G4250D_t *gyro, uint8_t *out) {
110#define INDEX_AXES(index, byte) 2 * gyro->axes[index] + byte
111 out[INDEX_AXES(0, 0)] = A3G4250D_readRegister(gyro, A3G4250D_OUT_X_H); // gyro X high
112 out[INDEX_AXES(0, 1)] = A3G4250D_readRegister(gyro, A3G4250D_OUT_X_L); // gyro X low
113 out[INDEX_AXES(1, 0)] = A3G4250D_readRegister(gyro, A3G4250D_OUT_Y_H); // gyro Y high
114 out[INDEX_AXES(1, 1)] = A3G4250D_readRegister(gyro, A3G4250D_OUT_Y_L); // gyro Y low
115 out[INDEX_AXES(2, 0)] = A3G4250D_readRegister(gyro, A3G4250D_OUT_Z_H); // gyro Z high
116 out[INDEX_AXES(2, 1)] = A3G4250D_readRegister(gyro, A3G4250D_OUT_Z_L); // gyro Z low
117#undef INDEX_AXES
118}
119
120/******************************** INTERFACE METHODS ********************************/
121
122void A3G4250D_writeRegister(A3G4250D_t *gyro, uint8_t address, uint8_t data) {
123 SPI_t *spi = gyro->base;
124 GPIOpin_t cs = gyro->cs;
125
126 cs.reset(&cs);
127
128 // Send read command and address
129 uint8_t payload = address & 0x7F; // Load payload with address and read command
130 spi->transmit(spi, payload); // Transmit payload
131 spi->transmit(spi, data); // Transmit dummy data and read response data
132
133 cs.set(&cs);
134}
135
136uint8_t A3G4250D_readRegister(A3G4250D_t *gyro, uint8_t address) {
137 uint8_t response = 0;
138 SPI_t *spi = gyro->base;
139 GPIOpin_t cs = gyro->cs;
140
141 cs.reset(&cs);
142
143 // Send read command and address
144 uint8_t payload = address | 0x80; // Load payload with address and read command
145 response = spi->transmit(spi, payload); // Transmit payload
146 response = spi->transmit(spi, 0xFF); // Transmit dummy data and read response data
147
148 cs.set(&cs);
149
150 return response;
151}
152
void A3G4250D_processRawBytes(A3G4250D_t *, uint8_t *, float *)
Process raw 3-axis data to floating point gyro rates.
Definition a3g4250d.c:94
void A3G4250D_update(A3G4250D_t *)
Updates internally stored gyro readings.
Definition a3g4250d.c:79
void A3G4250D_readRawBytes(A3G4250D_t *, uint8_t *)
Read raw 3-axis data.
Definition a3g4250d.c:109
void A3G4250D_readGyro(A3G4250D_t *, float *)
Read 3-axis floating point gyro rates.
Definition a3g4250d.c:65
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:28
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 gyro rates array.
Definition a3g4250d.h:48
GPIOpin_t cs
Chip select GPIO.
Definition a3g4250d.h:40
void(* readRawBytes)(struct A3G4250D *, uint8_t *)
Raw gyro read method.
Definition a3g4250d.h:44
SPI_t * base
Parent SPI interface.
Definition a3g4250d.h:39
int8_t sign[A3G4250D_DATA_COUNT]
Array defining sign of axes.
Definition a3g4250d.h:47
uint8_t axes[A3G4250D_DATA_COUNT]
Array defining axes of mounting.
Definition a3g4250d.h:46
void(* readGyro)(struct A3G4250D *, float *)
Gyro read method.
Definition a3g4250d.h:43
float sensitivity
Gyroscope sensitivity.
Definition a3g4250d.h:41
void(* processRawBytes)(struct A3G4250D *, uint8_t *, float *)
Process raw gyro method.
Definition a3g4250d.h:45
void(* update)(struct A3G4250D *)
Gyro update method.
Definition a3g4250d.h:42
float gyroData[A3G4250D_DATA_COUNT]
Processed gyro rates array.
Definition a3g4250d.h:49