不韋 呂 / F746_SAI_IO

Dependencies:   Array_Matrix

Dependents:   F746_SD_WavPlayer F746_SD_GraphicEqualizer_ren0620 Joerg_turbo_ede CW_Decoder_using_FFT_on_DiscoF746NG ... more

Files at this revision

API Documentation at this revision

Comitter:
MikamiUitOpen
Date:
Sat Jul 23 06:13:40 2016 +0000
Parent:
4:2170289dfe94
Child:
6:282075852ccf
Commit message:
6

Changed in this revision

Array_Matrix.lib Show annotated file Show diff for this revision Revisions of this file
MyBSP_AUDIO_IN_OUT_Init.hpp Show diff for this revision Revisions of this file
SAI_InOut.cpp Show annotated file Show diff for this revision Revisions of this file
SAI_InOut.hpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Array_Matrix.lib	Sat Jul 23 06:13:40 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/MikamiUitOpen/code/Array_Matrix/#efe9b1f01090
--- a/MyBSP_AUDIO_IN_OUT_Init.hpp	Wed Jun 15 12:53:20 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,223 +0,0 @@
-//------------------------------------------------------------------------------
-//  The contents of this file are extracted from "stm32746g_discovery_audio.c".
-//
-//  The function MyBSP_AUDIO_IN_OUT_Init() is modified version of
-//  BSP_AUDIO_IN_OUT_Init() in "stm32746g_discovery_audio.c".
-//
-//  2016/05/04, by MIKAMI, Naoki
-//------------------------------------------------------------------------------
-
-#ifndef F746_MY_BSP_AUDIO_IN_OUT_INIT_HPP
-#define F746_MY_BSP_AUDIO_IN_OUT_INIT_HPP
-
-#include "stm32746g_discovery_audio.h"
-
-extern AUDIO_DrvTypeDef  *audio_drv;
-extern SAI_HandleTypeDef haudio_out_sai;
-extern SAI_HandleTypeDef haudio_in_sai;
-extern TIM_HandleTypeDef haudio_tim;
-
-static void SAIx_Out_DeInit(void);
-static void SAIx_In_Init(uint32_t SaiOutMode, uint32_t SlotActive, uint32_t AudioFreq);
-static void SAIx_In_DeInit(void);
-
-uint8_t MyBSP_AUDIO_IN_OUT_Init(uint16_t InputDevice, uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq)
-{
-  uint8_t ret = AUDIO_ERROR;
-  uint32_t deviceid = 0x00;
-  uint32_t slot_active;
-
-//  if (InputDevice != INPUT_DEVICE_DIGITAL_MICROPHONE_2)  /* Only MICROPHONE_2 input supported */
-//  This if statement is modified by MIKAMI
-  if ((InputDevice != INPUT_DEVICE_INPUT_LINE_1) &&       /* INPUT_LINE_1 and MICROPHONE_2 inputs supported */
-      (InputDevice != INPUT_DEVICE_DIGITAL_MICROPHONE_2))
-  {
-    ret = AUDIO_ERROR;
-  }
-  else
-  {
-    /* Disable SAI */
-    SAIx_In_DeInit();
-    SAIx_Out_DeInit();
-
-    /* PLL clock is set depending on the AudioFreq (44.1khz vs 48khz groups) */
-    BSP_AUDIO_OUT_ClockConfig(&haudio_in_sai, AudioFreq, NULL); /* Clock config is shared between AUDIO IN and OUT */
-
-    /* SAI data transfer preparation:
-    Prepare the Media to be used for the audio transfer from SAI peripheral to memory */
-    haudio_in_sai.Instance = AUDIO_IN_SAIx;
-    if(HAL_SAI_GetState(&haudio_in_sai) == HAL_SAI_STATE_RESET)
-    {
-      /* Init the SAI MSP: this __weak function can be redefined by the application*/
-      BSP_AUDIO_IN_MspInit(&haudio_in_sai, NULL);
-    }
-
-    /* SAI data transfer preparation:
-    Prepare the Media to be used for the audio transfer from memory to SAI peripheral */
-    haudio_out_sai.Instance = AUDIO_OUT_SAIx;
-    if(HAL_SAI_GetState(&haudio_out_sai) == HAL_SAI_STATE_RESET)
-    {
-      /* Init the SAI MSP: this __weak function can be redefined by the application*/
-      BSP_AUDIO_OUT_MspInit(&haudio_out_sai, NULL);
-    }
-
-    /* Configure SAI in master mode :
-     *   - SAI2_block_A in master TX mode
-     *   - SAI2_block_B in slave RX mode synchronous from SAI2_block_A
-     */
-    if (InputDevice == INPUT_DEVICE_DIGITAL_MICROPHONE_2)
-    {
-      slot_active = CODEC_AUDIOFRAME_SLOT_13;
-    }
-    else
-    {
-      slot_active = CODEC_AUDIOFRAME_SLOT_02;
-    }
-    SAIx_In_Init(SAI_MODEMASTER_TX, slot_active, AudioFreq);
-
-    /* wm8994 codec initialization */
-    deviceid = wm8994_drv.ReadID(AUDIO_I2C_ADDRESS);
-
-    if((deviceid) == WM8994_ID)
-    {
-      /* Reset the Codec Registers */
-      wm8994_drv.Reset(AUDIO_I2C_ADDRESS);
-      /* Initialize the audio driver structure */
-      audio_drv = &wm8994_drv;
-      ret = AUDIO_OK;
-    }
-    else
-    {
-      ret = AUDIO_ERROR;
-    }
-
-    if(ret == AUDIO_OK)
-    {
-      /* Initialize the codec internal registers */
-      audio_drv->Init(AUDIO_I2C_ADDRESS, InputDevice | OutputDevice, Volume, AudioFreq);
-    }
-  }
-  return ret;
-}
-
-static void SAIx_Out_DeInit(void)
-{
-  /* Initialize the haudio_out_sai Instance parameter */
-  haudio_out_sai.Instance = AUDIO_OUT_SAIx;
-
-  /* Disable SAI peripheral */
-  __HAL_SAI_DISABLE(&haudio_out_sai);
-
-  HAL_SAI_DeInit(&haudio_out_sai);
-}
-
-static void SAIx_In_DeInit(void)
-{
-  /* Initialize the haudio_in_sai Instance parameter */
-  haudio_in_sai.Instance = AUDIO_IN_SAIx;
-
-  /* Disable SAI peripheral */
-  __HAL_SAI_DISABLE(&haudio_in_sai);
-
-  HAL_SAI_DeInit(&haudio_in_sai);
-}
-
-static void SAIx_In_Init(uint32_t SaiOutMode, uint32_t SlotActive, uint32_t AudioFreq)
-{
-  /* Initialize SAI2 block A in MASTER RX */
-  /* Initialize the haudio_out_sai Instance parameter */
-  haudio_out_sai.Instance = AUDIO_OUT_SAIx;
-
-  /* Disable SAI peripheral to allow access to SAI internal registers */
-  __HAL_SAI_DISABLE(&haudio_out_sai);
-
-  /* Configure SAI_Block_x
-  LSBFirst: Disabled
-  DataSize: 16 */
-  haudio_out_sai.Init.AudioFrequency = AudioFreq;
-  haudio_out_sai.Init.AudioMode = SaiOutMode;
-  haudio_out_sai.Init.NoDivider = SAI_MASTERDIVIDER_ENABLED;
-  haudio_out_sai.Init.Protocol = SAI_FREE_PROTOCOL;
-  haudio_out_sai.Init.DataSize = SAI_DATASIZE_16;
-  haudio_out_sai.Init.FirstBit = SAI_FIRSTBIT_MSB;
-  haudio_out_sai.Init.ClockStrobing = SAI_CLOCKSTROBING_RISINGEDGE;
-  haudio_out_sai.Init.Synchro = SAI_ASYNCHRONOUS;
-  haudio_out_sai.Init.OutputDrive = SAI_OUTPUTDRIVE_ENABLED;
-  haudio_out_sai.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_1QF;
-
-  /* Configure SAI_Block_x Frame
-  Frame Length: 64
-  Frame active Length: 32
-  FS Definition: Start frame + Channel Side identification
-  FS Polarity: FS active Low
-  FS Offset: FS asserted one bit before the first bit of slot 0 */
-  haudio_out_sai.FrameInit.FrameLength = 64;
-  haudio_out_sai.FrameInit.ActiveFrameLength = 32;
-  haudio_out_sai.FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION;
-  haudio_out_sai.FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW;
-  haudio_out_sai.FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT;
-
-  /* Configure SAI Block_x Slot
-  Slot First Bit Offset: 0
-  Slot Size  : 16
-  Slot Number: 4
-  Slot Active: All slot actives */
-  haudio_out_sai.SlotInit.FirstBitOffset = 0;
-  haudio_out_sai.SlotInit.SlotSize = SAI_SLOTSIZE_DATASIZE;
-  haudio_out_sai.SlotInit.SlotNumber = 4;
-  haudio_out_sai.SlotInit.SlotActive = SlotActive;
-
-  HAL_SAI_Init(&haudio_out_sai);
-
-  /* Initialize SAI2 block B in SLAVE RX synchronous from SAI2 block A */
-  /* Initialize the haudio_in_sai Instance parameter */
-  haudio_in_sai.Instance = AUDIO_IN_SAIx;
-  
-  /* Disable SAI peripheral to allow access to SAI internal registers */
-  __HAL_SAI_DISABLE(&haudio_in_sai);
-  
-  /* Configure SAI_Block_x
-  LSBFirst: Disabled
-  DataSize: 16 */
-  haudio_in_sai.Init.AudioFrequency = AudioFreq;
-  haudio_in_sai.Init.AudioMode = SAI_MODESLAVE_RX;
-  haudio_in_sai.Init.NoDivider = SAI_MASTERDIVIDER_ENABLED;
-  haudio_in_sai.Init.Protocol = SAI_FREE_PROTOCOL;
-  haudio_in_sai.Init.DataSize = SAI_DATASIZE_16;
-  haudio_in_sai.Init.FirstBit = SAI_FIRSTBIT_MSB;
-  haudio_in_sai.Init.ClockStrobing = SAI_CLOCKSTROBING_RISINGEDGE;
-  haudio_in_sai.Init.Synchro = SAI_SYNCHRONOUS;
-  haudio_in_sai.Init.OutputDrive = SAI_OUTPUTDRIVE_DISABLED;
-  haudio_in_sai.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_1QF;
-  
-  /* Configure SAI_Block_x Frame
-  Frame Length: 64
-  Frame active Length: 32
-  FS Definition: Start frame + Channel Side identification
-  FS Polarity: FS active Low
-  FS Offset: FS asserted one bit before the first bit of slot 0 */
-  haudio_in_sai.FrameInit.FrameLength = 64;
-  haudio_in_sai.FrameInit.ActiveFrameLength = 32;
-  haudio_in_sai.FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION;
-  haudio_in_sai.FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW;
-  haudio_in_sai.FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT;
-  
-  /* Configure SAI Block_x Slot
-  Slot First Bit Offset: 0
-  Slot Size  : 16
-  Slot Number: 4
-  Slot Active: All slot active */
-  haudio_in_sai.SlotInit.FirstBitOffset = 0;
-  haudio_in_sai.SlotInit.SlotSize = SAI_SLOTSIZE_DATASIZE;
-  haudio_in_sai.SlotInit.SlotNumber = 4;
-  haudio_in_sai.SlotInit.SlotActive = SlotActive;
-
-  HAL_SAI_Init(&haudio_in_sai);
-
-  /* Enable SAI peripheral to generate MCLK */
-  __HAL_SAI_ENABLE(&haudio_out_sai);
-
-  /* Enable SAI peripheral */
-  __HAL_SAI_ENABLE(&haudio_in_sai);
-}
-#endif  // F746_MY_BSP_AUDIO_IN_OUT_INIT_HPP
--- a/SAI_InOut.cpp	Wed Jun 15 12:53:20 2016 +0000
+++ b/SAI_InOut.cpp	Sat Jul 23 06:13:40 2016 +0000
@@ -1,10 +1,9 @@
 //-----------------------------------------------------------
 //  SiaIO class
