Skip to content

Commit 41f6948

Browse files
author
Daniel Campora
committed
cc3200: New WDT API.
1 parent 8332044 commit 41f6948

5 files changed

Lines changed: 97 additions & 49 deletions

File tree

cc3200/mods/pybwdt.c

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -88,59 +88,63 @@ void pybwdt_sl_alive (void) {
8888
/******************************************************************************/
8989
// Micro Python bindings
9090

91-
/// \function constructor('msec')
92-
/// Enables the watchdog timer with msec timeout value
93-
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) {
91+
STATIC const mp_arg_t pyb_wdt_init_args[] = {
92+
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} },
93+
{ MP_QSTR_timeout, MP_ARG_INT, {.u_int = 5000} }, // 5 s
94+
};
95+
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) {
9496
// check the arguments
95-
mp_arg_check_num(n_args, n_kw, 0, 1, false);
96-
97-
if (n_args > 0) {
98-
mp_int_t msec = mp_obj_get_int(args[0]);
99-
if (msec < PYBWDT_MIN_TIMEOUT_MS) {
100-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
101-
}
102-
if (pybwdt_data.running) {
103-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
104-
}
105-
106-
// Enable the WDT peripheral clock
107-
MAP_PRCMPeripheralClkEnable(PRCM_WDT, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
108-
109-
// Unlock to be able to configure the registers
110-
MAP_WatchdogUnlock(WDT_BASE);
111-
112-
#ifdef DEBUG
113-
// make the WDT stall when the debugger stops on a breakpoint
114-
MAP_WatchdogStallEnable (WDT_BASE);
115-
#endif
116-
117-
// set the watchdog timer reload value
118-
// the WDT trigger a system reset after the second timeout
119-
// so, divide by 2 the timeout value received
120-
MAP_WatchdogReloadSet(WDT_BASE, PYBWDT_MILLISECONDS_TO_TICKS(msec / 2));
121-
122-
// start the timer. Once it's started, it cannot be disabled.
123-
MAP_WatchdogEnable(WDT_BASE);
124-
pybwdt_data.running = true;
97+
mp_map_t kw_args;
98+
mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args);
99+
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_wdt_init_args)];
100+
mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_wdt_init_args, args);
101+
102+
if (args[0].u_obj != mp_const_none && mp_obj_get_int(args[0].u_obj) > 0) {
103+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
104+
}
105+
uint timeout_ms = args[1].u_int;
106+
if (timeout_ms < PYBWDT_MIN_TIMEOUT_MS) {
107+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
125108
}
109+
if (pybwdt_data.running) {
110+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
111+
}
112+
113+
// Enable the WDT peripheral clock
114+
MAP_PRCMPeripheralClkEnable(PRCM_WDT, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
115+
116+
// Unlock to be able to configure the registers
117+
MAP_WatchdogUnlock(WDT_BASE);
118+
119+
#ifdef DEBUG
120+
// make the WDT stall when the debugger stops on a breakpoint
121+
MAP_WatchdogStallEnable (WDT_BASE);
122+
#endif
123+
124+
// set the watchdog timer reload value
125+
// the WDT trigger a system reset after the second timeout
126+
// so, divide by 2 the timeout value received
127+
MAP_WatchdogReloadSet(WDT_BASE, PYBWDT_MILLISECONDS_TO_TICKS(timeout_ms / 2));
128+
129+
// start the timer. Once it's started, it cannot be disabled.
130+
MAP_WatchdogEnable(WDT_BASE);
131+
pybwdt_data.running = true;
126132

127133
return (mp_obj_t)&pyb_wdt_obj;
128134
}
129135

