Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. int main(void)
  2. {
  3. /* USER CODE BEGIN 1 */
  4. HAL_StatusTypeDef serial_port_status;
  5. int number=0;
  6. uint16_t buffer_size=0;
  7. uint8_t buffer[50];
  8. int flag=0;
  9. /* USER CODE END 1 */
  10.  
  11.  
  12. /* MCU Configuration--------------------------------------------------------*/
  13.  
  14. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  15. HAL_Init();
  16.  
  17. /* USER CODE BEGIN Init */
  18.  
  19. /* USER CODE END Init */
  20.  
  21. /* Configure the system clock */
  22. SystemClock_Config();
  23.  
  24. /* USER CODE BEGIN SysInit */
  25.  
  26. /* USER CODE END SysInit */
  27.  
  28. /* Initialize all configured peripherals */
  29. MX_GPIO_Init();
  30. MX_ETH_Init();
  31. MX_USART3_UART_Init();
  32. MX_USB_OTG_FS_PCD_Init();
  33. /* USER CODE BEGIN 2 */
  34. //HAL_UART_Receive(&huart3,received_data, 11);
  35. //serial_port_status = HAL_UART_Transmit(&huart3,"Hello World", 13, 0xffff);
  36. /* USER CODE END 2 */
  37.  
  38. /* Infinite loop */
  39. /* USER CODE BEGIN WHILE */
  40. while (1)
  41. {
  42. /* USER CODE END WHILE */
  43.  
  44. /* USER CODE BEGIN 3 */
  45. buffer_size=sprintf(buffer,"Hello: %d.\n\r", number);
  46. if(HAL_GPIO_ReadPin(USER_Btn_GPIO_Port, USER_Btn_Pin)==GPIO_PIN_SET && flag==0)
  47. {
  48. number++;
  49. serial_port_status = HAL_UART_Transmit(&huart3,buffer, buffer_size, 0xffff);
  50. flag=1;
  51. }
  52. else if (HAL_GPIO_ReadPin(USER_Btn_GPIO_Port, USER_Btn_Pin)==GPIO_PIN_RESET) flag=0;
  53.  
  54. HAL_Delay(100);
  55. }
  56. /* USER CODE END 3 */
  57. }
  58.  
  59.  
  60.  
  61. ***************************************************************************************************************************************
  62. * USER CODE BEGIN Header */
  63. /**
  64. ******************************************************************************
  65. * @file : main.c
  66. * @brief : Main program body
  67. ******************************************************************************
  68. * @attention
  69. *
  70. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  71. * All rights reserved.</center></h2>
  72. *
  73. * This software component is licensed by ST under BSD 3-Clause license,
  74. * the "License"; You may not use this file except in compliance with the
  75. * License. You may obtain a copy of the License at:
  76. * opensource.org/licenses/BSD-3-Clause
  77. *
  78. ******************************************************************************
  79. */
  80. /* USER CODE END Header */
  81.  
  82. /* Includes ------------------------------------------------------------------*/
  83. #include "main.h"
  84. #include "eth.h"
  85. #include "usart.h"
  86. #include "usb_otg.h"
  87. #include "gpio.h"
  88.  
  89. /* Private includes ----------------------------------------------------------*/
  90. /* USER CODE BEGIN Includes */
  91.  
  92. /* USER CODE END Includes */
  93.  
  94. /* Private typedef -----------------------------------------------------------*/
  95. /* USER CODE BEGIN PTD */
  96.  
  97. /* USER CODE END PTD */
  98.  
  99. /* Private define ------------------------------------------------------------*/
  100. /* USER CODE BEGIN PD */
  101.  
  102. /* USER CODE END PD */
  103.  
  104. /* Private macro -------------------------------------------------------------*/
  105. /* USER CODE BEGIN PM */
  106.  
  107. /* USER CODE END PM */
  108.  
  109. /* Private variables ---------------------------------------------------------*/
  110.  
  111. /* USER CODE BEGIN PV */
  112.  
  113. /* USER CODE END PV */
  114.  
  115. /* Private function prototypes -----------------------------------------------*/
  116. void SystemClock_Config(void);
  117. /* USER CODE BEGIN PFP */
  118.  
  119. /* USER CODE END PFP */
  120.  
  121. /* Private user code ---------------------------------------------------------*/
  122. /* USER CODE BEGIN 0 */
  123. uint8_t buffer2;
  124. /* USER CODE END 0 */
  125.  
  126. /**
  127. * @brief The application entry point.
  128. * @retval int
  129. */
  130. int main(void)
  131. {
  132. /* USER CODE BEGIN 1 */
  133. HAL_StatusTypeDef serial_port_status;
  134. int number=0;
  135. uint16_t buffer_size=0;
  136. uint8_t buffer[50];
  137. int flag=0;
  138. /* USER CODE END 1 */
  139.  
  140.  
  141. /* MCU Configuration--------------------------------------------------------*/
  142.  
  143. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  144. HAL_Init();
  145.  
  146. /* USER CODE BEGIN Init */
  147.  
  148. /* USER CODE END Init */
  149.  
  150. /* Configure the system clock */
  151. SystemClock_Config();
  152.  
  153. /* USER CODE BEGIN SysInit */
  154.  
  155. /* USER CODE END SysInit */
  156.  
  157. /* Initialize all configured peripherals */
  158. MX_GPIO_Init();
  159. MX_ETH_Init();
  160. MX_USART3_UART_Init();
  161. MX_USB_OTG_FS_PCD_Init();
  162. /* USER CODE BEGIN 2 */
  163. //HAL_UART_Receive(&huart3,received_data, 11);
  164. //serial_port_status = HAL_UART_Transmit(&huart3,"Hello World", 13, 0xffff);
  165. /* USER CODE END 2 */
  166.  
  167. /* Infinite loop */
  168. /* USER CODE BEGIN WHILE */
  169. while (1)
  170. {
  171. /* USER CODE END WHILE */
  172. if(flag==0)
  173. {
  174. HAL_UART_Receive_IT(&huart3,&buffer2,1);
  175. flag=1;
  176. }
  177. /* USER CODE BEGIN 3 */
  178.  
  179. //HAL_Delay(100);
  180. }
  181. /* USER CODE END 3 */
  182. }
  183.  
  184. /**
  185. * @brief System Clock Configuration
  186. * @retval None
  187. */
  188. void SystemClock_Config(void)
  189. {
  190. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  191. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  192. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  193.  
  194. /** Configure LSE Drive Capability
  195. */
  196. HAL_PWR_EnableBkUpAccess();
  197. /** Configure the main internal regulator output voltage
  198. */
  199. __HAL_RCC_PWR_CLK_ENABLE();
  200. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  201. /** Initializes the CPU, AHB and APB busses clocks
  202. */
  203. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  204. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  205. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  206. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  207. RCC_OscInitStruct.PLL.PLLM = 4;
  208. RCC_OscInitStruct.PLL.PLLN = 72;
  209. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  210. RCC_OscInitStruct.PLL.PLLQ = 3;
  211. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  212. {
  213. Error_Handler();
  214. }
  215. /** Initializes the CPU, AHB and APB busses clocks
  216. */
  217. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  218. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  219. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  220. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  221. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  222. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  223.  
  224. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  225. {
  226. Error_Handler();
  227. }
  228. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_CLK48;
  229. PeriphClkInitStruct.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
  230. PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48SOURCE_PLL;
  231. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  232. {
  233. Error_Handler();
  234. }
  235. }
  236.  
  237. /* USER CODE BEGIN 4 */
  238. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  239. {
  240. HAL_UART_Transmit_IT(&huart3,&buffer2,1);
  241. HAL_UART_Receive_IT(&huart3,&buffer2,1);
  242. HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);
  243. }
  244.  
  245. /* USER CODE END 4 */
  246.  
  247. /**
  248. * @brief This function is executed in case of error occurrence.
  249. * @retval None
  250. */
  251. void Error_Handler(void)
  252. {
  253. /* USER CODE BEGIN Error_Handler_Debug */
  254. /* User can add his own implementation to report the HAL error return state */
  255.  
  256. /* USER CODE END Error_Handler_Debug */
  257. }
  258.  
  259. #ifdef USE_FULL_ASSERT
  260. /**
  261. * @brief Reports the name of the source file and the source line number
  262. * where the assert_param error has occurred.
  263. * @param file: pointer to the source file name
  264. * @param line: assert_param error line source number
  265. * @retval None
  266. */
  267. void assert_failed(uint8_t *file, uint32_t line)
  268. {
  269. /* USER CODE BEGIN 6 */
  270. /* User can add his own implementation to report the file name and line number,
  271. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  272. /* USER CODE END 6 */
  273. }
  274. #endif /* USE_FULL_ASSERT */
  275.  
  276. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement