Advertisement
flyxtop

STM32F401RE uart_print main.c

Dec 2nd, 2015
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. /**
  2. ******************************************************************************
  3. * @file UART/UART_Printf/Src/main.c
  4. * @author MCD Application Team
  5. * @version V1.2.2
  6. * @date 09-October-2015
  7. * @brief This example shows how to retarget the C library printf function
  8. * to the UART.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  13. *
  14. * Redistribution and use in source and binary forms, with or without modification,
  15. * are permitted provided that the following conditions are met:
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ******************************************************************************
  37. */
  38.  
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "main.h"
  41.  
  42. /** @addtogroup STM32F4xx_HAL_Examples
  43. * @{
  44. */
  45.  
  46. /** @addtogroup UART_Printf
  47. * @{
  48. */
  49.  
  50. /* Private typedef -----------------------------------------------------------*/
  51. /* Private define ------------------------------------------------------------*/
  52. /* Private macro -------------------------------------------------------------*/
  53. /* Private variables ---------------------------------------------------------*/
  54. /* UART handler declaration */
  55. UART_HandleTypeDef UartHandle;
  56.  
  57. /* Private function prototypes -----------------------------------------------*/
  58. #ifdef __GNUC__
  59. /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  60. set to 'Yes') calls __io_putchar() */
  61. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  62. #else
  63. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  64. #endif /* __GNUC__ */
  65. static void SystemClock_Config(void);
  66. static void Error_Handler(void);
  67.  
  68. /* Private functions ---------------------------------------------------------*/
  69.  
  70. /**
  71. * @brief Main program
  72. * @param None
  73. * @retval None
  74. */
  75. int main(void)
  76. {
  77. /* STM32F4xx HAL library initialization:
  78. - Configure the Flash prefetch, instruction and Data caches
  79. - Configure the Systick to generate an interrupt each 1 msec
  80. - Set NVIC Group Priority to 4
  81. - Global MSP (MCU Support Package) initialization
  82. */
  83. HAL_Init();
  84.  
  85. /* Configure the system clock to 84 MHz */
  86. SystemClock_Config();
  87.  
  88. /*##-1- Configure the UART peripheral ######################################*/
  89. /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  90. /* UART1 configured as follow:
  91. - Word Length = 8 Bits
  92. - Stop Bit = One Stop bit
  93. - Parity = ODD parity
  94. - BaudRate = 9600 baud
  95. - Hardware flow control disabled (RTS and CTS signals) */
  96. UartHandle.Instance = USARTx;
  97.  
  98. UartHandle.Init.BaudRate = 9600;
  99. UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  100. UartHandle.Init.StopBits = UART_STOPBITS_1;
  101. UartHandle.Init.Parity = UART_PARITY_ODD;
  102. UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  103. UartHandle.Init.Mode = UART_MODE_TX_RX;
  104. UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
  105.  
  106. if(HAL_UART_Init(&UartHandle) != HAL_OK)
  107. {
  108. /* Initialization Error */
  109. Error_Handler();
  110. }
  111.  
  112. /* Output a message on Hyperterminal using printf function */
  113. printf("\n\r UART Printf Example: retarget the C library printf function to the UART\n\r");
  114.  
  115. /* Infinite loop */
  116. while (1)
  117. {
  118. }
  119. }
  120.  
  121. /**
  122. * @brief Retargets the C library printf function to the USART.
  123. * @param None
  124. * @retval None
  125. */
  126. PUTCHAR_PROTOTYPE
  127. {
  128. /* Place your implementation of fputc here */
  129. /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  130.  
  131. // while( USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
  132. // USART_SendData(USART1, (uint8_t) ch);
  133.  
  134. HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 1, 0xFFFF);
  135.  
  136. return ch;
  137. }
  138.  
  139. /**
  140. * @brief System Clock Configuration
  141. * The system Clock is configured as follow :
  142. * System Clock source = PLL (HSI)
  143. * SYSCLK(Hz) = 84000000
  144. * HCLK(Hz) = 84000000
  145. * AHB Prescaler = 1
  146. * APB1 Prescaler = 2
  147. * APB2 Prescaler = 1
  148. * HSI Frequency(Hz) = 16000000
  149. * PLL_M = 16
  150. * PLL_N = 336
  151. * PLL_P = 4
  152. * PLL_Q = 7
  153. * VDD(V) = 3.3
  154. * Main regulator output voltage = Scale2 mode
  155. * Flash Latency(WS) = 2
  156. * @param None
  157. * @retval None
  158. */
  159. static void SystemClock_Config(void)
  160. {
  161. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  162. RCC_OscInitTypeDef RCC_OscInitStruct;
  163.  
  164. /* Enable Power Control clock */
  165. __HAL_RCC_PWR_CLK_ENABLE();
  166.  
  167. /* The voltage scaling allows optimizing the power consumption when the device is
  168. clocked below the maximum system frequency, to update the voltage scaling value
  169. regarding system frequency refer to product datasheet. */
  170. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  171.  
  172. /* Enable HSI Oscillator and activate PLL with HSI as source */
  173. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  174. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  175. RCC_OscInitStruct.HSICalibrationValue = 0x10;
  176. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  177. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  178. RCC_OscInitStruct.PLL.PLLM = 16;
  179. RCC_OscInitStruct.PLL.PLLN = 336;
  180. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  181. RCC_OscInitStruct.PLL.PLLQ = 7;
  182. if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  183. {
  184. Error_Handler();
  185. }
  186.  
  187. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  188. clocks dividers */
  189. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  190. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  191. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  192. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  193. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  194. if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  195. {
  196. Error_Handler();
  197. }
  198. }
  199.  
  200. /**
  201. * @brief This function is executed in case of error occurrence.
  202. * @param None
  203. * @retval None
  204. */
  205. static void Error_Handler(void)
  206. {
  207. /* Turn LED2 on */
  208. BSP_LED_On(LED2);
  209. while(1)
  210. {
  211. }
  212. }
  213.  
  214. #ifdef USE_FULL_ASSERT
  215. /**
  216. * @brief Reports the name of the source file and the source line number
  217. * where the assert_param error has occurred.
  218. * @param file: pointer to the source file name
  219. * @param line: assert_param error line source number
  220. * @retval None
  221. */
  222. void assert_failed(uint8_t* file, uint32_t line)
  223. {
  224. /* User can add his own implementation to report the file name and line number,
  225. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  226.  
  227. /* Infinite loop */
  228. while (1)
  229. {
  230. }
  231. }
  232. #endif
  233.  
  234. /**
  235. * @}
  236. */
  237.  
  238. /**
  239. * @}
  240. */
  241.  
  242. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement