Skip to content

Commit 055833b

Browse files
committed
fix: Update gpio and docs
1 parent 9b46bd2 commit 055833b

2 files changed

Lines changed: 35 additions & 31 deletions

File tree

docs/source/gpio-tutorial.rst

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This programming example describes how to directly access the registers which co
1717
// start address of the I/O peripheral register space on the VideoCore bus
1818
#define BUS_REG_BASE 0x7E000000
1919
// start address of the I/O peripheral register space seen from the CPU bus
20-
#define PHYS_REG_BASE 0xFE000000 // RPi 4
20+
#define PHYS_REG_BASE 0xFE000000 // RPi 4
2121
// start address of the GPIO register space on the VideoCore bus
2222
#define GPIO_BASE 0x7E200000
2323
// address offsets for the individual registers
@@ -27,7 +27,7 @@ This programming example describes how to directly access the registers which co
2727
#define GPIO_SET0 0x1C // set outputs to '1' GPIO 0-31
2828
#define GPIO_CLR0 0x28 // set outputs to '0' GPIO 0-31
2929
#define GPIO_LEV0 0x34 // get input states GPIO 0-31
30-
30+
3131
// calculate the GPIO register physical address from the bus address
3232
uint32_t gpio_phys_addr = GPIO_BASE - BUS_REG_BASE + PHYS_REG_BASE;
3333
@@ -59,18 +59,18 @@ Finally, the GPIO mode is set for a given pin which then can be used for output
5959
*gpclr0 = 1 << 27; // set output to '0'
6060
*gpset0 = 1 << 27; // set output to '1'
6161
*gpclr0 = 1 << 27; // set output to '0'
62-
*gpfsel2 = 0; // set default mode (all GPIO become inputs)
62+
*gpfsel2 = 0; // set default mode (all GPIO become inputs)
6363
munmap(gpio_virt_addr_ptr, 0x1000); // free allocated memory
6464
6565
.. warning::
6666
The function ``mmap("dev/mem/"...)`` returns a handle which allows unrestricted access to system wide memory and I/O resources. Since this is a security sensitive access, it can only be executed with elevated access rights. Therefore, programs using that kind of functions have to be called as super user ``sudo -E ./<program_name>``.
67-
67+
6868
Python GPIO Example
6969
--------------------
70-
The **Python** example uses the `Rpi.GPIO library <https://sourceforge.net/p/raspberry-gpio-python/wiki/Home/>`_ library. Setting up the access to the GPIO registers is done in a similar way as in the C-code example. However, the detailed implementation is hidden in the library.
70+
The **Python** example uses the `Rpi.GPIO library <https://sourceforge.net/p/raspberry-gpio-python/wiki/Home/>`_ library. Setting up the access to the GPIO registers is done in a similar way as in the C-code example. However, the detailed implementation is hidden in the library.
7171

