The simplest IO-driven wisdom

Dry goods | The easiest IO-driven wisdom

From microcontrollers to ARM7, ARM9, Cortex-A8, from uC/OS to WinCE, Linux, GPIO drivers are the simplest and easiest to write drivers. But is it a simple, unskilled driver, is it complete? Is it regulated? Is it safe?

Typical Case

In this section, we will select two typical cases and compare them from the opposite and the positive.

Anti-plan

Take the XXX driver in a source code as an example, and intercept the code of the XXX_IOControl part, as shown in Listing 1; please pay attention to the code highlighting part.

Listing 1

The simplest IO-driven wisdom

From the reverse scheme, the function of reading or writing the GPIO level state requires only a few lines of code, which is very simple.

Positive program example

As shown in Listing 2, the code is taken from a core board GPIO driver from ZLG. Please pay attention to the highlighted part of the code.

Listing 2
The simplest IO-driven wisdom
The simplest IO-driven wisdom
The simplest IO-driven wisdom
The simplest IO-driven wisdom

From the positive example, the function of reading or writing the GPIO level state takes twice the code workload. Why is the difference so large?

Case review

First, the pointer is used

In the reverse scheme example, the pointer parameters passed in by the function are used without judgment. In this case, if the pointer is a null pointer or a wild pointer, the program may be abnormal or even crash!

In the inverse example, after the read operation, use "*pBytesReturned = 2;" to return the number of bytes actually read, but the pointer is still used without judgment!

In the case of positive and negative cases, the parameter range and validity are judged before each parameter is used, which fundamentally avoids the occurrence of parameter anomalies!

Second, the error prompt

In the inverse scenario, XXX_IOControl simply returns TRUE or FALSE. When FALSE is returned, the application layer does not get or know what caused the "FALSE"!

Compared with the positive example, the error prompt is added when the parameter is judged. Before the return, the SetLastError function is called, and the application layer can obtain the error reason through GetLastError, allowing the user to locate the error point more quickly and accurately.

Third, the notes

The anti-scheme function has almost no comments inside and outside the function;

In the positive example, no matter the key position in the function body or the function body, the necessary and detailed explanations are made, which brings great convenience to the later maintenance of the program!

Drive implementation is very easy, including the simplest GPIO, but the integrity and reliability of the driver is the wisdom of software engineering.

Molded Case Circuit Breaker

Molded Case Circuit Breaker,Ir Windows For Switchgear,Automatic Transfer Switchgear,Eaton 15Kv Switchgear

Shandong Shunkai electrical equipment co., LTD. , https://www.chinasdsk.com

Posted on