Skip to content

Commit 64efd23

Browse files
committed
add more examples
plus fixes some things
1 parent 7fe32a2 commit 64efd23

File tree

11 files changed

+210
-28
lines changed

11 files changed

+210
-28
lines changed

cores/arduino/sdk/core-extend/AnalogInternal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ int analogReadTemp( void );
3333
int analogReadVCCDiv3( void );
3434
int analogReadVSS( void );
3535

36+
float getTempDegF( void );
37+
float getVCCV( void );
38+
3639
uint32_t initializeADC( void );
3740

3841
#endif // _ARDUINO_SDK_CORE_EXTEND_ANALOG_INTERNAL_H_

cores/arduino/sdk/core-implement/CommonAnalog.cpp

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
#define AP3_ANALOG_FRAME_PERIOD (24000)
1818
#define AP3_MAX_ANALOG_WRITE_WIDTH (0x0000FFFF)
1919
#define AP3_MIN_ANALOG_WRITE_WIDTH (0x03)
20-
#define AP3_MAX_PWM_RESOLUTION (16)
20+
#define AP3_ANALOG_WRITE_RESOLUTION_MAX (16)
21+
#define AP3_ANALOG_WRITE_RESOLUTION_MIN (1)
2122
#define AP3_ANALOG_READ_RESOLUTION_MAX (16)
23+
#define AP3_ANALOG_READ_RESOLUTION_MIN (1)
2224
#define AP3_ADC_RESOLUTION (14)
2325

2426
int ap3_analog_read(ap3_adc_channel_config_t* config);
@@ -50,6 +52,32 @@ ap3_adc_channel_config_t ap3_adc_channel_configs[] = {
5052
{ 0, AP3_ADC_INTERNAL_CHANNELS_VSS, AM_HAL_ADC_SLOT_CHSEL_VSS, 0, },
5153
};
5254

55+
float getTempDegF( void ) {
56+
const float v_ref = 2.0;
57+
uint16_t counts = analogReadTemp();
58+
59+
//
60+
// Convert and scale the temperature.
61+
// Temperatures are in Fahrenheit range -40 to 225 degrees.
62+
// Voltage range is 0.825V to 1.283V
63+
// First get the ADC voltage corresponding to temperature.
64+
//
65+
float volts = ((float)counts) * v_ref / ((float)(pow(2, _analogReadResolution)));
66+
67+
float fVT[3];
68+
fVT[0] = volts;
69+
fVT[1] = 0.0f;
70+
fVT[2] = -123.456;
71+
uint32_t ui32Retval = am_hal_adc_control(g_ADCHandle, AM_HAL_ADC_REQ_TEMP_CELSIUS_GET, fVT);
72+
MBED_ASSERT(ui32Retval == AM_HAL_STATUS_SUCCESS);
73+
74+
return fVT[1]; // Get the temperature
75+
}
76+
77+
float getVCCV( void ){
78+
return ((float)analogReadVCCDiv3() * 6.0) / 16384.0;
79+
}
80+
5381
int indexAnalogRead(pin_size_t index){
5482
// todo: replace with mbed "AnalogIn" functionality
5583
pin_size_t pinNumber = pinNumberByIndex(index);
@@ -157,8 +185,12 @@ void indexTone(pin_size_t index, unsigned int frequency, unsigned long duration)
157185
}
158186

