src/hp34comm.cpp

Mon, 23 Nov 2020 21:55:40 +0100

author
David Douard <david.douard@sdf3.org>
date
Mon, 23 Nov 2020 21:55:40 +0100
changeset 57
cce151e4bf3d
parent 50
279868684eb3
child 66
48f29a1d43d6
permissions
-rw-r--r--

Small fixes in main

- toggle the shift indicator when the key is pressed while already on
- attempt to fix the restart-while-shuting-down bug

5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
1 #include "hp34comm.h"
28
424d792fea4f compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents: 21
diff changeset
2
424d792fea4f compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents: 21
diff changeset
3 #include <mbed.h>
424d792fea4f compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents: 21
diff changeset
4 #include <CircularBuffer.h>
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
6 /***** HP 34970A communication class ***/
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
7
37
07e8ca2bdf6d Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents: 28
diff changeset
8 #define RXTIMEOUT 50ms
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
9 #define STARTUPRETRY 0.5
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
10
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
11 HPSerial::statemethod HPSerial::state_table[NUM_STATES] = {
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
12 &HPSerial::do_state_initial, // STATE_IDLE
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
13 &HPSerial::do_state_command, // STATE_COMMAND
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
14 &HPSerial::do_state_payload_size, // STATE_PAYLOAD_SIZE
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
15 &HPSerial::do_state_payload, // STATE_PAYLOAD
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
16 &HPSerial::do_state_sending, // STATE_SENDING
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
17 };
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
18
37
07e8ca2bdf6d Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents: 28
diff changeset
19
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
20 HPSerial::HPSerial(PinName tx, PinName rx):
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
21 serial(tx, rx),
37
07e8ca2bdf6d Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents: 28
diff changeset
22 ncmd(0),
07e8ca2bdf6d Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents: 28
diff changeset
23 cur_gstate(GSTATE_IDLE)
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
24 {
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
25 serial.baud(187500);
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
26 serial.format(8, BufferedSerial::Even, 1);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
27 cur_state = STATE_IDLE;
49
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
28 send_thread.start(callback(this, &HPSerial::send_pending_key));
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
29 serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
30 }
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
31
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
32
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
33 // SEND related methods
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
34 void HPSerial::sendkey(uint8_t keycode)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
35 {
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
36 if (!sendbuf.full())
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
37 sendbuf.push(keycode);
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
38 }
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
39
49
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
40
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
41 bool HPSerial::wait_for(uint8_t value)
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
42 { // wait for an expected character in the serial port
49
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
43 char c;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
44
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
45 for(uint8_t i=0; i<2; i++)
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
46 {
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
47 while(!serial.readable())
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
48 wait_us(10);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
49 serial.read(&c, 1);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
50 if (value == c)
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
51 return true;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
52 }
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
53 return false;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
54 }
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
55
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
56
49
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
57 void HPSerial::send_pending_key() {
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
58 uint8_t c;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
59
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
60 while(true)
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
61 {
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
62 if (!sendbuf.empty())
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
63 {
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
64 if (cur_gstate == GSTATE_IDLE)
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
65 {
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
66 serial.attach(0, SerialBase::RxIrq);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
67 cur_gstate = GSTATE_TX;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
68
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
69 c = 0x66;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
70 serial.write(&c, 1);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
71 if (!wait_for(0x99)) {}
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
72 // break; // XXX what to do?
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
73
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
74 sendbuf.pop(c);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
75 serial.write(&c, 1);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
76 if (!wait_for(0x00)) {}
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
77
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
78 c = 0x55;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
79 serial.write(&c, 1);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
80 cur_gstate = GSTATE_IDLE;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
81 serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
82 }
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
83 }
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
84 //else // prevent from flooding the main unit
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
85 ThisThread::sleep_for(5ms);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
86 }
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
87 }
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
88
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
89
49
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
90 void HPSerial::send_startup_seq(uint8_t keycode) {
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
91 uint8_t c;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
92
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
93 while (cur_gstate != GSTATE_IDLE) {
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
94 ThisThread::sleep_for(1ms);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
95 }
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
96
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
97 serial.attach(0, SerialBase::RxIrq);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
98 cur_gstate = GSTATE_TX;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
99
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
100 // Send the init seq 0x33 0x02 0xFF <keycode> 0x55
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
101 c = 0x33;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
102 serial.write(&c, 1);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
103 if (!wait_for(0xCC)) {}
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
104
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
105 c = 0x02;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
106 serial.write(&c, 1);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
107 if (!wait_for(0x00)) {}
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
108
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
109 c = 0xFF;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
110 serial.write(&c, 1);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
111 if (!wait_for(0x00)) {}
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
112
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
113 c = keycode;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
114 serial.write(&c, 1);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
115 if (!wait_for(0x00)) {}
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
116
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
117 c = 0x55;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
118 serial.write(&c, 1);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
119 cur_gstate = GSTATE_IDLE;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
120 serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
121
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
122 }
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
123
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
124
49
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
125 void HPSerial::send_startup_seq() {
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
126 uint8_t c;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
127
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
128 while (cur_gstate != GSTATE_IDLE) {
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
129 ThisThread::sleep_for(1ms);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
130 }
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
131
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
132 serial.attach(0, SerialBase::RxIrq);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
133 cur_gstate = GSTATE_TX;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
134
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
135 // Send the init seq 0x33 0x02 0x00 0x55
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
136 c = 0x33;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
137 serial.write(&c, 1);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
138 if (!wait_for(0xCC)) {}
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
139
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
140 c = 0x02;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
141 serial.write(&c, 1);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
142 if (!wait_for(0x00)) {}
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
143
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
144 c = 0x00;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
145 serial.write(&c, 1);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
146 if (!wait_for(0x00)) {}
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
147
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
148 c = 0x55;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
149 serial.write(&c, 1);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
150 cur_gstate = GSTATE_IDLE;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
151 serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
152 }
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
153
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
154
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
155 // RECV related methods
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
156
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
157 bool HPSerial::cmd_available(void)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
158 {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
159 return !cmdbuf.empty();
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
160 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
161
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
162
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
163 bool HPSerial::pop(CMD& cmd)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
164 {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
165 return cmdbuf.pop(cmd);
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
166 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
167
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
168
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
169 bool HPSerial::cmd_buf_full(void)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
170 {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
171 return cmdbuf.full();
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
172 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
173
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
174
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
175 unsigned int HPSerial::nerrors(uint8_t errorno)
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
176 {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
177 return errs[errorno];
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
178 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
179
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
180
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
181 void HPSerial::pushCmd(uint8_t cmd, uint8_t size, char *payload) {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
182 CMD val;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
183 uint8_t i;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
184 val.id = ncmd++;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
185 val.cmd = cmd;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
186 val.size = size;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
187 for(i=0; i<size; i++)
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
188 val.value[i] = payload[i];
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
189 val.value[i] = 0x00;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
190 cmdbuf.push(val);
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
191 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
192
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
193
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
194 void HPSerial::send_ack(uint8_t c) {
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
195 serial.write(&c, 1);
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
196 set_timer(RXTIMEOUT); // if nothing else happen in the next RXTIMEOUT ms, reset
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
197 }
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
198
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
199
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
200 HPSerial::state_t HPSerial::do_state_initial(uint8_t c)
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
201 {
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
202 // we are idle, incoming char is a handcheck
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
203 // knwon handcheck values are 0x66 and 0x33
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
204 set_timer(RXTIMEOUT); // reset the watchdog
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
205 switch (c) {
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
206 case 0x33: // XXX? when are we expecting a 0x33 here?
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
207 send_ack(0xCC);
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
208 return HPSerial::STATE_PAYLOAD_SIZE;
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
209 break;
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
210 case 0x55: // EoT
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
211 return HPSerial::STATE_IDLE;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
212 break;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
213 case 0x66:
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
214 send_ack(0x99);
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
215 return HPSerial::STATE_COMMAND;
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
216 break;
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
217 case 0xFF:
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
218 return HPSerial::STATE_IDLE;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
219 default: // unknown value
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
220 send_ack(0xFF);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
221 return HPSerial::STATE_IDLE;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
222 }
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
223 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
224
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
225
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
226 HPSerial::state_t HPSerial::do_state_command(uint8_t c)
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
227 {
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
228 if (c == 0x55) { // EoT
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
229 return STATE_IDLE;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
230 }
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
231
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
232 tr_data.cmd = c;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
233 tr_data.size = 0;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
234 tr_data.pos = 0;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
235 send_ack(0x00);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
236
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
237 if (c == 0x86) { // shutdown
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
238 pushCmd(tr_data.cmd, tr_data.size, tr_data.payload);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
239 return HPSerial::STATE_IDLE;
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
240 }
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
241 return STATE_PAYLOAD_SIZE;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
242 }
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
243
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
244
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
245 HPSerial::state_t HPSerial::do_state_payload_size(uint8_t c)
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
246 {
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
247 tr_data.size = c;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
248 tr_data.pos = 0;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
249 send_ack(0x00);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
250 return STATE_PAYLOAD;
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
251 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
252
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
253
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
254 HPSerial::state_t HPSerial::do_state_payload(uint8_t c)
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
255 {
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
256 tr_data.payload[tr_data.pos++] = c;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
257 send_ack(0x00);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
258 if (tr_data.pos >= tr_data.size) {
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
259 pushCmd(tr_data.cmd, tr_data.size, tr_data.payload);
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
260 return STATE_IDLE;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
261 }
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
262 return STATE_PAYLOAD;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
263 }
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
264
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
265
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
266 HPSerial::state_t HPSerial::do_state_sending(uint8_t c)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
267 {
47
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
268 // check the ack value returned by the main unit
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
269
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
270 if ((tr_data.pos == 1) && (tr_data.payload[0] == 0x66))
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
271 {
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
272 if (c != 0x99)
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
273 {
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
274 // did not received the expected ack
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
275 if (c == 0x66)
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
276 {
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
277 // we received a start of transmission while trying to emit something,
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
278 // ignore it, the correct ack should be sent but the main unit just behind...
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
279 set_timer(RXTIMEOUT);
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
280 return cur_state;
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
281 }
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
282 else
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
283 {
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
284 // not sure how this may happen, in doubt, try again
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
285 tr_data.pos--;
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
286 }
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
287 }
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
288 }
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
289 /*
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
290 else if (c != 0x00)
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
291 { // resend current char
47
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
292 tr_data.pos--;
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
293 }
47
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
294 */
11c57010e4f9 Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
295
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
296 // TODO: check ACK values (c is the received ack)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
297 if (tr_data.pos >= tr_data.size)
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
298 {
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
299 c = 0x55;
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
300 serial.write(&c, 1); // EoT
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
301 cur_gstate = GSTATE_IDLE;
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
302 set_timer(); // We are IDLE, detach the timeouter
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
303 return STATE_IDLE;
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
304 }
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
305 else
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
306 {
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
307 serial.write(&tr_data.payload[tr_data.pos++], 1);
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
308 set_timer(RXTIMEOUT);
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
309 return STATE_SENDING;
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
310 }
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
311 }
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
312
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
313
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
314 HPSerial::state_t HPSerial::run_state(HPSerial::state_t cur_state,
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
315 uint8_t c)
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
316 {
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
317 return (this->*(HPSerial::state_table[cur_state]))(c);
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
318 };
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
319
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
320
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
321 void HPSerial::rx_irq(void) {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
322 uint8_t val;
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
323 if(serial.readable())
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
324 { // no reason why we would end here without
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
325 // this condition, but hey
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
326 if (cur_gstate == GSTATE_IDLE)
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
327 // occurs when the CPU starts a new transmission
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
328 // at this point, cur_state should be STATE_IDLE also (TODO add a check?)
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
329 cur_gstate = GSTATE_RX;
28
424d792fea4f compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents: 21
diff changeset
330 serial.read(&val, 1);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
331 cur_state = run_state(cur_state, val);
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
332 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
333 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
334
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
335
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
336 void HPSerial::timeout(void) {
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
337 set_timer(); // detach the timeouter
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
338 cur_gstate = GSTATE_IDLE;
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
339 cur_state = STATE_IDLE;
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
340 }

mercurial