Raspberry Pi 7: Overview of Raspberry Pi peripheral development (detailed explanation of the Raspberry Pi wiringPi library)

Overview of Raspberry Pi Peripheral Development

1. Interface of Raspberry Pi:
IO port:
intput output ------- for the main control,
intput (collection of data): human infrared sensor, smoke sensor, flame sensor, vibration sensor, etc.;
output (realization control): relay switch, bee
Multiplexing of IO ports such as buzzer , LED, etc .: One port for multi-purpose
PWM: motor speed adjustment, light brightness adjustment
Serial port: USART
IIC, SPI, IIS
Other specific hardware interfaces: flash,
etc.
Supplement:
C51, STM32, WemosD1; all of these Interface, but purely logical operation, no operating system (STM32 contains RTOS real-time operating system)
Raspberry Pi, Nanopi, S3c2410, Tiny210, HiSilicon solution, Rockchip solution, Remote solution, all use operating system

2. Raspberry Pi peripheral development interface documentation reference

wiringPi library (provides API): i is a great Raspberry Pi IO control library-----(Linux dynamic library.so, static library.a)

Installation:
Type in the terminal:

gpio -v

Check whether the Raspberry Pi operation contains wiringPi,
if not, please refer to the first step of the linked document to install it.

Compile and run:
If you write a LEDtest.c project, it is as follows:

//编译
gcc -Wall -o LEDtest LEDtest.c   -lwiringPi     //使用C语言编程
//运行
sudo ./LEDtest

View the pin number table:
use the following command in the console:

gpio readall

You can view the following picture:
note: When checking, face the USB interface of the Raspberry Pi to yourself, so that the view is correct.
Insert picture description here
WiringPi library API:
When using the wiringPi library, you need to include the header files:

 #include<wiringPi.h>

All programs that write wiringPi include this header file.

3. Interface function of Raspberry Pi

Hardware initialization function:
When using wiringPi, you must initialize the Raspberry Pi before performing any operations, otherwise the program will not work normally.
canCall one of the following functions to initialize, They all return an int, and return -1 to indicate that the initialization failed.

Insert picture description here
General GPIO control function:

Insert picture description here
Insert picture description here
Time control function:

Insert picture description here
Interrupt:

wiringPi provides an interrupt handling registration function, which is only a registration function and does not handle interrupts. He does not need root privileges.

Insert picture description here
Multithreading:

wiringPi provides a simple common Posix threads thread library interface under Linux system to support concurrency.
Insert picture description here
Insert picture description here
Whenever multi-threaded programming is involved, it will involve thread safety. Multi-threaded access to the same data requires the use of synchronization locks to ensure the correctness of data operations and meet expectations.
When thread A locks lock S, other competing threads sharing this lock can only continue execution until the lock is released.
The thread that successfully executes the piLock function will own the lock. If other threads want to own the lock, they must wait until this thread releases the lock, that is, after this thread executes piUnlock.
At the same time, the knowledge to be expanded is: volatile is a keyword in C/C++, which requests the compiler not to cache the data of this variable, but to read it from memory every time. Especially in the multi-threaded sharing of variables, you must use the volatile keyword statement to be safe.

softPwm, PWM implemented by software:

The Raspberry Pi hardware supports limited PWM output pins. To break through this limitation, wiringPi provides a software-implemented PWM output API.
Need to include header files:#include <softPwm.h>
Need to add pthread library link when compiling -lpthread

Insert picture description here
Serial communication:

Need to include the header file when using:#include <wiringSerial.h>
Insert picture description here
Insert picture description here
note: The first time you use the Raspberry Pi serial port to program, you need to configure it.

The following:
Shift shift register chip API
Raspberry Pi hardware platform-specific APIs

will be studied when they are used,

Learning reference:
detailed explanation of the wiringPi library for Raspberry Pi

Guess you like

Origin blog.csdn.net/weixin_40734514/article/details/108614975