Skip to content

Commit 70374d0

Browse files
committed
pwm: update drivers with PWM to use new interface
Signed-off-by: deadprogram <[email protected]>
1 parent 23776bf commit 70374d0

File tree

5 files changed

+96
-48
lines changed

5 files changed

+96
-48
lines changed

examples/apa102/itsybitsy-m0/main.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
var (
1616
apa apa102.Device
1717

18-
led = machine.PWM{machine.LED}
18+
pwm = machine.TCC0
1919
leds = make([]color.RGBA, 1)
2020
wheel = &Wheel{Brightness: 0x10}
2121
)
@@ -27,12 +27,21 @@ func init() {
2727
apa = apa102.NewSoftwareSPI(machine.PA00, machine.PA01, 1)
2828

2929
// Configure the regular on-board LED for PWM fading
30-
machine.InitPWM()
31-
led.Configure()
32-
30+
err := pwm.Configure(machine.PWMConfig{
31+
Period: 16384e3, // 16.384ms
32+
})
33+
if err != nil {
34+
println("failed to configure PWM")
35+
return
36+
}
3337
}
3438

3539
func main() {
40+
channelLED, err := pwm.Channel(machine.LED)
41+
if err != nil {
42+
println("failed to configure LED PWM channel")
43+
return
44+
}
3645

3746
// We'll fade the on-board LED in a goroutine to show/ensure that the APA102
3847
// works fine with the scheduler enabled. Comment this out to test this code
@@ -47,7 +56,7 @@ func main() {
4756
if !brightening {
4857
brightness = 0xFFFF - brightness
4958
}
50-
led.Set(brightness)
59+
pwm.Set(channelLED, uint32(brightness)/0xFFFF)
5160
time.Sleep(5 * time.Millisecond)
5261
}
5362
}()

examples/l293x/speed/main.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ import (
88
)
99

1010
const (
11-
maxSpeed = 30000
11+
maxSpeed = 100
1212
)
1313

