当前位置:网站首页>Single chip computer engineering experience - layered idea
Single chip computer engineering experience - layered idea
2022-07-05 06:16:00 【Blue worry cloud maple】
SCM engineering experience - Stratification thought
The experience of single-chip microcomputer engineering is a summary of my experience in single-chip microcomputer programming , I hope to improve the efficiency of your career , Don't fall into the situation of building wheels repeatedly all day . Life is more than work , And life .
Stratification thought
The idea of stratification is not mysterious at all , It simply divides a project into multiple levels , Only the upper layer can call the lower interface , And cannot be called across levels . Here I simply divide the SCM program into 3 layer .
| application layer |
|---|
| Driver layer |
| Hardware layer |
Matrix keyboard as an example
Here we take matrix keyboard as an example .
- take ROW1 ~ ROW4 Set to output mode 、 take COL1 ~ COL4 Set to input .
- take ROW1 Output low level ,ROW2、ROW3、ROW4 Output high level .
- Read COL1 ~ COL4 level , If the level of one column is low , Press the corresponding key , such as K2 When pressed COL2 Will receive low level , And then K5~K16 Whether pressed or not , It has no effect on this level , Because even if you press , Just add one 3.3V Pull it up .
- Will be different ROW Output low level , Other output high repeat reads COL The level on the . So every time on which line ROW Set it low to read this ROW On 4 The state of the keys .
layered
Hardware layer
ROW1 ~ ROW4,COL1 ~ COL4 Need to be connected to the single chip 8 Ordinary IO On the mouth , And different microcontrollers IO Port operation registers or library functions are different , If there is no layering here , Then changing a single-chip microcomputer will have to rewrite the drive of the matrix keyboard , This is obviously what we don't want .
First of all, we are right IO Port operation is abstracted , We found that IO Mouth operation is nothing more than setting IO I / O mode , Output high and low level , Check the input level .
Then we can abstract these three functions
drv_pin_mode(int id,int mode);
drv_pin_write(int id,int level);
drv_pin_read(int id);
Because it's different IO Ports can be configured separately , So we're going to IO Passing in functions , It's different here id Can represent different IO mouth .
The implementation of these three functions of different single-chip computers is different , But it doesn't matter , As long as we write the implementation of these three functions into a single source file , So is it OK to change the source file as long as you change the MCU ? In this way, I realize the layering of the hardware layer .
Driver layer
The driver layer needs to consider what we need to provide for the upper application layer , For example, the matrix keyboard needs to provide which key is pressed 、led Need to provide light on, light off and flashing 、 Temperature sensor needs to provide temperature value . And for upper tier applications , I don't need to know how you achieved it , Drivers only need to provide what the application layer wants .
For matrix keyboards , The application layer only needs one function
char get_key_date();
The application layer can call this function in real time to know which key has been pressed .
Or we can use callback function .
void set_key_callback(void (*key_date)(char key));
The upper application writes the function first , This function can handle different key values to press , Then call the above function to set the callback , When the matrix keyboard driver detects that the key is pressed, it can automatically call this callback function .
Using these two methods, the matrix keyboard driver can be written , If you need to use it next time, you can copy the driver directly without changing any code .
Matrix keyboard driver implementation
First, let's think about the relationship between matrix keyboard and hardware .
- ROW and COL The number of is uncertain
- Every ROW and COL Corresponding IO Mouth is uncertain .
Because these two are uncertain , So we need to pass these two uncertain conditions into the driver
void set_key_row_col(int *rowMap,int rowNum,int *colMap,int colNum);
here rowMap yes ROW Column pins id Array ,rowNum It's array length ,col Also in the same way .
such as row Of id yes 1357,col Of id yes 2468, So you can call
int row[] = {
1,3,5,7};
int col[] = {
2,4,6,8};
set_key_row_col(row,4,col,4);
In the driver layer , We can save these parameters , For later use .
Then we need to think about the concrete implementation of matrix keyboard , We know that at the beginning, we need to row Set to output ,col Set to input , So you can define an initialization function
void keyi_init()
{
int i = 0;
for(i = 0; i < rowNum;i++)
{
drv_pin_mode(rowMap[i],PIN_MODE_OUT);
drv_pin_write(rowMap[i],1);
}
for(i = 0; i < colNum;i++)
{
drv_pin_mode(colMap[i],PIN_MODE_IN);
}
}
Then we have to consider taking turns to read col Value .
void key_thread()
{
static int rowIndex = 0;
int i = 0;
drv_pin_write(rowMap[rowIndex],0);// Put a certain row Output low level
for(i = 0; i < colNum;i++)
{
if(drv_pin_read(colMap[i]) == 0)// If a low level is read
{
if(key_date)// If the user sets the callback function
{
key_date(i*rowNum + rowIndex);// Execute the callback function and enter the key value
}
}
}
drv_pin_write(rowMap[rowIndex],1);// Put this row Output high level
rowIndex++;// Switch to the next output low level
if(rowIndex >= rowNum)// If all row After checking, go back to No 0 row Start checking
{
rowIndex = 0;
}
}
As long as the application layer keeps calling key_thread() function , Then you can easily get the key value . Of course, this driver doesn't consider key chattering , Identify long press, double click, etc , Only ideas are introduced here , Without further study , You can think about how to improve this program according to these ideas .
application layer
After the above introduction , What the application layer needs to do is very simple
- Well defined row and col Of id, And then call
set_key_row_colfunction - Write the callback function and call
set_key_callbackTell the driver - call
keyi_initFunction to initialize the driver - Keep calling
key_threadFunction to complete the operation of the driver
thus , We can find out , No matter what MCU , No matter how many ports of matrix keyboard , Just copy the hardware layer and driver layer into the project , All you need is the above 4 You can complete the use of the matrix keyboard .
summary
The advantage of hierarchical thinking is , Both the hardware layer and the driver layer only need to be written once , Greatly reduce the workload , Engineers can concentrate on the implementation of the application layer . And because these two layers do not involve logic at all , You can easily write unit test code , Only one test passes , In the future, these two layers of code will be used without worry bug Appearance , Reduce test pressure .
Of course, the layered idea is not without shortcomings , The first is the increase of resource occupation , Layering is bound to add a lot of redundant code , And each layer has its own resource allocation , Although different levels of resources represent the same things , But in order to reduce coupling, we have to allocate additional resources , At the same time, some compilers may have restrictions on the call stack , Layered implementation will aggravate the depth of function calls . And some of the code may not support , such as 51 There may be problems in the use of function pointer by single chip microcomputer .
So in summary , For the resource rich modern MCU, it is suggested to use the layered idea to build the program , Such a program is not only fast to write but also of high quality . If SCM is a backward product in the 1980s and 1990s , Then don't use it .
边栏推荐
- [rust notes] 14 set (Part 2)
- LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
- Daily question 1688 Number of matches in the competition
- Leetcode-3: Longest substring without repeated characters
- Leetcode divide and conquer / dichotomy
- 【LeetCode】Easy | 20. Valid parentheses
- Collection: programming related websites and books
- 1.13 - RISC/CISC
- 剑指 Offer II 058:日程表
- [leetcode] day95 effective Sudoku & matrix zeroing
猜你喜欢

Is it impossible for lamda to wake up?

Leetcode stack related

LVS简介【暂未完成(半成品)】

阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队

1.14 - assembly line

MySQL advanced part 2: the use of indexes

leetcode-6108:解密消息

Doing SQL performance optimization is really eye-catching

MySQL advanced part 2: MySQL architecture

Leetcode-6108: decrypt messages
随机推荐
wordpress切换页面,域名变回了IP地址
1996. number of weak characters in the game
Leetcode-6108: decrypt messages
[practical skills] technical management of managers with non-technical background
[rust notes] 14 set (Part 2)
CPU内核和逻辑处理器的区别
leetcode-556:下一个更大元素 III
7. Processing the input of multidimensional features
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
Leetcode divide and conquer / dichotomy
LeetCode 1200. Minimum absolute difference
[rust notes] 15 string and text (Part 1)
1039 Course List for Student
Multi screen computer screenshots will cut off multiple screens, not only the current screen
The difference between CPU core and logical processor
【Rust 笔记】17-并发(上)
leetcode-31:下一个排列
leetcode-22:括号生成
【Rust 笔记】13-迭代器(中)
[practical skills] how to do a good job in technical training?