joey shelton / LED_Demo2

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Embed: (wiki syntax)

« Back to documentation index

SerialHalfDuplex Class Reference

SerialHalfDuplex Class Reference

A serial port (UART) for communication with other devices using Half-Duplex, allowing transmit and receive on a single shared transmit and receive line. More...

#include <SerialHalfDuplex.h>

Inherits mbed::Serial.

Public Member Functions

 SerialHalfDuplex (PinName tx, PinName rx)
 Create a half-duplex serial port, connected to the specified transmit and receive pins.
void baud (int baudrate)
 Set the baud rate of the serial port.
void format (int bits=8, Parity parity=SerialBase::None, int stop_bits=1)
 Set the transmission format used by the serial port.
int readable ()
 Determine if there is a character available to read.
int writeable ()
 Determine if there is space available to write a character.
void attach (Callback< void()> func, IrqType type=RxIrq)
 Attach a function to call whenever a serial interrupt is generated.
template<typename T >
 MBED_DEPRECATED_SINCE ("mbed-os-5.1","The attach function does not support cv-qualifiers. Replaced by ""attach(callback(obj, method), type).") void attach(T *obj
 Attach a member function to call whenever a serial interrupt is generated.
void set_flow_control (Flow type, PinName flow1=NC, PinName flow2=NC)
 Set the flow control type on the serial port.
int write (const uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE)
 Begin asynchronous write using 8bit buffer.
int write (const uint16_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE)
 Begin asynchronous write using 16bit buffer.
void abort_write ()
 Abort the on-going write transfer.
int read (uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_RX_COMPLETE, unsigned char char_match=SERIAL_RESERVED_CHAR_MATCH)
 Begin asynchronous reading using 8bit buffer.
int read (uint16_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_RX_COMPLETE, unsigned char char_match=SERIAL_RESERVED_CHAR_MATCH)
 Begin asynchronous reading using 16bit buffer.
void abort_read ()
 Abort the on-going read transfer.
int set_dma_usage_tx (DMAUsage usage)
 Configure DMA usage suggestion for non-blocking TX transfers.
int set_dma_usage_rx (DMAUsage usage)
 Configure DMA usage suggestion for non-blocking RX transfers.

Protected Member Functions

virtual void lock ()
 Acquire exclusive access to this serial port.
virtual void unlock ()
 Release exclusive access to this serial port.
virtual ssize_t write (const void *buffer, size_t length)
 Write the contents of a buffer to the file.
virtual ssize_t read (void *buffer, size_t length)
 Function read Reads the contents of the file into a buffer.
virtual int close ()
 Close the file.
virtual off_t lseek (off_t offset, int whence)
 Move the file position to a given offset from a given location.
virtual int isatty ()
 Check if the handle is for a interactive terminal device.
virtual int fsync ()
 Flush any buffers associated with the FileHandle, ensuring it is up to date on disk.

Detailed Description

A serial port (UART) for communication with other devices using Half-Duplex, allowing transmit and receive on a single shared transmit and receive line.

Only one end should be transmitting at a time.

Both the tx and rx pin should be defined, and wired together. This is in addition to them being wired to the other serial device to allow both read and write functions to operate.

For Simplex and Full-Duplex Serial communication, see Serial()

Example:

 // Send a byte to a second HalfDuplex device, and read the response

 #include "mbed.h"

 // p9 and p10 should be wired together to form "a"
 // p28 and p27 should be wired together to form "b"
 // p9/p10 should be wired to p28/p27 as the Half Duplex connection

 SerialHalfDuplex a(p9, p10);
 SerialHalfDuplex b(p28, p27);

 void b_rx() { // second device response
     b.putc(b.getc() + 4);
 }

 int main() {
     b.attach(&b_rx);
     for (int c = 'A'; c < 'Z'; c++) {
         a.putc(c);
         printf("sent [%c]\n", c);
         wait(0.5);   // b should respond
         if (a.readable()) {
             printf("received [%c]\n", a.getc());
         }
     }
 }

Definition at line 57 of file SerialHalfDuplex.h.


Constructor & Destructor Documentation

SerialHalfDuplex ( PinName  tx,
PinName  rx 
)

Create a half-duplex serial port, connected to the specified transmit and receive pins.

These pins should be wired together, as well as to the target device

Parameters:
txTransmit pin
rxReceive pin

Definition at line 14 of file SerialHalfDuplex.cpp.


Member Function Documentation

void abort_read ( void   ) [inherited]

Abort the on-going read transfer.

Definition at line 183 of file SerialBase.cpp.

void abort_write ( void   ) [inherited]

Abort the on-going write transfer.

Definition at line 178 of file SerialBase.cpp.

void attach ( Callback< void()>  func,
IrqType  type = RxIrq 
) [inherited]

Attach a function to call whenever a serial interrupt is generated.

Parameters:
funcA pointer to a void function, or 0 to set as none
typeWhich serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)

Definition at line 70 of file SerialBase.cpp.

void baud ( int  baudrate ) [inherited]

Set the baud rate of the serial port.

Parameters:
baudrateThe baudrate of the serial port (default = 9600).

Definition at line 42 of file SerialBase.cpp.

int close (  ) [protected, virtual, inherited]

Close the file.

Returns:
Zero on success, -1 on error.

Implements FileHandle.

Definition at line 63 of file Stream.cpp.

void format ( int  bits = 8,
Parity  parity = SerialBase::None,
int  stop_bits = 1 
) [inherited]

Set the transmission format used by the serial port.

Parameters:
bitsThe number of bits in a word (5-8; default = 8)
parityThe parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
stopThe number of stop bits (1 or 2; default = 1)

Definition at line 49 of file SerialBase.cpp.

int fsync (  ) [protected, virtual, inherited]

