UART RAW protocol
UART RAW
protocol implements
Serialization PHY
API using
UART
interface.
UART RAW
for serialization uses 4 standard UART lines (
RX
,
TX
,
/CTS
,
/RTS
). Hardware flow control is enabled. The protocol supports full duplex communication.
Every packet consists of 2-byte header followed by payload. Packet header contains number of bytes of the payload. Header is transmitted least significant byte first.
UART RAW packet
Packets are transmitted in the following format :
TX_RAW_PACKET = [TX_HEADER][TX_PAYLOAD]
Firstly, on
RX
line length is received as a header:
[RX_HEADER]=[0x0004]
It is then followed by payload
[RX_PAYLOAD]=[0x00 0x78 0x41 0x03]
After the reception of a packet, transmission starts on
TX
line. Packet format is the same as during reception:
[TX_HEADER]=[0x0006]
,
[TX_PAYLOAD]=[0x01 0x78 0x00 0x00 0x00 0x00]
UART driver
UART RAW
protocol for serialization is implemented in 'ser_phy_nrf51_uart.c' file. It uses 'app_uart.c' as a low-level UART driver.
The implementation is event-driven. Events from the low-level driver are handled in static 'ser_phy_uart_evt_callback()' function. Three types of
app_uart_evt_t
events are processed:
APP_UART_COMMUNICATION_ERROR
,
APP_UART_TX_EMPTY
,
APP_UART_DATA
.
-
In case of
APP_UART_COMMUNICATION_ERROR
event, error source (taken from
app_uart_evt_t
structure) is checked. If the source is parity or overrun error,
then upper layer is notified with SER_PHY_EVT_HW_ERROR . Error source is passed in error_code member of a ser_phy_evt_hw_error_params_t structure.
-
APP_UART_TX_EMPTY
indicates a TXDRDY interrupt. In such case a static function for transmission is called: 'ser_phy_uart_tx()'
Note: when transmission of a packet begins 'ser_phy_uart_tx()' function is called for the first time in ser_phy_tx_pkt_send() function. Later it is only called in
app_uart_evt_t event callback.
-
APP_UART_DATA
indicates a RXDRDY interrupt. In such case a static function for reception: 'ser_phy_uart_rx()' is called with received byte as a parameter. Received byte
is taken from app_uart_evt_t structure.
Following
ser_phy_evt_t
events are send to upper layer:
-
SER_PHY_EVT_HW_ERROR
when a hardware error is reported by low-level driver
-
SER_PHY_EVT_RX_BUF_REQUEST
when a header is received and therefore the payload length is known. From this point reception is paused until
ser_phy_rx_buf_set() function is called by the upper layer, indicating whether a receive buffer has been allocated or not. Subsequently the packet payload is
received or dropped.
-
SER_PHY_EVT_RX_PKT_RECEIVED
when all bytes have been received. This event indicates how many bytes have been received to which memory address.
-
SER_PHY_EVT_RX_PKT_DROPPED
when all bytes are clocked in, but the packet is discarded because no receive buffer was allocated by upper layers.
-
SER_PHY_EVT_TX_PKT_SENT
when all bytes are transmitted.
Function
ser_phy_open()
initializes UART using
APP_UART_INIT
macro with configuration structure of type
app_uart_comm_params_t
as input. It also registers
SER_PHY
event callback.
Function
ser_phy_close()
closes UART using
app_uart_close()
function. It also de-registers
SER_PHY
event callback.
Note: During initialization of UART, pin connected to the RX line is configured as pull-down until first byte is received. This is done to avoid false byte detection due to
glitches on the line when the other side is turned off or starting up.