Input data from Line-in is output to Line-out.

Dependencies:   R_BSP TLV320_RBSP mbed-rtos mbed

Input data from Line-in is output to Line-out.

Please refer to following link about Audio/Camera Shield.
https://developer.mbed.org/teams/Renesas/wiki/Audio_Camera-shield

main.cpp

Committer:
Osamu Nakamura
Date:
2017-03-14
Revision:
3:d3782a74823f
Parent:
2:ab3033c3bb17

File content as of revision 3:d3782a74823f:

#include "mbed.h"
#include "TLV320_RBSP.h"

#define WRITE_BUFF_NUM         (16)
#define READ_BUFF_SIZE         (4096)
#define READ_BUFF_NUM          (16)
#define MAIL_QUEUE_SIZE        (WRITE_BUFF_NUM + READ_BUFF_NUM + 1)
#define INFO_TYPE_WRITE_END    (0)
#define INFO_TYPE_READ_END     (1)

TLV320_RBSP audio(P10_13, I2C_SDA, I2C_SCL, P4_4, P4_5, P4_7, P4_6, 0x80, WRITE_BUFF_NUM, READ_BUFF_NUM); // I2S Codec

typedef struct {
    uint32_t info_type;
    void *   p_data;
    int32_t  result;
} mail_t;
Mail<mail_t, MAIL_QUEUE_SIZE> mail_box;

#if defined(__ICCARM__)
#pragma data_alignment=4
static uint8_t audio_read_buff[READ_BUFF_NUM][READ_BUFF_SIZE]@ ".mirrorram";  //4 bytes aligned! No cache memory
#else
static uint8_t audio_read_buff[READ_BUFF_NUM][READ_BUFF_SIZE] __attribute((section("NC_BSS"),aligned(4)));  //4 bytes aligned! No cache memory
#endif

static void callback_audio_tans_end(void * p_data, int32_t result, void * p_app_data) {
    mail_t *mail = mail_box.alloc();

    if (result < 0) {
        printf("error %d\n", result);
    }
    if (mail == NULL) {
        printf("error mail alloc\n");
    } else {
        mail->info_type = (uint32_t)p_app_data;
        mail->p_data    = p_data;
        mail->result    = result;
        mail_box.put(mail);
    }
}

void audio_read_task(void const*) {
    uint32_t cnt;
    rbsp_data_conf_t audio_write_data = {&callback_audio_tans_end, (void *)INFO_TYPE_WRITE_END};
    rbsp_data_conf_t audio_read_data  = {&callback_audio_tans_end, (void *)INFO_TYPE_READ_END};

    // Read buffer setting
    for (cnt = 0; cnt < READ_BUFF_NUM; cnt++) {
        if (audio.read(audio_read_buff[cnt], READ_BUFF_SIZE, &audio_read_data) < 0) {
            printf("read error\n");
        }
    }

    while (1) {
        osEvent evt = mail_box.get();
        if (evt.status == osEventMail) {
            mail_t *mail = (mail_t *)evt.value.p;

            if ((mail->info_type == INFO_TYPE_READ_END) && (mail->result > 0)) {
                audio.write(mail->p_data, mail->result, &audio_write_data);
            } else {
                audio.read(mail->p_data, READ_BUFF_SIZE, &audio_read_data);     // Resetting read buffer
            }
            mail_box.free(mail);
        }
    }
}

int main() {
#if(0)
    // Microphone
    audio.mic(true);   // Input select for ADC is microphone.
    audio.power(0x00); // mic on
#else
    // Line in
    audio.power(0x02); // mic off
#endif
    audio.inputVolume(0.7, 0.7);

    Thread audioReadTask(audio_read_task, NULL, osPriorityNormal, 1024 * 8);

    while(1) {
        Thread::wait(500);
    }
}