summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/eve/eve_eos.c
blob: 283aa6aeb9bfdffa4e115e534e9c24f5bca16c03 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdlib.h>

#include "platform.h"

#include "eos.h"
#include "interrupt.h"
#include "event.h"
#include "pwr.h"

#include "board.h"

#include "eve.h"
#include "eve_eos.h"

static int _run;

static void handle_time(unsigned char type) {
    if (_run) {
        eve_spi_start();
        eve_handle_time();
        eve_spi_stop();
    }
}

static void handle_evt(unsigned char type, unsigned char *buffer, uint16_t len) {
    if (_run) {
        eve_spi_start();
        eve_handle_intr();
        eve_spi_stop();

        GPIO_REG(GPIO_LOW_IE) |= (1 << EVE_PIN_INTR);
    }
}

static void handle_intr(void) {
    GPIO_REG(GPIO_LOW_IE) &= ~(1 << EVE_PIN_INTR);
    GPIO_REG(GPIO_LOW_IP) = (1 << EVE_PIN_INTR);
    eos_evtq_push_isr(EOS_EVT_UI | EVE_ETYPE_INTR, NULL, 0);
}


static void _start(void) {
    eve_start();

    GPIO_REG(GPIO_INPUT_EN)     |=  (1 << EVE_PIN_INTR);
    GPIO_REG(GPIO_OUTPUT_EN)    &= ~(1 << EVE_PIN_INTR);

    GPIO_REG(GPIO_LOW_IE)       |=  (1 << EVE_PIN_INTR);

    eos_intr_enable(INT_GPIO_BASE + EVE_PIN_INTR);
    _run = 1;
}

static void _stop(void) {
    _run = 0;
    eos_intr_disable(INT_GPIO_BASE + EVE_PIN_INTR);

    GPIO_REG(GPIO_LOW_IE)       &= ~(1 << EVE_PIN_INTR);

    eve_stop();
}

int eos_eve_init(uint8_t wakeup_cause, uint8_t gpio_dir, int touch_calibrate, uint32_t *touch_matrix) {
    int rst = (wakeup_cause == EOS_PWR_WAKE_RST);
    int rv = EVE_OK;

    eve_spi_start();
    if (rst) {
        rv = eve_init(gpio_dir, touch_calibrate, touch_matrix);
    } else {
        eve_active();
    }
    eve_spi_stop();

    if (rv) return EOS_ERR;

    eos_evtq_set_handler(EOS_EVT_UI, handle_evt);
    eos_timer_set_handler(EOS_TIMER_ETYPE_UI, handle_time);
    eos_intr_set(INT_GPIO_BASE + EVE_PIN_INTR, IRQ_PRIORITY_UI, handle_intr);
    eos_intr_disable(INT_GPIO_BASE + EVE_PIN_INTR);

    return EOS_OK;
}

int eos_eve_run(uint8_t wakeup_cause) {
    eve_spi_start();
    _start();
    eve_start_clk();
    eve_spi_stop();

    return EOS_OK;
}

void eos_eve_start(void) {
    eve_spi_start();
    _start();
    eve_spi_stop();
}

void eos_eve_stop(void) {
    eve_spi_start();
    _stop();
    eve_spi_stop();
}