This information applies to the following SoftDevices: S132, S140, S212, S332
The UART RAW protocol implements the Serialization PHY API using the UART interface.
UART RAW for serialization uses four standard UART lines: RX, TX, /CTS, and /RTS. Hardware flow control is enabled. The protocol supports full duplex communication. Every packet consists of a two-byte header followed by payload. The packet header contains the number of bytes of the payload. The header is transmitted with the least significant byte first.
UART RAW packet
Packets are transmitted in the following format:
TX_RAW_PACKET = [TX_HEADER][TX_PAYLOAD]
Reception and transmission of a packet
- First, on the RX line, the length is received as the header:
[RX_HEADER]=[0x0004]
.
- It is then followed by the payload
[RX_PAYLOAD]=[0x00 0x78 0x41 0x03]
.
- After the reception of a packet, transmission starts on the TX line. The packet format is the same as during reception:
[TX_HEADER]=[0x0006]
, [TX_PAYLOAD]=[0x01 0x78 0x00 0x00 0x00 0x00]
.
UART driver
The UART RAW protocol for serialization is implemented in the ser_phy_uart.c
file. It uses the UART driver as a low-level driver.
The implementation is event-driven. Events from the low-level driver are handled in a static uart_event_handler()
function. Three types of nrf_drv_uart_event_t events are processed: NRF_DRV_UART_EVT_ERROR, NRF_DRV_UART_EVT_TX_DONE, and NRF_DRV_UART_EVT_RX_DONE.
- In case of an NRF_DRV_UART_EVT_ERROR event, the error source (taken from the nrf_drv_uart_error_evt_t structure) is checked. If the source is parity or overrun error, then the upper layer is notified with the SER_PHY_EVT_HW_ERROR event. Error source is passed in the error_code member of the ser_phy_evt_hw_error_params_t structure.
- NRF_DRV_UART_EVT_TX_DONE indicates that another part of a packet has been successfully transmitted. Transmission of the subsequent part is then requested, or the upper layer is notified with the SER_PHY_EVT_TX_PKT_SENT event when the process is complete.
- Note
- When transmission of a packet begins, the nrf_drv_uart_tx() function is called for the first time from the ser_phy_tx_pkt_send() function. Later, it is only called from the nrf_drv_uart_event_t event callback.
- NRF_DRV_UART_EVT_RX_DONE indicates that another part of a packet has been successfully received. After the packet header is received, a request to the upper layer is made to provide a proper buffer for that packet. After the final part of the packet is received, the upper layer is notified that the packet has been received or dropped, based on the fact if the buffer was provided or not.
The following ser_phy_evt_t events are sent to the upper layer:
- SER_PHY_EVT_HW_ERROR, when a hardware error is reported by the 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 the 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 the nrf_drv_uart_init() function and prepares reception of incoming packets. It also registers the ser_phy_evt_t event callback.
Function ser_phy_close() closes UART using the nrf_drv_uart_uninit() function. It also deregisters the ser_phy_evt_t event callback.