This package contains a simple test of tests for various elements of the SmartBoard hardware, which is a simple baseboard designed for easy embedding. It is able to run both a semi-automatic test suite as well as allow interactive testing.

Dependencies:   EthernetNetIf NTPClient_NetServices mbed

This program is most of what you need to test your SmartBoard baseboard hardware. It provides a means to test the following:

  • Two channels of CAN (with a loopback cable)
  • RS-232 Ports
  • Analog inputs
  • PWM outputs
  • Ethernet port
  • Real time clock
  • micro SD
  • USB Host port
Committer:
WiredHome
Date:
Mon Jan 24 00:41:01 2011 +0000
Revision:
1:586392c0e935
Parent:
0:5db287f0060b
Child:
2:02e7d896824f
v0.09

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:5db287f0060b 1 /// @file SmartBoard_Tester.cpp is the simple test framework
WiredHome 0:5db287f0060b 2 ///
WiredHome 0:5db287f0060b 3 /// This file contains the startup, interactive, and test code
WiredHome 0:5db287f0060b 4 /// to evaluate the SmartBoard baseboard.
WiredHome 0:5db287f0060b 5 ///
WiredHome 0:5db287f0060b 6 /// @note Copyright © 2011 by Smartware Computing, all rights reserved.
WiredHome 0:5db287f0060b 7 /// @author David Smart
WiredHome 0:5db287f0060b 8 ///
WiredHome 0:5db287f0060b 9 #include "mbed.h"
WiredHome 0:5db287f0060b 10 #include "SmartBoard.h"
WiredHome 0:5db287f0060b 11 #include "ShowTime.h"
WiredHome 0:5db287f0060b 12 #include "EthernetNetIf.h"
WiredHome 0:5db287f0060b 13 #include "NTPClient.h"
WiredHome 0:5db287f0060b 14 #include "SDFileSystem.h"
WiredHome 0:5db287f0060b 15 #include "MSCFileSystem.h"
WiredHome 0:5db287f0060b 16
WiredHome 0:5db287f0060b 17 Serial pc(USBTX, USBRX); ///< Used as the console for interactively reporting progress
WiredHome 0:5db287f0060b 18
WiredHome 0:5db287f0060b 19 const char * TicTocServer = "ntp.okstate.edu"; ///< time server since it is closer than "0.uk.pool.ntp.org"
WiredHome 0:5db287f0060b 20 const int tzOffsetHr = -6; ///< time zone offset hours to print time in local time
WiredHome 0:5db287f0060b 21 const int tzOffsetMin = 0; ///< time zone offset minutes to print time in local time
WiredHome 0:5db287f0060b 22
WiredHome 0:5db287f0060b 23 void LED_Tests(void);
WiredHome 0:5db287f0060b 24 void PWM_Tests(void);
WiredHome 0:5db287f0060b 25 void AnalogIn_Tests(void);
WiredHome 0:5db287f0060b 26 void RTC_Tests(void);
WiredHome 1:586392c0e935 27 void RTC_Set(void);
WiredHome 0:5db287f0060b 28 void MicroSD_Tests(void);
WiredHome 0:5db287f0060b 29 void RS_232_Tests(void);
WiredHome 0:5db287f0060b 30 void CAN_Tests(void);
WiredHome 0:5db287f0060b 31 void Ethernet_Tests(void);
WiredHome 0:5db287f0060b 32 void USBHost_Tests(void);
WiredHome 0:5db287f0060b 33
WiredHome 0:5db287f0060b 34 /// TestVector will execute a given test, based on the parameter
WiredHome 0:5db287f0060b 35 ///
WiredHome 0:5db287f0060b 36 /// It can show the list of available commands, as for an interactive
WiredHome 0:5db287f0060b 37 /// test session, or it can simply execute the chosen test. This is
WiredHome 0:5db287f0060b 38 /// used for both the automated testing and the interactive testing,
WiredHome 0:5db287f0060b 39 /// so a couple of the commands for interactive would not be used
WiredHome 0:5db287f0060b 40 /// for the automated testing.
WiredHome 0:5db287f0060b 41 /// '?' causes it to display the available commands.
WiredHome 0:5db287f0060b 42 ///
WiredHome 0:5db287f0060b 43 /// @param i contains the single character value indicating the operation
WiredHome 0:5db287f0060b 44 /// to perform.
WiredHome 0:5db287f0060b 45 /// @returns false if the input paramter was 'X'.
WiredHome 0:5db287f0060b 46 /// @returns true if the input parameters was not 'X'.
WiredHome 0:5db287f0060b 47 ///
WiredHome 0:5db287f0060b 48 bool TestVector(int i) {
WiredHome 0:5db287f0060b 49 bool r = true; ///< expect to return true
WiredHome 0:5db287f0060b 50
WiredHome 0:5db287f0060b 51 switch (i) {
WiredHome 0:5db287f0060b 52 default:
WiredHome 0:5db287f0060b 53 case '?':
WiredHome 1:586392c0e935 54 pc.printf("Commands:\r\n"
WiredHome 0:5db287f0060b 55 " L LED_Tests();\r\n"
WiredHome 0:5db287f0060b 56 " P PWM_Tests(); // note interaction between LEDs and PWMs\r\n"
WiredHome 0:5db287f0060b 57 " A AnalogIn_Tests();\r\n"
WiredHome 0:5db287f0060b 58 " R RTC_Tests();\r\n"
WiredHome 0:5db287f0060b 59 " M MicroSD_Tests();\r\n"
WiredHome 0:5db287f0060b 60 " S RS_232_Tests();\r\n"
WiredHome 0:5db287f0060b 61 " C CAN_Tests();\r\n"
WiredHome 0:5db287f0060b 62 " E Ethernet_Tests();\r\n"
WiredHome 0:5db287f0060b 63 " U USBHost_Tests();\r\n"
WiredHome 0:5db287f0060b 64 " X eXit to automatic testing\r\n");
WiredHome 0:5db287f0060b 65 break;
WiredHome 0:5db287f0060b 66 case 'X':
WiredHome 0:5db287f0060b 67 r = false;
WiredHome 0:5db287f0060b 68 break;
WiredHome 0:5db287f0060b 69 case 'L':
WiredHome 0:5db287f0060b 70 LED_Tests();
WiredHome 0:5db287f0060b 71 break;
WiredHome 0:5db287f0060b 72 case 'P':
WiredHome 0:5db287f0060b 73 PWM_Tests();
WiredHome 0:5db287f0060b 74 break;
WiredHome 0:5db287f0060b 75 case 'A':
WiredHome 0:5db287f0060b 76 AnalogIn_Tests();
WiredHome 0:5db287f0060b 77 break;
WiredHome 0:5db287f0060b 78 case 'R':
WiredHome 0:5db287f0060b 79 RTC_Tests();
WiredHome 0:5db287f0060b 80 break;
WiredHome 1:586392c0e935 81 case 'r':
WiredHome 1:586392c0e935 82 RTC_Set();
WiredHome 1:586392c0e935 83 break;
WiredHome 0:5db287f0060b 84 case 'M':
WiredHome 0:5db287f0060b 85 MicroSD_Tests();
WiredHome 0:5db287f0060b 86 break;
WiredHome 0:5db287f0060b 87 case 'S':
WiredHome 0:5db287f0060b 88 RS_232_Tests();
WiredHome 0:5db287f0060b 89 break;
WiredHome 0:5db287f0060b 90 case 'C':
WiredHome 0:5db287f0060b 91 CAN_Tests();
WiredHome 0:5db287f0060b 92 break;
WiredHome 0:5db287f0060b 93 case 'E':
WiredHome 0:5db287f0060b 94 Ethernet_Tests();
WiredHome 0:5db287f0060b 95 break;
WiredHome 0:5db287f0060b 96 case 'U':
WiredHome 0:5db287f0060b 97 USBHost_Tests();
WiredHome 0:5db287f0060b 98 break;
WiredHome 0:5db287f0060b 99 }
WiredHome 0:5db287f0060b 100 return r;
WiredHome 0:5db287f0060b 101 }
WiredHome 0:5db287f0060b 102
WiredHome 0:5db287f0060b 103 /// main is the main startup code.
WiredHome 0:5db287f0060b 104 ///
WiredHome 0:5db287f0060b 105 /// This initializes the test environment, shows a banner,
WiredHome 0:5db287f0060b 106 /// and starts the automated testing.
WiredHome 0:5db287f0060b 107 /// It also detects if the user is attempting to interact, and
WiredHome 0:5db287f0060b 108 /// between each test category there is the possibility to transfer
WiredHome 0:5db287f0060b 109 /// to the interactive test mode.
WiredHome 0:5db287f0060b 110 /// When in interactive test mode, the user determines which test
WiredHome 0:5db287f0060b 111 /// to run. The user can also exit interactive mode back to the
WiredHome 0:5db287f0060b 112 /// automated test mode.
WiredHome 0:5db287f0060b 113 ///
WiredHome 0:5db287f0060b 114 /// @returns never
WiredHome 0:5db287f0060b 115 ///
WiredHome 0:5db287f0060b 116 int main() {
WiredHome 0:5db287f0060b 117 bool init = true; ///< init is slightly different
WiredHome 0:5db287f0060b 118 bool interactive = false; ///< track when in interactive mode
WiredHome 0:5db287f0060b 119 int test = 0; ///< which test to run
WiredHome 1:586392c0e935 120 char TestList[] = "XLPARrMSCEU"; ///< list of valid test commands AUTOTESTS are uppercase
WiredHome 0:5db287f0060b 121
WiredHome 0:5db287f0060b 122 while (1) {
WiredHome 0:5db287f0060b 123 if (pc.readable() || init) {
WiredHome 0:5db287f0060b 124 pc.printf("\r\n\r\n");
WiredHome 0:5db287f0060b 125 pc.printf("SmartBoard Hardware Tester\r\n");
WiredHome 0:5db287f0060b 126 pc.printf(" SmartBoard Hardware v0.05\r\n");
WiredHome 1:586392c0e935 127 pc.printf(" SmartBoard Software v0.09\r\n");
WiredHome 0:5db287f0060b 128 pc.printf("\r\n");
WiredHome 0:5db287f0060b 129 pc.printf(" [USB] [Eth/USB] \r\n");
WiredHome 0:5db287f0060b 130 pc.printf(" +---------------+------------+---+-------+---+\r\n");
WiredHome 0:5db287f0060b 131 pc.printf(" |O [RS232 1-2] | | | | | | O|\r\n");
WiredHome 0:5db287f0060b 132 pc.printf(" | | |microSD| | | | |\r\n");
WiredHome 0:5db287f0060b 133 pc.printf(" |S | | | | | | C|\r\n");
WiredHome 0:5db287f0060b 134 pc.printf(" |P | +-------+ | | | A|\r\n");
WiredHome 0:5db287f0060b 135 pc.printf(" |I | | |Yl Gr| N|\r\n");
WiredHome 0:5db287f0060b 136 pc.printf(" |1 | | +-------+ 1|\r\n");
WiredHome 0:5db287f0060b 137 pc.printf(" |- | | -|\r\n");
WiredHome 0:5db287f0060b 138 pc.printf(" |2 | RTC | 2|\r\n");
WiredHome 0:5db287f0060b 139 pc.printf(" | | (Battery) | |\r\n");
WiredHome 0:5db287f0060b 140 pc.printf(" | | | |\r\n");
WiredHome 0:5db287f0060b 141 pc.printf(" | | 1 2 3 4 | |\r\n");
WiredHome 0:5db287f0060b 142 pc.printf(" | +------------+ |\r\n");
WiredHome 0:5db287f0060b 143 pc.printf(" |O[Analog In ] O [PWM Out] O|\r\n");
WiredHome 0:5db287f0060b 144 pc.printf(" +--------------------------------------------+\r\n");
WiredHome 0:5db287f0060b 145 pc.printf("\r\n");
WiredHome 0:5db287f0060b 146 init = false;
WiredHome 0:5db287f0060b 147 }
WiredHome 0:5db287f0060b 148 if (pc.readable()) {
WiredHome 0:5db287f0060b 149 interactive = true;
WiredHome 0:5db287f0060b 150 while (pc.readable())
WiredHome 0:5db287f0060b 151 (void)pc.getc();
WiredHome 0:5db287f0060b 152
WiredHome 0:5db287f0060b 153 while (interactive) {
WiredHome 0:5db287f0060b 154 pc.printf("> ");
WiredHome 0:5db287f0060b 155 int i = pc.getc();
WiredHome 0:5db287f0060b 156 pc.putc(i);
WiredHome 0:5db287f0060b 157 pc.putc('\r');
WiredHome 0:5db287f0060b 158 pc.putc('\n');
WiredHome 0:5db287f0060b 159 interactive = TestVector(i);
WiredHome 0:5db287f0060b 160 }
WiredHome 0:5db287f0060b 161 } else {
WiredHome 0:5db287f0060b 162 if (test == 0)
WiredHome 0:5db287f0060b 163 pc.printf("\x07"); // Bell character indicating start of tests
WiredHome 0:5db287f0060b 164 TestVector(TestList[test++]);
WiredHome 0:5db287f0060b 165 if (TestList[test] == '\0')
WiredHome 0:5db287f0060b 166 test = 0;
WiredHome 0:5db287f0060b 167 wait(5.0); // Extra pause
WiredHome 0:5db287f0060b 168 }
WiredHome 0:5db287f0060b 169 }
WiredHome 0:5db287f0060b 170 }
WiredHome 0:5db287f0060b 171
WiredHome 0:5db287f0060b 172 /// LED_Tests performs some simple digital output to the
WiredHome 0:5db287f0060b 173 /// LEDs.
WiredHome 0:5db287f0060b 174 ///
WiredHome 0:5db287f0060b 175 /// It will attempt to exercise the LEDs on the Ethernet ports
WiredHome 0:5db287f0060b 176 /// as well, but by jumper configuration these may not be available.
WiredHome 0:5db287f0060b 177 ///
WiredHome 0:5db287f0060b 178 void LED_Tests(void) {
WiredHome 0:5db287f0060b 179 int l;
WiredHome 0:5db287f0060b 180 int i;
WiredHome 0:5db287f0060b 181 struct {
WiredHome 0:5db287f0060b 182 const char * name;
WiredHome 0:5db287f0060b 183 DigitalOut led;
WiredHome 0:5db287f0060b 184 } Leds[] = {
WiredHome 0:5db287f0060b 185 {"Ethernet Green", ETHERGREEN},
WiredHome 0:5db287f0060b 186 {"Ethernet Yellow", ETHERYELLOW},
WiredHome 0:5db287f0060b 187 {"Led 1", LED1},
WiredHome 0:5db287f0060b 188 {"Led 2", LED2},
WiredHome 0:5db287f0060b 189 {"Led 3", LED3},
WiredHome 0:5db287f0060b 190 {"Led 4", LED4}
WiredHome 0:5db287f0060b 191 };
WiredHome 0:5db287f0060b 192 const int numLeds = sizeof(Leds) / sizeof(Leds[0]);
WiredHome 0:5db287f0060b 193
WiredHome 0:5db287f0060b 194 printf("LED Test:\r\n");
WiredHome 0:5db287f0060b 195 for (l=0; l<numLeds; l++) {
WiredHome 0:5db287f0060b 196 printf(" Blink %s LED 3 times\r\n", Leds[l].name);
WiredHome 0:5db287f0060b 197 for (i=0; i<3; i++) {
WiredHome 0:5db287f0060b 198 Leds[l].led = true;
WiredHome 0:5db287f0060b 199 wait(0.4);
WiredHome 0:5db287f0060b 200 Leds[l].led = false;
WiredHome 0:5db287f0060b 201 wait(0.4);
WiredHome 0:5db287f0060b 202 }
WiredHome 0:5db287f0060b 203 }
WiredHome 0:5db287f0060b 204 }
WiredHome 0:5db287f0060b 205
WiredHome 0:5db287f0060b 206 /// PWM_Tests performs some simple pwm output to the
WiredHome 0:5db287f0060b 207 /// PWM channels and the LEDs.
WiredHome 0:5db287f0060b 208 ///
WiredHome 0:5db287f0060b 209 /// It will attempt to exercise the outputs with a simple ramping
WiredHome 0:5db287f0060b 210 /// signal, but by jumper configuration these may not be available.
WiredHome 0:5db287f0060b 211 ///
WiredHome 0:5db287f0060b 212 void PWM_Tests(void) {
WiredHome 0:5db287f0060b 213 int l;
WiredHome 0:5db287f0060b 214 int i;
WiredHome 0:5db287f0060b 215 float f;
WiredHome 0:5db287f0060b 216 struct {
WiredHome 0:5db287f0060b 217 const char * name;
WiredHome 0:5db287f0060b 218 PwmOut pwm;
WiredHome 0:5db287f0060b 219 } Pwms[] = {
WiredHome 0:5db287f0060b 220 {"PWM 1", p21},
WiredHome 0:5db287f0060b 221 {"PWM 2", p22},
WiredHome 0:5db287f0060b 222 {"PWM 3", p23},
WiredHome 0:5db287f0060b 223 {"PWM 4", p24},
WiredHome 0:5db287f0060b 224 {"PWM 5", p25},
WiredHome 0:5db287f0060b 225 {"PWM 6", p26},
WiredHome 0:5db287f0060b 226 {"Led 1", LED1},
WiredHome 0:5db287f0060b 227 {"Led 2", LED2},
WiredHome 0:5db287f0060b 228 {"Led 3", LED3},
WiredHome 0:5db287f0060b 229 {"Led 4", LED4}
WiredHome 0:5db287f0060b 230 };
WiredHome 0:5db287f0060b 231 const int numPwms = sizeof(Pwms) / sizeof(Pwms[0]);
WiredHome 0:5db287f0060b 232
WiredHome 0:5db287f0060b 233 printf("PWM Test:\r\n");
WiredHome 0:5db287f0060b 234 for (l=0; l<numPwms; l++) {
WiredHome 0:5db287f0060b 235 printf(" Ramp %s PWM 3 times\r\n", Pwms[l].name);
WiredHome 0:5db287f0060b 236 for (i=0; i<3; i++) {
WiredHome 0:5db287f0060b 237 for (f=0.0; f<=1.0; f+= 0.1) {
WiredHome 0:5db287f0060b 238 Pwms[l].pwm = f;
WiredHome 0:5db287f0060b 239 wait(0.1);
WiredHome 0:5db287f0060b 240 }
WiredHome 0:5db287f0060b 241 }
WiredHome 0:5db287f0060b 242 Pwms[l].pwm = 0; // off when done
WiredHome 0:5db287f0060b 243 }
WiredHome 0:5db287f0060b 244 }
WiredHome 0:5db287f0060b 245
WiredHome 0:5db287f0060b 246 /// AnalogIn_Tests takes a few sample measurements on each channel
WiredHome 0:5db287f0060b 247 ///
WiredHome 0:5db287f0060b 248 /// It samples each channel a number of times and presents the
WiredHome 0:5db287f0060b 249 /// converted results on the console.
WiredHome 0:5db287f0060b 250 ///
WiredHome 0:5db287f0060b 251 void AnalogIn_Tests(void) {
WiredHome 0:5db287f0060b 252 int l;
WiredHome 0:5db287f0060b 253 int i;
WiredHome 0:5db287f0060b 254 const int samples = 20;
WiredHome 0:5db287f0060b 255 struct {
WiredHome 0:5db287f0060b 256 const char * name;
WiredHome 0:5db287f0060b 257 AnalogIn in;
WiredHome 0:5db287f0060b 258 } Analogs[] = {
WiredHome 0:5db287f0060b 259 {"Ain 1", p15},
WiredHome 0:5db287f0060b 260 {"Ain 2", p16},
WiredHome 0:5db287f0060b 261 {"Ain 3", p17},
WiredHome 0:5db287f0060b 262 {"Ain 4", p18},
WiredHome 0:5db287f0060b 263 {"Ain 5", p19},
WiredHome 0:5db287f0060b 264 {"Ain 6", p20}
WiredHome 0:5db287f0060b 265 };
WiredHome 0:5db287f0060b 266 const int numAnalogs = sizeof(Analogs) / sizeof(Analogs[0]);
WiredHome 0:5db287f0060b 267
WiredHome 0:5db287f0060b 268 printf("Analog Test:\r\n");
WiredHome 0:5db287f0060b 269 for (l=0; l<numAnalogs; l++) {
WiredHome 0:5db287f0060b 270 for (i=0; i<samples; i++) {
WiredHome 0:5db287f0060b 271 uint16_t raw = Analogs[l].in.read_u16();
WiredHome 0:5db287f0060b 272 float flt = Analogs[l].in.read();
WiredHome 0:5db287f0060b 273 printf(" Analog %i is %04X, %3.2f, %3.2fv\r", l, raw, flt, flt*3.3);
WiredHome 0:5db287f0060b 274 wait(0.1);
WiredHome 0:5db287f0060b 275 }
WiredHome 0:5db287f0060b 276 printf("\n");
WiredHome 0:5db287f0060b 277 }
WiredHome 0:5db287f0060b 278 }
WiredHome 0:5db287f0060b 279
WiredHome 0:5db287f0060b 280 /// RTC_Tests will perform simple tests on the Real Time Clock
WiredHome 0:5db287f0060b 281 ///
WiredHome 0:5db287f0060b 282 /// It will first sample the time from the RTC and later restore
WiredHome 0:5db287f0060b 283 /// it as best it can.
WiredHome 0:5db287f0060b 284 /// In the middle of that it will set the clock, then simply show
WiredHome 0:5db287f0060b 285 /// the time once per second for 5 seconds. After this it
WiredHome 0:5db287f0060b 286 /// will restore the clock at best it can.
WiredHome 0:5db287f0060b 287 ///
WiredHome 0:5db287f0060b 288 void RTC_Tests(void) {
WiredHome 0:5db287f0060b 289 time_t x;
WiredHome 0:5db287f0060b 290 int i;
WiredHome 0:5db287f0060b 291 const int oldTime = 1256729737;
WiredHome 0:5db287f0060b 292
WiredHome 0:5db287f0060b 293 printf("RTC Test:\r\n");
WiredHome 1:586392c0e935 294 ShowTime(tzOffsetHr, tzOffsetMin);
WiredHome 0:5db287f0060b 295 x = time(NULL); // Save the time before the test
WiredHome 0:5db287f0060b 296 printf(" Saving current time(%d)\r\n", x);
WiredHome 0:5db287f0060b 297
WiredHome 0:5db287f0060b 298 set_time(oldTime); // Set RTC time to Wed, 28 Oct 2009 11:35:37
WiredHome 0:5db287f0060b 299 printf(" Set time to Wed, 28 Oct 2009 11:35:37\r\n");
WiredHome 0:5db287f0060b 300
WiredHome 0:5db287f0060b 301 for (i=0; i<5; i++) {
WiredHome 0:5db287f0060b 302 ShowTime();
WiredHome 0:5db287f0060b 303 wait(1.0);
WiredHome 0:5db287f0060b 304 }
WiredHome 0:5db287f0060b 305 set_time(x + time(NULL) - 1256729737); // Approximately restored
WiredHome 1:586392c0e935 306 ShowTime(tzOffsetHr, tzOffsetMin);
WiredHome 0:5db287f0060b 307 wait(1.0);
WiredHome 1:586392c0e935 308 ShowTime(tzOffsetHr, tzOffsetMin);
WiredHome 1:586392c0e935 309 }
WiredHome 1:586392c0e935 310
WiredHome 1:586392c0e935 311 /// GetNumber will get from the user a number using the
WiredHome 1:586392c0e935 312 /// specified number of digits.
WiredHome 1:586392c0e935 313 ///
WiredHome 1:586392c0e935 314 /// They can enter a number from 0 to 10^(digits-1)
WiredHome 1:586392c0e935 315 ///
WiredHome 1:586392c0e935 316 /// @param digits is the number of digits to enter
WiredHome 1:586392c0e935 317 /// @param pValue is a pointer to where to store the result
WiredHome 1:586392c0e935 318 /// @returns true if a number was entered
WiredHome 1:586392c0e935 319 /// @returns false if they entered a non-digit
WiredHome 1:586392c0e935 320 ///
WiredHome 1:586392c0e935 321 int GetNumber(int digits, int * pValue) {
WiredHome 1:586392c0e935 322 int tempValue = 0;
WiredHome 1:586392c0e935 323 int i;
WiredHome 1:586392c0e935 324
WiredHome 1:586392c0e935 325 while (digits--) {
WiredHome 1:586392c0e935 326 i = pc.getc();
WiredHome 1:586392c0e935 327 if (i == ' ') i = '0'; // special case for leading blank
WiredHome 1:586392c0e935 328 if (i >= '0' && i <= '9') {
WiredHome 1:586392c0e935 329 pc.putc(i);
WiredHome 1:586392c0e935 330 tempValue = tempValue * 10 + (i - '0');
WiredHome 1:586392c0e935 331 } else
WiredHome 1:586392c0e935 332 return false;
WiredHome 1:586392c0e935 333 }
WiredHome 1:586392c0e935 334 *pValue = tempValue;
WiredHome 1:586392c0e935 335 return true;
WiredHome 0:5db287f0060b 336 }
WiredHome 0:5db287f0060b 337
WiredHome 1:586392c0e935 338 /// RTC_Set will interactively set the Real Time Clock
WiredHome 1:586392c0e935 339 ///
WiredHome 1:586392c0e935 340 /// It will allow you to enter the date and time information
WiredHome 1:586392c0e935 341 /// and then create a timestamp. After this, it will set
WiredHome 1:586392c0e935 342 /// the RTC to that timestamp.
WiredHome 1:586392c0e935 343 ///
WiredHome 1:586392c0e935 344 void RTC_Set(void) {
WiredHome 1:586392c0e935 345 time_t seconds = time(NULL);
WiredHome 1:586392c0e935 346 struct tm *t = localtime(&seconds);
WiredHome 1:586392c0e935 347 int i;
WiredHome 1:586392c0e935 348
WiredHome 1:586392c0e935 349 printf("RTC Set:\r\n");
WiredHome 1:586392c0e935 350 ShowTime(seconds, tzOffsetHr, tzOffsetMin);
WiredHome 1:586392c0e935 351 printf(" Enter the time MM/DD/YYYY HH:MM:SS\r\n");
WiredHome 1:586392c0e935 352 while (1) {
WiredHome 1:586392c0e935 353 printf(" > ");
WiredHome 1:586392c0e935 354 if (GetNumber(2, &t->tm_mon)) {
WiredHome 1:586392c0e935 355 pc.putc('/');
WiredHome 1:586392c0e935 356 if (!GetNumber(2, &t->tm_mday))
WiredHome 1:586392c0e935 357 continue;
WiredHome 1:586392c0e935 358 pc.putc('/');
WiredHome 1:586392c0e935 359 if (!GetNumber(4, &t->tm_year))
WiredHome 1:586392c0e935 360 continue;
WiredHome 1:586392c0e935 361 } else {
WiredHome 1:586392c0e935 362 pc.printf("%02d/%02d/%04d", t->tm_mon+1, t->tm_mday, t->tm_year+1900);
WiredHome 1:586392c0e935 363 }
WiredHome 1:586392c0e935 364 pc.putc(' ');
WiredHome 1:586392c0e935 365 if (!GetNumber(2, &t->tm_hour)) {
WiredHome 1:586392c0e935 366 pc.printf("%02d:%02d:%02d\r\n", t->tm_hour, t->tm_min, t->tm_sec);
WiredHome 1:586392c0e935 367 break; // they can bail here
WiredHome 1:586392c0e935 368 }
WiredHome 1:586392c0e935 369 pc.putc(':');
WiredHome 1:586392c0e935 370 if (!GetNumber(2, &t->tm_min))
WiredHome 1:586392c0e935 371 continue;
WiredHome 1:586392c0e935 372 pc.putc(':');
WiredHome 1:586392c0e935 373 if (!GetNumber(2, &t->tm_sec))
WiredHome 1:586392c0e935 374 continue;
WiredHome 1:586392c0e935 375 else {
WiredHome 1:586392c0e935 376 t->tm_mon--; // 1-12 => 0-11
WiredHome 1:586392c0e935 377 t->tm_year -= 1900;
WiredHome 1:586392c0e935 378 t->tm_hour -= tzOffsetHr;
WiredHome 1:586392c0e935 379 t->tm_min -= tzOffsetMin;
WiredHome 1:586392c0e935 380 pc.printf("\r\n");
WiredHome 1:586392c0e935 381 // convert to timestamp and display (1256729737)
WiredHome 1:586392c0e935 382 time_t seconds = mktime(t);
WiredHome 1:586392c0e935 383 set_time(seconds);
WiredHome 1:586392c0e935 384 break;
WiredHome 1:586392c0e935 385 }
WiredHome 1:586392c0e935 386 }
WiredHome 1:586392c0e935 387 for (i=0; i<5; i++) {
WiredHome 1:586392c0e935 388 ShowTime(tzOffsetHr, tzOffsetMin);
WiredHome 1:586392c0e935 389 wait(1.0);
WiredHome 1:586392c0e935 390 }
WiredHome 1:586392c0e935 391 }
WiredHome 1:586392c0e935 392
WiredHome 1:586392c0e935 393
WiredHome 0:5db287f0060b 394 /// Ethernet_Tests will attempt to test the Ethernet interface
WiredHome 0:5db287f0060b 395 ///
WiredHome 0:5db287f0060b 396 /// It will connect to the network - if possible, then it will
WiredHome 0:5db287f0060b 397 /// try to connect to a network time server and set the clock,
WiredHome 0:5db287f0060b 398 /// using hard coded time server and time zone offset values.
WiredHome 0:5db287f0060b 399 ///
WiredHome 0:5db287f0060b 400 /// It appears that the Ethernet interface cannot be instantiated,
WiredHome 0:5db287f0060b 401 /// destroyed, and later instantiated again (it would reliably "hang").
WiredHome 0:5db287f0060b 402 /// So, this test is "runonce" protected.
WiredHome 0:5db287f0060b 403 ///
WiredHome 0:5db287f0060b 404 void Ethernet_Tests(void) {
WiredHome 0:5db287f0060b 405 EthernetNetIf eth;
WiredHome 0:5db287f0060b 406 NTPClient ntp;
WiredHome 0:5db287f0060b 407 static bool runonce = true;
WiredHome 0:5db287f0060b 408
WiredHome 0:5db287f0060b 409 printf("Ethernet Test:\r\n");
WiredHome 0:5db287f0060b 410 if (runonce) {
WiredHome 0:5db287f0060b 411 EthernetErr ethErr = eth.setup();
WiredHome 0:5db287f0060b 412 if (ethErr) {
WiredHome 0:5db287f0060b 413 printf("Error %d in setup.\r\n", ethErr);
WiredHome 0:5db287f0060b 414 return;
WiredHome 0:5db287f0060b 415 }
WiredHome 0:5db287f0060b 416 printf(" Ethernet Setup OK\r\n");
WiredHome 0:5db287f0060b 417 ShowTime(0, tzOffsetHr, tzOffsetMin);
WiredHome 0:5db287f0060b 418 printf(" Setting clock to %s\r\n", TicTocServer);
WiredHome 0:5db287f0060b 419 Host server(IpAddr(), 123, TicTocServer);
WiredHome 0:5db287f0060b 420 ntp.setTime(server);
WiredHome 0:5db287f0060b 421 printf(" Clock was set.\r\n");
WiredHome 0:5db287f0060b 422 wait(1.0);
WiredHome 0:5db287f0060b 423 ShowTime(0, tzOffsetHr, tzOffsetMin);
WiredHome 0:5db287f0060b 424 runonce = false;
WiredHome 0:5db287f0060b 425 } else {
WiredHome 0:5db287f0060b 426 printf(" only runs once per cold-boot.\r\n");
WiredHome 0:5db287f0060b 427 }
WiredHome 0:5db287f0060b 428 }
WiredHome 0:5db287f0060b 429
WiredHome 1:586392c0e935 430 /// isPrint tests the passed in character for being 'printable'
WiredHome 1:586392c0e935 431 ///
WiredHome 1:586392c0e935 432 /// It will evaluate the character on a simple ASCII range of
WiredHome 1:586392c0e935 433 /// characters 0 to 127.
WiredHome 1:586392c0e935 434 ///
WiredHome 1:586392c0e935 435 /// @param i is the character to test
WiredHome 1:586392c0e935 436 /// @returns true if the character is in the printable set (including <space>)
WiredHome 1:586392c0e935 437 /// @returns false if the character is not printable (may be control code or extended character)
WiredHome 1:586392c0e935 438 ///
WiredHome 1:586392c0e935 439 bool isPrint(char i) {
WiredHome 1:586392c0e935 440 if (i >= ' ' && i <= '~')
WiredHome 1:586392c0e935 441 return true;
WiredHome 1:586392c0e935 442 else
WiredHome 1:586392c0e935 443 return false;
WiredHome 1:586392c0e935 444 }
WiredHome 1:586392c0e935 445
WiredHome 0:5db287f0060b 446 /// MicroSD_Tests attempts to access and write a file on the micro SD card
WiredHome 0:5db287f0060b 447 ///
WiredHome 0:5db287f0060b 448 /// It will mount the file system, then attempt to write a simple
WiredHome 0:5db287f0060b 449 /// file on the micro SD card.
WiredHome 0:5db287f0060b 450 ///
WiredHome 0:5db287f0060b 451 void MicroSD_Tests(void) {
WiredHome 1:586392c0e935 452 SDFileSystem sd(p5, p6, p7, p8, "ud"); // the pinout on the mbed Cool Components workshop board
WiredHome 1:586392c0e935 453 DigitalIn cardIn(p11);
WiredHome 0:5db287f0060b 454 FILE *fp;
WiredHome 0:5db287f0060b 455 char buffer[50];
WiredHome 0:5db287f0060b 456
WiredHome 0:5db287f0060b 457 printf("SD File System Tests:\r\n");
WiredHome 1:586392c0e935 458 printf(" Card Detection: %s\r\n", (cardIn) ? "in" : "out");
WiredHome 1:586392c0e935 459 if (cardIn) {
WiredHome 1:586392c0e935 460 mkdir("/ud/testDir", 0777);
WiredHome 1:586392c0e935 461 fp = fopen("/ud/testDir/sdtest.txt", "w");
WiredHome 1:586392c0e935 462 if (fp == NULL) {
WiredHome 1:586392c0e935 463 printf(" Could not open file for write\r\n");
WiredHome 1:586392c0e935 464 } else {
WiredHome 1:586392c0e935 465 fprintf(fp, "Write a message to the micro SD card!\n");
WiredHome 1:586392c0e935 466 fclose(fp);
WiredHome 1:586392c0e935 467 printf(" Closed file.\r\n");
WiredHome 1:586392c0e935 468 fp = fopen("/ud/testDir/sdtest.txt", "r");
WiredHome 1:586392c0e935 469 if (fp) {
WiredHome 1:586392c0e935 470 printf(" Reading file back.\r\n");
WiredHome 1:586392c0e935 471 if (fgets(buffer, sizeof(buffer), fp)) {
WiredHome 1:586392c0e935 472 while (strlen(buffer) > 0 && !isPrint(buffer[strlen(buffer)-1]))
WiredHome 1:586392c0e935 473 buffer[strlen(buffer)-1] = '\0'; // chomp the <LF>
WiredHome 1:586392c0e935 474 printf(" Read: {%s}\r\n", buffer);
WiredHome 1:586392c0e935 475 }
WiredHome 1:586392c0e935 476 fclose(fp);
WiredHome 0:5db287f0060b 477 }
WiredHome 0:5db287f0060b 478 }
WiredHome 0:5db287f0060b 479 }
WiredHome 0:5db287f0060b 480 printf(" test complete!\r\n");
WiredHome 0:5db287f0060b 481 }
WiredHome 0:5db287f0060b 482
WiredHome 0:5db287f0060b 483
WiredHome 0:5db287f0060b 484 /// USBHost_Tests attempts to access and write a file on USB stick
WiredHome 0:5db287f0060b 485 ///
WiredHome 0:5db287f0060b 486 /// It will mount the file system, then attempt to write a simple
WiredHome 0:5db287f0060b 487 /// file on the USB interface.
WiredHome 0:5db287f0060b 488 ///
WiredHome 0:5db287f0060b 489 void USBHost_Tests(void) {
WiredHome 0:5db287f0060b 490 MSCFileSystem fs ("fs");
WiredHome 0:5db287f0060b 491 FILE *fp;
WiredHome 0:5db287f0060b 492 char buffer[50];
WiredHome 0:5db287f0060b 493
WiredHome 0:5db287f0060b 494 printf("USB Host Tests: [installed memory stick required]\r\n");
WiredHome 0:5db287f0060b 495 fp = fopen("/fs/hello.txt","w");
WiredHome 0:5db287f0060b 496 if (fp) {
WiredHome 0:5db287f0060b 497 printf(" Writing to hello.txt file\r\n");
WiredHome 0:5db287f0060b 498 fprintf(fp,"Hello world!\r\n");
WiredHome 0:5db287f0060b 499 fclose (fp);
WiredHome 0:5db287f0060b 500 printf(" Closed file.\r\n");
WiredHome 0:5db287f0060b 501 fp = fopen("/fs/hello.txt", "r");
WiredHome 0:5db287f0060b 502 if (fp) {
WiredHome 0:5db287f0060b 503 printf(" Reading file back.\r\n");
WiredHome 0:5db287f0060b 504 if (fgets(buffer, sizeof(buffer), fp)) {
WiredHome 0:5db287f0060b 505 if (strlen(buffer) > 2)
WiredHome 0:5db287f0060b 506 buffer[strlen(buffer)-2] = '\0'; // chomp the <LF>
WiredHome 0:5db287f0060b 507 printf(" Read: {%s}\r\n", buffer);
WiredHome 0:5db287f0060b 508 }
WiredHome 0:5db287f0060b 509 fclose(fp);
WiredHome 0:5db287f0060b 510 }
WiredHome 0:5db287f0060b 511 }
WiredHome 0:5db287f0060b 512 }
WiredHome 0:5db287f0060b 513
WiredHome 1:586392c0e935 514
WiredHome 1:586392c0e935 515 /// FormatMicroseconds will format a number of microseconds int ascii seconds.
WiredHome 1:586392c0e935 516 ///
WiredHome 1:586392c0e935 517 /// It will support formatting the number with decimal and thousands
WiredHome 1:586392c0e935 518 /// separators.
WiredHome 1:586392c0e935 519 ///
WiredHome 1:586392c0e935 520 /// @param value to format
WiredHome 1:586392c0e935 521 /// @returns the formatted string
WiredHome 1:586392c0e935 522 ///
WiredHome 1:586392c0e935 523 char * FormatMicroseconds(int value) {
WiredHome 1:586392c0e935 524 static char result[15] = "";
WiredHome 1:586392c0e935 525 int uSec = value % 1000000;
WiredHome 1:586392c0e935 526 int Sec = value / 1000000;
WiredHome 1:586392c0e935 527
WiredHome 1:586392c0e935 528 if (Sec > 1000)
WiredHome 1:586392c0e935 529 sprintf(result, "%3i,%3i.%06i", Sec/1000, Sec%1000, uSec);
WiredHome 1:586392c0e935 530 else
WiredHome 1:586392c0e935 531 sprintf(result, "%7i.%06i", Sec, uSec);
WiredHome 1:586392c0e935 532 return result;
WiredHome 1:586392c0e935 533 }
WiredHome 1:586392c0e935 534
WiredHome 1:586392c0e935 535
WiredHome 1:586392c0e935 536 /// ShowCANMessage will print to the console as specified
WiredHome 1:586392c0e935 537 ///
WiredHome 1:586392c0e935 538 /// This format is used in other tools, and is not explained
WiredHome 1:586392c0e935 539 /// here except in the code.
WiredHome 1:586392c0e935 540 ///
WiredHome 1:586392c0e935 541 /// @param tx indicates it is a transmit message when non-zero, receive otherwise
WiredHome 1:586392c0e935 542 /// @param ch is the communication channel of this message
WiredHome 1:586392c0e935 543 /// @msg is the CAN message to be shown
WiredHome 1:586392c0e935 544 /// @uSec is the timestamp in microseconds
WiredHome 1:586392c0e935 545 /// @returns nothing
WiredHome 1:586392c0e935 546 ///
WiredHome 1:586392c0e935 547 void ShowCANMessage(int tx, int ch, CANMessage msg, int uSec) {
WiredHome 1:586392c0e935 548 pc.printf("%c %s %d %08X %02X ", (tx) ? 't' : 'r', (msg.format == CANExtended) ? "xtd" : "nrm", ch, msg.id, msg.len);
WiredHome 1:586392c0e935 549 for (int d=0; d<8; d++) {
WiredHome 1:586392c0e935 550 if (d < msg.len)
WiredHome 1:586392c0e935 551 pc.printf("%02X ", msg.data[d]);
WiredHome 1:586392c0e935 552 else
WiredHome 1:586392c0e935 553 pc.printf(" ");
WiredHome 1:586392c0e935 554 }
WiredHome 1:586392c0e935 555 pc.printf("0 0 %s\r\n", FormatMicroseconds(uSec));
WiredHome 1:586392c0e935 556 }
WiredHome 1:586392c0e935 557
WiredHome 1:586392c0e935 558
WiredHome 0:5db287f0060b 559 /// CAN_Tests will send some packets on one CAN port and expect them on the other
WiredHome 0:5db287f0060b 560 ///
WiredHome 0:5db287f0060b 561 /// It will attempt to send 10 messages on one port and expect that
WiredHome 0:5db287f0060b 562 /// all 10 messages were received on the other port. The two ports should
WiredHome 0:5db287f0060b 563 /// be wired from one to the other with a loop-back cable and a termination
WiredHome 0:5db287f0060b 564 /// resistor.
WiredHome 0:5db287f0060b 565 ///
WiredHome 0:5db287f0060b 566 void CAN_Tests(void) {
WiredHome 0:5db287f0060b 567 CAN can1(p9, p10);
WiredHome 0:5db287f0060b 568 CAN can2(p30, p29);
WiredHome 0:5db287f0060b 569 char Txcounter = 0;
WiredHome 0:5db287f0060b 570 char Rxcounter = 0;
WiredHome 1:586392c0e935 571 CANMessage tMsg;
WiredHome 1:586392c0e935 572 CANMessage rMsg;
WiredHome 1:586392c0e935 573 Timer timer;
WiredHome 0:5db287f0060b 574 int i;
WiredHome 0:5db287f0060b 575
WiredHome 1:586392c0e935 576 pc.printf("CAN Tests:\r\n");
WiredHome 1:586392c0e935 577 i = can1.frequency(250000);
WiredHome 1:586392c0e935 578 pc.printf(" can1 frequency set: %i\r\n", i);
WiredHome 1:586392c0e935 579 i = can2.frequency(250000);
WiredHome 1:586392c0e935 580 pc.printf(" can2 frequency set: %i\r\n", i);
WiredHome 1:586392c0e935 581 timer.start();
WiredHome 1:586392c0e935 582 for (i=0; i<25; i++) { // gets a few more passes to receive the messages
WiredHome 1:586392c0e935 583
WiredHome 1:586392c0e935 584 for (int x=0; x<8; x++)
WiredHome 1:586392c0e935 585 tMsg.data[x] = (char)(rand());
WiredHome 1:586392c0e935 586 tMsg.data[0] = Txcounter;
WiredHome 1:586392c0e935 587 tMsg.id = rand();
WiredHome 1:586392c0e935 588 tMsg.len = rand() % 9;
WiredHome 1:586392c0e935 589 tMsg.type = CANData;
WiredHome 1:586392c0e935 590 tMsg.format = (rand() & 1) ? CANExtended : CANStandard;
WiredHome 1:586392c0e935 591
WiredHome 1:586392c0e935 592 if (Txcounter < 10 && can1.write(tMsg)) {
WiredHome 1:586392c0e935 593 pc.printf(" ");
WiredHome 1:586392c0e935 594 ShowCANMessage(1, 1, tMsg, timer.read_us());
WiredHome 0:5db287f0060b 595 Txcounter++;
WiredHome 1:586392c0e935 596 //wait(0.05);
WiredHome 0:5db287f0060b 597 }
WiredHome 1:586392c0e935 598 if (can2.read(rMsg)) {
WiredHome 0:5db287f0060b 599 Rxcounter++;
WiredHome 1:586392c0e935 600 pc.printf(" ");
WiredHome 1:586392c0e935 601 ShowCANMessage(0, 2, rMsg, timer.read_us());
WiredHome 0:5db287f0060b 602 }
WiredHome 1:586392c0e935 603 wait(0.005);
WiredHome 0:5db287f0060b 604 }
WiredHome 0:5db287f0060b 605 if (Txcounter == Rxcounter)
WiredHome 0:5db287f0060b 606 printf(" passed.\r\n");
WiredHome 0:5db287f0060b 607 else
WiredHome 0:5db287f0060b 608 printf(" **** Txcounter (%d) != Rxcounter (%d) ****\r\n", Txcounter, Rxcounter);
WiredHome 0:5db287f0060b 609 }
WiredHome 0:5db287f0060b 610
WiredHome 0:5db287f0060b 611 /// RS_232_Tests will say hello on each of the RS-232 channels
WiredHome 0:5db287f0060b 612 ///
WiredHome 0:5db287f0060b 613 /// It will print a hello text string out each of the ports.
WiredHome 0:5db287f0060b 614 ///
WiredHome 0:5db287f0060b 615 void RS_232_Tests(void) {
WiredHome 0:5db287f0060b 616 Serial s1(p13, p14);
WiredHome 0:5db287f0060b 617 Serial s2(p28, p27);
WiredHome 0:5db287f0060b 618
WiredHome 0:5db287f0060b 619 pc.printf("RS-232 Tests:\r\n");
WiredHome 0:5db287f0060b 620 s1.printf(" Hello going out S1\r\n");
WiredHome 0:5db287f0060b 621 s2.printf(" Hello going out S2\r\n");
WiredHome 0:5db287f0060b 622 pc.printf(" end tests.\r\n");
WiredHome 0:5db287f0060b 623 }