src/hp34comm.cpp

changeset 8
55021f3f1929
parent 5
f1c85c2500f2
child 9
e5254c6aa0c8
equal deleted inserted replaced
7:5cf4034ba4e0 8:55021f3f1929
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;
82 else 81 else
83 if (tx_cmd == 0xFF) // still at the beginning of a packet, expect 0x99 as ack 82 if (tx_cmd == 0xFF) // still at the beginning of a packet, expect 0x99 as ack
84 if (val == 0x99) 83 if (val == 0x99)
85 { 84 {
86 tx_ack = true; 85 tx_ack = true;
87 #ifdef DEBUG 86 #ifdef DEBUG2
88 ack = 1; 87 ack = 1;
89 wait_us(2); 88 wait_us(2);
90 ack = 0; 89 ack = 0;
91 #endif 90 #endif
92 } 91 }
95 94
96 else // expect 0x00 as ack 95 else // expect 0x00 as ack
97 if (val == 0x00) 96 if (val == 0x00)
98 { 97 {
99 tx_ack = true; 98 tx_ack = true;
100 #ifdef DEBUG 99 #ifdef DEBUG2
101 ack = 1; 100 ack = 1;
102 wait_us(2); 101 wait_us(2);
103 ack = 0; 102 ack = 0;
104 wait_us(2); 103 wait_us(2);
105 ack = 1; 104 ack = 1;
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
229 // no transmission in progress, expect a start of transmission 230 // no transmission in progress, expect a start of transmission
230 tx_state = Tx; 231 tx_state = Tx;
231 tx_ack = false; 232 tx_ack = false;
232 setstatedbg(); 233 setstatedbg();
233 } 234 }
234 else 235 else {
235 reset(5); 236 reset(5);
237 }
236 238
237 else if (tx_state == Rx) // manage the acks 239 else if (tx_state == Rx) // manage the acks
238 handleAck(val); 240 handleAck(val);
239 241
240 else 242 else
241 handleChar(val); 243 handleChar(val);
242 } 244 }
243 #ifdef DEBUG 245 #ifdef DEBUG2
244 intx=0; 246 intx=0;
245 #endif 247 #endif
246 } 248 }
247 249
248 void HPSerial::timeout(void) { 250 void HPSerial::timeout(void) {

mercurial