Advertisement
Nanne118

25-05-2019 DSP code that works??

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