COSA
An Object-Oriented Platform for Arduino Programming
RS485 Class Reference

#include <RS485.hh>

Inheritance diagram for RS485:
Inheritance graph
Collaboration diagram for RS485:
Collaboration graph

Classes

struct  header_t
 

Public Types

enum  {
  DATA5 = 0, DATA6 = _BV(UCSZ00), DATA7 = _BV(UCSZ01), DATA8 = _BV(UCSZ01) | _BV(UCSZ00),
  NO_PARITY = 0, EVEN_PARITY = _BV(UPM01), ODD_PARITY = _BV(UPM01) | _BV(UPM00), STOP1 = 0,
  STOP2 = _BV(USBS0)
}
 

Public Member Functions

 RS485 (uint8_t port, IOStream::Device *ibuf, IOStream::Device *obuf, uint16_t size, Board::DigitalPin de, uint8_t addr=MASTER)
 
virtual int putchar (char c)
 
void address (uint8_t addr=MASTER)
 
int send (const void *buf, size_t len, uint8_t dest=MASTER)
 
int broadcast (const void *buf, size_t len)
 
int recv (void *buf, size_t len, uint32_t ms=0L)
 
virtual int available ()
 
virtual int room ()
 
virtual int peekchar ()
 
virtual int peekchar (char c)
 
virtual int getchar ()
 
virtual int flush ()
 
virtual void empty ()
 
virtual bool begin (uint32_t baudrate=DEFAULT_BAUDRATE, uint8_t format=DEFAULT_FORMAT)
 
virtual bool end ()
 
virtual void powerup ()
 
virtual void powerdown ()
 
void non_blocking ()
 
void blocking ()
 
bool is_blocking () const
 
void eol (Mode mode)
 
Mode eol () const
 
virtual int puts (const char *s)
 
virtual int puts (str_P s)
 
virtual int write (const void *buf, size_t size)
 
virtual int write (const iovec_t *vec)
 
virtual int write_P (const void *buf, size_t size)
 
virtual char * gets (char *s, size_t count)
 
virtual int read (void *buf, size_t size)
 
virtual int read (iovec_t *vec)
 

Static Public Attributes

static const uint8_t SOT = 0x01
 
static const uint8_t FRAME_MAX = sizeof(header_t) + sizeof(uint16_t) + 1
 
static const uint16_t BUFFER_MAX = COSA_RS485_BUFFER_MAX
 
static const uint8_t BROADCAST = 0
 
static const uint8_t MASTER = 255
 
static const uint16_t RX_BUFFER_MAX = COSA_UART_RX_BUFFER_MAX
 
static const uint16_t TX_BUFFER_MAX = COSA_UART_TX_BUFFER_MAX
 
static const uint32_t DEFAULT_BAUDRATE = 9600L
 
static const uint8_t DEFAULT_FORMAT = DATA8 + NO_PARITY + STOP2
 

Protected Member Functions

virtual void on_udre_interrupt ()
 
virtual void on_tx_interrupt ()
 
virtual void on_transmit_completed ()
 
volatile uint8_t * UCSRnA () const
 
volatile uint8_t * UCSRnB () const
 
volatile uint8_t * UCSRnC () const
 
volatile uint16_t * UBRRn () const
 
volatile uint8_t * UDRn () const
 
virtual void on_rx_interrupt ()
 

Protected Attributes

const uint16_t PAYLOAD_MAX
 
header_t m_header
 
OutputPin m_de
 
uint8_t m_addr
 
uint8_t m_state
 
uint8_t m_port
 UART port index. More...
 
volatile uint8_t *const m_sfr
 Special Function Register Pointer. More...
 
IOStream::Devicem_ibuf
 Input Buffer/Device. More...
 
IOStream::Devicem_obuf
 Output Buffer/Device. More...
 
bool m_idle
 Flag idle mode. More...
 
bool m_blocking
 
Mode m_eol
 

Static Protected Attributes

static UARTuart [Board::UART_MAX] = { NULL }
 

Detailed Description

RS485 link handler; Master-Slave protocol. Master always initiates communication with request message to Slave who responds. The Master may also broadcast messages to all slave devices. The message format is; <header,payload,crc> where header contains the length of the payload, the destination and source device address or the broadcast address(0x00), the source device address and a 8-bit CRC7 check-sum for the header. The payload is the message data and a 16-bit payload CCITT/XMODEM check-sum.

Definition at line 44 of file RS485.hh.

Member Enumeration Documentation

anonymous enum
inherited

Serial formats; DATA + PARITY + STOP.