1414
func main() {
15-
machine.InitPWM()
16-
17-
wheel := l293x.NewWithSpeed(machine.D10, machine.D11, machine.PWM{machine.D12})
15+
wheel := l293x.NewWithSpeed(machine.D10, machine.D11, machine.D12, machine.TCC0)
1816
wheel.Configure()
1917

2018
for i := 0; i <= 10; i++ {
2119
println("Forward")
22-
var i uint16
23-
for i = 0; i < maxSpeed; i += 1000 {
20+
var i uint32
21+
for i = 0; i < maxSpeed; i += 10 {
2422
wheel.Forward(i)
2523
time.Sleep(time.Millisecond * 100)
2624
}
@@ -30,7 +28,7 @@ func main() {
3028
time.Sleep(time.Millisecond * 1000)
3129

3230
println("Backward")
33-
for i = 0; i < maxSpeed; i += 1000 {
31+
for i = 0; i < maxSpeed; i += 10 {
3432
wheel.Backward(i)
3533
time.Sleep(time.Millisecond * 100)
3634
}

examples/l9110x/speed/main.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ import (
88
)
99

1010
const (
11-
maxSpeed = 30000
11+
maxSpeed = 100
1212
)
1313

1414
func main() {
15-
machine.InitPWM()
16-
17-
wheel := l9110x.NewWithSpeed(machine.PWM{machine.D11}, machine.PWM{machine.D12})
15+
wheel := l9110x.NewWithSpeed(machine.D11, machine.D12, machine.TCC0)
1816
wheel.Configure()
1917

2018
for i := 0; i <= 10; i++ {
2119
println("Forward")
22-
var i uint16
23-
for i = 0; i < maxSpeed; i += 1000 {
20+
var i uint32
21+
for i = 0; i < maxSpeed; i += 10 {
2422
wheel.Forward(i)
2523
time.Sleep(time.Millisecond * 100)
2624
}
@@ -30,7 +28,7 @@ func main() {
3028
time.Sleep(time.Millisecond * 1000)
3129

3230
println("Backward")
33-
for i = 0; i < maxSpeed; i += 1000 {
31+
for i = 0; i < maxSpeed; i += 10 {
3432
wheel.Backward(i)
3533
time.Sleep(time.Millisecond * 100)
3634
}

l293x/l293x.go

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,44 +61,67 @@ func (d *Device) Stop() {
6161
// en is the PWM pin that controls the motor speed.
6262
type PWMDevice struct {
6363
a1, a2 machine.Pin
64-
en machine.PWM
64+
en machine.Pin
65+
pwm *machine.TCC
66+
ch uint8
6567
}
6668

6769
// NewWithSpeed returns a new PWMMotor driver that uses a PWM pin to control speed.
68-
func NewWithSpeed(direction1, direction2 machine.Pin, speedPin machine.PWM) PWMDevice {
70+
func NewWithSpeed(direction1, direction2 machine.Pin, speedPin machine.Pin, pwm *machine.TCC) PWMDevice {
6971
return PWMDevice{
70-
a1: direction1,
71-
a2: direction2,
72-
en: speedPin,
72+
a1: direction1,
73+
a2: direction2,
74+
en: speedPin,
75+
pwm: pwm,
7376
}
7477
}
7578

7679
// Configure configures the PWMDevice.
77-
func (d *PWMDevice) Configure() {
80+
func (d *PWMDevice) Configure() error {
7881
d.a1.Configure(machine.PinConfig{Mode: machine.PinOutput})
7982
d.a2.Configure(machine.PinConfig{Mode: machine.PinOutput})
80-
d.en.Configure()
83+
err := d.pwm.Configure(machine.PWMConfig{
84+
Period: 16384e3, // 16.384ms
85+
})
86+
if err != nil {
87+
return err
88+
}
89+
90+
d.ch, err = d.pwm.Channel(d.en)
91+
if err != nil {
92+
return err
93+
}
8194

8295
d.Stop()
96+
97+
return nil
8398
}
8499

85-
// Forward turns motor on in forward direction at specific speed.
86-
func (d *PWMDevice) Forward(speed uint16) {
100+
// Forward turns motor on in forward direction at specific speed as a percentage.
101+
func (d *PWMDevice) Forward(speed uint32) {
102+
if speed > 100 {
103+
speed = 100
104+
}
105+
87106
d.a1.High()
88107
d.a2.Low()
89-
d.en.Set(speed)
108+
d.pwm.Set(d.ch, speed)
90109
}
91110

92-
// Backward turns motor on in backward direction at specific speed.
93-
func (d *PWMDevice) Backward(speed uint16) {
111+
// Backward turns motor on in backward direction at specific speed as a percentage.
112+
func (d *PWMDevice) Backward(speed uint32) {
113+
if speed > 100 {
114+
speed = 100
115+
}
116+
94117
d.a1.Low()
95118
d.a2.High()
96-
d.en.Set(speed)
119+
d.pwm.Set(d.ch, speed)
97120
}
98121

99122
// Stop turns motor off.
100123
func (d *PWMDevice) Stop() {
101124
d.a1.Low()
102125
d.a2.Low()
103-
d.en.Set(0)
126+
d.pwm.Set(d.ch, 0)
104127
}

l9110x/l9110x.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,39 +52,59 @@ func (d *Device) Stop() {
5252
// PWMDevice is a motor with speed control.
5353
// ia and ib are the directional/speed PWM pins.
5454
type PWMDevice struct {
55-
ia, ib machine.PWM
55+
ia, ib machine.Pin
56+
pwm *machine.TCC
57+
ca, cb uint8
5658
}
5759

5860
// NewWithSpeed returns a new PWMMotor driver that uses 2 PWM pins to control both direction and speed.
59-
func NewWithSpeed(direction1, direction2 machine.PWM) PWMDevice {
61+
func NewWithSpeed(direction1, direction2 machine.Pin, pwm *machine.TCC) PWMDevice {
6062
return PWMDevice{
61-
ia: direction1,
62-
ib: direction2,
63+
ia: direction1,
64+
ib: direction2,
65+
pwm: pwm,
6366
}
6467
}
6568

6669
// Configure configures the PWMDevice.
67-
func (d *PWMDevice) Configure() {
68-
d.ia.Configure()
69-
d.ib.Configure()
70+
func (d *PWMDevice) Configure() (err error) {
71+
d.ia.Configure(machine.PinConfig{Mode: machine.PinOutput})
72+
d.ib.Configure(machine.PinConfig{Mode: machine.PinOutput})
73+
err = d.pwm.Configure(machine.PWMConfig{
74+
Period: 16384e3, // 16.384ms
75+
})
76+
if err != nil {
77+
return
78+
}
79+
80+
d.ca, err = d.pwm.Channel(d.ia)
81+
if err != nil {
82+
return
83+
}
84+
85+
d.cb, err = d.pwm.Channel(d.ib)
86+
if err != nil {
87+
return
88+
}
7089

7190
d.Stop()
91+
return
7292
}
7393

7494
// Forward turns motor on in forward direction at specific speed.
75-
func (d *PWMDevice) Forward(speed uint16) {
76-
d.ia.Set(speed)
77-
d.ib.Set(0)
95+
func (d *PWMDevice) Forward(speed uint32) {
96+
d.pwm.Set(d.ca, speed)
97+
d.pwm.Set(d.cb, 0)
7898
}
7999

80100
// Backward turns motor on in backward direction at specific speed.
81-
func (d *PWMDevice) Backward(speed uint16) {
82-
d.ia.Set(0)
83-
d.ib.Set(speed)
101+
func (d *PWMDevice) Backward(speed uint32) {
102+
d.pwm.Set(d.ca, 0)
103+
d.pwm.Set(d.cb, speed)
84104
}
85105

86106
// Stop turns motor off.
87107
func (d *PWMDevice) Stop() {
88-
d.ia.Set(0)
89-
d.ib.Set(0)
108+
d.pwm.Set(d.ca, 0)
109+
d.pwm.Set(d.cb, 0)
90110
}

0 commit comments

Comments
 (0)