【EventOS Nano】Quick Start for EventOS Nano

Time:2022-8-11

Quick start


EventOS Nano is very simple. In addition to the source code, you only need to implement three codes to write programs using EventOS Nano.

  • main.c main function, initializes and starts EventOS
  • eos_port.c, such as the interface implementation of EventOS on a specific platform, is the relevant code for EventOS Nano porting
  • eos_led.c Blink state machine for LEDs. The LED light flashes, similar to the hello world in the microcontroller world

Each file is described in detail as follows:


1.main.c

EventOS initialization -> subscription table initialization -> LED state machine initialization -> EventOS startup




/* 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 to table data space/* main function ------------------------------------------------- ----------- */ int main(void) { if (SysTick_Config(SystemCoreClock / 1000) != 0) while (1); eos_init(); // EventOS initialization #if (EOS_USE_PUB_SUB != 0) eos_sub_init(eos_sub_table, Event_Max); // Subscription table initialization #endif #if (EOS_USE_SM_MODE != 0) eos_sm_led_init(); // LED state machine initialization #endif eos_reactor_led_init(); eos_run(); // EventOS start return 0 ; }


In QP and EventOS Nano, event topics are defined using enumerations. However, in many event-driven and message-driven applications, such as the implementation of ROS and MQTT, the definition of events (messages) uses strings. Both have their own advantages and disadvantages. The advantage of enumeration is that it saves RAM and ROM, which is suitable for resource-constrained occasions. The advantage of string is that it is easy to use, does not generate coupling, and is conducive to realizing distribution.


The event topic event_def.h is defined as follows:

#include "eventos.h" enum { Event_Test = Event_User, // The definition of the event topic starts from Event_User, and the system event is smaller than Event_User. Event_Time_500ms, Event_Time_1000ms, Event_ActEnd, Event_Max };


2.eos_port.c


eos_port.c is a hardware or platform dependent file

#include "eventos.h" // Enter the critical section, close the global interrupt void eos_port_critical_enter(void) { __disable_irq(); // When the IDE is MDK, for ARM Cortex-M series, you can refer to the implementation} // Exit the critical section , enable global interrupt void eos_port_critical_exit(void) { __enable_irq(); // When IDE is MDK, for ARM Cortex-M series, you can refer to the implementation} // Assertion interface, EventOS Nano respects defensive programming, and uses a lot of internal Assertion check the validity of the program running eos_u32_te eos_error_id = 0; void eos_port_assert(eos_u32_t error_id) { // Print the assertion information. SEGGER_RTT_printf(0, "------------------------------------\n"); SEGGER_RTT_printf(0, "ASSERT >>> Module: EventOS Nano, ErrorId: %d.\n", error_id); SEGGER_RTT_printf(0, "----------------------- -------------\n"); eos_error_id = error_id; while (1) { /* User code */ } } // Idle callback function: when EventOS Nano has no events to handle When, this function will be called // It can realize: rotation training of hardware devices, calculation of random numbers, etc. If not, just implement an empty function void eos_hook_idle(void) { } // Start the callback function // After EventOS Nano is initialized , and this callback function is called before the state machine runs. Here, hardware devices can be initialized, function modules or middleware can be initialized, and so on. void eos_hook_start(void) { // Hardware initialization/* User code */ // Function module initialization/* User code */ } // Stop callback function // After EventOS Nano enters assertion, or user calls eventos_stop function to stop EventOS After that, this callback function will be called // This function is mainly used to orderly shut down sensitive equipment (turn off motor operation), or provide information to the outside world (open the alarm, display the alarm interface, or print the alarm information, etc.). void eos_hook_stop(void) { // orderly shut down sensitive devices motor_stop(Motor_Left); motor_stop(Motor_Right); // display necessary information led_set_status(LedStatus_Stop); }


3.eos_led.c和eos_led.h

How the state machine is used is described in eos_led.c

/* include ----------------------------------------------- ------------------- */ #include "eos_led.h" // Module header #include "eventos.h" // EventOS header #include "event_def. h" // event definition header file #include// stdio/* data structure ---------------------------------------- ------------------- */ typedef struct eos_led_tag ​​{ // LED class eos_sm_t super; eos_bool_t status; } eos_led_t; static eos_led_t led; // led object, singleton mode /* static state function ---------------------------------------------- ------- */ // initial state static eos_ret_t state_init(eos_led_t * const me, eos_event_t const * const e); // ON state of Led static eos_ret_t state_on(eos_led_t * const me, eos_event_t const * const e) ; // Led's Off state 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]; // event queue 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 event Event_Time_500ms EOS_EVENT_SUB (Event_Time_500ms); // Make the event Event_Time_500ms to be sent every 500ms. eos_event_pub_period(Event_Time_500ms, 500); // EOS_TRAN is a callback interface return EOS_TRAN(state_off); } /*Every 500ms, the subscribed 500ms event will be published once, switch the on/off state, and enter the Event_Enter state of the corresponding function to execute*/ 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 of state state_on printf("State On!\n"); me->status = 1 ; return EOS_Ret_Handled; case Event_Time_500ms: // Receive Event_Time_500ms, 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: // Enter event of state state_off printf("State Off!\n"); me->status = 0; return EOS_Ret_Handled; case Event_Time_500ms: // Receive Event_Time_500ms, jump to state_on return EOS_TRAN(state_on); default: return EOS_SUPER(eos_state_top); } }

















Next step: Analysis of EventOS Nano source code and implementation of event mechanism





Warehouse Address:https://gitee.com/event-os/eventos.git