[活动专区] 【AT-START-F425测评】12、RT-Thread移植到AT32F425

[复制链接]
 楼主| freeelectron 发表于 2022-3-18 10:56 | 显示全部楼层 |阅读模式
本帖最后由 freeelectron 于 2022-3-18 11:02 编辑

1、准备好一个keil 工程

2、点击“Manage Run-Time Environment”
217436233dba16edbd.png

3、在“Manage Run-Time Environment”中勾选kernel和shell
545156233dc4792676.png

4、勾选之后,在工程目录中,可以看到多了RTOS一项,里面是RT-Thread的相关代码
98866233dcfe5470e.png

5、FinSH配置,在rtconfig.h中使能FinSH,可以使用配置向导,也可以使用代码
517766233f636e7511.png
6、控制台使能,在rtconfig.h中使能控制台,可以使用配置向导,也可以使用代码
698746233de5fdf7d6.png

7、”系统时钟节拍配置报错,#error "TODO 1: OS Tick Configuration."
769186233dec179f1e.png

8、配置系统时钟节拍
88616233dfcc3c697.png
在void rt_hw_board_init(void)函数中,设置使用systick为系统时钟。


9、串口初始化报错,“"TODO 2: Enable the hardware uart and config baudrate."”
615746233e17690ba0.png

10、配置串口
625326233e267bc004.png
在uart_init函数中,调用串口初始化的函数即可。
  1. void SerialInit(void)
  2. {
  3.         gpio_init_type gpio_init_struct;
  4.            crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);  

  5. #if 1        
  6.         /* enable the usart1 and gpio clock */
  7.         crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);

  8.         gpio_default_para_init(&gpio_init_struct);

  9.         /* configure the usart1 tx/rx pin */
  10.         gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  11.         gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  12.         gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  13.         gpio_init_struct.gpio_pins = GPIO_PINS_6 | GPIO_PINS_7;
  14.         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  15.         gpio_init(GPIOB, &gpio_init_struct);

  16.         /* config usart1 iomux */
  17.         gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE6, GPIO_MUX_0);
  18.         gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE7, GPIO_MUX_0);
  19. #else

  20.         /* enable the usart1 and gpio clock */
  21.         crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);

  22.         gpio_default_para_init(&gpio_init_struct);

  23.         /* configure the usart1 tx/rx pin */
  24.         gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  25.         gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  26.         gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  27.         gpio_init_struct.gpio_pins = GPIO_PINS_9 ;
  28.         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  29.         gpio_init(GPIOA, &gpio_init_struct);
  30.         
  31.         gpio_init_struct.gpio_pins =  GPIO_PINS_10;
  32.         gpio_init(GPIOA, &gpio_init_struct);

  33.         /* config usart1 iomux */
  34.         gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE9, GPIO_MUX_1);
  35.         gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE10, GPIO_MUX_1);

  36. #endif

  37. /* configure usart1 param */
  38.         usart_init(USART1, 115200, USART_DATA_8BITS, USART_STOP_1_BIT);
  39.         usart_parity_selection_config(USART1, USART_PARITY_NONE);
  40.     usart_hardware_flow_control_set(USART1,USART_HARDWARE_FLOW_NONE);

  41.         usart_transmitter_enable(USART1, TRUE);
  42.         usart_receiver_enable(USART1, TRUE);
  43.         usart_enable(USART1, TRUE);
  44. }


11、串口输出报错,“"TODO 3: Output the string 'str' through the uart."”
991996233e388e9731.png

12、配置串口输出
  1. void rt_hw_console_output(const char *str)
  2. {
  3. //#error "TODO 3: Output the string 'str' through the uart."
  4.         
  5.         rt_size_t i = 0, size = 0;
  6.     char a = '\r';
  7.    
  8.     size = rt_strlen(str);

  9.     for (i = 0; i < size; i++)
  10.     {
  11.         if (*(str + i) == '\n')
  12.         {
  13.             usart_data_transmit(USART1, (uint32_t )a);
  14.             while((usart_flag_get(USART1, USART_TDC_FLAG) == RESET));
  15.         }
  16.         usart_data_transmit(USART1, (uint32_t)*(str + i));
  17.         while((usart_flag_get(USART1, USART_TDC_FLAG) == RESET));
  18.     }
  19. }

