Serial Wombat Arduino Library

Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | List of all members
SerialWombatUART Class Reference

A class for the Serial Wombat 4B or SW18AB chips which creates an I2C to UART Bridge. More...

`#include <SerialWombatUART.h>`

Inheritance diagram for SerialWombatUART:
[legend]
Collaboration diagram for SerialWombatUART:
[legend]

## Public Member Functions
| SerialWombatUART (SerialWombatChip &serialWombat)
| Constructor for the SerialWombatUART class. Only one instance is allowed per SerialWombatChip 4B.
int16_t | begin (uint32_t baudRate, uint8_t pin, uint8_t rxPin, uint8_t txPin, uint8_t HWinterface=1)
virtual int | available ()
| Queries the SerialWombatUART for number bytes available to read.
virtual int | read ()
| Reads a byte from the SerialWombatUART.
virtual void | flush ()
| Discard all received bytes.
virtual int | peek ()
| Query the SerialWombatUART for the next avaialble byte, but don't remove it from the queue.
virtual size_t | write (uint8_t data)
| Write a byte to the SerialWombatUART for Transmit.
virtual size_t | write (const uint8_t *buffer, size_t size)
| Write bytes to the SerialWombatUART for Transmit.
virtual int | availableForWrite ()
| Queries the SerialWombatUART for the amount of free TX queue space.
virtual size_t | readBytes (char *buffer, size_t length)
| Reads a specified number of bytes from the SerialWombatUART RX queue.
virtual void | setTimeout (long timeout_mS)
Public Member Functions inherited from SerialWombatPin
| SerialWombatPin (SerialWombatChip &serialWombatChip)
| Instantiates a Serial Wombat Pin.
| SerialWombatPin (SerialWombatChip &serialWombatChip, uint8_t pin)
| Instantiates a Serial Wombat Pin.
uint16_t | readPublicData ()
| Read the 16 Bit public data associated with this pin.
void | pinMode (uint8_t mode, bool pullDown=false, bool openDrain=false)
| Set pin to INPUT or OUTPUT, with options for pull Ups and open Drain settings.
void | digitalWrite (uint8_t val)
| Set output pin High or Low.
int | digitalRead ()
| Reads the state of the Pin.
uint16_t | writePublicData (uint16_t value)
| Write a 16 bit value to this pin.
uint8_t | pin ()
| Returns the current SW pin number. Used primarily for virtual calls by derived classes.
uint8_t | swPinModeNumber ()
| Returns the Mode number. Used primarily by derived classes to populate packet data.
int16_t | initPacketNoResponse (uint8_t packetNumber, uint8_t param0=0x55, uint8_t param1=0x55, uint8_t param2=0x55, uint8_t param3=0x55, uint8_t param4=0x55)
int16_t | initPacketNoResponse (uint8_t packetNumber, uint16_t param0=0x55, uint8_t param1=0x55, uint8_t param2=0x55, uint8_t param3=0x55)
int16_t | initPacketNoResponse (uint8_t packetNumber, uint16_t param0, uint16_t param1, uint8_t param2=0x55)
int16_t | disable ()
| Disables the pin mode (if applicable)
int16_t | enablePullup (bool enabled)
| Enables the weak pull up on a pin. Implemented on SW18AB and SW8B only.
int16_t | enablePulldown (bool enabled)
| Enables the weak pull down on a pin. Implemented on SW18AB and SW8B only.
int16_t | enableOpenDrain (bool enabled)
| Enables open drain mode on a pin. Implemented on SW18AB and SW8B only.
int16_t | forceDMA (bool enabled)
| Forces use of DMA instead of hardware timing resources on SW18AB.
void | setPinNumberForTesting (uint8_t pin)
| A function designed for testing only. May have unpredictable results if used in real time. Not intended for general use.

## Protected Attributes
uint8_t | _rxPin = 255
uint8_t | _txPin = 255
uint8_t | _baudMarker = 0
uint32_t | timeout = 5000
uint8_t | _pinMode = PIN_MODE_UART_RX_TX
uint8_t | _tx7Command = (uint8_t)SerialWombatCommands::COMMAND_UART0_TX_7BYTES
uint8_t | _rx7Command = (uint8_t)SerialWombatCommands::COMMAND_UART0_RX_7BYTES
Protected Attributes inherited from SerialWombatPin
uint8_t | _pin = 255
SerialWombatChip & | _sw
uint8_t | _pinMode = 0

