打印
[开发资料]

【Ubuntu VSCODE+GCC】CW32L031实现printf工程

[复制链接]
104|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
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及以下的包的有文件的读写权限。目录结构如下:

五、借鉴官方示例log,编写user_uart.c如下:
#include "user_uart.h"

static void SerialInit(uint32_t BaudRate);

static void SerialSend(uint8_t Data);

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

void LogInit(void)

{

SerialInit(LOG_SERIAL_BPS);

}

static void SerialInit(uint32_t BaudRate)

{

uint32_t PCLK_Freq;

GPIO_InitTypeDef GPIO_InitStructure = {0};

USART_InitTypeDef USART_InitStructure = {0};

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

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

// 调试串口使用UART1

// PA8->TX

// PA9<-RX

// 时钟使能

__RCC_GPIOA_CLK_ENABLE();

__RCC_UART1_CLK_ENABLE();

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

PA08_AFx_UART1TXD();

PA09_AFx_UART1RXD();

GPIO_InitStructure.Pins = GPIO_PIN_8;

GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_Init(CW_GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.Pins = GPIO_PIN_9;

GPIO_InitStructure.Mode = GPIO_MODE_INPUT;

GPIO_Init(CW_GPIOA, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = BaudRate;

USART_InitStructure.USART_Over = USART_Over_16;

USART_InitStructure.USART_Source = USART_Source_PCLK;

USART_InitStructure.USART_UclkFreq = PCLK_Freq;

USART_InitStructure.USART_StartBit = USART_StartBit_FE;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_InitStructure.USART_Parity = USART_Parity_No;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(CW_UART1, &USART_InitStructure);

}

static void SerialSend(uint8_t Data)

{

USART_SendData_8bit(CW_UART1, Data);

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

}

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

{

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

{

SerialSend((uint8_t)pBuffer[i]);

}

return size;

}

【代码解释】在log.c中,我们是基于mdk的printf函数重定向,在gcc工程下面编译是不会报错,但是是不会向串口输出的,所以要修改_write函数。
六、添加User目录到Core.mk中:

七、编译与下载,我们执行make flash就可实现工程编译与下载:

八、效果展示:
PA8PA9分别接到USB转TTL,打开串口调度助手,就可以实现hello cw32l031的欢迎信息了:

【小结】使用ubuntu下的vscode+gcc进行CW32L031开发,相比MDK\IAR,是一款免费的开发板工具,同时相比MDK编译等速度上又有质的飞跃!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

139

主题

679

帖子

6

粉丝