@@ -1216,7 +1216,7 @@ const (
12161216const period = 0xFFFF
12171217
12181218// Configure configures a PWM pin for output.
1219- func (pwm PWM ) Configure () {
1219+ func (pwm PWM ) Configure () error {
12201220 // Set pin as output
12211221 sam .PORT .GROUP [0 ].DIRSET .Set (1 << uint8 (pwm .Pin ))
12221222 // Set pin to low
@@ -1240,6 +1240,9 @@ func (pwm PWM) Configure() {
12401240
12411241 // figure out which TCCX timer for this pin
12421242 timer := pwm .getTimer ()
1243+ if timer == nil {
1244+ return ErrInvalidOutputPin
1245+ }
12431246
12441247 // disable timer
12451248 timer .CTRLA .ClearBits (sam .TCC_CTRLA_ENABLE )
@@ -1264,7 +1267,7 @@ func (pwm PWM) Configure() {
12641267
12651268 // Set the initial value
12661269 // TCCx->CC[tcChannel].reg = (uint32_t) value;
1267- pwm .setChannel (0 )
1270+ pwm .setChannel (timer , 0 )
12681271
12691272 for timer .SYNCBUSY .HasBits (sam .TCC_SYNCBUSY_CC0 ) ||
12701273 timer .SYNCBUSY .HasBits (sam .TCC_SYNCBUSY_CC1 ) {
@@ -1282,12 +1285,19 @@ func (pwm PWM) Configure() {
12821285 // Wait for synchronization
12831286 for timer .SYNCBUSY .HasBits (sam .TCC_SYNCBUSY_ENABLE ) {
12841287 }
1288+
1289+ return nil
12851290}
12861291
12871292// Set turns on the duty cycle for a PWM pin using the provided value.
12881293func (pwm PWM ) Set (value uint16 ) {
12891294 // figure out which TCCX timer for this pin
12901295 timer := pwm .getTimer ()
1296+ if timer == nil {
1297+ // The Configure call above cannot have succeeded, so simply ignore this
1298+ // error.
1299+ return
1300+ }
12911301
12921302 // Wait for synchronization
12931303 for timer .SYNCBUSY .HasBits (sam .TCC_SYNCBUSY_CTRLB ) {
@@ -1297,7 +1307,7 @@ func (pwm PWM) Set(value uint16) {
12971307 }
12981308
12991309 // TCCx->CCBUF[tcChannel].reg = (uint32_t) value;
1300- pwm .setChannelBuffer (uint32 (value ))
1310+ pwm .setChannelBuffer (timer , uint32 (value ))
13011311
13021312 for timer .SYNCBUSY .HasBits (sam .TCC_SYNCBUSY_CC0 ) ||
13031313 timer .SYNCBUSY .HasBits (sam .TCC_SYNCBUSY_CC1 ) {
@@ -1329,61 +1339,61 @@ func (pwm PWM) setPinCfg(val uint8) {
13291339 pwm .Pin .setPinCfg (val )
13301340}
13311341
1332- // setChannel sets the value for the correct channel for PWM on this pin
1333- func (pwm PWM ) setChannel (val uint32 ) {
1342+ // setChannel sets the value for the correct channel for PWM on this pin.
1343+ func (pwm PWM ) setChannel (timer * sam. TCC_Type , val uint32 ) {
13341344 switch pwm .Pin {
13351345 case PA16 :
1336- pwm . getTimer () .CC [0 ].Set (val )
1346+ timer .CC [0 ].Set (val )
13371347 case PA17 :
1338- pwm . getTimer () .CC [1 ].Set (val )
1348+ timer .CC [1 ].Set (val )
13391349 case PA14 :
1340- pwm . getTimer () .CC [0 ].Set (val )
1350+ timer .CC [0 ].Set (val )
13411351 case PA15 :
1342- pwm . getTimer () .CC [1 ].Set (val )
1352+ timer .CC [1 ].Set (val )
13431353 case PA18 :
1344- pwm . getTimer () .CC [2 ].Set (val )
1354+ timer .CC [2 ].Set (val )
13451355 case PA19 :
1346- pwm . getTimer () .CC [3 ].Set (val )
1356+ timer .CC [3 ].Set (val )
13471357 case PA20 :
1348- pwm . getTimer () .CC [0 ].Set (val )
1358+ timer .CC [0 ].Set (val )
13491359 case PA21 :
1350- pwm . getTimer () .CC [1 ].Set (val )
1360+ timer .CC [1 ].Set (val )
13511361 case PA23 :
1352- pwm . getTimer () .CC [3 ].Set (val )
1362+ timer .CC [3 ].Set (val )
13531363 case PA22 :
1354- pwm . getTimer () .CC [2 ].Set (val )
1364+ timer .CC [2 ].Set (val )
13551365 case PB31 :
1356- pwm . getTimer () .CC [1 ].Set (val )
1366+ timer .CC [1 ].Set (val )
13571367 default :
13581368 return // not supported on this pin
13591369 }
13601370}
13611371
13621372// setChannelBuffer sets the value for the correct channel buffer for PWM on this pin
1363- func (pwm PWM ) setChannelBuffer (val uint32 ) {
1373+ func (pwm PWM ) setChannelBuffer (timer * sam. TCC_Type , val uint32 ) {
13641374 switch pwm .Pin {
13651375 case PA16 :
1366- pwm . getTimer () .CCBUF [0 ].Set (val )
1376+ timer .CCBUF [0 ].Set (val )
13671377 case PA17 :
1368- pwm . getTimer () .CCBUF [1 ].Set (val )
1378+ timer .CCBUF [1 ].Set (val )
13691379 case PA14 :
1370- pwm . getTimer () .CCBUF [0 ].Set (val )
1380+ timer .CCBUF [0 ].Set (val )
13711381 case PA15 :
1372- pwm . getTimer () .CCBUF [1 ].Set (val )
1382+ timer .CCBUF [1 ].Set (val )
13731383 case PA18 :
1374- pwm . getTimer () .CCBUF [2 ].Set (val )
1384+ timer .CCBUF [2 ].Set (val )
13751385 case PA19 :
1376- pwm . getTimer () .CCBUF [3 ].Set (val )
1386+ timer .CCBUF [3 ].Set (val )
13771387 case PA20 :
1378- pwm . getTimer () .CCBUF [0 ].Set (val )
1388+ timer .CCBUF [0 ].Set (val )
13791389 case PA21 :
1380- pwm . getTimer () .CCBUF [1 ].Set (val )
1390+ timer .CCBUF [1 ].Set (val )
13811391 case PA23 :
1382- pwm . getTimer () .CCBUF [3 ].Set (val )
1392+ timer .CCBUF [3 ].Set (val )
13831393 case PA22 :
1384- pwm . getTimer () .CCBUF [2 ].Set (val )
1394+ timer .CCBUF [2 ].Set (val )
13851395 case PB31 :
1386- pwm . getTimer () .CCBUF [1 ].Set (val )
1396+ timer .CCBUF [1 ].Set (val )
13871397 default :
13881398 return // not supported on this pin
13891399 }
0 commit comments