@@ -63,6 +63,7 @@ typedef struct {
6363 DECLARE PRIVATE DATA
6464 ******************************************************************************/
6565static pybwdt_data_t pybwdt_data = {.servers = false, .servers_sleeping = false, .simplelink = false, .running = false};
66+ STATIC const mp_obj_base_t pyb_wdt_obj = {& pyb_wdt_type };
6667
6768/******************************************************************************
6869 DEFINE PUBLIC FUNCTIONS
@@ -96,41 +97,44 @@ void pybwdt_sl_alive (void) {
9697/******************************************************************************/
9798// Micro Python bindings
9899
99- /// \function wdt_enable('msec')
100- /// Enabled the watchdog timer with msec timeout value
101- STATIC mp_obj_t pyb_enable_wdt (mp_obj_t self , mp_obj_t msec_in ) {
102- mp_int_t msec = mp_obj_get_int (msec_in );
103-
104- if (msec < PYBWDT_MIN_TIMEOUT_MS ) {
105- nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
106- }
107- if (pybwdt_data .running ) {
108- nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_request_not_possible ));
100+ /// \function constructor('msec')
101+ /// Enables the watchdog timer with msec timeout value
102+ 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 ) {
103+ // check the arguments
104+ mp_arg_check_num (n_args , n_kw , 0 , 1 , false);
105+
106+ if (n_args > 0 ) {
107+ mp_int_t msec = mp_obj_get_int (args [0 ]);
108+ if (msec < PYBWDT_MIN_TIMEOUT_MS ) {
109+ nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
110+ }
111+ if (pybwdt_data .running ) {
112+ nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_request_not_possible ));
113+ }
114+
115+ // Enable the WDT peripheral clock
116+ MAP_PRCMPeripheralClkEnable (PRCM_WDT , PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK );
117+
118+ // Unlock to be able to configure the registers
119+ MAP_WatchdogUnlock (WDT_BASE );
120+
121+ #ifdef DEBUG
122+ // make the WDT stall when the debugger stops on a breakpoint
123+ MAP_WatchdogStallEnable (WDT_BASE );
124+ #endif
125+
126+ // set the watchdog timer reload value
127+ // the WDT trigger a system reset after the second timeout
128+ // so, divide by 2 the timeout value received
129+ MAP_WatchdogReloadSet (WDT_BASE , PYBWDT_MILLISECONDS_TO_TICKS (msec / 2 ));
130+
131+ // start the timer. Once it's started, it cannot be disabled.
132+ MAP_WatchdogEnable (WDT_BASE );
133+ pybwdt_data .running = true;
109134 }
110135
111- // Enable the WDT peripheral clock
112- MAP_PRCMPeripheralClkEnable (PRCM_WDT , PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK );
113-
114- // Unlock to be able to configure the registers
115- MAP_WatchdogUnlock (WDT_BASE );
116-
117- #ifdef DEBUG
118- // make the WDT stall when the debugger stops on a breakpoint
119- MAP_WatchdogStallEnable (WDT_BASE );
120- #endif
121-
122- // set the watchdog timer reload value
123- // the WDT trigger a system reset after the second timeout
124- // so, divide by 2 the timeout value received
125- MAP_WatchdogReloadSet (WDT_BASE , PYBWDT_MILLISECONDS_TO_TICKS (msec / 2 ));
126-
127- // start the timer. Once it's started, it cannot be disabled.
128- MAP_WatchdogEnable (WDT_BASE );
129- pybwdt_data .running = true;
130-
131- return mp_const_none ;
136+ return (mp_obj_t )& pyb_wdt_obj ;
132137}
133- STATIC MP_DEFINE_CONST_FUN_OBJ_2 (pyb_enable_wdt_obj , pyb_enable_wdt );
134138
135139/// \function wdt_kick()
136140/// Kicks the watchdog timer
@@ -141,15 +145,14 @@ STATIC mp_obj_t pyb_kick_wdt(mp_obj_t self) {
141145STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pyb_kick_wdt_obj , pyb_kick_wdt );
142146
143147STATIC const mp_map_elem_t pybwdt_locals_dict_table [] = {
144- { MP_OBJ_NEW_QSTR (MP_QSTR_enable ), (mp_obj_t )& pyb_enable_wdt_obj },
145148 { MP_OBJ_NEW_QSTR (MP_QSTR_kick ), (mp_obj_t )& pyb_kick_wdt_obj },
146149};
147150STATIC MP_DEFINE_CONST_DICT (pybwdt_locals_dict , pybwdt_locals_dict_table );
148151
149- static const mp_obj_type_t pybwdt_type = {
152+ const mp_obj_type_t pyb_wdt_type = {
150153 { & mp_type_type },
151154 .name = MP_QSTR_WDT ,
155+ .make_new = pyb_wdt_make_new ,
152156 .locals_dict = (mp_obj_t )& pybwdt_locals_dict ,
153157};
154158
155- const mp_obj_base_t pyb_wdt_obj = {& pybwdt_type };
0 commit comments