1、安装RT-Thread keil Pack
或者离线下载安装:
https://www.rt-thread.org/download/mdk/RealThread.RT-Thread.3.1.5.pack
2、在工程中勾选RTOS中的kernel 和 shell
3、编译报错
4、配置时钟#error “TODO 1: OS Tick Configuration.”
// #error "TODO 1: OS Tick Configuration."
SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
NVIC_SetPriority(SysTick_IRQn, 0x00U);
void SysTick_Handler(void)
{
void rt_os_tick_callback(void);
rt_os_tick_callback();
}
5、配置串口#error “TODO 2: Enable the hardware uart and config baudrate.”
//#error "TODO 2: Enable the hardware uart and config baudrate."
extern void SerialInit(void);
SerialInit();
void SerialInit(void)
{
/* enable COM GPIO clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* enable USART clock */
rcu_periph_clock_enable(RCU_USART0);
/* connect port to USART TX */
gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_9);
/* connect port to USART RX */
gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_10);
/* configure USART TX as alternate function push-pull */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_10);
/* configure USART RX as alternate function push-pull */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_10);
/* USART configure */
usart_deinit(USART0);
usart_word_length_set(USART0, USART_WL_8BIT);
usart_stop_bit_set(USART0, USART_STB_1BIT);
usart_parity_config(USART0, USART_PM_NONE);
usart_baudrate_set(USART0, 115200U);
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
usart_enable(USART0);
}
6、串口输出#error “TODO 3: Output the string ‘str’ through the uart.”
//#error "TODO 3: Output the string 'str' through the uart."
extern void _rt_hw_console_output( const char *str );
_rt_hw_console_output(str);
void _rt_hw_console_output( const char *str )
{
rt_size_t i = 0, size = 0;
char a = '\r';
size = rt_strlen(str);
for (i = 0; i < size; i++)
{
if (*(str + i) == '\n')
{
usart_data_transmit(USART0, (uint32_t )a);
while((usart_flag_get(USART0, USART_FLAG_TC) == RESET));
}
usart_data_transmit(USART0, (uint32_t)*(str + i));
while((usart_flag_get(USART0, USART_FLAG_TC) == RESET));
}
}
7、串口输入#error “TODO 4: Read a char from the uart and assign it to ‘ch’.”
//#error "TODO 4: Read a char from the uart and assign it to 'ch'."
extern char _rt_hw_console_getchar(void);
ch= _rt_hw_console_getchar();
char _rt_hw_console_getchar(void)
{
/* the initial value of ch must < 0 */
int ch = -1;
if (usart_flag_get(USART0, USART_FLAG_RBNE) != RESET)
{
ch = usart_data_receive(USART0);
}
else
{
rt_thread_mdelay(10);
}
return ch;
}
8、PendSV_Handler和HardFault_Handler重复定义
PendSV_Handler
和HardFault_Handler
两个中断,RT-Thread系统处理,屏蔽掉gd32l23x_it.c中的即可。
9、finsh_config.h未包含
在rtconfig.h中包含finsh_config.h。
10、创建led线程
static rt_thread_t led_thread = RT_NULL;
void LedInit(void)
{
/* enable the LED GPIO clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* configure LED GPIO pin */
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_7 | GPIO_PIN_8);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7 | GPIO_PIN_8);
/* reset LED GPIO pin */
gpio_bit_reset(GPIOA, GPIO_PIN_7 | GPIO_PIN_8);
}
static void led_thread_entry(void *parameter)
{
while (1)
{
gpio_bit_toggle(GPIOA, GPIO_PIN_7|GPIO_PIN_8);
rt_thread_mdelay(1000);
rt_kprintf("led blink\r\n");
}
}
void LedThreadStart(void)
{
led_thread = rt_thread_create( "led", /*线程名字*/
led_thread_entry,/*线程入口函数*/
RT_NULL,/*线程入口函数参数*/
256, /*线程栈大小*/
4 , /*线程优先级*/
20); /*线程时间片*/
rt_thread_startup (led_thread);
}
11、硬件初始化
在rt_hw_board_init函数中包含led初始化函数。
12、在main函数中调用启动led线程
int main(void)
{
LedThreadStart();
return 0;
}
13、现象