summaryrefslogtreecommitdiff
path: root/fw/esp32/components/eos/net.c
blob: 0ba2a2d49b54559353397cee76239446c1edfc87 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#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.h"
#include "net_priv.h"

#define SPI_GPIO_RTS        35
#define SPI_GPIO_CTS        3
#define SPI_GPIO_MOSI       40
#define SPI_GPIO_MISO       39
#define SPI_GPIO_SCLK       38
#define SPI_GPIO_CS         8

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

#define SPI_HOST            SPI3_HOST

static EOSBufQ net_buf_q;
static unsigned char *net_bufq_array[EOS_NET_SIZE_BUFQ];

static EOSMsgQ net_send_q;
static EOSMsgItem net_sndq_array[EOS_NET_SIZE_SNDQ];

static NETConfig net_config;
static eos_net_handler_t net_handler[EOS_NET_MAX_MTYPE];

static spi_bus_config_t net_spi_bus_cfg;
static spi_slave_interface_config_t net_spi_iface_cfg;
static spi_slave_transaction_t net_spi_tr_cfg;

static const char *TAG = "EOS NET";

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

static void net_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_NET_MAX_MTYPE) && (buf_len <= EOS_NET_MTU)) {
        net_handler[idx - 1](mtype, buffer, buf_len);
    } else {
        bad_handler(mtype, buffer, buf_len);
    }
}

/* Called after a transaction is queued and ready for pickup by master */
static void _post_setup_cb(spi_slave_transaction_t *trans) {
    NETConfig *config = trans->user;

    gpio_set_level(config->gpio_cts, 0);
}

/* Called after transaction is sent/received */
static void _post_trans_cb(spi_slave_transaction_t *trans) {
    NETConfig *config = trans->user;

    gpio_set_level(config->gpio_cts, 1);
}

static void net_goto_sleep(NETConfig *config) {
    esp_err_t ret;
    uint8_t msgq_len;


    xSemaphoreTake(config->mutex, portMAX_DELAY);
    msgq_len = eos_msgq_len(config->send_q);
    if (msgq_len) {
        gpio_set_level(config->gpio_rts, 0);
    } else {
        gpio_set_level(config->gpio_rts, 1);
    }
    xSemaphoreGive(config->mutex);

    ret = spi_slave_free(config->spi_host);
    assert(ret == ESP_OK);

    /* for deep sleep */
    gpio_hold_en(config->gpio_cts);

    eos_power_sleep_rdy(config->dev);
    vTaskSuspend(NULL);

    gpio_hold_dis(config->gpio_cts);

    ret = spi_slave_initialize(config->spi_host, config->spi_bus_cfg, config->spi_iface_cfg, SPI_DMA_CH_AUTO);
    assert(ret == ESP_OK);

    xSemaphoreTake(config->mutex, portMAX_DELAY);
    config->sleep = 0;
    msgq_len = eos_msgq_len(config->send_q);
    if (msgq_len) {
        gpio_set_level(config->gpio_rts, 0);
    } else {
        gpio_set_level(config->gpio_rts, 1);
    }
    xSemaphoreGive(config->mutex);
}

