GR-MANGO sample using mbed-os library from my repository.
sample_programs/sample_12_usb_func_audio_2.cpp@0:b4c1e32627f2, 2020-10-12 (annotated)
- Committer:
- RyoheiHagimoto
- Date:
- Mon Oct 12 02:25:49 2020 +0000
- Revision:
- 0:b4c1e32627f2
replaced mbed-os library
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| RyoheiHagimoto | 0:b4c1e32627f2 | 1 | /******************************************************************************* |
| RyoheiHagimoto | 0:b4c1e32627f2 | 2 | * DISCLAIMER |
| RyoheiHagimoto | 0:b4c1e32627f2 | 3 | * This software is supplied by Renesas Electronics Corporation and is only |
| RyoheiHagimoto | 0:b4c1e32627f2 | 4 | * intended for use with Renesas products. No other uses are authorized. This |
| RyoheiHagimoto | 0:b4c1e32627f2 | 5 | * software is owned by Renesas Electronics Corporation and is protected under |
| RyoheiHagimoto | 0:b4c1e32627f2 | 6 | * all applicable laws, including copyright laws. |
| RyoheiHagimoto | 0:b4c1e32627f2 | 7 | * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING |
| RyoheiHagimoto | 0:b4c1e32627f2 | 8 | * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT |
| RyoheiHagimoto | 0:b4c1e32627f2 | 9 | * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE |
| RyoheiHagimoto | 0:b4c1e32627f2 | 10 | * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. |
| RyoheiHagimoto | 0:b4c1e32627f2 | 11 | * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS |
| RyoheiHagimoto | 0:b4c1e32627f2 | 12 | * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE |
| RyoheiHagimoto | 0:b4c1e32627f2 | 13 | * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR |
| RyoheiHagimoto | 0:b4c1e32627f2 | 14 | * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE |
| RyoheiHagimoto | 0:b4c1e32627f2 | 15 | * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. |
| RyoheiHagimoto | 0:b4c1e32627f2 | 16 | * Renesas reserves the right, without notice, to make changes to this software |
| RyoheiHagimoto | 0:b4c1e32627f2 | 17 | * and to discontinue the availability of this software. By using this software, |
| RyoheiHagimoto | 0:b4c1e32627f2 | 18 | * you agree to the additional terms and conditions found by accessing the |
| RyoheiHagimoto | 0:b4c1e32627f2 | 19 | * following link: |
| RyoheiHagimoto | 0:b4c1e32627f2 | 20 | * http://www.renesas.com/disclaimer |
| RyoheiHagimoto | 0:b4c1e32627f2 | 21 | * |
| RyoheiHagimoto | 0:b4c1e32627f2 | 22 | * Copyright (C) 2019 Renesas Electronics Corporation. All rights reserved. |
| RyoheiHagimoto | 0:b4c1e32627f2 | 23 | *******************************************************************************/ |
| RyoheiHagimoto | 0:b4c1e32627f2 | 24 | #include "sample_select.h" |
| RyoheiHagimoto | 0:b4c1e32627f2 | 25 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 26 | #if (SAMPLE_PROGRAM_NO == 12) |
| RyoheiHagimoto | 0:b4c1e32627f2 | 27 | // SAMPLE_PROGRAM_NO 12 : USBAudio and SSIF sample |
| RyoheiHagimoto | 0:b4c1e32627f2 | 28 | // Sound from PC will be output to SSIF. (speaker) |
| RyoheiHagimoto | 0:b4c1e32627f2 | 29 | // Sound from SSIF will be input to the PC. (microphone) |
| RyoheiHagimoto | 0:b4c1e32627f2 | 30 | // [Attention!] Delete the "OVERRIDE_CONSOLE_USBSERIAL" macro in "mbed_app.json" file if it is set. |
| RyoheiHagimoto | 0:b4c1e32627f2 | 31 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 32 | #if defined(TARGET_SEMB1402) || defined(TARGET_RZ_A2M_SBEV) |
| RyoheiHagimoto | 0:b4c1e32627f2 | 33 | #error "Audio is not supported." |
| RyoheiHagimoto | 0:b4c1e32627f2 | 34 | #endif |
| RyoheiHagimoto | 0:b4c1e32627f2 | 35 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 36 | #include "mbed.h" |
| RyoheiHagimoto | 0:b4c1e32627f2 | 37 | #include "USBAudio.h" |
| RyoheiHagimoto | 0:b4c1e32627f2 | 38 | #include "AUDIO_GRBoard.h" |
| RyoheiHagimoto | 0:b4c1e32627f2 | 39 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 40 | #define AUDIO_OUT_BUFF_NUM (8) |
| RyoheiHagimoto | 0:b4c1e32627f2 | 41 | #define AUDIO_IN_BUFF_NUM (8) |
| RyoheiHagimoto | 0:b4c1e32627f2 | 42 | #define AUDIO_BUFF_SIZE (4096) |
| RyoheiHagimoto | 0:b4c1e32627f2 | 43 | AUDIO_GRBoard i2s_audio(0x80, AUDIO_OUT_BUFF_NUM - 1, AUDIO_IN_BUFF_NUM); |
| RyoheiHagimoto | 0:b4c1e32627f2 | 44 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 45 | //32 bytes aligned! No cache memory |
| RyoheiHagimoto | 0:b4c1e32627f2 | 46 | static uint8_t audio_out_buff[AUDIO_OUT_BUFF_NUM][AUDIO_BUFF_SIZE] __attribute((section("NC_BSS"),aligned(32))); |
| RyoheiHagimoto | 0:b4c1e32627f2 | 47 | static uint8_t audio_in_buff[AUDIO_IN_BUFF_NUM][AUDIO_BUFF_SIZE] __attribute((section("NC_BSS"),aligned(32))); |
| RyoheiHagimoto | 0:b4c1e32627f2 | 48 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 49 | Semaphore mic_in_sem(0); |
| RyoheiHagimoto | 0:b4c1e32627f2 | 50 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 51 | static void callback_audio_in(void * p_data, int32_t result, void * p_app_data) { |
| RyoheiHagimoto | 0:b4c1e32627f2 | 52 | mic_in_sem.release(); |
| RyoheiHagimoto | 0:b4c1e32627f2 | 53 | } |
| RyoheiHagimoto | 0:b4c1e32627f2 | 54 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 55 | void mic_task(USBAudio * p_usb_audio) { |
| RyoheiHagimoto | 0:b4c1e32627f2 | 56 | rbsp_data_conf_t audio_in_cb = {&callback_audio_in, NULL}; |
| RyoheiHagimoto | 0:b4c1e32627f2 | 57 | uint8_t * p_buf; |
| RyoheiHagimoto | 0:b4c1e32627f2 | 58 | int audio_in_idx = 0; |
| RyoheiHagimoto | 0:b4c1e32627f2 | 59 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 60 | // Read buffer setting |
| RyoheiHagimoto | 0:b4c1e32627f2 | 61 | for (int i = 0; i < AUDIO_IN_BUFF_NUM; i++) { |
| RyoheiHagimoto | 0:b4c1e32627f2 | 62 | i2s_audio.read(audio_in_buff[i], AUDIO_BUFF_SIZE, &audio_in_cb); |
| RyoheiHagimoto | 0:b4c1e32627f2 | 63 | } |
| RyoheiHagimoto | 0:b4c1e32627f2 | 64 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 65 | while (1) { |
| RyoheiHagimoto | 0:b4c1e32627f2 | 66 | mic_in_sem.acquire(); |
| RyoheiHagimoto | 0:b4c1e32627f2 | 67 | p_buf = audio_in_buff[audio_in_idx++]; |
| RyoheiHagimoto | 0:b4c1e32627f2 | 68 | if (audio_in_idx >= AUDIO_IN_BUFF_NUM) { |
| RyoheiHagimoto | 0:b4c1e32627f2 | 69 | audio_in_idx = 0; |
| RyoheiHagimoto | 0:b4c1e32627f2 | 70 | } |
| RyoheiHagimoto | 0:b4c1e32627f2 | 71 | if (i2s_audio.read(p_buf, AUDIO_BUFF_SIZE, &audio_in_cb) < 0) { |
| RyoheiHagimoto | 0:b4c1e32627f2 | 72 | memset(p_buf, 0, AUDIO_BUFF_SIZE); |
| RyoheiHagimoto | 0:b4c1e32627f2 | 73 | } |
| RyoheiHagimoto | 0:b4c1e32627f2 | 74 | p_usb_audio->write(p_buf, AUDIO_BUFF_SIZE); |
| RyoheiHagimoto | 0:b4c1e32627f2 | 75 | } |
| RyoheiHagimoto | 0:b4c1e32627f2 | 76 | } |
| RyoheiHagimoto | 0:b4c1e32627f2 | 77 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 78 | int main() { |
| RyoheiHagimoto | 0:b4c1e32627f2 | 79 | USBAudio usb_audio(true, 44100, 2, 44100, 2); |
| RyoheiHagimoto | 0:b4c1e32627f2 | 80 | rbsp_data_conf_t audio_out_cb = {NULL, NULL}; |
| RyoheiHagimoto | 0:b4c1e32627f2 | 81 | uint8_t * p_buf; |
| RyoheiHagimoto | 0:b4c1e32627f2 | 82 | int audio_out_idx = 0; |
| RyoheiHagimoto | 0:b4c1e32627f2 | 83 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 84 | i2s_audio.power(); // For GR-PEACH and GR-LYCHEE |
| RyoheiHagimoto | 0:b4c1e32627f2 | 85 | i2s_audio.outputVolume(0.5, 0.5); // For GR-PEACH and GR-LYCHEE |
| RyoheiHagimoto | 0:b4c1e32627f2 | 86 | i2s_audio.micVolume(0.7); // For GR-PEACH and GR-LYCHEE |
| RyoheiHagimoto | 0:b4c1e32627f2 | 87 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 88 | Thread micTask; |
| RyoheiHagimoto | 0:b4c1e32627f2 | 89 | micTask.start(callback(mic_task, &usb_audio)); |
| RyoheiHagimoto | 0:b4c1e32627f2 | 90 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 91 | while (1) { |
| RyoheiHagimoto | 0:b4c1e32627f2 | 92 | p_buf = audio_out_buff[audio_out_idx++]; |
| RyoheiHagimoto | 0:b4c1e32627f2 | 93 | if (audio_out_idx >= AUDIO_OUT_BUFF_NUM) { |
| RyoheiHagimoto | 0:b4c1e32627f2 | 94 | audio_out_idx = 0; |
| RyoheiHagimoto | 0:b4c1e32627f2 | 95 | } |
| RyoheiHagimoto | 0:b4c1e32627f2 | 96 | if (!usb_audio.read(p_buf, AUDIO_BUFF_SIZE)) { |
| RyoheiHagimoto | 0:b4c1e32627f2 | 97 | memset(p_buf, 0, AUDIO_BUFF_SIZE); |
| RyoheiHagimoto | 0:b4c1e32627f2 | 98 | } |
| RyoheiHagimoto | 0:b4c1e32627f2 | 99 | i2s_audio.write(p_buf, AUDIO_BUFF_SIZE, &audio_out_cb); |
| RyoheiHagimoto | 0:b4c1e32627f2 | 100 | } |
| RyoheiHagimoto | 0:b4c1e32627f2 | 101 | } |
| RyoheiHagimoto | 0:b4c1e32627f2 | 102 | |
| RyoheiHagimoto | 0:b4c1e32627f2 | 103 | #endif |