Guest User

Untitled

a guest
Oct 7th, 2015
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.68 KB | None | 0 0
  1.  
  2.  
  3. #include "stm32f4xx.h"
  4. #include <stdio.h>
  5. #include "stm32f4xx_it.h"
  6. #include "pdm_filter.h"
  7.  
  8. #include "stm32f4xx_rcc.h"
  9. #include "stm32f4xx_gpio.h"
  10. #include "stm32f4xx_tim.h"
  11.    
  12. #define SPI_SCK_PIN                       GPIO_Pin_10
  13. #define SPI_SCK_GPIO_PORT                 GPIOB
  14. #define SPI_SCK_GPIO_CLK                  RCC_AHB1Periph_GPIOB
  15. #define SPI_SCK_SOURCE                    GPIO_PinSource10
  16. #define SPI_SCK_AF                        GPIO_AF_SPI2
  17. #define SPI_MOSI_PIN                      GPIO_Pin_3
  18. #define SPI_MOSI_GPIO_PORT                GPIOC
  19. #define SPI_MOSI_GPIO_CLK                 RCC_AHB1Periph_GPIOC
  20. #define SPI_MOSI_SOURCE                   GPIO_PinSource3
  21. #define SPI_MOSI_AF                       GPIO_AF_SPI2
  22.  
  23. #define AUDIO_REC_SPI_IRQHANDLER          SPI2_IRQHandler
  24. #define REC_FREQ                16000     // Audio recording frequency in Hz
  25. #define INTERNAL_BUFF_SIZE      64        // PDM buffer input size
  26. #define PCM_OUT_SIZE            16        // PCM buffer output size
  27.  
  28. static uint32_t AudioRecInited = 0;       // Current state of the audio recorder interface intialization
  29. PDMFilter_InitStruct Filter;              // Audio recording Samples format (from 8 to 16 bits)
  30. uint32_t AudioRecBitRes = 16;             // The audio sample amplitude resolution
  31. uint16_t RecBuf[PCM_OUT_SIZE];            // A pointer to an buffer
  32. uint32_t AudioRecChnlNbr = 1;             // Audio recording number of channels (1 for Mono or 2 for Stereo)
  33. uint16_t* pAudioRecBuf;                   // Main buffer pointer for the recorded data storing
  34. uint32_t AudioRecCurrSize = 0;            // Current size of the recorded buffer
  35.  
  36. static uint16_t InternalBuffer[INTERNAL_BUFF_SIZE]; // Temporary data sample
  37. static uint32_t InternalBufferSize = 0;
  38.  
  39. void WaveRecorderBeginSampling (void);
  40.  
  41. void WaveRecorder_GPIO_Init(void);
  42. void WaveRecorder_SPI_Init(uint32_t Freq);
  43. void WaveRecorder_NVIC_Init(void);
  44. uint32_t WaveRecorderInit(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr);
  45. uint8_t WaveRecorderStart(uint16_t* pbuf, uint32_t size);
  46.  
  47. void gpio_init(void);
  48.    
  49. int main(void) {
  50.    
  51.     /* Initialize system */
  52.     SystemInit();
  53.   gpio_init();
  54.     WaveRecorderBeginSampling();
  55.    
  56.     while (1) {
  57.         GPIO_SetBits(GPIOD, GPIO_Pin_12);      
  58.     }
  59.  
  60. }
  61.  
  62.  
  63. void WaveRecorderBeginSampling (void)
  64. {
  65.   WaveRecorderInit(32000,16, 1);
  66.   WaveRecorderStart(RecBuf, PCM_OUT_SIZE);
  67. }
  68.  
  69. uint32_t WaveRecorderInit(uint32_t AudioFreq, uint32_t BitRes, uint32_t ChnlNbr)
  70. {
  71.   /* Check if the interface is already initialized */
  72.   if (AudioRecInited)
  73.   {
  74.     /* No need for initialization */
  75.     return 0;
  76.   }
  77.   else
  78.   {
  79.     /* Enable CRC module */
  80.     RCC->AHB1ENR |= RCC_AHB1ENR_CRCEN;
  81.    
  82.     /* Filter LP & HP Init */
  83.     Filter.LP_HZ = 8000;
  84.     Filter.HP_HZ = 0;
  85.     Filter.Fs = 16000;
  86.     Filter.Out_MicChannels = 1;
  87.     Filter.In_MicChannels = 1;
  88.    
  89.     PDM_Filter_Init((PDMFilter_InitStruct *)&Filter);
  90.    
  91.     /* Configure the GPIOs */
  92.     WaveRecorder_GPIO_Init();
  93.    
  94.     /* Configure the interrupts (for timer) */
  95.     WaveRecorder_NVIC_Init();
  96.    
  97.     /* Configure the SPI */
  98.     WaveRecorder_SPI_Init(AudioFreq);
  99.    
  100.     /* Set the local parameters */
  101.     AudioRecBitRes = BitRes;
  102.     AudioRecChnlNbr = ChnlNbr;
  103.    
  104.     /* Set state of the audio recorder to initialized */
  105.     AudioRecInited = 1;
  106.    
  107.     /* Return 0 if all operations are OK */
  108.     return 0;
  109.   }  
  110. }
  111.  
  112. uint8_t WaveRecorderStart(uint16_t* pbuf, uint32_t size)
  113. {
  114. /* Check if the interface has already been initialized */
  115.   if (AudioRecInited)
  116.   {
  117.     /* Store the location and size of the audio buffer */
  118.     pAudioRecBuf = pbuf;
  119.     AudioRecCurrSize = size;
  120.    
  121.     /* Enable the Rx buffer not empty interrupt */
  122.     SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
  123.     /* The Data transfer is performed in the SPI interrupt routine */
  124.     /* Enable the SPI peripheral */
  125.     I2S_Cmd(SPI2, ENABLE);
  126.    
  127.     /* Return 0 if all operations are OK */
  128.     return 0;
  129.   }
  130.   else
  131.   {
  132.     /* Cannot perform operation */
  133.     return 1;
  134.   }
  135. }
  136.  
  137. void AUDIO_REC_SPI_IRQHANDLER(void)
  138. {  
  139.    u16 volume;
  140.    u16 app;
  141.  
  142.   /* Check if data are available in SPI Data register */
  143.   if (SPI_GetITStatus(SPI2, SPI_I2S_IT_RXNE) != RESET)
  144.   {
  145.     app = SPI_I2S_ReceiveData(SPI2);
  146.     InternalBuffer[InternalBufferSize++] = HTONS(app);
  147.    
  148.     /* Check to prevent overflow condition */
  149.     if (InternalBufferSize >= INTERNAL_BUFF_SIZE)
  150.     {
  151.       InternalBufferSize = 0;
  152.       volume = 100;    
  153.       PDM_Filter_64_LSB((uint8_t *)InternalBuffer, (uint16_t *)pAudioRecBuf, volume , (PDMFilter_InitStruct *)&Filter);
  154.            
  155.             // Do something here, such as set a flag
  156.     }
  157.   }
  158. }
  159.  
  160. void WaveRecorder_GPIO_Init(void)
  161. {  
  162.   GPIO_InitTypeDef GPIO_InitStructure;
  163.  
  164.   /* Enable GPIO clocks */
  165.   RCC_AHB1PeriphClockCmd(SPI_SCK_GPIO_CLK | SPI_MOSI_GPIO_CLK, ENABLE);
  166.  
  167.  
  168.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  169.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  170.   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  171.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  172.  
  173.   /* SPI SCK pin configuration */
  174.   GPIO_InitStructure.GPIO_Pin = SPI_SCK_PIN;
  175.   GPIO_Init(SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
  176.  
  177.   /* Connect SPI pins to AF5 */  
  178.   GPIO_PinAFConfig(SPI_SCK_GPIO_PORT, SPI_SCK_SOURCE, SPI_SCK_AF);
  179.  
  180.   /* SPI MOSI pin configuration */
  181.   GPIO_InitStructure.GPIO_Pin =  SPI_MOSI_PIN;
  182.   GPIO_Init(SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
  183.   GPIO_PinAFConfig(SPI_MOSI_GPIO_PORT, SPI_MOSI_SOURCE, SPI_MOSI_AF);
  184. }
  185.  
  186. void WaveRecorder_SPI_Init(uint32_t Freq)
  187. {
  188.   I2S_InitTypeDef I2S_InitStructure;
  189.  
  190.   /* Enable the SPI clock */
  191.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
  192.  
  193.   /* SPI configuration */
  194.   SPI_I2S_DeInit(SPI2);
  195.   I2S_InitStructure.I2S_AudioFreq = Freq;
  196.   I2S_InitStructure.I2S_Standard = I2S_Standard_LSB;
  197.   I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
  198.   I2S_InitStructure.I2S_CPOL = I2S_CPOL_High;
  199.   I2S_InitStructure.I2S_Mode = I2S_Mode_MasterRx;
  200.   I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
  201.   /* Initialize the I2S peripheral with the structure above */
  202.   I2S_Init(SPI2, &I2S_InitStructure);
  203.  
  204.   /* Enable the Rx buffer not empty interrupt */
  205.   SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
  206. }
  207.  
  208. void WaveRecorder_NVIC_Init(void)
  209. {
  210.   NVIC_InitTypeDef NVIC_InitStructure;
  211.  
  212.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
  213.   /* Configure the SPI interrupt priority */
  214.   NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
  215.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  // lower number means more important
  216.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;   // lower number means more important
  217.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  218.   NVIC_Init(&NVIC_InitStructure);
  219. }
  220.  
  221. void gpio_init(void) {
  222.     /* STM32F4-Discovery LEDS on PD12, PD13, PD14, PD15 */
  223.     // See GPIO alternative function mapping at http://stm32f4-discovery.com/2014/09/library-33-pwm-stm32f4xx/
  224.     GPIO_InitTypeDef GPIO_InitStruct;
  225.    
  226.     /* Clock for GPIOD */
  227.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  228.  
  229.     /* Alternating functions for pins */
  230.     GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
  231.     GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
  232.     GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM4);
  233.     GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_TIM4);
  234.    
  235.     /* Set pins */
  236.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  237.     GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  238.     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  239.       GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  240.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
  241.     GPIO_Init(GPIOD, &GPIO_InitStruct);
  242. }
Advertisement
Add Comment
Please, Sign In to add comment