Introduction to mr-library
mr-library is a lightweight framework for embedded systems that provides 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 functions such as mutexes and object management. It integrates asynchronous event-driven frameworks (events), multi-timer software timers (soft-timer), and other services. It provides driver device models for common peripherals such as serial ports, SPI, I2C, ADC/DAC, allowing access to underlying hardware devices through a unified driver interface (open, close, ioctl, read, write), decoupling the low-level drivers and applications.
Application Scenarios
-
Low-level driver programs developed for MCUs. -
External frameworks for RTOS real-time operating systems (used as driver device frameworks). -
Rapid development of various IoT and smart hardware products.
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, enabling developers to quickly port them to different hardware platforms.
The driver device framework supports general interfaces for common devices, automatic bus control for bus devices, and interrupt takeover for multiple devices.
Driver Device Interface
The device driver framework provides a unified operational interface, and all operations on the device must be implemented through the following interfaces:
Interface | Description |
---|---|
mr_device_add | Add device |
mr_device_find | Find device |
mr_device_open | Open device |
mr_device_close | Close device |
mr_device_ioctl | Control device |
mr_device_read | Read data from device |
mr_device_write | Write data to device |
Example of Using SPI Device:
/* Define SPI device */
#define SPI_DEVICE0_CS_PIN 10
#define SPI_DEVICE1_CS_PIN 20
struct 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);
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 modularization, customizability, clear business logic, rapid development, and high code reuse.
Event Service
The event service is an asynchronous event handling mechanism that effectively improves the system’s asynchronous processing capability, decoupling, and extensibility 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 buffering. The event server periodically retrieves events from the event queue for dispatching, finding the corresponding event callback for event processing.
Event Service Operation Interface
Interface | Description |
---|---|
mr_event_server_find | Find event server |
mr_event_server_add | Add event server |
mr_event_server_remove | Remove event server |
mr_event_server_handle | Event server handling |
mr_event_create | Create event |
mr_event_delete | Remove event |
mr_event_notify | Notify event occurrence |
mr_event_trigger | Trigger event |
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 of event 2 occurrence */
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 of event 3 occurrence */
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 of event 1 occurrence */
mr_event_notify(EVENT1, &event_server);
while (1)
{
/* Event server handling */
mr_event_server_handle(&event_server);
}
}
Phenomenon:
event1_cb
event2_cb
event3_cb
Software Timer Service
The software timer is a mechanism for implementing timing functions at the software level. Through the software timer, specific events can be triggered at specific time points or time intervals. Software timers are commonly used for periodic tasks, timeout handling, timer interrupts, etc.
The software timer consists of two main components: the timer server and the timer.
-
The timer server is used for time management and timer handling. -
The timer is used for handling specific timeout processing; it needs to be registered with the timer server and provide a callback function.
Software Timer Service Operation Interface
Interface | Description |
---|---|
mr_soft_timer_server_find | Find timer server |
mr_soft_timer_server_add | Add timer server |
mr_soft_timer_server_remove | Remove timer server |
mr_soft_timer_server_update | Update timer server time base signal |
mr_soft_timer_server_handle | Timer server handling |
mr_soft_timer_add | Add timer |
mr_soft_timer_remove | Remove timer |
mr_soft_timer_start | Start timer |
mr_soft_timer_stop | Pause timer |
mr_soft_timer_add_then_start | Add timer and start |
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) */
mr_soft_timer_server_handle(&server);
}
}
Code Directory
The code directory structure of mr-library is shown in the table below:
Name | Description |
---|---|
bsp | Board support package |
device | Device files |
document | Documentation |
driver | Driver files |
include | Library header files |
module | Components |
package | Software packages |
src | Library source files |
-
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 APIs provided by the framework, including but not limited to virtual file systems, general sensor modules, network frameworks, etc. -
Software Package: Independently usable software packages without dependencies.
Source:https://gitee.com/MacRsh/mr-library
Reprinted from WeChat public account: Embedded Miscellaneous
Copyright statement: This article is sourced from the internet, and the copyright belongs to the original author. For copyright issues, please contact to delete.
Overview of Basic Knowledge of GPIO! (Most Complete Information)
The Artifact is Here! One-Click Improvement of Embedded Code Quality
How to Fit Hundreds of Billions of Transistors on a Chip the Size of a Fingernail?
Leave a Comment
Your email address will not be published. Required fields are marked *