Play with LiteOS component: TinyFrame

Absrtact: TinyFrame is a simple library for parsing communication data frames of serial ports (such as UART, telnet, socket, etc.).

This article is shared from Huawei cloud community< A taste of LiteOS components - play with TinyFrame >, by Lionlace.

Basic introduction

TinyFrame is a simple library for parsing communication data frames of serial ports (such as UART, telnet, socket, etc.). It can flexibly handle the message session, response listener, verification and timeout between communication parties. The library allows the registration of listeners (in the form of callback functions) for waiting: any frame, specific frame type and specific message ID. this high-throughput API can meet most communication scenarios.

Function overview

Frame structure

Each frame consists of a frame header and a payload. Both parts can be protected by checksums to exclude malformed headers (for example, incorrect field lengths) or payload corrupted frames. All field sizes in the frame are configurable. For example, a user can change a field in a configuration file_ LEN_ Bytes are different values (1, 2, or 4).

,-----+-----+-----+------+------------+- - - -+-------------,
| SOF | ID | LEN | TYPE | HEAD_CKSUM | DATA | DATA_CKSUM |
| 0-1 | 1-4 | 1-4 | 1-4 | 0-4 | ... | 0-4 | <- size (bytes)
'-----+-----+-----+------+------------+- - - -+-------------'
SOF ......... start of frame, usually 0x01 (optional, configurable)
ID ......... the frame ID (MSb is the peer bit)
LEN ......... number of data bytes in the frame
TYPE ........ message type (used to run Type Listeners, pick any values you like)
HEAD_CKSUM .. header checksum
DATA ........ LEN bytes of data
DATA_CKSUM .. data checksum (left out if LEN is 0)

Message listening

TinyFrame has three types of listeners, in order of priority:

  • ID listener - waiting for response
  • Type listener - waits for a message for a given type field
  • Universal listener - fallback

Data buffer

TinyFrame uses two data buffers: a small send buffer and a large receive buffer. The sending buffer is used to prepare the bytes to be sent, which can be sent all at once; If the buffer is not large enough, it is sent in a circular manner. The buffer must contain only the whole frame header, for example, 32 bytes for short messages.

instructions

LiteOS provides TinyFrameDemoTask to demonstrate how to use TinyFrame components. Currently, TinyFrameDemoTask can be run on the following development boards.

  • STM32F769
  • STM32F429
  • realview-pbx-a9
  • qemu-virt-a53

Next, take TinyFrameDemo as an example to introduce the use of TinyFrame.

Parsing TinyFrame Demo

In TinyFrame_demo.c mainly realizes three functions:

  1. Send TF without listener and structure_ SendSimple();
  2. The listener is waiting for a reply when sending, but does not use the structure TF_QuerySimple();
  3. Send TF without listener_ Send().

Enable TinyFrame Demo

Execute the make menuconfig command in the root directory of LiteOS source code, and enable it according to the following menu path

TinyFrame Demo. 
Demos --->
Utility Demo --->
[*] Enable TinyFrame Demo (NEW)

After enabling TinyFrame Demo, TinyFrame components will be enabled automatically.

After saving and exiting, the TinyFrame source code will be automatically downloaded from github, and the patch package suitable for LiteOS system will be downloaded from gitee and entered into patch.

Compile and run TinyFrame Demo

The operation steps are as follows:

  1. Refer to the above enable TinyFrame demo.
  2. Execute make clean under the root directory of liteos source code of Linux host; The make - J command compiles the liteos project. After successful compilation, it will be displayed in ~ / Huawei_ Generate the library file libtinyframe of TinyFrame in liteos / out / ${platform} / lib directory A and this Demo library file libTinyFrame_demo.a. The system image file is Huawei_LiteOS.bin. Take realview-pbx-a9 / development board as an example. Its directory is out/realview-pbx-a9 /.
  3. Execute QEMU system arm - machine realview-pbx-a9 - SMP 4 - M 512M - kernel out / realview-pbx-a9 / Huawei_ LiteOS. After the bin - nographic command, you can see the running results of the Demo part output from the serial port, as shown below.
********Hello Huawei LiteOS********
LiteOS Kernel Version : 5.1.0
Processor : Cortex-A9 * 4
Run Mode : SMP
GIC Rev : GICv1
build time : Dec 20 2021 11:30:07
**********************************
main core booting up...
OsAppInit
releasing 3 secondary cores
cpu 0 entering scheduler
cpu 3 entering scheduler
cpu 2 entering scheduler
cpu 1 entering scheduler
app init!
TinyFrame demo task start to run.
--------------------
TF_WriteImpl - sending frame:
1 01 .
128 80 .
0 00 .
7 07 .
1 01 .
240 F0 .
215 D7 .
108 6C l
105 69 i
116 74 t
101 65 e
111 6F o
115 73 s
0 00 .
213 D5 .
30 1E .
--- end of frame ---
GenericListener demo
Frame info
type: 01h
data: "liteos"
len: 7
id: 80h
--------------------
...
...
...

Click follow to learn about Huawei's new cloud technology for the first time~

Keywords: liteos

Added by J-C on Sun, 30 Jan 2022 12:14:24 +0200