Design Review: pinctrl01.sh
Script Information
- File: RZGC130gcc/pinctrl01.sh
- Date: 20250302
- Description: Write and read GPIO using pinctrl and awk of shell command
- Board: Raspberry Pi Zero W rev 1.1
- Uname: Linux rpz130 6.6.74+rpt-rpi-v6 #1 Raspbian 1:6.6.74-1+rpt1 (2025-01-27) armv6l GNU/Linux
- OS: PRETTY_NAME="Raspbian GNU/Linux 12 (bookworm)"
- Language: Shell script
- IDE: VS code with ssh, sftp on Windows 11
Script Code
#!/bin/bash
# RZGC130gcc/pinctrl01.sh 20250302
# description: write and read GPIO using pinctrl and awk of shell command
# board: Raspberry Pi Zero W rev 1.1
# uname: Linux rpz130 6.6.74+rpt-rpi-v6 #1 Raspbian 1:6.6.74-1+rpt1 (2025-01-27) armv6l GNU/Linux
# os: PRETTY_NAME="Raspbian GNU/Linux 12 (bookworm)"
# language: shell script
# IDE: VS code with ssh,sftp on windows11
# cd ~/RZIH130
# sudo chmod +x pinctrl01.sh
# ./pinctrl01.sh
echo "(codeId:RZIH130)"
date
pinctrl set 17 op ; pinctrl set 27 op ; pinctrl set 22 op
pinctrl get 17; pinctrl get 27; pinctrl get 22
pinctrl set 17 dh ; pinctrl get 17 | awk '{printf("%s\n" ,$6 == "hi" ? 1:0 )}'
sleep 1
pinctrl set 17 dl ; pinctrl get 17 | awk '{printf("%s\n" ,$6 == "hi" ? 1:0 )}'
pinctrl set 27 dh ; pinctrl get 27 | awk '{printf("%s\n" ,$6 == "hi" ? 1:0 )}'
sleep 1
pinctrl set 27 dl ; pinctrl get 27 | awk '{printf("%s\n" ,$6 == "hi" ? 1:0 )}'
pinctrl set 22 dh ; pinctrl get 22 | awk '{printf("%s\n" ,$6 == "hi" ? 1:0 )}'
sleep 1
pinctrl set 22 dl ; pinctrl get 22 | awk '{printf("%s\n" ,$6 == "hi" ? 1:0 )}'
Execution Result
(codeId:RZIH130)
Sun 2 Mr 12:55:29 JST 2025
17: op - -- | lo // GPIO17 = output
27: op - -- | lo // GPIO27 = output
22: op - -- | lo // GPIO22 = output
1
0
1
0
1
0
Review Points
- Typo Correction: The script initially had a typo (
pintctrl instead of pinctrl). This has been corrected.
- GPIO Configuration: The script correctly sets GPIO pins 17, 27, and 22 as output pins.
- GPIO High/Low Control: The script effectively toggles the GPIO pins between high and low states, verifying the changes using
pinctrl get and awk.
- Readability: The script is generally readable, with comments explaining the purpose of each section.
- Error Handling: The script lacks error handling. It would be beneficial to add checks for command execution success (e.g., using
$?).
- Output Clarity: the output is now clear.
- Spacing: added spacing around the `==` operator in the awk command.
Recommendations
- Add Error Handling: Implement error checks after each
pinctrl command to ensure they execute successfully.
- Improve Comments: Add more detailed comments, especially for the
awk command, to explain its functionality.
- Consider Function Usage: for larger scripts, using functions to group repetitive code (like setting and reading each pin) can improve readability and maintainability.