5353#include "mpcallback.h"
5454#include "mperror.h"
5555#include "sleeprestore.h"
56+ #include "serverstask.h"
5657
5758/******************************************************************************
5859 DECLARE PRIVATE CONSTANTS
@@ -160,12 +161,6 @@ void pybsleep_remove (const mp_obj_t obj) {
160161}
161162
162163void pybsleep_set_wlan_wake_callback (mp_obj_t cb_obj ) {
163- if (cb_obj ) {
164- MAP_PRCMLPDSWakeupSourceEnable (PRCM_LPDS_HOST_IRQ );
165- }
166- else {
167- MAP_PRCMLPDSWakeupSourceDisable (PRCM_LPDS_HOST_IRQ );
168- }
169164 pybsleep_wake_cb .wlan_wake_cb = cb_obj ;
170165}
171166
@@ -269,9 +264,10 @@ STATIC NORETURN void pybsleep_suspend_enter (void) {
269264 nvic_reg_store -> int_priority [i ] = base_reg_addr [i ];
270265 }
271266
272- // park the io pins
267+ // park the gpio pins
273268 pybsleep_iopark ();
274269
270+ // store the cpu registers
275271 sleep_store ();
276272
277273 // save the restore info and enter LPDS
@@ -326,10 +322,12 @@ void pybsleep_suspend_exit (void) {
326322
327323 // ungate the clock to the shared spi bus
328324 MAP_PRCMPeripheralClkEnable (PRCM_SSPI , PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK );
329- MAP_PRCMIntEnable (PRCM_INT_SLOW_CLK_CTR );
330325
331- // reinitialize simplelink's bus
332- sl_IfOpen (NULL , 0 );
326+ // if wakeups are enabled, simplelink is as well, so we only need to
327+ // reinitialize the interface bus
328+ if (pybsleep_wake_cb .wlan_wake_cb ) {
329+ sl_IfOpen (NULL , 0 );
330+ }
333331
334332 // initialize the system led
335333 mperror_init0 ();
@@ -441,9 +439,22 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_sleep_idle_obj, pyb_sleep_idle);
441439// calling this method.
442440STATIC mp_obj_t pyb_sleep_suspend (mp_obj_t self_in ) {
443441 nlr_buf_t nlr ;
442+ bool servers_enabled = servers_are_enabled ();
443+
444+ // check if we need to enable network wake-up
445+ if (pybsleep_wake_cb .wlan_wake_cb ) {
446+ MAP_PRCMLPDSWakeupSourceEnable (PRCM_LPDS_HOST_IRQ );
447+ }
448+ else {
449+ MAP_PRCMLPDSWakeupSourceDisable (PRCM_LPDS_HOST_IRQ );
450+ if (servers_enabled ) {
451+ wlan_stop_servers ();
452+ }
453+ sl_Stop (SL_STOP_TIMEOUT );
454+ }
444455
445456 // entering and exiting suspend mode must be an atomic operation
446- // therefore interrupts must be disabled
457+ // therefore interrupts have to be disabled
447458 uint primsk = disable_irq ();
448459 if (nlr_push (& nlr ) == 0 ) {
449460 pybsleep_suspend_enter ();
@@ -452,6 +463,15 @@ STATIC mp_obj_t pyb_sleep_suspend (mp_obj_t self_in) {
452463 // an exception is always raised when exiting suspend mode
453464 enable_irq (primsk );
454465
466+ // enable simplelink if previously disabled
467+ if (!pybsleep_wake_cb .wlan_wake_cb ) {
468+ // start simplelink, then enable the servers
469+ sl_Start (0 , 0 , 0 );
470+ if (servers_enabled ) {
471+ servers_enable ();
472+ }
473+ }
474+
455475 return mp_const_none ;
456476}
457477STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pyb_sleep_suspend_obj , pyb_sleep_suspend );
0 commit comments