Skip to content

Commit f9110f5

Browse files
Crypterearlephilhower
authored andcommitted
Add I2S callback on DMA read (#4205)
Add optional callback after every DMA buffer interrupt for lower delay, better timing control of I2S output.
1 parent d112dfa commit f9110f5

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

cores/esp8266/core_esp8266_i2s.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ static uint32_t *i2s_slc_buf_pntr[SLC_BUF_CNT]; //Pointer to the I2S DMA buffer
5656
static struct slc_queue_item i2s_slc_items[SLC_BUF_CNT]; //I2S DMA buffer descriptors
5757
static uint32_t *i2s_curr_slc_buf=NULL;//current buffer for writing
5858
static int i2s_curr_slc_buf_pos=0; //position in the current buffer
59+
static void (*i2s_callback) (void)=0; //Callback function should be defined as 'void ICACHE_FLASH_ATTR function_name()', placing the function in IRAM for faster execution. Avoid long computational tasks in this function, use it to set flags and process later.
5960

6061
bool ICACHE_FLASH_ATTR i2s_is_full(){
6162
return (i2s_curr_slc_buf_pos==SLC_BUF_LEN || i2s_curr_slc_buf==NULL) && (i2s_slc_queue_len == 0);
@@ -92,10 +93,15 @@ void ICACHE_FLASH_ATTR i2s_slc_isr(void) {
9293
i2s_slc_queue_next_item(); //free space for finished_item
9394
}
9495
i2s_slc_queue[i2s_slc_queue_len++] = finished_item->buf_ptr;
96+
if (i2s_callback) i2s_callback();
9597
ETS_SLC_INTR_ENABLE();
9698
}
9799
}
98100

101+
void i2s_set_callback(void (*callback) (void)){
102+
i2s_callback = callback;
103+
}
104+
99105
void ICACHE_FLASH_ATTR i2s_slc_begin(){
100106
i2s_slc_queue_len = 0;
101107
int x, y;
@@ -248,7 +254,6 @@ float ICACHE_FLASH_ATTR i2s_get_real_rate(){
248254
return (float)I2SBASEFREQ/32/((I2SC>>I2SBD) & I2SBDM)/((I2SC >> I2SCD) & I2SCDM);
249255
}
250256

251-
252257
void ICACHE_FLASH_ATTR i2s_begin(){
253258
_i2s_sample_rate = 0;
254259
i2s_slc_begin();

cores/esp8266/i2s.h

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ bool i2s_write_lr(int16_t left, int16_t right);//combines both channels and call
5151
bool i2s_is_full();//returns true if DMA is full and can not take more bytes (overflow)
5252
bool i2s_is_empty();//returns true if DMA is empty (underflow)
5353
int16_t i2s_available();// returns the number of samples than can be written before blocking
54+
void i2s_set_callback(void (*callback) (void));
5455

5556
#ifdef __cplusplus
5657
}

0 commit comments

Comments
 (0)