Skip to content

Commit 3fb2a2c

Browse files
author
IcingTomato
committed
Add: Add Speaker demo
1 parent 3d1c095 commit 3fb2a2c

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

examples/Basics/Speaker/Speaker.ino

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#include <Arduino.h>
2+
#include <Wire.h>
3+
#include <driver/i2c.h>
4+
#include <driver/i2s.h>
5+
#include "driver/gpio.h"
6+
#include "esp_system.h"
7+
#include "esp_log.h"
8+
#include <math.h>
9+
#include <stdio.h>
10+
11+
#define LITTLETOBIG(x) ((x<<8)|(x>>8))
12+
13+
#define SAMPLE_RATE (36000)
14+
#define I2S_NUM (I2S_NUM_0)
15+
#define WAVE_FREQ_HZ (100)
16+
// #define PI (3.14159265)
17+
#define I2S_BCK_IO (GPIO_NUM_34)
18+
#define I2S_WS_IO (GPIO_NUM_33)
19+
#define I2S_DO_IO (GPIO_NUM_13)
20+
#define I2S_DI_IO (GPIO_NUM_14)
21+
22+
#define SAMPLE_PER_CYCLE (SAMPLE_RATE/WAVE_FREQ_HZ)
23+
24+
extern "C" void aw88298_init(void);
25+
extern "C" int aw88298_i2c_write_16(uint8_t slv_addr, uint8_t reg, uint16_t data);
26+
extern "C" void setup_triangle_sine_waves(int bits);
27+
void speaker_test(void);
28+
29+
void setup(void)
30+
{
31+
Serial.begin(115200);
32+
Wire.begin(12, 11, 100000L);
33+
aw88298_init();
34+
35+
speaker_test();
36+
}
37+
38+
void loop(void)
39+
{
40+
41+
}
42+
43+
extern "C" int aw88298_i2c_write_16(uint8_t slv_addr, uint8_t reg, uint16_t data)
44+
{
45+
esp_err_t ret = ESP_FAIL;
46+
uint16_t data_htons = LITTLETOBIG(data);
47+
uint8_t *data_u8 = (uint8_t *)&data_htons;
48+
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
49+
i2c_master_start(cmd);
50+
i2c_master_write_byte(cmd, ( slv_addr << 1 ) | I2C_MASTER_WRITE, 0x01);
51+
i2c_master_write_byte(cmd, reg, 0x01);
52+
i2c_master_write_byte(cmd, data_u8[0], 0x01);
53+
i2c_master_write_byte(cmd, data_u8[1], 0x01);
54+
i2c_master_stop(cmd);
55+
ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, pdMS_TO_TICKS(1000));
56+
i2c_cmd_link_delete(cmd);
57+
if(ret != ESP_OK) {
58+
ESP_LOGI(TAG, "W [%04x]=%04x fail\n", reg, data);
59+
}
60+
return ret == ESP_OK ? 0 : -1;
61+
}
62+
63+
extern "C" void aw88298_init(void)
64+
{
65+
aw88298_i2c_write_16(0x36, 0x61, 0x0673);
66+
aw88298_i2c_write_16(0x36, 0x04, 0x4040);
67+
aw88298_i2c_write_16(0x36, 0x05, 0x0008);
68+
aw88298_i2c_write_16(0x36, 0x06, 0x14C8);
69+
aw88298_i2c_write_16(0x36, 0x0C, 0x0064);
70+
}
71+
72+
extern "C" void setup_triangle_sine_waves(int bits)
73+
{
74+
int *samples_data = (int*) malloc(((bits+8)/16)*SAMPLE_PER_CYCLE*4);
75+
unsigned int i, sample_val;
76+
double sin_float, triangle_float, triangle_step = (double) pow(2, bits) / SAMPLE_PER_CYCLE;
77+
size_t i2s_bytes_write = 0;
78+
79+
printf("\r\nTest bits=%d free mem=%d, written data=%d\n", bits, esp_get_free_heap_size(), ((bits+8)/16)*SAMPLE_PER_CYCLE*4);
80+
81+
triangle_float = -(pow(2, bits)/2 - 1);
82+
83+
for(i = 0; i < SAMPLE_PER_CYCLE; i++) {
84+
sin_float = sin(i * 2 * PI / SAMPLE_PER_CYCLE);
85+
if(sin_float >= 0)
86+
triangle_float += triangle_step;
87+
else
88+
triangle_float -= triangle_step;
89+
90+
sin_float *= (pow(2, bits)/2 - 1);
91+
92+
if (bits == 16) {
93+
sample_val = 0;
94+
sample_val += (short)triangle_float;
95+
sample_val = sample_val << 16;
96+
sample_val += (short) sin_float;
97+
samples_data[i] = sample_val;
98+
} else if (bits == 24) { //1-bytes unused
99+
samples_data[i*2] = ((int) triangle_float) << 8;
100+
samples_data[i*2 + 1] = ((int) sin_float) << 8;
101+
} else {
102+
samples_data[i*2] = ((int) triangle_float);
103+
samples_data[i*2 + 1] = ((int) sin_float);
104+
}
105+
106+
}
107+
ESP_LOGI(TAG, "set clock");
108+
i2s_set_clk(I2S_NUM, SAMPLE_RATE, bits, I2S_CHANNEL_MONO);
109+
//Using push
110+
// for(i = 0; i < SAMPLE_PER_CYCLE; i++) {
111+
// if (bits == 16)
112+
// i2s_push_sample(0, &samples_data[i], 100);
113+
// else
114+
// i2s_push_sample(0, &samples_data[i*2], 100);
115+
// }
116+
// or write
117+
ESP_LOGI(TAG, "write data");
118+
i2s_write(I2S_NUM, samples_data, ((bits+8)/16)*SAMPLE_PER_CYCLE*4, &i2s_bytes_write, 100);
119+
120+
free(samples_data);
121+
}
122+
123+
void speaker_test(void)
124+
{
125+
i2s_config_t i2s_config = {
126+
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
127+
.sample_rate = SAMPLE_RATE,
128+
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
129+
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
130+
.communication_format = I2S_COMM_FORMAT_STAND_MSB,
131+
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
132+
.dma_buf_count = 6,
133+
.dma_buf_len = 60,
134+
.use_apll = false //Interrupt level 1
135+
};
136+
137+
i2s_pin_config_t pin_config = {
138+
.mck_io_num = I2S_PIN_NO_CHANGE,
139+
.bck_io_num = I2S_BCK_IO,
140+
.ws_io_num = I2S_WS_IO,
141+
.data_out_num = I2S_DO_IO,
142+
.data_in_num = I2S_DI_IO //Not used
143+
};
144+
145+
i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
146+
i2s_set_pin(I2S_NUM, &pin_config);
147+
148+
int test_bits = 16;
149+
while (1) {
150+
setup_triangle_sine_waves(test_bits);
151+
vTaskDelay(5000/portTICK_RATE_MS);
152+
test_bits += 8;
153+
if(test_bits > 32)
154+
test_bits = 16;
155+
156+
}
157+
}

0 commit comments

Comments
 (0)