src/hp34comm.h

Mon, 09 Nov 2020 23:05:24 +0100

author
David Douard <david.douard@sdf3.org>
date
Mon, 09 Nov 2020 23:05:24 +0100
changeset 49
c146d19101a3
parent 44
b3c3d54d2c7c
child 50
279868684eb3
permissions
-rw-r--r--

Refactor HPSerial to get rid of packet collision misbehavior

completely split the key sending code from the irq-based receiveing logic.
When sending keycodes, disable the RxIrq callback and handle send and recv
of bytes synchronously.

The keycode sending routine rus in a dedicated thread.

5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
1 #ifndef HP34COMM_H
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
2 #define HP34COMM_H
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
3
28
424d792fea4f compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents: 21
diff changeset
4 #include <mbed.h>
424d792fea4f compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents: 21
diff changeset
5 #include <CircularBuffer.h>
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
6
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
7 /***** HP 34970A communication class ***/
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
8
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
9 #define MAX_ERRS 10
8
55021f3f1929 pff so many stuff
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
10 #define MAX_BUFF 16
55021f3f1929 pff so many stuff
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
11 #define BUF_SIZE 16
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
12
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 10
diff changeset
13
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 10
diff changeset
14
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
15 class HPSerial {
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
16
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
17 public:
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
18 typedef struct _CMD
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
19 {
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
20 uint8_t cmd;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
21 uint8_t size;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
22 char value[MAX_BUFF+1];
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
23 unsigned long id;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
24 } CMD;
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
25
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 10
diff changeset
26 HPSerial(PinName tx, PinName rx);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
27
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
28 bool cmd_available(void);
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
29 bool pop(CMD& cmd);
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
30 bool cmd_buf_full(void);
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
31 unsigned int nerrors(uint8_t errorno);
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
32
39
63c6a720cb97 hp34comm: add support for boot keycode
David Douard <david.douard@sdf3.org>
parents: 37
diff changeset
33 void startup(uint8_t keycode=0xFF);
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
34 void sendkey(uint8_t keycode);
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
35 void send_key_when_idle();
49
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
36 void send_startup_seq();
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
37 void send_startup_seq(uint8_t keycode);
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
38
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
39 private:
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 10
diff changeset
40 void pushCmd(uint8_t cmd, uint8_t size, char *payload);
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
41 void rx_irq(void);
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
42 void timeout(void);
37
07e8ca2bdf6d Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents: 28
diff changeset
43 void set_timer(Kernel::Clock::duration_u32 v=0ms) {
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
44 timeouter.detach();
37
07e8ca2bdf6d Extracted the display related functions in a Display class
David Douard <david.douard@sdf3.org>
parents: 28
diff changeset
45 if (v > 0ms)
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
46 timeouter.attach(callback(this, &HPSerial::timeout), v);
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
47 };
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
48
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
49 private:
28
424d792fea4f compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents: 21
diff changeset
50 UnbufferedSerial serial;
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
51 uint8_t buf[BUF_SIZE];
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
52 uint8_t head;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
53 CircularBuffer<CMD, 32> cmdbuf;
44
b3c3d54d2c7c Many improvements
David Douard <david.douard@sdf3.org>
parents: 39
diff changeset
54 CircularBuffer<uint8_t, 32> sendbuf;
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
55 unsigned long ncmd;
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
56 unsigned int errs[MAX_ERRS];
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
57 Ticker timeouter;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 10
diff changeset
58
49
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
59 Thread send_thread;
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
60 void send_pending_key();
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
61 bool wait_for(uint8_t);
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
62
c146d19101a3 Refactor HPSerial to get rid of packet collision misbehavior
David Douard <david.douard@sdf3.org>
parents: 44
diff changeset
63
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
64 public:
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
65 // global state machine
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
66 typedef enum {
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
67 GSTATE_STARTING,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
68 GSTATE_IDLE,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
69 GSTATE_TX,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
70 GSTATE_RX,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
71 NUM_GSTATES} gstate_t;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
72 gstate_t gstate() {return cur_gstate;};
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
73 /* gstate_t do_start(); */
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
74 /* gstate_t do_send(); */
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
75 /* gstate_t do_receive(); */
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
76
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
77 /* typedef gstate_t(HPSerial::*gstatemethod)(); */
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
78 /* statemethod const gstate_table[NUM_GSTATES] = { */
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
79 /* &HPSerial::do_start, */
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
80 /* &HPSerial::do_send, */
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
81 /* &HPSerial::do_receive, */
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
82 /* }; */
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
83
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
84 /* gstate_t run_gstate(gstate_t cur_state); */
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
85 private:
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
86 gstate_t cur_gstate;
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
87
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
88 public:
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
89 // transmission state machine
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
90 typedef enum {
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
91 STATE_IDLE,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
92 STATE_COMMAND,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
93 STATE_PAYLOAD_SIZE,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
94 STATE_PAYLOAD,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
95 STATE_SENDING,
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
96 NUM_STATES} state_t;
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
97 state_t state() {return cur_state;};
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
98
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
99 private:
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
100 typedef struct state_data {
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 10
diff changeset
101 uint8_t cmd;
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 10
diff changeset
102 uint8_t size;
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
103 uint8_t pos;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 10
diff changeset
104 char payload[MAX_BUFF];
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
105 } state_data_t;
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 10
diff changeset
106
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
107 void _startup(void);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
108 state_t do_state_initial(uint8_t c=0x00);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
109 state_t do_state_command(uint8_t c);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
110 state_t do_state_payload_size(uint8_t c);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
111 state_t do_state_payload(uint8_t c);
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
112 state_t do_state_sending(uint8_t c=0x00);
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 10
diff changeset
113
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 10
diff changeset
114 void send_ack(uint8_t c);
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
115
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
116 typedef state_t(HPSerial::*statemethod)(uint8_t c);
21
David Douard <david.douard@logilab.fr>
parents: 19
diff changeset
117 static statemethod state_table[NUM_STATES];
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
118 state_t run_state(state_t cur_state, uint8_t c);
18
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 10
diff changeset
119
4fd621551d55 [full replacement] implement a state machine for Rx
David Douard <david.douard@logilab.fr>
parents: 10
diff changeset
120 state_t cur_state;
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
121 state_data_t tr_data;
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
122 };
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
123
19
a52d60613cf7 Stuff...
David Douard <david.douard@logilab.fr>
parents: 18
diff changeset
124
5
f1c85c2500f2 several improvements
David Douard <david.douard@logilab.fr>
parents:
diff changeset
125 #endif

mercurial