src/hp34comm.cpp

Mon, 28 Jan 2019 21:51:45 +0100

author
David Douard <david.douard@logilab.fr>
date
Mon, 28 Jan 2019 21:51:45 +0100
changeset 24
b43536c064f6
parent 21
9ffa9d727d80
child 28
424d792fea4f
permissions
-rw-r--r--

Add the (missing?) Keypad lib

5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
1 #include "hp34comm.h"
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
2 #include "mbed.h"
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
3 #include "CircularBuffer.h"
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
4
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
5 /***** HP 34970A communication class ***/
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
6
8
55021f3f1929 pff so many stuff
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
7 #ifdef DEBUG2
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
8
8
55021f3f1929 pff so many stuff
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
9 DigitalOut inrx(D9);
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
10
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
11 #endif
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
12
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
13 DigitalOut lled(LED3);
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
14
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
15 #define RXTIMEOUT 0.05
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
16 #define STARTUPRETRY 0.5
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
17
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
18 const uint8_t startup_seq[] = {
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
19 0x33,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
20 0x02,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
21 0x00,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
22 };
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
23
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
24 HPSerial::statemethod HPSerial::state_table[NUM_STATES] = {
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
25 &HPSerial::do_state_initial,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
26 &HPSerial::do_state_command,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
27 &HPSerial::do_state_payload_size,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
28 &HPSerial::do_state_payload,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
29 &HPSerial::do_state_sending,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
30 &HPSerial::do_state_eot,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
31 };
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
32
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
33 HPSerial::HPSerial(PinName tx, PinName rx):
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
34 serial(tx, rx),
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
35 cur_gstate(GSTATE_IDLE),
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
36 ncmd(0)
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
37 {
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
38 serial.baud(187500);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
39 cur_state = STATE_IDLE;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
40 serial.attach(callback(this, &HPSerial::rxIrq), Serial::RxIrq);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
41 }
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
42
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
43 void HPSerial::startup(void) {
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
44 cur_gstate = GSTATE_STARTING;
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
45 set_timer(0.002); // launch the startup in 10ms
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
46 }
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
47
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
48 void HPSerial::_startup(void)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
49 {
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
50 cur_gstate = GSTATE_STARTING;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
51 tr_data.size = sizeof(startup_seq);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
52 tr_data.cmd = 0xFF;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
53 tr_data.pos = 0;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
54 for(uint8_t i=0; i<tr_data.size; i++)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
55 tr_data.payload[i] = startup_seq[i];
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
56 cur_state = do_state_sending();
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
57 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
58
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
59 void HPSerial::send(const uint8_t *buf, uint8_t size)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
60 {
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
61 // tx_data->
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
62 //send(startup, sizeof(startup)/sizeof(startup[0]));
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
63 }
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
64
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
65 void HPSerial::sendkey(uint8_t keycode)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
66 {
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
67 tr_data.size = 2;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
68 tr_data.cmd = 0xFF;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
69 tr_data.pos = 0;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
70 tr_data.payload[0] = 0x66;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
71 tr_data.payload[1] = keycode;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
72 cur_state = do_state_sending();
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
73 }
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
74
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
75 bool HPSerial::cmd_available(void)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
76 {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
77 return !cmdbuf.empty();
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
78 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
79
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
80 bool HPSerial::pop(CMD& cmd)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
81 {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
82 return cmdbuf.pop(cmd);
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
83 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
84
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
85 bool HPSerial::cmd_buf_full(void)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
86 {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
87 return cmdbuf.full();
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
88 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
89
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
90 unsigned int HPSerial::nerrors(uint8_t errorno)
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
91 {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
92 return errs[errorno];
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
93 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
94
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
95 void HPSerial::pushCmd(uint8_t cmd, uint8_t size, char *payload) {
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
96 CMD val;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
97 uint8_t i;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
98 val.id = ncmd++;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
99 val.cmd = cmd;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
100 val.size = size;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
101 for(i=0; i<size; i++)
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
102 val.value[i] = payload[i];
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
103 val.value[i] = 0x00;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
104 cmdbuf.push(val);
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
105 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
106
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
107 void HPSerial::send_ack(uint8_t c) {
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
108 serial.putc(c);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
109 set_timer(RXTIMEOUT); // if nothing else happen in the next ms, reset
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
110 }
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
111
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
112 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
113 {
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
114 // 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
115 // knwon handcheck values are 0x66 and 0x33
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
116 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
117 switch (c) {
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
118 case 0x33:
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
119 send_ack(0xCC);
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
120 return HPSerial::STATE_PAYLOAD_SIZE;
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
121 break;
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
122 case 0x55: // EoT
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
123 return HPSerial::STATE_IDLE;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
124 break;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
125 case 0x66:
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
126 send_ack(0x99);
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
127 return HPSerial::STATE_COMMAND;
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
128 break;
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
129 case 0xFF:
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
130 return HPSerial::STATE_IDLE;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
131 default: // unknown value
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
132 send_ack(0xFF);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
133 return HPSerial::STATE_IDLE;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
134 }
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
135 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
136
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
137 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
138 {
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
139 if (c == 0x55) { // EoT
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
140 return STATE_IDLE;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
141 }
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
142
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
143 tr_data.cmd = c;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
144 tr_data.size = 0;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
145 tr_data.pos = 0;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
146 send_ack(0x00);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
147
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
148 if (c == 0x86) { // shutdown
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
149 pushCmd(tr_data.cmd, tr_data.size, tr_data.payload);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
150 return HPSerial::STATE_IDLE;
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
151 }
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
152 return STATE_PAYLOAD_SIZE;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
153 }
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
154
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
155 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
156 {
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
157 tr_data.size = c;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
158 tr_data.pos = 0;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
159 send_ack(0x00);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
160 return STATE_PAYLOAD;
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
161 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
162
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
163 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
164 {
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
165 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
166 send_ack(0x00);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
167 if (tr_data.pos >= tr_data.size) {
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
168 pushCmd(tr_data.cmd, tr_data.size, tr_data.payload);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
169 return HPSerial::STATE_IDLE;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
170 }
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
171 return HPSerial::STATE_PAYLOAD;
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
172 }
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
173
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
174 HPSerial::state_t HPSerial::do_state_sending(uint8_t c)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
175 {
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
176 // ghee
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
177 if (c == 0xFF)
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
178 { // resend current char
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
179 tr_data.pos--;
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
180 tr_data.payload[tr_data.pos] += 1;
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
181 }
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
182 // TODO: check ACK values (c is the received ack)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
183 if (tr_data.pos >= tr_data.size)
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
184 {
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
185 return do_state_eot();
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
186 }
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
187 serial.putc(tr_data.payload[tr_data.pos++]);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
188 set_timer(RXTIMEOUT);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
189 return HPSerial::STATE_SENDING;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
190 }
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
191
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
192 HPSerial::state_t HPSerial::do_state_eot(uint8_t c)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
193 {
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
194 serial.putc(0x55); // EoT
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
195 cur_gstate = GSTATE_IDLE;
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
196 set_timer(); // We are IDLE, detach the timeouter
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
197 return STATE_IDLE;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
198 }
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
199
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
200 HPSerial::state_t HPSerial::run_state(HPSerial::state_t cur_state,
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
201 uint8_t c)
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
202 {
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
203 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
204 };
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
205
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
206 void HPSerial::rxIrq(void) {
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
207 uint8_t val;
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
208 if(serial.readable())
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
209 { // no reason why we would end here without
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
210 // this condition, but hey
8
55021f3f1929 pff so many stuff
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
211 #ifdef DEBUG2
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
212 inrx=1;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
213 #endif
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
214 //lled = 1;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 16
diff changeset
215 val = serial.getc();
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
216 cur_state = run_state(cur_state, val);
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
217 //lled = 0;
8
55021f3f1929 pff so many stuff
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
218 #ifdef DEBUG2
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
219 inrx=0;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
220 #endif
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
221 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
222 }
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
223
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
224
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
225 void HPSerial::timeout(void) {
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
226 set_timer(); // detach the timeouter
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
227 if (cur_gstate == GSTATE_STARTING)
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
228 _startup();
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
229 else // reset
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
230 {
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
231 cur_gstate = GSTATE_IDLE;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
232 cur_state = STATE_IDLE;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
233 }
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
234 }

mercurial