13、串口输入报错,“#error "TODO 4: Read a char from the uart and assign it to 'ch'."”
125016233e49973a6a.png

14、配置串口输入
  1. RT_WEAK char rt_hw_console_getchar(void)
  2. {
  3.     /* Note: the initial value of ch must < 0 */

  4. //#error "TODO 4: Read a char from the uart and assign it to 'ch'."

  5.     /* the initial value of ch must < 0 */
  6.     int ch = -1;

  7.     if (usart_flag_get(USART1, USART_RDBF_FLAG) != RESET)
  8.     {
  9.         ch = usart_data_receive(USART1);
  10.     }
  11.     else
  12.     {
  13.         rt_thread_mdelay(10);
  14.     }
  15.     return ch;

  16. }

15、HardFault_Handler和PendSV_Handler重复定义
695336233e5654cd3e.png
在at32f425_int.c中屏蔽掉即可,这两个中断RT-Thread已经处理了。

16、Systick中断回调处理
配置了Systick为系统的时钟,需要需要在Systick中处理系统的回调函数
  1. void SysTick_Handler(void)
  2. {
  3.         void rt_os_tick_callback(void);
  4.         rt_os_tick_callback();
  5. }

17、创建一个led线程
  1. static rt_thread_t led_thread = RT_NULL;


  2. void LedInit(void)
  3. {
  4.         gpio_init_type gpio_init_struct;

  5.         /* enable the led clock */
  6.         crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);

  7.         /* set default parameter */
  8.         gpio_default_para_init(&gpio_init_struct);

  9.         /* configure the led gpio */
  10.         gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  11.         gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  12.         gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
  13.         gpio_init_struct.gpio_pins = GPIO_PINS_2|GPIO_PINS_3|GPIO_PINS_5;
  14.         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  15.         gpio_init(GPIOC, &gpio_init_struct);
  16. }



  17. static void led_thread_entry(void *parameter)
  18. {
  19.     while (1)
  20.     {
  21.                gpio_bits_write(GPIOC, GPIO_PINS_2|GPIO_PINS_3|GPIO_PINS_5, (confirm_state)!gpio_output_data_bit_read(GPIOC, GPIO_PINS_2|GPIO_PINS_3|GPIO_PINS_5));

  22.               rt_thread_mdelay(1000);
  23.               rt_kprintf("led blink\r\n");
  24.     }
  25. }


  26. void LedThreadStart(void)
  27. {
  28.         led_thread = rt_thread_create( "led",     /*线程名字*/                    
  29.                                                                    led_thread_entry,/*线程入口函数*/
  30.                                                                    RT_NULL,/*线程入口函数参数*/
  31.                                                                    256,    /*线程栈大小*/
  32.                                                                    4 ,    /*线程优先级*/
  33.                                                                    20);   /*线程时间片*/
  34.         rt_thread_startup (led_thread);
  35. }

18、硬件初始化

417556233ee46a7d36.png
在rt_hw_board_init函数中包含led初始化函数。

19、在main函数中调用启动led线程
  1. int main(void)
  2. {

  3.         LedThreadStart();

  4.         return 0;
  5. }

20、现象

345926233f3356aee4.gif



豌豆爹 发表于 2022-3-23 10:03 来自手机 | 显示全部楼层
学习学习
bbb641681146 发表于 2022-3-31 10:25 | 显示全部楼层
请问我的KEIL5的manage run time没有 RTOS,  这个是要手动安装吗?具体要怎么操作呢?
94671624510b382410.png
 楼主| freeelectron 发表于 2022-3-31 16:12 | 显示全部楼层
bbb641681146 发表于 2022-3-31 10:25
请问我的KEIL5的manage run time没有 RTOS,  这个是要手动安装吗?具体要怎么操作呢? ...

需要手动安装pack,https://www.rt-thread.org/download/mdk/RealThread.RT-Thread.3.1.5.pack
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:stm32/LoRa物联网:304350312

65

主题

785

帖子

11

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