Uranus board test

Dependencies:   USBDevice mbed

Committer:
swxu
Date:
Fri May 19 07:41:18 2017 +0000
Revision:
1:7d1fb66613d9
Parent:
0:ae1f7d2c4fbc
add some tests

Who changed what in which revision?

UserRevisionLine numberNew contents of line
swxu 0:ae1f7d2c4fbc 1 #include "mbed.h"
swxu 0:ae1f7d2c4fbc 2 #include "USBSerial.h"
swxu 0:ae1f7d2c4fbc 3
swxu 1:7d1fb66613d9 4 #include <time.h>
swxu 1:7d1fb66613d9 5
swxu 1:7d1fb66613d9 6 USBSerial vcom; // Virtual serial port over USB
swxu 0:ae1f7d2c4fbc 7 Serial uart(P0_19, P0_18);
swxu 1:7d1fb66613d9 8
swxu 1:7d1fb66613d9 9 DigitalOut led0(P0_20);
swxu 1:7d1fb66613d9 10 DigitalOut led1(P0_21);
swxu 1:7d1fb66613d9 11 DigitalOut led2(P0_11);
swxu 1:7d1fb66613d9 12
swxu 1:7d1fb66613d9 13 void led_test()
swxu 1:7d1fb66613d9 14 {
swxu 1:7d1fb66613d9 15 for (int i = 0; i < 10; i++) {
swxu 1:7d1fb66613d9 16 led0 = !led0; wait(0.33);
swxu 1:7d1fb66613d9 17 led1 = !led1; wait(0.33);
swxu 1:7d1fb66613d9 18 led2 = !led2; wait(0.33);
swxu 1:7d1fb66613d9 19 }
swxu 1:7d1fb66613d9 20 led0 = led1 = led2 = 1;
swxu 1:7d1fb66613d9 21 uart.printf("led_test done!\r\n");
swxu 1:7d1fb66613d9 22 }
swxu 1:7d1fb66613d9 23
swxu 1:7d1fb66613d9 24 DigitalIn button(P0_1, PullUp);
swxu 1:7d1fb66613d9 25 struct InterruptTest {
swxu 1:7d1fb66613d9 26 InterruptTest(PinName pin) : _ini(pin) {
swxu 1:7d1fb66613d9 27 _ini.mode(PullNone);
swxu 1:7d1fb66613d9 28 _ini.rise(callback(this, &InterruptTest::on_rise));
swxu 1:7d1fb66613d9 29 _ini.fall(callback(this, &InterruptTest::on_fall));
swxu 1:7d1fb66613d9 30 _rises = _falls = 0;
swxu 1:7d1fb66613d9 31 }
swxu 1:7d1fb66613d9 32
swxu 1:7d1fb66613d9 33 int rises() { return _rises; }
swxu 1:7d1fb66613d9 34 int falls() { return _falls; }
swxu 1:7d1fb66613d9 35
swxu 1:7d1fb66613d9 36 private:
swxu 1:7d1fb66613d9 37 void on_rise() {
swxu 1:7d1fb66613d9 38 led1 = !led1;
swxu 1:7d1fb66613d9 39 _rises++;
swxu 1:7d1fb66613d9 40 }
swxu 1:7d1fb66613d9 41
swxu 1:7d1fb66613d9 42 void on_fall() {
swxu 1:7d1fb66613d9 43 led2 = !led2;
swxu 1:7d1fb66613d9 44 _falls++;
swxu 1:7d1fb66613d9 45 }
swxu 1:7d1fb66613d9 46
swxu 1:7d1fb66613d9 47 private:
swxu 1:7d1fb66613d9 48 InterruptIn _ini;
swxu 1:7d1fb66613d9 49 int _rises, _falls;
swxu 1:7d1fb66613d9 50 };
swxu 1:7d1fb66613d9 51
swxu 1:7d1fb66613d9 52 void button_test()
swxu 1:7d1fb66613d9 53 {
swxu 1:7d1fb66613d9 54 DigitalIn btn(P0_1, PullUp);
swxu 1:7d1fb66613d9 55
swxu 1:7d1fb66613d9 56 uart.printf("button input test start(10s) %d:\r\n", clock());
swxu 1:7d1fb66613d9 57
swxu 1:7d1fb66613d9 58 int count = 0;
swxu 1:7d1fb66613d9 59 clock_t start = clock();
swxu 1:7d1fb66613d9 60 while (clock() - start <= 10*CLOCKS_PER_SEC) {
swxu 1:7d1fb66613d9 61 led2 = btn.read();
swxu 1:7d1fb66613d9 62 if (!led2) {
swxu 1:7d1fb66613d9 63 count++;
swxu 1:7d1fb66613d9 64 }
swxu 1:7d1fb66613d9 65 }
swxu 1:7d1fb66613d9 66 uart.printf("\r\n");
swxu 1:7d1fb66613d9 67 uart.printf("button input test done! count: %d, %d\r\n", count, clock());
swxu 1:7d1fb66613d9 68 }
swxu 1:7d1fb66613d9 69
swxu 1:7d1fb66613d9 70 void interrupt_test()
swxu 1:7d1fb66613d9 71 {
swxu 1:7d1fb66613d9 72 uart.printf("button interrupt test start(10s) %d:\r\n", clock());
swxu 1:7d1fb66613d9 73 InterruptTest it(P0_1);
swxu 1:7d1fb66613d9 74 wait(10);
swxu 1:7d1fb66613d9 75 uart.printf("button interrupt test done, %d\r\n", clock());
swxu 1:7d1fb66613d9 76 uart.printf("rises: %d, falls: %d\r\n", it.rises(), it.falls());
swxu 1:7d1fb66613d9 77 }
swxu 1:7d1fb66613d9 78
swxu 1:7d1fb66613d9 79 void check_button()
swxu 1:7d1fb66613d9 80 {
swxu 1:7d1fb66613d9 81 led2 = button.read();
swxu 1:7d1fb66613d9 82 }
swxu 0:ae1f7d2c4fbc 83
swxu 0:ae1f7d2c4fbc 84 int main(void) {
swxu 0:ae1f7d2c4fbc 85 uart.baud(115200);
swxu 0:ae1f7d2c4fbc 86 uart.format();
swxu 0:ae1f7d2c4fbc 87
swxu 1:7d1fb66613d9 88 uart.printf("BUILD: %s %s\r\n", __DATE__, __TIME__);
swxu 1:7d1fb66613d9 89 uart.printf("System core clock: %d\r\n", SystemCoreClock);
swxu 1:7d1fb66613d9 90
swxu 1:7d1fb66613d9 91 // led_test();
swxu 1:7d1fb66613d9 92 // button_test();
swxu 1:7d1fb66613d9 93 // interrupt_test();
swxu 1:7d1fb66613d9 94
swxu 1:7d1fb66613d9 95 Ticker ticker;
swxu 1:7d1fb66613d9 96 ticker.attach_us(check_button, 10000);
swxu 1:7d1fb66613d9 97
swxu 0:ae1f7d2c4fbc 98 while(1) {
swxu 1:7d1fb66613d9 99 // led2 = button.read();
swxu 0:ae1f7d2c4fbc 100 clock_t ts = clock();
swxu 0:ae1f7d2c4fbc 101
swxu 0:ae1f7d2c4fbc 102 uart.printf("Hello UART! %d\r\n", ts);
swxu 1:7d1fb66613d9 103 vcom.printf("I am a virtual vcom port %d\r\n", ts);
swxu 1:7d1fb66613d9 104
swxu 1:7d1fb66613d9 105 led0 = !led0; wait(1);
swxu 1:7d1fb66613d9 106 led1 = !led1; wait(1);
swxu 1:7d1fb66613d9 107 // led2 = !led2; wait(1);
swxu 0:ae1f7d2c4fbc 108 }
swxu 1:7d1fb66613d9 109 }