ARMEBS4  revision-26.06.2015
gyroscope.c
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////
2 /// \file heivs/gyroscope.c
3 /// \brief ARMEBS4 gyroscope access functions
4 /// \author Pascal Sartoretti (sap at hevs dot ch)
5 //////////////////////////////////////////////////////////////////////////////////
6 #include "heivs/gyroscope.h"
7 #include "heivs/stm32_i2c.h"
8 
9 // address of gyroscope L3GD20 mounted on ARMEBS4
10 #define I2C_DEV_ADDR 0x6A
11 
12 #define GYR_WHO_AM_I 0x0F // who am i, answer 0xD4
13 #define GYR_CTRL_REG1 0x20 // axis enable, power down, bandwidth
14 #define GYR_CTRL_REG2 0x21 // highpass filters
15 #define GYR_CTRL_REG3 0x22 // interrupts control
16 #define GYR_CTRL_REG4 0x23 // scale and data selection
17 #define GYR_CTRL_REG5 0x24 // miscanellous
18 #define GYR_REFERENCE 0x25 // reference register
19 #define GYR_OUT_TEMP 0x26 // temperature measure
20 #define GYR_STATUS_REG 0x27 // data status
21 #define GYR_OUT_X_L 0x28 // X value axis LSB
22 #define GYR_OUT_X_H 0x29 // X value axis MSB
23 #define GYR_OUT_Y_L 0x2A // Y value axis LSB
24 #define GYR_OUT_Y_H 0x2B // Y value axis MSB
25 #define GYR_OUT_Z_L 0x2C // Z value axis LSB
26 #define GYR_OUT_Z_H 0x2D // Z value axis MSB
27 #define GYR_FIFO_CTRL_REG 0x2E // fifo control register
28 #define GYR_FIFO_SRC_REG 0x2F // fifo status register
29 #define GYR_INT1_CFG 0x30 // interrupt control register
30 #define GYR_INT1_SRC 0x31 // interrupt status register
31 #define GYR_INT_TSH_XH 0x32 // X value treshold interrupt MSB
32 #define GYR_INT_TSH_XL 0x33 // X value treshold interrupt LSB
33 #define GYR_INT_TSH_YH 0x34 // Y value treshold interrupt MSB
34 #define GYR_INT_TSH_YL 0x35 // Y value treshold interrupt LSB
35 #define GYR_INT_TSH_ZH 0x36 // Z value treshold interrupt MSB
36 #define GYR_INT_TSH_ZL 0x37 // Z value treshold interrupt LSB
37 #define GYR_INT1_DURATION 0x38 // duration before interrupt
38 
39 #define GYR_WHO_AM_I_DATA 0xD4 // signature of gyroscope
40 
41 static const struct heivs_bus_t *bus = &bus_i2c[0];
42 
43 //////////////////////////////////////////////////////////////////////////////////
44 /// \brief Read a data byte from gyroscope
45 /// \param address register address
46 /// \param data Pointer to data
47 /// \\return NO_ERROR for no problem
48 //////////////////////////////////////////////////////////////////////////////////
49 static status_e Gyro_RD(uint8_t address, uint8_t * data)
50 {
51  status_e status;
52  uint8_t regAddress;
53 
54  regAddress = address;
55  status = bus_get(bus);
56  if(status != NO_ERROR) return status;
57  status = bus_writeread(bus,I2C_DEV_ADDR,&regAddress,1,data,1,NULL);
58  if(status != NO_ERROR) return status;
59  status = bus_release(bus);
60  if(status != NO_ERROR) return status;
61 
62  return NO_ERROR;
63 }
64 
66 {
67  uint8_t data;
68  status_e status;
69 
70  status = bus_init(bus);
71  if(status != NO_ERROR)
72  return status;
73 
74  status = Gyro_RD(GYR_WHO_AM_I,&data);
75  if(status != NO_ERROR)
76  return status;
77  if(data != GYR_WHO_AM_I_DATA)
78  {
79  return ERROR_HW_FAILED; // bad gyro signature
80  }
81  status = Gyro_Write(GYR_CTRL_REG1, 0x0F);
82  if(status != NO_ERROR)
83  return status;
84 
85  return NO_ERROR;
86 }
87 
89 {
90  status_e status;
91  uint8_t regAddress;
92  uint8_t data[6];
93 
94  regAddress = GYR_OUT_X_L | (1 << 7); // multiple bytes read
95  status = bus_get(bus);
96  if(status != NO_ERROR) return status;
97  status = bus_writeread(bus,I2C_DEV_ADDR,&regAddress,1,data,6,NULL);
98  if(status != NO_ERROR) return status;
99  status = bus_release(bus);
100  if(status != NO_ERROR) return status;
101 
102  /**
103  * To match the accelerometer axis as reference
104  * the x and y channels must be swapped and y inverted.
105  */
106  measure->y = -(data[0] | (data[1] << 8));
107  measure->x = +(data[2] | (data[3] << 8));
108  measure->z = +(data[4] | (data[5] << 8));
109 
110  return NO_ERROR;
111 }
112 
113 status_e Gyro_Write(uint8_t reg, uint8_t data )
114 {
115  status_e status;
116  uint8_t dta[2];
117 
118  dta[0] = reg;
119  dta[1] = data;
120 
121  status = bus_get(bus);
122  if(status != NO_ERROR) return status;
123  status = bus_write(bus,I2C_DEV_ADDR,&dta,2);
124  if(status != NO_ERROR) return status;
125  status = bus_release(bus);
126  if(status != NO_ERROR) return status;
127 
128  return NO_ERROR;
129 }
Bus handler.
Definition: bus.h:92
status_e bus_release(const struct heivs_bus_t *bus)
Release exclusive access to the bus.
Definition: bus.c:149
int16_t z
z axis gyroscope value (32767 to -32768)
Definition: gyroscope.h:33
static status_e Gyro_RD(uint8_t address, uint8_t *data)
Read a data byte from gyroscope.
Definition: gyroscope.c:49
status_e Gyro_Write(uint8_t reg, uint8_t data)
Write special config to accelrometer.
Definition: gyroscope.c:113
Gyroscope data.
Definition: gyroscope.h:29
int16_t y
y axis gyroscope value (32767 to -32768)
Definition: gyroscope.h:32
status_e bus_write(const struct heivs_bus_t *bus, uint32_t address, const void *data, size_t len)
Write data to the bus.
Definition: bus.c:243
status_e bus_init(const struct heivs_bus_t *bus)
initialize the bus
Definition: bus.c:59
const struct heivs_bus_t bus_i2c[BSP_I2C_BUS_COUNT]
All i2c busses.
Definition: stm32_i2c.c:424
You should check your solder/cables.
Definition: error.h:66
status_e Gyro_Init(void)
Initialise the gyroscope Accelerometer in initialised for fast acquisition.
Definition: gyroscope.c:65
status_e bus_get(const struct heivs_bus_t *bus)
Get exclusive access to the bus.
Definition: bus.c:134
ARMEBS4 eeprom access functions.
status_e
Known errors.
Definition: error.h:21
status_e Gyro_Read(struct gyroscope_data_t *measure)
Read the X, Y and Z values from gyroscope.
Definition: gyroscope.c:88
status_e bus_writeread(const struct heivs_bus_t *bus, uint32_t address, const void *src, size_t src_len, void *dst, size_t dst_len, size_t *rlen)
Combined write and read data.
Definition: bus.c:279
No error.
Definition: error.h:28
int16_t x
x axis gyroscope value (32767 to -32768)
Definition: gyroscope.h:31
i2c bus