Skip to content

Commit 27b2f5f

Browse files
author
Owen
authored
Merge pull request #251 from sparkfun/sync-release-candidate
2 parents f92ce2d + 3bdee38 commit 27b2f5f

File tree

5 files changed

+256
-4
lines changed

5 files changed

+256
-4
lines changed

Diff for: cores/arduino/am_sdk_ap3/mcu/apollo3/hal/am_hal_uart.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ am_hal_uart_transfer_t;
172172

173173
// Flow control
174174
#define AM_HAL_UART_FLOW_CTRL_NONE 0
175-
#define AM_HAL_UART_FLOW_CTRL_CTS_ONLY UART_CR_CTSEN_Msk
176-
#define AM_HAL_UART_FLOW_CTRL_RTS_ONLY UART_CR_RTSEN_Msk
175+
#define AM_HAL_UART_FLOW_CTRL_CTS_ONLY UART0_CR_CTSEN_Msk
176+
#define AM_HAL_UART_FLOW_CTRL_RTS_ONLY UART0_CR_RTSEN_Msk
177177
#define AM_HAL_UART_FLOW_CTRL_RTS_CTS (UART0_CR_CTSEN_Msk | \
178178
UART0_CR_RTSEN_Msk)
179179
// FIFO enable/disable.

Diff for: cores/arduino/ard_sup/ard_supers/HardwareSerial.h

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class HardwareSerial : public Stream
6868
virtual int read(void) = 0;
6969
virtual void flush(void) = 0;
7070
virtual size_t write(uint8_t) = 0;
71+
using Print::write; // pull in write(str) and write(buf, size) from Print
7172
virtual operator bool() = 0;
7273
};
7374

Diff for: cores/arduino/ard_sup/uart/ap3_uart.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,22 @@ ap3_err_t Uart::set_config(HardwareSerial_Config_e HWSconfig)
296296
retval = AP3_INVALID_ARG;
297297
break;
298298
}
299+
300+
//Setup flow control
301+
_config.ui32FlowControl = AM_HAL_UART_FLOW_CTRL_NONE;
302+
if(_pinRTS != AP3_UART_PIN_UNUSED && _pinCTS != AP3_UART_PIN_UNUSED)
303+
{
304+
_config.ui32FlowControl = AM_HAL_UART_FLOW_CTRL_RTS_CTS;
305+
}
306+
else if(_pinRTS != AP3_UART_PIN_UNUSED)
307+
{
308+
_config.ui32FlowControl = AM_HAL_UART_FLOW_CTRL_RTS_ONLY;
309+
}
310+
else if(_pinCTS != AP3_UART_PIN_UNUSED)
311+
{
312+
_config.ui32FlowControl = AM_HAL_UART_FLOW_CTRL_CTS_ONLY;
313+
}
314+
299315
return retval;
300316
}
301317

@@ -375,7 +391,7 @@ ap3_err_t Uart::_begin(void)
375391

