diff options
author | Uros Majstorovic <majstor@majstor.org> | 2021-02-24 19:54:18 +0100 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2021-02-24 19:54:18 +0100 |
commit | 4dea6262b57f614cabd5cf4fa96c1e3d83c45fb4 (patch) | |
tree | 765eb9b4322c6158089232cccf6a30eb62ba90d9 | |
parent | 3050565531af2b3a09f2213893f10c64cf9fe43f (diff) |
upgraded to esp-idf 4.2
-rw-r--r-- | fw/esp32/components/eos/bq25895.c | 85 | ||||
-rw-r--r-- | fw/esp32/components/eos/cell.c | 6 | ||||
-rw-r--r-- | fw/esp32/components/eos/cell_modem.c | 90 | ||||
-rw-r--r-- | fw/esp32/components/eos/cell_pcm.c | 6 | ||||
-rw-r--r-- | fw/esp32/components/eos/cell_voice.c | 4 | ||||
-rw-r--r-- | fw/esp32/components/eos/drv2605l.c | 9 | ||||
-rw-r--r-- | fw/esp32/components/eos/i2c.c | 35 | ||||
-rw-r--r-- | fw/esp32/components/eos/include/cell.h | 31 | ||||
-rw-r--r-- | fw/esp32/components/eos/include/i2c.h | 4 | ||||
-rw-r--r-- | fw/esp32/components/eos/include/power.h | 2 | ||||
-rw-r--r-- | fw/esp32/components/eos/include/wifi.h | 8 | ||||
-rw-r--r-- | fw/esp32/components/eos/sock.c | 4 | ||||
-rwxr-xr-x | fw/esp32/components/eos/wifi.c | 195 | ||||
-rw-r--r-- | fw/esp32/main/app_main.c | 14 |
14 files changed, 278 insertions, 215 deletions
diff --git a/fw/esp32/components/eos/bq25895.c b/fw/esp32/components/eos/bq25895.c index 4501888..2c9a551 100644 --- a/fw/esp32/components/eos/bq25895.c +++ b/fw/esp32/components/eos/bq25895.c @@ -11,50 +11,51 @@ static const char *TAG = "EOS BQ25895"; void eos_bq25895_set_ilim(void) { uint8_t data = 0; + int ret = EOS_OK; eos_i2c_write8(BQ25895_ADDR, 0, 0x26); // input current: 2.0 A 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); - 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); + ret = eos_i2c_read8(BQ25895_ADDR, 0x00, &data); + if (!ret) ESP_LOGI(TAG, "REG00: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x01, &data); + if (!ret) ESP_LOGI(TAG, "REG01: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x02, &data); + if (!ret) ESP_LOGI(TAG, "REG02: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x03, &data); + if (!ret) ESP_LOGI(TAG, "REG03: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x04, &data); + if (!ret) ESP_LOGI(TAG, "REG04: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x05, &data); + if (!ret) ESP_LOGI(TAG, "REG05: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x06, &data); + if (!ret) ESP_LOGI(TAG, "REG06: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x07, &data); + if (!ret) ESP_LOGI(TAG, "REG07: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x08, &data); + if (!ret) ESP_LOGI(TAG, "REG08: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x09, &data); + if (!ret) ESP_LOGI(TAG, "REG09: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x0a, &data); + if (!ret) ESP_LOGI(TAG, "REG0A: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x0b, &data); + if (!ret) ESP_LOGI(TAG, "REG0B: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x0c, &data); + if (!ret) ESP_LOGI(TAG, "REG0C: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x0d, &data); + if (!ret) ESP_LOGI(TAG, "REG0D: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x0e, &data); + if (!ret) ESP_LOGI(TAG, "REG0E: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x0f, &data); + if (!ret) ESP_LOGI(TAG, "REG0F: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x10, &data); + if (!ret) ESP_LOGI(TAG, "REG10: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x11, &data); + if (!ret) ESP_LOGI(TAG, "REG11: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x12, &data); + if (!ret) ESP_LOGI(TAG, "REG12: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x13, &data); + if (!ret) ESP_LOGI(TAG, "REG13: %02x", data); + ret = eos_i2c_read8(BQ25895_ADDR, 0x14, &data); + if (!ret) ESP_LOGI(TAG, "REG14: %02x", data); } diff --git a/fw/esp32/components/eos/cell.c b/fw/esp32/components/eos/cell.c index 11a96e8..886cc7a 100644 --- a/fw/esp32/components/eos/cell.c +++ b/fw/esp32/components/eos/cell.c @@ -32,7 +32,11 @@ static void _cell_handler(unsigned char _mtype, unsigned char *buffer, uint16_t mtype = buffer[0]; switch (mtype & EOS_CELL_MTYPE_MASK) { case EOS_CELL_MTYPE_DEV: - switch (mtype) { + switch (mtype & ~EOS_CELL_MTYPE_MASK) { + case EOS_CELL_MTYPE_RESET: + eos_modem_reset(); + break; + case EOS_CELL_MTYPE_UART_DATA: if (eos_modem_get_mode() == EOS_CELL_UART_MODE_RELAY) eos_modem_write(buffer+1, size-1); break; diff --git a/fw/esp32/components/eos/cell_modem.c b/fw/esp32/components/eos/cell_modem.c index 0a4c754..3bbce90 100644 --- a/fw/esp32/components/eos/cell_modem.c +++ b/fw/esp32/components/eos/cell_modem.c @@ -47,6 +47,7 @@ static char *at_cmd_init[AT_CMD_INIT_SIZE] = { "AT+CPMS=\"ME\",\"ME\",\"ME\"\r" }; +static char modem_initialized = 0; static SemaphoreHandle_t mutex; static QueueHandle_t modem_queue; @@ -107,7 +108,7 @@ static void uart_data_read(uint8_t mode) { int _rd; buf = eos_net_alloc(); - buf[0] = EOS_CELL_MTYPE_UART_DATA; + buf[0] = EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_UART_DATA; _rd = eos_modem_read(buf + 1, MIN(bsize - rd, EOS_NET_SIZE_BUF - 1), 100); eos_net_send(EOS_NET_MTYPE_CELL, buf, _rd + 1); rd += _rd; @@ -170,15 +171,28 @@ static void IRAM_ATTR uart_ri_isr_handler(void *arg) { xQueueSendFromISR(modem_queue, &evt, NULL); } +static void modem_set_mode(uint8_t mode) { + uart_event_t evt; + + evt.type = UART_EEVT_MODE; + evt.size = mode; + xQueueSend(uart_queue, &evt, portMAX_DELAY); +} + static int modem_atcmd_init(void) { unsigned char *buf; int echo_on = 0; int tries = 3; int i, r; - int rv = EOS_OK; - rv = eos_modem_take(1000); - if (rv) return rv; + xSemaphoreTake(mutex, portMAX_DELAY); + modem_set_mode(EOS_CELL_UART_MODE_NONE); + r = xSemaphoreTake(uart_mutex, 1000); + if (r == pdFALSE) { + modem_set_mode(uart_mode); + xSemaphoreGive(mutex); + return EOS_ERR_TIMEOUT; + } do { at_cmd("AT\r"); @@ -194,7 +208,10 @@ static int modem_atcmd_init(void) { } while (tries); if (tries == 0) { - eos_modem_give(); + modem_set_mode(uart_mode); + xSemaphoreGive(uart_mutex); + xSemaphoreGive(mutex); + return EOS_ERR_TIMEOUT; } @@ -205,7 +222,6 @@ static int modem_atcmd_init(void) { } else { at_cmd("AT&F\r"); r = at_expect("^OK", NULL, 1000); - } at_cmd("ATE0\r"); r = at_expect("^ATE0", NULL, 1000); @@ -217,14 +233,18 @@ static int modem_atcmd_init(void) { } buf = eos_net_alloc(); - buf[0] = EOS_CELL_MTYPE_READY; + buf[0] = EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_READY; eos_net_send(EOS_NET_MTYPE_CELL, buf, 1); - eos_modem_give(); + modem_initialized = 1; + modem_set_mode(uart_mode); + xSemaphoreGive(uart_mutex); + xSemaphoreGive(mutex); return EOS_OK; } + static void modem_atcmd_read(size_t bsize) { char *ln_end; int rd = 0; @@ -265,14 +285,6 @@ static void modem_urc_init_handler(char *urc, regmatch_t m[]) { xQueueSend(modem_queue, &evt, portMAX_DELAY); } -static void modem_set_mode(uint8_t mode) { - uart_event_t evt; - - evt.type = UART_EEVT_MODE; - evt.size = mode; - xQueueSend(uart_queue, &evt, portMAX_DELAY); -} - static void modem_event_task(void *pvParameters) { modem_event_t evt; @@ -340,7 +352,9 @@ static uint32_t ppp_output_cb(ppp_pcb *pcb, uint8_t *data, uint32_t len, void *c /* PPP status callback */ static void ppp_status_cb(ppp_pcb *pcb, int err_code, void *ctx) { + unsigned char *rbuf; struct netif *pppif = ppp_netif(pcb); + LWIP_UNUSED_ARG(ctx); switch(err_code) { @@ -349,6 +363,10 @@ static void ppp_status_cb(ppp_pcb *pcb, int err_code, void *ctx) { ESP_LOGI(TAG," our_ipaddr = %s\n", ipaddr_ntoa(&pppif->ip_addr)); ESP_LOGI(TAG," his_ipaddr = %s\n", ipaddr_ntoa(&pppif->gw)); ESP_LOGI(TAG," netmask = %s\n", ipaddr_ntoa(&pppif->netmask)); + rbuf = eos_net_alloc(); + rbuf[0] = EOS_CELL_MTYPE_DATA | EOS_CELL_MTYPE_DATA_CONNECT; + rbuf[1] = EOS_OK; + eos_net_send(EOS_NET_MTYPE_CELL, rbuf, 2); return; } case PPPERR_PARAM: { @@ -703,20 +721,21 @@ int eos_modem_take(uint32_t timeout) { int rv = EOS_OK; xSemaphoreTake(mutex, portMAX_DELAY); + if (!modem_initialized) rv = EOS_ERR_BUSY; + if (!rv) { + if (uart_mode == EOS_CELL_UART_MODE_PPP) { + rv = ppp_pause(timeout); + } else { + int r; - if (uart_mode == EOS_CELL_UART_MODE_PPP) { - rv = ppp_pause(timeout); - } else { - int r; - - modem_set_mode(EOS_CELL_UART_MODE_NONE); - r = xSemaphoreTake(uart_mutex, timeout ? timeout / portTICK_PERIOD_MS : portMAX_DELAY); - if (r == pdFALSE) { - modem_set_mode(uart_mode); - rv = EOS_ERR_TIMEOUT; + modem_set_mode(EOS_CELL_UART_MODE_NONE); + r = xSemaphoreTake(uart_mutex, timeout ? timeout / portTICK_PERIOD_MS : portMAX_DELAY); + if (r == pdFALSE) { + modem_set_mode(uart_mode); + rv = EOS_ERR_TIMEOUT; + } } } - if (rv) xSemaphoreGive(mutex); return rv; @@ -769,6 +788,21 @@ void eos_modem_wake(uint8_t source, uint8_t mode) { } } +int eos_modem_reset(void) { + int rv; + + rv = eos_modem_take(1000); + if (rv) return rv; + + at_cmd("AT+CRESET\r"); + at_expect("^OK", NULL, 1000); + uart_mode = EOS_CELL_UART_MODE_ATCMD; + modem_initialized = 0; + eos_modem_give(); + + return EOS_OK; +} + void eos_ppp_set_apn(char *apn) { xSemaphoreTake(mutex, portMAX_DELAY); strncpy(ppp_apn, apn, sizeof(ppp_apn) - 1); @@ -788,4 +822,4 @@ int eos_ppp_connect(void) { void eos_ppp_disconnect(void) { eos_modem_set_mode(EOS_CELL_UART_MODE_ATCMD); -}
\ No newline at end of file +} diff --git a/fw/esp32/components/eos/cell_pcm.c b/fw/esp32/components/eos/cell_pcm.c index f8d41b8..4930d66 100644 --- a/fw/esp32/components/eos/cell_pcm.c +++ b/fw/esp32/components/eos/cell_pcm.c @@ -61,14 +61,14 @@ static void i2s_event_task(void *pvParameters) { // Event of I2S receiving data if (!hold_cnt) { buf = eos_net_alloc(); - buf[0] = EOS_CELL_MTYPE_PCM_DATA; + buf[0] = EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_PCM_DATA; bytes_r = eos_cell_pcm_read(buf + 1, PCM_MIC_WM); eos_net_send(EOS_NET_MTYPE_CELL, buf, bytes_r + 1); } else { hold_cnt--; if (hold_buf == NULL) { hold_buf = eos_net_alloc(); - hold_buf[0] = EOS_CELL_MTYPE_PCM_DATA; + hold_buf[0] = EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_PCM_DATA; } if (1 + hold_bytes_r + PCM_MIC_WM <= EOS_NET_SIZE_BUF) hold_bytes_r += eos_cell_pcm_read(hold_buf + 1 + hold_bytes_r, PCM_MIC_WM); if (hold_cnt == 0) { @@ -113,7 +113,7 @@ void eos_cell_pcm_init(void) { .sample_rate = 32000, .bits_per_sample = 32, .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, - .communication_format = I2S_COMM_FORMAT_I2S, + .communication_format = I2S_COMM_FORMAT_STAND_I2S, .dma_buf_count = 4, .dma_buf_len = PCM_MIC_WM, .use_apll = true, diff --git a/fw/esp32/components/eos/cell_voice.c b/fw/esp32/components/eos/cell_voice.c index d28c38c..f820b63 100644 --- a/fw/esp32/components/eos/cell_voice.c +++ b/fw/esp32/components/eos/cell_voice.c @@ -106,13 +106,11 @@ static void call_end_handler(char *urc, regmatch_t m[]) { static void call_missed_handler(char *urc, regmatch_t m[]) { unsigned char *buf; uint16_t len; - int duration = 0; eos_cell_pcm_stop(); - sscanf(urc + m[1].rm_so, "%6d", &duration); buf = eos_net_alloc(); - buf[0] = EOS_CELL_MTYPE_VOICE | EOS_CELL_MTYPE_VOICE_MISSED; + buf[0] = EOS_CELL_MTYPE_VOICE | EOS_CELL_MTYPE_VOICE_MISS; urc[m[1].rm_eo] = '\0'; strcpy((char *)buf + 1, urc + m[1].rm_so); len = 2 + m[1].rm_eo - m[1].rm_so; diff --git a/fw/esp32/components/eos/drv2605l.c b/fw/esp32/components/eos/drv2605l.c index 3944289..8cf17cd 100644 --- a/fw/esp32/components/eos/drv2605l.c +++ b/fw/esp32/components/eos/drv2605l.c @@ -55,8 +55,9 @@ static const char *TAG = "EOS DRV2605L"; void eos_drv2605l_test(void) { uint8_t data = 0; + int ret; - int ret = eos_i2c_read(DRV2605L_ADDR, DRV2605_REG_STATUS, &data, 1); + ret = eos_i2c_read8(DRV2605L_ADDR, DRV2605_REG_STATUS, &data); if (ret) ESP_LOGE(TAG, "I2C ERROR!"); eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_MODE, 0x00); // out of standby @@ -73,8 +74,10 @@ void eos_drv2605l_test(void) { eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_AUDIOMAX, 0x64); // LRA open loop - 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_read8(DRV2605L_ADDR, DRV2605_REG_FEEDBACK, &data); + eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_FEEDBACK, data | 0x80); // turn on N_ERM_LRA + eos_i2c_read8(DRV2605L_ADDR, DRV2605_REG_CONTROL3, &data); + eos_i2c_write8(DRV2605L_ADDR, DRV2605_REG_CONTROL3, data | 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 diff --git a/fw/esp32/components/eos/i2c.c b/fw/esp32/components/eos/i2c.c index 5b8fcc7..828e4cd 100644 --- a/fw/esp32/components/eos/i2c.c +++ b/fw/esp32/components/eos/i2c.c @@ -19,10 +19,6 @@ static const char *TAG = "EOS I2C"; #define ACK_VAL 0x0 /*!< I2C ack value */ #define NCK_VAL 0x1 /*!< I2C nack value */ -/** - * @brief i2c initialization - */ - void eos_i2c_init(void) { i2c_config_t conf; conf.mode = I2C_MODE_MASTER; @@ -36,10 +32,6 @@ void eos_i2c_init(void) { ESP_LOGI(TAG, "INIT"); } -/** - * @brief i2c read - */ - int eos_i2c_read(uint8_t addr, uint8_t reg, uint8_t *data, size_t len) { int i, ret; i2c_cmd_handle_t cmd = i2c_cmd_link_create(); @@ -53,6 +45,7 @@ int eos_i2c_read(uint8_t addr, uint8_t reg, uint8_t *data, size_t len) { } i2c_master_read_byte(cmd, data+i, NCK_VAL); i2c_master_stop(cmd); + ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS); i2c_cmd_link_delete(cmd); if (ret != ESP_OK) { @@ -61,20 +54,6 @@ int eos_i2c_read(uint8_t addr, uint8_t reg, uint8_t *data, size_t len) { return EOS_OK; } -/** - * @brief i2c read8 - */ - -uint8_t eos_i2c_read8(uint8_t addr, uint8_t reg) { - uint8_t data; - eos_i2c_read(addr, reg, &data, 1); - return data; -} - -/** - * @brief i2c write - */ - int eos_i2c_write(uint8_t addr, uint8_t reg, uint8_t *data, size_t len) { int i, ret; i2c_cmd_handle_t cmd = i2c_cmd_link_create(); @@ -85,6 +64,7 @@ int eos_i2c_write(uint8_t addr, uint8_t reg, uint8_t *data, size_t len) { i2c_master_write_byte(cmd, *(data+i), ACK_CHECK_EN); } i2c_master_stop(cmd); + ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS); i2c_cmd_link_delete(cmd); if (ret != ESP_OK) { @@ -93,11 +73,10 @@ int eos_i2c_write(uint8_t addr, uint8_t reg, uint8_t *data, size_t len) { return EOS_OK; } -/** - * @brief i2c write8 - */ - -void eos_i2c_write8(uint8_t addr, uint8_t reg, uint8_t data) { - eos_i2c_write(addr, reg, &data, 1); +int eos_i2c_read8(uint8_t addr, uint8_t reg, uint8_t *data) { + return eos_i2c_read(addr, reg, data, 1); } +int eos_i2c_write8(uint8_t addr, uint8_t reg, uint8_t data) { + return eos_i2c_write(addr, reg, &data, 1); +} diff --git a/fw/esp32/components/eos/include/cell.h b/fw/esp32/components/eos/include/cell.h index 89f3372..07d1144 100644 --- a/fw/esp32/components/eos/include/cell.h +++ b/fw/esp32/components/eos/include/cell.h @@ -1,23 +1,25 @@ #include <sys/types.h> #include <stdint.h> -#define EOS_CELL_MTYPE_DEV 0x00 -#define EOS_CELL_MTYPE_VOICE 0x10 -#define EOS_CELL_MTYPE_SMS 0x20 -#define EOS_CELL_MTYPE_CBS 0x30 -#define EOS_CELL_MTYPE_USSD 0x40 +#define EOS_CELL_MTYPE_DEV 0x10 +#define EOS_CELL_MTYPE_VOICE 0x20 +#define EOS_CELL_MTYPE_SMS 0x30 +#define EOS_CELL_MTYPE_CBS 0x40 +#define EOS_CELL_MTYPE_USSD 0x50 #define EOS_CELL_MTYPE_DATA 0x70 #define EOS_CELL_MTYPE_MASK 0xf0 #define EOS_CELL_MAX_MTYPE 8 -#define EOS_CELL_MTYPE_READY 0 -#define EOS_CELL_MTYPE_UART_DATA 1 -#define EOS_CELL_MTYPE_UART_TAKE 2 -#define EOS_CELL_MTYPE_UART_GIVE 3 -#define EOS_CELL_MTYPE_PCM_DATA 4 -#define EOS_CELL_MTYPE_PCM_START 5 -#define EOS_CELL_MTYPE_PCM_STOP 6 +/* EOS_CELL_MTYPE_DEV subtypes */ +#define EOS_CELL_MTYPE_READY 1 +#define EOS_CELL_MTYPE_UART_DATA 2 +#define EOS_CELL_MTYPE_UART_TAKE 3 +#define EOS_CELL_MTYPE_UART_GIVE 4 +#define EOS_CELL_MTYPE_PCM_DATA 5 +#define EOS_CELL_MTYPE_PCM_START 6 +#define EOS_CELL_MTYPE_PCM_STOP 7 +#define EOS_CELL_MTYPE_RESET 8 #define EOS_CELL_MTYPE_VOICE_DIAL 1 #define EOS_CELL_MTYPE_VOICE_RING 2 @@ -25,7 +27,9 @@ #define EOS_CELL_MTYPE_VOICE_HANGUP 4 #define EOS_CELL_MTYPE_VOICE_BEGIN 5 #define EOS_CELL_MTYPE_VOICE_END 6 -#define EOS_CELL_MTYPE_VOICE_MISSED 7 +#define EOS_CELL_MTYPE_VOICE_MISS 7 +#define EOS_CELL_MTYPE_VOICE_BUSY 8 +#define EOS_CELL_MTYPE_VOICE_ERR 9 #define EOS_CELL_MTYPE_SMS_LIST 1 #define EOS_CELL_MTYPE_SMS_SEND 2 @@ -66,6 +70,7 @@ int eos_modem_take(uint32_t timeout); void eos_modem_give(void); void eos_modem_sleep(uint8_t mode); void eos_modem_wake(uint8_t source, uint8_t mode); +int eos_modem_reset(void); void eos_ppp_set_apn(char *apn); void eos_ppp_set_auth(char *user, char *pass); diff --git a/fw/esp32/components/eos/include/i2c.h b/fw/esp32/components/eos/include/i2c.h index 144f5e1..f014141 100644 --- a/fw/esp32/components/eos/include/i2c.h +++ b/fw/esp32/components/eos/include/i2c.h @@ -4,6 +4,6 @@ void eos_i2c_init(void); int eos_i2c_read(uint8_t addr, uint8_t reg, uint8_t *data, size_t len); -uint8_t eos_i2c_read8(uint8_t addr, uint8_t reg); int eos_i2c_write(uint8_t addr, uint8_t reg, uint8_t *data, size_t len); -void eos_i2c_write8(uint8_t addr, uint8_t reg, uint8_t data); +int eos_i2c_read8(uint8_t addr, uint8_t reg, uint8_t *data); +int eos_i2c_write8(uint8_t addr, uint8_t reg, uint8_t data); diff --git a/fw/esp32/components/eos/include/power.h b/fw/esp32/components/eos/include/power.h index 0a57b19..3d2e0bc 100644 --- a/fw/esp32/components/eos/include/power.h +++ b/fw/esp32/components/eos/include/power.h @@ -1,6 +1,6 @@ #include <stdint.h> -#define EOS_PWR_MTYPE_BUTTON 0 +#define EOS_PWR_MTYPE_BUTTON 1 #define EOS_PWR_WAKE_RST 0 #define EOS_PWR_WAKE_BTN 1 diff --git a/fw/esp32/components/eos/include/wifi.h b/fw/esp32/components/eos/include/wifi.h index 6009f7c..d579fc5 100644 --- a/fw/esp32/components/eos/include/wifi.h +++ b/fw/esp32/components/eos/include/wifi.h @@ -1,8 +1,8 @@ -#define EOS_WIFI_MTYPE_SCAN 0 -#define EOS_WIFI_MTYPE_CONNECT 1 -#define EOS_WIFI_MTYPE_DISCONNECT 2 +#define EOS_WIFI_MTYPE_SCAN 1 +#define EOS_WIFI_MTYPE_CONNECT 2 +#define EOS_WIFI_MTYPE_DISCONNECT 3 -#define EOS_WIFI_MAX_MTYPE 3 +#define EOS_WIFI_MAX_MTYPE 4 void eos_wifi_init(void); diff --git a/fw/esp32/components/eos/sock.c b/fw/esp32/components/eos/sock.c index 2691a67..54b09b0 100644 --- a/fw/esp32/components/eos/sock.c +++ b/fw/esp32/components/eos/sock.c @@ -7,12 +7,8 @@ #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> diff --git a/fw/esp32/components/eos/wifi.c b/fw/esp32/components/eos/wifi.c index 03e3ab4..a20e076 100755 --- a/fw/esp32/components/eos/wifi.c +++ b/fw/esp32/components/eos/wifi.c @@ -7,10 +7,10 @@ #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 "eos.h" #include "net.h" @@ -43,104 +43,134 @@ static wifi_scan_config_t wifi_scan_config; static int wifi_connect_cnt = 0; static uint8_t wifi_action; static uint8_t wifi_state; +static wifi_ap_record_t scan_r[WIFI_MAX_SCAN_RECORDS]; +static uint16_t scan_n; -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) { +static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { esp_err_t ret = ESP_OK; char _disconnect; uint8_t _action, _state; - unsigned char *rbuf; - wifi_ap_record_t scan_r[WIFI_MAX_SCAN_RECORDS]; - uint16_t scan_n = WIFI_MAX_SCAN_RECORDS; + unsigned char *rbuf, *p; + int i, len; + ip_event_got_ip_t *got_ip; - switch(event->event_id) { - case WIFI_EVENT_SCAN_DONE: - memset(scan_r, 0, sizeof(scan_r)); - esp_wifi_scan_get_ap_records(&scan_n, scan_r); + if (event_base == WIFI_EVENT) { + switch(event_id) { + case WIFI_EVENT_SCAN_DONE: + scan_n = WIFI_MAX_SCAN_RECORDS; + memset(scan_r, 0, sizeof(scan_r)); + esp_wifi_scan_get_ap_records(&scan_n, scan_r); - xSemaphoreTake(mutex, portMAX_DELAY); - _state = wifi_state; - if (wifi_state == WIFI_STATE_CONNECTED) wifi_action = WIFI_ACTION_NONE; - xSemaphoreGive(mutex); + ESP_LOGI(TAG, "Scan done: %d", scan_n); + xSemaphoreTake(mutex, portMAX_DELAY); + _state = wifi_state; + if (wifi_state == WIFI_STATE_CONNECTED) wifi_action = WIFI_ACTION_NONE; + xSemaphoreGive(mutex); - if (_state != WIFI_STATE_CONNECTED) ret = esp_wifi_stop(); - break; - - case WIFI_EVENT_STA_START: - xSemaphoreTake(mutex, portMAX_DELAY); - _action = wifi_action; - xSemaphoreGive(mutex); + if (_state != WIFI_STATE_CONNECTED) ret = esp_wifi_stop(); - switch (_action) { - case WIFI_ACTION_SCAN: - ret = esp_wifi_scan_start(&wifi_scan_config, 0); - break; + rbuf = eos_net_alloc(); + rbuf[0] = EOS_WIFI_MTYPE_SCAN; + p = rbuf + 1; + for (i=0; i<scan_n; i++) { + len = strlen((char *)scan_r[i].ssid); + if (p - rbuf + len + 1 > EOS_NET_SIZE_BUF) break; + strcpy((char *)p, (char *)scan_r[i].ssid); + p += len + 1; + } + eos_net_send(EOS_NET_MTYPE_WIFI, rbuf, p - rbuf); + break; - case WIFI_ACTION_CONNECT: - ret = esp_wifi_connect(); - break; + case WIFI_EVENT_STA_START: + xSemaphoreTake(mutex, portMAX_DELAY); + _action = wifi_action; + xSemaphoreGive(mutex); - default: - break; - } - break; + switch (_action) { + case WIFI_ACTION_SCAN: + ret = esp_wifi_scan_start(&wifi_scan_config, 0); + break; - case WIFI_EVENT_STA_STOP: - xSemaphoreTake(mutex, portMAX_DELAY); - wifi_state = WIFI_STATE_STOPPED; - wifi_action = WIFI_ACTION_NONE; - xSemaphoreGive(mutex); - break; + case WIFI_ACTION_CONNECT: + ret = esp_wifi_connect(); + break; - case WIFI_EVENT_STA_CONNECTED: - xSemaphoreTake(mutex, portMAX_DELAY); - wifi_state = WIFI_STATE_CONNECTED; - wifi_action = WIFI_ACTION_NONE; - wifi_connect_cnt = WIFI_MAX_CONNECT_ATTEMPTS; - xSemaphoreGive(mutex); - break; - - case WIFI_EVENT_STA_DISCONNECTED: - xSemaphoreTake(mutex, portMAX_DELAY); - if (wifi_connect_cnt) wifi_connect_cnt--; - _action = wifi_action; - _disconnect = (wifi_connect_cnt == 0); - if (_disconnect) wifi_state = WIFI_STATE_DISCONNECTED; - xSemaphoreGive(mutex); - - if (_disconnect) { - rbuf = eos_net_alloc(); - if (_action == WIFI_ACTION_CONNECT) { - rbuf[0] = EOS_WIFI_MTYPE_CONNECT; - rbuf[1] = EOS_ERR; - eos_net_send(EOS_NET_MTYPE_WIFI, rbuf, 2); + default: + break; + } + break; + + case WIFI_EVENT_STA_STOP: + xSemaphoreTake(mutex, portMAX_DELAY); + wifi_state = WIFI_STATE_STOPPED; + wifi_action = WIFI_ACTION_NONE; + xSemaphoreGive(mutex); + break; + + case WIFI_EVENT_STA_CONNECTED: + xSemaphoreTake(mutex, portMAX_DELAY); + wifi_state = WIFI_STATE_CONNECTED; + wifi_action = WIFI_ACTION_NONE; + wifi_connect_cnt = WIFI_MAX_CONNECT_ATTEMPTS; + xSemaphoreGive(mutex); + break; + + case WIFI_EVENT_STA_DISCONNECTED: + xSemaphoreTake(mutex, portMAX_DELAY); + if (wifi_connect_cnt) wifi_connect_cnt--; + _action = wifi_action; + _disconnect = (wifi_connect_cnt == 0); + if (_disconnect) wifi_state = WIFI_STATE_DISCONNECTED; + xSemaphoreGive(mutex); + + if (_disconnect) { + rbuf = eos_net_alloc(); + if (_action == WIFI_ACTION_CONNECT) { + rbuf[0] = EOS_WIFI_MTYPE_CONNECT; + rbuf[1] = EOS_ERR; + eos_net_send(EOS_NET_MTYPE_WIFI, rbuf, 2); + } else { + rbuf[0] = EOS_WIFI_MTYPE_DISCONNECT; + eos_net_send(EOS_NET_MTYPE_WIFI, rbuf, 1); + } + if (!_action) ret = esp_wifi_stop(); } else { - rbuf[0] = EOS_WIFI_MTYPE_DISCONNECT; - eos_net_send(EOS_NET_MTYPE_WIFI, rbuf, 1); + ret = esp_wifi_connect(); } - if (!_action) ret = esp_wifi_stop(); - } else { - ret = esp_wifi_connect(); - } - break; - - case IP_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "IP address: " IPSTR, IP2STR(&event->event_info.got_ip.ip_info.ip)); - if (event->event_info.got_ip.ip_changed) { - // send wifi reconnect - } else { + break; + + default: // Ignore the other event types + break; + } + } else if (event_base == IP_EVENT) { + switch(event_id) { + case IP_EVENT_STA_GOT_IP: + got_ip = (ip_event_got_ip_t *)event_data; + ESP_LOGI(TAG, "IP address: " IPSTR, IP2STR(&got_ip->ip_info.ip)); rbuf = eos_net_alloc(); rbuf[0] = EOS_WIFI_MTYPE_CONNECT; rbuf[1] = EOS_OK; eos_net_send(EOS_NET_MTYPE_WIFI, rbuf, 2); - } - break; - default: // Ignore the other event types - break; + /* ip_changed is set even at normal connect! + if (got_ip->ip_changed) { + ESP_LOGI(TAG, "IP changed"); + // send wifi reconnect + } else { + rbuf = eos_net_alloc(); + rbuf[0] = EOS_WIFI_MTYPE_CONNECT; + rbuf[1] = EOS_OK; + eos_net_send(EOS_NET_MTYPE_WIFI, rbuf, 2); + } + */ + break; + + default: // Ignore the other event types + break; + } } - if (ret != ESP_OK) ESP_LOGE(TAG, "EVT HANDLER ERR:%d EVT:%d", ret, event->event_id); - return ESP_OK; + if (ret != ESP_OK) ESP_LOGE(TAG, "EVT HANDLER ERR:%d EVT:%d", ret, event_id); } static void wifi_handler(unsigned char _mtype, unsigned char *buffer, uint16_t size) { @@ -173,12 +203,17 @@ void eos_wifi_init(void) { memset(&wifi_sta_config, 0, sizeof(wifi_sta_config)); - ret = esp_event_loop_init(wifi_event_handler, NULL); - assert(ret == ESP_OK); + esp_netif_create_default_wifi_sta(); ret = esp_wifi_init(&wifi_config); assert(ret == ESP_OK); + ret = esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL); + assert(ret == ESP_OK); + + ret = esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, NULL); + assert(ret == ESP_OK); + ret = esp_wifi_set_storage(WIFI_STORAGE_RAM); assert(ret == ESP_OK); diff --git a/fw/esp32/main/app_main.c b/fw/esp32/main/app_main.c index 4babcdc..8374c45 100644 --- a/fw/esp32/main/app_main.c +++ b/fw/esp32/main/app_main.c @@ -1,9 +1,11 @@ #include <freertos/FreeRTOS.h> #include <freertos/task.h> -#include <tcpip_adapter.h> #include <driver/gpio.h> -#include <esp_sleep.h> +#include <esp_event.h> +#include <esp_netif.h> +#include <esp_err.h> +#include <esp_log.h> #include "i2c.h" #include "cell.h" @@ -17,7 +19,13 @@ // Main application void app_main() { - tcpip_adapter_init(); + esp_err_t ret; + + ret = esp_netif_init(); + assert(ret == ESP_OK); + + ret = esp_event_loop_create_default(); + assert(ret == ESP_OK); eos_net_init(); |