当前位置:网站首页>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 .
 Matrix keyboard

  1. take ROW1 ~ ROW4 Set to output mode 、 take COL1 ~ COL4 Set to input .
  2. take ROW1 Output low level ,ROW2、ROW3、ROW4 Output high level .
  3. 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 .
  4. 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 .

  1. ROW and COL The number of is uncertain
  2. 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

  1. Well defined row and col Of id, And then call set_key_row_col function
  2. Write the callback function and call set_key_callback Tell the driver
  3. call keyi_init Function to initialize the driver
  4. Keep calling key_thread Function 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 .

原网站

版权声明
本文为[Blue worry cloud maple]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140618560340.html