Wim Huiskamp
/
SC16IS750_Test
Testsoftware for SC16IS750
main.cpp@1:f0d3984f2586, 2014-02-09 (annotated)
- Committer:
- wim
- Date:
- Sun Feb 09 14:58:33 2014 +0000
- Revision:
- 1:f0d3984f2586
- Parent:
- 0:d83a90125711
- Child:
- 2:8aba07490dce
working main, tested on BOB
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 0:d83a90125711 | 1 | #include "mbed.h" |
wim | 0:d83a90125711 | 2 | #include "SC16IS750.h" |
wim | 0:d83a90125711 | 3 | |
wim | 0:d83a90125711 | 4 | //SPI Version |
wim | 0:d83a90125711 | 5 | SPI spi(PTD2, PTD3, PTD1); //MOSI, MISO, SCK |
wim | 0:d83a90125711 | 6 | //DigitalOut CS(PTD0); //CS |
wim | 1:f0d3984f2586 | 7 | SC16IS750_SPI serial_bridge(&spi, PTD0); |
wim | 0:d83a90125711 | 8 | |
wim | 0:d83a90125711 | 9 | //I2C Version |
wim | 1:f0d3984f2586 | 10 | //I2C i2c(PTE0, PTE1); //SDA, SCL |
wim | 1:f0d3984f2586 | 11 | //SC16IS750_I2C serial_bridge(&i2c, DEFAULT_SC16IS750_ADDR); |
wim | 0:d83a90125711 | 12 | |
wim | 0:d83a90125711 | 13 | DigitalOut myled1(LED_RED); |
wim | 1:f0d3984f2586 | 14 | //DigitalOut myled2(LED_GREEN); |
wim | 1:f0d3984f2586 | 15 | //DigitalOut myled3(LED_BLUE); // Same as PTD1 (SCK) |
wim | 1:f0d3984f2586 | 16 | DigitalOut heartbeatLED(LED_GREEN); |
wim | 0:d83a90125711 | 17 | |
wim | 0:d83a90125711 | 18 | Serial pc(USBTX,USBRX); |
wim | 0:d83a90125711 | 19 | |
wim | 1:f0d3984f2586 | 20 | void show_menu() { |
wim | 1:f0d3984f2586 | 21 | pc.printf("0: Exit\n\r"); |
wim | 1:f0d3984f2586 | 22 | pc.printf("1: Show Menu\n\r"); |
wim | 1:f0d3984f2586 | 23 | pc.printf("2: Init\n\r"); |
wim | 1:f0d3984f2586 | 24 | pc.printf("3: IO Out\n\r"); |
wim | 1:f0d3984f2586 | 25 | pc.printf("4: Echo Text\n\r"); |
wim | 1:f0d3984f2586 | 26 | |
wim | 1:f0d3984f2586 | 27 | #if(0) |
wim | 1:f0d3984f2586 | 28 | |
wim | 1:f0d3984f2586 | 29 | pc.printf("5: \n\r"); |
wim | 1:f0d3984f2586 | 30 | pc.printf("6: \n\r"); |
wim | 1:f0d3984f2586 | 31 | pc.printf("7: \n\r"); |
wim | 1:f0d3984f2586 | 32 | pc.printf("8: \n\r"); |
wim | 1:f0d3984f2586 | 33 | pc.printf("9: \n\r"); |
wim | 1:f0d3984f2586 | 34 | pc.printf("A: \n\r"); |
wim | 1:f0d3984f2586 | 35 | pc.printf("B: \n\r"); |
wim | 1:f0d3984f2586 | 36 | pc.printf("C: \n\r"); |
wim | 1:f0d3984f2586 | 37 | pc.printf("D: \n\r"); |
wim | 1:f0d3984f2586 | 38 | pc.printf("P: \n\r"); |
wim | 1:f0d3984f2586 | 39 | #endif |
wim | 1:f0d3984f2586 | 40 | pc.printf("\n\r"); |
wim | 1:f0d3984f2586 | 41 | } |
wim | 1:f0d3984f2586 | 42 | |
wim | 1:f0d3984f2586 | 43 | // Variables for Heartbeat and Status monitoring |
wim | 1:f0d3984f2586 | 44 | Ticker heartbeat; |
wim | 1:f0d3984f2586 | 45 | bool heartbeatflag=false; |
wim | 1:f0d3984f2586 | 46 | |
wim | 1:f0d3984f2586 | 47 | // Local functions |
wim | 1:f0d3984f2586 | 48 | void clear_screen() { |
wim | 1:f0d3984f2586 | 49 | //ANSI Terminal Commands |
wim | 1:f0d3984f2586 | 50 | pc.printf("\x1B[2J"); |
wim | 1:f0d3984f2586 | 51 | pc.printf("\x1B[H"); |
wim | 1:f0d3984f2586 | 52 | } |
wim | 1:f0d3984f2586 | 53 | |
wim | 1:f0d3984f2586 | 54 | |
wim | 1:f0d3984f2586 | 55 | // Heartbeat monitor |
wim | 1:f0d3984f2586 | 56 | void pulse() { |
wim | 1:f0d3984f2586 | 57 | heartbeatLED = !heartbeatLED; |
wim | 1:f0d3984f2586 | 58 | } |
wim | 1:f0d3984f2586 | 59 | |
wim | 1:f0d3984f2586 | 60 | void heartbeat_start() { |
wim | 1:f0d3984f2586 | 61 | heartbeat.attach(&pulse, 0.5); |
wim | 1:f0d3984f2586 | 62 | } |
wim | 1:f0d3984f2586 | 63 | |
wim | 1:f0d3984f2586 | 64 | void heartbeat_stop() { |
wim | 1:f0d3984f2586 | 65 | heartbeat.detach(); |
wim | 1:f0d3984f2586 | 66 | } |
wim | 1:f0d3984f2586 | 67 | |
wim | 1:f0d3984f2586 | 68 | |
wim | 0:d83a90125711 | 69 | int main() { |
wim | 1:f0d3984f2586 | 70 | bool running=true; |
wim | 1:f0d3984f2586 | 71 | char command; |
wim | 1:f0d3984f2586 | 72 | int i=0; |
wim | 1:f0d3984f2586 | 73 | |
wim | 1:f0d3984f2586 | 74 | pc.printf("\nHello World!\n\r"); |
wim | 1:f0d3984f2586 | 75 | |
wim | 1:f0d3984f2586 | 76 | heartbeat_start(); |
wim | 1:f0d3984f2586 | 77 | |
wim | 1:f0d3984f2586 | 78 | myled1 = 1; // LED Off |
wim | 1:f0d3984f2586 | 79 | |
wim | 1:f0d3984f2586 | 80 | // We need to enable flow control or we overflow buffers and |
wim | 1:f0d3984f2586 | 81 | // lose data when used with the WiFly. Note that flow control |
wim | 1:f0d3984f2586 | 82 | // needs to be enabled on the WiFly for this to work but it's |
wim | 1:f0d3984f2586 | 83 | // possible to do that with flow control enabled here but not there. |
wim | 1:f0d3984f2586 | 84 | // serial_bridge.set_flow_control(SC16IS750::RTSCTS); |
wim | 1:f0d3984f2586 | 85 | |
wim | 1:f0d3984f2586 | 86 | serial_bridge.ioSetDirection(0xFF); // All outputs |
wim | 1:f0d3984f2586 | 87 | serial_bridge.ioSetState(0x00); // All On |
wim | 1:f0d3984f2586 | 88 | |
wim | 1:f0d3984f2586 | 89 | show_menu(); |
wim | 1:f0d3984f2586 | 90 | |
wim | 1:f0d3984f2586 | 91 | while(running) { |
wim | 0:d83a90125711 | 92 | |
wim | 1:f0d3984f2586 | 93 | if(pc.readable()) { |
wim | 1:f0d3984f2586 | 94 | command = pc.getc(); |
wim | 1:f0d3984f2586 | 95 | pc.printf("command= %c \n\r", command); |
wim | 1:f0d3984f2586 | 96 | |
wim | 1:f0d3984f2586 | 97 | switch (command) { |
wim | 1:f0d3984f2586 | 98 | case '0' : |
wim | 1:f0d3984f2586 | 99 | pc.printf("Done\n\r"); |
wim | 1:f0d3984f2586 | 100 | running = false; |
wim | 1:f0d3984f2586 | 101 | break; |
wim | 1:f0d3984f2586 | 102 | |
wim | 1:f0d3984f2586 | 103 | case '1' : |
wim | 1:f0d3984f2586 | 104 | show_menu(); |
wim | 1:f0d3984f2586 | 105 | break; |
wim | 1:f0d3984f2586 | 106 | |
wim | 1:f0d3984f2586 | 107 | case '2' : |
wim | 1:f0d3984f2586 | 108 | pc.printf("Init\n\r"); |
wim | 1:f0d3984f2586 | 109 | serial_bridge._init(); |
wim | 1:f0d3984f2586 | 110 | break; |
wim | 1:f0d3984f2586 | 111 | |
wim | 1:f0d3984f2586 | 112 | case '3' : |
wim | 1:f0d3984f2586 | 113 | pc.printf("IO Out\n\r"); |
wim | 1:f0d3984f2586 | 114 | |
wim | 1:f0d3984f2586 | 115 | i=0; |
wim | 1:f0d3984f2586 | 116 | while (!pc.readable()) { |
wim | 1:f0d3984f2586 | 117 | serial_bridge.ioSetState(~i); |
wim | 1:f0d3984f2586 | 118 | serial_bridge.ioGetState() ; //test |
wim | 1:f0d3984f2586 | 119 | wait(0.5); |
wim | 1:f0d3984f2586 | 120 | pc.putc('*'); |
wim | 1:f0d3984f2586 | 121 | i=(i+1) & 0xFF; |
wim | 1:f0d3984f2586 | 122 | } |
wim | 1:f0d3984f2586 | 123 | |
wim | 1:f0d3984f2586 | 124 | pc.getc(); |
wim | 1:f0d3984f2586 | 125 | pc.printf("IO Out Done\n\r"); |
wim | 1:f0d3984f2586 | 126 | break; |
wim | 1:f0d3984f2586 | 127 | |
wim | 1:f0d3984f2586 | 128 | case '4' : |
wim | 1:f0d3984f2586 | 129 | pc.printf("Echo Text, Enter '$' to quit...\n\r"); |
wim | 1:f0d3984f2586 | 130 | |
wim | 1:f0d3984f2586 | 131 | char ch; |
wim | 1:f0d3984f2586 | 132 | bool running_test=true; |
wim | 1:f0d3984f2586 | 133 | |
wim | 1:f0d3984f2586 | 134 | while (running_test) { |
wim | 1:f0d3984f2586 | 135 | // From SPI/I2C to serial |
wim | 1:f0d3984f2586 | 136 | while (running_test && pc.readable()) { |
wim | 1:f0d3984f2586 | 137 | ch = pc.getc(); |
wim | 1:f0d3984f2586 | 138 | running_test = (ch != '$'); |
wim | 1:f0d3984f2586 | 139 | serial_bridge.putc(ch); |
wim | 1:f0d3984f2586 | 140 | } |
wim | 1:f0d3984f2586 | 141 | |
wim | 1:f0d3984f2586 | 142 | // From Serial to SPI/I2C |
wim | 1:f0d3984f2586 | 143 | while (running_test && serial_bridge.readable()) { |
wim | 1:f0d3984f2586 | 144 | ch = serial_bridge.getc(); |
wim | 1:f0d3984f2586 | 145 | running_test = (ch != '$'); |
wim | 1:f0d3984f2586 | 146 | pc.putc(ch); |
wim | 1:f0d3984f2586 | 147 | } |
wim | 1:f0d3984f2586 | 148 | |
wim | 1:f0d3984f2586 | 149 | } |
wim | 1:f0d3984f2586 | 150 | |
wim | 1:f0d3984f2586 | 151 | pc.printf("\n\rEcho Text Done\n\r"); |
wim | 1:f0d3984f2586 | 152 | break; |
wim | 1:f0d3984f2586 | 153 | |
wim | 1:f0d3984f2586 | 154 | default : |
wim | 1:f0d3984f2586 | 155 | break; |
wim | 1:f0d3984f2586 | 156 | |
wim | 1:f0d3984f2586 | 157 | } //switch |
wim | 1:f0d3984f2586 | 158 | } //if |
wim | 1:f0d3984f2586 | 159 | } //while |
wim | 1:f0d3984f2586 | 160 | |
wim | 1:f0d3984f2586 | 161 | |
wim | 1:f0d3984f2586 | 162 | |
wim | 1:f0d3984f2586 | 163 | |
wim | 1:f0d3984f2586 | 164 | #if(0) |
wim | 1:f0d3984f2586 | 165 | |
wim | 1:f0d3984f2586 | 166 | |
wim | 0:d83a90125711 | 167 | // CS = 1; |
wim | 0:d83a90125711 | 168 | |
wim | 0:d83a90125711 | 169 | spi.format(8, 0); |
wim | 0:d83a90125711 | 170 | // spi.frequency(100000); |
wim | 0:d83a90125711 | 171 | // spi.frequency(500000); |
wim | 0:d83a90125711 | 172 | spi.frequency(1000000); |
wim | 0:d83a90125711 | 173 | // spi.frequency(1500000); |
wim | 0:d83a90125711 | 174 | |
wim | 0:d83a90125711 | 175 | |
wim | 1:f0d3984f2586 | 176 | |
wim | 0:d83a90125711 | 177 | while(1) { |
wim | 0:d83a90125711 | 178 | CS=0; |
wim | 0:d83a90125711 | 179 | spi.write(0xAA); |
wim | 0:d83a90125711 | 180 | spi.write(0x81); |
wim | 0:d83a90125711 | 181 | CS=1; |
wim | 0:d83a90125711 | 182 | wait_us(5); |
wim | 0:d83a90125711 | 183 | } |
wim | 0:d83a90125711 | 184 | #endif |
wim | 0:d83a90125711 | 185 | |
wim | 0:d83a90125711 | 186 | pc.printf("\nBye World!\n"); |
wim | 0:d83a90125711 | 187 | } |