Use pendsv exception to interrupt running code

This commit is contained in:
iabdalkader 2014-02-23 03:37:46 +02:00
parent 427b03aaed
commit 7891a89723
5 changed files with 29 additions and 8 deletions

View File

@ -20,6 +20,7 @@
#include "py_sensor.h"
#include "py_imlib.h"
#include "py_file.h"
#include "usbdbg.h"
int errno;
@ -224,6 +225,9 @@ int main(void)
/* Init MicroPython */
libmp_init();
/* init USB debug */
usbdbg_init();
/* add some functions to the global python namespace */
rt_store_name(MP_QSTR_help, rt_make_function_n(0, py_help));
rt_store_name(MP_QSTR_delay, rt_make_function_n(1, py_delay));
@ -290,9 +294,5 @@ int main(void)
libmp_do_repl();
printf("PYB: sync filesystems\n");
py_sync();
printf("PYB: soft reboot\n");
while(1);
}

View File

@ -7,6 +7,12 @@
static int xfer_bytes;
static int xfer_length;
static enum usbdbg_cmd cmd;
mp_obj_t mp_const_ide_interrupt = MP_OBJ_NULL;
void usbdbg_init()
{
mp_const_ide_interrupt = mp_obj_new_exception_msg(&mp_type_OSError, "IDEInterrupt");
}
void usbdbg_data_in(void *buffer, int *length)
{
@ -39,7 +45,7 @@ void usbdbg_data_out(void *buffer, int length)
xfer_bytes += length;
if (xfer_bytes == xfer_length) {
/* interrupt do_repl */
libmp_line_feed();
libmp_int_repl();
}
break;
@ -56,6 +62,7 @@ void usbdbg_control(uint8_t request, int length)
xfer_bytes = 0;
xfer_length = 12;
break;
case USBDBG_DUMP_FB: /* dump framebuffer */
/* reset bytes counter */
xfer_bytes = 0;
@ -68,6 +75,14 @@ void usbdbg_control(uint8_t request, int length)
xfer_length =length;
break;
case USBDBG_STOP_SCRIPT:
if (libmp_code_running()) {
/* Stop any running code by raising an exception */
mp_obj_exception_clear_traceback(mp_const_ide_interrupt);
pendsv_nlr_jump(mp_const_ide_interrupt);
}
break;
default: /* error */
cmd = USBDBG_NONE;
break;

View File

@ -1,11 +1,13 @@
#ifndef __USBDBG_H__
#define __USBDBG_H__
enum usbdbg_cmd {
enum usbdbg_cmd {
USBDBG_NONE=0,
USBDBG_FB_SIZE,
USBDBG_DUMP_FB,
USBDBG_EXEC_SCRIPT,
USBDBG_READ_SCRIPT,
USBDBG_WRITE_SCRIPT,
USBDBG_STOP_SCRIPT,
};
void usbdbg_init();
#endif /* __USBDBG_H__ */

View File

@ -86,13 +86,13 @@ class OMVGtk:
def execute_clicked(self, widget):
buf = self.buffer.get_text(self.buffer.get_start_iter(), self.buffer.get_end_iter())
# interrupt any running code
self.terminal.feed_child("\x03")
openmv.stop_script()
sleep(0.1)
# exec script
openmv.exec_script(buf)
def stop_clicked(self, widget):
self.terminal.feed_child("\x03\r\n")
openmv.stop_script();
def motion_notify(self, widget, event):
x = int(event.x)

View File

@ -11,6 +11,7 @@ __USBDBG_DUMP_FB=2
__USBDBG_EXEC_SCRIPT=3
__USBDBG_READ_SCRIPT=4
__USBDBG_WRITE_SCRIPT=5
__USBDBG_STOP_SCRIPT=6
# Debug __INTERFACE
__INTERFACE = 2;
@ -80,6 +81,9 @@ def exec_script(buf):
__dev.ctrl_transfer(0x41, __USBDBG_EXEC_SCRIPT, len(buf), __INTERFACE, None, __TIMEOUT)
__dev.write(0x03, buf, __INTERFACE, __TIMEOUT)
def stop_script():
__dev.ctrl_transfer(0x41, __USBDBG_STOP_SCRIPT, 0, __INTERFACE, None, __TIMEOUT)
if __name__ == '__main__':
if len(sys.argv)!= 2:
print 'usage: openmv.py <script>'