private fork
Fork of GSwifiInterface by
GSwifi/GSwifi_hal.cpp@22:2085f090fe31, 2014-06-06 (annotated)
- Committer:
- okini3939
- Date:
- Fri Jun 06 00:41:27 2014 +0000
- Revision:
- 22:2085f090fe31
- Parent:
- 19:d035412a7803
modify httpd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsfan | 8:64184a968e3b | 1 | /* Copyright (C) 2013 gsfan, MIT License |
gsfan | 8:64184a968e3b | 2 | * |
gsfan | 8:64184a968e3b | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
gsfan | 8:64184a968e3b | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
gsfan | 8:64184a968e3b | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
gsfan | 8:64184a968e3b | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
gsfan | 8:64184a968e3b | 7 | * furnished to do so, subject to the following conditions: |
gsfan | 8:64184a968e3b | 8 | * |
gsfan | 8:64184a968e3b | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
gsfan | 8:64184a968e3b | 10 | * substantial portions of the Software. |
gsfan | 8:64184a968e3b | 11 | * |
gsfan | 8:64184a968e3b | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
gsfan | 8:64184a968e3b | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
gsfan | 8:64184a968e3b | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
gsfan | 8:64184a968e3b | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
gsfan | 8:64184a968e3b | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
gsfan | 8:64184a968e3b | 17 | */ |
gsfan | 8:64184a968e3b | 18 | |
gsfan | 8:64184a968e3b | 19 | #include "GSwifi.h" |
gsfan | 8:64184a968e3b | 20 | |
gsfan | 8:64184a968e3b | 21 | void GSwifi::setReset (bool flg) { |
gsfan | 8:64184a968e3b | 22 | if (flg) { |
gsfan | 8:64184a968e3b | 23 | // low |
gsfan | 8:64184a968e3b | 24 | _reset.output(); |
gsfan | 8:64184a968e3b | 25 | _reset = 0; |
gsfan | 8:64184a968e3b | 26 | } else { |
gsfan | 8:64184a968e3b | 27 | // high z |
gsfan | 8:64184a968e3b | 28 | _reset.input(); |
gsfan | 8:64184a968e3b | 29 | _reset.mode(PullNone); |
gsfan | 8:64184a968e3b | 30 | } |
gsfan | 8:64184a968e3b | 31 | } |
gsfan | 8:64184a968e3b | 32 | |
gsfan | 8:64184a968e3b | 33 | void GSwifi::setAlarm (bool flg) { |
gsfan | 8:64184a968e3b | 34 | if (_alarm == NULL) return; |
gsfan | 8:64184a968e3b | 35 | |
gsfan | 8:64184a968e3b | 36 | if (flg) { |
gsfan | 8:64184a968e3b | 37 | // low |
gsfan | 8:64184a968e3b | 38 | _alarm->output(); // low |
gsfan | 8:64184a968e3b | 39 | _alarm->write(0); |
gsfan | 8:64184a968e3b | 40 | } else { |
gsfan | 8:64184a968e3b | 41 | // high |
gsfan | 8:64184a968e3b | 42 | _alarm->input(); // high |
gsfan | 8:64184a968e3b | 43 | _alarm->mode(PullUp); |
gsfan | 8:64184a968e3b | 44 | } |
gsfan | 8:64184a968e3b | 45 | } |
gsfan | 8:64184a968e3b | 46 | |
gsfan | 8:64184a968e3b | 47 | void GSwifi::isrUart () { |
gsfan | 8:64184a968e3b | 48 | recvData(getUart()); |
gsfan | 8:64184a968e3b | 49 | } |
gsfan | 8:64184a968e3b | 50 | |
gsfan | 8:64184a968e3b | 51 | int GSwifi::getUart () { |
gsfan | 8:64184a968e3b | 52 | #ifdef CFG_UART_DIRECT |
gsfan | 8:64184a968e3b | 53 | return _uart->RBR; |
gsfan | 8:64184a968e3b | 54 | #else |
gsfan | 8:64184a968e3b | 55 | return _gs.getc(); |
gsfan | 8:64184a968e3b | 56 | #endif |
gsfan | 8:64184a968e3b | 57 | } |
gsfan | 8:64184a968e3b | 58 | |
gsfan | 8:64184a968e3b | 59 | void GSwifi::putUart (char c) { |
gsfan | 8:64184a968e3b | 60 | #ifdef CFG_UART_DIRECT |
gsfan | 8:64184a968e3b | 61 | while(!(_uart->LSR & (1<<5))); |
gsfan | 8:64184a968e3b | 62 | _uart->THR = c; |
gsfan | 8:64184a968e3b | 63 | #else |
gsfan | 8:64184a968e3b | 64 | _gs.putc(c); |
gsfan | 8:64184a968e3b | 65 | #endif |
gsfan | 8:64184a968e3b | 66 | } |
gsfan | 8:64184a968e3b | 67 | |
gsfan | 8:64184a968e3b | 68 | void GSwifi::setRts (bool flg) { |
gsfan | 8:64184a968e3b | 69 | if (flg) { |
gsfan | 8:64184a968e3b | 70 | // low |
gsfan | 8:64184a968e3b | 71 | if (_flow == 1) { |
gsfan | 16:105b56570533 | 72 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC11U24) || defined(TARGET_LPC4088) || defined(TARGET_LPC176X) || defined(TARGET_LPC11UXX) || defined(TARGET_LPC408X) |
gsfan | 8:64184a968e3b | 73 | _uart->MCR |= (1<<6); // RTSEN |
gsfan | 8:64184a968e3b | 74 | #endif |
gsfan | 8:64184a968e3b | 75 | } else |
gsfan | 8:64184a968e3b | 76 | if (_flow == 2) { |
gsfan | 8:64184a968e3b | 77 | if (_rts) { |
gsfan | 8:64184a968e3b | 78 | _rts->write(0); // low |
gsfan | 8:64184a968e3b | 79 | } |
gsfan | 8:64184a968e3b | 80 | } |
gsfan | 8:64184a968e3b | 81 | } else { |
gsfan | 8:64184a968e3b | 82 | // high |
gsfan | 8:64184a968e3b | 83 | if (_flow == 1) { |
gsfan | 16:105b56570533 | 84 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC11U24) || defined(TARGET_LPC4088) || defined(TARGET_LPC176X) || defined(TARGET_LPC11UXX) || defined(TARGET_LPC408X) |
gsfan | 8:64184a968e3b | 85 | _uart->MCR &= ~(1<<6); // RTS off |
gsfan | 8:64184a968e3b | 86 | _uart->MCR &= ~(1<<1); |
gsfan | 8:64184a968e3b | 87 | #endif |
gsfan | 8:64184a968e3b | 88 | } else |
gsfan | 8:64184a968e3b | 89 | if (_flow == 2) { |
gsfan | 8:64184a968e3b | 90 | if (_rts) { |
gsfan | 8:64184a968e3b | 91 | _rts->write(1); // high |
gsfan | 8:64184a968e3b | 92 | } |
gsfan | 8:64184a968e3b | 93 | } |
gsfan | 8:64184a968e3b | 94 | } |
gsfan | 8:64184a968e3b | 95 | } |
gsfan | 8:64184a968e3b | 96 | |
gsfan | 8:64184a968e3b | 97 | int GSwifi::lockUart (int ms) { |
gsfan | 8:64184a968e3b | 98 | Timer t; |
gsfan | 8:64184a968e3b | 99 | |
gsfan | 8:64184a968e3b | 100 | if (_state.mode != MODE_COMMAND) { |
gsfan | 8:64184a968e3b | 101 | t.start(); |
gsfan | 8:64184a968e3b | 102 | while (_state.mode != MODE_COMMAND) { |
gsfan | 8:64184a968e3b | 103 | if (t.read_ms() >= ms) { |
gsfan | 12:057089026a20 | 104 | WARN("lock timeout (%d)\r\n", _state.mode); |
gsfan | 8:64184a968e3b | 105 | return -1; |
gsfan | 8:64184a968e3b | 106 | } |
gsfan | 8:64184a968e3b | 107 | } |
gsfan | 8:64184a968e3b | 108 | } |
gsfan | 8:64184a968e3b | 109 | |
gsfan | 11:71d67fea5ace | 110 | #ifdef CFG_ENABLE_RTOS |
gsfan | 8:64184a968e3b | 111 | if (_mutexUart.lock(ms) != osOK) return -1; |
gsfan | 8:64184a968e3b | 112 | #endif |
gsfan | 8:64184a968e3b | 113 | |
gsfan | 8:64184a968e3b | 114 | if (_flow == 1) { |
gsfan | 16:105b56570533 | 115 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC11U24) || defined(TARGET_LPC4088) || defined(TARGET_LPC176X) || defined(TARGET_LPC11UXX) || defined(TARGET_LPC408X) |
gsfan | 8:64184a968e3b | 116 | if (!(_uart->MSR & (1<<4))) { |
gsfan | 8:64184a968e3b | 117 | // CTS check |
gsfan | 8:64184a968e3b | 118 | t.start(); |
gsfan | 8:64184a968e3b | 119 | while (!(_uart->MSR & (1<<4))) { |
gsfan | 8:64184a968e3b | 120 | if (t.read_ms() >= ms) { |
gsfan | 8:64184a968e3b | 121 | DBG("cts timeout\r\n"); |
gsfan | 8:64184a968e3b | 122 | return -1; |
gsfan | 8:64184a968e3b | 123 | } |
gsfan | 8:64184a968e3b | 124 | } |
gsfan | 8:64184a968e3b | 125 | } |
gsfan | 8:64184a968e3b | 126 | #endif |
gsfan | 8:64184a968e3b | 127 | } else |
gsfan | 8:64184a968e3b | 128 | if (_flow == 2) { |
gsfan | 8:64184a968e3b | 129 | if (_cts && _cts->read()) { |
gsfan | 8:64184a968e3b | 130 | // CTS check |
gsfan | 8:64184a968e3b | 131 | t.start(); |
gsfan | 8:64184a968e3b | 132 | while (_cts->read()) { |
gsfan | 8:64184a968e3b | 133 | if (t.read_ms() >= ms) { |
gsfan | 8:64184a968e3b | 134 | DBG("cts timeout\r\n"); |
gsfan | 8:64184a968e3b | 135 | return -1; |
gsfan | 8:64184a968e3b | 136 | } |
gsfan | 8:64184a968e3b | 137 | } |
gsfan | 8:64184a968e3b | 138 | } |
gsfan | 8:64184a968e3b | 139 | } |
gsfan | 11:71d67fea5ace | 140 | |
gsfan | 8:64184a968e3b | 141 | setRts(false); |
gsfan | 8:64184a968e3b | 142 | return 0; |
gsfan | 8:64184a968e3b | 143 | } |
gsfan | 8:64184a968e3b | 144 | |
gsfan | 8:64184a968e3b | 145 | void GSwifi::unlockUart () { |
gsfan | 8:64184a968e3b | 146 | setRts(true); |
gsfan | 11:71d67fea5ace | 147 | #ifdef CFG_ENABLE_RTOS |
gsfan | 8:64184a968e3b | 148 | _mutexUart.unlock(); |
gsfan | 8:64184a968e3b | 149 | #endif |
gsfan | 8:64184a968e3b | 150 | } |
gsfan | 8:64184a968e3b | 151 | |
gsfan | 8:64184a968e3b | 152 | void GSwifi::initUart (PinName cts, PinName rts, PinName alarm, int baud) { |
gsfan | 8:64184a968e3b | 153 | |
gsfan | 8:64184a968e3b | 154 | _baud = baud; |
gsfan | 8:64184a968e3b | 155 | if (_baud) _gs.baud(_baud); |
gsfan | 8:64184a968e3b | 156 | _gs.attach(this, &GSwifi::isrUart, Serial::RxIrq); |
gsfan | 8:64184a968e3b | 157 | |
gsfan | 8:64184a968e3b | 158 | _cts = NULL; |
gsfan | 8:64184a968e3b | 159 | _rts = NULL; |
gsfan | 8:64184a968e3b | 160 | _flow = 0; |
gsfan | 16:105b56570533 | 161 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC176X) |
gsfan | 8:64184a968e3b | 162 | _uart = LPC_UART1; |
gsfan | 8:64184a968e3b | 163 | if (cts == p12) { // CTS input (P0_17) |
gsfan | 8:64184a968e3b | 164 | _uart->MCR |= (1<<7); // CTSEN |
gsfan | 8:64184a968e3b | 165 | LPC_PINCON->PINSEL1 &= ~(3 << 2); |
gsfan | 8:64184a968e3b | 166 | LPC_PINCON->PINSEL1 |= (1 << 2); // UART CTS |
gsfan | 8:64184a968e3b | 167 | } else |
gsfan | 8:64184a968e3b | 168 | if (cts != NC) { |
gsfan | 8:64184a968e3b | 169 | _cts = new DigitalIn(cts); |
gsfan | 8:64184a968e3b | 170 | } |
gsfan | 8:64184a968e3b | 171 | if (rts == P0_22) { // RTS output (P0_22) |
gsfan | 8:64184a968e3b | 172 | _uart->MCR |= (1<<6); // RTSEN |
gsfan | 8:64184a968e3b | 173 | LPC_PINCON->PINSEL1 &= ~(3 << 12); |
gsfan | 8:64184a968e3b | 174 | LPC_PINCON->PINSEL1 |= (1 << 12); // UART RTS |
gsfan | 8:64184a968e3b | 175 | _flow = 1; |
gsfan | 8:64184a968e3b | 176 | } else |
gsfan | 8:64184a968e3b | 177 | if (rts != NC) { |
gsfan | 8:64184a968e3b | 178 | _rts = new DigitalOut(rts); |
gsfan | 8:64184a968e3b | 179 | _flow = 2; |
gsfan | 8:64184a968e3b | 180 | } |
gsfan | 16:105b56570533 | 181 | #elif defined(TARGET_LPC11U24) || defined(TARGET_LPC11UXX) |
gsfan | 8:64184a968e3b | 182 | _uart = LPC_USART; |
gsfan | 8:64184a968e3b | 183 | if (cts == p21) { // CTS input (P0_7) |
gsfan | 8:64184a968e3b | 184 | _uart->MCR |= (1<<7); // CTSEN |
gsfan | 8:64184a968e3b | 185 | LPC_IOCON->PIO0_7 &= ~0x07; |
gsfan | 8:64184a968e3b | 186 | LPC_IOCON->PIO0_7 |= 0x01; // UART CTS |
gsfan | 8:64184a968e3b | 187 | } else |
gsfan | 8:64184a968e3b | 188 | if (cts != NC) { |
gsfan | 8:64184a968e3b | 189 | _cts = new DigitalIn(cts); |
gsfan | 8:64184a968e3b | 190 | } |
gsfan | 8:64184a968e3b | 191 | if (rts == p22) { // RTS output (P0_17) |
gsfan | 8:64184a968e3b | 192 | _uart->MCR |= (1<<6); // RTSEN |
gsfan | 8:64184a968e3b | 193 | LPC_IOCON->PIO0_17 &= ~0x07; |
gsfan | 8:64184a968e3b | 194 | LPC_IOCON->PIO0_17 |= 0x01; // UART RTS |
gsfan | 8:64184a968e3b | 195 | _flow = 1; |
gsfan | 8:64184a968e3b | 196 | } else |
gsfan | 8:64184a968e3b | 197 | if (rts != NC) { |
gsfan | 8:64184a968e3b | 198 | _rts = new DigitalOut(rts); |
gsfan | 8:64184a968e3b | 199 | _flow = 2; |
gsfan | 8:64184a968e3b | 200 | } |
gsfan | 16:105b56570533 | 201 | #elif defined(TARGET_LPC4088) || defined(TARGET_LPC408X) |
gsfan | 19:d035412a7803 | 202 | _uart = (LPC_UART1_TypeDef*)LPC_UART2; |
gsfan | 15:086d1a33a197 | 203 | if (cts != NC) { |
gsfan | 8:64184a968e3b | 204 | _cts = new DigitalIn(cts); |
gsfan | 15:086d1a33a197 | 205 | } |
gsfan | 15:086d1a33a197 | 206 | if (rts != NC) { |
gsfan | 8:64184a968e3b | 207 | _rts = new DigitalOut(rts); |
gsfan | 8:64184a968e3b | 208 | _flow = 2; |
gsfan | 8:64184a968e3b | 209 | } |
gsfan | 8:64184a968e3b | 210 | #else |
gsfan | 15:086d1a33a197 | 211 | if (cts != NC) { |
gsfan | 8:64184a968e3b | 212 | _cts = new DigitalIn(cts); |
gsfan | 15:086d1a33a197 | 213 | } |
gsfan | 15:086d1a33a197 | 214 | if (rts != NC) { |
gsfan | 8:64184a968e3b | 215 | _rts = new DigitalOut(rts); |
gsfan | 8:64184a968e3b | 216 | _flow = 2; |
gsfan | 8:64184a968e3b | 217 | } |
gsfan | 8:64184a968e3b | 218 | #endif |
gsfan | 8:64184a968e3b | 219 | |
gsfan | 8:64184a968e3b | 220 | if (alarm != NC) { |
gsfan | 8:64184a968e3b | 221 | _alarm = new DigitalInOut(alarm); |
gsfan | 8:64184a968e3b | 222 | } else { |
gsfan | 8:64184a968e3b | 223 | _alarm = NULL; |
gsfan | 8:64184a968e3b | 224 | } |
gsfan | 8:64184a968e3b | 225 | } |