376392
if (_pinRTS != AP3_UART_PIN_UNUSED)
377393
{
378-
retval = ap3_uart_pad_funcsel(_instance, AP3_UART_TX, ap3_gpio_pin2pad(_pinRTS), &funcsel);
394+
retval = ap3_uart_pad_funcsel(_instance, AP3_UART_RTS, ap3_gpio_pin2pad(_pinRTS), &funcsel);
379395
if (retval != AP3_OK)
380396
{
381397
return retval;
@@ -391,7 +407,7 @@ ap3_err_t Uart::_begin(void)
391407

392408
if (_pinCTS != AP3_UART_PIN_UNUSED)
393409
{
394-
retval = ap3_uart_pad_funcsel(_instance, AP3_UART_RX, ap3_gpio_pin2pad(_pinCTS), &funcsel);
410+
retval = ap3_uart_pad_funcsel(_instance, AP3_UART_CTS, ap3_gpio_pin2pad(_pinCTS), &funcsel);
395411
if (retval != AP3_OK)
396412
{
397413
return retval;

Diff for: keywords.txt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#######################################
2+
# Syntax Coloring Map For Wire
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
#######################################
10+
# Methods and Functions (KEYWORD2)
11+
#######################################
12+
13+
enableBurstMode KEYWORD2
14+
disableBurstMode KEYWORD2
15+
getCpuFreqMHz KEYWORD2
16+
17+
getInternalTemp KEYWORD2
18+
19+
analogWriteResolution KEYWORD2
20+
analogWriteFrameWidth KEYWORD2
21+
analogWriteFrequency KEYWORD2
22+
servoWrite KEYWORD2
23+
24+
enableFastShift KEYWORD2
25+
fastShiftOut KEYWORD2
26+
fastShiftIn KEYWORD2
27+
28+
secs KEYWORD2
29+
systicks KEYWORD2
30+
sysoverflows KEYWORD2
31+
32+
#######################################
33+
# Instances (KEYWORD2)
34+
#######################################
35+
36+
#######################################
37+
# Constants (LITERAL1)
38+
#######################################
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/* Author: Stephen Fordyce (adapted from WDT example code by Adam Garbo - https://forum.sparkfun.com/viewtopic.php?f=169&t=52431&p=213296&hilit=watchdog#p213296)
2+
Created: 24th July 2020
3+
License: MIT. See SparkFun Arduino Apollo3 Project for more information
4+
5+
This example demonstrates use of the watchdog timer, also different ways of resetting the Artemis as well as how to determine the last reset type
6+
*/
7+
8+
// Global variables
9+
volatile uint8_t watchdogCounter = 0; // Watchdog interrupt counter
10+
uint32_t resetStatus = 0; // Reset status register
11+
bool testWatchdogResetFlag = false;
12+
bool watchdogInterruptCallsEmergencyReset = false;
13+
bool resetWDTEveryLoop = true;
14+
15+
// Watchdog timer configuration structure.
16+
am_hal_wdt_config_t g_sWatchdogConfig = {
17+
//Substitude other values for AM_HAL_WDT_LFRC_CLK_16HZ to increase/decrease the range
18+
.ui32Config = AM_HAL_WDT_LFRC_CLK_16HZ | AM_HAL_WDT_ENABLE_RESET | AM_HAL_WDT_ENABLE_INTERRUPT, // Configuration values for generated watchdog timer event.
19+
//****** EVEN THOUGH THESE IMPLY 16-BIT, THEY ARE ONLY 8-BIT - 255 MAX!
20+
.ui16InterruptCount = 120, //MAX 255! // Set WDT interrupt timeout for 5 seconds (120 / 16 = 8). // Number of watchdog timer ticks allowed before a watchdog interrupt event is generated.
21+
.ui16ResetCount = 160 //MAX 255! // Set WDT reset timeout for 15 seconds (160 / 16 = 10). // Number of watchdog timer ticks allowed before the watchdog will issue a system reset.
22+
};
23+
24+
void setup(void)
25+
{
26+
pinMode(LED_BUILTIN, OUTPUT);
27+
Serial.begin(115200); delay(10);
28+
Serial.println();Serial.println();
29+
Serial.println("****Artemis Reset & Watchdog Reset Example****");
30+
31+
printResetReason(); //Explain reason for reset
32+
am_hal_reset_control(AM_HAL_RESET_CONTROL_STATUSCLEAR, 0); // Clear reset status register for next time we reset.
33+
delay(1000);
34+
35+
startWatchdogTimer(); //Initialise and start the watchdog timer, in case the program gets stuck in a while loop or waiting for user input
36+
printWatchdogDetails();
37+
delay(2000);
38+
39+
Serial.println("\nRunning a time-consuming loop with watchdog running");
40+
for(int i=0; i<5; i++)
41+
{
42+
delay(1000); //Phew, a bunch of hard processing
43+
printWatchdogValue();
44+
}
45+
Serial.println("Yikes, the time kept increasing, even though we were doing legitimate work\n");
46+
47+
Serial.println("\nRunning a time-consuming loop that restarts the watchdog timer each loop");
48+
for(int i=0; i<5; i++)
49+
{
50+
delay(1000); //Phew, a bunch of hard processing
51+
printWatchdogValue();
52+
am_hal_wdt_restart(); // "Pet" the dog. // Restart the watchdog.
53+
}
54+
Serial.println("Plenty of time, but the watchdog timer will still catch a freeze\n");
55+
delay(2000);
56+
}
57+
58+
void loop(void)
59+
{
60+
Serial.println("Start of loop()");
61+
if(resetWDTEveryLoop)
62+
am_hal_wdt_restart(); // "Pet" the dog. // Restart the watchdog. (every loop run seems good)
63+
64+
if(resetWDTEveryLoop)
65+
{
66+
Serial.println("Enter 'r' to do a scheduled software reset");
67+
Serial.println("Enter 's' to stop resetting the watchdog timer each loop");
68+
delay(1000);
69+
char option = Serial.read();
70+
if(option == 'r')
71+
myScheduledReset();
72+
if(option == 's')
73+
{
74+
Serial.println("I take no responsibility for the consequences!");
75+
Serial.println("(the watchdog interrupt routine when triggered will reset the timer 3 times, watch the timer values to see when it's reset by the ISR)");
76+
delay(2000);
77+
resetWDTEveryLoop = false;
78+
}
79+
}
80+
81+
if(resetWDTEveryLoop == false)
82+
{
83+
Serial.println("Enter 'e' to let the watchdog interrupt call emergencyReset()");
84+
delay(1000);
85+
char option = Serial.read();
86+
if(option == 'e')
87+
{
88+
Serial.println("Cool, that'll happen in a sec"); delay(20);
89+
watchdogInterruptCallsEmergencyReset = true;
90+
}
91+
}
92+
93+
if(testWatchdogResetFlag)
94+
{
95+
Serial.println("Just about to go down the rabbit hole...");
96+
delay(20); //Let serial buffer clear
97+
while(1); //Intentionally get stuck, and hope that the watchdog timer saves the day
98+
}
99+
100+
printWatchdogValue();
101+
Serial.println();
102+
delay(10);
103+
104+
//Optional code for sleeping - just halt the WDT before you disable everything, then re-initialise the WDT after waking up. Tested elsewhere, and does not have any appreciable impact on sleep currents or performance
105+
//am_hal_wdt_halt();
106+
//sleepForABit();
107+
//startWatchdog();
108+
}
109+
110+
// Interrupt handler for the watchdog.
111+
extern "C" void am_watchdog_isr(void)
112+
{
113+
am_hal_wdt_int_clear(); // Clear the watchdog interrupt.
114+
if(watchdogInterruptCallsEmergencyReset)
115+
emergencyReset();
116+
117+
if ( watchdogCounter < 3 ) // Catch the first three watchdog interrupts, but let the fourth through untouched.
118+
{
119+
digitalWrite(LED_BUILTIN, LOW);
120+
am_hal_wdt_restart(); // "Pet" the dog (reset the timer)
121+
}
122+
else
123+
{
124+
digitalWrite(LED_BUILTIN, HIGH); // Indicator that a reset will occur.
125+
testWatchdogResetFlag = true;
126+
}
127+
128+
watchdogCounter++; // Increment the number of watchdog interrupts.
129+
}
130+
131+
void startWatchdogTimer()
132+
{
133+
am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_LFRC_START, 0); // LFRC must be turned on for this example as the watchdog only runs off of the LFRC.
134+
am_hal_wdt_init(&g_sWatchdogConfig); // Configure the watchdog.
135+
NVIC_EnableIRQ(WDT_IRQn); // Enable the interrupt for the watchdog in the NVIC.
136+
am_hal_interrupt_master_enable();
137+
am_hal_wdt_start(); // Enable the watchdog.
138+
}
139+
140+
//IMPORTANT: IF THIS IS CALLED BY THE WATCHDOG TIMER INTERRUPT ROUTINE, IT NEEDS TO BE QUICK (NO PRINTING TO SERIAL OR DELAYS)
141+
void emergencyReset()
142+
{
143+
//Optional: write some bits to flash/EEPROM to help with recovery
144+
145+
am_hal_reset_control(AM_HAL_RESET_CONTROL_SWPOI,0); //Reset with option "Software Power On Initialization" SWPOI
146+
// am_hal_reset_control(AM_HAL_RESET_CONTROL_SWPOR,0); //Reset with option SWPOR (same as SWPOI but different am_hal_reset_status bit)
147+
}
148+
149+
void myScheduledReset()
150+
{
151+
//Optional: write some bits to flash/EEPROM to help with recovery
152+
153+
// am_hal_reset_control(AM_HAL_RESET_CONTROL_SWPOI,0); //Reset with option "Software Power On Initialization" SWPOI
154+
am_hal_reset_control(AM_HAL_RESET_CONTROL_SWPOR,0); //Reset with option SWPOR (same as SWPOI but different am_hal_reset_status bit)
155+
}
156+
157+
void printWatchdogDetails()
158+
{
159+
Serial.print("Interrupt Count = "); Serial.print(g_sWatchdogConfig.ui16InterruptCount ); Serial.println(" ticks");
160+
Serial.print("Reset Count = "); Serial.print(g_sWatchdogConfig.ui16ResetCount); Serial.println(" ticks");
161+
162+
// Print out reset status register.
163+
am_hal_reset_status_t sStatus; // (Note: See am_hal_reset.h for RESET status structure)
164+
am_hal_reset_status_get(&sStatus);
165+
resetStatus = sStatus.eStatus;
166+
char rStatus[30];
167+
sprintf(rStatus, "Reset Status Register = 0x%x", resetStatus); // (Note: Watch Dog Timer reset = 0x40)
168+
Serial.println(rStatus);
169+
}
170+
171+
void printWatchdogValue()
172+
{
173+
Serial.print("Watchdog timer:");
174+
Serial.println(am_hal_wdt_counter_get());
175+
}
176+
177+
void printResetReason()
178+
{
179+
am_hal_reset_status_t resetResult;
180+
uint32_t resultOk = am_hal_reset_status_get(&resetResult);
181+
Serial.print("Reset reason: ");
182+
if(resultOk == AM_HAL_STATUS_FAIL) Serial.println("Failed to get reset status");
183+
if(resetResult.bEXTStat) Serial.println("External reset");
184+
if(resetResult.bPORStat) Serial.println("Power-On reset");
185+
if(resetResult.bBODStat) Serial.println("Brown-Out reset");
186+
if(resetResult.bSWPORStat) Serial.println("SW Power-On reset or AIRCR reset - in this example, indicates reset by myScheduledReset()");
187+
if(resetResult.bSWPOIStat) Serial.println("SW Power On Initialization reset - in this example, indicates reset by emergencyReset()");
188+
if(resetResult.bDBGRStat) Serial.println("Debugger reset");
189+
if(resetResult.bWDTStat) Serial.println("Watch Dog Timer reset - nothing in the Watchdog Timer Interrupt routine got to restarting the Watchdog Timer");
190+
if(resetResult.bBOUnregStat) Serial.println("Unregulated Supply Brownout event");
191+
if(resetResult.bBOCOREStat) Serial.println("Core Regulator Brownout event");
192+
if(resetResult.bBOMEMStat) Serial.println("Memory Regulator Brownout event");
193+
if(resetResult.bBOBLEStat) Serial.println("BLE/Burst Regulator Brownout event");
194+
// am_hal_reset_control(AM_HAL_RESET_CONTROL_STATUSCLEAR, 0); // (do this in setup() Clear reset status register for next time we reset.
195+
Serial.println();
196+
}

0 commit comments

Comments
 (0)