@@ -353,42 +353,52 @@ ap3_err_t ap3_change_channel(uint8_t padNumber)
353
353
}
354
354
}
355
355
356
-
357
- void ap3_pwm_wait_for_pulse ( uint32_t timer, uint32_t segment, uint32_t output, uint32_t margin) {
356
+ void ap3_pwm_wait_for_pulse ( uint32_t timer, uint32_t segment, uint32_t output, uint32_t margin)
357
+ {
358
358
359
359
volatile uint32_t *pui32CompareReg;
360
360
volatile uint32_t ctimer_val;
361
361
uint32_t cmpr0;
362
362
363
363
// Get the comapre register address
364
- if ( segment == AM_HAL_CTIMER_TIMERA ){
365
- if ( output == AM_HAL_CTIMER_OUTPUT_NORMAL ) {
366
- pui32CompareReg = ( uint32_t *) CTIMERADDRn (CTIMER, timer, CMPRA0);
367
- } else {
368
- pui32CompareReg = (uint32_t *)CTIMERADDRn (CTIMER, timer, CMPRAUXA0 );
364
+ if ( segment == AM_HAL_CTIMER_TIMERA)
365
+ {
366
+ if (output == AM_HAL_CTIMER_OUTPUT_NORMAL)
367
+ {
368
+ pui32CompareReg = (uint32_t *)CTIMERADDRn (CTIMER, timer, CMPRA0 );
369
369
}
370
- }else {
371
- if ( output == AM_HAL_CTIMER_OUTPUT_NORMAL ){
372
- pui32CompareReg = (uint32_t *)CTIMERADDRn (CTIMER, timer, CMPRB0);
373
- }else {
374
- pui32CompareReg = (uint32_t *)CTIMERADDRn (CTIMER, timer, CMPRAUXB0);
370
+ else
371
+ {
372
+ pui32CompareReg = (uint32_t *)CTIMERADDRn (CTIMER, timer, CMPRAUXA0);
373
+ }
374
+ }
375
+ else
376
+ {
377
+ if (output == AM_HAL_CTIMER_OUTPUT_NORMAL)
378
+ {
379
+ pui32CompareReg = (uint32_t *)CTIMERADDRn (CTIMER, timer, CMPRB0);
380
+ }
381
+ else
382
+ {
383
+ pui32CompareReg = (uint32_t *)CTIMERADDRn (CTIMER, timer, CMPRAUXB0);
375
384
}
376
385
}
377
386
378
387
// Get the compare value
379
388
cmpr0 = ((uint32_t )(*(pui32CompareReg)) & 0x0000FFFF );
380
-
389
+
381
390
// Wait for the timer value to be less than the compare value so that it is safe to change
382
- ctimer_val = am_hal_ctimer_read ( timer, segment);
383
- while ( (ctimer_val + 0 ) > cmpr0 ){
384
- ctimer_val = am_hal_ctimer_read ( timer, segment);
391
+ ctimer_val = am_hal_ctimer_read (timer, segment);
392
+ while ((ctimer_val + 0 ) > cmpr0)
393
+ {
394
+ ctimer_val = am_hal_ctimer_read (timer, segment);
385
395
}
386
396
}
387
397
388
398
// **********************************************
389
399
// ap3_pwm_output
390
- // - This function allows you to specify an arbitrary pwm output signal with a given frame width (fw) and time high (th).
391
- // - Due to contraints of the hardware th must be lesser than fw by at least 2.
400
+ // - This function allows you to specify an arbitrary pwm output signal with a given frame width (fw) and time high (th).
401
+ // - Due to contraints of the hardware th must be lesser than fw by at least 2.
392
402
// - Furthermore fw must be at least 3 to see any high pulses
393
403
//
394
404
// This causes the most significant deviations for small values of fw. For example:
@@ -410,7 +420,7 @@ void ap3_pwm_wait_for_pulse(uint32_t timer, uint32_t segment, uint32_t output, u
410
420
//
411
421
// ...
412
422
//
413
- // Then we conclude that for the case th == (fw - 1) the duty cycle will be 100% and
423
+ // Then we conclude that for the case th == (fw - 1) the duty cycle will be 100% and
414
424
// the percent error from the expected duty cycle will be 100/fw
415
425
// **********************************************
416
426
@@ -419,7 +429,8 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
419
429
// handle configuration, if necessary
420
430
ap3_err_t retval = AP3_OK;
421
431
422
- if ( fw > 0 ){ // reduce fw so that the user's desired value is the period
432
+ if (fw > 0 )
433
+ { // reduce fw so that the user's desired value is the period
423
434
fw--;
424
435
}
425
436
@@ -467,7 +478,7 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
467
478
}
468
479
}
469
480
else
470
- { // Use the 0th index of the outcfg_tbl to select the functions
481
+ { // Use the 0th index of the outcfg_tbl to select the functions
471
482
timer = OUTCTIMN (ctx, 0 );
472
483
if (OUTCTIMB (ctx, 0 ))
473
484
{
@@ -480,16 +491,17 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
480
491
}
481
492
482
493
// Ensure that th is not greater than the fw
483
- if (th > fw){
494
+ if (th > fw)
495
+ {
484
496
th = fw;
485
497
}
486
498
487
499
// Test for AM_HAL_CTIMER_OUTPUT_FORCE0 or AM_HAL_CTIMER_OUTPUT_FORCE1
488
- if (( th == 0 ) || ( fw == 0 ))
500
+ if (( th == 0 ) || (fw == 0 ))
489
501
{
490
502
output = AM_HAL_CTIMER_OUTPUT_FORCE0;
491
503
}
492
- else if ( th == fw )
504
+ else if ( th == fw)
493
505
{
494
506
output = AM_HAL_CTIMER_OUTPUT_FORCE1;
495
507
}
@@ -508,7 +520,7 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
508
520
(AM_HAL_CTIMER_FN_PWM_REPEAT | clk));
509
521
510
522
// Wait until after high pulse to change the state (avoids inversion)
511
- ap3_pwm_wait_for_pulse ( timer, segment, output, 10 );
523
+ ap3_pwm_wait_for_pulse (timer, segment, output, 10 );
512
524
513
525
// If this pad uses secondary output:
514
526
if (output == AM_HAL_CTIMER_OUTPUT_SECONDARY)
@@ -553,14 +565,17 @@ ap3_err_t analogWriteResolution(uint8_t res)
553
565
ap3_err_t analogWrite (uint8_t pin, uint32_t val)
554
566
{
555
567
// Determine the high time based on input value and the current resolution setting
556
- uint32_t fw = 0xFFFF ; // Choose the frame width in clock periods (32767 -> ~ 180 Hz)
557
- if ( val == ((0x01 << _analogWriteBits ) - 1 ) ){
558
- val = fw; // Enable FORCE1
559
- }else {
560
- val <<= (16 - _analogWriteBits); // Shift over the value to fill available resolution
561
- }
562
- uint32_t clk = AM_HAL_CTIMER_HFRC_12MHZ; // Use an Ambiq HAL provided value to select which clock
563
-
568
+ uint32_t fw = 0xFFFF ; // Choose the frame width in clock periods (32767 -> ~ 180 Hz)
569
+ if (val == ((0x01 << _analogWriteBits) - 1 ))
570
+ {
571
+ val = fw; // Enable FORCE1
572
+ }
573
+ else
574
+ {
575
+ val <<= (16 - _analogWriteBits); // Shift over the value to fill available resolution
576
+ }
577
+ uint32_t clk = AM_HAL_CTIMER_HFRC_12MHZ; // Use an Ambiq HAL provided value to select which clock
578
+
564
579
return ap3_pwm_output (pin, val, fw, clk);
565
580
}
566
581
0 commit comments