mirror of
https://github.com/stianeikeland/go-rpio.git
synced 2025-02-02 15:30:36 +01:00
Improve doc
This commit is contained in:
parent
b1c90c6087
commit
a1c4bb376e
43
rpio.go
43
rpio.go
|
@ -161,12 +161,12 @@ func (pin Pin) Toggle() {
|
||||||
TogglePin(pin)
|
TogglePin(pin)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set frequency of Clock pin
|
// Set frequency of Clock or Pwm pin (see doc of SetFreq)
|
||||||
func (pin Pin) Freq(freq int) {
|
func (pin Pin) Freq(freq int) {
|
||||||
SetFreq(pin, freq)
|
SetFreq(pin, freq)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set duty cycle for pwm pin
|
// Set duty cycle for Pwm pin (see doc of SetDutyCycle)
|
||||||
func (pin Pin) DutyCycle(dutyLen, cycleLen uint32) {
|
func (pin Pin) DutyCycle(dutyLen, cycleLen uint32) {
|
||||||
SetDutyCycle(pin, dutyLen, cycleLen)
|
SetDutyCycle(pin, dutyLen, cycleLen)
|
||||||
}
|
}
|
||||||
|
@ -329,14 +329,14 @@ 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
|
||||||
//
|
//
|
||||||
// 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,
|
||||||
// (for smaller frequencies use Pwm pin with large cycle range or implement custom software clock using output pin and sleep)
|
// hovewer 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.)
|
||||||
//
|
//
|
||||||
// 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: clk0 (4, 20, 32, 34), clk1 (5, 21, 42, 43) and clk2 (6 and 43).
|
||||||
// Also all pwm pins (12, 13, 18, 19, 40, 41, 45) share same source clock,
|
// Also all pwm pins (12, 13, 18, 19, 40, 41, 45) share same source clock.
|
||||||
// but final output frequency of pwm chanel can be adjusted individually with setDutyCycle
|
|
||||||
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
|
||||||
|
@ -360,7 +360,7 @@ func SetFreq(pin Pin, freq int) {
|
||||||
case 6, 43: // clk2
|
case 6, 43: // clk2
|
||||||
clkCtlReg += 4
|
clkCtlReg += 4
|
||||||
clkDivReg += 5
|
clkDivReg += 5
|
||||||
case 12, 13, 40, 41, 45, 18, 19: // pwm_clk - shared clk for both pwm chanels
|
case 12, 13, 40, 41, 45, 18, 19: // pwm_clk - shared clk for both pwm channels
|
||||||
clkCtlReg += 12
|
clkCtlReg += 12
|
||||||
clkDivReg += 13
|
clkDivReg += 13
|
||||||
StopPwm() // pwm clk busy wont go down without stopping pwm first
|
StopPwm() // pwm clk busy wont go down without stopping pwm first
|
||||||
|
@ -402,12 +402,19 @@ func SetFreq(pin Pin, freq int) {
|
||||||
//
|
//
|
||||||
// |<- duty ->|
|
// |<- duty ->|
|
||||||
// __________
|
// __________
|
||||||
// _/ \___________/
|
// _/ \_____________/
|
||||||
// |<------ cycle ------->|
|
// |<------- cycle -------->|
|
||||||
//
|
//
|
||||||
// Note that some pins share common pwm chanel,
|
// Output frequency is computed as pwm clock frequency divided by cycle length
|
||||||
// so calling this function will set same duty cycle for all pins belonig to chanel
|
// So, to set Pwm pin to freqency 38kHz with duty cycle 1/4 use this combination:
|
||||||
// Its chanel pwm0 for pins 12, 18, 40, and pwm1 for pins 13, 19, 41, 45
|
//
|
||||||
|
// pin.Pwm()
|
||||||
|
// pin.DutyCycle(1, 4)
|
||||||
|
// pin.Freq(38000*4)
|
||||||
|
//
|
||||||
|
// Note that some pins share common pwm channel,
|
||||||
|
// so calling this function will set same duty cycle for all pins belongig to channel.
|
||||||
|
// It is channel 1 (pwm0) for pins 12, 18, 40, and 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 (
|
||||||
|
@ -417,11 +424,11 @@ func SetDutyCycle(pin Pin, dutyLen, cycleLen uint32) {
|
||||||
)
|
)
|
||||||
|
|
||||||
switch pin {
|
switch pin {
|
||||||
case 12, 18, 40: // chanel pwm0
|
case 12, 18, 40: // channel pwm0
|
||||||
pwmRngReg = 4
|
pwmRngReg = 4
|
||||||
pwmDatReg = 5
|
pwmDatReg = 5
|
||||||
shift = 0
|
shift = 0
|
||||||
case 13, 19, 41, 45: // chanel pwm1
|
case 13, 19, 41, 45: // channel pwm1
|
||||||
pwmRngReg = 8
|
pwmRngReg = 8
|
||||||
pwmDatReg = 9
|
pwmDatReg = 9
|
||||||
shift = 8
|
shift = 8
|
||||||
|
@ -429,7 +436,7 @@ func SetDutyCycle(pin Pin, dutyLen, cycleLen uint32) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const ctlMask = 255 // ctl setting has 8 bits for each chanel
|
const ctlMask = 255 // ctl setting has 8 bits for each channel
|
||||||
const pwen = 1 << 0 // enable pwm
|
const pwen = 1 << 0 // enable pwm
|
||||||
const msen = 1 << 7 // use M/S transition instead of pwm algorithm
|
const msen = 1 << 7 // use M/S transition instead of pwm algorithm
|
||||||
|
|
||||||
|
@ -443,14 +450,14 @@ func SetDutyCycle(pin Pin, dutyLen, cycleLen uint32) {
|
||||||
// NOTE without root permission this changes will simply do nothing successfully
|
// NOTE without root permission this changes will simply do nothing successfully
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop pwm for both chanels
|
// Stop pwm for both channels
|
||||||
func StopPwm() {
|
func StopPwm() {
|
||||||
const pwmCtlReg = 0
|
const pwmCtlReg = 0
|
||||||
const pwen = 1
|
const pwen = 1
|
||||||
pwmMem[pwmCtlReg] = pwmMem[pwmCtlReg] &^ (pwen<<8 | pwen)
|
pwmMem[pwmCtlReg] = pwmMem[pwmCtlReg] &^ (pwen<<8 | pwen)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start pwm for both chanels
|
// Start pwm for both channels
|
||||||
func StartPwm() {
|
func StartPwm() {
|
||||||
const pwmCtlReg = 0
|
const pwmCtlReg = 0
|
||||||
const pwen = 1
|
const pwen = 1
|
||||||
|
|
Loading…
Reference in New Issue
Block a user