ARMEBS4  revision-26.06.2015
accelerometer.c
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////
2 /// \file accelerometer.c
3 /// \brief ARMEBS4 accelerometer access functions
4 /// \author Pascal Sartoretti (sap at hevs dot ch)
5 //////////////////////////////////////////////////////////////////////////////////
6 #include "heivs/accelerometer.h"
7 #include "heivs/stm32_i2c.h"
8 
9 // address of ACCELEROMETER MMA7660FC mounted on ARMEBS4
10 #define ADDR_MMA7660 0x4C
11 
12 #define ACC_XOUT 0x00 // 6-bit output value X - Alert XOUT[5] XOUT[4] XOUT[3] XOUT[2] XOUT[1] XOUT[0]
13 #define ACC_YOUT 0x01 // 6-bit output value Y - Alert YOUT[5] YOUT[4] YOUT[3] YOUT[2] YOUT[1] YOUT[0]
14 #define ACC_ZOUT 0x02 // 6-bit output value Z - Alert ZOUT[5] ZOUT[4] ZOUT[3] ZOUT[2] ZOUT[1] ZOUT[0]
15 #define ACC_TILT 0x03 // Tilt Status Shake Alert Tap PoLa[2] PoLa[1] PoLa[0] BaFro[1] BaFro[0]
16 #define ACC_SRST 0x04 // Sampling Rate Status 0 0 0 0 0 0 AWSRS AMSRS
17 #define ACC_SPCNT 0x05 // Sleep Count SC[7] SC[6] SC[5] SC[4] SC[3] SC[2] SC[1] SC[0]
18 #define ACC_INTSU 0x06 // Interrupt Setup SHINTX SHINTY SHINTZ GINT ASINT PDINT PLINT FBINT
19 #define ACC_MODE 0x07 // Mode IAH IPP SCPS ASE AWE TON - MODE
20 #define ACC_SR 0x08 // Auto-Wake/Sleep and Portrait/Landscape samples per seconds and Debounce Filter
21  // FILT[2] FILT[1] FILT[0] AWSR[1] AWSR[0] AMSR[2] AMSR[1] AMSR[0]
22 #define ACC_PDET 0x09 // Tap Detection ZDA YDA XDA PDTH[4] PDTH[3] PDTH[2] PDTH[1] PDTH[0]
23 #define ACC_PD 0x0A // Tap Debounce Count PD[7] PD[6] PD[5] PD[4] PD[3] PD[2] PD[1] PD[0]
24 
25 #define SIGN_BIT 0x20 // sign bit in data register
26 #define MASK_ERROR 0x40 // mask status read bit in data register
27 
28 #define STDBY_MODE 0x00 // stanby mode for configuration
29 #define ACTIVE_MODE 0x01 // active mode for measure
30 static const struct heivs_bus_t *bus = &bus_i2c[0];
31 
33 {
34  status_e status;
35 
36  status = bus_init(bus);
37  if(status != NO_ERROR)
38  return status;
39  status = Accel_WriteCfg(ACC_MODE,STDBY_MODE);
40  if(status != NO_ERROR)
41  return status;
42  status = Accel_WriteCfg(ACC_SR,0);
43  if(status != NO_ERROR)
44  return status;
45  status = Accel_WriteCfg(ACC_MODE,ACTIVE_MODE);
46  if(status != NO_ERROR)
47  return status;
48  return NO_ERROR;
49 }
50 
52 {
53  status_e status;
54  uint8_t regAddress = ACC_XOUT;
55  uint8_t readData[3];
56  uint8_t i;
57 
58  status = bus_get(bus);
59  if (status != NO_ERROR) return status;
60 
61  while (1)
62  {
63  status = bus_writeread(bus,ADDR_MMA7660,&regAddress,1,readData,3,NULL);
64  if(status != NO_ERROR) return status;
65 
66  // The measure is good when there is no alert bit set
67  if (!(readData[0] & MASK_ERROR) && !(readData[1] & MASK_ERROR) && !(readData[2] & MASK_ERROR))
68  {
69  break;
70  }
71  }
72 
73  status = bus_release(bus);
74  if (status != NO_ERROR) return status;
75 
76  for (i = 0 ; i < ARRAY_SIZE(readData); i++)
77  {
78  if (readData[i] & SIGN_BIT)
79  {
80  readData[i] |= 0xc0;
81  }
82  }
83  accelData->x = readData[0];
84  accelData->y = readData[1];
85  accelData->z = readData[2];
86 
87  return NO_ERROR;
88 }
89 
90 status_e Accel_WriteCfg(uint8_t reg, uint8_t data )
91 {
92  status_e status;
93  uint8_t dta[2];
94 
95  dta[0] = reg;
96  dta[1] = data;
97  status = bus_get(bus);
98  if(status != NO_ERROR) return status;
99  status = bus_write(bus,ADDR_MMA7660,&dta,2);
100  if(status != NO_ERROR) return status;
101  status = bus_release(bus);
102  if(status != NO_ERROR) return status;
103 
104  return NO_ERROR;
105 }
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
int8_t y
y axis acceleration value (31 to -32)
Definition: accelerometer.h:35
status_e Accel_WriteCfg(uint8_t reg, uint8_t data)
Write special config to accelrometer.
Definition: accelerometer.c:90
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
int8_t x
x axis acceleration value (31 to -32)
Definition: accelerometer.h:34
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
ARMEBS4 accelerometer access functions.
Accelerometer data.
Definition: accelerometer.h:32
status_e bus_get(const struct heivs_bus_t *bus)
Get exclusive access to the bus.
Definition: bus.c:134
status_e Accel_Init(void)
Initialize the accelerometer.
Definition: accelerometer.c:32
status_e
Known errors.
Definition: error.h:21
#define ARRAY_SIZE(x)
Number of elements in the array.
Definition: utils.h:19
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
int8_t z
z axis acceleration value (31 to -32)
Definition: accelerometer.h:36
No error.
Definition: error.h:28
i2c bus
status_e Accel_Read(struct accelerometer_data_t *accelData)
Read data from accelerometer.
Definition: accelerometer.c:51