Bug fix release

Dependents:   AntiTheftGPS XbeeReceive XbeeSend Superball_Ball2 ... more

MODSERIAL is an easy to use library that extends Serial to add fully buffered input and output.

The features of MODSERIAL include:-

/media/uploads/mbedofficial/serial_interfaces.png

Connecting up the MODSERIAL module

The starting point for using MODSERIAL is the Mbed's own handbook for Serial library object. MODSERIAL inherits Serial and adds extensions for buffering. So getting started is easy. Follow the Mbed instructions for Serial to get setup. Here's a reproduction of Serial's simple code starter:-

1  #include "mbed.h"
2
3  Serial pc(USBTX, USBRX); // tx, rx
4 
5  int main() {
6      pc.printf("Hello World!");
7      while(1) {
8          pc.putc(pc.getc() + 1);
9      }
10 }

All we need to do to use MODSERIAL is to add a #include and alter one line thus:-

1  #include "mbed.h"
2  #include "MODSERIAL.h"
3  MODSERIAL pc(USBTX, USBRX); // tx, rx
4 
5  int main() {
6      pc.printf("Hello World!");
7      while(1) {
8          pc.putc(pc.getc() + 1);
9      }
10 }

As we can see, all we have done is add the header at line 2 and changed line 3 to specify the use of MODSERIAL in replacement for Serial. The default settings for MODSERIAL are that both the TX and RX buffers are assigned 256 bytes each of storage space. This storage space is acquired from the heap using malloc.

The default buffer assignment can be manipulated in three ways. First is the compile time setting which alters the default parameters used when creating a MODSERIAL object. This is done thus:-

1  #include "mbed.h"
2
3  #define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 512
4  #define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 1024 
5  #include "MODSERIAL.h"
6
7  MODSERIAL pc(USBTX, USBRX); // tx, rx
8  ...

By defining the two #defines before the #include "MODSERIAL.h" alters the defaults MODSERIAL uses to create it's buffers.

The second method is the run-time version. To get TX at 1024 and RX buffer at 512 as above during run-time initialisation, alter the constructor thus:-

1  #include "mbed.h"
2  #include "MODSERIAL.h"
3
4  // Make TX buffer 1024bytes and RX buffer use 512bytes.
5  MODSERIAL pc(USBTX, USBRX, 1024, 512); // tx, rx
6  ...

If you supply only one numeric value, as shown below, both TX and RX will have the same buffer sizes assigned to them:-

1  #include "mbed.h"
2  #include "MODSERIAL.h"
3
4  // Make both TX and RX use a 512byte buffer.
5  MODSERIAL pc(USBTX, USBRX, 512); // tx, rx
6  ...

The third method is reassigning a new buffer while the program is running. This allows the program to grow and shrink either buffer as required. However, there are caveats to do this as will be shown below.

First, expanding the buffer involves increasing the buffer size. This is fairly straight forward and is accomplished thus:-

1  #include "mbed.h"
2  #include "MODSERIAL.h"
3  MODSERIAL pc(USBTX, USBRX); // tx, rx
4 
5  int main() {
6
7      // Increase the TX buffer from the default 256bytes to 1024bytes.
8      if (pc.txBufferSetSize(1024) != MODSERIAL::Ok) {
9         error("Failed to allocate memory for new buffer");
10     }
11
12     pc.printf("Hello World!");
13     while(1) {
14         pc.putc(pc.getc() + 1);
15     }
16 }

As can be seen, growing the buffer is fairly straight forward. However, how it is done should be understood by the user. First, a new buffer allocation is made using malloc. Once acquired the current buffer is checked for contents. If the current buffer is not empty it is copied to the new buffer so the old buffer contents is maintained after resizing. The last step is then to free() the old memory buffer.

The buffer can also be contracted to a smaller length buffer. Here's the code:-

1  #include "mbed.h"
2  #include "MODSERIAL.h"
3  MODSERIAL pc(USBTX, USBRX); // tx, rx
4 
5  int main() {
6      int result;
7
8      // Decrease the TX buffer from the default 256bytes to 32bytes.
9      result = pc.txBufferSetSize(32);
10     if (result != MODSERIAL::Ok) {
11         switch(result) {
12             case MODSERIAL::BufferOversize: 
13                 error("Contents too big to fit into new allocation");
14                 break;
15             case MODSERIAL::NoMemory: 
16                 error("Not enough memory for new allocation");
17                 break;
18         }
19     }
11
12     pc.printf("Hello World!");
13     while(1) {
14         pc.putc(pc.getc() + 1);
15     }
16 }

Since buffer resizing involves the copying over of any existing old buffer contents the possibility exists that the current buffer contains more bytes than will fit into the new requested buffer. In these conditions the user must handle the return value of the resize functions. If the contents are of no concern then calling txBufferFlush() to empty of the contents before resizing.

MODSERIAL Interrupts

Users of Serial will be familar with the fact that you can attach functions or methods to TxIrq or RxIrq. This attachment of callbacks allows users to have Interrupt Service Routines (ISR) for both the TX and RX channel of the Uart. MODSERIAL uses both of these callbacks to maintain it's buffers and so are not available to users. However, MODSERIAL does contain five potential callbacks the user can use. These are:-

  • TxIrq - This callback is used to inform the user's program that a character was transferred from the TX buffer to the Uart's TX THR FIFO.
  • RxIrq - This callback is used to inform the user's program that a character was transferred from the Uart's RX FIFO RBR to the RX buffer.
  • RxOvIrq - This callback is used to inform the user's program that a character in the Uart's RX FIFO RBR failed to transfer to the RX buffer because the RX buffer was full. The failed byte is availble via xxGetLastChar() methods.
  • TxOvIrq - As RX overflow above
  • TxEmpty - This callback is made when the last byte in the TX buffer is transferred to the Uart's TX THR FIFO. It informs the user's program that the TX buffer has become empty. However, it does not mean transmission is complete. See the example1.cpp example for more information.

Delineating "packets"

Many devices send information on RS232 interfaces in distinct "packets". As an example of this is NMEA information sent by many GPS modules. Each NMEA sentence is delineated by a '\n' newline character. Each sentence can be of vary length depending upon the information being sent, however, all are seperated by a '\n' newline. Detecting this if very simple with MODSERIAL. Here's an example:-

#include "mbed.h"
#include "MODSERIAL.h"

// Connect the TX of the GPS module to p10 RX input
MODSERIAL gps(NC, p10);

bool newline_detected = false;

// Called everytime a new character goes into
// the RX buffer. Test that character for \n
// Note, rxGetLastChar() gets the last char that
// we received but it does NOT remove it from
// the RX buffer.
void rxCallback(MODSERIAL_IRQ_INFO *q) {
    MODSERIAL *serial = q->serial;
    if ( serial->rxGetLastChar() == '\n') {
    	newline_detected = true;
    }
}

int main() {
    gps.baud(9600);
    gps.attach(&rxCallback, MODSERIAL::RxIrq);

    // Wait here until we detect the \n going into the buffer.
    while (! newline_detected ) ;    
    
    // When we get here the RX buffer now contains a NMEA sentence.
    // ...

}

Note, the txGetLastChar() and rxGetLastChar() methods only return the last character but they do not remove that character from the associated buffer.

If this is your first time using MODSERIAL or would just like to test it out then see the example.cpp that comes with the library.



