From 41f6948545c8f5909413badbef2dcd5794598bf0 Mon Sep 17 00:00:00 2001 From: Daniel Campora Date: Sun, 13 Sep 2015 17:06:12 +0200 Subject: [PATCH] cc3200: New WDT API. --- cc3200/mods/pybwdt.c | 78 +++++++++++++++++++++------------------- cc3200/qstrdefsport.h | 3 +- docs/library/pyb.WDT.rst | 19 +++++----- tests/wipy/wdt.py | 37 +++++++++++++++++++ tests/wipy/wdt.py.exp | 7 ++++ 5 files changed, 96 insertions(+), 48 deletions(-) create mode 100644 tests/wipy/wdt.py create mode 100644 tests/wipy/wdt.py.exp diff --git a/cc3200/mods/pybwdt.c b/cc3200/mods/pybwdt.c index 3ea2719f5..b1f9086ce 100644 --- a/cc3200/mods/pybwdt.c +++ b/cc3200/mods/pybwdt.c @@ -88,48 +88,52 @@ void pybwdt_sl_alive (void) { /******************************************************************************/ // Micro Python bindings -/// \function constructor('msec') -/// Enables the watchdog timer with msec timeout value -STATIC mp_obj_t pyb_wdt_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { +STATIC const mp_arg_t pyb_wdt_init_args[] = { + { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} }, + { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 5000} }, // 5 s +}; +STATIC mp_obj_t pyb_wdt_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *all_args) { // check the arguments - mp_arg_check_num(n_args, n_kw, 0, 1, false); + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args); + mp_arg_val_t args[MP_ARRAY_SIZE(pyb_wdt_init_args)]; + mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_wdt_init_args, args); - if (n_args > 0) { - mp_int_t msec = mp_obj_get_int(args[0]); - if (msec < PYBWDT_MIN_TIMEOUT_MS) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); - } - if (pybwdt_data.running) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible)); - } - - // Enable the WDT peripheral clock - MAP_PRCMPeripheralClkEnable(PRCM_WDT, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK); - - // Unlock to be able to configure the registers - MAP_WatchdogUnlock(WDT_BASE); - - #ifdef DEBUG - // make the WDT stall when the debugger stops on a breakpoint - MAP_WatchdogStallEnable (WDT_BASE); - #endif - - // set the watchdog timer reload value - // the WDT trigger a system reset after the second timeout - // so, divide by 2 the timeout value received - MAP_WatchdogReloadSet(WDT_BASE, PYBWDT_MILLISECONDS_TO_TICKS(msec / 2)); - - // start the timer. Once it's started, it cannot be disabled. - MAP_WatchdogEnable(WDT_BASE); - pybwdt_data.running = true; + if (args[0].u_obj != mp_const_none && mp_obj_get_int(args[0].u_obj) > 0) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable)); } + uint timeout_ms = args[1].u_int; + if (timeout_ms < PYBWDT_MIN_TIMEOUT_MS) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + } + if (pybwdt_data.running) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible)); + } + + // Enable the WDT peripheral clock + MAP_PRCMPeripheralClkEnable(PRCM_WDT, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK); + + // Unlock to be able to configure the registers + MAP_WatchdogUnlock(WDT_BASE); + +#ifdef DEBUG + // make the WDT stall when the debugger stops on a breakpoint + MAP_WatchdogStallEnable (WDT_BASE); +#endif + + // set the watchdog timer reload value + // the WDT trigger a system reset after the second timeout + // so, divide by 2 the timeout value received + MAP_WatchdogReloadSet(WDT_BASE, PYBWDT_MILLISECONDS_TO_TICKS(timeout_ms / 2)); + + // start the timer. Once it's started, it cannot be disabled. + MAP_WatchdogEnable(WDT_BASE); + pybwdt_data.running = true; return (mp_obj_t)&pyb_wdt_obj; } -/// \function wdt.kick() -/// Kicks the watchdog timer -STATIC mp_obj_t pyb_wdt_kick(mp_obj_t self) { +STATIC mp_obj_t pyb_wdt_feed(mp_obj_t self) { if ((pybwdt_data.servers || pybwdt_data.servers_sleeping) && pybwdt_data.simplelink && pybwdt_data.running) { pybwdt_data.servers = false; pybwdt_data.simplelink = false; @@ -137,10 +141,10 @@ STATIC mp_obj_t pyb_wdt_kick(mp_obj_t self) { } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_wdt_kick_obj, pyb_wdt_kick); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_wdt_feed_obj, pyb_wdt_feed); STATIC const mp_map_elem_t pybwdt_locals_dict_table[] = { - { MP_OBJ_NEW_QSTR(MP_QSTR_kick), (mp_obj_t)&pyb_wdt_kick_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_feed), (mp_obj_t)&pyb_wdt_feed_obj }, }; STATIC MP_DEFINE_CONST_DICT(pybwdt_locals_dict, pybwdt_locals_dict_table); diff --git a/cc3200/qstrdefsport.h b/cc3200/qstrdefsport.h index 337ea9b06..485ff4ec5 100644 --- a/cc3200/qstrdefsport.h +++ b/cc3200/qstrdefsport.h @@ -299,7 +299,8 @@ Q(EXTERNAL) // for WDT class Q(WDT) -Q(kick) +Q(feed) +Q(timeout) // for HeartBeat class Q(HeartBeat) diff --git a/docs/library/pyb.WDT.rst b/docs/library/pyb.WDT.rst index 9728e7bca..867026b32 100644 --- a/docs/library/pyb.WDT.rst +++ b/docs/library/pyb.WDT.rst @@ -10,24 +10,23 @@ watchdog periodically to prevent it from expiring and resetting the system. Example usage:: - wdt = pyb.WDT(5000) # enable with a timeout of 5s - wdt.kick() + wdt = pyb.WDT(timeout=2000) # enable with a timeout of 2s + wdt.feed() Constructors ------------ -.. class:: pyb.WDT([timeout]) +.. class:: pyb.WDT(id=0, timeout=5000) - Create a WDT object. If the timeout is specified the WDT is started. - The timeout must be given in seconds and 1s the minimum value that - is accepted. Once it is running the timeout cannot be changed and - the WDT cannot be stopped either. + Create a WDT object and start it. The timeout must be given in seconds and + the minimum value that is accepted is 1 second. Once it is running the timeout + cannot be changed and the WDT cannot be stopped either. Methods ------- -.. method:: wdt.kick() +.. method:: wdt.feed() - Kick the WDT to prevent it from resetting the system. The application + Feed the WDT to prevent it from resetting the system. The application should place this call in a sensible place ensuring that the WDT is - only kicked after verifying that everything is functioning correctly. + only fed after verifying that everything is functioning correctly. diff --git a/tests/wipy/wdt.py b/tests/wipy/wdt.py new file mode 100644 index 000000000..9be6293b0 --- /dev/null +++ b/tests/wipy/wdt.py @@ -0,0 +1,37 @@ +''' +WDT test for the CC3200 based boards +''' + +from pyb import WDT + +# test the invalid cases first +try: + wdt = WDT(1) +except Exception: + print("Exception") + +try: + wdt = WDT(0, 500) +except Exception: + print("Exception") + +try: + wdt = WDT(1, timeout=2000) +except Exception: + print("Exception") + +wdt = WDT(timeout=1000) +print(wdt) + +try: + wdt = WDT(0, timeout=2000) +except Exception: + print("Exception") + +pyb.delay(500) +wdt.feed() +print(wdt) +pyb.delay(900) +wdt.feed() +print(wdt) +pyb.delay(950) diff --git a/tests/wipy/wdt.py.exp b/tests/wipy/wdt.py.exp new file mode 100644 index 000000000..71f5e13b5 --- /dev/null +++ b/tests/wipy/wdt.py.exp @@ -0,0 +1,7 @@ +Exception +Exception +Exception + +Exception + +