Skip to content

Commit 739083e

Browse files
committed
Many updates [TO BE SQUASHED]
1 parent 9313a0a commit 739083e

12 files changed

+207
-175
lines changed

Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,6 @@ smoketest:
328328
@$(MD5SUM) test.hex
329329
$(TINYGO) build -size short -o test.hex -target=feather-m4 examples/pwm
330330
@$(MD5SUM) test.hex
331-
$(TINYGO) build -size short -o test.hex -target=pyportal examples/pwm
332-
@$(MD5SUM) test.hex
333331
ifneq ($(AVR), 0)
334332
$(TINYGO) build -size short -o test.hex -target=atmega1284p examples/serial
335333
@$(MD5SUM) test.hex

src/examples/pwm/arduino.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// +build arduino
2+
3+
package main
4+
5+
import "machine"
6+
7+
var (
8+
// Configuration on an Arduino Uno.
9+
pwm = machine.Timer2
10+
pinA = machine.PB3 // pin 11 on the Uno
11+
pinB = machine.PD3 // pin 3 on the Uno
12+
)

src/examples/pwm/feather-m4.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// +build feather-m4
2+
3+
package main
4+
5+
import "machine"
6+
7+
var (
8+
pwm = machine.TCC0
9+
pinA = machine.D12
10+
pinB = machine.D13
11+
)

src/examples/pwm/itsybitsy-m0.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// +build itsybitsy_m0
2+
3+
package main
4+
5+
import "machine"
6+
7+
var (
8+
pwm = machine.TCC0
9+
pinA = machine.D3
10+
pinB = machine.D4
11+
)

src/examples/pwm/itsybitsy-m4.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// +build itsybitsy_m4
2+
3+
package main
4+
5+
import "machine"
6+
7+
var (
8+
pwm = machine.TCC0
9+
pinA = machine.D12
10+
pinB = machine.D13
11+
)

src/examples/pwm/pwm.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@ import (
77
"time"
88
)
99

10-
var (
11-
// Configuration on an Adafruit Circuit Playground Express.
12-
pwm = machine.PWM1
13-
pinA = machine.A2
14-
pinB = machine.A3
15-
16-
// Configuration on an Arduino Uno.
17-
//pwm = machine.Timer2
18-
//pinA = machine.PB3 // pin 11 on the Uno
19-
//pinB = machine.PD3 // pin 3 on the Uno
20-
)
21-
2210
const delayBetweenPeriods = time.Second * 5
2311

2412
func main() {

src/machine/machine_atsamd21.go

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const (
2828
PinInput PinMode = 9
2929
PinInputPullup PinMode = 10
3030
PinOutput PinMode = 11
31-
PinPWM PinMode = PinTimer
32-
PinPWMAlt PinMode = PinTimerAlt
31+
PinTCC PinMode = PinTimer
32+
PinTCCAlt PinMode = PinTimerAlt
3333
PinInputPulldown PinMode = 12
3434
)
3535

@@ -1221,28 +1221,28 @@ func (spi SPI) Transfer(w byte) (byte, error) {
12211221
return byte(spi.Bus.DATA.Get()), nil
12221222
}
12231223

1224-
// PWM is one PWM peripheral, which consists of a counter and multiple output
1225-
// channels (that can be connected to actual pins). You can set the frequency
1226-
// using SetPeriod, but only for all the channels in this PWM peripheral at
1227-
// once.
1228-
type PWM sam.TCC_Type
1224+
// TCC is one timer/counter peripheral, which consists of a counter and multiple
1225+
// output channels (that can be connected to actual pins). You can set the
1226+
// frequency using SetPeriod, but only for all the channels in this TCC
1227+
// peripheral at once.
1228+
type TCC sam.TCC_Type
12291229

12301230
// The SAM D21 has three TCC peripherals, which have PWM as one feature.
12311231
var (
1232-
PWM0 = (*PWM)(sam.TCC0)
1233-
PWM1 = (*PWM)(sam.TCC1)
1234-
PWM2 = (*PWM)(sam.TCC2)
1232+
TCC0 = (*TCC)(sam.TCC0)
1233+
TCC1 = (*TCC)(sam.TCC1)
1234+
TCC2 = (*TCC)(sam.TCC2)
12351235
)
12361236

12371237
//go:inline
1238-
func (pwm *PWM) timer() *sam.TCC_Type {
1239-
return (*sam.TCC_Type)(pwm)
1238+
func (tcc *TCC) timer() *sam.TCC_Type {
1239+
return (*sam.TCC_Type)(tcc)
12401240
}
12411241

1242-
// Configure enables and configures this PWM.
1243-
func (pwm *PWM) Configure(config PWMConfig) error {
1242+
// Configure enables and configures this TCC.
1243+
func (tcc *TCC) Configure(config PWMConfig) error {
12441244
// Enable the clock source for this timer.
1245-
switch pwm.timer() {
1245+
switch tcc.timer() {
12461246
case sam.TCC0:
12471247
sam.PM.APBCMASK.SetBits(sam.PM_APBCMASK_TCC0_)
12481248
// Use GCLK0 for TCC0/TCC1
@@ -1270,32 +1270,32 @@ func (pwm *PWM) Configure(config PWMConfig) error {
12701270
}
12711271

12721272
// Disable timer (if it was enabled). This is necessary because
1273-
// pwm.setPeriod may want to change the prescaler bits in CTRLA, which is
1273+
// tcc.setPeriod may want to change the prescaler bits in CTRLA, which is
12741274
// only allowed when the TCC is disabled.
1275-
pwm.timer().CTRLA.ClearBits(sam.TCC_CTRLA_ENABLE)
1275+
tcc.timer().CTRLA.ClearBits(sam.TCC_CTRLA_ENABLE)
12761276

12771277
// Use "Normal PWM" (single-slope PWM)
1278-
pwm.timer().WAVE.Set(sam.TCC_WAVE_WAVEGEN_NPWM)
1278+
tcc.timer().WAVE.Set(sam.TCC_WAVE_WAVEGEN_NPWM)
12791279

12801280
// Wait for synchronization of all changed registers.
1281-
for pwm.timer().SYNCBUSY.Get() != 0 {
1281+
for tcc.timer().SYNCBUSY.Get() != 0 {
12821282
}
12831283

12841284
// Set the period and prescaler.
1285-
err := pwm.setPeriod(config.Period, true)
1285+
err := tcc.setPeriod(config.Period, true)
12861286

12871287
// Enable the timer.
1288-
pwm.timer().CTRLA.SetBits(sam.TCC_CTRLA_ENABLE)
1288+
tcc.timer().CTRLA.SetBits(sam.TCC_CTRLA_ENABLE)
12891289

12901290
// Wait for synchronization of all changed registers.
1291-
for pwm.timer().SYNCBUSY.Get() != 0 {
1291+
for tcc.timer().SYNCBUSY.Get() != 0 {
12921292
}
12931293

1294-
// Return any error that might have occured in the pwm.setPeriod call.
1294+
// Return any error that might have occured in the tcc.setPeriod call.
12951295
return err
12961296
}
12971297

1298-
// SetPeriod updates the period of this PWM peripheral.
1298+
// SetPeriod updates the period of this TCC peripheral.
12991299
// To set a particular frequency, use the following formula:
13001300
//
13011301
// period = 1e9 / frequency
@@ -1306,28 +1306,28 @@ func (pwm *PWM) Configure(config PWMConfig) error {
13061306
// value in any of the channels. This means that you may need to update the
13071307
// value for the particular channel.
13081308
//
1309-
// Note that you cannot pick any arbitrary period after the PWM peripheral has
1309+
// Note that you cannot pick any arbitrary period after the TCC peripheral has
13101310
// been configured. If you want to switch between frequencies, pick the lowest
13111311
// frequency (longest period) once when calling Configure and adjust the
13121312
// frequency here as needed.
1313-
func (pwm *PWM) SetPeriod(period uint64) error {
1314-
err := pwm.setPeriod(period, false)
1313+
func (tcc *TCC) SetPeriod(period uint64) error {
1314+
err := tcc.setPeriod(period, false)
13151315
if err == nil {
1316-
if pwm.Counter() >= pwm.Top() {
1316+
if tcc.Counter() >= tcc.Top() {
13171317
// When setting the timer to a shorter period, there is a chance
13181318
// that it passes the counter value and thus goes all the way to MAX
13191319
// before wrapping back to zero.
13201320
// To avoid this, reset the counter back to 0.
1321-
pwm.timer().COUNT.Set(0)
1321+
tcc.timer().COUNT.Set(0)
13221322
}
13231323
}
13241324
return err
13251325
}
13261326

1327-
// setPeriod sets the period of this PWM, possibly updating the prescaler as
1328-
// well. The prescaler can only modified when the PWM is disabled, that is, in
1327+
// setPeriod sets the period of this TCC, possibly updating the prescaler as
1328+
// well. The prescaler can only modified when the TCC is disabled, that is, in
13291329
// the Configure function.
1330-
func (pwm *PWM) setPeriod(period uint64, updatePrescaler bool) error {
1330+
func (tcc *TCC) setPeriod(period uint64, updatePrescaler bool) error {
13311331
var top uint64
13321332
if period == 0 {
13331333
// Make sure the TOP value is at 0xffff (enough for a 16-bit timer).
@@ -1340,7 +1340,7 @@ func (pwm *PWM) setPeriod(period uint64, updatePrescaler bool) error {
13401340
}
13411341

13421342
maxTop := uint64(0xffffff)
1343-
if pwm.timer() == sam.TCC2 {
1343+
if tcc.timer() == sam.TCC2 {
13441344
// TCC2 is a 16-bit timer, not a 24-bit timer.
13451345
maxTop = 0xffff
13461346
}
@@ -1377,12 +1377,12 @@ func (pwm *PWM) setPeriod(period uint64, updatePrescaler bool) error {
13771377
default:
13781378
return ErrPWMPeriodTooLong
13791379
}
1380-
pwm.timer().CTRLA.Set((pwm.timer().CTRLA.Get() &^ sam.TCC_CTRLA_PRESCALER_Msk) | (prescaler << sam.TCC_CTRLA_PRESCALER_Pos))
1380+
tcc.timer().CTRLA.Set((tcc.timer().CTRLA.Get() &^ sam.TCC_CTRLA_PRESCALER_Msk) | (prescaler << sam.TCC_CTRLA_PRESCALER_Pos))
13811381
} else {
13821382
// Do not update the prescaler, but use the already-configured
13831383
// prescaler. This is the normal SetPeriod case, where the prescaler
13841384
// must not be changed.
1385-
prescaler := (pwm.timer().CTRLA.Get() & sam.TCC_CTRLA_PRESCALER_Msk) >> sam.TCC_CTRLA_PRESCALER_Pos
1385+
prescaler := (tcc.timer().CTRLA.Get() & sam.TCC_CTRLA_PRESCALER_Msk) >> sam.TCC_CTRLA_PRESCALER_Pos
13861386
switch prescaler {
13871387
case sam.TCC_CTRLA_PRESCALER_DIV1:
13881388
top /= 1 // no-op
@@ -1409,10 +1409,10 @@ func (pwm *PWM) setPeriod(period uint64, updatePrescaler bool) error {
14091409
}
14101410

14111411
// Set the period (the counter top).
1412-
pwm.timer().PER.Set(uint32(top) - 1)
1412+
tcc.timer().PER.Set(uint32(top) - 1)
14131413

14141414
// Wait for synchronization of CTRLA.PRESCALER and PER registers.
1415-
for pwm.timer().SYNCBUSY.Get() != 0 {
1415+
for tcc.timer().SYNCBUSY.Get() != 0 {
14161416
}
14171417

14181418
return nil
@@ -1425,17 +1425,17 @@ func (pwm *PWM) setPeriod(period uint64, updatePrescaler bool) error {
14251425
// The value returned here is hardware dependent. In general, it's best to treat
14261426
// it as an opaque value that can be divided by some number and passed to Set
14271427
// (see Set documentation for more information).
1428-
func (pwm *PWM) Top() uint32 {
1429-
return pwm.timer().PER.Get() + 1
1428+
func (tcc *TCC) Top() uint32 {
1429+
return tcc.timer().PER.Get() + 1
14301430
}
14311431

1432-
// Counter returns the current counter value of the timer in this PWM
1432+
// Counter returns the current counter value of the timer in this TCC
14331433
// peripheral. It may be useful for debugging.
1434-
func (pwm *PWM) Counter() uint32 {
1435-
pwm.timer().CTRLBSET.Set(sam.TCC_CTRLBSET_CMD_READSYNC << sam.TCC_CTRLBSET_CMD_Pos)
1436-
for pwm.timer().SYNCBUSY.Get() != 0 {
1434+
func (tcc *TCC) Counter() uint32 {
1435+
tcc.timer().CTRLBSET.Set(sam.TCC_CTRLBSET_CMD_READSYNC << sam.TCC_CTRLBSET_CMD_Pos)
1436+
for tcc.timer().SYNCBUSY.Get() != 0 {
14371437
}
1438-
return pwm.timer().COUNT.Get()
1438+
return tcc.timer().COUNT.Get()
14391439
}
14401440

14411441
// Some constans to make pinTimerMapping below easier to read.
@@ -1454,8 +1454,8 @@ const (
14541454

14551455
// Mapping from pin number to TCC peripheral and channel using a special
14561456
// encoding. Note that only TCC0-TCC2 are included, not TC3 and up.
1457-
// Every byte is split in two nibbles where the low nibble describes PinPWM and
1458-
// the high nibble describes PinPWMAlt. Within a nibble, there is one bit that
1457+
// Every byte is split in two nibbles where the low nibble describes PinTCC and
1458+
// the high nibble describes PinTCCAlt. Within a nibble, there is one bit that
14591459
// indicates Ch0/Ch1 or Ch2/Ch3, and three other bits that contain the TCC
14601460
// peripheral number plus one (to distinguish between TCC0Ch0 and 0).
14611461
//
@@ -1489,7 +1489,7 @@ var pinTimerMapping = [...]uint8{
14891489
PB30 / 2: pinTCC0Ch0 | pinTCC1Ch2<<4,
14901490
}
14911491

1492-
// findPinPadMapping returns the pin mode (PinPWM or PinPWMAlt) and the channel
1492+
// findPinPadMapping returns the pin mode (PinTCC or PinTCCAlt) and the channel
14931493
// number for a given timer and pin. A zero PinMode is returned if no mapping
14941494
// could be found.
14951495
func findPinTimerMapping(timer uint8, pin Pin) (PinMode, uint8) {
@@ -1499,23 +1499,24 @@ func findPinTimerMapping(timer uint8, pin Pin) (PinMode, uint8) {
14991499
if mapping&0x07 == timer+1 {
15001500
// PWM output is on peripheral function E.
15011501
evenChannel := ((mapping >> 3) & 1) * 2
1502-
return PinPWM, evenChannel + uint8(pin&1)
1502+
return PinTCC, evenChannel + uint8(pin&1)
15031503
}
15041504
if (mapping&0x70)>>4 == timer+1 {
15051505
// PWM output is on peripheral function F.
15061506
evenChannel := ((mapping >> 7) & 1) * 2
1507-
return PinPWMAlt, evenChannel + uint8(pin&1)
1507+
return PinTCCAlt, evenChannel + uint8(pin&1)
15081508
}
15091509
return 0, 0
15101510
}
15111511

15121512
// Channel returns a PWM channel for the given pin. Note that one channel may be
15131513
// shared between multiple pins, and so will have the same duty cycle. If this
1514-
// is not desirable, look for a different PWM or consider using a different pin.
1515-
func (pwm *PWM) Channel(pin Pin) (uint8, error) {
1514+
// is not desirable, look for a different TCC peripheral or consider using a
1515+
// different pin.
1516+
func (tcc *TCC) Channel(pin Pin) (uint8, error) {
15161517
var pinMode PinMode
15171518
var channel uint8
1518-
switch pwm.timer() {
1519+
switch tcc.timer() {
15191520
case sam.TCC0:
15201521
pinMode, channel = findPinTimerMapping(0, pin)
15211522
case sam.TCC1:
@@ -1549,44 +1550,44 @@ func (pwm *PWM) Channel(pin Pin) (uint8, error) {
15491550
// the time and low for the rest. Inverting flips the output as if a NOT gate
15501551
// was placed at the output, meaning that the output would be 25% low and 75%
15511552
// high with a duty cycle of 25%.
1552-
func (pwm *PWM) SetInverting(channel uint8, inverting bool) {
1553+
func (tcc *TCC) SetInverting(channel uint8, inverting bool) {
15531554
if inverting {
1554-
pwm.timer().WAVE.SetBits(1 << (sam.TCC_WAVE_POL0_Pos + channel))
1555+
tcc.timer().WAVE.SetBits(1 << (sam.TCC_WAVE_POL0_Pos + channel))
15551556
} else {
1556-
pwm.timer().WAVE.ClearBits(1 << (sam.TCC_WAVE_POL0_Pos + channel))
1557+
tcc.timer().WAVE.ClearBits(1 << (sam.TCC_WAVE_POL0_Pos + channel))
15571558
}
15581559

15591560
// Wait for synchronization of the WAVE register.
1560-
for pwm.timer().SYNCBUSY.Get() != 0 {
1561+
for tcc.timer().SYNCBUSY.Get() != 0 {
15611562
}
15621563
}
15631564

15641565
// Set updates the channel value. This is used to control the channel duty
15651566
// cycle, in other words the fraction of time the channel output is high (or low
15661567
// when inverted). For example, to set it to a 25% duty cycle, use:
15671568
//
1568-
// pwm.Set(channel, pwm.Top() / 4)
1569+
// tcc.Set(channel, tcc.Top() / 4)
15691570
//
1570-
// pwm.Set(channel, 0) will set the output to low and pwm.Set(channel,
1571-
// pwm.Top()) will set the output to high, assuming the output isn't inverted.
1572-
func (pwm *PWM) Set(channel uint8, value uint32) {
1571+
// tcc.Set(channel, 0) will set the output to low and tcc.Set(channel,
1572+
// tcc.Top()) will set the output to high, assuming the output isn't inverted.
1573+
func (tcc *TCC) Set(channel uint8, value uint32) {
15731574
// Set PWM signal to output duty cycle
15741575
switch channel {
15751576
case 0:
1576-
pwm.timer().CC0.Set(value)
1577+
tcc.timer().CC0.Set(value)
15771578
case 1:
1578-
pwm.timer().CC1.Set(value)
1579+
tcc.timer().CC1.Set(value)
15791580
case 2:
1580-
pwm.timer().CC2.Set(value)
1581+
tcc.timer().CC2.Set(value)
15811582
case 3:
1582-
pwm.timer().CC3.Set(value)
1583+
tcc.timer().CC3.Set(value)
15831584
default:
1584-
// invalid PWMChannel struct, ignore.
1585+
// invalid PWM channel, ignore.
15851586
}
15861587

15871588
// Wait for synchronization on all channels (or anything in this peripheral,
15881589
// really).
1589-
for pwm.timer().SYNCBUSY.Get() != 0 {
1590+
for tcc.timer().SYNCBUSY.Get() != 0 {
15901591
}
15911592
}
15921593

0 commit comments

Comments
 (0)