-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserial.c
291 lines (229 loc) · 5.7 KB
/
serial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* mµOS - my micro OS
*
* Copyright (C)
* 2015 Christian Thäter <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <muos/muos.h>
#include <muos/serial.h>
#include <muos/hpq.h>
#ifdef MUOS_SERIAL
volatile struct muos_serial_flags muos_serial_status[MUOS_SERIAL_NUM];
char muos_serial_rxsync[MUOS_SERIAL_NUM];
muos_serial_rxcallback_type muos_serial_rxcallback[MUOS_SERIAL_NUM];
/*
TX/RX buffers
*/
#define UART(hw, txsize, rxsize) \
MUOS_CBUFFERDEF(muos_txbuffer##hw, txsize); \
MUOS_CBUFFERDEF(muos_rxbuffer##hw, rxsize);
MUOS_SERIAL_HW
#undef UART
// indexed descriptor arrays are only used when there is more than one serial
#if MUOS_SERIAL_NUM > 1 || defined(MUOS_SERIAL_FORCE_HW)
struct muos_cbuffer* const muos_txbuffer[] =
{
#define UART(hw, txsize, rxsize) \
&muos_txbuffer##hw.descriptor,
MUOS_SERIAL_HW
#undef UART
};
struct muos_cbuffer* const muos_rxbuffer[] =
{
#define UART(hw, txsize, rxsize) \
&muos_rxbuffer##hw.descriptor,
MUOS_SERIAL_HW
#undef UART
};
#endif
/*
init/startup
*/
#if MUOS_SERIAL_NUM > 1 || defined(MUOS_SERIAL_FORCE_HW)
muos_error
muos_serial_start (uint8_t hw, uint32_t baud, const char config[3], int rxsync, muos_serial_rxcallback_type callback)
{
if (hw >= MUOS_SERIAL_NUM)
return muos_fatal_nodev;
MUOS_OK (muos_hw_serial_start (hw, baud, config, rxsync));
muos_serial_status[hw].lineedit_echo = true; //PLANNED: configurable
muos_serial_rxcallback[hw] = callback;
return muos_success;
}
#else
muos_error
muos_serial_start (uint32_t baud, const char config[3], int rxsync, muos_serial_rxcallback_type callback)
{
MUOS_OK (muos_hw_serial_start (0, baud, config, rxsync));
muos_serial_rxcallback[hw] = callback;
return muos_success;
}
#endif
/*
Sending
*/
#if MUOS_SERIAL_NUM > 1 || defined(MUOS_SERIAL_FORCE_HW)
muos_error
muos_serial_tx_byte (uint8_t hw, uint8_t b)
{
if (hw >= MUOS_SERIAL_NUM)
return muos_fatal_nodev;
muos_error ret = muos_success;
muos_serial_tx_stop (hw);
if (muos_cbuffer_free (muos_txbuffer[hw]) > 0)
{
muos_cbuffer_push (muos_txbuffer[hw], b);
}
else
{
ret = muos_error_tx_overflow;
}
muos_serial_tx_run (hw);
return ret;
}
void
muos_serial_tx_flush (uint8_t hw)
{
muos_serial_tx_stop (hw);
muos_cbuffer_init (muos_txbuffer[hw]);
muos_serial_tx_run (hw);
}
#else
muos_error
muos_serial_tx_byte (uint8_t b)
{
muos_error ret = muos_success;
muos_serial_tx_stop ();
if (muos_cbuffer_free (&muos_txbuffer0) > 0)
{
muos_cbuffer_psuh (&muos_txbuffer0, b);
}
else
{
ret = muos_error_tx_overflow;
}
muos_serial_tx_run ();
return ret;
}
void
muos_serial_tx_flush (void)
{
muos_serial_tx_stop ();
muos_cbuffer_init (&muos_txbuffer0);
muos_serial_tx_run ();
}
#endif
/*
Receiving
*/
#if MUOS_SERIAL_NUM > 1 || defined(MUOS_SERIAL_FORCE_HW)
int16_t
muos_serial_rx_byte (uint8_t hw)
{
int16_t ret;
muos_serial_rx_stop (hw);
if (muos_cbuffer_used (muos_rxbuffer[hw]))
{
ret = muos_cbuffer_pop (muos_rxbuffer[hw]);
}
else
{
ret = -muos_error_rx_underflow;
}
muos_serial_rx_run (hw);
return ret;
}
void
muos_serial_rx_flush (uint8_t hw, bool desync)
{
muos_serial_rx_stop (hw);
muos_cbuffer_init (muos_rxbuffer[hw]);
muos_serial_status[hw].serial_rx_insync = !desync;
muos_serial_rx_run (hw);
}
#else
int16_t
muos_serial_rx_byte (void)
{
int16_t ret;
muos_serial_rx_stop ();
if (muos_cbuffer_used (&muos_rxbuffer0))
{
ret = muos_cbuffer_pop (&muos_rxbuffer0);
}
else
{
ret = -muos_error_rx_underflow;
}
muos_serial_rx_run ();
return ret;
}
void
muos_serial_rx_flush (bool desync)
{
muos_serial_rx_stop ();
muos_cbuffer_init (&muos_rxbuffer0);
muos_serial_status[0].serial_rx_insync = !desync;
muos_serial_rx_run ();
}
#endif
/*FIXME: SERIAL_NUM=0*/
void
muos_serial_rxhpq_call (void)
{
uint8_t hw = muos_hpq_pop ();
bool again = false;
if (muos_serial_rxcallback[hw])
again = muos_serial_rxcallback[hw] (hw);
//FIXME: use muos_serial_rx_stop (hw);
muos_interrupt_disable (); // no need for enable, mainloop will disable on return
if (again && muos_cbuffer_used (muos_rxbuffer[hw]))
muos_error_set (muos_hpq_push_arg_isr (muos_serial_rxhpq_call, hw, true));
else
muos_serial_status[hw].serial_rxhpq_pending = false;
}
#endif
//ATTIC
#if 0 //#ifdef MUOS_SCHED_DEPTH implement later better waiting
static bool
txtest (intptr_t n)
{
bool ret;
muos_hw_serial_tx_stop ();
ret = MUOS_CBUFFER_FREE (muos_txbuffer) >= (muos_cbuffer_index) n;
muos_hw_serial_tx_run ();
return ret;
}
//muos_cbuffer_index
//PLANNED: muos_serial_tx_avail (void)
//{
// return MUOS_CBUFFER_FREE(muos_txbuffer);
//}
static bool
rxtest (intptr_t n)
{
bool ret;
muos_hw_serial_rx_stop ();
ret = MUOS_CBUFFER_USED (muos_rxbuffer) >= (muos_cbuffer_index) n;
muos_hw_serial_rx_run ();
return ret;
}
//muos_cbuffer_index
//PLANNED: muos_serial_rx_avail (void)
//{
// return muos_atomic_get(MUOS_CBUFFER_USED(muos_rxbuffer));
//}
#endif