130-
/// \function wdt.kick()
131-
/// Kicks the watchdog timer
132-
STATIC mp_obj_t pyb_wdt_kick(mp_obj_t self) {
136+
STATIC mp_obj_t pyb_wdt_feed(mp_obj_t self) {
133137
if ((pybwdt_data.servers || pybwdt_data.servers_sleeping) && pybwdt_data.simplelink && pybwdt_data.running) {
134138
pybwdt_data.servers = false;
135139
pybwdt_data.simplelink = false;
136140
MAP_WatchdogIntClear(WDT_BASE);
137141
}
138142
return mp_const_none;
139143
}
140-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_wdt_kick_obj, pyb_wdt_kick);
144+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_wdt_feed_obj, pyb_wdt_feed);
141145

142146
STATIC const mp_map_elem_t pybwdt_locals_dict_table[] = {
143-
{ MP_OBJ_NEW_QSTR(MP_QSTR_kick), (mp_obj_t)&pyb_wdt_kick_obj },
147+
{ MP_OBJ_NEW_QSTR(MP_QSTR_feed), (mp_obj_t)&pyb_wdt_feed_obj },
144148
};
145149
STATIC MP_DEFINE_CONST_DICT(pybwdt_locals_dict, pybwdt_locals_dict_table);
146150

cc3200/qstrdefsport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ Q(EXTERNAL)
299299

300300
// for WDT class
301301
Q(WDT)
302-
Q(kick)
302+
Q(feed)
303+
Q(timeout)
303304

304305
// for HeartBeat class
305306
Q(HeartBeat)

docs/library/pyb.WDT.rst

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,23 @@ watchdog periodically to prevent it from expiring and resetting the system.
1010

1111
Example usage::
1212

13-
wdt = pyb.WDT(5000) # enable with a timeout of 5s
14-
wdt.kick()
13+
wdt = pyb.WDT(timeout=2000) # enable with a timeout of 2s
14+
wdt.feed()
1515

1616
Constructors
1717
------------
1818

19-
.. class:: pyb.WDT([timeout])
19+
.. class:: pyb.WDT(id=0, timeout=5000)
2020

21-
Create a WDT object. If the timeout is specified the WDT is started.
22-
The timeout must be given in seconds and 1s the minimum value that
23-
is accepted. Once it is running the timeout cannot be changed and
24-
the WDT cannot be stopped either.
21+
Create a WDT object and start it. The timeout must be given in seconds and
22+
the minimum value that is accepted is 1 second. Once it is running the timeout
23+
cannot be changed and the WDT cannot be stopped either.
2524

2625
Methods
2726
-------
2827

29-
.. method:: wdt.kick()
28+
.. method:: wdt.feed()
3029

31-
Kick the WDT to prevent it from resetting the system. The application
30+
Feed the WDT to prevent it from resetting the system. The application
3231
should place this call in a sensible place ensuring that the WDT is
33-
only kicked after verifying that everything is functioning correctly.
32+
only fed after verifying that everything is functioning correctly.

tests/wipy/wdt.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
WDT test for the CC3200 based boards
3+
'''
4+
5+
from pyb import WDT
6+
7+
# test the invalid cases first
8+
try:
9+
wdt = WDT(1)
10+
except Exception:
11+
print("Exception")
12+
13+
try:
14+
wdt = WDT(0, 500)
15+
except Exception:
16+
print("Exception")
17+
18+
try:
19+
wdt = WDT(1, timeout=2000)
20+
except Exception:
21+
print("Exception")
22+
23+
wdt = WDT(timeout=1000)
24+
print(wdt)
25+
26+
try:
27+
wdt = WDT(0, timeout=2000)
28+
except Exception:
29+
print("Exception")
30+
31+
pyb.delay(500)
32+
wdt.feed()
33+
print(wdt)
34+
pyb.delay(900)
35+
wdt.feed()
36+
print(wdt)
37+
pyb.delay(950)

tests/wipy/wdt.py.exp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Exception
2+
Exception
3+
Exception
4+
<WDT>
5+
Exception
6+
<WDT>
7+
<WDT>

0 commit comments

Comments
 (0)