1 front

Let us briefly review the history of The Internet Of Things.

In 1999, the concept of the Internet of Things was first proposed;

In 2009, the concept of “perceive China” was proposed, and the Internet of Things was officially listed as one of the country’s emerging strategic industries;

In 2013, Semtech LoRa technology was commercially available;

In 2016, the NB-IoT R13 core technology standard was frozen, global operators had a proprietary IoT-based protocol, and narrow-band Internet of Things ushered in commercial scale;

In 2017, the “LPWAN” IoT technology represented by NB-IoT and LoRa has sprung up and developed rapidly. The technical characteristics of LPWAN have made the realization of the Internet of Everything possible.

In 2019, 5G commercial officially kicked off, which will build a highway for the development of the Internet of Things industry….

At the same time, with the development of Internet technology for more than ten years, the maturity and integration of various technologies such as big data, cloud computing, artificial intelligence (machine learning), and IoT communication technology, the vision of the Internet of Things has once again evolved – let everything connect reality.

According to analysis reports from IHS, IDC, and Gartner, the world’s leading information providers, by 2020, the number of connected devices worldwide will exceed 200-300 billion. Faced with such a huge number of connected devices, the Internet of Things has once again become a battlefield. At the same time, in the face of the highly fragmented nature of the Internet of Things, all the big roads have shouted and shouted, creating an Internet of Things ecosystem.

In 2016, at the HUAWEI CONNECT conference, Huawei officially released the OceanConnect IoT platform with the IoT connection management platform as its core.

Beginning in 2017, Tencent Cloud fully deployed the Internet of Things to provide industry customers with the basic services to accelerate the Internet of Things +, including: basic cloud services, intelligent AI big data services, and IoT PaaS services.

At the end of 2017, Huawei identified a new vision closely related to IoT: “Bring the digital world to everyone, every family, every organization, and build a smart world of Internet of Everything”.

2018 Yunqi Conference·Shenzhen Summit Alibaba President Hu Xiaoming announced: Alibaba will fully enter the field of Internet of Things , IoT is Alibaba Group’s new main track after e-commerce, finance, logistics and cloud computing.

In March 2018, Alibaba Cloud released the LinkWAN platform that supports the LoRa protocol.

In July 2019, Tencent released a one-stop IoT development platform: Tencent Cloud IoT Explorer, or will remove the last threshold for the explosive growth of IoT applications.

As the basic software of the IoT terminal side, the Internet of Things RTOS can crack the fragmentation dilemma downstream of the Internet of Things to a certain extent, speed up the development process of downstream IoT applications, and the massive outbreak and popularization of IoT applications will also drive the chip\storage. The cost continues to decrease, just like the IoT WiFi chip in the early years, the current IoT BLE chip, after the scale effect, the cost of the single chip has already reached the bottom, and the cost pressure will become less sensitive, which will further release the Internet of Things. Hardware resource bottleneck on the terminal side. All the way to the layout of the Internet of Things RTOS, card data entry, making the RTOS plug in the wings of the Internet of Things, once again take off in the era of the Internet of Things…

Next, let’s take a look at the new moon of the Internet of Things RTOS – TencentOS tiny

1.1 IoT RTOS upstart TencentOS tiny

As a rising star of IoT RTOS, TencentOS has several typical features of the Internet of Things RTOS:

  • Expanded chip platform
  • Hard real-time kernel, multitasking, IPC communication…
  • Small FLASH\RAM overhead
  • Low power consumption
  • Multiple networking and cloud capabilities
  • Safety ability
  • Rich components

Here’s how to implement the first TencentOS tiny application based on MDK and STM32L4 – the serial output “Hello World”, as the first milestone in the introduction of TencentOS tiny, open the door to TencentOS tiny knowledge.

PS: This code is based on the TencentOS tiny 1.0 version (2019.09.18 officially open source version)

1.2 Executive summary

The following topics cover the introduction of TencentOS tiny, the MDK-based development environment, the TencentOS tiny kernel startup process, the main entry and application layer code entry, and the helloworld application examples.

  • TencentOS tiny entry knowledge
    • TencentOS tiny system architecture
    • TencentOS tiny source code directory tree
  • MDK-based development environment to build
  • MDK-based TencentOS tiny startup process
    • TencentOS tiny main function entry
    • Application entry
  • TencentOS tiny first application helloworld

