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

Dependencies:   R_BSP TLV320_RBSP mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TLV320_RBSP.h"
00003 
00004 #define WRITE_BUFF_NUM         (16)
00005 #define READ_BUFF_SIZE         (4096)
00006 #define READ_BUFF_NUM          (16)
00007 #define MAIL_QUEUE_SIZE        (WRITE_BUFF_NUM + READ_BUFF_NUM + 1)
00008 #define INFO_TYPE_WRITE_END    (0)
00009 #define INFO_TYPE_READ_END     (1)
00010 
00011 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
00012 
00013 typedef struct {
00014     uint32_t info_type;
00015     void *   p_data;
00016     int32_t  result;
00017 } mail_t;
00018 Mail<mail_t, MAIL_QUEUE_SIZE> mail_box;
00019 
00020 #if defined(__ICCARM__)
00021 #pragma data_alignment=4
00022 static uint8_t audio_read_buff[READ_BUFF_NUM][READ_BUFF_SIZE]@ ".mirrorram";  //4 bytes aligned! No cache memory
00023 #else
00024 static uint8_t audio_read_buff[READ_BUFF_NUM][READ_BUFF_SIZE] __attribute((section("NC_BSS"),aligned(4)));  //4 bytes aligned! No cache memory
00025 #endif
00026 
00027 static void callback_audio_tans_end(void * p_data, int32_t result, void * p_app_data) {
00028     mail_t *mail = mail_box.alloc();
00029 
00030     if (result < 0) {
00031         printf("error %d\n", result);
00032     }
00033     if (mail == NULL) {
00034         printf("error mail alloc\n");
00035     } else {
00036         mail->info_type = (uint32_t)p_app_data;
00037         mail->p_data    = p_data;
00038         mail->result    = result;
00039         mail_box.put(mail);
00040     }
00041 }
00042 
00043 void audio_read_task(void const*) {
00044     uint32_t cnt;
00045     rbsp_data_conf_t audio_write_data = {&callback_audio_tans_end, (void *)INFO_TYPE_WRITE_END};
00046     rbsp_data_conf_t audio_read_data  = {&callback_audio_tans_end, (void *)INFO_TYPE_READ_END};
00047 
00048     // Read buffer setting
00049     for (cnt = 0; cnt < READ_BUFF_NUM; cnt++) {
00050         if (audio.read(audio_read_buff[cnt], READ_BUFF_SIZE, &audio_read_data) < 0) {
00051             printf("read error\n");
00052         }
00053     }
00054 
00055     while (1) {
00056         osEvent evt = mail_box.get();
00057         if (evt.status == osEventMail) {
00058             mail_t *mail = (mail_t *)evt.value.p;
00059 
00060             if ((mail->info_type == INFO_TYPE_READ_END) && (mail->result > 0)) {
00061                 audio.write(mail->p_data, mail->result, &audio_write_data);
00062             } else {
00063                 audio.read(mail->p_data, READ_BUFF_SIZE, &audio_read_data);     // Resetting read buffer
00064             }
00065             mail_box.free(mail);
00066         }
00067     }
00068 }
00069 
00070 int main() {
00071 #if(0)
00072     // Microphone
00073     audio.mic(true);   // Input select for ADC is microphone.
00074     audio.power(0x00); // mic on
00075 #else
00076     // Line in
00077     audio.power(0x02); // mic off
00078 #endif
00079     audio.inputVolume(0.7, 0.7);
00080 
00081     Thread audioReadTask(audio_read_task, NULL, osPriorityNormal, 1024 * 8);
00082 
00083     while(1) {
00084         Thread::wait(500);
00085     }
00086 }