LP-89 - Port OP_15.05.01 fixes. Release notes:
[librepilot.git] / flight / libraries / inc / ssp.h
blob423892588de61ce41fdd6e965910bd076601d688
1 /*******************************************************************
3 * NAME: ssp.h
6 *******************************************************************/
7 #ifndef SSP_H
8 #define SSP_H
9 /** INCLUDE FILES **/
10 #include <stdint.h>
12 /** LOCAL DEFINITIONS **/
13 #ifndef TRUE
14 #define TRUE 1
15 #endif
17 #ifndef FALSE
18 #define FALSE 0
19 #endif
21 #define SPP_USES_CRC
23 #define SSP_TX_IDLE 0 // not expecting a ACK packet (no current transmissions in progress)
24 #define SSP_TX_WAITING 1 // waiting for a valid ACK to arrive
25 #define SSP_TX_TIMEOUT 2 // failed to receive a valid ACK in the timeout period, after retrying.
26 #define SSP_TX_ACKED 3 // valid ACK received before timeout period.
27 #define SSP_TX_BUFOVERRUN 4 // amount of data to send execeds the transmission buffer sizeof
28 #define SSP_TX_BUSY 5 // Attempted to start a transmission while a transmission was already in progress.
29 // #define SSP_TX_FAIL - failure...
31 #define SSP_RX_IDLE 0
32 #define SSP_RX_RECEIVING 1
33 #define SSP_RX_COMPLETE 2
35 // types of packet that can be received
36 #define SSP_RX_DATA 5
37 #define SSP_RX_ACK 6
38 #define SSP_RX_SYNCH 7
40 typedef enum decodeState_ {
41 decode_len1_e = 0,
42 decode_seqNo_e,
43 decode_data_e,
44 decode_crc1_e,
45 decode_crc2_e,
46 decode_idle_e
47 } DecodeState_t;
49 typedef enum ReceiveState {
50 state_escaped_e = 0, state_unescaped_e
51 } ReceiveState_t;
53 typedef struct {
54 uint8_t *pbuff;
55 uint16_t length;
56 uint16_t crc;
57 uint8_t seqNo;
58 } Packet_t;
60 typedef struct {
61 uint8_t *rxBuf; // Buffer used to store rcv data
62 uint16_t rxBufSize; // rcv buffer size.
63 uint8_t *txBuf; // Length of data in buffer
64 uint16_t txBufSize; // CRC for data in Packet buff
65 uint16_t max_retry; // Maximum number of retrys for a single transmit.
66 int32_t timeoutLen; // how long to wait for each retry to succeed
67 void (*pfCallBack)(uint8_t *, uint16_t); // call back function that is called when a full packet has been received
68 int16_t (*pfSerialRead)(void); // function to call to read a byte from serial hardware
69 void (*pfSerialWrite)(uint8_t); // function used to write a byte to serial hardware for transmission
70 uint32_t (*pfGetTime)(void); // function returns time in number of seconds that has elapsed from a given reference point
71 } PortConfig_t;
73 typedef struct Port_tag {
74 void (*pfCallBack)(uint8_t *, uint16_t); // call back function that is called when a full packet has been received
75 int16_t (*pfSerialRead)(void); // function to read a character from the serial input stream
76 void (*pfSerialWrite)(uint8_t); // function to write a byte to be sent out the serial port
77 uint32_t (*pfGetTime)(void); // function returns time in number of seconds that has elapsed from a given reference point
78 uint8_t retryCount; // how many times have we tried to transmit the 'send' packet
79 uint8_t maxRetryCount; // max. times to try to transmit the 'send' packet
80 int32_t timeoutLen; // how long to wait for each retry to succeed
81 uint32_t timeout; // current timeout. when 'time' reaches this point we have timed out
82 uint8_t txSeqNo; // current 'send' packet sequence number
83 uint16_t rxBufPos; // current buffer position in the receive packet
84 uint16_t rxBufLen; // number of 'data' bytes in the buffer
85 uint8_t rxSeqNo; // current 'receive' packet number
86 uint16_t rxBufSize; // size of the receive buffer.
87 uint16_t txBufSize; // size of the transmit buffer.
88 uint8_t *txBuf; // transmit buffer. REquired to store a copy of packet data in case a retry is needed.
89 uint8_t *rxBuf; // receive buffer. Used to store data as a packet is received.
90 uint16_t sendSynch; // flag to indicate that we should send a synchronize packet to the host
91 // this is required when switching from the application to the bootloader
92 // and vice-versa. This fixes the firwmare download timeout.
93 // when this flag is set to true, the next time we send a packet we will first
94 // send a synchronize packet.
95 ReceiveState_t InputState;
96 DecodeState_t DecodeState;
97 uint16_t SendState;
98 uint16_t crc;
99 uint32_t RxError;
100 uint32_t TxError;
101 uint16_t flags;
102 } Port_t;
104 /** Public Data **/
106 /** PUBLIC FUNCTIONS **/
107 int16_t ssp_ReceiveProcess(Port_t *thisport);
108 int16_t ssp_SendProcess(Port_t *thisport);
109 uint16_t ssp_SendString(Port_t *thisport, char *str);
110 int16_t ssp_SendData(Port_t *thisport, const uint8_t *data,
111 const uint16_t length);
112 void ssp_Init(Port_t *thisport, const PortConfig_t *const info);
113 int16_t ssp_ReceiveByte(Port_t *thisport);
114 uint16_t ssp_Synchronise(Port_t *thisport);
116 /** EXTERNAL FUNCTIONS **/
118 #endif // ifndef SSP_H