Mon, 11 Mar 2024 15:35:36 +0100
Add links to sr.ht repos and add an image in the README file
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() { |
70
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
67 | // body of the send_trhead: dedicated to sending pending keys |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
68 | // Note that a key can also be sent after receiving a SoT packet |
49
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()) |
70
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
72 | do_send_key(); |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
73 | else // prevent from flooding the main unit |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
74 | ThisThread::sleep_for(1ms); |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
75 | } |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
76 | } |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
77 | |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
78 | void HPSerial::do_send_key() { |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
79 | uint8_t c; |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
80 | if (!sendbuf.empty()) |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
81 | { |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
82 | if (cur_gstate == GSTATE_IDLE) |
49
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
83 | { |
70
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
84 | serial.attach(0, SerialBase::RxIrq); |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
85 | cur_gstate = GSTATE_TX; |
49
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
86 | |
70
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
87 | c = 0x66; |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
88 | serial.write(&c, 1); |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
89 | if (!wait_for(0x99)) {} |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
90 | // break; // XXX what to do? |
49
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
91 | |
70
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
92 | sendbuf.pop(c); |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
93 | serial.write(&c, 1); |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
94 | if (!wait_for(0x00)) {} |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
95 | |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
96 | c = 0x55; |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
97 | serial.write(&c, 1); |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
98 | cur_gstate = GSTATE_IDLE; |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
99 | serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq); |
49
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
100 | } |
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 | } |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
103 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
104 | |
49
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
105 | 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
|
106 | uint8_t c; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
107 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
108 | 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
|
109 | ThisThread::sleep_for(1ms); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
110 | } |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
111 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
112 | 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
|
113 | cur_gstate = GSTATE_TX; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
114 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
115 | // 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
|
116 | c = 0x33; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
117 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
118 | if (!wait_for(0xCC)) {} |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
119 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
120 | c = 0x02; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
121 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
122 | if (!wait_for(0x00)) {} |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
123 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
124 | c = 0xFF; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
125 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
126 | if (!wait_for(0x00)) {} |
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 | c = keycode; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
129 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
130 | if (!wait_for(0x00)) {} |
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 | c = 0x55; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
133 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
134 | cur_gstate = GSTATE_IDLE; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
135 | 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
|
136 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
137 | } |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
138 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
139 | |
49
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
140 | 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
|
141 | uint8_t c; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
142 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
143 | 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
|
144 | ThisThread::sleep_for(1ms); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
145 | } |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
146 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
147 | 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
|
148 | cur_gstate = GSTATE_TX; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
149 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
150 | // 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
|
151 | c = 0x33; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
152 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
153 | if (!wait_for(0xCC)) {} |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
154 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
155 | c = 0x02; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
156 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
157 | if (!wait_for(0x00)) {} |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
158 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
159 | c = 0x00; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
160 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
161 | if (!wait_for(0x00)) {} |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
162 | |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
163 | c = 0x55; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
164 | serial.write(&c, 1); |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
165 | cur_gstate = GSTATE_IDLE; |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
166 | 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
|
167 | } |
c146d19101a3
Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents:
47
diff
changeset
|
168 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
169 | |
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
170 | // RECV related methods |
19 | 171 | |
172 | bool HPSerial::cmd_available(void) | |
173 | { | |
5 | 174 | return !cmdbuf.empty(); |
175 | } | |
176 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
177 | |
19 | 178 | bool HPSerial::pop(CMD& cmd) |
179 | { | |
5 | 180 | return cmdbuf.pop(cmd); |
181 | } | |
182 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
183 | |
19 | 184 | bool HPSerial::cmd_buf_full(void) |
185 | { | |
5 | 186 | return cmdbuf.full(); |
187 | } | |
188 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
189 | |
19 | 190 | unsigned int HPSerial::nerrors(uint8_t errorno) |
21 | 191 | { |
5 | 192 | return errs[errorno]; |
193 | } | |
194 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
195 | |
70
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
196 | void HPSerial::push_cmd(uint8_t cmd, uint8_t size, char *payload) { |
5 | 197 | CMD val; |
198 | uint8_t i; | |
199 | val.id = ncmd++; | |
200 | val.cmd = cmd; | |
201 | val.size = size; | |
202 | for(i=0; i<size; i++) | |
203 | val.value[i] = payload[i]; | |
204 | val.value[i] = 0x00; | |
205 | cmdbuf.push(val); | |
206 | } | |
207 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
208 | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
209 | void HPSerial::send_ack(uint8_t c) { |
44 | 210 | serial.write(&c, 1); |
211 | 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
|
212 | } |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
213 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
214 | |
19 | 215 | 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
|
216 | { |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
217 | // 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
|
218 | // knwon handcheck values are 0x66 and 0x33 |
19 | 219 | 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
|
220 | switch (c) { |
44 | 221 | 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
|
222 | send_ack(0xCC); |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
223 | return HPSerial::STATE_PAYLOAD_SIZE; |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
224 | break; |
19 | 225 | case 0x55: // EoT |
226 | return HPSerial::STATE_IDLE; | |
227 | break; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
228 | case 0x66: |
70
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
229 | if (!sendbuf.empty()) { |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
230 | // hijack the transmission to send a keycode |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
231 | do_send_key(); |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
232 | return HPSerial::STATE_IDLE; |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
233 | } |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
234 | else |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
235 | { |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
236 | send_ack(0x99); |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
237 | return HPSerial::STATE_COMMAND; |
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
238 | } |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
239 | break; |
19 | 240 | case 0xFF: |
241 | return HPSerial::STATE_IDLE; | |
242 | default: // unknown value | |
243 | send_ack(0xFF); | |
244 | return HPSerial::STATE_IDLE; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
245 | } |
5 | 246 | } |
247 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
248 | |
19 | 249 | 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
|
250 | { |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
251 | if (c == 0x55) { // EoT |
19 | 252 | return STATE_IDLE; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
253 | } |
5 | 254 | |
19 | 255 | tr_data.cmd = c; |
256 | tr_data.size = 0; | |
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 | |
260 | if (c == 0x86) { // shutdown | |
70
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
261 | push_cmd(tr_data.cmd, tr_data.size, tr_data.payload); |
19 | 262 | return HPSerial::STATE_IDLE; |
21 | 263 | } |
19 | 264 | return STATE_PAYLOAD_SIZE; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
265 | } |
5 | 266 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
267 | |
19 | 268 | 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
|
269 | { |
19 | 270 | tr_data.size = c; |
271 | tr_data.pos = 0; | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
272 | send_ack(0x00); |
19 | 273 | return STATE_PAYLOAD; |
5 | 274 | } |
275 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
276 | |
19 | 277 | 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
|
278 | { |
19 | 279 | 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
|
280 | send_ack(0x00); |
19 | 281 | if (tr_data.pos >= tr_data.size) { |
70
7b4735e9c2c1
Make sure to send pending keys
David Douard <david.douard@sdfa3.org>
parents:
67
diff
changeset
|
282 | push_cmd(tr_data.cmd, tr_data.size, tr_data.payload); |
44 | 283 | return STATE_IDLE; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
284 | } |
44 | 285 | return STATE_PAYLOAD; |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
286 | } |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
287 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
288 | |
19 | 289 | HPSerial::state_t HPSerial::do_state_sending(uint8_t c) |
290 | { | |
47
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
291 | // 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
|
292 | |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
293 | 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
|
294 | { |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
295 | 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
|
296 | { |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
297 | // 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
|
298 | 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
|
299 | { |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
300 | // 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
|
301 | // 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
|
302 | 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
|
303 | 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
|
304 | } |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
305 | else |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
306 | { |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
307 | // 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
|
308 | 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
|
309 | } |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
310 | } |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
311 | } |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
312 | /* |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
313 | else if (c != 0x00) |
21 | 314 | { // 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
|
315 | tr_data.pos--; |
21 | 316 | } |
47
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
317 | */ |
11c57010e4f9
Attempt to improve the detection of packet collisions (not fixed yet)
David Douard <david.douard@sdf3.org>
parents:
44
diff
changeset
|
318 | |
19 | 319 | // TODO: check ACK values (c is the received ack) |
320 | if (tr_data.pos >= tr_data.size) | |
21 | 321 | { |
44 | 322 | c = 0x55; |
323 | serial.write(&c, 1); // EoT | |
324 | cur_gstate = GSTATE_IDLE; | |
325 | set_timer(); // We are IDLE, detach the timeouter | |
326 | return STATE_IDLE; | |
21 | 327 | } |
44 | 328 | else |
329 | { | |
330 | serial.write(&tr_data.payload[tr_data.pos++], 1); | |
331 | set_timer(RXTIMEOUT); | |
332 | return STATE_SENDING; | |
333 | } | |
19 | 334 | } |
335 | ||
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
336 | |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
337 | HPSerial::state_t HPSerial::run_state(HPSerial::state_t cur_state, |
44 | 338 | uint8_t c) |
18
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
339 | { |
19 | 340 | 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
|
341 | }; |
4fd621551d55
[full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents:
16
diff
changeset
|
342 | |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
343 | |
44 | 344 | void HPSerial::rx_irq(void) { |
5 | 345 | uint8_t val; |
21 | 346 | if(serial.readable()) |
347 | { // no reason why we would end here without | |
348 | // this condition, but hey | |
44 | 349 | if (cur_gstate == GSTATE_IDLE) |
350 | // occurs when the CPU starts a new transmission | |
351 | // at this point, cur_state should be STATE_IDLE also (TODO add a check?) | |
352 | cur_gstate = GSTATE_RX; | |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
21
diff
changeset
|
353 | serial.read(&val, 1); |
19 | 354 | cur_state = run_state(cur_state, val); |
5 | 355 | } |
356 | } | |
357 | ||
358 | ||
359 | void HPSerial::timeout(void) { | |
19 | 360 | set_timer(); // detach the timeouter |
50
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
361 | cur_gstate = GSTATE_IDLE; |
279868684eb3
Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents:
49
diff
changeset
|
362 | cur_state = STATE_IDLE; |
5 | 363 | } |