From 4c28fb525edc64fa16c6c3bf10929dca65d9c77c Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Wed, 4 Dec 2019 06:12:15 +0100 Subject: esp32 refactoring --- code/esp32/components/eos/bq25895.c | 50 ++++++- code/esp32/components/eos/include/eos.h | 15 +- code/esp32/components/eos/include/i2c.h | 1 + code/esp32/components/eos/include/modem.h | 2 +- code/esp32/components/eos/include/msgq.h | 11 +- code/esp32/components/eos/include/net.h | 31 ++-- code/esp32/components/eos/include/pcm.h | 1 + code/esp32/components/eos/include/transport.h | 13 -- code/esp32/components/eos/include/wifi.h | 9 ++ code/esp32/components/eos/modem.c | 9 +- code/esp32/components/eos/msgq.c | 17 +-- code/esp32/components/eos/net.c | 111 ++++++--------- code/esp32/components/eos/pcm.c | 15 +- code/esp32/components/eos/transport.c | 196 -------------------------- code/esp32/components/eos/wifi.c | 105 ++++++++++++++ 15 files changed, 255 insertions(+), 331 deletions(-) delete mode 100644 code/esp32/components/eos/include/transport.h create mode 100644 code/esp32/components/eos/include/wifi.h delete mode 100755 code/esp32/components/eos/transport.c create mode 100755 code/esp32/components/eos/wifi.c (limited to 'code/esp32/components/eos') diff --git a/code/esp32/components/eos/bq25895.c b/code/esp32/components/eos/bq25895.c index 24d8686..fc32fbd 100644 --- a/code/esp32/components/eos/bq25895.c +++ b/code/esp32/components/eos/bq25895.c @@ -25,7 +25,49 @@ static void disp_buf(uint8_t *buf, int len) void eos_bq25895_set_ilim(void) { uint8_t data = 0; - eos_i2c_write8(BQ25895_ADDR, 0, 0x3E); - data = eos_i2c_read8(BQ25895_ADDR, 0); - ESP_LOGI(TAG, "BUFFER: %02x", data); -} \ No newline at end of file + // eos_i2c_write8(BQ25895_ADDR, 2, 0xfd); + eos_i2c_write8(BQ25895_ADDR, 2, 0xec); + eos_i2c_write8(BQ25895_ADDR, 0, 0x26); + data = eos_i2c_read8(BQ25895_ADDR, 0x00); + ESP_LOGI(TAG, "REG00: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x01); + ESP_LOGI(TAG, "REG01: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x02); + ESP_LOGI(TAG, "REG02: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x03); + ESP_LOGI(TAG, "REG03: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x04); + ESP_LOGI(TAG, "REG04: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x05); + ESP_LOGI(TAG, "REG05: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x06); + ESP_LOGI(TAG, "REG06: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x07); + ESP_LOGI(TAG, "REG07: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x08); + ESP_LOGI(TAG, "REG08: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x09); + ESP_LOGI(TAG, "REG09: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x0a); + ESP_LOGI(TAG, "REG0A: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x0b); + ESP_LOGI(TAG, "REG0B: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x0c); + ESP_LOGI(TAG, "REG0C: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x0d); + ESP_LOGI(TAG, "REG0D: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x0e); + ESP_LOGI(TAG, "REG0E: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x0f); + ESP_LOGI(TAG, "REG0F: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x10); + ESP_LOGI(TAG, "REG10: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x11); + ESP_LOGI(TAG, "REG11: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x12); + ESP_LOGI(TAG, "REG12: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x13); + ESP_LOGI(TAG, "REG13: %02x", data); + data = eos_i2c_read8(BQ25895_ADDR, 0x14); + ESP_LOGI(TAG, "REG14: %02x", data); +} diff --git a/code/esp32/components/eos/include/eos.h b/code/esp32/components/eos/include/eos.h index 1b52605..a21985a 100644 --- a/code/esp32/components/eos/include/eos.h +++ b/code/esp32/components/eos/include/eos.h @@ -1,8 +1,9 @@ -#define EOS_OK 0 -#define EOS_ERR -1 -#define EOS_ERR_Q_FULL -10 +#define EOS_OK 0 +#define EOS_ERR -1 +#define EOS_ERR_Q_FULL -10 -#define EOS_PRIORITY_WIFI 1 -#define EOS_PRIORITY_SPI 1 -#define EOS_PRIORITY_PCM 1 -#define EOS_PRIORITY_MODEM 1 \ No newline at end of file + +#define EOS_IRQ_PRIORITY_UART 1 +#define EOS_IRQ_PRIORITY_I2S 1 +#define EOS_IRQ_PRIORITY_NET_XCHG 1 +#define EOS_IRQ_PRIORITY_UDP_RCVR 1 diff --git a/code/esp32/components/eos/include/i2c.h b/code/esp32/components/eos/include/i2c.h index 6f89654..995a77e 100644 --- a/code/esp32/components/eos/include/i2c.h +++ b/code/esp32/components/eos/include/i2c.h @@ -1,3 +1,4 @@ +#include #include void eos_i2c_init(void); diff --git a/code/esp32/components/eos/include/modem.h b/code/esp32/components/eos/include/modem.h index 424c0db..6a80918 100644 --- a/code/esp32/components/eos/include/modem.h +++ b/code/esp32/components/eos/include/modem.h @@ -1,4 +1,4 @@ -#include +#include void eos_modem_init(void); ssize_t eos_modem_write(void *data, size_t size); \ No newline at end of file diff --git a/code/esp32/components/eos/include/msgq.h b/code/esp32/components/eos/include/msgq.h index ebf54ce..9c9c757 100644 --- a/code/esp32/components/eos/include/msgq.h +++ b/code/esp32/components/eos/include/msgq.h @@ -1,11 +1,10 @@ #include -#include "fe310.h" - typedef struct EOSMsgItem { - unsigned char cmd; - unsigned char buffer[EOS_FE310_SIZE_BUF]; + unsigned char type; + unsigned char *buffer; uint16_t len; + uint8_t flags; } EOSMsgItem; typedef struct EOSMsgQ { @@ -16,5 +15,5 @@ typedef struct EOSMsgQ { } 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); +int eos_msgq_push(EOSMsgQ *msgq, unsigned char type, unsigned char *buffer, uint16_t len, uint8_t flags); +void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *type, unsigned char **buffer, uint16_t *len, uint8_t *flags); diff --git a/code/esp32/components/eos/include/net.h b/code/esp32/components/eos/include/net.h index 21b12dc..3ce142a 100644 --- a/code/esp32/components/eos/include/net.h +++ b/code/esp32/components/eos/include/net.h @@ -1,19 +1,24 @@ #include -#define EOS_FE310_CMD_FLAG_ONEW 0x10 +#define EOS_NET_MTYPE_SOCK 1 +#define EOS_NET_MTYPE_AUDIO 2 -#define EOS_FE310_CMD_WIFI_CONNECT 1 -#define EOS_FE310_CMD_WIFI_DISCONNECT 2 -#define EOS_FE310_CMD_WIFI_SCAN 3 -#define EOS_FE310_CMD_WIFI_PKT 4 -#define EOS_FE310_CMD_MODEM_DATA 5 -#define EOS_FE310_CMD_MODEM_CALL 6 +#define EOS_NET_MTYPE_WIFI 5 +#define EOS_NET_MTYPE_CELL 6 +#define EOS_NET_MTYPE_SIP 7 +#define EOS_NET_MTYPE_APP 8 -#define EOS_FE310_MAX_CMD 8 -#define EOS_FE310_SIZE_Q 64 -#define EOS_FE310_SIZE_BUF 2048 +#define EOS_NET_MAX_MTYPE 8 -typedef void (*eos_fe310_fptr_t) (unsigned char, unsigned char *, uint16_t); +#define EOS_NET_MTYPE_FLAG_ONEW 0x10 -void eos_fe310_init(void); -int eos_fe310_send(unsigned char cmd, unsigned char *buffer, uint16_t len); +#define EOS_NET_SIZE_BUF 1500 +#define EOS_NET_SIZE_BUFQ 64 + +#define EOS_NET_FLAG_BUF_FREE 0x01 + +typedef void (*eos_net_fptr_t) (unsigned char, unsigned char *, uint16_t); + +void eos_net_init(void); +int eos_net_send(unsigned char mtype, unsigned char *buffer, uint16_t len, uint8_t flags); +void eos_net_set_handler(unsigned char mtype, eos_net_fptr_t handler); \ No newline at end of file diff --git a/code/esp32/components/eos/include/pcm.h b/code/esp32/components/eos/include/pcm.h index ef8a303..dcccea7 100644 --- a/code/esp32/components/eos/include/pcm.h +++ b/code/esp32/components/eos/include/pcm.h @@ -1,3 +1,4 @@ +#include #include void eos_pcm_init(void); diff --git a/code/esp32/components/eos/include/transport.h b/code/esp32/components/eos/include/transport.h deleted file mode 100644 index 356014d..0000000 --- a/code/esp32/components/eos/include/transport.h +++ /dev/null @@ -1,13 +0,0 @@ -#include - -#define EOS_IPv4_ADDR_SIZE 4 - -typedef struct EOSNetAddr { - unsigned char host[EOS_IPv4_ADDR_SIZE]; - uint16_t port; -} EOSNetAddr; - -void eos_wifi_init(void); -void eos_wifi_connect(char *ssid, char *password); -void eos_wifi_disconnect(void); -ssize_t eos_wifi_send(void *msg, size_t msg_size, EOSNetAddr *addr); diff --git a/code/esp32/components/eos/include/wifi.h b/code/esp32/components/eos/include/wifi.h new file mode 100644 index 0000000..42c77bd --- /dev/null +++ b/code/esp32/components/eos/include/wifi.h @@ -0,0 +1,9 @@ +#define EOS_WIFI_MTYPE_SCAN 0 +#define EOS_WIFI_MTYPE_CONNECT 1 +#define EOS_WIFI_MTYPE_DISCONNECT 2 + +#define EOS_WIFI_MAX_MTYPE 3 + +void eos_wifi_init(void); +void eos_wifi_connect(char *ssid, char *pass); +void eos_wifi_disconnect(void); diff --git a/code/esp32/components/eos/modem.c b/code/esp32/components/eos/modem.c index 5b55f03..7e3131f 100644 --- a/code/esp32/components/eos/modem.c +++ b/code/esp32/components/eos/modem.c @@ -10,7 +10,6 @@ #include "eos.h" -#include "fe310.h" #define BUF_SIZE 1024 #define UART_GPIO_DTR 32 @@ -35,7 +34,7 @@ static void uart_event_task(void *pvParameters) { uart_get_buffered_data_len(UART_NUM_2, &len); if (len) { len = uart_read_bytes(UART_NUM_2, data, len, 100 / portTICK_RATE_MS); - eos_fe310_send(EOS_FE310_CMD_MODEM_DATA, data, len); + // eos_net_send(EOS_FE310_CMD_MODEM_DATA, data, len); } break; default: @@ -60,10 +59,10 @@ void eos_modem_init(void) { uart_param_config(UART_NUM_2, &uart_config); uart_set_pin(UART_NUM_2, 17, 16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); uart_driver_install(UART_NUM_2, BUF_SIZE, BUF_SIZE, 10, &uart_queue, 0); - + // Configuration for the DTR/RI lines gpio_config_t io_conf; - + io_conf.intr_type = GPIO_INTR_DISABLE; io_conf.mode = GPIO_MODE_OUTPUT; io_conf.pin_bit_mask = ((uint64_t)1 << UART_GPIO_DTR); @@ -71,7 +70,7 @@ void eos_modem_init(void) { gpio_set_level(UART_GPIO_DTR, 1); // Create a task to handle uart event from ISR - xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, EOS_PRIORITY_MODEM, NULL); + xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, EOS_IRQ_PRIORITY_UART, NULL); } ssize_t eos_modem_write(void *data, size_t size) { diff --git a/code/esp32/components/eos/msgq.c b/code/esp32/components/eos/msgq.c index 0160bf5..6fda261 100644 --- a/code/esp32/components/eos/msgq.c +++ b/code/esp32/components/eos/msgq.c @@ -1,5 +1,4 @@ -#include -#include +#include #include "eos.h" #include "msgq.h" @@ -13,26 +12,28 @@ void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size) { msgq->size = size; } -int eos_msgq_push(EOSMsgQ *msgq, unsigned char cmd, unsigned char *buffer, uint16_t len) { +int eos_msgq_push(EOSMsgQ *msgq, unsigned char type, unsigned char *buffer, uint16_t len, uint8_t flags) { if ((uint8_t)(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].type = type; + msgq->array[idx].buffer = buffer; msgq->array[idx].len = len; + msgq->array[idx].flags = flags; msgq->idx_w++; return EOS_OK; } -void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *cmd, unsigned char **buffer, uint16_t *len) { +void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *type, unsigned char **buffer, uint16_t *len, uint8_t *flags) { if (msgq->idx_r == msgq->idx_w) { - *cmd = 0; + *type = 0; *buffer = NULL; } else { uint8_t idx = EOS_MSGQ_IDX_MASK(msgq->idx_r, msgq->size); - *cmd = msgq->array[idx].cmd; + *type = msgq->array[idx].type; *buffer = msgq->array[idx].buffer; *len = msgq->array[idx].len; + *flags = msgq->array[idx].flags; msgq->idx_r++; } } diff --git a/code/esp32/components/eos/net.c b/code/esp32/components/eos/net.c index e9e3af4..bad078a 100644 --- a/code/esp32/components/eos/net.c +++ b/code/esp32/components/eos/net.c @@ -9,8 +9,6 @@ // #include #include -#include -#include #include #include #include @@ -19,13 +17,10 @@ #include "eos.h" #include "msgq.h" -#include "transport.h" -#include "modem.h" -#include "pcm.h" -#include "fe310.h" +#include "net.h" -static EOSMsgQ send_q; -static EOSMsgItem send_q_array[EOS_FE310_SIZE_Q]; +static EOSMsgQ net_send_q; +static EOSMsgItem net_sndq_array[EOS_NET_SIZE_BUFQ]; #define SPI_GPIO_RTS 22 #define SPI_GPIO_CTS 21 @@ -38,35 +33,39 @@ static SemaphoreHandle_t mutex; static const char *TAG = "EOS"; -static eos_fe310_fptr_t cmd_handler[EOS_FE310_MAX_CMD]; +static eos_net_fptr_t mtype_handler[EOS_NET_MAX_MTYPE]; -static void bad_handler(unsigned char cmd, unsigned char *buffer, uint16_t len) { - ESP_LOGI(TAG, "FE310 RECV: bad handler: %d", cmd); +static void bad_handler(unsigned char mtype, unsigned char *buffer, uint16_t len) { + ESP_LOGE(TAG, "NET RECV: bad handler: %d", mtype); } -static void transceiver(void *pvParameters) { +static void exchange(void *pvParameters) { int repeat = 0; - unsigned char cmd = 0; + unsigned char mtype = 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); + uint8_t flags; + unsigned char *buf_send = heap_caps_malloc(EOS_NET_SIZE_BUF, MALLOC_CAP_DMA); + unsigned char *buf_recv = heap_caps_malloc(EOS_NET_SIZE_BUF, MALLOC_CAP_DMA); spi_slave_transaction_t t; memset(&t, 0, sizeof(t)); - t.length = EOS_FE310_SIZE_BUF*8; + t.length = EOS_NET_SIZE_BUF*8; t.tx_buffer = buf_send; t.rx_buffer = buf_recv; for (;;) { if (!repeat) { xSemaphoreTake(mutex, portMAX_DELAY); - eos_msgq_pop(&send_q, &cmd, &buffer, &len); - if (cmd) { - buf_send[0] = ((cmd << 3) | (len >> 8)) & 0xFF; + eos_msgq_pop(&net_send_q, &mtype, &buffer, &len, &flags); + if (mtype) { + buf_send[0] = ((mtype << 3) | (len >> 8)) & 0xFF; buf_send[1] = len & 0xFF; - if (buffer) memcpy(buf_send + 2, buffer, len); + if (buffer) { + memcpy(buf_send + 2, buffer, len); + if (flags & EOS_NET_FLAG_BUF_FREE) free(buffer); + } } else { WRITE_PERI_REG(GPIO_OUT_W1TC_REG, (1 << SPI_GPIO_RTS)); buf_send[0] = 0; @@ -76,25 +75,25 @@ static void transceiver(void *pvParameters) { xSemaphoreGive(mutex); } - memset(buf_recv, 0, EOS_FE310_SIZE_BUF); + memset(buf_recv, 0, EOS_NET_SIZE_BUF); spi_slave_transmit(HSPI_HOST, &t, portMAX_DELAY); repeat = 0; if (buf_recv[0] != 0) { - cmd = (buf_recv[0] >> 3); + mtype = (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 (mtype & EOS_NET_MTYPE_FLAG_ONEW) { + mtype &= ~EOS_NET_MTYPE_FLAG_ONEW; if (buf_send[0]) repeat = 1; } - if (cmd < EOS_FE310_MAX_CMD) { - cmd_handler[cmd](cmd, buffer, len); + if (mtype <= EOS_NET_MAX_MTYPE) { + mtype_handler[mtype-1](mtype, buffer, len); } else { - bad_handler(cmd, buffer, len); + bad_handler(mtype, buffer, len); } } else { - // ESP_LOGI(TAG, "FE310 RECV NULL"); + // ESP_LOGI(TAG, "NET RECV NULL"); } // vTaskDelay(5000 / portTICK_PERIOD_MS); } @@ -110,37 +109,12 @@ static void _post_trans_cb(spi_slave_transaction_t *trans) { WRITE_PERI_REG(GPIO_OUT_W1TC_REG, (1 << SPI_GPIO_CTS)); } -static void fe310_wifi_connect_handler(unsigned char cmd, unsigned char *buffer, uint16_t size) { - eos_wifi_connect((char *)buffer, (char *)(buffer+strlen((char *)buffer)+1)); -} - -static void fe310_wifi_pkt_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_wifi_send(buffer+addr_len, size-addr_len, &addr); -} - -static void fe310_modem_data_handler(unsigned char cmd, unsigned char *buffer, uint16_t size) { - eos_modem_write(buffer, size); -} - -static void fe310_modem_call_handler(unsigned char cmd, unsigned char *buffer, uint16_t size) { - eos_pcm_call(); -} - -static void fe310_set_handler(unsigned char cmd, eos_fe310_fptr_t handler) { - cmd_handler[cmd] = handler; -} - -void eos_fe310_init(void) { +void eos_net_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); @@ -176,33 +150,30 @@ void eos_fe310_init(void) { gpio_set_pull_mode(SPI_GPIO_CS, GPIO_PULLUP_ONLY); int i; - for (i=0; i -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "eos.h" -#include "fe310.h" -#include "transport.h" -#include "modem.h" -#include "bq25895.h" -#include "drv2605l.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(3000); - - 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_WIFI_PKT, buffer, rv+addr_len); - } -} - -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, "wifi_receiver", 4096, NULL, EOS_PRIORITY_WIFI, &receiver_task); - // xTaskCreatePinnedToCore(&receiver, "wifi_receiver", 4096, NULL, EOS_PRIORITY_WIFI, &receiver_task, 1); - eos_fe310_send(EOS_FE310_CMD_WIFI_CONNECT, NULL, 0); - // eos_drv2605l_test(); - break; - - default: // Ignore the other event types - break; - } - - return ESP_OK; -} - -void eos_wifi_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() ); -} - -void eos_wifi_connect(char *ssid, char *password) { - wifi_config_t wifi_config; - - ESP_LOGI(TAG, "CONNECTING...."); - 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_wifi_disconnect(void) { - ESP_ERROR_CHECK( esp_wifi_disconnect() ); -} - -ssize_t eos_wifi_send(void *msg, size_t msg_size, EOSNetAddr *addr) { - return t_send(msg, msg_size, addr); -} diff --git a/code/esp32/components/eos/wifi.c b/code/esp32/components/eos/wifi.c new file mode 100755 index 0000000..628c4fd --- /dev/null +++ b/code/esp32/components/eos/wifi.c @@ -0,0 +1,105 @@ +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "eos.h" +#include "net.h" +#include "wifi.h" + +static const char *TAG = "EOS"; + +static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) { + switch(event->event_id) { + case SYSTEM_EVENT_SCAN_DONE: + break; + + case SYSTEM_EVENT_STA_START: + break; + + case SYSTEM_EVENT_STA_STOP: + break; + + case SYSTEM_EVENT_STA_CONNECTED: + break; + + case SYSTEM_EVENT_STA_DISCONNECTED: + ESP_LOGI(TAG, "DISCONNECTED"); + 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, "********************************************"); + unsigned char *buf = malloc(32); + buf[0] = EOS_WIFI_MTYPE_CONNECT; + eos_net_send(EOS_NET_MTYPE_WIFI, buf, 1, EOS_NET_FLAG_BUF_FREE); + break; + + default: // Ignore the other event types + break; + } + + return ESP_OK; +} + +static void wifi_handler(unsigned char _mtype, unsigned char *buffer, uint16_t size) { + uint8_t mtype = buffer[0]; + char *ssid, *pass; + + switch (mtype) { + case EOS_WIFI_MTYPE_SCAN: + break; + case EOS_WIFI_MTYPE_CONNECT: + ssid = (char *)buffer+1; + pass = ssid+strlen(ssid)+1; + eos_wifi_connect(ssid, pass); + break; + case EOS_WIFI_MTYPE_DISCONNECT: + eos_wifi_disconnect(); + break; + } + // eos_wifi_connect((char *)buffer, (char *)(buffer+strlen((char *)buffer)+1)); +} + +void eos_wifi_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(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_net_set_handler(EOS_NET_MTYPE_WIFI, wifi_handler); +} + +void eos_wifi_connect(char *ssid, char *pass) { + 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, pass, 63); + ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + ESP_ERROR_CHECK( esp_wifi_connect() ); +} + +void eos_wifi_disconnect(void) { + ESP_ERROR_CHECK( esp_wifi_disconnect() ); +} + -- cgit v1.2.3