[CW32F030系列] 【CW32F030CxTx StartKit测评】通过板载串口打印信息

[复制链接]
 楼主| 740071911 发表于 2022-7-20 21:16 | 显示全部楼层 |阅读模式
<
本帖最后由 740071911 于 2022-7-20 21:51 编辑

刚拿到板子的时候把玩过串口,然后时隔时隔20天,就忘记怎么用了,写个帖子作为备忘录还是很好的,还不占地方
串口实验结果:
1:系统初始化时,打印系统时钟频率
2:串口定时打印信息

无标题.png
硬件连接:
(1)需要注意板载的串口模块CH340,并未与MCU的串口相连接,同时CH340的电源也需要自己选择。
看原理图,CN9排插线,VDDU,RX,TX都是悬空的,用板载串口模块时,需要用杜邦线与MCU的串口相连。
2.png
(2)板载串口模块CH340的电源VDDU的选择不同,J5跳帽的位置也要相应的变动,见手册CH340N有记录,
3.png
所以,这里也要有所注意。
实物连接:串口的GND与MCU的gnd是连接的,如果只打印信息,可以只用两根线,
VDDU--->VDD,PCRXD--->PB08即可。
4.png
代码实现:
  1. /* UART.c */
  1. /******************************************************************************
  2. * Include files
  3. ******************************************************************************/
  4. #include "Uart.h"

  5. /******************************************************************************
  6. * Local pre-processor symbols/macros ('#define')
  7. ******************************************************************************/
  8. //UARTx
  9. #define  DEBUG_USARTx                   CW_UART1
  10. #define  DEBUG_USART_BaudRate           9600
  11. #define  DEBUG_USART_UclkFreq           64000000

  12. //UARTx GPIO
  13. #define  DEBUG_USART_GPIO_CLK           RCC_AHB_PERIPH_GPIOB
  14. #define  DEBUG_USART_TX_GPIO_PORT       CW_GPIOB
  15. #define  DEBUG_USART_TX_GPIO_PIN        GPIO_PIN_8
  16. #define  DEBUG_USART_RX_GPIO_PORT       CW_GPIOB
  17. #define  DEBUG_USART_RX_GPIO_PIN        GPIO_PIN_9

  18. //GPIO AF
  19. #define  DEBUG_USART_AFTX               PB08_AFx_UART1TXD()
  20. #define  DEBUG_USART_AFRX               PB09_AFx_UART1RXD()
  21. /******************************************************************************
  22. * Global variable definitions (declared in header file with 'extern')
  23. ******************************************************************************/

  24. /******************************************************************************
  25. * Local type definitions ('typedef')
  26. ******************************************************************************/

  27. /******************************************************************************
  28. * Local function prototypes ('static')
  29. ******************************************************************************/


  30. void MX_UART_Init(void)
  31. {
  32.     GPIO_InitTypeDef ioConfig;
  33.     USART_InitTypeDef uartConfig;
  34.    
  35.     RCC_AHBPeriphClk_Enable(RCC_AHB_PERIPH_GPIOB, ENABLE);
  36.     RCC_APBPeriphClk_Enable2(RCC_APB2_PERIPH_UART1, ENABLE);
  37.    
  38.     //UART TX RX 复用
  39.     #if 0
  40.     DEBUG_USART_AFTX;
  41.     DEBUG_USART_AFRX;
  42.     #endif
  43.     PB08_AFx_UART1TXD();
  44.     PB09_AFx_UART1RXD();

  45.     ioConfig.Pins = DEBUG_USART_TX_GPIO_PIN;
  46.     ioConfig.Mode = GPIO_MODE_OUTPUT_PP;
  47.     ioConfig.Speed = GPIO_SPEED_HIGH;
  48.     GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &ioConfig);

  49.     ioConfig.Pins = DEBUG_USART_RX_GPIO_PIN;
  50.     ioConfig.Mode = GPIO_MODE_INPUT_PULLUP;
  51.     GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &ioConfig);
  52.    
  53.     uartConfig.USART_BaudRate = DEBUG_USART_BaudRate;
  54.     uartConfig.USART_Over = USART_Over_16;
  55.     uartConfig.USART_Source = USART_Source_PCLK;
  56.     uartConfig.USART_UclkFreq = DEBUG_USART_UclkFreq;
  57.     uartConfig.USART_StartBit = USART_StartBit_FE;
  58.     uartConfig.USART_StopBits = USART_StopBits_1;
  59.     uartConfig.USART_Parity = USART_Parity_No ;
  60.     uartConfig.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  61.     uartConfig.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  62.     USART_Init(DEBUG_USARTx, &uartConfig);
  63.    
  64. }



  65. #ifdef __GNUC__
  66.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  67.      set to 'Yes') calls __io_putchar() */
  68.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  69. #else
  70.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  71. #endif /* __GNUC__ */
  72.   
  73. /**
  74. * [url=home.php?mod=space&uid=247401]@brief[/url] Retargets the C library printf function to the USART.
  75. *
  76. */
  77. PUTCHAR_PROTOTYPE
  78. {
  79.     USART_SendData_8bit(CW_UART1, (uint8_t)ch);

  80.     while (USART_GetFlagStatus(CW_UART1, USART_FLAG_TXE) == RESET);

  81.     return ch;
  82. }




