diff --git a/drivers/Serial.h b/drivers/Serial.h index d167e69580..e48397d356 100644 --- a/drivers/Serial.h +++ b/drivers/Serial.h @@ -73,6 +73,16 @@ public: */ Serial(PinName tx, PinName rx, const char *name = NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); + /** Create a Serial port, connected to the specified transmit and receive pins + * + * @param explicit_pinmap reference to strucure which holds static pinmap. + * @param name The name of the stream associated with this serial port (optional) + * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE or 9600) + * + * @note + * Either tx or rx may be specified as NC (Not Connected) if unused + */ + Serial(const serial_pinmap_t &explicit_pinmap, const char *name = NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); /** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud * @@ -85,6 +95,16 @@ public: */ Serial(PinName tx, PinName rx, int baud); + /** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud + * + * @param explicit_pinmap reference to strucure which holds static pinmap. + * @param baud The baud rate of the serial port + * + * @note + * Either tx or rx may be specified as NC (Not Connected) if unused + */ + Serial(const serial_pinmap_t &explicit_pinmap, int baud); + /* Stream gives us a FileHandle with non-functional poll()/readable()/writable. Pass through * the calls from the SerialBase instead for backwards compatibility. This problem is * part of why Stream and Serial should be deprecated. diff --git a/drivers/SerialBase.h b/drivers/SerialBase.h index 9dc31512f1..5963d53cae 100644 --- a/drivers/SerialBase.h +++ b/drivers/SerialBase.h @@ -200,6 +200,13 @@ public: * @param flow2 the second flow control pin (CTS for RTSCTS) */ void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC); + + /** Set the flow control type on the serial port + * + * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) + * @param pinmap reference to strucure which holds static pinmap + */ + void set_flow_control(Flow type, const serial_fc_pinmap_t &explicit_pinmap); #endif static void _irq_handler(uint32_t id, SerialIrq irq_type); @@ -313,6 +320,7 @@ protected: #if !defined(DOXYGEN_ONLY) protected: SerialBase(PinName tx, PinName rx, int baud); + SerialBase(const serial_pinmap_t &explicit_pinmap, int baud); virtual ~SerialBase(); int _base_getc(); diff --git a/drivers/UARTSerial.h b/drivers/UARTSerial.h index e23a4a65b0..506e11a9ca 100644 --- a/drivers/UARTSerial.h +++ b/drivers/UARTSerial.h @@ -58,6 +58,13 @@ public: * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) */ UARTSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); + + /** Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate. + * @param explicit_pinmap reference to strucure which holds static pinmap + * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) + */ + UARTSerial(const serial_pinmap_t &explicit_pinmap, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); + virtual ~UARTSerial(); /** Equivalent to POSIX poll(). Derived from FileHandle. diff --git a/drivers/source/Serial.cpp b/drivers/source/Serial.cpp index 03ee49697a..fa95a68810 100644 --- a/drivers/source/Serial.cpp +++ b/drivers/source/Serial.cpp @@ -24,10 +24,18 @@ Serial::Serial(PinName tx, PinName rx, const char *name, int baud) : SerialBase( { } +Serial::Serial(const serial_pinmap_t &explicit_pinmap, const char *name, int baud) : SerialBase(explicit_pinmap, baud), Stream(name) +{ +} + Serial::Serial(PinName tx, PinName rx, int baud): SerialBase(tx, rx, baud), Stream(NULL) { } +Serial::Serial(const serial_pinmap_t &explicit_pinmap, int baud): SerialBase(explicit_pinmap, baud), Stream(NULL) +{ +} + int Serial::_getc() { // Mutex is already held diff --git a/drivers/source/SerialBase.cpp b/drivers/source/SerialBase.cpp index cdc8636bbf..60c6055209 100644 --- a/drivers/source/SerialBase.cpp +++ b/drivers/source/SerialBase.cpp @@ -40,6 +40,29 @@ SerialBase::SerialBase(PinName tx, PinName rx, int baud) : _init(); } +SerialBase::SerialBase(const serial_pinmap_t &explicit_pinmap, int baud) : +#if DEVICE_SERIAL_ASYNCH + _thunk_irq(this), _tx_usage(DMA_USAGE_NEVER), + _rx_usage(DMA_USAGE_NEVER), _tx_callback(NULL), + _rx_callback(NULL), _tx_asynch_set(false), + _rx_asynch_set(false), +#endif + _serial(), + _baud(baud), + _tx_pin(explicit_pinmap.tx_pin), + _rx_pin(explicit_pinmap.rx_pin) +{ + // No lock needed in the constructor + + for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { + _irq[i] = NULL; + } + + serial_init_direct(&_serial, &explicit_pinmap); + serial_baud(&_serial, _baud); + serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this); +} + void SerialBase::baud(int baudrate) { lock(); @@ -292,6 +315,14 @@ void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) } unlock(); } + +void SerialBase::set_flow_control(Flow type, const serial_fc_pinmap_t &explicit_pinmap) +{ + lock(); + FlowControl flow_type = (FlowControl)type; + serial_set_flow_control_direct(&_serial, flow_type, &explicit_pinmap); + unlock(); +} #endif #if DEVICE_SERIAL_ASYNCH diff --git a/drivers/source/UARTSerial.cpp b/drivers/source/UARTSerial.cpp index d07e4e0d27..88bb816ca4 100644 --- a/drivers/source/UARTSerial.cpp +++ b/drivers/source/UARTSerial.cpp @@ -36,6 +36,19 @@ UARTSerial::UARTSerial(PinName tx, PinName rx, int baud) : enable_rx_irq(); } +UARTSerial::UARTSerial(const serial_pinmap_t &explicit_pinmap, int baud) : + SerialBase(explicit_pinmap, baud), + _blocking(true), + _tx_irq_enabled(false), + _rx_irq_enabled(false), + _tx_enabled(true), + _rx_enabled(true), + _dcd_irq(NULL) +{ + /* Attatch IRQ routines to the serial device. */ + enable_rx_irq(); +} + UARTSerial::~UARTSerial() { delete _dcd_irq;