Skip to content

Commit 0ddfbb0

Browse files
authored
Merge pull request adafruit#2055 from jepler/run-background-tasks
Implement and use RUN_BACKGROUND_TASKS in place of MICROPY_VM_HOOK_LOOP
2 parents 6b54e67 + fc7ae2a commit 0ddfbb0

22 files changed

Lines changed: 33 additions & 76 deletions

File tree

ports/atmel-samd/common-hal/audiobusio/PDMIn.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,7 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
408408
// If wait_counts exceeds the max count, buffer has probably stopped filling;
409409
// DMA may have missed an I2S trigger event.
410410
while (!event_interrupt_active(event_channel) && ++wait_counts < MAX_WAIT_COUNTS) {
411-
#ifdef MICROPY_VM_HOOK_LOOP
412-
MICROPY_VM_HOOK_LOOP
413-
#endif
411+
RUN_BACKGROUND_TASKS;
414412
}
415413

416414
// The mic is running all the time, so we don't need to wait the usual 10msec or 100msec

ports/atmel-samd/common-hal/audioio/AudioOut.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ static void ramp_value(uint16_t start, uint16_t end) {
6969
DAC->DATA.reg = value;
7070
DAC->DATABUF.reg = value;
7171
common_hal_mcu_delay_us(50);
72-
#ifdef MICROPY_VM_HOOK_LOOP
73-
MICROPY_VM_HOOK_LOOP
74-
#endif
72+
RUN_BACKGROUND_TASKS;
7573
}
7674
}
7775
#endif
@@ -94,9 +92,7 @@ static void ramp_value(uint16_t start, uint16_t end) {
9492
DAC->DATABUF[1].reg = value;
9593

9694
common_hal_mcu_delay_us(50);
97-
#ifdef MICROPY_VM_HOOK_LOOP
98-
MICROPY_VM_HOOK_LOOP
99-
#endif
95+
RUN_BACKGROUND_TASKS;
10096
}
10197
}
10298
#endif

ports/atmel-samd/common-hal/busio/UART.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,11 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
291291
// Reset the timeout on every character read.
292292
start_ticks = ticks_ms;
293293
}
294-
#ifdef MICROPY_VM_HOOK_LOOP
295-
MICROPY_VM_HOOK_LOOP ;
294+
RUN_BACKGROUND_TASKS;
296295
// Allow user to break out of a timeout with a KeyboardInterrupt.
297296
if (mp_hal_is_interrupted()) {
298297
break;
299298
}
300-
#endif
301299
// If we are zero timeout, make sure we don't loop again (in the event
302300
// we read in under 1ms)
303301
if (self->timeout_ms == 0) {
@@ -339,9 +337,7 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
339337
done = true;
340338
break;
341339
}
342-
#ifdef MICROPY_VM_HOOK_LOOP
343-
MICROPY_VM_HOOK_LOOP
344-
#endif
340+
RUN_BACKGROUND_TASKS;
345341
}
346342

