LwPKT

group LWPKT

Lightweight packet protocol.

Defines

lwpkt_get_from_addr(pkt)

Get address from where packet was sent.

Parameters
  • pkt[in] LwPKT instance

Returns

Address

lwpkt_get_to_addr(pkt)

Get address to where packet was sent.

Parameters
  • pkt[in] LwPKT instance

Returns

Address

lwpkt_get_data_len(pkt)

Get length of packet.

Parameters
  • pkt[in] LwPKT instance

Returns

Number of data bytes in packet

lwpkt_get_data(pkt)

Get pointer to packet data.

Parameters
  • pkt[in] LwPKT instance

Returns

Pointer to data

lwpkt_get_cmd(pkt)

Get packet command data field.

Parameters
  • pkt[in] LwPKT instance

Returns

Command data field

lwpkt_get_flags(pkt)

Get packet flags.

Parameters
  • pkt[in] LwPKT instance

Returns

Last received packet flags

lwpkt_is_for_me(pkt)

Check if packet to field address matches device address.

Parameters
  • pkt[in] LwPKT instance

Returns

1 on success, 0 otherwise

lwpkt_is_broadcast(pkt)

Check if packet was sent to all devices on network.

Parameters
  • pkt[in] LwPKT instance

Returns

1 if broadcast, 0 otherwise

Typedefs

typedef void (*lwpkt_evt_fn)(struct lwpkt *pkt, lwpkt_evt_type_t evt_type)

Event function prototype.

Param pkt

[in] Packet structure

Param evt_type

[in] Event type

typedef uint32_t lwpkt_addr_t

Device address data type.

Enums

enum lwpkt_state_t

Packet state enumeration.

Values:

enumerator LWPKT_STATE_START = 0x00

Packet waits for start byte

enumerator LWPKT_STATE_FROM

Packet waits for “packet from” byte

enumerator LWPKT_STATE_TO

Packet waits for “packet to” byte

enumerator LWPKT_STATE_CMD

Packet waits for “packet cmd” byte

enumerator LWPKT_STATE_FLAGS

Packet waits for “packet flags” byte (custom user flags)

enumerator LWPKT_STATE_LEN

Packet waits for (multiple) data length bytes

enumerator LWPKT_STATE_DATA

Packet waits for actual data bytes

enumerator LWPKT_STATE_CRC

Packet waits for CRC data

enumerator LWPKT_STATE_STOP

Packet waits for stop byte

enumerator LWPKT_STATE_END

Last entry

enum lwpktr_t

Packet result enumeration.

Values:

enumerator lwpktOK = 0x00

Function returns successfully

enumerator lwpktERR

General error for function status

enumerator lwpktINPROG

Receive is in progress

enumerator lwpktVALID

packet valid and ready to be read as CRC is valid and STOP received

enumerator lwpktERRCRC

CRC integrity error for the packet. Will not wait STOP byte if received

enumerator lwpktERRSTOP

Packet error with STOP byte, wrong character received for STOP

enumerator lwpktWAITDATA

Packet state is in start mode, waiting start byte to start receiving

enumerator lwpktERRMEM

No enough memory available for write

enum lwpkt_evt_type_t

List of event types.

Values:

enumerator LWPKT_EVT_PKT

Valid packet ready to read

enumerator LWPKT_EVT_TIMEOUT

Timeout on packat, reset event

enumerator LWPKT_EVT_READ

Packet read operation. Called when read operation happens from RX buffer

enumerator LWPKT_EVT_WRITE

Packet write operation. Called when write operation happens to TX buffer

enumerator LWPKT_EVT_PRE_WRITE

Packet pre-write operation. Called before write operation could even start. It can be used to get exclusive mutex access to the resource

enumerator LWPKT_EVT_POST_WRITE

Packet post-write operation. Called after write operation finished. It can be used to release exclusive mutex access from the resource

enumerator LWPKT_EVT_PRE_READ

Packet pre-read operation. Called before read operation could even start. It can be used to get exclusive mutex access to the resource

enumerator LWPKT_EVT_POST_READ

Packet post-read operation. Called after read operation finished. It can be used to release exclusive mutex access from the resource

Functions

lwpktr_t lwpkt_init(lwpkt_t *pkt, lwrb_t *tx_rb, lwrb_t *rx_rb)

Initialize packet instance and set device address.

Parameters
  • pkt[in] Packet instance

  • tx_rb[in] TX LwRB instance for data write

  • rx_rb[in] RX LwRB instance for data read

Returns

lwpktOK on success, member of lwpktr_t otherwise

lwpktr_t lwpkt_set_addr(lwpkt_t *pkt, lwpkt_addr_t addr)

Set device address for packet instance.

Parameters
  • pkt[in] Packet instance

  • addr[in] New device address

Returns

lwpktOK on success, member of lwpktr_t otherwise

lwpktr_t lwpkt_read(lwpkt_t *pkt)

Read raw data from RX ring buffer, parse the characters and try to construct the receive packet.

Use lwpkt_process function instead

Note

