summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/dev/eve.c
blob: dd72b0f2da4b45c42b767470edcc67e7fcf49f86 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdlib.h>
#include <stdio.h>

#include "platform.h"

#include "eos.h"
#include "event.h"

#include "board.h"

#include "soc/interrupt.h"
#include "soc/pwr.h"
#include "soc/i2s.h"

#include "eve/eve.h"
#include "eve/eve_touch_engine.h"

#include "eve.h"

#define EVE_POLL_INTERVAL   2

static void handle_time(unsigned char type) {
    eve_select();
    eve_handle_time();
    eve_deselect();
}

static void handle_poll(unsigned char type) {
    if (eos_eve_intr_enabled()) return;

    eve_select();
    eve_handle_intr();
    eve_deselect();
}

static void handle_intr_evt(unsigned char type, unsigned char *buffer, uint16_t len) {
    if (!eos_eve_intr_enabled()) return;

    eve_select();
    eve_handle_intr();
    eve_deselect();

    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_EVE | EVE_ETYPE_INTR, NULL, 0);
}

int eos_eve_init(uint8_t wakeup_cause) {
    int rst = (wakeup_cause == EOS_PWR_WAKE_RST);
    int rv = EVE_OK;

    eve_select();
    if (rst) {
        rv = eve_init();
        if (!rv) {
            eve_gpio_set_dir(EVE_GPIO_DIR);
            eve_touch_init_engine();
        }
    } else {
        eve_activate();
    }
    eve_deselect();

    if (rv) return EOS_ERR;

    eve_touch_init();

    eos_evtq_set_handler(EOS_EVT_EVE, handle_intr_evt);
    eos_timer_set_handler(EOS_TIMER_ETYPE_UI, handle_time);
    eos_intr_set_handler(INT_GPIO_BASE + EVE_PIN_INTR, handle_intr);
    eos_intr_set_priority(INT_GPIO_BASE + EVE_PIN_INTR, IRQ_PRIORITY_EVE);

    return EOS_OK;
}

void eos_eve_calibrate(void) {
    uint32_t matrix[6];
    int r;

    eve_select();

    eve_brightness(0x40);
    eve_touch_set_extended(0);

    eve_cmd(CMD_TEXT, "hhhhs", EVE_HSIZE/2, EVE_VSIZE/2, 27, EVE_OPT_CENTER, "Please tap on the dot.");
    eve_cmd(CMD_CALIBRATE, "w", 0);
    eve_cmd_exec(0);

    do {
        r = eve_cmd_done();
        if (r < 0) break;
        eve_deselect();
        eos_evtq_exec();
        eve_select();
    } while (!r);

    eve_touch_set_extended(1);
    eve_brightness(0);

    eve_touch_get_matrix(matrix);
    eve_deselect();

    printf("TOUCH MATRIX:\n");
    printf("uint32_t touch_matrix[6] = {0x%x,0x%x,0x%x,0x%x,0x%x,0x%x}\n", matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
}

void eos_eve_set_tmatrix(const uint32_t *matrix) {
    eve_select();
    eve_touch_set_matrix(matrix);
    eve_deselect();
}

int eos_eve_run(uint8_t wakeup_cause) {
    eve_select();
    eve_touch_start();
    eve_intr_enable();
    eve_clk_start();
    eve_deselect();

    eos_eve_intr_enable();
    return EOS_OK;
}

void eos_eve_intr_enable(void) {
    GPIO_REG(GPIO_INPUT_EN)     |=  (1 << EVE_PIN_INTR);
    GPIO_REG(GPIO_LOW_IE)       |=  (1 << EVE_PIN_INTR);

    eos_intr_enable(INT_GPIO_BASE + EVE_PIN_INTR);
    eos_timer_set_handler(EOS_TIMER_ETYPE_EVE, NULL);
}

void eos_eve_intr_disable(void) {
    eos_intr_disable(INT_GPIO_BASE + EVE_PIN_INTR);

    GPIO_REG(GPIO_LOW_IE)       &= ~(1 << EVE_PIN_INTR);
    GPIO_REG(GPIO_INPUT_EN)     &= ~(1 << EVE_PIN_INTR);
    eos_timer_set_handler(EOS_TIMER_ETYPE_EVE, handle_poll);
}

int eos_eve_intr_enabled(void) {
    return !!(GPIO_REG(GPIO_INPUT_EN) & (1 << EVE_PIN_INTR));
}

void eos_eve_poll(void) {
    if (eos_eve_intr_enabled()) return;
    if (eos_timer_get(EOS_TIMER_ETYPE_EVE) != EOS_TIMER_NONE) return;

    eos_timer_set(EOS_TIMER_ETYPE_EVE, EVE_POLL_INTERVAL);
}