summaryrefslogtreecommitdiff
path: root/fw/esp32/components/eos/app.c
blob: 662da17e17c963a98be099052a6630900e2f2e64 (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
#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 "app.h"
#include "net_priv.h"

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

#define SPI_SIZE_BUF        (EOS_APP_SIZE_BUF + 4)
#define SPI_SIZE_HDR        3

#define SPI_HOST            SPI2_HOST

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

static EOSMsgQ app_send_q;
static EOSMsgItem app_sndq_array[EOS_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 spi_slave_transaction_t app_spi_tr_cfg;

static const char *TAG = "EOS APP";

static void bad_handler(unsigned char mtype, unsigned char *buffer, uint16_t len) {
    ESP_LOGE(TAG, "bad handler: 0x%.2X len: %d", mtype, len);
}

static void app_msg_handler(unsigned char mtype, unsigned char *buffer, uint16_t buf_len) {
    uint8_t idx;

    idx = mtype & EOS_NET_MTYPE_MASK;
    if (idx && (idx <= EOS_APP_MAX_MTYPE) && (buf_len <= EOS_APP_MTU)) {
        app_handler[idx - 1](mtype, buffer, buf_len);
    } else {
        bad_handler(mtype, buffer, buf_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, EOS_APP_SIZE_SNDQ);
    eos_bufq_init(&app_buf_q, app_bufq_array, EOS_APP_SIZE_BUFQ);
    for (i=0; i<EOS_APP_SIZE_BUFQ; i++) {
        unsigned char *buffer;

        buffer = malloc(EOS_APP_SIZE_BUF);
        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(EOS_APP_SIZE_BUFQ, EOS_APP_SIZE_BUFQ);
    assert(bufq_semaph != NULL);

    xSemaphoreGive(mutex);
    xSemaphoreGive(bufq_mutex);

    app_config.sleep     = 0;
    app_config.sleep_req = 0;
    app_config.present   = 0;
    app_config.dev       = NET_DEV_APP;
    app_config.gpio_mosi = SPI_GPIO_MOSI;
    app_config.gpio_miso = SPI_GPIO_MISO;
    app_config.gpio_sclk = SPI_GPIO_SCLK;
    app_config.gpio_cs   = SPI_GPIO_CS;
    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.spi_tr_cfg    = &app_spi_tr_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.msg_handler   = app_msg_handler;

    _eos_net_init_gpio(&app_config);

    if (esp_reset_reason() == ESP_RST_DEEPSLEEP) {
        gpio_hold_dis(app_config.gpio_cts);
    }

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

unsigned char *eos_app_alloc(void) {
    return _eos_net_alloc(&app_config);
}

void eos_app_free(unsigned char *buf) {
    _eos_net_free(&app_config, buf);
}

int eos_app_send(unsigned char mtype, unsigned char *buffer, uint16_t buf_len) {
    return _eos_net_send(&app_config, mtype, buffer, buf_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_set_handler(unsigned char mtype, eos_net_handler_t handler) {
    if (handler == NULL) handler = bad_handler;
    if (mtype && (mtype <= EOS_APP_MAX_MTYPE)) app_handler[mtype - 1] = handler;
}