void eos_net_xchg_task(void *param) {
    NETConfig *config = param;
    int present, skip_msg = 0, sleep_msg = 0;
    unsigned char mtype = 0;
    unsigned char mtype_flags = 0;
    unsigned char *buffer;
    unsigned char *buf_send = heap_caps_malloc(SPI_SIZE_BUF, MALLOC_CAP_DMA);
    unsigned char *buf_recv = heap_caps_malloc(SPI_SIZE_BUF, MALLOC_CAP_DMA);
    uint16_t buf_len;
    size_t trans_len;
    spi_bus_config_t *spi_bus_cfg = config->spi_bus_cfg;
    spi_slave_interface_config_t *spi_iface_cfg = config->spi_iface_cfg;
    spi_slave_transaction_t *spi_tr_cfg = config->spi_tr_cfg;
    esp_err_t ret;

    assert(buf_send != NULL);
    assert(buf_recv != NULL);

    /* Configuration for the SPI bus */
    spi_bus_cfg->mosi_io_num = config->gpio_mosi;
    spi_bus_cfg->miso_io_num = config->gpio_miso;
    spi_bus_cfg->sclk_io_num = config->gpio_sclk;

    /* Configuration for the SPI slave interface */
    spi_iface_cfg->mode = 0;
    spi_iface_cfg->spics_io_num = config->gpio_cs;
    spi_iface_cfg->queue_size = 2;
    spi_iface_cfg->flags = 0;
    spi_iface_cfg->post_setup_cb = _post_setup_cb;
    spi_iface_cfg->post_trans_cb = _post_trans_cb;

    spi_tr_cfg->tx_buffer = buf_send;
    spi_tr_cfg->rx_buffer = buf_recv;
    spi_tr_cfg->length = SPI_SIZE_BUF * 8;
    spi_tr_cfg->user = config;

    xSemaphoreTake(config->mutex, portMAX_DELAY);
    present = config->present;
    xSemaphoreGive(config->mutex);
    if (!present) {
        vTaskSuspend(NULL);
    }

    /* Initialize SPI slave interface */
    ret = spi_slave_initialize(config->spi_host, spi_bus_cfg, spi_iface_cfg, SPI_DMA_CH_AUTO);
    assert(ret == ESP_OK);

    while (1) {
        if (!skip_msg) {
            xSemaphoreTake(config->mutex, portMAX_DELAY);

            eos_msgq_pop(config->send_q, &mtype, &buffer, &buf_len);
            if (mtype) {
                buf_send[0] = mtype;
                buf_send[1] = buf_len >> 8;
                buf_send[2] = buf_len & 0xFF;
                if (buffer) {
                    memcpy(buf_send + SPI_SIZE_HDR, buffer, buf_len);
                    xSemaphoreTake(config->bufq_mutex, portMAX_DELAY);
                    eos_bufq_push(config->buf_q, buffer);
                    xSemaphoreGive(config->bufq_mutex);
                    xSemaphoreGive(config->bufq_semaph);
                }
            } else if (!sleep_msg && config->sleep_req) {
                buf_send[0] = EOS_NET_MTYPE_SLEEP;
                buf_send[1] = 0;
                buf_send[2] = 0;
                sleep_msg = 1;
                config->sleep_req = 0;
                if (config->dev == NET_DEV_NET) config->sleep = 1;
            } else {
                gpio_set_level(config->gpio_rts, 1);
                buf_send[0] = 0;
                buf_send[1] = 0;
                buf_send[2] = 0;
            }

            xSemaphoreGive(config->mutex);
        }
        skip_msg = 0;

        buf_recv[0] = 0;
        buf_recv[1] = 0;
        buf_recv[2] = 0;
        ret = spi_slave_transmit(config->spi_host, spi_tr_cfg, portMAX_DELAY);
        assert(ret == ESP_OK);

        trans_len = spi_tr_cfg->trans_len / 8;

        ESP_LOGI(TAG, "RECV:0x%.2X DEV:0x%.8lX LEN:%d", buf_recv[0], config->dev, trans_len);

        if (sleep_msg && (config->dev == NET_DEV_NET)) {
            net_goto_sleep(config);
            sleep_msg = 0;
            continue;
        }

        if (trans_len < 1) {
            ESP_LOGE(TAG, "RECV LEN:%d", trans_len);
            continue;
        }

        /* SPI reset */
        if ((trans_len == 1) && (buf_recv[0] == 0)) {
            if (buf_send[0]) skip_msg = 1;
            continue;
        }

        mtype = buf_recv[0] & EOS_NET_MTYPE_MASK;
        mtype_flags = buf_recv[0] & ~EOS_NET_MTYPE_MASK;
        if (buf_send[0] && (mtype_flags & EOS_NET_MTYPE_FLAG_ONEW)) {
            skip_msg = 1;
        }

        if (buf_send[0] && (trans_len < buf_len + SPI_SIZE_HDR) && !skip_msg) {
            spi_tr_cfg->tx_buffer = buf_send + trans_len;
            spi_tr_cfg->rx_buffer = buf_recv + trans_len;
            spi_tr_cfg->length = (SPI_SIZE_BUF - trans_len) * 8;
            ret = spi_slave_transmit(config->spi_host, spi_tr_cfg, portMAX_DELAY);
            assert(ret == ESP_OK);
            trans_len += spi_tr_cfg->trans_len / 8;
            spi_tr_cfg->tx_buffer = buf_send;
            spi_tr_cfg->rx_buffer = buf_recv;
            spi_tr_cfg->length = SPI_SIZE_BUF * 8;
        }

        if (mtype == 0) continue;

        if (mtype == EOS_NET_MTYPE_SLEEP) {
            uint8_t mode;

            mode = EOS_PWR_SMODE_LIGHT;

            switch (config->dev) {
                case NET_DEV_NET: {
                    eos_power_sleep_req(mode, EOS_PWR_DEV_ALL);
                    break;
                }
                case NET_DEV_APP: {
                    uint8_t msgq_len;

                    xSemaphoreTake(config->mutex, portMAX_DELAY);
                    msgq_len = eos_msgq_len(config->send_q);
                    if (msgq_len == 0) {
                        config->sleep = 1;
                        config->sleep_req = 0;
                    }
                    xSemaphoreGive(config->mutex);
                    if (msgq_len == 0) {
                        net_goto_sleep(config);
                        sleep_msg = 0;
                    }
                    break;
                }
            }
            continue;
        }

        if (trans_len < SPI_SIZE_HDR) {
            ESP_LOGE(TAG, "RECV LEN:%d", trans_len);
            continue;
        }
        buf_len  = (uint16_t)buf_recv[1] << 8;
        buf_len |= (uint16_t)buf_recv[2] & 0xFF;
        buffer = buf_recv + SPI_SIZE_HDR;
        config->msg_handler(buf_recv[0], buffer, buf_len);

        if (mtype_flags & EOS_NET_MTYPE_FLAG_REPL) {
            spi_tr_cfg->tx_buffer = buf_recv;
            spi_tr_cfg->rx_buffer = NULL;
            ret = spi_slave_transmit(config->spi_host, spi_tr_cfg, portMAX_DELAY);
            assert(ret == ESP_OK);
            spi_tr_cfg->tx_buffer = buf_send;
            spi_tr_cfg->rx_buffer = buf_recv;
        }
    }

    vTaskDelete(NULL);
}

