Advertisement
jiapei100

bsp_debug_usart.c

May 22nd, 2018
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file    bsp_debug_usart.c
  4.   * @author  fire
  5.   * @version V1.0
  6.   * @date    2016-xx-xx
  7.   * @brief   使用串口1,重定向c库printf函数到usart端口,中断接收模式
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * 实验平台:秉火  STM32 F746 开发板
  12.   * 论坛    :http://www.firebbs.cn
  13.   * 淘宝    :http://firestm32.taobao.com
  14.   *
  15.   ******************************************************************************
  16.   */
  17.  
  18. #include "stm32f7xx_hal_uart.h"
  19. //#include "../usart/bsp_debug_usart.h"
  20.  
  21. UART_HandleTypeDef UartHandle;
  22. extern uint8_t ucTemp;  
  23.  /**
  24.   * @brief  DEBUG_USART GPIO configuration, working mode configuration. 115200 8-N-1
  25.   * @param  None
  26.   * @retval None
  27.   */  
  28. void DEBUG_USART_Config(void)
  29. {
  30.     GPIO_InitTypeDef GPIO_InitStruct;
  31.  
  32.     RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
  33.        
  34.     DEBUG_USART_RX_GPIO_CLK_ENABLE();
  35.     DEBUG_USART_TX_GPIO_CLK_ENABLE();
  36.    
  37.     /* 配置串口1时钟源*/
  38.     RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
  39.     RCC_PeriphClkInit.Usart1ClockSelection = RCC_USART2CLKSOURCE_SYSCLK;
  40.     HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit);
  41.     /* 使能 UART 时钟 */
  42.     DEBUG_USART_CLK_ENABLE();
  43.  
  44.     /**USART1 GPIO Configuration    
  45.     PA9     ------> USART2_TX
  46.     PA10    ------> USART2_RX
  47.     */
  48.     /* 配置Tx引脚为复用功能  */
  49.     GPIO_InitStruct.Pin = DEBUG_USART_TX_PIN;
  50.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  51.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  52.     GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  53.     GPIO_InitStruct.Alternate = DEBUG_USART_TX_AF;
  54.     HAL_GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStruct);
  55.    
  56.     /* 配置Rx引脚为复用功能 */
  57.     GPIO_InitStruct.Pin = DEBUG_USART_RX_PIN;
  58.     GPIO_InitStruct.Alternate = DEBUG_USART_RX_AF;
  59.     HAL_GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStruct);
  60.    
  61.     /* 配置串DEBUG_USART 模式 */
  62.     UartHandle.Instance = DEBUG_USART;
  63.     UartHandle.Init.BaudRate = 115200;
  64.     UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  65.     UartHandle.Init.StopBits = UART_STOPBITS_1;
  66.     UartHandle.Init.Parity = UART_PARITY_NONE;
  67.     UartHandle.Init.Mode = UART_MODE_TX_RX;
  68.     UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  69.     UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
  70.     UartHandle.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;
  71.     UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  72.     HAL_UART_Init(&UartHandle);
  73.  
  74.     /*串口1中断初始化 */
  75.     HAL_NVIC_SetPriority(DEBUG_USART_IRQ, 0, 0);
  76.     HAL_NVIC_EnableIRQ(DEBUG_USART_IRQ);
  77.     /*配置串口接收中断 */
  78.     __HAL_UART_ENABLE_IT(&UartHandle,UART_IT_RXNE);  
  79. }
  80.  
  81.  
  82. /*****************  发送字符串 **********************/
  83. void Usart_SendString( USART_TypeDef * pUSARTx, uint8_t *str)
  84. {
  85.     unsigned int k=0;
  86.   do
  87.   {
  88.       HAL_UART_Transmit( &UartHandle,(uint8_t *)(str + k) ,1,1000);
  89.       k++;
  90.   } while(*(str + k)!='\0');
  91.  
  92. }
  93. ///重定向c库函数printf到串口DEBUG_USART,重定向后可使用printf函数
  94. int fputc(int ch, FILE *f)
  95. {
  96.     /* 发送一个字节数据到串口DEBUG_USART */
  97.     HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 1, 1000);   
  98.    
  99.     return (ch);
  100. }
  101.  
  102. ///重定向c库函数scanf到串口DEBUG_USART,重写向后可使用scanf、getchar等函数
  103. int fgetc(FILE *f)
  104. {
  105.        
  106.     int ch;
  107.     HAL_UART_Receive(&UartHandle, (uint8_t *)&ch, 1, 1000);
  108.     return (ch);
  109. }
  110. /*********************************************END OF FILE**********************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement