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@3:2b4fe31e8d15, 2011-01-25 (annotated)
- Committer:
- WiredHome
- Date:
- Tue Jan 25 02:02:19 2011 +0000
- Revision:
- 3:2b4fe31e8d15
- Parent:
- 2:02e7d896824f
- Child:
- 4:ca93a8d4874d
v0.10
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 | 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 | 2:02e7d896824f | 17 | Serial pc(USBTX, USBRX); ///!< Used as the console for interactively reporting progress |
WiredHome | 0:5db287f0060b | 18 | |
WiredHome | 2:02e7d896824f | 19 | const char * TicTocServer = "ntp.okstate.edu"; ///!< time server since it is closer than "0.uk.pool.ntp.org" |
WiredHome | 2:02e7d896824f | 20 | const int tzOffsetHr = -6; ///!< time zone offset hours to print time in local time |
WiredHome | 2:02e7d896824f | 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 | 2:02e7d896824f | 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 | 2:02e7d896824f | 117 | bool init = true; ///!< init is slightly different |
WiredHome | 2:02e7d896824f | 118 | bool interactive = false; ///!< track when in interactive mode |
WiredHome | 2:02e7d896824f | 119 | int test = 0; ///!< which test to run |
WiredHome | 2:02e7d896824f | 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 | 3:2b4fe31e8d15 | 127 | pc.printf(" SmartBoard Software v0.10\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 | 3:2b4fe31e8d15 | 349 | pc.printf("RTC Set:\r\n"); |
WiredHome | 1:586392c0e935 | 350 | ShowTime(seconds, tzOffsetHr, tzOffsetMin); |
WiredHome | 3:2b4fe31e8d15 | 351 | pc.printf(" Enter the time MM/DD/YYYY HH:MM:SS\r\n"); |
WiredHome | 1:586392c0e935 | 352 | while (1) { |
WiredHome | 3:2b4fe31e8d15 | 353 | int _m, _d, _y, _H, _M, _S; |
WiredHome | 1:586392c0e935 | 354 | printf(" > "); |
WiredHome | 3:2b4fe31e8d15 | 355 | if (GetNumber(2, &_m)) { |
WiredHome | 1:586392c0e935 | 356 | pc.putc('/'); |
WiredHome | 3:2b4fe31e8d15 | 357 | if (!GetNumber(2, &_d)) |
WiredHome | 1:586392c0e935 | 358 | continue; |
WiredHome | 1:586392c0e935 | 359 | pc.putc('/'); |
WiredHome | 3:2b4fe31e8d15 | 360 | if (!GetNumber(4, &_y)) |
WiredHome | 1:586392c0e935 | 361 | continue; |
WiredHome | 3:2b4fe31e8d15 | 362 | t->tm_mon = _m - 1; |
WiredHome | 3:2b4fe31e8d15 | 363 | t->tm_mday = _d; |
WiredHome | 3:2b4fe31e8d15 | 364 | t->tm_year = _y - 1900; |
WiredHome | 1:586392c0e935 | 365 | } else { |
WiredHome | 1:586392c0e935 | 366 | pc.printf("%02d/%02d/%04d", t->tm_mon+1, t->tm_mday, t->tm_year+1900); |
WiredHome | 1:586392c0e935 | 367 | } |
WiredHome | 1:586392c0e935 | 368 | pc.putc(' '); |
WiredHome | 3:2b4fe31e8d15 | 369 | if (GetNumber(2, &_H)) { |
WiredHome | 3:2b4fe31e8d15 | 370 | pc.putc(':'); |
WiredHome | 3:2b4fe31e8d15 | 371 | if (!GetNumber(2, &_M)) |
WiredHome | 3:2b4fe31e8d15 | 372 | continue; |
WiredHome | 3:2b4fe31e8d15 | 373 | pc.putc(':'); |
WiredHome | 3:2b4fe31e8d15 | 374 | if (!GetNumber(2, &_S)) |
WiredHome | 3:2b4fe31e8d15 | 375 | continue; |
WiredHome | 3:2b4fe31e8d15 | 376 | t->tm_hour = _H; |
WiredHome | 3:2b4fe31e8d15 | 377 | t->tm_min = _M; |
WiredHome | 3:2b4fe31e8d15 | 378 | t->tm_sec = _S; |
WiredHome | 3:2b4fe31e8d15 | 379 | pc.printf("\r\n"); |
WiredHome | 3:2b4fe31e8d15 | 380 | pc.printf("%02d/%02d/%04d ", t->tm_mon+1, t->tm_mday, t->tm_year+1900); |
WiredHome | 3:2b4fe31e8d15 | 381 | pc.printf("%02d:%02d:%02d\r\n", t->tm_hour, t->tm_min, t->tm_sec); |
WiredHome | 3:2b4fe31e8d15 | 382 | // convert to timestamp and display (1256729737) |
WiredHome | 3:2b4fe31e8d15 | 383 | time_t seconds = mktime(t); |
WiredHome | 3:2b4fe31e8d15 | 384 | seconds = seconds - (tzOffsetHr * 3600 + tzOffsetMin * 60); |
WiredHome | 3:2b4fe31e8d15 | 385 | set_time(seconds); |
WiredHome | 3:2b4fe31e8d15 | 386 | break; |
WiredHome | 3:2b4fe31e8d15 | 387 | } else { |
WiredHome | 1:586392c0e935 | 388 | pc.printf("%02d:%02d:%02d\r\n", t->tm_hour, t->tm_min, t->tm_sec); |
WiredHome | 1:586392c0e935 | 389 | break; // they can bail here |
WiredHome | 1:586392c0e935 | 390 | } |
WiredHome | 1:586392c0e935 | 391 | } |
WiredHome | 1:586392c0e935 | 392 | for (i=0; i<5; i++) { |
WiredHome | 1:586392c0e935 | 393 | ShowTime(tzOffsetHr, tzOffsetMin); |
WiredHome | 1:586392c0e935 | 394 | wait(1.0); |
WiredHome | 1:586392c0e935 | 395 | } |
WiredHome | 1:586392c0e935 | 396 | } |
WiredHome | 1:586392c0e935 | 397 | |
WiredHome | 1:586392c0e935 | 398 | |
WiredHome | 0:5db287f0060b | 399 | /// Ethernet_Tests will attempt to test the Ethernet interface |
WiredHome | 0:5db287f0060b | 400 | /// |
WiredHome | 0:5db287f0060b | 401 | /// It will connect to the network - if possible, then it will |
WiredHome | 0:5db287f0060b | 402 | /// try to connect to a network time server and set the clock, |
WiredHome | 0:5db287f0060b | 403 | /// using hard coded time server and time zone offset values. |
WiredHome | 0:5db287f0060b | 404 | /// |
WiredHome | 0:5db287f0060b | 405 | /// It appears that the Ethernet interface cannot be instantiated, |
WiredHome | 0:5db287f0060b | 406 | /// destroyed, and later instantiated again (it would reliably "hang"). |
WiredHome | 0:5db287f0060b | 407 | /// So, this test is "runonce" protected. |
WiredHome | 0:5db287f0060b | 408 | /// |
WiredHome | 0:5db287f0060b | 409 | void Ethernet_Tests(void) { |
WiredHome | 0:5db287f0060b | 410 | EthernetNetIf eth; |
WiredHome | 0:5db287f0060b | 411 | NTPClient ntp; |
WiredHome | 0:5db287f0060b | 412 | static bool runonce = true; |
WiredHome | 0:5db287f0060b | 413 | |
WiredHome | 0:5db287f0060b | 414 | printf("Ethernet Test:\r\n"); |
WiredHome | 0:5db287f0060b | 415 | if (runonce) { |
WiredHome | 0:5db287f0060b | 416 | EthernetErr ethErr = eth.setup(); |
WiredHome | 0:5db287f0060b | 417 | if (ethErr) { |
WiredHome | 0:5db287f0060b | 418 | printf("Error %d in setup.\r\n", ethErr); |
WiredHome | 0:5db287f0060b | 419 | return; |
WiredHome | 0:5db287f0060b | 420 | } |
WiredHome | 0:5db287f0060b | 421 | printf(" Ethernet Setup OK\r\n"); |
WiredHome | 0:5db287f0060b | 422 | ShowTime(0, tzOffsetHr, tzOffsetMin); |
WiredHome | 0:5db287f0060b | 423 | printf(" Setting clock to %s\r\n", TicTocServer); |
WiredHome | 0:5db287f0060b | 424 | Host server(IpAddr(), 123, TicTocServer); |
WiredHome | 0:5db287f0060b | 425 | ntp.setTime(server); |
WiredHome | 0:5db287f0060b | 426 | printf(" Clock was set.\r\n"); |
WiredHome | 0:5db287f0060b | 427 | wait(1.0); |
WiredHome | 0:5db287f0060b | 428 | ShowTime(0, tzOffsetHr, tzOffsetMin); |
WiredHome | 0:5db287f0060b | 429 | runonce = false; |
WiredHome | 0:5db287f0060b | 430 | } else { |
WiredHome | 0:5db287f0060b | 431 | printf(" only runs once per cold-boot.\r\n"); |
WiredHome | 0:5db287f0060b | 432 | } |
WiredHome | 0:5db287f0060b | 433 | } |
WiredHome | 0:5db287f0060b | 434 | |
WiredHome | 1:586392c0e935 | 435 | /// isPrint tests the passed in character for being 'printable' |
WiredHome | 1:586392c0e935 | 436 | /// |
WiredHome | 1:586392c0e935 | 437 | /// It will evaluate the character on a simple ASCII range of |
WiredHome | 1:586392c0e935 | 438 | /// characters 0 to 127. |
WiredHome | 1:586392c0e935 | 439 | /// |
WiredHome | 1:586392c0e935 | 440 | /// @param i is the character to test |
WiredHome | 1:586392c0e935 | 441 | /// @returns true if the character is in the printable set (including <space>) |
WiredHome | 1:586392c0e935 | 442 | /// @returns false if the character is not printable (may be control code or extended character) |
WiredHome | 1:586392c0e935 | 443 | /// |
WiredHome | 1:586392c0e935 | 444 | bool isPrint(char i) { |
WiredHome | 1:586392c0e935 | 445 | if (i >= ' ' && i <= '~') |
WiredHome | 1:586392c0e935 | 446 | return true; |
WiredHome | 1:586392c0e935 | 447 | else |
WiredHome | 1:586392c0e935 | 448 | return false; |
WiredHome | 1:586392c0e935 | 449 | } |
WiredHome | 1:586392c0e935 | 450 | |
WiredHome | 0:5db287f0060b | 451 | /// MicroSD_Tests attempts to access and write a file on the micro SD card |
WiredHome | 0:5db287f0060b | 452 | /// |
WiredHome | 0:5db287f0060b | 453 | /// It will mount the file system, then attempt to write a simple |
WiredHome | 0:5db287f0060b | 454 | /// file on the micro SD card. |
WiredHome | 0:5db287f0060b | 455 | /// |
WiredHome | 0:5db287f0060b | 456 | void MicroSD_Tests(void) { |
WiredHome | 1:586392c0e935 | 457 | SDFileSystem sd(p5, p6, p7, p8, "ud"); // the pinout on the mbed Cool Components workshop board |
WiredHome | 1:586392c0e935 | 458 | DigitalIn cardIn(p11); |
WiredHome | 0:5db287f0060b | 459 | FILE *fp; |
WiredHome | 0:5db287f0060b | 460 | char buffer[50]; |
WiredHome | 0:5db287f0060b | 461 | |
WiredHome | 0:5db287f0060b | 462 | printf("SD File System Tests:\r\n"); |
WiredHome | 1:586392c0e935 | 463 | printf(" Card Detection: %s\r\n", (cardIn) ? "in" : "out"); |
WiredHome | 1:586392c0e935 | 464 | if (cardIn) { |
WiredHome | 1:586392c0e935 | 465 | mkdir("/ud/testDir", 0777); |
WiredHome | 1:586392c0e935 | 466 | fp = fopen("/ud/testDir/sdtest.txt", "w"); |
WiredHome | 1:586392c0e935 | 467 | if (fp == NULL) { |
WiredHome | 1:586392c0e935 | 468 | printf(" Could not open file for write\r\n"); |
WiredHome | 1:586392c0e935 | 469 | } else { |
WiredHome | 1:586392c0e935 | 470 | fprintf(fp, "Write a message to the micro SD card!\n"); |
WiredHome | 1:586392c0e935 | 471 | fclose(fp); |
WiredHome | 1:586392c0e935 | 472 | printf(" Closed file.\r\n"); |
WiredHome | 1:586392c0e935 | 473 | fp = fopen("/ud/testDir/sdtest.txt", "r"); |
WiredHome | 1:586392c0e935 | 474 | if (fp) { |
WiredHome | 1:586392c0e935 | 475 | printf(" Reading file back.\r\n"); |
WiredHome | 1:586392c0e935 | 476 | if (fgets(buffer, sizeof(buffer), fp)) { |
WiredHome | 1:586392c0e935 | 477 | while (strlen(buffer) > 0 && !isPrint(buffer[strlen(buffer)-1])) |
WiredHome | 1:586392c0e935 | 478 | buffer[strlen(buffer)-1] = '\0'; // chomp the <LF> |
WiredHome | 1:586392c0e935 | 479 | printf(" Read: {%s}\r\n", buffer); |
WiredHome | 1:586392c0e935 | 480 | } |
WiredHome | 1:586392c0e935 | 481 | fclose(fp); |
WiredHome | 0:5db287f0060b | 482 | } |
WiredHome | 0:5db287f0060b | 483 | } |
WiredHome | 0:5db287f0060b | 484 | } |
WiredHome | 0:5db287f0060b | 485 | printf(" test complete!\r\n"); |
WiredHome | 0:5db287f0060b | 486 | } |
WiredHome | 0:5db287f0060b | 487 | |
WiredHome | 0:5db287f0060b | 488 | |
WiredHome | 0:5db287f0060b | 489 | /// USBHost_Tests attempts to access and write a file on USB stick |
WiredHome | 0:5db287f0060b | 490 | /// |
WiredHome | 0:5db287f0060b | 491 | /// It will mount the file system, then attempt to write a simple |
WiredHome | 0:5db287f0060b | 492 | /// file on the USB interface. |
WiredHome | 0:5db287f0060b | 493 | /// |
WiredHome | 0:5db287f0060b | 494 | void USBHost_Tests(void) { |
WiredHome | 0:5db287f0060b | 495 | MSCFileSystem fs ("fs"); |
WiredHome | 0:5db287f0060b | 496 | FILE *fp; |
WiredHome | 0:5db287f0060b | 497 | char buffer[50]; |
WiredHome | 0:5db287f0060b | 498 | |
WiredHome | 0:5db287f0060b | 499 | printf("USB Host Tests: [installed memory stick required]\r\n"); |
WiredHome | 0:5db287f0060b | 500 | fp = fopen("/fs/hello.txt","w"); |
WiredHome | 0:5db287f0060b | 501 | if (fp) { |
WiredHome | 0:5db287f0060b | 502 | printf(" Writing to hello.txt file\r\n"); |
WiredHome | 0:5db287f0060b | 503 | fprintf(fp,"Hello world!\r\n"); |
WiredHome | 0:5db287f0060b | 504 | fclose (fp); |
WiredHome | 0:5db287f0060b | 505 | printf(" Closed file.\r\n"); |
WiredHome | 0:5db287f0060b | 506 | fp = fopen("/fs/hello.txt", "r"); |
WiredHome | 0:5db287f0060b | 507 | if (fp) { |
WiredHome | 0:5db287f0060b | 508 | printf(" Reading file back.\r\n"); |
WiredHome | 0:5db287f0060b | 509 | if (fgets(buffer, sizeof(buffer), fp)) { |
WiredHome | 0:5db287f0060b | 510 | if (strlen(buffer) > 2) |
WiredHome | 0:5db287f0060b | 511 | buffer[strlen(buffer)-2] = '\0'; // chomp the <LF> |
WiredHome | 0:5db287f0060b | 512 | printf(" Read: {%s}\r\n", buffer); |
WiredHome | 0:5db287f0060b | 513 | } |
WiredHome | 0:5db287f0060b | 514 | fclose(fp); |
WiredHome | 0:5db287f0060b | 515 | } |
WiredHome | 0:5db287f0060b | 516 | } |
WiredHome | 0:5db287f0060b | 517 | } |
WiredHome | 0:5db287f0060b | 518 | |
WiredHome | 1:586392c0e935 | 519 | |
WiredHome | 1:586392c0e935 | 520 | /// FormatMicroseconds will format a number of microseconds int ascii seconds. |
WiredHome | 1:586392c0e935 | 521 | /// |
WiredHome | 1:586392c0e935 | 522 | /// It will support formatting the number with decimal and thousands |
WiredHome | 1:586392c0e935 | 523 | /// separators. |
WiredHome | 1:586392c0e935 | 524 | /// |
WiredHome | 1:586392c0e935 | 525 | /// @param value to format |
WiredHome | 1:586392c0e935 | 526 | /// @returns the formatted string |
WiredHome | 1:586392c0e935 | 527 | /// |
WiredHome | 1:586392c0e935 | 528 | char * FormatMicroseconds(int value) { |
WiredHome | 1:586392c0e935 | 529 | static char result[15] = ""; |
WiredHome | 1:586392c0e935 | 530 | int uSec = value % 1000000; |
WiredHome | 1:586392c0e935 | 531 | int Sec = value / 1000000; |
WiredHome | 3:2b4fe31e8d15 | 532 | |
WiredHome | 1:586392c0e935 | 533 | if (Sec > 1000) |
WiredHome | 1:586392c0e935 | 534 | sprintf(result, "%3i,%3i.%06i", Sec/1000, Sec%1000, uSec); |
WiredHome | 1:586392c0e935 | 535 | else |
WiredHome | 1:586392c0e935 | 536 | sprintf(result, "%7i.%06i", Sec, uSec); |
WiredHome | 1:586392c0e935 | 537 | return result; |
WiredHome | 1:586392c0e935 | 538 | } |
WiredHome | 1:586392c0e935 | 539 | |
WiredHome | 1:586392c0e935 | 540 | |
WiredHome | 1:586392c0e935 | 541 | /// ShowCANMessage will print to the console as specified |
WiredHome | 1:586392c0e935 | 542 | /// |
WiredHome | 1:586392c0e935 | 543 | /// This format is used in other tools, and is not explained |
WiredHome | 1:586392c0e935 | 544 | /// here except in the code. |
WiredHome | 1:586392c0e935 | 545 | /// |
WiredHome | 1:586392c0e935 | 546 | /// @param tx indicates it is a transmit message when non-zero, receive otherwise |
WiredHome | 1:586392c0e935 | 547 | /// @param ch is the communication channel of this message |
WiredHome | 1:586392c0e935 | 548 | /// @msg is the CAN message to be shown |
WiredHome | 1:586392c0e935 | 549 | /// @uSec is the timestamp in microseconds |
WiredHome | 1:586392c0e935 | 550 | /// @returns nothing |
WiredHome | 1:586392c0e935 | 551 | /// |
WiredHome | 1:586392c0e935 | 552 | void ShowCANMessage(int tx, int ch, CANMessage msg, int uSec) { |
WiredHome | 1:586392c0e935 | 553 | pc.printf("%c %s %d %08X %02X ", (tx) ? 't' : 'r', (msg.format == CANExtended) ? "xtd" : "nrm", ch, msg.id, msg.len); |
WiredHome | 1:586392c0e935 | 554 | for (int d=0; d<8; d++) { |
WiredHome | 1:586392c0e935 | 555 | if (d < msg.len) |
WiredHome | 1:586392c0e935 | 556 | pc.printf("%02X ", msg.data[d]); |
WiredHome | 1:586392c0e935 | 557 | else |
WiredHome | 1:586392c0e935 | 558 | pc.printf(" "); |
WiredHome | 1:586392c0e935 | 559 | } |
WiredHome | 1:586392c0e935 | 560 | pc.printf("0 0 %s\r\n", FormatMicroseconds(uSec)); |
WiredHome | 1:586392c0e935 | 561 | } |
WiredHome | 1:586392c0e935 | 562 | |
WiredHome | 1:586392c0e935 | 563 | |
WiredHome | 0:5db287f0060b | 564 | /// CAN_Tests will send some packets on one CAN port and expect them on the other |
WiredHome | 0:5db287f0060b | 565 | /// |
WiredHome | 0:5db287f0060b | 566 | /// It will attempt to send 10 messages on one port and expect that |
WiredHome | 0:5db287f0060b | 567 | /// all 10 messages were received on the other port. The two ports should |
WiredHome | 0:5db287f0060b | 568 | /// be wired from one to the other with a loop-back cable and a termination |
WiredHome | 0:5db287f0060b | 569 | /// resistor. |
WiredHome | 0:5db287f0060b | 570 | /// |
WiredHome | 0:5db287f0060b | 571 | void CAN_Tests(void) { |
WiredHome | 0:5db287f0060b | 572 | CAN can1(p9, p10); |
WiredHome | 0:5db287f0060b | 573 | CAN can2(p30, p29); |
WiredHome | 0:5db287f0060b | 574 | char Txcounter = 0; |
WiredHome | 0:5db287f0060b | 575 | char Rxcounter = 0; |
WiredHome | 1:586392c0e935 | 576 | CANMessage tMsg; |
WiredHome | 1:586392c0e935 | 577 | CANMessage rMsg; |
WiredHome | 1:586392c0e935 | 578 | Timer timer; |
WiredHome | 0:5db287f0060b | 579 | int i; |
WiredHome | 0:5db287f0060b | 580 | |
WiredHome | 1:586392c0e935 | 581 | pc.printf("CAN Tests:\r\n"); |
WiredHome | 1:586392c0e935 | 582 | i = can1.frequency(250000); |
WiredHome | 1:586392c0e935 | 583 | pc.printf(" can1 frequency set: %i\r\n", i); |
WiredHome | 1:586392c0e935 | 584 | i = can2.frequency(250000); |
WiredHome | 1:586392c0e935 | 585 | pc.printf(" can2 frequency set: %i\r\n", i); |
WiredHome | 1:586392c0e935 | 586 | timer.start(); |
WiredHome | 1:586392c0e935 | 587 | for (i=0; i<25; i++) { // gets a few more passes to receive the messages |
WiredHome | 1:586392c0e935 | 588 | |
WiredHome | 1:586392c0e935 | 589 | for (int x=0; x<8; x++) |
WiredHome | 1:586392c0e935 | 590 | tMsg.data[x] = (char)(rand()); |
WiredHome | 1:586392c0e935 | 591 | tMsg.data[0] = Txcounter; |
WiredHome | 1:586392c0e935 | 592 | tMsg.id = rand(); |
WiredHome | 1:586392c0e935 | 593 | tMsg.len = rand() % 9; |
WiredHome | 1:586392c0e935 | 594 | tMsg.type = CANData; |
WiredHome | 1:586392c0e935 | 595 | tMsg.format = (rand() & 1) ? CANExtended : CANStandard; |
WiredHome | 1:586392c0e935 | 596 | |
WiredHome | 1:586392c0e935 | 597 | if (Txcounter < 10 && can1.write(tMsg)) { |
WiredHome | 1:586392c0e935 | 598 | pc.printf(" "); |
WiredHome | 1:586392c0e935 | 599 | ShowCANMessage(1, 1, tMsg, timer.read_us()); |
WiredHome | 0:5db287f0060b | 600 | Txcounter++; |
WiredHome | 1:586392c0e935 | 601 | //wait(0.05); |
WiredHome | 0:5db287f0060b | 602 | } |
WiredHome | 1:586392c0e935 | 603 | if (can2.read(rMsg)) { |
WiredHome | 0:5db287f0060b | 604 | Rxcounter++; |
WiredHome | 1:586392c0e935 | 605 | pc.printf(" "); |
WiredHome | 1:586392c0e935 | 606 | ShowCANMessage(0, 2, rMsg, timer.read_us()); |
WiredHome | 0:5db287f0060b | 607 | } |
WiredHome | 1:586392c0e935 | 608 | wait(0.005); |
WiredHome | 0:5db287f0060b | 609 | } |
WiredHome | 0:5db287f0060b | 610 | if (Txcounter == Rxcounter) |
WiredHome | 0:5db287f0060b | 611 | printf(" passed.\r\n"); |
WiredHome | 0:5db287f0060b | 612 | else |
WiredHome | 0:5db287f0060b | 613 | printf(" **** Txcounter (%d) != Rxcounter (%d) ****\r\n", Txcounter, Rxcounter); |
WiredHome | 0:5db287f0060b | 614 | } |
WiredHome | 0:5db287f0060b | 615 | |
WiredHome | 0:5db287f0060b | 616 | /// RS_232_Tests will say hello on each of the RS-232 channels |
WiredHome | 0:5db287f0060b | 617 | /// |
WiredHome | 0:5db287f0060b | 618 | /// It will print a hello text string out each of the ports. |
WiredHome | 0:5db287f0060b | 619 | /// |
WiredHome | 0:5db287f0060b | 620 | void RS_232_Tests(void) { |
WiredHome | 0:5db287f0060b | 621 | Serial s1(p13, p14); |
WiredHome | 0:5db287f0060b | 622 | Serial s2(p28, p27); |
WiredHome | 0:5db287f0060b | 623 | |
WiredHome | 0:5db287f0060b | 624 | pc.printf("RS-232 Tests:\r\n"); |
WiredHome | 0:5db287f0060b | 625 | s1.printf(" Hello going out S1\r\n"); |
WiredHome | 0:5db287f0060b | 626 | s2.printf(" Hello going out S2\r\n"); |
WiredHome | 0:5db287f0060b | 627 | pc.printf(" end tests.\r\n"); |
WiredHome | 0:5db287f0060b | 628 | } |