Advertisement
Nanne118

Example DSP file

May 23rd, 2019
3,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.67 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c from ExampleDSP
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2019 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.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm32f4xx.h"
  23. #include "arm_const_structs.h"
  24. #include "core_cm4.h"
  25. #include "math.h"
  26. #include "arm_math.h"
  27. #include <stdio.h>
  28. #include <stdbool.h>
  29. #include "main.h"
  30.  
  31. #define ARM_MATH_CM4
  32.  
  33. /* Private includes ----------------------------------------------------------*/
  34. /* USER CODE BEGIN Includes */
  35.  
  36. /* USER CODE END Includes */
  37.  
  38. /* Private typedef -----------------------------------------------------------*/
  39. /* USER CODE BEGIN PTD */
  40.  
  41. /* USER CODE END PTD */
  42.  
  43. /* Private define ------------------------------------------------------------*/
  44. /* USER CODE BEGIN PD */
  45.  
  46. /* USER CODE END PD */
  47.  
  48. /* Private macro -------------------------------------------------------------*/
  49. /* USER CODE BEGIN PM */
  50.  
  51. /* USER CODE END PM */
  52.  
  53. /* Private variables ---------------------------------------------------------*/
  54. ADC_HandleTypeDef hadc1;
  55. DMA_HandleTypeDef hdma_adc1;
  56. UART_HandleTypeDef huart2;
  57.  
  58. /* USER CODE BEGIN PV */
  59.  
  60. /* USER CODE END PV */
  61.  
  62. /* Private function prototypes -----------------------------------------------*/
  63. void SystemClock_Config(void);
  64. static void MX_GPIO_Init(void);
  65. static void MX_DMA_Init(void);
  66. static void MX_ADC1_Init(void);
  67. static void MX_USART2_UART_Init(void);
  68. /* USER CODE BEGIN PFP */
  69.  
  70. /* USER CODE END PFP */
  71.  
  72. /* Private user code ---------------------------------------------------------*/
  73. /* USER CODE BEGIN 0 */
  74.  
  75.  
  76. /* USER CODE END 0 */
  77.  
  78. /**
  79.   * @brief  The application entry point.
  80.   * @retval int
  81.   */
  82.  
  83. arm_rfft_fast_instance_f32 inst;
  84.  
  85. int main(void)
  86. {
  87.   /* USER CODE BEGIN 1 */
  88.  
  89.     int16_t fftLen = 4096; // Supported FFT Lengths are 32, 64, 128, 256, 512, 1024, 2048, 4096.
  90.     int16_t halfsamples = fftLen/2;
  91.     //int16_t buffer_length = 3 * fftLen;
  92.  
  93.     int32_t timeLeng[fftLen];
  94.  
  95.   /* USER CODE END 1 */
  96.  
  97.   /* MCU Configuration--------------------------------------------------------*/
  98.  
  99.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  100.   HAL_Init();
  101.  
  102.   /* USER CODE BEGIN Init */
  103.  
  104.   /* USER CODE END Init */
  105.  
  106.   /* Configure the system clock */
  107.   SystemClock_Config();
  108.  
  109.   /* USER CODE BEGIN SysInit */
  110.  
  111.   /* USER CODE END SysInit */
  112.  
  113.   /* Initialize all configured peripherals */
  114.   //   MX_GPIO_Init();
  115.    MX_DMA_Init();
  116.    MX_ADC1_Init();
  117.    MX_USART2_UART_Init();
  118.   /* USER CODE BEGIN 2 */
  119.  
  120.  
  121.   /* USER CODE END 2 */
  122.  
  123.   /* Infinite loop */
  124.   /* USER CODE BEGIN WHILE */
  125.   while (1)
  126.   {
  127.     /* USER CODE END WHILE */
  128.  
  129.       float32_t sinA[fftLen], sinB[fftLen], sinC[fftLen], signalCombined[fftLen];
  130.       float32_t  fftCombined[fftLen], fft_magnitude[fftLen];
  131.       float32_t maxAmp;
  132.       uint32_t maxAmpInd;
  133.  
  134.       for (int i = 0; i < fftLen; i++)
  135.      {
  136.           sinA[i] = 5 * arm_sin_f32(4*i);
  137.           sinB[i] = 7 * arm_sin_f32(7*i);
  138.           sinC[i] = 3 * arm_sin_f32(9*i);
  139.           signalCombined[i] = sinA[i] + sinB[i] + sinC[i];
  140.      }
  141.  
  142.      arm_status status;
  143. //   status = arm_rfft_fast_init_f32(&inst, fftLen);
  144.      arm_rfft_fast_f32(&inst, signalCombined, fftCombined, 0);
  145.      arm_cmplx_mag_squared_f32(fftCombined, fft_magnitude, halfsamples);
  146.      arm_max_f32(fft_magnitude, halfsamples, &maxAmp, &maxAmpInd);
  147.  
  148.  
  149.  
  150.      if (status == ARM_MATH_SUCCESS)
  151.      {
  152.          //printf statements
  153.          //maxAmp: %f, maxAmp
  154.          //maxAmpInd: %i, maxAmpInd
  155.      }
  156.      else
  157.      {
  158.  
  159.      }
  160.  
  161.     /* USER CODE BEGIN 3 */
  162.   }
  163.   /* USER CODE END 3 */
  164. }
  165.  
  166. /**
  167.   * @brief System Clock Configuration
  168.   * @retval None
  169.   */
  170. void SystemClock_Config(void)
  171. {
  172.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  173.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  174.  
  175.   /** Configure the main internal regulator output voltage
  176.   */
  177.   __HAL_RCC_PWR_CLK_ENABLE();
  178.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  179.   /** Initializes the CPU, AHB and APB busses clocks
  180.   */
  181.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  182.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  183.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  184.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  185.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  186.   RCC_OscInitStruct.PLL.PLLM = 8;
  187.   RCC_OscInitStruct.PLL.PLLN = 100;
  188.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  189.   RCC_OscInitStruct.PLL.PLLQ = 4;
  190.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  191.   {
  192.     Error_Handler();
  193.   }
  194.   /** Initializes the CPU, AHB and APB busses clocks
  195.   */
  196.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  197.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  198.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  199.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  200.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  201.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  202.  
  203.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  204.   {
  205.     Error_Handler();
  206.   }
  207. }
  208.  
  209. /**
  210.   * @brief ADC1 Initialization Function
  211.   * @param None
  212.   * @retval None
  213.   */
  214. static void MX_ADC1_Init(void)
  215. {
  216.  
  217.   /* USER CODE BEGIN ADC1_Init 0 */
  218.  
  219.   /* USER CODE END ADC1_Init 0 */
  220.  
  221.   ADC_ChannelConfTypeDef sConfig = {0};
  222.  
  223.   /* USER CODE BEGIN ADC1_Init 1 */
  224.  
  225.   /* USER CODE END ADC1_Init 1 */
  226.   /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  227.   */
  228.   hadc1.Instance = ADC1;
  229.   hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  230.   hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  231.   hadc1.Init.ScanConvMode = ENABLE;
  232.   hadc1.Init.ContinuousConvMode = ENABLE;
  233.   hadc1.Init.DiscontinuousConvMode = DISABLE;
  234.   hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  235.   hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  236.   hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  237.   hadc1.Init.NbrOfConversion = 3;
  238.   hadc1.Init.DMAContinuousRequests = DISABLE;
  239.   hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  240.   if (HAL_ADC_Init(&hadc1) != HAL_OK)
  241.   {
  242.     Error_Handler();
  243.   }
  244.   /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  245.   */
  246.   sConfig.Channel = ADC_CHANNEL_0;
  247.   sConfig.Rank = 1;
  248.   sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  249.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  250.   {
  251.     Error_Handler();
  252.   }
  253.   /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  254.   */
  255.   sConfig.Channel = ADC_CHANNEL_1;
  256.   sConfig.Rank = 2;
  257.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  258.   {
  259.     Error_Handler();
  260.   }
  261.   /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  262.   */
  263.   sConfig.Channel = ADC_CHANNEL_4;
  264.   sConfig.Rank = 3;
  265.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  266.   {
  267.     Error_Handler();
  268.   }
  269.   /* USER CODE BEGIN ADC1_Init 2 */
  270.  
  271.   /* USER CODE END ADC1_Init 2 */
  272.  
  273. }
  274.  
  275. /**
  276.   * @brief USART2 Initialization Function
  277.   * @param None
  278.   * @retval None
  279.   */
  280. static void MX_USART2_UART_Init(void)
  281. {
  282.  
  283.   /* USER CODE BEGIN USART2_Init 0 */
  284.  
  285.   /* USER CODE END USART2_Init 0 */
  286.  
  287.   /* USER CODE BEGIN USART2_Init 1 */
  288.  
  289.   /* USER CODE END USART2_Init 1 */
  290.   huart2.Instance = USART2;
  291.   huart2.Init.BaudRate = 115200;
  292.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  293.   huart2.Init.StopBits = UART_STOPBITS_1;
  294.   huart2.Init.Parity = UART_PARITY_NONE;
  295.   huart2.Init.Mode = UART_MODE_TX_RX;
  296.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  297.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  298.   if (HAL_UART_Init(&huart2) != HAL_OK)
  299.   {
  300.     Error_Handler();
  301.   }
  302.   /* USER CODE BEGIN USART2_Init 2 */
  303.  
  304.   /* USER CODE END USART2_Init 2 */
  305.  
  306. }
  307.  
  308. /**
  309.   * Enable DMA controller clock
  310.   */
  311. static void MX_DMA_Init(void)
  312. {
  313.   /* DMA controller clock enable */
  314.   __HAL_RCC_DMA2_CLK_ENABLE();
  315.  
  316.   /* DMA interrupt init */
  317.   /* DMA2_Stream0_IRQn interrupt configuration */
  318.   HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0);
  319.   HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
  320.  
  321. }
  322.  
  323. /* USER CODE BEGIN 4 */
  324.  
  325. /* USER CODE END 4 */
  326.  
  327. /**
  328.   * @brief  This function is executed in case of error occurrence.
  329.   * @retval None
  330.   */
  331. void Error_Handler(void)
  332. {
  333.   /* USER CODE BEGIN Error_Handler_Debug */
  334.   /* User can add his own implementation to report the HAL error return state */
  335.  
  336.   /* USER CODE END Error_Handler_Debug */
  337. }
  338.  
  339. #ifdef  USE_FULL_ASSERT
  340. /**
  341.   * @brief  Reports the name of the source file and the source line number
  342.   *         where the assert_param error has occurred.
  343.   * @param  file: pointer to the source file name
  344.   * @param  line: assert_param error line source number
  345.   * @retval None
  346.   */
  347. void assert_failed(uint8_t *file, uint32_t line)
  348. {
  349.   /* USER CODE BEGIN 6 */
  350.   /* User can add his own implementation to report the file name and line number,
  351.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  352.   /* USER CODE END 6 */
  353. }
  354. #endif /* USE_FULL_ASSERT */
  355.  
  356. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement