Porting RT thread to GD32F150 Series MCU

GD32F150C6T6 MCU is used in this paper. The basic resources are Flash: 32KB and SRAM: 6KB.

1. Download the source code of RT thread nano as follows:

https://www.rt-thread.org/page/download.html

2. Add the RT thread folder under the project directory as follows:

3. Unzip the source code of the downloaded RT thread nano, and then copy it to the RT thread folder, as follows:

4. Add the kernel code to the project as follows:


5. Add the cpu related code to the project as follows:

GD32F130 belongs to ARM cortex m3 kernel and is developed with keil,

6. Add board level support files as follows:


The system clock is mainly configured in the board.c file to make the kernel timer systick, with a clock beat of 1ms by default.

7. Project add RTT related header file path

8. Compilation and error reporting, "RTE_ The "components. H" file cannot be found, as follows:


It can be solved by shielding:

9. Compile again, report an error, PendSV_Handler and HardFault_Handler duplicate definition:


It can be seen that the two interrupts are in the context_rvds and gd32f1x0_ Duplicate definitions in it, delete gd32f1x0_it file.

10. Enable LED flashing

(1) Hardware initialization

void LedInit(void)
{
	rcu_periph_clock_enable(RCU_GPIOF);
	/* configure led GPIO port */ 
	gpio_mode_set(GPIOF, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,GPIO_PIN_6|GPIO_PIN_7);
	gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6|GPIO_PIN_7);
}

(2) Create a thread (task) and start it

void LedThreadStart(void)
{
//Thread creation, using static mode
	rt_thread_init(&led_thread,
                   "led",
                   led_thread_entry,
                   RT_NULL,
                   &led_thread_stack[0],
                   sizeof(led_thread_stack),
                   RT_THREAD_PRIORITY_MAX - 3,
                   32);			
	/* Start thread, start scheduling */
	rt_thread_startup(&led_thread);
}

(3) Thread main body to realize LED flashing

void led_thread_entry(void *parameter)
{
	while (1)
	{
		gpio_bit_reset(GPIOF, GPIO_PIN_6|GPIO_PIN_7);
		rt_thread_delay(500);   /* Delay 500 tick s */
	
		gpio_bit_set(GPIOF, GPIO_PIN_6|GPIO_PIN_7);
		rt_thread_delay(500);   /* Delay 500 tick s */		 		
	}
}

(4) Call LED initialization function during system initialization

void rt_hw_board_init()
{
    /* System Clock Update */
    SystemCoreClockUpdate();
	
	extern	void LedInit(void);	
	LedInit();	
    
    /* System Tick Configuration */
    _SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);

    /* Call components board initial (use INIT_BOARD_EXPORT()) */
#ifdef RT_USING_COMPONENTS_INIT
    rt_components_board_init();
#endif

#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
    rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
#endif
}

(5) call the LED thread to create and start functions in the face function.

int main(void)
{		   
    LedThreadStart();
	return 0;
}

11. Realize serial port printout

(1) Enable RT in rtconfig.h_ USING_ Console macro definition

(2) Serial port hardware initialization

void HandCommInit(void)
{
	rcu_periph_clock_enable( RCU_GPIOA);
    rcu_periph_clock_enable( RCU_USART1);

    /* connect port to USARTx_Tx */
    gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_2);

    /* connect port to USARTx_Rx */
    gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_3);

    /* configure USART Tx as alternate function push-pull */
    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_2);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ,GPIO_PIN_2);

    /* configure USART Rx as alternate function push-pull */
    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_3);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ,GPIO_PIN_3);

    /* USART configure */
    usart_deinit(USART1);
    usart_baudrate_set(USART1,115200U);
    usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);
    usart_receive_config(USART1, USART_RECEIVE_ENABLE);
    usart_enable(USART1);
}

(3) Implement the system console output function
It is given in the form of virtual function in the system, and the user can re implement it:

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(USART1, (uint32_t )a);
            while((usart_flag_get(USART1, USART_FLAG_TC) == RESET));
        }
        usart_data_transmit(USART1, (uint32_t)*(str + i));
        while((usart_flag_get(USART1, USART_FLAG_TC) == RESET));
    }
}

(4) calling initialization function in the system initialization function.

void rt_hw_board_init()
{
    /* System Clock Update */
    SystemCoreClockUpdate();
	
	extern	void LedInit(void);	
	LedInit();	
	extern void  HandCommInit(void);
	HandCommInit();
	
    /* System Tick Configuration */
    _SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);

    /* Call components board initial (use INIT_BOARD_EXPORT()) */
#ifdef RT_USING_COMPONENTS_INIT
    rt_components_board_init();
#endif

#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
    rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
#endif
}

12. Migrate fish components (implement command input)

(1) Add fish component code to the project


(2) Include #include "fish_config. H" in rtconfig.h

(3) Implement char rt_hw_console_getchar(void) function
rt_ hw_ console_ The getchar system is given in the form of virtual function, and the precompiled error message is added to prompt the user to realize character input.

Remove the precompiled error information in the original virtual function and re implement it.

char rt_hw_console_getchar(void)
{
    /* the initial value of ch must < 0 */
    int ch = -1;

    if (usart_flag_get(USART1, USART_FLAG_RBNE) != RESET)
    {
        ch = usart_data_receive(USART1);
    }
    else
    {
        rt_thread_mdelay(10);
    }
    return ch;	
}

13. Connect the serial port correctly, use the serial port terminal to open and restart the mcu, and the following printing information will appear:

Enter help to view supported commands, ps to view threads, etc.

Space constraints, the article only lists part of the code, complete code engineering download.

Tags: IoT rt-thread MCU gd32

Posted on Fri, 15 Oct 2021 04:43:31 -0400 by arya6000