Senior Design: Sound Monitor / OneHopeOnePrayer

Dependencies:   STM32L4xx_HAL_Driver CMSIS_DSP_401

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Main.c Source File

Main.c

00001 #include "main.h"
00002 
00003 int32_t delay=0;
00004 uint32_t AppStatus = 0x00;
00005 FlagStatus AppLaunched = RESET;
00006 uint8_t DemoIndex = 0;
00007 
00008 /* initialization variable */
00009 __IO FlagStatus KeyPressed = RESET;
00010 FlagStatus JoyInitialized = RESET;
00011 FlagStatus IddInitialized = RESET;
00012 FlagStatus LcdInitialized = RESET;
00013 FlagStatus LedInitialized = RESET;
00014 
00015 JOYState_TypeDef JoyState = JOY_NONE;
00016 
00017 __IO uint8_t NbLoop = 1;
00018 
00019 /* Volume of the audio playback */
00020 /* Initial volume level (from 50% to 100% (Max)) */
00021 __IO uint8_t Volume = 70;
00022 __IO uint8_t VolumeChange = 0;
00023 __IO uint8_t AudioPlaybackExit = 0;
00024 __IO uint8_t AudioRecordExit = 0;
00025 __IO uint8_t AudioRecordSampleRateSelected = 0;  
00026 __IO uint8_t SampleRateIndex = 0;
00027 __IO uint8_t AudioRecordSampleRateChange = 0;
00028 
00029 extern __IO uint8_t bLCDGlass_KeyPressed;
00030 extern FlagStatus IddItOccurred;
00031 extern uint8_t IddIndex;
00032 
00033 
00034 
00035 typedef enum
00036 {
00037   DEMO_STATE_LED            = 0x00,   /*!< Led application launched */
00038   DEMO_STATE_JOYSTICK,                /*!< Joystick application launched */
00039   DEMO_STATE_LCD ,                    /*!< LCD application launched */
00040   DEMO_STATE_AUDIO_RECORD,            /*!< Audio record application launched */
00041   DEMO_STATE_FIRST_STARTUP,           /*!< Demo first Startup */
00042   DEMO_STATE_IDLE,                    /*!< Demo in idle */
00043 } Demo_StateTypeDef;
00044 
00045 /* Private function prototypes -----------------------------------------------*/
00046 static void Display_DemoDescription(void);
00047 
00048 BSP_DemoTypedef  BSP_examples[]=
00049 {
00050   {AudioRecord_demo, "AUDIO RECORD", DEMO_STATE_AUDIO_RECORD}
00051 };
00052 
00053  int main(void)
00054 {
00055   uint8_t i = 0;
00056   uint8_t menudisplay[DEMO_NAME_CHAR_NB + 5] = {0};
00057   
00058   /* STM32L4xx HAL library initialization:
00059        - Configure the Flash prefetch
00060        - Systick timer is configured by default as source of time base, but user 
00061          can eventually implement his proper time base source (a general purpose 
00062          timer for example or other time source), keeping in mind that Time base 
00063          duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
00064          handled in milliseconds basis.
00065        - Set NVIC Group Priority to 4
00066        - Low Level Initialization
00067      */
00068   HAL_Init();
00069 
00070   /*##-1- Configure the system clock #########################################*/
00071   /*  */
00072   SystemClock_Config(); 
00073 
00074   /*##-2-Configure minimum hardware resources at boot ########################*/
00075   SystemHardwareInit();
00076   
00077     /* Display demo explaination on LCD Glass */
00078     AppStatus = DEMO_STATE_FIRST_STARTUP;
00079     Display_DemoDescription();
00080 
00081   /* Menu display first characters initialization */
00082   for(i = 0; i < 5; i++)
00083   {
00084     menudisplay[i] = ' ';
00085   }
00086 
00087 
00088   /* Wait For User inputs */
00089   while (1)
00090   {
00091      for(i = 0; i < DEMO_NAME_CHAR_NB; i++)
00092      {
00093        menudisplay[i+5] = BSP_examples[DemoIndex].DemoName[i];
00094      }
00095       
00096       BSP_LCD_GLASS_ScrollSentence(menudisplay, 1, SCROLL_SPEED_HIGH);
00097 
00098       if(AppLaunched != RESET)
00099       {
00100         AppLaunched = RESET;
00101         KeyPressed = RESET;
00102         
00103         /* Clear screen */
00104         BSP_LCD_GLASS_Clear();
00105 
00106         /* Set Application status in case of specific joystick usage */
00107         AppStatus = BSP_examples[DemoIndex].DemoIndex;
00108 
00109         /* launched application */
00110         BSP_examples[DemoIndex].DemoFunc();
00111         
00112         /* Set Application status to Idle */
00113         AppStatus = DEMO_STATE_IDLE;
00114         
00115         /* Notify end of test and clear screen */
00116         BSP_LCD_GLASS_ScrollSentence((uint8_t *)"      END OF TEST", 1, SCROLL_SPEED_HIGH);
00117         BSP_LCD_GLASS_Clear();
00118       }
00119   }
00120 }
00121 
00122 void SystemClock_Config(void)
00123 {
00124   /* oscillator and clocks configs */
00125   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
00126   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
00127   
00128   /* The voltage scaling allows optimizing the power consumption when the device is
00129      clocked below the maximum system frequency, to update the voltage scaling value
00130      regarding system frequency refer to product datasheet.  */
00131 
00132   /* Enable Power Control clock */
00133   __HAL_RCC_PWR_CLK_ENABLE();
00134 
00135   if(HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
00136   {
00137     /* Initialization Error */
00138     Error_Handler();
00139   }
00140 
00141   /* Disable Power Control clock */
00142   __HAL_RCC_PWR_CLK_DISABLE();
00143 
00144   /* 80 Mhz from MSI 8Mhz */
00145   /* MSI is enabled after System reset, activate PLL with MSI as source */
00146   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
00147   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
00148   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_7;
00149   RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
00150   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
00151   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
00152   RCC_OscInitStruct.PLL.PLLM = 1;
00153   RCC_OscInitStruct.PLL.PLLN = 20;
00154   RCC_OscInitStruct.PLL.PLLR = 2;
00155   RCC_OscInitStruct.PLL.PLLP = 7;
00156   RCC_OscInitStruct.PLL.PLLQ = 4;
00157 
00158   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
00159   {
00160     /* Initialization Error */
00161     Error_Handler();
00162   }
00163   
00164   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
00165      clocks dividers */
00166   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
00167   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
00168   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
00169   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
00170   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
00171   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
00172   {
00173     /* Initialization Error */
00174     Error_Handler();
00175   }
00176   
00177   /* The voltage scaling allows optimizing the power consumption when the device is
00178      clocked below the maximum system frequency, to update the voltage scaling value
00179      regarding system frequency refer to product datasheet.  */
00180 
00181   /* Enable Power Control clock */
00182   __HAL_RCC_PWR_CLK_ENABLE();
00183 
00184   if(HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
00185   {
00186     /* Initialization Error */
00187     Error_Handler();
00188   }
00189 
00190   /* Disable Power Control clock */
00191   __HAL_RCC_PWR_CLK_DISABLE();
00192 }
00193 
00194 void SystemLowClock_Config(void)
00195 {
00196   /* oscillator and clocks configs */
00197   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
00198   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
00199   uint32_t flatency = 0;
00200 
00201   /* Retrieve clock parameters */
00202   HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &flatency );
00203   
00204   /* switch SYSCLK to MSI in order to modify PLL divider */
00205   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
00206   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
00207   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, flatency) != HAL_OK)
00208   {
00209     /* Initialization Error */
00210     Error_Handler();
00211   }
00212   
00213   /* Retrieve oscillator parameters */
00214   HAL_RCC_GetOscConfig(&RCC_OscInitStruct);
00215   
00216   /* turn off PLL */
00217   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_NONE;
00218   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_OFF;
00219   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
00220   {
00221     /* Initialization Error */
00222     Error_Handler();
00223   }
00224 }
00225 
00226 /**
00227   * @brief  System Power Configuration at Boot
00228   * @param  None
00229   * @retval None
00230   */
00231 void SystemHardwareInit(void)
00232 {
00233   /* Init LED4 and LED5  */
00234   if(LedInitialized != SET)
00235   {
00236     BSP_LED_Init(LED_RED);
00237     BSP_LED_Init(LED_GREEN);
00238     LedInitialized = SET;
00239   }
00240   
00241   /* Init STM32L476G-Discovery joystick in interrupt mode */
00242   if(JoyInitialized != SET)
00243   {
00244     BSP_JOY_Init(JOY_MODE_EXTI);
00245     JoyInitialized = SET;
00246   }
00247   
00248   /* Initialize the LCD */
00249   if(LcdInitialized != SET)
00250   {
00251     BSP_LCD_GLASS_Init();
00252     LcdInitialized = SET;
00253   }
00254   __HAL_RCC_I2C2_CLK_ENABLE();
00255 }
00256 
00257 /**
00258   * @brief  System Low Power Configuration
00259   * @param  None
00260   * @retval None
00261   */
00262 void SystemHardwareDeInit(void)
00263 {
00264   if(LedInitialized != RESET)
00265   {
00266     BSP_LED_DeInit(LED_RED);
00267     BSP_LED_DeInit(LED_GREEN);
00268     LedInitialized = RESET;
00269   }
00270 
00271   if(JoyInitialized != RESET)
00272   {
00273     BSP_JOY_DeInit();
00274     JoyInitialized = RESET;
00275   }
00276 
00277   if(LcdInitialized != RESET)
00278   {
00279     BSP_LCD_GLASS_DeInit();
00280     LcdInitialized = RESET;
00281   }
00282 
00283   /* Disable remaining clocks */
00284   __HAL_RCC_PWR_CLK_DISABLE();
00285   __HAL_RCC_FLASH_CLK_DISABLE();
00286   __HAL_RCC_SYSCFG_CLK_DISABLE();
00287   __HAL_RCC_GPIOA_CLK_DISABLE();
00288   __HAL_RCC_GPIOB_CLK_DISABLE();
00289   __HAL_RCC_GPIOC_CLK_DISABLE();
00290   __HAL_RCC_GPIOD_CLK_DISABLE();
00291   __HAL_RCC_GPIOE_CLK_DISABLE();
00292   __HAL_RCC_I2C2_CLK_DISABLE();
00293   RCC->AHB1SMENR = 0x0;
00294   RCC->AHB2SMENR = 0x0;
00295   RCC->AHB3SMENR = 0x0;
00296   RCC->APB1SMENR1 = 0x0;
00297   RCC->APB1SMENR2 = 0x0;
00298   RCC->APB2SMENR = 0x0;
00299   }
00300 
00301 static void Display_DemoDescription(void)
00302 {
00303   /* Clear the LCD */ 
00304   BSP_LCD_GLASS_Clear();
00305 
00306   /* Display LCD messages */
00307   BSP_LCD_GLASS_ScrollSentence((uint8_t *)"     STM32L476G-DISCO BSP DRIVER EXAMPLE", 1, SCROLL_SPEED_HIGH);
00308   BSP_LCD_GLASS_ScrollSentence((uint8_t *)"     COPYRIGHT STMICROELECTRONICS 2015", 1, SCROLL_SPEED_HIGH);
00309 
00310   /* Introduce short delay before new message displayed */
00311   HAL_Delay(50);
00312 
00313   BSP_LCD_GLASS_ScrollSentence((uint8_t *)"      Press Joy Right to start ", 0xFFFF, SCROLL_SPEED_HIGH);
00314 
00315   /* Clear the LCD & reset Key pressed variable */
00316   BSP_LCD_GLASS_Clear();
00317 
00318   AppStatus = DEMO_STATE_IDLE;
00319 }
00320 
00321 uint8_t CheckForUserInput(void)
00322 {
00323   if(BSP_JOY_GetState() == JOY_SEL)
00324   {
00325     while (BSP_JOY_GetState() == JOY_SEL);
00326     return 1;
00327   }
00328   return 0;
00329 }
00330 
00331 /**lerais 
00332   * @brief  This function is executed in case of error occurrence.
00333   * @param  None
00334   * @retval None
00335   */
00336 void Error_Handler(void)
00337 {
00338   /* Turn LED RED on */
00339   while(1)
00340   {
00341     BSP_LED_Toggle(LED_RED);
00342     HAL_Delay(40);
00343   }
00344 }
00345 
00346 
00347 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
00348 {
00349   if((GPIO_Pin & (DOWN_JOY_PIN | UP_JOY_PIN | SEL_JOY_PIN | RIGHT_JOY_PIN | LEFT_JOY_PIN))
00350       != RESET)
00351   {
00352     KeyPressed = SET;
00353     
00354     switch(AppStatus)
00355     {
00356       case DEMO_STATE_FIRST_STARTUP:
00357       
00358       
00359       case DEMO_STATE_IDLE:
00360         bLCDGlass_KeyPressed = 0x01;
00361         switch(GPIO_Pin)
00362         {
00363           case DOWN_JOY_PIN :
00364             DemoIndex++;
00365             if(DemoIndex > COUNT_OF_EXAMPLE(BSP_examples))
00366             {
00367              DemoIndex = 0;
00368             }
00369             break;
00370           case UP_JOY_PIN :
00371             DemoIndex--;
00372             if(DemoIndex > COUNT_OF_EXAMPLE(BSP_examples))
00373             {
00374              DemoIndex = (COUNT_OF_EXAMPLE(BSP_examples) - 1);
00375             }
00376             break;
00377           case RIGHT_JOY_PIN :
00378             AppLaunched = SET;
00379             break;
00380         }
00381         break;
00382     
00383       case DEMO_STATE_LED:
00384         break;
00385       
00386       case DEMO_STATE_JOYSTICK:
00387       case DEMO_STATE_LCD:
00388         switch(GPIO_Pin)
00389         {
00390           case DOWN_JOY_PIN :
00391             JoyState = JOY_DOWN;
00392             break;
00393           case UP_JOY_PIN :
00394             JoyState = JOY_UP;
00395             break;
00396           case SEL_JOY_PIN :
00397             JoyState = JOY_SEL;
00398             break;
00399           case RIGHT_JOY_PIN :
00400             JoyState = JOY_RIGHT;
00401             break;
00402           case LEFT_JOY_PIN :
00403             JoyState = JOY_LEFT;
00404             break;
00405         }
00406         break;
00407         
00408       
00409     case DEMO_STATE_AUDIO_RECORD:
00410         switch(GPIO_Pin)
00411         {
00412           case RIGHT_JOY_PIN :
00413             /* SEL is used to confirm audio record sample rate and start audio recording */
00414             AudioRecordSampleRateSelected = 1;
00415             break;
00416         }
00417       break;
00418     }
00419   }
00420 }