summaryrefslogtreecommitdiffstats
path: root/esp32
diff options
context:
space:
mode:
Diffstat (limited to 'esp32')
-rw-r--r--esp32/CMakeLists.txt6
-rw-r--r--esp32/main/CMakeLists.txt4
-rw-r--r--esp32/main/main.c89
-rw-r--r--esp32/main/wifi.c97
-rw-r--r--esp32/main/wifi.h6
5 files changed, 202 insertions, 0 deletions
diff --git a/esp32/CMakeLists.txt b/esp32/CMakeLists.txt
new file mode 100644
index 0000000..7de91bc
--- /dev/null
+++ b/esp32/CMakeLists.txt
@@ -0,0 +1,6 @@
+# The following lines of boilerplate have to be in your project's
+# CMakeLists in this exact order for cmake to work correctly
+cmake_minimum_required(VERSION 3.16)
+
+include($ENV{IDF_PATH}/tools/cmake/project.cmake)
+project(audtest)
diff --git a/esp32/main/CMakeLists.txt b/esp32/main/CMakeLists.txt
new file mode 100644
index 0000000..5f86806
--- /dev/null
+++ b/esp32/main/CMakeLists.txt
@@ -0,0 +1,4 @@
+set(SRC "main.c" "wifi.c")
+
+idf_component_register(SRCS ${SRC}
+ INCLUDE_DIRS ".")
diff --git a/esp32/main/main.c b/esp32/main/main.c
new file mode 100644
index 0000000..3cce6e5
--- /dev/null
+++ b/esp32/main/main.c
@@ -0,0 +1,89 @@
+#include <freertos/FreeRTOS.h>
+#include <freertos/task.h>
+
+#include <esp_log.h>
+#include <esp_event.h>
+#include <esp_netif.h>
+#include <nvs_flash.h>
+
+#include <driver/gpio.h>
+#include <driver/i2s_std.h>
+
+#include "wifi.h"
+
+#define BUFLEN 1024
+#define SAMP_RATE 8000
+
+#define I2S_WS GPIO_NUM_4
+#define I2S_SD GPIO_NUM_1
+#define I2S_SCK GPIO_NUM_5
+
+static i2s_chan_handle_t chan;
+
+static void i2s_read_task(void *args)
+{
+ size_t n;
+ char *buf = calloc(1, BUFLEN);
+
+ ESP_ERROR_CHECK(i2s_channel_enable(chan));
+
+ for (;;) {
+ if (i2s_channel_read(chan, buf, BUFLEN, &n, 1000) == ESP_OK) {
+ if (n > 0) {
+ // todo: copy to a separate buffer and send over udp
+ // esp_mqtt_client_publish(client, "snd", buf, n, 1, 0);
+ }
+ } else
+ printf("Read Task: i2s read failed\n");
+ }
+
+ free(buf);
+ vTaskDelete(NULL);
+}
+
+static inline void i2s_init(void)
+{
+ i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(
+ I2S_NUM_AUTO,
+ I2S_ROLE_MASTER);
+
+ ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, NULL, &chan));
+
+ i2s_std_config_t std_cfg = {
+ .clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(SAMP_RATE),
+ .slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(
+ I2S_DATA_BIT_WIDTH_24BIT,
+ I2S_SLOT_MODE_MONO),
+ .gpio_cfg = {
+ .ws = I2S_WS,
+ .din = I2S_SD,
+ .bclk = I2S_SCK,
+ .dout = I2S_GPIO_UNUSED,
+ .mclk = I2S_GPIO_UNUSED,
+ .invert_flags = {
+ .ws_inv = false,
+ .bclk_inv = false,
+ .mclk_inv = false
+ }
+ }
+ };
+
+ std_cfg.clk_cfg.mclk_multiple = I2S_MCLK_MULTIPLE_384;
+
+ ESP_ERROR_CHECK(i2s_channel_init_std_mode(chan, &std_cfg));
+}
+
+void app_main(void)
+{
+ ESP_ERROR_CHECK(nvs_flash_init());
+ ESP_ERROR_CHECK(esp_event_loop_create_default());
+
+ wifi_connect();
+
+ i2s_init();
+ xTaskCreate(i2s_read_task, "i2s_read_task", 4096, NULL, 5, NULL);
+
+ for (;;)
+ vTaskDelay(500 / portTICK_PERIOD_MS);
+}
+
diff --git a/esp32/main/wifi.c b/esp32/main/wifi.c
new file mode 100644
index 0000000..9489a72
--- /dev/null
+++ b/esp32/main/wifi.c
@@ -0,0 +1,97 @@
+#include <freertos/FreeRTOS.h>
+#include <freertos/event_groups.h>
+#include <freertos/task.h>
+
+#include <esp_log.h>
+#include <esp_wifi.h>
+
+#include "wifi.h"
+
+#define WIFI_MAX_RETRY 5
+
+#define WIFI_CONNECTED_BIT BIT0
+#define WIFI_ERROR_BIT BIT1
+
+static const char* TAG = "wifi";
+
+static int wifi_retry_num = 0;
+static EventGroupHandle_t wifi_evt_group;
+
+static void wifi_evt_handler(
+ void *arg,
+ esp_event_base_t eb,
+ int32_t id,
+ void *data)
+{
+ if (eb == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) {
+ if (wifi_retry_num < WIFI_MAX_RETRY) {
+ esp_wifi_connect();
+ wifi_retry_num++;
+ ESP_LOGI(TAG, "trying to connect to AP...");
+ } else {
+ ESP_LOGE(TAG,"connection to AP failed");
+ xEventGroupSetBits(wifi_evt_group, WIFI_ERROR_BIT);
+ }
+ } else if (eb == IP_EVENT && id == IP_EVENT_STA_GOT_IP) {
+ ip_event_got_ip_t* evt = (ip_event_got_ip_t*) data;
+ ESP_LOGI(
+ TAG,
+ "connected to AP with ip:" IPSTR,
+ IP2STR(&evt->ip_info.ip));
+
+ wifi_retry_num = 0;
+ xEventGroupSetBits(wifi_evt_group, WIFI_CONNECTED_BIT);
+ }
+}
+
+void wifi_connect(void)
+{
+ ESP_ERROR_CHECK(esp_netif_init());
+
+ wifi_evt_group = xEventGroupCreate();
+ esp_netif_create_default_wifi_sta();
+
+ wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
+ ESP_ERROR_CHECK(esp_wifi_init(&cfg));
+
+ esp_event_handler_instance_t any_id;
+ ESP_ERROR_CHECK(
+ esp_event_handler_instance_register(
+ WIFI_EVENT,
+ ESP_EVENT_ANY_ID,
+ &wifi_evt_handler,
+ NULL,
+ &any_id));
+
+ esp_event_handler_instance_t got_ip;
+ ESP_ERROR_CHECK(
+ esp_event_handler_instance_register(
+ IP_EVENT,
+ IP_EVENT_STA_GOT_IP,
+ &wifi_evt_handler,
+ NULL,
+ &got_ip));
+
+ wifi_config_t wifi_config = {
+ .sta = {
+ .ssid = CONFIG_WIFI_SSID,
+ .password = CONFIG_WIFI_PASS,
+ .threshold.authmode = WIFI_AUTH_WPA2_PSK
+ },
+ };
+
+ ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
+ ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
+
+ ESP_ERROR_CHECK(esp_wifi_start());
+ ESP_ERROR_CHECK(esp_wifi_connect());
+
+ xEventGroupWaitBits(
+ wifi_evt_group,
+ WIFI_CONNECTED_BIT | WIFI_ERROR_BIT,
+ pdFALSE,
+ pdFALSE,
+ portMAX_DELAY);
+
+ ESP_LOGI(TAG, "wifi station initialized");
+}
diff --git a/esp32/main/wifi.h b/esp32/main/wifi.h
new file mode 100644
index 0000000..e526eef
--- /dev/null
+++ b/esp32/main/wifi.h
@@ -0,0 +1,6 @@
+#ifndef WIFI_H
+#define WIFI_H
+
+void wifi_connect(void);
+
+#endif /* WIFI_H */