Dependents: TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more
drv/serial/buf/SerialBuf.cpp@0:ac1725ba162c, 2010-11-09 (annotated)
- Committer:
- segundo
- Date:
- Tue Nov 09 20:54:15 2010 +0000
- Revision:
- 0:ac1725ba162c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
segundo | 0:ac1725ba162c | 1 | |
segundo | 0:ac1725ba162c | 2 | /* |
segundo | 0:ac1725ba162c | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
segundo | 0:ac1725ba162c | 4 | |
segundo | 0:ac1725ba162c | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
segundo | 0:ac1725ba162c | 6 | of this software and associated documentation files (the "Software"), to deal |
segundo | 0:ac1725ba162c | 7 | in the Software without restriction, including without limitation the rights |
segundo | 0:ac1725ba162c | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
segundo | 0:ac1725ba162c | 9 | copies of the Software, and to permit persons to whom the Software is |
segundo | 0:ac1725ba162c | 10 | furnished to do so, subject to the following conditions: |
segundo | 0:ac1725ba162c | 11 | |
segundo | 0:ac1725ba162c | 12 | The above copyright notice and this permission notice shall be included in |
segundo | 0:ac1725ba162c | 13 | all copies or substantial portions of the Software. |
segundo | 0:ac1725ba162c | 14 | |
segundo | 0:ac1725ba162c | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
segundo | 0:ac1725ba162c | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
segundo | 0:ac1725ba162c | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
segundo | 0:ac1725ba162c | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
segundo | 0:ac1725ba162c | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
segundo | 0:ac1725ba162c | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
segundo | 0:ac1725ba162c | 21 | THE SOFTWARE. |
segundo | 0:ac1725ba162c | 22 | */ |
segundo | 0:ac1725ba162c | 23 | |
segundo | 0:ac1725ba162c | 24 | #include "SerialBuf.h" |
segundo | 0:ac1725ba162c | 25 | #include "mbed.h" |
segundo | 0:ac1725ba162c | 26 | |
segundo | 0:ac1725ba162c | 27 | //#define __DEBUG |
segundo | 0:ac1725ba162c | 28 | #include "dbg/dbg.h" |
segundo | 0:ac1725ba162c | 29 | |
segundo | 0:ac1725ba162c | 30 | #include "netCfg.h" |
segundo | 0:ac1725ba162c | 31 | #if NET_GPRS |
segundo | 0:ac1725ba162c | 32 | |
segundo | 0:ac1725ba162c | 33 | #if NET_USB_SERIAL |
segundo | 0:ac1725ba162c | 34 | #define m_pStream( a ) (m_pSerial?m_pSerial->a:m_pUsbSerial->a) |
segundo | 0:ac1725ba162c | 35 | #else |
segundo | 0:ac1725ba162c | 36 | #define m_pStream( a ) (m_pSerial->a) |
segundo | 0:ac1725ba162c | 37 | #endif |
segundo | 0:ac1725ba162c | 38 | |
segundo | 0:ac1725ba162c | 39 | //Circular buf |
segundo | 0:ac1725ba162c | 40 | |
segundo | 0:ac1725ba162c | 41 | SerialCircularBuf::SerialCircularBuf(int len) : m_readMode(false) |
segundo | 0:ac1725ba162c | 42 | { |
segundo | 0:ac1725ba162c | 43 | m_buf = new char[len]; |
segundo | 0:ac1725ba162c | 44 | m_len = len; |
segundo | 0:ac1725ba162c | 45 | m_pReadStart = m_pRead = m_buf; |
segundo | 0:ac1725ba162c | 46 | m_pWrite = m_buf; |
segundo | 0:ac1725ba162c | 47 | } |
segundo | 0:ac1725ba162c | 48 | |
segundo | 0:ac1725ba162c | 49 | SerialCircularBuf::~SerialCircularBuf() |
segundo | 0:ac1725ba162c | 50 | { |
segundo | 0:ac1725ba162c | 51 | if(m_buf) |
segundo | 0:ac1725ba162c | 52 | delete[] m_buf; |
segundo | 0:ac1725ba162c | 53 | } |
segundo | 0:ac1725ba162c | 54 | |
segundo | 0:ac1725ba162c | 55 | int SerialCircularBuf::room() //Return room available in buf |
segundo | 0:ac1725ba162c | 56 | { |
segundo | 0:ac1725ba162c | 57 | //return m_len - len() - 1; //-1 is to avoid loop |
segundo | 0:ac1725ba162c | 58 | if ( m_pReadStart > m_pWrite ) |
segundo | 0:ac1725ba162c | 59 | return ( m_pReadStart - m_pWrite - 1 ); |
segundo | 0:ac1725ba162c | 60 | else |
segundo | 0:ac1725ba162c | 61 | return m_len - ( m_pWrite - m_pReadStart ) - 1; |
segundo | 0:ac1725ba162c | 62 | } |
segundo | 0:ac1725ba162c | 63 | |
segundo | 0:ac1725ba162c | 64 | int SerialCircularBuf::len() //Return chars length in buf |
segundo | 0:ac1725ba162c | 65 | { |
segundo | 0:ac1725ba162c | 66 | if ( m_pWrite >= m_pRead ) |
segundo | 0:ac1725ba162c | 67 | return ( m_pWrite - m_pRead ); |
segundo | 0:ac1725ba162c | 68 | else |
segundo | 0:ac1725ba162c | 69 | return m_len - ( m_pRead - m_pWrite ); // = ( ( m_buf + m_len) - m_pRead ) + ( m_pWrite - m_buf ) |
segundo | 0:ac1725ba162c | 70 | } |
segundo | 0:ac1725ba162c | 71 | |
segundo | 0:ac1725ba162c | 72 | void SerialCircularBuf::write(char c) |
segundo | 0:ac1725ba162c | 73 | { |
segundo | 0:ac1725ba162c | 74 | #if 0 |
segundo | 0:ac1725ba162c | 75 | if(!room()) |
segundo | 0:ac1725ba162c | 76 | return; |
segundo | 0:ac1725ba162c | 77 | #endif |
segundo | 0:ac1725ba162c | 78 | //WARN: Must call room() before |
segundo | 0:ac1725ba162c | 79 | *m_pWrite = c; |
segundo | 0:ac1725ba162c | 80 | m_pWrite++; |
segundo | 0:ac1725ba162c | 81 | if(m_pWrite>=m_buf+m_len) |
segundo | 0:ac1725ba162c | 82 | m_pWrite=m_buf; |
segundo | 0:ac1725ba162c | 83 | } |
segundo | 0:ac1725ba162c | 84 | char SerialCircularBuf::read() |
segundo | 0:ac1725ba162c | 85 | { |
segundo | 0:ac1725ba162c | 86 | #if 0 |
segundo | 0:ac1725ba162c | 87 | if(!len()) |
segundo | 0:ac1725ba162c | 88 | return 0; |
segundo | 0:ac1725ba162c | 89 | #endif |
segundo | 0:ac1725ba162c | 90 | //WARN: Must call len() before |
segundo | 0:ac1725ba162c | 91 | char c = *m_pRead; |
segundo | 0:ac1725ba162c | 92 | m_pRead++; |
segundo | 0:ac1725ba162c | 93 | |
segundo | 0:ac1725ba162c | 94 | if(m_pRead>=m_buf+m_len) |
segundo | 0:ac1725ba162c | 95 | m_pRead=m_buf; |
segundo | 0:ac1725ba162c | 96 | |
segundo | 0:ac1725ba162c | 97 | if(!m_readMode) //If readmode=false, trash this char |
segundo | 0:ac1725ba162c | 98 | m_pReadStart=m_pRead; |
segundo | 0:ac1725ba162c | 99 | |
segundo | 0:ac1725ba162c | 100 | return c; |
segundo | 0:ac1725ba162c | 101 | } |
segundo | 0:ac1725ba162c | 102 | |
segundo | 0:ac1725ba162c | 103 | void SerialCircularBuf::setReadMode(bool readMode) //If true, keeps chars in buf when read, false by default |
segundo | 0:ac1725ba162c | 104 | { |
segundo | 0:ac1725ba162c | 105 | if(m_readMode == true && readMode == false) |
segundo | 0:ac1725ba162c | 106 | { |
segundo | 0:ac1725ba162c | 107 | //Trash bytes that have been read |
segundo | 0:ac1725ba162c | 108 | flushRead(); |
segundo | 0:ac1725ba162c | 109 | } |
segundo | 0:ac1725ba162c | 110 | m_readMode = readMode; |
segundo | 0:ac1725ba162c | 111 | } |
segundo | 0:ac1725ba162c | 112 | |
segundo | 0:ac1725ba162c | 113 | void SerialCircularBuf::flushRead() //Delete chars that have been read & return chars len (only useful with readMode = true) |
segundo | 0:ac1725ba162c | 114 | { |
segundo | 0:ac1725ba162c | 115 | m_pReadStart = m_pRead; |
segundo | 0:ac1725ba162c | 116 | } |
segundo | 0:ac1725ba162c | 117 | |
segundo | 0:ac1725ba162c | 118 | void SerialCircularBuf::resetRead() //Go back to initial read position & return chars len (only useful with readMode = true) |
segundo | 0:ac1725ba162c | 119 | { |
segundo | 0:ac1725ba162c | 120 | m_pRead = m_pReadStart; |
segundo | 0:ac1725ba162c | 121 | } |
segundo | 0:ac1725ba162c | 122 | |
segundo | 0:ac1725ba162c | 123 | //SerialBuf |
segundo | 0:ac1725ba162c | 124 | |
segundo | 0:ac1725ba162c | 125 | SerialBuf::SerialBuf(int len) : m_rxBuf(len), m_txBuf(len), m_pSerial(NULL) //Buffer length |
segundo | 0:ac1725ba162c | 126 | #if NET_USB_SERIAL |
segundo | 0:ac1725ba162c | 127 | , m_pUsbSerial(NULL) |
segundo | 0:ac1725ba162c | 128 | #endif |
segundo | 0:ac1725ba162c | 129 | { |
segundo | 0:ac1725ba162c | 130 | DBG("New Serial buf@%p\n", this); |
segundo | 0:ac1725ba162c | 131 | } |
segundo | 0:ac1725ba162c | 132 | |
segundo | 0:ac1725ba162c | 133 | SerialBuf::~SerialBuf() |
segundo | 0:ac1725ba162c | 134 | { |
segundo | 0:ac1725ba162c | 135 | |
segundo | 0:ac1725ba162c | 136 | } |
segundo | 0:ac1725ba162c | 137 | |
segundo | 0:ac1725ba162c | 138 | void SerialBuf::attach(Serial* pSerial) |
segundo | 0:ac1725ba162c | 139 | { |
segundo | 0:ac1725ba162c | 140 | DBG("Serial buf@%p in attach\n", this); |
segundo | 0:ac1725ba162c | 141 | m_pSerial = pSerial; |
segundo | 0:ac1725ba162c | 142 | m_pSerial->attach<SerialBuf>(this, &SerialBuf::onRxInterrupt, Serial::RxIrq); |
segundo | 0:ac1725ba162c | 143 | m_pSerial->attach<SerialBuf>(this, &SerialBuf::onTxInterrupt, Serial::TxIrq); |
segundo | 0:ac1725ba162c | 144 | onRxInterrupt(); //Read data |
segundo | 0:ac1725ba162c | 145 | } |
segundo | 0:ac1725ba162c | 146 | |
segundo | 0:ac1725ba162c | 147 | void SerialBuf::detach() |
segundo | 0:ac1725ba162c | 148 | { |
segundo | 0:ac1725ba162c | 149 | if(m_pSerial) |
segundo | 0:ac1725ba162c | 150 | { |
segundo | 0:ac1725ba162c | 151 | m_pSerial->attach<SerialBuf>(NULL, NULL, Serial::RxIrq); |
segundo | 0:ac1725ba162c | 152 | m_pSerial->attach<SerialBuf>(NULL, NULL, Serial::TxIrq); |
segundo | 0:ac1725ba162c | 153 | m_pSerial = NULL; |
segundo | 0:ac1725ba162c | 154 | } |
segundo | 0:ac1725ba162c | 155 | #if NET_USB_SERIAL |
segundo | 0:ac1725ba162c | 156 | else if(m_pUsbSerial) |
segundo | 0:ac1725ba162c | 157 | { |
segundo | 0:ac1725ba162c | 158 | m_pUsbSerial->attach<SerialBuf>(NULL, NULL, UsbSerial::RxIrq); |
segundo | 0:ac1725ba162c | 159 | m_pUsbSerial->attach<SerialBuf>(NULL, NULL, UsbSerial::TxIrq); |
segundo | 0:ac1725ba162c | 160 | m_pUsbSerial = NULL; |
segundo | 0:ac1725ba162c | 161 | } |
segundo | 0:ac1725ba162c | 162 | #endif |
segundo | 0:ac1725ba162c | 163 | } |
segundo | 0:ac1725ba162c | 164 | |
segundo | 0:ac1725ba162c | 165 | #if NET_USB_SERIAL |
segundo | 0:ac1725ba162c | 166 | void SerialBuf::attach(UsbSerial* pUsbSerial) |
segundo | 0:ac1725ba162c | 167 | { |
segundo | 0:ac1725ba162c | 168 | m_pUsbSerial = pUsbSerial; |
segundo | 0:ac1725ba162c | 169 | m_pUsbSerial->attach<SerialBuf>(this, &SerialBuf::onRxInterrupt, UsbSerial::RxIrq); |
segundo | 0:ac1725ba162c | 170 | m_pUsbSerial->attach<SerialBuf>(this, &SerialBuf::onTxInterrupt, UsbSerial::TxIrq); |
segundo | 0:ac1725ba162c | 171 | onRxInterrupt(); //Read data |
segundo | 0:ac1725ba162c | 172 | } |
segundo | 0:ac1725ba162c | 173 | #endif |
segundo | 0:ac1725ba162c | 174 | |
segundo | 0:ac1725ba162c | 175 | char SerialBuf::getc() |
segundo | 0:ac1725ba162c | 176 | { |
segundo | 0:ac1725ba162c | 177 | while(!readable()); |
segundo | 0:ac1725ba162c | 178 | char c = m_rxBuf.read(); |
segundo | 0:ac1725ba162c | 179 | return c; |
segundo | 0:ac1725ba162c | 180 | } |
segundo | 0:ac1725ba162c | 181 | |
segundo | 0:ac1725ba162c | 182 | void SerialBuf::putc(char c) |
segundo | 0:ac1725ba162c | 183 | { |
segundo | 0:ac1725ba162c | 184 | while(!writeable()); |
segundo | 0:ac1725ba162c | 185 | m_txBuf.write(c); |
segundo | 0:ac1725ba162c | 186 | onTxInterrupt(); |
segundo | 0:ac1725ba162c | 187 | } |
segundo | 0:ac1725ba162c | 188 | |
segundo | 0:ac1725ba162c | 189 | bool SerialBuf::readable() |
segundo | 0:ac1725ba162c | 190 | { |
segundo | 0:ac1725ba162c | 191 | if( !m_rxBuf.len() ) //Fill buf if possible |
segundo | 0:ac1725ba162c | 192 | onRxInterrupt(); |
segundo | 0:ac1725ba162c | 193 | return (m_rxBuf.len() > 0); |
segundo | 0:ac1725ba162c | 194 | } |
segundo | 0:ac1725ba162c | 195 | |
segundo | 0:ac1725ba162c | 196 | bool SerialBuf::writeable() |
segundo | 0:ac1725ba162c | 197 | { |
segundo | 0:ac1725ba162c | 198 | if( !m_txBuf.room() ) //Free buf is possible |
segundo | 0:ac1725ba162c | 199 | onTxInterrupt(); |
segundo | 0:ac1725ba162c | 200 | return (m_txBuf.room() > 0); |
segundo | 0:ac1725ba162c | 201 | } |
segundo | 0:ac1725ba162c | 202 | |
segundo | 0:ac1725ba162c | 203 | void SerialBuf::setReadMode(bool readMode) //If true, keeps chars in buf when read, false by default |
segundo | 0:ac1725ba162c | 204 | { |
segundo | 0:ac1725ba162c | 205 | m_rxBuf.setReadMode(readMode); |
segundo | 0:ac1725ba162c | 206 | } |
segundo | 0:ac1725ba162c | 207 | |
segundo | 0:ac1725ba162c | 208 | void SerialBuf::flushRead() //Delete chars that have been read & return chars len (only useful with readMode = true) |
segundo | 0:ac1725ba162c | 209 | { |
segundo | 0:ac1725ba162c | 210 | m_rxBuf.flushRead(); |
segundo | 0:ac1725ba162c | 211 | } |
segundo | 0:ac1725ba162c | 212 | |
segundo | 0:ac1725ba162c | 213 | void SerialBuf::resetRead() //Go back to initial read position & return chars len (only useful with readMode = true) |
segundo | 0:ac1725ba162c | 214 | { |
segundo | 0:ac1725ba162c | 215 | m_rxBuf.resetRead(); |
segundo | 0:ac1725ba162c | 216 | } |
segundo | 0:ac1725ba162c | 217 | |
segundo | 0:ac1725ba162c | 218 | void SerialBuf::onRxInterrupt() //Callback from m_pSerial |
segundo | 0:ac1725ba162c | 219 | { |
segundo | 0:ac1725ba162c | 220 | while( m_rxBuf.room() && m_pStream(readable()) ) |
segundo | 0:ac1725ba162c | 221 | { |
segundo | 0:ac1725ba162c | 222 | m_rxBuf.write(m_pStream(getc())); |
segundo | 0:ac1725ba162c | 223 | } |
segundo | 0:ac1725ba162c | 224 | } |
segundo | 0:ac1725ba162c | 225 | |
segundo | 0:ac1725ba162c | 226 | void SerialBuf::onTxInterrupt() //Callback from m_pSerial |
segundo | 0:ac1725ba162c | 227 | { |
segundo | 0:ac1725ba162c | 228 | while( m_txBuf.len() && m_pStream(writeable()) ) |
segundo | 0:ac1725ba162c | 229 | { |
segundo | 0:ac1725ba162c | 230 | m_pStream(putc(m_txBuf.read())); |
segundo | 0:ac1725ba162c | 231 | } |
segundo | 0:ac1725ba162c | 232 | } |
segundo | 0:ac1725ba162c | 233 | |
segundo | 0:ac1725ba162c | 234 | |
segundo | 0:ac1725ba162c | 235 | #endif |