Skip to content

Commit b670a86

Browse files
committed
codal_port: Add mp_sched_schedule_exception helper and use it.
Signed-off-by: Damien George <[email protected]>
1 parent 9679263 commit b670a86

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

src/codal_port/drv_display.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static void draw_object(mp_obj_t obj) {
9393
async_stop();
9494
}
9595
} else {
96-
MP_STATE_VM(mp_pending_exception) = mp_obj_new_exception_msg(&mp_type_TypeError, "not an image");
96+
mp_sched_schedule_exception(mp_obj_new_exception_msg(&mp_type_TypeError, "not an image"));
9797
async_stop();
9898
}
9999
}
@@ -126,7 +126,7 @@ void microbit_display_update(void) {
126126
if (mp_obj_get_type(nlr.ret_val) == &mp_type_MemoryError) {
127127
mp_printf(&mp_plat_print, "Allocation in interrupt handler");
128128
}
129-
MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(nlr.ret_val);
129+
mp_sched_schedule_exception(MP_OBJ_FROM_PTR(nlr.ret_val));
130130
}
131131
obj = MP_OBJ_STOP_ITERATION;
132132
}

src/codal_port/modaudio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ STATIC void audio_data_fetcher(void) {
7777
} else {
7878
if (!mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t*)nlr.ret_val)->type),
7979
MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
80-
MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(nlr.ret_val);
80+
mp_sched_schedule_exception(MP_OBJ_FROM_PTR(nlr.ret_val));
8181
}
8282
buffer_obj = MP_OBJ_STOP_ITERATION;
8383
}
@@ -89,7 +89,7 @@ STATIC void audio_data_fetcher(void) {
8989
// Audio iterator did not return an AudioFrame
9090
audio_source_iter = NULL;
9191
microbit_audio_stop();
92-
MP_STATE_VM(mp_pending_exception) = mp_obj_new_exception_msg(&mp_type_TypeError, "not an AudioFrame");
92+
mp_sched_schedule_exception(mp_obj_new_exception_msg(&mp_type_TypeError, "not an AudioFrame"));
9393
} else {
9494
microbit_audio_frame_obj_t *buffer = (microbit_audio_frame_obj_t *)buffer_obj;
9595
uint8_t *dest = &audio_output_buffer[0];

src/codal_port/mphalport.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
#include "py/runtime.h"
2828
#include "py/mphal.h"
2929

30+
void mp_sched_schedule_exception(mp_obj_t exc) {
31+
MP_STATE_VM(mp_pending_exception) = exc;
32+
if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
33+
MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
34+
}
35+
}
36+
3037
void mp_hal_delay_us(mp_uint_t us) {
3138
if (us <= 0) {
3239
return;

src/codal_port/mphalport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/obj.h"
2728
#include "microbithal.h"
2829

2930
// Not implemented and not exposed in utime module.
3031
#define mp_hal_ticks_cpu() (0)
3132

33+
void mp_sched_schedule_exception(mp_obj_t exc);
3234
void mp_hal_set_interrupt_char(int c);
3335

3436
// MicroPython low-level C API for pins

0 commit comments

Comments
 (0)