7272
.. code-block:: python
73-
73+
7474
# import the library and define the prefix for using its members
7575
import RPi.GPIO as GPIO
7676
@@ -94,52 +94,54 @@ The **Python** example uses the `Rpi.GPIO library <https://sourceforge.net/p/ras
9494
9595
# set GPIO configuration back to default
9696
GPIO.cleanup()
97-
98-
Exercises
99-
---------
97+
98+
Exercises
99+
---------
100100

101101
.. admonition:: Exercise 1. GPIO programming with C-code
102102

103103
Copy the file :file:`gpio.c` from the :file:`code/GPIO` folder to your :file:`work` folder. Compile (``CTRL+Shift+b`` or ``Terminal->Run build Task``) and run the program by typing :file:`sudo -E ./gpio` into a terminal from within your :file:`work` folder.
104-
104+
105105
If you receive an error about 'gpio' not found, double check that you've compiled the source code into a binary with the correct file name.
106106

107107
1. Connect an oscilloscope probe to the GPIO27 pin (red LED of the RGB LED) on the base board and adjust the oscilloscope setting such that it triggers on the output pulse when the GPIO program runs. Make sure you select an appropriate horizontal resolution because the pulse will be very narrow (~ 30ns).
108-
2. Add a loop statement around the code which toggles the GPIO output state to produce a stream of output pulses.
109-
3. Measure the output average pulse width and its peak-to-peak jitter (i.e. the minimum and maximum width).
108+
2. Add a loop statement around the code which toggles the GPIO output state to produce a stream of output pulses.
109+
3. Measure the output average pulse width and its peak-to-peak jitter (i.e. the minimum and maximum width).
110110
4. Modify the code to extend the pulse width by inserting additional function calls between the writes to GPSET and GPCLR registers:
111-
111+
112112
* ``asm("nop")``, adds the smallest possible delay by inserting a ``NOP`` command (no operation) into the loop
113113
* ``usleep(<some number>)``, adds delay in microseconds units
114114
* ``sleep(<some number>)``, adds delay in second units (for visible blinking LED, for example)
115-
115+
116116
Measure the pulse width again for the different pulse width modifications. What happens when the CPU runs other tasks while the output is toggling (start another application or just move a window with the mouse). Explain what you see.
117117

118-
.. admonition:: Exercise 2. GPIO programming with Python
118+
.. admonition:: Exercise 2. GPIO programming with Python
119119

120120
Copy the file :file:`gpio.py` from the :file:`code/GPIO` folder to your :file:`work` folder. Proceed similar to the tasks in the C-code exercises.
121121

122122
1. If not yet done, connect an oscilloscope probe to the GPIO27 pin (red LED of the RGB LED) on the base board and adjust the oscilloscope setting such that it triggers on the output pulse when the GPIO scripts runs. What is the pulse width now?
123-
2. Add a loop statement around the code which toggles the GPIO output state to produce a stream of output pulses.
124-
3. Measure the output average pulse width and its peak-to-peak jitter (i.e. the minimum and maximum width).
125-
4. Compare the minimum pulse width as generated by the C-code and the Python implementations.
126-
5. Increase the pulse width by inserting calls to ``sleep()`` (add ``import time`` at the top of your script).
127-
6. Adjust both C- and Python codes to generate a ~100 us pulse. How stable is the pulse width? Is there a difference between the C-code and Python implementation?
128-
123+
2. Add a loop statement around the code which toggles the GPIO output state to produce a stream of output pulses.
124+
3. Measure the output average pulse width and its peak-to-peak jitter (i.e. the minimum and maximum width).
125+
4. Compare the minimum pulse width as generated by the C-code and the Python implementations.
126+
5. Increase the pulse width by inserting calls to ``sleep()`` (add ``import time`` at the top of your script).
127+
6. Adjust both C- and Python codes to generate a ~100 us pulse. How stable is the pulse width? Is there a difference between the C-code and Python implementation?
128+
129+
Note: You must name files carefully, to not conflict with libraries you're already using, like `serial`.
130+
129131
.. admonition:: Exercise 3. Serial UART communication with Python
130-
132+
131133
The goal of this exercise is to implement a simple terminal program running on two Raspberry Pi boards (or a board with itself, in loopback) and to establish a serial link using the UART interface on GPIO pins 14 (TX) and 15 (RX).
132134

133135
Prerequisites:
134136
- In Raspberry Pi Configuration GUI ensure 'Serial port' is enabled, and 'Serial Console' is disabled. If a change was necessary, you must reboot for it to take effect.
135137
- A Python library that instantiates a serial port object (for example PySerial) and allows sending and receiving data.
136-
- A direct connection between RX and TX pins (loop-back) on a single board for testing the script.
138+
- A direct connection between RX and TX pins (loop-back) on a single board for testing the script.
137139
- A cross-over connection for making the RX-TX / TX-RX connection between two boards.
138-
140+
139141
Tasks:
140142
- Write a script, using the `PySerial library <https://pyserial.readthedocs.io/en/latest/shortintro.html>`_, to transmit user input strings across the serial interface. Make sure you properly convert strings to a binary format. (Hint: `str.encode() <https://docs.python.org/3/library/stdtypes.html#str.encode>`_)
141-
- Connect an oscilloscope to RX (TX) pins and examine the waveform. Set various serial port configuration parameters (baud rate, number of stop bits, parity) and explain their effect.
143+
- Connect an oscilloscope to RX (TX) pins and examine the waveform. Set various serial port configuration parameters (baud rate, number of stop bits) and explain their effect. (Note: The parity option is pyserial is bugged and shouldn't be used.)
142144
- Connect the serial link between two boards connecting TX of one board to RX of the other board and vice versa.
143-
- Make sure the serial configuration is the same on both boards and send and receive data.
145+
- Make sure the serial configuration is the same on both boards and send and receive data. Be sure to `reset_output_buffer()` before sending data, to ensure only a fresh message is read.
144146
- What happens if the settings are not the same on both boards?
145147
- Extend your script to send and receive binary files.

tmux_ini.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
#!/bin/bash -x
1+
#!/bin/bash
22

33
ssh_list=( pi@pilab01.physik.uni-bonn.de \
44
pi@pilab02.physik.uni-bonn.de \
55
pi@pilab03.physik.uni-bonn.de \
66
pi@pilab04.physik.uni-bonn.de \
77
pi@pilab05.physik.uni-bonn.de \
88
pi@pilab06.physik.uni-bonn.de \
9-
)
9+
pi@pilab08.physik.uni-bonn.de \
10+
)
11+
1012
split_list=()
1113
for ssh_entry in "${ssh_list[@]:1}"; do
12-
split_list+=( split-pane -v -p 30 -h -p 50 ssh "$ssh_entry" ';' select-layout tiled ';' )
14+
split_list+=( split-pane -h ssh "$ssh_entry" ';' select-layout tiled ';' )
1315
done
1416

15-
tmux new-session ssh "${ssh_list[0]}" ';' \
17+
tmux new-session -s pilab ssh "${ssh_list[0]}" ';' \
1618
"${split_list[@]}" \
17-
set-option -w synchronize-panes
19+
set-option -w synchronize-panes on

0 commit comments

Comments
 (0)