2 TencentOS tiny entry knowledge

2.1 TencentOS tiny system architecture

TencentOS_tiny_Start_0.png

The TencentOS tiny system architecture conforms to the hierarchical architecture design and component architecture design principles. TencentOS tiny architecture down to (layer) and top (layer), respectively

TencentOS_tiny_Start_1.png

  • 2.2 TencentOS tiny folder directory tree

The TencentOS tiny system architecture is embodied in the source code file organization, as shown in the following figure.

TencentOS tiny source code level folder directory tree

TencentOS_tiny_Start_3.png
TencentOS_tiny_Start_4.png
TencentOS_tiny_Start_5.png
TencentOS_tiny_Start_6.png
TencentOS_tiny_Start_7.png
TencentOS_tiny_Start_8.png

3 development environment to build

The development environment depends mainly on the hardware chip platform used and personal development habits.

TencentOS tiny currently supports Cortex-M and Risc-V 32-bit MCUs. Currently, BSP provides KEIL (MDK) sample projects, and some also provide IAR and GCC. For the development of Cortex-M products in Windows. Little friends will be more familiar.

For the development environment, simply compare the other two very popular IoT RTOS in China:

  • RT-Thread4.0.x will be more complete. It provides Env tools based on Python & Scons and does not need to be installed. By simply modifying Kconfig and SConscript files, MDK and IAR projects can be generated very quickly. It is very efficient, and RT-Thread has a long history of development, and there are many BSPs that have accumulated support.
  • AliOS Things2.1.x also provides AOS-Cube tools based on Python & Scons, which can quickly generate MDK and IAR projects, but currently there are not many BSPs supported, and they are still being improved.

3.1 Based on MDK and STM32L4 development environment

See if TencentOS tiny\board has a sample project similar to the target main chip.

  1. If there is a similar platform, copy the sample project directly as the project prototype, and then reconfigure according to the actual hardware, the migration will be relatively simple and efficient. For example, the target chip of this platform is STM32L476VGT6, STM32 can share the same underlying code with the same series of chips, so modify TencentOS tiny\board\NUCLEO_STM32L476RG directly here.
  2. If not, you need to refer to the official migration documentation, as described in the “6 References” section.

4 MDK-based TOS kernel startup process

In the embedded system (MCU), the startup file (such as startup_stm32l476xx.s) is the first program running on the chip, which is a very important part of the whole system software. If the startup file has an error, the whole system will not run. .

4.1 MCU startup process

After the MCU is powered on, the startup file will automatically initialize the underlying MCU chip and build the necessary environment for the program to run, including:

  • Stack space definition and initialization
    • Variable initialization
    • Application and definition interrupt vector table
    • Define the reset interrupt function (Reset_Handler)
  • Use the SystemInit() function to initialize the various clock and vector table offsets of the system, then call (jump to) the __main() function.
    • __main() call library function initialization stack
  • Other interrupt exception service function definitions (weak [WEAK] declaration)

Usually, the chip factory provides MDK corresponding kernel chip (such as ST STM32L4xx…) startup file, the startup file is written by the assembler, generally named startup_xxx.s, xxx is the supported chip model, such as startup_stm32l476xx.s.

Based on MDK development, it is generally possible to directly use the startup file provided by the chip manufacturer, and does not need to involve modification of the startup file.

4.2 Kernel boot process

Through the “4.1 MCU startup process”, the MCU is ready to start from the system power-on to the system to enter the application main code (main function). The next step is to start the TencentOS tiny kernel.

The following is a description of the development board example of TencentOS tiny and board\NUCLEO_STM32L476RG.

4.2.1 TencentOS tiny main program entry

