66
66
*/
67
67
#define ACC_BOARD_CS 0
68
68
69
+ /**
70
+ * @brief Number of GPIO pins
71
+ */
72
+ #define GPIO_PIN_COUNT 28
73
+
69
74
70
75
/**
71
76
* @brief Sensor states
@@ -90,8 +95,15 @@ static const uint_fast8_t sensor_interrupt_pins[SENSOR_COUNT] = {
90
95
91
96
static acc_board_isr_t master_isr ;
92
97
static acc_device_handle_t spi_handle ;
98
+ static gpio_t gpios [GPIO_PIN_COUNT ];
93
99
94
- static void isr_sensor1 (void ) {if (master_isr ) master_isr (1 );}
100
+ static void isr_sensor1 (void )
101
+ {
102
+ if (master_isr )
103
+ {
104
+ master_isr (1 );
105
+ }
106
+ }
95
107
96
108
97
109
/**
@@ -101,8 +113,10 @@ static void isr_sensor1(void) {if (master_isr) master_isr(1);}
101
113
*/
102
114
static bool acc_board_all_sensors_inactive (void )
103
115
{
104
- for (uint_fast8_t sensor_index = 0 ; sensor_index < SENSOR_COUNT ; sensor_index ++ ) {
105
- if (sensor_state [sensor_index ] == SENSOR_STATE_BUSY ) {
116
+ for (uint_fast8_t sensor_index = 0 ; sensor_index < SENSOR_COUNT ; sensor_index ++ )
117
+ {
118
+ if (sensor_state [sensor_index ] == SENSOR_STATE_BUSY )
119
+ {
106
120
return false;
107
121
}
108
122
}
@@ -113,39 +127,40 @@ static bool acc_board_all_sensors_inactive(void)
113
127
114
128
acc_status_t acc_board_gpio_init (void )
115
129
{
116
- acc_status_t status ;
117
130
static bool init_done = false;
118
131
static acc_os_mutex_t init_mutex = NULL ;
119
132
120
- if (init_done ) {
133
+ if (init_done )
134
+ {
121
135
return ACC_STATUS_SUCCESS ;
122
136
}
123
137
124
138
acc_os_init ();
125
139
init_mutex = acc_os_mutex_create ();
126
140
127
141
acc_os_mutex_lock (init_mutex );
128
- if (init_done ) {
142
+
143
+ if (init_done )
144
+ {
129
145
acc_os_mutex_unlock (init_mutex );
130
146
return ACC_STATUS_SUCCESS ;
131
147
}
132
148
133
149
if (
134
- ( status = acc_device_gpio_set_initial_pull (GPIO0_PIN , 0 ) ) ||
135
- ( status = acc_device_gpio_set_initial_pull (RSTn_PIN , 1 ) ) ||
136
- ( status = acc_device_gpio_set_initial_pull (ENABLE_PIN , 0 ))
137
- ) {
138
- ACC_LOG_WARNING ("%s: failed to set initial pull with status: %s " , __func__ , acc_log_status_name ( status ) );
150
+ ! acc_device_gpio_set_initial_pull (GPIO0_PIN , 0 ) ||
151
+ ! acc_device_gpio_set_initial_pull (RSTn_PIN , 1 ) ||
152
+ ! acc_device_gpio_set_initial_pull (ENABLE_PIN , 0 ))
153
+ {
154
+ ACC_LOG_WARNING ("%s: failed to set initial pull" , __func__ );
139
155
}
140
156
141
157
if (
142
- (status = acc_device_gpio_input (GPIO0_PIN )) ||
143
- (status = acc_device_gpio_write (RSTn_PIN , 0 )) ||
144
- (status = acc_device_gpio_write (ENABLE_PIN , 0 ))
145
- ) {
146
- ACC_LOG_ERROR ("%s failed with %s" , __func__ , acc_log_status_name (status ));
158
+ !acc_device_gpio_input (GPIO0_PIN ) ||
159
+ !acc_device_gpio_write (RSTn_PIN , 0 ) ||
160
+ !acc_device_gpio_write (ENABLE_PIN , 0 ))
161
+ {
147
162
acc_os_mutex_unlock (init_mutex );
148
- return status ;
163
+ return ACC_STATUS_FAILURE ;
149
164
}
150
165
151
166
init_done = true;
@@ -160,7 +175,8 @@ acc_status_t acc_board_init(void)
160
175
static bool init_done = false;
161
176
static acc_os_mutex_t init_mutex = NULL ;
162
177
163
- if (init_done ) {
178
+ if (init_done )
179
+ {
164
180
return ACC_STATUS_SUCCESS ;
165
181
}
166
182
@@ -169,26 +185,30 @@ acc_status_t acc_board_init(void)
169
185
init_mutex = acc_os_mutex_create ();
170
186
171
187
acc_os_mutex_lock (init_mutex );
172
- if (init_done ) {
188
+
189
+ if (init_done )
190
+ {
173
191
acc_os_mutex_unlock (init_mutex );
174
192
return ACC_STATUS_SUCCESS ;
175
193
}
176
194
177
- acc_driver_gpio_linux_sysfs_register (28 );
195
+ acc_driver_gpio_linux_sysfs_register (GPIO_PIN_COUNT , gpios );
178
196
acc_driver_spi_linux_spidev_register ();
179
197
180
198
acc_device_gpio_init ();
181
199
182
200
acc_device_spi_configuration_t configuration ;
183
- configuration .bus = ACC_BOARD_BUS ;
184
- configuration .configuration = NULL ;
185
- configuration .device = ACC_BOARD_CS ;
186
- configuration .master = true;
187
- configuration .speed = ACC_BOARD_SPI_SPEED ;
201
+
202
+ configuration .bus = ACC_BOARD_BUS ;
203
+ configuration .configuration = NULL ;
204
+ configuration .device = ACC_BOARD_CS ;
205
+ configuration .master = true;
206
+ configuration .speed = ACC_BOARD_SPI_SPEED ;
188
207
189
208
spi_handle = acc_device_spi_create (& configuration );
190
209
191
- for (uint_fast8_t sensor_index = 0 ; sensor_index < SENSOR_COUNT ; sensor_index ++ ) {
210
+ for (uint_fast8_t sensor_index = 0 ; sensor_index < SENSOR_COUNT ; sensor_index ++ )
211
+ {
192
212
sensor_state [sensor_index ] = SENSOR_STATE_UNKNOWN ;
193
213
}
194
214
@@ -208,18 +228,16 @@ acc_status_t acc_board_init(void)
208
228
*/
209
229
static acc_status_t acc_board_reset_sensor (void )
210
230
{
211
- acc_status_t status ;
212
-
213
- status = acc_device_gpio_write (RSTn_PIN , 0 );
214
- if (status != ACC_STATUS_SUCCESS ) {
231
+ if (!acc_device_gpio_write (RSTn_PIN , 0 ))
232
+ {
215
233
ACC_LOG_ERROR ("Unable to activate RSTn" );
216
- return status ;
234
+ return ACC_STATUS_FAILURE ;
217
235
}
218
236
219
- status = acc_device_gpio_write (ENABLE_PIN , 0 );
220
- if ( status != ACC_STATUS_SUCCESS ) {
237
+ if (! acc_device_gpio_write (ENABLE_PIN , 0 ))
238
+ {
221
239
ACC_LOG_ERROR ("Unable to deactivate ENABLE" );
222
- return status ;
240
+ return ACC_STATUS_FAILURE ;
223
241
}
224
242
225
243
return ACC_STATUS_SUCCESS ;
@@ -228,42 +246,43 @@ static acc_status_t acc_board_reset_sensor(void)
228
246
229
247
acc_status_t acc_board_start_sensor (acc_sensor_t sensor )
230
248
{
231
- acc_status_t status ;
232
-
233
- if (sensor_state [sensor - 1 ] == SENSOR_STATE_BUSY ) {
249
+ if (sensor_state [sensor - 1 ] == SENSOR_STATE_BUSY )
250
+ {
234
251
ACC_LOG_ERROR ("Sensor %u already active." , sensor );
235
252
return ACC_STATUS_FAILURE ;
236
253
}
237
254
238
- if (acc_board_all_sensors_inactive ()) {
239
- status = acc_device_gpio_write (RSTn_PIN , 0 );
240
- if (status != ACC_STATUS_SUCCESS ) {
255
+ if (acc_board_all_sensors_inactive ())
256
+ {
257
+ if (!acc_device_gpio_write (RSTn_PIN , 0 ))
258
+ {
241
259
ACC_LOG_ERROR ("Unable to activate RSTn" );
242
260
acc_board_reset_sensor ();
243
- return status ;
261
+ return ACC_STATUS_FAILURE ;
244
262
}
245
263
246
264
// Wait for PMU to stabilize
247
265
acc_os_sleep_us (5000 );
248
266
249
- status = acc_device_gpio_write (ENABLE_PIN , 1 );
250
- if ( status != ACC_STATUS_SUCCESS ) {
267
+ if (! acc_device_gpio_write (ENABLE_PIN , 1 ))
268
+ {
251
269
ACC_LOG_ERROR ("Unable to activate ENABLE" );
252
270
acc_board_reset_sensor ();
253
- return status ;
271
+ return ACC_STATUS_FAILURE ;
254
272
}
255
273
256
274
// Wait for Power On Reset
257
275
acc_os_sleep_us (5000 );
258
276
259
- status = acc_device_gpio_write (RSTn_PIN , 1 );
260
- if ( status != ACC_STATUS_SUCCESS ) {
277
+ if (! acc_device_gpio_write (RSTn_PIN , 1 ))
278
+ {
261
279
ACC_LOG_ERROR ("Unable to deactivate RSTn" );
262
280
acc_board_reset_sensor ();
263
- return status ;
281
+ return ACC_STATUS_FAILURE ;
264
282
}
265
283
266
- for (uint_fast8_t sensor_index = 0 ; sensor_index < SENSOR_COUNT ; sensor_index ++ ) {
284
+ for (uint_fast8_t sensor_index = 0 ; sensor_index < SENSOR_COUNT ; sensor_index ++ )
285
+ {
267
286
sensor_state [sensor_index ] = SENSOR_STATE_READY ;
268
287
}
269
288
}
@@ -281,14 +300,16 @@ acc_status_t acc_board_start_sensor(acc_sensor_t sensor)
281
300
282
301
acc_status_t acc_board_stop_sensor (acc_sensor_t sensor )
283
302
{
284
- if (sensor_state [sensor - 1 ] != SENSOR_STATE_BUSY ) {
303
+ if (sensor_state [sensor - 1 ] != SENSOR_STATE_BUSY )
304
+ {
285
305
ACC_LOG_ERROR ("Sensor %u already inactive." , sensor );
286
306
return ACC_STATUS_FAILURE ;
287
307
}
288
308
289
309
sensor_state [sensor - 1 ] = SENSOR_STATE_UNKNOWN ;
290
310
291
- if (acc_board_all_sensors_inactive ()) {
311
+ if (acc_board_all_sensors_inactive ())
312
+ {
292
313
return acc_board_reset_sensor ();
293
314
}
294
315
@@ -321,12 +342,11 @@ bool acc_board_is_sensor_interrupt_connected(acc_sensor_t sensor)
321
342
322
343
bool acc_board_is_sensor_interrupt_active (acc_sensor_t sensor )
323
344
{
324
- acc_status_t status ;
325
345
uint_fast8_t value ;
326
346
327
- status = acc_device_gpio_read (sensor_interrupt_pins [sensor - 1 ], & value );
328
- if ( status != ACC_STATUS_SUCCESS ) {
329
- ACC_LOG_ERROR ("Could not obtain GPIO interrupt value for sensor %" PRIsensor " with status: %s. " , sensor , acc_log_status_name ( status ) );
347
+ if (! acc_device_gpio_read (sensor_interrupt_pins [sensor - 1 ], & value ))
348
+ {
349
+ ACC_LOG_ERROR ("Could not obtain GPIO interrupt value for sensor %" PRIsensor "" , sensor );
330
350
return false;
331
351
}
332
352
@@ -336,22 +356,20 @@ bool acc_board_is_sensor_interrupt_active(acc_sensor_t sensor)
336
356
337
357
acc_status_t acc_board_register_isr (acc_board_isr_t isr )
338
358
{
339
- acc_status_t status ;
340
-
341
359
if (isr != NULL )
342
360
{
343
361
if (
344
- ( status = acc_device_gpio_register_isr (GPIO0_PIN , ACC_DEVICE_GPIO_EDGE_RISING , & isr_sensor1 ))
345
- ) {
346
- return status ;
362
+ ! acc_device_gpio_register_isr (GPIO0_PIN , ACC_DEVICE_GPIO_EDGE_RISING , & isr_sensor1 ))
363
+ {
364
+ return ACC_STATUS_FAILURE ;
347
365
}
348
366
}
349
367
else
350
368
{
351
369
if (
352
- ( status = acc_device_gpio_register_isr (GPIO0_PIN , ACC_DEVICE_GPIO_EDGE_NONE , NULL ))
353
- ) {
354
- return status ;
370
+ ! acc_device_gpio_register_isr (GPIO0_PIN , ACC_DEVICE_GPIO_EDGE_NONE , NULL ))
371
+ {
372
+ return ACC_STATUS_FAILURE ;
355
373
}
356
374
}
357
375
@@ -377,28 +395,31 @@ acc_status_t acc_board_set_ref_freq(float ref_freq)
377
395
378
396
acc_status_t acc_board_sensor_transfer (acc_sensor_t sensor_id , uint8_t * buffer , size_t buffer_length )
379
397
{
380
- acc_status_t status ;
381
- uint_fast8_t bus = acc_device_spi_get_bus (spi_handle );
382
-
383
- acc_device_spi_lock (bus );
384
-
385
- if ((status = acc_board_chip_select (sensor_id , 1 ))) {
386
- ACC_LOG_ERROR ("%s failed with %s" , __func__ , acc_log_status_name (status ));
387
- acc_device_spi_unlock (bus );
388
- return status ;
389
- }
398
+ acc_status_t status ;
399
+ uint_fast8_t bus = acc_device_spi_get_bus (spi_handle );
390
400
391
- if ((status = acc_device_spi_transfer (spi_handle , buffer , buffer_length ))) {
392
- acc_device_spi_unlock (bus );
393
- return status ;
394
- }
401
+ acc_device_spi_lock (bus );
395
402
396
- if ((status = acc_board_chip_select (sensor_id , 0 ))) {
397
- acc_device_spi_unlock (bus );
398
- return status ;
399
- }
403
+ if ((status = acc_board_chip_select (sensor_id , 1 )))
404
+ {
405
+ ACC_LOG_ERROR ("%s failed with %s" , __func__ , acc_log_status_name (status ));
406
+ acc_device_spi_unlock (bus );
407
+ return status ;
408
+ }
400
409
410
+ if ((status = acc_device_spi_transfer (spi_handle , buffer , buffer_length )))
411
+ {
401
412
acc_device_spi_unlock (bus );
413
+ return status ;
414
+ }
402
415
416
+ if ((status = acc_board_chip_select (sensor_id , 0 )))
417
+ {
418
+ acc_device_spi_unlock (bus );
403
419
return status ;
420
+ }
421
+
422
+ acc_device_spi_unlock (bus );
423
+
424
+ return status ;
404
425
}
0 commit comments