current position:Home>Application of RT thread - use RT thread on stm32l051 (v. conclusion of wireless temperature and humidity sensor)

Application of RT thread - use RT thread on stm32l051 (v. conclusion of wireless temperature and humidity sensor)

2022-04-29 09:54:09Due to Jin Chen

 Application articles - stay STM32L051 Upper use RT-Thread  Chapter five , It is also the end of this application .
 Transplant the key driver , Timer use plus .

Preface

The sensor acquisition is basically completed , Serial port communication is transplanted and modified , In fact, as a single product, the sensor has been working normally , But if we have special circumstances when using the product , For example, you need to send a specific wireless message , Or set different working modes , We can make certain configuration through key operation .

So this article will transplant a key driver , Able to support keys Short press , Long press for a fixed time , The program executed periodically at the same time is changed to use timer to control .

This article is about 《RT-Thread Application articles — stay STM32L051 Upper use RT-Thread》 Conclusion

A simple application : Wireless temperature and humidity sensor

A chip with a small memory :STM32L051C8T6

A small and beautiful Internet of things operating system :RT-Thread

stay STM32L051C8 Upper use RT-Thread Apply a series of blog posts to connect :
RT-Thread Application articles — stay STM32L051 Upper use RT-Thread ( One 、 Wireless temperature and humidity sensor And New projects )
RT-Thread Application articles — stay STM32L051 Upper use RT-Thread ( Two 、 Wireless temperature and humidity sensor And CubeMX To configure )
RT-Thread Application articles — stay STM32L051 Upper use RT-Thread ( 3、 ... and 、 Wireless temperature and humidity sensor And I2C Communications )
RT-Thread Application articles — stay STM32L051 Upper use RT-Thread ( Four 、 Wireless temperature and humidity sensor And A serial port communication )

One 、 Key operation

In the preliminary test framework , I used a simple key handling method , Here is also a test backup of the source code :

static void key_thread_entry(void *par){
    
    while(1){
    
        if(key1_read == 0){
    
            rt_thread_mdelay(10); // To shake 
            if(key1_read == 0){
    
                // Press the key ,do something
                while(key1_read == 0){
    rt_thread_mdelay(10);}
            }
        }
        if(key2_read == 0){
    
            rt_thread_mdelay(10); // To shake 
            if(key2_read == 0){
    
                // Press the key ,do something
                while(key2_read == 0){
    rt_thread_mdelay(10);}
            }
        }
        rt_thread_mdelay(1);
    }
}

But in practice , If you want to achieve long press , Combine in such a way , Still need a suitable key driver .

1.1 Key driven migration

Key driven It's the one I've been using , I uploaded it before my blog ,.c.h The files are open source :

Several practical key drivers (STM32、51 All applicable )

The same as usual , Put the file under our own driver folder , Then right-click , Sync scons Configure to project :
 Insert picture description here

Because the source code of this driver is given to , Transplantation and replacement are not troublesome , So take a simple look :
 Insert picture description here

1.2 Key test

I also said in my blog about how to use the buttons , In this application, only 3 Middle button mode :
 Insert picture description here

Let's look directly at the test results , The key is normal :
 Insert picture description here

Key transplantation succeeded , Because all of our keystroke calls use the start thread that has been created , Therefore, compared with the previous additional memory occupation, there is no , There is only one global variable Timer21_count, Here's the picture :

 Insert picture description here
Details : unsigned int A few bytes may be different in different compilers or platforms , We just need a simple test :
 Insert picture description here

Two 、 Timer operation

Although the key also uses a timer , But that timer is specially used for key driver , We also initialized a timing time at the beginning 1S Timer for , It was tested at first .

When using bare metal , It takes a certain period of time to carry out a certain work , We can only use timers , And used the operating system , The effect can also be achieved by directly using the delay function of the operating system in a separate thread .

In general applications , When the timing is not so strict , It can be used directly rt_thread_mdelay, For example, the original cycle processing :
 Insert picture description here

But more complex applications , All use rt_thread_mdelay, It's inconvenient to manage more threads , If you use a timer with a semaphore ( In bare metal, it's a global variable ) That's a more standardized way .

2.1 Timer logic added

We need to create a counting global variable for the timer , Then, at a certain time, notify the thread that needs to execute the task , This informs us to create a new semaphore implementation ( Although notifications can also use global variables ), But now that it's used RT-Thread , Just put IPC The mechanism works , This ensures good habits and lays a good foundation for bigger applications in the future .

Our operation is shown in the figure below :
 Insert picture description here

So our original cycle processing code is changed to the following :
 Insert picture description here

2.2 Timer test

The test is nothing , The message received by serial communication is also normal , The keys and timers changed today are also normal .

Finally, all functions and results are normal :
 Insert picture description here

3、 ... and 、 Always pay attention to accounting RAM size

Today's work is finished , This application is over , Finally, let's take a look at the occupation RAM Size :

 Insert picture description here

Final procedure , When running, it needs to occupy RAM Size : 7456 byte , Our chips RAM:8192 byte .

The final size description

from the beginning , Because we use it on a chip with small memory RT-Thread , So every time the code is changed, the blogger will record the memory usage .

The final program we need to use is recorded above RAM The size is 7456 byte , In fact, there is still some optimization space for the memory occupation of this application , For example, the main thread 2K You can still reduce it a little , Keystroke threads don't actually need 512 Bytes and so on ...

But because this application is really simple , And the memory didn't overflow , Therefore, there is no bottleneck and no special effort to deal with the appropriate size of each thread .

In fact, in the blog , Bloggers often explain some details that affect memory , I hope all my friends who have read my blog will understand .

Conclusion

Bloggers hope that everyone can master the memory of their own programs .️

Master your own memory , First of all, we should understand how data is stored on the chip .

Using the operating system , Also understand the relationship between task stack and system stack . For each thread , You need to know which operations need to occupy memory .

For basic programming , You have to understand Different locations of modifier variables .

In fact, all these bloggers have explained in their blog posts , I believe that the little partners who pay attention to the blogger will gain something !


A simple application : Wireless temperature and humidity sensor

A chip with a small memory :STM32L051C8T6

A small and beautiful Internet of things operating system :RT-Thread

Okay , As of this article , We 《RT-Thread Application articles — stay STM32L051 Upper use RT-Thread》 Even if the series is over .

I hope you can give me more support , Your smile !

thank you !

copyright notice
author[Due to Jin Chen],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204290810437192.html

Random recommended