8 |
8 |
9 #define MAX_ERRS 10 |
9 #define MAX_ERRS 10 |
10 #define MAX_BUFF 16 |
10 #define MAX_BUFF 16 |
11 #define BUF_SIZE 16 |
11 #define BUF_SIZE 16 |
12 |
12 |
|
13 |
|
14 |
13 class HPSerial { |
15 class HPSerial { |
14 |
16 |
15 public: |
17 public: |
16 enum TrState { |
|
17 Idle = 0, |
|
18 Tx, |
|
19 Rx, |
|
20 }; |
|
21 typedef struct _CMD |
18 typedef struct _CMD |
22 { |
19 { |
23 TrState direction; |
|
24 uint8_t cmd; |
20 uint8_t cmd; |
25 uint8_t size; |
21 uint8_t size; |
26 char value[MAX_BUFF+1]; |
22 char value[MAX_BUFF+1]; |
27 unsigned long id; |
23 unsigned long id; |
28 } CMD; |
24 } CMD; |
29 |
25 |
30 |
26 |
31 |
27 |
32 HPSerial(PinName rx); |
28 HPSerial(PinName tx, PinName rx); |
33 |
29 |
34 bool cmd_available(void); |
30 bool cmd_available(void); |
35 bool pop(CMD& cmd); |
31 bool pop(CMD& cmd); |
36 bool cmd_buf_full(void); |
32 bool cmd_buf_full(void); |
37 unsigned int nerrors(uint8_t errorno); |
33 unsigned int nerrors(uint8_t errorno); |
38 |
34 |
39 |
35 |
40 private: |
36 private: |
41 void reset(uint8_t errorno=0xFF); |
37 void pushCmd(uint8_t cmd, uint8_t size, char *payload); |
42 void handleAck(uint8_t val); |
|
43 void pushCmd(TrState direction, uint8_t cmd, uint8_t size, char *payload); |
|
44 void handleChar(uint8_t val); |
|
45 void setstatedbg(void); |
|
46 void rxIrq(void); |
38 void rxIrq(void); |
47 void timeout(void); |
39 void timeout(void); |
48 |
40 |
49 private: |
41 private: |
50 RawSerial serial_rx; |
42 RawSerial serial; |
51 uint8_t buf[BUF_SIZE]; |
43 uint8_t buf[BUF_SIZE]; |
52 uint8_t head; |
44 uint8_t head; |
53 uint8_t tx_state; |
|
54 uint8_t tx_cmd; |
|
55 uint8_t tx_len; |
|
56 bool tx_ack; |
|
57 CircularBuffer<CMD, 32> cmdbuf; |
45 CircularBuffer<CMD, 32> cmdbuf; |
58 unsigned long ncmd; |
46 unsigned long ncmd; |
59 unsigned int errs[MAX_ERRS]; |
47 unsigned int errs[MAX_ERRS]; |
60 Ticker timeouter; |
48 Ticker timeouter; |
|
49 |
|
50 // state machine stuff |
|
51 typedef enum {STATE_IDLE, |
|
52 STATE_HANDCHEKED, |
|
53 STATE_COMMAND, |
|
54 STATE_PAYLOAD_SIZE, |
|
55 STATE_PAYLOAD, |
|
56 NUM_STATES} state_t; |
|
57 |
|
58 |
|
59 typedef struct instance_data { |
|
60 uint8_t cmd; |
|
61 uint8_t size; |
|
62 uint8_t received; |
|
63 char payload[MAX_BUFF]; |
|
64 } instance_data_t; |
|
65 |
|
66 state_t do_state_initial(instance_data_t *data, uint8_t c); |
|
67 state_t do_state_handcheck(instance_data_t *data, uint8_t c); |
|
68 state_t do_state_command(instance_data_t *data, uint8_t c); |
|
69 state_t do_state_payload_size(instance_data_t *data, uint8_t c); |
|
70 state_t do_state_payload(instance_data_t *data, uint8_t c); |
|
71 |
|
72 void send_ack(uint8_t c); |
|
73 |
|
74 // pointer to "state method" (ie. one of the just above) |
|
75 typedef state_t(HPSerial::*statemethod)(instance_data_t *data, uint8_t c); |
|
76 statemethod const state_table[NUM_STATES] = { |
|
77 &HPSerial::do_state_initial, |
|
78 &HPSerial::do_state_handcheck, |
|
79 &HPSerial::do_state_command, |
|
80 &HPSerial::do_state_payload_size, |
|
81 &HPSerial::do_state_payload, |
|
82 }; |
|
83 |
|
84 state_t run_state(state_t cur_state, instance_data_t *data, uint8_t c); |
|
85 |
|
86 state_t cur_state; |
|
87 instance_data rx_data; |
61 }; |
88 }; |
62 |
89 |
63 #endif |
90 #endif |