[RT-Thread] First experience of rtt – project template + create thread + FINSH command

generalize

This chapter introduces the rtt project template based on the firmware library, and realizes the creation of threads. Use the FINSH command statement to view the information of thread tasks.

Add rtt to firmware library project template

We must first have a project template for the firmware library. For details, please refer to this blogger’s article: Firmware Library Project Template
Compared with the firmware library project, our rtt project only adds one folder:
let’s take a look at adding What’s in the rtt folder:
these are the files we need, we need to find these folders from the rtt downloaded from the official website, and then add them to our project.
We download this file from the official website:
this is the kernel of rtt, and only this kernel is enough for us to use the microcontroller to develop.
We opened the folder and found that there are many files in it:
find the files we mentioned above and add them to the project.
We also need two particularly important files in the bsp folder:
copy these two files directly to the USER file of our project:
here we also need to add a board.h file by ourselves.
Now we open the project, add three groups in the project directory and add new files in USER: The
rtt/source group contains the source files of rtt, just in the src folder we added, the files in this folder All are added to this group: The
rtt/ports group is the interface function to connect to our MCU architecture. Let’s open the libcup folder first:
The blogger uses stm32 MCU, so it is an arm architecture:
I believe everyone will understand after seeing this, we Just add the series corresponding to our microcontroller, here the blogger chooses m3:

The above three assembly files correspond to different compilation software. The blogger uses keil software, so only the last two files need to be added: The
rtt/finsh group is to add the finsh command language function file, and we find the finsh file in the components folder. , add the following files to the group:
then add the corresponding header file of rtt in the hammer:
the following is the configuration of rtt, let’s configure the board.h file first:
we can add all user files here in the future, and then It’s the board.c file:
we changed it so that all peripheral initializations are here in the future.
To open the FINSH command statement, we only need to define a macro:
re-create two functions at the end of the board.c file:

void rt_hw_console_output( const char *str )
{
    rt_enter_critical();

    while ( *str != '
void rt_hw_console_output( const char *str )
{
rt_enter_critical();
while ( *str != '\0' )
{      
if ( *str == '\n' )
{
USART_SendData( DEBUG_USARTx, '\r' );
while ( USART_GetFlagStatus( DEBUG_USARTx, USART_FLAG_TXE ) == RESET );
}
USART_SendData( DEBUG_USARTx, *str++ );
while ( USART_GetFlagStatus( DEBUG_USARTx, USART_FLAG_TXE ) == RESET );
}
rt_exit_critical();
}
char rt_hw_console_getchar( void )
{
int ch = -1;
if( USART_GetFlagStatus( DEBUG_USARTx, USART_FLAG_RXNE ) != RESET )
{
ch = (int)USART_ReceiveData( DEBUG_USARTx );
USART_ClearFlag( DEBUG_USARTx, USART_FLAG_RXNE );
}
else
{
if( USART_GetFlagStatus( DEBUG_USARTx, USART_FLAG_ORE ) != RESET )
{
USART_ClearFlag( DEBUG_USARTx, USART_FLAG_TC );
}
rt_thread_mdelay(10);
}
return ch;
}
' ) { if ( *str == '\n' ) { USART_SendData( DEBUG_USARTx, '\r' ); while ( USART_GetFlagStatus( DEBUG_USARTx, USART_FLAG_TXE ) == RESET ); } USART_SendData( DEBUG_USARTx, *str++ ); while ( USART_GetFlagStatus( DEBUG_USARTx, USART_FLAG_TXE ) == RESET ); } rt_exit_critical(); } char rt_hw_console_getchar( void ) { int ch = -1; if( USART_GetFlagStatus( DEBUG_USARTx, USART_FLAG_RXNE ) != RESET ) { ch = (int)USART_ReceiveData( DEBUG_USARTx ); USART_ClearFlag( DEBUG_USARTx, USART_FLAG_RXNE ); } else { if( USART_GetFlagStatus( DEBUG_USARTx, USART_FLAG_ORE ) != RESET ) { USART_ClearFlag( DEBUG_USARTx, USART_FLAG_TC ); } rt_thread_mdelay(10); } return ch; }

Note: the serial port should be changed to its own serial port name
main.c file content:

/* Included header files */  
# include  "board.h"

/* Static configuration of led_thread thread*/ 
static  struct  rt_thread  led_thread ;
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t rt_led_thread_stack[256];
static void led_thread_entry(void* parameter);

/* Static configuration of print_thread thread*/ 
static  struct  rt_thread  print_thread ;
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t rt_print_thread_stack[256];
static void print_thread_entry(void* parameter);

/* function declaration */ 
static  void  led_thread_entry ( void * parameter )
 {
      while ( 1 )
     {
         LED(1);
         rt_thread_mdelay(500); 

         LED(0);
         rt_thread_mdelay(500); 
     }
}

static void print_thread_entry(void* parameter)
{
     while (1)
     {
         rt_kprintf("led on!\r\n");
         rt_thread_mdelay(600); 

         rt_kprintf("led off!\r\n");
         rt_thread_mdelay(600); 
     }
}

/* submit a task */ 
void  test ( void )
 {
    rt_kprintf("hello RT-Thread!\n");
}
MSH_CMD_EXPORT(test , it is a test);

/* main function */ 
int  main ( void )
 {  
    rt_thread_init(&led_thread, 
                         "led",
                         led_thread_entry, 
                         RT_NULL, 
                         &rt_led_thread_stack[0], 
                         sizeof(rt_led_thread_stack), 
                         3, 
                         20); 
    rt_thread_startup(&led_thread); 

    rt_thread_init(&print_thread, 
                         "print",
                         print_thread_entry, 
                         RT_NULL, 
                         &rt_print_thread_stack[0], 
                         sizeof(rt_print_thread_stack), 
                         5, 
                         20); 
     rt_thread_startup(&led_thread); 
}

/********************************END OF FILE****************************/

Software simulation configuration:
Start simulation:
View thread information:
Submit task:
At this point, the addition and implementation of rtt is completed. This article is just to let you feel rtt. Several important file introductions about rtt and thread creation bloggers will Introduced in future articles in this column.
Example attached project template link: rtt project template

Leave a Comment

Your email address will not be published. Required fields are marked *