Advertisement
baldengineer

WiFly Example, Fixed with Dummy

Jan 2nd, 2012
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * WiFly UART-SPI bridge Example
  3.  * Copyright (c) 2010 SparkFun Electronics.  All right reserved.
  4.  * Written by Chris Taylor
  5.  *
  6.  * This code was written to demonstrate the WiFly Shield from SparkFun Electronics
  7.  *
  8.  * This code will initialize and test the SC16IS750 UART-SPI bridge, and allow
  9.  * transparent communication with the device from a terminal.
  10.  *
  11.  * http://www.sparkfun.com
  12.  */
  13.  
  14. char dummy; //fixes bug in GUI / complier
  15.  
  16. // SCI16IS750 Registers
  17. #define THR        0x00 << 3
  18. #define RHR        0x00 << 3
  19. #define IER        0x01 << 3
  20. #define FCR        0x02 << 3
  21. #define IIR        0x02 << 3
  22. #define LCR        0x03 << 3
  23. #define MCR        0x04 << 3
  24. #define LSR        0x05 << 3
  25. #define MSR        0x06 << 3
  26. #define SPR        0x07 << 3
  27. #define TXFIFO     0x08 << 3
  28. #define RXFIFO     0x09 << 3
  29. #define DLAB       0x80 << 3
  30. #define IODIR      0x0A << 3
  31. #define IOSTATE    0x0B << 3
  32. #define IOINTMSK   0x0C << 3
  33. #define IOCTRL     0x0E << 3
  34. #define EFCR       0x0F << 3
  35.  
  36. #define DLL        0x00 << 3
  37. #define DLM        0x01 << 3
  38. #define EFR        0x02 << 3
  39. #define XON1       0x04 << 3  
  40. #define XON2       0x05 << 3
  41. #define XOFF1      0x06 << 3
  42. #define XOFF2      0x07 << 3
  43.  
  44. // Arduino SPI pins
  45. #define CS         10
  46. #define MOSI       11
  47. #define MISO       12
  48. #define SCK        13
  49.  
  50. // Communication flags and variables
  51. char incoming_data;
  52. char TX_Fifo_Address = THR;
  53.  
  54. char clr = 0;
  55. char polling = 0;
  56.  
  57. // SC16IS750 Configuration Parameters
  58. struct SPI_UART_cfg
  59. {
  60.   char DivL,DivM,DataFormat,Flow;
  61. };
  62.  
  63. struct SPI_UART_cfg SPI_Uart_config = {
  64.   0x50,0x00,0x03,0x10};
  65.  
  66. void setup()
  67. {
  68.   // Initialize SPI pins
  69.   pinMode(MOSI, OUTPUT);
  70.   pinMode(MISO, INPUT);
  71.   pinMode(SCK,OUTPUT);
  72.   pinMode(CS,OUTPUT);
  73.   digitalWrite(CS,HIGH); //disable device
  74.  
  75.   SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);
  76.   clr=SPSR;
  77.   clr=SPDR;
  78.   delay(10);
  79.  
  80.   Serial.begin(9600);
  81.   Serial.println("\n\r\n\rWiFly Shield Terminal Routine");
  82.  
  83.   // Test SPI communication
  84.   if(SPI_Uart_Init()){
  85.     Serial.println("Bridge initialized successfully!");
  86.   }
  87.   else{
  88.     Serial.println("Could not initialize bridge, locking up.\n\r");
  89.     while(1);
  90.   }
  91. }
  92.  
  93. void loop()
  94. {
  95.   // Poll for new data in SC16IS750 Recieve buffer
  96.   if(SPI_Uart_ReadByte(LSR) & 0x01)
  97.   {
  98.     polling = 1;
  99.     while(polling)
  100.     {
  101.       if((SPI_Uart_ReadByte(LSR) & 0x01))
  102.       {
  103.         incoming_data = SPI_Uart_ReadByte(RHR);
  104.         Serial.print(incoming_data);
  105.       }  
  106.       else
  107.       {
  108.         polling = 0;
  109.       }
  110.     }
  111.  
  112.   }
  113.   // Otherwise, send chars from terminal to SC16IS750
  114.   else if(Serial.available())
  115.   {
  116.     incoming_data = Serial.read();
  117.     select();
  118.     spi_transfer(0x00); // Transmit command
  119.     spi_transfer(incoming_data);
  120.     deselect();
  121.   }
  122.  
  123. }
  124.  
  125.  
  126.  
  127. char SPI_Uart_Init(void)
  128. // Initialize SC16IS750
  129. {
  130.   char data = 0;
  131.  
  132.   SPI_Uart_WriteByte(LCR,0x80); // 0x80 to program baudrate
  133.   SPI_Uart_WriteByte(DLL,SPI_Uart_config.DivL); //0x50 = 9600 with Xtal = 12.288MHz
  134.   SPI_Uart_WriteByte(DLM,SPI_Uart_config.DivM);
  135.  
  136.   SPI_Uart_WriteByte(LCR, 0xBF); // access EFR register
  137.   SPI_Uart_WriteByte(EFR, SPI_Uart_config.Flow); // enable enhanced registers
  138.   SPI_Uart_WriteByte(LCR, SPI_Uart_config.DataFormat); // 8 data bit, 1 stop bit, no parity
  139.   SPI_Uart_WriteByte(FCR, 0x06); // reset TXFIFO, reset RXFIFO, non FIFO mode
  140.   SPI_Uart_WriteByte(FCR, 0x01); // enable FIFO mode
  141.  
  142.   // Perform read/write test to check if UART is working
  143.   SPI_Uart_WriteByte(SPR,'H');
  144.   data = SPI_Uart_ReadByte(SPR);
  145.  
  146.   if(data == 'H'){
  147.     return 1;
  148.   }
  149.   else{
  150.     return 0;
  151.   }
  152.  
  153. }
  154.  
  155. void SPI_Uart_WriteByte(char address, char data)
  156. // Write byte to register address on SC16IS750
  157. {
  158.   long int length;
  159.   char senddata[2];
  160.   senddata[0] = address;
  161.   senddata[1] = data;
  162.  
  163.   select();
  164.   length = SPI_Write(senddata, 2);
  165.   deselect();
  166. }
  167.  
  168. long int SPI_Write(char* srcptr, long int length)
  169. // Send entire string to SC16IS750
  170. {
  171.   for(long int i = 0; i < length; i++)
  172.   {
  173.     spi_transfer(srcptr[i]);
  174.   }
  175.   return length;
  176. }
  177.  
  178. void SPI_Uart_WriteArray(char *data, long int NumBytes)
  179. // Send entire string to THR of SC16IS750
  180. {
  181.   long int length;
  182.   select();
  183.   length = SPI_Write(&TX_Fifo_Address,1);
  184.  
  185.   while(NumBytes > 16) // Split array into 16 character chunks
  186.   {
  187.     length = SPI_Write(data,16);
  188.     NumBytes -= 16;
  189.     data += 16;
  190.   }
  191.   length = SPI_Write(data,NumBytes);
  192.  
  193.   deselect();
  194. }
  195.  
  196. char SPI_Uart_ReadByte(char address)
  197. // Read from SC16IS750 register
  198. {
  199.   char data;
  200.  
  201.   address = (address | 0x80);
  202.  
  203.   select();
  204.   spi_transfer(address);
  205.   data = spi_transfer(0xFF);
  206.   deselect();
  207.   return data;  
  208. }
  209.  
  210. void SPI_Uart_println(char *data)
  211. // Write string to SC16IS750 followed by a carriage return
  212. {
  213.   SPI_Uart_WriteArray(data,strlen(data));
  214.   SPI_Uart_WriteByte(THR, 0x0d);
  215. }
  216.  
  217. void SPI_Uart_print(char *data)
  218. // Write string to SC16IS750, no carriage return
  219. {
  220.   SPI_Uart_WriteArray(data,strlen(data));
  221. }
  222.  
  223. char spi_transfer(volatile char data)
  224. {
  225.   SPDR = data;                    // Start the transmission
  226.   while (!(SPSR & (1<<SPIF)))     // Wait for the end of the transmission
  227.   {
  228.   };
  229.   return SPDR;                    // return the received byte
  230. }
  231.  
  232. void select(void)
  233. {
  234.   digitalWrite(CS,LOW);
  235. }
  236.  
  237. void deselect(void)
  238. {
  239.   digitalWrite(CS,HIGH);
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement