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#include "devices.h"
14
15/* =============================================================================== */
28 A3G4250D *gyro,
29 char name[DEVICE_NAME_LENGTH],
30 GPIO_TypeDef *port,
31 unsigned long cs,
32 float sensitivity,
33 const uint8_t *axes,
34 const int8_t *sign
35) {
36 SPI_init(&gyro->base, SENSOR_GYRO, SPI1, MODE8, port, cs);
37 gyro->sensitivity = sensitivity;
38 gyro->update = A3G4250D_update;
42 memcpy(gyro->axes, axes, A3G4250D_DATA_COUNT);
43 memcpy(gyro->sign, sign, A3G4250D_DATA_COUNT);
44
45 const uint32_t superDelay = 0xFFFF;
46 volatile uint8_t counter = 0;
47
48 // Wait for the spefified period - need to wait for 2ms here.
49 for (uint32_t i = 0; i < superDelay; i++) {
50 counter++;
51 }
52
53 A3G4250D_writeRegister(gyro, A3G4250D_CTRL_REG1, A3G4250D_CTRL_REG1_ODR_800Hz | A3G4250D_CTRL_REG1_AXIS_ENABLE | A3G4250D_CTRL_REG1_PD_ENABLE);
54
55 static DeviceHandle_t handle;
56 strcpy(handle.name, name);
57 handle.device = gyro;
58 return handle;
59}
60
61/******************************** DEVICE METHODS ********************************/
62
63/* =============================================================================== */
72void A3G4250D_readGyro(A3G4250D *gyro, float *out) {
73 uint8_t bytes[A3G4250D_DATA_TOTAL];
74 gyro->readRawBytes(gyro, bytes);
75 gyro->processRawBytes(gyro, bytes, out);
76}
77
78/* =============================================================================== */
87 gyro->readRawBytes(gyro, gyro->rawGyroData);
88 gyro->processRawBytes(gyro, gyro->rawGyroData, gyro->gyroData);
89}
90
91/* =============================================================================== */
101void A3G4250D_processRawBytes(A3G4250D *gyro, uint8_t *bytes, float *out) {
102 out[0] = gyro->sign[0] * gyro->sensitivity * (int16_t)(((uint16_t)bytes[0] << 8) | bytes[1]); // gyro X
103 out[1] = gyro->sign[1] * gyro->sensitivity * (int16_t)(((uint16_t)bytes[2] << 8) | bytes[3]); // gyro Y
104 out[2] = gyro->sign[2] * gyro->sensitivity * (int16_t)(((uint16_t)bytes[4] << 8) | bytes[5]); // gyro Z
105}
106
107/* =============================================================================== */
116void A3G4250D_readRawBytes(A3G4250D *gyro, uint8_t *out) {
117#define INDEX_AXES(index, byte) 2 * gyro->axes[index] + byte
118 out[INDEX_AXES(0, 0)] = A3G4250D_readRegister(gyro, A3G4250D_OUT_X_H); // gyro X high
119 out[INDEX_AXES(0, 1)] = A3G4250D_readRegister(gyro, A3G4250D_OUT_X_L); // gyro X low
120 out[INDEX_AXES(1, 0)] = A3G4250D_readRegister(gyro, A3G4250D_OUT_Y_H); // gyro Y high
121 out[INDEX_AXES(1, 1)] = A3G4250D_readRegister(gyro, A3G4250D_OUT_Y_L); // gyro Y low
122 out[INDEX_AXES(2, 0)] = A3G4250D_readRegister(gyro, A3G4250D_OUT_Z_H); // gyro Z high
123 out[INDEX_AXES(2, 1)] = A3G4250D_readRegister(gyro, A3G4250D_OUT_Z_L); // gyro Z low
124#undef INDEX_AXES
125}
126
127/******************************** INTERFACE METHODS ********************************/
128
129void A3G4250D_writeRegister(A3G4250D *gyro, uint8_t address, uint8_t data) {
130 SPI spi = gyro->base;
131
132 spi.port->ODR &= ~spi.cs;
133
134 // Send read command and address
135 uint8_t payload = address & 0x7F; // Load payload with address and read command
136 spi.transmit(&spi, payload); // Transmit payload
137 spi.transmit(&spi, data); // Transmit dummy data and read response data
138
139 spi.port->ODR |= spi.cs;
140}
141
142uint8_t A3G4250D_readRegister(A3G4250D *gyro, uint8_t address) {
143 uint8_t response = 0;
144 SPI spi = gyro->base;
145
146 spi.port->ODR &= ~spi.cs;
147
148 // Send read command and address
149 uint8_t payload = address | 0x80; // Load payload with address and read command
150 response = spi.transmit(&spi, payload); // Transmit payload
151 response = spi.transmit(&spi, 0xFF); // Transmit dummy data and read response data
152
153 spi.port->ODR |= spi.cs;
154
155 return response;
156}
157
void A3G4250D_processRawBytes(A3G4250D *, uint8_t *, float *)
Process raw 3-axis data to floating point gyro rates.
Definition a3g4250d.c:101
void A3G4250D_readGyro(A3G4250D *, float *)
Read 3-axis floating point gyro rates.
Definition a3g4250d.c:72
void A3G4250D_readRawBytes(A3G4250D *, uint8_t *)
Read raw 3-axis data.
Definition a3g4250d.c:116
DeviceHandle_t A3G4250D_init(A3G4250D *gyro, char name[DEVICE_NAME_LENGTH], GPIO_TypeDef *port, unsigned long cs, float sensitivity, const uint8_t *axes, const int8_t *sign)
Initialiser for a A3G4250D gyroscope.
Definition a3g4250d.c:27
void A3G4250D_update(A3G4250D *)
Updates internally stored gyro readings.
Definition a3g4250d.c:86
void SPI_init(SPI *, DeviceType, SPI_TypeDef *, DataFormat, GPIO_TypeDef *, unsigned long)
Initialiser for an SPI device interface.
Definition spi.c:32
@ SENSOR_GYRO
Gyroscope.
Definition spi.h:30
uint8_t rawGyroData[A3G4250D_DATA_TOTAL]
Raw gyro rates array.
Definition a3g4250d.h:49
SPI base
Parent SPI interface.
Definition a3g4250d.h:41
void(* readRawBytes)(struct A3G4250D *, uint8_t *)
Raw gyro read method.
Definition a3g4250d.h:45
int8_t sign[A3G4250D_DATA_COUNT]
Array defining sign of axes.
Definition a3g4250d.h:48
uint8_t axes[A3G4250D_DATA_COUNT]
Array defining axes of mounting.
Definition a3g4250d.h:47
void(* readGyro)(struct A3G4250D *, float *)
Gyro read method.
Definition a3g4250d.h:44
float sensitivity
Gyroscope sensitivity.
Definition a3g4250d.h:42
void(* processRawBytes)(struct A3G4250D *, uint8_t *, float *)
Process raw gyro method.
Definition a3g4250d.h:46
void(* update)(struct A3G4250D *)
Gyro update method.
Definition a3g4250d.h:43
float gyroData[A3G4250D_DATA_COUNT]
Processed gyro rates array.
Definition a3g4250d.h:50
Struct definition for SPI interface. Provides the interface for API consumers to interact with the SP...
Definition spi.h:49
uint16_t(* transmit)(struct SPI *, uint16_t)
SPI transmit method.
Definition spi.h:56
GPIO_TypeDef * port
Pointer to GPIO port struct.
Definition spi.h:52
unsigned long cs
Device chip select address.
Definition spi.h:53