当前位置:网站首页>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 .
边栏推荐
- Uncaught typeerror cannot set properties of undefined (setting 'classname') reported by binding onclick event in jsfor loop
- How to open an account online? Is it safe to open an account online?
- Research Report on investment share and application prospect of 1,3-propanediol (PDO) industry in the world and China 2022
- JS dynamic table creation
- Wechat applet authorization login + mobile phone sending verification code +jwt verification interface (laravel8+php)
- Handling skills of SQL optimization (2)
- Aviator an expression evaluation engine
- Research Report on brand strategic management and marketing trends in the global and Chinese preserved fruit market 2022
- BGP - basic concept
- Vegetables sklearn - xgboost (2)
猜你喜欢
![[short time energy] short time energy of speech signal based on MATLAB [including Matlab source code 1719]](/img/a1/0cb61368cb1d0817d74781084a4466.jpg)
[short time energy] short time energy of speech signal based on MATLAB [including Matlab source code 1719]

What elements are indispensable for the development of the character? What are the stages

You can see the classification of SQL injection. SQL injection point /sql injection type /sql injection has several /sql injection point classifications

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

Laravel8+ wechat applet generates QR code

Monitoring access: how to grant minimum WMI access to the monitoring service account
![[speech discrimination] discrimination of speech signals based on MATLAB double threshold method [including Matlab source code 1720]](/img/36/ad86f403b47731670879f01299b416.jpg)
[speech discrimination] discrimination of speech signals based on MATLAB double threshold method [including Matlab source code 1720]

Large funds support ecological construction, and Plato farm builds a real meta universe with Dao as its governance
![[kicad image] download and installation](/img/88/cebf8cc55cb8904c91f9096312859a.jpg)
[kicad image] download and installation

How to use asemi FET 7n80 and how to use 7n80
随机推荐
Digitalization, transformation?
VMware virtual machine prompt: the virtual device ide1:0 cannot be connected because there is no corresponding device on the host.
PHP and WMI – explore windows with PHP
Lesson 8: FTP server setup and loading
Rhcsa day 4
Socket, network model notes
Understand what MTU is
You can see the classification of SQL injection. SQL injection point /sql injection type /sql injection has several /sql injection point classifications
Mongodb delete data
After five years of software testing in ByteDance, I was dismissed in December to remind my brother of paddling
The elephant turns around and starts the whole body. Ali pushes Maoxiang not only to Jingdong
PHP output (print) log to TXT text
Report on strategic suggestions on investment direction and Prospect of global and Chinese marine biological industry (2022 Edition)
Research Report on marketing channel analysis and competitive strategy of China's polycarbonate industry 2022
Notes on dashboard & kuboard installation in kubernetes cluster
Tablespace free space
Research Report on brand strategic management and marketing trends in the global and Chinese preserved fruit market 2022
Three tier architecture experiment
Wechat applet simply realizes chat room function
Rhcsa--- day 6 operation