src/hp34comm.cpp

Mon, 11 Mar 2024 15:35:36 +0100

author
David Douard <david.douard@sdf3.org>
date
Mon, 11 Mar 2024 15:35:36 +0100
changeset 71
d193c24a078a
parent 70
7b4735e9c2c1
permissions
-rw-r--r--

Add links to sr.ht repos and add an image in the README file

5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
1 #include "hp34comm.h"
28
424d792fea4f compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents: 21
diff changeset
2
424d792fea4f compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents: 21
diff changeset
3 #include <mbed.h>
424d792fea4f compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents: 21
diff changeset
4 #include <CircularBuffer.h>
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
6 /***** HP 34970A communication class ***/
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
7
37
07e8ca2bdf6d Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents: 28
diff changeset
8 #define RXTIMEOUT 50ms
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
9 #define STARTUPRETRY 0.5
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
10
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
11 HPSerial::statemethod HPSerial::state_table[NUM_STATES] = {
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
12 &HPSerial::do_state_initial, // STATE_IDLE
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
13 &HPSerial::do_state_command, // STATE_COMMAND
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
14 &HPSerial::do_state_payload_size, // STATE_PAYLOAD_SIZE
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
15 &HPSerial::do_state_payload, // STATE_PAYLOAD
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
16 &HPSerial::do_state_sending, // STATE_SENDING
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
17 };
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
18
37
07e8ca2bdf6d Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents: 28
diff changeset
19
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
20 HPSerial::HPSerial(PinName tx, PinName rx):
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
21 serial(tx, rx),
37
07e8ca2bdf6d Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents: 28
diff changeset
22 ncmd(0),
07e8ca2bdf6d Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents: 28
diff changeset
23 cur_gstate(GSTATE_IDLE)
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
24 {
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
25 serial.baud(187500);
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
26 serial.format(8, BufferedSerial::Even, 1);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
27 cur_state = STATE_IDLE;
49
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 47
diff changeset
28 send_thread.start(callback(this, &HPSerial::send_pending_key));
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
29 serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
30 }
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
31
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
32
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
43 void HPSerial::sendkey(uint8_t keycode)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
44 {
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
45 if (!sendbuf.full())
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
46 sendbuf.push(keycode);
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
47 }
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
171
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
172 bool HPSerial::cmd_available(void)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
173 {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
174 return !cmdbuf.empty();
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
175 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
176
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
177
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
178 bool HPSerial::pop(CMD& cmd)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
179 {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
180 return cmdbuf.pop(cmd);
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
181 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
182
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
183
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
184 bool HPSerial::cmd_buf_full(void)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
185 {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
186 return cmdbuf.full();
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
187 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
188
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
189
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
190 unsigned int HPSerial::nerrors(uint8_t errorno)
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
191 {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
192 return errs[errorno];
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
193 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
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
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
197 CMD val;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
198 uint8_t i;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
199 val.id = ncmd++;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
200 val.cmd = cmd;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
201 val.size = size;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
202 for(i=0; i<size; i++)
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
203 val.value[i] = payload[i];
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
204 val.value[i] = 0x00;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
205 cmdbuf.push(val);
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
206 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
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
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
210 serial.write(&c, 1);
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
225 case 0x55: // EoT
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
226 return HPSerial::STATE_IDLE;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
240 case 0xFF:
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
241 return HPSerial::STATE_IDLE;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
242 default: // unknown value
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
243 send_ack(0xFF);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
246 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
247
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
248
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
254
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
255 tr_data.cmd = c;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
256 tr_data.size = 0;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
259
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
262 return HPSerial::STATE_IDLE;
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
263 }
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
266
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
267
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
270 tr_data.size = c;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
273 return STATE_PAYLOAD;
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
274 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
275
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
276
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
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
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
289 HPSerial::state_t HPSerial::do_state_sending(uint8_t c)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
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
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
319 // TODO: check ACK values (c is the received ack)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
320 if (tr_data.pos >= tr_data.size)
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
321 {
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
322 c = 0x55;
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
323 serial.write(&c, 1); // EoT
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
324 cur_gstate = GSTATE_IDLE;
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
325 set_timer(); // We are IDLE, detach the timeouter
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
326 return STATE_IDLE;
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
327 }
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
328 else
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
329 {
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
330 serial.write(&tr_data.payload[tr_data.pos++], 1);
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
331 set_timer(RXTIMEOUT);
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
332 return STATE_SENDING;
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
333 }
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
334 }
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
344 void HPSerial::rx_irq(void) {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
345 uint8_t val;
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
346 if(serial.readable())
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
347 { // no reason why we would end here without
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
348 // this condition, but hey
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
349 if (cur_gstate == GSTATE_IDLE)
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
350 // occurs when the CPU starts a new transmission
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
351 // at this point, cur_state should be STATE_IDLE also (TODO add a check?)
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
354 cur_state = run_state(cur_state, val);
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
355 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
356 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
357
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
358
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
359 void HPSerial::timeout(void) {
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
363 }

mercurial