summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/soc/pwr.c
blob: db9f2735216f4093d221fbf117527c97d6d36859 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdlib.h>
#include <stdint.h>

#include "encoding.h"
#include "platform.h"

#include "eos.h"
#include "timer.h"

#include "pwr.h"

#define PWR_RTC_SCALE   15
#define PWR_RTC_SFREQ   (EOS_TIMER_RTC_FREQ >> PWR_RTC_SCALE)

int eos_pwr_init(uint8_t wakeup_cause) {
    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;

    return EOS_OK;
}

uint8_t eos_pwr_wakeup_cause(void) {
    return AON_REG(AON_PMUCAUSE) & 0xff;
}

uint8_t eos_pwr_reset_cause(void) {
    return (AON_REG(AON_PMUCAUSE) >> 8) & 0xff;
}

void eos_pwr_sleep(void) {
    AON_REG(AON_PMUKEY) = 0x51F15E;
    AON_REG(AON_PMUSLEEP) = 1;
}

void eos_pwr_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_pwr_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;
}