diff -r 55021f3f1929 -r e5254c6aa0c8 src/hp34comm.cpp --- a/src/hp34comm.cpp Tue Oct 11 00:14:20 2016 +0200 +++ b/src/hp34comm.cpp Tue Oct 11 00:46:18 2016 +0200 @@ -17,7 +17,6 @@ DigitalOut lled(LED1); HPSerial::HPSerial(PinName rx, PinName tx): - serial_tx(A7, tx), serial_rx(NC, rx), ncmd(0) { @@ -27,9 +26,6 @@ errs[i] = 0; reset(); - serial_tx.baud(187500); - serial_tx.attach(this, &HPSerial::txIrq, Serial::RxIrq); //sic! - serial_rx.baud(187500); serial_rx.attach(this, &HPSerial::rxIrq, Serial::RxIrq); } @@ -74,40 +70,35 @@ tx_state = Tx; setstatedbg(); } else - - if (tx_ack == true) - reset(0); - - else - if (tx_cmd == 0xFF) // still at the beginning of a packet, expect 0x99 as ack - if (val == 0x99) - { - tx_ack = true; + if (tx_cmd == 0xFF) // beginning of a packet, expect 0x99 as ack + if (val == 0x99) + { + tx_ack = true; #ifdef DEBUG2 - ack = 1; - wait_us(2); - ack = 0; + ack = 1; + wait_us(2); + ack = 0; #endif - } - else - reset(1); + } + else + reset(1); - else // expect 0x00 as ack - if (val == 0x00) - { - tx_ack = true; + else // expect 0x00 as ack + if (val == 0x00) + { + tx_ack = true; #ifdef DEBUG2 - ack = 1; - wait_us(2); - ack = 0; - wait_us(2); - ack = 1; - wait_us(2); - ack = 0; + ack = 1; + wait_us(2); + ack = 0; + wait_us(2); + ack = 1; + wait_us(2); + ack = 0; #endif - } - else - reset(2); + } + else + reset(2); } void HPSerial::pushCmd(TrState direction, uint8_t cmd, uint8_t size, char *payload) { @@ -131,16 +122,19 @@ // begin of transmission, expect a cmd tx_cmd = val; tx_ack = false; - if (tx_state == Rx) - tx_len = 0xFF; - else - tx_len = 0x00; // no payload: tx_cmd is the key stroke + tx_len = 0xFF; } else if (tx_len == 0xFF) { // got a cmd, expect a payload size tx_len = val; tx_ack = false; } + else if (tx_len == 0x55) { + // packet was in fact a keystroke, tx_cmd is in fact the key + // stroke and no payload is expected + pushCmd((TrState)Tx, tx_cmd, 0, NULL); + reset(); + } else if (tx_len > 0) { // a payload char buf[head++] = val; @@ -188,7 +182,7 @@ #ifdef DEBUG2 inrx=1; #endif - lled = 1; + lled = 1; val = serial_rx.getc(); timeouter.attach(this, &HPSerial::timeout, 0.001); // if nothing else happen in the next ms, reset @@ -203,49 +197,17 @@ else { reset(4); } - else if (tx_state == Tx) // manage the acks + else if (tx_ack == false) // manage the acks handleAck(val); - else handleChar(val); - lled = 0; + lled = 0; #ifdef DEBUG2 inrx=0; #endif } } -void HPSerial::txIrq(void) { - uint8_t val; - if(serial_tx.readable()) { // no reason why we would end here without this condition, but hey -#ifdef DEBUG2 - 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 DEBUG2 - intx=0; -#endif -} void HPSerial::timeout(void) { if (tx_state != Idle) {