This is the raw implementation, and does not take into account potential data desynchronization or data loss at the transmission layer.

Parameters

pkt[in] Packet instance

Returns

lwpktVALID when packet valid, member of lwpktr_t otherwise

lwpktr_t lwpkt_write(lwpkt_t *pkt, lwpkt_addr_t to, uint32_t flags, uint32_t cmd, const void *data, size_t len)

Write packet data to TX ringbuffer.

Parameters
  • pkt[in] Packet instance

  • to[in] End device address

  • cmd[in] Packet command

  • data[in] Pointer to input data. Set to NULL if not used

  • len[in] Length of input data. Must be set to 0 if data == NULL

Returns

lwpktOK on success, member of lwpktr_t otherwise

lwpktr_t lwpkt_reset(lwpkt_t *pkt)

Reset packet state.

Parameters

pkt[in] Packet instance

Returns

lwpktOK on success, member of lwpktr_t otherwise

lwpktr_t lwpkt_process(lwpkt_t *pkt, uint32_t time)

Process packet RX data and check for timeouts, which may occur of there is no receive data for up to maximum time.

Note

This function shall be called periodically, at least once per every LWPKT_CFG_PROCESS_INPROG_TIMEOUT milliseconds, to properly handle delays and timeouts in the application

Parameters
  • pkt[in] Packet instance

  • time[in] Current time in units of milliseconds

Returns

lwpktOK if processing OK, member of lwpktr_t otherwise

lwpktr_t lwpkt_set_evt_fn(lwpkt_t *pkt, lwpkt_evt_fn evt_fn)

Set event function for packet events.

Parameters
  • pkt[in] Packet structure

  • evt_fn[in] Function pointer for events

Returns

lwpktOK on success, member of lwpktr_t otherwise

void lwpkt_set_addr_enabled(lwpkt_t *pkt, uint8_t enable)

Enable addressing in the packet.

Note

This function is only available, if LWPKT_CFG_USE_ADDR is 2

Parameters
  • pkt – LwPKT instance

  • enable1 to enable, 0 otherwise

void lwpkt_set_addr_extended_enabled(lwpkt_t *pkt, uint8_t enable)

Enable extended addressing in the packet.

Note

This function is only available, if LWPKT_CFG_ADDR_EXTENDED is 2

Parameters
  • pkt – LwPKT instance

  • enable1 to enable, 0 otherwise

void lwpkt_set_flags_enabled(lwpkt_t *pkt, uint8_t enable)

Enable FLAGS mode in the packet.

Note

This function is only available, if LWPKT_CFG_USE_FLAGS is 2

Parameters
  • pkt – LwPKT instance

  • enable1 to enable, 0 otherwise

void lwpkt_set_cmd_enabled(lwpkt_t *pkt, uint8_t enable)

Enable CMD mode in the packet.

Note

This function is only available, if LWPKT_CFG_USE_CMD is 2

Parameters
  • pkt – LwPKT instance

  • enable1 to enable, 0 otherwise

void lwpkt_set_cmd_extended_enabled(lwpkt_t *pkt, uint8_t enable)

Enable extended addressing in the packet.

Note

This function is only available, if LWPKT_CFG_CMD_EXTENDED is 2

Parameters
  • pkt – LwPKT instance

  • enable1 to enable, 0 otherwise

void lwpkt_set_crc_enabled(lwpkt_t *pkt, uint8_t enable)

Set CRC mode enabled.

Note

This function is only available, if LWPKT_CFG_USE_CRC is 2

Parameters
  • pkt – LwPKT instance

  • enable1 to enable, 0 otherwise

void lwpkt_set_crc32_enabled(lwpkt_t *pkt, uint8_t enable)

Enable extended addressing in the packet.

Note

This function is only available, if LWPKT_CFG_CRC32 is 2

Parameters
  • pkt – LwPKT instance

  • enable1 to enable, 0 otherwise

struct lwpkt_crc_t
#include <lwpkt.h>

CRC structure for packet.

Public Members

uint32_t crc

Current CRC value

struct lwpkt_t
#include <lwpkt.h>

Packet structure.

Public Members

lwpkt_addr_t addr

Current device address

uint8_t data[LWPKT_CFG_MAX_DATA_LEN]

Memory to write received data

lwrb_t *tx_rb

TX ringbuffer

lwrb_t *rx_rb

RX ringbuffer

uint32_t last_rx_time

Last RX time in units of milliseconds

lwpkt_evt_fn evt_fn

Global event function for read and write operation

uint8_t flags

List of flags

lwpkt_state_t state

Actual packet state machine

lwpkt_crc_t crc

Packet CRC object

uint32_t crc_data

CRC data received from the other side

lwpkt_addr_t from

Device address packet is coming from

lwpkt_addr_t to

Device address packet is intended for

uint32_t flags

Custom flags

uint32_t cmd

Command packet

size_t len

Number of bytes to receive

size_t index

General index variable for multi-byte parts of packet

struct lwpkt_t::[anonymous] m

Module that is periodically reset for next packet