-//  2016/06/16, Copyright (c) 2016 MIKAMI, Naoki
+//  2016/07/23, Copyright (c) 2016 MIKAMI, Naoki
 //-----------------------------------------------------------
 
 #include "SAI_InOut.hpp"
-#include "MyBSP_AUDIO_IN_OUT_Init.hpp"
 
 namespace Mikami
 {
@@ -18,14 +17,14 @@
         
         if (ioBoth != OUTPUT)
         {
-            inBuffer_ = new int16_t[(size*2)*2];
+            inBuffer_.SetSize((size*2)*2);
             inOffset_ = 0;
             captured_ = false;
         }
         if (ioBoth != INPUT)
         {
-            outBuffer_ = new int16_t[(size*2)*2];
-            tmp_ = new int16_t[size*2];
+            outBuffer_.SetSize((size*2)*2);
+            tmp_.SetSize(size*2);
             tmpIndex_ = 0;
             xferred_ = false;
             ClearBuffer();
@@ -33,17 +32,10 @@
         InitCodec(inputDevice);
     }
 
-    SaiIO::~SaiIO()
-    {
-        delete[] tmp_;
-        delete[] outBuffer_;
-        if (IOBOTH_ != OUTPUT) delete[] inBuffer_;
-    }
-
     // Input start
     void SaiIO::RecordIn()
     {
-        if (BSP_AUDIO_IN_Record((uint16_t*)inBuffer_,
+        if (BSP_AUDIO_IN_Record((uint16_t *)((int16_t *)inBuffer_),
                                 bufferSize_) == AUDIO_ERROR)
             ErrorTrap();
     }
@@ -69,22 +61,16 @@
         return true;
     }
 
-    // Input from input buffer
-    void SaiIO::Input(int16_t &xL, int16_t &xR)
-    {
-        xL = inBuffer_[inIndex_++];
-        xR = inBuffer_[inIndex_++];
-    }
-
     void SaiIO::PlayOut()
     {
         ClearBuffer();
         BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02);
-        if (BSP_AUDIO_OUT_Play((uint16_t *)outBuffer_,
+        if (BSP_AUDIO_OUT_Play((uint16_t *)((int16_t *)outBuffer_),
                                bufferSize_*AUDIODATA_SIZE) == AUDIO_ERROR)
             ErrorTrap();
     }
 
+    // Return true if transfer completion to output
     bool SaiIO::IsXferred()
     {
         if (!xferred_) return false;
@@ -127,11 +113,13 @@
     void SaiIO::InitInput(uint16_t inputDevice)
     {
         int audioInVolume = (inputDevice == INPUT_DEVICE_INPUT_LINE_1) ?
-                            60 : 100;
+                            70 : 100;
+        InputFp = (inputDevice == INPUT_DEVICE_INPUT_LINE_1) ?
+                  &SaiIO::InputNormal : &SaiIO::InputReversal;
 
         if (IOBOTH_ == BOTH)
-            if (MyBSP_AUDIO_IN_OUT_Init(inputDevice, OUTPUT_DEVICE_HEADPHONE,
-                                        audioInVolume, FS_) == AUDIO_ERROR)
+            if (BSP_AUDIO_IN_OUT_Init(inputDevice, OUTPUT_DEVICE_HEADPHONE,
+                                      audioInVolume, FS_) == AUDIO_ERROR)
                 ErrorTrap();
 
         if (IOBOTH_ == INPUT)
@@ -151,12 +139,6 @@
         NVIC_SetVector(AUDIO_OUT_SAIx_DMAx_IRQ,
                        (uint32_t)AUDIO_OUT_SAIx_DMAx_IRQHandler);
         BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02);
-            
-        // Set output volume of headphone maximum
-        //      AUDIO_IO_Write() is defined in "stm32746g_discovery.c"
-        //      AUDIO_I2C_ADDRESS is defined in "stm32746g_discovery.h"
-        AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x1C, 0x17F); // 0x1C: R28 of WM8994
-        AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x1D, 0x17F); // 0x1D: R29 of WM8994
     }
 
     void SaiIO::ClearBuffer()
@@ -167,6 +149,20 @@
         for (int n=0; n<nData_*2; n++) tmp_[n] = 0;
     }
 
