GD32F303移植FreeRTOS-Plus-CLI
FreeRTOS移植好是没有命令行交互的,刚好系统提供了相关的代码,那么就方便多了。
示例基于FreeRTOS-Kernel-9.0.0.zip,在此之前需要把系统移植完毕。
移植FreeRTOS-Plus-CLI需要干好几件事
- 串口初始化
- 串口终端服务函数
- mian添加初始化
- 添加自己定制的命令
下面讲添加哪些文件
新建一个分组,放CLI相关代码
添加如下文件
下面讲改动哪些文件
- 串口初始化,将图片所示位置注释掉,换成自己GD32F303标准库代码,使用中断,未使用DMA
串口初始化
void UART1_Init(unsigned int baudrate)
{
/* enable GPIO clock */
rcu_periph_clock_enable(RCU_GPIOD);
/* enable USART clock */
rcu_periph_clock_enable(RCU_USART1);
rcu_periph_clock_enable(RCU_AF);
gpio_pin_remap_config(GPIO_USART1_REMAP,ENABLE);
/* connect port to USARTx_Tx */
gpio_init(GPIOD, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
/* connect port to USARTx_Rx */
gpio_init(GPIOD, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
/* USART configure */
usart_deinit(USART1);
usart_baudrate_set(USART1, baudrate);
usart_word_length_set(USART1, USART_WL_8BIT); //设置数据位
usart_parity_config(USART1, USART_PM_NONE); //设置奇偶校验位
usart_hardware_flow_rts_config(USART1, USART_RTS_DISABLE);
usart_hardware_flow_cts_config(USART1, USART_CTS_DISABLE); //无硬件数据流控制
usart_receive_config(USART1, USART_RECEIVE_ENABLE);
usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);
nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
nvic_irq_enable(USART1_IRQn, 15, 0);//配置NVIC分组,初始化NVIC分组4
usart_interrupt_enable(USART1, USART_INT_RBNE);//使能接收中断
usart_enable(USART1);
}
这里需要特别注意!!nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
关于这里,请看FreeRTOS串口中断优先级设置
- 更改中断服务函数,这里需要将serial.c中所有关于串口的函数改为GD32标准库函数
中断服务函数
void USART1_IRQHandler( void )// vUARTInterruptHandler( void )
{
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
char cChar;
if( usart_interrupt_flag_get( USART1, USART_INT_FLAG_TBE ) == SET )
{
/* The interrupt was caused by the THR becoming empty. Are there any
more characters to transmit? */
if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
{
/* A character was retrieved from the queue so can be sent to the
THR now. */
usart_data_transmit( USART1, cChar );
while(RESET == usart_flag_get(USART1, USART_FLAG_TBE));
}
else
{
usart_interrupt_disable( USART1, USART_INT_TBE );
}
}
if( usart_interrupt_flag_get( USART1, USART_INT_FLAG_RBNE ) == SET )
{
cChar = usart_data_receive( USART1 );
xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
}
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
}
- mn添加初始化,下面代码一定要在启动调度,开始执行任务前
vTaskStartScheduler();
main添加初始化
vRegisterSampleCLICommands();
vUARTCommandConsoleStart( 512, 1 );
- 添加自己定制的命令,添加一个kuawo的命令,输入kuawo,系统会回复一个
大帅哥
定制的命令
static const CLI_Command_Definition_t xKuaWo =
{
"kuawo", /* The command string to type. */
"\r\nkuawo:\r\n can kua kua ni\r\n",
prvKuaWoCommand, /* The function to run. */
0 /* No parameters are expected. */
};
static BaseType_t prvKuaWoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
{
( void ) pcCommandString;
( void ) xWriteBufferLen;
configASSERT( pcWriteBuffer );
sprintf( pcWriteBuffer, "you are da shuai ge\r\n");
/* There is no more data to return after this single string, so return
pdFALSE. */
return pdFALSE;
}
看看实际上效果,输入help
,出现kuawo
命令,手动输入kuawo
,出现你是一个大帅哥,搞定