Skip to content

Commit

Permalink
support interrupting main.py and run script as a whole
Browse files Browse the repository at this point in the history
support interrupting main.py and run script as a whole
wrapper event poll hook code from the MICROPY_EVENT_POLL_HOOK macro to a function EventPollHook
try run usb script in EventPollHook()
nlr_jump returns exception object (top->ret_val)
  • Loading branch information
RockySong committed Mar 8, 2020
1 parent d1dc85c commit e0b9198
Show file tree
Hide file tree
Showing 11 changed files with 73,768 additions and 73,714 deletions.
2 changes: 1 addition & 1 deletion extmod/moduselect.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ STATIC mp_obj_t select_select(size_t n_args, const mp_obj_t *args) {
mp_map_deinit(&poll_map);
return mp_obj_new_tuple(3, list_array);
}
MICROPY_EVENT_POLL_HOOK
do {EventPollHook();} while(0);
}
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_select_obj, 3, 4, select_select);
Expand Down
3 changes: 1 addition & 2 deletions ports/nxp_rt1050_60/hal_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
return HAL_OK;
}

__WEAK void usbdbg_try_run_script(void) {}
void HAL_WFI(void)
{
// #ifndef MCU_SERIES_RT106
// __WFI();
// #else
// #endif
usbdbg_try_run_script();

}

__weak void HAL_IncTick(void)
Expand Down
9 changes: 8 additions & 1 deletion ports/nxp_rt1050_60/main_omv.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ __WEAK int OverlaySetToDefault(void) {return 0;}

#define SCnSCB_ACTLR_DISDEFWBUF_Pos 1U /*!< ACTLR: DISDEFWBUF Position */
#define SCnSCB_ACTLR_DISDEFWBUF_Msk (1UL << SCnSCB_ACTLR_DISDEFWBUF_Pos) /*!< ACTLR: DISDEFWBUF Mask */
volatile uint8_t g_isMainDotPyRunning;
int OpenMV_Main(uint32_t first_soft_reset)
{
int ret = 0;
Expand Down Expand Up @@ -300,7 +301,6 @@ int OpenMV_Main(uint32_t first_soft_reset)

memset(&openmv_config, 0, sizeof(openmv_config));
#endif
MainLoop:
// Run boot script(s)
if (!usbdbg_script_ready()) {
if (first_soft_reset) {
Expand Down Expand Up @@ -334,7 +334,9 @@ int OpenMV_Main(uint32_t first_soft_reset)
if (stat == MP_IMPORT_STAT_FILE) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
g_isMainDotPyRunning = 1;
int ret = pyexec_file("main.py");
g_isMainDotPyRunning = 0;
if (ret & PYEXEC_FORCED_EXIT) {
ret = 1;
}
Expand All @@ -344,10 +346,15 @@ int OpenMV_Main(uint32_t first_soft_reset)
nlr_pop();
}
else {
g_isMainDotPyRunning = 0;
#if 0
// 2019.03.27 19:52 rocky: if main.py is interrupted by running another script,
// we have to do soft reset, otherwise fb alloc logic may fail and led to hard fault
// In this case, it makes user have to press start button twice to start the script in OpenMV IDE
goto cleanup;
#else
fb_free_all();
#endif
}
}
// exec_boot_script("/sd/main.py", false, true);
Expand Down
25 changes: 3 additions & 22 deletions ports/nxp_rt1050_60/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,28 +388,9 @@ static inline mp_uint_t disable_irq(void) {
#define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq()
#define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state)

#if MICROPY_PY_THREAD
#define MICROPY_EVENT_POLL_HOOK \
do { \
extern void mp_handle_pending(void); \
mp_handle_pending(); \
if (pyb_thread_enabled) { \
MP_THREAD_GIL_EXIT(); \
pyb_thread_yield(); \
MP_THREAD_GIL_ENTER(); \
} else { \
HAL_WFI(); \
} \
} while (0);
#else
extern void HAL_WFI(void);
#define MICROPY_EVENT_POLL_HOOK \
do { \
extern void mp_handle_pending(void); \
mp_handle_pending(); \
HAL_WFI(); \
} while (0);
#endif

extern void EventPollHook(void);
#define MICROPY_EVENT_POLL_HOOK do {EventPollHook();} while(0);

// There is no classical C heap in bare-metal ports, only Python
// garbage-collected heap. For completeness, emulate C heap via
Expand Down
14 changes: 14 additions & 0 deletions ports/nxp_rt1050_60/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ const byte mp_hal_status_to_errno_table[4] = {
NORETURN void mp_hal_raise(HAL_StatusTypeDef status) {
mp_raise_OSError(mp_hal_status_to_errno_table[status]);
}
__WEAK void usbdbg_try_run_script(void) {}
extern void mp_handle_pending(void);
void EventPollHook(void) {
mp_handle_pending();
usbdbg_try_run_script();
#if MICROPY_PY_THREAD
if (pyb_thread_enabled) {
MP_THREAD_GIL_EXIT();
pyb_thread_yield();
MP_THREAD_GIL_ENTER();
}
#endif
HAL_WFI();
}

int mp_hal_stdin_rx_chr(void) {
int c;
Expand Down
20 changes: 12 additions & 8 deletions ports/nxp_rt1050_60/omv/usbdbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ inline void usbdbg_set_irq_enabled(bool enabled)
#endif
volatile uint8_t g_omvIdeConnecting;
__WEAK int sensor_get_id(void) {return 1;}

__WEAK void Hook_OnUsbDbgScriptExec(void) {}
volatile uint8_t s_UsbDbgIsToRunScript;
void usbdbg_data_in(void *buffer, int length)
{
logout("usbdbg_data_in: cmd=%x, buffer=0x%08x, bytes=%d\r\n", cmd, buffer, length);
Expand Down Expand Up @@ -217,7 +218,9 @@ void usbdbg_data_in(void *buffer, int length)

case USBDBG_SCRIPT_RUNNING: {
uint32_t *buf = buffer;
buf[0] = (uint32_t) script_running;
// rocky: though may not run yet, set running flag in case openmv ide get not running
// flag before script get run.
buf[0] = (uint32_t) (s_UsbDbgIsToRunScript || script_running);
cmd = USBDBG_NONE;
break;
}
Expand All @@ -228,19 +231,14 @@ void usbdbg_data_in(void *buffer, int length)

extern int py_image_descriptor_from_roi(image_t *image, const char *path, rectangle_t *roi);
extern void ProfReset(void);
__WEAK void Hook_OnUsbDbgScriptExec(void) {}
volatile uint8_t s_UsbDbgIsToRunScript;

void usbdbg_try_run_script(void)
{
if (!s_UsbDbgIsToRunScript)
return;
// Disable IDE IRQ (re-enabled by pyexec or main).
usbdbg_set_irq_enabled(false);
s_UsbDbgIsToRunScript = 0;
// Set script ready flag
script_ready = true;
// Set script running flag
script_running = true;
// Clear interrupt traceback
mp_obj_exception_clear_traceback(mp_const_ide_interrupt);
// Interrupt running REPL
Expand All @@ -250,6 +248,7 @@ void usbdbg_try_run_script(void)
ProfReset();
pendsv_nlr_jump_hard(mp_const_ide_interrupt);
}
extern uint8_t g_isMainDotPyRunning;
void usbdbg_data_out(void *buffer, int length)
{
switch (cmd) {
Expand Down Expand Up @@ -278,6 +277,11 @@ void usbdbg_data_out(void *buffer, int length)
xfer_bytes += length;
if (xfer_bytes == xfer_length) {
s_UsbDbgIsToRunScript = 1;
script_ready = true;
script_running = true;
// in case main.py is running just after system reset, notify the VM to stop it.
if (g_isMainDotPyRunning)
pendsv_nlr_jump(mp_const_ide_interrupt);
}
}
else {
Expand Down
170 changes: 85 additions & 85 deletions ports/prj_keil_rt1060/mpyrt1060.uvguix.pearlpig

Large diffs are not rendered by default.

50 changes: 44 additions & 6 deletions ports/prj_keil_rt1060/mpyrt1060.uvoptx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
<SetRegEntry>
<Number>0</Number>
<Key>DLGUARM</Key>
<Name></Name>
<Name>d</Name>
</SetRegEntry>
<SetRegEntry>
<Number>0</Number>
Expand Down Expand Up @@ -148,7 +148,40 @@
<Name></Name>
</SetRegEntry>
</TargetDriverDllRegistry>
<Breakpoint/>
<Breakpoint>
<Bp>
<Number>0</Number>
<Type>0</Type>
<LineNumber>281</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>1611156028</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>1</BreakIfRCount>
<Filename>..\nxp_rt1050_60\omv\usbdbg.c</Filename>
<ExecCommand></ExecCommand>
<Expression>\\mpyrt_flexspi\../nxp_rt1050_60/omv/usbdbg.c\281</Expression>
</Bp>
<Bp>
<Number>1</Number>
<Type>0</Type>
<LineNumber>238</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>1611005944</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>1</BreakIfRCount>
<Filename>..\..\py\nlrthumb.c</Filename>
<ExecCommand></ExecCommand>
<Expression>\\mpyrt_flexspi\../../py/nlrthumb.c\238</Expression>
</Bp>
</Breakpoint>
<WatchWindow1>
<Ww>
<count>0</count>
Expand Down Expand Up @@ -205,12 +238,17 @@
<WinNumber>1</WinNumber>
<ItemText>s_jpegEncTicks,0x0A</ItemText>
</Ww>
<Ww>
<count>11</count>
<WinNumber>1</WinNumber>
<ItemText>mp_state_ctx</ItemText>
</Ww>
</WatchWindow1>
<MemoryWindow1>
<Mm>
<WinNumber>1</WinNumber>
<SubType>0</SubType>
<ItemText>0x60084094</ItemText>
<SubType>2</SubType>
<ItemText>R0</ItemText>
<AccSizeX>0</AccSizeX>
</Mm>
</MemoryWindow1>
Expand Down Expand Up @@ -1809,7 +1847,7 @@

<Group>
<GroupName>prebuilt</GroupName>
<tvExp>0</tvExp>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
Expand Down Expand Up @@ -8701,7 +8739,7 @@

<Group>
<GroupName>omv_img_mini</GroupName>
<tvExp>0</tvExp>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
Expand Down
Loading

0 comments on commit e0b9198

Please sign in to comment.