add an example to test PWM balanced mode

This commit is contained in:
youngkin 2021-12-01 22:14:59 +00:00
parent 915fc229cf
commit c34d95b74c
2 changed files with 89 additions and 45 deletions

View File

@ -0,0 +1,44 @@
/*
A PWM example by @youngkin, using the go-rpio library
Fades a PWM hardware pin in and out using PWM mode balanced (vs. markspace)
*/
package main
import (
"os"
"time"
"github.com/stianeikeland/go-rpio/v4"
)
func main() {
err := rpio.Open()
if err != nil {
os.Exit(1)
}
defer rpio.Close()
pin := rpio.Pin(19)
pin.Mode(rpio.Pwm)
pin.Freq(64000)
pin.DutyCycleWithPwmMode(0, 32, rpio.Balanced)
// the LED will be blinking at 2000Hz
// (source frequency divided by cycle length => 64000/32 = 2000)
// five times smoothly fade in and out
for i := 0; i < 5; i++ {
for i := uint32(0); i < 32; i++ { // increasing brightness
pin.DutyCycleWithPwmMode(i, 32, rpio.Balanced)
time.Sleep(time.Second / 32)
}
for i := uint32(32); i > 0; i-- { // decreasing brightness
pin.DutyCycleWithPwmMode(i, 32, rpio.Balanced)
time.Sleep(time.Second / 32)
}
}
pin.DutyCycleWithPwmMode(0, 32, rpio.Balanced)
}

90
rpio.go
View File