Flush any buffers associated with the FileHandle, ensuring it is up to date on disk.

Returns:
0 on success or un-needed, -1 on error

Implements FileHandle.

Definition at line 105 of file Stream.cpp.

int isatty (  ) [protected, virtual, inherited]

Check if the handle is for a interactive terminal device.

If so, line buffered behaviour is used by default

Returns:
1 if it is a terminal, 0 otherwise

Implements FileHandle.

Definition at line 101 of file Stream.cpp.

void lock ( void   ) [protected, virtual, inherited]

Acquire exclusive access to this serial port.

Reimplemented from SerialBase.

Definition at line 36 of file Serial.cpp.

off_t lseek ( off_t  offset,
int  whence 
) [protected, virtual, inherited]

Move the file position to a given offset from a given location.

Parameters:
offsetThe offset from whence to move to
whenceSEEK_SET for the start of the file, SEEK_CUR for the current file position, or SEEK_END for the end of the file.
Returns:
new file position on success, -1 on failure or unsupported

Implements FileHandle.

Definition at line 97 of file Stream.cpp.

MBED_DEPRECATED_SINCE ( "mbed-os-5.1"  ,
"The attach function does not support cv-qualifiers. Replaced by ""attach(callback(obj, method), type)."   
) [inherited]

Attach a member function to call whenever a serial interrupt is generated.

Parameters:
objpointer to the object to call the member function on
methodpointer to the member function to be called
typeWhich serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
int read ( uint8_t *  buffer,
int  length,
const event_callback_t &  callback,
int  event = SERIAL_EVENT_RX_COMPLETE,
unsigned char  char_match = SERIAL_RESERVED_CHAR_MATCH 
) [inherited]

Begin asynchronous reading using 8bit buffer.

The completition invokes registred RX event callback.

Parameters:
bufferThe buffer where received data will be stored
lengthThe buffer length in bytes
callbackThe event callback function
eventThe logical OR of RX events
char_matchThe matching character

Definition at line 206 of file SerialBase.cpp.

ssize_t read ( void *  buffer,
size_t  length 
) [protected, virtual, inherited]

Function read Reads the contents of the file into a buffer.

Parameters:
bufferthe buffer to read in to
lengththe number of characters to read
Returns:
The number of characters read (zero at end of file) on success, -1 on error.

Implements FileHandle.

Definition at line 82 of file Stream.cpp.

int read ( uint16_t *  buffer,
int  length,
const event_callback_t &  callback,
int  event = SERIAL_EVENT_RX_COMPLETE,
unsigned char  char_match = SERIAL_RESERVED_CHAR_MATCH 
) [inherited]

Begin asynchronous reading using 16bit buffer.

The completition invokes registred RX event callback.

Parameters:
bufferThe buffer where received data will be stored
lengthThe buffer length in bytes
callbackThe event callback function
eventThe logical OR of RX events
char_matchThe matching character

Definition at line 216 of file SerialBase.cpp.

int readable (  ) [inherited]

Determine if there is a character available to read.

Returns:
1 if there is a character available to read, 0 otherwise

Definition at line 55 of file SerialBase.cpp.

int set_dma_usage_rx ( DMAUsage  usage ) [inherited]

Configure DMA usage suggestion for non-blocking RX transfers.

Parameters:
usageThe usage DMA hint for peripheral
Returns:
Zero if the usage was set, -1 if a transaction is on-going

Definition at line 197 of file SerialBase.cpp.

int set_dma_usage_tx ( DMAUsage  usage ) [inherited]

Configure DMA usage suggestion for non-blocking TX transfers.

Parameters:
usageThe usage DMA hint for peripheral
Returns:
Zero if the usage was set, -1 if a transaction is on-going

Definition at line 188 of file SerialBase.cpp.

void set_flow_control ( Flow  type,
PinName  flow1 = NC,
PinName  flow2 = NC 
) [inherited]

Set the flow control type on the serial port.

Parameters:
typethe flow control type (Disabled, RTS, CTS, RTSCTS)
flow1the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
flow2the second flow control pin (CTS for RTSCTS)

Definition at line 126 of file SerialBase.cpp.

void unlock ( void   ) [protected, virtual, inherited]

Release exclusive access to this serial port.

Reimplemented from SerialBase.

Definition at line 40 of file Serial.cpp.

int write ( const uint8_t *  buffer,
int  length,
const event_callback_t &  callback,
int  event = SERIAL_EVENT_TX_COMPLETE 
) [inherited]

Begin asynchronous write using 8bit buffer.

The completition invokes registered TX event callback

Parameters:
bufferThe buffer where received data will be stored
lengthThe buffer length in bytes
callbackThe event callback function
eventThe logical OR of TX events

Definition at line 152 of file SerialBase.cpp.

int write ( const uint16_t *  buffer,
int  length,
const event_callback_t &  callback,
int  event = SERIAL_EVENT_TX_COMPLETE 
) [inherited]

Begin asynchronous write using 16bit buffer.

The completition invokes registered TX event callback

Parameters:
bufferThe buffer where received data will be stored
lengthThe buffer length in bytes
callbackThe event callback function
eventThe logical OR of TX events

Definition at line 161 of file SerialBase.cpp.

ssize_t write ( const void *  buffer,
size_t  length 
) [protected, virtual, inherited]

Write the contents of a buffer to the file.

Parameters:
bufferthe buffer to write from
lengththe number of characters to write
Returns:
The number of characters written (possibly 0) on success, -1 on error.

Implements FileHandle.

Definition at line 67 of file Stream.cpp.

int writeable (  ) [inherited]

Determine if there is space available to write a character.

Returns:
1 if there is space to write a character, 0 otherwise

Definition at line 63 of file SerialBase.cpp.