Sun, 25 Oct 2020 17:02:53 +0100
Update the custom target HP34970_FP_303RD
update the platformio.ini:
remove unused flags, and configure so it uses includes from the project's root
directory (in TARGET_HP34970_FP_F303RD) so these .h/.c files do not conflict
when compiling for other targets.
Add a TARGET_HP34970_FR_F303RD/ for target specific headers/linker files.
Add a src/TARGET_HP34970_FR_F303RD/ for c files (protected by '#if defined'
statments).
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 | |
19 | 16 | #define RXTIMEOUT 0.05 |
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 | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
34 | HPSerial::HPSerial(PinName tx, PinName rx): |
19 | 35 | serial(tx, rx), |
36 | cur_gstate(GSTATE_IDLE), | |
37 | ncmd(0) | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
38 | { |
21 | 39 | serial.baud(187500); |
19 | 40 | cur_state = STATE_IDLE; |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
41 | serial.attach(callback(this, &HPSerial::rxIrq), SerialBase::RxIrq); |
19 | 42 | } |
43 | ||
44 | void HPSerial::startup(void) { | |
21 | 45 | cur_gstate = GSTATE_STARTING; |
19 | 46 | set_timer(0.002); // launch the startup in 10ms |
47 | } | |
48 | ||
49 | void HPSerial::_startup(void) | |
50 | { | |
51 | cur_gstate = GSTATE_STARTING; | |
52 | tr_data.size = sizeof(startup_seq); | |
53 | tr_data.cmd = 0xFF; | |
54 | tr_data.pos = 0; | |
55 | for(uint8_t i=0; i<tr_data.size; i++) | |
56 | tr_data.payload[i] = startup_seq[i]; | |
57 | cur_state = do_state_sending(); | |
5 | 58 | } |
59 | ||
19 | 60 | void HPSerial::send(const uint8_t *buf, uint8_t size) |
61 | { | |
62 | // tx_data-> | |
63 | //send(startup, sizeof(startup)/sizeof(startup[0])); | |
64 | } | |
65 | ||
66 | void HPSerial::sendkey(uint8_t keycode) | |
67 | { | |
68 | tr_data.size = 2; | |
69 | tr_data.cmd = 0xFF; | |
70 | tr_data.pos = 0; | |
71 | tr_data.payload[0] = 0x66; | |
72 | tr_data.payload[1] = keycode; | |
73 | cur_state = do_state_sending(); | |
74 | } | |
75 | ||
76 | bool HPSerial::cmd_available(void) | |
77 | { | |
5 | 78 | return !cmdbuf.empty(); |
79 | } | |
80 | ||
19 | 81 | bool HPSerial::pop(CMD& cmd) |
82 | { | |
5 | 83 | return cmdbuf.pop(cmd); |
84 | } | |
85 | ||
19 | 86 | bool HPSerial::cmd_buf_full(void) |
87 | { | |
5 | 88 | return cmdbuf.full(); |
89 | } | |
90 | ||
19 | 91 | unsigned int HPSerial::nerrors(uint8_t errorno) |
21 | 92 | { |
5 | 93 | return errs[errorno]; |
94 | } | |
95 | ||
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
96 | void HPSerial::pushCmd(uint8_t cmd, uint8_t size, char *payload) { |
5 | 97 | CMD val; |
98 | uint8_t i; | |
99 | val.id = ncmd++; | |
100 | val.cmd = cmd; | |
101 | val.size = size; | |
102 | for(i=0; i<size; i++) | |
103 | val.value[i] = payload[i]; | |
104 | val.value[i] = 0x00; | |
105 | cmdbuf.push(val); | |
106 | } | |
107 | ||
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
108 | 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
|
109 | serial.write(&c, 1); |
19 | 110 | 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
|
111 | } |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
112 | |
19 | 113 | 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
|
114 | { |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
115 | // 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
|
116 | // knwon handcheck values are 0x66 and 0x33 |
19 | 117 | 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
|
118 | switch (c) { |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
119 | case 0x33: |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
120 | send_ack(0xCC); |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
121 | return HPSerial::STATE_PAYLOAD_SIZE; |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
122 | break; |
19 | 123 | case 0x55: // EoT |
124 | return HPSerial::STATE_IDLE; | |
125 | break; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
126 | case 0x66: |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
127 | send_ack(0x99); |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
128 | return HPSerial::STATE_COMMAND; |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
129 | break; |
19 | 130 | case 0xFF: |
131 | return HPSerial::STATE_IDLE; | |
132 | default: // unknown value | |
133 | send_ack(0xFF); | |
134 | return HPSerial::STATE_IDLE; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
135 | } |
5 | 136 | } |
137 | ||
19 | 138 | 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
|
139 | { |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
140 | if (c == 0x55) { // EoT |
19 | 141 | return STATE_IDLE; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
142 | } |
5 | 143 | |
19 | 144 | tr_data.cmd = c; |
145 | tr_data.size = 0; | |
146 | tr_data.pos = 0; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
147 | send_ack(0x00); |
19 | 148 | |
149 | if (c == 0x86) { // shutdown | |
150 | pushCmd(tr_data.cmd, tr_data.size, tr_data.payload); | |
151 | return HPSerial::STATE_IDLE; | |
21 | 152 | } |
19 | 153 | return STATE_PAYLOAD_SIZE; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
154 | } |
5 | 155 | |
19 | 156 | 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
|
157 | { |
19 | 158 | tr_data.size = c; |
159 | tr_data.pos = 0; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
160 | send_ack(0x00); |
19 | 161 | return STATE_PAYLOAD; |
5 | 162 | } |
163 | ||
19 | 164 | 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
|
165 | { |
19 | 166 | 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
|
167 | send_ack(0x00); |
19 | 168 | if (tr_data.pos >= tr_data.size) { |
169 | pushCmd(tr_data.cmd, tr_data.size, tr_data.payload); | |
170 | return HPSerial::STATE_IDLE; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
171 | } |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
172 | return HPSerial::STATE_PAYLOAD; |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
173 | } |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
174 | |
19 | 175 | HPSerial::state_t HPSerial::do_state_sending(uint8_t c) |
176 | { | |
177 | // ghee | |
178 | if (c == 0xFF) | |
21 | 179 | { // resend current char |
180 | tr_data.pos--; | |
181 | tr_data.payload[tr_data.pos] += 1; | |
182 | } | |
19 | 183 | // TODO: check ACK values (c is the received ack) |
184 | if (tr_data.pos >= tr_data.size) | |
21 | 185 | { |
186 | return do_state_eot(); | |
187 | } | |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
188 | serial.write(&tr_data.payload[tr_data.pos++], 1); |
19 | 189 | set_timer(RXTIMEOUT); |
190 | return HPSerial::STATE_SENDING; | |
191 | } | |
192 | ||
193 | HPSerial::state_t HPSerial::do_state_eot(uint8_t c) | |
194 | { | |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
195 | serial.write(&c, 1); // EoT |
21 | 196 | cur_gstate = GSTATE_IDLE; |
197 | set_timer(); // We are IDLE, detach the timeouter | |
19 | 198 | return STATE_IDLE; |
199 | } | |
200 | ||
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
201 | 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
|
202 | uint8_t c) |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
203 | { |
19 | 204 | 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
|
205 | }; |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
206 | |
5 | 207 | void HPSerial::rxIrq(void) { |
208 | uint8_t val; | |
21 | 209 | if(serial.readable()) |
210 | { // no reason why we would end here without | |
211 | // this condition, but hey | |
8 | 212 | #ifdef DEBUG2 |
5 | 213 | inrx=1; |
214 | #endif | |
21 | 215 | //lled = 1; |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
216 | //val = serial.getc(); |
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
217 | serial.read(&val, 1); |
19 | 218 | cur_state = run_state(cur_state, val); |
21 | 219 | //lled = 0; |
8 | 220 | #ifdef DEBUG2 |
5 | 221 | inrx=0; |
222 | #endif | |
223 | } | |
224 | } | |
225 | ||
226 | ||
227 | void HPSerial::timeout(void) { | |
19 | 228 | set_timer(); // detach the timeouter |
229 | if (cur_gstate == GSTATE_STARTING) | |
230 | _startup(); | |
231 | else // reset | |
232 | { | |
233 | cur_gstate = GSTATE_IDLE; | |
234 | cur_state = STATE_IDLE; | |
235 | } | |
5 | 236 | } |