159187
ap3_err_t analogWriteResolution(uint8_t bits){
160-
if (bits > AP3_MAX_PWM_RESOLUTION){
161-
_analogWriteResolution = AP3_MAX_PWM_RESOLUTION;
188+
if (bits > AP3_ANALOG_WRITE_RESOLUTION_MAX){
189+
_analogWriteResolution = AP3_ANALOG_WRITE_RESOLUTION_MAX;
190+
return AP3_ERR;
191+
}
192+
if (bits < AP3_ANALOG_WRITE_RESOLUTION_MIN){
193+
_analogWriteResolution = AP3_ANALOG_WRITE_RESOLUTION_MIN;
162194
return AP3_ERR;
163195
}
164196
_analogWriteResolution = bits;
@@ -187,8 +219,12 @@ ap3_err_t analogWriteFrequency(float freq){
187219
}
188220

189221
ap3_err_t servoWriteResolution(uint8_t bits){
190-
if (bits > AP3_MAX_PWM_RESOLUTION){
191-
_servoWriteResolution = AP3_MAX_PWM_RESOLUTION;
222+
if (bits > AP3_ANALOG_WRITE_RESOLUTION_MAX){
223+
_servoWriteResolution = AP3_ANALOG_WRITE_RESOLUTION_MAX;
224+
return AP3_ERR;
225+
}
226+
if (bits < AP3_ANALOG_WRITE_RESOLUTION_MIN){
227+
_servoWriteResolution = AP3_ANALOG_WRITE_RESOLUTION_MIN;
192228
return AP3_ERR;
193229
}
194230
_servoWriteResolution = bits;
@@ -224,6 +260,10 @@ ap3_err_t analogReadResolution(uint8_t bits){
224260
_analogReadResolution = AP3_ANALOG_READ_RESOLUTION_MAX;
225261
return AP3_ERR;
226262
}
263+
if(bits < AP3_ANALOG_READ_RESOLUTION_MIN){
264+
_analogReadResolution = AP3_ANALOG_READ_RESOLUTION_MIN;
265+
return AP3_ERR;
266+
}
227267
_analogReadResolution = bits;
228268
return AP3_OK;
229269
}
@@ -306,12 +346,12 @@ uint32_t powerControlADC(bool on){
306346
uint32_t status = AM_HAL_STATUS_SUCCESS;
307347

308348
if(on){
309-
status = am_hal_adc_power_control(g_ADCHandle, AM_HAL_SYSCTRL_WAKE, false);
310-
if(status != AM_HAL_STATUS_SUCCESS){ return status; }
311-
312349
status = am_hal_adc_initialize(0, &g_ADCHandle);
313350
if(status != AM_HAL_STATUS_SUCCESS){ return status; }
314351

352+
status = am_hal_adc_power_control(g_ADCHandle, AM_HAL_SYSCTRL_WAKE, false);
353+
if(status != AM_HAL_STATUS_SUCCESS){ return status; }
354+
315355
adc_initialized = true;
316356
}else{
317357
adc_initialized = false;
@@ -395,21 +435,15 @@ int ap3_analog_read(ap3_adc_channel_config_t* config){
395435

396436
// Clear the ADC interrupt.
397437
am_hal_adc_interrupt_status(g_ADCHandle, &ui32IntMask, false);
398-
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_interrupt_clear(g_ADCHandle, ui32IntMask)){
399-
return 0; //Error
400-
}
438+
MBED_ASSERT(AM_HAL_STATUS_SUCCESS == am_hal_adc_interrupt_clear(g_ADCHandle, ui32IntMask));
401439

402440
am_hal_adc_sw_trigger(g_ADCHandle);
403441

404442
do { // Wait for interrupt
405-
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_interrupt_status(g_ADCHandle, &ui32IntMask, false)){
406-
return 0; //Error
407-
}
443+
MBED_ASSERT(AM_HAL_STATUS_SUCCESS == am_hal_adc_interrupt_status(g_ADCHandle, &ui32IntMask, false));
408444
} while(!(ui32IntMask & AM_HAL_ADC_INT_CNVCMP));
409445

410-
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_samples_read(g_ADCHandle, false, NULL, &ui32NumSamples, &Sample)){
411-
return 0; //Error
412-
}
446+
MBED_ASSERT(AM_HAL_STATUS_SUCCESS == am_hal_adc_samples_read(g_ADCHandle, false, NULL, &ui32NumSamples, &Sample));
413447

414448
uint32_t result = Sample.ui32Sample;
415449

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
// This file is subject to the terms and conditions defined in
3+
// file 'LICENSE.md', which is part of this source code package.
4+
*/
5+
6+
/*
7+
8+
The Apollo3 microcontroller includes an onboard ADC (analog to digital converter)
9+
It is able to read analog voltages from 0V to 2V on any of the following 10 pads:
10+
11, 12, 13, 16, 29, 31, 32, 33, 34, 35
11+
12+
(these Apollo3 pads will map to various pin numbers depending on which board is in use)
13+
14+
The Apollo3 also has some internal ADC channels that allow you to measure:
15+
- differential pairs
16+
- the internal die temperature
17+
- the internal VCC voltage
18+
- the internal VSS voltage
19+
20+
This sketch demonstrates usage of analogRead by fading the built-in LED to match the voltage
21+
read in on one of the analog pins. Additionally some of the internal ADC connections are used
22+
to measure the die temperature and VCC levels.
23+
24+
*/
25+
26+
#define RESOLUTION_BITS (16) // choose resolution (explained in depth below)
27+
28+
#ifdef ADCPIN
29+
#define EXTERNAL_ADC_PIN ADCPIN // ADCPIN is the lowest analog capable pin exposed on the variant
30+
#endif // - if no exposed pins are analog capable this will be undefined
31+
// - to use another pin provide an analog capable pin number such as:
32+
// - A0 -> A9 (when analog pins are named sequentially from 0)
33+
// - A11 -> A13, A16, A29, A31 -> A35 (when pins are named after Apollo3 pads)
34+
// - A variant-specific pin number (when none of the above apply)
35+
36+
void setup() {
37+
Serial.begin(115200);
38+
Serial.println("Apollo3 - analogRead");
39+
40+
analogReadResolution(RESOLUTION_BITS); // set the resolution of analogRead results
41+
// - maximum: 16 bits (padded with trailing zeroes)
42+
// - ADC: 14 bits (maximum ADC resolution)
43+
// - default: 10 bits (standard Arduino setting)
44+
// - minimum: 1 bit
45+
46+
analogWriteResolution(RESOLUTION_BITS); // match resolution for analogWrite
47+
}
48+
49+
void loop() {
50+
#ifdef ADCPIN
51+
int external = analogRead(EXTERNAL_ADC_PIN); // reads the analog voltage on the selected analog pin
52+
Serial.printf("external (counts): %d, ", external);
53+
analogWrite(LED_BUILTIN, external);
54+
#endif
55+
56+
int vcc_3 = analogReadVCCDiv3(); // reads VCC across a 1/3 voltage divider
57+
int vss = analogReadVSS(); // ideally 0
58+
int temp_raw = analogReadTemp(); // raw ADC counts from die temperature sensor
59+
60+
float temp_f = getTempDegF(); // computed die temperature in deg F
61+
float vcc_v = getVCCV(); // computed supply voltage in V
62+
63+
Serial.printf("temp (counts): %d, vcc/3 (counts): %d, vss (counts): %d, time (ms) %d\n", temp_raw, vcc_3, vss, millis());
64+
}

libraries/Apollo3/examples/AnalogWrite/AnalogWrite.ino

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ void setup() {
4343
analogWriteResolution(16); // new range is 0 to 65535
4444
analogWrite(LED_BUILTIN, 32767); // 50% duty cycle w/ 16-bit resolution
4545
// note: changing resolution does not
46-
// affect calls to analogWrite
47-
analogWriteResolution(8);
46+
// affect previous calls to analogWrite
47+
48+
analogWriteResolution(8); // sets the resolution of analogWrite output
49+
// - maximum: 16 bits
50+
// - default: 8 bits
51+
// - minimum: 1 bit
4852
}
4953

5054
void loop() {
55+
// the goal is to make the built-in LED "breathe" by fading up / down
56+
5157
static uint8_t val = 0;
5258
static int dir = 1;
5359

libraries/Apollo3/examples/DetachInterrupt/DetachInterrupt.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void hearbeat( void ){
4545

4646
void setup()
4747
{
48+
Serial.begin(115200);
4849
printf("Apollo3 - detachInterrupt\n\n");
4950

5051
pinMode(LED_BUILTIN, OUTPUT);
@@ -71,4 +72,4 @@ void loop()
7172
detachInterrupt(INT_PIN);
7273
}
7374
}
74-
}
75+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
// This file is subject to the terms and conditions defined in
3+
// file 'LICENSE.md', which is part of this source code package.
4+
*/
5+
6+
/*
7+
8+
RTOS stands for Real Time Operating System, another commonly used term is "scheduler"
9+
thanks to the fact that the system will schedule different tasks that need to be
10+
completed.
11+
12+
When one task no longer requires processing time (for example in sleep or delay) the
13+
RTOS will check to see if any other threads need to be handled.
14+
https://os.mbed.com/docs/mbed-os/v6.2/apis/scheduling-concepts.html
15+
16+
An RTOS can help to make groups of logical operations. In this example the main thread
17+
is used to blink the onboard LED and another thread is used to print status messages
18+
over Serial.
19+
20+
This sketch, written without an RTOS, would be growing into a nest of coniditonal
21+
statements all written in one main loop. Instead
22+
23+
*/
24+
25+
#define STATUS_COUNT_TOTAL (10)
26+
27+
rtos::Thread thread; // create a Thread object (from the rtos namespace)
28+
// https://os.mbed.com/docs/mbed-os/v6.2/apis/thread.html
29+
30+
void thread_fn( void ){ // create a function to run as a thread
31+
Serial.begin(115200); // perform setup related to this thread
32+
Serial.print("Apollo3 - Threads\n\n");
33+
34+
uint8_t status_count = 0;
35+
while(status_count < STATUS_COUNT_TOTAL){
36+
rtos::ThisThread::sleep_for(1000); // equivalent to calling "delay()"
37+
Serial.printf("time (ms): %d, status count: %d\n", millis(), status_count++);
38+
}
39+
40+
Serial.print("Stopping status thread\n");
41+
Serial.end(); // de-initialiaze for this thread
42+
}
43+
44+
// setup() and loop() run within the function main() which is the first thread
45+
void setup() {
46+
pinMode(LED_BUILTIN, OUTPUT);
47+
thread.start(thread_fn); // assign thread_fn to the object thread and begin execution
48+
}
49+
50+
void loop() {
51+
digitalWrite(LED_BUILTIN, HIGH);
52+
delay(200);
53+
digitalWrite(LED_BUILTIN, LOW);
54+
delay(200);
55+
}

libraries/SPI/src/SPI.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55

66
#include "SPI.h"
77

8-
arduino::MbedSPI::MbedSPI(int miso, int mosi, int sck) : _miso(miso), _mosi(mosi), _sck(sck) {
8+
arduino::MbedSPI::MbedSPI(pin_size_t miso, pin_size_t mosi, pin_size_t sck) {
9+
_miso = pinNameByNumber(miso);
10+
_mosi = pinNameByNumber(mosi);
11+
_sck = pinNameByNumber(sck);
12+
}
13+
14+
arduino::MbedSPI::MbedSPI(PinName miso, PinName mosi, PinName sck) : _miso(miso), _mosi(mosi), _sck(sck) {
915
}
1016

1117
uint8_t arduino::MbedSPI::transfer(uint8_t data) {
@@ -42,6 +48,9 @@ void arduino::MbedSPI::notUsingInterrupt(int interruptNumber) {
4248
}
4349

4450
void arduino::MbedSPI::beginTransaction(SPISettings settings) {
51+
if(!dev){
52+
begin();
53+
}
4554
if (settings != this->settings) {
4655
dev->format(8, settings.getDataMode());
4756
dev->frequency(settings.getClockFreq());

libraries/SPI/src/SPI.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ namespace arduino {
1515
class MbedSPI : public SPIClass
1616
{
1717
public:
18-
MbedSPI(int miso, int mosi, int sck);
18+
MbedSPI(pin_size_t miso, pin_size_t mosi, pin_size_t sck);
19+
MbedSPI(PinName miso, PinName mosi, PinName sck);
1920
virtual uint8_t transfer(uint8_t data);
2021
virtual uint16_t transfer16(uint16_t data);
2122
virtual void transfer(void *buf, size_t count);
@@ -36,9 +37,9 @@ class MbedSPI : public SPIClass
3637
private:
3738
SPISettings settings = SPISettings(0, MSBFIRST, SPI_MODE0);
3839
mbed::SPI* dev;
39-
int _miso;
40-
int _mosi;
41-
int _sck;
40+
PinName _miso;
41+
PinName _mosi;
42+
PinName _sck;
4243
};
4344

4445
}

platform.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ compiler.preproc.flags={compiler.cxx.flags} -w -x c++ -E -CC {compiler.preproc.e
4646
compiler.asm.flags=-include {build.variant.path}/mbed/mbed_config.h -iprefix{runtime.platform.path}/cores/ @{build.variant.path}/mbed/.asm-flags {compiler.asm.extra_flags}
4747
compiler.c.flags=-include {build.variant.path}/mbed/mbed_config.h -iprefix{runtime.platform.path}/cores/ @{build.variant.path}/mbed/.c-flags {compiler.c.extra_flags}
4848
compiler.cxx.flags=-include {build.variant.path}/mbed/mbed_config.h -iprefix{runtime.platform.path}/cores/ @{build.variant.path}/mbed/.cxx-flags {compiler.cxx.extra_flags}
49-
compiler.ld.flags=@{build.variant.path}/mbed/.ld-flags {compiler.ld.extra_flags} --specs=nosys.specs -lsupc++ -lstdc++
49+
compiler.ld.flags=@{build.variant.path}/mbed/.ld-flags {compiler.ld.extra_flags} --specs=nano.specs -lsupc++ -lstdc++ -lm
5050
compiler.ar.flags=rcsP {compiler.ar.extra_flags} {compiler.ar.extra_flags}
5151
compiler.axf2bin.flags={compiler.axf2bin.extra_flags} {compiler.axf2bin.extra_flags} -O binary
5252
compiler.axf2hex.flags={compiler.axf2hex.extra_flags} {compiler.axf2hex.extra_flags} -O ihex
@@ -79,6 +79,7 @@ recipe.ar.pattern="{compiler.ar.cmd}" {compiler.ar.flags} "{archive_file_path}"
7979
recipe.c.combine.pattern="{compiler.gcc.cmd}" "-T{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" -o "{build.path}/{build.project_name}.axf" {object_files} {libs.all} {compiler.ld.flags} {defines.ld}
8080
recipe.objcopy.bin.pattern="{compiler.objcopy.cmd}" {compiler.axf2bin.flags} "{build.path}/{build.project_name}.axf" "{build.path}/{build.project_name}.bin"
8181
recipe.objcopy.hex.pattern="{compiler.objcopy.cmd}" {compiler.axf2hex.flags} "{build.path}/{build.project_name}.axf" "{build.path}/{build.project_name}.hex"
82+
recipe.objcopy.hex.pattern.windows=
8283
recipe.size.pattern="{compiler.size.cmd}" -A "{build.path}/{build.project_name}.axf"
8384
recipe.size.regex=\.text\s+([0-9]+).*
8485
recipe.size.regex.data=^(?:\.data|\.bss)\s+([0-9]+).*

0 commit comments

Comments
 (0)