A Lightweight Framework for Embedded Systems!

1 Introduction to mr-library

mr-library is a lightweight framework designed for embedded systems, providing a unified low-level driver device model and basic service functions. It features modular design, configurability, and extensibility, helping developers quickly build embedded applications.The mr-library framework supports basic kernel functionalities such as mutexes and object management. It integrates services like an asynchronous event-driven framework (event) and multi-timer software timers (soft-timer). It provides driver device models for common peripherals such as serial ports, SPI, I2C, and ADC/DAC, allowing access to underlying hardware devices through a unified driver interface (open, close, ioctl, read, write), decoupling the low-level drivers from the application.

1.1 Application Scenarios

  • Low-level driver programs for MCU development.
  • External framework for RTOS real-time operating systems (used as a driver device framework).
  • Rapid development of various IoT and smart hardware products.

2 Driver Device Framework

Developers can access peripherals in an object-oriented manner, simplifying the development process of driver logic. The framework implements generic driver templates for commonly used peripherals, allowing developers to quickly port them to different hardware platforms.The driver device framework supports a generic interface for ordinary devices, automatic bus control for bus devices, and interrupt takeover for various devices.

2.1 Driver Device Interface

The device driver framework provides a unified operation interface, and all operations on the device must be implemented through the following interfaces:A Lightweight Framework for Embedded Systems!

2.2 Example of Using SPI Device:

/* Define SPI device */#define SPI_DEVICE0_CS_PIN              10#define SPI_DEVICE1_CS_PIN              20struct mr_spi_device spi_device0, spi_device1;
/* Add SPI device */mr_spi_device_add(&spi_device0, "spi10", SPI_DEVICE0_CS_PIN);mr_spi_device_add(&spi_device1, "spi11", SPI_DEVICE1_CS_PIN);
/* Find SPI device */mr_device_t spi0_device = mr_device_find("spi10");mr_device_t spi1_device = mr_device_find("spi11");
/* Mount bus */mr_device_ioctl(spi0_device, MR_CTRL_ATTACH, "spi1");mr_device_ioctl(spi1_device, MR_CTRL_ATTACH, "spi1");
/* Open SPI device in read-write mode */mr_device_open(spi0_device, MR_OPEN_RDWR);mr_device_open(spi1_device, MR_OPEN_RDWR);
/* Send data */char buffer0[] = "hello";char buffer1[] = "world";mr_device_write(spi0_device, 0, buffer0, sizeof(buffer0) - 1);mr_device_write(spi1_device, 0, buffer1, sizeof(buffer1) - 1);
/* Read data */mr_device_read(spi0_device, 0, buffer0, sizeof(buffer0) - 1);mr_device_read(spi1_device, 0, buffer1, sizeof(buffer1) - 1);
/* Close device */mr_device_close(spi0_device);mr_device_close(spi1_device);

3 Service Framework

The mr-library framework integrates a lightweight service framework for building application services in embedded development, supporting asynchronous event listening, multi-timer software timers, etc. The service framework decouples different applications at the application layer, achieving modularity, customizability, clear business logic, rapid development, and high code reuse.

3.1 Event Service

The event service is an asynchronous event handling mechanism that effectively improves the system’s asynchronous processing capability, decoupling, and scalability through event dispatching and callbacks.The event service consists of two parts: the event server and events.

  • The event server is used to receive and dispatch events, maintaining an event queue for storing pending events and an event list for storing created events.
  • Events need to be created on the event server and provide a callback function.

When an event occurs, the event server inserts the event into the event queue for caching. The event server periodically retrieves events from the event queue for dispatching, finding the corresponding event callback for event handling.

3.2 Event Service Operation Interface

A Lightweight Framework for Embedded Systems!

3.3 Example of Using Event Service:

/* Define events */#define EVENT1                          1#define EVENT2                          2#define EVENT3                          3
/* Define event server */struct mr_event_server event_server;
mr_err_t event1_cb(mr_event_server_t server, void *args){    printf("event1_cb\r\n");        /* Notify event server that event2 has occurred */    mr_event_notify(EVENT2, server);    return MR_ERR_OK;}
mr_err_t event2_cb(mr_event_server_t server, void *args){    printf("event2_cb\r\n");
    /* Notify event server that event3 has occurred */    mr_event_notify(EVENT3, server);    return MR_ERR_OK;}
mr_err_t event3_cb(mr_event_server_t server, void *args){    printf("event3_cb\r\n");    return MR_ERR_OK;}
int main(void){    /* Add event server to kernel container */    mr_event_server_add(&event_server, "server", 4);        /* Create events on the server */    mr_event_create(EVENT1, event1_cb, MR_NULL, &event_server);    mr_event_create(EVENT2, event2_cb, MR_NULL, &event_server);    mr_event_create(EVENT3, event3_cb, MR_NULL, &event_server);        /* Notify event server that event1 has occurred */    mr_event_notify(EVENT1, &event_server);        while (1)    {        /* Event server handling */        mr_event_server_handle(&event_server);    }}

Phenomenon:

event1_cbevent2_cbevent3_cb

3.4 Software Timer Service

The software timer is a mechanism that implements timing functions at the software level. Through software timers, specific events can be triggered at specific time points or intervals. Software timers are commonly used for implementing periodic tasks, timeout handling, timer interrupts, and other functions.The software timer consists of two main components: the timer server and the timer.

  • The timer server is responsible for time management and timer handling.
  • The timer is used for handling specific timeout processes; it needs to be registered with the timer server and provide a callback function.

3.5 Software Timer Service Operation Interface

A Lightweight Framework for Embedded Systems!

3.6 Example of Using Software Timer Service:

/* Define timer server and timers */struct mr_soft_timer_server server;struct mr_soft_timer timer1, timer2, timer3;
mr_err_t timer1_callback(mr_soft_timer timer, void *args){    printf("timer1_callback\r\n");    return MR_ERR_OK;}
mr_err_t timer2_callback(mr_soft_timer timer, void *args){    printf("timer2_callback\r\n");    return MR_ERR_OK;}
mr_err_t timer3_callback(mr_soft_timer timer, void *args){    printf("timer3_callback\r\n");    mr_soft_timer_stop(timer);    return MR_ERR_OK;}
int main(void){    /* Add timer server */    mr_soft_timer_server_add(&server, "soft-timer");
    /* Add timers and start */    mr_soft_timer_add_then_start(&timer1, 5, timer1_callback, MR_NULL, &server);    mr_soft_timer_add_then_start(&timer2, 10, timer2_callback, MR_NULL, &server);    mr_soft_timer_add_then_start(&timer3, 15, timer3_callback, MR_NULL, &server);
    while (1)    {        /* Update timer server clock */        mr_soft_timer_server_update(&server, 1);                /* Timer server handling (where it is placed, the callback will be called there) */        mr_soft_timer_server_handle(&server);    }}

4 Code Directory

The code directory structure of mr-library is shown in the table below:A Lightweight Framework for Embedded Systems!

  • Kernel Layer: The core part of mr-library, implementing object management, device control, service interfaces, etc.
  • Device Layer: Provides a unified device interface to connect devices to the kernel.
  • Driver Layer: Provides low-level hardware drivers for devices; only the driver layer needs to be modified when hardware changes.
  • Component Layer: Implements different functionalities through the APIs provided by the framework, including but not limited to virtual file systems, general sensor modules, network frameworks, etc.
  • Package: Standalone packages that can be used independently without dependencies.

Source: https://gitee.com/MacRsh/mr-library

Leave a Comment