see http://mbed.org/users/okini3939/notebook/wattmeter-shield-on-mbed/

Dependencies:   mbed

Fork of GSwifi_xively by gs fan

Committer:
okini3939
Date:
Wed Nov 27 08:18:45 2013 +0000
Revision:
4:9a2415f2ab07
update GSwifiInterface library

Who changed what in which revision?

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