Skip to content

Commit c49ba18

Browse files
committed
ArduinoTestSuite updates from Paul Stoffregen
1 parent 4553cee commit c49ba18

File tree

10 files changed

+251
-105
lines changed

10 files changed

+251
-105
lines changed

libraries/ArduinoTestSuite/ArduinoTestSuite.cpp

+73-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//************************************************************************
22
//* Arduino Test Suite
33
//* (C) 2010 by Mark Sproul
4+
//* (C) 2011 by Matthew Murdoch
45
//* Open source as per standard Arduino code
56
//*
67
//* This library is free software; you can redistribute it and/or
@@ -15,6 +16,7 @@
1516
//************************************************************************
1617
//* Aug 31, 2010 <MLS> Started on TestArduino
1718
//* Oct 18, 2010 <MLS> Added memory testing
19+
//* Jun 10, 2011 <MEM> Added free list to memory usage calculation
1820
//************************************************************************
1921

2022
#include <avr/pgmspace.h>
@@ -26,11 +28,6 @@
2628
#include "ArduinoTestSuite.h"
2729

2830

29-
#include "Arduino.h"
30-
#include "HardwareSerial.h"
31-
#include "pins_arduino.h"
32-
33-
3431
#include "avr_cpunames.h"
3532

3633
#if defined(USART3_RX_vect)
@@ -58,6 +55,7 @@ enum
5855

5956
};
6057
unsigned long gTestStartTime;
58+
unsigned long gTestTotalElapsedTime;
6159
short gTagIndent;
6260
int gYotalErrors;
6361
int gTestCount;
@@ -176,9 +174,9 @@ char memoryMsg[48];
176174
gTestCount = 0;
177175

178176
Serial.begin(9600);
179-
delay(1000);
177+
delay(100);
180178

181-
gTestStartTime = millis();
179+
gTestTotalElapsedTime = 0;
182180

183181
Serial.println();
184182
Serial.println();
@@ -197,29 +195,40 @@ char memoryMsg[48];
197195

198196
randomSeed(analogRead(0));
199197

198+
gTestStartTime = micros();
200199
}
201200

202201
//************************************************************************
203202
void ATS_end()
204203
{
205-
long seconds;
206-
long milliSecs;
204+
unsigned long seconds;
205+
unsigned long microSecs;
206+
char buf[8];
207207

208+
gTestTotalElapsedTime += (micros() - gTestStartTime);
208209

209210
Serial_println_P(gTextMsg_dashLine);
210211

211212
// Ran 4 tests in 0.000s
212213
Serial.print("Ran ");
213214
Serial.print(gTestCount);
214215
Serial.print(" tests in ");
216+
217+
seconds = gTestTotalElapsedTime / 1000000;
218+
microSecs = gTestTotalElapsedTime % 1000000;
215219

216-
seconds = millis() / 1000;
217-
milliSecs = millis() % 1000;
218220
Serial.print(seconds);
219-
Serial.print('.');
220-
Serial.print(milliSecs);
221+
ultoa(microSecs + 1000000, buf, 10); // add forces leading zeros
222+
buf[0] = '.'; // replace leading '1' with decimal point
223+
Serial.print(buf);
221224
Serial.print('s');
222225
Serial.println();
226+
227+
int used = ATS_GetMaximumMemoryAllocated();
228+
if (used >= 0) {
229+
Serial.print("Maximum heap memory: ");
230+
Serial.println(used);
231+
}
223232
Serial.println();
224233

225234
if (gYotalErrors == 0)
@@ -245,6 +254,9 @@ void ATS_PrintTestStatus(char *testString, boolean passed)
245254
{
246255
int sLen;
247256

257+
// do not include time printing status in total test time
258+
gTestTotalElapsedTime += (micros() - gTestStartTime);
259+
248260
Serial.print(testString);
249261
sLen = strlen(testString);
250262
while (sLen < 60)
@@ -265,6 +277,9 @@ int sLen;
265277
Serial.println();
266278

267279
gTestCount++;
280+
281+
// begin counting total test time again
282+
gTestStartTime = micros();
268283
}
269284

270285

@@ -474,8 +489,15 @@ uint8_t helperpin;
474489

475490
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
476491
#define kAnalogPinOffset 54
492+
#define DIGITAL_ANAPIN(a) ((a) + kAnalogPinOffset)
493+
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
494+
#define kAnalogPinOffset 38
495+
#define DIGITAL_ANAPIN(a) ((a) + kAnalogPinOffset)
496+
#elif defined(__AVR_ATmega32U4__)
497+
#define DIGITAL_ANAPIN(a) ((a) < 11 ? 21 - (a) : 22)
477498
#else
478499
#define kAnalogPinOffset 14
500+
#define DIGITAL_ANAPIN(a) ((a) + kAnalogPinOffset)
479501
#endif
480502

481503

@@ -490,7 +512,7 @@ int analogValueLow;
490512

491513

492514
//* first we have to set the ANALOG pin to INPUT
493-
pinMode(analogPintoTest + kAnalogPinOffset, INPUT);
515+
pinMode(DIGITAL_ANAPIN(analogPintoTest), INPUT);
494516

495517
passedOK = true;
496518

@@ -532,15 +554,15 @@ boolean ATS_Test_AnalogInput(uint8_t analogPinToTest)
532554
boolean passedOK;
533555
uint8_t helperpin;
534556

535-
if ((analogPinToTest % 2) == 0)
557+
if ((DIGITAL_ANAPIN(analogPinToTest) % 2) == 0)
536558
{
537559
//* if its EVEN, add 1
538-
helperpin = kAnalogPinOffset + analogPinToTest + 1;
560+
helperpin = DIGITAL_ANAPIN(analogPinToTest) + 1;
539561
}
540562
else
541563
{
542564
//* if its ODD
543-
helperpin = kAnalogPinOffset + analogPinToTest - 1;
565+
helperpin = DIGITAL_ANAPIN(analogPinToTest) - 1;
544566
}
545567
passedOK = ATS_Test_AnalogInputWithHelper(analogPinToTest, helperpin);
546568
return(passedOK);
@@ -551,7 +573,7 @@ uint8_t helperpin;
551573
#define kSerialTestDelay 3
552574

553575

554-
#if (SERIAL_PORT_COUNT > 1) && !defined(__AVR_ATmega32U4__)
576+
#if (SERIAL_PORT_COUNT > 1)
555577
//************************************************************************
556578
//* retunrs 0 if no errors, 1 if an error occured
557579
short ATS_TestSerialLoopback(HardwareSerial *theSerialPort, char *serialPortName)
@@ -693,8 +715,32 @@ extern unsigned int __bss_start;
693715
extern unsigned int __bss_end;
694716
extern unsigned int __heap_start;
695717
extern void *__brkval;
718+
char *__brkval_maximum __attribute__((weak));
719+
720+
/*
721+
* The free list structure as maintained by the avr-libc memory allocation routines.
722+
*/
723+
struct __freelist {
724+
size_t sz;
725+
struct __freelist *nx;
726+
};
696727

728+
/* The head of the free list structure */
729+
extern struct __freelist *__flp;
697730

731+
/* Calculates the size of the free list */
732+
int ATS_FreeListSize()
733+
{
734+
struct __freelist* current;
735+
int total = 0;
736+
737+
for (current = __flp; current; current = current->nx) {
738+
total += 2; /* Add two bytes for the memory block's header */
739+
total += (int) current->sz;
740+
}
741+
742+
return total;
743+
}
698744

699745
//************************************************************************
700746
int ATS_GetFreeMemory()
@@ -703,13 +749,21 @@ int free_memory;
703749

704750
if((int)__brkval == 0)
705751
{
706-
free_memory = ((int)&free_memory) - ((int)&__bss_end);
752+
free_memory = ((int)&free_memory) - ((int)&__heap_start);
707753
}
708754
else
709755
{
710756
free_memory = ((int)&free_memory) - ((int)__brkval);
757+
free_memory += ATS_FreeListSize();
711758
}
712759
return free_memory;
713760
}
714761