347343
if (!done) {

ports/atmel-samd/common-hal/pulseio/PulseOut.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,7 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pu
198198
while(pulse_index < length) {
199199
// Do other things while we wait. The interrupts will handle sending the
200200
// signal.
201-
#ifdef MICROPY_VM_HOOK_LOOP
202-
MICROPY_VM_HOOK_LOOP
203-
#endif
201+
RUN_BACKGROUND_TASKS;
204202
}
205203

206204
tc->COUNT16.CTRLBSET.reg = TC_CTRLBSET_CMD_STOP;

ports/atmel-samd/common-hal/touchio/TouchIn.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
5151

5252
while (!adafruit_ptc_is_conversion_finished(PTC)) {
5353
// wait
54-
#ifdef MICROPY_VM_HOOK_LOOP
55-
MICROPY_VM_HOOK_LOOP
56-
#endif
54+
RUN_BACKGROUND_TASKS;
5755
}
5856

5957
return adafruit_ptc_get_conversion_result(PTC);

ports/atmel-samd/mphalport.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ void mp_hal_delay_ms(mp_uint_t delay) {
5353
uint64_t start_tick = ticks_ms;
5454
uint64_t duration = 0;
5555
while (duration < delay) {
56-
#ifdef MICROPY_VM_HOOK_LOOP
57-
MICROPY_VM_HOOK_LOOP
58-
#endif
56+
RUN_BACKGROUND_TASKS;
5957
// Check to see if we've been CTRL-Ced by autoreload or the user.
6058
if(MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)) ||
6159
MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) {

ports/nrf/bluetooth/ble_uart.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ void ble_uart_init(void) {
138138
m_cccd_enabled = false;
139139

140140
while (!m_cccd_enabled) {
141-
#ifdef MICROPY_VM_HOOK_LOOP
142-
MICROPY_VM_HOOK_LOOP
143-
#endif
141+
RUN_BACKGROUND_TASKS;
144142
}
145143
}
146144

@@ -150,9 +148,7 @@ bool ble_uart_connected(void) {
150148

151149
char ble_uart_rx_chr(void) {
152150
while (isBufferEmpty(&m_rx_ring_buffer)) {
153-
#ifdef MICROPY_VM_HOOK_LOOP
154-
MICROPY_VM_HOOK_LOOP
155-
#endif
151+
RUN_BACKGROUND_TASKS;
156152
}
157153

158154
uint8_t byte;

ports/nrf/common-hal/bleio/Central.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void common_hal_bleio_central_connect(bleio_central_obj_t *self, bleio_address_o
115115
}
116116

117117
while (self->waiting_to_connect) {
118-
MICROPY_VM_HOOK_LOOP;
118+
RUN_BACKGROUND_TASKS;
119119
}
120120

121121
if (self->conn_handle == BLE_CONN_HANDLE_INVALID) {

ports/nrf/common-hal/bleio/Characteristic.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ STATIC void characteristic_gatts_notify_indicate(uint16_t handle, uint16_t conn_
9494
// TX buffer is full
9595
// We could wait for an event indicating the write is complete, but just retrying is easier.
9696
if (err_code == NRF_ERROR_RESOURCES) {
97-
MICROPY_VM_HOOK_LOOP;
97+
RUN_BACKGROUND_TASKS;
9898
continue;
9999
}
100100

@@ -118,7 +118,7 @@ STATIC void characteristic_gattc_read(bleio_characteristic_obj_t *characteristic
118118
}
119119

120120
while (m_read_characteristic != NULL) {
121-
MICROPY_VM_HOOK_LOOP;
121+
RUN_BACKGROUND_TASKS;
122122
}
123123

124124
ble_drv_remove_event_handler(characteristic_on_gattc_read_rsp_evt, characteristic);
@@ -258,7 +258,7 @@ void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self,
258258
// Write without reponse will return NRF_ERROR_RESOURCES if too many writes are pending.
259259
if (err_code == NRF_ERROR_BUSY || err_code == NRF_ERROR_RESOURCES) {
260260
// We could wait for an event indicating the write is complete, but just retrying is easier.
261-
MICROPY_VM_HOOK_LOOP;
261+
RUN_BACKGROUND_TASKS;
262262
continue;
263263
}
264264

ports/nrf/common-hal/bleio/CharacteristicBuffer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ int common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_
9999

100100
// Wait for all bytes received or timeout
101101
while ( (ringbuf_count(&self->ringbuf) < len) && (ticks_ms - start_ticks < self->timeout_ms) ) {
102-
MICROPY_VM_HOOK_LOOP;
102+
RUN_BACKGROUND_TASKS;
103103
// Allow user to break out of a timeout with a KeyboardInterrupt.
104104
if ( mp_hal_is_interrupted() ) {
105105
return 0;

0 commit comments

Comments
 (0)