Advertisement
Guest User

Untitled

a guest
Jun 7th, 2021
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.24 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22.  
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25.  
  26. /* USER CODE END Includes */
  27.  
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30.  
  31. /* USER CODE END PTD */
  32.  
  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  35. /* USER CODE END PD */
  36.  
  37. /* Private macro -------------------------------------------------------------*/
  38. /* USER CODE BEGIN PM */
  39.  
  40. /* USER CODE END PM */
  41.  
  42. /* Private variables ---------------------------------------------------------*/
  43.  
  44. I2S_HandleTypeDef hi2s1;
  45. DMA_HandleTypeDef hdma_spi1_tx;
  46. DMA_HandleTypeDef hdma_spi1_rx;
  47.  
  48. /* USER CODE BEGIN PV */
  49.  
  50. /* USER CODE END PV */
  51.  
  52. /* Private function prototypes -----------------------------------------------*/
  53. void SystemClock_Config(void);
  54. static void MX_GPIO_Init(void);
  55. static void MX_DMA_Init(void);
  56. static void MX_I2S1_Init(void);
  57. /* USER CODE BEGIN PFP */
  58.  
  59. /* USER CODE END PFP */
  60.  
  61. /* Private user code ---------------------------------------------------------*/
  62. /* USER CODE BEGIN 0 */
  63.  
  64. /* USER CODE END 0 */
  65.  
  66. /**
  67.   * @brief  The application entry point.
  68.   * @retval int
  69.   */
  70. uint16_t rxBuf[8];
  71. uint16_t txBuf[8];
  72. float l_a0, l_a1, l_a2, l_b1, l_b2, lin_z1, lin_z2, lout_z1, lout_z2;
  73. float r_a0, r_a1, r_a2, r_b1, r_b2, rin_z1, rin_z2, rout_z1, rout_z2;
  74. int main(void)
  75. {
  76.   /* USER CODE BEGIN 1 */
  77.  
  78.   /* USER CODE END 1 */
  79.  
  80.   /* MCU Configuration--------------------------------------------------------*/
  81.  
  82.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  83.   HAL_Init();
  84.  
  85.   /* USER CODE BEGIN Init */
  86.  
  87.   /* USER CODE END Init */
  88.  
  89.   /* Configure the system clock */
  90.   SystemClock_Config();
  91.  
  92.   /* USER CODE BEGIN SysInit */
  93.  
  94.   /* USER CODE END SysInit */
  95.  
  96.   /* Initialize all configured peripherals */
  97.   MX_GPIO_Init();
  98.   MX_DMA_Init();
  99.   MX_I2S1_Init();
  100.   /* USER CODE BEGIN 2 */
  101.  
  102.   /* USER CODE END 2 */
  103.  
  104.   /* Infinite loop */
  105.   /* USER CODE BEGIN WHILE */
  106.   HAL_I2SEx_TransmitReceive_DMA (&hi2s1, txBuf, rxBuf, 4);
  107.  
  108.   //left-channel, High-Pass, 1kHz, fs=96kHz, q=0.7
  109.    l_a0 = 0.9543457485325094f;
  110.    l_a1 = -1.9086914970650188f;
  111.    l_a2 = 0.9543457485325094f;
  112.    l_b1 = -1.9066459797557103f;
  113.    l_b2 = 0.9107370143743273f;
  114.  
  115.    //right-channel, Low-Pass, 1kHz, fs)96 kHz, q=0.7
  116.    r_a0 = 0.0010227586546542474f;
  117.    r_a1 = 0.002045517309308495f;
  118.    r_a2 = 0.0010227586546542474f;
  119.    r_b1 = -1.9066459797557103f;
  120.    r_b2 = 0.9107370143743273f;
  121.  
  122.   while (1)
  123.   {
  124.     /* USER CODE END WHILE */
  125.  
  126.     /* USER CODE BEGIN 3 */
  127.   }
  128.   /* USER CODE END 3 */
  129. }
  130.  
  131. int Calc_IIR_Left (int inSample) {
  132.     float inSampleF = (float)inSample;
  133.     float outSampleF =
  134.             l_a0 * inSampleF
  135.             + l_a1 * lin_z1
  136.             + l_a2 * lin_z2
  137.             - l_b1 * lout_z1
  138.             - l_b2 * lout_z2;
  139.     lin_z2 = lin_z1;
  140.     lin_z1 = inSampleF;
  141.     lout_z2 = lout_z1;
  142.     lout_z1 = outSampleF;
  143.  
  144.     return (int) outSampleF;
  145. }
  146.  
  147. int Calc_IIR_Right (int inSample) {
  148.     float inSampleF = (float)inSample;
  149.     float outSampleF =
  150.             r_a0 * inSampleF
  151.             + r_a1 * rin_z1
  152.             + r_a2 * rin_z2
  153.             - r_b1 * rout_z1
  154.             - r_b2 * rout_z2;
  155.     rin_z2 = rin_z1;
  156.     rin_z1 = inSampleF;
  157.     rout_z2 = rout_z1;
  158.     rout_z1 = outSampleF;
  159.  
  160.     return (int) outSampleF;
  161. }
  162.  
  163. void HAL_I2SEx_TxRxHalfCpltCallback(I2S_HandleTypeDef *hi2s){
  164.  
  165.     //restore signed 24 bit sample from 16-bit buffers
  166.     int lSample = (int) (rxBuf[0]<<16)|rxBuf[1];
  167.     int rSample = (int) (rxBuf[2]<<16)|rxBuf[3];
  168.  
  169.     // divide by 2 (rightshift) -> -3dB per sample
  170.     lSample = lSample>>1;
  171.     rSample = rSample>>1;
  172.  
  173.     //sum to mono
  174.     lSample = rSample + lSample;
  175.     rSample = lSample;
  176.  
  177.     //run HP on left channel and LP on right channel
  178.     lSample = Calc_IIR_Left(lSample);
  179.     rSample = Calc_IIR_Right(rSample);
  180.  
  181.     //restore to buffer
  182.     txBuf[0] = (lSample>>16)&0xFFFF;
  183.     txBuf[1] = lSample&0xFFFF;
  184.     txBuf[2] = (rSample>>16)&0xFFFF;
  185.     txBuf[3] = rSample&0xFFFF;
  186. }
  187.  
  188. void HAL_I2SEx_TxRxCpltCallback(I2S_HandleTypeDef *hi2s){
  189.  
  190.     //restore signed 24 bit sample from 16-bit buffers
  191.     int lSample = (int) (rxBuf[4]<<16)|rxBuf[5];
  192.     int rSample = (int) (rxBuf[6]<<16)|rxBuf[7];
  193.  
  194.     // divide by 2 (rightshift) -> -3dB per sample
  195.     lSample = lSample>>1;
  196.     rSample = rSample>>1;
  197.  
  198.     //sum to mono
  199.     lSample = rSample + lSample;
  200.     rSample = lSample;
  201.  
  202.     //run HP on left channel and LP on right channel
  203.     lSample = Calc_IIR_Left(lSample);
  204.     rSample = Calc_IIR_Right(rSample);
  205.  
  206.     //restore to buffer
  207.     txBuf[4] = (lSample>>16)&0xFFFF;
  208.     txBuf[5] = lSample&0xFFFF;
  209.     txBuf[6] = (rSample>>16)&0xFFFF;
  210.     txBuf[7] = rSample&0xFFFF;
  211. }
  212.  
  213. /**
  214.   * @brief System Clock Configuration
  215.   * @retval None
  216.   */
  217. void SystemClock_Config(void)
  218. {
  219.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  220.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  221.  
  222.   /** Supply configuration update enable
  223.   */
  224.   HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
  225.   /** Configure the main internal regulator output voltage
  226.   */
  227.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
  228.  
  229.   while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  230.   /** Macro to configure the PLL clock source
  231.   */
  232.   __HAL_RCC_PLL_PLLSOURCE_CONFIG(RCC_PLLSOURCE_HSI);
  233.   /** Initializes the RCC Oscillators according to the specified parameters
  234.   * in the RCC_OscInitTypeDef structure.
  235.   */
  236.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  237.   RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  238.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  239.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  240.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  241.   RCC_OscInitStruct.PLL.PLLM = 4;
  242.   RCC_OscInitStruct.PLL.PLLN = 60;
  243.   RCC_OscInitStruct.PLL.PLLP = 2;
  244.   RCC_OscInitStruct.PLL.PLLQ = 2;
  245.   RCC_OscInitStruct.PLL.PLLR = 2;
  246.   RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
  247.   RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  248.   RCC_OscInitStruct.PLL.PLLFRACN = 0;
  249.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  250.   {
  251.     Error_Handler();
  252.   }
  253.   /** Initializes the CPU, AHB and APB buses clocks
  254.   */
  255.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  256.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  257.                               |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  258.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  259.   RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  260.   RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  261.   RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  262.   RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  263.   RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  264.   RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  265.  
  266.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  267.   {
  268.     Error_Handler();
  269.   }
  270. }
  271.  
  272. /**
  273.   * @brief I2S1 Initialization Function
  274.   * @param None
  275.   * @retval None
  276.   */
  277. static void MX_I2S1_Init(void)
  278. {
  279.  
  280.   /* USER CODE BEGIN I2S1_Init 0 */
  281.  
  282.   /* USER CODE END I2S1_Init 0 */
  283.  
  284.   /* USER CODE BEGIN I2S1_Init 1 */
  285.  
  286.   /* USER CODE END I2S1_Init 1 */
  287.   hi2s1.Instance = SPI1;
  288.   hi2s1.Init.Mode = I2S_MODE_MASTER_FULLDUPLEX;
  289.   hi2s1.Init.Standard = I2S_STANDARD_PHILIPS;
  290.   hi2s1.Init.DataFormat = I2S_DATAFORMAT_24B;
  291.   hi2s1.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
  292.   hi2s1.Init.AudioFreq = I2S_AUDIOFREQ_96K;
  293.   hi2s1.Init.CPOL = I2S_CPOL_LOW;
  294.   hi2s1.Init.FirstBit = I2S_FIRSTBIT_MSB;
  295.   hi2s1.Init.WSInversion = I2S_WS_INVERSION_DISABLE;
  296.   hi2s1.Init.Data24BitAlignment = I2S_DATA_24BIT_ALIGNMENT_LEFT;
  297.   hi2s1.Init.MasterKeepIOState = I2S_MASTER_KEEP_IO_STATE_DISABLE;
  298.   if (HAL_I2S_Init(&hi2s1) != HAL_OK)
  299.   {
  300.     Error_Handler();
  301.   }
  302.   /* USER CODE BEGIN I2S1_Init 2 */
  303.  
  304.   /* USER CODE END I2S1_Init 2 */
  305.  
  306. }
  307.  
  308. /**
  309.   * Enable DMA controller clock
  310.   */
  311. static void MX_DMA_Init(void)
  312. {
  313.  
  314.   /* DMA controller clock enable */
  315.   __HAL_RCC_DMA1_CLK_ENABLE();
  316.  
  317.   /* DMA interrupt init */
  318.   /* DMA1_Stream0_IRQn interrupt configuration */
  319.   HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0);
  320.   HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
  321.   /* DMA1_Stream1_IRQn interrupt configuration */
  322.   HAL_NVIC_SetPriority(DMA1_Stream1_IRQn, 0, 0);
  323.   HAL_NVIC_EnableIRQ(DMA1_Stream1_IRQn);
  324.  
  325. }
  326.  
  327. /**
  328.   * @brief GPIO Initialization Function
  329.   * @param None
  330.   * @retval None
  331.   */
  332. static void MX_GPIO_Init(void)
  333. {
  334.  
  335.   /* GPIO Ports Clock Enable */
  336.   __HAL_RCC_GPIOA_CLK_ENABLE();
  337.   __HAL_RCC_GPIOC_CLK_ENABLE();
  338.  
  339. }
  340.  
  341. /* USER CODE BEGIN 4 */
  342.  
  343. /* USER CODE END 4 */
  344.  
  345. /**
  346.   * @brief  This function is executed in case of error occurrence.
  347.   * @retval None
  348.   */
  349. void Error_Handler(void)
  350. {
  351.   /* USER CODE BEGIN Error_Handler_Debug */
  352.   /* User can add his own implementation to report the HAL error return state */
  353.   __disable_irq();
  354.   while (1)
  355.   {
  356.   }
  357.   /* USER CODE END Error_Handler_Debug */
  358. }
  359.  
  360. #ifdef  USE_FULL_ASSERT
  361. /**
  362.   * @brief  Reports the name of the source file and the source line number
  363.   *         where the assert_param error has occurred.
  364.   * @param  file: pointer to the source file name
  365.   * @param  line: assert_param error line source number
  366.   * @retval None
  367.   */
  368. void assert_failed(uint8_t *file, uint32_t line)
  369. {
  370.   /* USER CODE BEGIN 6 */
  371.   /* User can add his own implementation to report the file name and line number,
  372.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  373.   /* USER CODE END 6 */
  374. }
  375. #endif /* USE_FULL_ASSERT */
  376.  
  377. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  378.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement