summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/soc/pwr.c
blob: a2adfd4a9f766b0911f7b90a279f0287557f96f3 (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
63
64
65
66
67
68
69
70
#include <stdlib.h>
#include <stdint.h>

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

#include "eos.h"
#include "timer.h"
#include "dev/net.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;
}

int eos_pwr_sleep(void) {
    int rv;

    rv = eos_net_sleep(1000);
    if (rv) return rv;

    AON_REG(AON_PMUKEY) = 0x51F15E;
    AON_REG(AON_PMUSLEEP) = 1;

    return EOS_OK;
}

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;
}