Test for SC16IS750 and SC16IS752 I2C or SPI to UART bridge.

Dependencies:   SC16IS750 mbed

Committer:
wim
Date:
Wed Dec 24 01:09:26 2014 +0000
Revision:
0:20d8ef79d787
Test for v04 of lib. Set I2C clock at 100kb/s for reliable operation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:20d8ef79d787 1 #include "mbed.h"
wim 0:20d8ef79d787 2 #include "SC16IS750.h"
wim 0:20d8ef79d787 3
wim 0:20d8ef79d787 4
wim 0:20d8ef79d787 5 #if defined(TARGET_LPC1768)
wim 0:20d8ef79d787 6 // SPI for LPC1768
wim 0:20d8ef79d787 7 #define D_MOSI p5
wim 0:20d8ef79d787 8 #define D_MISO p6
wim 0:20d8ef79d787 9 #define D_SCLK p7
wim 0:20d8ef79d787 10 #define D_CS p8
wim 0:20d8ef79d787 11 #define D_INTR p15
wim 0:20d8ef79d787 12 #define D_RST p16
wim 0:20d8ef79d787 13
wim 0:20d8ef79d787 14 // I2C for KL25Z
wim 0:20d8ef79d787 15 #define D_SCL p10
wim 0:20d8ef79d787 16 #define D_SDA p9
wim 0:20d8ef79d787 17
wim 0:20d8ef79d787 18 // Serial for LPC1768
wim 0:20d8ef79d787 19 #define D_TX USBTX
wim 0:20d8ef79d787 20 #define D_RX USBRX
wim 0:20d8ef79d787 21 //#define D_RST p25
wim 0:20d8ef79d787 22 //#define D_STS p26
wim 0:20d8ef79d787 23
wim 0:20d8ef79d787 24 DigitalOut myled1(LED1);
wim 0:20d8ef79d787 25 //DigitalOut myled2(LED2);
wim 0:20d8ef79d787 26 //DigitalOut myled3(LED3);
wim 0:20d8ef79d787 27 DigitalOut heartbeatLED(LED4);
wim 0:20d8ef79d787 28 #endif
wim 0:20d8ef79d787 29
wim 0:20d8ef79d787 30 #if defined(TARGET_KL25Z)
wim 0:20d8ef79d787 31 // SPI for KL25Z
wim 0:20d8ef79d787 32 #define D_MOSI PTD2
wim 0:20d8ef79d787 33 #define D_MISO PTD3
wim 0:20d8ef79d787 34 #define D_SCLK PTD1
wim 0:20d8ef79d787 35 #define D_CS PTD0
wim 0:20d8ef79d787 36 //#define D_INTR PTA13
wim 0:20d8ef79d787 37 #define D_RST PTD5
wim 0:20d8ef79d787 38
wim 0:20d8ef79d787 39 // I2C for KL25Z
wim 0:20d8ef79d787 40 #define D_SCL PTE1
wim 0:20d8ef79d787 41 #define D_SDA PTE0
wim 0:20d8ef79d787 42
wim 0:20d8ef79d787 43 // Serial for KL25Z
wim 0:20d8ef79d787 44 #define D_TX USBTX
wim 0:20d8ef79d787 45 #define D_RX USBRX
wim 0:20d8ef79d787 46
wim 0:20d8ef79d787 47 DigitalOut myled1(LED_RED);
wim 0:20d8ef79d787 48 //DigitalOut myled2(LED_GREEN);
wim 0:20d8ef79d787 49 //DigitalOut myled3(LED_BLUE); // Same as PTD1 (SCK)
wim 0:20d8ef79d787 50 DigitalOut heartbeatLED(LED_GREEN);
wim 0:20d8ef79d787 51 #endif
wim 0:20d8ef79d787 52
wim 0:20d8ef79d787 53 #if defined(TARGET_LPC812)
wim 0:20d8ef79d787 54 // SPI for LPC812
wim 0:20d8ef79d787 55 #define D_MOSI P0_14
wim 0:20d8ef79d787 56 #define D_MISO P0_15
wim 0:20d8ef79d787 57 #define D_SCLK P0_12
wim 0:20d8ef79d787 58 #define D_CS P0_13
wim 0:20d8ef79d787 59 //#define D_INTR P0_7
wim 0:20d8ef79d787 60 //#define D_RST P0_8
wim 0:20d8ef79d787 61
wim 0:20d8ef79d787 62 // Serial for LPC812
wim 0:20d8ef79d787 63 #define D_TX P0_4
wim 0:20d8ef79d787 64 #define D_RX P0_0
wim 0:20d8ef79d787 65 //#define D_RST P0_9
wim 0:20d8ef79d787 66 //#define D_STS P0_17
wim 0:20d8ef79d787 67 #endif
wim 0:20d8ef79d787 68
wim 0:20d8ef79d787 69
wim 0:20d8ef79d787 70 //SPI Version
wim 0:20d8ef79d787 71 SPI spi(D_MOSI, D_MISO, D_SCLK); //MOSI, MISO, SCK
wim 0:20d8ef79d787 72 //SC16IS750_SPI serial_bridge(&spi, D_CS);
wim 0:20d8ef79d787 73 //SC16IS750_SPI serial_bridge(&spi, D_CS, D_RST); // version with Hardware Reset pin
wim 0:20d8ef79d787 74 //SC16IS752_SPI serial_bridge(&spi, D_CS, D_RST, SC16IS750::Channel_A); // Dual UART version with Hardware Reset pin
wim 0:20d8ef79d787 75 //SC16IS752_SPI serial_bridge(&spi, D_CS, D_RST, SC16IS750::Channel_B); // Dual UART version with Hardware Reset pin
wim 0:20d8ef79d787 76
wim 0:20d8ef79d787 77 //I2C Version
wim 0:20d8ef79d787 78 I2C i2c(D_SDA, D_SCL); //SDA, SCL
wim 0:20d8ef79d787 79 //SC16IS750_I2C serial_bridge(&i2c, SC16IS750_DEFAULT_ADDR);
wim 0:20d8ef79d787 80 //SC16IS750_I2C serial_bridge(&i2c, SC16IS750_DEFAULT_ADDR, D_RST);
wim 0:20d8ef79d787 81 SC16IS752_I2C serial_bridge(&i2c, SC16IS750_DEFAULT_ADDR, D_RST, SC16IS750::Channel_A); // Dual UART version with Hardware Reset pin
wim 0:20d8ef79d787 82 //SC16IS752_I2C serial_bridge(&i2c, SC16IS750_DEFAULT_ADDR, D_RST, SC16IS750::Channel_B); // Dual UART version with Hardware Reset pin
wim 0:20d8ef79d787 83
wim 0:20d8ef79d787 84
wim 0:20d8ef79d787 85 Serial pc(D_TX,D_RX);
wim 0:20d8ef79d787 86
wim 0:20d8ef79d787 87 void show_menu() {
wim 0:20d8ef79d787 88 pc.printf("0: Exit\n\r");
wim 0:20d8ef79d787 89 pc.printf("1: Show Menu\n\r");
wim 0:20d8ef79d787 90 pc.printf("2: Init\n\r");
wim 0:20d8ef79d787 91 pc.printf("3: IO Port Out\n\r");
wim 0:20d8ef79d787 92 pc.printf("4: Transparant mode\n\r");
wim 0:20d8ef79d787 93 pc.printf("5: Free bufferspace\n\r");
wim 0:20d8ef79d787 94 pc.printf("6: Enable RTS/CTS\n\r");
wim 0:20d8ef79d787 95 pc.printf("7: Disable RTS/CTS\n\r");
wim 0:20d8ef79d787 96 pc.printf("8: Write block\n\r");
wim 0:20d8ef79d787 97 pc.printf("9: Baudrate 9600\n\r");
wim 0:20d8ef79d787 98 pc.printf("A: Baudrate 115200\n\r");
wim 0:20d8ef79d787 99 pc.printf("B: Transparant mode with bufferdisplay\n\r");
wim 0:20d8ef79d787 100 pc.printf("C: Test printf \n\r");
wim 0:20d8ef79d787 101 pc.printf("D: Test connected\n\r");
wim 0:20d8ef79d787 102
wim 0:20d8ef79d787 103 #if(0)
wim 0:20d8ef79d787 104 pc.printf("D: \n\r");
wim 0:20d8ef79d787 105 pc.printf("P: \n\r");
wim 0:20d8ef79d787 106 #endif
wim 0:20d8ef79d787 107 pc.printf("\n\r");
wim 0:20d8ef79d787 108 }
wim 0:20d8ef79d787 109
wim 0:20d8ef79d787 110 // Variables for Heartbeat and Status monitoring
wim 0:20d8ef79d787 111 Ticker heartbeat;
wim 0:20d8ef79d787 112 bool heartbeatflag=false;
wim 0:20d8ef79d787 113
wim 0:20d8ef79d787 114 // Local functions
wim 0:20d8ef79d787 115 void clear_screen() {
wim 0:20d8ef79d787 116 //ANSI Terminal Commands
wim 0:20d8ef79d787 117 pc.printf("\x1B[2J");
wim 0:20d8ef79d787 118 pc.printf("\x1B[H");
wim 0:20d8ef79d787 119 }
wim 0:20d8ef79d787 120
wim 0:20d8ef79d787 121
wim 0:20d8ef79d787 122 // Heartbeat monitor
wim 0:20d8ef79d787 123 void pulse() {
wim 0:20d8ef79d787 124 heartbeatLED = !heartbeatLED;
wim 0:20d8ef79d787 125 }
wim 0:20d8ef79d787 126
wim 0:20d8ef79d787 127 void heartbeat_start() {
wim 0:20d8ef79d787 128 heartbeat.attach(&pulse, 0.5);
wim 0:20d8ef79d787 129 }
wim 0:20d8ef79d787 130
wim 0:20d8ef79d787 131 void heartbeat_stop() {
wim 0:20d8ef79d787 132 heartbeat.detach();
wim 0:20d8ef79d787 133 }
wim 0:20d8ef79d787 134
wim 0:20d8ef79d787 135
wim 0:20d8ef79d787 136 int main() {
wim 0:20d8ef79d787 137 bool running=true;
wim 0:20d8ef79d787 138 bool running_test=true;
wim 0:20d8ef79d787 139 char command, ch;
wim 0:20d8ef79d787 140 int i=0;
wim 0:20d8ef79d787 141
wim 0:20d8ef79d787 142 #if defined(TARGET_LPC1768)
wim 0:20d8ef79d787 143 pc.printf("\r\nHello World from LPC1768\r\n");
wim 0:20d8ef79d787 144 #endif
wim 0:20d8ef79d787 145 #if defined(TARGET_KL25Z)
wim 0:20d8ef79d787 146 pc.printf("\r\nHello World from KL25Z\r\n");
wim 0:20d8ef79d787 147 #endif
wim 0:20d8ef79d787 148 #if defined(TARGET_LPC812)
wim 0:20d8ef79d787 149 pc.printf("\r\nHello World from LPC812\r\n");
wim 0:20d8ef79d787 150 #endif
wim 0:20d8ef79d787 151
wim 0:20d8ef79d787 152
wim 0:20d8ef79d787 153 heartbeat_start();
wim 0:20d8ef79d787 154
wim 0:20d8ef79d787 155 myled1 = 1; // LED Off
wim 0:20d8ef79d787 156
wim 0:20d8ef79d787 157 // We need to enable flow control or we overflow buffers and
wim 0:20d8ef79d787 158 // lose data when used with the WiFly. Note that flow control
wim 0:20d8ef79d787 159 // needs to be enabled on the WiFly for this to work but it's
wim 0:20d8ef79d787 160 // possible to do that with flow control enabled here but not there.
wim 0:20d8ef79d787 161 // serial_bridge.set_flow_control(SC16IS750::RTSCTS);
wim 0:20d8ef79d787 162
wim 0:20d8ef79d787 163 serial_bridge.ioSetDirection(0xFF); // All outputs
wim 0:20d8ef79d787 164 serial_bridge.ioSetState(0xFF); // All On
wim 0:20d8ef79d787 165
wim 0:20d8ef79d787 166 show_menu();
wim 0:20d8ef79d787 167
wim 0:20d8ef79d787 168 while(running) {
wim 0:20d8ef79d787 169
wim 0:20d8ef79d787 170 if(pc.readable()) {
wim 0:20d8ef79d787 171 command = pc.getc();
wim 0:20d8ef79d787 172 pc.printf("command= %c \n\r", command);
wim 0:20d8ef79d787 173
wim 0:20d8ef79d787 174 switch (command) {
wim 0:20d8ef79d787 175 case '0' :
wim 0:20d8ef79d787 176 pc.printf("Done\n\r");
wim 0:20d8ef79d787 177 running = false;
wim 0:20d8ef79d787 178 break;
wim 0:20d8ef79d787 179
wim 0:20d8ef79d787 180 case '1' :
wim 0:20d8ef79d787 181 show_menu();
wim 0:20d8ef79d787 182 break;
wim 0:20d8ef79d787 183
wim 0:20d8ef79d787 184 case '2' :
wim 0:20d8ef79d787 185 pc.printf("Hardware Reset\n\r");
wim 0:20d8ef79d787 186 serial_bridge.hwReset(); //test
wim 0:20d8ef79d787 187
wim 0:20d8ef79d787 188 pc.printf("Init\n\r");
wim 0:20d8ef79d787 189 serial_bridge._init();
wim 0:20d8ef79d787 190 pc.printf("Hardware Init done\n\r");
wim 0:20d8ef79d787 191 break;
wim 0:20d8ef79d787 192
wim 0:20d8ef79d787 193 case '3' :
wim 0:20d8ef79d787 194 pc.printf("IO Port Out\n\r");
wim 0:20d8ef79d787 195
wim 0:20d8ef79d787 196 i=0;
wim 0:20d8ef79d787 197 while (!pc.readable()) {
wim 0:20d8ef79d787 198 serial_bridge.ioSetState(~i);
wim 0:20d8ef79d787 199 // serial_bridge.ioGetState() ; //test
wim 0:20d8ef79d787 200 wait(0.5);
wim 0:20d8ef79d787 201 pc.putc('*');
wim 0:20d8ef79d787 202 i=(i+1) & 0xFF;
wim 0:20d8ef79d787 203 }
wim 0:20d8ef79d787 204
wim 0:20d8ef79d787 205 pc.getc();
wim 0:20d8ef79d787 206 pc.printf("IO Port Out Done\n\r");
wim 0:20d8ef79d787 207 break;
wim 0:20d8ef79d787 208
wim 0:20d8ef79d787 209 case '4' :
wim 0:20d8ef79d787 210 pc.printf("Transparant Mode, Enter '#' to quit...\n\r");
wim 0:20d8ef79d787 211
wim 0:20d8ef79d787 212 running_test=true;
wim 0:20d8ef79d787 213
wim 0:20d8ef79d787 214 while (running_test) {
wim 0:20d8ef79d787 215 // From SPI/I2C to serial
wim 0:20d8ef79d787 216 while (running_test && pc.readable()) {
wim 0:20d8ef79d787 217 ch = pc.getc();
wim 0:20d8ef79d787 218 running_test = (ch != '#');
wim 0:20d8ef79d787 219 serial_bridge.putc(ch);
wim 0:20d8ef79d787 220 }
wim 0:20d8ef79d787 221
wim 0:20d8ef79d787 222 // From Serial to SPI/I2C
wim 0:20d8ef79d787 223 while (running_test && serial_bridge.readable()) {
wim 0:20d8ef79d787 224 ch = serial_bridge.getc();
wim 0:20d8ef79d787 225 running_test = (ch != '#');
wim 0:20d8ef79d787 226 pc.putc(ch);
wim 0:20d8ef79d787 227 }
wim 0:20d8ef79d787 228
wim 0:20d8ef79d787 229 }
wim 0:20d8ef79d787 230
wim 0:20d8ef79d787 231 pc.printf("\n\rTransparant Mode done\n\r");
wim 0:20d8ef79d787 232 break;
wim 0:20d8ef79d787 233
wim 0:20d8ef79d787 234 case '5' :
wim 0:20d8ef79d787 235 pc.printf("Available for Reading = %3d (Free Space = %3d)\n\r", serial_bridge.readableCount(), SC16IS750_FIFO_RX - serial_bridge.readableCount());
wim 0:20d8ef79d787 236 pc.printf("Available for Writing = %3d (Used Space = %3d)\n\r", serial_bridge.writableCount(), SC16IS750_FIFO_TX - serial_bridge.writableCount());
wim 0:20d8ef79d787 237 break;
wim 0:20d8ef79d787 238
wim 0:20d8ef79d787 239 case '6' :
wim 0:20d8ef79d787 240 pc.printf("Enable RTS/CTS\n\r");
wim 0:20d8ef79d787 241 serial_bridge.set_flow_control(SC16IS750::RTSCTS);
wim 0:20d8ef79d787 242 break;
wim 0:20d8ef79d787 243 case '7' :
wim 0:20d8ef79d787 244 pc.printf("Disable RTS/CTS\n\r");
wim 0:20d8ef79d787 245 serial_bridge.set_flow_control(SC16IS750::Disabled);
wim 0:20d8ef79d787 246 break;
wim 0:20d8ef79d787 247
wim 0:20d8ef79d787 248 case '8' :
wim 0:20d8ef79d787 249 pc.printf("Write block\n\r");
wim 0:20d8ef79d787 250 serial_bridge.writeString("Hello World from mbed and SC16IS750 ");
wim 0:20d8ef79d787 251 break;
wim 0:20d8ef79d787 252
wim 0:20d8ef79d787 253 case '9' :
wim 0:20d8ef79d787 254 pc.printf("Baudrate = 9600, Divisor = %d\n\r", SC16IS750_BAUDRATE_DIVISOR(9600));
wim 0:20d8ef79d787 255 serial_bridge.baud(9600);
wim 0:20d8ef79d787 256 break;
wim 0:20d8ef79d787 257
wim 0:20d8ef79d787 258 case 'A' :
wim 0:20d8ef79d787 259 pc.printf("Baudrate = 115200, Divisor = %d\n\r", SC16IS750_BAUDRATE_DIVISOR(115200));
wim 0:20d8ef79d787 260 serial_bridge.baud(115200);
wim 0:20d8ef79d787 261 break;
wim 0:20d8ef79d787 262
wim 0:20d8ef79d787 263 case 'B' :
wim 0:20d8ef79d787 264 pc.printf("Transparant Mode with buffer display, Enter '#' to quit...\n\r");
wim 0:20d8ef79d787 265
wim 0:20d8ef79d787 266 running_test=true;
wim 0:20d8ef79d787 267
wim 0:20d8ef79d787 268 while (running_test) {
wim 0:20d8ef79d787 269 // From SPI/I2C to serial
wim 0:20d8ef79d787 270 while (running_test && pc.readable()) {
wim 0:20d8ef79d787 271 ch = pc.getc();
wim 0:20d8ef79d787 272 running_test = (ch != '#');
wim 0:20d8ef79d787 273 serial_bridge.putc(ch);
wim 0:20d8ef79d787 274
wim 0:20d8ef79d787 275 // Show buffers when character was entered
wim 0:20d8ef79d787 276 pc.printf("\n\r");
wim 0:20d8ef79d787 277 pc.printf("Available for Reading = %3d (Free Space = %3d)\n\r", serial_bridge.readableCount(), SC16IS750_FIFO_RX - serial_bridge.readableCount());
wim 0:20d8ef79d787 278 pc.printf("Available for Writing = %3d (Used Space = %3d)\n\r", serial_bridge.writableCount(), SC16IS750_FIFO_TX - serial_bridge.writableCount());
wim 0:20d8ef79d787 279 }
wim 0:20d8ef79d787 280
wim 0:20d8ef79d787 281
wim 0:20d8ef79d787 282 // From Serial to SPI/I2C
wim 0:20d8ef79d787 283 while (running_test && serial_bridge.readable()) {
wim 0:20d8ef79d787 284 ch = serial_bridge.getc();
wim 0:20d8ef79d787 285 running_test = (ch != '#');
wim 0:20d8ef79d787 286 pc.putc(ch);
wim 0:20d8ef79d787 287 }
wim 0:20d8ef79d787 288
wim 0:20d8ef79d787 289 }
wim 0:20d8ef79d787 290
wim 0:20d8ef79d787 291 pc.printf("\n\rTransparant Mode done\n\r");
wim 0:20d8ef79d787 292 break;
wim 0:20d8ef79d787 293
wim 0:20d8ef79d787 294 case 'C' :
wim 0:20d8ef79d787 295 pc.printf("Test printf() \n\r");
wim 0:20d8ef79d787 296
wim 0:20d8ef79d787 297 serial_bridge.printf("Available for Reading = %3d (Free Space = %3d)\n\r", serial_bridge.readableCount(), SC16IS750_FIFO_RX - serial_bridge.readableCount() );
wim 0:20d8ef79d787 298 serial_bridge.printf("Available for Writing = %3d (Used Space = %3d)\n\r", serial_bridge.writableCount(), SC16IS750_FIFO_TX - serial_bridge.writableCount());
wim 0:20d8ef79d787 299
wim 0:20d8ef79d787 300 pc.printf("\n\rTest printf() done\n\r");
wim 0:20d8ef79d787 301 break;
wim 0:20d8ef79d787 302 case 'D' :
wim 0:20d8ef79d787 303 pc.printf("Test connected\n\r");
wim 0:20d8ef79d787 304 if(!serial_bridge.connected()){
wim 0:20d8ef79d787 305 pc.printf("Failed to detect UART bridge\r\n");
wim 0:20d8ef79d787 306 }
wim 0:20d8ef79d787 307 else {
wim 0:20d8ef79d787 308 pc.printf("Found UART bridge!\r\n");
wim 0:20d8ef79d787 309 }
wim 0:20d8ef79d787 310 break;
wim 0:20d8ef79d787 311 default :
wim 0:20d8ef79d787 312 break;
wim 0:20d8ef79d787 313
wim 0:20d8ef79d787 314 } //switch
wim 0:20d8ef79d787 315 } //if
wim 0:20d8ef79d787 316 } //while
wim 0:20d8ef79d787 317
wim 0:20d8ef79d787 318 pc.printf("\nBye World!\n");
wim 0:20d8ef79d787 319 }
wim 0:20d8ef79d787 320