my_pool_buffers: +----------------------------+ | i2s_sample_buffer[0] |<---------+ | sample_data |------+ | + - - - - - - - - - - - - - -+ | | | i2s_sample_buffer[1] | | | +----| sample_data | | | | +----------------------------+ | | | | buffer0[0] |<-----+ | | | .... | | | | buffer0[511] | | | +----------------------------| | +--->| buffer1[0] | | | .... | | | buffer1[511] | | +----------------------------| | | my_pool: +----------------------------+ | | buf_size: 512 | | | buf_count: 2 | | | buffers: |----------+ +----------------------------+
#####1. Simple code that streams data to output I2S using own task
int total_data_to_write = .... uint8_t *data_ptr = .... struct i2s *spkr; int data_written; spekr = i2s_open("speaker", OS_WAIT_FOREVER, NULL); while (total_data_to_write) { data_written = i2s_write(spkr, data_ptr, total_data_to_write); total_data_to_write -= data_written; data_ptr += total_data_to_write; } i2s_close(spkr);
#####2. Blocking read from microphone example
int total_data_to_read = .... uint8_t *data_ptr = .... struct i2s *mic; int data_read; spekr = i2s_open("mic", OS_WAIT_FOREVER, NULL); while (total_data_to_read) { data_read = i2s_read(mic, data_ptr, total_data_to_read); total_data_to_read -= data_read; data_ptr += data_read; } i2s_close(spkr);
#####3. Reading without dedicated task Example shows how to read I2S data stream without blocking calls.
struct i2s mic; void buffer_ready_event_cb(struct os_event *ev) { struct i2s_sample_buffer *buffer; /* This function is called so there is buffer ready. i2s_buffer_get() could be called * with timeout 0 */ buffer = i2s_buffer_get(mic, OS_WAIT_FOREVER); /* Handle microphone data */ ... /* Microphone samples processed, send buffer back to i2s device */ i2s_buffer_put(mic, buffer); } struct os_event buffer_ready_event = { .ev_cb = buffer_ready_event_cb, }; /* Function called from interrupt telling client that new buffer with samples is available */ static int more_data(struct i2s *i2s, struct i2s_sample_buffer *sample_buffer) { /* Handle incoming data in main task queue */ os_eventq_put(os_eventq_dflt_get(), &buffer_ready_event); return 0; } static void state_changed(struct i2s *i2s, enum i2s_state state) {} struct i2s_client client = { .sample_buffer_ready_cb = more_data, .state_changed_cb = state_changed, }; void start_microphone(void) { mic = i2s_open("mic", OS_WAIT_FOREVER, client); /* I2S input device is not started when device is opened */ i2s_start(mic); }