## Detailed Description

A class for the Serial Wombat 4B or SW18AB chips which creates an I2C to UART Bridge.

This class allows use of the Serial Wombat 4B chips's internal UART hardware to send and receive data at standard baud rates in 8-N-1 format.

A Tutorial video is avaialble:

https://youtu.be/C1FjcaiBYZs

The class inherits from the Arduino Sream class, so functions such as println() can be used once the UART is initialized.

The Serial Wombat 4B chip has a 64 byte transmit buffer and 128 byte receive buffer. Therefore, up to 64 bytes can be sent to the SerialWombatUART at a time. Attempts to send more than that will result in the write, print, etc command blocking until space is available on the SerialWombatUART to buffer the data.

Received data is buffered on the Serial Wombat chip until it is retreived from the host.

Warning
  Due to the overhead of querying and retreiving data from the SerialWombatUART, data loss is likely when receiving streams of data greater than the buffer size at higher baud rates.

To minimize this possiblity, read data frequently from the Serial Wombat chip, and set I2C frequency to 400kHz to maximize throughput (SW4B only) .

This class can Send, Receive, or both. A single instance of this class is allowed per Serial Wombat 4B chip due to the fact that it relies on the PIC16F15214's UART module, of which there is only one on the micro.

Two instances of this class can be used on the Serial Wombat 18AB chip by using the begin() call which takes a hardware indicator of 0 or 1.

A full Serial Wombat packet send / receive sequence (8 bytes in each direction) over I2C is necessary to query the status of the queues or to read or receive a byte of data.

The protocol becomes more efficient if multiple bytes are read or written using the readBytes or write(const uint8_t* buffer, size_t size) interfaces rather than read() or write(uint8_t data).

The class must be assigned to a pin. This may be either the receive or transmit pin.

Pin 0 on the Serial Wombat 4B is suggested as a receive pin as it has only input capability.

Serial Wombat 18AB pins must be enhanced digital performance pins.

Available baud rates are:

- 300
- 1200
- 2400
- 4800
- 9600
- 19200
- 38400
- 57600
- 115200

Definition at line 94 of file SerialWombatUART.h.

## Constructor & Destructor Documentation

## ◆ SerialWombatUART()

SerialWombatUART::SerialWombatUART | ( | SerialWombatChip & | serialWombat | ) |
| inline

Constructor for the SerialWombatUART class. Only one instance is allowed per SerialWombatChip 4B.

Parameters

  serialWombat | The Serial Wombat chip on which the SerialWombatUART instance will run.

Definition at line 102 of file SerialWombatUART.h.

## Member Function Documentation

## ◆ available()

virtual int SerialWombatUART::available | ( |  | ) |
| inline virtual

Queries the SerialWombatUART for number bytes available to read.

Returns
  Number of bytes available to read.

Definition at line 180 of file SerialWombatUART.h.

## ◆ availableForWrite()

virtual int SerialWombatUART::availableForWrite | ( |  | ) |
| inline virtual

Queries the SerialWombatUART for the amount of free TX queue space.

Returns
  A value between 0 and 64 for the SW4B

Definition at line 328 of file SerialWombatUART.h.

## ◆ begin()

int16_t SerialWombatUART::begin | ( | uint32_t | baudRate,
|  | uint8_t | pin,
|  | uint8_t | rxPin,
|  | uint8_t | txPin,
|  | uint8_t | HWinterface = 1 )
| inline

    @brief Initalize the SerialWombatUART.

Parameters

  baudRate | 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200
  pin | The pin that will host the state machine. This can be either the rxPin or txPin
  rxPin | The pin that will receive. All 4 pins on the SW4B may be used. 255 if no receive function is needed
  txPin | The pin that will transmit. Valid values for the SW4B are 1-3. 255 if no transmit function is needed
  HWinterface | 1 or 2 for HW UART 1 or 2

