Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Autoflight2018_22_MODSERIAL
Fork of MODSERIAL by
Revision 12:8c7394e2ae7f, committed 2011-01-20
- Comitter:
- AjK
- Date:
- Thu Jan 20 11:57:32 2011 +0000
- Parent:
- 11:a93a62eeeb9d
- Child:
- 13:70bb7c1769fa
- Commit message:
- 1.12 See ChangeLog
Changed in this revision
--- a/ChangeLog.c Wed Nov 24 00:33:40 2010 +0000
+++ b/ChangeLog.c Thu Jan 20 11:57:32 2011 +0000
@@ -1,5 +1,16 @@
/* $Id:$
+1.12 - 20/01/2011
+
+ * Added new "autoDetectChar()" function. To use:-
+ 1st: Add a callback to invoke when the char is detected:-
+ .attach(&detectedChar, MODSERIAL::RxAutoDetect);
+ 2nd: Send the char to detect.
+ .autoDectectChar('\n');
+ Whenever that char goes into the RX buffer your callback will be invoked.
+ Added example2.cpp to demo a simple messaging system using this auto feature.
+
+
1.11 - 23/11/2010
* Fixed a minor issue with 1.10 missed an alteration of name change.
--- a/INIT.cpp Wed Nov 24 00:33:40 2010 +0000
+++ b/INIT.cpp Thu Jan 20 11:57:32 2011 +0000
@@ -64,6 +64,8 @@
_FCR = MODSERIAL_FIFO_ENABLE | MODSERIAL_FIFO_RX_RESET | MODSERIAL_FIFO_TX_RESET;
+ auto_detect_char = 0;
+
enableIrq();
}
--- a/ISR_RX.cpp Wed Nov 24 00:33:40 2010 +0000
+++ b/ISR_RX.cpp Thu Jan 20 11:57:32 2011 +0000
@@ -50,6 +50,9 @@
}
_isr[RxIrq].call();
}
+ if (auto_detect_char == rxc) {
+ _isr[RxAutoDetect].call();
+ }
}
}
--- a/ISR_TX.cpp Wed Nov 24 00:33:40 2010 +0000
+++ b/ISR_TX.cpp Thu Jan 20 11:57:32 2011 +0000
@@ -43,7 +43,7 @@
if (doCallback) _isr[TxIrq].call();
}
- if ( MODSERIAL_TX_BUFFER_EMPTY && MODSERIAL_THR_HAS_SPACE ) {
+ if ( MODSERIAL_TX_BUFFER_EMPTY ) {
_IER = 1;
_isr[TxEmpty].call();
}
--- a/MACROS.h Wed Nov 24 00:33:40 2010 +0000 +++ b/MACROS.h Thu Jan 20 11:57:32 2011 +0000 @@ -1,70 +1,70 @@ -/* - Copyright (c) 2010 Andy Kirkham - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. -*/ - -#ifndef MODSERIAL_MACROS_H -#define MODSERIAL_MACROS_H - -#define MODSERIAL_RBR 0x00 -#define MODSERIAL_THR 0x00 -#define MODSERIAL_DLL 0x00 -#define MODSERIAL_IER 0x04 -#define MODSERIAL_DML 0x04 -#define MODSERIAL_IIR 0x08 -#define MODSERIAL_FCR 0x08 -#define MODSERIAL_LCR 0x0C -#define MODSERIAL_LSR 0x14 -#define MODSERIAL_SCR 0x1C -#define MODSERIAL_ACR 0x20 -#define MODSERIAL_ICR 0x24 -#define MODSERIAL_FDR 0x28 -#define MODSERIAL_TER 0x30 - -#define MODSERIAL_LSR_RDR (1UL << 0) -#define MODSERIAL_LSR_OE (1UL << 1) -#define MODSERIAL_LSR_PE (1UL << 2) -#define MODSERIAL_LSR_FE (1UL << 3) -#define MODSERIAL_LSR_BR (1UL << 4) -#define MODSERIAL_LSR_THRE (1UL << 5) -#define MODSERIAL_LSR_TEMT (1UL << 6) -#define MODSERIAL_LSR_RXFE (1UL << 7) - -#define MODSERIAL_FIFO_ENABLE 1 -#define MODSERIAL_FIFO_RX_RESET 2 -#define MODSERIAL_FIFO_TX_RESET 4 - -#define _RBR *((char *)_base+MODSERIAL_RBR) -#define _THR *((char *)_base+MODSERIAL_THR) -#define _IIR *((char *)_base+MODSERIAL_IIR) -#define _IER *((char *)_base+MODSERIAL_IER) -#define _LSR *((char *)_base+MODSERIAL_LSR) -#define _FCR *((char *)_base+MODSERIAL_FCR) - -#define MODSERIAL_TX_BUFFER_EMPTY (buffer_count[TxIrq]==0) -#define MODSERIAL_RX_BUFFER_EMPTY (buffer_count[RxIrq]==0) -#define MODSERIAL_TX_BUFFER_FULL (buffer_count[TxIrq]==buffer_size[TxIrq]) -#define MODSERIAL_RX_BUFFER_FULL (buffer_count[RxIrq]==buffer_size[RxIrq]) - -#define MODSERIAL_THR_HAS_SPACE (int)_LSR&MODSERIAL_LSR_THRE -#define MODSERIAL_TEMT_IS_EMPTY (int)_LSR&MODSERIAL_LSR_TEMT -#define MODSERIAL_RBR_HAS_DATA (int)_LSR&MODSERIAL_LSR_RDR - -#endif +/* + Copyright (c) 2010 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef MODSERIAL_MACROS_H +#define MODSERIAL_MACROS_H + +#define MODSERIAL_RBR 0x00 +#define MODSERIAL_THR 0x00 +#define MODSERIAL_DLL 0x00 +#define MODSERIAL_IER 0x04 +#define MODSERIAL_DML 0x04 +#define MODSERIAL_IIR 0x08 +#define MODSERIAL_FCR 0x08 +#define MODSERIAL_LCR 0x0C +#define MODSERIAL_LSR 0x14 +#define MODSERIAL_SCR 0x1C +#define MODSERIAL_ACR 0x20 +#define MODSERIAL_ICR 0x24 +#define MODSERIAL_FDR 0x28 +#define MODSERIAL_TER 0x30 + +#define MODSERIAL_LSR_RDR (1UL << 0) +#define MODSERIAL_LSR_OE (1UL << 1) +#define MODSERIAL_LSR_PE (1UL << 2) +#define MODSERIAL_LSR_FE (1UL << 3) +#define MODSERIAL_LSR_BR (1UL << 4) +#define MODSERIAL_LSR_THRE (1UL << 5) +#define MODSERIAL_LSR_TEMT (1UL << 6) +#define MODSERIAL_LSR_RXFE (1UL << 7) + +#define MODSERIAL_FIFO_ENABLE 1 +#define MODSERIAL_FIFO_RX_RESET 2 +#define MODSERIAL_FIFO_TX_RESET 4 + +#define _RBR *((char *)_base+MODSERIAL_RBR) +#define _THR *((char *)_base+MODSERIAL_THR) +#define _IIR *((char *)_base+MODSERIAL_IIR) +#define _IER *((char *)_base+MODSERIAL_IER) +#define _LSR *((char *)_base+MODSERIAL_LSR) +#define _FCR *((char *)_base+MODSERIAL_FCR) + +#define MODSERIAL_TX_BUFFER_EMPTY (buffer_count[TxIrq]==0) +#define MODSERIAL_RX_BUFFER_EMPTY (buffer_count[RxIrq]==0) +#define MODSERIAL_TX_BUFFER_FULL (buffer_count[TxIrq]==buffer_size[TxIrq]) +#define MODSERIAL_RX_BUFFER_FULL (buffer_count[RxIrq]==buffer_size[RxIrq]) + +#define MODSERIAL_THR_HAS_SPACE (int)_LSR&MODSERIAL_LSR_THRE +#define MODSERIAL_TEMT_IS_EMPTY (int)_LSR&MODSERIAL_LSR_TEMT +#define MODSERIAL_RBR_HAS_DATA (int)_LSR&MODSERIAL_LSR_RDR + +#endif
--- a/MODSERIAL.h Wed Nov 24 00:33:40 2010 +0000
+++ b/MODSERIAL.h Thu Jan 20 11:57:32 2011 +0000
@@ -1,879 +1,933 @@
-/*
- Copyright (c) 2010 Andy Kirkham
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
-
- @file MODSERIAL.h
- @purpose Extends Serial to provide fully buffered IO
- @version see ChangeLog.c
- @date Nov 2010
- @author Andy Kirkham
-*/
-
-#ifndef MODSERIAL_H
-#define MODSERIAL_H
-
-/** @defgroup API The MODSERIAL API */
-/** @defgroup MISC Misc MODSERIAL functions */
-/** @defgroup INTERNALS MODSERIAL Internals */
-
-#ifndef MODSERIAL_DEFAULT_RX_BUFFER_SIZE
-#define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 256
-#endif
-
-#ifndef MODSERIAL_DEFAULT_TX_BUFFER_SIZE
-#define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 256
-#endif
-
-#include "mbed.h"
-
-namespace AjK {
-
-/**
- * @author Andy Kirkham
- * @see http://mbed.org/cookbook/MODSERIAL
- * @see http://mbed.org/handbook/Serial
- * @see example.cpp
- * @see API
- *
- * <b>MODSERIAL</b> extends the Mbed library <a href="/handbook/Serial">Serial</a> to provide fully buffered
- * TX and RX streams. Buffer length is fully customisable.
- *
- * Before using MODSERIAL users should be familar with Mbed's standard <a href="/handbook/Serial">Serial</a>
- * library object. MODSERIAL is a direct "drop in" replacement for <a href="/handbook/Serial">Serial</a>. Where
- * previously Serial was used, MODSERIAL can be used as adirect replacement instantly offering standard
- * TX and RX buffering. By default, both TX and RX buffers are 256 bytes in length.
- *
- * @image html /media/uploads/mbedofficial/serial_interfaces.png
- *
- * Standard example:
- * @code
- * #include "mbed.h"
- * #include "MODSERIAL.h"
- *
- * MODSERIAL pc(USBTX, USBRX); // tx, rx
- *
- * int main() {
- * pc.printf("Hello World!");
- * while(1) {
- * pc.putc(pc.getc() + 1);
- * }
- * }
- * @endcode
- *
- * Example with alternate buffer length:
- * @code
- * #include "mbed.h"
- * #include "MODSERIAL.h"
- *
- * // Make TX and RX buffers 512byes in length
- * MODSERIAL pc(USBTX, USBRX, 512); // tx, rx
- *
- * int main() {
- * pc.printf("Hello World!");
- * while(1) {
- * pc.putc(pc.getc() + 1);
- * }
- * }
- * @endcode
- *
- * Example with alternate buffer length:
- * @code
- * #include "mbed.h"
- * #include "MODSERIAL.h"
- *
- * // Make TX 1024bytes and RX 512byes in length
- * MODSERIAL pc(USBTX, USBRX, 1024, 512); // tx, rx
- *
- * int main() {
- * pc.printf("Hello World!");
- * while(1) {
- * pc.putc(pc.getc() + 1);
- * }
- * }
- * @endcode
- */
-class MODSERIAL : public Serial
-{
-public:
-
- //! A copy of the Serial parity enum
- /** @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.format */
- enum Parity {
- None = 0
- , Odd
- , Even
- , Forced1
- , Forced0
- };
-
- //! A copy of the Serial IrqType enum
- enum IrqType {
- RxIrq = 0
- , TxIrq
- , RxOvIrq
- , TxOvIrq
- , TxEmpty
- };
-
- //! Non-blocking functions return code.
- enum Result {
- Ok = 0 /*!< Ok. */
- , NoMemory = -1 /*!< Memory allocation failed. */
- , NoChar = -1 /*!< No character in buffer. */
- , BufferOversize = -2 /*!< Oversized buffer. */
- };
-
- /**
- * The MODSERIAL constructor is used to initialise the serial object.
- *
- * @param tx PinName of the TX pin.
- * @param rx PinName of the TX pin.
- * @param name An option name for RPC usage.
- */
- MODSERIAL(PinName tx, PinName rx, const char *name = NULL);
-
- /**
- * The MODSERIAL constructor is used to initialise the serial object.
- *
- * @param tx PinName of the TX pin.
- * @param rx PinName of the TX pin.
- * @param bufferSize Integer of the TX and RX buffer sizes.
- * @param name An option name for RPC usage.
- */
- MODSERIAL(PinName tx, PinName rx, int bufferSize, const char *name = NULL);
-
- /**
- * The MODSERIAL constructor is used to initialise the serial object.
- *
- * @param tx PinName of the TX pin.
- * @param rx PinName of the TX pin.
- * @param txBufferSize Integer of the TX buffer sizes.
- * @param rxBufferSize Integer of the RX buffer sizes.
- * @param name An option name for RPC usage.
- */
- MODSERIAL(PinName tx, PinName rx, int txBufferSize, int rxBufferSize, const char *name = NULL);
-
- virtual ~MODSERIAL();
-
- /**
- * Function: attach
- *
- * The Mbed standard <a href="/handbook/Serial">Serial</a> library object allows an interrupt callback
- * to be made when a byte is received by the TX or RX UART hardware. MODSERIAL traps these interrupts
- * to enable it's buffering system. However, after the byte has been received/sent under interrupt control,
- * MODSERIAL can callback a user function as a notification of the interrupt. Note, user code should not
- * directly interact with the Uart hardware, MODSERIAL does that, instead, MODSERIAL API functions should
- * be used.
- *
- * <b>Note</b>, a character is written out then, if there is room in the TX FIFO and the TX buffer is empty,
- * putc() will put the character directly into THR (the output holding register). If the TX FIFO is full and
- * cannot accept the character, it is placed into the TX output buffer. The TX interrupts are then enabled
- * so that when the TX FIFO empties, the TX buffer is then transferred to the THR FIFO. The TxIrq will ONLY
- * be activated when this transfer of a character from BUFFER to THR FIFO takes place. If your character
- * throughput is not high bandwidth, then the 16 byte TX FIFO may be enough and the TX output buffer may
- * never come into play.
- *
- * @code
- * #include "mbed.h"
- * #include "MODSERIAL.h"
- *
- * DigitalOut led1(LED1);
- * DigitalOut led2(LED2);
- * DigitalOut led3(LED3);
- *
- * // To test, connect p9 to p10 as a loopback.
- * MODSERIAL pc(p9, p10);
- *
- * // This function is called when a character goes into the TX buffer.
- * void txCallback(void) {
- * led2 = !led2;
- * }
- *
- * // This function is called when a character goes into the RX buffer.
- * void rxCallback(void) {
- * led3 = !led3;
- * }
- *
- * int main() {
- * pc.baud(115200);
- * pc.attach(&txCallback, MODSERIAL::TxIrq);
- * pc.attach(&rxCallback, MODSERIAL::RxIrq);
- *
- * while(1) {
- * led1 = !led1;
- * wait(0.5);
- * pc.putc('A');
- * wait(0.5);
- * }
- * ]
- * @endcode
- *
- * @ingroup API
- * @param fptr A pointer to a void function, or 0 to set as none
- * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
- */
- void attach(void (*fptr)(void), IrqType type = RxIrq) { _isr[type].attach(fptr); }
-
- /**
- * Function: attach
- *
- * The Mbed standard <a href="/handbook/Serial">Serial</a> library object allows an interrupt callback
- * to be made when a byte is received by the TX or RX UART hardware. MODSERIAL traps these interrupts
- * to enable it's buffering system. However, after the byte has been received/sent under interrupt control,
- * MODSERIAL can callback a user function as a notification of the interrupt. Note, user code should not
- * directly interact with the Uart hardware, MODSERIAL does that, instead, MODSERIAL API functions should
- * be used.
- *
- * <b>Note</b>, a character is written out then, if there is room in the TX FIFO and the TX buffer is empty,
- * putc() will put the character directly into THR (the output holding register). If the TX FIFO is full and
- * cannot accept the character, it is placed into the TX output buffer. The TX interrupts are then enabled
- * so that when the TX FIFO empties, the TX buffer is then transferred to the THR FIFO. The TxIrq will ONLY
- * be activated when this transfer of a character from BUFFER to THR FIFO takes place. If your character
- * throughput is not high bandwidth, then the 16 byte TX FIFO may be enough and the TX output buffer may
- * never come into play.
- *
- * @code
- * #include "mbed.h"
- * #include "MODSERIAL.h"
- *
- * DigitalOut led1(LED1);
- * DigitalOut led2(LED2);
- * DigitalOut led3(LED3);
- *
- * // To test, connect p9 to p10 as a loopback.
- * MODSERIAL pc(p9, p10);
- *
- * class Foo {
- * public:
- * // This method is called when a character goes into the TX buffer.
- * void txCallback(void) { led2 = !led2; }
- *
- * // This method is called when a character goes into the RX buffer.
- * void rxCallback(void) { led3 = !led3; }
- * };
- *
- * Foo foo;
- *
- * int main() {
- * pc.baud(115200);
- * pc.attach(&foo, &Foo::txCallback, MODSERIAL::TxIrq);
- * pc.attach(&foo, &Foo::rxCallback, MODSERIAL::RxIrq);
- *
- * while(1) {
- * led1 = !led1;
- * wait(0.5);
- * pc.putc('A');
- * wait(0.5);
- * }
- * ]
- * @endcode
- *
- * @ingroup API
- * @param tptr A pointer to the object to call the member function on
- * @param mptr A pointer to the member function to be called
- * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
- */
- template<typename T>
- void attach(T* tptr, void (T::*mptr)(void), IrqType type = RxIrq) {
- if((mptr != NULL) && (tptr != NULL)) {
- _isr[type].attach(tptr, mptr);
- }
- }
-
- /**
- * @see attach
- * @ingroup API
- */
- void connect(void (*fptr)(void), IrqType type = RxIrq) { _isr[RxIrq].attach(fptr); }
-
- /**
- * @see attach
- * @ingroup API
- */
- template<typename T>
- void connect(T* tptr, void (T::*mptr)(void), IrqType type = RxIrq) {
- if((mptr != NULL) && (tptr != NULL)) {
- _isr[type].attach(tptr, mptr);
- }
- }
-
- /**
- * Function: writeable
- *
- * Determine if there is space available to write a byte
- *
- * @ingroup API
- * @return 1 if there is space to write a character, else 0
- */
- int writeable() { return txBufferFull() ? 0 : 1; }
-
- /**
- * Function: readable
- *
- * Determine if there is a byte available to read
- *
- * @ingroup API
- * @return 1 if there is a character available to read, else 0
- */
- int readable() { return rxBufferEmpty() ? 0 : 1; }
-
- /**
- * Function: txBufferSane
- *
- * Determine if the TX buffer has been initialized.
- *
- * @ingroup API
- * @return true if the buffer is initialized, else false
- */
- bool txBufferSane(void) { return buffer[TxIrq] != (char *)NULL ? true : false; }
-
- /**
- * Function: rxBufferSane
- *
- * Determine if the RX buffer has been initialized.
- *
- * @ingroup API
- * @return true if the buffer is initialized, else false
- */
- bool rxBufferSane(void) { return buffer[TxIrq] != (char *)NULL ? true : false; }
-
- /**
- * Function: txBufferGetCount
- *
- * Returns how many bytes are in the TX buffer
- *
- * @ingroup API
- * @return The number of bytes in the TX buffer
- */
- int txBufferGetCount(void) { return buffer_count[TxIrq]; }
-
- /**
- * Function: rxBufferGetCount
- *
- * Returns how many bytes are in the RX buffer
- *
- * @ingroup API
- * @return The number of bytes in the RX buffer
- */
- int rxBufferGetCount(void) { return buffer_count[RxIrq]; }
-
- /**
- * Function: txBufferGetSize
- *
- * Returns the current size of the TX buffer
- *
- * @ingroup API
- * @return The length iof the TX buffer in bytes
- */
- int txBufferGetSize(int size) { return buffer_size[TxIrq]; }
-
- /**
- * Function: rxBufferGetSize
- *
- * Returns the current size of the RX buffer
- *
- * @ingroup API
- * @return The length iof the RX buffer in bytes
- */
- int rxBufferGetSize(int size) { return buffer_size[RxIrq]; }
-
- /**
- * Function: txBufferFull
- *
- * Is the TX buffer full?
- *
- * @ingroup API
- * @return true if the TX buffer is full, otherwise false
- */
- bool txBufferFull(void);
-
- /**
- * Function: rxBufferFull
- *
- * Is the RX buffer full?
- *
- * @ingroup API
- * @return true if the RX buffer is full, otherwise false
- */
- bool rxBufferFull(void);
-
- /**
- * Function: txBufferEmpty
- *
- * Is the TX buffer empty?
- *
- * @ingroup API
- * @return true if the TX buffer is empty, otherwise false
- */
- bool txBufferEmpty(void);
-
- /**
- * Function: rxBufferEmpty
- *
- * Is the RX buffer empty?
- *
- * @ingroup API
- * @return true if the RX buffer is empty, otherwise false
- */
- bool rxBufferEmpty(void);
-
- /**
- * Function: txBufferSetSize
- *
- * Change the TX buffer size.
- *
- * @see Result
- * @ingroup API
- * @param size The new TX buffer size in bytes.
- * @param m Perform a memory sanity check. Errs the Mbed if memory alloc fails.
- * @return Result Ok on success.
- */
- int txBufferSetSize(int size, bool m) { return resizeBuffer(size, TxIrq, m); }
-
- /**
- * Function: rxBufferSetSize
- *
- * Change the RX buffer size.
- *
- * @see Result
- * @ingroup API
- * @param size The new RX buffer size in bytes.
- * @param m Perform a memory sanity check. Errs the Mbed if memory alloc fails.
- * @return Result Ok on success.
- */
- int rxBufferSetSize(int size, bool m) { return resizeBuffer(size, RxIrq, m); }
-
- /**
- * Function: txBufferSetSize
- *
- * Change the TX buffer size.
- * Always performs a memory sanity check, halting the Mbed on failure.
- *
- * @see Result
- * @ingroup API
- * @param size The new TX buffer size in bytes.
- * @return Result Ok on success.
- */
- int txBufferSetSize(int size) { return resizeBuffer(size, TxIrq, true); }
-
- /**
- * Function: rxBufferSetSize
- *
- * Change the RX buffer size.
- * Always performs a memory sanity check, halting the Mbed on failure.
- *
- * @see Result
- * @ingroup API
- * @param size The new RX buffer size in bytes.
- * @return Result Ok on success.
- */
- int rxBufferSetSize(int size) { return resizeBuffer(size, RxIrq, true); }
-
- /**
- * Function: txBufferFlush
- *
- * Remove all bytes from the TX buffer.
- * @ingroup API
- */
- void txBufferFlush(void) { flushBuffer(TxIrq); }
-
- /**
- * Function: rxBufferFlush
- *
- * Remove all bytes from the RX buffer.
- * @ingroup API
- */
- void rxBufferFlush(void) { flushBuffer(RxIrq); }
-
- /**
- * Function: getcNb
- *
- * Like getc() but is non-blocking. If no bytes are in the RX buffer this
- * function returns Result::NoChar (-1)
- *
- * @ingroup API
- * @return A byte from the RX buffer or Result::NoChar (-1) if bufer empty.
- */
- int getcNb() { return __getc(false); }
-
- /**
- * Function: getc
- *
- * Overloaded version of Serial::getc()
- *
- * This function blocks (if the RX buffer is empty the function will wait for a
- * character to arrive and then return that character).
- *
- * @ingroup API
- * @return A byte from the RX buffer
- */
- int getc() { return __getc(true); }
-
- /**
- * Function: txGetLastChar
- *
- * Rteurn the last byte to pass through the TX interrupt handler.
- *
- * @ingroup MISC
- * @return The byte
- */
- char txGetLastChar(void) { return txc; }
-
- /**
- * Function: rxGetLastChar
- *
- * Return the last byte to pass through the RX interrupt handler.
- *
- * @ingroup MISC
- * @return The byte
- */
- char rxGetLastChar(void) { return rxc; }
-
- /**
- * Function: txIsBusy
- *
- * If the Uart is still actively sending characters this
- * function will return true.
- *
- * @ingroup API
- * @return bool
- */
- bool txIsBusy(void);
-
- #if 0 // Inhereted from Serial/Stream, for documentation only
- /**
- * Function: putc
- *
- * Write a character
- * Inhereted from Serial/Stream
- *
- * @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.putc
- * @ingroup API
- * @param c The character to write to the serial port
- */
- int putc(int c);
- #endif
-
- #if 0 // Inhereted from Serial/Stream, for documentation only
- /**
- * Function: printf
- *
- * Write a formated string
- * Inhereted from Serial/Stream
- *
- * @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.printf
- * @ingroup API
- * @param format A printf-style format string, followed by the variables to use in formating the string.
- */
- int printf(const char* format, ...);
- #endif
-
- #if 0 // Inhereted from Serial/Stream, for documentation only
- /**
- * Function: scanf
- *
- * Read a formated string
- * Inhereted from Serial/Stream
- *
- * @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.scanf
- * @ingroup API
- * @param format - A scanf-style format string, followed by the pointers to variables to store the results.
- */
- int scanf(const char* format, ...);
- #endif
-
-protected:
-
- /**
- * A pointer to the UART peripheral base address being used.
- * @ingroup INTERNALS
- */
- void *_base;
-
- /**
- * The last byte to pass through the TX IRQ handler.
- * @ingroup INTERNALS
- */
- volatile char txc;
-
- /**
- * The last byte to pass through the RX IRQ handler.
- * @ingroup INTERNALS
- */
- volatile char rxc;
-
- /**
- * Pointers to the TX and RX buffers.
- * @ingroup INTERNALS
- */
- volatile char *buffer[2];
-
- /**
- * Buffer in pointers.
- * @ingroup INTERNALS
- */
- volatile int buffer_in[2];
-
- /**
- * Buffer out pointers.
- * @ingroup INTERNALS
- */
- volatile int buffer_out[2];
-
- /**
- * Buffer lengths.
- * @ingroup INTERNALS
- */
- volatile int buffer_size[2];
-
- /**
- * Buffer content counters.
- * @ingroup INTERNALS
- */
- volatile int buffer_count[2];
-
- /**
- * Buffer overflow.
- * @ingroup INTERNALS
- */
- volatile int buffer_overflow[2];
-
- /**
- * Callback system.
- * @ingroup INTERNALS
- */
- FunctionPointer _isr[5];
-
- /**
- * TX Interrupt Service Routine.
- * @ingroup INTERNALS
- */
- void isr_tx(bool doCallback);
-
- /**
- * TX Interrupt Service Routine stub version.
- * @ingroup INTERNALS
- */
- void isr_tx(void) { isr_tx(true); }
-
-
- /**
- * RX Interrupt Service Routine.
- * @ingroup INTERNALS
- */
- void isr_rx(void);
-
- /**
- * Disable the interrupts for this Uart.
- * @ingroup INTERNALS
- */
- void disableIrq(void);
-
- /**
- * Enable the interrupts for this Uart.
- * @ingroup INTERNALS
- */
- void enableIrq(void);
-
- /**
- * Get a character from the RX buffer
- * @ingroup INTERNALS
- * @param bool True to block (wait for input)
- * @return A byte from the buffer.
- */
- int __getc(bool);
-
- /**
- * Put a character from the TX buffer
- * @ingroup INTERNALS
- * @param bool True to block (wait for space in the TX buffer if full)
- * @return 0 on success
- */
- int __putc(int c, bool);
-
- /**
- * Function: _putc
- * Overloaded virtual function.
- */
- virtual int _putc(int c) { return __putc(c, true); }
-
- /**
- * Function: _getc
- * Overloaded virtual function.
- */
- virtual int _getc() { return __getc(true); }
-
- /**
- * Function: init
- * Initialize the MODSERIAL object
- * @ingroup INTERNALS
- */
- void init(int txSize, int rxSize);
-
- /**
- * Function: flushBuffer
- * @ingroup INTERNALS
- */
- void flushBuffer(IrqType type);
-
- /**
- * Function: resizeBuffer
- * @ingroup INTERNALS
- */
- int resizeBuffer(int size, IrqType type = RxIrq, bool memory_check = true);
-
- /**
- * Function: downSizeBuffer
- * @ingroup INTERNALS
- */
- int downSizeBuffer(int size, IrqType type, bool memory_check);
-
- /**
- * Function: upSizeBuffer
- * @ingroup INTERNALS
- */
- int upSizeBuffer(int size, IrqType type, bool memory_check);
-
- /*
- * If MODDMA is available the compile in code to handle sending
- * an arbitary char buffer. Note, the parts before teh #ifdef
- * are declared so that MODSERIAL can access then even if MODDMA
- * isn't avaiable. Since MODDMA.h is only available at this point
- * all DMA functionality must be declared inline in the class
- * definition.
- */
-public:
-
- int dmaSendChannel;
- void *moddma_p;
-
-#ifdef MODDMA_H
-
- /**
- * Set the "void pointer" moddma_p to be a pointer to a
- * MODDMA controller class instance. Used to manage the
- * data transfer of DMA configurations.
- *
- * @ingroup API
- * @param p A pointer to "the" instance of MODDMA.
- */
- void MODDMA(MODDMA *p) { moddma_p = p; }
-
- /**
- * Send a char buffer to the Uarts TX system
- * using DMA. This blocks regular library
- * sending.
- *
- * @param buffer A char buffer of bytes to send.
- * @param len The length of the buffer to send.
- * @param dmaChannel The DMA channel to use, defaults to 7
- * @return MODDMA::Status MODDMA::ok if all went ok
- */
- int dmaSend(char *buffer, int len, int dmaChannel = 7)
- {
- if (moddma_p == (void *)NULL) return -2;
- class MODDMA *dma = (class MODDMA *)moddma_p;
-
- dmaSendChannel = dmaChannel & 0x7;
-
- uint32_t conn = MODDMA::UART0_Tx;
- switch(_uidx) {
- case 0: conn = MODDMA::UART0_Tx; break;
- case 1: conn = MODDMA::UART1_Tx; break;
- case 2: conn = MODDMA::UART2_Tx; break;
- case 3: conn = MODDMA::UART3_Tx; break;
- }
-
- MODDMA_Config *config = new MODDMA_Config;
- config
- ->channelNum ( (MODDMA::CHANNELS)(dmaSendChannel & 0x7) )
- ->srcMemAddr ( (uint32_t) buffer )
- ->transferSize ( len )
- ->transferType ( MODDMA::m2p )
- ->dstConn ( conn )
- ->attach_tc ( this, &MODSERIAL::dmaSendCallback )
- ->attach_err ( this, &MODSERIAL::dmaSendCallback )
- ; // config end
-
- // Setup the configuration.
- if (dma->Setup(config) != MODDMA::Ok) {
- return -1;
- }
-
- //dma.Enable( MODDMA::Channel_0 );
- dma->Enable( config->channelNum() );
- return MODDMA::Ok;
- }
-
- /**
- * Attach a callback to the DMA completion.
- *
- * @ingroup API
- * @param fptr A function pointer to call
- * @return this
- */
- void attach_dmaSendComplete(void (*fptr)(void)) {
- _isrDmaSendComplete.attach(fptr);
- }
-
- /**
- * Attach a callback to the DMA completion.
- *
- * @ingroup API
- * @param tptr A template pointer to the calling object
- * @param mptr A method pointer within the object to call.
- * @return this
- */
- template<typename T>
- void attach_dmaSendComplete(T* tptr, void (T::*mptr)(void)) {
- if((mptr != NULL) && (tptr != NULL)) {
- _isrDmaSendComplete.attach(tptr, mptr);
- }
- }
-
- FunctionPointer _isrDmaSendComplete;
-
-protected:
- /**
- * Callback for dmaSend().
- */
- void dmaSendCallback(void)
- {
- if (moddma_p == (void *)NULL) return;
- class MODDMA *dma = (class MODDMA *)moddma_p;
-
- MODDMA_Config *config = dma->getConfig();
- dma->haltAndWaitChannelComplete( (MODDMA::CHANNELS)config->channelNum());
- dma->Disable( (MODDMA::CHANNELS)config->channelNum() );
- if (dma->irqType() == MODDMA::TcIrq) dma->clearTcIrq();
- if (dma->irqType() == MODDMA::ErrIrq) dma->clearErrIrq();
- dmaSendChannel = -1;
- _isrDmaSendComplete.call();
- }
-
-#endif // MODDMA_H
-
-};
-
-}; // namespace AjK ends
-
-using namespace AjK;
-
-#endif
+/*
+ Copyright (c) 2010 Andy Kirkham
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+
+ @file MODSERIAL.h
+ @purpose Extends Serial to provide fully buffered IO
+ @version see ChangeLog.c
+ @date Nov 2010
+ @author Andy Kirkham
+*/
+
+#ifndef MODSERIAL_H
+#define MODSERIAL_H
+
+/** @defgroup API The MODSERIAL API */
+/** @defgroup MISC Misc MODSERIAL functions */
+/** @defgroup INTERNALS MODSERIAL Internals */
+
+#ifndef MODSERIAL_DEFAULT_RX_BUFFER_SIZE
+#define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 256
+#endif
+
+#ifndef MODSERIAL_DEFAULT_TX_BUFFER_SIZE
+#define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 256
+#endif
+
+#include "mbed.h"
+
+namespace AjK {
+
+/**
+ * @author Andy Kirkham
+ * @see http://mbed.org/cookbook/MODSERIAL
+ * @see http://mbed.org/handbook/Serial
+ * @see example.cpp
+ * @see API
+ *
+ * <b>MODSERIAL</b> extends the Mbed library <a href="/handbook/Serial">Serial</a> to provide fully buffered
+ * TX and RX streams. Buffer length is fully customisable.
+ *
+ * Before using MODSERIAL users should be familar with Mbed's standard <a href="/handbook/Serial">Serial</a>
+ * library object. MODSERIAL is a direct "drop in" replacement for <a href="/handbook/Serial">Serial</a>. Where
+ * previously Serial was used, MODSERIAL can be used as adirect replacement instantly offering standard
+ * TX and RX buffering. By default, both TX and RX buffers are 256 bytes in length.
+ *
+ * @image html /media/uploads/mbedofficial/serial_interfaces.png
+ *
+ * Standard example:
+ * @code
+ * #include "mbed.h"
+ * #include "MODSERIAL.h"
+ *
+ * MODSERIAL pc(USBTX, USBRX); // tx, rx
+ *
+ * int main() {
+ * pc.printf("Hello World!");
+ * while(1) {
+ * pc.putc(pc.getc() + 1);
+ * }
+ * }
+ * @endcode
+ *
+ * Example with alternate buffer length:
+ * @code
+ * #include "mbed.h"
+ * #include "MODSERIAL.h"
+ *
+ * // Make TX and RX buffers 512byes in length
+ * MODSERIAL pc(USBTX, USBRX, 512); // tx, rx
+ *
+ * int main() {
+ * pc.printf("Hello World!");
+ * while(1) {
+ * pc.putc(pc.getc() + 1);
+ * }
+ * }
+ * @endcode
+ *
+ * Example with alternate buffer length:
+ * @code
+ * #include "mbed.h"
+ * #include "MODSERIAL.h"
+ *
+ * // Make TX 1024bytes and RX 512byes in length
+ * MODSERIAL pc(USBTX, USBRX, 1024, 512); // tx, rx
+ *
+ * int main() {
+ * pc.printf("Hello World!");
+ * while(1) {
+ * pc.putc(pc.getc() + 1);
+ * }
+ * }
+ * @endcode
+ */
+class MODSERIAL : public Serial
+{
+public:
+
+ //! A copy of the Serial parity enum
+ /** @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.format */
+ enum Parity {
+ None = 0
+ , Odd
+ , Even
+ , Forced1
+ , Forced0
+ };
+
+ //! A copy of the Serial IrqType enum
+ enum IrqType {
+ RxIrq = 0
+ , TxIrq
+ , RxOvIrq
+ , TxOvIrq
+ , TxEmpty
+ , RxAutoDetect
+ , NumOfIrqTypes
+ };
+
+ //! Non-blocking functions return code.
+ enum Result {
+ Ok = 0 /*!< Ok. */
+ , NoMemory = -1 /*!< Memory allocation failed. */
+ , NoChar = -1 /*!< No character in buffer. */
+ , BufferOversize = -2 /*!< Oversized buffer. */
+ };
+
+ /**
+ * The MODSERIAL constructor is used to initialise the serial object.
+ *
+ * @param tx PinName of the TX pin.
+ * @param rx PinName of the TX pin.
+ * @param name An option name for RPC usage.
+ */
+ MODSERIAL(PinName tx, PinName rx, const char *name = NULL);
+
+ /**
+ * The MODSERIAL constructor is used to initialise the serial object.
+ *
+ * @param tx PinName of the TX pin.
+ * @param rx PinName of the TX pin.
+ * @param bufferSize Integer of the TX and RX buffer sizes.
+ * @param name An option name for RPC usage.
+ */
+ MODSERIAL(PinName tx, PinName rx, int bufferSize, const char *name = NULL);
+
+ /**
+ * The MODSERIAL constructor is used to initialise the serial object.
+ *
+ * @param tx PinName of the TX pin.
+ * @param rx PinName of the TX pin.
+ * @param txBufferSize Integer of the TX buffer sizes.
+ * @param rxBufferSize Integer of the RX buffer sizes.
+ * @param name An option name for RPC usage.
+ */
+ MODSERIAL(PinName tx, PinName rx, int txBufferSize, int rxBufferSize, const char *name = NULL);
+
+ virtual ~MODSERIAL();
+
+ /**
+ * Function: attach
+ *
+ * The Mbed standard <a href="/handbook/Serial">Serial</a> library object allows an interrupt callback
+ * to be made when a byte is received by the TX or RX UART hardware. MODSERIAL traps these interrupts
+ * to enable it's buffering system. However, after the byte has been received/sent under interrupt control,
+ * MODSERIAL can callback a user function as a notification of the interrupt. Note, user code should not
+ * directly interact with the Uart hardware, MODSERIAL does that, instead, MODSERIAL API functions should
+ * be used.
+ *
+ * <b>Note</b>, a character is written out then, if there is room in the TX FIFO and the TX buffer is empty,
+ * putc() will put the character directly into THR (the output holding register). If the TX FIFO is full and
+ * cannot accept the character, it is placed into the TX output buffer. The TX interrupts are then enabled
+ * so that when the TX FIFO empties, the TX buffer is then transferred to the THR FIFO. The TxIrq will ONLY
+ * be activated when this transfer of a character from BUFFER to THR FIFO takes place. If your character
+ * throughput is not high bandwidth, then the 16 byte TX FIFO may be enough and the TX output buffer may
+ * never come into play.
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "MODSERIAL.h"
+ *
+ * DigitalOut led1(LED1);
+ * DigitalOut led2(LED2);
+ * DigitalOut led3(LED3);
+ *
+ * // To test, connect p9 to p10 as a loopback.
+ * MODSERIAL pc(p9, p10);
+ *
+ * // This function is called when a character goes into the TX buffer.
+ * void txCallback(void) {
+ * led2 = !led2;
+ * }
+ *
+ * // This function is called when a character goes into the RX buffer.
+ * void rxCallback(void) {
+ * led3 = !led3;
+ * }
+ *
+ * int main() {
+ * pc.baud(115200);
+ * pc.attach(&txCallback, MODSERIAL::TxIrq);
+ * pc.attach(&rxCallback, MODSERIAL::RxIrq);
+ *
+ * while(1) {
+ * led1 = !led1;
+ * wait(0.5);
+ * pc.putc('A');
+ * wait(0.5);
+ * }
+ * ]
+ * @endcode
+ *
+ * @ingroup API
+ * @param fptr A pointer to a void function, or 0 to set as none
+ * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
+ */
+ void attach(void (*fptr)(void), IrqType type = RxIrq) { _isr[type].attach(fptr); }
+
+ /**
+ * Function: attach
+ *
+ * The Mbed standard <a href="/handbook/Serial">Serial</a> library object allows an interrupt callback
+ * to be made when a byte is received by the TX or RX UART hardware. MODSERIAL traps these interrupts
+ * to enable it's buffering system. However, after the byte has been received/sent under interrupt control,
+ * MODSERIAL can callback a user function as a notification of the interrupt. Note, user code should not
+ * directly interact with the Uart hardware, MODSERIAL does that, instead, MODSERIAL API functions should
+ * be used.
+ *
+ * <b>Note</b>, a character is written out then, if there is room in the TX FIFO and the TX buffer is empty,
+ * putc() will put the character directly into THR (the output holding register). If the TX FIFO is full and
+ * cannot accept the character, it is placed into the TX output buffer. The TX interrupts are then enabled
+ * so that when the TX FIFO empties, the TX buffer is then transferred to the THR FIFO. The TxIrq will ONLY
+ * be activated when this transfer of a character from BUFFER to THR FIFO takes place. If your character
+ * throughput is not high bandwidth, then the 16 byte TX FIFO may be enough and the TX output buffer may
+ * never come into play.
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "MODSERIAL.h"
+ *
+ * DigitalOut led1(LED1);
+ * DigitalOut led2(LED2);
+ * DigitalOut led3(LED3);
+ *
+ * // To test, connect p9 to p10 as a loopback.
+ * MODSERIAL pc(p9, p10);
+ *
+ * class Foo {
+ * public:
+ * // This method is called when a character goes into the TX buffer.
+ * void txCallback(void) { led2 = !led2; }
+ *
+ * // This method is called when a character goes into the RX buffer.
+ * void rxCallback(void) { led3 = !led3; }
+ * };
+ *
+ * Foo foo;
+ *
+ * int main() {
+ * pc.baud(115200);
+ * pc.attach(&foo, &Foo::txCallback, MODSERIAL::TxIrq);
+ * pc.attach(&foo, &Foo::rxCallback, MODSERIAL::RxIrq);
+ *
+ * while(1) {
+ * led1 = !led1;
+ * wait(0.5);
+ * pc.putc('A');
+ * wait(0.5);
+ * }
+ * ]
+ * @endcode
+ *
+ * @ingroup API
+ * @param tptr A pointer to the object to call the member function on
+ * @param mptr A pointer to the member function to be called
+ * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
+ */
+ template<typename T>
+ void attach(T* tptr, void (T::*mptr)(void), IrqType type = RxIrq) {
+ if((mptr != NULL) && (tptr != NULL)) {
+ _isr[type].attach(tptr, mptr);
+ }
+ }
+
+ /**
+ * @see attach
+ * @ingroup API
+ */
+ void connect(void (*fptr)(void), IrqType type = RxIrq) { _isr[RxIrq].attach(fptr); }
+
+ /**
+ * @see attach
+ * @ingroup API
+ */
+ template<typename T>
+ void connect(T* tptr, void (T::*mptr)(void), IrqType type = RxIrq) {
+ if((mptr != NULL) && (tptr != NULL)) {
+ _isr[type].attach(tptr, mptr);
+ }
+ }
+
+ /**
+ * Function: writeable
+ *
+ * Determine if there is space available to write a byte
+ *
+ * @ingroup API
+ * @return 1 if there is space to write a character, else 0
+ */
+ int writeable() { return txBufferFull() ? 0 : 1; }
+
+ /**
+ * Function: readable
+ *
+ * Determine if there is a byte available to read
+ *
+ * @ingroup API
+ * @return 1 if there is a character available to read, else 0
+ */
+ int readable() { return rxBufferEmpty() ? 0 : 1; }
+
+ /**
+ * Function: txBufferSane
+ *
+ * Determine if the TX buffer has been initialized.
+ *
+ * @ingroup API
+ * @return true if the buffer is initialized, else false
+ */
+ bool txBufferSane(void) { return buffer[TxIrq] != (char *)NULL ? true : false; }
+
+ /**
+ * Function: rxBufferSane
+ *
+ * Determine if the RX buffer has been initialized.
+ *
+ * @ingroup API
+ * @return true if the buffer is initialized, else false
+ */
+ bool rxBufferSane(void) { return buffer[TxIrq] != (char *)NULL ? true : false; }
+
+ /**
+ * Function: txBufferGetCount
+ *
+ * Returns how many bytes are in the TX buffer
+ *
+ * @ingroup API
+ * @return The number of bytes in the TX buffer
+ */
+ int txBufferGetCount(void) { return buffer_count[TxIrq]; }
+
+ /**
+ * Function: rxBufferGetCount
+ *
+ * Returns how many bytes are in the RX buffer
+ *
+ * @ingroup API
+ * @return The number of bytes in the RX buffer
+ */
+ int rxBufferGetCount(void) { return buffer_count[RxIrq]; }
+
+ /**
+ * Function: txBufferGetSize
+ *
+ * Returns the current size of the TX buffer
+ *
+ * @ingroup API
+ * @return The length iof the TX buffer in bytes
+ */
+ int txBufferGetSize(int size) { return buffer_size[TxIrq]; }
+
+ /**
+ * Function: rxBufferGetSize
+ *
+ * Returns the current size of the RX buffer
+ *
+ * @ingroup API
+ * @return The length iof the RX buffer in bytes
+ */
+ int rxBufferGetSize(int size) { return buffer_size[RxIrq]; }
+
+ /**
+ * Function: txBufferFull
+ *
+ * Is the TX buffer full?
+ *
+ * @ingroup API
+ * @return true if the TX buffer is full, otherwise false
+ */
+ bool txBufferFull(void);
+
+ /**
+ * Function: rxBufferFull
+ *
+ * Is the RX buffer full?
+ *
+ * @ingroup API
+ * @return true if the RX buffer is full, otherwise false
+ */
+ bool rxBufferFull(void);
+
+ /**
+ * Function: txBufferEmpty
+ *
+ * Is the TX buffer empty?
+ *
+ * @ingroup API
+ * @return true if the TX buffer is empty, otherwise false
+ */
+ bool txBufferEmpty(void);
+
+ /**
+ * Function: rxBufferEmpty
+ *
+ * Is the RX buffer empty?
+ *
+ * @ingroup API
+ * @return true if the RX buffer is empty, otherwise false
+ */
+ bool rxBufferEmpty(void);
+
+ /**
+ * Function: txBufferSetSize
+ *
+ * Change the TX buffer size.
+ *
+ * @see Result
+ * @ingroup API
+ * @param size The new TX buffer size in bytes.
+ * @param m Perform a memory sanity check. Errs the Mbed if memory alloc fails.
+ * @return Result Ok on success.
+ */
+ int txBufferSetSize(int size, bool m) { return resizeBuffer(size, TxIrq, m); }
+
+ /**
+ * Function: rxBufferSetSize
+ *
+ * Change the RX buffer size.
+ *
+ * @see Result
+ * @ingroup API
+ * @param size The new RX buffer size in bytes.
+ * @param m Perform a memory sanity check. Errs the Mbed if memory alloc fails.
+ * @return Result Ok on success.
+ */
+ int rxBufferSetSize(int size, bool m) { return resizeBuffer(size, RxIrq, m); }
+
+ /**
+ * Function: txBufferSetSize
+ *
+ * Change the TX buffer size.
+ * Always performs a memory sanity check, halting the Mbed on failure.
+ *
+ * @see Result
+ * @ingroup API
+ * @param size The new TX buffer size in bytes.
+ * @return Result Ok on success.
+ */
+ int txBufferSetSize(int size) { return resizeBuffer(size, TxIrq, true); }
+
+ /**
+ * Function: rxBufferSetSize
+ *
+ * Change the RX buffer size.
+ * Always performs a memory sanity check, halting the Mbed on failure.
+ *
+ * @see Result
+ * @ingroup API
+ * @param size The new RX buffer size in bytes.
+ * @return Result Ok on success.
+ */
+ int rxBufferSetSize(int size) { return resizeBuffer(size, RxIrq, true); }
+
+ /**
+ * Function: txBufferFlush
+ *
+ * Remove all bytes from the TX buffer.
+ * @ingroup API
+ */
+ void txBufferFlush(void) { flushBuffer(TxIrq); }
+
+ /**
+ * Function: rxBufferFlush
+ *
+ * Remove all bytes from the RX buffer.
+ * @ingroup API
+ */
+ void rxBufferFlush(void) { flushBuffer(RxIrq); }
+
+ /**
+ * Function: getcNb
+ *
+ * Like getc() but is non-blocking. If no bytes are in the RX buffer this
+ * function returns Result::NoChar (-1)
+ *
+ * @ingroup API
+ * @return A byte from the RX buffer or Result::NoChar (-1) if bufer empty.
+ */
+ int getcNb() { return __getc(false); }
+
+ /**
+ * Function: getc
+ *
+ * Overloaded version of Serial::getc()
+ *
+ * This function blocks (if the RX buffer is empty the function will wait for a
+ * character to arrive and then return that character).
+ *
+ * @ingroup API
+ * @return A byte from the RX buffer
+ */
+ int getc() { return __getc(true); }
+
+ /**
+ * Function: txGetLastChar
+ *
+ * Rteurn the last byte to pass through the TX interrupt handler.
+ *
+ * @ingroup MISC
+ * @return The byte
+ */
+ char txGetLastChar(void) { return txc; }
+
+ /**
+ * Function: rxGetLastChar
+ *
+ * Return the last byte to pass through the RX interrupt handler.
+ *
+ * @ingroup MISC
+ * @return The byte
+ */
+ char rxGetLastChar(void) { return rxc; }
+
+ /**
+ * Function: txIsBusy
+ *
+ * If the Uart is still actively sending characters this
+ * function will return true.
+ *
+ * @ingroup API
+ * @return bool
+ */
+ bool txIsBusy(void);
+
+ /**
+ * Function: autoDectectChar
+ *
+ * Set the char that, if seen incoming, invokes the AutoDetectChar callback.
+ *
+ * @ingroup API
+ * @param int c The character to detect.
+ */
+ void autoDectectChar(char c) { auto_detect_char = c; }
+
+ /**
+ * Function: move
+ *
+ * Move contents of RX buffer to external buffer. Stops if "end" detected.
+ *
+ * @ingroup API
+ * @param char *s The destination buffer address
+ * @param int max The maximum number of chars to move.
+ * @param char end If this char is detected stop moving.
+ */
+ int move(char *s, int max, char end) {
+ int counter = 0;
+ char c;
+ while(readable()) {
+ c = getc();
+ if (c == end) break;
+ *(s++) = c;
+ counter++;
+ if (counter == max) break;
+ }
+ return counter;
+ }
+
+ /**
+ * Function: move (overloaded)
+ *
+ * Move contents of RX buffer to external buffer. Stops if auto_detect_char detected.
+ *
+ * @ingroup API
+ * @param int max The maximum number of chars to move.
+ * @param char *s The destination buffer address
+ */
+ int move(char *s, int max) {
+ return move(s, max, auto_detect_char);
+ }
+
+ #if 0 // Inhereted from Serial/Stream, for documentation only
+ /**
+ * Function: putc
+ *
+ * Write a character
+ * Inhereted from Serial/Stream
+ *
+ * @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.putc
+ * @ingroup API
+ * @param c The character to write to the serial port
+ */
+ int putc(int c);
+ #endif
+
+ #if 0 // Inhereted from Serial/Stream, for documentation only
+ /**
+ * Function: printf
+ *
+ * Write a formated string
+ * Inhereted from Serial/Stream
+ *
+ * @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.printf
+ * @ingroup API
+ * @param format A printf-style format string, followed by the variables to use in formating the string.
+ */
+ int printf(const char* format, ...);
+ #endif
+
+ #if 0 // Inhereted from Serial/Stream, for documentation only
+ /**
+ * Function: scanf
+ *
+ * Read a formated string
+ * Inhereted from Serial/Stream
+ *
+ * @see http://mbed.org/projects/libraries/api/mbed/trunk/Serial#Serial.scanf
+ * @ingroup API
+ * @param format - A scanf-style format string, followed by the pointers to variables to store the results.
+ */
+ int scanf(const char* format, ...);
+ #endif
+
+protected:
+
+ /**
+ * A pointer to the UART peripheral base address being used.
+ * @ingroup INTERNALS
+ */
+ void *_base;
+
+ /**
+ * The last byte to pass through the TX IRQ handler.
+ * @ingroup INTERNALS
+ */
+ volatile char txc;
+
+ /**
+ * The last byte to pass through the RX IRQ handler.
+ * @ingroup INTERNALS
+ */
+ volatile char rxc;
+
+ /**
+ * Pointers to the TX and RX buffers.
+ * @ingroup INTERNALS
+ */
+ volatile char *buffer[2];
+
+ /**
+ * Buffer in pointers.
+ * @ingroup INTERNALS
+ */
+ volatile int buffer_in[2];
+
+ /**
+ * Buffer out pointers.
+ * @ingroup INTERNALS
+ */
+ volatile int buffer_out[2];
+
+ /**
+ * Buffer lengths.
+ * @ingroup INTERNALS
+ */
+ volatile int buffer_size[2];
+
+ /**
+ * Buffer content counters.
+ * @ingroup INTERNALS
+ */
+ volatile int buffer_count[2];
+
+ /**
+ * Buffer overflow.
+ * @ingroup INTERNALS
+ */
+ volatile int buffer_overflow[2];
+
+ /**
+ * Auto-detect character.
+ * @ingroup INTERNALS
+ */
+ volatile char auto_detect_char;
+
+ /**
+ * Callback system.
+ * @ingroup INTERNALS
+ */
+ FunctionPointer _isr[NumOfIrqTypes];
+
+ /**
+ * TX Interrupt Service Routine.
+ * @ingroup INTERNALS
+ */
+ void isr_tx(bool doCallback);
+
+ /**
+ * TX Interrupt Service Routine stub version.
+ * @ingroup INTERNALS
+ */
+ void isr_tx(void) { isr_tx(true); }
+
+
+ /**
+ * RX Interrupt Service Routine.
+ * @ingroup INTERNALS
+ */
+ void isr_rx(void);
+
+ /**
+ * Disable the interrupts for this Uart.
+ * @ingroup INTERNALS
+ */
+ void disableIrq(void);
+
+ /**
+ * Enable the interrupts for this Uart.
+ * @ingroup INTERNALS
+ */
+ void enableIrq(void);
+
+ /**
+ * Get a character from the RX buffer
+ * @ingroup INTERNALS
+ * @param bool True to block (wait for input)
+ * @return A byte from the buffer.
+ */
+ int __getc(bool);
+
+ /**
+ * Put a character from the TX buffer
+ * @ingroup INTERNALS
+ * @param bool True to block (wait for space in the TX buffer if full)
+ * @return 0 on success
+ */
+ int __putc(int c, bool);
+
+ /**
+ * Function: _putc
+ * Overloaded virtual function.
+ */
+ virtual int _putc(int c) { return __putc(c, true); }
+
+ /**
+ * Function: _getc
+ * Overloaded virtual function.
+ */
+ virtual int _getc() { return __getc(true); }
+
+ /**
+ * Function: init
+ * Initialize the MODSERIAL object
+ * @ingroup INTERNALS
+ */
+ void init(int txSize, int rxSize);
+
+ /**
+ * Function: flushBuffer
+ * @ingroup INTERNALS
+ */
+ void flushBuffer(IrqType type);
+
+ /**
+ * Function: resizeBuffer
+ * @ingroup INTERNALS
+ */
+ int resizeBuffer(int size, IrqType type = RxIrq, bool memory_check = true);
+
+ /**
+ * Function: downSizeBuffer
+ * @ingroup INTERNALS
+ */
+ int downSizeBuffer(int size, IrqType type, bool memory_check);
+
+ /**
+ * Function: upSizeBuffer
+ * @ingroup INTERNALS
+ */
+ int upSizeBuffer(int size, IrqType type, bool memory_check);
+
+ /*
+ * If MODDMA is available the compile in code to handle sending
+ * an arbitary char buffer. Note, the parts before teh #ifdef
+ * are declared so that MODSERIAL can access then even if MODDMA
+ * isn't avaiable. Since MODDMA.h is only available at this point
+ * all DMA functionality must be declared inline in the class
+ * definition.
+ */
+public:
+
+ int dmaSendChannel;
+ void *moddma_p;
+
+#ifdef MODDMA_H
+
+ /**
+ * Set the "void pointer" moddma_p to be a pointer to a
+ * MODDMA controller class instance. Used to manage the
+ * data transfer of DMA configurations.
+ *
+ * @ingroup API
+ * @param p A pointer to "the" instance of MODDMA.
+ */
+ void MODDMA(MODDMA *p) { moddma_p = p; }
+
+ /**
+ * Send a char buffer to the Uarts TX system
+ * using DMA. This blocks regular library
+ * sending.
+ *
+ * @param buffer A char buffer of bytes to send.
+ * @param len The length of the buffer to send.
+ * @param dmaChannel The DMA channel to use, defaults to 7
+ * @return MODDMA::Status MODDMA::ok if all went ok
+ */
+ int dmaSend(char *buffer, int len, int dmaChannel = 7)
+ {
+ if (moddma_p == (void *)NULL) return -2;
+ class MODDMA *dma = (class MODDMA *)moddma_p;
+
+ dmaSendChannel = dmaChannel & 0x7;
+
+ uint32_t conn = MODDMA::UART0_Tx;
+ switch(_uidx) {
+ case 0: conn = MODDMA::UART0_Tx; break;
+ case 1: conn = MODDMA::UART1_Tx; break;
+ case 2: conn = MODDMA::UART2_Tx; break;
+ case 3: conn = MODDMA::UART3_Tx; break;
+ }
+
+ MODDMA_Config *config = new MODDMA_Config;
+ config
+ ->channelNum ( (MODDMA::CHANNELS)(dmaSendChannel & 0x7) )
+ ->srcMemAddr ( (uint32_t) buffer )
+ ->transferSize ( len )
+ ->transferType ( MODDMA::m2p )
+ ->dstConn ( conn )
+ ->attach_tc ( this, &MODSERIAL::dmaSendCallback )
+ ->attach_err ( this, &MODSERIAL::dmaSendCallback )
+ ; // config end
+
+ // Setup the configuration.
+ if (dma->Setup(config) != MODDMA::Ok) {
+ return -1;
+ }
+
+ //dma.Enable( MODDMA::Channel_0 );
+ dma->Enable( config->channelNum() );
+ return MODDMA::Ok;
+ }
+
+ /**
+ * Attach a callback to the DMA completion.
+ *
+ * @ingroup API
+ * @param fptr A function pointer to call
+ * @return this
+ */
+ void attach_dmaSendComplete(void (*fptr)(void)) {
+ _isrDmaSendComplete.attach(fptr);
+ }
+
+ /**
+ * Attach a callback to the DMA completion.
+ *
+ * @ingroup API
+ * @param tptr A template pointer to the calling object
+ * @param mptr A method pointer within the object to call.
+ * @return this
+ */
+ template<typename T>
+ void attach_dmaSendComplete(T* tptr, void (T::*mptr)(void)) {
+ if((mptr != NULL) && (tptr != NULL)) {
+ _isrDmaSendComplete.attach(tptr, mptr);
+ }
+ }
+
+ FunctionPointer _isrDmaSendComplete;
+
+protected:
+ /**
+ * Callback for dmaSend().
+ */
+ void dmaSendCallback(void)
+ {
+ if (moddma_p == (void *)NULL) return;
+ class MODDMA *dma = (class MODDMA *)moddma_p;
+
+ MODDMA_Config *config = dma->getConfig();
+ dma->haltAndWaitChannelComplete( (MODDMA::CHANNELS)config->channelNum());
+ dma->Disable( (MODDMA::CHANNELS)config->channelNum() );
+ if (dma->irqType() == MODDMA::TcIrq) dma->clearTcIrq();
+ if (dma->irqType() == MODDMA::ErrIrq) dma->clearErrIrq();
+ dmaSendChannel = -1;
+ _isrDmaSendComplete.call();
+ }
+
+#endif // MODDMA_H
+
+};
+
+}; // namespace AjK ends
+
+using namespace AjK;
+
+#endif
--- a/PUTC.cpp Wed Nov 24 00:33:40 2010 +0000
+++ b/PUTC.cpp Thu Jan 20 11:57:32 2011 +0000
@@ -42,6 +42,7 @@
}
else {
if (block) {
+ uint32_t ier = _IER; _IER = 1;
while ( MODSERIAL_TX_BUFFER_FULL ) { // Blocks!
// If putc() is called from an ISR then we are stuffed
// because in an ISR no bytes from the TX buffer will
@@ -55,6 +56,7 @@
// are blocking.
isr_tx(false);
}
+ _IER = ier;
}
else if( MODSERIAL_TX_BUFFER_FULL ) {
buffer_overflow[TxIrq] = c; // Oh dear, no room in buffer.
--- a/example.cpp Wed Nov 24 00:33:40 2010 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,120 +0,0 @@
-#ifdef COMPILE_EXAMPLE_CODE_MODSERIAL
-
-/*
- * To run this test program, link p9 to p10 so the Serial loops
- * back and receives characters it sends.
- */
-
-#include "mbed.h"
-#include "MODSERIAL.h"
-
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
-
-MODSERIAL pc(USBTX, USBRX);
-
-/*
- * As experiement, you can define MODSERIAL as show here and see what
- * effects it has on the LEDs.
- *
- * MODSERIAL uart(TX_PIN, RX_PIN, 512);
- * With this, the 512 characters sent can straight into the buffer
- * vary quickly. This means LED1 is only on briefly as the TX buffer
- * fills.
- *
- * MODSERIAL uart(TX_PIN, RX_PIN, 32);
- * With this, the buffer is smaller than the default 256 bytes and
- * therefore LED1 stays on much longer while the system waits for
- * room in the TX buffer.
- */
-MODSERIAL uart(TX_PIN, RX_PIN);
-
-// This function is called when a character goes from the TX buffer
-// to the Uart THR FIFO register.
-void txCallback(void) {
- led2 = !led2;
-}
-
-// This function is called when TX buffer goes empty
-void txEmpty(void) {
- led2 = 0;
- pc.puts(" Done. ");
-}
-
-// This function is called when a character goes into the RX buffer.
-void rxCallback(void) {
- led3 = !led3;
- pc.putc(uart.getc());
-}
-
-int main() {
- int c = 'A';
-
- // Ensure the baud rate for the PC "USB" serial is much
- // higher than "uart" baud rate below.
- pc.baud(PC_BAUD);
-
- // Use a deliberatly slow baud to fill up the TX buffer
- uart.baud(1200);
-
- uart.attach(&txCallback, MODSERIAL::TxIrq);
- uart.attach(&rxCallback, MODSERIAL::RxIrq);
- uart.attach(&txEmpty, MODSERIAL::TxEmpty);
-
- // Loop sending characters. We send 512
- // which is twice the default TX/RX buffer size.
-
- led1 = 1; // Show start of sending with LED1.
-
- for (int loop = 0; loop < 512; loop++) {
- uart.printf("%c", c);
- c++;
- if (c > 'Z') c = 'A';
- }
-
- led1 = 0; // Show the end of sending by switching off LED1.
-
- // End program. Flash LED4. Notice how LED 2 and 3 continue
- // to flash for a short period while the interrupt system
- // continues to send the characters left in the TX buffer.
-
- while(1) {
- led4 = !led4;
- wait(0.25);
- }
-}
-
-/*
- * Notes. Here is the sort of output you can expect on your PC/Mac/Linux host
- * machine that is connected to the "pc" USB serial port.
- *
- * ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV
- * WXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQR
- * STUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMN
- * OPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ
- * KLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF
- * GHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZAB
- * CDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ Done. R
- *
- * Of interest is that last "R" character after the system has said "Done."
- * This comes from the fact that the TxEmpty callback is made when the TX buffer
- * becomes empty. MODSERIAL makes use of the fact that the Uarts built into the
- * LPC17xx device use a 16 byte FIFO on both RX and TX channels. This means that
- * when the TxEmpty callback is made, the TX buffer is empty, but that just means
- * the "last few characters" were written to the TX FIFO. So although the TX
- * buffer has gone empty, the Uart's transmit system is still sending any remaining
- * characters from it's TX FIFO. If you want to be truely sure all the characters
- * you have sent have left the Mbed then call txIsBusy(); This function will
- * return true if characters are still being sent. If it returns false after
- * the Tx buffer is empty then all your characters have been sent.
- *
- * In a similar way, when characters are received into the RX FIFO, the entire
- * FIFO contents is moved to the RX buffer, assuming there is room left in the
- * RX buffer. If there is not, any remaining characters are left in the RX FIFO
- * and will be moved to the RX buffer on the next interrupt or when the running
- * program removes a character(s) from the RX buffer with the getc() method.
- */
-
-#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/example1.cpp Thu Jan 20 11:57:32 2011 +0000
@@ -0,0 +1,120 @@
+#ifdef COMPILE_EXAMPLE1_CODE_MODSERIAL
+
+/*
+ * To run this test program, link p9 to p10 so the Serial loops
+ * back and receives characters it sends.
+ */
+
+#include "mbed.h"
+#include "MODSERIAL.h"
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+MODSERIAL pc(USBTX, USBRX);
+
+/*
+ * As experiement, you can define MODSERIAL as show here and see what
+ * effects it has on the LEDs.
+ *
+ * MODSERIAL uart(TX_PIN, RX_PIN, 512);
+ * With this, the 512 characters sent can straight into the buffer
+ * vary quickly. This means LED1 is only on briefly as the TX buffer
+ * fills.
+ *
+ * MODSERIAL uart(TX_PIN, RX_PIN, 32);
+ * With this, the buffer is smaller than the default 256 bytes and
+ * therefore LED1 stays on much longer while the system waits for
+ * room in the TX buffer.
+ */
+MODSERIAL uart(TX_PIN, RX_PIN);
+
+// This function is called when a character goes from the TX buffer
+// to the Uart THR FIFO register.
+void txCallback(void) {
+ led2 = !led2;
+}
+
+// This function is called when TX buffer goes empty
+void txEmpty(void) {
+ led2 = 0;
+ pc.puts(" Done. ");
+}
+
+// This function is called when a character goes into the RX buffer.
+void rxCallback(void) {
+ led3 = !led3;
+ pc.putc(uart.getc());
+}
+
+int main() {
+ int c = 'A';
+
+ // Ensure the baud rate for the PC "USB" serial is much
+ // higher than "uart" baud rate below.
+ pc.baud(PC_BAUD);
+
+ // Use a deliberatly slow baud to fill up the TX buffer
+ uart.baud(1200);
+
+ uart.attach(&txCallback, MODSERIAL::TxIrq);
+ uart.attach(&rxCallback, MODSERIAL::RxIrq);
+ uart.attach(&txEmpty, MODSERIAL::TxEmpty);
+
+ // Loop sending characters. We send 512
+ // which is twice the default TX/RX buffer size.
+
+ led1 = 1; // Show start of sending with LED1.
+
+ for (int loop = 0; loop < 512; loop++) {
+ uart.printf("%c", c);
+ c++;
+ if (c > 'Z') c = 'A';
+ }
+
+ led1 = 0; // Show the end of sending by switching off LED1.
+
+ // End program. Flash LED4. Notice how LED 2 and 3 continue
+ // to flash for a short period while the interrupt system
+ // continues to send the characters left in the TX buffer.
+
+ while(1) {
+ led4 = !led4;
+ wait(0.25);
+ }
+}
+
+/*
+ * Notes. Here is the sort of output you can expect on your PC/Mac/Linux host
+ * machine that is connected to the "pc" USB serial port.
+ *
+ * ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV
+ * WXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQR
+ * STUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMN
+ * OPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ
+ * KLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF
+ * GHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZAB
+ * CDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ Done. R
+ *
+ * Of interest is that last "R" character after the system has said "Done."
+ * This comes from the fact that the TxEmpty callback is made when the TX buffer
+ * becomes empty. MODSERIAL makes use of the fact that the Uarts built into the
+ * LPC17xx device use a 16 byte FIFO on both RX and TX channels. This means that
+ * when the TxEmpty callback is made, the TX buffer is empty, but that just means
+ * the "last few characters" were written to the TX FIFO. So although the TX
+ * buffer has gone empty, the Uart's transmit system is still sending any remaining
+ * characters from it's TX FIFO. If you want to be truely sure all the characters
+ * you have sent have left the Mbed then call txIsBusy(); This function will
+ * return true if characters are still being sent. If it returns false after
+ * the Tx buffer is empty then all your characters have been sent.
+ *
+ * In a similar way, when characters are received into the RX FIFO, the entire
+ * FIFO contents is moved to the RX buffer, assuming there is room left in the
+ * RX buffer. If there is not, any remaining characters are left in the RX FIFO
+ * and will be moved to the RX buffer on the next interrupt or when the running
+ * program removes a character(s) from the RX buffer with the getc() method.
+ */
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/example2.cpp Thu Jan 20 11:57:32 2011 +0000
@@ -0,0 +1,148 @@
+/*
+ Copyright (c) 2011 Andy Kirkham
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+
+ @file example2.cpp
+ @purpose Demos a simple messaging system.
+ @version see ChangeLog.c
+ @date Jan 2011
+ @author Andy Kirkham
+*/
+
+/*
+ This example demostrates a simple "messaging" system. You can use it with
+ a terminal program to test it out or write a cusom C#/C++/VB/etc program
+ to read and write messages to or from the Mbed. The default buad rate in
+ this example is 115200.
+
+ In this example, the LEDs are controlled and pins p21 to p24 are set as
+ InterruptIn and send messages out when their value changes.
+
+ To use, hook up the MBed USB and open your fav terminal. All messages
+ end with the \n character, don't forget to hit carriage return!.
+ As an example:-
+
+ to switch on LED1 send LED1:1\n, off is LED1:0\n and toggle is LED1:2\n
+ to switch on LED2 send LED2:1\n, off is LED2:0\n and toggle is LED2:2\n
+ to switch on LED3 send LED3:1\n, off is LED3:0\n and toggle is LED3:2\n
+ to switch on LED4 send LED4:1\n, off is LED4:0\n and toggle is LED4:2\n
+
+ When a pin change on p21 to p24 happens, a message is sent. As an example
+ when p21 goes low PIN21:0\n is sent, when goes high PIN21:1\n is sent.
+
+ Note, the InterruptIn pins p21 to p24 are setup to have pullups. This means
+ they are high. To activate them use a wire to short the pin to 0volts.
+
+ If you find that p21 to p24 sent a lot of on/off/on/off then it's probably
+ due to "bounce". If you are connecting a mechanical switch to a pin you
+ may prefer to use the PinDetect library rather than using InterruptIn.
+ @see http://mbed.org/users/AjK/libraries/PinDetect/latest
+
+*/
+
+
+#ifdef COMPILE_EXAMPLE1_CODE_MODSERIAL
+
+#include "mbed.h"
+#include "MODSERIAL.h"
+
+#define MESSAGE_BUFFER_SIZE 32
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+InterruptIn P21(p21);
+InterruptIn P22(p22);
+InterruptIn P23(p23);
+InterruptIn P24(p24);
+
+MODSERIAL messageSystem(USBTX, USBRX);
+char messageBufferIncoming[MESSAGE_BUFFER_SIZE];
+char messageBufferOutgoing[MESSAGE_BUFFER_SIZE];
+bool messageReceived;
+
+uint32_t pinChanged, pinAction;
+
+void messageReceive(void) {
+ messageSystem.move(messageBufferIncoming, MESSAGE_BUFFER_SIZE);
+ messageReceived = true;
+}
+
+void messageProcess(void) {
+ if (!strncmp(messageBufferIncoming, "LED1:1", sizeof("LED1:1")-1)) led1 = 1;
+ else if (!strncmp(messageBufferIncoming, "LED1:0", sizeof("LED1:0")-1)) led1 = 0;
+ else if (!strncmp(messageBufferIncoming, "LED1:2", sizeof("LED1:2")-1)) led1 = !led1;
+
+ else if (!strncmp(messageBufferIncoming, "LED2:1", sizeof("LED2:1")-1)) led2 = 1;
+ else if (!strncmp(messageBufferIncoming, "LED2:0", sizeof("LED2:0")-1)) led2 = 0;
+ else if (!strncmp(messageBufferIncoming, "LED2:2", sizeof("LED2:2")-1)) led2 = !led2;
+
+ else if (!strncmp(messageBufferIncoming, "LED3:1", sizeof("LED3:1")-1)) led3 = 1;
+ else if (!strncmp(messageBufferIncoming, "LED3:0", sizeof("LED3:0")-1)) led3 = 0;
+ else if (!strncmp(messageBufferIncoming, "LED3:2", sizeof("LED3:2")-1)) led3 = !led3;
+
+ else if (!strncmp(messageBufferIncoming, "LED4:1", sizeof("LED4:1")-1)) led4 = 1;
+ else if (!strncmp(messageBufferIncoming, "LED4:0", sizeof("LED4:0")-1)) led4 = 0;
+ else if (!strncmp(messageBufferIncoming, "LED4:2", sizeof("LED4:2")-1)) led4 = !led4;
+
+ messageReceived = false;
+}
+
+#define PIN_MESSAGE_SEND(x,y) sprintf(messageBufferOutgoing,"PIN%02d:%d\n",x,y);messageSystem.puts(messageBufferOutgoing);
+
+void pin21Rise(void) { PIN_MESSAGE_SEND(21, 1); }
+void pin21Fall(void) { PIN_MESSAGE_SEND(21, 0); }
+void pin22Rise(void) { PIN_MESSAGE_SEND(22, 1); }
+void pin22Fall(void) { PIN_MESSAGE_SEND(22, 0); }
+void pin23Rise(void) { PIN_MESSAGE_SEND(23, 1); }
+void pin23Fall(void) { PIN_MESSAGE_SEND(23, 0); }
+void pin24Rise(void) { PIN_MESSAGE_SEND(24, 1); }
+void pin24Fall(void) { PIN_MESSAGE_SEND(24, 0); }
+
+int main() {
+
+ messageReceived = false;
+ messageSystem.baud(115200);
+ messageSystem.attach(&messageReceive, MODSERIAL::RxAutoDetect);
+ messageSystem.autoDectectChar('\n');
+
+ pinChanged = pinAction = 0;
+
+ // Enable pullup resistors on pins.
+ P21.mode(PullUp); P22.mode(PullUp); P23.mode(PullUp); P24.mode(PullUp);
+
+ // Fix Mbed library bug, see http://mbed.org/forum/bugs-suggestions/topic/1498
+ LPC_GPIOINT->IO2IntClr = (1UL << 5) | (1UL << 4) | (1UL << 3) | (1UL << 2);
+
+ // Attach callbacks.
+ P21.rise(&pin21Rise); P21.fall(&pin21Fall);
+ P22.rise(&pin22Rise); P22.fall(&pin22Fall);
+ P23.rise(&pin23Rise); P23.fall(&pin23Fall);
+ P24.rise(&pin24Rise); P24.fall(&pin24Fall);
+
+ while(1) {
+ // Process incoming messages.
+ if (messageReceived) messageProcess();
+ }
+}
+
+#endif
--- a/example_dma.cpp Wed Nov 24 00:33:40 2010 +0000
+++ b/example_dma.cpp Thu Jan 20 11:57:32 2011 +0000
@@ -1,144 +1,144 @@
-#ifdef COMPILE_EXAMPLE_CODE_MODSERIAL_MODDMA
-
-/*
- * To run this test program, link p9 to p10 so the Serial loops
- * back and receives characters it sends.
- */
-
-#include "mbed.h"
-
-/* Note, this example requires that you also import into the Mbed
- compiler the MODDMA project as well as MODSERIAL
- http://mbed.org/users/AjK/libraries/MODDMA/latest
- MODDMA.h MUST come before MODSERIAL.h */
-#include "MODDMA.h" // <--- Declare first
-#include "MODSERIAL.h" // Flollowed by MODSERIAL
-
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
-
-MODSERIAL pc(USBTX, USBRX);
-
-/*
- * As experiement, you can define MODSERIAL as show here and see what
- * effects it has on the LEDs.
- *
- * MODSERIAL uart(TX_PIN, RX_PIN, 512);
- * With this, the 512 characters sent can straight into the buffer
- * vary quickly. This means LED1 is only on briefly as the TX buffer
- * fills.
- *
- * MODSERIAL uart(TX_PIN, RX_PIN, 32);
- * With this, the buffer is smaller than the default 256 bytes and
- * therefore LED1 stays on much longer while the system waits for
- * room in the TX buffer.
- */
-MODSERIAL uart(TX_PIN, RX_PIN);
-
-MODDMA dma;
-
-// This function is called when a character goes from the TX buffer
-// to the Uart THR FIFO register.
-void txCallback(void) {
- led2 = !led2;
-}
-
-// This function is called when TX buffer goes empty
-void txEmpty(void) {
- led2 = 0;
- pc.puts(" Done. ");
-}
-
-void dmaComplete(void) {
- led1 = 1;
-}
-
-// This function is called when a character goes into the RX buffer.
-void rxCallback(void) {
- led3 = !led3;
- pc.putc(uart.getc());
-}
-
-int main() {
- char s1[] = " *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* ";
- int c = 'A';
-
- // Tell MODSERIAL where the MODDMA controller is.
- pc.MODDMA( &dma );
-
- // Ensure the baud rate for the PC "USB" serial is much
- // higher than "uart" baud rate below.
- pc.baud( PC_BAUD );
-
- // Use a deliberatly slow baud to fill up the TX buffer
- uart.baud(1200);
-
- uart.attach( &txCallback, MODSERIAL::TxIrq );
- uart.attach( &rxCallback, MODSERIAL::RxIrq );
- uart.attach( &txEmpty, MODSERIAL::TxEmpty );
-
- // Loop sending characters. We send 512
- // which is twice the default TX/RX buffer size.
-
- led1 = 0;
-
- // Send the buffer s using DMA channel 7
- pc.attach_dmaSendComplete( &dmaComplete );
- pc.dmaSend( s1, sizeof(s1), MODDMA::Channel_7 );
-
- for (int loop = 0; loop < 512; loop++) {
- uart.printf("%c", c);
- c++;
- if (c > 'Z') c = 'A';
- }
-
- led1 = 0; // Show the end of sending by switching off LED1.
-
- // End program. Flash LED4. Notice how LED 2 and 3 continue
- // to flash for a short period while the interrupt system
- // continues to send the characters left in the TX buffer.
-
- while(1) {
- led4 = !led4;
- wait(0.25);
- }
-}
-
-/*
- * Notes. Here is the sort of output you can expect on your PC/Mac/Linux host
- * machine that is connected to the "pc" USB serial port.
- *
- * *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* ABCDEFGHIJKLMNOPQRSTUVWXYZABCDE
- * FGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZA
- * BCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVW
- * XYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRS
- * TUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNO
- * PQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJK
- * LMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFG
- * HIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ Done. R
- *
- * Note how the DMA blocks the TX buffer sending under standard interrupt control.
- * Not until the DMA transfer is complete will "normal" buffered TX sending resume.
- *
- * Of interest is that last "R" character after the system has said "Done."
- * This comes from the fact that the TxEmpty callback is made when the TX buffer
- * becomes empty. MODSERIAL makes use of the fact that the Uarts built into the
- * LPC17xx device use a 16 byte FIFO on both RX and TX channels. This means that
- * when the TxEmpty callback is made, the TX buffer is empty, but that just means
- * the "last few characters" were written to the TX FIFO. So although the TX
- * buffer has gone empty, the Uart's transmit system is still sending any remaining
- * characters from it's TX FIFO. If you want to be truely sure all the characters
- * you have sent have left the Mbed then call txIsBusy(); This function will
- * return true if characters are still being sent. If it returns false after
- * the Tx buffer is empty then all your characters have been sent.
- *
- * In a similar way, when characters are received into the RX FIFO, the entire
- * FIFO contents is moved to the RX buffer, assuming there is room left in the
- * RX buffer. If there is not, any remaining characters are left in the RX FIFO
- * and will be moved to the RX buffer on the next interrupt or when the running
- * program removes a character(s) from the RX buffer with the getc() method.
- */
-
-#endif
+#ifdef COMPILE_EXAMPLE_CODE_MODSERIAL_MODDMA
+
+/*
+ * To run this test program, link p9 to p10 so the Serial loops
+ * back and receives characters it sends.
+ */
+
+#include "mbed.h"
+
+/* Note, this example requires that you also import into the Mbed
+ compiler the MODDMA project as well as MODSERIAL
+ http://mbed.org/users/AjK/libraries/MODDMA/latest
+ MODDMA.h MUST come before MODSERIAL.h */
+#include "MODDMA.h" // <--- Declare first
+#include "MODSERIAL.h" // Flollowed by MODSERIAL
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+MODSERIAL pc(USBTX, USBRX);
+
+/*
+ * As experiement, you can define MODSERIAL as show here and see what
+ * effects it has on the LEDs.
+ *
+ * MODSERIAL uart(TX_PIN, RX_PIN, 512);
+ * With this, the 512 characters sent can straight into the buffer
+ * vary quickly. This means LED1 is only on briefly as the TX buffer
+ * fills.
+ *
+ * MODSERIAL uart(TX_PIN, RX_PIN, 32);
+ * With this, the buffer is smaller than the default 256 bytes and
+ * therefore LED1 stays on much longer while the system waits for
+ * room in the TX buffer.
+ */
+MODSERIAL uart(TX_PIN, RX_PIN);
+
+MODDMA dma;
+
+// This function is called when a character goes from the TX buffer
+// to the Uart THR FIFO register.
+void txCallback(void) {
+ led2 = !led2;
+}
+
+// This function is called when TX buffer goes empty
+void txEmpty(void) {
+ led2 = 0;
+ pc.puts(" Done. ");
+}
+
+void dmaComplete(void) {
+ led1 = 1;
+}
+
+// This function is called when a character goes into the RX buffer.
+void rxCallback(void) {
+ led3 = !led3;
+ pc.putc(uart.getc());
+}
+
+int main() {
+ char s1[] = " *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* ";
+ int c = 'A';
+
+ // Tell MODSERIAL where the MODDMA controller is.
+ pc.MODDMA( &dma );
+
+ // Ensure the baud rate for the PC "USB" serial is much
+ // higher than "uart" baud rate below.
+ pc.baud( PC_BAUD );
+
+ // Use a deliberatly slow baud to fill up the TX buffer
+ uart.baud(1200);
+
+ uart.attach( &txCallback, MODSERIAL::TxIrq );
+ uart.attach( &rxCallback, MODSERIAL::RxIrq );
+ uart.attach( &txEmpty, MODSERIAL::TxEmpty );
+
+ // Loop sending characters. We send 512
+ // which is twice the default TX/RX buffer size.
+
+ led1 = 0;
+
+ // Send the buffer s using DMA channel 7
+ pc.attach_dmaSendComplete( &dmaComplete );
+ pc.dmaSend( s1, sizeof(s1), MODDMA::Channel_7 );
+
+ for (int loop = 0; loop < 512; loop++) {
+ uart.printf("%c", c);
+ c++;
+ if (c > 'Z') c = 'A';
+ }
+
+ led1 = 0; // Show the end of sending by switching off LED1.
+
+ // End program. Flash LED4. Notice how LED 2 and 3 continue
+ // to flash for a short period while the interrupt system
+ // continues to send the characters left in the TX buffer.
+
+ while(1) {
+ led4 = !led4;
+ wait(0.25);
+ }
+}
+
+/*
+ * Notes. Here is the sort of output you can expect on your PC/Mac/Linux host
+ * machine that is connected to the "pc" USB serial port.
+ *
+ * *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* ABCDEFGHIJKLMNOPQRSTUVWXYZABCDE
+ * FGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZA
+ * BCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVW
+ * XYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRS
+ * TUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNO
+ * PQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJK
+ * LMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFG
+ * HIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ Done. R
+ *
+ * Note how the DMA blocks the TX buffer sending under standard interrupt control.
+ * Not until the DMA transfer is complete will "normal" buffered TX sending resume.
+ *
+ * Of interest is that last "R" character after the system has said "Done."
+ * This comes from the fact that the TxEmpty callback is made when the TX buffer
+ * becomes empty. MODSERIAL makes use of the fact that the Uarts built into the
+ * LPC17xx device use a 16 byte FIFO on both RX and TX channels. This means that
+ * when the TxEmpty callback is made, the TX buffer is empty, but that just means
+ * the "last few characters" were written to the TX FIFO. So although the TX
+ * buffer has gone empty, the Uart's transmit system is still sending any remaining
+ * characters from it's TX FIFO. If you want to be truely sure all the characters
+ * you have sent have left the Mbed then call txIsBusy(); This function will
+ * return true if characters are still being sent. If it returns false after
+ * the Tx buffer is empty then all your characters have been sent.
+ *
+ * In a similar way, when characters are received into the RX FIFO, the entire
+ * FIFO contents is moved to the RX buffer, assuming there is room left in the
+ * RX buffer. If there is not, any remaining characters are left in the RX FIFO
+ * and will be moved to the RX buffer on the next interrupt or when the running
+ * program removes a character(s) from the RX buffer with the getc() method.
+ */
+
+#endif