The main() function of TencentOS tiny mainly does the following:

  1. Board_init() board level initialization, initialization system clock (system clock), related peripheral initialization (TencentOS tiny does not currently provide a device driver framework, so this piece is the same as bare metal development, directly using STM32CubeMX to generate code)
  2. Call osKernelInitialize, call the kernel related initialization operation, the user customizes the kernel service by board\NUCLEO_STM32L476RG\TOS_CONFIG\tos_config.h related macro according to the actual project.
    1. In this case, the system IDLE task will be created.
  3. Call osThreadCreate to create an application_entry task that will begin executing application layer code in the application_entry() task. Application_entry() defaults to __weak weakly defined, and the user can re-implement application_entry().
  4. Finally call osKernelStart to start system scheduling

The TencentOS tiny kernel starts the process as follows:

TencentOS_tiny_Start_12.png

Take a look at the actual code from TencentOS tiny:

1. The location of the main() function is located in the BSP folder of the hardware platform, such as \board\NUCLEO_STM32L476RG\main.c.

    1. int main(void)

 

    1. {

 

    1. board_init();

 

    1. printf(“Welcome to TencentOS tiny\r\n”);

 

    1. osKernelInitialize(); // TOS Tiny kernel initialize

 

    1. osThreadCreate(osThread(application_entry), NULL); // Create TOS Tiny task

 

    1. osKernelStart(); // Start TOS Tiny

 

    1. }

 

 

2. board\LR_STM32L476RG\BSP\Src\mcu_init.c defines the system clock, GPIO, serial port and other MCU peripheral initialization of the hardware platform.

    1. void board_init(void)

 

    1. {

 

    1. HAL_Init();

 

    1. SystemClock_Config();

 

    1. MX_GPIO_Init();

 

    1. MX_USART2_UART_Init();

 

    1. }

 

 

4.2.2 Application Portal

By default, TencentOS tiny uses the weakly defined application_entry() task as the entry point for the application layer code. The user re-customizes application_entry() according to the actual project and adds the application layer code.

The application layer reference code is placed in the example directory, and the application code is implemented in the application_entry() function, such as the “5 chapters” Hello Word sample application.

    1. __weak void application_entry(void *arg)

 

    1. {

 

    1. while (1) {

 

    1. printf(“This is a demo task,please use your task entry!\r\n”);

 

    1. tos_task_delay(1000);

 

    1. }

 

    1. }

 

 

5 first application HelloWorld

TencentOS tiny’s hello world application is placed in TencentOS-tiny\examples\hello_world\hello_world.c

    1. #define TASK1_STK_SIZE 512

 

    1. void task1(void *arg);

 

    1. osThreadDef(task1, osPriorityNormal, 1, TASK1_STK_SIZE);

 

 

    1. #define TASK2_STK_SIZE 512

 

    1. void task2(void *arg);

 

    1. osThreadDef(task2, osPriorityNormal, 1, TASK2_STK_SIZE);

 

 

    1. void task1(void *arg)

 

    1. {

 

    1. int count = 1;

 

    1. while (1) {

 

    1. printf(“###This is task1,Hello World %d\r\n”, count++);

 

    1. osDelay(2000);

 

    1. }

 

    1. }

 

 

    1. void task2(void *arg)

 

    1. {

 

    1. int count = 1;

 

    1. while (1) {

 

    1. #if TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN > 0u

 

    1. k_err_t rc;

 

    1. int depth;

 

 

    1. rc = tos_task_stack_draught_depth(K_NULL, &depth);

 

    1. printf(“%d %d\n”, rc, depth);

 

    1. #endif

 

 

    1. printf(“***This is task2,Hello World %d\r\n”, count++);

 

    1. osDelay(1000);

 

    1. }

 

    1. }

 

 

    1. void application_entry(void *arg)

 

    1. {

 

    1. osThreadCreate(osThread(task1), NULL); // Create task1

 

    1. osThreadCreate(osThread(task2), NULL); // Create task2

 

    1. }

 

 

Using Doxygen generated function call relationships, you can clearly see the calling logic between functions, as shown below

TencentOS_tiny_Start_13.png


Application_entry system call graph

Application_entry system call graph (partial enlargement)

 

Task1 function call graph

 

Task2 function call graph

Downloaded to the STM32L4 hardware board by Jlink, etc., and the actual effect is as follows:

 

Serial Hello World example

 

6 Reference

  • TencenOS tiny open source code base
  • Introduction to TencenOS tiny