EventOS Nano, which is recommended in this issue, is an event-driven embedded development platform for microcontroller.
class=”pgc-h-arrow-right” data-track=”2″>
It has two main technical features: one is event-driven, the other is ultra-lightweight. EventOS Nano and its parent project EventOS, the goal is to develop an enterprise level embedded development platform, with event bus as the core, to create a unified embedded technology ecosystem, For the majority of enterprise users and embedded developers, to provide a reliable, high-performance, modern and high development efficiency of the embedded development environment.
The main features of EventOS Nano are listed below:
- The
- event bus is the core component, which is flexible and easy to use. It is the main means of synchronization or communication between threads (state machines), and the only means to support the distributed characteristics and cross-platform development of EventOS. Events support either a broadcast send or a publisher-subscribe mechanism (one of two options).
- global event queue , which eliminates the way that each thread (state machine) has its own event queue. There is only one global event queue, which minimizes RAM consumption.
- Collaborative kernel , which has the advantage of not generating resource competition and is extremely reliable.
- extremely lightweight, easy to embed into other systems , all features except event bus (hierarchical state machine, planar state machine, publishing-subscribe mechanism, event carrying data, event bridge, etc.) can be trimmed to reduce resource usage to the limit. Available as low as 1.2KB ROM and 172Byte RAM. Can be used as a subsystem, “quietly” embedded in other software systems.
- Powerful soft timer , in the form of time events, soft timer function, elegant and powerful implementation.
- API design, more concise, more in line with the habits of local embedded engineers.
- Easy to migrate, only need to implement a few interface functions.
- will use the Event Bridge mechanism to get through the event bus with EventOS in the future, To support the distributed nature of EventOS.
- Focus on three application scenarios: single chip microcomputer, embedded as a module to other software systems and high reliability requirements of embedded scenarios.
Programming ideas advocated by EventOS Nano
Event driver and event bus
Event driver and event bus, is the core of EventOS Nano, is also the core of EventOS. The event mechanism, which is completely different from the event concept in RTOS, is more like the message in windows programming. Events can be regarded as topic + variable length data . Through events, the coupling between modules can be greatly decoupling, the testability of software can be enhanced, and cross-platform development and distributed expansion can be carried out.
Defensive programming
EventOS Nano uses a lot of assertions, a lot of checks on the running process of the system and the user’s use of EventOS Nano. We strongly recommend that users carefully design and implement assertion interface functions so that assertions are still turned on in actual production code. In this way, the software will converge to the steady state at a very fast rate.
Cross-platform development
EventOS Nano advocates cross-platform development. The so-called cross-platform development is to complete most of the development work in the convenient and friendly development environment such as Windows and Linux, including programming, debugging, running and unit testing, and then carry out the final transplantation, debugging and adaptation work on the target platform. There are many advantages of cross-platform, such as very high development efficiency, engineers into more programming areas, and program stability and reliability. EventOS Nano is mainly developed on the 32-bit MinGW platform and Linux platform. For setting up the development environment, see the document Setting up the Development Environment. Of course, you can also use MDK directly on the microcontroller development, the efficiency is slightly lower.
Decoupling
Whether it is broadcast event sending mechanism or publist-subscribe event sending mechanism, in fact, it is to eliminate the coupling between software modules.
class=”pgc-h-arrow-right” data-track=”23″>
EventOS Nano is easy to get started with. In addition to the source code, you only need to implement three codes to write programs using EventOS Nano.
- main.c main function, initialize and start EventOS Nano.
- eos_port.c For example, the interface implementation of EventOS on a specific platform, that is, the related code of EventOS Nano migration.
- eos_led.c LED flashing state machine. LED lights flashing, is the MCU World Hello World. I believe it is the entry code for many people.
main.c The process of booting from EventOS is very simple and can be started in just a few steps.
/* include ------------------------------------------------------------------ */
#include "eventos.h" // EventOS Nano header file
#include "event_def.h" // Enumeration of event topics
#include "eos_led.h" // LED flashing state machine
/* define ------------------------------------------------------------------- */
static eos_u32_t eos_sub_table[Event_Max]; // Subscribe table data space
static eos_u8_t eos_heap_memory[1024]; // Event pool space
/* main function ------------------------------------------------------------ */
int main(void)
{
// EventOS Nano
eos_init(); // Initialize EventOS
eos_sub_init(eos_sub_table); //Initialize subscription table
eos_event_pool_init(eos_heap_memory, 1024); // Event pool initialization
//Initialization of state machine module
eos_led_init(); // LED state machine initialization
// Start EventOS Nano.
eos_run(); //EventOS is up and running
return 0;
}
Event theme event_def.h is defined as follows.
#include "eventos.h"
enum {
Event_Test = Event_User, // The event topic definition starts with Event_User, and the less than Event_User is the system event.
Event_Time_500ms,
Event_Max
};
eos_port.c migration file, which has been detailed in UM-02-002 EventOS Nano Migration Document, will not be described again.
eos_led.c and eos_led.h headers. Instead, let’s focus on the.c file, which is how the state machine is used.
/* include ------------------------------------------------------------------ */
#include "eos_led.h" // Module header file
#include "eventos.h" // EventOS header file
#include "event_def.h" // Event definition header file
#include <stdio.h> // Standard input and output libraries
/* data structure ----------------------------------------------------------- */
typedef struct eos_led_tag { // LED Span
eos_sm_t super;
eos_bool_t status;
} eos_led_t;
static eos_led_t led; //led object, singleton mode
/* static state function ---------------------------------------------------- */
// Span
static eos_ret_t state_init(eos_led_t * const me, eos_event_t const * const e);
// ON status of Led
static eos_ret_t state_on(eos_led_t * const me, eos_event_t const * const e);
// Led Off status
static eos_ret_t state_off(eos_led_t * const me, eos_event_t const * const e);
/* api ---------------------------------------------------- */
void eos_led_init(void)
{
static eos_u32_t queue[32]; // Queue of events
eos_sm_init(&led.super, 1, queue, 32); // State machine initialization
// The state machine starts with state_init as the initial state.
eos_sm_start(&led.super, EOS_STATE_CAST(state_init));
led.status = 0;
}
/* static state function ---------------------------------------------------- */
static eos_ret_t state_init(eos_led_t * const me, eos_event_t const * const e)
{
// Subscribe to eventsEvent_Time_500ms
EOS_EVENT_SUB(Event_Time_500ms);
// Make event Event_Time_500ms,It's sent every 500ms.
eos_event_pub_period(Event_Time_500ms, 500);
return EOS_TRAN(state_off);
}
static eos_ret_t state_on(eos_led_t * const me, eos_event_t const * const e)
{
switch (e->topic) {
case Event_Enter: //Entry event for state state_on
printf("State On!\n");
me->status = 1;
return EOS_Ret_Handled;
case Event_Time_500ms: // Event_Time_500ms received, jump to state_off
return EOS_TRAN(state_off);
default:
return EOS_SUPER(eos_state_top);
}
}
static eos_ret_t state_off(eos_led_t * const me, eos_event_t const * const e)
{
switch (e->topic) {
case Event_Enter: // Entry event for state state_on
printf("State Off!\n");
me->status = 0;
return EOS_Ret_Handled;
case Event_Time_500ms: //Event_Time_500ms received, jump to state_on
return EOS_TRAN(state_on);
default:
return EOS_SUPER(eos_state_top);
}
}
Structure of code
Core code
- EventOS/evenTOs. c Implementation of EventOS Nano state machine framework
- eventos/eventos.h header
- eventos/eventos_config.h Configure and crop the EventOS Nano
third-party code base
- RTT Segger JLink provides logging libraries that depend on JLink hardware.
- unity unit testing framework