ESP_LOGE(I2S_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
return (ret); \
}
+#define I2S_MAX_BUFFER_SIZE (4 * 1024 * 1024) //the maximum RAM can be allocated
#define I2S_BASE_CLK (2*APB_CLK_FREQ)
#define I2S_ENTER_CRITICAL_ISR() portENTER_CRITICAL_ISR(&i2s_spinlock[i2s_num])
#define I2S_EXIT_CRITICAL_ISR() portEXIT_CRITICAL_ISR(&i2s_spinlock[i2s_num])
float avg;
float min_rate, max_rate, min_diff;
if (rate/bits_per_sample/2/8 < APLL_I2S_MIN_RATE) {
- return ESP_FAIL;
+ return ESP_ERR_INVALID_ARG;
}
*sdm0 = 0;
if (clkmdiv > 256) {
ESP_LOGE(I2S_TAG, "clkmdiv is too large\r\n");
- return ESP_FAIL;
+ return ESP_ERR_INVALID_ARG;
}
// wait all on-going writing finish
if (p_i2s_obj[i2s_num]->tx == NULL) {
ESP_LOGE(I2S_TAG, "Failed to create tx dma buffer");
i2s_driver_uninstall(i2s_num);
- return ESP_FAIL;
+ return ESP_ERR_NO_MEM;
}
I2S[i2s_num]->out_link.addr = (uint32_t) p_i2s_obj[i2s_num]->tx->desc[0];
if (p_i2s_obj[i2s_num]->rx == NULL){
ESP_LOGE(I2S_TAG, "Failed to create rx dma buffer");
i2s_driver_uninstall(i2s_num);
- return ESP_FAIL;
+ return ESP_ERR_NO_MEM;
}
I2S[i2s_num]->rx_eof_num = (p_i2s_obj[i2s_num]->dma_buf_len * p_i2s_obj[i2s_num]->channel_num * p_i2s_obj[i2s_num]->bytes_per_sample)/4;
I2S[i2s_num]->in_link.addr = (uint32_t) p_i2s_obj[i2s_num]->rx->desc[0];
esp_err_t i2s_start(i2s_port_t i2s_num)
{
+ I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
//start DMA link
I2S_ENTER_CRITICAL();
i2s_reset_fifo(i2s_num);
esp_err_t i2s_stop(i2s_port_t i2s_num)
{
+ I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
I2S_ENTER_CRITICAL();
esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
}
I2S[i2s_num]->int_clr.val = I2S[i2s_num]->int_st.val; //clear pending interrupt
I2S_EXIT_CRITICAL();
- return 0;
+ return ESP_OK;
}
esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode)
}
}
if (p_i2s_obj[i2s_num]->tx && p_i2s_obj[i2s_num]->tx->buf != NULL && p_i2s_obj[i2s_num]->tx->buf_size != 0) {
+ int bytes_left = (p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos) % 4;
+ if (bytes_left) {
+ int zero_bytes = 0;
+ i2s_write_bytes(i2s_num, (const char *)&zero_bytes, bytes_left, portMAX_DELAY);
+ }
for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
memset(p_i2s_obj[i2s_num]->tx->buf[i], 0, p_i2s_obj[i2s_num]->tx->buf_size);
}
p_i2s_obj[i2s_num] = (i2s_obj_t*) malloc(sizeof(i2s_obj_t));
if (p_i2s_obj[i2s_num] == NULL) {
ESP_LOGE(I2S_TAG, "Malloc I2S driver error");
- return ESP_FAIL;
+ return ESP_ERR_NO_MEM;
}
memset(p_i2s_obj[i2s_num], 0, sizeof(i2s_obj_t));
return i2s_set_clk(i2s_num, i2s_config->sample_rate, i2s_config->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
}
- ESP_LOGE(I2S_TAG, "I2S driver already installed");
- return ESP_ERR_INVALID_STATE;
+ ESP_LOGW(I2S_TAG, "I2S driver already installed");
+ return ESP_OK;
}
esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num)
{
I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
if (p_i2s_obj[i2s_num] == NULL) {
- ESP_LOGI(I2S_TAG, "ALREADY NULL");
+ ESP_LOGI(I2S_TAG, "already uninstalled");
return ESP_OK;
}
i2s_stop(i2s_num);
{
char *data_ptr;
int bytes_can_write, bytes_writen = 0;
- I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
- if (p_i2s_obj[i2s_num]->tx == NULL) {
- return 0;
- }
+ I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
+ I2S_CHECK((size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_FAIL);
+ I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_FAIL);
xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
while (size > 0) {
if (p_i2s_obj[i2s_num]->tx->rw_pos == p_i2s_obj[i2s_num]->tx->buf_size || p_i2s_obj[i2s_num]->tx->curr_ptr == NULL) {
return ESP_OK;
}
+int i2s_write_expand(i2s_port_t i2s_num, const char *src, int size, int src_bits, int aim_bits, TickType_t ticks_to_wait)
+{
+ char *data_ptr;
+ int bytes_can_write, bytes_writen = 0, tail;
+ I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
+ I2S_CHECK((size > 0), "size must greater than zero", ESP_FAIL);
+ I2S_CHECK((aim_bits * size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_FAIL);
+ I2S_CHECK((aim_bits >= src_bits), "aim_bits musn't less than src_bits", ESP_FAIL);
+ I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_FAIL);
+ if (src_bits < I2S_BITS_PER_SAMPLE_8BIT || aim_bits < I2S_BITS_PER_SAMPLE_8BIT) {
+ ESP_LOGE(I2S_TAG,"bits musn't be less than 8, src_bits %d aim_bits %d", src_bits, aim_bits);
+ return ESP_FAIL;
+ }
+ if (src_bits > I2S_BITS_PER_SAMPLE_32BIT || aim_bits > I2S_BITS_PER_SAMPLE_32BIT) {
+ ESP_LOGE(I2S_TAG,"bits musn't be greater than 32, src_bits %d aim_bits %d", src_bits, aim_bits);
+ return ESP_FAIL;
+ }
+ if ((src_bits == I2S_BITS_PER_SAMPLE_16BIT || src_bits == I2S_BITS_PER_SAMPLE_32BIT) && (size % 2 != 0)) {
+ ESP_LOGE(I2S_TAG,"size must be a even number while src_bits is even, src_bits %d size %d", src_bits, size);
+ return ESP_FAIL;
+ }
+ if (src_bits == I2S_BITS_PER_SAMPLE_24BIT && (size % 3 != 0)) {
+ ESP_LOGE(I2S_TAG,"size must be a multiple of 3 while src_bits is 24, size %d", size);
+ return ESP_FAIL;
+ }
+ int src_bytes = src_bits / 8, aim_bytes = aim_bits / 8;
+ int zero_bytes = aim_bytes - src_bytes;
+ xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
+ size = size * aim_bytes / src_bytes;
+ ESP_LOGD(I2S_TAG,"aim_bytes %d src_bytes %d size %d", aim_bytes, src_bytes, size);
+ while (size > 0) {
+ if (p_i2s_obj[i2s_num]->tx->rw_pos == p_i2s_obj[i2s_num]->tx->buf_size || p_i2s_obj[i2s_num]->tx->curr_ptr == NULL) {
+ if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
+ break;
+ }
+ p_i2s_obj[i2s_num]->tx->rw_pos = 0;
+ }
+ data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
+ data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
+ bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
+ if (bytes_can_write > size) {
+ bytes_can_write = size;
+ }
+ tail = bytes_can_write % aim_bytes;
+ bytes_can_write = bytes_can_write - tail;
+
+ memset(data_ptr, 0, bytes_can_write);
+ for (int j = 0; j < bytes_can_write; j += (aim_bytes - zero_bytes)) {
+ j += zero_bytes;
+ memcpy(&data_ptr[j], &src[bytes_writen], aim_bytes - zero_bytes);
+ bytes_writen += (aim_bytes - zero_bytes);
+ }
+ size -= bytes_can_write;
+ p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
+ }
+ xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
+ return bytes_writen;
+}
+
int i2s_read_bytes(i2s_port_t i2s_num, char* dest, size_t size, TickType_t ticks_to_wait)
{
char *data_ptr;
int bytes_can_read, byte_read = 0;
- I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
- if (p_i2s_obj[i2s_num]->rx == NULL) {
- return 0;
- }
+ I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
+ I2S_CHECK((size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_FAIL);
+ I2S_CHECK((p_i2s_obj[i2s_num]->rx), "rx NULL", ESP_FAIL);
xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
while (size > 0) {
if (p_i2s_obj[i2s_num]->rx->rw_pos == p_i2s_obj[i2s_num]->rx->buf_size || p_i2s_obj[i2s_num]->rx->curr_ptr == NULL) {
xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
return byte_read;
}
+
int i2s_push_sample(i2s_port_t i2s_num, const char *sample, TickType_t ticks_to_wait)
{
int i, bytes_to_push = 0;
char *data_ptr;
- I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+ I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
if (p_i2s_obj[i2s_num]->tx->rw_pos == p_i2s_obj[i2s_num]->tx->buf_size || p_i2s_obj[i2s_num]->tx->curr_ptr == NULL) {
if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
- return 0;
+ return bytes_to_push;
}
ESP_LOGD(I2S_TAG, "rw_pos: %d, buf_size: %d, curr_ptr: %d", p_i2s_obj[i2s_num]->tx->rw_pos, p_i2s_obj[i2s_num]->tx->buf_size, (int)p_i2s_obj[i2s_num]->tx->curr_ptr);
p_i2s_obj[i2s_num]->tx->rw_pos = 0;
{
int i, bytes_to_pop = 0;
char *data_ptr;
- I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+ I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_FAIL);
if (p_i2s_obj[i2s_num]->rx->rw_pos == p_i2s_obj[i2s_num]->rx->buf_size || p_i2s_obj[i2s_num]->rx->curr_ptr == NULL) {
if (xQueueReceive(p_i2s_obj[i2s_num]->rx->queue, &p_i2s_obj[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) {
- return 0;
+ return bytes_to_pop;
}
p_i2s_obj[i2s_num]->rx->rw_pos = 0;
}
* if you don't want this to happen and you want to initialize only one of the DAC channels, you can call i2s_set_dac_mode instead.
*
* @return
- * - ESP_OK Success
- * - ESP_FAIL Parameter error
+ * - ESP_OK Success
+ * - ESP_ERR_INVALID_ARG Parameter error
+ * - ESP_FAIL IO error
*/
esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin);
* be used as RTC DAC function at the same time.
*
* @return
- * - ESP_OK Success
+ * - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode);
* This function must be called before any I2S driver read/write operations.
*
* @return
- * - ESP_OK Success
- * - ESP_FAIL Parameter error
+ * - ESP_OK Success
+ * - ESP_ERR_INVALID_ARG Parameter error
+ * - ESP_ERR_NO_MEM Out of memory
*/
esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void* i2s_queue);
* @param i2s_num I2S_NUM_0, I2S_NUM_1
*
* @return
- * - ESP_OK Success
- * - ESP_FAIL Parameter error
+ * - ESP_OK Success
+ * - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num);
* Format of the data in source buffer is determined by the I2S
* configuration (see i2s_config_t).
*
- * @return Number of bytes written, or ESP_FAIL (-1) for parameter error. If a timeout occurred, bytes written will be less than total size.
+ * @return
+ * - Number of bytes written, if timeout occurred, bytes written will be less than the size passed.
+ * - ESP_FAIL Parameter error.
*/
int i2s_write_bytes(i2s_port_t i2s_num, const char *src, size_t size, TickType_t ticks_to_wait);
+/**
+ * @brief Write data to I2S DMA transmit buffer while expanding the number of bits per sample. For example, expanding 16-bit PCM to 32-bit PCM.
+ *
+ * @param i2s_num I2S_NUM_0, I2S_NUM_1
+ *
+ * @param src Source address to write from
+ *
+ * @param size Size of data in bytes
+ *
+ * @param src_bits Source audio bit
+ *
+ * @param aim_bits Bit wanted, no more than 32, and must be greater than src_bits
+ *
+ * @param ticks_to_wait TX buffer wait timeout in RTOS ticks. If this
+ * many ticks pass without space becoming available in the DMA
+ * transmit buffer, then the function will return (note that if the
+ * data is written to the DMA buffer in pieces, the overall operation
+ * may still take longer than this timeout.) Pass portMAX_DELAY for no
+ * timeout.
+ *
+ * Format of the data in source buffer is determined by the I2S
+ * configuration (see i2s_config_t).
+ *
+ * @return
+ * - Number of bytes written, if timeout occurred, bytes written will be less than the size passed.
+ * - ESP_FAIL Parameter error.
+ */
+int i2s_write_expand(i2s_port_t i2s_num, const char *src, int size, int src_bits, int aim_bits, TickType_t ticks_to_wait);
+
/**
* @brief Read data from I2S DMA receive buffer
*
* @note If the built-in ADC mode is enabled, we should call i2s_adc_start and i2s_adc_stop around the whole reading process,
* to prevent the data getting corrupted.
*
- * @return Number of bytes read, or ESP_FAIL (-1) for parameter error. If a timeout occurred, bytes read will be less than total size.
+ * @return
+ * - Number of bytes read, if timeout occurred, bytes written will be less than the size passed.
+ * - ESP_FAIL Parameter error.
*/
int i2s_read_bytes(i2s_port_t i2s_num, char* dest, size_t size, TickType_t ticks_to_wait);
*
* @param ticks_to_wait Push timeout in RTOS ticks. If space is not available in the DMA TX buffer within this period, no data is written and function returns 0.
*
- * @return Number of bytes successfully pushed to DMA buffer, or ESP_FAIL (-1) for parameter error. Will be either zero or the size of configured sample buffer.
+ * @return
+ * - Number of bytes successfully pushed to DMA buffer, will be either zero or the size of configured sample buffer.
+ * - ESP_FAIL Parameter error.
*/
int i2s_push_sample(i2s_port_t i2s_num, const char *sample, TickType_t ticks_to_wait);
*
* @param ticks_to_wait Pop timeout in RTOS ticks. If a sample is not available in the DMA buffer within this period, no data is read and function returns zero.
*
- * @return Number of bytes successfully read from DMA buffer, or ESP_FAIL (-1) for parameter error. Byte count will be either zero or the size of the configured sample buffer.
-
+ * @return
+ * - Number of bytes successfully read from DMA buffer, will be either zero or the size of configured sample buffer.
+ * - ESP_FAIL Parameter error.
*/
int i2s_pop_sample(i2s_port_t i2s_num, char *sample, TickType_t ticks_to_wait);
* @param rate I2S sample rate (ex: 8000, 44100...)
*
* @return
- * - ESP_OK Success
- * - ESP_FAIL Parameter error
+ * - ESP_OK Success
+ * - ESP_ERR_INVALID_ARG Parameter error
+ * - ESP_FAIL I2s is not initialized
+ * - ESP_ERR_NO_MEM Out of memory
*/
esp_err_t i2s_set_sample_rates(i2s_port_t i2s_num, uint32_t rate);
* @param i2s_num I2S_NUM_0, I2S_NUM_1
*
* @return
- * - ESP_OK Success
- * - ESP_FAIL Parameter error
+ * - ESP_OK Success
+ * - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t i2s_stop(i2s_port_t i2s_num);
* @param i2s_num I2S_NUM_0, I2S_NUM_1
*
* @return
- * - ESP_OK Success
- * - ESP_FAIL Parameter error
+ * - ESP_OK Success
+ * - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t i2s_start(i2s_port_t i2s_num);
* @param i2s_num I2S_NUM_0, I2S_NUM_1
*
* @return
- * - ESP_OK Success
- * - ESP_FAIL Parameter error
+ * - ESP_OK Success
+ * - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t i2s_zero_dma_buffer(i2s_port_t i2s_num);
* @param ch I2S channel, (I2S_CHANNEL_MONO, I2S_CHANNEL_STEREO)
*
* @return
- * - ESP_OK Success
- * - ESP_FAIL Parameter error
+ * - ESP_OK Success
+ * - ESP_FAIL Not initialized
+ * - ESP_ERR_INVALID_ARG Parameter error
+ * - ESP_ERR_NO_MEM Out of memory
*/
esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, i2s_bits_per_sample_t bits, i2s_channel_t ch);
* @param adc_unit SAR ADC unit index
* @param adc_channel ADC channel index
* @return
- * - ESP_OK Success
- * - ESP_FAIL Parameter error
+ * - ESP_OK Success
+ * - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t i2s_set_adc_mode(adc_unit_t adc_unit, adc1_channel_t adc_channel);