Tue, 11 Oct 2016 00:14:20 +0200
pff so many stuff
let's break everything again
5 | 1 | #include "hp34comm.h" |
2 | #include "mbed.h" | |
3 | #include "CircularBuffer.h" | |
4 | ||
5 | /***** HP 34970A communication class ***/ | |
6 | ||
8 | 7 | #ifdef DEBUG2 |
5 | 8 | |
8 | 9 | DigitalOut inrx(D9); |
10 | DigitalOut intx(D5); | |
11 | DigitalOut ack(D6); | |
12 | DigitalOut staterx(D7); | |
13 | DigitalOut statetx(D8); | |
5 | 14 | |
15 | #endif | |
16 | ||
8 | 17 | DigitalOut lled(LED1); |
5 | 18 | |
8 | 19 | HPSerial::HPSerial(PinName rx, PinName tx): |
20 | serial_tx(A7, tx), | |
21 | serial_rx(NC, rx), | |
22 | ncmd(0) { | |
5 | 23 | |
24 | //pc.printf("HPSerial init\n"); | |
25 | ||
26 | for(uint8_t i=0; i<MAX_ERRS; i++) | |
27 | errs[i] = 0; | |
28 | reset(); | |
29 | ||
30 | serial_tx.baud(187500); | |
31 | serial_tx.attach(this, &HPSerial::txIrq, Serial::RxIrq); //sic! | |
32 | ||
33 | serial_rx.baud(187500); | |
34 | serial_rx.attach(this, &HPSerial::rxIrq, Serial::RxIrq); | |
35 | } | |
36 | ||
37 | bool HPSerial::cmd_available(void) { | |
38 | return !cmdbuf.empty(); | |
39 | } | |
40 | ||
41 | bool HPSerial::pop(CMD& cmd) { | |
42 | return cmdbuf.pop(cmd); | |
43 | } | |
44 | ||
45 | bool HPSerial::cmd_buf_full(void) { | |
46 | return cmdbuf.full(); | |
47 | } | |
48 | ||
49 | unsigned int HPSerial::nerrors(uint8_t errorno) { | |
50 | return errs[errorno]; | |
51 | } | |
52 | ||
53 | ||
54 | ||
55 | void HPSerial::reset(uint8_t errorno) { | |
56 | timeouter.detach(); | |
57 | head = 0; | |
58 | tx_state = Idle; | |
59 | setstatedbg(); | |
60 | tx_cmd = 0xFF; | |
61 | tx_ack = false; | |
62 | tx_len = 0xFF; | |
63 | memset(buf, 0, BUF_SIZE); | |
64 | if (errorno != 0xFF){ | |
65 | errs[errorno]++; | |
66 | } | |
67 | ||
68 | } | |
69 | ||
70 | void HPSerial::handleAck(uint8_t val) { | |
71 | if ((tx_state == Rx) & (tx_cmd == 0xFF) & (tx_ack == false) & (val == 0x66)) { | |
72 | // special case: keypad does not acknwledge and takes precedence | |
73 | // on the "bus" | |
74 | tx_state = Tx; | |
75 | setstatedbg(); | |
76 | } else | |
77 | ||
78 | if (tx_ack == true) | |
79 | reset(0); | |
80 | ||
81 | else | |
82 | if (tx_cmd == 0xFF) // still at the beginning of a packet, expect 0x99 as ack | |
83 | if (val == 0x99) | |
84 | { | |
85 | tx_ack = true; | |
8 | 86 | #ifdef DEBUG2 |
5 | 87 | ack = 1; |
88 | wait_us(2); | |
89 | ack = 0; | |
90 | #endif | |
91 | } | |
92 | else | |
93 | reset(1); | |
94 | ||
95 | else // expect 0x00 as ack | |
96 | if (val == 0x00) | |
97 | { | |
98 | tx_ack = true; | |
8 | 99 | #ifdef DEBUG2 |
5 | 100 | ack = 1; |
101 | wait_us(2); | |
102 | ack = 0; | |
103 | wait_us(2); | |
104 | ack = 1; | |
105 | wait_us(2); | |
106 | ack = 0; | |
107 | #endif | |
108 | } | |
109 | else | |
110 | reset(2); | |
111 | } | |
112 | ||
113 | void HPSerial::pushCmd(TrState direction, uint8_t cmd, uint8_t size, char *payload) { | |
114 | CMD val; | |
115 | uint8_t i; | |
116 | val.id = ncmd++; | |
117 | val.direction = direction; | |
118 | val.cmd = cmd; | |
119 | val.size = size; | |
120 | for(i=0; i<size; i++) | |
121 | val.value[i] = payload[i]; | |
122 | val.value[i] = 0x00; | |
123 | cmdbuf.push(val); | |
124 | } | |
125 | ||
126 | void HPSerial::handleChar(uint8_t val) { | |
127 | if (tx_ack == false) | |
128 | reset(3); | |
129 | else // remaining of the state machine | |
130 | if (tx_cmd == 0xFF) { | |
131 | // begin of transmission, expect a cmd | |
132 | tx_cmd = val; | |
133 | tx_ack = false; | |
134 | if (tx_state == Rx) | |
135 | tx_len = 0xFF; | |
136 | else | |
137 | tx_len = 0x00; // no payload: tx_cmd is the key stroke | |
138 | } | |
139 | else if (tx_len == 0xFF) { | |
140 | // got a cmd, expect a payload size | |
141 | tx_len = val; | |
142 | tx_ack = false; | |
143 | } | |
144 | else if (tx_len > 0) { | |
145 | // a payload char | |
146 | buf[head++] = val; | |
147 | tx_len--; | |
148 | tx_ack = false; | |
149 | } | |
150 | else { // end of payload, manage sent content | |
151 | uint8_t cur_state = tx_state; | |
8 | 152 | pushCmd((TrState)tx_state, tx_cmd, head, (char*)buf); |
5 | 153 | |
154 | switch(val) { | |
155 | case 0x66: // a new transmisson | |
156 | reset(); | |
157 | tx_state = cur_state; | |
158 | setstatedbg(); | |
159 | break; | |
160 | case 0x55: | |
161 | reset(); | |
162 | break; | |
163 | default: | |
164 | reset(6); | |
165 | break; | |
166 | } | |
167 | } | |
168 | } | |
169 | ||
170 | ||
171 | ||
172 | void HPSerial::setstatedbg(void) { | |
8 | 173 | #ifdef DEBUG2 |
5 | 174 | if (tx_state == Rx) |
8 | 175 | staterx = 1; |
5 | 176 | else |
8 | 177 | staterx = 0; |
5 | 178 | if (tx_state == Tx) |
8 | 179 | statetx = 1; |
5 | 180 | else |
8 | 181 | statetx = 0; |
182 | #endif | |
5 | 183 | } |
184 | ||
185 | void HPSerial::rxIrq(void) { | |
186 | uint8_t val; | |
187 | if(serial_rx.readable()) { // no reason why we would end here without this condition, but hey | |
8 | 188 | #ifdef DEBUG2 |
5 | 189 | inrx=1; |
190 | #endif | |
8 | 191 | lled = 1; |
5 | 192 | val = serial_rx.getc(); |
193 | ||
194 | timeouter.attach(this, &HPSerial::timeout, 0.001); // if nothing else happen in the next ms, reset | |
195 | ||
196 | if (tx_state == Idle) | |
197 | if (val == 0x66) { | |
198 | // no transmission in progress, expect a start of transmission | |
199 | tx_state = Rx; | |
200 | tx_ack = false; | |
201 | setstatedbg(); | |
202 | } | |
8 | 203 | else { |
5 | 204 | reset(4); |
8 | 205 | } |
5 | 206 | else if (tx_state == Tx) // manage the acks |
207 | handleAck(val); | |
208 | ||
209 | else | |
210 | handleChar(val); | |
8 | 211 | lled = 0; |
212 | #ifdef DEBUG2 | |
5 | 213 | inrx=0; |
214 | #endif | |
215 | } | |
216 | } | |
217 | ||
218 | void HPSerial::txIrq(void) { | |
219 | uint8_t val; | |
220 | if(serial_tx.readable()) { // no reason why we would end here without this condition, but hey | |
8 | 221 | #ifdef DEBUG2 |
5 | 222 | intx=1; |
223 | #endif | |
224 | val = serial_tx.getc(); | |
225 | ||
226 | timeouter.attach(this, &HPSerial::timeout, 0.001); // if nothing else happen in the next ms, reset | |
227 | ||
228 | if (tx_state == Idle) | |
229 | if (val == 0x66) { | |
230 | // no transmission in progress, expect a start of transmission | |
231 | tx_state = Tx; | |
232 | tx_ack = false; | |
233 | setstatedbg(); | |
234 | } | |
8 | 235 | else { |
5 | 236 | reset(5); |
8 | 237 | } |
5 | 238 | |
239 | else if (tx_state == Rx) // manage the acks | |
240 | handleAck(val); | |
241 | ||
242 | else | |
243 | handleChar(val); | |
244 | } | |
8 | 245 | #ifdef DEBUG2 |
5 | 246 | intx=0; |
247 | #endif | |
248 | } | |
249 | ||
250 | void HPSerial::timeout(void) { | |
251 | if (tx_state != Idle) { | |
252 | reset(7); | |
253 | } | |
254 | } |