2 #include "mbed.h" |
2 #include "mbed.h" |
3 #include "CircularBuffer.h" |
3 #include "CircularBuffer.h" |
4 |
4 |
5 /***** HP 34970A communication class ***/ |
5 /***** HP 34970A communication class ***/ |
6 |
6 |
7 #define HP_SERIAL_TX PC_7 // serial6 RX |
7 #ifdef DEBUG2 |
8 #define HP_SERIAL_RX PA_1 // serial4 RX |
8 |
9 |
9 DigitalOut inrx(D9); |
10 #ifdef DEBUG |
10 DigitalOut intx(D5); |
11 |
11 DigitalOut ack(D6); |
12 DigitalOut inrx(PC_0); |
12 DigitalOut staterx(D7); |
13 DigitalOut intx(PC_1); |
13 DigitalOut statetx(D8); |
14 DigitalOut ack(PC_3); |
14 |
15 DigitalOut staterx(PC_2); |
15 #endif |
16 DigitalOut statetx(PH_1); |
16 |
17 |
17 DigitalOut lled(LED1); |
18 #endif |
18 |
19 |
19 HPSerial::HPSerial(PinName rx, PinName tx): |
20 |
20 serial_tx(A7, tx), |
21 HPSerial::HPSerial(): |
21 serial_rx(NC, rx), |
22 ncmd(0), |
22 ncmd(0) { |
23 serial_tx(NC, HP_SERIAL_TX), serial_rx(NC, HP_SERIAL_RX) { |
|
24 |
23 |
25 //pc.printf("HPSerial init\n"); |
24 //pc.printf("HPSerial init\n"); |
26 |
25 |
27 for(uint8_t i=0; i<MAX_ERRS; i++) |
26 for(uint8_t i=0; i<MAX_ERRS; i++) |
28 errs[i] = 0; |
27 errs[i] = 0; |
148 tx_len--; |
147 tx_len--; |
149 tx_ack = false; |
148 tx_ack = false; |
150 } |
149 } |
151 else { // end of payload, manage sent content |
150 else { // end of payload, manage sent content |
152 uint8_t cur_state = tx_state; |
151 uint8_t cur_state = tx_state; |
153 pushCmd(tx_state, tx_cmd, head, buf); |
152 pushCmd((TrState)tx_state, tx_cmd, head, (char*)buf); |
154 |
153 |
155 switch(val) { |
154 switch(val) { |
156 case 0x66: // a new transmisson |
155 case 0x66: // a new transmisson |
157 reset(); |
156 reset(); |
158 tx_state = cur_state; |
157 tx_state = cur_state; |
169 } |
168 } |
170 |
169 |
171 |
170 |
172 |
171 |
173 void HPSerial::setstatedbg(void) { |
172 void HPSerial::setstatedbg(void) { |
174 /* |
173 #ifdef DEBUG2 |
175 if (tx_state == Rx) |
174 if (tx_state == Rx) |
176 staterx = 1; |
175 staterx = 1; |
177 else |
176 else |
178 staterx = 0; |
177 staterx = 0; |
179 if (tx_state == Tx) |
178 if (tx_state == Tx) |
180 statetx = 1; |
179 statetx = 1; |
181 else |
180 else |
182 statetx = 0; |
181 statetx = 0; |
183 */ |
182 #endif |
184 } |
183 } |
185 |
184 |
186 void HPSerial::rxIrq(void) { |
185 void HPSerial::rxIrq(void) { |
187 uint8_t val; |
186 uint8_t val; |
188 if(serial_rx.readable()) { // no reason why we would end here without this condition, but hey |
187 if(serial_rx.readable()) { // no reason why we would end here without this condition, but hey |
189 #ifdef DEBUG |
188 #ifdef DEBUG2 |
190 inrx=1; |
189 inrx=1; |
191 #endif |
190 #endif |
|
191 lled = 1; |
192 val = serial_rx.getc(); |
192 val = serial_rx.getc(); |
193 |
193 |
194 timeouter.attach(this, &HPSerial::timeout, 0.001); // if nothing else happen in the next ms, reset |
194 timeouter.attach(this, &HPSerial::timeout, 0.001); // if nothing else happen in the next ms, reset |
195 |
195 |
196 if (tx_state == Idle) |
196 if (tx_state == Idle) |
198 // no transmission in progress, expect a start of transmission |
198 // no transmission in progress, expect a start of transmission |
199 tx_state = Rx; |
199 tx_state = Rx; |
200 tx_ack = false; |
200 tx_ack = false; |
201 setstatedbg(); |
201 setstatedbg(); |
202 } |
202 } |
203 else |
203 else { |
204 reset(4); |
204 reset(4); |
205 |
205 } |
206 else if (tx_state == Tx) // manage the acks |
206 else if (tx_state == Tx) // manage the acks |
207 handleAck(val); |
207 handleAck(val); |
208 |
208 |
209 else |
209 else |
210 handleChar(val); |
210 handleChar(val); |
211 #ifdef DEBUG |
211 lled = 0; |
|
212 #ifdef DEBUG2 |
212 inrx=0; |
213 inrx=0; |
213 #endif |
214 #endif |
214 } |
215 } |
215 } |
216 } |
216 |
217 |
217 void HPSerial::txIrq(void) { |
218 void HPSerial::txIrq(void) { |
218 uint8_t val; |
219 uint8_t val; |
219 if(serial_tx.readable()) { // no reason why we would end here without this condition, but hey |
220 if(serial_tx.readable()) { // no reason why we would end here without this condition, but hey |
220 #ifdef DEBUG |
221 #ifdef DEBUG2 |
221 intx=1; |
222 intx=1; |
222 #endif |
223 #endif |
223 val = serial_tx.getc(); |
224 val = serial_tx.getc(); |
224 |
225 |
225 timeouter.attach(this, &HPSerial::timeout, 0.001); // if nothing else happen in the next ms, reset |
226 timeouter.attach(this, &HPSerial::timeout, 0.001); // if nothing else happen in the next ms, reset |