Advertisement
Guest User

Untitled

a guest
Oct 31st, 2018
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.23 KB | None | 0 0
  1.  
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2018 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "main.h"
  41. #include "stm32l0xx_hal.h"
  42.  
  43. /* USER CODE BEGIN Includes */
  44. uint8_t tx_buff[]={1};
  45. uint8_t rx_buff[10];
  46. char statusas=0;
  47. /* USER CODE END Includes */
  48.  
  49. /* Private variables ---------------------------------------------------------*/
  50. UART_HandleTypeDef huart1;
  51. DMA_HandleTypeDef hdma_usart1_rx;
  52. DMA_HandleTypeDef hdma_usart1_tx;
  53.  
  54. /* USER CODE BEGIN PV */
  55. /* Private variables ---------------------------------------------------------*/
  56.  
  57. /* USER CODE END PV */
  58.  
  59. /* Private function prototypes -----------------------------------------------*/
  60. void SystemClock_Config(void);
  61. static void MX_GPIO_Init(void);
  62. static void MX_DMA_Init(void);
  63. static void MX_USART1_UART_Init(void);
  64.  
  65. /* USER CODE BEGIN PFP */
  66. /* Private function prototypes -----------------------------------------------*/
  67.  
  68. /* USER CODE END PFP */
  69.  
  70. /* USER CODE BEGIN 0 */
  71.  
  72. /* USER CODE END 0 */
  73.  
  74. /**
  75. * @brief The application entry point.
  76. *
  77. * @retval None
  78. */
  79. int main(void)
  80. {
  81. /* USER CODE BEGIN 1 */
  82.  
  83. /* USER CODE END 1 */
  84.  
  85. /* MCU Configuration----------------------------------------------------------*/
  86.  
  87. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  88. HAL_Init();
  89.  
  90. /* USER CODE BEGIN Init */
  91.  
  92. /* USER CODE END Init */
  93.  
  94. /* Configure the system clock */
  95. SystemClock_Config();
  96.  
  97. /* USER CODE BEGIN SysInit */
  98.  
  99. /* USER CODE END SysInit */
  100.  
  101. /* Initialize all configured peripherals */
  102. MX_GPIO_Init();
  103. MX_DMA_Init();
  104. MX_USART1_UART_Init();
  105. /* USER CODE BEGIN 2 */
  106.  
  107. /* USER CODE END 2 */
  108.  
  109. /* Infinite loop */
  110. /* USER CODE BEGIN WHILE */
  111. while (1)
  112. {
  113. HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
  114. // HAL_UART_Receive_DMA(&huart1, rx_buff, 10);
  115. // HAL_UART_Transmit(&huart1,tx_buff,sizeof(tx_buff),30);// Sending in normal mode
  116. // HAL_Delay(1000);
  117. HAL_UART_Transmit_DMA(&huart1,tx_buff,sizeof(tx_buff));// Sending in DMA mode
  118. HAL_Delay(1000);
  119.  
  120.  
  121. /* USER CODE END WHILE */
  122.  
  123. /* USER CODE BEGIN 3 */
  124.  
  125. }
  126. /* USER CODE END 3 */
  127.  
  128. }
  129.  
  130. /**
  131. * @brief System Clock Configuration
  132. * @retval None
  133. */
  134. void SystemClock_Config(void)
  135. {
  136.  
  137. RCC_OscInitTypeDef RCC_OscInitStruct;
  138. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  139. RCC_PeriphCLKInitTypeDef PeriphClkInit;
  140.  
  141. /**Configure the main internal regulator output voltage
  142. */
  143. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  144.  
  145. /**Initializes the CPU, AHB and APB busses clocks
  146. */
  147. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  148. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  149. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  150. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  151. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_8;
  152. RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  153. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  154. {
  155. _Error_Handler(__FILE__, __LINE__);
  156. }
  157.  
  158. /**Initializes the CPU, AHB and APB busses clocks
  159. */
  160. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  161. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  162. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  163. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  164. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  165. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  166.  
  167. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  168. {
  169. _Error_Handler(__FILE__, __LINE__);
  170. }
  171.  
  172. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  173. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  174. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  175. {
  176. _Error_Handler(__FILE__, __LINE__);
  177. }
  178.  
  179. /**Configure the Systick interrupt time
  180. */
  181. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  182.  
  183. /**Configure the Systick
  184. */
  185. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  186.  
  187. /* SysTick_IRQn interrupt configuration */
  188. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  189. }
  190.  
  191. /* USART1 init function */
  192. static void MX_USART1_UART_Init(void)
  193. {
  194.  
  195. huart1.Instance = USART1;
  196. huart1.Init.BaudRate = 9800;
  197. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  198. huart1.Init.StopBits = UART_STOPBITS_1;
  199. huart1.Init.Parity = UART_PARITY_NONE;
  200. huart1.Init.Mode = UART_MODE_TX_RX;
  201. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  202. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  203. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  204. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT|UART_ADVFEATURE_DMADISABLEONERROR_INIT;
  205. huart1.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;
  206. huart1.AdvancedInit.DMADisableonRxError = UART_ADVFEATURE_DMA_DISABLEONRXERROR;
  207. if (HAL_UART_Init(&huart1) != HAL_OK)
  208. {
  209. _Error_Handler(__FILE__, __LINE__);
  210. }
  211.  
  212. }
  213.  
  214. /**
  215. * Enable DMA controller clock
  216. */
  217. static void MX_DMA_Init(void)
  218. {
  219. /* DMA controller clock enable */
  220. __HAL_RCC_DMA1_CLK_ENABLE();
  221.  
  222. /* DMA interrupt init */
  223. /* DMA1_Channel2_3_IRQn interrupt configuration */
  224. HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0);
  225. HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);
  226.  
  227. }
  228.  
  229. /** Configure pins as
  230. * Analog
  231. * Input
  232. * Output
  233. * EVENT_OUT
  234. * EXTI
  235. */
  236. static void MX_GPIO_Init(void)
  237. {
  238.  
  239. GPIO_InitTypeDef GPIO_InitStruct;
  240.  
  241. /* GPIO Ports Clock Enable */
  242. __HAL_RCC_GPIOH_CLK_ENABLE();
  243. __HAL_RCC_GPIOA_CLK_ENABLE();
  244.  
  245. /*Configure GPIO pin Output Level */
  246. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
  247.  
  248. /*Configure GPIO pin : PA5 */
  249. GPIO_InitStruct.Pin = GPIO_PIN_5;
  250. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  251. GPIO_InitStruct.Pull = GPIO_NOPULL;
  252. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  253. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  254.  
  255. }
  256.  
  257. /* USER CODE BEGIN 4 */
  258. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
  259. statusas=1;
  260. }
  261. /* USER CODE END 4 */
  262.  
  263. /**
  264. * @brief This function is executed in case of error occurrence.
  265. * @param file: The file name as string.
  266. * @param line: The line in file as a number.
  267. * @retval None
  268. */
  269. void _Error_Handler(char *file, int line)
  270. {
  271. /* USER CODE BEGIN Error_Handler_Debug */
  272. /* User can add his own implementation to report the HAL error return state */
  273. while(1)
  274. {
  275. }
  276. /* USER CODE END Error_Handler_Debug */
  277. }
  278.  
  279. #ifdef USE_FULL_ASSERT
  280. /**
  281. * @brief Reports the name of the source file and the source line number
  282. * where the assert_param error has occurred.
  283. * @param file: pointer to the source file name
  284. * @param line: assert_param error line source number
  285. * @retval None
  286. */
  287. void assert_failed(uint8_t* file, uint32_t line)
  288. {
  289. /* USER CODE BEGIN 6 */
  290. /* User can add his own implementation to report the file name and line number,
  291. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  292. /* USER CODE END 6 */
  293. }
  294. #endif /* USE_FULL_ASSERT */
  295.  
  296. /**
  297. * @}
  298. */
  299.  
  300. /**
  301. * @}
  302. */
  303.  
  304. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement