mirror of
https://github.com/stianeikeland/go-rpio.git
synced 2025-01-23 10:41:03 +01:00
Add Pwm mode for pins
This commit is contained in:
parent
c0dfd7a7e3
commit
4a31f2cfa6
30
rpio.go
30
rpio.go
|
@ -91,11 +91,12 @@ func init() {
|
||||||
clkBase = base + clkOffset
|
clkBase = base + clkOffset
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pin mode, a pin can be set in Input or Output mode, or clock
|
// Pin mode, a pin can be set in Input or Output, Clock or Pwm mode
|
||||||
const (
|
const (
|
||||||
Input Mode = iota
|
Input Mode = iota
|
||||||
Output
|
Output
|
||||||
Clock
|
Clock
|
||||||
|
Pwm
|
||||||
)
|
)
|
||||||
|
|
||||||
// State of pin, High / Low
|
// State of pin, High / Low
|
||||||
|
@ -135,6 +136,11 @@ func (pin Pin) Clock() {
|
||||||
PinMode(pin, Clock)
|
PinMode(pin, Clock)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set pin as Pwm
|
||||||
|
func (pin Pin) Pwm() {
|
||||||
|
PinMode(pin, Pwm)
|
||||||
|
}
|
||||||
|
|
||||||
// Set pin High
|
// Set pin High
|
||||||
func (pin Pin) High() {
|
func (pin Pin) High() {
|
||||||
WritePin(pin, High)
|
WritePin(pin, High)
|
||||||
|
@ -150,6 +156,7 @@ func (pin Pin) Toggle() {
|
||||||
TogglePin(pin)
|
TogglePin(pin)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set frequency of Clock pin
|
||||||
func (pin Pin) Freq(freq int) {
|
func (pin Pin) Freq(freq int) {
|
||||||
SetFreq(pin, freq)
|
SetFreq(pin, freq)
|
||||||
}
|
}
|
||||||
|
@ -189,9 +196,10 @@ func (pin Pin) PullOff() {
|
||||||
PullMode(pin, PullOff)
|
PullMode(pin, PullOff)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PinMode sets the mode (direction) of a given pin (Input, Output or Clock)
|
// PinMode sets the mode (direction) of a given pin (Input, Output, Clock or Pwm)
|
||||||
//
|
//
|
||||||
// Clock is possible only for some pins (bcm 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
|
||||||
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
|
||||||
|
@ -199,6 +207,9 @@ func PinMode(pin Pin, mode Mode) {
|
||||||
shift := (uint8(pin) % 10) * 3
|
shift := (uint8(pin) % 10) * 3
|
||||||
f := uint32(0)
|
f := uint32(0)
|
||||||
|
|
||||||
|
const alt0 = 4 // 100
|
||||||
|
const alt5 = 2 // 010
|
||||||
|
|
||||||
switch mode {
|
switch mode {
|
||||||
case Input:
|
case Input:
|
||||||
f = 0 // 000
|
f = 0 // 000
|
||||||
|
@ -207,9 +218,18 @@ func PinMode(pin Pin, mode Mode) {
|
||||||
case Clock:
|
case Clock:
|
||||||
switch pin {
|
switch pin {
|
||||||
case 4, 5, 6, 32, 34, 42, 43, 44:
|
case 4, 5, 6, 32, 34, 42, 43, 44:
|
||||||
f = 4 // 100 - alt0
|
f = alt0
|
||||||
case 20, 21:
|
case 20, 21:
|
||||||
f = 2 // 010 - alt5
|
f = alt5
|
||||||
|
default:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case Pwm:
|
||||||
|
switch pin {
|
||||||
|
case 12, 13, 40, 41, 45:
|
||||||
|
f = alt0
|
||||||
|
case 18, 19:
|
||||||
|
f = alt5
|
||||||
default:
|
default:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user