当前位置:网站首页>How to realize hierarchical management of application and hardware in embedded projects
How to realize hierarchical management of application and hardware in embedded projects
2022-06-25 06:27:00 【strongerHuang】
Focus on + Star sign public Number , Don't miss the highlights

layout | Embedded hodgepodge
source :
https://blog.csdn.net/ziqi5543/article/details/101512722
One 、 Preface
With STM32 For example , Open the routines downloaded from the network or purchase the routines from the development board , You will find that there will be stm32f10x.h perhaps stm32f10x_gpio.h, These files strictly belong to the hardware layer , If these files appear in the software layer, they will appear very messy .
Have used Linux Children's shoes must know linux The system cannot directly operate the hardware layer , open linux perhaps rt_thread The code will find that there will be device The source file , you 're right , This is the driver layer .

Two 、 Realization principle
The principle is to put all the hardware operation interfaces on the drive chain list , In the driver layer device Of open、read、write Wait for the operation . Of course, there are drawbacks in doing so , It's driving find You need to traverse the drive linked list , This will increase the code running time .
3、 ... and 、 Code implementation
International practice , Write the header file before writing the code .rt_thread Is a two-way linked list , For simplicity, I only use one-way linked list here . If you are interested, you can study it yourself rt_thread
Header file interface :
Only the following interfaces are implemented this time ,device_open and device_close The rest of the interfaces can be researched by yourself . In this way, only the following interfaces can be called in the application layer :
/*
Driver registration
*/
int cola_device_register(cola_device_t *dev);
/*
Drive lookup
*/
cola_device_t *cola_device_find(const char *name);
/*
Drive read
*/
int cola_device_read(cola_device_t *dev, int pos, void *buffer, int size);
/*
Drive write
*/
int cola_device_write(cola_device_t *dev, int pos, const void *buffer, int size);
/*
drive control
*/
int cola_device_ctrl(cola_device_t *dev, int cmd, void *arg);;The header file cola_device.h:
#ifndef _COLA_DEVICE_H_
#define _COLA_DEVICE_H_
enum LED_state
{
LED_OFF,
LED_ON,
LED_TOGGLE,
};
typedef struct cola_device cola_device_t;
struct cola_device_ops
{
int (*init) (cola_device_t *dev);
int (*open) (cola_device_t *dev, int oflag);
int (*close) (cola_device_t *dev);
int (*read) (cola_device_t *dev, int pos, void *buffer, int size);
int (*write) (cola_device_t *dev, int pos, const void *buffer, int size);
int (*control)(cola_device_t *dev, int cmd, void *args);
};
struct cola_device
{
const char * name;
struct cola_device_ops *dops;
struct cola_device *next;
};
/*
Driver registration
*/
int cola_device_register(cola_device_t *dev);
/*
Drive lookup
*/
cola_device_t *cola_device_find(const char *name);
/*
Drive read
*/
int cola_device_read(cola_device_t *dev, int pos, void *buffer, int size);
/*
Drive write
*/
int cola_device_write(cola_device_t *dev, int pos, const void *buffer, int size);
/*
drive control
*/
int cola_device_ctrl(cola_device_t *dev, int cmd, void *arg);
#endifSource file cola_device.c:
#include "cola_device.h"
#include <string.h>
#include <stdbool.h>
struct cola_device *device_list = NULL;
/*
Find out if the task exists
*/
static bool cola_device_is_exists( cola_device_t *dev )
{
cola_device_t* cur = device_list;
while( cur != NULL )
{
if( strcmp(cur->name,dev->name)==0)
{
return true;
}
cur = cur->next;
}
return false;
}
static int device_list_inster(cola_device_t *dev)
{
cola_device_t *cur = device_list;
if(NULL == device_list)
{
device_list = dev;
dev->next = NULL;
}
else
{
while(NULL != cur->next)
{
cur = cur->next;
}
cur->next = dev;
dev->next = NULL;
}
return 1;
}
/*
Driver registration
*/
int cola_device_register(cola_device_t *dev)
{
if((NULL == dev) || (cola_device_is_exists(dev)))
{
return 0;
}
if((NULL == dev->name) || (NULL == dev->dops))
{
return 0;
}
return device_list_inster(dev);
}
/*
Drive lookup
*/
cola_device_t *cola_device_find(const char *name)
{
cola_device_t* cur = device_list;
while( cur != NULL )
{
if( strcmp(cur->name,name)==0)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
/*
Drive read
*/
int cola_device_read(cola_device_t *dev, int pos, void *buffer, int size)
{
if(dev)
{
if(dev->dops->read)
{
return dev->dops->read(dev, pos, buffer, size);
}
}
return 0;
}
/*
Drive write
*/
int cola_device_write(cola_device_t *dev, int pos, const void *buffer, int size)
{
if(dev)
{
if(dev->dops->write)
{
return dev->dops->write(dev, pos, buffer, size);
}
}
return 0;
}
/*
drive control
*/
int cola_device_ctrl(cola_device_t *dev, int cmd, void *arg)
{
if(dev)
{
if(dev->dops->control)
{
return dev->dops->control(dev, cmd, arg);
}
}
return 0;
}Hardware registration method : With LED For example , Initialization interface void led_register(void), You need to call... During initialization .
#include "stm32f0xx.h"
#include "led.h"
#include "cola_device.h"
#define PORT_GREEN_LED GPIOC
#define PIN_GREENLED GPIO_Pin_13
/* LED bright 、 destroy 、 change */
#define LED_GREEN_OFF (PORT_GREEN_LED->BSRR = PIN_GREENLED)
#define LED_GREEN_ON (PORT_GREEN_LED->BRR = PIN_GREENLED)
#define LED_GREEN_TOGGLE (PORT_GREEN_LED->ODR ^= PIN_GREENLED)
static cola_device_t led_dev;
static void led_gpio_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = PIN_GREENLED;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(PORT_GREEN_LED, &GPIO_InitStructure);
LED_GREEN_OFF;
}
static int led_ctrl(cola_device_t *dev, int cmd, void *args)
{
if(LED_TOGGLE == cmd)
{
LED_GREEN_TOGGLE;
}
else
{
}
return 1;
}
static struct cola_device_ops ops =
{
.control = led_ctrl,
};
void led_register(void)
{
led_gpio_init();
led_dev.dops = &ops;
led_dev.name = "led";
cola_device_register(&led_dev);
}application layer app Code :
#include <string.h>
#include "app.h"
#include "config.h"
#include "cola_device.h"
#include "cola_os.h"
static task_t timer_500ms;
static cola_device_t *app_led_dev;
//led Every time 500ms The state changes once
static void timer_500ms_cb(uint32_t event)
{
cola_device_ctrl(app_led_dev,LED_TOGGLE,0);
}
void app_init(void)
{
app_led_dev = cola_device_find("led");
assert(app_led_dev);
cola_timer_create(&timer_500ms,timer_500ms_cb);
cola_timer_start(&timer_500ms,TIMER_ALWAYS,500);
}such app.c There is no need to call... In the file led.h Header file ,rtt That's how it works .
Four 、 summary
In this way, the software and hardware can be layered , It's not very easy to use !
5、 ... and 、 Code download link
https://gitee.com/schuck/cola_os
------------ END ------------
Pay attention to the reply of the official account “ Add group ” Join the technical exchange group according to the rules , reply “1024” See more .


Click on “ Read the original ” See more sharing .
边栏推荐
- Tablespace free space
- [road of system analyst] collection of wrong questions in the chapters of Applied Mathematics and economic management
- An interview question record about where in MySQL
- Curl command – file transfer tool
- Copying DNA
- [data visualization application] draw spatial map (with R language code)
- JS to determine whether an element exists in the array (four methods)
- What happens when redis runs out of memory
- Global and Chinese benzoic acid market competition strategy and demand scale forecast report 2022
- TFTP command – uploading and downloading files
猜你喜欢

CTFSHOW

Understand what MSS is

Viewing Chinese science and technology from the Winter Olympics (V): the Internet of things

Exercise: completion

Wireless industrial Internet of things data monitoring terminal

Cannot activate inspection type when SAP retail uses transaction code mm41 to create commodity master data?
![[kicad image] download and installation](/img/88/cebf8cc55cb8904c91f9096312859a.jpg)
[kicad image] download and installation

JSON. toJSONString(object, SerializerFeature.WriteMapNullValue); Second parameter action

Asemi fast recovery diode us1m parameters, us1m recovery time, us1m voltage drop
![[network security] sharing of experience and ideas of an emergency battle](/img/9b/dd6e47ad7610eefd2b8c93602d6a7e.jpg)
[network security] sharing of experience and ideas of an emergency battle
随机推荐
You can't specify target table for update in from clause error in MySQL
Noi Mathematics: Dirichlet convolution
At the age of 26, I was transferred to software testing with zero foundation. Now I have successfully entered the job with a monthly salary of 12K. However, no one understands my bitterness
Research Report on marketing channel analysis and competitive strategy of China's polycarbonate industry 2022
[short time energy] short time energy of speech signal based on MATLAB [including Matlab source code 1719]
Pre knowledge of asynchronous operation
Gavin's insight on transformer live class - line by line analysis and field experiment analysis of insurance BOT microservice code of insurance industry in the actual combat of Rasa dialogue robot pro
Hands on deep learning (III)
[Suanli network] problems and challenges faced by the development of Suanli network
Analysis report on production and sales demand and sales prospect of global and Chinese phosphating solution Market 2022-2028
Es11 new methods: dynamic import(), bigint, globalthis, optional chain, and null value merging operator
[speech discrimination] discrimination of speech signals based on MATLAB double threshold method [including Matlab source code 1720]
ctfshow-misc
delphi-UUID
Analysis report on global and Chinese pharmaceutical excipients industry competition and marketing model 2022-2028
[road of system analyst] collection of wrong questions in the chapters of Applied Mathematics and economic management
Tail command – view the contents at the end of the file
Research Report on investment share and application prospect of 1,3-propanediol (PDO) industry in the world and China 2022
Cat command – display the file contents on the terminal device
What happens when redis runs out of memory