1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
#include <errno.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_event.h>
#include <esp_log.h>
#include <esp_netif.h>
#include <nvs_flash.h>
#include <driver/gpio.h>
#include <driver/i2s_std.h>
#include <lwip/err.h>
#include <lwip/sockets.h>
#include <lwip/sys.h>
#include <lwip/netdb.h>
#include "wifi.h"
#define BUFLEN 1024
#define SAMPLE_RATE 8000
#define I2S_WS GPIO_NUM_4
#define I2S_SD GPIO_NUM_1
#define I2S_SCK GPIO_NUM_5
const char *TAG = "mimir";
static int sock;
static size_t sock_addr_len;
static struct sockaddr *sock_addr;
static i2s_chan_handle_t chan;
static void i2s_read_task(void *args)
{
int rc;
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) {
rc = sendto(sock, buf, n, 0, sock_addr, sock_addr_len);
if (rc < 0)
ESP_LOGE(TAG, "sendto() failed: %s", strerror(errno));
}
} 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(SAMPLE_RATE),
.slot_cfg = I2S_STD_PHILIPS_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));
}
static void udp_client_init(void)
{
char *ip_addr;
int family, port;
struct timeval timeout;
struct sockaddr_in *dest_addr;
family = AF_INET;
port = CONFIG_UDP_PORT;
ip_addr = CONFIG_UDP_ADDR;
sock = socket(family, SOCK_DGRAM, IPPROTO_IP);
if (sock < 0) {
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
return;
}
timeout.tv_sec = 10;
timeout.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
if (!(dest_addr = malloc(sizeof(dest_addr)))) {
ESP_LOGE(TAG, "malloc() failed for destination address");
return;
}
dest_addr->sin_addr.s_addr = inet_addr(ip_addr);
dest_addr->sin_family = family;
dest_addr->sin_port = htons(port);
sock_addr = (struct sockaddr *) dest_addr;
sock_addr_len = sizeof dest_addr;
ESP_LOGI(TAG, "Socket created, sending to %s:%d", ip_addr, port);
}
void app_main(void)
{
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_connect();
i2s_init();
udp_client_init();
xTaskCreate(i2s_read_task, "i2s_read_task", 4096, NULL, 5, NULL);
for (;;)
vTaskDelay(500 / portTICK_PERIOD_MS);
}
|