@@ -28,8 +28,8 @@ const (
28
28
PinInput PinMode = 9
29
29
PinInputPullup PinMode = 10
30
30
PinOutput PinMode = 11
31
- PinPWM PinMode = PinTimer
32
- PinPWMAlt PinMode = PinTimerAlt
31
+ PinTCC PinMode = PinTimer
32
+ PinTCCAlt PinMode = PinTimerAlt
33
33
PinInputPulldown PinMode = 12
34
34
)
35
35
@@ -1221,28 +1221,28 @@ func (spi SPI) Transfer(w byte) (byte, error) {
1221
1221
return byte (spi .Bus .DATA .Get ()), nil
1222
1222
}
1223
1223
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
1229
1229
1230
1230
// The SAM D21 has three TCC peripherals, which have PWM as one feature.
1231
1231
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 )
1235
1235
)
1236
1236
1237
1237
//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 )
1240
1240
}
1241
1241
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 {
1244
1244
// Enable the clock source for this timer.
1245
- switch pwm .timer () {
1245
+ switch tcc .timer () {
1246
1246
case sam .TCC0 :
1247
1247
sam .PM .APBCMASK .SetBits (sam .PM_APBCMASK_TCC0_ )
1248
1248
// Use GCLK0 for TCC0/TCC1
@@ -1270,32 +1270,32 @@ func (pwm *PWM) Configure(config PWMConfig) error {
1270
1270
}
1271
1271
1272
1272
// 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
1274
1274
// 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 )
1276
1276
1277
1277
// 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 )
1279
1279
1280
1280
// Wait for synchronization of all changed registers.
1281
- for pwm .timer ().SYNCBUSY .Get () != 0 {
1281
+ for tcc .timer ().SYNCBUSY .Get () != 0 {
1282
1282
}
1283
1283
1284
1284
// Set the period and prescaler.
1285
- err := pwm .setPeriod (config .Period , true )
1285
+ err := tcc .setPeriod (config .Period , true )
1286
1286
1287
1287
// Enable the timer.
1288
- pwm .timer ().CTRLA .SetBits (sam .TCC_CTRLA_ENABLE )
1288
+ tcc .timer ().CTRLA .SetBits (sam .TCC_CTRLA_ENABLE )
1289
1289
1290
1290
// Wait for synchronization of all changed registers.
1291
- for pwm .timer ().SYNCBUSY .Get () != 0 {
1291
+ for tcc .timer ().SYNCBUSY .Get () != 0 {
1292
1292
}
1293
1293
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.
1295
1295
return err
1296
1296
}
1297
1297
1298
- // SetPeriod updates the period of this PWM peripheral.
1298
+ // SetPeriod updates the period of this TCC peripheral.
1299
1299
// To set a particular frequency, use the following formula:
1300
1300
//
1301
1301
// period = 1e9 / frequency
@@ -1306,28 +1306,28 @@ func (pwm *PWM) Configure(config PWMConfig) error {
1306
1306
// value in any of the channels. This means that you may need to update the
1307
1307
// value for the particular channel.
1308
1308
//
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
1310
1310
// been configured. If you want to switch between frequencies, pick the lowest
1311
1311
// frequency (longest period) once when calling Configure and adjust the
1312
1312
// 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 )
1315
1315
if err == nil {
1316
- if pwm .Counter () >= pwm .Top () {
1316
+ if tcc .Counter () >= tcc .Top () {
1317
1317
// When setting the timer to a shorter period, there is a chance
1318
1318
// that it passes the counter value and thus goes all the way to MAX
1319
1319
// before wrapping back to zero.
1320
1320
// To avoid this, reset the counter back to 0.
1321
- pwm .timer ().COUNT .Set (0 )
1321
+ tcc .timer ().COUNT .Set (0 )
1322
1322
}
1323
1323
}
1324
1324
return err
1325
1325
}
1326
1326
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
1329
1329
// the Configure function.
1330
- func (pwm * PWM ) setPeriod (period uint64 , updatePrescaler bool ) error {
1330
+ func (tcc * TCC ) setPeriod (period uint64 , updatePrescaler bool ) error {
1331
1331
var top uint64
1332
1332
if period == 0 {
1333
1333
// 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 {
1340
1340
}
1341
1341
1342
1342
maxTop := uint64 (0xffffff )
1343
- if pwm .timer () == sam .TCC2 {
1343
+ if tcc .timer () == sam .TCC2 {
1344
1344
// TCC2 is a 16-bit timer, not a 24-bit timer.
1345
1345
maxTop = 0xffff
1346
1346
}
@@ -1377,12 +1377,12 @@ func (pwm *PWM) setPeriod(period uint64, updatePrescaler bool) error {
1377
1377
default :
1378
1378
return ErrPWMPeriodTooLong
1379
1379
}
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 ))
1381
1381
} else {
1382
1382
// Do not update the prescaler, but use the already-configured
1383
1383
// prescaler. This is the normal SetPeriod case, where the prescaler
1384
1384
// 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
1386
1386
switch prescaler {
1387
1387
case sam .TCC_CTRLA_PRESCALER_DIV1 :
1388
1388
top /= 1 // no-op
@@ -1409,10 +1409,10 @@ func (pwm *PWM) setPeriod(period uint64, updatePrescaler bool) error {
1409
1409
}
1410
1410
1411
1411
// Set the period (the counter top).
1412
- pwm .timer ().PER .Set (uint32 (top ) - 1 )
1412
+ tcc .timer ().PER .Set (uint32 (top ) - 1 )
1413
1413
1414
1414
// Wait for synchronization of CTRLA.PRESCALER and PER registers.
1415
- for pwm .timer ().SYNCBUSY .Get () != 0 {
1415
+ for tcc .timer ().SYNCBUSY .Get () != 0 {
1416
1416
}
1417
1417
1418
1418
return nil
@@ -1425,17 +1425,17 @@ func (pwm *PWM) setPeriod(period uint64, updatePrescaler bool) error {
1425
1425
// The value returned here is hardware dependent. In general, it's best to treat
1426
1426
// it as an opaque value that can be divided by some number and passed to Set
1427
1427
// (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
1430
1430
}
1431
1431
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
1433
1433
// 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 {
1437
1437
}
1438
- return pwm .timer ().COUNT .Get ()
1438
+ return tcc .timer ().COUNT .Get ()
1439
1439
}
1440
1440
1441
1441
// Some constans to make pinTimerMapping below easier to read.
@@ -1454,8 +1454,8 @@ const (
1454
1454
1455
1455
// Mapping from pin number to TCC peripheral and channel using a special
1456
1456
// 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
1459
1459
// indicates Ch0/Ch1 or Ch2/Ch3, and three other bits that contain the TCC
1460
1460
// peripheral number plus one (to distinguish between TCC0Ch0 and 0).
1461
1461
//
@@ -1489,7 +1489,7 @@ var pinTimerMapping = [...]uint8{
1489
1489
PB30 / 2 : pinTCC0Ch0 | pinTCC1Ch2 << 4 ,
1490
1490
}
1491
1491
1492
- // findPinPadMapping returns the pin mode (PinPWM or PinPWMAlt ) and the channel
1492
+ // findPinPadMapping returns the pin mode (PinTCC or PinTCCAlt ) and the channel
1493
1493
// number for a given timer and pin. A zero PinMode is returned if no mapping
1494
1494
// could be found.
1495
1495
func findPinTimerMapping (timer uint8 , pin Pin ) (PinMode , uint8 ) {
@@ -1499,23 +1499,24 @@ func findPinTimerMapping(timer uint8, pin Pin) (PinMode, uint8) {
1499
1499
if mapping & 0x07 == timer + 1 {
1500
1500
// PWM output is on peripheral function E.
1501
1501
evenChannel := ((mapping >> 3 ) & 1 ) * 2
1502
- return PinPWM , evenChannel + uint8 (pin & 1 )
1502
+ return PinTCC , evenChannel + uint8 (pin & 1 )
1503
1503
}
1504
1504
if (mapping & 0x70 )>> 4 == timer + 1 {
1505
1505
// PWM output is on peripheral function F.
1506
1506
evenChannel := ((mapping >> 7 ) & 1 ) * 2
1507
- return PinPWMAlt , evenChannel + uint8 (pin & 1 )
1507
+ return PinTCCAlt , evenChannel + uint8 (pin & 1 )
1508
1508
}
1509
1509
return 0 , 0
1510
1510
}
1511
1511
1512
1512
// Channel returns a PWM channel for the given pin. Note that one channel may be
1513
1513
// 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 ) {
1516
1517
var pinMode PinMode
1517
1518
var channel uint8
1518
- switch pwm .timer () {
1519
+ switch tcc .timer () {
1519
1520
case sam .TCC0 :
1520
1521
pinMode , channel = findPinTimerMapping (0 , pin )
1521
1522
case sam .TCC1 :
@@ -1549,44 +1550,44 @@ func (pwm *PWM) Channel(pin Pin) (uint8, error) {
1549
1550
// the time and low for the rest. Inverting flips the output as if a NOT gate
1550
1551
// was placed at the output, meaning that the output would be 25% low and 75%
1551
1552
// 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 ) {
1553
1554
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 ))
1555
1556
} 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 ))
1557
1558
}
1558
1559
1559
1560
// Wait for synchronization of the WAVE register.
1560
- for pwm .timer ().SYNCBUSY .Get () != 0 {
1561
+ for tcc .timer ().SYNCBUSY .Get () != 0 {
1561
1562
}
1562
1563
}
1563
1564
1564
1565
// Set updates the channel value. This is used to control the channel duty
1565
1566
// cycle, in other words the fraction of time the channel output is high (or low
1566
1567
// when inverted). For example, to set it to a 25% duty cycle, use:
1567
1568
//
1568
- // pwm .Set(channel, pwm .Top() / 4)
1569
+ // tcc .Set(channel, tcc .Top() / 4)
1569
1570
//
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 ) {
1573
1574
// Set PWM signal to output duty cycle
1574
1575
switch channel {
1575
1576
case 0 :
1576
- pwm .timer ().CC0 .Set (value )
1577
+ tcc .timer ().CC0 .Set (value )
1577
1578
case 1 :
1578
- pwm .timer ().CC1 .Set (value )
1579
+ tcc .timer ().CC1 .Set (value )
1579
1580
case 2 :
1580
- pwm .timer ().CC2 .Set (value )
1581
+ tcc .timer ().CC2 .Set (value )
1581
1582
case 3 :
1582
- pwm .timer ().CC3 .Set (value )
1583
+ tcc .timer ().CC3 .Set (value )
1583
1584
default :
1584
- // invalid PWMChannel struct , ignore.
1585
+ // invalid PWM channel , ignore.
1585
1586
}
1586
1587
1587
1588
// Wait for synchronization on all channels (or anything in this peripheral,
1588
1589
// really).
1589
- for pwm .timer ().SYNCBUSY .Get () != 0 {
1590
+ for tcc .timer ().SYNCBUSY .Get () != 0 {
1590
1591
}
1591
1592
}
1592
1593
0 commit comments