Skip to content

Commit 6f0c8f2

Browse files
committed
pbio/busy_count: Introduce as module.
We were using macros to save a little bit of space, and duplicated them between pbdrv and pbsys. We need them in pbio init/deinit too. Instead of duplicating one more time, create a common module.
1 parent 6ee3094 commit 6f0c8f2

23 files changed

+108
-130
lines changed

bricks/_common/sources.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
193193
platform/$(PBIO_PLATFORM)/platform.c \
194194
src/angle.c \
195195
src/battery.c \
196+
src/busy_count.c \
196197
src/color/conversion.c \
197198
src/color/util.c \
198199
src/control_settings.c \

lib/pbio/drv/block_device/block_device_ev3.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <tiam1808/armv5/am1808/edma_event.h>
3030
#include <tiam1808/armv5/am1808/interrupt.h>
3131

32-
#include "../core.h"
3332
#include "../drv/gpio/gpio_ev3.h"
3433
#include "../sys/storage_data.h"
3534
#include "block_device_ev3.h"
@@ -38,6 +37,8 @@
3837
#include <pbdrv/clock.h>
3938
#include <pbdrv/compiler.h>
4039
#include <pbdrv/gpio.h>
40+
41+
#include <pbio/busy_count.h>
4142
#include <pbio/error.h>
4243
#include <pbio/int_math.h>
4344
#include <pbio/util.h>
@@ -886,7 +887,7 @@ pbio_error_t ev3_spi_process_thread(pbio_os_state_t *state, void *context) {
886887
// failure, it can reset the user data to factory defaults, and save it
887888
// properly on shutdown.
888889
pbdrv_block_device_load_err = err;
889-
pbdrv_init_busy_down();
890+
pbio_busy_count_down();
890891

891892
// Poll ADC continuously until cancellation is requested.
892893
while (!(ev3_spi_process.request & PBIO_OS_PROCESS_REQUEST_TYPE_CANCEL)) {
@@ -930,7 +931,7 @@ pbio_error_t pbdrv_block_device_write_all(pbio_os_state_t *state, uint32_t used_
930931

931932
void pbdrv_block_device_init(void) {
932933
spi_bus_init();
933-
pbdrv_init_busy_up();
934+
pbio_busy_count_up();
934935
pbio_os_process_start(&ev3_spi_process, ev3_spi_process_thread, NULL);
935936
}
936937

lib/pbio/drv/block_device/block_device_flash_stm32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
#include <stdint.h>
1111
#include <string.h>
1212

13-
#include "../core.h"
1413
#include "../sys/storage_data.h"
1514

1615
#include <pbdrv/block_device.h>
1716

17+
#include <pbio/busy_count.h>
1818
#include <pbio/error.h>
1919
#include <pbio/util.h>
2020

lib/pbio/drv/block_device/block_device_w25qxx_stm32.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
#include STM32_HAL_H
1515

16-
#include "../core.h"
1716
#include "../sys/storage_data.h"
1817
#include "block_device_w25qxx_stm32.h"
1918

2019
#include <pbdrv/block_device.h>
2120

21+
#include <pbio/busy_count.h>
2222
#include <pbio/error.h>
2323
#include <pbio/int_math.h>
2424

@@ -549,7 +549,7 @@ pbio_error_t pbdrv_block_device_w25qxx_stm32_init_process_thread(pbio_os_state_t
549549
// higher level code sees this error when requesting the RAM disk. On
550550
// failure, it can reset the user data to factory defaults, and save it
551551
// properly on shutdown.
552-
pbdrv_init_busy_down();
552+
pbio_busy_count_down();
553553

554554
PBIO_OS_ASYNC_END(err);
555555
}
@@ -606,7 +606,7 @@ void pbdrv_block_device_init(void) {
606606
HAL_NVIC_SetPriority(bdev.pdata->irq, 6, 2);
607607
HAL_NVIC_EnableIRQ(bdev.pdata->irq);
608608

609-
pbdrv_init_busy_up();
609+
pbio_busy_count_up();
610610
pbio_os_process_start(&pbdrv_block_device_w25qxx_stm32_init_process, pbdrv_block_device_w25qxx_stm32_init_process_thread, NULL);
611611
}
612612

lib/pbio/drv/button/button_resistor_ladder.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
#include <stddef.h>
1111

1212
#include <pbdrv/resistor_ladder.h>
13+
14+
#include <pbio/busy_count.h>
1315
#include <pbio/button.h>
1416
#include <pbio/error.h>
1517
#include <pbio/os.h>
1618

17-
#include "../core.h"
18-
1919
static pbio_os_process_t pbdrv_button_init_process;
2020

2121
/**
@@ -38,13 +38,13 @@ pbio_error_t pbdrv_button_init_process_thread(pbio_os_state_t *state, void *cont
3838
}
3939
PBIO_OS_AWAIT_MS(state, &timer, 30);
4040

41-
pbdrv_init_busy_down();
41+
pbio_busy_count_down();
4242

4343
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
4444
}
4545

4646
void pbdrv_button_init(void) {
47-
pbdrv_init_busy_up();
47+
pbio_busy_count_up();
4848
pbio_os_process_start(&pbdrv_button_init_process, pbdrv_button_init_process_thread, NULL);
4949
}
5050

lib/pbio/drv/charger/charger_mp2639a.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
#include <pbdrv/pwm.h>
2626
#include <pbdrv/resistor_ladder.h>
2727
#include <pbdrv/usb.h>
28+
29+
#include <pbio/busy_count.h>
2830
#include <pbio/error.h>
2931
#include <pbio/util.h>
3032
#include <pbio/os.h>
3133

32-
#include "../core.h"
3334
#include "charger_mp2639a.h"
3435

3536
#define platform pbdrv_charger_mp2639a_platform_data
@@ -204,7 +205,7 @@ pbio_error_t pbdrv_charger_mp2639a_process_thread(pbio_os_state_t *state, void *
204205
pbdrv_gpio_input(&platform.chg_gpio);
205206
#endif
206207

207-
pbdrv_init_busy_down();
208+
pbio_busy_count_down();
208209

209210
// When there is a fault the /CHG pin will toggle on and off at 1Hz, so we
210211
// have to try to detect that to get 3 possible states out of a digital input.
@@ -281,7 +282,7 @@ pbio_error_t pbdrv_charger_mp2639a_process_thread(pbio_os_state_t *state, void *
281282
}
282283

283284
void pbdrv_charger_init(void) {
284-
pbdrv_init_busy_up();
285+
pbio_busy_count_up();
285286
pbio_os_process_start(&pbdrv_charger_mp2639a_process, pbdrv_charger_mp2639a_process_thread, NULL);
286287
}
287288

lib/pbio/drv/core.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
#include <pbdrv/config.h>
77
#include <pbdrv/ioport.h>
8+
9+
#include <pbio/busy_count.h>
810
#include <pbio/os.h>
911

10-
#include "core.h"
1112
#include "adc/adc.h"
1213
#include "battery/battery.h"
1314
#include "block_device/block_device.h"
@@ -30,8 +31,6 @@
3031
#include "usb/usb.h"
3132
#include "watchdog/watchdog.h"
3233

33-
uint32_t pbdrv_init_busy_count;
34-
3534
/** Initializes all enabled drivers. */
3635
void pbdrv_init(void) {
3736

@@ -79,7 +78,7 @@ void pbdrv_init(void) {
7978

8079
// Wait for all async pbdrv drivers to initialize before starting
8180
// higher level system processes.
82-
while (pbdrv_init_busy()) {
81+
while (pbio_busy_count_busy()) {
8382
pbio_os_run_processes_once();
8483
}
8584

@@ -98,7 +97,7 @@ void pbdrv_deinit(void) {
9897

9998
pbdrv_imu_deinit();
10099

101-
while (pbdrv_init_busy()) {
100+
while (pbio_busy_count_busy()) {
102101
pbio_os_run_processes_once();
103102
}
104103

lib/pbio/drv/core.h

Lines changed: 0 additions & 38 deletions
This file was deleted.

lib/pbio/drv/display/display_ev3.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
#include <stdio.h>
1414
#include <string.h>
1515

16-
#include "../core.h"
17-
1816
#include <pbdrv/display.h>
1917
#include <pbdrv/gpio.h>
18+
19+
#include <pbio/busy_count.h>
2020
#include <pbio/error.h>
2121
#include <pbio/os.h>
2222
#include <pbio/util.h>
@@ -500,7 +500,7 @@ static pbio_error_t pbdrv_display_ev3_process_thread(pbio_os_state_t *state, voi
500500
pbdrv_gpio_out_high(&pin_lcd_cs);
501501

502502
// Done initializing.
503-
pbdrv_init_busy_down();
503+
pbio_busy_count_down();
504504

505505
// Update the display with the user frame buffer, if changed.
506506
for (;;) {
@@ -533,7 +533,7 @@ void pbdrv_display_init(void) {
533533
ST7586S_NUM_COL_TRIPLETS * 3);
534534

535535
// Start display process and ask pbdrv to wait until it is initialized.
536-
pbdrv_init_busy_up();
536+
pbio_busy_count_up();
537537
pbio_os_process_start(&pbdrv_display_ev3_process, pbdrv_display_ev3_process_thread, NULL);
538538
}
539539

lib/pbio/drv/imu/imu_lsm6ds3tr_c_stm32.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#include <pbdrv/clock.h>
1616
#include <pbdrv/imu.h>
1717

18-
#include <contiki.h>
18+
#include <pbio/busy_count.h>
19+
1920
#include <lsm6ds3tr_c_reg.h>
2021

2122
#include STM32_HAL_H
@@ -24,7 +25,6 @@
2425
#include <stm32l4xx_ll_i2c.h>
2526
#endif
2627

27-
#include "../core.h"
2828
#include "./imu_lsm6ds3tr_c_stm32.h"
2929

3030
struct _pbdrv_imu_dev_t {
@@ -335,7 +335,7 @@ static pbio_error_t pbdrv_imu_lsm6ds3tr_c_stm32_process_thread(pbio_os_state_t *
335335

336336
PBIO_OS_AWAIT(state, &sub, err = pbdrv_imu_lsm6ds3tr_c_stm32_init(&sub));
337337

338-
pbdrv_init_busy_down();
338+
pbio_busy_count_down();
339339

340340
if (err != PBIO_SUCCESS) {
341341
// The IMU is not essential. It just won't be available if init fails.
@@ -406,19 +406,19 @@ static pbio_error_t pbdrv_imu_lsm6ds3tr_c_stm32_process_thread(pbio_os_state_t *
406406

407407
// Cancellation complete.
408408
pbdrv_imu_lsm6ds3tr_c_stm32_i2c_reset(hi2c);
409-
pbdrv_init_busy_down();
409+
pbio_busy_count_down();
410410
PBIO_OS_ASYNC_END(PBIO_ERROR_CANCELED);
411411
}
412412

413413
// internal driver interface implementation
414414

415415
void pbdrv_imu_init(void) {
416-
pbdrv_init_busy_up();
416+
pbio_busy_count_up();
417417
pbio_os_process_start(&pbdrv_imu_lsm6ds3tr_c_stm32_process, pbdrv_imu_lsm6ds3tr_c_stm32_process_thread, NULL);
418418
}
419419

420420
void pbdrv_imu_deinit(void) {
421-
pbdrv_init_busy_up();
421+
pbio_busy_count_up();
422422
pbio_os_process_make_request(&pbdrv_imu_lsm6ds3tr_c_stm32_process, PBIO_OS_PROCESS_REQUEST_TYPE_CANCEL);
423423
}
424424

@@ -428,7 +428,7 @@ pbio_error_t pbdrv_imu_get_imu(pbdrv_imu_dev_t **imu_dev, pbdrv_imu_config_t **c
428428

429429
// When this is called from pbio, driver initialization must have finished
430430
// and the process should be up and running to process sensor data.
431-
if (pbdrv_init_busy() || pbdrv_imu_lsm6ds3tr_c_stm32_process.err != PBIO_ERROR_AGAIN) {
431+
if (pbio_busy_count_busy() || pbdrv_imu_lsm6ds3tr_c_stm32_process.err != PBIO_ERROR_AGAIN) {
432432
return PBIO_ERROR_FAILED;
433433
}
434434

0 commit comments

Comments
 (0)