Page MenuHomec4science

packet.h
No OneTemporary

File Metadata

Created
Thu, Apr 3, 17:45

packet.h

#ifndef SBCP_MASTER_PACKETS_H_
#define SBCP_MASTER_PACKETS_H_
#include <sbcp-uc/sbcp_common.h>
#include "register.h"
#include "timer.h"
typedef enum packet_rx_state {
PCK_RX_UNITIALIZED = 0,
PCK_RX_WAIT_FOR_HEADER_BYTE = 1,
PCK_RX_WAIT_FOR_FULL_HEADER = 2,
PCK_RX_WAIT_FOR_FULL_DATA = 3,
PCK_RX_FINISHED = 4
} packet_rx_state;
typedef enum packet_rx_error {
PCK_RX_NO_ERROR = 0,
PCK_RX_TERR,
PCK_RX_FERR,
PCK_RX_PERR,
PCK_RX_OERR
} packet_rx_error;
typedef struct packet_mdata {
unsigned int dma_adr;
volatile unsigned int * data;
ticks start_rx_date;
ticks end_tx_date;
packet_rx_state rx_state;
packet_rx_error rx_error;
uint8_t id;
} packet_mdata;
//define the maximal packet tx time to 1 ticks per byte. a byte should take 3us, we give him 6.5us
#define MAX_PACKET_TX_TIME SBCP_PACKET_SIZE
// define the std pck rsp time to 1ms (\approx 156 ticks)
#define STD_PACKET_RSP_TIMEOUT (sbcp_reg_uint(SLAVE_RSP_TIMEOUT))
//define the low latency time out arroun 13us
#define LLA_PACKET_RSP_TIMEOUT (sbcp_reg_uint(SLAVE_LL_RSP_TIMEOUT))
/**
* Returns if the rx of a packet timeouted. It occurs when the packet
* has at least received one byte but not all and there is a
* timeout.
* \warning : Concurency UNSAFE
*/
#define pck_rx_timeouted(packet) ( ( (packet)->rx_state > PCK_RX_WAIT_FOR_HEADER_BYTE ) \
&& ( (packet)->rx_state < PCK_RX_FINISHED ) \
&& ( ( current_time() - (packet)->start_rx_date ) >= MAX_PACKET_TX_TIME ) )
// 1 if the std tx of a packet timeouted
#define pck_std_tx_timeouted(packet) ( ( current_time() - (packet)->end_tx_date ) >= STD_PACKET_RSP_TIMEOUT)
// 1 if the low latency tx of a packet timeouted
#define pck_lla_tx_timeouted(packet) ( ( current_time() - (packet)->end_tx_date ) >= LLA_PACKET_RSP_TIMEOUT )
#define pck_init_rx(packet) do{\
(packet)->rx_state = PCK_RX_WAIT_FOR_HEADER_BYTE; \
(packet)->rx_error = PCK_RX_NO_ERROR; \
}while(0)
#define pck_start_header_rx(packet) do{\
(packet)->rx_state = PCK_RX_WAIT_FOR_FULL_HEADER; \
(packet)->start_rx_date = current_time(); \
}while(0)
#define pck_start_data_rx(packet) do{\
(packet)->rx_state = PCK_RX_WAIT_FOR_FULL_DATA; \
}while(0)
#define pck_finish_rx(packet) do{\
(packet)->rx_state = PCK_RX_FINISHED; \
}while(0)
#define pck_clean_mdata(packet) do{\
(packet)->rx_state = PCK_RX_UNITIALIZED; \
}while(0)
#define pck_payload_size(packet) ( (packet)->data[SBCP_PAYLOAD_SIZE_POS] )
#define pck_byte_size(packet) ( pck_payload_size(packet) + SBCP_HEADER_SIZE )
#endif //SBCP_MASTER_PACKET_H_

Event Timeline