summaryrefslogtreecommitdiff
path: root/fw/esp32/components/eos/app.c
blob: ab04c2c9f616a69fac38ddad75398fd70c0bb312 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>

#include <esp_system.h>
#include <esp_log.h>
#include <esp_err.h>
#include <esp_heap_caps.h>
#include <driver/gpio.h>
#include <driver/spi_slave.h>

#include "eos.h"
#include "msgq.h"
#include "power.h"
#include "net.h"
#include "app.h"
#include "net_priv.h"

#define SPI_GPIO_RTS        47
#define SPI_GPIO_CTS        9
#define SPI_GPIO_MOSI       11
#define SPI_GPIO_MISO       13
#define SPI_GPIO_SCLK       12
#define SPI_GPIO_CS         10

#define SPI_HOST            SPI2_HOST

#define APP_SIZE_BUFQ       16
#define APP_SIZE_SNDQ       16

static EOSBufQ app_buf_q;
static unsigned char *app_bufq_array[APP_SIZE_BUFQ];

static EOSMsgQ app_send_q;
static EOSMsgItem app_sndq_array[APP_SIZE_SNDQ];

static NETConfig app_config;
static eos_net_handler_t app_handler[EOS_APP_MAX_MTYPE];

static spi_bus_config_t app_spi_bus_cfg;
static spi_slave_interface_config_t app_spi_iface_cfg;

static const char *TAG = "EOS APP";

static void bad_handler(unsigned char mtype, EOSMessage *msg, uint16_t len) {
    ESP_LOGE(TAG, "BAD HANDLER: 0x%.2X LEN: %d", mtype, len);
}

static void IRAM_ATTR app_bridge(unsigned char mtype, EOSMessage *msg, uint16_t len) {
    EOSMessage _msg;
    int rv;

    eos_net_alloc(&_msg);
    if (len > _msg.size) {
        eos_net_free(&_msg);
        return;
    }
    memcpy(_msg.buffer, msg->buffer, len);
    rv = eos_net_send(mtype & EOS_NET_MTYPE_MASK, &_msg, len);
    if (rv) ESP_LOGE(TAG, "BRIDGE ERR: %d", rv);
}

static void IRAM_ATTR app_msg_handler(unsigned char mtype, EOSMessage *msg, uint16_t len) {
    uint8_t idx;

    idx = mtype & EOS_NET_MTYPE_FLAG_BRIDGE ? EOS_NET_MTYPE_BRIDGE : mtype & EOS_NET_MTYPE_MASK;
    if ((idx < EOS_APP_MAX_MTYPE) && (len <= msg->size)) {
        app_handler[idx](mtype, msg, len);
    } else {
        bad_handler(mtype, msg, len);
    }
}

void eos_app_init(void) {
    SemaphoreHandle_t mutex;
    SemaphoreHandle_t bufq_mutex;
    SemaphoreHandle_t bufq_semaph;
    int i;

    eos_msgq_init(&app_send_q, app_sndq_array, APP_SIZE_SNDQ);
    eos_bufq_init(&app_buf_q, app_bufq_array, APP_SIZE_BUFQ);
    for (i=0; i<APP_SIZE_BUFQ; i++) {
        unsigned char *buffer;

        buffer = malloc(EOS_NET_MTU);
        assert(buffer != NULL);
        eos_bufq_push(&app_buf_q, buffer);
    }

    for (i=0; i<EOS_APP_MAX_MTYPE; i++) {
        app_handler[i] = bad_handler;
    }

    mutex = xSemaphoreCreateBinary();
    assert(mutex != NULL);
    bufq_mutex = xSemaphoreCreateBinary();
    assert(bufq_mutex != NULL);
    bufq_semaph = xSemaphoreCreateCounting(APP_SIZE_BUFQ, APP_SIZE_BUFQ);
    assert(bufq_semaph != NULL);

    xSemaphoreGive(mutex);
    xSemaphoreGive(bufq_mutex);

    app_config.sleep     = esp_reset_reason() == ESP_RST_DEEPSLEEP ? 1 : 0;
    app_config.sleep_req = 0;
    app_config.present   = 1;
    app_config.dev       = NET_DEV_APP;
    app_config.gpio_rts  = SPI_GPIO_RTS;
    app_config.gpio_cts  = SPI_GPIO_CTS;
    app_config.spi_host  = SPI_HOST;
    app_config.spi_bus_cfg   = &app_spi_bus_cfg;
    app_config.spi_iface_cfg = &app_spi_iface_cfg;
    app_config.mutex         = mutex;
    app_config.bufq_mutex    = bufq_mutex;
    app_config.bufq_semaph   = bufq_semaph;
    app_config.buf_q         = &app_buf_q;
    app_config.send_q        = &app_send_q;
    app_config.handler       = app_msg_handler;

    /* Configuration for the SPI bus */
    app_spi_bus_cfg.mosi_io_num = SPI_GPIO_MOSI;
    app_spi_bus_cfg.miso_io_num = SPI_GPIO_MISO;
    app_spi_bus_cfg.sclk_io_num = SPI_GPIO_SCLK;
    app_spi_bus_cfg.intr_flags = ESP_INTR_FLAG_IRAM;

    /* Configuration for the SPI slave interface */
    app_spi_iface_cfg.mode = 0;
    app_spi_iface_cfg.spics_io_num = SPI_GPIO_CS;
    app_spi_iface_cfg.queue_size = 2;
    app_spi_iface_cfg.flags = 0;

    _eos_net_init_gpio(&app_config);
    eos_app_set_handler(EOS_NET_MTYPE_BRIDGE, app_bridge);

    ESP_LOGI(TAG, "INIT");
}

void eos_app_run(void) {
    BaseType_t rv;

    rv = xTaskCreate(&eos_net_xchg_task, "app_xchg", EOS_TASK_SSIZE_APP, &app_config, EOS_TASK_PRIORITY_APP, &app_config.xchg_task_handle);
    assert(rv == pdPASS);

    ESP_LOGI(TAG, "RUN");
}

void eos_app_alloc(EOSMessage *msg) {
    _eos_net_alloc(&app_config, msg);
}

void eos_app_free(EOSMessage *msg) {
    _eos_net_free(&app_config, msg);
}

int eos_app_send(unsigned char mtype, EOSMessage *msg, uint16_t len) {
    return _eos_net_send(&app_config, mtype, msg, len);
}

void eos_app_sleep_req(void) {
    _eos_net_sleep_req(&app_config);
}

void eos_app_wake(void) {
    _eos_net_wake(&app_config);
}

void eos_app_deep_sleep(void) {
    _eos_net_deep_sleep(&app_config);
}

void eos_app_deep_wake(void) {
    _eos_net_deep_wake(&app_config);
}

void eos_app_set_handler(unsigned char mtype, eos_net_handler_t handler) {
    if (handler == NULL) handler = bad_handler;
    if (mtype < EOS_APP_MAX_MTYPE) app_handler[mtype] = handler;
}