|
8 | 8 | #include "freertos/FreeRTOS.h" |
9 | 9 | #include "driver/i2s.h" |
10 | 10 |
|
| 11 | +static int i2s0_sample_rate = 44100; |
| 12 | +static int i2s0_bits_per_sample = 16; |
| 13 | + |
11 | 14 | void i2s0_init(void) |
12 | 15 | { |
13 | | - int use_apll = 0; |
14 | 16 | esp_chip_info_t chip_info; |
15 | 17 | esp_chip_info(&chip_info); |
16 | | - // Don't use apll on rev0 chips |
17 | | - if (chip_info.revision != 0) { |
18 | | - use_apll = 1; |
19 | | - } |
20 | 18 | i2s_config_t i2s_config = { |
21 | 19 | .mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX |
22 | | - .sample_rate = 44100, |
23 | | - .bits_per_sample = 16, |
24 | | - .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, // 2-channels |
25 | 20 | .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB, |
| 21 | + .use_apll = chip_info.revision, // Don't use apll on rev0 chips |
| 22 | + .sample_rate = i2s0_sample_rate, |
| 23 | + .bits_per_sample = i2s0_bits_per_sample, |
| 24 | + .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, // 2-channels |
26 | 25 | .dma_buf_count = 8, |
27 | 26 | .dma_buf_len = 64, |
28 | | - .use_apll = use_apll, |
29 | 27 | .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 // Interrupt level 1 |
30 | 28 | }; |
| 29 | + i2s_driver_install(0, &i2s_config, 0, NULL); |
31 | 30 | i2s_pin_config_t pin_config = { |
32 | 31 | .bck_io_num = 22, |
33 | 32 | .ws_io_num = 21, |
34 | 33 | .data_out_num = 19, |
35 | 34 | .data_in_num = -1 // Not used |
36 | 35 | }; |
37 | | - i2s_driver_install(0, &i2s_config, 0, NULL); |
38 | 36 | i2s_set_pin(0, &pin_config); |
39 | 37 | } |
| 38 | + |
| 39 | +void i2s0_set_sample_rate(int rate) |
| 40 | +{ |
| 41 | + if (rate != i2s0_sample_rate) { |
| 42 | + i2s_set_sample_rates(0, rate); |
| 43 | + i2s0_sample_rate = rate; |
| 44 | + } |
| 45 | +} |
0 commit comments