summaryrefslogtreecommitdiff
path: root/code/esp32/components
diff options
context:
space:
mode:
Diffstat (limited to 'code/esp32/components')
-rw-r--r--code/esp32/components/eos/component.mk0
-rw-r--r--code/esp32/components/eos/fe310.c180
-rw-r--r--code/esp32/components/eos/include/eos.h2
-rw-r--r--code/esp32/components/eos/include/fe310.h18
-rw-r--r--code/esp32/components/eos/include/msgq.h20
-rw-r--r--code/esp32/components/eos/include/transport.h13
-rw-r--r--code/esp32/components/eos/msgq.c39
-rwxr-xr-xcode/esp32/components/eos/transport.c205
8 files changed, 477 insertions, 0 deletions
diff --git a/code/esp32/components/eos/component.mk b/code/esp32/components/eos/component.mk
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/code/esp32/components/eos/component.mk
diff --git a/code/esp32/components/eos/fe310.c b/code/esp32/components/eos/fe310.c
new file mode 100644
index 0000000..6dceb51
--- /dev/null
+++ b/code/esp32/components/eos/fe310.c
@@ -0,0 +1,180 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <string.h>
+
+#include <freertos/FreeRTOS.h>
+#include <freertos/task.h>
+#include <freertos/semphr.h>
+// #include <freertos/heap_regions.h>
+
+#include <esp_system.h>
+#include <esp_event.h>
+#include <esp_event_loop.h>
+#include <esp_log.h>
+#include <esp_err.h>
+#include <esp_heap_caps.h>
+#include <driver/spi_slave.h>
+
+#include "msgq.h"
+#include "transport.h"
+#include "fe310.h"
+
+static EOSMsgQ send_q;
+static EOSMsgItem send_q_array[EOS_FE310_SIZE_Q];
+
+#define SPI_GPIO_RTS 4
+#define SPI_GPIO_CTS 2
+#define SPI_GPIO_MOSI 23
+#define SPI_GPIO_MISO 19
+#define SPI_GPIO_SCLK 18
+#define SPI_GPIO_CS 5
+
+static SemaphoreHandle_t mutex;
+
+static const char *TAG = "EOS";
+
+static eos_fe310_fptr_t cmd_handler[EOS_FE310_MAX_CMD];
+
+static void bad_handler(unsigned char cmd, unsigned char *buffer, uint16_t len) {
+ ESP_LOGI(TAG, "FE310 RECV: bad handler: %d", cmd);
+}
+
+static void worker(void *pvParameters) {
+ int repeat = 0;
+ esp_err_t ret;
+ unsigned char cmd = 0;
+ unsigned char *buffer;
+ uint16_t len;
+ unsigned char *buf_send = heap_caps_malloc(EOS_FE310_SIZE_BUF, MALLOC_CAP_DMA);
+ unsigned char *buf_recv = heap_caps_malloc(EOS_FE310_SIZE_BUF, MALLOC_CAP_DMA);
+
+ spi_slave_transaction_t t;
+ memset(&t, 0, sizeof(t));
+ t.length = EOS_FE310_SIZE_BUF*8;
+ t.tx_buffer = buf_send;
+ t.rx_buffer = buf_recv;
+
+ xSemaphoreTake(mutex, portMAX_DELAY);
+ for (;;) {
+ if (!repeat) {
+ eos_msgq_pop(&send_q, &cmd, &buffer, &len);
+ if (cmd) {
+ buf_send[0] = ((cmd << 3) | (len >> 8)) & 0xFF;
+ buf_send[1] = len & 0xFF;
+ if (buffer) memcpy(buf_send + 2, buffer, len);
+ } else {
+ WRITE_PERI_REG(GPIO_OUT_W1TC_REG, (1 << SPI_GPIO_RTS));
+ buf_send[0] = 0;
+ buf_send[1] = 0;
+ }
+ }
+ memset(buf_recv, 0, EOS_FE310_SIZE_BUF);
+ ret = spi_slave_transmit(HSPI_HOST, &t, portMAX_DELAY);
+
+ repeat = 0;
+ if (buf_recv[0] != 0) {
+ xSemaphoreGive(mutex);
+
+ cmd = (buf_recv[0] >> 3);
+ len = ((buf_recv[0] & 0x07) << 8);
+ len |= buf_recv[1];
+ buffer = buf_recv + 2;
+ if (cmd & EOS_FE310_CMD_FLAG_ONEW) {
+ cmd &= ~EOS_FE310_CMD_FLAG_ONEW;
+ if (buf_send[0]) repeat = 1;
+ }
+ if (cmd < EOS_FE310_MAX_CMD) {
+ cmd_handler[cmd](cmd, buffer, len);
+ } else {
+ bad_handler(cmd, buffer, len);
+ }
+
+ xSemaphoreTake(mutex, portMAX_DELAY);
+ } else {
+ ESP_LOGI(TAG, "FE310 RECV NULL");
+ }
+ // vTaskDelay(5000 / portTICK_PERIOD_MS);
+ }
+ xSemaphoreGive(mutex);
+}
+
+// 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) {
+ xSemaphoreGive(mutex);
+ WRITE_PERI_REG(GPIO_OUT_W1TS_REG, (1 << SPI_GPIO_CTS));
+}
+
+// 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) {
+ xSemaphoreTake(mutex, portMAX_DELAY);
+ WRITE_PERI_REG(GPIO_OUT_W1TC_REG, (1 << SPI_GPIO_CTS));
+}
+
+void eos_fe310_init(void) {
+ esp_err_t ret;
+
+ // 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.pin_bit_mask = (1 << SPI_GPIO_CTS);
+ gpio_config(&io_conf);
+ WRITE_PERI_REG(GPIO_OUT_W1TC_REG, (1 << SPI_GPIO_CTS));
+
+ io_conf.intr_type = GPIO_INTR_DISABLE;
+ io_conf.mode = GPIO_MODE_OUTPUT;
+ io_conf.pin_bit_mask = (1 << SPI_GPIO_RTS);
+ gpio_config(&io_conf);
+ WRITE_PERI_REG(GPIO_OUT_W1TC_REG, (1 << SPI_GPIO_RTS));
+
+ //Configuration for the SPI bus
+ spi_bus_config_t buscfg = {
+ .mosi_io_num = SPI_GPIO_MOSI,
+ .miso_io_num = SPI_GPIO_MISO,
+ .sclk_io_num = SPI_GPIO_SCLK
+ };
+
+ //Configuration for the SPI slave interface
+ spi_slave_interface_config_t slvcfg = {
+ .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
+ };
+
+ //Enable pull-ups on SPI lines so we don't detect rogue pulses when no master is connected.
+ gpio_set_pull_mode(SPI_GPIO_MOSI, GPIO_PULLUP_ONLY);
+ gpio_set_pull_mode(SPI_GPIO_SCLK, GPIO_PULLUP_ONLY);
+ gpio_set_pull_mode(SPI_GPIO_CS, GPIO_PULLUP_ONLY);
+
+ int i;
+ for (i=0; i<EOS_FE310_MAX_CMD; i++) {
+ cmd_handler[i] = bad_handler;
+ }
+
+ //Initialize SPI slave interface
+ ret=spi_slave_initialize(HSPI_HOST, &buscfg, &slvcfg, 1);
+ assert(ret==ESP_OK);
+
+ eos_msgq_init(&send_q, send_q_array, EOS_FE310_SIZE_Q);
+ mutex = xSemaphoreCreateMutex();
+ xTaskCreate(&worker, "fe310_receiver", 4096, NULL, 5, NULL);
+}
+
+int eos_fe310_send(unsigned char cmd, unsigned char *buffer, uint16_t len) {
+ xSemaphoreTake(mutex, portMAX_DELAY);
+ WRITE_PERI_REG(GPIO_OUT_W1TS_REG, (1 << SPI_GPIO_RTS));
+ int rv = eos_msgq_push(&send_q, cmd, buffer, len);
+ xSemaphoreGive(mutex);
+
+ return rv;
+}
+
+void eos_fe310_set_handler(unsigned char cmd, eos_fe310_fptr_t handler) {
+ cmd_handler[cmd] = handler;
+}
+
diff --git a/code/esp32/components/eos/include/eos.h b/code/esp32/components/eos/include/eos.h
new file mode 100644
index 0000000..a272ac8
--- /dev/null
+++ b/code/esp32/components/eos/include/eos.h
@@ -0,0 +1,2 @@
+#define EOS_OK 0
+#define EOS_ERR_Q_FULL -10 \ No newline at end of file
diff --git a/code/esp32/components/eos/include/fe310.h b/code/esp32/components/eos/include/fe310.h
new file mode 100644
index 0000000..f4bc787
--- /dev/null
+++ b/code/esp32/components/eos/include/fe310.h
@@ -0,0 +1,18 @@
+#include <stdint.h>
+
+#define EOS_FE310_CMD_FLAG_ONEW 0x10
+
+#define EOS_FE310_CMD_CONNECT 1
+#define EOS_FE310_CMD_DISCONNECT 2
+#define EOS_FE310_CMD_SCAN 3
+#define EOS_FE310_CMD_PKT 4
+
+#define EOS_FE310_MAX_CMD 8
+#define EOS_FE310_SIZE_Q 64
+#define EOS_FE310_SIZE_BUF 2048
+
+typedef void (*eos_fe310_fptr_t) (unsigned char, unsigned char *, uint16_t);
+
+void eos_fe310_init(void);
+int eos_fe310_send(unsigned char cmd, unsigned char *buffer, uint16_t len);
+void eos_fe310_set_handler(unsigned char cmd, eos_fe310_fptr_t handler);
diff --git a/code/esp32/components/eos/include/msgq.h b/code/esp32/components/eos/include/msgq.h
new file mode 100644
index 0000000..ebf54ce
--- /dev/null
+++ b/code/esp32/components/eos/include/msgq.h
@@ -0,0 +1,20 @@
+#include <stdint.h>
+
+#include "fe310.h"
+
+typedef struct EOSMsgItem {
+ unsigned char cmd;
+ unsigned char buffer[EOS_FE310_SIZE_BUF];
+ uint16_t len;
+} EOSMsgItem;
+
+typedef struct EOSMsgQ {
+ uint8_t idx_r;
+ uint8_t idx_w;
+ uint8_t size;
+ EOSMsgItem *array;
+} EOSMsgQ;
+
+void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size);
+int eos_msgq_push(EOSMsgQ *msgq, unsigned char cmd, unsigned char *buffer, uint16_t len);
+void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *cmd, unsigned char **buffer, uint16_t *len);
diff --git a/code/esp32/components/eos/include/transport.h b/code/esp32/components/eos/include/transport.h
new file mode 100644
index 0000000..9d76d1b
--- /dev/null
+++ b/code/esp32/components/eos/include/transport.h
@@ -0,0 +1,13 @@
+#include <stdint.h>
+
+#define EOS_IPv4_ADDR_SIZE 4
+
+typedef struct EOSNetAddr {
+ unsigned char host[EOS_IPv4_ADDR_SIZE];
+ uint16_t port;
+} EOSNetAddr;
+
+void eos_net_init(void);
+void eos_net_connect(char *ssid, char *password);
+void eos_net_disconnect(void);
+ssize_t eos_net_send(void *msg, size_t msg_size, EOSNetAddr *addr);
diff --git a/code/esp32/components/eos/msgq.c b/code/esp32/components/eos/msgq.c
new file mode 100644
index 0000000..ec89efe
--- /dev/null
+++ b/code/esp32/components/eos/msgq.c
@@ -0,0 +1,39 @@
+#include <stddef.h>
+#include <string.h>
+
+#include "eos.h"
+#include "msgq.h"
+
+#define EOS_MSGQ_IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1))
+
+void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size) {
+ msgq->idx_r = 0;
+ msgq->idx_w = 0;
+ msgq->array = array;
+ msgq->size = size;
+}
+
+int eos_msgq_push(EOSMsgQ *msgq, unsigned char cmd, unsigned char *buffer, uint16_t len) {
+ if (msgq->idx_w - msgq->idx_r == msgq->size) return EOS_ERR_Q_FULL;
+
+ uint8_t idx = EOS_MSGQ_IDX_MASK(msgq->idx_w, msgq->size);
+ msgq->array[idx].cmd = cmd;
+ memcpy(msgq->array[idx].buffer, buffer, len);
+ msgq->array[idx].len = len;
+ msgq->idx_w++;
+ return EOS_OK;
+}
+
+void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *cmd, unsigned char **buffer, uint16_t *len) {
+ if (msgq->idx_r == msgq->idx_w) {
+ *cmd = 0;
+ *buffer = NULL;
+ } else {
+ uint8_t idx = EOS_MSGQ_IDX_MASK(msgq->idx_r, msgq->size);
+ *cmd = msgq->array[idx].cmd;
+ *buffer = msgq->array[idx].buffer;
+ *len = msgq->array[idx].len;
+ msgq->idx_r++;
+ }
+}
+
diff --git a/code/esp32/components/eos/transport.c b/code/esp32/components/eos/transport.c
new file mode 100755
index 0000000..162fe86
--- /dev/null
+++ b/code/esp32/components/eos/transport.c
@@ -0,0 +1,205 @@
+/*
+MIT License
+
+Copyright (c) 2017 Olof Astrand (Ebiroll)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include <string.h>
+#include <stdint.h>
+
+#include <freertos/FreeRTOS.h>
+#include <freertos/task.h>
+
+#include <esp_system.h>
+#include <esp_event.h>
+#include <esp_event_loop.h>
+#include <esp_log.h>
+#include <esp_err.h>
+#include <esp_wifi.h>
+#include <nvs_flash.h>
+
+#include <lwip/sockets.h>
+#include <lwip/err.h>
+#include <lwip/sockets.h>
+#include <lwip/sys.h>
+#include <lwip/netdb.h>
+#include <lwip/dns.h>
+
+#include "eos.h"
+#include "fe310.h"
+#include "transport.h"
+
+static const char *TAG = "EOS";
+static int udp_sock = -1;
+static TaskHandle_t receiver_task;
+
+static int t_open(void) {
+ struct sockaddr_in _myaddr;
+
+ udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
+ if (udp_sock < 0) return udp_sock;
+
+ memset((char *)&_myaddr, 0, sizeof(_myaddr));
+ _myaddr.sin_family = AF_INET;
+ _myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
+ _myaddr.sin_port = htons(0);
+
+ int rv = bind(udp_sock, (struct sockaddr *)&_myaddr, sizeof(_myaddr));
+ if (rv < 0) {
+ close(udp_sock);
+ return rv;
+ }
+ return EOS_OK;
+}
+
+static void t_close(void) {
+ close(udp_sock);
+}
+
+static ssize_t t_send(void *msg, size_t msg_size, EOSNetAddr *addr) {
+ struct sockaddr_in servaddr;
+
+ memset((void *)&servaddr, 0, sizeof(servaddr));
+ servaddr.sin_family = AF_INET;
+ servaddr.sin_port = addr->port;
+ memcpy((void *)&servaddr.sin_addr, addr->host, sizeof(addr->host));
+ return sendto(udp_sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
+}
+
+static ssize_t t_recv(void *msg, size_t msg_size, EOSNetAddr *addr) {
+ struct sockaddr_in servaddr;
+ socklen_t addrlen = sizeof(servaddr);
+ memset((void *)&servaddr, 0, sizeof(servaddr));
+
+ ssize_t recvlen = recvfrom(udp_sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, &addrlen);
+ if (recvlen < 0) return recvlen;
+
+ if (addr) {
+ addr->port = servaddr.sin_port;
+ memcpy(addr->host, (void *)&servaddr.sin_addr, sizeof(addr->host));
+ }
+ return recvlen;
+}
+
+static void receiver(void *pvParameters) {
+ EOSNetAddr addr;
+ size_t addr_len = sizeof(addr.host) + sizeof(addr.port);
+ unsigned char buffer[2048];
+
+ for (;;) {
+ ssize_t rv = t_recv(buffer+addr_len, sizeof(buffer)-addr_len, &addr);
+ if (rv < 0) {
+ ESP_LOGI(TAG, "UDP RECV ERR:%d", rv);
+ continue;
+ }
+ memcpy(buffer, addr.host, sizeof(addr.host));
+ memcpy(buffer+sizeof(addr.host), &addr.port, sizeof(addr.port));
+ eos_fe310_send(EOS_FE310_CMD_PKT, buffer, rv+addr_len);
+ }
+}
+
+static void fe310_connect_cmd_handler(unsigned char cmd, unsigned char *buffer, uint16_t size) {
+ eos_net_connect((char *)buffer, (char *)(buffer+strlen((char *)buffer)+1));
+}
+
+static void fe310_packet_cmd_handler(unsigned char cmd, unsigned char *buffer, uint16_t size) {
+ EOSNetAddr addr;
+ size_t addr_len = sizeof(addr.host) + sizeof(addr.port);
+
+ memcpy(addr.host, buffer, sizeof(addr.host));
+ memcpy(&addr.port, buffer+sizeof(addr.host), sizeof(addr.port));
+ eos_net_send(buffer+addr_len, size-addr_len, &addr);
+}
+
+static esp_err_t esp32_wifi_event_handler(void *ctx, system_event_t *event) {
+ switch(event->event_id) {
+ case SYSTEM_EVENT_WIFI_READY:
+ break;
+
+ case SYSTEM_EVENT_AP_STACONNECTED:
+ break;
+
+ case SYSTEM_EVENT_AP_START:
+ break;
+
+ case SYSTEM_EVENT_SCAN_DONE:
+ break;
+
+ case SYSTEM_EVENT_STA_CONNECTED:
+ break;
+
+ case SYSTEM_EVENT_STA_DISCONNECTED:
+ ESP_LOGI(TAG, "DISCONNECT");
+ vTaskDelete(receiver_task);
+ t_close();
+ break;
+
+ case SYSTEM_EVENT_STA_GOT_IP:
+ ESP_LOGI(TAG, "********************************************");
+ ESP_LOGI(TAG, "* We are now connected to AP")
+ ESP_LOGI(TAG, "* - Our IP address is: " IPSTR, IP2STR(&event->event_info.got_ip.ip_info.ip));
+ ESP_LOGI(TAG, "********************************************");
+ t_open();
+ xTaskCreate(&receiver, "receiver", 4096, NULL, 5, &receiver_task);
+ eos_fe310_send(EOS_FE310_CMD_CONNECT, NULL, 0);
+ break;
+
+ default: // Ignore the other event types
+ break;
+ }
+
+ return ESP_OK;
+}
+
+void eos_net_init(void) {
+ wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
+ wifi_config_t wifi_config;
+
+ memset(&wifi_config, 0, sizeof(wifi_config));
+ tcpip_adapter_init();
+ ESP_ERROR_CHECK( nvs_flash_init() );
+ ESP_ERROR_CHECK( esp_event_loop_init(esp32_wifi_event_handler, NULL) );
+ ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
+ ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
+ ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
+ ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
+ ESP_ERROR_CHECK( esp_wifi_start() );
+ eos_fe310_set_handler(EOS_FE310_CMD_CONNECT, fe310_connect_cmd_handler);
+ eos_fe310_set_handler(EOS_FE310_CMD_PKT, fe310_packet_cmd_handler);
+}
+
+void eos_net_connect(char *ssid, char *password) {
+ wifi_config_t wifi_config;
+
+ memset(&wifi_config, 0, sizeof(wifi_config));
+ strncpy((char *)wifi_config.sta.ssid, ssid, 31);
+ strncpy((char *)wifi_config.sta.password, password, 63);
+ ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
+ ESP_ERROR_CHECK( esp_wifi_connect() );
+}
+
+void eos_net_disconnect(void) {
+ ESP_ERROR_CHECK( esp_wifi_disconnect() );
+}
+
+ssize_t eos_net_send(void *msg, size_t msg_size, EOSNetAddr *addr) {
+ return t_send(msg, msg_size, addr);
+}