RT thread learning record 3: a simple thread example -- horse running lamp experiment

1 thread state transition diagram

The initial state of the thread is not running, when RT is called_ thread_ After startup(), the thread is in ready state. The system determines which thread runs according to the priority of ready state,

The running state returns to the ready state after execution.

When the running state needs to share resources, the function in Figure 1 is called and becomes suspended (also known as blocking state).

Run state call RT_ thread_ It turns off after exit().

The operation of the system is transformed among the states in the figure, mainly among the ready state, the running state and the pending state.

When a thread does not need to run, you can call rt_thread_exit() is in the closed state.

Every operating system has a "system heartbeat" clock, which is the smallest clock unit in the operating system. This clock is responsible for some time related operations of the system. As the time scale of operating system, heartbeat clock is generated by the timing interrupt of hardware timer.

The heartbeat clock of the system is often called system tick or clock beat. The frequency of system tick needs to be determined according to the processing capacity of the cpu.

Clock ticks enable the kernel to delay several integer clock ticks of a thread, and provide the basis for waiting timeout when a thread waits for an event.

The faster the frequency is, the more likely the kernel function will intervene in the system operation, the longer the processor time occupied by the kernel, and the greater the system load;

The smaller the frequency is, the less accurate the time processing is;

We usually set the system tick frequency to 100HZ on stm32 platform, that is, the time of each tick is 10ms

In the code board.c, there is the STM32 hardware timer configuration function as shown in the figure below. The configuration clock is 1 second, 100 time hops = 100 Hz, and one clock beat is 10 milliseconds.

Using the delay function RT in the program_ thread_ Mdelay (Num), delay num milliseconds. It's done by system ticking

3 creation of running lamp

Operation of IO port

3.1 IO initialization
void rt_pin_mode(rt_base_t pin,rt_base_t mode)
mode It can be:
//Select pin, DRV in file Drive_ GPIO. C, if there are 144 MCU pins, select
   #if (STM32F10X_ PIN_ Corresponding name under numbers = = 144)
    __STM32_PIN(1, E, 2),//The first parameter in parentheses represents the above function rt_pin_mode() first parameter pin
    __STM32_PIN(2, E, 3),//Pin PE3 for pin = 2
    __STM32_PIN(3, E, 4),//Pin PE4 for pin = 3
    __STM32_PIN(4, E, 5),
    __STM32_PIN(5, E, 6),

3.2 IO write in
void rt_pin_write(rt_base_t pin,rt_base_t value)
value It can be:
3.3 IO read
 int rt_pin_read(rt_base_t pin)

Create a running lamp thread, I create a dynamic thread, run.

int thread_led_test(void)
    /* Create thread 1, name is led, entry is led_entry*/
    tid1 = rt_thread_create("led",                    //Thread name
                            led_entry,                //Thread entry function
                            10, 10);
    /* If you get the thread control block, start the thread */
    if (tid1 != RT_NULL)          //Judge whether the thread is created successfully
        rt_thread_startup(led);   //If it is created successfully, it will change from initial state to ready state
    return 0;
/* The entry function of thread led to complete the flashing operation of LED */
static void led_entry(void *parameter)
    rt_uint32_t count = 0;
    rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);  //Set LED_PIN pin mode, to include header file 
                                            //#include <rtdevice.h>

    while (1)
        rt_pin_write(LED_PIN, PIN_HIGH);
        rt_kprintf("led on, count : %d\r\n", count);
        rt_thread_delay(500);               //When execution is delayed by 500 ms, other threads are running
        rt_pin_write(LED_PIN, PIN_LOW);
        rt_kprintf("led off\r\n");               
        //rt_thread_delay() delays in clock ticks,
        //rt_thread_mdelay() delays in 1 millisecond,
        //rt_thread_sleep() delays in ticks. All three functions act as a delay
//Calling thread in main function_ led_ Test () will do.

First, set a fixed value (such as 2048) for the thread stack size. When the thread is running, set a reasonable stack size according to the situation by checking the usage of the thread stack.

In general, the maximum thread stack usage is set to 70%.

Through the command list_thread prints information for all threads. In the figure, tshell is the thread where the command line is located, and idle is the idle thread,

pri is the priority of the thread, status is the status of the thread, and sp is the sp pointer of the thread.

stack size is the size allocation of thread stack when creating a thread. max used has used the percentage of thread stack. left tick has clock ticks left.

Tags: less

Posted on Fri, 05 Jun 2020 23:59:37 -0400 by prometheuzz