void _eos_net_init_gpio(NETConfig *config) {
    /* Configuration for the handshake lines */
    gpio_config_t io_conf = {};

    gpio_set_level(config->gpio_rts, 1);
    gpio_set_level(config->gpio_cts, 1);
    io_conf.intr_type = GPIO_INTR_DISABLE;
    io_conf.mode = GPIO_MODE_OUTPUT;
    io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
    io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    io_conf.pin_bit_mask = BIT64(config->gpio_rts) | BIT64(config->gpio_cts);
    gpio_config(&io_conf);
}

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

    eos_msgq_init(&net_send_q, net_sndq_array, EOS_NET_SIZE_SNDQ);
    eos_bufq_init(&net_buf_q, net_bufq_array, EOS_NET_SIZE_BUFQ);
    for (i=0; i<EOS_NET_SIZE_BUFQ; i++) {
        unsigned char *buffer;

        buffer = malloc(EOS_NET_SIZE_BUF);
        assert(buffer != NULL);
        eos_bufq_push(&net_buf_q, buffer);
    }

    for (i=0; i<EOS_NET_MAX_MTYPE; i++) {
        net_handler[i] = bad_handler;
    }

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

    xSemaphoreGive(mutex);
    xSemaphoreGive(bufq_mutex);

    net_config.sleep     = 0;
    net_config.sleep_req = 0;
    net_config.present   = 1;
    net_config.dev       = NET_DEV_NET;
    net_config.gpio_mosi = SPI_GPIO_MOSI;
    net_config.gpio_miso = SPI_GPIO_MISO;
    net_config.gpio_sclk = SPI_GPIO_SCLK;
    net_config.gpio_cs   = SPI_GPIO_CS;
    net_config.gpio_rts  = SPI_GPIO_RTS;
    net_config.gpio_cts  = SPI_GPIO_CTS;
    net_config.spi_host  = SPI_HOST;
    net_config.spi_bus_cfg   = &net_spi_bus_cfg;
    net_config.spi_iface_cfg = &net_spi_iface_cfg;
    net_config.spi_tr_cfg    = &net_spi_tr_cfg;
    net_config.mutex         = mutex;
    net_config.bufq_mutex    = bufq_mutex;
    net_config.bufq_semaph   = bufq_semaph;
    net_config.buf_q         = &net_buf_q;
    net_config.send_q        = &net_send_q;
    net_config.msg_handler   = net_msg_handler;

    _eos_net_init_gpio(&net_config);

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

    ESP_LOGI(TAG, "INIT");
}

