Mon, 23 Nov 2020 21:55:40 +0100
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 | 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 | 5 | |
6 | /***** HP 34970A communication class ***/ | |
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 | 9 | #define STARTUPRETRY 0.5 |
21 | 10 | |
19 | 11 | HPSerial::statemethod HPSerial::state_table[NUM_STATES] = { |
44 | 12 | &HPSerial::do_state_initial, // STATE_IDLE |
13 | &HPSerial::do_state_command, // STATE_COMMAND | |
14 | &HPSerial::do_state_payload_size, // STATE_PAYLOAD_SIZE | |
15 | &HPSerial::do_state_payload, // STATE_PAYLOAD | |
16 | &HPSerial::do_state_sending, // STATE_SENDING | |
19 | 17 | }; |
5 | 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 | 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 | 25 | serial.baud(187500); |
44 | 26 | serial.format(8, BufferedSerial::Even, 1); |
19 | 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 | 29 | serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq); |
19 | 30 | } |
31 | ||
44 | 32 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
33 | // SEND related methods |
19 | 34 | void HPSerial::sendkey(uint8_t keycode) |
35 | { | |
44 | 36 | if (!sendbuf.full()) |
37 | sendbuf.push(keycode); | |
38 | } | |
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 | 156 | |
157 | bool HPSerial::cmd_available(void) | |
158 | { | |
5 | 159 | return !cmdbuf.empty(); |
160 | } | |
161 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
162 | |
19 | 163 | bool HPSerial::pop(CMD& cmd) |
164 | { | |
5 | 165 | return cmdbuf.pop(cmd); |
166 | } | |
167 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
168 | |
19 | 169 | bool HPSerial::cmd_buf_full(void) |
170 | { | |
5 | 171 | return cmdbuf.full(); |
172 | } | |
173 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
174 | |
19 | 175 | unsigned int HPSerial::nerrors(uint8_t errorno) |
21 | 176 | { |
5 | 177 | return errs[errorno]; |
178 | } | |
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 | 182 | CMD val; |
183 | uint8_t i; | |
184 | val.id = ncmd++; | |
185 | val.cmd = cmd; | |
186 | val.size = size; | |
187 | for(i=0; i<size; i++) | |
188 | val.value[i] = payload[i]; | |
189 | val.value[i] = 0x00; | |
190 | cmdbuf.push(val); | |
191 | } | |
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 | 195 | serial.write(&c, 1); |
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 | 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 | 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 | 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 | 210 | case 0x55: // EoT |
211 | return HPSerial::STATE_IDLE; | |
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 | 217 | case 0xFF: |
218 | return HPSerial::STATE_IDLE; | |
219 | default: // unknown value | |
220 | send_ack(0xFF); | |
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 | 223 | } |
224 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
225 | |
19 | 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 | 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 | 231 | |
19 | 232 | tr_data.cmd = c; |
233 | tr_data.size = 0; | |
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 | 236 | |
237 | if (c == 0x86) { // shutdown | |
238 | pushCmd(tr_data.cmd, tr_data.size, tr_data.payload); | |
239 | return HPSerial::STATE_IDLE; | |
21 | 240 | } |
19 | 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 | 243 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
244 | |
19 | 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 | 247 | tr_data.size = c; |
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 | 250 | return STATE_PAYLOAD; |
5 | 251 | } |
252 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
253 | |
19 | 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 | 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 | 258 | if (tr_data.pos >= tr_data.size) { |
259 | pushCmd(tr_data.cmd, tr_data.size, tr_data.payload); | |
44 | 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 | 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 | 266 | HPSerial::state_t HPSerial::do_state_sending(uint8_t c) |
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 | 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 | 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 | 296 | // TODO: check ACK values (c is the received ack) |
297 | if (tr_data.pos >= tr_data.size) | |
21 | 298 | { |
44 | 299 | c = 0x55; |
300 | serial.write(&c, 1); // EoT | |
301 | cur_gstate = GSTATE_IDLE; | |
302 | set_timer(); // We are IDLE, detach the timeouter | |
303 | return STATE_IDLE; | |
21 | 304 | } |
44 | 305 | else |
306 | { | |
307 | serial.write(&tr_data.payload[tr_data.pos++], 1); | |
308 | set_timer(RXTIMEOUT); | |
309 | return STATE_SENDING; | |
310 | } | |
19 | 311 | } |
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 | 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 | 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 | 321 | void HPSerial::rx_irq(void) { |
5 | 322 | uint8_t val; |
21 | 323 | if(serial.readable()) |
324 | { // no reason why we would end here without | |
325 | // this condition, but hey | |
44 | 326 | if (cur_gstate == GSTATE_IDLE) |
327 | // occurs when the CPU starts a new transmission | |
328 | // at this point, cur_state should be STATE_IDLE also (TODO add a check?) | |
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 | 331 | cur_state = run_state(cur_state, val); |
5 | 332 | } |
333 | } | |
334 | ||
335 | ||
336 | void HPSerial::timeout(void) { | |
19 | 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 | 340 | } |