762+
int ATS_GetMaximumMemoryAllocated()
763+
{
764+
if (__brkval_maximum) {
765+
return (int)__brkval_maximum - (int)&__heap_start;
766+
}
767+
return -1;
768+
}
715769

libraries/ArduinoTestSuite/ArduinoTestSuite.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
//* Aug 31, 2010 <MLS> Started on TestArduino
44
//************************************************************************
55

6-
#ifndef _AVR_IO_H_
7-
#include <avr/io.h>
8-
#endif
9-
10-
#ifndef Arduino_h
11-
#include "Arduino.h"
12-
#endif
13-
#ifndef HardwareSerial_h
14-
#include "HardwareSerial.h"
6+
#if defined(ARDUINO) && ARDUINO >= 100
7+
#include "Arduino.h"
8+
#include "pins_arduino.h"
9+
#else
10+
#include "WProgram.h"
11+
#include "pins_arduino.h"
1512
#endif
1613

1714

@@ -37,9 +34,12 @@ short ATS_TestSerialLoopback(HardwareSerial *theSerialPort, char *serialPortName
3734

3835

3936
int ATS_GetFreeMemory();
37+
int ATS_GetMaximumMemoryAllocated();
38+
4039

4140
//************************************************************************
4241
//* this has to be an inline function because calling subroutines affects free memory
42+
inline void ATS_ReportMemoryUsage(int _memoryUsageAtStart) __attribute__((always_inline, unused));
4343
inline void ATS_ReportMemoryUsage(int _memoryUsageAtStart)
4444
{
4545
int freeMemoryAtEnd;

libraries/ArduinoTestSuite/examples/ATS_Constants/ATS_Constants.pde

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//* Oct 16, 2010 <ROA> Test of Arduino Constants
88
//************************************************************************
99

10-
#include "HardwareSerial.h"
1110
#include <ArduinoTestSuite.h>
1211

1312
//************************************************************************
Original file line numberDiff line numberDiff line change
@@ -1 +1,101 @@
1-
//************************************************************************//* Arduino Test Suite//* ATS_ToneTest//* //* Copyright (c) 2010 Mark Sproul All right reserved.//* //* This library is free software; you can redistribute it and/or//* modify it under the terms of the GNU Lesser General Public//* License as published by the Free Software Foundation; either//* version 2.1 of the License, or (at your option) any later version.//* //* This library is distributed in the hope that it will be useful,//* but WITHOUT ANY WARRANTY; without even the implied warranty of//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU//* Lesser General Public License for more details.//* //* You should have received a copy of the GNU Lesser General Public//* License along with this library; if not, write to the Free Software//* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA//************************************************************************//* Aug 31, 2010 <MLS> Started on TestArduino//* Oct 28, 2010 <MLS> Started on Delay//************************************************************************#include "HardwareSerial.h"#include <ArduinoTestSuite.h>//************************************************************************void setup(){short ii;short testNum;int startMemoryUsage;unsigned long startMillis;unsigned long endMillis;unsigned long deltaMillis;unsigned long errMillis;boolean passed;char testNameString[80]; startMemoryUsage = ATS_GetFreeMemory(); ATS_begin("Arduino", "DelayTest"); testNum = 1; //* we start at 2 because 0/1 are RXD/TXD for (ii=0; ii<1000; ii+= 15) { startMillis = millis(); delay(ii); endMillis = millis(); deltaMillis = endMillis - startMillis; if (deltaMillis >= ii) { errMillis = deltaMillis - ii; } else { errMillis = ii - deltaMillis; } if (errMillis <= 1) { passed = true; } else { passed = false; } sprintf(testNameString, "DelayTest.%02d (delay= %4d actual delay=%ld err=%ld)", testNum, ii, deltaMillis, errMillis); ATS_PrintTestStatus(testNameString, passed); testNum++; } ATS_ReportMemoryUsage(startMemoryUsage); ATS_end();}//************************************************************************void loop(){}
1+
//************************************************************************
2+
//* Arduino Test Suite
3+
//* ATS_ToneTest
4+
//*
5+
//* Copyright (c) 2010 Mark Sproul All right reserved.
6+
//*
7+
//* This library is free software; you can redistribute it and/or
8+
//* modify it under the terms of the GNU Lesser General Public
9+
//* License as published by the Free Software Foundation; either
10+
//* version 2.1 of the License, or (at your option) any later version.
11+
//*
12+
//* This library is distributed in the hope that it will be useful,
13+
//* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
//* Lesser General Public License for more details.
16+
//*
17+
//* You should have received a copy of the GNU Lesser General Public
18+
//* License along with this library; if not, write to the Free Software
19+
//* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
//************************************************************************
21+
//* Aug 31, 2010 <MLS> Started on TestArduino
22+
//* Oct 28, 2010 <MLS> Started on Delay
23+
//************************************************************************
24+
25+
#include <ArduinoTestSuite.h>
26+
27+
//************************************************************************
28+
void setup()
29+
{
30+
short ii;
31+
short testNum;
32+
int startMemoryUsage;
33+
unsigned long startMillis;
34+
unsigned long endMillis;
35+
unsigned long deltaMillis;
36+
unsigned long errMillis;
37+
boolean passed;
38+
char testNameString[80];
39+
40+
41+
startMemoryUsage = ATS_GetFreeMemory();
42+
43+
ATS_begin("Arduino", "DelayTest");
44+
45+
testNum = 1;
46+
//* we start at 2 because 0/1 are RXD/TXD
47+
for (ii=0; ii<1000; ii+= 15)
48+
{
49+
startMillis = millis();
50+
51+
delay(ii);
52+
53+
endMillis = millis();
54+
55+
deltaMillis = endMillis - startMillis;
56+
57+
if (deltaMillis >= ii)
58+
{
59+
errMillis = deltaMillis - ii;
60+
}
61+
else
62+
{
63+
errMillis = ii - deltaMillis;
64+
}
65+
66+
if (errMillis <= 1)
67+
{
68+
passed = true;
69+
}
70+
else
71+
{
72+
passed = false;
73+
}
74+
sprintf(testNameString, "DelayTest.%02d (delay= %4d actual delay=%ld err=%ld)", testNum, ii, deltaMillis, errMillis);
75+
76+
ATS_PrintTestStatus(testNameString, passed);
77+
78+
79+
testNum++;
80+
}
81+
82+
83+
84+
85+
ATS_ReportMemoryUsage(startMemoryUsage);
86+
87+
ATS_end();
88+
89+
}
90+
91+
92+
//************************************************************************
93+
void loop()
94+
{
95+
96+
97+
}
98+
99+
100+
101+

libraries/ArduinoTestSuite/examples/ATS_General/ATS_General.pde

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@
88
//* Oct 18, 2010 <MLS> Added memory testing
99
//************************************************************************
1010

11-
#include "HardwareSerial.h"
12-
#include "pins_arduino.h"
1311
#include <ArduinoTestSuite.h>
14-
#include "avr_cpunames.h"
15-
1612

1713
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
1814
#define kBoard_PinCount 20
1915
#define kBoard_AnalogCount 6
2016
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
2117
#define kBoard_PinCount 70
2218
#define kBoard_AnalogCount 16
19+
20+
#elif defined(CORE_TEENSY)
21+
#define kBoard_PinCount CORE_NUM_TOTAL_PINS
22+
#define kBoard_AnalogCount CORE_NUM_ANALOG
23+
#define SERIAL_PORT_COUNT 2
24+
HardwareSerial Serial1 = HardwareSerial();
2325
#endif
2426

2527

0 commit comments

Comments
 (0)