summaryrefslogtreecommitdiff
path: root/fw/esp32/components/eos/app.c
diff options
context:
space:
mode:
Diffstat (limited to 'fw/esp32/components/eos/app.c')
-rw-r--r--fw/esp32/components/eos/app.c243
1 files changed, 88 insertions, 155 deletions
diff --git a/fw/esp32/components/eos/app.c b/fw/esp32/components/eos/app.c
index 8396376..662da17 100644
--- a/fw/esp32/components/eos/app.c
+++ b/fw/esp32/components/eos/app.c
@@ -15,209 +15,142 @@
#include "eos.h"
#include "msgq.h"
+#include "power.h"
#include "app.h"
+#include "net_priv.h"
-#define SPI_GPIO_CTS 26
-#define SPI_GPIO_RTS 27
-#define SPI_GPIO_MOSI 13
-#define SPI_GPIO_MISO 12
-#define SPI_GPIO_SCLK 14
-#define SPI_GPIO_CS 15
+#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 + 8)
+#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 SemaphoreHandle_t mutex;
-static SemaphoreHandle_t semaph;
-static TaskHandle_t app_xchg_task_handle;
-static const char *TAG = "EOS APP";
+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 eos_app_fptr_t app_handler[EOS_APP_MAX_MTYPE];
+static const char *TAG = "EOS APP";
static void bad_handler(unsigned char mtype, unsigned char *buffer, uint16_t len) {
- ESP_LOGE(TAG, "bad handler: %d len: %d", mtype, len);
+ ESP_LOGE(TAG, "bad handler: 0x%.2X len: %d", mtype, len);
}
-// Called after a transaction is queued and ready for pickup by master. We use this to set the handshake line high.
-static void _post_setup_cb(spi_slave_transaction_t *trans) {
- gpio_set_level(SPI_GPIO_CTS, 1);
-}
+static void app_msg_handler(unsigned char mtype, unsigned char *buffer, uint16_t buf_len) {
+ uint8_t idx;
-// Called after transaction is sent/received. We use this to set the handshake line low.
-static void _post_trans_cb(spi_slave_transaction_t *trans) {
- gpio_set_level(SPI_GPIO_CTS, 0);
-}
-
-static void app_xchg_task(void *pvParameters) {
- unsigned char mtype = 0;
- unsigned char *buffer;
- uint16_t len;
- 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);
- esp_err_t ret;
- size_t trans_len;
-
- static spi_slave_transaction_t spi_tr;
-
- //Configuration for the SPI bus
- static spi_bus_config_t spi_bus_cfg = {
- .mosi_io_num = SPI_GPIO_MOSI,
- .miso_io_num = SPI_GPIO_MISO,
- .sclk_io_num = SPI_GPIO_SCLK
- };
-
- //Configuration for the SPI slave interface
- static spi_slave_interface_config_t spi_slave_cfg = {
- .mode = 0,
- .spics_io_num = SPI_GPIO_CS,
- .queue_size = 2,
- .flags = 0,
- .post_setup_cb = _post_setup_cb,
- .post_trans_cb = _post_trans_cb
- };
-
- //Initialize SPI slave interface
- ret = spi_slave_initialize(HSPI_HOST, &spi_bus_cfg, &spi_slave_cfg, 1);
- assert(ret == ESP_OK);
-
- memset(&spi_tr, 0, sizeof(spi_tr));
- spi_tr.tx_buffer = buf_send;
- spi_tr.rx_buffer = buf_recv;
- spi_tr.length = SPI_SIZE_BUF * 8;
-
- while (1) {
- xSemaphoreTake(mutex, portMAX_DELAY);
-
- eos_msgq_pop(&app_send_q, &mtype, &buffer, &len);
- if (mtype) {
- buf_send[0] = mtype;
- buf_send[1] = len >> 8;
- buf_send[2] = len & 0xFF;
- if (buffer) {
- memcpy(buf_send + SPI_SIZE_HDR, buffer, len);
- eos_bufq_push(&app_buf_q, buffer);
- xSemaphoreGive(semaph);
- }
- } else {
- gpio_set_level(SPI_GPIO_RTS, 0);
- buf_send[0] = 0;
- buf_send[1] = 0;
- buf_send[2] = 0;
- len = 0;
- }
-
- xSemaphoreGive(mutex);
-
- buf_recv[0] = 0;
- buf_recv[1] = 0;
- buf_recv[2] = 0;
- spi_slave_transmit(HSPI_HOST, &spi_tr, portMAX_DELAY);
-
- trans_len = spi_tr.trans_len / 8;
- if (trans_len < SPI_SIZE_HDR) continue;
-
- if (len + SPI_SIZE_HDR > trans_len) {
- spi_tr.tx_buffer = buf_send + trans_len;
- spi_tr.rx_buffer = buf_recv + trans_len;
- spi_tr.length = (SPI_SIZE_BUF - trans_len) * 8;
- spi_slave_transmit(HSPI_HOST, &spi_tr, portMAX_DELAY);
- spi_tr.tx_buffer = buf_send;
- spi_tr.rx_buffer = buf_recv;
- spi_tr.length = SPI_SIZE_BUF * 8;
- }
- mtype = buf_recv[0] & ~EOS_APP_MTYPE_FLAG_MASK;
- len = (uint16_t)buf_recv[1] << 8;
- len |= (uint16_t)buf_recv[2] & 0xFF;
- buffer = buf_recv + 3;
-
- if (mtype == 0x00) continue;
-
- if ((mtype <= EOS_APP_MAX_MTYPE) && (len <= EOS_APP_MTU)) {
- app_handler[mtype - 1](mtype, buffer, len);
- } else {
- bad_handler(mtype, buffer, len);
- }
+ 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);
}
- vTaskDelete(NULL);
}
void eos_app_init(void) {
+ SemaphoreHandle_t mutex;
+ SemaphoreHandle_t bufq_mutex;
+ SemaphoreHandle_t bufq_semaph;
int i;
- // Configuration for the handshake lines
- gpio_config_t io_conf = {};
-
- io_conf.intr_type = GPIO_INTR_DISABLE;
- io_conf.mode = GPIO_MODE_OUTPUT;
- io_conf.pull_up_en = 0;
- io_conf.pull_down_en = 0;
- io_conf.pin_bit_mask = ((uint64_t)1 << SPI_GPIO_CTS);
- gpio_config(&io_conf);
- gpio_set_level(SPI_GPIO_CTS, 0);
-
- io_conf.intr_type = GPIO_INTR_DISABLE;
- io_conf.mode = GPIO_MODE_OUTPUT;
- io_conf.pull_up_en = 0;
- io_conf.pull_down_en = 0;
- io_conf.pin_bit_mask = ((uint64_t)1 << SPI_GPIO_RTS);
- gpio_config(&io_conf);
- gpio_set_level(SPI_GPIO_RTS, 0);
-
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++) {
- eos_bufq_push(&app_buf_q, malloc(EOS_APP_SIZE_BUF));
+ 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;
}
- semaph = xSemaphoreCreateCounting(EOS_APP_SIZE_BUFQ, EOS_APP_SIZE_BUFQ);
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);
- xTaskCreate(&app_xchg_task, "app_xchg", EOS_TASK_SSIZE_APP_XCHG, NULL, EOS_TASK_PRIORITY_APP_XCHG, &app_xchg_task_handle);
+ 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");
}
-unsigned char *eos_app_alloc(void) {
- unsigned char *ret;
+void eos_app_run(void) {
+ BaseType_t rv;
- xSemaphoreTake(semaph, portMAX_DELAY);
- xSemaphoreTake(mutex, portMAX_DELAY);
- ret = eos_bufq_pop(&app_buf_q);
- xSemaphoreGive(mutex);
+ 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);
- return ret;
+ ESP_LOGI(TAG, "RUN");
}
-void eos_app_free(unsigned char *buf) {
- xSemaphoreTake(mutex, portMAX_DELAY);
- eos_bufq_push(&app_buf_q, buf);
- xSemaphoreGive(semaph);
- xSemaphoreGive(mutex);
+unsigned char *eos_app_alloc(void) {
+ return _eos_net_alloc(&app_config);
}
-int eos_app_send(unsigned char mtype, unsigned char *buffer, uint16_t len) {
- int rv = EOS_OK;
+void eos_app_free(unsigned char *buf) {
+ _eos_net_free(&app_config, buf);
+}
- xSemaphoreTake(mutex, portMAX_DELAY);
- gpio_set_level(SPI_GPIO_RTS, 1);
- rv = eos_msgq_push(&app_send_q, mtype, buffer, len);
- xSemaphoreGive(mutex);
+int eos_app_send(unsigned char mtype, unsigned char *buffer, uint16_t buf_len) {
+ return _eos_net_send(&app_config, mtype, buffer, buf_len);
+}
- if (rv) eos_app_free(buffer);
+void eos_app_sleep_req(void) {
+ _eos_net_sleep_req(&app_config);
+}
- return rv;
+void eos_app_wake(void) {
+ _eos_net_wake(&app_config);
}
-void eos_app_set_handler(unsigned char mtype, eos_app_fptr_t handler) {
+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;
}