Advertisement
Guest User

wifly example, fixed

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