Advertisement
Guest User

STM32 read GPIO with DMA

a guest
Mar 15th, 2023
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.87 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.   * Copyright (c) 2023 STMicroelectronics.
  10.   * All rights reserved.
  11.   *
  12.   * This software is licensed under terms that can be found in the LICENSE file
  13.   * in the root directory of this software component.
  14.   * If no LICENSE file comes with this software, it is provided AS-IS.
  15.   *
  16.   ******************************************************************************
  17.   */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21.  
  22. #define BUFFER_SIZE 1000 // Adjust buffer size as needed
  23.  
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include "usart.h"
  27. /* USER CODE END Includes */
  28.  
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31. #ifdef __GNUC__
  32. /* With GCC, small printf (option LD Linker->Libraries->Small printf
  33.    set to 'Yes') calls __io_putchar() */
  34. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  35. #else
  36. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  37. #endif /* __GNUC__ */
  38. /* USER CODE END PTD */
  39.  
  40. /* Private define ------------------------------------------------------------*/
  41. /* USER CODE BEGIN PD */
  42. /* USER CODE END PD */
  43.  
  44. /* Private macro -------------------------------------------------------------*/
  45. /* USER CODE BEGIN PM */
  46.  
  47. /* USER CODE END PM */
  48.  
  49. /* Private variables ---------------------------------------------------------*/
  50.  
  51. /* TIM handle declaration */
  52. DMA_HandleTypeDef hdma2;
  53. TIM_HandleTypeDef htim1;                        // DMA handle
  54. uint32_t buffer[BUFFER_SIZE];                   // Buffer to store data
  55.  
  56. /* USER CODE BEGIN PV */
  57. /* Prescaler declaration */
  58. uint32_t uwPrescalerValue = 0;
  59. /* USER CODE END PV */
  60.  
  61. /* Private function prototypes -----------------------------------------------*/
  62. void SystemClock_Config(void);
  63. static void MX_GPIO_Init(void);
  64. static void MX_DMA_Init(void);
  65. static void MX_TIM1_Init(void);
  66. /* USER CODE BEGIN PFP */
  67.  
  68. /* USER CODE END PFP */
  69.  
  70. /* Private user code ---------------------------------------------------------*/
  71. /* USER CODE BEGIN 0 */
  72. /* USER CODE END 0 */
  73.  
  74. /**
  75.   * @brief  The application entry point.
  76.   * @retval int
  77.   */
  78. int main(void)
  79. {
  80.   /* USER CODE BEGIN 1 */
  81.  
  82.   /* USER CODE END 1 */
  83.  
  84.   /* MCU Configuration--------------------------------------------------------*/
  85.  
  86.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  87.   HAL_Init();
  88.  
  89.   /* USER CODE BEGIN Init */
  90.  
  91.   /* USER CODE END Init */
  92.  
  93.   /* Configure the system clock */
  94.   SystemClock_Config();
  95.  
  96.   /* USER CODE BEGIN SysInit */
  97.  
  98.   /* USER CODE END SysInit */
  99.  
  100.   /* Initialize all configured peripherals */
  101.  
  102.   /* USER CODE BEGIN 2 */
  103.   BSP_LED_Init(LED_RED);
  104.   BSP_LED_Init(LED_GREEN);
  105.   MX_USART3_UART_Init();
  106.  
  107.   /* Initialize all configured peripherals */
  108.   MX_GPIO_Init();
  109.   MX_DMA_Init();
  110.   MX_TIM1_Init();
  111.  
  112.  
  113.   /* Start the timer */
  114.   HAL_TIM_Base_Start(&htim1);
  115.  
  116.   /* USER CODE END 2 */
  117.  
  118.   /* Infinite loop */
  119.   /* USER CODE BEGIN WHILE */
  120.   while (1)
  121.   {
  122.         //volatile int timerValue = TIM1->CNT;
  123.         //printf("Timer 1 value: %d\n", timerValue);
  124.  
  125.           uint32_t data = 0;
  126.           HAL_DMA_Start(&hdma2, (uint32_t)&GPIOC->IDR, (uint32_t)&data, 1);
  127.           HAL_DMA_PollForTransfer(&hdma2, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY);
  128.           if (data & GPIO_PIN_13) {
  129.               // GPIOC13 is high
  130.             BSP_LED_On(LED_GREEN);
  131.           } else {
  132.             // GPIOC13 is low
  133.               BSP_LED_Off(LED_GREEN);
  134.           }
  135.  
  136.         HAL_Delay(100);
  137.  
  138.     /* USER CODE END WHILE */
  139.  
  140.     /* USER CODE BEGIN 3 */
  141.   }
  142.   /* USER CODE END 3 */
  143. }
  144.  
  145. /* TIM1 init function */
  146. void MX_TIM1_Init(void)
  147. {
  148.  
  149.   htim1.Instance = TIM1;
  150.   htim1.Init.Prescaler = (HAL_RCC_GetHCLKFreq() / 1000000) - 1; // 1MHz clock
  151.   htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  152.   htim1.Init.Period = 0xFFFFFFFF;
  153.   htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  154.   htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  155.   HAL_TIM_Base_Init(&htim1);
  156.  
  157.   TIM_MasterConfigTypeDef sMasterConfig;
  158.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  159.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  160.   HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
  161.  
  162. }
  163.  
  164. /* DMA init function */
  165. void MX_DMA_Init(void)
  166. {
  167.   /* DMA controller clock enable */
  168.   __HAL_RCC_DMA2_CLK_ENABLE();
  169.  
  170.   /* Configure DMA request hdma_memtomem_dma1_channel1 on DMA1_Channel1 */
  171.   hdma2.Instance = DMA2_Stream5;
  172.   hdma2.Init.Request = DMA_REQUEST_MEM2MEM;
  173.   hdma2.Init.Direction = DMA_PERIPH_TO_MEMORY;
  174.   hdma2.Init.PeriphInc = DMA_PINC_DISABLE;
  175.   hdma2.Init.MemInc = DMA_MINC_ENABLE;
  176.   hdma2.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  177.   hdma2.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
  178.   hdma2.Init.Mode = DMA_CIRCULAR;
  179.   hdma2.Init.Priority = DMA_PRIORITY_HIGH;
  180.   if (HAL_DMA_Init(&hdma2) != HAL_OK)
  181.   {
  182.     Error_Handler();
  183.   }
  184.  
  185.   /* DMA interrupt init */
  186.   /* DMA1_Channel1_IRQn interrupt configuration */
  187.   HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 0, 0);
  188.   HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn);
  189.   }
  190.  
  191. /* GPIO init function */
  192. void MX_GPIO_Init(void)
  193. {
  194.     /* GPIO Ports Clock Enable */
  195.     __HAL_RCC_GPIOC_CLK_ENABLE();
  196.  
  197.     GPIO_InitTypeDef GPIO_InitStruct;
  198.     /*Configure GPIO pin : PC13 */
  199.     GPIO_InitStruct.Pin = GPIO_PIN_13;
  200.     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  201.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  202.     HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  203. }
  204.  
  205. /**
  206.   * @brief System Clock Configuration
  207.   * @retval None
  208.   */
  209. void SystemClock_Config(void)
  210. {
  211.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  212.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  213.  
  214.   /** Supply configuration update enable
  215.   */
  216.   HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
  217.  
  218.   /** Configure the main internal regulator output voltage
  219.   */
  220.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  221.  
  222.   while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  223.  
  224.   /** Macro to configure the PLL clock source
  225.   */
  226.   __HAL_RCC_PLL_PLLSOURCE_CONFIG(RCC_PLLSOURCE_HSE);
  227.  
  228.   /** Initializes the RCC Oscillators according to the specified parameters
  229.   * in the RCC_OscInitTypeDef structure.
  230.   */
  231.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
  232.   RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  233.   RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  234.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  235.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  236.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  237.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  238.   {
  239.     Error_Handler();
  240.   }
  241.  
  242.   /** Initializes the CPU, AHB and APB buses clocks
  243.   */
  244.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  245.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  246.                               |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  247.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  248.   RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  249.   RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
  250.   RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
  251.   RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
  252.   RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
  253.   RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;
  254.  
  255.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  256.   {
  257.     Error_Handler();
  258.   }
  259. }
  260.  
  261. /* USER CODE BEGIN 4 */
  262.  
  263.  
  264.  
  265. /**
  266.   * @brief  Retargets the C library printf function to the USART.
  267.   * @param  None
  268.   * @retval None
  269.   */
  270. PUTCHAR_PROTOTYPE
  271. {
  272.   /* Place your implementation of fputc here */
  273.   /* e.g. write a character to the USART2 and Loop until the end of transmission */
  274.       HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 0xFFFF);
  275.  
  276.   return ch;
  277. }
  278. /* USER CODE END 4 */
  279.  
  280. /**
  281.   * @brief  This function is executed in case of error occurrence.
  282.   * @retval None
  283.   */
  284. void Error_Handler(void)
  285. {
  286.   /* USER CODE BEGIN Error_Handler_Debug */
  287.   /* User can add his own implementation to report the HAL error return state */
  288.   __disable_irq();
  289.   while (1)
  290.   {
  291.   }
  292.   /* USER CODE END Error_Handler_Debug */
  293. }
  294.  
  295. #ifdef  USE_FULL_ASSERT
  296. /**
  297.   * @brief  Reports the name of the source file and the source line number
  298.   *         where the assert_param error has occurred.
  299.   * @param  file: pointer to the source file name
  300.   * @param  line: assert_param error line source number
  301.   * @retval None
  302.   */
  303. void assert_failed(uint8_t *file, uint32_t line)
  304. {
  305.   /* USER CODE BEGIN 6 */
  306.   /* User can add his own implementation to report the file name and line number,
  307.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  308.   /* USER CODE END 6 */
  309. }
  310. #endif /* USE_FULL_ASSERT */
  311.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement