diff -r 219766126afb -r f1c85c2500f2 src/hp34comm.h~ --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/hp34comm.h~ Tue Sep 20 23:50:45 2016 +0200 @@ -0,0 +1,270 @@ +#ifndef HP34COMM_H +#define HP34COMM_H + +/***** HP 34970A communication class ***/ + +class HPSerial { + +public: + enum TrState { + Idle = 0, + Tx, + Rx, + }; + typedef struct _CMD + { + TrState direction; + uint8_t cmd; + uint8_t size; + char value[MAX_BUFF+1]; + unsigned long id; + } CMD; + + + + HPSerial(): ncmd(0), serial_tx(NC, HP_SERIAL_TX), serial_rx(NC, HP_SERIAL_RX) { + pc.printf("HPSerial init\n"); + for(uint8_t i=0; i 0) { + // a payload char + buf[head++] = val; + tx_len--; + tx_ack = false; + } + else { // end of payload, manage sent content + uint8_t cur_state = tx_state; + pushCmd(tx_state, tx_cmd, head, buf); + + switch(val) { + case 0x66: // a new transmisson + reset(); + tx_state = cur_state; + setstatedbg(); + break; + case 0x55: + reset(); + break; + default: + reset(6); + break; + } + } + } + + + + void setstatedbg(void) { + /* + if (tx_state == Rx) + staterx = 1; + else + staterx = 0; + if (tx_state == Tx) + statetx = 1; + else + statetx = 0; + */ + } + + void rxIrq(void) { + uint8_t val; + if(serial_rx.readable()) { // no reason why we would end here without this condition, but hey +#ifdef DEBUG + inrx=1; +#endif + val = serial_rx.getc(); + + timeouter.attach(this, &HPSerial::timeout, 0.001); // if nothing else happen in the next ms, reset + + if (tx_state == Idle) + if (val == 0x66) { + // no transmission in progress, expect a start of transmission + tx_state = Rx; + tx_ack = false; + setstatedbg(); + } + else + reset(4); + + else if (tx_state == Tx) // manage the acks + handleAck(val); + + else + handleChar(val); +#ifdef DEBUG + inrx=0; +#endif + } + } + + void txIrq(void) { + uint8_t val; + if(serial_tx.readable()) { // no reason why we would end here without this condition, but hey +#ifdef DEBUG + intx=1; +#endif + val = serial_tx.getc(); + + timeouter.attach(this, &HPSerial::timeout, 0.001); // if nothing else happen in the next ms, reset + + if (tx_state == Idle) + if (val == 0x66) { + // no transmission in progress, expect a start of transmission + tx_state = Tx; + tx_ack = false; + setstatedbg(); + } + else + reset(5); + + else if (tx_state == Rx) // manage the acks + handleAck(val); + + else + handleChar(val); + } +#ifdef DEBUG + intx=0; +#endif + } + + void timeout(void) { + if (tx_state != Idle) { + reset(7); + } + } +private: + RawSerial serial_tx; + RawSerial serial_rx; + uint8_t buf[BUF_SIZE]; + uint8_t head; + uint8_t tx_state; + uint8_t tx_cmd; + uint8_t tx_len; + bool tx_ack; + CircularBuffer cmdbuf; + unsigned long ncmd; + unsigned int errs[MAX_ERRS]; + Ticker timeouter; +}; + +#endif