demo是在上一个工程基础上添加的,下面是系统时钟的获取打印

  1. /**
  2. *\*\name    PrintfClockInfo.
  3. *\*\fun     Printf clock information.
  4. *\*\param   none
  5. *\*\return  none
  6. **/
  7. void PrintfClockInfo(const char* msg)
  8. {
  9.     u32 nHclk_Freq;
  10.     u32 nPclk_Freq;
  11.    
  12.     /* reinit after sysclk changed */
  13.     MX_UART_Init();
  14.    
  15.     /* Wait for the configuration to succeed */
  16.     Delay(0xFFFF);
  17.    
  18.     printf("--------------------------------\n");
  19.     printf("%s:\n", msg);
  20.    
  21.     nHclk_Freq = RCC_Sysctrl_GetHClkFreq();
  22.     nPclk_Freq = RCC_Sysctrl_GetPClkFreq();
  23.    
  24.     printf("SystemCoreClock: %d MHz\n", SystemCoreClock/1000000);
  25.     printf("HCLK: %d MHz\n", nHclk_Freq/1000000);
  26.     printf("PCLK: %d MHz\n", nPclk_Freq/1000000);

  27.     /*
  28.     --------------------------------
  29.     clock acquisition, wait a minute...:
  30.     */
  31. }
代码附件:
最后附上代码
cw32-startkit-v1.1.zip (215.42 KB, 下载次数: 4)


zhaochunqing 发表于 2022-7-21 14:46 | 显示全部楼层
代码很详细,学习学习
janewood 发表于 2022-7-24 22:05 | 显示全部楼层
好像自带log这个函数吧   
sheflynn 发表于 2022-7-24 23:23 | 显示全部楼层
直接重映射printf就行。   
louliana 发表于 2022-8-18 18:27 | 显示全部楼层
自带的源代码有这个功能的。   
alvpeg 发表于 2022-8-20 16:36 | 显示全部楼层
使用printf函数吧。   
51xlf 发表于 2022-8-20 21:28 | 显示全部楼层
串口调试是首选方式。   
pixhw 发表于 2022-11-2 17:05 | 显示全部楼层
如何重新映射printf呢?              
janewood 发表于 2022-11-2 17:37 | 显示全部楼层
这个串口引脚是否支持5V的电平?
modesty3jonah 发表于 2022-11-2 17:58 | 显示全部楼层
这个CW32F030CxTx是否能配置1mhz的波特率呢
olivem55arlowe 发表于 2022-11-2 18:32 | 显示全部楼层
感觉串口连接都非常的复杂了呢。              
您需要登录后才可以回帖 登录 | 注册

本版积分规则

48

主题

887

帖子

5

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

48

主题

887

帖子

5

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