summaryrefslogtreecommitdiff
path: root/code/fe310/eos
diff options
context:
space:
mode:
Diffstat (limited to 'code/fe310/eos')
-rw-r--r--code/fe310/eos/power.c97
-rw-r--r--code/fe310/eos/power.h22
2 files changed, 119 insertions, 0 deletions
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 <stdlib.h>
+#include <stdint.h>
+
+#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<EOS_PWR_MAX_MTYPE; i++) {
+ evt_handler[i] = eos_net_bad_handler;
+ }
+ eos_net_set_handler(EOS_NET_MTYPE_POWER, power_handle_evt);
+ eos_power_set_handler(EOS_PWR_MTYPE_BUTTON, power_handle_btn);
+
+ AON_REG(AON_PMUKEY) = 0x51F15E;
+ AON_REG(AON_PMUIE) = 0x5;
+
+ AON_REG(AON_RTCCMP) = 0xFFFFFFFF;
+ AON_REG(AON_RTCCFG) = PWR_RTC_SCALE;
+ AON_REG(AON_RTCHI) = 0;
+ AON_REG(AON_RTCLO) = 0;
+}
+
+uint8_t eos_power_cause_wake(void) {
+ return AON_REG(AON_PMUCAUSE) & 0xff;
+}
+
+uint8_t eos_power_cause_rst(void) {
+ return (AON_REG(AON_PMUCAUSE) >> 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 <stdint.h>
+#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