Committer:
AjK
Date:
Sun Nov 21 02:15:07 2010 +0000
Revision:
1:b7e435fbfe8e
Parent:
0:eb2522b41db8
Child:
2:b936b4acbd92
1.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:eb2522b41db8 1 /*
AjK 0:eb2522b41db8 2 Copyright (c) 2010 Andy Kirkham
AjK 0:eb2522b41db8 3
AjK 0:eb2522b41db8 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 0:eb2522b41db8 5 of this software and associated documentation files (the "Software"), to deal
AjK 0:eb2522b41db8 6 in the Software without restriction, including without limitation the rights
AjK 0:eb2522b41db8 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 0:eb2522b41db8 8 copies of the Software, and to permit persons to whom the Software is
AjK 0:eb2522b41db8 9 furnished to do so, subject to the following conditions:
AjK 0:eb2522b41db8 10
AjK 0:eb2522b41db8 11 The above copyright notice and this permission notice shall be included in
AjK 0:eb2522b41db8 12 all copies or substantial portions of the Software.
AjK 0:eb2522b41db8 13
AjK 0:eb2522b41db8 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 0:eb2522b41db8 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 0:eb2522b41db8 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 0:eb2522b41db8 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 0:eb2522b41db8 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 0:eb2522b41db8 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 0:eb2522b41db8 20 THE SOFTWARE.
AjK 0:eb2522b41db8 21 */
AjK 0:eb2522b41db8 22
AjK 0:eb2522b41db8 23 #ifndef MODSERIAL_H
AjK 0:eb2522b41db8 24 #define MODSERIAL_H
AjK 0:eb2522b41db8 25
AjK 0:eb2522b41db8 26 /** @defgroup API The MODSERIAL API */
AjK 0:eb2522b41db8 27 /** @defgroup MISC Misc MODSERIAL functions */
AjK 0:eb2522b41db8 28 /** @defgroup INTERNALS MODSERIAL Internals */
AjK 0:eb2522b41db8 29
AjK 0:eb2522b41db8 30 #ifndef MODSERIAL_DEFAULT_RX_BUFFER_SIZE
AjK 0:eb2522b41db8 31 #define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 256
AjK 0:eb2522b41db8 32 #endif
AjK 0:eb2522b41db8 33
AjK 0:eb2522b41db8 34 #ifndef MODSERIAL_DEFAULT_TX_BUFFER_SIZE
AjK 0:eb2522b41db8 35 #define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 256
AjK 0:eb2522b41db8 36 #endif
AjK 0:eb2522b41db8 37
AjK 0:eb2522b41db8 38 #include "mbed.h"
AjK 0:eb2522b41db8 39
AjK 0:eb2522b41db8 40 namespace AjK {
AjK 0:eb2522b41db8 41
AjK 0:eb2522b41db8 42 /**
AjK 0:eb2522b41db8 43 * @author Andy Kirkham
AjK 0:eb2522b41db8 44 * @see http://mbed.org/cookbook/MODSERIAL
AjK 0:eb2522b41db8 45 * @see http://mbed.org/handbook/Serial
AjK 0:eb2522b41db8 46 * @see example.cpp
AjK 0:eb2522b41db8 47 * @see API
AjK 0:eb2522b41db8 48 *
AjK 0:eb2522b41db8 49 * <b>MODSERIAL</b> extends the Mbed library <a href="/handbook/Serial">Serial</a> to provide fully buffered
AjK 0:eb2522b41db8 50 * TX and RX streams. Buffer length is fully customisable.
AjK 0:eb2522b41db8 51 *
AjK 0:eb2522b41db8 52 * Before using MODSERIAL users should be familar with Mbed's standard <a href="/handbook/Serial">Serial</a>
AjK 0:eb2522b41db8 53 * library object. MODSERIAL is a direct "drop in" replacement for <a href="/handbook/Serial">Serial</a>. Where
AjK 0:eb2522b41db8 54 * previously Serial was used, MODSERIAL can be used as adirect replacement instantly offering standard
AjK 0:eb2522b41db8 55 * TX and RX buffering. By default, both TX and RX buffers are 256 bytes in length.
AjK 0:eb2522b41db8 56 *
AjK 0:eb2522b41db8 57 * @image html /media/uploads/mbedofficial/serial_interfaces.png
AjK 0:eb2522b41db8 58 *
AjK 0:eb2522b41db8 59 * Standard example:
AjK 0:eb2522b41db8 60 * @code
AjK 0:eb2522b41db8 61 * #include "mbed.h"
AjK 0:eb2522b41db8 62 * #include "MODSERIAL.h"
AjK 0:eb2522b41db8 63 *
AjK 0:eb2522b41db8 64 * MODSERIAL pc(USBTX, USBRX); // tx, rx
AjK 0:eb2522b41db8 65 *
AjK 0:eb2522b41db8 66 * int main() {
AjK 0:eb2522b41db8 67 * pc.printf("Hello World!");
AjK 0:eb2522b41db8 68 * while(1) {
AjK 0:eb2522b41db8 69 * pc.putc(pc.getc() + 1);
AjK 0:eb2522b41db8 70 * }
AjK 0:eb2522b41db8 71 * }
AjK 0:eb2522b41db8 72 * @endcode
AjK 0:eb2522b41db8 73 *
AjK 0:eb2522b41db8 74 * Example with alternate buffer length:
AjK 0:eb2522b41db8 75 * @code
AjK 0:eb2522b41db8 76 * #include "mbed.h"
AjK 0:eb2522b41db8 77 * #include "MODSERIAL.h"
AjK 0:eb2522b41db8 78 *
AjK 0:eb2522b41db8 79 * // Make TX and RX buffers 512byes in length
AjK 0:eb2522b41db8 80 * MODSERIAL pc(USBTX, USBRX, 512); // tx, rx
AjK 0:eb2522b41db8 81 *
AjK 0:eb2522b41db8 82 * int main() {
AjK 0:eb2522b41db8 83 * pc.printf("Hello World!");
AjK 0:eb2522b41db8 84 * while(1) {
AjK 0:eb2522b41db8 85 * pc.putc(pc.getc() + 1);
AjK 0:eb2522b41db8 86 * }
AjK 0:eb2522b41db8 87 * }
AjK 0:eb2522b41db8 88 * @endcode
AjK 0:eb2522b41db8 89 *
AjK 0:eb2522b41db8 90 * Example with alternate buffer length:
AjK 0:eb2522b41db8 91 * @code
AjK 0:eb2522b41db8 92 * #include "mbed.h"
AjK 0:eb2522b41db8 93 * #include "MODSERIAL.h"
AjK 0:eb2522b41db8 94 *
AjK 0:eb2522b41db8 95 * // Make TX 1024bytes and RX 512byes in length
AjK 0:eb2522b41db8 96 * MODSERIAL pc(USBTX, USBRX, 1024, 512); // tx, rx
AjK 0:eb2522b41db8 97 *
AjK 0:eb2522b41db8 98 * int main() {
AjK 0:eb2522b41db8 99 * pc.printf("Hello World!");
AjK 0:eb2522b41db8 100 * while(1) {
AjK 0:eb2522b41db8 101 * pc.putc(pc.getc() + 1);
AjK 0:eb2522b41db8 102 * }
AjK 0:eb2522b41db8 103 * }
AjK 0:eb2522b41db8 104 * @endcode
AjK 0:eb2522b41db8 105 */
AjK 0:eb2522b41db8 106 class MODSERIAL : public Serial
AjK 0:eb2522b41db8 107 {
AjK 0:eb2522b41db8 108 public:
AjK 0:eb2522b41db8 109
AjK 0:eb2522b41db8 110 //! A copy of the Serial parity enum
AjK 0:eb2522b41db8 111 /** @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.format */
AjK 0:eb2522b41db8 112 enum Parity {
AjK 0:eb2522b41db8 113 None = 0
AjK 0:eb2522b41db8 114 , Odd
AjK 0:eb2522b41db8 115 , Even
AjK 0:eb2522b41db8 116 , Forced1
AjK 0:eb2522b41db8 117 , Forced0
AjK 0:eb2522b41db8 118 };
AjK 0:eb2522b41db8 119
AjK 0:eb2522b41db8 120 //! A copy of the Serial IrqType enum
AjK 0:eb2522b41db8 121 enum IrqType {
AjK 0:eb2522b41db8 122 RxIrq = 0
AjK 0:eb2522b41db8 123 , TxIrq
AjK 0:eb2522b41db8 124 , RxOvIrq
AjK 0:eb2522b41db8 125 , TxOvIrq
AjK 0:eb2522b41db8 126 , TxEmpty
AjK 0:eb2522b41db8 127 };
AjK 0:eb2522b41db8 128
AjK 0:eb2522b41db8 129 //! Non-blocking functions return code.
AjK 0:eb2522b41db8 130 enum Result {
AjK 0:eb2522b41db8 131 Ok = 0 /*!< Ok. */
AjK 0:eb2522b41db8 132 , NoMemory = -1 /*!< Memory allocation failed. */
AjK 0:eb2522b41db8 133 , NoChar = -1 /*!< No character in buffer. */
AjK 0:eb2522b41db8 134 , BufferOversize = -2 /*!< Oversized buffer. */
AjK 0:eb2522b41db8 135 };
AjK 0:eb2522b41db8 136
AjK 0:eb2522b41db8 137 /**
AjK 0:eb2522b41db8 138 * The MODSERIAL constructor is used to initialise the serial object.
AjK 0:eb2522b41db8 139 *
AjK 0:eb2522b41db8 140 * @param tx PinName of the TX pin.
AjK 0:eb2522b41db8 141 * @param rx PinName of the TX pin.
AjK 0:eb2522b41db8 142 * @param name An option name for RPC usage.
AjK 0:eb2522b41db8 143 */
AjK 0:eb2522b41db8 144 MODSERIAL(PinName tx, PinName rx, const char *name = NULL);
AjK 0:eb2522b41db8 145
AjK 0:eb2522b41db8 146 /**
AjK 0:eb2522b41db8 147 * The MODSERIAL constructor is used to initialise the serial object.
AjK 0:eb2522b41db8 148 *
AjK 0:eb2522b41db8 149 * @param tx PinName of the TX pin.
AjK 0:eb2522b41db8 150 * @param rx PinName of the TX pin.
AjK 0:eb2522b41db8 151 * @param bufferSize Integer of the TX and RX buffer sizes.
AjK 0:eb2522b41db8 152 * @param name An option name for RPC usage.
AjK 0:eb2522b41db8 153 */
AjK 0:eb2522b41db8 154 MODSERIAL(PinName tx, PinName rx, int bufferSize, const char *name = NULL);
AjK 0:eb2522b41db8 155
AjK 0:eb2522b41db8 156 /**
AjK 0:eb2522b41db8 157 * The MODSERIAL constructor is used to initialise the serial object.
AjK 0:eb2522b41db8 158 *
AjK 0:eb2522b41db8 159 * @param tx PinName of the TX pin.
AjK 0:eb2522b41db8 160 * @param rx PinName of the TX pin.
AjK 0:eb2522b41db8 161 * @param txBufferSize Integer of the TX buffer sizes.
AjK 0:eb2522b41db8 162 * @param rxBufferSize Integer of the RX buffer sizes.
AjK 0:eb2522b41db8 163 * @param name An option name for RPC usage.
AjK 0:eb2522b41db8 164 */
AjK 0:eb2522b41db8 165 MODSERIAL(PinName tx, PinName rx, int txBufferSize, int rxBufferSize, const char *name = NULL);
AjK 0:eb2522b41db8 166
AjK 0:eb2522b41db8 167 virtual ~MODSERIAL();
AjK 0:eb2522b41db8 168
AjK 0:eb2522b41db8 169 /**
AjK 0:eb2522b41db8 170 * Function: attach
AjK 0:eb2522b41db8 171 *
AjK 0:eb2522b41db8 172 * The Mbed standard <a href="/handbook/Serial">Serial</a> library object allows an interrupt callback
AjK 0:eb2522b41db8 173 * to be made when a byte is received by the TX or RX UART hardware. MODSERIAL traps these interrupts
AjK 1:b7e435fbfe8e 174 * to enable it's buffering system. However, after the byte has been received/sent under interrupt control,
AjK 0:eb2522b41db8 175 * MODSERIAL can callback a user function as a notification of the interrupt. Note, user code should not
AjK 1:b7e435fbfe8e 176 * directly interact with the Uart hardware, MODSERIAL does that, instead, MODSERIAL API functions should
AjK 0:eb2522b41db8 177 * be used.
AjK 0:eb2522b41db8 178 *
AjK 0:eb2522b41db8 179 * <b>Note</b>, a character is written out then, if there is room in the TX FIFO and the TX buffer is empty,
AjK 0:eb2522b41db8 180 * putc() will put the character directly into THR (the output holding register). If the TX FIFO is full and
AjK 0:eb2522b41db8 181 * cannot accept the character, it is placed into the TX output buffer. The TX interrupts are then enabled
AjK 1:b7e435fbfe8e 182 * so that when the TX FIFO empties, the TX buffer is then transferred to the THR FIFO. The TxIrq will ONLY
AjK 0:eb2522b41db8 183 * be activated when this transfer of a character from BUFFER to THR FIFO takes place. If your character
AjK 0:eb2522b41db8 184 * throughput is not high bandwidth, then the 16 byte TX FIFO may be enough and the TX output buffer may
AjK 0:eb2522b41db8 185 * never come into play.
AjK 0:eb2522b41db8 186 *
AjK 0:eb2522b41db8 187 * @code
AjK 0:eb2522b41db8 188 * #include "mbed.h"
AjK 0:eb2522b41db8 189 * #include "MODSERIAL.h"
AjK 0:eb2522b41db8 190 *
AjK 0:eb2522b41db8 191 * DigitalOut led1(LED1);
AjK 0:eb2522b41db8 192 * DigitalOut led2(LED2);
AjK 0:eb2522b41db8 193 * DigitalOut led3(LED3);
AjK 0:eb2522b41db8 194 *
AjK 0:eb2522b41db8 195 * // To test, connect p9 to p10 as a loopback.
AjK 0:eb2522b41db8 196 * MODSERIAL pc(p9, p10);
AjK 0:eb2522b41db8 197 *
AjK 0:eb2522b41db8 198 * // This function is called when a character goes into the TX buffer.
AjK 0:eb2522b41db8 199 * void txCallback(void) {
AjK 0:eb2522b41db8 200 * led2 = !led2;
AjK 0:eb2522b41db8 201 * }
AjK 0:eb2522b41db8 202 *
AjK 0:eb2522b41db8 203 * // This function is called when a character goes into the RX buffer.
AjK 0:eb2522b41db8 204 * void rxCallback(void) {
AjK 0:eb2522b41db8 205 * led3 = !led3;
AjK 0:eb2522b41db8 206 * }
AjK 0:eb2522b41db8 207 *
AjK 0:eb2522b41db8 208 * int main() {
AjK 0:eb2522b41db8 209 * pc.baud(115200);
AjK 0:eb2522b41db8 210 * pc.attach(&txCallback, MODSERIAL::TxIrq);
AjK 0:eb2522b41db8 211 * pc.attach(&rxCallback, MODSERIAL::RxIrq);
AjK 0:eb2522b41db8 212 *
AjK 0:eb2522b41db8 213 * while(1) {
AjK 0:eb2522b41db8 214 * led1 = !led1;
AjK 0:eb2522b41db8 215 * wait(0.5);
AjK 0:eb2522b41db8 216 * pc.putc('A');
AjK 0:eb2522b41db8 217 * wait(0.5);
AjK 0:eb2522b41db8 218 * }
AjK 0:eb2522b41db8 219 * ]
AjK 0:eb2522b41db8 220 * @endcode
AjK 0:eb2522b41db8 221 *
AjK 0:eb2522b41db8 222 * @ingroup API
AjK 0:eb2522b41db8 223 * @param fptr A pointer to a void function, or 0 to set as none
AjK 0:eb2522b41db8 224 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
AjK 0:eb2522b41db8 225 */
AjK 0:eb2522b41db8 226 void attach(void (*fptr)(void), IrqType type = RxIrq) { _isr[type].attach(fptr); }
AjK 0:eb2522b41db8 227
AjK 0:eb2522b41db8 228 /**
AjK 0:eb2522b41db8 229 * Function: attach
AjK 0:eb2522b41db8 230 *
AjK 0:eb2522b41db8 231 * The Mbed standard <a href="/handbook/Serial">Serial</a> library object allows an interrupt callback
AjK 0:eb2522b41db8 232 * to be made when a byte is received by the TX or RX UART hardware. MODSERIAL traps these interrupts
AjK 1:b7e435fbfe8e 233 * to enable it's buffering system. However, after the byte has been received/sent under interrupt control,
AjK 0:eb2522b41db8 234 * MODSERIAL can callback a user function as a notification of the interrupt. Note, user code should not
AjK 1:b7e435fbfe8e 235 * directly interact with the Uart hardware, MODSERIAL does that, instead, MODSERIAL API functions should
AjK 0:eb2522b41db8 236 * be used.
AjK 0:eb2522b41db8 237 *
AjK 0:eb2522b41db8 238 * <b>Note</b>, a character is written out then, if there is room in the TX FIFO and the TX buffer is empty,
AjK 0:eb2522b41db8 239 * putc() will put the character directly into THR (the output holding register). If the TX FIFO is full and
AjK 0:eb2522b41db8 240 * cannot accept the character, it is placed into the TX output buffer. The TX interrupts are then enabled
AjK 1:b7e435fbfe8e 241 * so that when the TX FIFO empties, the TX buffer is then transferred to the THR FIFO. The TxIrq will ONLY
AjK 0:eb2522b41db8 242 * be activated when this transfer of a character from BUFFER to THR FIFO takes place. If your character
AjK 0:eb2522b41db8 243 * throughput is not high bandwidth, then the 16 byte TX FIFO may be enough and the TX output buffer may
AjK 0:eb2522b41db8 244 * never come into play.
AjK 0:eb2522b41db8 245 *
AjK 0:eb2522b41db8 246 * @code
AjK 0:eb2522b41db8 247 * #include "mbed.h"
AjK 0:eb2522b41db8 248 * #include "MODSERIAL.h"
AjK 0:eb2522b41db8 249 *
AjK 0:eb2522b41db8 250 * DigitalOut led1(LED1);
AjK 0:eb2522b41db8 251 * DigitalOut led2(LED2);
AjK 0:eb2522b41db8 252 * DigitalOut led3(LED3);
AjK 0:eb2522b41db8 253 *
AjK 0:eb2522b41db8 254 * // To test, connect p9 to p10 as a loopback.
AjK 0:eb2522b41db8 255 * MODSERIAL pc(p9, p10);
AjK 0:eb2522b41db8 256 *
AjK 0:eb2522b41db8 257 * class Foo {
AjK 0:eb2522b41db8 258 * public:
AjK 0:eb2522b41db8 259 * // This method is called when a character goes into the TX buffer.
AjK 0:eb2522b41db8 260 * void txCallback(void) { led2 = !led2; }
AjK 0:eb2522b41db8 261 *
AjK 0:eb2522b41db8 262 * // This method is called when a character goes into the RX buffer.
AjK 0:eb2522b41db8 263 * void rxCallback(void) { led3 = !led3; }
AjK 0:eb2522b41db8 264 * };
AjK 0:eb2522b41db8 265 *
AjK 0:eb2522b41db8 266 * Foo foo;
AjK 0:eb2522b41db8 267 *
AjK 0:eb2522b41db8 268 * int main() {
AjK 0:eb2522b41db8 269 * pc.baud(115200);
AjK 0:eb2522b41db8 270 * pc.attach(&foo, &Foo::txCallback, MODSERIAL::TxIrq);
AjK 0:eb2522b41db8 271 * pc.attach(&foo, &Foo::rxCallback, MODSERIAL::RxIrq);
AjK 0:eb2522b41db8 272 *
AjK 0:eb2522b41db8 273 * while(1) {
AjK 0:eb2522b41db8 274 * led1 = !led1;
AjK 0:eb2522b41db8 275 * wait(0.5);
AjK 0:eb2522b41db8 276 * pc.putc('A');
AjK 0:eb2522b41db8 277 * wait(0.5);
AjK 0:eb2522b41db8 278 * }
AjK 0:eb2522b41db8 279 * ]
AjK 0:eb2522b41db8 280 * @endcode
AjK 0:eb2522b41db8 281 *
AjK 0:eb2522b41db8 282 * @ingroup API
AjK 0:eb2522b41db8 283 * @param tptr A pointer to the object to call the member function on
AjK 0:eb2522b41db8 284 * @param mptr A pointer to the member function to be called
AjK 0:eb2522b41db8 285 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
AjK 0:eb2522b41db8 286 */
AjK 0:eb2522b41db8 287 template<typename T>
AjK 0:eb2522b41db8 288 void attach(T* tptr, void (T::*mptr)(void), IrqType type = RxIrq) {
AjK 0:eb2522b41db8 289 if((mptr != NULL) && (tptr != NULL)) {
AjK 0:eb2522b41db8 290 _isr[type].attach(tptr, mptr);
AjK 0:eb2522b41db8 291 }
AjK 0:eb2522b41db8 292 }
AjK 0:eb2522b41db8 293
AjK 0:eb2522b41db8 294 /**
AjK 0:eb2522b41db8 295 * @see attach
AjK 0:eb2522b41db8 296 * @ingroup API
AjK 0:eb2522b41db8 297 */
AjK 0:eb2522b41db8 298 void connect(void (*fptr)(void), IrqType type = RxIrq) { _isr[RxIrq].attach(fptr); }
AjK 0:eb2522b41db8 299
AjK 0:eb2522b41db8 300 /**
AjK 0:eb2522b41db8 301 * @see attach
AjK 0:eb2522b41db8 302 * @ingroup API
AjK 0:eb2522b41db8 303 */
AjK 0:eb2522b41db8 304 template<typename T>
AjK 0:eb2522b41db8 305 void connect(T* tptr, void (T::*mptr)(void), IrqType type = RxIrq) {
AjK 0:eb2522b41db8 306 if((mptr != NULL) && (tptr != NULL)) {
AjK 0:eb2522b41db8 307 _isr[type].attach(tptr, mptr);
AjK 0:eb2522b41db8 308 }
AjK 0:eb2522b41db8 309 }
AjK 0:eb2522b41db8 310
AjK 0:eb2522b41db8 311 /**
AjK 0:eb2522b41db8 312 * Function: writeable
AjK 0:eb2522b41db8 313 *
AjK 0:eb2522b41db8 314 * Determine if there is space available to write a byte
AjK 0:eb2522b41db8 315 *
AjK 0:eb2522b41db8 316 * @ingroup API
AjK 0:eb2522b41db8 317 * @return 1 if there is space to write a character, else 0
AjK 0:eb2522b41db8 318 */
AjK 0:eb2522b41db8 319 int writeable() { return txBufferFull() ? 0 : 1; }
AjK 0:eb2522b41db8 320
AjK 0:eb2522b41db8 321 /**
AjK 0:eb2522b41db8 322 * Function: readable
AjK 0:eb2522b41db8 323 *
AjK 0:eb2522b41db8 324 * Determine if there is a byte available to read
AjK 0:eb2522b41db8 325 *
AjK 0:eb2522b41db8 326 * @ingroup API
AjK 0:eb2522b41db8 327 * @return 1 if there is a character available to read, else 0
AjK 0:eb2522b41db8 328 */
AjK 0:eb2522b41db8 329 int readable() { return rxBufferEmpty() ? 0 : 1; }
AjK 0:eb2522b41db8 330
AjK 0:eb2522b41db8 331 /**
AjK 0:eb2522b41db8 332 * Function: txBufferSane
AjK 0:eb2522b41db8 333 *
AjK 0:eb2522b41db8 334 * Determine if the TX buffer has been initialized.
AjK 0:eb2522b41db8 335 *
AjK 0:eb2522b41db8 336 * @ingroup API
AjK 0:eb2522b41db8 337 * @return true if the buffer is initialized, else false
AjK 0:eb2522b41db8 338 */
AjK 0:eb2522b41db8 339 bool txBufferSane(void) { return buffer[TxIrq] != (char *)NULL ? true : false; }
AjK 0:eb2522b41db8 340
AjK 0:eb2522b41db8 341 /**
AjK 0:eb2522b41db8 342 * Function: rxBufferSane
AjK 0:eb2522b41db8 343 *
AjK 0:eb2522b41db8 344 * Determine if the RX buffer has been initialized.
AjK 0:eb2522b41db8 345 *
AjK 0:eb2522b41db8 346 * @ingroup API
AjK 0:eb2522b41db8 347 * @return true if the buffer is initialized, else false
AjK 0:eb2522b41db8 348 */
AjK 0:eb2522b41db8 349 bool rxBufferSane(void) { return buffer[TxIrq] != (char *)NULL ? true : false; }
AjK 0:eb2522b41db8 350
AjK 0:eb2522b41db8 351 /**
AjK 0:eb2522b41db8 352 * Function: txBufferGetCount
AjK 0:eb2522b41db8 353 *
AjK 0:eb2522b41db8 354 * Returns how many bytes are in the TX buffer
AjK 0:eb2522b41db8 355 *
AjK 0:eb2522b41db8 356 * @ingroup API
AjK 0:eb2522b41db8 357 * @return The number of bytes in the TX buffer
AjK 0:eb2522b41db8 358 */
AjK 0:eb2522b41db8 359 int txBufferGetCount(void) { return buffer_count[TxIrq]; }
AjK 0:eb2522b41db8 360
AjK 0:eb2522b41db8 361 /**
AjK 0:eb2522b41db8 362 * Function: rxBufferGetCount
AjK 0:eb2522b41db8 363 *
AjK 0:eb2522b41db8 364 * Returns how many bytes are in the RX buffer
AjK 0:eb2522b41db8 365 *
AjK 0:eb2522b41db8 366 * @ingroup API
AjK 0:eb2522b41db8 367 * @return The number of bytes in the RX buffer
AjK 0:eb2522b41db8 368 */
AjK 0:eb2522b41db8 369 int rxBufferGetCount(void) { return buffer_count[RxIrq]; }
AjK 0:eb2522b41db8 370
AjK 0:eb2522b41db8 371 /**
AjK 0:eb2522b41db8 372 * Function: txBufferGetSize
AjK 0:eb2522b41db8 373 *
AjK 0:eb2522b41db8 374 * Returns the current size of the TX buffer
AjK 0:eb2522b41db8 375 *
AjK 0:eb2522b41db8 376 * @ingroup API
AjK 0:eb2522b41db8 377 * @return The length iof the TX buffer in bytes
AjK 0:eb2522b41db8 378 */
AjK 0:eb2522b41db8 379 int txBufferGetSize(int size) { return buffer_size[TxIrq]; }
AjK 0:eb2522b41db8 380
AjK 0:eb2522b41db8 381 /**
AjK 0:eb2522b41db8 382 * Function: rxBufferGetSize
AjK 0:eb2522b41db8 383 *
AjK 0:eb2522b41db8 384 * Returns the current size of the RX buffer
AjK 0:eb2522b41db8 385 *
AjK 0:eb2522b41db8 386 * @ingroup API
AjK 0:eb2522b41db8 387 * @return The length iof the RX buffer in bytes
AjK 0:eb2522b41db8 388 */
AjK 0:eb2522b41db8 389 int rxBufferGetSize(int size) { return buffer_size[RxIrq]; }
AjK 0:eb2522b41db8 390
AjK 0:eb2522b41db8 391 /**
AjK 0:eb2522b41db8 392 * Function: txBufferFull
AjK 0:eb2522b41db8 393 *
AjK 0:eb2522b41db8 394 * Is the TX buffer full?
AjK 0:eb2522b41db8 395 *
AjK 0:eb2522b41db8 396 * @ingroup API
AjK 0:eb2522b41db8 397 * @return true if the TX buffer is full, otherwise false
AjK 0:eb2522b41db8 398 */
AjK 0:eb2522b41db8 399 bool txBufferFull(void);
AjK 0:eb2522b41db8 400
AjK 0:eb2522b41db8 401 /**
AjK 0:eb2522b41db8 402 * Function: rxBufferFull
AjK 0:eb2522b41db8 403 *
AjK 0:eb2522b41db8 404 * Is the RX buffer full?
AjK 0:eb2522b41db8 405 *
AjK 0:eb2522b41db8 406 * @ingroup API
AjK 0:eb2522b41db8 407 * @return true if the RX buffer is full, otherwise false
AjK 0:eb2522b41db8 408 */
AjK 0:eb2522b41db8 409 bool rxBufferFull(void);
AjK 0:eb2522b41db8 410
AjK 0:eb2522b41db8 411 /**
AjK 0:eb2522b41db8 412 * Function: txBufferEmpty
AjK 0:eb2522b41db8 413 *
AjK 0:eb2522b41db8 414 * Is the TX buffer empty?
AjK 0:eb2522b41db8 415 *
AjK 0:eb2522b41db8 416 * @ingroup API
AjK 0:eb2522b41db8 417 * @return true if the TX buffer is empty, otherwise false
AjK 0:eb2522b41db8 418 */
AjK 0:eb2522b41db8 419 bool txBufferEmpty(void);
AjK 0:eb2522b41db8 420
AjK 0:eb2522b41db8 421 /**
AjK 0:eb2522b41db8 422 * Function: rxBufferEmpty
AjK 0:eb2522b41db8 423 *
AjK 0:eb2522b41db8 424 * Is the RX buffer empty?
AjK 0:eb2522b41db8 425 *
AjK 0:eb2522b41db8 426 * @ingroup API
AjK 0:eb2522b41db8 427 * @return true if the RX buffer is empty, otherwise false
AjK 0:eb2522b41db8 428 */
AjK 0:eb2522b41db8 429 bool rxBufferEmpty(void);
AjK 0:eb2522b41db8 430
AjK 0:eb2522b41db8 431 /**
AjK 0:eb2522b41db8 432 * Function: txBufferSetSize
AjK 0:eb2522b41db8 433 *
AjK 0:eb2522b41db8 434 * Change the TX buffer size.
AjK 0:eb2522b41db8 435 *
AjK 0:eb2522b41db8 436 * @see Result
AjK 0:eb2522b41db8 437 * @ingroup API
AjK 0:eb2522b41db8 438 * @param size The new TX buffer size in bytes.
AjK 0:eb2522b41db8 439 * @param m Perform a memory sanity check. Errs the Mbed if memory alloc fails.
AjK 0:eb2522b41db8 440 * @return Result Ok on success.
AjK 0:eb2522b41db8 441 */
AjK 0:eb2522b41db8 442 int txBufferSetSize(int size, bool m) { return resizeBuffer(size, TxIrq, m); }
AjK 0:eb2522b41db8 443
AjK 0:eb2522b41db8 444 /**
AjK 0:eb2522b41db8 445 * Function: rxBufferSetSize
AjK 0:eb2522b41db8 446 *
AjK 0:eb2522b41db8 447 * Change the RX buffer size.
AjK 0:eb2522b41db8 448 *
AjK 0:eb2522b41db8 449 * @see Result
AjK 0:eb2522b41db8 450 * @ingroup API
AjK 0:eb2522b41db8 451 * @param size The new RX buffer size in bytes.
AjK 0:eb2522b41db8 452 * @param m Perform a memory sanity check. Errs the Mbed if memory alloc fails.
AjK 0:eb2522b41db8 453 * @return Result Ok on success.
AjK 0:eb2522b41db8 454 */
AjK 0:eb2522b41db8 455 int rxBufferSetSize(int size, bool m) { return resizeBuffer(size, RxIrq, m); }
AjK 0:eb2522b41db8 456
AjK 0:eb2522b41db8 457 /**
AjK 0:eb2522b41db8 458 * Function: txBufferSetSize
AjK 0:eb2522b41db8 459 *
AjK 0:eb2522b41db8 460 * Change the TX buffer size.
AjK 0:eb2522b41db8 461 * Always performs a memory sanity check, halting the Mbed on failure.
AjK 0:eb2522b41db8 462 *
AjK 0:eb2522b41db8 463 * @see Result
AjK 0:eb2522b41db8 464 * @ingroup API
AjK 0:eb2522b41db8 465 * @param size The new TX buffer size in bytes.
AjK 0:eb2522b41db8 466 * @return Result Ok on success.
AjK 0:eb2522b41db8 467 */
AjK 0:eb2522b41db8 468 int txBufferSetSize(int size) { return resizeBuffer(size, TxIrq, true); }
AjK 0:eb2522b41db8 469
AjK 0:eb2522b41db8 470 /**
AjK 0:eb2522b41db8 471 * Function: rxBufferSetSize
AjK 0:eb2522b41db8 472 *
AjK 0:eb2522b41db8 473 * Change the RX buffer size.
AjK 0:eb2522b41db8 474 * Always performs a memory sanity check, halting the Mbed on failure.
AjK 0:eb2522b41db8 475 *
AjK 0:eb2522b41db8 476 * @see Result
AjK 0:eb2522b41db8 477 * @ingroup API
AjK 0:eb2522b41db8 478 * @param size The new RX buffer size in bytes.
AjK 0:eb2522b41db8 479 * @return Result Ok on success.
AjK 0:eb2522b41db8 480 */
AjK 0:eb2522b41db8 481 int rxBufferSetSize(int size) { return resizeBuffer(size, RxIrq, true); }
AjK 0:eb2522b41db8 482
AjK 0:eb2522b41db8 483 /**
AjK 0:eb2522b41db8 484 * Function: txBufferFlush
AjK 0:eb2522b41db8 485 *
AjK 0:eb2522b41db8 486 * Remove all bytes from the TX buffer.
AjK 0:eb2522b41db8 487 * @ingroup API
AjK 0:eb2522b41db8 488 */
AjK 0:eb2522b41db8 489 void txBufferFlush(void) { flushBuffer(TxIrq); }
AjK 0:eb2522b41db8 490
AjK 0:eb2522b41db8 491 /**
AjK 0:eb2522b41db8 492 * Function: rxBufferFlush
AjK 0:eb2522b41db8 493 *
AjK 0:eb2522b41db8 494 * Remove all bytes from the RX buffer.
AjK 0:eb2522b41db8 495 * @ingroup API
AjK 0:eb2522b41db8 496 */
AjK 0:eb2522b41db8 497 void rxBufferFlush(void) { flushBuffer(RxIrq); }
AjK 0:eb2522b41db8 498
AjK 0:eb2522b41db8 499 /**
AjK 0:eb2522b41db8 500 * Function: getcNb
AjK 0:eb2522b41db8 501 *
AjK 0:eb2522b41db8 502 * Like getc() but is non-blocking. If no bytes are in the RX buffer this
AjK 0:eb2522b41db8 503 * function returns Result::NoChar (-1)
AjK 0:eb2522b41db8 504 *
AjK 0:eb2522b41db8 505 * @ingroup API
AjK 0:eb2522b41db8 506 * @return A byte from the RX buffer or Result::NoChar (-1) if bufer empty.
AjK 0:eb2522b41db8 507 */
AjK 0:eb2522b41db8 508 int getcNb() { return __getc(false); }
AjK 0:eb2522b41db8 509
AjK 0:eb2522b41db8 510 /**
AjK 0:eb2522b41db8 511 * Function: getc
AjK 0:eb2522b41db8 512 *
AjK 0:eb2522b41db8 513 * Overloaded version of Serial::getc()
AjK 0:eb2522b41db8 514 *
AjK 0:eb2522b41db8 515 * This function blocks (if the RX buffer is empty the function will wait for a
AjK 0:eb2522b41db8 516 * character to arrive and then return that character).
AjK 0:eb2522b41db8 517 *
AjK 0:eb2522b41db8 518 * @ingroup API
AjK 0:eb2522b41db8 519 * @return A byte from the RX buffer
AjK 0:eb2522b41db8 520 */
AjK 0:eb2522b41db8 521 int getc() { return __getc(true); }
AjK 0:eb2522b41db8 522
AjK 0:eb2522b41db8 523 /**
AjK 0:eb2522b41db8 524 * Function: txGetLastChar
AjK 0:eb2522b41db8 525 *
AjK 1:b7e435fbfe8e 526 * Rteurn the last byte to pass through the TX interrupt handler.
AjK 0:eb2522b41db8 527 *
AjK 0:eb2522b41db8 528 * @ingroup MISC
AjK 0:eb2522b41db8 529 * @return The byte
AjK 0:eb2522b41db8 530 */
AjK 0:eb2522b41db8 531 char txGetLastChar(void) { return txc; }
AjK 0:eb2522b41db8 532
AjK 0:eb2522b41db8 533 /**
AjK 0:eb2522b41db8 534 * Function: rxGetLastChar
AjK 0:eb2522b41db8 535 *
AjK 0:eb2522b41db8 536 * Return the last byte to pass through the RX interrupt handler.
AjK 0:eb2522b41db8 537 *
AjK 0:eb2522b41db8 538 * @ingroup MISC
AjK 0:eb2522b41db8 539 * @return The byte
AjK 0:eb2522b41db8 540 */
AjK 0:eb2522b41db8 541 char rxGetLastChar(void) { return rxc; }
AjK 0:eb2522b41db8 542
AjK 0:eb2522b41db8 543 /**
AjK 0:eb2522b41db8 544 * Function: txIsBusy
AjK 0:eb2522b41db8 545 *
AjK 1:b7e435fbfe8e 546 * If the Uart is still actively sending characters this
AjK 0:eb2522b41db8 547 * function will return true.
AjK 0:eb2522b41db8 548 *
AjK 0:eb2522b41db8 549 * @ingroup API
AjK 0:eb2522b41db8 550 * @return bool
AjK 0:eb2522b41db8 551 */
AjK 0:eb2522b41db8 552 bool txIsBusy(void) { return (_LSR & (3UL << 5) == 0) ? true : false; }
AjK 0:eb2522b41db8 553
AjK 0:eb2522b41db8 554 #if 0 // Inhereted from Serial/Stream, for documentation only
AjK 0:eb2522b41db8 555 /**
AjK 0:eb2522b41db8 556 * Function: putc
AjK 0:eb2522b41db8 557 *
AjK 0:eb2522b41db8 558 * Write a character
AjK 0:eb2522b41db8 559 * Inhereted from Serial/Stream
AjK 0:eb2522b41db8 560 *
AjK 0:eb2522b41db8 561 * @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.putc
AjK 0:eb2522b41db8 562 * @ingroup API
AjK 0:eb2522b41db8 563 * @param c The character to write to the serial port
AjK 0:eb2522b41db8 564 */
AjK 0:eb2522b41db8 565 int putc(int c);
AjK 0:eb2522b41db8 566 #endif
AjK 0:eb2522b41db8 567
AjK 0:eb2522b41db8 568 #if 0 // Inhereted from Serial/Stream, for documentation only
AjK 0:eb2522b41db8 569 /**
AjK 0:eb2522b41db8 570 * Function: printf
AjK 0:eb2522b41db8 571 *
AjK 0:eb2522b41db8 572 * Write a formated string
AjK 0:eb2522b41db8 573 * Inhereted from Serial/Stream
AjK 0:eb2522b41db8 574 *
AjK 0:eb2522b41db8 575 * @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.printf
AjK 0:eb2522b41db8 576 * @ingroup API
AjK 0:eb2522b41db8 577 * @param format A printf-style format string, followed by the variables to use in formating the string.
AjK 0:eb2522b41db8 578 */
AjK 0:eb2522b41db8 579 int printf(const char* format, ...);
AjK 0:eb2522b41db8 580 #endif
AjK 0:eb2522b41db8 581
AjK 0:eb2522b41db8 582 #if 0 // Inhereted from Serial/Stream, for documentation only
AjK 0:eb2522b41db8 583 /**
AjK 0:eb2522b41db8 584 * Function: scanf
AjK 0:eb2522b41db8 585 *
AjK 0:eb2522b41db8 586 * Read a formated string
AjK 0:eb2522b41db8 587 * Inhereted from Serial/Stream
AjK 0:eb2522b41db8 588 *
AjK 0:eb2522b41db8 589 * @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.scanf
AjK 0:eb2522b41db8 590 * @ingroup API
AjK 0:eb2522b41db8 591 * @param format - A scanf-style format string, followed by the pointers to variables to store the results.
AjK 0:eb2522b41db8 592 */
AjK 0:eb2522b41db8 593 int scanf(const char* format, ...);
AjK 0:eb2522b41db8 594 #endif
AjK 0:eb2522b41db8 595
AjK 0:eb2522b41db8 596 protected:
AjK 0:eb2522b41db8 597
AjK 0:eb2522b41db8 598 /**
AjK 0:eb2522b41db8 599 * A pointer to the UART peripheral base address being used.
AjK 0:eb2522b41db8 600 * @ingroup INTERNALS
AjK 0:eb2522b41db8 601 */
AjK 0:eb2522b41db8 602 void *_base;
AjK 0:eb2522b41db8 603
AjK 0:eb2522b41db8 604 /**
AjK 0:eb2522b41db8 605 * The last byte to pass through the TX IRQ handler.
AjK 0:eb2522b41db8 606 * @ingroup INTERNALS
AjK 0:eb2522b41db8 607 */
AjK 0:eb2522b41db8 608 volatile char txc;
AjK 0:eb2522b41db8 609
AjK 0:eb2522b41db8 610 /**
AjK 0:eb2522b41db8 611 * The last byte to pass through the RX IRQ handler.
AjK 0:eb2522b41db8 612 * @ingroup INTERNALS
AjK 0:eb2522b41db8 613 */
AjK 0:eb2522b41db8 614 volatile char rxc;
AjK 0:eb2522b41db8 615
AjK 0:eb2522b41db8 616 /**
AjK 1:b7e435fbfe8e 617 * Pointers to the TX and RX buffers.
AjK 0:eb2522b41db8 618 * @ingroup INTERNALS
AjK 0:eb2522b41db8 619 */
AjK 0:eb2522b41db8 620 volatile char *buffer[2];
AjK 0:eb2522b41db8 621
AjK 0:eb2522b41db8 622 /**
AjK 0:eb2522b41db8 623 * Buffer in pointers.
AjK 0:eb2522b41db8 624 * @ingroup INTERNALS
AjK 0:eb2522b41db8 625 */
AjK 0:eb2522b41db8 626 volatile int buffer_in[2];
AjK 0:eb2522b41db8 627
AjK 0:eb2522b41db8 628 /**
AjK 0:eb2522b41db8 629 * Buffer out pointers.
AjK 0:eb2522b41db8 630 * @ingroup INTERNALS
AjK 0:eb2522b41db8 631 */
AjK 0:eb2522b41db8 632 volatile int buffer_out[2];
AjK 0:eb2522b41db8 633
AjK 0:eb2522b41db8 634 /**
AjK 0:eb2522b41db8 635 * Buffer lengths.
AjK 0:eb2522b41db8 636 * @ingroup INTERNALS
AjK 0:eb2522b41db8 637 */
AjK 0:eb2522b41db8 638 volatile int buffer_size[2];
AjK 0:eb2522b41db8 639
AjK 0:eb2522b41db8 640 /**
AjK 0:eb2522b41db8 641 * Buffer content counters.
AjK 0:eb2522b41db8 642 * @ingroup INTERNALS
AjK 0:eb2522b41db8 643 */
AjK 0:eb2522b41db8 644 volatile int buffer_count[2];
AjK 0:eb2522b41db8 645
AjK 0:eb2522b41db8 646 /**
AjK 0:eb2522b41db8 647 * Buffer overflow.
AjK 0:eb2522b41db8 648 * @ingroup INTERNALS
AjK 0:eb2522b41db8 649 */
AjK 0:eb2522b41db8 650 volatile int buffer_overflow[2];
AjK 0:eb2522b41db8 651
AjK 0:eb2522b41db8 652 /**
AjK 0:eb2522b41db8 653 * Callback system.
AjK 0:eb2522b41db8 654 * @ingroup INTERNALS
AjK 0:eb2522b41db8 655 */
AjK 0:eb2522b41db8 656 FunctionPointer _isr[5];
AjK 0:eb2522b41db8 657
AjK 0:eb2522b41db8 658 /**
AjK 0:eb2522b41db8 659 * TX Interrupt Service Routine.
AjK 0:eb2522b41db8 660 * @ingroup INTERNALS
AjK 0:eb2522b41db8 661 */
AjK 0:eb2522b41db8 662 void isr_tx(void);
AjK 0:eb2522b41db8 663
AjK 0:eb2522b41db8 664 /**
AjK 0:eb2522b41db8 665 * RX Interrupt Service Routine.
AjK 0:eb2522b41db8 666 * @ingroup INTERNALS
AjK 0:eb2522b41db8 667 */
AjK 0:eb2522b41db8 668 void isr_rx(void);
AjK 0:eb2522b41db8 669
AjK 0:eb2522b41db8 670 /**
AjK 0:eb2522b41db8 671 * Disable the interrupts for this Uart.
AjK 0:eb2522b41db8 672 * @ingroup INTERNALS
AjK 0:eb2522b41db8 673 */
AjK 0:eb2522b41db8 674 void disableIrq(void);
AjK 0:eb2522b41db8 675
AjK 0:eb2522b41db8 676 /**
AjK 0:eb2522b41db8 677 * Enable the interrupts for this Uart.
AjK 0:eb2522b41db8 678 * @ingroup INTERNALS
AjK 0:eb2522b41db8 679 */
AjK 0:eb2522b41db8 680 void enableIrq(void);
AjK 0:eb2522b41db8 681
AjK 0:eb2522b41db8 682 /**
AjK 0:eb2522b41db8 683 * Get a character from the RX buffer
AjK 0:eb2522b41db8 684 * @ingroup INTERNALS
AjK 0:eb2522b41db8 685 * @param bool True to block (wait for input)
AjK 0:eb2522b41db8 686 * @return A byte from the buffer.
AjK 0:eb2522b41db8 687 */
AjK 0:eb2522b41db8 688 int __getc(bool);
AjK 0:eb2522b41db8 689
AjK 0:eb2522b41db8 690 /**
AjK 0:eb2522b41db8 691 * Put a character from the TX buffer
AjK 0:eb2522b41db8 692 * @ingroup INTERNALS
AjK 1:b7e435fbfe8e 693 * @param bool True to block (wait for space in the TX buffer if full)
AjK 0:eb2522b41db8 694 * @return 0 on success
AjK 0:eb2522b41db8 695 */
AjK 0:eb2522b41db8 696 int __putc(int c, bool);
AjK 0:eb2522b41db8 697
AjK 0:eb2522b41db8 698 /**
AjK 0:eb2522b41db8 699 * Function: _putc
AjK 0:eb2522b41db8 700 * Overloaded virtual function.
AjK 0:eb2522b41db8 701 */
AjK 0:eb2522b41db8 702 virtual int _putc(int c) { return __putc(c, true); }
AjK 0:eb2522b41db8 703
AjK 0:eb2522b41db8 704 /**
AjK 0:eb2522b41db8 705 * Function: _getc
AjK 0:eb2522b41db8 706 * Overloaded virtual function.
AjK 0:eb2522b41db8 707 */
AjK 0:eb2522b41db8 708 virtual int _getc() { return __getc(true); }
AjK 0:eb2522b41db8 709
AjK 0:eb2522b41db8 710 /**
AjK 0:eb2522b41db8 711 * Set's the Uart base pointer.
AjK 0:eb2522b41db8 712 * @ingroup INTERNALS
AjK 0:eb2522b41db8 713 */
AjK 0:eb2522b41db8 714 void setBase(void);
AjK 0:eb2522b41db8 715
AjK 0:eb2522b41db8 716 /**
AjK 0:eb2522b41db8 717 * Function: init
AjK 0:eb2522b41db8 718 * Initialize the MODSERIAL object
AjK 0:eb2522b41db8 719 * @ingroup INTERNALS
AjK 0:eb2522b41db8 720 */
AjK 0:eb2522b41db8 721 void init(int txSize, int rxSize);
AjK 0:eb2522b41db8 722
AjK 0:eb2522b41db8 723 /**
AjK 0:eb2522b41db8 724 * Function: flushBuffer
AjK 0:eb2522b41db8 725 * @ingroup INTERNALS
AjK 0:eb2522b41db8 726 */
AjK 0:eb2522b41db8 727 void flushBuffer(IrqType type);
AjK 0:eb2522b41db8 728
AjK 0:eb2522b41db8 729 /**
AjK 0:eb2522b41db8 730 * Function: resizeBuffer
AjK 0:eb2522b41db8 731 * @ingroup INTERNALS
AjK 0:eb2522b41db8 732 */
AjK 0:eb2522b41db8 733 int resizeBuffer(int size, IrqType type = RxIrq, bool memory_check = true);
AjK 0:eb2522b41db8 734
AjK 0:eb2522b41db8 735 /**
AjK 0:eb2522b41db8 736 * Function: downSizeBuffer
AjK 0:eb2522b41db8 737 * @ingroup INTERNALS
AjK 0:eb2522b41db8 738 */
AjK 0:eb2522b41db8 739 int downSizeBuffer(int size, IrqType type, bool memory_check);
AjK 0:eb2522b41db8 740
AjK 0:eb2522b41db8 741 /**
AjK 0:eb2522b41db8 742 * Function: upSizeBuffer
AjK 0:eb2522b41db8 743 * @ingroup INTERNALS
AjK 0:eb2522b41db8 744 */
AjK 0:eb2522b41db8 745 int upSizeBuffer(int size, IrqType type, bool memory_check);
AjK 0:eb2522b41db8 746 };
AjK 0:eb2522b41db8 747
AjK 0:eb2522b41db8 748 }; // namespace AjK ends
AjK 0:eb2522b41db8 749
AjK 0:eb2522b41db8 750 using namespace AjK;
AjK 0:eb2522b41db8 751
AjK 0:eb2522b41db8 752 #endif