void eos_net_run(void) {
    BaseType_t rv;

    rv = xTaskCreate(&eos_net_xchg_task, "net_xchg", EOS_TASK_SSIZE_NET, &net_config, EOS_TASK_PRIORITY_NET, &net_config.xchg_task_handle);
    assert(rv == pdPASS);

    ESP_LOGI(TAG, "RUN");
}

unsigned char *_eos_net_alloc(NETConfig *config) {
    unsigned char *ret;

    xSemaphoreTake(config->bufq_semaph, portMAX_DELAY);
    xSemaphoreTake(config->bufq_mutex, portMAX_DELAY);
    ret = eos_bufq_pop(config->buf_q);
    xSemaphoreGive(config->bufq_mutex);

    return ret;
}

void _eos_net_free(NETConfig *config, unsigned char *buf) {
    xSemaphoreTake(config->bufq_mutex, portMAX_DELAY);
    eos_bufq_push(config->buf_q, buf);
    xSemaphoreGive(config->bufq_semaph);
    xSemaphoreGive(config->bufq_mutex);
}

unsigned char *eos_net_alloc(void) {
    return _eos_net_alloc(&net_config);
}

void eos_net_free(unsigned char *buf) {
    _eos_net_free(&net_config, buf);
}

int _eos_net_send(NETConfig *config, unsigned char mtype, unsigned char *buffer, uint16_t buf_len) {
    int rv = EOS_OK;
    int sleep;

    xSemaphoreTake(config->mutex, portMAX_DELAY);
    sleep = config->sleep;
    rv = eos_msgq_push(config->send_q, mtype, buffer, buf_len);
    if (!rv && !sleep) gpio_set_level(config->gpio_rts, 0);
    xSemaphoreGive(config->mutex);

    if (!rv && sleep) eos_power_wake(config->dev);
    if (rv) _eos_net_free(config, buffer);

    return rv;
}

int eos_net_send(unsigned char mtype, unsigned char *buffer, uint16_t buf_len) {
    return _eos_net_send(&net_config, mtype, buffer, buf_len);
}

void eos_net_reply(unsigned char mtype, unsigned char *buffer, uint16_t buf_len) {
    buffer -= SPI_SIZE_HDR;
    buffer[0] = mtype;
    buffer[1] = buf_len >> 8;
    buffer[2] = buf_len & 0xFF;
}

void _eos_net_sleep_req(NETConfig *config) {
    int sleep, present;

    xSemaphoreTake(config->mutex, portMAX_DELAY);
    sleep = config->sleep;
    present = config->present;
    if (!sleep && present) {
        config->sleep_req = 1;
        gpio_set_level(config->gpio_rts, 0);
    }
    xSemaphoreGive(config->mutex);

    if (!present) eos_power_sleep_rdy(config->dev);
}

void _eos_net_wake(NETConfig *config) {
    int sleep, present;

    xSemaphoreTake(config->mutex, portMAX_DELAY);
    present = config->present;
    xSemaphoreGive(config->mutex);

    if (!present) return;
    do {
        vTaskResume(config->xchg_task_handle);
        vTaskDelay(10 / portTICK_PERIOD_MS);
        xSemaphoreTake(config->mutex, portMAX_DELAY);
        sleep = config->sleep;
        xSemaphoreGive(config->mutex);
    } while (sleep);
}

void eos_net_sleep_req(void) {
    _eos_net_sleep_req(&net_config);
}

void eos_net_wake(void) {
    _eos_net_wake(&net_config);
}

void eos_net_set_handler(unsigned char mtype, eos_net_handler_t handler) {
    if (handler == NULL) handler = bad_handler;
    if (mtype && (mtype <= EOS_NET_MAX_MTYPE)) net_handler[mtype - 1] = handler;
}