Thu, 07 Oct 2021 21:05:13 +0200
Reduce the sleep time of the send_pending_key thread to 1ms
to get a chance of sending a keycode when the CPU flood the FP with DSP
messages (e.g. after the last 'Last-N' value).
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 | |
66
48f29a1d43d6
Clean several compilation warnings
David Douard <david.douard@sdfa3.org>
parents:
50
diff
changeset
|
33 | void HPSerial::reset(void) |
48f29a1d43d6
Clean several compilation warnings
David Douard <david.douard@sdfa3.org>
parents:
50
diff
changeset
|
34 | { |
48f29a1d43d6
Clean several compilation warnings
David Douard <david.douard@sdfa3.org>
parents:
50
diff
changeset
|
35 | sendbuf.reset(); |
48f29a1d43d6
Clean several compilation warnings
David Douard <david.douard@sdfa3.org>
parents:
50
diff
changeset
|
36 | cmdbuf.reset(); |
48f29a1d43d6
Clean several compilation warnings
David Douard <david.douard@sdfa3.org>
parents:
50
diff
changeset
|
37 | cur_state = STATE_IDLE; |
48f29a1d43d6
Clean several compilation warnings
David Douard <david.douard@sdfa3.org>
parents:
50
diff
changeset
|
38 | cur_gstate = GSTATE_IDLE; |
48f29a1d43d6
Clean several compilation warnings
David Douard <david.douard@sdfa3.org>
parents:
50
diff
changeset
|
39 | } |
48f29a1d43d6
Clean several compilation warnings
David Douard <david.douard@sdfa3.org>
parents:
50
diff
changeset
|
40 | |
48f29a1d43d6
Clean several compilation warnings
David Douard <david.douard@sdfa3.org>
parents:
50
diff
changeset
|
41 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
42 | // SEND related methods |
19 | 43 | void HPSerial::sendkey(uint8_t keycode) |
44 | { | |
44 | 45 | if (!sendbuf.full()) |
46 | sendbuf.push(keycode); | |
47 | } | |
48 | ||
49
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
49 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
50 | bool HPSerial::wait_for(uint8_t value) |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
51 | { // 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
|
52 | char c; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
53 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
54 | 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
|
55 | { |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
56 | while(!serial.readable()) |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
57 | wait_us(10); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
58 | serial.read(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
59 | if (value == c) |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
60 | return 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 | return false; |
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 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
65 | |
49
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
66 | 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
|
67 | uint8_t c; |
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 | while(true) |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
70 | { |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
71 | if (!sendbuf.empty()) |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
72 | { |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
73 | 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
|
74 | { |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
75 | 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
|
76 | cur_gstate = GSTATE_TX; |
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 = 0x66; |
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 | if (!wait_for(0x99)) {} |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
81 | // 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
|
82 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
83 | sendbuf.pop(c); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
84 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
85 | if (!wait_for(0x00)) {} |
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 | c = 0x55; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
88 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
89 | cur_gstate = GSTATE_IDLE; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
90 | 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
|
91 | } |
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 | //else // prevent from flooding the main unit |
67
e19b4147caa9
Reduce the sleep time of the send_pending_key thread to 1ms
David Douard <david.douard@sdfa3.org>
parents:
66
diff
changeset
|
94 | ThisThread::sleep_for(1ms); |
49
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 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
98 | |
49
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
99 | 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
|
100 | uint8_t c; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
101 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
102 | 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
|
103 | ThisThread::sleep_for(1ms); |
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 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
106 | 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
|
107 | cur_gstate = GSTATE_TX; |
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 | // 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
|
110 | c = 0x33; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
111 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
112 | if (!wait_for(0xCC)) {} |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
113 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
114 | c = 0x02; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
115 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
116 | if (!wait_for(0x00)) {} |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
117 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
118 | c = 0xFF; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
119 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
120 | if (!wait_for(0x00)) {} |
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 | c = keycode; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
123 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
124 | if (!wait_for(0x00)) {} |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
125 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
126 | c = 0x55; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
127 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
128 | 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 | 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
|
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 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
133 | |
49
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
134 | 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
|
135 | uint8_t c; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
136 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
137 | 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
|
138 | ThisThread::sleep_for(1ms); |
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 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
141 | 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
|
142 | cur_gstate = GSTATE_TX; |
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 | // 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
|
145 | c = 0x33; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
146 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
147 | if (!wait_for(0xCC)) {} |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
148 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
149 | c = 0x02; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
150 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
151 | if (!wait_for(0x00)) {} |
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 | c = 0x00; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
154 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
155 | if (!wait_for(0x00)) {} |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
156 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
157 | c = 0x55; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
158 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
159 | cur_gstate = GSTATE_IDLE; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
160 | 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
|
161 | } |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
162 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
163 | |
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
164 | // RECV related methods |
19 | 165 | |
166 | bool HPSerial::cmd_available(void) | |
167 | { | |
5 | 168 | return !cmdbuf.empty(); |
169 | } | |
170 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
171 | |
19 | 172 | bool HPSerial::pop(CMD& cmd) |
173 | { | |
5 | 174 | return cmdbuf.pop(cmd); |
175 | } | |
176 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
177 | |
19 | 178 | bool HPSerial::cmd_buf_full(void) |
179 | { | |
5 | 180 | return cmdbuf.full(); |
181 | } | |
182 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
183 | |
19 | 184 | unsigned int HPSerial::nerrors(uint8_t errorno) |
21 | 185 | { |
5 | 186 | return errs[errorno]; |
187 | } | |
188 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
189 | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
190 | void HPSerial::pushCmd(uint8_t cmd, uint8_t size, char *payload) { |
5 | 191 | CMD val; |
192 | uint8_t i; | |
193 | val.id = ncmd++; | |
194 | val.cmd = cmd; | |
195 | val.size = size; | |
196 | for(i=0; i<size; i++) | |
197 | val.value[i] = payload[i]; | |
198 | val.value[i] = 0x00; | |
199 | cmdbuf.push(val); | |
200 | } | |
201 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
202 | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
203 | void HPSerial::send_ack(uint8_t c) { |
44 | 204 | serial.write(&c, 1); |
205 | 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
|
206 | } |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
207 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
208 | |
19 | 209 | 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
|
210 | { |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
211 | // 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
|
212 | // knwon handcheck values are 0x66 and 0x33 |
19 | 213 | 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
|
214 | switch (c) { |
44 | 215 | 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
|
216 | send_ack(0xCC); |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
217 | return HPSerial::STATE_PAYLOAD_SIZE; |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
218 | break; |
19 | 219 | case 0x55: // EoT |
220 | return HPSerial::STATE_IDLE; | |
221 | break; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
222 | case 0x66: |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
223 | send_ack(0x99); |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
224 | return HPSerial::STATE_COMMAND; |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
225 | break; |
19 | 226 | case 0xFF: |
227 | return HPSerial::STATE_IDLE; | |
228 | default: // unknown value | |
229 | send_ack(0xFF); | |
230 | return HPSerial::STATE_IDLE; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
231 | } |
5 | 232 | } |
233 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
234 | |
19 | 235 | 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
|
236 | { |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
237 | if (c == 0x55) { // EoT |
19 | 238 | return STATE_IDLE; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
239 | } |
5 | 240 | |
19 | 241 | tr_data.cmd = c; |
242 | tr_data.size = 0; | |
243 | tr_data.pos = 0; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
244 | send_ack(0x00); |
19 | 245 | |
246 | if (c == 0x86) { // shutdown | |
247 | pushCmd(tr_data.cmd, tr_data.size, tr_data.payload); | |
248 | return HPSerial::STATE_IDLE; | |
21 | 249 | } |
19 | 250 | return STATE_PAYLOAD_SIZE; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
251 | } |
5 | 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_size(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.size = c; |
257 | tr_data.pos = 0; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
258 | send_ack(0x00); |
19 | 259 | return STATE_PAYLOAD; |
5 | 260 | } |
261 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
262 | |
19 | 263 | 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
|
264 | { |
19 | 265 | 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
|
266 | send_ack(0x00); |
19 | 267 | if (tr_data.pos >= tr_data.size) { |
268 | pushCmd(tr_data.cmd, tr_data.size, tr_data.payload); | |
44 | 269 | return STATE_IDLE; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
270 | } |
44 | 271 | return STATE_PAYLOAD; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
272 | } |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
273 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
274 | |
19 | 275 | HPSerial::state_t HPSerial::do_state_sending(uint8_t c) |
276 | { | |
47
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
277 | // 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
|
278 | |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
279 | 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
|
280 | { |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
281 | 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
|
282 | { |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
283 | // 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
|
284 | 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
|
285 | { |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
286 | // 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
|
287 | // 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
|
288 | 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
|
289 | 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
|
290 | } |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
291 | else |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
292 | { |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
293 | // 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
|
294 | 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
|
295 | } |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
296 | } |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
297 | } |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
298 | /* |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
299 | else if (c != 0x00) |
21 | 300 | { // 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
|
301 | tr_data.pos--; |
21 | 302 | } |
47
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
303 | */ |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
304 | |
19 | 305 | // TODO: check ACK values (c is the received ack) |
306 | if (tr_data.pos >= tr_data.size) | |
21 | 307 | { |
44 | 308 | c = 0x55; |
309 | serial.write(&c, 1); // EoT | |
310 | cur_gstate = GSTATE_IDLE; | |
311 | set_timer(); // We are IDLE, detach the timeouter | |
312 | return STATE_IDLE; | |
21 | 313 | } |
44 | 314 | else |
315 | { | |
316 | serial.write(&tr_data.payload[tr_data.pos++], 1); | |
317 | set_timer(RXTIMEOUT); | |
318 | return STATE_SENDING; | |
319 | } | |
19 | 320 | } |
321 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
322 | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
323 | HPSerial::state_t HPSerial::run_state(HPSerial::state_t cur_state, |
44 | 324 | uint8_t c) |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
325 | { |
19 | 326 | 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
|
327 | }; |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
328 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
329 | |
44 | 330 | void HPSerial::rx_irq(void) { |
5 | 331 | uint8_t val; |
21 | 332 | if(serial.readable()) |
333 | { // no reason why we would end here without | |
334 | // this condition, but hey | |
44 | 335 | if (cur_gstate == GSTATE_IDLE) |
336 | // occurs when the CPU starts a new transmission | |
337 | // at this point, cur_state should be STATE_IDLE also (TODO add a check?) | |
338 | cur_gstate = GSTATE_RX; | |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
339 | serial.read(&val, 1); |
19 | 340 | cur_state = run_state(cur_state, val); |
5 | 341 | } |
342 | } | |
343 | ||
344 | ||
345 | void HPSerial::timeout(void) { | |
19 | 346 | set_timer(); // detach the timeouter |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
347 | cur_gstate = GSTATE_IDLE; |
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
348 | cur_state = STATE_IDLE; |
5 | 349 | } |