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.c102
1 files changed, 64 insertions, 38 deletions
diff --git a/fw/esp32/components/eos/app.c b/fw/esp32/components/eos/app.c
index 662da17..ab04c2c 100644
--- a/fw/esp32/components/eos/app.c
+++ b/fw/esp32/components/eos/app.c
@@ -16,48 +16,62 @@
#include "eos.h"
#include "msgq.h"
#include "power.h"
+#include "net.h"
#include "app.h"
#include "net_priv.h"
-#define SPI_GPIO_CTS 9
#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_SIZE_BUF (EOS_APP_SIZE_BUF + 4)
-#define SPI_SIZE_HDR 3
-
#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[EOS_APP_SIZE_BUFQ];
+static unsigned char *app_bufq_array[APP_SIZE_BUFQ];
static EOSMsgQ app_send_q;
-static EOSMsgItem app_sndq_array[EOS_APP_SIZE_SNDQ];
+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 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 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 app_msg_handler(unsigned char mtype, unsigned char *buffer, uint16_t buf_len) {
+static void IRAM_ATTR app_msg_handler(unsigned char mtype, EOSMessage *msg, uint16_t 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);
+ 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, buffer, buf_len);
+ bad_handler(mtype, msg, len);
}
}
@@ -67,12 +81,12 @@ void eos_app_init(void) {
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++) {
+ 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_APP_SIZE_BUF);
+ buffer = malloc(EOS_NET_MTU);
assert(buffer != NULL);
eos_bufq_push(&app_buf_q, buffer);
}
@@ -85,38 +99,42 @@ void eos_app_init(void) {
assert(mutex != NULL);
bufq_mutex = xSemaphoreCreateBinary();
assert(bufq_mutex != NULL);
- bufq_semaph = xSemaphoreCreateCounting(EOS_APP_SIZE_BUFQ, EOS_APP_SIZE_BUFQ);
+ bufq_semaph = xSemaphoreCreateCounting(APP_SIZE_BUFQ, APP_SIZE_BUFQ);
assert(bufq_semaph != NULL);
xSemaphoreGive(mutex);
xSemaphoreGive(bufq_mutex);
- app_config.sleep = 0;
+ app_config.sleep = esp_reset_reason() == ESP_RST_DEEPSLEEP ? 1 : 0;
app_config.sleep_req = 0;
- app_config.present = 0;
+ app_config.present = 1;
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;
+ app_config.handler = app_msg_handler;
- _eos_net_init_gpio(&app_config);
+ /* 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;
- if (esp_reset_reason() == ESP_RST_DEEPSLEEP) {
- gpio_hold_dis(app_config.gpio_cts);
- }
+ /* 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");
}
@@ -130,16 +148,16 @@ void eos_app_run(void) {
ESP_LOGI(TAG, "RUN");
}
-unsigned char *eos_app_alloc(void) {
- return _eos_net_alloc(&app_config);
+void eos_app_alloc(EOSMessage *msg) {
+ _eos_net_alloc(&app_config, msg);
}
-void eos_app_free(unsigned char *buf) {
- _eos_net_free(&app_config, buf);
+void eos_app_free(EOSMessage *msg) {
+ _eos_net_free(&app_config, msg);
}
-int eos_app_send(unsigned char mtype, unsigned char *buffer, uint16_t buf_len) {
- return _eos_net_send(&app_config, mtype, buffer, buf_len);
+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) {
@@ -150,7 +168,15 @@ 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 && (mtype <= EOS_APP_MAX_MTYPE)) app_handler[mtype - 1] = handler;
+ if (mtype < EOS_APP_MAX_MTYPE) app_handler[mtype] = handler;
}