Guest User

Untitled

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