raspberry pi
raspberry pi
Lecture 11
Vince Weaver
http://web.eece.maine.edu/~vweaver
[email protected]
25 September 2020
Announcements
1
Brief Overview of the Raspberry Pi Board
Model
Power
1B 1B+/2B/3B/3B+ Model 4B Pin1 Pin2
Pin1 Pin2 Pin1 Pin2
Power
Power
Camera
HDMI
HDMI
HDMI
Audio
Ethernet
Ethernet USB USB Ethernet
USB USB USB
2
Rasp-pi Header
3
Rasp-pi Header
3.3V 1 2 5V
GPIO2 (SDA) 3 4 5V
GPIO3 (SCL) 5 6 GND
GPIO4 (1-wire) 7 8 GPIO14 (UART TXD)
GND 9 10 GPIO15 (UART RXD)
GPIO17 11 12 GPIO18 (PCM CLK)
GPIO27 13 14 GND
GPIO22 15 16 GPIO23
3.3V 17 18 GPIO24
GPIO10 (MOSI) 19 20 GND
GPIO9 (MISO) 21 22 GPIO25
GPIO11 (SCLK) 23 24 GPIO8 (CE0)
GND 25 26 GPIO7 (CE1)
ID SD (EEPROM) 27 28 ID SC (EEPROM)
GPIO5 29 30 GND
GPIO6 31 32 GPIO12
GPIO13 33 34 GND
GPIO19 35 36 GPIO16
GPIO26 37 38 GPIO20
GND 39 40 GPIO21
4
How you enable GPIO on STM32L
A lot of read/modify/write instructions to read current
register values and then to shift/mask to write out updated
bitfields.
6
Letting the OS handle it for you
7
“Old” Linux sysfs GPIO interface
9
“New” Linux GPIO interface
10
GPIOD utils
11
There is a library
12
A few low-level Linux Coding Instructions
13
gpio – getting interface info
# include " linux / gpio . h "
// struct gpiochip_info {
// char name [32];
// char label [32];
// __u32 lines ; }
int fd , rv ;
struct gpiochip_info chip_info ;
/* ask for chipinfo from open file descriptor , put in chip_info struct */
rv = ioctl ( fd , GPIO_GET_CHIPINFO_IOCTL ,& chip_info );
if ( rv <0 ) printf ( " Error ioctl % s \ n " , strerror ( errno ));
14
gpio – get info about line gpio17
// struct gpioline_info {
// __u32 line_offset ;
// __u32 flags ;
// char name [32];
// char consumer [32]; }
15
gpio – configure request structure
// struct gpiohandle_request {
// __u32 lineoffsets [ GPIOHANDLES_MAX ];
// __u32 flags ;
// __u8 default_values [ GPIOHANDLES_MAX ];
// char consumer_label [32];
// __u32 lines ; int fd ;}
// # define G PI O H A ND L E _ RE Q U E ST _ I N PU T (1 UL << 0)
// # define G P I O H A N D L E _ R E QU E S T _ O U T P U T (1 UL << 1)
// # define G P I O H A N D L E _ R E Q U E S T _ A C T I V E _ L O W (1 UL << 2)
// # define G P I O H A N D L E _ R E Q U E S T _ O P E N _ D R A I N (1 UL << 3)
// # define G P I O H A N D L E _ R E Q U E S T _ O P E N _ S O U R C E (1 UL << 4)
// # define G P I O H A N D L E _ R E Q U E S T _ B I A S _ P U L L _ U P (1 UL << 5)
// # define GPIOHANDLE_REQUEST_BIAS_PULL_DOWN (1 UL << 6)
// # define G P I O H A N D L E _ R E Q U E S T _ B I A S _ D I S A B L E (1 UL << 7)
16
gpio – actually do request
struct gpiohandle_request req ;
17
gpio – change value of gpio17
// struct gpiohandle_data {
// __u8 values [ GPIOHANDLES_MAX ]; }
/* set output to 0 */
data . values [0]=0;
/* set output to 1 */
data . values [0]=1;
18
gpio – read from gpio17
struct gpiohandle_data data ;
19
Delay
20
Waiting for Input
21
gpio – using poll()
// struct gpioevent_request {
// __u32 lineoffset ;
// __u32 handleflags ;
// __u32 eventflags ;
// char consumer_label [32];
// int fd ; }
// struct gpioevent_data {
// __u64 timestamp ;
// __u32 id ; }
22
rv = ioctl ( fd , GPIO_GET_LINEEVENT_IOCTL , & req );
pfd . fd = ereq . fd ;
pfd . events = POLLIN | POLLPRI ;
rv = poll (& pfd , 1 , 1000); // 1000 = timeout 1 s
if ( rv >0) {
rd = read ( req . fd , & event , sizeof ( event ));
printf ( " Timestamp : % lld id % d \ n " ,
edata . timestamp , edata . id );
}
23
Circuit
GPIO18 3.3V
470 Ohm
GPIO17
1K
10K
24
Circuit Discussion
25
Debouncing! Noisy Switches
time
0 0 0 0 0 1 0 1 1 1
volts
time
Actual Switch Press
26
Debouncing!
27
Permissions!
28
Bypassing Linux for speed
http://codeandlife.com/2012/07/03/benchmarking-raspberry-pi-gpio-speed/
29
Appendix: Linux userspace sysfs interface
THIS IS INCLUDED FOR HISTORICAL PURPOSES
30
Linux GPIO interface
• Documentation/gpio/sysfs.txt
31
A few low-level Linux Coding Instructions
32
Enable a GPIO for use
To enable GPIO 17:
write “17” to /sys/class/gpio/export
To disable GPIO 17:
write “17” to /sys/class/gpio/unexport
33
Set GPIO Direction
To make GPIO 17 an input:
write “in” to /sys/class/gpio/gpio17/direction
To make GPIO 17 an output:
write “out” to /sys/class/gpio/gpio17/direction
34
Write GPIO Value
To write value of GPIO 17:
write /sys/class/gpio/gpio17/value
35
Read GPIO Value
To read value of GPIO 17:
read /sys/class/gpio/gpio17/value
char buffer [16];
fd = open ( " / sys / class / gpio / gpio17 / value " , O_RDONLY );
if ( fd <0) fprintf ( stderr , " Error opening !\ n " );
read ( fd , buffer ,16);
printf ( " Read % c from GPIO17 \ n " , buffer [0]);
close ( fd );
Also Note, if reading and you do not close after read you will have to rewind using
lseek(fd,0,SEEK SET); after your read.
36
Delay
37
Using fopen instead?
38
C Pitfalls
39
Waiting for Input
40
GPIO Interrupts on Linux
May need a recent version of Raspbian.
First write ”rising”, ”falling”, or ”both” to
/sys/class/gpio/gpio17/edge.
Then open
struct and
pollfd fds ; poll /sys/class/gpio/gpio17/value.
int result ;
41
Buffered “Stream” I/O
43