+    // For line input
+    void SaiIO::InputNormal(int16_t &xL, int16_t &xR)
+    {
+        xL = inBuffer_[inIndex_++];
+        xR = inBuffer_[inIndex_++];
+    }
+
+    // For MEMS microphone input
+    void SaiIO::InputReversal(int16_t &xL, int16_t &xR)       
+    {
+        xR = inBuffer_[inIndex_++];
+        xL = inBuffer_[inIndex_++];
+    }
+
     void SaiIO::Captured(int32_t offset)
     {
         captured_ = true;
@@ -187,7 +183,7 @@
     __IO bool SaiIO::captured_;
     __IO int32_t SaiIO::inOffset_;
 
-    int16_t* SaiIO::outBuffer_;
-    int16_t* SaiIO::tmp_;       
+    Array<int16_t> SaiIO::outBuffer_;
+    Array<int16_t> SaiIO::tmp_;       
     __IO bool SaiIO::xferred_;
 }
--- a/SAI_InOut.hpp	Wed Jun 15 12:53:20 2016 +0000
+++ b/SAI_InOut.hpp	Sat Jul 23 06:13:40 2016 +0000
@@ -1,6 +1,6 @@
 //-----------------------------------------------------------
 //  SiaIO class (Header)
-//  2016/06/16, Copyright (c) 2016 MIKAMI, Naoki
+//  2016/07/23, Copyright (c) 2016 MIKAMI, Naoki
 //-----------------------------------------------------------
 
 #ifndef F746_SAI_IO_HPP
