/
ADXL345.c
246 lines (184 loc) · 7.7 KB
/
ADXL345.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_exti.h"
#include "FreeRTOS.h"
#include "queue.h"
#include "task.h"
#include "stdio.h"
#include "string.h"
#include "mc_globals.h"
#include "number_format.h"
#include "I2C.h"
#include "ADXL345.h"
void ADXL345_exti0_init(){
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/* Enable GPIOB clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Configure PB7 pin as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Connect EXTI Line0 to */
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource7);
/* Configure EXTI Line0 */
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI Line0 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void ADXL345_init(I2C_TypeDef * I2Cx){
uint8_t readback_data;
ADXL345_write_register(I2Cx,ADXL345_REGISTER_PWRCTL,ADXL345_SLAVE_WRITE_ADDR,ADXL345_PWRCTL_MEASURE);
readback_data = ADXL345_read_ack(I2Cx,ADXL345_REGISTER_PWRCTL,ADXL345_SLAVE_WRITE_ADDR);
ADXL345_read_nack(I2Cx,ADXL345_REGISTER_PWRCTL,ADXL345_SLAVE_WRITE_ADDR);
}
uint8_t ADXL345_read_ack(I2C_TypeDef * I2Cx, uint8_t regaddr,uint8_t devread ){
I2C_start(I2Cx, devread, I2C_Direction_Transmitter);
I2C_SendData(I2Cx, regaddr); //send register address to be read from
while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BTF) == RESET)
I2C_stop(I2Cx);
I2C_start(I2Cx,(uint8_t)ADXL345_SLAVE_READ_ADDR , I2C_Direction_Receiver);
I2C_AcknowledgeConfig(I2Cx, ENABLE);
// wait until one byte has been received
while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
// read data from I2C data register and return data byte
uint8_t data = I2C_ReceiveData(I2Cx);
I2C_stop(I2Cx);
return data;
}
void ADXL345_read_burst(I2C_TypeDef * I2Cx, uint8_t devread, uint8_t * data_out, uint8_t nBytes ){
//write mode byte to the fifo control register
ADXL345_write_register(I2C1, ADXL345_FIFO_CONTROL_REG, devread, ADXL345_FIFO_MODE(ADXL345_FIFO_STREAM));
I2C_start(I2Cx, devread, I2C_Direction_Transmitter);
I2C_SendData(I2Cx, ADXL345_X_AXIS_DATA_REG0);
//send register address to be read from first (x0) the ADXL345 increments pointer
//to access follow up registers automatically
while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BTF) == RESET);
I2C_stop(I2Cx);
I2C_start(I2Cx,(uint8_t)ADXL345_SLAVE_READ_ADDR , I2C_Direction_Receiver);
I2C_AcknowledgeConfig(I2Cx, ENABLE);
for(int i = 0; i < nBytes-1; i++) {
// wait until one byte has been received
while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
// read data from I2C data register and return data byte
data_out[i] = I2C_ReceiveData(I2Cx);
}
data_out[nBytes -1] = ADXL345_read_nack(I2Cx, ADXL345_Z_AXIS_DATA_REG1, (uint8_t)ADXL345_SLAVE_READ_ADDR);
}
uint8_t ADXL345_read_nack(I2C_TypeDef* I2Cx, uint8_t regaddr, uint8_t devread){
// disable acknowledge of received data
I2C_AcknowledgeConfig(I2Cx, DISABLE);
(void)I2Cx->SR2;
// wait until one byte has been received
while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
// read data from I2C data register and return data byte
uint8_t data = I2C_ReceiveData(I2Cx);
I2C_stop(I2Cx);
return data;
}
void ADXL345_write_register(I2C_TypeDef* I2Cx, uint8_t regaddr, uint8_t devread, uint8_t data_in){
I2C_start(I2Cx, devread, I2C_Direction_Transmitter);
I2C_SendData(I2Cx, regaddr);
I2C_AcknowledgeConfig(I2Cx, ENABLE);
I2C_SendData(I2Cx, data_in);
while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED) );
I2C_stop(I2Cx);
I2C_AcknowledgeConfig(I2Cx,DISABLE);
I2C_stop(I2Cx);
}
void ADXL345_start_selftest(){
uint8_t bytes[12];
int accel_data_2comp[6];
ADXL345_write_register(I2C1,
ADXL345_DATA_FORMAT_REG,
ADXL345_SLAVE_WRITE_ADDR,
ADXL345_ENABLE_SELFTEST);
for(int i = 0; i < 20; i++){
memset(accel_data_2comp,0,6);
ADXL345_read_burst(I2C1, ADXL345_SLAVE_WRITE_ADDR, bytes, 6);
for(int j = 0; j < 3; j++) {
accel_data_2comp[j] = (int)bytes[2*j] + (((int)bytes[2*j + 1]) << 8);
debug_printf("2 comp val: %d\n", accel_data_2comp[j]);
}
}
ADXL345_write_register(I2C1,
ADXL345_DATA_FORMAT_REG,
ADXL345_SLAVE_WRITE_ADDR,
ADXL345_DISABLE_SELFTEST);
}
//void ADXL345_detect_freefall(I2C_TypeDef* I2Cx){
//
//
////ADXL345_exti0_init(); //initialize INT PIN7
//
//// set the treshold value for the freefall detection on 437,5 uG
//
//ADXL345_write_register(I2Cx,
// ADXL345_TRESH_FREE_FALL_REG,
// ADXL345_SLAVE_WRITE_ADDR,
// ADXL345_TEST_FREE_FALL_IMPACT_VAL );
//
////set the time that the device needs to be exposed to a negative g force to detect a freefall event
//
//ADXL345_write_register(I2Cx,
// ADXL345_TIME_FREE_FALL_REG,
// ADXL345_SLAVE_WRITE_ADDR,
// ADXL345_TEST_TIME_FREE_FALL);
//
//ADXL345_write_register(I2Cx,
// ADXL354_INT_ENABLE_REG,
// ADXL345_SLAVE_WRITE_ADDR,
// ADXL345_DTAP_FREEFALL_DETECT_MASK);
//ADXL345_exti0_init();
//}
void ADXL345_readoutTask(void *pvParameters){
uint8_t accel_data3d[6];
//uint8_t accel_trans_buffer[TRANS_BUFFER_LENGTH];
int accel_read_buffer[3];
int pvarx =0;
int pvary =0;
int pvarz =0;
//xQueueHandle *USARTQueueHandle;
portBASE_TYPE USARTQueueStatus;
// if(pvParameters != NULL){
// USARTQueueHandle = (xQueueHandle *) pvParameters ;
// }else{
// debug_printf("ERROR : ADXL345_readoutTask routine needs xQueueHandle as a parameter");
// }
for(;;){
ADXL345_read_burst(I2C1, ADXL345_SLAVE_WRITE_ADDR ,accel_data3d, 6 /*2 datasets*/ );
for (int i = 0;i < 3; ++i) {
accel_read_buffer[i] = (int)accel_data3d[2*i] + (((int)accel_data3d[2*i + 1]) << 8);
}
memset(sensor_trans_buffer,0,TRANS_BUFFER_LENGTH);
pvarx = correct_number_format(accel_read_buffer[0]);
pvary = correct_number_format(accel_read_buffer[1]);
pvarz = correct_number_format(accel_read_buffer[2]);
debug_printf("accel x: %d\n",pvarx);
debug_printf("accel y: %d\n",pvary);
debug_printf("accel z: %d\n",pvarz);
if(sensor_trans_buffer != NULL) {
snprintf(sensor_trans_buffer,TRANS_BUFFER_LENGTH,"a %d, %d, %d \n",pvarx, pvary, pvarz);
debug_printf("%s",sensor_trans_buffer);
}
USARTQueueStatus= xQueueSendToBack(USARTQueueHandle, sensor_trans_buffer,3);
if(USARTQueueStatus != pdPASS){
debug_printf("ADXL345 Error sending to the USART queue\n");
}
if(USARTQueueStatus == errQUEUE_FULL){
debug_printf("ADXL345 Error USART queue already full\n");
}
taskYIELD();
}
}