Skip to content

Commit 410fe55

Browse files
authored
Merge pull request #51 from sparkfun/adcChannelMove
Move ADC channel selection out of variant files
2 parents 7d6aa1c + 6f8975d commit 410fe55

File tree

10 files changed

+143
-141
lines changed

10 files changed

+143
-141
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 & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,17 @@ uint16_t analogRead(uint8_t pinNumber)
129129
am_hal_adc_sample_t Sample;
130130
uint32_t ui32NumSamples = 1;
131131

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+
}
133143

134144
//Look up configuration status based on pad number
135145
uint8_t indi;
@@ -292,8 +302,8 @@ ap3_err_t ap3_set_pin_to_analog(uint8_t pinNumber)
292302

293303
uint8_t funcsel = 0;
294304
am_hal_gpio_pincfg_t pincfg = INPUT;
295-
296305
retval = ap3_analog_pad_funcsel(ap3_gpio_pin2pad(pinNumber), &funcsel);
306+
297307
if (retval != AP3_OK)
298308
{
299309
return retval;
@@ -355,15 +365,15 @@ ap3_err_t ap3_change_channel(uint8_t padNumber)
355365
return AP3_OK;
356366
}
357367

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+
{
360370
volatile uint32_t *pui32ConfigReg;
361371
bool is_enabled = false;
362372

363373
//
364374
// Find the correct control register.
365375
//
366-
pui32ConfigReg = (uint32_t*)CTIMERADDRn(CTIMER, ui32TimerNumber, CTRL0);
376+
pui32ConfigReg = (uint32_t *)CTIMERADDRn(CTIMER, ui32TimerNumber, CTRL0);
367377

368378
//
369379
// 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){
378388
//
379389
// Check the "enable bit"
380390
//
381-
if( ui32ConfigVal & (CTIMER_CTRL0_TMRA0EN_Msk | CTIMER_CTRL0_TMRB0EN_Msk) ){
391+
if (ui32ConfigVal & (CTIMER_CTRL0_TMRA0EN_Msk | CTIMER_CTRL0_TMRB0EN_Msk))
392+
{
382393
is_enabled = true;
383394
}
384395

@@ -390,39 +401,51 @@ bool ap3_pwm_is_running(uint32_t ui32TimerNumber, uint32_t ui32TimerSegment){
390401
return is_enabled;
391402
}
392403

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+
{
395406

396407
volatile uint32_t *pui32CompareReg;
397408
volatile uint32_t ctimer_val;
398409
uint32_t cmpr0;
399410

400411
// 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+
{
402414

403415
// 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);
409421
}
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);
415436
}
416437
}
417438

418439
// Get the compare value
419440
cmpr0 = ((uint32_t)(*(pui32CompareReg)) & 0x0000FFFF);
420441

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
422444
// 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);
426449
}
427450
}
428451
}
@@ -534,16 +557,16 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
534557
if ((th == 0) || (fw == 0))
535558
{
536559
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
538561
}
539562
else if (th == fw)
540563
{
541564
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
543566
}
544567

545568
// 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);
547570

548571
// Configure the pin
549572
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)
558581
// (AM_HAL_CTIMER_FN_PWM_REPEAT | AP3_ANALOG_CLK | AM_HAL_CTIMER_INT_ENABLE) );
559582
(AM_HAL_CTIMER_FN_PWM_REPEAT | clk));
560583

561-
if(set_periods){
584+
if (set_periods)
585+
{
562586
// If this pad uses secondary output:
563587
if (output == AM_HAL_CTIMER_OUTPUT_SECONDARY)
564588
{
@@ -603,17 +627,17 @@ ap3_err_t analogWriteResolution(uint8_t res)
603627
ap3_err_t analogWrite(uint8_t pin, uint32_t val)
604628
{
605629
// 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)
608632
if (val >= ((0x01 << _analogWriteBits) - 1))
609633
{
610634
val = fw; // Enable FORCE1
611635
}
612636
else
613637
{
614-
val <<= (16 - _analogWriteBits); // Shift over the value to fill available resolution
638+
val <<= (16 - _analogWriteBits); // Shift over the value to fill available resolution
615639
}
616-
640+
617641
return ap3_pwm_output(pin, val, fw, clk);
618642
}
619643

cores/arduino/ard_sup/ap3_analog_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ SOFTWARE.
2828

2929
enum EXTRA_ADC_PADS
3030
{
31-
AP3_ADC_DIFF0_PAD = 51, //More than physical pads on Apollo3
31+
AP3_ADC_DIFF0_PAD = 100, //More than physical pads on Apollo3
3232
AP3_ADC_DIFF1_PAD,
3333
AP3_ADC_TEMP_PAD,
3434
AP3_ADC_DIV3_PAD,

libraries/Examples/examples/Example4_analogRead/Example4_analogRead.ino

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
Feel like supporting open source hardware?
1515
Buy a board from SparkFun!
16-
SparkFun Edge: https://www.sparkfun.com/products/15170
16+
SparkFun Edge: https://www.sparkfun.com/artemis
1717
1818
Hardware Connections:
1919
Upload code
@@ -30,37 +30,37 @@ void setup()
3030

3131
pinMode(LED, OUTPUT);
3232

33-
//analogReadResolution(14); //Set resolution to 14 bit
33+
analogReadResolution(14); //Set resolution to 14 bit
3434
//analogReadResolution(16); //Set resolution to 16 bit - will pad ADC output with two zeros
3535
}
3636

3737
void loop()
3838
{
3939
digitalWrite(LED, LOW);
4040

41-
int myValue1 = analogRead(A1); //Automatically sets pin to analog input
42-
Serial.print("A1: ");
41+
int myValue1 = analogRead(A3); //Automatically sets pin to analog input
42+
Serial.print("A3: ");
4343
Serial.print(myValue1);
4444

45-
int internalTemp = analogRead(ADC_TEMP); //Read internal temp sensor. 3.8mV/C, +/-3C
46-
Serial.print("\tinternalTemp: ");
47-
Serial.print(internalTemp);
48-
49-
int vss = analogRead(ADC_VSS); //Read internal VSS
50-
Serial.print("\tvss: ");
51-
Serial.print(vss);
52-
5345
//TODO enable battload
54-
int div3 = analogRead(ADC_DIV3); //Read VCC across a 1/3 resistor divider
46+
int div3 = analogRead(ADC_INTERNAL_VCC_DIV3); //Read VCC across a 1/3 resistor divider
5547
Serial.print("\tVCC/3: ");
5648
Serial.print(div3);
5749

58-
float vcc = (float)div3 * 6 / 1024.0; //Convert 1/3 VCC to VCC
50+
float vcc = (float)div3 * 6 / 16384.0; //Convert 1/3 VCC to VCC
5951
Serial.print(" VCC: ");
6052
Serial.print(vcc, 2);
6153
Serial.print("V");
6254

63-
//pinMode(A4, OUTPUT); //Reset analog function to false.
55+
int internalTempVoltage = analogRead(ADC_INTERNAL_TEMP); //Read internal temp sensor. 3.8mV/C, +/-3C
56+
double internalTemp = internalTempVoltage * vcc / 16384.0; //Convert internal temp reading to voltage
57+
internalTemp /= 0.0038; //Convert voltage to degrees C
58+
Serial.print("\tinternalTemp: ");
59+
Serial.print(internalTemp, 2);
60+
61+
int vss = analogRead(ADC_INTERNAL_VSS); //Read internal VSS (should be 0)
62+
Serial.print("\tvss: ");
63+
Serial.print(vss);
6464

6565
Serial.println();
6666

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)