Skip to content

Commit bdef111

Browse files
Address @devyte's style and code issues
1 parent 0478d47 commit bdef111

File tree

2 files changed

+56
-63
lines changed

2 files changed

+56
-63
lines changed

cores/esp8266/core_esp8266_i2s.c

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ typedef struct i2s_state {
6565
uint32_t * curr_slc_buf; // Current buffer for writing
6666
uint32_t curr_slc_buf_pos; // Position in the current buffer
6767
void (*callback) (void);
68-
// Callback function should be defined as 'void ICACHE_FLASH_ATTR function_name()',
68+
// Callback function should be defined as 'void ICACHE_RAM_ATTR function_name()',
6969
// and be placed in IRAM for faster execution. Avoid long computational tasks in this
7070
// function, use it to set flags and process later.
7171
} i2s_state_t;
@@ -74,65 +74,65 @@ typedef struct i2s_state {
7474
static i2s_state_t *rx = NULL;
7575
static i2s_state_t *tx = NULL;
7676

77-
volatile int rx_irqs = 0;
78-
volatile int tx_irqs = 0;
77+
volatile uint32_t rx_irqs = 0;
78+
volatile uint32_t tx_irqs = 0;
7979

8080
// Some constants that aren't defined in i2s_regs.h
81+
#define I2SO_DATA 3
82+
#define I2SO_BCK 15
83+
#define I2SO_WS 2
8184
#define I2SI_DATA 12
8285
#define I2SI_BCK 13
8386
#define I2SI_WS 14
8487

8588

86-
static bool ICACHE_FLASH_ATTR _i2s_is_full(const i2s_state_t *ch) {
89+
static bool _i2s_is_full(const i2s_state_t *ch) {
8790
if (!ch) {
8891
return false;
89-
} else {
90-
return (ch->curr_slc_buf_pos==SLC_BUF_LEN || ch->curr_slc_buf==NULL) && (ch->slc_queue_len == 0);
9192
}
93+
return (ch->curr_slc_buf_pos==SLC_BUF_LEN || ch->curr_slc_buf==NULL) && (ch->slc_queue_len == 0);
9294
}
9395

94-
bool ICACHE_FLASH_ATTR i2s_is_full() {
96+
bool i2s_is_full() {
9597
return _i2s_is_full( tx );
9698
}
9799

98-
bool ICACHE_FLASH_ATTR i2s_rx_is_full() {
100+
bool i2s_rx_is_full() {
99101
return _i2s_is_full( rx );
100102
}
101103

102-
static bool ICACHE_FLASH_ATTR _i2s_is_empty(const i2s_state_t *ch) {
104+
static bool _i2s_is_empty(const i2s_state_t *ch) {
103105
if (!ch) {
104106
return false;
105-
} else {
106-
return (ch->slc_queue_len >= SLC_BUF_CNT-1);
107107
}
108+
return (ch->slc_queue_len >= SLC_BUF_CNT-1);
108109
}
109110

110-
bool ICACHE_FLASH_ATTR i2s_is_empty() {
111+
bool i2s_is_empty() {
111112
return _i2s_is_empty( tx );
112113
}
113114

114-
bool ICACHE_FLASH_ATTR i2s_rx_is_empty() {
115+
bool i2s_rx_is_empty() {
115116
return _i2s_is_empty( rx );
116117
}
117118

118-
static int16_t ICACHE_FLASH_ATTR _i2s_available(const i2s_state_t *ch) {
119+
static int16_t _i2s_available(const i2s_state_t *ch) {
119120
if (!ch) {
120121
return 0;
121-
} else {
122-
return (SLC_BUF_CNT - ch->slc_queue_len) * SLC_BUF_LEN;
123122
}
123+
return (SLC_BUF_CNT - ch->slc_queue_len) * SLC_BUF_LEN;
124124
}
125125

126-
int16_t ICACHE_FLASH_ATTR i2s_available(){
126+
int16_t i2s_available(){
127127
return _i2s_available( tx );
128128
}
129129

130-
int16_t ICACHE_FLASH_ATTR i2s_rx_available(){
130+
int16_t i2s_rx_available(){
131131
return _i2s_available( rx );
132132
}
133133

134134
// Pop the top off of the queue and return it
135-
uint32_t * ICACHE_FLASH_ATTR i2s_slc_queue_next_item(i2s_state_t *ch) {
135+
static uint32_t * ICACHE_RAM_ATTR i2s_slc_queue_next_item(i2s_state_t *ch) {
136136
uint8_t i;
137137
uint32_t *item = ch->slc_queue[0];
138138
ch->slc_queue_len--;
@@ -143,7 +143,7 @@ uint32_t * ICACHE_FLASH_ATTR i2s_slc_queue_next_item(i2s_state_t *ch) {
143143
}
144144

145145
// Append an item to the end of the queue from receive
146-
void ICACHE_FLASH_ATTR i2s_slc_queue_append_item(i2s_state_t *ch, uint32_t *item) {
146+
static void ICACHE_RAM_ATTR i2s_slc_queue_append_item(i2s_state_t *ch, uint32_t *item) {
147147
// Shift everything up, except for the one corresponding to this item
148148
for (int i=0, dest=0; i < ch->slc_queue_len; i++) {
149149
if (ch->slc_queue[i] != item) {
@@ -160,7 +160,7 @@ void ICACHE_FLASH_ATTR i2s_slc_queue_append_item(i2s_state_t *ch, uint32_t *item
160160
// This routine is called as soon as the DMA routine has something to tell us. All we
161161
// handle here is the RX_EOF_INT status, which indicate the DMA has sent a buffer whose
162162
// descriptor has the 'EOF' field set to 1.
163-
void ICACHE_FLASH_ATTR i2s_slc_isr(void) {
163+
void ICACHE_RAM_ATTR i2s_slc_isr(void) {
164164
uint32_t slc_intr_status = SLCIS;
165165
SLCIC = 0xFFFFFFFF;
166166
if (slc_intr_status & SLCIRXEOF) {
@@ -172,7 +172,9 @@ void ICACHE_FLASH_ATTR i2s_slc_isr(void) {
172172
i2s_slc_queue_next_item(tx); //free space for finished_item
173173
}
174174
tx->slc_queue[tx->slc_queue_len++] = finished_item->buf_ptr;
175-
if (tx->callback) tx->callback();
175+
if (tx->callback) {
176+
tx->callback();
177+
}
176178
ETS_SLC_INTR_ENABLE();
177179
}
178180
if (slc_intr_status & SLCITXEOF) {
@@ -182,7 +184,9 @@ void ICACHE_FLASH_ATTR i2s_slc_isr(void) {
182184
finished_item->owner = 1; // Or else RX just stops
183185
finished_item->myid++;
184186
i2s_slc_queue_append_item(rx, finished_item->buf_ptr);
185-
if (rx->callback) rx->callback();
187+
if (rx->callback) {
188+
rx->callback();
189+
}
186190
ETS_SLC_INTR_ENABLE();
187191
}
188192
}
@@ -195,13 +199,11 @@ void i2s_rx_set_callback(void (*callback) (void)){
195199
rx->callback = callback;
196200
}
197201

198-
static void ICACHE_FLASH_ATTR _alloc_channel(i2s_state_t *ch) {
199-
int x, y;
200-
202+
static void _alloc_channel(i2s_state_t *ch) {
201203
ch->slc_queue_len = 0;
202-
for (x=0; x<SLC_BUF_CNT; x++) {
204+
for (int x=0; x<SLC_BUF_CNT; x++) {
203205
ch->slc_buf_pntr[x] = malloc(SLC_BUF_LEN*4);
204-
for (y=0; y<SLC_BUF_LEN; y++) ch->slc_buf_pntr[x][y] = 0;
206+
memset(ch->slc_buf_pntr[x], 0, SLC_BUF_LEN * sizeof(ch->slc_buf_pntr[x][0]));
205207

206208
ch->slc_items[x].unused = 0;
207209
ch->slc_items[x].owner = 1;
@@ -225,7 +227,7 @@ rx->slc_items[i].buf_ptr, rx->slc_items[i].next_link_ptr);
225227
#endif
226228

227229

228-
static void ICACHE_FLASH_ATTR i2s_slc_begin() {
230+
static void i2s_slc_begin() {
229231
if (tx) {
230232
_alloc_channel(tx);
231233
}
@@ -242,7 +244,7 @@ static void ICACHE_FLASH_ATTR i2s_slc_begin() {
242244
SLCC0 &= ~(SLCMM << SLCM); // Clear DMA MODE
243245
SLCC0 |= (1 << SLCM); // Set DMA MODE to 1
244246
SLCRXDC |= SLCBINR | SLCBTNR; // Enable INFOR_NO_REPLACE and TOKEN_NO_REPLACE
245-
SLCRXDC &= ~(/*SLCBRXFE |*/ SLCBRXEM | SLCBRXFM); //disable RX_FILL, RX_EOF_MODE and RX_FILL_MODE
247+
SLCRXDC &= ~(/*SLCBRXFE |*/ SLCBRXEM | SLCBRXFM); // Disable RX_FILL, RX_EOF_MODE and RX_FILL_MODE
246248

247249
//Feed DMA the 1st buffer desc addr
248250
//To send data to the I2S subsystem, counter-intuitively we use the RXLINK part, not the TXLINK as you might
@@ -256,7 +258,7 @@ static void ICACHE_FLASH_ATTR i2s_slc_begin() {
256258
SLCTXL |= (uint32)&rx->slc_items[0] << SLCTXLA; // Set real RX address
257259
}
258260
if (!tx) {
259-
// SLCRXL |= (uint32)&rx->slc_items[1] << SLCRXLA; // Set fake (ununsed) TX descriptor address
261+
SLCRXL |= (uint32)&rx->slc_items[1] << SLCRXLA; // Set fake (ununsed) TX descriptor address
260262
} else {
261263
SLCRXL |= (uint32)&tx->slc_items[0] << SLCRXLA; // Set real TX address
262264
}
@@ -273,7 +275,7 @@ static void ICACHE_FLASH_ATTR i2s_slc_begin() {
273275
}
274276
}
275277

276-
static void ICACHE_FLASH_ATTR i2s_slc_end(){
278+
static void i2s_slc_end(){
277279
ETS_SLC_INTR_DISABLE();
278280
SLCIC = 0xFFFFFFFF;
279281
SLCIE = 0;
@@ -296,7 +298,7 @@ static void ICACHE_FLASH_ATTR i2s_slc_end(){
296298
//at least the current sample rate. You can also call it quicker: it will suspend the calling
297299
//thread if the buffer is full and resume when there's room again.
298300

299-
static bool ICACHE_FLASH_ATTR _i2s_write_sample(uint32_t sample, bool nb) {
301+
static bool _i2s_write_sample(uint32_t sample, bool nb) {
300302
if (tx->curr_slc_buf_pos==SLC_BUF_LEN || tx->curr_slc_buf==NULL) {
301303
if (tx->slc_queue_len == 0) {
302304
if (nb) return false;
@@ -326,14 +328,14 @@ bool ICACHE_FLASH_ATTR i2s_write_sample_nb(uint32_t sample) {
326328
return _i2s_write_sample(sample, true);
327329
}
328330

329-
bool ICACHE_FLASH_ATTR i2s_write_lr(int16_t left, int16_t right){
331+
bool i2s_write_lr(int16_t left, int16_t right){
330332
int sample = right & 0xFFFF;
331333
sample = sample << 16;
332334
sample |= left & 0xFFFF;
333335
return i2s_write_sample(sample);
334336
}
335337

336-
bool ICACHE_FLASH_ATTR i2s_read_sample(uint32_t *left, uint32_t *right, bool blocking) {
338+
bool i2s_read_sample(uint32_t *left, uint32_t *right, bool blocking) {
337339
if (rx->curr_slc_buf_pos==SLC_BUF_LEN || rx->curr_slc_buf==NULL) {
338340
if (rx->slc_queue_len == 0) {
339341
if (!blocking) return false;
@@ -352,22 +354,15 @@ bool ICACHE_FLASH_ATTR i2s_read_sample(uint32_t *left, uint32_t *right, bool blo
352354
rx->curr_slc_buf_pos=0;
353355
}
354356

355-
// *left = rx->slc_items[0].buf_ptr[rx->curr_slc_buf_pos++];
356-
// *right = rx->slc_items[0].buf_ptr[rx->curr_slc_buf_pos++];
357357
*left = rx->curr_slc_buf[rx->curr_slc_buf_pos++];
358358
*right = rx->curr_slc_buf[rx->curr_slc_buf_pos++];
359359

360360
return true;
361361
}
362362

363-
// END DMA
364-
// =========
365-
// START I2S
366-
367-
368363
static uint32_t _i2s_sample_rate;
369364

370-
void ICACHE_FLASH_ATTR i2s_set_rate(uint32_t rate){ //Rate in HZ
365+
void i2s_set_rate(uint32_t rate){ //Rate in HZ
371366
if(rate == _i2s_sample_rate) return;
372367
_i2s_sample_rate = rate;
373368

@@ -392,19 +387,19 @@ void ICACHE_FLASH_ATTR i2s_set_rate(uint32_t rate){ //Rate in HZ
392387
I2SC |= I2SRF | I2SMR | I2SRMS | ((sbd_div_best) << I2SBD) | ((scd_div_best) << I2SCD);
393388
}
394389

395-
void ICACHE_FLASH_ATTR i2s_set_dividers(uint8_t div1, uint8_t div2){
390+
void i2s_set_dividers(uint8_t div1, uint8_t div2){
396391
div1 &= I2SBDM;
397392
div2 &= I2SCDM;
398393

399394
I2SC &= ~(I2STSM | I2SRSM | (I2SBMM << I2SBM) | (I2SBDM << I2SBD) | (I2SCDM << I2SCD));
400395
I2SC |= I2SRF | I2SMR | I2SRSM | I2SRMS | (div1 << I2SBD) | (div2 << I2SCD);
401396
}
402397

403-
float ICACHE_FLASH_ATTR i2s_get_real_rate(){
398+
float i2s_get_real_rate(){
404399
return (float)I2SBASEFREQ/32/((I2SC>>I2SBD) & I2SBDM)/((I2SC >> I2SCD) & I2SCDM);
405400
}
406401

407-
void ICACHE_FLASH_ATTR i2s_rxtx_begin(bool enableRx, bool enableTx) {
402+
void i2s_rxtx_begin(bool enableRx, bool enableTx) {
408403
if (tx || rx) {
409404
i2s_end(); // Stop and free any ongoing stuff
410405
}
@@ -414,9 +409,9 @@ void ICACHE_FLASH_ATTR i2s_rxtx_begin(bool enableRx, bool enableTx) {
414409
if (!tx) {
415410
return; // OOM Error!
416411
}
417-
pinMode(2, FUNCTION_1); // I2SO_WS (LRCK)
418-
pinMode(3, FUNCTION_1); // I2SO_DATA (SDIN)
419-
pinMode(15, FUNCTION_1); // I2SO_BCK (SCLK)
412+
pinMode(I2SO_WS, FUNCTION_1);
413+
pinMode(I2SO_DATA, FUNCTION_1);
414+
pinMode(I2SO_BCK, FUNCTION_1);
420415
}
421416
if (enableRx) {
422417
rx = (i2s_state_t*)calloc(1, sizeof(*rx));
@@ -460,24 +455,24 @@ void ICACHE_FLASH_ATTR i2s_rxtx_begin(bool enableRx, bool enableTx) {
460455
I2SC |= (rx?I2SRXS:0) | (tx?I2STXS:0); // Start transmission/reception
461456
}
462457

463-
void ICACHE_FLASH_ATTR i2s_begin() {
458+
void i2s_begin() {
464459
i2s_rxtx_begin(false, true);
465460
}
466461

467-
void ICACHE_FLASH_ATTR i2s_end() {
462+
void i2s_end() {
468463
I2SC &= ~I2STXS;
469464

470-
//Reset I2S
465+
// Reset I2S
471466
I2SC &= ~(I2SRST);
472467
I2SC |= I2SRST;
473468
I2SC &= ~(I2SRST);
474469

475470
i2s_slc_end();
476471

477472
if (tx) {
478-
pinMode(2, INPUT);
479-
pinMode(3, INPUT);
480-
pinMode(15, INPUT);
473+
pinMode(I2SO_DATA, INPUT);
474+
pinMode(I2SO_BCK, INPUT);
475+
pinMode(I2SO_WS, INPUT);
481476
free(tx);
482477
tx = NULL;
483478
}

libraries/esp8266/examples/I2SInput/I2SInput.ino

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/*
2-
I2S stereo microphone (input) example
2+
I2S stereo microphone (input) example
33
Run using the Arduion Serial Plotter to see waveform.
44
Released to the Public Domain by Earle F. Philhower, III
55
*/
66

77
#include <ESP8266WiFi.h>
88
#include <i2s.h>
99

10-
void dump()
11-
{
10+
void dump() {
1211
Serial.printf("I2SC: %08x\n", I2SC);
1312
Serial.printf("I2SFC: %08x\n", I2SFC);
1413
Serial.printf("I2SCC: %08x\n", I2SCC);
@@ -29,12 +28,12 @@ void setup() {
2928
dump();
3029

3130
delay(1000);
32-
31+
3332
while (1) {
3433
uint32_t l, r;
3534
i2s_read_sample(&l, &r, true);
36-
int16_t lh = l>>16;
37-
int16_t rh = r>>16;
35+
int16_t lh = l >> 16;
36+
int16_t rh = r >> 16;
3837
char withScale[256];
3938
sprintf(withScale, "%d %d", lh, rh);
4039
Serial.println(withScale);
@@ -43,6 +42,5 @@ void setup() {
4342
}
4443

4544
void loop() {
45+
/* Nothing here */
4646
}
47-
48-

0 commit comments

Comments
 (0)