@@ -10,6 +10,7 @@
 #include "stm32746g_discovery_audio.h"
 #include "BSP_AudioIn_Overwrite.hpp"
 #include "BSP_AudioOut_Overwrite.hpp"
+#include "Array.hpp"
 
 namespace Mikami
 {
@@ -25,7 +26,7 @@
         //      inputDevice == 0 : not use input device
         SaiIO(InOutBoth ioBoth, int size, int fs,
               uint16_t inputDevice = 0);
-        ~SaiIO();
+        virtual ~SaiIO() {}
  
         int32_t GetLength() { return nData_; }
         
@@ -36,7 +37,9 @@
 
         bool IsCaptured();
         void ResetCaptured() { captured_ = false; }
-        void Input(int16_t &xL, int16_t &xR);
+        // Input using SAI
+        void Input(int16_t &xL, int16_t &xR)
+        {   (this->*InputFp)(xL, xR); }
 
         void StopIn()   { BSP_AUDIO_IN_Stop(CODEC_PDWN_SW); }
         void PauseIn()  { BSP_AUDIO_IN_Pause(); }
@@ -45,6 +48,7 @@
         void PlayOut();
         bool IsXferred();
         void ResetXferred() { xferred_ = false; }
+        // Output using SAI
         void Output(int16_t xL, int16_t xR);
       
         void StopOut()   { BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW); }
@@ -76,9 +80,9 @@
         const int FS_;
         const InOutBoth IOBOTH_; 
 
-        int16_t* inBuffer_;
-        static int16_t* outBuffer_;
-        static int16_t* tmp_;       
+        Array<int16_t> inBuffer_;
+        static Array<int16_t> outBuffer_;
+        static Array<int16_t> tmp_;       
         __IO int32_t inIndex_;
         __IO int32_t tmpIndex_;
 
@@ -88,13 +92,20 @@
         static __IO int32_t inOffset_;
         static __IO bool xferred_;
 
-
         void InitCodec(uint16_t inputDevice);
         void InitInput(uint16_t inputDevice);
         void SetInput();
         void SetOutput();
         void ClearBuffer();
 
+        // This function pointer is assigned by
+        // InputNormal() or InputReversal()
+        void (SaiIO::*InputFp)(int16_t &, int16_t &);
+        // For line input
+        void InputNormal(int16_t &xL, int16_t &xR);
+        // For MEMS microphone input
+        void InputReversal(int16_t &xL, int16_t &xR);
+        
         static void Captured(int32_t offset);
         static void FillBuffer(uint32_t offset);
     };