summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/dev/lcd.c
blob: 4557fb0f4df9c7e8ba649c6ce884ec490c641c9a (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
#include <stdlib.h>
#include <stdint.h>

#include "platform.h"

#include "eos.h"

#include "board.h"

#include "soc/timer.h"
#include "soc/i2s.h"
#include "eve/eve.h"

#include "eve.h"
#include "gt911.h"
#include "ili9806e.h"

#include "lcd.h"

static int lcd_enable(void) {
    int rv;

    rv = eos_spi_select(EOS_SPI_DEV_EVE);
    if (rv) return rv;

    eve_gpio_set(EVE_GPIO_LCD_EN, 1);
    eos_spi_deselect();

    return EOS_OK;
}

static int lcd_disable(void) {
    int rv;

    rv = eos_spi_select(EOS_SPI_DEV_EVE);
    if (rv) return rv;

    eve_gpio_set(EVE_GPIO_LCD_EN, 0);
    eos_spi_deselect();

    return EOS_OK;
}

static int lcd_select(void) {
    int rv;

    GPIO_REG(GPIO_OUTPUT_XOR)   |=  (1 << SPI_CSPIN_LCD);
    GPIO_REG(GPIO_OUTPUT_VAL)   |=  (1 << SPI_CSPIN_LCD);
    GPIO_REG(GPIO_OUTPUT_EN)    |=  (1 << SPI_CSPIN_LCD);

    rv = eos_spi_select(EOS_SPI_DEV_LCD);
    if (rv) {
        GPIO_REG(GPIO_OUTPUT_EN)    &= ~(1 << SPI_CSPIN_LCD);
        GPIO_REG(GPIO_OUTPUT_XOR)   &= ~(1 << SPI_CSPIN_LCD);
        GPIO_REG(GPIO_OUTPUT_VAL)   &= ~(1 << SPI_CSPIN_LCD);
        return rv;
    }

    return EOS_OK;
}

static void lcd_deselect(void) {
    eos_spi_deselect();
    GPIO_REG(GPIO_OUTPUT_EN)    &= ~(1 << SPI_CSPIN_LCD);
    GPIO_REG(GPIO_OUTPUT_XOR)   &= ~(1 << SPI_CSPIN_LCD);
    GPIO_REG(GPIO_OUTPUT_VAL)   &= ~(1 << SPI_CSPIN_LCD);
}

static int lcd_init(void) {
    int rv;

    rv = lcd_enable();
    if (rv) return rv;

    eos_time_sleep(200);

    rv = lcd_select();
    if (rv) {
        lcd_disable();
        return rv;
    }

    rv = eos_ili9806e_init();
    if (rv == EOS_ERR_ABSENT) eve_lcd_absent();
    lcd_deselect();

    if (rv) lcd_disable();
    return rv;
}

static int lcd_sleep(void) {
    int rv;

    rv = lcd_select();
    if (rv) return rv;

    eos_ili9806e_sleep();
    lcd_deselect();

    rv = lcd_disable();
    if (rv) return rv;

    return EOS_OK;
}

int eos_lcd_init(uint8_t wakeup_cause) {
    int rv;

    rv = lcd_init();
    if (rv) return rv;

    rv = eos_gt911_init(wakeup_cause);
    if (rv) return rv;

    return EOS_OK;
}

int eos_lcd_sleep(void) {
    int rv;

    if (eos_i2s_running()) return EOS_ERR_BUSY;

    /* There is a problem with GT911 and sleep */
    // eos_gt911_sleep();

    rv = lcd_sleep();
    if (rv) return rv;

    return EOS_OK;
}

int eos_lcd_wake(void) {
    int rv;

    if (eos_i2s_running()) return EOS_ERR_BUSY;

    rv = lcd_init();
    if (rv) return rv;

    /* There is a problem with GT911 and sleep */
    // eos_gt911_wake();

    return EOS_OK;
}