[开发资料] 【Ubuntu VSCODE+GCC】CW32L031实现printf工程

[复制链接]
 楼主| lulugl 发表于 2023-6-28 17:22 | 显示全部楼层 |阅读模式
#申请原创# #有奖活动# @21ic小跑堂
一、工程包准备
按照这个帖子完成工程包https://bbs.21ic.com/icview-3309340-1-1.html
我到时会把包附在后面。
二、在ubuntu下面新建一个目录cw32l031_uart,把工程包拷进这个目录,同时赋于文件所有的读写权限:chmod 777 -R ./CW32l031_GCC
三、用vscode打开CW32l031_GCC文件夹,并把文件夹添加到工种区。
四、在Core目录下面新建User文件夹,文件夹下新建user_uart.c/user_uar.h。同时赋予User及以下的包的有文件的读写权限。目录结构如下:
e00e9af794248dc32387f8567d199477
五、借鉴官方示例log,编写user_uart.c如下:
  1. #include "user_uart.h"

  2. static void SerialInit(uint32_t BaudRate);

  3. static void SerialSend(uint8_t Data);

  4. static uint8_t const pow2_table[] = {0, 1, 2, 3, 4, 5, 6, 7};

  5. void LogInit(void)

  6. {

  7. SerialInit(LOG_SERIAL_BPS);

  8. }

  9. static void SerialInit(uint32_t BaudRate)

  10. {

  11. uint32_t PCLK_Freq;

  12. GPIO_InitTypeDef GPIO_InitStructure = {0};

  13. USART_InitTypeDef USART_InitStructure = {0};

  14. PCLK_Freq = SystemCoreClock >> pow2_table[CW_SYSCTRL->CR0_f.HCLKPRS];

  15. PCLK_Freq >>= pow2_table[CW_SYSCTRL->CR0_f.PCLKPRS];

  16. // 调试串口使用UART1

  17. // PA8->TX

  18. // PA9<-RX

  19. // 时钟使能

  20. __RCC_GPIOA_CLK_ENABLE();

  21. __RCC_UART1_CLK_ENABLE();

  22. // 先设置UART TX RX 复用,后设置GPIO的属性,避免口线上出现毛刺

  23. PA08_AFx_UART1TXD();

  24. PA09_AFx_UART1RXD();

  25. GPIO_InitStructure.Pins = GPIO_PIN_8;

  26. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;

  27. GPIO_Init(CW_GPIOA, &GPIO_InitStructure);

  28. GPIO_InitStructure.Pins = GPIO_PIN_9;

  29. GPIO_InitStructure.Mode = GPIO_MODE_INPUT;

  30. GPIO_Init(CW_GPIOA, &GPIO_InitStructure);

  31. USART_InitStructure.USART_BaudRate = BaudRate;

  32. USART_InitStructure.USART_Over = USART_Over_16;

  33. USART_InitStructure.USART_Source = USART_Source_PCLK;

  34. USART_InitStructure.USART_UclkFreq = PCLK_Freq;

  35. USART_InitStructure.USART_StartBit = USART_StartBit_FE;

  36. USART_InitStructure.USART_StopBits = USART_StopBits_1;

  37. USART_InitStructure.USART_Parity = USART_Parity_No;

  38. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  39. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  40. USART_Init(CW_UART1, &USART_InitStructure);

  41. }

  42. static void SerialSend(uint8_t Data)

  43. {

  44. USART_SendData_8bit(CW_UART1, Data);

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

  46. }

  47. int _write (int fd, char *pBuffer, int size)

  48. {

  49. for (int i = 0; i < size; i++)

  50. {

  51. SerialSend((uint8_t)pBuffer[i]);

  52. }

  53. return size;

  54. }

【代码解释】在log.c中,我们是基于mdk的printf函数重定向,在gcc工程下面编译是不会报错,但是是不会向串口输出的,所以要修改_write函数。
六、添加User目录到Core.mk中:
13df3b1ed00afe9286d73b4295e2a910
七、编译与下载,我们执行make flash就可实现工程编译与下载:
1317741d5be319d350736ab5549dd243
八、效果展示:
PA8PA9分别接到USB转TTL,打开串口调度助手,就可以实现hello cw32l031的欢迎信息了:
5c0969a1bf9b8e3b45ceca09f27f44aa
【小结】使用ubuntu下的vscode+gcc进行CW32L031开发,相比MDK\IAR,是一款免费的开发板工具,同时相比MDK编译等速度上又有质的飞跃!

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

本版积分规则

180

主题

830

帖子

12

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

180

主题

830

帖子

12

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