Definition at line 113 of file SerialWombatUART.h.

## ◆ flush()

virtual void SerialWombatUART::flush | ( |  | ) |
| inline virtual

Discard all received bytes.

Definition at line 212 of file SerialWombatUART.h.

## ◆ peek()

virtual int SerialWombatUART::peek | ( |  | ) |
| inline virtual

Query the SerialWombatUART for the next avaialble byte, but don't remove it from the queue.

Returns
  A byte from 0-255, or -1 if no bytes were avaialble

Definition at line 222 of file SerialWombatUART.h.

## ◆ read()

virtual int SerialWombatUART::read | ( |  | ) |
| inline virtual

Reads a byte from the SerialWombatUART.

Returns
  A byte from 0-255, or -1 if no bytes were avaialble

Definition at line 191 of file SerialWombatUART.h.

## ◆ readBytes()

virtual size_t SerialWombatUART::readBytes | ( | char * | buffer,
|  | size_t | length )
| inline virtual

Reads a specified number of bytes from the SerialWombatUART RX queue.

Parameters

  buffer | An array into which to put received bytes
  length | The maximum number of bytes to be received

Returns
  the number of bytes written to buffer

This function will read bytes from the SerialWombatUART RX queue into buffer. If 'length' characters are not available to read then the value returned will be less than length.

Reimplemented in SerialWombatSWUART.

Definition at line 345 of file SerialWombatUART.h.

## ◆ setTimeout()

virtual void SerialWombatUART::setTimeout | ( | long | timeout_mS | ) |
| inline virtual

Definition at line 409 of file SerialWombatUART.h.

## ◆ write() [1/2]

virtual size_t SerialWombatUART::write | ( | const uint8_t * | buffer,
|  | size_t | size )
| inline virtual

Write bytes to the SerialWombatUART for Transmit.

Parameters

  buffer | An array of uint8_t bytes to send
  size | the number of bytes to send

Returns
  the number of bytes sent

This function queries the SerialWombatUART for avaialble TX buffer space, and sends bytes as buffer space is avaialble. If avaialable buffer space is not sufficient to send the entire array then the function will block and continue trying until the entire message has been sent to the SerialWombatUART transmit queue.

Reimplemented in SerialWombatSWUART.

Definition at line 266 of file SerialWombatUART.h.

## ◆ write() [2/2]

virtual size_t SerialWombatUART::write | ( | uint8_t | data | ) |
| inline virtual

Write a byte to the SerialWombatUART for Transmit.

Parameters

  data | Byte to write

Returns
  Number of bytes written

This does not check to see if space is avaialble in order to improve perfomance . This isn't an issue at high baud rates, as overhead to transmit one byte at a time allows sufficent time for queuing data to be sent by the UART. This could be a problem at very low baud rates and high I2C bus speeds.

Definition at line 247 of file SerialWombatUART.h.

## Member Data Documentation

## ◆ _baudMarker

uint8_t SerialWombatUART::_baudMarker = 0
| protected

Definition at line 423 of file SerialWombatUART.h.

## ◆ _pinMode

uint8_t SerialWombatUART::_pinMode = PIN_MODE_UART_RX_TX
| protected

Definition at line 425 of file SerialWombatUART.h.

## ◆ _rx7Command

uint8_t SerialWombatUART::_rx7Command = (uint8_t)SerialWombatCommands::COMMAND_UART0_RX_7BYTES
| protected

Definition at line 427 of file SerialWombatUART.h.

## ◆ _rxPin

uint8_t SerialWombatUART::_rxPin = 255
| protected

Definition at line 421 of file SerialWombatUART.h.

## ◆ _tx7Command

uint8_t SerialWombatUART::_tx7Command = (uint8_t)SerialWombatCommands::COMMAND_UART0_TX_7BYTES
| protected

Definition at line 426 of file SerialWombatUART.h.

## ◆ _txPin

uint8_t SerialWombatUART::_txPin = 255
| protected

Definition at line 422 of file SerialWombatUART.h.

## ◆ timeout

uint32_t SerialWombatUART::timeout = 5000
| protected

Definition at line 424 of file SerialWombatUART.h.