@@ -129,7 +129,17 @@ uint16_t analogRead(uint8_t pinNumber)
129
129
am_hal_adc_sample_t Sample;
130
130
uint32_t ui32NumSamples = 1 ;
131
131
132
- uint8_t padNumber = ap3_gpio_pin2pad (pinNumber);
132
+ uint8_t padNumber;
133
+
134
+ if (pinNumber >= ADC_DIFF0 && pinNumber <= ADC_INTERNAL_VSS)
135
+ {
136
+ // Special handling of internal ADC channels
137
+ padNumber = pinNumber;
138
+ }
139
+ else
140
+ {
141
+ padNumber = ap3_gpio_pin2pad (pinNumber);
142
+ }
133
143
134
144
// Look up configuration status based on pad number
135
145
uint8_t indi;
@@ -292,8 +302,8 @@ ap3_err_t ap3_set_pin_to_analog(uint8_t pinNumber)
292
302
293
303
uint8_t funcsel = 0 ;
294
304
am_hal_gpio_pincfg_t pincfg = INPUT;
295
-
296
305
retval = ap3_analog_pad_funcsel (ap3_gpio_pin2pad (pinNumber), &funcsel);
306
+
297
307
if (retval != AP3_OK)
298
308
{
299
309
return retval;
@@ -355,15 +365,15 @@ ap3_err_t ap3_change_channel(uint8_t padNumber)
355
365
return AP3_OK;
356
366
}
357
367
358
-
359
- bool ap3_pwm_is_running ( uint32_t ui32TimerNumber, uint32_t ui32TimerSegment) {
368
+ bool ap3_pwm_is_running ( uint32_t ui32TimerNumber, uint32_t ui32TimerSegment)
369
+ {
360
370
volatile uint32_t *pui32ConfigReg;
361
371
bool is_enabled = false ;
362
372
363
373
//
364
374
// Find the correct control register.
365
375
//
366
- pui32ConfigReg = (uint32_t *)CTIMERADDRn (CTIMER, ui32TimerNumber, CTRL0);
376
+ pui32ConfigReg = (uint32_t *)CTIMERADDRn (CTIMER, ui32TimerNumber, CTRL0);
367
377
368
378
//
369
379
// Begin critical section while config registers are read and modified.
@@ -378,7 +388,8 @@ bool ap3_pwm_is_running(uint32_t ui32TimerNumber, uint32_t ui32TimerSegment){
378
388
//
379
389
// Check the "enable bit"
380
390
//
381
- if ( ui32ConfigVal & (CTIMER_CTRL0_TMRA0EN_Msk | CTIMER_CTRL0_TMRB0EN_Msk) ){
391
+ if (ui32ConfigVal & (CTIMER_CTRL0_TMRA0EN_Msk | CTIMER_CTRL0_TMRB0EN_Msk))
392
+ {
382
393
is_enabled = true ;
383
394
}
384
395
@@ -390,39 +401,51 @@ bool ap3_pwm_is_running(uint32_t ui32TimerNumber, uint32_t ui32TimerSegment){
390
401
return is_enabled;
391
402
}
392
403
393
-
394
- void ap3_pwm_wait_for_pulse ( uint32_t timer, uint32_t segment, uint32_t output, uint32_t margin) {
404
+ void ap3_pwm_wait_for_pulse ( uint32_t timer, uint32_t segment, uint32_t output, uint32_t margin)
405
+ {
395
406
396
407
volatile uint32_t *pui32CompareReg;
397
408
volatile uint32_t ctimer_val;
398
409
uint32_t cmpr0;
399
410
400
411
// Only wait if the ctimer is running to avoid a deadlock
401
- if ( ap3_pwm_is_running ( timer, segment) ){
412
+ if (ap3_pwm_is_running (timer, segment))
413
+ {
402
414
403
415
// Get the comapre register address
404
- if ( segment == AM_HAL_CTIMER_TIMERA ){
405
- if ( output == AM_HAL_CTIMER_OUTPUT_NORMAL ) {
406
- pui32CompareReg = ( uint32_t *) CTIMERADDRn (CTIMER, timer, CMPRA0);
407
- } else {
408
- pui32CompareReg = (uint32_t *)CTIMERADDRn (CTIMER, timer, CMPRAUXA0 );
416
+ if ( segment == AM_HAL_CTIMER_TIMERA)
417
+ {
418
+ if (output == AM_HAL_CTIMER_OUTPUT_NORMAL)
419
+ {
420
+ pui32CompareReg = (uint32_t *)CTIMERADDRn (CTIMER, timer, CMPRA0 );
409
421
}
410
- }else {
411
- if ( output == AM_HAL_CTIMER_OUTPUT_NORMAL ){
412
- pui32CompareReg = (uint32_t *)CTIMERADDRn (CTIMER, timer, CMPRB0);
413
- }else {
414
- pui32CompareReg = (uint32_t *)CTIMERADDRn (CTIMER, timer, CMPRAUXB0);
422
+ else
423
+ {
424
+ pui32CompareReg = (uint32_t *)CTIMERADDRn (CTIMER, timer, CMPRAUXA0);
425
+ }
426
+ }
427
+ else
428
+ {
429
+ if (output == AM_HAL_CTIMER_OUTPUT_NORMAL)
430
+ {
431
+ pui32CompareReg = (uint32_t *)CTIMERADDRn (CTIMER, timer, CMPRB0);
432
+ }
433
+ else
434
+ {
435
+ pui32CompareReg = (uint32_t *)CTIMERADDRn (CTIMER, timer, CMPRAUXB0);
415
436
}
416
437
}
417
438
418
439
// Get the compare value
419
440
cmpr0 = ((uint32_t )(*(pui32CompareReg)) & 0x0000FFFF );
420
441
421
- if ( cmpr0 ){ // Only wait when cmpr0 is greater than 0 to avoid an infinite while loop
442
+ if (cmpr0)
443
+ { // Only wait when cmpr0 is greater than 0 to avoid an infinite while loop
422
444
// Wait for the timer value to be less than the compare value so that it is safe to change
423
- ctimer_val = am_hal_ctimer_read ( timer, segment);
424
- while ( (ctimer_val + 0 ) >= cmpr0 ){
425
- ctimer_val = am_hal_ctimer_read ( timer, segment);
445
+ ctimer_val = am_hal_ctimer_read (timer, segment);
446
+ while ((ctimer_val + 0 ) >= cmpr0)
447
+ {
448
+ ctimer_val = am_hal_ctimer_read (timer, segment);
426
449
}
427
450
}
428
451
}
@@ -534,16 +557,16 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
534
557
if ((th == 0 ) || (fw == 0 ))
535
558
{
536
559
output = AM_HAL_CTIMER_OUTPUT_FORCE0;
537
- set_periods = false ; // disable setting periods when going into a forced mode
560
+ set_periods = false ; // disable setting periods when going into a forced mode
538
561
}
539
562
else if (th == fw)
540
563
{
541
564
output = AM_HAL_CTIMER_OUTPUT_FORCE1;
542
- set_periods = false ; // disable setting periods when going into a forced mode
565
+ set_periods = false ; // disable setting periods when going into a forced mode
543
566
}
544
567
545
568
// Wait until after high pulse to change the state (avoids inversion)
546
- ap3_pwm_wait_for_pulse ( timer, segment, output, 10 );
569
+ ap3_pwm_wait_for_pulse (timer, segment, output, 10 );
547
570
548
571
// Configure the pin
549
572
am_hal_ctimer_output_config (timer,
@@ -558,7 +581,8 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
558
581
// (AM_HAL_CTIMER_FN_PWM_REPEAT | AP3_ANALOG_CLK | AM_HAL_CTIMER_INT_ENABLE) );
559
582
(AM_HAL_CTIMER_FN_PWM_REPEAT | clk));
560
583
561
- if (set_periods){
584
+ if (set_periods)
585
+ {
562
586
// If this pad uses secondary output:
563
587
if (output == AM_HAL_CTIMER_OUTPUT_SECONDARY)
564
588
{
@@ -603,17 +627,17 @@ ap3_err_t analogWriteResolution(uint8_t res)
603
627
ap3_err_t analogWrite (uint8_t pin, uint32_t val)
604
628
{
605
629
// Determine the high time based on input value and the current resolution setting
606
- uint32_t clk = AM_HAL_CTIMER_HFRC_12MHZ; // Use an Ambiq HAL provided value to select which clock
607
- uint32_t fw = 0xFFFF ; // Choose the frame width in clock periods (32767 -> ~ 180 Hz)
630
+ uint32_t clk = AM_HAL_CTIMER_HFRC_12MHZ; // Use an Ambiq HAL provided value to select which clock
631
+ uint32_t fw = 0xFFFF ; // Choose the frame width in clock periods (32767 -> ~ 180 Hz)
608
632
if (val >= ((0x01 << _analogWriteBits) - 1 ))
609
633
{
610
634
val = fw; // Enable FORCE1
611
635
}
612
636
else
613
637
{
614
- val <<= (16 - _analogWriteBits); // Shift over the value to fill available resolution
638
+ val <<= (16 - _analogWriteBits); // Shift over the value to fill available resolution
615
639
}
616
-
640
+
617
641
return ap3_pwm_output (pin, val, fw, clk);
618
642
}
619
643
0 commit comments