2017-10-18 11:59:14 +02:00
|
|
|
/*
|
2020-02-28 17:25:23 +01:00
|
|
|
* Copyright (C) 2020 ML!PA Consulting GmbH
|
2017-10-18 11:59:14 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-02-28 17:25:23 +01:00
|
|
|
* @ingroup cpu_cc2538
|
2017-10-18 11:59:14 +02:00
|
|
|
* @ingroup drivers_periph_pm
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
2020-02-28 17:25:23 +01:00
|
|
|
* @brief Implementation of the kernels power management interface
|
2017-10-18 11:59:14 +02:00
|
|
|
*
|
2020-02-28 17:25:23 +01:00
|
|
|
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
|
2017-10-18 11:59:14 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2020-02-28 17:25:23 +01:00
|
|
|
#include "vendor/hw_nvic.h"
|
2017-10-18 11:59:14 +02:00
|
|
|
#include "periph/pm.h"
|
|
|
|
|
2020-10-22 11:34:00 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
2020-02-28 17:25:23 +01:00
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
void pm_set(unsigned mode)
|
2017-10-18 11:59:14 +02:00
|
|
|
{
|
2020-02-28 17:25:23 +01:00
|
|
|
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;
|
2020-03-20 18:25:11 +01:00
|
|
|
/* set to 0 in order to ensure that the 32 MHz XOSC is started as quick
|
|
|
|
as possible after power mode */
|
|
|
|
SYS_CTRL->cc2538_sys_ctrl_clk_ctrl.CLOCK_CTRLbits.OSC_PD = 0;
|
2020-02-28 17:25:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* switch to 16 MHz clock */
|
|
|
|
if (switch_osc) {
|
|
|
|
SYS_CTRL->cc2538_sys_ctrl_clk_ctrl.CLOCK_CTRLbits.OSC = 1;
|
2020-03-20 18:25:11 +01:00
|
|
|
while (!SYS_CTRL->cc2538_sys_ctrl_clk_sta.CLOCK_STAbits.OSC) {}
|
2020-02-28 17:25:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cortexm_sleep(deep);
|
|
|
|
|
|
|
|
/* switch back to 32 MHz clock */
|
|
|
|
if (switch_osc) {
|
|
|
|
SYS_CTRL->cc2538_sys_ctrl_clk_ctrl.CLOCK_CTRLbits.OSC = 0;
|
2020-03-20 18:25:11 +01:00
|
|
|
/* Power down the oscillator not selected by OSC bit */
|
|
|
|
SYS_CTRL->cc2538_sys_ctrl_clk_ctrl.CLOCK_CTRLbits.OSC_PD = 1;
|
|
|
|
while (SYS_CTRL->cc2538_sys_ctrl_clk_sta.CLOCK_STAbits.OSC) {}
|
2020-02-28 17:25:23 +01:00
|
|
|
}
|
2017-10-18 11:59:14 +02:00
|
|
|
}
|