Testsoftware for SC16IS750

Dependencies:   SC16IS750 mbed

main.cpp

Committer:
wim
Date:
2014-02-09
Revision:
1:f0d3984f2586
Parent:
0:d83a90125711
Child:
2:8aba07490dce

File content as of revision 1:f0d3984f2586:

#include "mbed.h"
#include "SC16IS750.h"

//SPI Version
SPI spi(PTD2, PTD3, PTD1); //MOSI, MISO, SCK
//DigitalOut CS(PTD0);       //CS 
SC16IS750_SPI serial_bridge(&spi, PTD0);

//I2C Version
//I2C i2c(PTE0, PTE1);       //SDA, SCL
//SC16IS750_I2C serial_bridge(&i2c, DEFAULT_SC16IS750_ADDR);

DigitalOut myled1(LED_RED);
//DigitalOut myled2(LED_GREEN); 
//DigitalOut myled3(LED_BLUE);  // Same as PTD1 (SCK)
DigitalOut heartbeatLED(LED_GREEN);
 
Serial pc(USBTX,USBRX);

void show_menu() {
    pc.printf("0: Exit\n\r");
    pc.printf("1: Show Menu\n\r");    
    pc.printf("2: Init\n\r"); 
    pc.printf("3: IO Out\n\r");     
    pc.printf("4: Echo Text\n\r");        

 #if(0)

    pc.printf("5: \n\r");            
    pc.printf("6: \n\r");                
    pc.printf("7: \n\r");                    
    pc.printf("8: \n\r");                        
    pc.printf("9: \n\r");                            
    pc.printf("A: \n\r");                    
    pc.printf("B: \n\r");                        
    pc.printf("C: \n\r");                    
    pc.printf("D: \n\r");                           
    pc.printf("P: \n\r");                        
#endif    
    pc.printf("\n\r");                
}

// Variables for Heartbeat and Status monitoring
Ticker heartbeat;
bool heartbeatflag=false;

// Local functions
void clear_screen() {
//ANSI Terminal Commands
    pc.printf("\x1B[2J");
    pc.printf("\x1B[H");
}

   
// Heartbeat monitor
void pulse() {
  heartbeatLED = !heartbeatLED;
}

void heartbeat_start() {
  heartbeat.attach(&pulse, 0.5);
}

void heartbeat_stop() {
  heartbeat.detach();
}


int main() {
  bool running=true;
  char command;
  int i=0;
   
  pc.printf("\nHello World!\n\r");

  heartbeat_start();     

  myled1 = 1; // LED Off

 // We need to enable flow control or we overflow buffers and
  // lose data when used with the WiFly. Note that flow control 
  // needs to be enabled on the WiFly for this to work but it's
  // possible to do that with flow control enabled here but not there.
//  serial_bridge.set_flow_control(SC16IS750::RTSCTS);

    serial_bridge.ioSetDirection(0xFF); // All outputs
    serial_bridge.ioSetState(0x00);     // All On

    show_menu();
      
    while(running) {
     
       if(pc.readable()) {
         command = pc.getc();       
         pc.printf("command= %c \n\r", command);         

         switch (command) {
          case '0' :
                     pc.printf("Done\n\r");                    
                     running = false;  
                     break;
                     
          case '1' :
                     show_menu();
                     break;

          case '2' :
                     pc.printf("Init\n\r");                    
                     serial_bridge._init();                      
                     break;
          
          case '3' :        
                     pc.printf("IO Out\n\r");                    
                     
                     i=0;
                     while (!pc.readable()) {
                       serial_bridge.ioSetState(~i); 
                       serial_bridge.ioGetState() ;                       //test
                       wait(0.5);
                       pc.putc('*');                
                       i=(i+1) & 0xFF;
                     }
                     
                     pc.getc();
                     pc.printf("IO Out Done\n\r");                                         
                     break;

          case '4' :        
                     pc.printf("Echo Text, Enter '$' to quit...\n\r");                    
                     
                     char ch;
                     bool running_test=true;
                      
                     while (running_test) {
                       // From SPI/I2C to serial                       
                       while (running_test && pc.readable()) {
                         ch = pc.getc();
                         running_test = (ch != '$');
                         serial_bridge.putc(ch);
                       }
                       
                       // From Serial to SPI/I2C 
                       while (running_test && serial_bridge.readable()) {
                         ch = serial_bridge.getc();
                         running_test = (ch != '$');
                         pc.putc(ch);
                       }
                       
                     }
                     
                     pc.printf("\n\rEcho Text Done\n\r");                                         
                     break;

          default :
                    break;                     

         } //switch
       } //if  
    } //while




#if(0)


//  CS = 1;
 
  spi.format(8, 0);          
//    spi.frequency(100000);    
//    spi.frequency(500000);    
  spi.frequency(1000000);
//    spi.frequency(1500000);    



  while(1) {
    CS=0;
    spi.write(0xAA);
    spi.write(0x81);
    CS=1;        
    wait_us(5);      
  }
#endif
    
  pc.printf("\nBye World!\n");    
}