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