Raspberry Pi 5 IoT In C - Gpio5 |
Written by Harry Fairhead | ||||||||
Tuesday, 01 April 2025 | ||||||||
Page 4 of 4
ExamplesWith Gpio5 available it is very easy to write blinky by typing in a program targeting the Pico: #include <stdio.h> #include <stdlib.h> #include "Gpio5.h" int main(int argc, char **argv) { rp1_Init(); gpio_init(2); gpio_set_dir(2, true); while (true) { gpio_put(2, 1); sleep(1); gpio_put(2, 0); sleep(1); } return (EXIT_SUCCESS); } The only change that was required was adding the call to rp1_Init().
Another Pico program that runs without modification is to produce two pulses out of phase: #include <stdio.h> #include <stdlib.h> #include "Gpio5.h" int main(int argc, char **argv) { int pin1 = 4; int pin2 = 2; rp1_Init(); gpio_init(pin1); gpio_set_dir(pin1, true); gpio_init(pin2); gpio_set_dir(pin2, true); uint32_t mask = (1 << pin1) | (1 << pin2); uint32_t value1 = 1 << pin1; uint32_t value2 = 1 << pin2; while (true) { gpio_put_masked(mask, value1); gpio_put_masked(mask, value2); } return (EXIT_SUCCESS);
you will notice that the speed has reduced to 1.14µs. This is because of setting the state of the two lines at the same time without changing the other lines. The slowdown is due to the need to read the RIO. If you set the lines without worrying about reading the state of the other lines then you get back to 24ns pulses. Finally, here’s an example of using a GPIO line as input: #include <stdio.h> #include <stdlib.h> #include "Gpio5.h" int main(int argc, char **argv) { int pin1 = 4; int pin2 = 2; rp1_Init(); gpio_init(pin1); gpio_set_dir(pin1, false); gpio_pull_up(pin1); gpio_init(pin2); gpio_set_dir(pin2, true); while (true) { if (gpio_get(pin1)) { gpio_put(pin2, 0); } else { gpio_put(pin2, 1); } } return (EXIT_SUCCESS); } Again, the program is unchanged from a Pico program, except for the addition of rp1_Init(). The program simply tests for the line, GPIO4, to be pulled low by the switch being closed and then sets GPIO2 high. If you connect GPIO2 to an LED or a logic analyzer you will see the effect of the button being closed – the LED will light up while it is pressed. That is, GPIO2 is set high when GPIO4 is pulled low by the switch.
The program still works, but now GPIO2 is only high when the switch is pressed and hence, with no other changes to the program, the LED is on when the switch is not pressed. Interrupts?You may be wondering why the extensive list of Pico GPIO interrupt functions have not been added to Gpio5? The reason is that interrupts in Linux user space are not a good idea. It is part of the cost of using a complete operating system like Linux that things like raw interrupts are not allowed. In particular, the Linux kernel isn’t re-entrant and so a hardware interrupt that Linux knows nothing about, and could occur when the Linux kernel is running, is a very bad idea. The proper way to implement GPIO interrupts so as to integrate with Linux is to write a kernel driver that allows you to use epolling to wait on a file handle, as is done in gpiod. This is a fairly large task and raises the question, why not simply use gpiod? Another alternative to using interrupts is simply to create a thread to monitor by polling a GPIO line and then use this to call an event function as needed. Having said all of this, it is possible to work with raw GPIO interrupts, but there are some important restrictions. The first is that any interrupt handler should run for as short a time as possible and should not make any Linux system calls. This generally means that the interrupt routine cannot call any library functions that in turn make Linux system calls. Overall, this makes working with raw GPIO interrupts error prone and generally not very useful. Summary
Raspberry Pi 5 IoT In C
|
Microsoft Announces Hyperlight Wasm 01/04/2025 Microsoft has released Hyperlight Wasm, a Hyperlight virtual machine (VM) "micro-guest" that can run Wasm component workloads written in many programming languages. |
Software Security Report Finds Third Party Code Most Problematic 12/03/2025
The latest edition of Veracode's annual State of Software Security report has identified that 80% of the applications tested over the last year have at least one security flaw, and just u [ ... ] |
More News
|
Comments
or email your comment to: comments@i-programmer.info