Mon, 26 Oct 2020 00:23:55 +0100
kill tabs (again) in src/main.cpp
5 | 1 | #include "hp34comm.h" |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
2 | |
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
3 | #include <mbed.h> |
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
4 | #include <CircularBuffer.h> |
5 | 5 | |
6 | /***** HP 34970A communication class ***/ | |
7 | ||
8 | 8 | #ifdef DEBUG2 |
5 | 9 | |
8 | 10 | DigitalOut inrx(D9); |
5 | 11 | |
12 | #endif | |
13 | ||
21 | 14 | DigitalOut lled(LED3); |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
15 | |
37
07e8ca2bdf6d
Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents:
28
diff
changeset
|
16 | #define RXTIMEOUT 50ms |
19 | 17 | #define STARTUPRETRY 0.5 |
21 | 18 | |
19 | 19 | const uint8_t startup_seq[] = { |
20 | 0x33, | |
21 | 0x02, | |
22 | 0x00, | |
23 | }; | |
24 | ||
25 | HPSerial::statemethod HPSerial::state_table[NUM_STATES] = { | |
26 | &HPSerial::do_state_initial, | |
27 | &HPSerial::do_state_command, | |
28 | &HPSerial::do_state_payload_size, | |
29 | &HPSerial::do_state_payload, | |
30 | &HPSerial::do_state_sending, | |
31 | &HPSerial::do_state_eot, | |
32 | }; | |
5 | 33 | |
37
07e8ca2bdf6d
Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents:
28
diff
changeset
|
34 | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
35 | HPSerial::HPSerial(PinName tx, PinName rx): |
19 | 36 | serial(tx, rx), |
37
07e8ca2bdf6d
Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents:
28
diff
changeset
|
37 | ncmd(0), |
07e8ca2bdf6d
Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents:
28
diff
changeset
|
38 | cur_gstate(GSTATE_IDLE) |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
39 | { |
21 | 40 | serial.baud(187500); |
19 | 41 | cur_state = STATE_IDLE; |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
42 | serial.attach(callback(this, &HPSerial::rxIrq), SerialBase::RxIrq); |
19 | 43 | } |
44 | ||
45 | void HPSerial::startup(void) { | |
21 | 46 | cur_gstate = GSTATE_STARTING; |
37
07e8ca2bdf6d
Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents:
28
diff
changeset
|
47 | set_timer(10ms); // launch the startup in 10ms |
19 | 48 | } |
49 | ||
50 | void HPSerial::_startup(void) | |
51 | { | |
52 | cur_gstate = GSTATE_STARTING; | |
53 | tr_data.size = sizeof(startup_seq); | |
54 | tr_data.cmd = 0xFF; | |
55 | tr_data.pos = 0; | |
56 | for(uint8_t i=0; i<tr_data.size; i++) | |
57 | tr_data.payload[i] = startup_seq[i]; | |
58 | cur_state = do_state_sending(); | |
5 | 59 | } |
60 | ||
19 | 61 | void HPSerial::send(const uint8_t *buf, uint8_t size) |
62 | { | |
63 | // tx_data-> | |
64 | //send(startup, sizeof(startup)/sizeof(startup[0])); | |
65 | } | |
66 | ||
67 | void HPSerial::sendkey(uint8_t keycode) | |
68 | { | |
69 | tr_data.size = 2; | |
70 | tr_data.cmd = 0xFF; | |
71 | tr_data.pos = 0; | |
72 | tr_data.payload[0] = 0x66; | |
73 | tr_data.payload[1] = keycode; | |
74 | cur_state = do_state_sending(); | |
75 | } | |
76 | ||
77 | bool HPSerial::cmd_available(void) | |
78 | { | |
5 | 79 | return !cmdbuf.empty(); |
80 | } | |
81 | ||
19 | 82 | bool HPSerial::pop(CMD& cmd) |
83 | { | |
5 | 84 | return cmdbuf.pop(cmd); |
85 | } | |
86 | ||
19 | 87 | bool HPSerial::cmd_buf_full(void) |
88 | { | |
5 | 89 | return cmdbuf.full(); |
90 | } | |
91 | ||
19 | 92 | unsigned int HPSerial::nerrors(uint8_t errorno) |
21 | 93 | { |
5 | 94 | return errs[errorno]; |
95 | } | |
96 | ||
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
97 | void HPSerial::pushCmd(uint8_t cmd, uint8_t size, char *payload) { |
5 | 98 | CMD val; |
99 | uint8_t i; | |
100 | val.id = ncmd++; | |
101 | val.cmd = cmd; | |
102 | val.size = size; | |
103 | for(i=0; i<size; i++) | |
104 | val.value[i] = payload[i]; | |
105 | val.value[i] = 0x00; | |
106 | cmdbuf.push(val); | |
107 | } | |
108 | ||
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
109 | void HPSerial::send_ack(uint8_t c) { |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
110 | serial.write(&c, 1); |
19 | 111 | set_timer(RXTIMEOUT); // if nothing else happen in the next ms, reset |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
112 | } |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
113 | |
19 | 114 | HPSerial::state_t HPSerial::do_state_initial(uint8_t c) |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
115 | { |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
116 | // we are idle, incoming char is a handcheck |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
117 | // knwon handcheck values are 0x66 and 0x33 |
19 | 118 | set_timer(RXTIMEOUT); // reset the watchdog |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
119 | switch (c) { |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
120 | case 0x33: |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
121 | send_ack(0xCC); |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
122 | return HPSerial::STATE_PAYLOAD_SIZE; |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
123 | break; |
19 | 124 | case 0x55: // EoT |
125 | return HPSerial::STATE_IDLE; | |
126 | break; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
127 | case 0x66: |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
128 | send_ack(0x99); |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
129 | return HPSerial::STATE_COMMAND; |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
130 | break; |
19 | 131 | case 0xFF: |
132 | return HPSerial::STATE_IDLE; | |
133 | default: // unknown value | |
134 | send_ack(0xFF); | |
135 | return HPSerial::STATE_IDLE; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
136 | } |
5 | 137 | } |
138 | ||
19 | 139 | HPSerial::state_t HPSerial::do_state_command(uint8_t c) |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
140 | { |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
141 | if (c == 0x55) { // EoT |
19 | 142 | return STATE_IDLE; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
143 | } |
5 | 144 | |
19 | 145 | tr_data.cmd = c; |
146 | tr_data.size = 0; | |
147 | tr_data.pos = 0; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
148 | send_ack(0x00); |
19 | 149 | |
150 | if (c == 0x86) { // shutdown | |
151 | pushCmd(tr_data.cmd, tr_data.size, tr_data.payload); | |
152 | return HPSerial::STATE_IDLE; | |
21 | 153 | } |
19 | 154 | return STATE_PAYLOAD_SIZE; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
155 | } |
5 | 156 | |
19 | 157 | HPSerial::state_t HPSerial::do_state_payload_size(uint8_t c) |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
158 | { |
19 | 159 | tr_data.size = c; |
160 | tr_data.pos = 0; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
161 | send_ack(0x00); |
19 | 162 | return STATE_PAYLOAD; |
5 | 163 | } |
164 | ||
19 | 165 | HPSerial::state_t HPSerial::do_state_payload(uint8_t c) |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
166 | { |
19 | 167 | tr_data.payload[tr_data.pos++] = c; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
168 | send_ack(0x00); |
19 | 169 | if (tr_data.pos >= tr_data.size) { |
170 | pushCmd(tr_data.cmd, tr_data.size, tr_data.payload); | |
171 | return HPSerial::STATE_IDLE; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
172 | } |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
173 | return HPSerial::STATE_PAYLOAD; |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
174 | } |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
175 | |
19 | 176 | HPSerial::state_t HPSerial::do_state_sending(uint8_t c) |
177 | { | |
178 | // ghee | |
179 | if (c == 0xFF) | |
21 | 180 | { // resend current char |
181 | tr_data.pos--; | |
182 | tr_data.payload[tr_data.pos] += 1; | |
183 | } | |
19 | 184 | // TODO: check ACK values (c is the received ack) |
185 | if (tr_data.pos >= tr_data.size) | |
21 | 186 | { |
187 | return do_state_eot(); | |
188 | } | |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
189 | serial.write(&tr_data.payload[tr_data.pos++], 1); |
19 | 190 | set_timer(RXTIMEOUT); |
191 | return HPSerial::STATE_SENDING; | |
192 | } | |
193 | ||
194 | HPSerial::state_t HPSerial::do_state_eot(uint8_t c) | |
195 | { | |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
196 | serial.write(&c, 1); // EoT |
21 | 197 | cur_gstate = GSTATE_IDLE; |
198 | set_timer(); // We are IDLE, detach the timeouter | |
19 | 199 | return STATE_IDLE; |
200 | } | |
201 | ||
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
202 | HPSerial::state_t HPSerial::run_state(HPSerial::state_t cur_state, |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
203 | uint8_t c) |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
204 | { |
19 | 205 | return (this->*(HPSerial::state_table[cur_state]))(c); |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
206 | }; |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
207 | |
5 | 208 | void HPSerial::rxIrq(void) { |
209 | uint8_t val; | |
21 | 210 | if(serial.readable()) |
211 | { // no reason why we would end here without | |
212 | // this condition, but hey | |
8 | 213 | #ifdef DEBUG2 |
5 | 214 | inrx=1; |
215 | #endif | |
21 | 216 | //lled = 1; |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
217 | //val = serial.getc(); |
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
218 | serial.read(&val, 1); |
19 | 219 | cur_state = run_state(cur_state, val); |
21 | 220 | //lled = 0; |
8 | 221 | #ifdef DEBUG2 |
5 | 222 | inrx=0; |
223 | #endif | |
224 | } | |
225 | } | |
226 | ||
227 | ||
228 | void HPSerial::timeout(void) { | |
19 | 229 | set_timer(); // detach the timeouter |
230 | if (cur_gstate == GSTATE_STARTING) | |
231 | _startup(); | |
232 | else // reset | |
233 | { | |
234 | cur_gstate = GSTATE_IDLE; | |
235 | cur_state = STATE_IDLE; | |
236 | } | |
5 | 237 | } |