GSwifiInterface library (interface for GainSpan Wi-Fi GS1011 modules) Please see https://mbed.org/users/gsfan/notebook/GSwifiInterface/

Dependents:   GSwifiInterface_HelloWorld GSwifiInterface_HelloServo GSwifiInterface_UDPEchoServer GSwifiInterface_UDPEchoClient ... more

Fork of WiflyInterface by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GSwifi_hal.cpp Source File

GSwifi_hal.cpp

00001 /* Copyright (C) 2013 gsfan, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #include "GSwifi.h"
00020 
00021 void GSwifi::setReset (bool flg) {
00022     if (flg) {
00023         // low
00024         _reset.output();
00025         _reset = 0;
00026     } else {
00027         // high z
00028         _reset.input();
00029         _reset.mode(PullNone);
00030     }
00031 }
00032 
00033 void GSwifi::setAlarm (bool flg) {
00034     if (_alarm == NULL) return;
00035 
00036     if (flg) {
00037         // low
00038         _alarm->output(); // low
00039         _alarm->write(0);
00040     } else {
00041         // high
00042         _alarm->input(); // high
00043         _alarm->mode(PullUp);
00044     }
00045 }
00046 
00047 void GSwifi::isrUart () {
00048     recvData(getUart());
00049 }
00050 
00051 int GSwifi::getUart () {
00052 #ifdef CFG_UART_DIRECT
00053     return _uart->RBR;
00054 #else
00055     return _gs.getc();
00056 #endif
00057 }
00058 
00059 void GSwifi::putUart (char c) {
00060 #ifdef CFG_UART_DIRECT
00061     while(!(_uart->LSR & (1<<5)));
00062     _uart->THR = c;
00063 #else
00064     _gs.putc(c);
00065 #endif
00066 }
00067 
00068 void GSwifi::setRts (bool flg) {
00069     if (flg) {
00070         // low
00071         if (_flow == 1) {
00072 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC11U24) || defined(TARGET_LPC4088) || defined(TARGET_LPC176X) || defined(TARGET_LPC11UXX) || defined(TARGET_LPC408X)
00073             _uart->MCR |= (1<<6); // RTSEN
00074 #endif
00075         } else
00076         if (_flow == 2) {
00077             if (_rts) {
00078                 _rts->write(0); // low
00079             }
00080         }
00081     } else {
00082         // high
00083         if (_flow == 1) {
00084 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC11U24) || defined(TARGET_LPC4088) || defined(TARGET_LPC176X) || defined(TARGET_LPC11UXX) || defined(TARGET_LPC408X)
00085             _uart->MCR &= ~(1<<6); // RTS off
00086             _uart->MCR &= ~(1<<1);
00087 #endif
00088         } else
00089         if (_flow == 2) {
00090             if (_rts) {
00091                 _rts->write(1); // high
00092             }
00093         }
00094     }
00095 }
00096 
00097 int GSwifi::lockUart (int ms) {
00098     Timer t;
00099 
00100     if (_state.mode != MODE_COMMAND) {
00101         t.start();
00102         while (_state.mode != MODE_COMMAND) {
00103             if (t.read_ms() >= ms) {
00104                 WARN("lock timeout (%d)\r\n", _state.mode);
00105                 return -1;
00106             }
00107         }
00108     }
00109 
00110 #ifdef CFG_ENABLE_RTOS
00111     if (_mutexUart.lock(ms) != osOK) return -1;
00112 #endif
00113 
00114     if (_flow == 1) {
00115 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC11U24) || defined(TARGET_LPC4088) || defined(TARGET_LPC176X) || defined(TARGET_LPC11UXX) || defined(TARGET_LPC408X)
00116         if (!(_uart->MSR & (1<<4))) {
00117             // CTS check
00118             t.start();
00119             while (!(_uart->MSR & (1<<4))) {
00120                 if (t.read_ms() >= ms) {
00121                     DBG("cts timeout\r\n");
00122                     return -1;
00123                 }
00124             }
00125         }
00126 #endif
00127     } else
00128     if (_flow == 2) {
00129         if (_cts && _cts->read()) {
00130             // CTS check
00131             t.start();
00132             while (_cts->read()) {
00133                 if (t.read_ms() >= ms) {
00134                     DBG("cts timeout\r\n");
00135                     return -1;
00136                 }
00137             }
00138         }
00139     }
00140 
00141     setRts(false);
00142     return 0;
00143 }
00144 
00145 void GSwifi::unlockUart () {
00146     setRts(true);
00147 #ifdef CFG_ENABLE_RTOS
00148     _mutexUart.unlock();
00149 #endif
00150 }
00151 
00152 void GSwifi::initUart (PinName cts, PinName rts, PinName alarm, int baud) {
00153 
00154     _baud = baud;
00155     if (_baud) _gs.baud(_baud);
00156     _gs.attach(this, &GSwifi::isrUart, Serial::RxIrq);
00157 
00158     _cts = NULL;
00159     _rts = NULL;
00160     _flow = 0;
00161 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC176X)
00162     _uart = LPC_UART1;
00163     if (cts == p12) { // CTS input (P0_17)
00164         _uart->MCR |= (1<<7); // CTSEN
00165         LPC_PINCON->PINSEL1 &= ~(3 << 2);
00166         LPC_PINCON->PINSEL1 |= (1 << 2); // UART CTS
00167     } else
00168     if (cts != NC) {
00169         _cts = new DigitalIn(cts);
00170     }
00171     if (rts == P0_22) { // RTS output (P0_22)
00172         _uart->MCR |= (1<<6); // RTSEN
00173         LPC_PINCON->PINSEL1 &= ~(3 << 12);
00174         LPC_PINCON->PINSEL1 |= (1 << 12); // UART RTS
00175         _flow = 1;
00176     } else
00177     if (rts != NC) {
00178         _rts = new DigitalOut(rts);
00179         _flow = 2;
00180     }
00181 #elif defined(TARGET_LPC11U24) || defined(TARGET_LPC11UXX)
00182     _uart = LPC_USART;
00183     if (cts == p21) { // CTS input (P0_7)
00184         _uart->MCR |= (1<<7); // CTSEN
00185         LPC_IOCON->PIO0_7 &= ~0x07;
00186         LPC_IOCON->PIO0_7 |= 0x01; // UART CTS
00187     } else
00188     if (cts != NC) {
00189         _cts = new DigitalIn(cts);
00190     }
00191     if (rts == p22) { // RTS output (P0_17)
00192         _uart->MCR |= (1<<6); // RTSEN
00193         LPC_IOCON->PIO0_17 &= ~0x07;
00194         LPC_IOCON->PIO0_17 |= 0x01; // UART RTS
00195         _flow = 1;
00196     } else
00197     if (rts != NC) {
00198         _rts = new DigitalOut(rts);
00199         _flow = 2;
00200     }
00201 #elif defined(TARGET_LPC4088) || defined(TARGET_LPC408X)
00202     _uart = (LPC_UART1_TypeDef*)LPC_UART2;
00203     if (cts != NC) {
00204         _cts = new DigitalIn(cts);
00205     }
00206     if (rts != NC) {
00207         _rts = new DigitalOut(rts);
00208         _flow = 2;
00209     }
00210 #else
00211     if (cts != NC) {
00212         _cts = new DigitalIn(cts);
00213     }
00214     if (rts != NC) {
00215         _rts = new DigitalOut(rts);
00216         _flow = 2;
00217     }
00218 #endif
00219 
00220     if (alarm != NC) {
00221         _alarm = new DigitalInOut(alarm);
00222     } else {
00223         _alarm = NULL;
00224     }
00225 }
00226 
00227 #ifdef CFG_SPI_DATAINTERFACE
00228 void GSwifi::putSpi (char c) {
00229     _spi.write(c);
00230 }
00231 
00232 void GSwifi::isrSpi () {
00233     char c;
00234 
00235     while (_wake == 1) {
00236         c = _spi.write(0xf5);
00237         recvData(c);
00238     }
00239 }
00240 
00241 int GSwifi::lockSpi (int ms) {
00242     Timer t;
00243 
00244     if (_state.mode != MODE_COMMAND) {
00245         t.start();
00246         while (_state.mode != MODE_COMMAND) {
00247             if (t.read_ms() >= ms) {
00248                 WARN("lock timeout (%d)\r\n", _state.mode);
00249                 return -1;
00250             }
00251         }
00252     }
00253 
00254 #ifdef CFG_ENABLE_RTOS
00255     if (_mutexSpi.lock(ms) != osOK) return -1;
00256 #endif
00257 
00258     if (_wake.read()) {
00259         t.start();
00260         while (_wake.read()) {
00261             if (t.read_ms() >= ms) {
00262                 DBG("hostwake timeout\r\n");
00263                 return -1;
00264             }
00265         }
00266     }
00267     return 0;
00268 }
00269 
00270 void GSwifi::unlockSpi () {
00271 #ifdef CFG_ENABLE_RTOS
00272     _mutexSpi.unlock();
00273 #endif
00274 }
00275 
00276 void GSwifi::initSpi (PinName cs, int freq) {
00277     _spi.format(8, 0);
00278     _spi.frequency(freq);
00279     _wake.mode(PullDown);
00280     _wake.rise(this, &GSwifi::isrSpi);
00281 
00282 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC176X)
00283     if (cs == p8) { // SPI_1 (P0_6)
00284         LPC_PINCON->PINSEL0 &= ~(3 << 12);
00285         LPC_PINCON->PINSEL0 |= (2 << 12); // SSEL1
00286     } else
00287     if (cs == p14) { // (SPI_0) P0_16
00288         LPC_PINCON->PINSEL1 &= ~(3 << 0);
00289         LPC_PINCON->PINSEL1 |= (2 << 0); // SSEL0
00290     } else
00291     if (cs == P1_21) { // (SPI_0) P1_21
00292         LPC_PINCON->PINSEL3 &= ~(3 << 10);
00293         LPC_PINCON->PINSEL3 |= (3 << 10); // SSEL0
00294     }
00295 #endif
00296 }
00297 #endif