Skip to content

Commit dbbd525

Browse files
committed
Updating SparkX board source file for SDK 1.4.0
1 parent c4784f2 commit dbbd525

File tree

1 file changed

+101
-80
lines changed

1 file changed

+101
-80
lines changed

Software/sparkx-sdk-addon/source/acc_board_rpi_sparkx.c

Lines changed: 101 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
*/
6767
#define ACC_BOARD_CS 0
6868

69+
/**
70+
* @brief Number of GPIO pins
71+
*/
72+
#define GPIO_PIN_COUNT 28
73+
6974

7075
/**
7176
* @brief Sensor states
@@ -90,8 +95,15 @@ static const uint_fast8_t sensor_interrupt_pins[SENSOR_COUNT] = {
9095

9196
static acc_board_isr_t master_isr;
9297
static acc_device_handle_t spi_handle;
98+
static gpio_t gpios[GPIO_PIN_COUNT];
9399

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+
}
95107

96108

97109
/**
@@ -101,8 +113,10 @@ static void isr_sensor1(void) {if (master_isr) master_isr(1);}
101113
*/
102114
static bool acc_board_all_sensors_inactive(void)
103115
{
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+
{
106120
return false;
107121
}
108122
}
@@ -113,39 +127,40 @@ static bool acc_board_all_sensors_inactive(void)
113127

114128
acc_status_t acc_board_gpio_init(void)
115129
{
116-
acc_status_t status;
117130
static bool init_done = false;
118131
static acc_os_mutex_t init_mutex = NULL;
119132

120-
if (init_done) {
133+
if (init_done)
134+
{
121135
return ACC_STATUS_SUCCESS;
122136
}
123137

124138
acc_os_init();
125139
init_mutex = acc_os_mutex_create();
126140

127141
acc_os_mutex_lock(init_mutex);
128-
if (init_done) {
142+
143+
if (init_done)
144+
{
129145
acc_os_mutex_unlock(init_mutex);
130146
return ACC_STATUS_SUCCESS;
131147
}
132148

133149
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__);
139155
}
140156

141157
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+
{
147162
acc_os_mutex_unlock(init_mutex);
148-
return status;
163+
return ACC_STATUS_FAILURE;
149164
}
150165

151166
init_done = true;
@@ -160,7 +175,8 @@ acc_status_t acc_board_init(void)
160175
static bool init_done = false;
161176
static acc_os_mutex_t init_mutex = NULL;
162177

163-
if (init_done) {
178+
if (init_done)
179+
{
164180
return ACC_STATUS_SUCCESS;
165181
}
166182

@@ -169,26 +185,30 @@ acc_status_t acc_board_init(void)
169185
init_mutex = acc_os_mutex_create();
170186

171187
acc_os_mutex_lock(init_mutex);
172-
if (init_done) {
188+
189+
if (init_done)
190+
{
173191
acc_os_mutex_unlock(init_mutex);
174192
return ACC_STATUS_SUCCESS;
175193
}
176194

177-
acc_driver_gpio_linux_sysfs_register(28);
195+
acc_driver_gpio_linux_sysfs_register(GPIO_PIN_COUNT, gpios);
178196
acc_driver_spi_linux_spidev_register();
179197

180198
acc_device_gpio_init();
181199

182200
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;
188207

189208
spi_handle = acc_device_spi_create(&configuration);
190209

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+
{
192212
sensor_state[sensor_index] = SENSOR_STATE_UNKNOWN;
193213
}
194214

@@ -208,18 +228,16 @@ acc_status_t acc_board_init(void)
208228
*/
209229
static acc_status_t acc_board_reset_sensor(void)
210230
{
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+
{
215233
ACC_LOG_ERROR("Unable to activate RSTn");
216-
return status;
234+
return ACC_STATUS_FAILURE;
217235
}
218236

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+
{
221239
ACC_LOG_ERROR("Unable to deactivate ENABLE");
222-
return status;
240+
return ACC_STATUS_FAILURE;
223241
}
224242

225243
return ACC_STATUS_SUCCESS;
@@ -228,42 +246,43 @@ static acc_status_t acc_board_reset_sensor(void)
228246

229247
acc_status_t acc_board_start_sensor(acc_sensor_t sensor)
230248
{
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+
{
234251
ACC_LOG_ERROR("Sensor %u already active.", sensor);
235252
return ACC_STATUS_FAILURE;
236253
}
237254

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+
{
241259
ACC_LOG_ERROR("Unable to activate RSTn");
242260
acc_board_reset_sensor();
243-
return status;
261+
return ACC_STATUS_FAILURE;
244262
}
245263

246264
// Wait for PMU to stabilize
247265
acc_os_sleep_us(5000);
248266

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+
{
251269
ACC_LOG_ERROR("Unable to activate ENABLE");
252270
acc_board_reset_sensor();
253-
return status;
271+
return ACC_STATUS_FAILURE;
254272
}
255273

256274
// Wait for Power On Reset
257275
acc_os_sleep_us(5000);
258276

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+
{
261279
ACC_LOG_ERROR("Unable to deactivate RSTn");
262280
acc_board_reset_sensor();
263-
return status;
281+
return ACC_STATUS_FAILURE;
264282
}
265283

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+
{
267286
sensor_state[sensor_index] = SENSOR_STATE_READY;
268287
}
269288
}
@@ -281,14 +300,16 @@ acc_status_t acc_board_start_sensor(acc_sensor_t sensor)
281300

282301
acc_status_t acc_board_stop_sensor(acc_sensor_t sensor)
283302
{
284-
if (sensor_state[sensor - 1] != SENSOR_STATE_BUSY) {
303+
if (sensor_state[sensor - 1] != SENSOR_STATE_BUSY)
304+
{
285305
ACC_LOG_ERROR("Sensor %u already inactive.", sensor);
286306
return ACC_STATUS_FAILURE;
287307
}
288308

289309
sensor_state[sensor - 1] = SENSOR_STATE_UNKNOWN;
290310

291-
if (acc_board_all_sensors_inactive()) {
311+
if (acc_board_all_sensors_inactive())
312+
{
292313
return acc_board_reset_sensor();
293314
}
294315

@@ -321,12 +342,11 @@ bool acc_board_is_sensor_interrupt_connected(acc_sensor_t sensor)
321342

322343
bool acc_board_is_sensor_interrupt_active(acc_sensor_t sensor)
323344
{
324-
acc_status_t status;
325345
uint_fast8_t value;
326346

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);
330350
return false;
331351
}
332352

@@ -336,22 +356,20 @@ bool acc_board_is_sensor_interrupt_active(acc_sensor_t sensor)
336356

337357
acc_status_t acc_board_register_isr(acc_board_isr_t isr)
338358
{
339-
acc_status_t status;
340-
341359
if (isr != NULL)
342360
{
343361
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;
347365
}
348366
}
349367
else
350368
{
351369
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;
355373
}
356374
}
357375

@@ -377,28 +395,31 @@ acc_status_t acc_board_set_ref_freq(float ref_freq)
377395

378396
acc_status_t acc_board_sensor_transfer(acc_sensor_t sensor_id, uint8_t *buffer, size_t buffer_length)
379397
{
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);
390400

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);
395402

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+
}
400409

410+
if ((status = acc_device_spi_transfer(spi_handle, buffer, buffer_length)))
411+
{
401412
acc_device_spi_unlock(bus);
413+
return status;
414+
}
402415

416+
if ((status = acc_board_chip_select(sensor_id, 0)))
417+
{
418+
acc_device_spi_unlock(bus);
403419
return status;
420+
}
421+
422+
acc_device_spi_unlock(bus);
423+
424+
return status;
404425
}

0 commit comments

Comments
 (0)