Enumerator
DATA5 
DATA6 
DATA7 
DATA8 
NO_PARITY 
EVEN_PARITY 
ODD_PARITY 
STOP1 
STOP2 

Definition at line 46 of file Serial.hh.

Constructor & Destructor Documentation

RS485::RS485 ( uint8_t  port,
IOStream::Device ibuf,
IOStream::Device obuf,
uint16_t  size,
Board::DigitalPin  de,
uint8_t  addr = MASTER 
)
inline

Construct RS485 network driver with data output enable connect to given pin and given node address.

Parameters
[in]ibufinput buffer.
[in]obufoutput buffer.
[in]sizeof buffers.
[in]portuart port.
[in]dedata output enable.
[in]addrnode address (Default MASTER).

Definition at line 79 of file RS485.hh.

Member Function Documentation

void RS485::address ( uint8_t  addr = MASTER)
inline

Set device address.

Parameters
[in]addrdevice address.

Definition at line 102 of file RS485.hh.

virtual int UART::available ( )
inlinevirtualinherited

Number of bytes available in input buffer.

Returns
bytes.

Reimplemented from IOStream::Device.

Definition at line 87 of file UART.hh.

bool UART::begin ( uint32_t  baudrate = DEFAULT_BAUDRATE,
uint8_t  format = DEFAULT_FORMAT 
)
virtualinherited

Powerup and start UART for given baudrate and format. Returns true(1) if successful otherwise false(0).

Parameters
[in]baudrateserial bitrate (default 9600).
[in]formatserial frame format (default async, 8data, 2stop bit)
Returns
true(1) if successful otherwise false(0)

Implements Serial.

Definition at line 45 of file UART.cpp.

void IOStream::Device::blocking ( )
inlineinherited

Set blocking mode.

Definition at line 85 of file IOStream.hh.

int RS485::broadcast ( const void *  buf,
size_t  len 
)
inline

Send message in given buffer and number of bytes to all device on network. Return number of bytes sent or negative error code.

Parameters
[in]bufpointer to message buffer.
[in]lennumber of bytes.
Returns
number of bytes sent or negative error code.

Definition at line 125 of file RS485.hh.

virtual void UART::empty ( )
inlinevirtualinherited

Empty output device buffers.

Reimplemented from IOStream::Device.

Definition at line 156 of file UART.hh.

bool UART::end ( )
virtualinherited

Stop and powerdown UART device driver. Returns true(1) if successful otherwise false(0).

Returns
true(1) if successful otherwise false(0)

Reimplemented from Serial.

Definition at line 69 of file UART.cpp.

void IOStream::Device::eol ( Mode  mode)
inlineinherited

Set end of line mode.

Parameters
[in]modefor end of line.

Definition at line 103 of file IOStream.hh.

Mode IOStream::Device::eol ( ) const
inlineinherited

Get end of line mode.

Returns
mode.

Definition at line 112 of file IOStream.hh.

int UART::flush ( )
virtualinherited

Flush device output buffer and wait for device to become idle and all character transmitted.

Returns
zero(0) or negative error code.

Reimplemented from IOStream::Device.

Definition at line 130 of file UART.cpp.

virtual int UART::getchar ( )
inlinevirtualinherited

Read character from serial port input buffer. Returns character if successful otherwise a negative error code (EOF(-1)).

Returns
character or EOF(-1).

Reimplemented from IOStream::Device.

Definition at line 139 of file UART.hh.

char * IOStream::Device::gets ( char *  s,
size_t  count 
)
virtualinherited

Read string terminated by new-line or until size into given string buffer. Returns pointer to string or NULL if empty line.

Parameters
[in]sstring buffer to read into.
[in]countmax number of bytes to read.
Returns
string pointer or NULL.

Definition at line 118 of file IOStream_Device.cpp.

bool IOStream::Device::is_blocking ( ) const
inlineinherited

Is blocking mode?

Returns
bool.

Definition at line 94 of file IOStream.hh.

void IOStream::Device::non_blocking ( )
inlineinherited

Set non-blocking mode.

Definition at line 77 of file IOStream.hh.

void UART::on_rx_interrupt ( )
protectedvirtualinherited

Common UART receive interrupt handler.

Definition at line 221 of file UART.cpp.

virtual void RS485::on_transmit_completed ( )
inlineprotectedvirtual

Transmit completed callback. Clear data output enable pin.

Definition at line 177 of file RS485.hh.

void RS485::on_tx_interrupt ( )
protectedvirtual

RS485 transmit completed interrupt handler. Call extension

Reimplemented from UART.

Definition at line 79 of file RS485.cpp.

void RS485::on_udre_interrupt ( )
protectedvirtual

