You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
68
68
Python GPIO Example
69
69
--------------------
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.
71
71
72
72
.. code-block:: python
73
-
73
+
74
74
# import the library and define the prefix for using its members
75
75
import RPi.GPIOasGPIO
76
76
@@ -94,52 +94,54 @@ The **Python** example uses the `Rpi.GPIO library <https://sourceforge.net/p/ras
94
94
95
95
# set GPIO configuration back to default
96
96
GPIO.cleanup()
97
-
98
-
Exercises
99
-
---------
97
+
98
+
Exercises
99
+
---------
100
100
101
101
.. admonition:: Exercise 1. GPIO programming with C-code
102
102
103
103
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
+
105
105
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.
106
106
107
107
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).
110
110
4. Modify the code to extend the pulse width by inserting additional function calls between the writes to GPSET and GPCLR registers:
111
-
111
+
112
112
* ``asm("nop")``, adds the smallest possible delay by inserting a ``NOP`` command (no operation) into the loop
113
113
* ``usleep(<some number>)``, adds delay in microseconds units
114
114
* ``sleep(<some number>)``, adds delay in second units (for visible blinking LED, for example)
115
-
115
+
116
116
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.
117
117
118
-
.. admonition:: Exercise 2. GPIO programming with Python
118
+
.. admonition:: Exercise 2. GPIO programming with Python
119
119
120
120
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.
121
121
122
122
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
+
129
131
.. admonition:: Exercise 3. Serial UART communication with Python
130
-
132
+
131
133
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).
132
134
133
135
Prerequisites:
134
136
- 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.
135
137
- 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.
137
139
- A cross-over connection for making the RX-TX / TX-RX connection between two boards.
138
-
140
+
139
141
Tasks:
140
142
- 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.)
142
144
- 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.
144
146
- What happens if the settings are not the same on both boards?
145
147
- Extend your script to send and receive binary files.
0 commit comments