@ -3,65 +3,65 @@ Package rpio provides GPIO access on the Raspberry PI without any need
for external c libraries (eg. WiringPi or BCM2835).
Supports simple operations such as:
- Pin mode/direction (input/output/clock/pwm,alt0,alt1,alt2,alt3,alt4,alt5)
- Pin write (high/low)
- Pin read (high/low)
- Pin edge detection (no/rise/fall/any)
- Pull up/down/off
- Pin mode/direction (input/output/clock/pwm,alt0,alt1,alt2,alt3,alt4,alt5)
- Pin write (high/low)
- Pin read (high/low)
- Pin edge detection (no/rise/fall/any)
- Pull up/down/off
Also clock/pwm related oparations:
- Set Clock frequency
- Set Duty cycle
- Set Clock frequency
- Set Duty cycle
And SPI oparations:
- SPI transmit/recieve/exchange bytes
- Chip select
- Set speed
- SPI transmit/recieve/exchange bytes
- Set speed
- Chip select
Example of use:
rpio.Open()
defer rpio.Close()
rpio.Open()
defer rpio.Close()
pin := rpio.Pin(4)
pin.Output()
pin := rpio.Pin(4)
pin.Output()
for {
pin.Toggle()
time.Sleep(time.Second)
}
for {
pin.Toggle()
time.Sleep(time.Second)
}
The library use the raw BCM2835 pinouts, not the ports as they are mapped
on the output pins for the raspberry pi, and not the wiringPi convention.
Rev 2 and 3 Raspberry Pi Rev 1 Raspberry Pi (legacy)
+-----+---------+----------+---------+-----+ +-----+--------+----------+--------+-----+
| BCM | Name | Physical | Name | BCM | | BCM | Name | Physical | Name | BCM |
+-----+---------+----++----+---------+-----+ +-----+--------+----++----+--------+-----+
| | 3.3v | 1 || 2 | 5v | | | | 3.3v | 1 || 2 | 5v | |
| 2 | SDA 1 | 3 || 4 | 5v | | | 0 | SDA | 3 || 4 | 5v | |
| 3 | SCL 1 | 5 || 6 | 0v | | | 1 | SCL | 5 || 6 | 0v | |
| 4 | GPIO 7 | 7 || 8 | TxD | 14 | | 4 | GPIO 7 | 7 || 8 | TxD | 14 |
| | 0v | 9 || 10 | RxD | 15 | | | 0v | 9 || 10 | RxD | 15 |
| 17 | GPIO 0 | 11 || 12 | GPIO 1 | 18 | | 17 | GPIO 0 | 11 || 12 | GPIO 1 | 18 |
| 27 | GPIO 2 | 13 || 14 | 0v | | | 21 | GPIO 2 | 13 || 14 | 0v | |
| 22 | GPIO 3 | 15 || 16 | GPIO 4 | 23 | | 22 | GPIO 3 | 15 || 16 | GPIO 4 | 23 |
| | 3.3v | 17 || 18 | GPIO 5 | 24 | | | 3.3v | 17 || 18 | GPIO 5 | 24 |
| 10 | MOSI | 19 || 20 | 0v | | | 10 | MOSI | 19 || 20 | 0v | |
| 9 | MISO | 21 || 22 | GPIO 6 | 25 | | 9 | MISO | 21 || 22 | GPIO 6 | 25 |
| 11 | SCLK | 23 || 24 | CE0 | 8 | | 11 | SCLK | 23 || 24 | CE0 | 8 |
| | 0v | 25 || 26 | CE1 | 7 | | | 0v | 25 || 26 | CE1 | 7 |
| 0 | SDA 0 | 27 || 28 | SCL 0 | 1 | +-----+--------+----++----+--------+-----+
| 5 | GPIO 21 | 29 || 30 | 0v | |
| 6 | GPIO 22 | 31 || 32 | GPIO 26 | 12 |
| 13 | GPIO 23 | 33 || 34 | 0v | |
| 19 | GPIO 24 | 35 || 36 | GPIO 27 | 16 |
| 26 | GPIO 25 | 37 || 38 | GPIO 28 | 20 |
| | 0v | 39 || 40 | GPIO 29 | 21 |
+-----+---------+----++----+---------+-----+
Rev 2 and 3 Raspberry Pi Rev 1 Raspberry Pi (legacy)
+-----+---------+----------+---------+-----+ +-----+--------+----------+--------+-----+
| BCM | Name | Physical | Name | BCM | | BCM | Name | Physical | Name | BCM |
+-----+---------+----++----+---------+-----+ +-----+--------+----++----+--------+-----+
| | 3.3v | 1 || 2 | 5v | | | | 3.3v | 1 || 2 | 5v | |
| 2 | SDA 1 | 3 || 4 | 5v | | | 0 | SDA | 3 || 4 | 5v | |
| 3 | SCL 1 | 5 || 6 | 0v | | | 1 | SCL | 5 || 6 | 0v | |
| 4 | GPIO 7 | 7 || 8 | TxD | 14 | | 4 | GPIO 7 | 7 || 8 | TxD | 14 |
| | 0v | 9 || 10 | RxD | 15 | | | 0v | 9 || 10 | RxD | 15 |
| 17 | GPIO 0 | 11 || 12 | GPIO 1 | 18 | | 17 | GPIO 0 | 11 || 12 | GPIO 1 | 18 |
| 27 | GPIO 2 | 13 || 14 | 0v | | | 21 | GPIO 2 | 13 || 14 | 0v | |
| 22 | GPIO 3 | 15 || 16 | GPIO 4 | 23 | | 22 | GPIO 3 | 15 || 16 | GPIO 4 | 23 |
| | 3.3v | 17 || 18 | GPIO 5 | 24 | | | 3.3v | 17 || 18 | GPIO 5 | 24 |
| 10 | MOSI | 19 || 20 | 0v | | | 10 | MOSI | 19 || 20 | 0v | |
| 9 | MISO | 21 || 22 | GPIO 6 | 25 | | 9 | MISO | 21 || 22 | GPIO 6 | 25 |
| 11 | SCLK | 23 || 24 | CE0 | 8 | | 11 | SCLK | 23 || 24 | CE0 | 8 |
| | 0v | 25 || 26 | CE1 | 7 | | | 0v | 25 || 26 | CE1 | 7 |
| 0 | SDA 0 | 27 || 28 | SCL 0 | 1 | +-----+--------+----++----+--------+-----+
| 5 | GPIO 21 | 29 || 30 | 0v | |
| 6 | GPIO 22 | 31 || 32 | GPIO 26 | 12 |
| 13 | GPIO 23 | 33 || 34 | 0v | |
| 19 | GPIO 24 | 35 || 36 | GPIO 27 | 16 |
| 26 | GPIO 25 | 37 || 38 | GPIO 28 | 20 |
| | 0v | 39 || 40 | GPIO 29 | 21 |
+-----+---------+----++----+---------+-----+
See the spec for full details of the BCM2835 controller:
https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf
and https://elinux.org/BCM2835_datasheet_errata - for errors in that spec
https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf
and https://elinux.org/BCM2835_datasheet_errata - for errors in that spec
Changes to support the BCM2711, used on the Raspberry Pi 4, were cribbed from https://github.com/RPi-Distro/raspi-gpio/