diff --git a/src/examples/pwm/pwm.go b/src/examples/pwm/pwm.go index 96220ee5..380fbaa2 100644 --- a/src/examples/pwm/pwm.go +++ b/src/examples/pwm/pwm.go @@ -28,13 +28,16 @@ func main() { machine.InitPWM() red := machine.PWM{redPin} - red.Configure() + err := red.Configure() + checkError(err, "failed to configure red pin") green := machine.PWM{greenPin} - green.Configure() + err = green.Configure() + checkError(err, "failed to configure green pin") blue := machine.PWM{bluePin} - blue.Configure() + err = blue.Configure() + checkError(err, "failed to configure blue pin") var rc uint8 var gc uint8 = 20 @@ -52,3 +55,10 @@ func main() { time.Sleep(time.Millisecond * 500) } } + +func checkError(err error, msg string) { + if err != nil { + print(msg, ": ", err.Error()) + println() + } +} diff --git a/src/machine/machine_atmega328p.go b/src/machine/machine_atmega328p.go index 2e7926a9..58265ed1 100644 --- a/src/machine/machine_atmega328p.go +++ b/src/machine/machine_atmega328p.go @@ -79,13 +79,14 @@ func InitPWM() { } // Configure configures a PWM pin for output. -func (pwm PWM) Configure() { +func (pwm PWM) Configure() error { switch pwm.Pin / 8 { case 0: // port B avr.DDRB.SetBits(1 << uint8(pwm.Pin)) case 2: // port D avr.DDRD.SetBits(1 << uint8(pwm.Pin-16)) } + return nil } // Set turns on the duty cycle for a PWM pin using the provided value. On the AVR this is normally a diff --git a/src/machine/machine_atsamd21.go b/src/machine/machine_atsamd21.go index cd83b7b4..e96a7f40 100644 --- a/src/machine/machine_atsamd21.go +++ b/src/machine/machine_atsamd21.go @@ -1090,9 +1090,12 @@ func InitPWM() { } // Configure configures a PWM pin for output. -func (pwm PWM) Configure() { +func (pwm PWM) Configure() error { // figure out which TCCX timer for this pin timer := pwm.getTimer() + if timer == nil { + return ErrInvalidOutputPin + } // disable timer timer.CTRLA.ClearBits(sam.TCC_CTRLA_ENABLE) @@ -1139,12 +1142,19 @@ func (pwm PWM) Configure() { val := pwm.getPMux() & sam.PORT_PMUX0_PMUXO_Msk pwm.setPMux(val | uint8(pwmConfig<CC[tcChannel].reg = (uint32_t) value; - pwm.setChannel(0) + pwm.setChannel(timer, 0) for timer.SYNCBUSY.HasBits(sam.TCC_SYNCBUSY_CC0) || timer.SYNCBUSY.HasBits(sam.TCC_SYNCBUSY_CC1) { @@ -1282,12 +1285,19 @@ func (pwm PWM) Configure() { // Wait for synchronization for timer.SYNCBUSY.HasBits(sam.TCC_SYNCBUSY_ENABLE) { } + + return nil } // Set turns on the duty cycle for a PWM pin using the provided value. func (pwm PWM) Set(value uint16) { // figure out which TCCX timer for this pin timer := pwm.getTimer() + if timer == nil { + // The Configure call above cannot have succeeded, so simply ignore this + // error. + return + } // Wait for synchronization for timer.SYNCBUSY.HasBits(sam.TCC_SYNCBUSY_CTRLB) { @@ -1297,7 +1307,7 @@ func (pwm PWM) Set(value uint16) { } // TCCx->CCBUF[tcChannel].reg = (uint32_t) value; - pwm.setChannelBuffer(uint32(value)) + pwm.setChannelBuffer(timer, uint32(value)) for timer.SYNCBUSY.HasBits(sam.TCC_SYNCBUSY_CC0) || timer.SYNCBUSY.HasBits(sam.TCC_SYNCBUSY_CC1) { @@ -1329,61 +1339,61 @@ func (pwm PWM) setPinCfg(val uint8) { pwm.Pin.setPinCfg(val) } -// setChannel sets the value for the correct channel for PWM on this pin -func (pwm PWM) setChannel(val uint32) { +// setChannel sets the value for the correct channel for PWM on this pin. +func (pwm PWM) setChannel(timer *sam.TCC_Type, val uint32) { switch pwm.Pin { case PA16: - pwm.getTimer().CC[0].Set(val) + timer.CC[0].Set(val) case PA17: - pwm.getTimer().CC[1].Set(val) + timer.CC[1].Set(val) case PA14: - pwm.getTimer().CC[0].Set(val) + timer.CC[0].Set(val) case PA15: - pwm.getTimer().CC[1].Set(val) + timer.CC[1].Set(val) case PA18: - pwm.getTimer().CC[2].Set(val) + timer.CC[2].Set(val) case PA19: - pwm.getTimer().CC[3].Set(val) + timer.CC[3].Set(val) case PA20: - pwm.getTimer().CC[0].Set(val) + timer.CC[0].Set(val) case PA21: - pwm.getTimer().CC[1].Set(val) + timer.CC[1].Set(val) case PA23: - pwm.getTimer().CC[3].Set(val) + timer.CC[3].Set(val) case PA22: - pwm.getTimer().CC[2].Set(val) + timer.CC[2].Set(val) case PB31: - pwm.getTimer().CC[1].Set(val) + timer.CC[1].Set(val) default: return // not supported on this pin } } // setChannelBuffer sets the value for the correct channel buffer for PWM on this pin -func (pwm PWM) setChannelBuffer(val uint32) { +func (pwm PWM) setChannelBuffer(timer *sam.TCC_Type, val uint32) { switch pwm.Pin { case PA16: - pwm.getTimer().CCBUF[0].Set(val) + timer.CCBUF[0].Set(val) case PA17: - pwm.getTimer().CCBUF[1].Set(val) + timer.CCBUF[1].Set(val) case PA14: - pwm.getTimer().CCBUF[0].Set(val) + timer.CCBUF[0].Set(val) case PA15: - pwm.getTimer().CCBUF[1].Set(val) + timer.CCBUF[1].Set(val) case PA18: - pwm.getTimer().CCBUF[2].Set(val) + timer.CCBUF[2].Set(val) case PA19: - pwm.getTimer().CCBUF[3].Set(val) + timer.CCBUF[3].Set(val) case PA20: - pwm.getTimer().CCBUF[0].Set(val) + timer.CCBUF[0].Set(val) case PA21: - pwm.getTimer().CCBUF[1].Set(val) + timer.CCBUF[1].Set(val) case PA23: - pwm.getTimer().CCBUF[3].Set(val) + timer.CCBUF[3].Set(val) case PA22: - pwm.getTimer().CCBUF[2].Set(val) + timer.CCBUF[2].Set(val) case PB31: - pwm.getTimer().CCBUF[1].Set(val) + timer.CCBUF[1].Set(val) default: return // not supported on this pin } diff --git a/src/machine/machine_generic.go b/src/machine/machine_generic.go index a0b47837..ace5b1cb 100644 --- a/src/machine/machine_generic.go +++ b/src/machine/machine_generic.go @@ -90,7 +90,8 @@ func InitPWM() { } // Configure configures a PWM pin for output. -func (pwm PWM) Configure() { +func (pwm PWM) Configure() error { + return nil } // Set turns on the duty cycle for a PWM pin using the provided value. diff --git a/src/machine/machine_nrf52.go b/src/machine/machine_nrf52.go index caa0c4e7..003be7a6 100644 --- a/src/machine/machine_nrf52.go +++ b/src/machine/machine_nrf52.go @@ -159,7 +159,8 @@ func InitPWM() { } // Configure configures a PWM pin for output. -func (pwm PWM) Configure() { +func (pwm PWM) Configure() error { + return nil } // Set turns on the duty cycle for a PWM pin using the provided value. diff --git a/src/machine/machine_nrf52840.go b/src/machine/machine_nrf52840.go index 2a76ac27..c7e60826 100644 --- a/src/machine/machine_nrf52840.go +++ b/src/machine/machine_nrf52840.go @@ -104,8 +104,8 @@ func InitADC() { } // Configure configures an ADC pin to be able to read analog data. -func (a ADC) Configure() { - return // no pin specific setup on nrf52840 machine. +func (a ADC) Configure() error { + return nil // no pin specific setup on nrf52840 machine. } // Get returns the current value of a ADC pin in the range 0..0xffff.