src/hp34comm.cpp

Thu, 07 Oct 2021 21:05:13 +0200

author
David Douard <david.douard@sdfa3.org>
date
Thu, 07 Oct 2021 21:05:13 +0200
changeset 67
e19b4147caa9
parent 66
48f29a1d43d6
child 70
7b4735e9c2c1
permissions
-rw-r--r--

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
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() {
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
165
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
166 bool HPSerial::cmd_available(void)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
167 {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
168 return !cmdbuf.empty();
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
169 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
170
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
171
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
172 bool HPSerial::pop(CMD& cmd)
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.pop(cmd);
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::cmd_buf_full(void)
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.full();
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 unsigned int HPSerial::nerrors(uint8_t errorno)
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
185 {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
186 return errs[errorno];
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
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
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
191 CMD val;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
192 uint8_t i;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
193 val.id = ncmd++;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
194 val.cmd = cmd;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
195 val.size = size;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
196 for(i=0; i<size; i++)
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
197 val.value[i] = payload[i];
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
198 val.value[i] = 0x00;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
199 cmdbuf.push(val);
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
200 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
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
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
204 serial.write(&c, 1);
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
219 case 0x55: // EoT
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
220 return HPSerial::STATE_IDLE;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
226 case 0xFF:
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
227 return HPSerial::STATE_IDLE;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
228 default: // unknown value
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
229 send_ack(0xFF);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
232 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
233
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
234
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
240
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
241 tr_data.cmd = c;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
242 tr_data.size = 0;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
245
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
246 if (c == 0x86) { // shutdown
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
247 pushCmd(tr_data.cmd, tr_data.size, tr_data.payload);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
248 return HPSerial::STATE_IDLE;
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
249 }
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
252
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
253
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
254 HPSerial::state_t HPSerial::do_state_payload_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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
256 tr_data.size = c;
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 return STATE_PAYLOAD;
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
260 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
261
50
279868684eb3 Remove now dead/useless code
David Douard <david.douard@sdf3.org>
parents: 49
diff changeset
262
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
267 if (tr_data.pos >= tr_data.size) {
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
268 pushCmd(tr_data.cmd, tr_data.size, tr_data.payload);
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
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
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
275 HPSerial::state_t HPSerial::do_state_sending(uint8_t c)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
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
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
305 // TODO: check ACK values (c is the received ack)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
306 if (tr_data.pos >= tr_data.size)
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
307 {
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
308 c = 0x55;
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
309 serial.write(&c, 1); // EoT
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
310 cur_gstate = GSTATE_IDLE;
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
311 set_timer(); // We are IDLE, detach the timeouter
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
312 return STATE_IDLE;
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
313 }
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
314 else
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
315 {
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
316 serial.write(&tr_data.payload[tr_data.pos++], 1);
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
317 set_timer(RXTIMEOUT);
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
318 return STATE_SENDING;
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
319 }
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
320 }
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
330 void HPSerial::rx_irq(void) {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
331 uint8_t val;
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
332 if(serial.readable())
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
333 { // no reason why we would end here without
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
334 // this condition, but hey
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
335 if (cur_gstate == GSTATE_IDLE)
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
336 // occurs when the CPU starts a new transmission
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
337 // 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
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
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
340 cur_state = run_state(cur_state, val);
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
341 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
342 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
343
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
344
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
345 void HPSerial::timeout(void) {
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
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
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
349 }

mercurial