#include #include #include #include #include #include #include #include #include #include #include #include #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; ibufq_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; }