summaryrefslogtreecommitdiff
path: root/code/esp32/components/eos
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2019-12-07 17:13:23 +0100
committerUros Majstorovic <majstor@majstor.org>2019-12-07 17:13:23 +0100
commit040f4333678f46f3558e604014d460076244af6b (patch)
tree084b8a41eef67a9bf45a5c21124fc393061f88fb /code/esp32/components/eos
parent8cdc7528c800a3f10bca3e875e76f6d9661ab88f (diff)
added net_alloc; fixed cell modem driver
Diffstat (limited to 'code/esp32/components/eos')
-rw-r--r--code/esp32/components/eos/bq25895.c24
-rw-r--r--code/esp32/components/eos/cell_modem.c78
-rw-r--r--code/esp32/components/eos/cell_pcm.c33
-rw-r--r--code/esp32/components/eos/drv2605l.c45
-rw-r--r--code/esp32/components/eos/i2c.c10
-rw-r--r--code/esp32/components/eos/include/net.h7
-rw-r--r--code/esp32/components/eos/net.c91
-rw-r--r--code/esp32/components/eos/sock.c31
-rwxr-xr-xcode/esp32/components/eos/wifi.c11
9 files changed, 207 insertions, 123 deletions
diff --git a/code/esp32/components/eos/bq25895.c b/code/esp32/components/eos/bq25895.c
index fc32fbd..03116ca 100644
--- a/code/esp32/components/eos/bq25895.c
+++ b/code/esp32/components/eos/bq25895.c
@@ -4,30 +4,16 @@
#include "eos.h"
#include "i2c.h"
-static const char *TAG = "BQ25895";
+static const char *TAG = "EOS BQ25895";
#define BQ25895_ADDR 0x6A
-/**
- * @brief test function to show buffer
- */
-static void disp_buf(uint8_t *buf, int len)
-{
- int i;
- for (i = 0; i < len; i++) {
- printf("%02x ", buf[i]);
- if ((i + 1) % 16 == 0) {
- printf("\n");
- }
- }
- printf("\n");
-}
-
void eos_bq25895_set_ilim(void) {
uint8_t data = 0;
- // eos_i2c_write8(BQ25895_ADDR, 2, 0xfd);
- eos_i2c_write8(BQ25895_ADDR, 2, 0xec);
- eos_i2c_write8(BQ25895_ADDR, 0, 0x26);
+ eos_i2c_write8(BQ25895_ADDR, 0, 0x1c);
+ eos_i2c_write8(BQ25895_ADDR, 2, 0x28);
+ eos_i2c_write8(BQ25895_ADDR, 7, 0x8d);
+
data = eos_i2c_read8(BQ25895_ADDR, 0x00);
ESP_LOGI(TAG, "REG00: %02x", data);
data = eos_i2c_read8(BQ25895_ADDR, 0x01);
diff --git a/code/esp32/components/eos/cell_modem.c b/code/esp32/components/eos/cell_modem.c
index 7e3131f..26748a1 100644
--- a/code/esp32/components/eos/cell_modem.c
+++ b/code/esp32/components/eos/cell_modem.c
@@ -8,19 +8,33 @@
#include <driver/gpio.h>
#include <esp_log.h>
-
#include "eos.h"
+#include "net.h"
+#include "cell.h"
+
+#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
+#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
+
+#define UART_BUF_SIZE 1024
-#define BUF_SIZE 1024
+#define UART_GPIO_TXD 16
+#define UART_GPIO_RXD 17
#define UART_GPIO_DTR 32
+#define UART_GPIO_RI 35
+
+
+#define UART_EVENT_MODE_NONE 0
+#define UART_EVENT_MODE_PPP 1
+#define UART_EVENT_MODE_RELAY 2
+
static QueueHandle_t uart_queue;
+// static uint8_t *uart_data[UART_BUF_SIZE];
static void uart_event_task(void *pvParameters) {
+ char mode = 0;
uart_event_t event;
size_t len;
-
- // Reserve a buffer and process incoming data
- uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
+ unsigned char *buf;
while (1) {
/* Waiting for UART event.
@@ -30,22 +44,51 @@ static void uart_event_task(void *pvParameters) {
case UART_DATA:
/* Event of UART receiving data
*/
- len = 0;
- 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_net_send(EOS_FE310_CMD_MODEM_DATA, data, len);
+ switch (mode) {
+ case UART_EVENT_MODE_PPP:
+ break;
+
+ case UART_EVENT_MODE_RELAY:
+ buf = eos_net_alloc();
+ buf[0] = EOS_CELL_MTYPE_DATA;
+ len = uart_read_bytes(UART_NUM_2, buf+1, MIN(event.size, EOS_NET_SIZE_BUF-1), 100 / portTICK_RATE_MS);
+ eos_net_send(EOS_NET_MTYPE_CELL, buf, len, 0);
+ break;
+
+ default:
+ break;
}
+
+ case UART_EVENT_MAX:
+ /* Mode change
+ */
+ mode = (char)event.size;
break;
+
default:
break;
}
}
}
- free(data);
vTaskDelete(NULL);
}
+static void modem_handler(unsigned char _mtype, unsigned char *buffer, uint16_t size) {
+ uint8_t mtype = buffer[0];
+
+ switch (mtype) {
+ case EOS_CELL_MTYPE_DATA:
+ eos_modem_write(buffer+1, size-1);
+ break;
+ case EOS_CELL_MTYPE_DATA_START:
+ eos_modem_set_mode(UART_EVENT_MODE_RELAY);
+ break;
+ case EOS_CELL_MTYPE_DATA_STOP:
+ eos_modem_set_mode(0);
+ break;
+ }
+}
+
void eos_modem_init(void) {
/* Configure parameters of an UART driver,
* communication pins and install the driver */
@@ -57,8 +100,8 @@ void eos_modem_init(void) {
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
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);
+ uart_set_pin(UART_NUM_2, UART_GPIO_TXD, UART_GPIO_RXD, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
+ uart_driver_install(UART_NUM_2, UART_BUF_SIZE, UART_BUF_SIZE, 10, &uart_queue, 0);
// Configuration for the DTR/RI lines
gpio_config_t io_conf;
@@ -71,8 +114,17 @@ void eos_modem_init(void) {
// Create a task to handle uart event from ISR
xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, EOS_IRQ_PRIORITY_UART, NULL);
+
+ eos_net_set_handler(EOS_NET_MTYPE_CELL, modem_handler);
}
ssize_t eos_modem_write(void *data, size_t size) {
return uart_write_bytes(UART_NUM_2, (const char *)data, size);
}
+
+void eos_modem_set_mode(char mode) {
+ uart_event_t evt;
+ evt.type = UART_EVENT_MAX; /* my type */
+ evt.size = mode;
+ xQueueSend(&uart_queue, (void *)&evt, portMAX_DELAY);
+}
diff --git a/code/esp32/components/eos/cell_pcm.c b/code/esp32/components/eos/cell_pcm.c
index edfcf88..0c9dfc9 100644
--- a/code/esp32/components/eos/cell_pcm.c
+++ b/code/esp32/components/eos/cell_pcm.c
@@ -8,11 +8,17 @@
#include <esp_log.h>
#include "eos.h"
-#include "modem.h"
+#include "net.h"
+#include "cell.h"
static i2s_dev_t* I2S[I2S_NUM_MAX] = {&I2S0, &I2S1};
-#define BUF_SIZE 2048
+#define BUF_SIZE 2048
+
+#define PCM_GPIO_BCK 33
+#define PCM_GPIO_WS 4
+#define PCM_GPIO_DIN 34
+#define PCM_GPIO_DOUT 2
static QueueHandle_t i2s_queue;
@@ -59,6 +65,7 @@ static void i2s_event_task(void *pvParameters) {
vTaskDelete(NULL);
}
+/*
static void i2s_write_task(void *pvParameters) {
uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
memset(data, 0x0, BUF_SIZE);
@@ -78,16 +85,15 @@ static void i2s_write_task(void *pvParameters) {
free(data);
vTaskDelete(NULL);
}
+*/
void eos_pcm_init(void) {
- esp_err_t err;
-
i2s_config_t i2s_config = {
.mode = I2S_MODE_SLAVE | I2S_MODE_TX | I2S_MODE_RX,
.sample_rate = 32000,
.bits_per_sample = 32,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
- .communication_format = I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_PCM_LONG,
+ .communication_format = I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_PCM_LONG,
.dma_buf_count = 4,
.dma_buf_len = 512,
.use_apll = true,
@@ -96,15 +102,13 @@ void eos_pcm_init(void) {
};
i2s_pin_config_t pin_config = {
- .bck_io_num = 33,
- .ws_io_num = 4,
- .data_out_num = 2,
- .data_in_num = 34
+ .bck_io_num = PCM_GPIO_BCK,
+ .ws_io_num = PCM_GPIO_WS,
+ .data_in_num = PCM_GPIO_DIN,
+ .data_out_num = PCM_GPIO_DOUT
};
- err = i2s_driver_install(I2S_NUM_0, &i2s_config, 10, &i2s_queue); //install and start i2s driver
- printf("I2S ERR: %d\n", err);
- err = i2s_set_pin(I2S_NUM_0, &pin_config);
- printf("I2S ERR: %d\n", err);
+ i2s_driver_install(I2S_NUM_0, &i2s_config, 10, &i2s_queue); //install and start i2s driver
+ i2s_set_pin(I2S_NUM_0, &pin_config);
i2s_stop(I2S_NUM_0);
I2S[I2S_NUM_0]->conf.tx_mono = 1;
I2S[I2S_NUM_0]->conf.rx_mono = 1;
@@ -123,6 +127,7 @@ ssize_t eos_pcm_write(void *data, size_t size) {
return size_out;
}
+/*
void eos_pcm_call(void) {
const char *s = "ATD0631942317;\r";
@@ -131,4 +136,4 @@ void eos_pcm_call(void) {
vTaskDelay(1000 / portTICK_RATE_MS);
i2s_start(I2S_NUM_0);
}
-
+*/
diff --git a/code/esp32/components/eos/drv2605l.c b/code/esp32/components/eos/drv2605l.c
index 5a2aa8c..023367e 100644
--- a/code/esp32/components/eos/drv2605l.c
+++ b/code/esp32/components/eos/drv2605l.c
@@ -4,7 +4,7 @@
#include "eos.h"
#include "i2c.h"
-static const char *TAG = "DRV2605L";
+static const char *TAG = "EOS DRV2605L";
#define DRV2605L_ADDR 0x5A
@@ -52,37 +52,19 @@ static const char *TAG = "DRV2605L";
#define DRV2605_REG_VBAT 0x21 ///< Vbat voltage-monitor register
#define DRV2605_REG_LRARESON 0x22 ///< LRA resonance-period register
-/**
- * @brief test function to show buffer
- */
-static void disp_buf(uint8_t *buf, int len)
-{
- int i;
- for (i = 0; i < len; i++) {
- printf("%02x ", buf[i]);
- if ((i + 1) % 16 == 0) {
- printf("\n");
- }
- }
- printf("\n");
-}
-
void eos_drv2605l_test(void) {
uint8_t data = 0;
-
+
int ret = eos_i2c_read(DRV2605L_ADDR, DRV2605_REG_STATUS, &data, 1);
if (ret) ESP_LOGE(TAG, "I2C ERROR!");
- ESP_LOGI(TAG, "OK BUFFER: ");
- disp_buf(&data, 1);
-
- eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_MODE, 0x00); // out of standby
- eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_RTPIN, 0x00); // no real-time-playback
+ eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_MODE, 0x00); // out of standby
+ eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_RTPIN, 0x00); // no real-time-playback
- eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_WAVESEQ1, 1); // strong click
- eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_WAVESEQ2, 0); // end sequence
+ eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_WAVESEQ1, 1); // strong click
+ eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_WAVESEQ2, 0); // end sequence
- eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_OVERDRIVE, 0); // no overdrive
+ eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_OVERDRIVE, 0); // no overdrive
eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_SUSTAINPOS, 0);
eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_SUSTAINNEG, 0);
@@ -90,15 +72,10 @@ void eos_drv2605l_test(void) {
eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_AUDIOMAX, 0x64);
// LRA open loop
- // turn on N_ERM_LRA
- eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_FEEDBACK, eos_i2c_read8(DRV2605L_ADDR, DRV2605_REG_FEEDBACK) | 0x80);
-
- // turn on LRA_OPEN_LOOP
- eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_CONTROL3, eos_i2c_read8(DRV2605L_ADDR, DRV2605_REG_CONTROL3) | 0x01);
+ eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_FEEDBACK, eos_i2c_read8(DRV2605L_ADDR, DRV2605_REG_FEEDBACK) | 0x80); // turn on N_ERM_LRA
+ eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_CONTROL3, eos_i2c_read8(DRV2605L_ADDR, DRV2605_REG_CONTROL3) | 0x01); // turn on LRA_OPEN_LOOP
+ eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_LIBRARY, 6); // set LRA library
+ eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_GO, 1); // go
- // set LRA library
- eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_LIBRARY, 6);
- // go
- eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_GO, 1);
}
diff --git a/code/esp32/components/eos/i2c.c b/code/esp32/components/eos/i2c.c
index dcc313d..1182431 100644
--- a/code/esp32/components/eos/i2c.c
+++ b/code/esp32/components/eos/i2c.c
@@ -4,12 +4,12 @@
#include "eos.h"
-static const char *TAG = "I2C";
+static const char *TAG = "EOS I2C";
#define I2C_MASTER_NUM I2C_NUM_0
#define I2C_MASTER_FREQ_HZ 100000
-#define I2C_MASTER_SCL_IO 26
-#define I2C_MASTER_SDA_IO 25
+#define I2C_MASTER_GPIO_SCL 25
+#define I2C_MASTER_GPIO_SDA 26
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
@@ -25,9 +25,9 @@ static const char *TAG = "I2C";
void eos_i2c_init(void) {
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
- conf.sda_io_num = I2C_MASTER_SDA_IO;
+ conf.sda_io_num = I2C_MASTER_GPIO_SDA;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
- conf.scl_io_num = I2C_MASTER_SCL_IO;
+ conf.scl_io_num = I2C_MASTER_GPIO_SCL;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
i2c_param_config(I2C_MASTER_NUM, &conf);
diff --git a/code/esp32/components/eos/include/net.h b/code/esp32/components/eos/include/net.h
index 3ce142a..d6f3f34 100644
--- a/code/esp32/components/eos/include/net.h
+++ b/code/esp32/components/eos/include/net.h
@@ -13,12 +13,15 @@
#define EOS_NET_MTYPE_FLAG_ONEW 0x10
#define EOS_NET_SIZE_BUF 1500
-#define EOS_NET_SIZE_BUFQ 64
+#define EOS_NET_SIZE_BUFQ 8
+#define EOS_NET_SIZE_SNDQ 16
-#define EOS_NET_FLAG_BUF_FREE 0x01
+#define EOS_NET_FLAG_BFREE 0x1
+#define EOS_NET_FLAG_BCOPY 0x2
typedef void (*eos_net_fptr_t) (unsigned char, unsigned char *, uint16_t);
void eos_net_init(void);
+unsigned char *eos_net_alloc(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/net.c b/code/esp32/components/eos/net.c
index bad078a..3c7877d 100644
--- a/code/esp32/components/eos/net.c
+++ b/code/esp32/components/eos/net.c
@@ -19,8 +19,9 @@
#include "msgq.h"
#include "net.h"
-static EOSMsgQ net_send_q;
-static EOSMsgItem net_sndq_array[EOS_NET_SIZE_BUFQ];
+#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
+#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
+#define NET_BUFQ_IDX_MASK(IDX) ((IDX) & (EOS_NET_SIZE_BUFQ - 1))
#define SPI_GPIO_RTS 22
#define SPI_GPIO_CTS 21
@@ -29,9 +30,22 @@ static EOSMsgItem net_sndq_array[EOS_NET_SIZE_BUFQ];
#define SPI_GPIO_SCLK 18
#define SPI_GPIO_CS 5
+typedef struct EOSNetBufQ {
+ uint8_t idx_r;
+ uint8_t idx_w;
+ unsigned char *array[EOS_NET_SIZE_BUFQ];
+} EOSNetBufQ;
+
+static EOSNetBufQ net_buf_q;
+static unsigned char net_bufq_array[EOS_NET_SIZE_BUFQ][EOS_NET_SIZE_BUF];
+
+static EOSMsgQ net_send_q;
+static EOSMsgItem net_sndq_array[EOS_NET_SIZE_SNDQ];
+
static SemaphoreHandle_t mutex;
+static SemaphoreHandle_t semaph;
-static const char *TAG = "EOS";
+static const char *TAG = "EOS NET";
static eos_net_fptr_t mtype_handler[EOS_NET_MAX_MTYPE];
@@ -39,6 +53,25 @@ static void bad_handler(unsigned char mtype, unsigned char *buffer, uint16_t len
ESP_LOGE(TAG, "NET RECV: bad handler: %d", mtype);
}
+static void net_bufq_init(void) {
+ int i;
+
+ net_buf_q.idx_r = 0;
+ net_buf_q.idx_w = EOS_NET_SIZE_BUFQ;
+ for (i=0; i<EOS_NET_SIZE_BUFQ; i++) {
+ net_buf_q.array[i] = net_bufq_array[i];
+ }
+}
+
+static int net_bufq_push(unsigned char *buffer) {
+ net_buf_q.array[NET_BUFQ_IDX_MASK(net_buf_q.idx_w++)] = buffer;
+ return EOS_OK;
+}
+
+static unsigned char *net_bufq_pop(void) {
+ return net_buf_q.array[NET_BUFQ_IDX_MASK(net_buf_q.idx_r++)];
+}
+
static void exchange(void *pvParameters) {
int repeat = 0;
unsigned char mtype = 0;
@@ -64,7 +97,12 @@ static void exchange(void *pvParameters) {
buf_send[1] = len & 0xFF;
if (buffer) {
memcpy(buf_send + 2, buffer, len);
- if (flags & EOS_NET_FLAG_BUF_FREE) free(buffer);
+ if (flags & EOS_NET_FLAG_BFREE) {
+ free(buffer);
+ } else {
+ net_bufq_push(buffer);
+ xSemaphoreGive(semaph);
+ }
}
} else {
WRITE_PERI_REG(GPIO_OUT_W1TC_REG, (1 << SPI_GPIO_RTS));
@@ -77,6 +115,7 @@ static void exchange(void *pvParameters) {
memset(buf_recv, 0, EOS_NET_SIZE_BUF);
spi_slave_transmit(HSPI_HOST, &t, portMAX_DELAY);
+ ESP_LOGI(TAG, "RECV:%d", (buf_recv[0] >> 3));
repeat = 0;
if (buf_recv[0] != 0) {
mtype = (buf_recv[0] >> 3);
@@ -92,10 +131,7 @@ static void exchange(void *pvParameters) {
} else {
bad_handler(mtype, buffer, len);
}
- } else {
- // ESP_LOGI(TAG, "NET RECV NULL");
}
- // vTaskDelay(5000 / portTICK_PERIOD_MS);
}
}
@@ -110,6 +146,7 @@ static void _post_trans_cb(spi_slave_transaction_t *trans) {
}
void eos_net_init(void) {
+ int i;
esp_err_t ret;
// Configuration for the handshake lines
@@ -144,30 +181,46 @@ void eos_net_init(void) {
.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);
+ //Initialize SPI slave interface
+ ret=spi_slave_initialize(HSPI_HOST, &buscfg, &slvcfg, 1);
+ assert(ret==ESP_OK);
+
+ net_bufq_init();
+ eos_msgq_init(&net_send_q, net_sndq_array, EOS_NET_SIZE_SNDQ);
- int i;
for (i=0; i<EOS_NET_MAX_MTYPE; i++) {
mtype_handler[i] = bad_handler;
}
- //Initialize SPI slave interface
- ret=spi_slave_initialize(HSPI_HOST, &buscfg, &slvcfg, 1);
- assert(ret==ESP_OK);
-
- eos_msgq_init(&net_send_q, net_sndq_array, EOS_NET_SIZE_BUFQ);
+ semaph = xSemaphoreCreateCounting(EOS_NET_SIZE_BUFQ, EOS_NET_SIZE_BUFQ);
mutex = xSemaphoreCreateBinary();
xSemaphoreGive(mutex);
- xTaskCreate(&exchange, "net_xchg", 4096, NULL, EOS_IRQ_PRIORITY_NET_XCHG, NULL);
+ xTaskCreate(&exchange, "net_xchg", 2048, NULL, EOS_IRQ_PRIORITY_NET_XCHG, NULL);
+}
+
+unsigned char *eos_net_alloc(void) {
+ unsigned char *ret;
+
+ xSemaphoreTake(semaph, portMAX_DELAY);
+ xSemaphoreTake(mutex, portMAX_DELAY);
+ ret = net_bufq_pop();
+ xSemaphoreGive(mutex);
+
+ return ret;
}
int eos_net_send(unsigned char mtype, unsigned char *buffer, uint16_t len, uint8_t flags) {
+ int rv;
+
+ if (flags & EOS_NET_FLAG_BCOPY) xSemaphoreTake(semaph, portMAX_DELAY);
xSemaphoreTake(mutex, portMAX_DELAY);
WRITE_PERI_REG(GPIO_OUT_W1TS_REG, (1 << SPI_GPIO_RTS));
- int rv = eos_msgq_push(&net_send_q, mtype, buffer, len, flags);
+ if (flags & EOS_NET_FLAG_BCOPY) {
+ unsigned char *b = net_bufq_pop();
+ memcpy(b, buffer, len);
+ buffer = b;
+ }
+ rv = eos_msgq_push(&net_send_q, mtype, buffer, len, flags);
xSemaphoreGive(mutex);
return rv;
diff --git a/code/esp32/components/eos/sock.c b/code/esp32/components/eos/sock.c
index 2ccfb65..ee3c6a3 100644
--- a/code/esp32/components/eos/sock.c
+++ b/code/esp32/components/eos/sock.c
@@ -25,7 +25,7 @@
#include "net.h"
#include "sock.h"
-static const char *TAG = "EOS";
+static const char *TAG = "EOS SOCK";
static SemaphoreHandle_t mutex;
static int _socks[EOS_SOCK_MAX_SOCK];
@@ -80,22 +80,25 @@ static ssize_t t_recvfrom(int sock, void *msg, size_t msg_size, EOSNetAddr *addr
static void sock_receiver(void *pvParameters) {
EOSNetAddr addr;
- unsigned char buffer[EOS_NET_SIZE_BUF];
uint8_t esock = (uint8_t)pvParameters;
int sock = _socks[esock-1];
+ unsigned char *buf;
- buffer[0] = EOS_SOCK_MTYPE_PKT;
- buffer[1] = esock;
do {
- ssize_t rv = t_recvfrom(sock, buffer+EOS_SOCK_SIZE_UDP_HDR, EOS_NET_SIZE_BUF-EOS_SOCK_SIZE_UDP_HDR, &addr);
+ ssize_t rv;
+
+ buf = eos_net_alloc();
+ rv = t_recvfrom(sock, buf+EOS_SOCK_SIZE_UDP_HDR, EOS_NET_SIZE_BUF-EOS_SOCK_SIZE_UDP_HDR, &addr);
if (rv < 0) {
sock = 0;
ESP_LOGE(TAG, "UDP RECV ERR:%d", rv);
continue;
}
- memcpy(buffer+2, addr.host, sizeof(addr.host));
- memcpy(buffer+2+sizeof(addr.host), &addr.port, sizeof(addr.port));
- eos_net_send(EOS_NET_MTYPE_SOCK, buffer, rv+EOS_SOCK_SIZE_UDP_HDR, 0);
+ buf[0] = EOS_SOCK_MTYPE_PKT;
+ buf[1] = esock;
+ memcpy(buf+2, addr.host, sizeof(addr.host));
+ memcpy(buf+2+sizeof(addr.host), &addr.port, sizeof(addr.port));
+ eos_net_send(EOS_NET_MTYPE_SOCK, buf, rv+EOS_SOCK_SIZE_UDP_HDR, 0);
} while(sock);
xSemaphoreTake(mutex, portMAX_DELAY);
_socks[esock-1] = 0;
@@ -105,9 +108,9 @@ static void sock_receiver(void *pvParameters) {
static void sock_handler(unsigned char type, unsigned char *buffer, uint16_t size) {
EOSNetAddr addr;
- unsigned char ret[2];
uint8_t esock;
int sock, i;
+ unsigned char *rbuf;
if (size < 1) return;
@@ -119,6 +122,7 @@ static void sock_handler(unsigned char type, unsigned char *buffer, uint16_t siz
memcpy(&addr.port, buffer+2+sizeof(addr.host), sizeof(addr.port));
t_sendto(sock, buffer+EOS_SOCK_SIZE_UDP_HDR, size-EOS_SOCK_SIZE_UDP_HDR, &addr);
break;
+
case EOS_SOCK_MTYPE_OPEN_DGRAM:
sock = t_open_dgram();
esock = 0;
@@ -132,17 +136,20 @@ static void sock_handler(unsigned char type, unsigned char *buffer, uint16_t siz
}
xSemaphoreGive(mutex);
}
- ret[0] = EOS_SOCK_MTYPE_OPEN_DGRAM;
- ret[1] = esock;
// xTaskCreatePinnedToCore(&sock_receiver, "sock_receiver", 2048, NULL, EOS_IRQ_PRIORITY_UDP_RCVR, NULL, 1);
xTaskCreate(&sock_receiver, "sock_receiver", 2048, (void *)esock, EOS_IRQ_PRIORITY_UDP_RCVR, NULL);
- eos_net_send(EOS_NET_MTYPE_SOCK, ret, 2, 0);
+ rbuf = eos_net_alloc();
+ rbuf[0] = EOS_SOCK_MTYPE_OPEN_DGRAM;
+ rbuf[1] = esock;
+ eos_net_send(EOS_NET_MTYPE_SOCK, rbuf, 2, 0);
break;
+
case EOS_SOCK_MTYPE_CLOSE:
if (size < 2) return;
sock = _socks[buffer[1]-1];
t_close(sock);
break;
+
default:
break;
}
diff --git a/code/esp32/components/eos/wifi.c b/code/esp32/components/eos/wifi.c
index 628c4fd..29cd983 100755
--- a/code/esp32/components/eos/wifi.c
+++ b/code/esp32/components/eos/wifi.c
@@ -17,9 +17,11 @@
#include "net.h"
#include "wifi.h"
-static const char *TAG = "EOS";
+static const char *TAG = "EOS WIFI";
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) {
+ unsigned char *rbuf;
+
switch(event->event_id) {
case SYSTEM_EVENT_SCAN_DONE:
break;
@@ -42,9 +44,9 @@ static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) {
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);
+ rbuf = eos_net_alloc();
+ rbuf[0] = EOS_WIFI_MTYPE_CONNECT;
+ eos_net_send(EOS_NET_MTYPE_WIFI, rbuf, 1, 0);
break;
default: // Ignore the other event types
@@ -70,7 +72,6 @@ static void wifi_handler(unsigned char _mtype, unsigned char *buffer, uint16_t s
eos_wifi_disconnect();
break;
}
- // eos_wifi_connect((char *)buffer, (char *)(buffer+strlen((char *)buffer)+1));
}
void eos_wifi_init(void) {