mirror of
https://github.com/stianeikeland/go-rpio.git
synced 2025-03-21 20:48:41 +01:00
Update doc overview
This commit is contained in:
parent
a1c4bb376e
commit
ec49196c8c
97
rpio.go
97
rpio.go
|
@ -1,13 +1,15 @@
|
||||||
/*
|
/*
|
||||||
|
|
||||||
Package rpio provides GPIO access on the Raspberry PI without any need
|
Package rpio provides GPIO access on the Raspberry PI without any need
|
||||||
for external c libraries (ex: WiringPI or BCM2835).
|
for external c libraries (eg. WiringPi or BCM2835).
|
||||||
|
|
||||||
Supports simple operations such as:
|
Supports simple operations such as:
|
||||||
- Pin mode/direction (input/output/clock/pwm)
|
- Pin mode/direction (input/output/clock/pwm)
|
||||||
- Pin write (high/low)
|
- Pin write (high/low)
|
||||||
- Pin read (high/low)
|
- Pin read (high/low)
|
||||||
- Pull up/down/off
|
- Pull up/down/off
|
||||||
|
And clock/pwm related oparations:
|
||||||
|
- Set Clock frequency
|
||||||
|
- Set Duty cycle
|
||||||
|
|
||||||
Example of use:
|
Example of use:
|
||||||
|
|
||||||
|
@ -23,36 +25,40 @@ Example of use:
|
||||||
}
|
}
|
||||||
|
|
||||||
The library use the raw BCM2835 pinouts, not the ports as they are mapped
|
The library use the raw BCM2835 pinouts, not the ports as they are mapped
|
||||||
on the output pins for the raspberry pi
|
on the output pins for the raspberry pi, and not the wiringPi convention.
|
||||||
|
|
||||||
Rev 1 Raspberry Pi
|
Rev 2 and 3 Raspberry Pi Rev 1 Raspberry Pi (legacy)
|
||||||
+------+------+--------+
|
+-----+---------+----------+---------+-----+ +-----+--------+----------+--------+-----+
|
||||||
| GPIO | Phys | Name |
|
| BCM | Name | Physical | Name | BCM | | BCM | Name | Physical | Name | BCM |
|
||||||
+------+------+--------+
|
+-----+---------+----++----+---------+-----+ +-----+--------+----++----+--------+-----+
|
||||||
| 0 | 3 | SDA |
|
| | 3.3v | 1 || 2 | 5v | | | | 3.3v | 1 || 2 | 5v | |
|
||||||
| 1 | 5 | SCL |
|
| 2 | SDA 1 | 3 || 4 | 5v | | | 0 | SDA | 3 || 4 | 5v | |
|
||||||
| 4 | 7 | GPIO 7 |
|
| 3 | SCL 1 | 5 || 6 | 0v | | | 1 | SCL | 5 || 6 | 0v | |
|
||||||
| 7 | 26 | CE1 |
|
| 4 | GPIO 7 | 7 || 8 | TxD | 14 | | 4 | GPIO 7 | 7 || 8 | TxD | 14 |
|
||||||
| 8 | 24 | CE0 |
|
| | 0v | 9 || 10 | RxD | 15 | | | 0v | 9 || 10 | RxD | 15 |
|
||||||
| 9 | 21 | MISO |
|
| 17 | GPIO 0 | 11 || 12 | GPIO 1 | 18 | | 17 | GPIO 0 | 11 || 12 | GPIO 1 | 18 |
|
||||||
| 10 | 19 | MOSI |
|
| 27 | GPIO 2 | 13 || 14 | 0v | | | 21 | GPIO 2 | 13 || 14 | 0v | |
|
||||||
| 11 | 23 | SCLK |
|
| 22 | GPIO 3 | 15 || 16 | GPIO 4 | 23 | | 22 | GPIO 3 | 15 || 16 | GPIO 4 | 23 |
|
||||||
| 14 | 8 | TxD |
|
| | 3.3v | 17 || 18 | GPIO 5 | 24 | | | 3.3v | 17 || 18 | GPIO 5 | 24 |
|
||||||
| 15 | 10 | RxD |
|
| 10 | MOSI | 19 || 20 | 0v | | | 10 | MOSI | 19 || 20 | 0v | |
|
||||||
| 17 | 11 | GPIO 0 |
|
| 9 | MISO | 21 || 22 | GPIO 6 | 25 | | 9 | MISO | 21 || 22 | GPIO 6 | 25 |
|
||||||
| 18 | 12 | GPIO 1 |
|
| 11 | SCLK | 23 || 24 | CE0 | 8 | | 11 | SCLK | 23 || 24 | CE0 | 8 |
|
||||||
| 21 | 13 | GPIO 2 |
|
| | 0v | 25 || 26 | CE1 | 7 | | | 0v | 25 || 26 | CE1 | 7 |
|
||||||
| 22 | 15 | GPIO 3 |
|
| 0 | SDA 0 | 27 || 28 | SCL 0 | 1 | +-----+--------+----++----+--------+-----+
|
||||||
| 23 | 16 | GPIO 4 |
|
| 5 | GPIO 21 | 29 || 30 | 0v | |
|
||||||
| 24 | 18 | GPIO 5 |
|
| 6 | GPIO 22 | 31 || 32 | GPIO 26 | 12 |
|
||||||
| 25 | 22 | GPIO 6 |
|
| 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:
|
See the spec for full details of the BCM2835 controller:
|
||||||
http://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
|
|
||||||
|
https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf
|
||||||
|
and https://elinux.org/BCM2835_datasheet_errata - for errors in that spec
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package rpio
|
package rpio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -208,8 +214,8 @@ func (pin Pin) PullOff() {
|
||||||
|
|
||||||
// PinMode sets the mode (direction) of a given pin (Input, Output, Clock or Pwm)
|
// PinMode sets the mode (direction) of a given pin (Input, Output, Clock or Pwm)
|
||||||
//
|
//
|
||||||
// Clock is possible only for pins 4, 5, 6, 20, 21
|
// Clock is possible only for pins 4, 5, 6, 20, 21.
|
||||||
// Pwm is possible only for pins 12, 13, 18, 19
|
// Pwm is possible only for pins 12, 13, 18, 19.
|
||||||
func PinMode(pin Pin, mode Mode) {
|
func PinMode(pin Pin, mode Mode) {
|
||||||
|
|
||||||
// Pin fsel register, 0 or 1 depending on bank
|
// Pin fsel register, 0 or 1 depending on bank
|
||||||
|
@ -330,13 +336,16 @@ func PullMode(pin Pin, pull Pull) {
|
||||||
// Set clock speed for given pin in Clock or Pwm mode
|
// Set clock speed for given pin in Clock or Pwm mode
|
||||||
//
|
//
|
||||||
// Param freq should be in range 4688Hz - 19.2MHz to prevent unexpected behavior,
|
// Param freq should be in range 4688Hz - 19.2MHz to prevent unexpected behavior,
|
||||||
// hovewer output frequency of Pwm pins can be further adjusted with setDutyCycle.
|
// however output frequency of Pwm pins can be further adjusted with SetDutyCycle.
|
||||||
// (Thus for smaller frequencies use Pwm pin with large cycle range or implement custom software clock using output pin and sleep.)
|
// So for smaller frequencies use Pwm pin with large cycle range. (Or implement custom software clock using output pin and sleep.)
|
||||||
//
|
//
|
||||||
// Note that some pins share the same clock source, it means that
|
// Note that some pins share the same clock source, it means that
|
||||||
// changing frequency for one pin will change it also for all pins within a group
|
// changing frequency for one pin will change it also for all pins within a group.
|
||||||
// The groups are: clk0 (4, 20, 32, 34), clk1 (5, 21, 42, 43) and clk2 (6 and 43).
|
// The groups are:
|
||||||
// Also all pwm pins (12, 13, 18, 19, 40, 41, 45) share same source clock.
|
// gp_clk0: pins 4, 20, 32, 34
|
||||||
|
// gp_clk1: pins 5, 21, 42, 43
|
||||||
|
// gp_clk2: pins 6 and 43
|
||||||
|
// pwm_clk: pins 12, 13, 18, 19, 40, 41, 45
|
||||||
func SetFreq(pin Pin, freq int) {
|
func SetFreq(pin Pin, freq int) {
|
||||||
// TODO: would be nice to choose best clock source depending on target frequency, oscilator is used for now
|
// TODO: would be nice to choose best clock source depending on target frequency, oscilator is used for now
|
||||||
const sourceFreq = 19200000 // oscilator frequency
|
const sourceFreq = 19200000 // oscilator frequency
|
||||||
|
@ -398,23 +407,25 @@ func SetFreq(pin Pin, freq int) {
|
||||||
// NOTE without root permission this changes will simply do nothing successfully
|
// NOTE without root permission this changes will simply do nothing successfully
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set cycle length (range) and duty length (data) for pwm in M/S mode
|
// Set cycle length (range) and duty length (data) for Pwm pin in M/S mode
|
||||||
//
|
//
|
||||||
// |<- duty ->|
|
// |<- duty ->|
|
||||||
// __________
|
// __________
|
||||||
// _/ \_____________/
|
// _/ \_____________/
|
||||||
// |<------- cycle -------->|
|
// |<------- cycle -------->|
|
||||||
//
|
//
|
||||||
// Output frequency is computed as pwm clock frequency divided by cycle length
|
// Output frequency is computed as pwm clock frequency divided by cycle length.
|
||||||
// So, to set Pwm pin to freqency 38kHz with duty cycle 1/4 use this combination:
|
// So, to set Pwm pin to freqency 38kHz with duty cycle 1/4, use this combination:
|
||||||
//
|
//
|
||||||
// pin.Pwm()
|
// pin.Pwm()
|
||||||
// pin.DutyCycle(1, 4)
|
// pin.DutyCycle(1, 4)
|
||||||
// pin.Freq(38000*4)
|
// pin.Freq(38000*4)
|
||||||
//
|
//
|
||||||
// Note that some pins share common pwm channel,
|
// Note that some pins share common pwm channel,
|
||||||
// so calling this function will set same duty cycle for all pins belongig to channel.
|
// so calling this function will set same duty cycle for all pins belonging to channel.
|
||||||
// It is channel 1 (pwm0) for pins 12, 18, 40, and channel 2 (pwm1) for pins 13, 19, 41, 45.
|
// The channels are:
|
||||||
|
// channel 1 (pwm0) for pins 12, 18, 40
|
||||||
|
// channel 2 (pwm1) for pins 13, 19, 41, 45.
|
||||||
func SetDutyCycle(pin Pin, dutyLen, cycleLen uint32) {
|
func SetDutyCycle(pin Pin, dutyLen, cycleLen uint32) {
|
||||||
const pwmCtlReg = 0
|
const pwmCtlReg = 0
|
||||||
var (
|
var (
|
||||||
|
|
Loading…
Reference in New Issue
Block a user