Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / experimental / USB_UPLOAD_TOOL / SSP / ssp.h
blob2a2f4bff17cf3742154f57d7180a0ad22e8ca848
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 SSP_TX_IDLE 0 // not expecting a ACK packet (no current transmissions in progress)
22 #define SSP_TX_WAITING 1 // waiting for a valid ACK to arrive
23 #define SSP_TX_TIMEOUT 2 // failed to receive a valid ACK in the timeout period, after retrying.
24 #define SSP_TX_ACKED 3 // valid ACK received before timeout period.
25 #define SSP_TX_BUFOVERRUN 4 // amount of data to send execeds the transmission buffer sizeof
26 #define SSP_TX_BUSY 5 // Attempted to start a transmission while a transmission was already in progress.
27 // #define SSP_TX_FAIL - failure...
29 #define SSP_RX_IDLE 0
30 #define SSP_RX_RECEIVING 1
31 #define SSP_RX_COMPLETE 2
33 // types of packet that can be received
34 #define SSP_RX_DATA 5
35 #define SSP_RX_ACK 6
36 #define SSP_RX_SYNCH 7
38 typedef enum decodeState_ {
39 decode_len1_e = 0,
40 decode_seqNo_e,
41 decode_data_e,
42 decode_crc1_e,
43 decode_crc2_e,
44 decode_idle_e
45 } DecodeState_t;
47 typedef enum ReceiveState {
48 state_escaped_e = 0,
49 state_unescaped_e
50 } ReceiveState_t;
52 typedef struct {
53 uint8_t *pbuff;
54 uint16_t length;
55 uint16_t crc;
56 uint8_t seqNo;
57 } Packet_t;
59 typedef struct {
60 uint8_t *rxBuf; // Buffer used to store rcv data
61 uint16_t rxBufSize; // rcv buffer size.
62 uint8_t *txBuf; // Length of data in buffer
63 uint16_t txBufSize; // CRC for data in Packet buff
64 uint16_t max_retry; // Maximum number of retrys for a single transmit.
65 int32_t timeoutLen; // how long to wait for each retry to succeed
66 void (*pfCallBack)(uint8_t *, uint16_t); // call back function that is called when a full packet has been received
67 int16_t (*pfSerialRead)(void); // function to call to read a byte from serial hardware
68 void (*pfSerialWrite)(uint8_t); // function used to write a byte to serial hardware for transmission
69 uint32_t (*pfGetTime)(void); // function returns time in number of seconds that has elapsed from a given reference point
70 } PortConfig_t;
72 typedef struct Port_tag {
73 void (*pfCallBack)(uint8_t *, uint16_t); // call back function that is called when a full packet has been received
74 int16_t (*pfSerialRead)(void); // function to read a character from the serial input stream
75 void (*pfSerialWrite)(uint8_t); // function to write a byte to be sent out the serial port
76 uint32_t (*pfGetTime)(void); // function returns time in number of seconds that has elapsed from a given reference point
77 uint8_t retryCount; // how many times have we tried to transmit the 'send' packet
78 uint8_t maxRetryCount; // max. times to try to transmit the 'send' packet
79 int32_t timeoutLen; // how long to wait for each retry to succeed
80 int32_t timeout; // current timeout. when 'time' reaches this point we have timed out
81 uint8_t txSeqNo; // current 'send' packet sequence number
82 uint16_t rxBufPos; // current buffer position in the receive packet
83 uint16_t rxBufLen; // number of 'data' bytes in the buffer
84 uint8_t rxSeqNo; // current 'receive' packet number
85 uint16_t rxBufSize; // size of the receive buffer.
86 uint16_t txBufSize; // size of the transmit buffer.
87 uint8_t *txBuf; // transmit buffer. REquired to store a copy of packet data in case a retry is needed.
88 uint8_t *rxBuf; // receive buffer. Used to store data as a packet is received.
89 uint16_t sendSynch; // flag to indicate that we should send a synchronize packet to the host
90 // this is required when switching from the application to the bootloader
91 // and vice-versa. This fixes the firwmare download timeout.
92 // when this flag is set to true, the next time we send a packet we will first
93 // send a synchronize packet.
94 ReceiveState_t InputState;
95 DecodeState_t DecodeState;
96 uint16_t SendState;
97 uint16_t crc;
98 uint32_t RxError;
99 uint32_t TxError;
100 uint16_t flags;
101 } 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, const uint16_t length);
111 void ssp_Init(Port_t *thisport, const PortConfig_t *const info);
112 int16_t ssp_ReceiveByte(Port_t *thisport);
113 uint16_t ssp_Synchronise(Port_t *thisport);
116 /** EXTERNAL FUNCTIONS **/
118 #endif // ifndef SSP_H