Final version.
Fork of F746_SAI_IO by
SAI_InOut.cpp@2:1aef7b703249, 2016-05-09 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Mon May 09 13:31:36 2016 +0000
- Revision:
- 2:1aef7b703249
- Parent:
- 1:48ed86c8430a
- Child:
- 3:3bfdd8be834f
3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:eade5d3ae0eb | 1 | //----------------------------------------------------------- |
MikamiUitOpen | 0:eade5d3ae0eb | 2 | // SiaIO class |
MikamiUitOpen | 1:48ed86c8430a | 3 | // 2016/05/09, Copyright (c) 2016 MIKAMI, Naoki |
MikamiUitOpen | 0:eade5d3ae0eb | 4 | //----------------------------------------------------------- |
MikamiUitOpen | 0:eade5d3ae0eb | 5 | |
MikamiUitOpen | 0:eade5d3ae0eb | 6 | #include "SAI_InOut.hpp" |
MikamiUitOpen | 0:eade5d3ae0eb | 7 | #include "MyBSP_AUDIO_IN_OUT_Init.hpp" |
MikamiUitOpen | 0:eade5d3ae0eb | 8 | |
MikamiUitOpen | 0:eade5d3ae0eb | 9 | namespace Mikami |
MikamiUitOpen | 0:eade5d3ae0eb | 10 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 11 | // Constructor |
MikamiUitOpen | 0:eade5d3ae0eb | 12 | SaiIO::SaiIO(InOutBoth ioBoth, int size, int fs, |
MikamiUitOpen | 0:eade5d3ae0eb | 13 | uint16_t inputDevice) |
MikamiUitOpen | 0:eade5d3ae0eb | 14 | : FS_(fs), IOBOTH_(ioBoth) |
MikamiUitOpen | 0:eade5d3ae0eb | 15 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 16 | nData_ = size; |
MikamiUitOpen | 0:eade5d3ae0eb | 17 | bufferSize_ = (size*2)*2; |
MikamiUitOpen | 0:eade5d3ae0eb | 18 | |
MikamiUitOpen | 0:eade5d3ae0eb | 19 | if (ioBoth != OUTPUT) |
MikamiUitOpen | 0:eade5d3ae0eb | 20 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 21 | inBuffer_ = new int16_t[(size*2)*2]; |
MikamiUitOpen | 0:eade5d3ae0eb | 22 | inOffset_ = 0; |
MikamiUitOpen | 0:eade5d3ae0eb | 23 | captured_ = false; |
MikamiUitOpen | 0:eade5d3ae0eb | 24 | } |
MikamiUitOpen | 2:1aef7b703249 | 25 | if (ioBoth != INPUT) |
MikamiUitOpen | 2:1aef7b703249 | 26 | { |
MikamiUitOpen | 2:1aef7b703249 | 27 | outBuffer_ = new int16_t[(size*2)*2]; |
MikamiUitOpen | 2:1aef7b703249 | 28 | tmp_ = new int16_t[size*2]; |
MikamiUitOpen | 2:1aef7b703249 | 29 | tmpIndex_ = 0; |
MikamiUitOpen | 2:1aef7b703249 | 30 | xferred_ = false; |
MikamiUitOpen | 2:1aef7b703249 | 31 | ClearBuffer(); |
MikamiUitOpen | 2:1aef7b703249 | 32 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 33 | InitCodec(inputDevice); |
MikamiUitOpen | 0:eade5d3ae0eb | 34 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 35 | |
MikamiUitOpen | 0:eade5d3ae0eb | 36 | SaiIO::~SaiIO() |
MikamiUitOpen | 0:eade5d3ae0eb | 37 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 38 | delete[] tmp_; |
MikamiUitOpen | 0:eade5d3ae0eb | 39 | delete[] outBuffer_; |
MikamiUitOpen | 0:eade5d3ae0eb | 40 | if (IOBOTH_ != OUTPUT) delete[] inBuffer_; |
MikamiUitOpen | 0:eade5d3ae0eb | 41 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 42 | |
MikamiUitOpen | 2:1aef7b703249 | 43 | // Input start |
MikamiUitOpen | 2:1aef7b703249 | 44 | void SaiIO::RecordIn() |
MikamiUitOpen | 0:eade5d3ae0eb | 45 | { |
MikamiUitOpen | 2:1aef7b703249 | 46 | if (BSP_AUDIO_IN_Record((uint16_t*)inBuffer_, |
MikamiUitOpen | 2:1aef7b703249 | 47 | bufferSize_) == AUDIO_ERROR) |
MikamiUitOpen | 2:1aef7b703249 | 48 | ErrorTrap(); |
MikamiUitOpen | 0:eade5d3ae0eb | 49 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 50 | |
MikamiUitOpen | 0:eade5d3ae0eb | 51 | // Switching input device and run |
MikamiUitOpen | 0:eade5d3ae0eb | 52 | void SaiIO::SwitchInputDevice(int sw) |
MikamiUitOpen | 0:eade5d3ae0eb | 53 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 54 | uint16_t dev = (sw == 0) ? |
MikamiUitOpen | 0:eade5d3ae0eb | 55 | INPUT_DEVICE_DIGITAL_MICROPHONE_2 |
MikamiUitOpen | 0:eade5d3ae0eb | 56 | : INPUT_DEVICE_INPUT_LINE_1; |
MikamiUitOpen | 0:eade5d3ae0eb | 57 | InitCodec(dev); |
MikamiUitOpen | 0:eade5d3ae0eb | 58 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 59 | |
MikamiUitOpen | 0:eade5d3ae0eb | 60 | // If captured, return true |
MikamiUitOpen | 0:eade5d3ae0eb | 61 | bool SaiIO::IsCaptured() |
MikamiUitOpen | 0:eade5d3ae0eb | 62 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 63 | if (!captured_) return false; |
MikamiUitOpen | 0:eade5d3ae0eb | 64 | |
MikamiUitOpen | 0:eade5d3ae0eb | 65 | inIndex_ = inOffset_; |
MikamiUitOpen | 0:eade5d3ae0eb | 66 | return true; |
MikamiUitOpen | 0:eade5d3ae0eb | 67 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 68 | |
MikamiUitOpen | 0:eade5d3ae0eb | 69 | // Input from input buffer |
MikamiUitOpen | 0:eade5d3ae0eb | 70 | void SaiIO::Input(int16_t &xL, int16_t &xR) |
MikamiUitOpen | 0:eade5d3ae0eb | 71 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 72 | xL = inBuffer_[inIndex_++]; |
MikamiUitOpen | 0:eade5d3ae0eb | 73 | xR = inBuffer_[inIndex_++]; |
MikamiUitOpen | 0:eade5d3ae0eb | 74 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 75 | |
MikamiUitOpen | 1:48ed86c8430a | 76 | void SaiIO::PlayOut() |
MikamiUitOpen | 1:48ed86c8430a | 77 | { |
MikamiUitOpen | 2:1aef7b703249 | 78 | ClearBuffer(); |
MikamiUitOpen | 1:48ed86c8430a | 79 | if (BSP_AUDIO_OUT_Play((uint16_t *)outBuffer_, |
MikamiUitOpen | 1:48ed86c8430a | 80 | bufferSize_*AUDIODATA_SIZE) == AUDIO_ERROR) |
MikamiUitOpen | 1:48ed86c8430a | 81 | ErrorTrap(); |
MikamiUitOpen | 1:48ed86c8430a | 82 | } |
MikamiUitOpen | 1:48ed86c8430a | 83 | |
MikamiUitOpen | 0:eade5d3ae0eb | 84 | bool SaiIO::IsXferred() |
MikamiUitOpen | 0:eade5d3ae0eb | 85 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 86 | if (!xferred_) return false; |
MikamiUitOpen | 0:eade5d3ae0eb | 87 | |
MikamiUitOpen | 0:eade5d3ae0eb | 88 | tmpIndex_ = 0; |
MikamiUitOpen | 0:eade5d3ae0eb | 89 | return true; |
MikamiUitOpen | 0:eade5d3ae0eb | 90 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 91 | |
MikamiUitOpen | 0:eade5d3ae0eb | 92 | void SaiIO::Output(int16_t xL, int16_t xR) |
MikamiUitOpen | 0:eade5d3ae0eb | 93 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 94 | tmp_[tmpIndex_++] = xL; // Left |
MikamiUitOpen | 0:eade5d3ae0eb | 95 | tmp_[tmpIndex_++] = xR; // Right |
MikamiUitOpen | 0:eade5d3ae0eb | 96 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 97 | |
MikamiUitOpen | 0:eade5d3ae0eb | 98 | void SaiIO::ErrorTrap() |
MikamiUitOpen | 0:eade5d3ae0eb | 99 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 100 | DigitalOut led1(LED1); |
MikamiUitOpen | 0:eade5d3ae0eb | 101 | fprintf(stderr, "\r\n#### ERROR ####\r\n"); |
MikamiUitOpen | 0:eade5d3ae0eb | 102 | while(true) |
MikamiUitOpen | 0:eade5d3ae0eb | 103 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 104 | led1 = !led1; |
MikamiUitOpen | 0:eade5d3ae0eb | 105 | wait_ms(250); |
MikamiUitOpen | 0:eade5d3ae0eb | 106 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 107 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 108 | |
MikamiUitOpen | 0:eade5d3ae0eb | 109 | // Initialize audio input and output |
MikamiUitOpen | 0:eade5d3ae0eb | 110 | void SaiIO::InitCodec(uint16_t inputDevice) |
MikamiUitOpen | 0:eade5d3ae0eb | 111 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 112 | int audioInVolume = 0; |
MikamiUitOpen | 0:eade5d3ae0eb | 113 | if (inputDevice != 0) |
MikamiUitOpen | 0:eade5d3ae0eb | 114 | audioInVolume = (inputDevice == INPUT_DEVICE_INPUT_LINE_1) ? |
MikamiUitOpen | 0:eade5d3ae0eb | 115 | 60 : 100; |
MikamiUitOpen | 0:eade5d3ae0eb | 116 | |
MikamiUitOpen | 0:eade5d3ae0eb | 117 | if (IOBOTH_ == BOTH) |
MikamiUitOpen | 0:eade5d3ae0eb | 118 | if (MyBSP_AUDIO_IN_OUT_Init(inputDevice, OUTPUT_DEVICE_HEADPHONE, |
MikamiUitOpen | 0:eade5d3ae0eb | 119 | audioInVolume, FS_) == AUDIO_ERROR) |
MikamiUitOpen | 0:eade5d3ae0eb | 120 | ErrorTrap(); |
MikamiUitOpen | 0:eade5d3ae0eb | 121 | |
MikamiUitOpen | 0:eade5d3ae0eb | 122 | if (IOBOTH_ == INPUT) |
MikamiUitOpen | 0:eade5d3ae0eb | 123 | if (BSP_AUDIO_IN_Init(inputDevice, |
MikamiUitOpen | 0:eade5d3ae0eb | 124 | audioInVolume, FS_) == AUDIO_ERROR) |
MikamiUitOpen | 0:eade5d3ae0eb | 125 | ErrorTrap(); |
MikamiUitOpen | 0:eade5d3ae0eb | 126 | |
MikamiUitOpen | 0:eade5d3ae0eb | 127 | if (IOBOTH_ == OUTPUT) |
MikamiUitOpen | 0:eade5d3ae0eb | 128 | if (BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_HEADPHONE, |
MikamiUitOpen | 0:eade5d3ae0eb | 129 | 90, FS_) == AUDIO_ERROR) |
MikamiUitOpen | 0:eade5d3ae0eb | 130 | ErrorTrap(); |
MikamiUitOpen | 0:eade5d3ae0eb | 131 | |
MikamiUitOpen | 0:eade5d3ae0eb | 132 | if (IOBOTH_ != OUTPUT) InitInput(); |
MikamiUitOpen | 0:eade5d3ae0eb | 133 | if (IOBOTH_ != INPUT) InitOutput(); |
MikamiUitOpen | 0:eade5d3ae0eb | 134 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 135 | |
MikamiUitOpen | 0:eade5d3ae0eb | 136 | void SaiIO::InitInput() |
MikamiUitOpen | 0:eade5d3ae0eb | 137 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 138 | NVIC_SetVector(AUDIO_IN_SAIx_DMAx_IRQ, |
MikamiUitOpen | 0:eade5d3ae0eb | 139 | (uint32_t)AUDIO_IN_SAIx_DMAx_IRQHandler); |
MikamiUitOpen | 0:eade5d3ae0eb | 140 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 141 | |
MikamiUitOpen | 0:eade5d3ae0eb | 142 | void SaiIO::InitOutput() |
MikamiUitOpen | 0:eade5d3ae0eb | 143 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 144 | NVIC_SetVector(AUDIO_OUT_SAIx_DMAx_IRQ, |
MikamiUitOpen | 0:eade5d3ae0eb | 145 | (uint32_t)AUDIO_OUT_SAIx_DMAx_IRQHandler); |
MikamiUitOpen | 0:eade5d3ae0eb | 146 | BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02); |
MikamiUitOpen | 0:eade5d3ae0eb | 147 | |
MikamiUitOpen | 0:eade5d3ae0eb | 148 | // Set output volume of headphone maximum |
MikamiUitOpen | 0:eade5d3ae0eb | 149 | // AUDIO_IO_Write() is defined in "stm32746g_discovery.c" |
MikamiUitOpen | 0:eade5d3ae0eb | 150 | // AUDIO_I2C_ADDRESS is defined in "stm32746g_discovery.h" |
MikamiUitOpen | 0:eade5d3ae0eb | 151 | AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x1C, 0x17F); // 0x1C: R28 of WM8994 |
MikamiUitOpen | 0:eade5d3ae0eb | 152 | AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x1D, 0x17F); // 0x1D: R29 of WM8994 |
MikamiUitOpen | 0:eade5d3ae0eb | 153 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 154 | |
MikamiUitOpen | 0:eade5d3ae0eb | 155 | void SaiIO::ClearBuffer() |
MikamiUitOpen | 0:eade5d3ae0eb | 156 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 157 | if (IOBOTH_ != OUTPUT) |
MikamiUitOpen | 0:eade5d3ae0eb | 158 | for (int n=0; n<bufferSize_; n++) inBuffer_[n] = 0; |
MikamiUitOpen | 0:eade5d3ae0eb | 159 | for (int n=0; n<bufferSize_; n++) outBuffer_[n] = 0; |
MikamiUitOpen | 0:eade5d3ae0eb | 160 | for (int n=0; n<nData_*2; n++) tmp_[n] = 0; |
MikamiUitOpen | 0:eade5d3ae0eb | 161 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 162 | |
MikamiUitOpen | 0:eade5d3ae0eb | 163 | void SaiIO::Captured(int32_t offset) |
MikamiUitOpen | 0:eade5d3ae0eb | 164 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 165 | captured_ = true; |
MikamiUitOpen | 0:eade5d3ae0eb | 166 | inOffset_ = offset; |
MikamiUitOpen | 0:eade5d3ae0eb | 167 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 168 | |
MikamiUitOpen | 0:eade5d3ae0eb | 169 | void SaiIO::FillBuffer(uint32_t offset) |
MikamiUitOpen | 0:eade5d3ae0eb | 170 | { |
MikamiUitOpen | 0:eade5d3ae0eb | 171 | int k = offset; |
MikamiUitOpen | 0:eade5d3ae0eb | 172 | for (int n=0; n<nData_*2; n++) |
MikamiUitOpen | 0:eade5d3ae0eb | 173 | outBuffer_[k++] = tmp_[n]; |
MikamiUitOpen | 0:eade5d3ae0eb | 174 | xferred_ = true; |
MikamiUitOpen | 0:eade5d3ae0eb | 175 | } |
MikamiUitOpen | 0:eade5d3ae0eb | 176 | |
MikamiUitOpen | 0:eade5d3ae0eb | 177 | int32_t SaiIO::nData_; |
MikamiUitOpen | 0:eade5d3ae0eb | 178 | int32_t SaiIO::bufferSize_; |
MikamiUitOpen | 0:eade5d3ae0eb | 179 | |
MikamiUitOpen | 0:eade5d3ae0eb | 180 | __IO bool SaiIO::captured_; |
MikamiUitOpen | 0:eade5d3ae0eb | 181 | __IO int32_t SaiIO::inOffset_; |
MikamiUitOpen | 0:eade5d3ae0eb | 182 | |
MikamiUitOpen | 0:eade5d3ae0eb | 183 | int16_t* SaiIO::outBuffer_; |
MikamiUitOpen | 0:eade5d3ae0eb | 184 | int16_t* SaiIO::tmp_; |
MikamiUitOpen | 0:eade5d3ae0eb | 185 | __IO bool SaiIO::xferred_; |
MikamiUitOpen | 0:eade5d3ae0eb | 186 | } |
MikamiUitOpen | 2:1aef7b703249 | 187 |