Skip to content

Commit 8162eb2

Browse files
committed
Move internal ADC channels to Arduino_defines. Rename the channels to more closely match Arduino analog reference names.
See names on https://www.arduino.cc/reference/en/language/functions/analog-io/analogreference/
1 parent 7d6aa1c commit 8162eb2

File tree

4 files changed

+100
-81
lines changed

4 files changed

+100
-81
lines changed

cores/arduino/ard_sup/Arduino_defines.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11

22

3-
4-
53
// Constants
6-
#define LOW (0x0)
7-
#define HIGH (0x1)
4+
#define LOW (0x0)
5+
#define HIGH (0x1)
86

9-
#define CHANGE (0x02)
10-
#define FALLING (0x03)
11-
#define RISING (0x04)
7+
#define CHANGE (0x02)
8+
#define FALLING (0x03)
9+
#define RISING (0x04)
1210

1311
#define PI 3.1415926535897932384626433832795
1412
#define HALF_PI 1.5707963267948966192313216916398
@@ -17,6 +15,12 @@
1715
#define RAD_TO_DEG 57.295779513082320876798154814105
1816
#define EULER 2.718281828459045235360287471352
1917

18+
//Special "pads" used by the ADC to select the special internal ADC channels
19+
#define ADC_DIFF0 100
20+
#define ADC_DIFF1 101
21+
#define ADC_INTERNAL_TEMP 102
22+
#define ADC_INTERNAL_VCC_DIV3 103
23+
#define ADC_INTERNAL_VSS 104
2024

2125
// Functions
2226
// undefine stdlib's abs if encountered
@@ -43,4 +47,4 @@
4347

4448
#define bit(b) (1UL << (b))
4549

46-
#define digitalPinToInterrupt(P) (P) // all apollo3 pads are interrupt capable
50+
#define digitalPinToInterrupt(P) (P) // all apollo3 pads are interrupt capable

cores/arduino/ard_sup/analog/ap3_analog.cpp

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,18 @@ ap3_err_t ap3_set_pin_to_analog(uint8_t pinNumber)
293293
uint8_t funcsel = 0;
294294
am_hal_gpio_pincfg_t pincfg = INPUT;
295295

296-
retval = ap3_analog_pad_funcsel(ap3_gpio_pin2pad(pinNumber), &funcsel);
296+
//Handle special ADC channels
297+
if (pinNumber >= ADC_DIFF0 && pinNumber <= ADC_INTERNAL_VSS)
298+
{
299+
//Don't use the pin to pad lookup from the variant file
300+
retval = ap3_analog_pad_funcsel(pinNumber, &funcsel);
301+
}
302+
else
303+
{
304+
//Normal pin lookup
305+
retval = ap3_analog_pad_funcsel(ap3_gpio_pin2pad(pinNumber), &funcsel);
306+
}
307+
297308
if (retval != AP3_OK)
298309
{
299310
return retval;
@@ -355,15 +366,15 @@ ap3_err_t ap3_change_channel(uint8_t padNumber)
355366
return AP3_OK;
356367
}
357368