RS485 data register empty (transmit) interrupt handler with completion handling.

Reimplemented from UART.

Definition at line 65 of file RS485.cpp.

virtual int UART::peekchar ( )
inlinevirtualinherited

Peek at next character from serial port input buffer. Returns character if successful otherwise a negative error code (EOF(-1)).

Returns
character or EOF(-1).

Reimplemented from IOStream::Device.

Definition at line 117 of file UART.hh.

virtual int UART::peekchar ( char  c)
inlinevirtualinherited

Peek for given character from serial port input buffer.

Parameters
[in]ccharacter to peek for.
Returns
available or EOF(-1).

Reimplemented from IOStream::Device.

Definition at line 128 of file UART.hh.

void UART::powerdown ( )
virtualinherited

Powerdown UART.

Reimplemented from Serial.

Definition at line 179 of file UART.cpp.

void UART::powerup ( )
virtualinherited

Powerup UART.

Reimplemented from Serial.

Definition at line 150 of file UART.cpp.

int RS485::putchar ( char  c)
virtual

Write character to serial port output buffer. Returns character if successful otherwise a negative error code. returns EOF(-1),

Parameters
[in]ccharacter to write.
Returns
character written or EOF(-1).

Reimplemented from UART.

Definition at line 53 of file RS485.cpp.

int IOStream::Device::puts ( const char *  s)
virtualinherited

Write null terminated string to device. Terminating null is not written.

Parameters
[in]sstring to write.
Returns
zero(0) or negative error code.

Definition at line 43 of file IOStream_Device.cpp.

int IOStream::Device::puts ( str_P  s)
virtualinherited

Write null terminated string from program memory to device. Terminating null is not written.

Parameters
[in]sstring in program memory to write.
Returns
zero(0) or negative error code.

Definition at line 54 of file IOStream_Device.cpp.

int IOStream::Device::read ( void *  buf,
size_t  size 
)
virtualinherited

Read data to given buffer with given size from device.

Parameters
[in]bufbuffer to read into.
[in]sizenumber of bytes to read.
Returns
number of bytes read or EOF(-1).

Reimplemented in FAT16::File, W5200::Driver, W5500::Driver, W5100::Driver, CFFS::File, Socket, and CC3000::Driver.

Definition at line 144 of file IOStream_Device.cpp.

int IOStream::Device::read ( iovec_t vec)
virtualinherited

Read data to given buffers in null terminated io vector.

Parameters
[in]vecio vector with buffers to read into.
Returns
number of bytes read or EOF(-1).

Definition at line 157 of file IOStream_Device.cpp.

int RS485::recv ( void *  buf,
size_t  len,
uint32_t  ms = 0L 
)

Attempt within given time-limit in milli-seconds receive a message. If received the message is stored in the given buffer with given max length. Returns the number of bytes received or negative error code.

Parameters
[in]bufpointer to message buffer.
[in]lennumber of bytes.
[in]msnumber of milli-seconds timeout (Default BLOCK).
Returns
number of bytes sent or negative error code.

Definition at line 113 of file RS485.cpp.

virtual int UART::room ( )
inlinevirtualinherited

Number of bytes room in output buffer.

Returns
bytes.

Reimplemented from IOStream::Device.

Definition at line 97 of file UART.hh.

int RS485::send ( const void *  buf,
size_t  len,
uint8_t  dest = MASTER 
)

Send message in given buffer and number of bytes to given destination device. Return number of bytes sent or negative error code.

Parameters
[in]bufpointer to message buffer.
[in]lennumber of bytes.
[in]destdestination node (Default MASTER).
Returns
number of bytes sent or negative error code.

Definition at line 86 of file RS485.cpp.

volatile uint16_t* UART::UBRRn ( ) const
inlineprotectedinherited

Return pointer to UART Baud Rate Register (UBRRn).

Returns
UBRRn register pointer.

Definition at line 236 of file UART.hh.

volatile uint8_t* UART::UCSRnA ( ) const
inlineprotectedinherited

Return pointer to UART Control and Status Register A (UCSRnA).

Returns
UCSRAn register pointer.

Definition at line 209 of file UART.hh.

volatile uint8_t* UART::UCSRnB ( ) const
inlineprotectedinherited

Return pointer to UART Control and Status Register B (UCSRnB).

Returns
UCSRnB register pointer.

Definition at line 218 of file UART.hh.

volatile uint8_t* UART::UCSRnC ( ) const
inlineprotectedinherited

Return pointer to UART Control and Status Register C (UCSRnC).

Returns
UCSRnC register pointer.

Definition at line 227 of file UART.hh.

volatile uint8_t* UART::UDRn ( ) const
inlineprotectedinherited

Return pointer to UART I/O Data Register (UDRn).

Returns
UDRn register pointer.

Definition at line 245 of file UART.hh.

int IOStream::Device::write ( const void *  buf,
size_t  size 
)
virtualinherited

Write data from buffer with given size to device.

Parameters
[in]bufbuffer to write.
[in]sizenumber of bytes to write.
Returns
number of bytes written or EOF(-1).

Reimplemented in FAT16::File, HD44780, CFFS::File, VLCD, Socket, and CC3000::Driver.

Definition at line 65 of file IOStream_Device.cpp.

int IOStream::Device::write ( const iovec_t vec)
virtualinherited

Write data from buffers in null terminated io vector.

Parameters
[in]vecio vector with buffers to write.
Returns
number of bytes written or EOF(-1).

Definition at line 87 of file IOStream_Device.cpp.

int IOStream::Device::write_P ( const void *  buf,
size_t  size 
)
virtualinherited

Write data from buffer in program memory with given size to device.

Parameters
[in]bufbuffer to write.
[in]sizenumber of bytes to write.
Returns
number of bytes written or EOF(-1).

Reimplemented in CFFS::File, Socket, and CC3000::Driver.

Definition at line 76 of file IOStream_Device.cpp.

Member Data Documentation

const uint8_t RS485::BROADCAST = 0
static

Network broadcast address.

Definition at line 64 of file RS485.hh.

const uint16_t RS485::BUFFER_MAX = COSA_RS485_BUFFER_MAX
static

Default buffer size.

Definition at line 61 of file RS485.hh.

const uint32_t Serial::DEFAULT_BAUDRATE = 9600L
staticinherited

Default baudrate.

Definition at line 43 of file Serial.hh.

const uint8_t Serial::DEFAULT_FORMAT = DATA8 + NO_PARITY + STOP2
staticinherited

Default serial format.

Definition at line 59 of file Serial.hh.

const uint8_t RS485::FRAME_MAX = sizeof(header_t) + sizeof(uint16_t) + 1
static

Size of frame; SOT, header and crc.

Definition at line 58 of file RS485.hh.

uint8_t RS485::m_addr
protected

Network address; Special cases are MASTER and BROADCAST.

Definition at line 154 of file RS485.hh.

bool IOStream::Device::m_blocking
protectedinherited

Blocking state

Definition at line 248 of file IOStream.hh.

OutputPin RS485::m_de
protected

Data output enable pin; MAX485/DE and /RE.

Definition at line 151 of file RS485.hh.

Mode IOStream::Device::m_eol
protectedinherited

End of line mode

Definition at line 251 of file IOStream.hh.

header_t RS485::m_header
protected

Send/receive header.

Definition at line 148 of file RS485.hh.

IOStream::Device* UART::m_ibuf
protectedinherited

Input Buffer/Device.

Definition at line 195 of file UART.hh.

bool UART::m_idle
protectedinherited

Flag idle mode.

Definition at line 197 of file UART.hh.

IOStream::Device* UART::m_obuf
protectedinherited

Output Buffer/Device.

Definition at line 196 of file UART.hh.

uint8_t UART::m_port
protectedinherited

UART port index.

Definition at line 193 of file UART.hh.

volatile uint8_t* const UART::m_sfr
protectedinherited

Special Function Register Pointer.

Definition at line 194 of file UART.hh.

uint8_t RS485::m_state
protected

Receive state; wait for start symbol, header, payload and check-sum.

Definition at line 157 of file RS485.hh.

const uint8_t RS485::MASTER = 255
static

Network master address.

Definition at line 67 of file RS485.hh.

const uint16_t RS485::PAYLOAD_MAX
protected

Maximum payload size.

Definition at line 145 of file RS485.hh.

const uint16_t UART::RX_BUFFER_MAX = COSA_UART_RX_BUFFER_MAX
staticinherited

Default buffer size for standard UART0 (at 9600 baud).

Definition at line 62 of file UART.hh.

const uint8_t RS485::SOT = 0x01
static

Start of transmission token.

Definition at line 47 of file RS485.hh.

const uint16_t UART::TX_BUFFER_MAX = COSA_UART_TX_BUFFER_MAX
staticinherited

Definition at line 63 of file UART.hh.

UART * UART::uart = { NULL }
staticprotectedinherited

Serial port references. Only uart0 is predefined (reference to global uart). Others as installed by constructor when created in application.

Definition at line 203 of file UART.hh.


The documentation for this class was generated from the following files: