[STM32F7] 【STM32F767ZIT6测评】串口打印调试信息

[复制链接]
 楼主| hwl1023 发表于 2016-8-20 21:23 | 显示全部楼层 |阅读模式
     由于Nucleo 开发板没有带有显示屏幕,如果自己不想扩展LCD,那么在使用Nucleo 调试中想打印输出一些调整信息到PC就很有必要。我们使用Nucleo板子上自带的STLINK上的虚拟串口连接到STM32F767ZIT6上的串口3,这样我们的调试信息就可以通过STLINK的虚拟串口发送到电脑的串口助手了。
   硬件连接图如下:


下面我们分别使用串口的3中输出模式来打印调试信息:
1)查询模式发送数据:这个模式使用的是重定向printf函数实现的,使用之前要选择keil的Mocro LIB。
发送的重定向函数如下:
  1. fputc(int ch, FILE *f)
  2. {
  3.   /* Place your implementation of fputc here */
  4.   /* e.g. write a character to the USART3 and Loop until the end of transmission */
  5.   HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 1, 0xFFFF);
  6.   return ch;
  7. }
2)串口中断发送模式是使用串口发送中断实现的,实现函数如下:

  1. void My_Printf_IT(uint8_t *pData, uint16_t Size)
  2. {
  3.         HAL_UART_Transmit_IT(&UartHandle,pData,Size);
  4. }
3)串口DMA中断发送模式:
  1. void My_Printf_IT(uint8_t *pData, uint16_t Size)
  2. {
  3.         HAL_UART_Transmit_IT(&UartHandle,pData,Size);
  4. }
需要注意的是上面后2种是使用中断发送的所以要控制发送的时间间隔。
在串口的初始化中我们要初始化串口的dma发送和使能发送中断打开:
  1. void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  2. {
  3.         static  DMA_HandleTypeDef hdma_rx;
  4.         static  DMA_HandleTypeDef hdma_tx;

  5.   GPIO_InitTypeDef  GPIO_InitStruct;

  6.   RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;

  7.         __HAL_RCC_GPIOD_CLK_ENABLE();
  8.        
  9.   /* Select SysClk as source of USART1 clocks */
  10.   RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  11.   RCC_PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_SYSCLK;
  12.   HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit);

  13.   /* Enable USARTx clock */
  14.   __HAL_RCC_USART3_CLK_ENABLE();
  15.        
  16.         __HAL_RCC_DMA1_CLK_ENABLE();


  17.   /* UART TX GPIO pin configuration  */
  18.   GPIO_InitStruct.Pin       = GPIO_PIN_8;
  19.   GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
  20.   GPIO_InitStruct.Pull      = GPIO_PULLUP;
  21.   GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;
  22.   GPIO_InitStruct.Alternate = GPIO_AF7_USART3;

  23.   HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  24.   /* UART RX GPIO pin configuration  */
  25.   GPIO_InitStruct.Pin = GPIO_PIN_9;
  26.   GPIO_InitStruct.Alternate = GPIO_AF7_USART3;

  27.   HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  28.        
  29.         hdma_tx.Instance                 = DMA1_Stream3;
  30.         hdma_tx.Init.Channel             = DMA_CHANNEL_4;
  31.         hdma_tx.Init.Direction           = DMA_MEMORY_TO_PERIPH;
  32.         hdma_tx.Init.PeriphInc           = DMA_PINC_DISABLE;
  33.         hdma_tx.Init.MemInc              = DMA_MINC_ENABLE;
  34.         hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  35.         hdma_tx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
  36.         hdma_tx.Init.Mode                = DMA_NORMAL;
  37.         hdma_tx.Init.Priority            = DMA_PRIORITY_HIGH;
  38.         hdma_tx.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;
  39.         hdma_tx.Init.FIFOThreshold       = DMA_FIFO_THRESHOLD_FULL;
  40.         hdma_tx.Init.MemBurst            = DMA_MBURST_INC4;
  41.         hdma_tx.Init.PeriphBurst         = DMA_PBURST_INC4;

  42.         HAL_DMA_Init(&hdma_tx);

  43.         /* Associate the initialized DMA handle to the UART handle */
  44.         __HAL_LINKDMA(&UartHandle, hdmatx, hdma_tx);

  45.         hdma_rx.Instance                 = DMA1_Stream1;
  46.         hdma_rx.Init.Channel             = DMA_CHANNEL_4;
  47.         hdma_rx.Init.Direction           = DMA_PERIPH_TO_MEMORY;
  48.         hdma_rx.Init.PeriphInc           = DMA_PINC_DISABLE;
  49.         hdma_rx.Init.MemInc              = DMA_MINC_ENABLE;
  50.         hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  51.         hdma_rx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
  52.         hdma_rx.Init.Mode                = DMA_NORMAL;
  53.         hdma_rx.Init.Priority            = DMA_PRIORITY_HIGH;
  54.         hdma_rx.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;
  55.         hdma_rx.Init.FIFOThreshold       = DMA_FIFO_THRESHOLD_FULL;
  56.         hdma_rx.Init.MemBurst            = DMA_MBURST_INC4;
  57.         hdma_rx.Init.PeriphBurst         = DMA_PBURST_INC4;

  58.         HAL_DMA_Init(&hdma_rx);

  59.         /* Associate the initialized DMA handle to the UART handle */
  60.         __HAL_LINKDMA(&UartHandle, hdmarx, hdma_rx);


  61.         HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 0, 1);
  62.         HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn);


  63.         HAL_NVIC_SetPriority(DMA1_Stream4_IRQn, 0, 1);
  64.         HAL_NVIC_EnableIRQ(DMA1_Stream4_IRQn);

  65.         HAL_NVIC_SetPriority(USART3_IRQn, 0, 1);
  66.         HAL_NVIC_EnableIRQ(USART3_IRQn);

  67. }
最后我们在main函数中测试3种输出打印函数:
  1. uint8_t buff_loop[40] = "USART Loop Printf:hello,world\n";
  2. uint8_t buff_dma[40]         = "USART DMA  Printf:hello,world\n";
  3. uint8_t buff_IT[40]         = "USART IT   Printf:hello,world\n";

  4. int main(void)
  5. {
  6.   CPU_CACHE_Enable();
  7.   HAL_Init();
  8.   SystemClock_Config();
  9.         USART3_Init();

  10.   while (1)
  11.   {
  12.                 printf((const char*)buff_loop);
  13.                 HAL_Delay(1000);
  14.                 My_Printf_DMA(buff_dma,strlen((const char*)buff_dma));
  15.                 HAL_Delay(1000);
  16.                 My_Printf_IT(buff_IT,strlen((const char*)buff_IT));
  17.                 HAL_Delay(1000);
  18.         }               

  19. }
可以在电脑串口助手中查看输出如下:
USART DMA  Printf:hello,world
USART IT   Printf:hello,world
USART Loop Printf:hello,world
USART DMA  Printf:hello,world
USART IT   Printf:hello,world
USART Loop Printf:hello,world
USART DMA  Printf:hello,world
USART IT   Printf:hello,world
USART Loop Printf:hello,world
USART DMA  Printf:hello,world
USART IT   Printf:hello,world
USART Loop Printf:hello,world
USART DMA  Printf:hello,world
USART IT   Printf:hello,world
USART Loop Printf:hello,world
USART DMA  Printf:hello,world

您需要登录后才可以回帖 登录 | 注册

本版积分规则

9

主题

158

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部