From 8ae92e1a82307f98a4be99c1edd536495cdf21fd Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Fri, 4 Dec 2020 14:41:10 +0100 Subject: [PATCH] bsp/board/openmote-%: add DEEP_SLEEP configuration --- bsp/boards/openmote-b-24ghz/board.c | 15 ++- bsp/boards/openmote-b-24ghz/radio.c | 9 +- bsp/boards/openmote-b/board.c | 23 +++-- bsp/boards/openmote-b/radio.c | 145 +++++++++++++++------------- bsp/boards/openmote-cc2538/board.c | 11 +++ bsp/boards/openmote-cc2538/radio.c | 9 +- bsp/boards/radio.h | 2 + inc/check_config.h | 7 ++ inc/config.h | 11 +++ 9 files changed, 153 insertions(+), 79 deletions(-) diff --git a/bsp/boards/openmote-b-24ghz/board.c b/bsp/boards/openmote-b-24ghz/board.c index 7940176885..f46a3f5e20 100644 --- a/bsp/boards/openmote-b-24ghz/board.c +++ b/bsp/boards/openmote-b-24ghz/board.c @@ -96,20 +96,31 @@ void antenna_init(void) { void antenna_cc2538(void) { GPIOPinWrite(BSP_ANTENNA_BASE, BSP_ANTENNA_CC2538_24GHZ, 0); - GPIOPinWrite(BSP_ANTENNA_BASE, BSP_ANTENNA_AT215_24GHZ, BSP_ANTENNA_AT215_24GHZ); + GPIOPinWrite(BSP_ANTENNA_BASE, BSP_ANTENNA_AT215_24GHZ, BSP_ANTENNA_AT215_24GHZ); } void antenna_at86rf215(void) { GPIOPinWrite(BSP_ANTENNA_BASE, BSP_ANTENNA_AT215_24GHZ, 0); GPIOPinWrite(BSP_ANTENNA_BASE, BSP_ANTENNA_CC2538_24GHZ, BSP_ANTENNA_CC2538_24GHZ); -} +} /** * Puts the board to sleep */ void board_sleep(void) { +#if BOARD_DEEP_SLEEP + if(radio_is_enabled()) { + SysCtrlPowerModeSet(SYS_CTRL_PM_NOACTION); + SysCtrlSleep(); + } + else { + SysCtrlPowerModeSet(SYS_CTRL_PM_1); + SysCtrlDeepSleep(); + } +#else SysCtrlPowerModeSet(SYS_CTRL_PM_NOACTION); SysCtrlSleep(); +#endif } /** diff --git a/bsp/boards/openmote-b-24ghz/radio.c b/bsp/boards/openmote-b-24ghz/radio.c index 08fe64c205..3926a2f6b1 100644 --- a/bsp/boards/openmote-b-24ghz/radio.c +++ b/bsp/boards/openmote-b-24ghz/radio.c @@ -41,6 +41,7 @@ typedef struct { radio_capture_cbt startFrame_cb; radio_capture_cbt endFrame_cb; radio_state_t state; + bool radio_txrx_enabled; } radio_vars_t; radio_vars_t radio_vars; @@ -259,6 +260,7 @@ void radio_loadPacket(uint8_t* packet, uint16_t len) { } void radio_txEnable(void) { + radio_vars.radio_txrx_enabled = true; // change state radio_vars.state = RADIOSTATE_ENABLING_TX; @@ -299,7 +301,7 @@ void radio_txNow(void) { //===== RX void radio_rxEnable(void) { - + radio_vars.radio_txrx_enabled = true; // change state radio_vars.state = RADIOSTATE_ENABLING_RX; @@ -404,6 +406,7 @@ void radio_on(void){ } void radio_off(void){ + radio_vars.radio_txrx_enabled = false; /* Wait for ongoing TX to complete (e.g. this could be an outgoing ACK) */ while(HWREG(RFCORE_XREG_FSMSTAT1) & RFCORE_XREG_FSMSTAT1_TX_ACTIVE); //CC2538_RF_CSP_ISFLUSHRX(); @@ -538,3 +541,7 @@ void radio_error_isr(void){ //poipoi -- todo handle error } } + +bool radio_is_enabled(void) { + return radio_vars.radio_txrx_enabled; +} diff --git a/bsp/boards/openmote-b/board.c b/bsp/boards/openmote-b/board.c index 237a5d0c0c..519524a561 100644 --- a/bsp/boards/openmote-b/board.c +++ b/bsp/boards/openmote-b/board.c @@ -72,7 +72,7 @@ int main(void) { void board_init(void) { user_button_initialized = FALSE; - + gpio_init(); clock_init(); antenna_init(); @@ -104,8 +104,19 @@ void antenna_init(void) { * Puts the board to sleep */ void board_sleep(void) { +#if BOARD_DEEP_SLEEP + if(radio_is_enabled()) { + SysCtrlPowerModeSet(SYS_CTRL_PM_NOACTION); + SysCtrlSleep(); + } + else { + SysCtrlPowerModeSet(SYS_CTRL_PM_1); + SysCtrlDeepSleep(); + } +#else SysCtrlPowerModeSet(SYS_CTRL_PM_NOACTION); SysCtrlSleep(); +#endif } /** @@ -115,7 +126,7 @@ void board_sleep(void) { void board_timer_init(void) { // Configure the timer TimerConfigure(GPTIMER2_BASE, GPTIMER_CFG_PERIODIC_UP); - + // Enable the timer TimerEnable(GPTIMER2_BASE, GPTIMER_BOTH); } @@ -126,9 +137,9 @@ void board_timer_init(void) { */ uint32_t board_timer_get(void) { uint32_t current; - + current = TimerValueGet(GPTIMER2_BASE, GPTIMER_A) >> 5; - + return current; } @@ -143,7 +154,7 @@ bool board_timer_expired(uint32_t future) { current = TimerValueGet(GPTIMER2_BASE, GPTIMER_A) >> 5; remaining = (int32_t) (future - current); - + if (remaining > 0) { return false; } else { @@ -325,7 +336,7 @@ static void GPIO_C_Handler(void) { FlashMainPageErase(CC2538_FLASH_ADDRESS); leds_circular_shift(); - + /* Reset the board */ SysCtrlReset(); } diff --git a/bsp/boards/openmote-b/radio.c b/bsp/boards/openmote-b/radio.c index 2d4281a5cf..b6c303d3b5 100644 --- a/bsp/boards/openmote-b/radio.c +++ b/bsp/boards/openmote-b/radio.c @@ -40,7 +40,8 @@ typedef struct { radio_capture_cbt startFrame_cb; radio_capture_cbt endFrame_cb; - radio_state_t state; + radio_state_t state; + bool radio_txrx_enabled; } radio_vars_t; radio_vars_t radio_vars; @@ -61,21 +62,21 @@ void radio_isr_internal(void); //===== admin void radio_init(void) { - + // clear variables memset(&radio_vars,0,sizeof(radio_vars_t)); - + // change state radio_vars.state = RADIOSTATE_STOPPED; //flush fifos CC2538_RF_CSP_ISFLUSHRX(); CC2538_RF_CSP_ISFLUSHTX(); - + radio_off(); - + //disable radio interrupts disable_radio_interrupts(); - + /* This CORR_THR value should be changed to 0x14 before attempting RX. Testing has shown that too many false frames are received if the reset value is used. Make it more likely to detect @@ -85,26 +86,26 @@ void radio_init(void) { */ HWREG(RFCORE_XREG_MDMCTRL1) = 0x14; /* tuning adjustments for optimal radio performance; details available in datasheet */ - + HWREG(RFCORE_XREG_RXCTRL) = 0x3F; /* Adjust current in synthesizer; details available in datasheet. */ HWREG(RFCORE_XREG_FSCTRL) = 0x55; - + /* Makes sync word detection less likely by requiring two zero symbols before the sync word. * details available in datasheet. */ HWREG(RFCORE_XREG_MDMCTRL0) = 0x85; - + /* Adjust current in VCO; details available in datasheet. */ HWREG(RFCORE_XREG_FSCAL1) = 0x01; /* Adjust target value for AGC control loop; details available in datasheet. */ HWREG(RFCORE_XREG_AGCCTRL1) = 0x15; - + /* Tune ADC performance, details available in datasheet. */ HWREG(RFCORE_XREG_ADCTEST0) = 0x10; HWREG(RFCORE_XREG_ADCTEST1) = 0x0E; HWREG(RFCORE_XREG_ADCTEST2) = 0x03; - + //update CCA register to -81db as indicated by manual.. won't be used.. HWREG(RFCORE_XREG_CCACTRL0) = 0xF8; /* @@ -114,7 +115,7 @@ void radio_init(void) { HWREG(RFCORE_XREG_TXFILTCFG) = 0x09; /** TX anti-aliasing filter bandwidth */ HWREG(RFCORE_XREG_AGCCTRL1) = 0x15; /** AGC target value */ HWREG(ANA_REGS_O_IVCTRL) = 0x0B; /** Bias currents */ - + /* disable the CSPT register compare function */ HWREG(RFCORE_XREG_CSPT) = 0xFFUL; /* @@ -123,33 +124,33 @@ void radio_init(void) { * RX and TX modes with FIFOs */ HWREG(RFCORE_XREG_FRMCTRL0) = RFCORE_XREG_FRMCTRL0_AUTOCRC; - + //poipoi disable frame filtering by now.. sniffer mode. HWREG(RFCORE_XREG_FRMFILT0) &= ~RFCORE_XREG_FRMFILT0_FRAME_FILTER_EN; - + /* Disable source address matching and autopend */ HWREG(RFCORE_XREG_SRCMATCH) = 0; - + /* MAX FIFOP threshold */ HWREG(RFCORE_XREG_FIFOPCTRL) = CC2538_RF_MAX_PACKET_LEN; - + HWREG(RFCORE_XREG_TXPOWER) = CC2538_RF_TX_POWER; HWREG(RFCORE_XREG_FREQCTRL) = CC2538_RF_CHANNEL_MIN; - + /* Enable RF interrupts see page 751 */ // enable_radio_interrupts(); - + //register interrupt IntRegister(INT_RFCORERTX, radio_isr_internal); IntRegister(INT_RFCOREERR, radio_error_isr); - + IntEnable(INT_RFCORERTX); - + /* Enable all RF Error interrupts */ HWREG(RFCORE_XREG_RFERRM) = RFCORE_XREG_RFERRM_RFERRM_M; //all errors IntEnable(INT_RFCOREERR); //radio_on(); - + // change state radio_vars.state = RADIOSTATE_RFOFF; } @@ -167,11 +168,11 @@ void radio_setEndFrameCb(radio_capture_cbt cb) { void radio_reset(void) { /* Wait for ongoing TX to complete (e.g. this could be an outgoing ACK) */ while(HWREG(RFCORE_XREG_FSMSTAT1) & RFCORE_XREG_FSMSTAT1_TX_ACTIVE); - + //flush fifos CC2538_RF_CSP_ISFLUSHRX(); CC2538_RF_CSP_ISFLUSHTX(); - + /* Don't turn off if we are off as this will trigger a Strobe Error */ if(HWREG(RFCORE_XREG_RXENABLE) != 0) { CC2538_RF_CSP_ISRFOFF(); @@ -191,13 +192,13 @@ void radio_setFrequency(uint8_t frequency, radio_freq_t tx_or_rx) { if((frequency < CC2538_RF_CHANNEL_MIN) || (frequency > CC2538_RF_CHANNEL_MAX)) { while(1); } - + /* Changes to FREQCTRL take effect after the next recalibration */ HWREG(RFCORE_XREG_FREQCTRL) = (CC2538_RF_CHANNEL_MIN + (frequency - CC2538_RF_CHANNEL_MIN) * CC2538_RF_CHANNEL_SPACING); - + //radio_on(); - + // change state radio_vars.state = RADIOSTATE_FREQUENCY_SET; } @@ -207,7 +208,7 @@ void radio_rfOn(void) { } void radio_rfOff(void) { - + radio_vars.radio_txrx_enabled = false; // change state radio_vars.state = RADIOSTATE_TURNING_OFF; radio_off(); @@ -216,58 +217,63 @@ void radio_rfOff(void) { leds_radio_off(); //enable radio interrupts disable_radio_interrupts(); - + // change state radio_vars.state = RADIOSTATE_RFOFF; } + +bool radio_is_enabled(void) { + return radio_vars.radio_txrx_enabled; +} + //===== TX void radio_loadPacket(uint8_t* packet, uint16_t len) { uint8_t i=0; - + // change state radio_vars.state = RADIOSTATE_LOADING_PACKET; - + // load packet in TXFIFO /* When we transmit in very quick bursts, make sure previous transmission is not still in progress before re-writing to the TX FIFO */ while(HWREG(RFCORE_XREG_FSMSTAT1) & RFCORE_XREG_FSMSTAT1_TX_ACTIVE); - + CC2538_RF_CSP_ISFLUSHTX(); - + /* Send the phy length byte first */ HWREG(RFCORE_SFR_RFDATA) = len; //crc len is included - + for(i = 0; i < len; i++) { HWREG(RFCORE_SFR_RFDATA) = packet[i]; } - + // change state radio_vars.state = RADIOSTATE_PACKET_LOADED; } void radio_txEnable(void) { - + radio_vars.radio_txrx_enabled = true; // change state radio_vars.state = RADIOSTATE_ENABLING_TX; - + // wiggle debug pin debugpins_radio_set(); leds_radio_on(); - + //do nothing -- radio is activated by the strobe on rx or tx //radio_rfOn(); - + // change state radio_vars.state = RADIOSTATE_TX_ENABLED; } void radio_txNow(void) { PORT_TIMER_WIDTH count; - + // change state radio_vars.state = RADIOSTATE_TRANSMITTING; @@ -290,17 +296,18 @@ void radio_txNow(void) { //===== RX void radio_rxEnable(void) { - + radio_vars.radio_txrx_enabled = true; + // change state radio_vars.state = RADIOSTATE_ENABLING_RX; - + //enable radio interrupts - + // do nothing as we do not want to receive anything yet. // wiggle debug pin debugpins_radio_set(); leds_radio_on(); - + // change state radio_vars.state = RADIOSTATE_LISTENING; } @@ -308,11 +315,11 @@ void radio_rxEnable(void) { void radio_rxNow(void) { //empty buffer before receiving //CC2538_RF_CSP_ISFLUSHRX(); - + //enable radio interrupts CC2538_RF_CSP_ISFLUSHRX(); enable_radio_interrupts(); - + CC2538_RF_CSP_ISRXON(); // busy wait until radio really listening while(!((HWREG(RFCORE_XREG_FSMSTAT1) & RFCORE_XREG_FSMSTAT1_RX_ACTIVE))); @@ -325,49 +332,49 @@ void radio_getReceivedFrame(uint8_t* pBufRead, uint8_t* pLqi, bool* pCrc) { uint8_t crc_corr,i; - + uint8_t len=0; - + /* Check the length */ len = HWREG(RFCORE_SFR_RFDATA); //first byte is len - - + + /* Check for validity */ if(len > CC2538_RF_MAX_PACKET_LEN) { /* wrong len */ CC2538_RF_CSP_ISFLUSHRX(); return; } - - + + if(len <= CC2538_RF_MIN_PACKET_LEN) { //too short CC2538_RF_CSP_ISFLUSHRX(); return; } - + //check if this fits to the buffer if(len > maxBufLen) { CC2538_RF_CSP_ISFLUSHRX(); return; } - + // when reading the packet from the RX buffer, you get the following: // - *[1B] length byte // - [0-125B] packet (excluding CRC) // - [1B] RSSI // - *[2B] CRC - + //skip first byte is len for(i = 0; i < len - 2; i++) { pBufRead[i] = HWREG(RFCORE_SFR_RFDATA); } - + *pRssi = ((int8_t)(HWREG(RFCORE_SFR_RFDATA)) - RSSI_OFFSET); crc_corr = HWREG(RFCORE_SFR_RFDATA); *pCrc = crc_corr & CRC_BIT_MASK; *pLenRead = len; - + //flush it CC2538_RF_CSP_ISFLUSHRX(); } @@ -398,7 +405,7 @@ void radio_off(void){ /* Wait for ongoing TX to complete (e.g. this could be an outgoing ACK) */ while(HWREG(RFCORE_XREG_FSMSTAT1) & RFCORE_XREG_FSMSTAT1_TX_ACTIVE); //CC2538_RF_CSP_ISFLUSHRX(); - + /* Don't turn off if we are off as this will trigger a Strobe Error */ if(HWREG(RFCORE_XREG_RXENABLE) != 0) { CC2538_RF_CSP_ISRFOFF(); @@ -436,22 +443,22 @@ kick_scheduler_t radio_isr(void) { void radio_isr_internal(void) { volatile PORT_TIMER_WIDTH capturedTime; uint8_t irq_status0,irq_status1; - + debugpins_isr_set(); - + // capture the time capturedTime = sctimer_readCounter(); - + // reading IRQ_STATUS irq_status0 = HWREG(RFCORE_SFR_RFIRQF0); irq_status1 = HWREG(RFCORE_SFR_RFIRQF1); - + IntPendClear(INT_RFCORERTX); - + //clear interrupt HWREG(RFCORE_SFR_RFIRQF0) = 0; HWREG(RFCORE_SFR_RFIRQF1) = 0; - + //STATUS0 Register // start of frame event if ((irq_status0 & RFCORE_SFR_RFIRQF0_SFD) == RFCORE_SFR_RFIRQF0_SFD) { @@ -467,7 +474,7 @@ void radio_isr_internal(void) { while(1); } } - + //or RXDONE is full -- we have a packet. if (((irq_status0 & RFCORE_SFR_RFIRQF0_RXPKTDONE) == RFCORE_SFR_RFIRQF0_RXPKTDONE)) { // change state @@ -482,7 +489,7 @@ void radio_isr_internal(void) { while(1); } } - + // or FIFOP is full -- we have a packet. if (((irq_status0 & RFCORE_SFR_RFIRQF0_FIFOP) == RFCORE_SFR_RFIRQF0_FIFOP)) { // change state @@ -497,7 +504,7 @@ void radio_isr_internal(void) { while(1); } } - + //STATUS1 Register // end of frame event --either end of tx . if (((irq_status1 & RFCORE_SFR_RFIRQF1_TXDONE) == RFCORE_SFR_RFIRQF1_TXDONE)) { @@ -514,15 +521,15 @@ void radio_isr_internal(void) { } } debugpins_isr_clr(); - + return; } void radio_error_isr(void){ uint8_t rferrm; - + rferrm = (uint8_t)HWREG(RFCORE_XREG_RFERRM); - + if ((HWREG(RFCORE_XREG_RFERRM) & (((0x02)<