3939#include "prcm.h"
4040#include "utils.h"
4141#include "pybwdt.h"
42+ #include "mpexception.h"
43+ #include "mperror.h"
4244
4345
4446/******************************************************************************
@@ -65,59 +67,11 @@ static pybwdt_data_t pybwdt_data;
6567 DEFINE PUBLIC FUNCTIONS
6668 ******************************************************************************/
6769// must be called in main.c just after initializing the hal
70+ __attribute__ ((section (".boot" )))
6871void pybwdt_init0 (void ) {
6972 pybwdt_data .running = false;
7073}
7174
72- void pybwdt_check_reset_cause (void ) {
73- // if we are recovering from a WDT reset, trigger
74- // a hibernate cycle for a clean boot
75- if (MAP_PRCMSysResetCauseGet () == PRCM_WDT_RESET ) {
76- HWREG (0x400F70B8 ) = 1 ;
77- UtilsDelay (800000 /5 );
78- HWREG (0x400F70B0 ) = 1 ;
79- UtilsDelay (800000 /5 );
80-
81- HWREG (0x4402E16C ) |= 0x2 ;
82- UtilsDelay (800 );
83- HWREG (0x4402F024 ) &= 0xF7FFFFFF ;
84-
85- MAP_PRCMHibernateWakeupSourceEnable (PRCM_HIB_SLOW_CLK_CTR );
86- // set the sleep interval to 10ms
87- MAP_PRCMHibernateIntervalSet (330 );
88- MAP_PRCMHibernateEnter ();
89- }
90- }
91-
92- // minimum timeout value is 500ms
93- pybwdt_ret_code_t pybwdt_enable (uint32_t timeout ) {
94- if (timeout >= PYBWDT_MIN_TIMEOUT_MS ) {
95- if (!pybwdt_data .running ) {
96- // Enable the WDT peripheral clock
97- MAP_PRCMPeripheralClkEnable (PRCM_WDT , PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK );
98-
99- // Unlock to be able to configure the registers
100- MAP_WatchdogUnlock (WDT_BASE );
101-
102- // Make the WDT stall when the debugger stops on a breakpoint
103- MAP_WatchdogStallEnable (WDT_BASE );
104-
105- // Set the watchdog timer reload value
106- // the WDT trigger a system reset after the second timeout
107- // so, divide by the 2 timeout value received
108- MAP_WatchdogReloadSet (WDT_BASE , PYBWDT_MILLISECONDS_TO_TICKS (timeout / 2 ));
109-
110- // Start the timer. Once the timer is started, it cannot be disabled.
111- MAP_WatchdogEnable (WDT_BASE );
112- pybwdt_data .running = true;
113-
114- return E_PYBWDT_OK ;
115- }
116- return E_PYBWDT_IS_RUNNING ;
117- }
118- return E_PYBWDT_INVALID_TIMEOUT ;
119- }
120-
12175void pybwdt_kick (void ) {
12276 // check that the servers and simplelink are running fine
12377 if (pybwdt_data .servers && pybwdt_data .simplelink && pybwdt_data .running ) {
@@ -134,3 +88,62 @@ void pybwdt_srv_alive (void) {
13488void pybwdt_sl_alive (void ) {
13589 pybwdt_data .simplelink = true;
13690}
91+
92+ /******************************************************************************/
93+ // Micro Python bindings
94+
95+ /// \function wdt_enable('msec')
96+ /// Enabled the watchdog timer with msec timeout value
97+ STATIC mp_obj_t pyb_enable_wdt (mp_obj_t self , mp_obj_t msec_in ) {
98+ mp_int_t msec = mp_obj_get_int (msec_in );
99+
100+ if (msec < PYBWDT_MIN_TIMEOUT_MS ) {
101+ nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
102+ }
103+ if (pybwdt_data .running ) {
104+ nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_request_not_possible ));
105+ }
106+
107+ // Enable the WDT peripheral clock
108+ MAP_PRCMPeripheralClkEnable (PRCM_WDT , PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK );
109+
110+ // Unlock to be able to configure the registers
111+ MAP_WatchdogUnlock (WDT_BASE );
112+
113+ // make the WDT stall when the debugger stops on a breakpoint
114+ MAP_WatchdogStallEnable (WDT_BASE );
115+
116+ // set the watchdog timer reload value
117+ // the WDT trigger a system reset after the second timeout
118+ // so, divide by the 2 timeout value received
119+ MAP_WatchdogReloadSet (WDT_BASE , PYBWDT_MILLISECONDS_TO_TICKS (msec / 2 ));
120+
121+ // start the timer. Once wdt is started, it cannot be disabled.
122+ MAP_WatchdogEnable (WDT_BASE );
123+ pybwdt_data .running = true;
124+
125+ return mp_const_none ;
126+ }
127+ STATIC MP_DEFINE_CONST_FUN_OBJ_2 (pyb_enable_wdt_obj , pyb_enable_wdt );
128+
129+ /// \function wdt_kick()
130+ /// Kicks the watchdog timer
131+ STATIC mp_obj_t pyb_kick_wdt (mp_obj_t self ) {
132+ pybwdt_kick ();
133+ return mp_const_none ;
134+ }
135+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pyb_kick_wdt_obj , pyb_kick_wdt );
136+
137+ STATIC const mp_map_elem_t pybwdt_locals_dict_table [] = {
138+ { MP_OBJ_NEW_QSTR (MP_QSTR_enable ), (mp_obj_t )& pyb_enable_wdt_obj },
139+ { MP_OBJ_NEW_QSTR (MP_QSTR_kick ), (mp_obj_t )& pyb_kick_wdt_obj },
140+ };
141+ STATIC MP_DEFINE_CONST_DICT (pybwdt_locals_dict , pybwdt_locals_dict_table );
142+
143+ static const mp_obj_type_t pybwdt_type = {
144+ { & mp_type_type },
145+ .name = MP_QSTR_WDT ,
146+ .locals_dict = (mp_obj_t )& pybwdt_locals_dict ,
147+ };
148+
149+ const mp_obj_base_t pyb_wdt_obj = {& pybwdt_type };
0 commit comments