Guest User

Untitled

a guest
Dec 19th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.40 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file    UART/UART_Printf/Src/main.c
  4.   * @author  MCD Application Team
  5.   * @version V1.1.0
  6.   * @date    26-June-2014
  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) 2014 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.  
  105.   if(HAL_UART_Init(&UartHandle) != HAL_OK)
  106.   {
  107.     /* Initialization Error */
  108.     Error_Handler();
  109.   }
  110.  
  111.   /* Output a message on Hyperterminal using printf function */
  112.   printf("\n\r UART Printf Example: retarget the C library printf function to the UART\n\r");
  113.  
  114.   /* Infinite loop */
  115.   while (1)
  116.   {
  117.   }
  118. }
  119.  
  120. /**
  121.   * @brief  Retargets the C library printf function to the USART.
  122.   * @param  None
  123.   * @retval None
  124.   */
  125. PUTCHAR_PROTOTYPE
  126. {
  127.   /* Place your implementation of fputc here */
  128.   /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  129.   HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 1, 0xFFFF);
  130.  
  131.   return ch;
  132. }
  133.  
  134. /**
  135.   * @brief  System Clock Configuration
  136.   *         The system Clock is configured as follow :
  137.   *            System Clock source            = PLL (HSI)
  138.   *            SYSCLK(Hz)                     = 84000000
  139.   *            HCLK(Hz)                       = 84000000
  140.   *            AHB Prescaler                  = 1
  141.   *            APB1 Prescaler                 = 2
  142.   *            APB2 Prescaler                 = 1
  143.   *            HSI Frequency(Hz)              = 16000000
  144.   *            PLL_M                          = 16
  145.   *            PLL_N                          = 336
  146.   *            PLL_P                          = 4
  147.   *            PLL_Q                          = 7
  148.   *            VDD(V)                         = 3.3
  149.   *            Main regulator output voltage  = Scale2 mode
  150.   *            Flash Latency(WS)              = 2
  151.   * @param  None
  152.   * @retval None
  153.   */
  154. static void SystemClock_Config(void)
  155. {
  156.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  157.   RCC_OscInitTypeDef RCC_OscInitStruct;
  158.  
  159.   /* Enable Power Control clock */
  160.   __PWR_CLK_ENABLE();
  161.  
  162.   /* The voltage scaling allows optimizing the power consumption when the device is
  163.      clocked below the maximum system frequency, to update the voltage scaling value
  164.      regarding system frequency refer to product datasheet.  */
  165.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  166.  
  167.   /* Enable HSI Oscillator and activate PLL with HSI as source */
  168.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  169.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  170.   RCC_OscInitStruct.HSICalibrationValue = 0x10;
  171.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  172.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  173.   RCC_OscInitStruct.PLL.PLLM = 16;
  174.   RCC_OscInitStruct.PLL.PLLN = 336;
  175.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  176.   RCC_OscInitStruct.PLL.PLLQ = 7;
  177.   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  178.   {
  179.     Error_Handler();
  180.   }
  181.  
  182.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  183.      clocks dividers */
  184.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  185.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  186.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  187.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;  
  188.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;  
  189.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  190.   {
  191.     Error_Handler();
  192.   }
  193. }
  194.  
  195. /**
  196.   * @brief  This function is executed in case of error occurrence.
  197.   * @param  None
  198.   * @retval None
  199.   */
  200. static void Error_Handler(void)
  201. {
  202.   /* Turn LED2 on */
  203.   BSP_LED_On(LED2);
  204.   while(1)
  205.   {
  206.   }
  207. }
  208.  
  209. #ifdef  USE_FULL_ASSERT
  210. /**
  211.   * @brief  Reports the name of the source file and the source line number
  212.   *         where the assert_param error has occurred.
  213.   * @param  file: pointer to the source file name
  214.   * @param  line: assert_param error line source number
  215.   * @retval None
  216.   */
  217. void assert_failed(uint8_t* file, uint32_t line)
  218. {
  219.   /* User can add his own implementation to report the file name and line number,
  220.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  221.  
  222.   /* Infinite loop */
  223.   while (1)
  224.   {
  225.   }
  226. }
  227. #endif
  228.  
  229. /**
  230.   * @}
  231.   */
  232.  
  233. /**
  234.   * @}
  235.   */
  236.  
  237. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment