Wim Huiskamp
/
SC16IS750_Test
Testsoftware for SC16IS750
main.cpp@2:8aba07490dce, 2014-02-13 (annotated)
- Committer:
- wim
- Date:
- Thu Feb 13 17:12:12 2014 +0000
- Revision:
- 2:8aba07490dce
- Parent:
- 1:f0d3984f2586
- Child:
- 3:3d4269319b51
Testversion 2
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 | 2:8aba07490dce | 4 | |
wim | 2:8aba07490dce | 5 | #if defined(TARGET_LPC1768) |
wim | 2:8aba07490dce | 6 | // SPI for LPC1768 |
wim | 2:8aba07490dce | 7 | #define D_MOSI p5 |
wim | 2:8aba07490dce | 8 | #define D_MISO p6 |
wim | 2:8aba07490dce | 9 | #define D_SCLK p7 |
wim | 2:8aba07490dce | 10 | #define D_CS p8 |
wim | 2:8aba07490dce | 11 | #define D_INTR p15 |
wim | 2:8aba07490dce | 12 | |
wim | 2:8aba07490dce | 13 | // Serial for LPC1768 |
wim | 2:8aba07490dce | 14 | #define D_TX USBTX |
wim | 2:8aba07490dce | 15 | #define D_RX USBRX |
wim | 2:8aba07490dce | 16 | //#define D_RST p25 |
wim | 2:8aba07490dce | 17 | //#define D_STS p26 |
wim | 2:8aba07490dce | 18 | #endif |
wim | 2:8aba07490dce | 19 | |
wim | 2:8aba07490dce | 20 | #if defined(TARGET_KL25Z) |
wim | 2:8aba07490dce | 21 | // SPI for KL25Z |
wim | 2:8aba07490dce | 22 | #define D_MOSI PTD2 |
wim | 2:8aba07490dce | 23 | #define D_MISO PTD3 |
wim | 2:8aba07490dce | 24 | #define D_SCLK PTD1 |
wim | 2:8aba07490dce | 25 | #define D_CS PTD0 |
wim | 2:8aba07490dce | 26 | //#define D_INTR p15 |
wim | 2:8aba07490dce | 27 | |
wim | 2:8aba07490dce | 28 | // I2C for KL25Z |
wim | 2:8aba07490dce | 29 | #define D_SCL PTE1 |
wim | 2:8aba07490dce | 30 | #define D_SDA PTE0 |
wim | 2:8aba07490dce | 31 | |
wim | 2:8aba07490dce | 32 | // Serial for KL25Z |
wim | 2:8aba07490dce | 33 | #define D_TX USBTX |
wim | 2:8aba07490dce | 34 | #define D_RX USBRX |
wim | 2:8aba07490dce | 35 | #endif |
wim | 2:8aba07490dce | 36 | |
wim | 2:8aba07490dce | 37 | #if defined(TARGET_LPC812) |
wim | 2:8aba07490dce | 38 | // SPI for LPC812 |
wim | 2:8aba07490dce | 39 | #define D_MOSI P0_14 |
wim | 2:8aba07490dce | 40 | #define D_MISO P0_15 |
wim | 2:8aba07490dce | 41 | #define D_SCLK P0_12 |
wim | 2:8aba07490dce | 42 | #define D_CS P0_13 |
wim | 2:8aba07490dce | 43 | //#define D_INTR P0_7 |
wim | 2:8aba07490dce | 44 | |
wim | 2:8aba07490dce | 45 | // Serial for LPC812 |
wim | 2:8aba07490dce | 46 | #define D_TX P0_4 |
wim | 2:8aba07490dce | 47 | #define D_RX P0_0 |
wim | 2:8aba07490dce | 48 | //#define D_RST P0_9 |
wim | 2:8aba07490dce | 49 | //#define D_STS P0_17 |
wim | 2:8aba07490dce | 50 | #endif |
wim | 2:8aba07490dce | 51 | |
wim | 2:8aba07490dce | 52 | |
wim | 0:d83a90125711 | 53 | //SPI Version |
wim | 2:8aba07490dce | 54 | //SPI spi(PTD2, PTD3, PTD1); //MOSI, MISO, SCK |
wim | 2:8aba07490dce | 55 | SPI spi(D_MOSI, D_MISO, D_SCLK); //MOSI, MISO, SCK |
wim | 0:d83a90125711 | 56 | //DigitalOut CS(PTD0); //CS |
wim | 2:8aba07490dce | 57 | //SC16IS750_SPI serial_bridge(&spi, PTD0); |
wim | 2:8aba07490dce | 58 | SC16IS750_SPI serial_bridge(&spi, D_CS); |
wim | 0:d83a90125711 | 59 | |
wim | 0:d83a90125711 | 60 | //I2C Version |
wim | 2:8aba07490dce | 61 | //I2C i2c(D_SDA, D_SCL); //SDA, SCL |
wim | 1:f0d3984f2586 | 62 | //SC16IS750_I2C serial_bridge(&i2c, DEFAULT_SC16IS750_ADDR); |
wim | 0:d83a90125711 | 63 | |
wim | 0:d83a90125711 | 64 | DigitalOut myled1(LED_RED); |
wim | 1:f0d3984f2586 | 65 | //DigitalOut myled2(LED_GREEN); |
wim | 1:f0d3984f2586 | 66 | //DigitalOut myled3(LED_BLUE); // Same as PTD1 (SCK) |
wim | 1:f0d3984f2586 | 67 | DigitalOut heartbeatLED(LED_GREEN); |
wim | 0:d83a90125711 | 68 | |
wim | 2:8aba07490dce | 69 | Serial pc(D_TX,D_RX); |
wim | 0:d83a90125711 | 70 | |
wim | 1:f0d3984f2586 | 71 | void show_menu() { |
wim | 1:f0d3984f2586 | 72 | pc.printf("0: Exit\n\r"); |
wim | 1:f0d3984f2586 | 73 | pc.printf("1: Show Menu\n\r"); |
wim | 1:f0d3984f2586 | 74 | pc.printf("2: Init\n\r"); |
wim | 1:f0d3984f2586 | 75 | pc.printf("3: IO Out\n\r"); |
wim | 1:f0d3984f2586 | 76 | pc.printf("4: Echo Text\n\r"); |
wim | 2:8aba07490dce | 77 | pc.printf("5: Free bufferspace\n\r"); |
wim | 2:8aba07490dce | 78 | pc.printf("6: Enable RTS/CTS\n\r"); |
wim | 2:8aba07490dce | 79 | pc.printf("7: Disable RTS/CTS\n\r"); |
wim | 2:8aba07490dce | 80 | pc.printf("8: Write block\n\r"); |
wim | 1:f0d3984f2586 | 81 | |
wim | 1:f0d3984f2586 | 82 | #if(0) |
wim | 1:f0d3984f2586 | 83 | pc.printf("9: \n\r"); |
wim | 1:f0d3984f2586 | 84 | pc.printf("A: \n\r"); |
wim | 1:f0d3984f2586 | 85 | pc.printf("B: \n\r"); |
wim | 1:f0d3984f2586 | 86 | pc.printf("C: \n\r"); |
wim | 1:f0d3984f2586 | 87 | pc.printf("D: \n\r"); |
wim | 1:f0d3984f2586 | 88 | pc.printf("P: \n\r"); |
wim | 1:f0d3984f2586 | 89 | #endif |
wim | 1:f0d3984f2586 | 90 | pc.printf("\n\r"); |
wim | 1:f0d3984f2586 | 91 | } |
wim | 1:f0d3984f2586 | 92 | |
wim | 1:f0d3984f2586 | 93 | // Variables for Heartbeat and Status monitoring |
wim | 1:f0d3984f2586 | 94 | Ticker heartbeat; |
wim | 1:f0d3984f2586 | 95 | bool heartbeatflag=false; |
wim | 1:f0d3984f2586 | 96 | |
wim | 1:f0d3984f2586 | 97 | // Local functions |
wim | 1:f0d3984f2586 | 98 | void clear_screen() { |
wim | 1:f0d3984f2586 | 99 | //ANSI Terminal Commands |
wim | 1:f0d3984f2586 | 100 | pc.printf("\x1B[2J"); |
wim | 1:f0d3984f2586 | 101 | pc.printf("\x1B[H"); |
wim | 1:f0d3984f2586 | 102 | } |
wim | 1:f0d3984f2586 | 103 | |
wim | 1:f0d3984f2586 | 104 | |
wim | 1:f0d3984f2586 | 105 | // Heartbeat monitor |
wim | 1:f0d3984f2586 | 106 | void pulse() { |
wim | 1:f0d3984f2586 | 107 | heartbeatLED = !heartbeatLED; |
wim | 1:f0d3984f2586 | 108 | } |
wim | 1:f0d3984f2586 | 109 | |
wim | 1:f0d3984f2586 | 110 | void heartbeat_start() { |
wim | 1:f0d3984f2586 | 111 | heartbeat.attach(&pulse, 0.5); |
wim | 1:f0d3984f2586 | 112 | } |
wim | 1:f0d3984f2586 | 113 | |
wim | 1:f0d3984f2586 | 114 | void heartbeat_stop() { |
wim | 1:f0d3984f2586 | 115 | heartbeat.detach(); |
wim | 1:f0d3984f2586 | 116 | } |
wim | 1:f0d3984f2586 | 117 | |
wim | 1:f0d3984f2586 | 118 | |
wim | 0:d83a90125711 | 119 | int main() { |
wim | 1:f0d3984f2586 | 120 | bool running=true; |
wim | 2:8aba07490dce | 121 | bool running_test=true; |
wim | 2:8aba07490dce | 122 | char command, ch; |
wim | 1:f0d3984f2586 | 123 | int i=0; |
wim | 1:f0d3984f2586 | 124 | |
wim | 2:8aba07490dce | 125 | #if defined(TARGET_LPC1768) |
wim | 2:8aba07490dce | 126 | pc.printf("\r\nHello World from LPC1768\r\n"); |
wim | 2:8aba07490dce | 127 | #endif |
wim | 2:8aba07490dce | 128 | #if defined(TARGET_KL25Z) |
wim | 2:8aba07490dce | 129 | pc.printf("\r\nHello World from KL25Z\r\n"); |
wim | 2:8aba07490dce | 130 | #endif |
wim | 2:8aba07490dce | 131 | #if defined(TARGET_LPC812) |
wim | 2:8aba07490dce | 132 | pc.printf("\r\nHello World from LPC812\r\n"); |
wim | 2:8aba07490dce | 133 | #endif |
wim | 2:8aba07490dce | 134 | |
wim | 1:f0d3984f2586 | 135 | |
wim | 1:f0d3984f2586 | 136 | heartbeat_start(); |
wim | 1:f0d3984f2586 | 137 | |
wim | 1:f0d3984f2586 | 138 | myled1 = 1; // LED Off |
wim | 1:f0d3984f2586 | 139 | |
wim | 1:f0d3984f2586 | 140 | // We need to enable flow control or we overflow buffers and |
wim | 1:f0d3984f2586 | 141 | // lose data when used with the WiFly. Note that flow control |
wim | 1:f0d3984f2586 | 142 | // needs to be enabled on the WiFly for this to work but it's |
wim | 1:f0d3984f2586 | 143 | // possible to do that with flow control enabled here but not there. |
wim | 1:f0d3984f2586 | 144 | // serial_bridge.set_flow_control(SC16IS750::RTSCTS); |
wim | 1:f0d3984f2586 | 145 | |
wim | 1:f0d3984f2586 | 146 | serial_bridge.ioSetDirection(0xFF); // All outputs |
wim | 1:f0d3984f2586 | 147 | serial_bridge.ioSetState(0x00); // All On |
wim | 1:f0d3984f2586 | 148 | |
wim | 1:f0d3984f2586 | 149 | show_menu(); |
wim | 1:f0d3984f2586 | 150 | |
wim | 1:f0d3984f2586 | 151 | while(running) { |
wim | 0:d83a90125711 | 152 | |
wim | 1:f0d3984f2586 | 153 | if(pc.readable()) { |
wim | 1:f0d3984f2586 | 154 | command = pc.getc(); |
wim | 1:f0d3984f2586 | 155 | pc.printf("command= %c \n\r", command); |
wim | 1:f0d3984f2586 | 156 | |
wim | 1:f0d3984f2586 | 157 | switch (command) { |
wim | 1:f0d3984f2586 | 158 | case '0' : |
wim | 1:f0d3984f2586 | 159 | pc.printf("Done\n\r"); |
wim | 1:f0d3984f2586 | 160 | running = false; |
wim | 1:f0d3984f2586 | 161 | break; |
wim | 1:f0d3984f2586 | 162 | |
wim | 1:f0d3984f2586 | 163 | case '1' : |
wim | 1:f0d3984f2586 | 164 | show_menu(); |
wim | 1:f0d3984f2586 | 165 | break; |
wim | 1:f0d3984f2586 | 166 | |
wim | 1:f0d3984f2586 | 167 | case '2' : |
wim | 1:f0d3984f2586 | 168 | pc.printf("Init\n\r"); |
wim | 1:f0d3984f2586 | 169 | serial_bridge._init(); |
wim | 1:f0d3984f2586 | 170 | break; |
wim | 1:f0d3984f2586 | 171 | |
wim | 1:f0d3984f2586 | 172 | case '3' : |
wim | 1:f0d3984f2586 | 173 | pc.printf("IO Out\n\r"); |
wim | 1:f0d3984f2586 | 174 | |
wim | 1:f0d3984f2586 | 175 | i=0; |
wim | 1:f0d3984f2586 | 176 | while (!pc.readable()) { |
wim | 1:f0d3984f2586 | 177 | serial_bridge.ioSetState(~i); |
wim | 1:f0d3984f2586 | 178 | serial_bridge.ioGetState() ; //test |
wim | 1:f0d3984f2586 | 179 | wait(0.5); |
wim | 1:f0d3984f2586 | 180 | pc.putc('*'); |
wim | 1:f0d3984f2586 | 181 | i=(i+1) & 0xFF; |
wim | 1:f0d3984f2586 | 182 | } |
wim | 1:f0d3984f2586 | 183 | |
wim | 1:f0d3984f2586 | 184 | pc.getc(); |
wim | 1:f0d3984f2586 | 185 | pc.printf("IO Out Done\n\r"); |
wim | 1:f0d3984f2586 | 186 | break; |
wim | 1:f0d3984f2586 | 187 | |
wim | 1:f0d3984f2586 | 188 | case '4' : |
wim | 1:f0d3984f2586 | 189 | pc.printf("Echo Text, Enter '$' to quit...\n\r"); |
wim | 1:f0d3984f2586 | 190 | |
wim | 2:8aba07490dce | 191 | running_test=true; |
wim | 2:8aba07490dce | 192 | |
wim | 1:f0d3984f2586 | 193 | while (running_test) { |
wim | 1:f0d3984f2586 | 194 | // From SPI/I2C to serial |
wim | 1:f0d3984f2586 | 195 | while (running_test && pc.readable()) { |
wim | 1:f0d3984f2586 | 196 | ch = pc.getc(); |
wim | 1:f0d3984f2586 | 197 | running_test = (ch != '$'); |
wim | 1:f0d3984f2586 | 198 | serial_bridge.putc(ch); |
wim | 1:f0d3984f2586 | 199 | } |
wim | 1:f0d3984f2586 | 200 | |
wim | 1:f0d3984f2586 | 201 | // From Serial to SPI/I2C |
wim | 1:f0d3984f2586 | 202 | while (running_test && serial_bridge.readable()) { |
wim | 1:f0d3984f2586 | 203 | ch = serial_bridge.getc(); |
wim | 1:f0d3984f2586 | 204 | running_test = (ch != '$'); |
wim | 1:f0d3984f2586 | 205 | pc.putc(ch); |
wim | 1:f0d3984f2586 | 206 | } |
wim | 1:f0d3984f2586 | 207 | |
wim | 1:f0d3984f2586 | 208 | } |
wim | 1:f0d3984f2586 | 209 | |
wim | 1:f0d3984f2586 | 210 | pc.printf("\n\rEcho Text Done\n\r"); |
wim | 1:f0d3984f2586 | 211 | break; |
wim | 1:f0d3984f2586 | 212 | |
wim | 2:8aba07490dce | 213 | case '5' : |
wim | 2:8aba07490dce | 214 | pc.printf("Available for Reading = %3d (Free Space = %3d)\n\r", serial_bridge.readableCount(), SC16IS750_FIFO_RX - serial_bridge.readableCount()); |
wim | 2:8aba07490dce | 215 | pc.printf("Available for Writing = %3d (Used Space = %3d)\n\r", serial_bridge.writableCount(), SC16IS750_FIFO_TX - serial_bridge.writableCount()); |
wim | 2:8aba07490dce | 216 | break; |
wim | 2:8aba07490dce | 217 | |
wim | 2:8aba07490dce | 218 | case '6' : |
wim | 2:8aba07490dce | 219 | pc.printf("Enable RTS/CTS\n\r"); |
wim | 2:8aba07490dce | 220 | serial_bridge.set_flow_control(SC16IS750::RTSCTS); |
wim | 2:8aba07490dce | 221 | break; |
wim | 2:8aba07490dce | 222 | case '7' : |
wim | 2:8aba07490dce | 223 | pc.printf("Disable RTS/CTS\n\r"); |
wim | 2:8aba07490dce | 224 | serial_bridge.set_flow_control(SC16IS750::Disabled); |
wim | 2:8aba07490dce | 225 | break; |
wim | 2:8aba07490dce | 226 | |
wim | 2:8aba07490dce | 227 | case '8' : |
wim | 2:8aba07490dce | 228 | pc.printf("Write block\n\r"); |
wim | 2:8aba07490dce | 229 | serial_bridge.write("Hello World from mbed and SC16IS750 "); |
wim | 2:8aba07490dce | 230 | break; |
wim | 2:8aba07490dce | 231 | |
wim | 1:f0d3984f2586 | 232 | default : |
wim | 2:8aba07490dce | 233 | break; |
wim | 1:f0d3984f2586 | 234 | |
wim | 1:f0d3984f2586 | 235 | } //switch |
wim | 1:f0d3984f2586 | 236 | } //if |
wim | 1:f0d3984f2586 | 237 | } //while |
wim | 1:f0d3984f2586 | 238 | |
wim | 1:f0d3984f2586 | 239 | |
wim | 1:f0d3984f2586 | 240 | |
wim | 1:f0d3984f2586 | 241 | |
wim | 1:f0d3984f2586 | 242 | #if(0) |
wim | 1:f0d3984f2586 | 243 | |
wim | 1:f0d3984f2586 | 244 | |
wim | 0:d83a90125711 | 245 | // CS = 1; |
wim | 0:d83a90125711 | 246 | |
wim | 0:d83a90125711 | 247 | spi.format(8, 0); |
wim | 0:d83a90125711 | 248 | // spi.frequency(100000); |
wim | 0:d83a90125711 | 249 | // spi.frequency(500000); |
wim | 0:d83a90125711 | 250 | spi.frequency(1000000); |
wim | 0:d83a90125711 | 251 | // spi.frequency(1500000); |
wim | 0:d83a90125711 | 252 | |
wim | 0:d83a90125711 | 253 | |
wim | 1:f0d3984f2586 | 254 | |
wim | 0:d83a90125711 | 255 | while(1) { |
wim | 0:d83a90125711 | 256 | CS=0; |
wim | 0:d83a90125711 | 257 | spi.write(0xAA); |
wim | 0:d83a90125711 | 258 | spi.write(0x81); |
wim | 0:d83a90125711 | 259 | CS=1; |
wim | 0:d83a90125711 | 260 | wait_us(5); |
wim | 0:d83a90125711 | 261 | } |
wim | 0:d83a90125711 | 262 | #endif |
wim | 0:d83a90125711 | 263 | |
wim | 0:d83a90125711 | 264 | pc.printf("\nBye World!\n"); |
wim | 0:d83a90125711 | 265 | } |