From d4ceadf1afc2b3c8120d65ea1f03203df5714d7a Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Sun, 17 May 2020 04:04:22 +0200 Subject: power management implemented; net protocol change --- code/fe310/eos/power.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ code/fe310/eos/power.h | 22 ++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 code/fe310/eos/power.c create mode 100644 code/fe310/eos/power.h (limited to 'code/fe310') diff --git a/code/fe310/eos/power.c b/code/fe310/eos/power.c new file mode 100644 index 0000000..9a96689 --- /dev/null +++ b/code/fe310/eos/power.c @@ -0,0 +1,97 @@ +#include +#include + +#include "encoding.h" +#include "platform.h" + +#include "eos.h" +#include "event.h" +#include "timer.h" +#include "net.h" +#include "eve/eve.h" + +#include "power.h" + +#define PWR_RTC_SCALE 15 +#define PWR_RTC_SFREQ (EOS_TIMER_RTC_FREQ >> PWR_RTC_SCALE) + +static eos_evt_handler_t evt_handler[EOS_PWR_MAX_MTYPE]; + +static void power_handle_evt(unsigned char type, unsigned char *buffer, uint16_t len) { + if ((buffer == NULL) || (len < 1)) { + eos_net_bad_handler(type, buffer, len); + return; + } + + unsigned char mtype = buffer[0]; + if (mtype < EOS_PWR_MAX_MTYPE) { + evt_handler[mtype](type, buffer, len); + } else { + eos_net_bad_handler(type, buffer, len); + } +} + +static void power_handle_btn(unsigned char type, unsigned char *buffer, uint16_t len) { + // Do nothing + eos_net_free(buffer, 0); +} + +void eos_power_init(void) { + int i; + + for (i=0; i> 8) & 0xff; +} + +void eos_power_sleep(void) { + AON_REG(AON_PMUKEY) = 0x51F15E; + AON_REG(AON_PMUSLEEP) = 1; +} + +void eos_power_wake_at(uint32_t msec) { + uint32_t pmuie; + + AON_REG(AON_RTCCFG) |= AON_RTCCFG_ENALWAYS; + AON_REG(AON_RTCCMP) = msec * PWR_RTC_SFREQ / 1000; + + pmuie = AON_REG(AON_PMUIE) | 0x2; + AON_REG(AON_PMUKEY) = 0x51F15E; + AON_REG(AON_PMUIE) = pmuie; +} + +void eos_power_wake_disable(void) { + uint32_t pmuie; + + AON_REG(AON_RTCCMP) = 0xFFFFFFFF; + AON_REG(AON_RTCCFG) &= ~AON_RTCCFG_ENALWAYS; + AON_REG(AON_RTCHI) = 0; + AON_REG(AON_RTCLO) = 0; + + pmuie = AON_REG(AON_PMUIE) & ~0x2; + AON_REG(AON_PMUKEY) = 0x51F15E; + AON_REG(AON_PMUIE) = pmuie; +} + +void eos_power_set_handler(unsigned char mtype, eos_evt_handler_t handler) { + if (handler == NULL) handler = eos_net_bad_handler; + if (mtype < EOS_PWR_MAX_MTYPE) evt_handler[mtype] = handler; +} diff --git a/code/fe310/eos/power.h b/code/fe310/eos/power.h new file mode 100644 index 0000000..7f579a5 --- /dev/null +++ b/code/fe310/eos/power.h @@ -0,0 +1,22 @@ +#include +#include "event.h" + +#define EOS_PWR_MTYPE_BUTTON 0 + +#define EOS_PWR_MAX_MTYPE 2 + +#define EOS_PWR_WAKE_RESET 0 +#define EOS_PWR_WAKE_RTC 1 +#define EOS_PWR_WAKE_BTN 2 + +#define EOS_PWR_RST_PWRON 0 +#define EOS_PWR_RST_EXT 1 +#define EOS_PWR_RST_WDOG 2 + +void eos_power_init(void); +uint8_t eos_power_cause_wake(void); +uint8_t eos_power_cause_rst(void); +void eos_power_sleep(void); +void eos_power_wake_at(uint32_t msec); +void eos_power_wake_disable(void); +void eos_power_set_handler(unsigned char mtype, eos_evt_handler_t handler); \ No newline at end of file -- cgit v1.2.3