Skip to content

Commit de6f11c

Browse files
committed
pbio/drv_adc: Drop on-complete callback hook.
Most process that use the ADC are slow and don't need to get called back every other millisecond now that we increased the sample frequency. They can poll it at their own reduced independent frequency, as we do in most device detection loops.
1 parent 3a198b8 commit de6f11c

File tree

5 files changed

+26
-35
lines changed

5 files changed

+26
-35
lines changed

lib/pbio/drv/adc/adc_ev3.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,6 @@ static const uint32_t channel_cmd[PBDRV_CONFIG_ADC_EV3_ADC_NUM_CHANNELS + PBDRV_
9494
};
9595
static volatile uint16_t channel_data[PBDRV_CONFIG_ADC_EV3_ADC_NUM_CHANNELS + PBDRV_ADC_EV3_NUM_DELAY_SAMPLES];
9696

97-
static pbdrv_adc_callback_t pbdrv_adc_callbacks[1];
98-
static uint32_t pbdrv_adc_callback_count = 0;
99-
100-
void pbdrv_adc_set_callback(pbdrv_adc_callback_t callback) {
101-
if (pbdrv_adc_callback_count < PBIO_ARRAY_SIZE(pbdrv_adc_callbacks)) {
102-
pbdrv_adc_callbacks[pbdrv_adc_callback_count++] = callback;
103-
}
104-
}
105-
10697
pbio_error_t pbdrv_adc_get_ch(uint8_t ch, uint16_t *value) {
10798
if (ch >= PBDRV_CONFIG_ADC_EV3_ADC_NUM_CHANNELS) {
10899
return PBIO_ERROR_INVALID_ARG;
@@ -156,10 +147,6 @@ static pbio_error_t pbdrv_adc_ev3_process_thread(pbio_os_state_t *state, void *c
156147
// Await for actual transfer to complete.
157148
PBIO_OS_AWAIT_WHILE(state, pbdrv_block_device_ev3_is_busy());
158149

159-
for (uint32_t i = 0; i < pbdrv_adc_callback_count; i++) {
160-
pbdrv_adc_callbacks[i]();
161-
}
162-
163150
pbio_os_timer_set(&timer, ADC_SAMPLE_PERIOD);
164151
PBIO_OS_AWAIT_UNTIL(state, pbdrv_adc_ev3_process.request || pbio_os_timer_is_expired(&timer));
165152
}

lib/pbio/drv/adc/adc_stm32_hal.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ static uint32_t pbdrv_adc_last_error;
4747

4848
PROCESS(pbdrv_adc_process, "ADC");
4949

50-
void pbdrv_adc_set_callback(pbdrv_adc_callback_t callback) {
51-
}
52-
5350
pbio_error_t pbdrv_adc_await_new_samples(pbio_os_state_t *state) {
5451
return PBIO_ERROR_NOT_SUPPORTED;
5552
}

lib/pbio/drv/adc/adc_stm32f0.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ void pbdrv_adc_init(void) {
6666
process_start(&pbdrv_adc_process);
6767
}
6868

69-
void pbdrv_adc_set_callback(pbdrv_adc_callback_t callback) {
70-
}
71-
7269
pbio_error_t pbdrv_adc_await_new_samples(pbio_os_state_t *state) {
7370
return PBIO_ERROR_NOT_SUPPORTED;
7471
}

lib/pbio/drv/counter/counter_ev3.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,11 @@ static lego_device_type_id_t pbdrv_counter_ev3_get_type(uint16_t adc) {
130130
return LEGO_DEVICE_TYPE_ID_NONE;
131131
}
132132

133+
#define PBDRV_COUNTER_EV3_TYPE_LOOP_TIME (10)
134+
#define PBDRV_COUNTER_EV3_TYPE_MIN_STABLE_COUNT (20)
135+
133136
/**
134137
* Updates the type of all EV3 motors based on the current ADC values.
135-
*
136-
* This is called periodically by the ADC process.
137138
*/
138139
static void pbdrv_counter_ev3_update_type(void) {
139140

@@ -155,12 +156,32 @@ static void pbdrv_counter_ev3_update_type(void) {
155156

156157
// Update stable type if we have seen enough identical detections,
157158
// including none detections.
158-
if (dev->type_id_count >= 20) {
159+
if (dev->type_id_count >= PBDRV_COUNTER_EV3_TYPE_MIN_STABLE_COUNT) {
159160
dev->stable_type_id = type_id;
160161
}
161162
}
162163
}
163164

165+
static pbio_os_process_t pbdrv_counter_device_detect_process;
166+
167+
/**
168+
* Background process to periodically read the ADC values to detect devices.
169+
*/
170+
static pbio_error_t pbdrv_counter_device_detect_process_thread(pbio_os_state_t *state, void *context) {
171+
172+
static pbio_os_timer_t timer;
173+
174+
PBIO_OS_ASYNC_BEGIN(state);
175+
176+
for (;;) {
177+
pbdrv_counter_ev3_update_type();
178+
PBIO_OS_AWAIT_MS(state, &timer, PBDRV_COUNTER_EV3_TYPE_LOOP_TIME);
179+
}
180+
181+
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
182+
}
183+
184+
164185
pbio_error_t pbdrv_counter_assert_type(pbdrv_counter_dev_t *dev, lego_device_type_id_t *expected_type_id) {
165186

166187
if (dev->stable_type_id == LEGO_DEVICE_TYPE_ID_NONE) {
@@ -256,7 +277,8 @@ void pbdrv_counter_init(void) {
256277
HWREG(baseAddr + GPIO_SET_RIS_TRIG(3)) = HWREG(baseAddr + GPIO_SET_RIS_TRIG(3)) | 0x00000200;
257278
HWREG(baseAddr + GPIO_SET_FAL_TRIG(3)) = HWREG(baseAddr + GPIO_SET_FAL_TRIG(3)) | 0x00000200;
258279

259-
pbdrv_adc_set_callback(pbdrv_counter_ev3_update_type);
280+
// Monitor attached counter devices
281+
pbio_os_process_start(&pbdrv_counter_device_detect_process, pbdrv_counter_device_detect_process_thread, NULL);
260282
}
261283

262284
#endif // PBDRV_CONFIG_COUNTER_EV3

lib/pbio/include/pbdrv/adc.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,6 @@ typedef void (*pbdrv_adc_callback_t)(void);
2929
*/
3030
pbio_error_t pbdrv_adc_get_ch(uint8_t ch, uint16_t *value);
3131

32-
/**
33-
* Sets a callback to be called when the ADC has new data.
34-
*
35-
* NB: Not implemented on all platforms.
36-
*
37-
* @param [in] callback The callback function.
38-
*/
39-
void pbdrv_adc_set_callback(pbdrv_adc_callback_t callback);
40-
4132
/**
4233
* Awaits for ADC to have new samples ready to be read.
4334
*
@@ -54,9 +45,6 @@ static inline pbio_error_t pbdrv_adc_get_ch(uint8_t ch, uint16_t *value) {
5445
return PBIO_ERROR_NOT_SUPPORTED;
5546
}
5647

57-
static inline void pbdrv_adc_set_callback(pbdrv_adc_callback_t callback) {
58-
}
59-
6048
static inline pbio_error_t pbdrv_adc_await_new_samples(pbio_os_state_t *state) {
6149
return PBIO_ERROR_NOT_SUPPORTED;
6250
}

0 commit comments

Comments
 (0)