1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00
RIOT/cpu/cc2538/periph/pm.c
Benjamin Valentin 5d8c00e302 cpu/cc2538: implement periph/pm
cc2538 implements 4 sleep modes.
In the lightest mode (3) any interrupt source can wake up the CPU.
In mode 2, only RTT, GPIO or USB may wake the CPU.
In mode 1 only RTT and GPIO can wake the CPU.
In mode 0 only GPIO can wake the CPU.

In mode 0 and 1 the lower 16k RAM are lost. This is a problem since those
are usually used by RIOT.

The linkerscripts in cc2538/ldscripts take different approaches towards that.
Some only use the upper 16k and leave the other half to be managed by the
application.

`cc2538sf53.ld` which is used by `openmote-b` uses the entire RAM starting
at the lower half, so it will not be able to wake up from those modes.

A quick fix to test those modes with `tests/periph_pm` would be

--- a/cpu/cc2538/ldscripts/cc2538sf53.ld
+++ b/cpu/cc2538/ldscripts/cc2538sf53.ld
@@ -21,7 +21,7 @@ MEMORY
 {
     rom (rx)    : ORIGIN = 0x00200000, LENGTH = 512K - 44
     cca         : ORIGIN = 0x0027ffd4, LENGTH = 44
-    ram (w!rx)  : ORIGIN = 0x20000000, LENGTH = 32K
+    ram (w!rx)  : ORIGIN = 0x20004000, LENGTH = 16K
 }
2020-03-10 10:35:46 +01:00

77 lines
1.9 KiB
C

/*
* Copyright (C) 2020 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup cpu_cc2538
* @ingroup drivers_periph_pm
* @{
*
* @file
* @brief Implementation of the kernels power management interface
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*
* @}
*/
#include "vendor/hw_nvic.h"
#include "periph/pm.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
void pm_set(unsigned mode)
{
bool deep = false;
bool switch_osc = false;
switch (mode) {
case 0:
/* lowest 16k RAM are lost here, wake by GPIO */
SYS_CTRL_PMCTL = 0x3;
deep = true;
break;
case 1:
/* lowest 16k RAM are lost here, wake by GPIO & RTT */
SYS_CTRL_PMCTL = 0x2;
deep = true;
break;
case 2:
/* all memory retained, wake by GPIO, RTT & USB */
SYS_CTRL_PMCTL = 0x1;
deep = true;
break;
case 3:
/* all memory retained, wake by any interrupt source */
deep = true;
SYS_CTRL_PMCTL = 0x0;
break;
}
if (deep) {
*(cc2538_reg_t*) NVIC_SYS_CTRL |= NVIC_SYS_CTRL_SLEEPDEEP;
/* If we used the 32 MHz clock, we have to switch to 16 MHz for deep sleep */
switch_osc = !SYS_CTRL->cc2538_sys_ctrl_clk_ctrl.CLOCK_CTRLbits.OSC;
}
/* switch to 16 MHz clock */
if (switch_osc) {
SYS_CTRL->cc2538_sys_ctrl_clk_ctrl.CLOCK_CTRLbits.OSC = 1;
while (!SYS_CTRL->cc2538_sys_ctrl_clk_ctrl.CLOCK_CTRLbits.OSC) {}
}
cortexm_sleep(deep);
/* switch back to 32 MHz clock */
if (switch_osc) {
SYS_CTRL->cc2538_sys_ctrl_clk_ctrl.CLOCK_CTRLbits.OSC = 0;
while (SYS_CTRL->cc2538_sys_ctrl_clk_ctrl.CLOCK_CTRLbits.OSC) {}
}
}