358-
359-
bool ap3_pwm_is_running(uint32_t ui32TimerNumber, uint32_t ui32TimerSegment){
369+
bool ap3_pwm_is_running(uint32_t ui32TimerNumber, uint32_t ui32TimerSegment)
370+
{
360371
volatile uint32_t *pui32ConfigReg;
361372
bool is_enabled = false;
362373

363374
//
364375
// Find the correct control register.
365376
//
366-
pui32ConfigReg = (uint32_t*)CTIMERADDRn(CTIMER, ui32TimerNumber, CTRL0);
377+
pui32ConfigReg = (uint32_t *)CTIMERADDRn(CTIMER, ui32TimerNumber, CTRL0);
367378

368379
//
369380
// Begin critical section while config registers are read and modified.
@@ -378,7 +389,8 @@ bool ap3_pwm_is_running(uint32_t ui32TimerNumber, uint32_t ui32TimerSegment){
378389
//
379390
// Check the "enable bit"
380391
//
381-
if( ui32ConfigVal & (CTIMER_CTRL0_TMRA0EN_Msk | CTIMER_CTRL0_TMRB0EN_Msk) ){
392+
if (ui32ConfigVal & (CTIMER_CTRL0_TMRA0EN_Msk | CTIMER_CTRL0_TMRB0EN_Msk))
393+
{
382394
is_enabled = true;
383395
}
384396

@@ -390,39 +402,51 @@ bool ap3_pwm_is_running(uint32_t ui32TimerNumber, uint32_t ui32TimerSegment){
390402
return is_enabled;
391403
}
392404

393-
394-
void ap3_pwm_wait_for_pulse(uint32_t timer, uint32_t segment, uint32_t output, uint32_t margin){
405+
void ap3_pwm_wait_for_pulse(uint32_t timer, uint32_t segment, uint32_t output, uint32_t margin)
406+
{
395407

396408
volatile uint32_t *pui32CompareReg;
397409
volatile uint32_t ctimer_val;
398410
uint32_t cmpr0;
399411

400412
// Only wait if the ctimer is running to avoid a deadlock
401-
if( ap3_pwm_is_running( timer, segment) ){
413+
if (ap3_pwm_is_running(timer, segment))
414+
{
402415

403416
// 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);
417+
if (segment == AM_HAL_CTIMER_TIMERA)
418+
{
419+
if (output == AM_HAL_CTIMER_OUTPUT_NORMAL)
420+
{
421+
pui32CompareReg = (uint32_t *)CTIMERADDRn(CTIMER, timer, CMPRA0);
422+
}
423+
else
424+
{
425+
pui32CompareReg = (uint32_t *)CTIMERADDRn(CTIMER, timer, CMPRAUXA0);
426+
}
427+
}
428+
else
429+
{
430+
if (output == AM_HAL_CTIMER_OUTPUT_NORMAL)
431+
{
432+
pui32CompareReg = (uint32_t *)CTIMERADDRn(CTIMER, timer, CMPRB0);
409433
}
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);
434+
else
435+
{
436+
pui32CompareReg = (uint32_t *)CTIMERADDRn(CTIMER, timer, CMPRAUXB0);
415437
}
416438
}
417439

418440
// Get the compare value
419441
cmpr0 = ((uint32_t)(*(pui32CompareReg)) & 0x0000FFFF);
420442

421-
if( cmpr0 ){ // Only wait when cmpr0 is greater than 0 to avoid an infinite while loop
443+
if (cmpr0)
444+
{ // Only wait when cmpr0 is greater than 0 to avoid an infinite while loop
422445
// 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);
446+
ctimer_val = am_hal_ctimer_read(timer, segment);
447+
while ((ctimer_val + 0) >= cmpr0)
448+
{
449+
ctimer_val = am_hal_ctimer_read(timer, segment);
426450
}
427451
}
428452
}
@@ -534,16 +558,16 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
534558
if ((th == 0) || (fw == 0))
535559
{
536560
output = AM_HAL_CTIMER_OUTPUT_FORCE0;
537-
set_periods = false; // disable setting periods when going into a forced mode
561+
set_periods = false; // disable setting periods when going into a forced mode
538562
}
539563
else if (th == fw)
540564
{
541565
output = AM_HAL_CTIMER_OUTPUT_FORCE1;
542-
set_periods = false; // disable setting periods when going into a forced mode
566+
set_periods = false; // disable setting periods when going into a forced mode
543567
}
544568

545569
// Wait until after high pulse to change the state (avoids inversion)
546-
ap3_pwm_wait_for_pulse( timer, segment, output, 10);
570+
ap3_pwm_wait_for_pulse(timer, segment, output, 10);
547571

548572
// Configure the pin
549573
am_hal_ctimer_output_config(timer,
@@ -558,7 +582,8 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
558582
// (AM_HAL_CTIMER_FN_PWM_REPEAT | AP3_ANALOG_CLK | AM_HAL_CTIMER_INT_ENABLE) );
559583
(AM_HAL_CTIMER_FN_PWM_REPEAT | clk));
560584

561-
if(set_periods){
585+
if (set_periods)
586+
{
562587
// If this pad uses secondary output:
563588
if (output == AM_HAL_CTIMER_OUTPUT_SECONDARY)
564589
{
@@ -603,17 +628,17 @@ ap3_err_t analogWriteResolution(uint8_t res)
603628
ap3_err_t analogWrite(uint8_t pin, uint32_t val)
604629
{
605630
// 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)
631+
uint32_t clk = AM_HAL_CTIMER_HFRC_12MHZ; // Use an Ambiq HAL provided value to select which clock
632+
uint32_t fw = 0xFFFF; // Choose the frame width in clock periods (32767 -> ~ 180 Hz)
608633
if (val >= ((0x01 << _analogWriteBits) - 1))
609634
{
610635
val = fw; // Enable FORCE1
611636
}
612637
else
613638
{
614-
val <<= (16 - _analogWriteBits); // Shift over the value to fill available resolution
639+
val <<= (16 - _analogWriteBits); // Shift over the value to fill available resolution
615640
}
616-
641+
617642
return ap3_pwm_output(pin, val, fw, clk);
618643
}
619644

variants/SparkFun_BlackBoard_Artemis/config/variant.cpp

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,38 @@ SOFTWARE.
2525
const ap3_gpio_pad_t ap3_variant_pinmap[AP3_VARIANT_NUM_PINS] = {
2626
//~ = PWM, A = ADC
2727
//Apollo Pad, //Silkscreen indicator - Pin functions
28-
25, //0 - ~RX1/SDA2/MISO2
29-
24, //1 - ~TX1/32kHz/SWO
30-
35, //2/A6 - ~A/TX1/I2SDAT/PDMCLK
31-
4, //3 - ~RX1/SLINT
32-
22, //4 - ~PDMCLK/SWO
33-
23, //5 - ~I2SWCLK/CMPOUT
34-
27, //6 - ~SCL2/SCK2
35-
28, //7 - ~MOSI2/I2SWCLK
36-
32, //8/A8 - ~A/SCCIO
37-
12, //9/A9 - ~A/PDMCLK/TX1
38-
13, //10/A10 - ~A/I2SBCLK/RX1
39-
7, //11 - ~MOSI0/CLKOUT
40-
6, //12 - ~MISO0/SDA0/I2SDAT
41-
5, //13 - ~LED/SCK0/SCL0
42-
40, //14 - SDA4/MISO4/RX1
43-
39, //15 - ~SCL4/SCK4/TX1
44-
29, //16/A0 - ~A/PDMDATA
45-
11, //17/A1 - ~A/PDMDATA
46-
34, //18/A2 - A/CMPRF2/PDMDATA
47-
33, //19/A3 - ~A/SWO/32kHz
48-
16, //20/A4 - A/TRIG0/SCCRST
49-
31, //21/A5 - ~A/SCCCLK
50-
48, //22 - ~TX0/SCL5/SCK5
51-
49, //23 - ~RX0/SDA5/MISO5
52-
8, //24 - Solder pad, SCL1/SCK1/TX1/SCCLK
53-
9, //25 - Solder pad, SDA1/MISO1/RX1/SCCIO
54-
10, //26 - Solder pad, MOSI1/TX1/PDMCLK
55-
38, //27 - Solder pad, MOSI3/TX1
56-
42, //28 - Solder pad, ~SCL3/SCK3/TX1
57-
43, //29 - Solder pad, ~SDA3/MISO3/RX1
58-
36, //30 - Not exposed, PDMDATA of Mic
59-
37, //31 - Not exposed, PDMCLK of Mic
60-
AP3_ADC_DIFF0_PAD, //32 - Not a real pad, ADC_DIFF0
61-
AP3_ADC_DIFF1_PAD, //33 - Not a real pad, ADC_DIFF1
62-
AP3_ADC_TEMP_PAD, //34 - Not a real pad, ADC_TEMP
63-
AP3_ADC_DIV3_PAD, //35 - Not a real pad, ADC_DIV3
64-
AP3_ADC_VSS_PAD, //36 - Not a real pad, ADC_VSS
28+
25, //0 - ~RX1/SDA2/MISO2
29+
24, //1 - ~TX1/32kHz/SWO
30+
35, //2/A6 - ~A/TX1/I2SDAT/PDMCLK
31+
4, //3 - ~RX1/SLINT
32+
22, //4 - ~PDMCLK/SWO
33+
23, //5 - ~I2SWCLK/CMPOUT
34+
27, //6 - ~SCL2/SCK2
35+
28, //7 - ~MOSI2/I2SWCLK
36+
32, //8/A8 - ~A/SCCIO
37+
12, //9/A9 - ~A/PDMCLK/TX1
38+
13, //10/A10 - ~A/I2SBCLK/RX1
39+
7, //11 - ~MOSI0/CLKOUT
40+
6, //12 - ~MISO0/SDA0/I2SDAT
41+
5, //13 - ~LED/SCK0/SCL0
42+
40, //14 - SDA4/MISO4/RX1
43+
39, //15 - ~SCL4/SCK4/TX1
44+
29, //16/A0 - ~A/PDMDATA
45+
11, //17/A1 - ~A/PDMDATA
46+
34, //18/A2 - A/CMPRF2/PDMDATA
47+
33, //19/A3 - ~A/SWO/32kHz
48+
16, //20/A4 - A/TRIG0/SCCRST
49+
31, //21/A5 - ~A/SCCCLK
50+
48, //22 - ~TX0/SCL5/SCK5
51+
49, //23 - ~RX0/SDA5/MISO5
52+
8, //24 - Solder pad, SCL1/SCK1/TX1/SCCLK
53+
9, //25 - Solder pad, SDA1/MISO1/RX1/SCCIO
54+
10, //26 - Solder pad, MOSI1/TX1/PDMCLK
55+
38, //27 - Solder pad, MOSI3/TX1
56+
42, //28 - Solder pad, ~SCL3/SCK3/TX1
57+
43, //29 - Solder pad, ~SDA3/MISO3/RX1
58+
36, //30 - Not exposed, PDMDATA of Mic
59+
37, //31 - Not exposed, PDMCLK of Mic
6560
};
6661

6762
// Uart Definitions

variants/SparkFun_BlackBoard_Artemis/config/variant.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ SOFTWARE.
2424

2525
#include "Arduino.h"
2626

27-
#define AP3_VARIANT_NUM_PINS (37)
27+
#define AP3_VARIANT_NUM_PINS (32)
2828

2929
// Pin map declaration
3030
extern const ap3_gpio_pad_t ap3_variant_pinmap[AP3_VARIANT_NUM_PINS];
@@ -58,11 +58,6 @@ extern Uart Serial1;
5858
#define A8 8
5959
#define A9 9
6060
#define A10 10
61-
#define ADC_DIFF0 32 //Not legal pins. Used for pad lookup
62-
#define ADC_DIFF1 33
63-
#define ADC_TEMP 34
64-
#define ADC_DIV3 35
65-
#define ADC_VSS 36
6661

6762
#define LED_BUILTIN 13
6863

0 commit comments

Comments
 (0)