Guest User

Untitled

a guest
May 7th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.28 KB | None | 0 0
  1. /*
  2. Interactive Hardware Test Session for Maple
  3.  
  4. Connect via Serial2 (header pins D0 and D1 on the Maple) and press
  5. 'h' for a list of options.
  6.  
  7. To use this in a sketch, remove all the #includes and the function prototypes.
  8.  
  9. Created 22 April 2010
  10. By Bryan Newbold for LeafLabs
  11. This code is released with no strings attached.
  12.  
  13. */
  14.  
  15. #include "wiring.h"
  16. #include "HardwareSerial.h"
  17. #include "HardwareUsb.h"
  18. #include <math.h>
  19. #include "usb.h"
  20.  
  21. #define LED_PIN 13
  22. #define PWM_PIN 2
  23.  
  24. HardwareUsb Usb;
  25.  
  26. uint8 input = 0;
  27. uint8 tiddle = 0;
  28. int toggle = 0;
  29. int rate = 0;
  30. int sample = 0;
  31.  
  32. // read these off maple board rev3
  33. const uint8 pwm_pins[] = {0,1,2,3,5,6,7,8,9,11,12,14,24,25,27,28};
  34. // note that 38 is just a button and 39+ aren't functional as of 04/22/2010
  35. const uint8 adc_pins[] = {0,1,2,10,11,12,13,15,16,17,18,19,20,27,28};
  36. #define NUM_GPIO 44 // 43 is the MAX
  37. uint8 gpio_state[NUM_GPIO];
  38.  
  39. #define DUMMY_DAT "qwertyuiopasdfghjklzxcvbnmmmmmm,./1234567890-=qwertyuiopasdfghjklzxcvbnm,./1234567890"
  40.  
  41. void print_help(void);
  42. void do_noise(uint8 pin);
  43. void do_everything(void);
  44. void do_fast_gpio(void);
  45.  
  46. void setup() {
  47. /* Set up the LED to blink */
  48. pinMode(LED_PIN, OUTPUT);
  49.  
  50. /* Send a message out over USART2 */
  51. //Serial2.begin(115200); // 9600 is more compatible
  52. Serial2.begin(9600);
  53. Serial2.println("");
  54. Serial2.println(" __ __ _ _");
  55. Serial2.println(" | \\/ | __ _ _ __ | | ___| |");
  56. Serial2.println(" | |\\/| |/ _` | '_ \\| |/ _ \\ |");
  57. Serial2.println(" | | | | (_| | |_) | | __/_|");
  58. Serial2.println(" |_| |_|\\__,_| .__/|_|\\___(_)");
  59. Serial2.println(" |_|");
  60. Serial2.println(" by leaflabs");
  61. Serial2.println("");
  62. Serial2.println("");
  63. Serial2.println("Maple interactive test program (type '?' for help)");
  64. Serial2.println("------------------------------------------------------------");
  65. Serial2.print("> ");
  66.  
  67. /* Send a message out the USB virtual com port */
  68. // TODO: this should all be over usb as well
  69. Usb.println("Maple test program starting; use serial port for interactivity");
  70. }
  71.  
  72. void loop() {
  73. toggle ^= 1;
  74. digitalWrite(LED_PIN, toggle);
  75. delay(100);
  76.  
  77. while(Serial2.available()) {
  78. input = Serial2.read();
  79. Serial2.println(input);
  80. switch(input) {
  81. case 13: // Carriage Return
  82. break;
  83. case 32: // ' '
  84. Serial2.println("spacebar, nice!");
  85. break;
  86. case 63: // '?'
  87. case 104: // 'h'
  88. print_help();
  89. break;
  90. case 117: // 'u'
  91. Usb.println("Hello World!");
  92. break;
  93. case 119: // 'w'
  94. Serial1.println("Hello World!");
  95. Serial2.println("Hello World!");
  96. Serial3.println("Hello World!");
  97. break;
  98. case 109: // 'm'
  99. Serial2.println("Testing 57600 baud on USART1 and USART3. Press enter.");
  100. Serial1.begin(57600);
  101. Serial3.begin(57600);
  102. while(!Serial2.available()) {
  103. Serial1.println(DUMMY_DAT);
  104. Serial3.println(DUMMY_DAT);
  105. if(Serial1.available()) {
  106. Serial1.println(Serial1.read());
  107. delay(1000);
  108. }
  109. if(Serial3.available()) {
  110. Serial3.println(Serial3.read());
  111. delay(1000);
  112. }
  113. }
  114. Serial2.read();
  115. Serial2.println("Testing 115200 baud on USART1 and USART3. Press enter.");
  116. Serial1.begin(115200);
  117. Serial3.begin(115200);
  118. while(!Serial2.available()) {
  119. Serial1.println(DUMMY_DAT);
  120. Serial3.println(DUMMY_DAT);
  121. if(Serial1.available()) {
  122. Serial1.println(Serial1.read());
  123. delay(1000);
  124. }
  125. if(Serial3.available()) {
  126. Serial3.println(Serial3.read());
  127. delay(1000);
  128. }
  129. }
  130. Serial2.read();
  131. Serial2.println("Testing 9600 baud on USART1 and USART3. Press enter.");
  132. Serial1.begin(9600);
  133. Serial3.begin(9600);
  134. while(!Serial2.available()) {
  135. Serial1.println(DUMMY_DAT);
  136. Serial3.println(DUMMY_DAT);
  137. if(Serial1.available()) {
  138. Serial1.println(Serial1.read());
  139. delay(1000);
  140. }
  141. if(Serial3.available()) {
  142. Serial3.println(Serial3.read());
  143. delay(1000);
  144. }
  145. }
  146. Serial2.read();
  147. Serial2.println("Resetting USART1 and USART3...");
  148. Serial1.begin(9600);
  149. Serial3.begin(9600);
  150. break;
  151. case 46: // '.'
  152. while(!Serial2.available()) {
  153. Serial2.print(".");
  154. Usb.print(".");
  155. }
  156. //Serial2.flush();
  157. break;
  158. case 110: // 'n'
  159. Serial2.println("Taking ADC noise stats...");
  160. // turn off LED
  161. digitalWrite(LED_PIN, 0);
  162. // make sure to skip the TX/RX headers
  163. for(uint32 i = 2; i<sizeof(adc_pins); i++) {
  164. delay(5);
  165. do_noise(adc_pins[i]);
  166. }
  167. break;
  168. case 78: // 'N'
  169. Serial2.println("Taking ADC noise stats under duress...");
  170. // turn off LED
  171. digitalWrite(LED_PIN, 0);
  172. // make sure to skip the TX/RX headers
  173. for(uint32 i = 2; i<sizeof(adc_pins); i++) {
  174. // spool up PWM
  175. for(uint32 j = 2; j<(uint32)sizeof(pwm_pins); j++) {
  176. if(adc_pins[i] != pwm_pins[j]) {
  177. pinMode(pwm_pins[j],PWM);
  178. pwmWrite(pwm_pins[j], 1000 + i);
  179. }
  180. }
  181. Usb.print(DUMMY_DAT);
  182. Usb.print(DUMMY_DAT);
  183. do_noise(adc_pins[i]);
  184. for(uint32 j = 2; j<(uint32)sizeof(pwm_pins); j++) {
  185. if(adc_pins[i] != pwm_pins[j]) {
  186. pinMode(pwm_pins[j],OUTPUT);
  187. digitalWrite(pwm_pins[j],0);
  188. }
  189. }
  190. }
  191. break;
  192. case 101: // 'e'
  193. do_everything();
  194. break;
  195. case 87: // 'W'
  196. while(!Serial2.available()) {
  197. Serial1.print(DUMMY_DAT);
  198. Serial2.print(DUMMY_DAT);
  199. Serial3.print(DUMMY_DAT);
  200. }
  201. break;
  202. case 85: // 'U'
  203. Serial2.println("Dumping data to USB. Press enter.");
  204. while(!Serial2.available()) {
  205. Usb.print(DUMMY_DAT);
  206. }
  207. break;
  208. case 103: // 'g'
  209. Serial2.print("Sequentially testing GPIO write on all possible headers except D0 and D1.");
  210. Serial2.println("Anything for next, ESC to stop.");
  211. // turn off LED
  212. digitalWrite(LED_PIN, 0);
  213. // make sure to skip the TX/RX headers
  214. for(uint32 i = 2; i<NUM_GPIO; i++) {
  215. Serial2.print("GPIO write out on header D");
  216. Serial2.print(i, DEC);
  217. Serial2.println("...");
  218. pinMode(i, OUTPUT);
  219. digitalWrite(i, tiddle);
  220. while(!Serial2.available()) {
  221. tiddle ^= 1;
  222. digitalWrite(i, tiddle);
  223. }
  224. digitalWrite(i, 0);
  225. if((uint8)Serial2.read() == (uint8)27) break; // ESC
  226. }
  227. break;
  228. case 71: // 'G'
  229. Serial2.println("Flipping all GPIOs at the same time. Press enter.");
  230. // turn off LED
  231. digitalWrite(LED_PIN, 0);
  232. // make sure to skip the TX/RX headers
  233. for(uint32 i = 2; i<NUM_GPIO; i++) {
  234. pinMode(i, OUTPUT);
  235. }
  236. while(!Serial2.available()) {
  237. tiddle ^= 1;
  238. for(uint32 i = 2; i<NUM_GPIO; i++) {
  239. digitalWrite(i, tiddle);
  240. }
  241. }
  242. for(uint32 i = 2; i<NUM_GPIO; i++) {
  243. digitalWrite(i, 0);
  244. }
  245. if((uint8)Serial2.read() == (uint8)27) break; // ESC
  246. break;
  247. case 102: // 'f'
  248. Serial2.println("Wiggling GPIO header D4 as fast as possible in bursts. Press enter.");
  249. pinMode(4,OUTPUT);
  250. while(!Serial2.available()) {
  251. do_fast_gpio();
  252. delay(1);
  253. }
  254. break;
  255. case 112: // 'p'
  256. Serial2.println("Sequentially testing PWM on all possible headers except D0 and D1. ");
  257. Serial2.println("Anything for next, ESC to stop.");
  258. // turn off LED
  259. digitalWrite(LED_PIN, 0);
  260. // make sure to skip the TX/RX headers
  261. for(uint32 i = 2; i<sizeof(pwm_pins); i++) {
  262. Serial2.print("PWM out on header D");
  263. Serial2.print(pwm_pins[i], DEC);
  264. Serial2.println("...");
  265. pinMode(pwm_pins[i], PWM);
  266. pwmWrite(pwm_pins[i], 16000);
  267. while(!Serial2.available()) { delay(10); }
  268. pinMode(pwm_pins[i], OUTPUT);
  269. digitalWrite(pwm_pins[i], 0);
  270. if((uint8)Serial2.read() == (uint8)27) break; // ESC
  271. }
  272. break;
  273. case 80: // 'P'
  274. Serial2.println("Testing all PWM ports with a sweep. Press enter.");
  275. // turn off LED
  276. digitalWrite(LED_PIN, 0);
  277. // make sure to skip the TX/RX pins
  278. for(uint32 i = 2; i<sizeof(pwm_pins); i++) {
  279. pinMode(pwm_pins[i], PWM);
  280. pwmWrite(pwm_pins[i], 4000);
  281. }
  282. while(!Serial2.available()) {
  283. rate += 20;
  284. if(rate > 65500) rate = 0;
  285. for(uint32 i = 2; i<sizeof(pwm_pins); i++) {
  286. pwmWrite(pwm_pins[i], rate);
  287. }
  288. delay(1);
  289. }
  290. for(uint32 i = 2; i<sizeof(pwm_pins); i++) {
  291. pinMode(pwm_pins[i], OUTPUT);
  292. }
  293. break;
  294. case 95: // '_'
  295. Serial2.println("Delaying for 5 seconds...");
  296. delay(5000);
  297. break;
  298. case 116: // 't'
  299. break;
  300. case 84: // 'T'
  301. break;
  302. case 115: // 's'
  303. Serial2.println("Testing all PWM headers with a servo sweep. Press enter.");
  304. Serial2.println("");
  305. // turn off LED
  306. digitalWrite(LED_PIN, 0);
  307. timer_init(1, 21);
  308. timer_init(2, 21);
  309. timer_init(3, 21);
  310. timer_init(4, 21);
  311. // make sure to skip the TX/RX headers
  312. for(uint32 i = 2; i<sizeof(pwm_pins); i++) {
  313. pinMode(pwm_pins[i], PWM);
  314. pwmWrite(pwm_pins[i], 4000);
  315. }
  316. // 1.25ms = 4096counts = 0deg
  317. // 1.50ms = 4915counts = 90deg
  318. // 1.75ms = 5734counts = 180deg
  319. rate = 4096;
  320. while(!Serial2.available()) {
  321. rate += 20;
  322. if(rate > 5734) rate = 4096;
  323. for(uint32 i = 2; i<sizeof(pwm_pins); i++) {
  324. pwmWrite(pwm_pins[i], rate);
  325. }
  326. delay(20);
  327. }
  328. for(uint32 i = 2; i<sizeof(pwm_pins); i++) {
  329. pinMode(pwm_pins[i], OUTPUT);
  330. }
  331. timer_init(1, 1);
  332. timer_init(2, 1);
  333. timer_init(3, 1);
  334. timer_init(4, 1);
  335. Serial2.begin(9600);
  336. Serial2.println("(reset serial port)");
  337. break;
  338. case 100: // 'd'
  339. Serial2.println("Pulling down D4, D22");
  340. pinMode(22,INPUT_PULLDOWN);
  341. pinMode(4,INPUT_PULLDOWN);
  342. while(!Serial2.available()) {
  343. delay(1);
  344. }
  345. Serial2.read();
  346. Serial2.println("Pulling up D4, D22");
  347. pinMode(22,INPUT_PULLUP);
  348. pinMode(4,INPUT_PULLUP);
  349. while(!Serial2.available()) {
  350. delay(1);
  351. }
  352. Serial2.read();
  353. pinMode(4,OUTPUT);
  354. break;
  355. case 105: // 'i'
  356. break;
  357. case 73: // 'I'
  358. break;
  359. case 114: // 'r'
  360. Serial2.println("Monitoring GPIO read state changes. Press enter.");
  361. // turn off LED
  362. digitalWrite(LED_PIN, 0);
  363. // make sure to skip the TX/RX headers
  364. for(int i = 2; i<NUM_GPIO; i++) {
  365. pinMode(i, INPUT);
  366. gpio_state[i] = (uint8)digitalRead(i);
  367. }
  368. while(!Serial2.available()) {
  369. for(int i = 2; i<NUM_GPIO; i++) {
  370. tiddle = (uint8)digitalRead(i);
  371. if(tiddle != gpio_state[i]) {
  372. Serial2.print("State change on header D");
  373. Serial2.print(i,DEC);
  374. if(tiddle) Serial2.println(":\tHIGH");
  375. else Serial2.println(":\tLOW");
  376. gpio_state[i] = tiddle;
  377. }
  378. }
  379. }
  380. for(int i = 2; i<NUM_GPIO; i++) {
  381. pinMode(i, OUTPUT);
  382. }
  383. break;
  384. case 97: // 'a'
  385. Serial2.print("Sequentially reading each ADC port.");
  386. Serial2.println("Anything for next, ESC to stop.");
  387. // turn off LED
  388. digitalWrite(LED_PIN, 0);
  389. // make sure to skip the TX/RX headers
  390. for(uint32 i = 2; i<sizeof(adc_pins); i++) {
  391. Serial2.print("Reading on header D");
  392. Serial2.print(adc_pins[i], DEC);
  393. Serial2.println("...");
  394. pinMode(adc_pins[i], INPUT_ANALOG);
  395. while(!Serial2.available()) {
  396. sample = analogRead(adc_pins[i]);
  397. Serial2.print(adc_pins[i],DEC);
  398. Serial2.print("\t");
  399. Serial2.print(sample,DEC);
  400. Serial2.print("\t");
  401. Serial2.print("|");
  402. for(int j = 0; j<4096; j+= 100) {
  403. if(sample >= j) Serial2.print("#");
  404. else Serial2.print(" ");
  405. }
  406. Serial2.print("| ");
  407. for(int j = 0; j<12; j++) {
  408. if(sample & (1 << (11-j))) Serial2.print("1");
  409. else Serial2.print("0");
  410. }
  411. Serial2.println("");
  412. }
  413. pinMode(adc_pins[i], OUTPUT);
  414. digitalWrite(adc_pins[i], 0);
  415. if((uint8)Serial2.read() == (uint8)27) break; // ESC
  416. }
  417. break;
  418. default:
  419. Serial2.print("Unexpected: ");
  420. Serial2.println(input);
  421. }
  422. Serial2.print("> ");
  423. }
  424. }
  425.  
  426. void print_help(void) {
  427. Serial2.println("");
  428. //Serial2.println("Command Listing\t(# means any digit)");
  429. Serial2.println("Command Listing");
  430. Serial2.println("\t?: print this menu");
  431. Serial2.println("\th: print this menu");
  432. Serial2.println("\tw: print Hello World on all 3 USARTS");
  433. Serial2.println("\tn: measure noise and do statistics");
  434. Serial2.println("\tN: measure noise and do statistics with background stuff");
  435. Serial2.println("\ta: show realtime ADC info");
  436. Serial2.println("\t.: echo '.' until new input");
  437. Serial2.println("\tu: print Hello World on USB");
  438. Serial2.println("\t_: try to do as little as possible for a couple seconds (delay)");
  439. Serial2.println("\tp: test all PWM channels sequentially");
  440. Serial2.println("\tW: dump data as fast as possible on all 3 USARTS");
  441. Serial2.println("\tU: dump data as fast as possible on USB");
  442. Serial2.println("\tg: toggle all GPIOs sequentialy");
  443. Serial2.println("\tG: toggle all GPIOs at the same time");
  444. Serial2.println("\tf: toggle GPIO D4 as fast as possible in bursts");
  445. Serial2.println("\tP: test all PWM channels at the same time with different speeds/sweeps");
  446. Serial2.println("\tr: read in GPIO status changes and print them in realtime");
  447. Serial2.println("\ts: output a sweeping SERVO PWM on all PWM channels");
  448. Serial2.println("\tm: output serial data dumps on USART1 and USART3 with various rates");
  449.  
  450. Serial2.println("Unimplemented:");
  451. Serial2.println("\te: do everything all at once until new input");
  452. Serial2.println("\tt: output a 1khz squarewave on all GPIOs as well as possible");
  453. Serial2.println("\tT: output a 1hz squarewave on all GPIOs as well as possible");
  454. Serial2.println("\ti: print out a bunch of info about system state");
  455. Serial2.println("\tI: print out status of all headers");
  456. }
  457.  
  458. void do_noise(uint8 pin) { // TODO
  459. uint16 data[100];
  460. float mean = 0;
  461. //float stddev = 0;
  462. float delta = 0;
  463. float M2 = 0;
  464. pinMode(pin, INPUT_ANALOG);
  465.  
  466. // variance algorithm from knuth; see wikipedia
  467. // checked against python
  468. for(int i = 0; i<100; i++) {
  469. data[i] = analogRead(pin);
  470. delta = data[i] - mean;
  471. mean = mean + delta/(i+1);
  472. M2 = M2 + delta*(data[i] - mean);
  473. }
  474.  
  475. //sqrt is broken?
  476. //stddev = sqrt(variance);
  477. Serial2.print("header: D"); Serial2.print(pin,DEC);
  478. Serial2.print("\tn: "); Serial2.print(100,DEC);
  479. Serial2.print("\tmean: "); Serial2.print(mean);
  480. Serial2.print("\tvar: "); Serial2.println(M2/99.0);
  481. pinMode(pin, OUTPUT);
  482. }
  483.  
  484. void do_everything(void) { // TODO
  485. // TODO
  486. // print to usart
  487. // print to usb
  488. // toggle gpios
  489. // enable pwm
  490. Serial2.println("(unimplemented)");
  491. }
  492.  
  493. void do_fast_gpio(void) {
  494. // header D4 is on port B and is pin 5 on the uC
  495. gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0);
  496. gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0);
  497. gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0);
  498. gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0);
  499. gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0);
  500. gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0);
  501. gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0);
  502. gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0);
  503. gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0);
  504. gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0);
  505. gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0);
  506. gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0);
  507. gpio_write_bit(GPIOB_BASE, 5, 1); gpio_write_bit(GPIOB_BASE, 5, 0);
  508. }
  509.  
  510. int main(void) {
  511. init();
  512. setup();
  513.  
  514. while (1) {
  515. loop();
  516. }
  517. return 0;
  518. }
  519.  
  520. /* Required for C++ hackery */
  521. /* TODO: This really shouldn't go here... move it later
  522. * */
  523. extern "C" void __cxa_pure_virtual(void) {
  524. while(1)
  525. ;
  526. }
Add Comment
Please, Sign In to add comment