Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
uart.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 The Paparazzi Team
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
23 #include "mcu_periph/uart.h"
24 
25 #if PERIODIC_TELEMETRY
27 #endif
28 
29 #if USE_UART0
30 struct uart_periph uart0;
31 
32 #if PERIODIC_TELEMETRY
33 static void send_uart0_err(struct transport_tx *trans, struct link_device *dev)
34 {
35  uint16_t ore = uart0.ore;
36  uint16_t ne_err = uart0.ne_err;
37  uint16_t fe_err = uart0.fe_err;
38  uint8_t _bus0 = 0;
39  pprz_msg_send_UART_ERRORS(trans, dev, AC_ID,
40  &ore, &ne_err, &fe_err, &_bus0);
41 }
42 #endif
43 
44 #endif
45 
46 #if USE_UART1
47 struct uart_periph uart1;
48 
49 #if PERIODIC_TELEMETRY
50 static void send_uart1_err(struct transport_tx *trans, struct link_device *dev)
51 {
52  uint16_t ore = uart1.ore;
53  uint16_t ne_err = uart1.ne_err;
54  uint16_t fe_err = uart1.fe_err;
55  uint8_t _bus1 = 1;
56  pprz_msg_send_UART_ERRORS(trans, dev, AC_ID,
57  &ore, &ne_err, &fe_err, &_bus1);
58 }
59 #endif
60 
61 #endif
62 
63 #if USE_UART2
64 struct uart_periph uart2;
65 
66 #if PERIODIC_TELEMETRY
67 static void send_uart2_err(struct transport_tx *trans, struct link_device *dev)
68 {
69  uint16_t ore = uart2.ore;
70  uint16_t ne_err = uart2.ne_err;
71  uint16_t fe_err = uart2.fe_err;
72  uint8_t _bus2 = 2;
73  pprz_msg_send_UART_ERRORS(trans, dev, AC_ID,
74  &ore, &ne_err, &fe_err, &_bus2);
75 }
76 #endif
77 
78 #endif
79 
80 #if USE_UART3
81 struct uart_periph uart3;
82 
83 #if PERIODIC_TELEMETRY
84 static void send_uart3_err(struct transport_tx *trans, struct link_device *dev)
85 {
86  uint16_t ore = uart3.ore;
87  uint16_t ne_err = uart3.ne_err;
88  uint16_t fe_err = uart3.fe_err;
89  uint8_t _bus3 = 3;
90  pprz_msg_send_UART_ERRORS(trans, dev, AC_ID,
91  &ore, &ne_err, &fe_err, &_bus3);
92 }
93 #endif
94 
95 #endif
96 
97 #if USE_UART4
98 struct uart_periph uart4;
99 
100 #if PERIODIC_TELEMETRY
101 static void send_uart4_err(struct transport_tx *trans, struct link_device *dev)
102 {
103  uint16_t ore = uart4.ore;
104  uint16_t ne_err = uart4.ne_err;
105  uint16_t fe_err = uart4.fe_err;
106  uint8_t _bus4 = 4;
107  pprz_msg_send_UART_ERRORS(trans, dev, AC_ID,
108  &ore, &ne_err, &fe_err, &_bus4);
109 }
110 #endif
111 
112 #endif
113 
114 #if USE_UART5
115 struct uart_periph uart5;
116 
117 #if PERIODIC_TELEMETRY
118 static void send_uart5_err(struct transport_tx *trans, struct link_device *dev)
119 {
120  uint16_t ore = uart5.ore;
121  uint16_t ne_err = uart5.ne_err;
122  uint16_t fe_err = uart5.fe_err;
123  uint8_t _bus5 = 5;
124  pprz_msg_send_UART_ERRORS(trans, dev, AC_ID,
125  &ore, &ne_err, &fe_err, &_bus5);
126 }
127 #endif
128 
129 #endif
130 
131 #if USE_UART6
132 struct uart_periph uart6;
133 
134 #if PERIODIC_TELEMETRY
135 static void send_uart6_err(struct transport_tx *trans, struct link_device *dev)
136 {
137  uint16_t ore = uart6.ore;
138  uint16_t ne_err = uart6.ne_err;
139  uint16_t fe_err = uart6.fe_err;
140  uint8_t _bus6 = 6;
141  pprz_msg_send_UART_ERRORS(trans, dev, AC_ID,
142  &ore, &ne_err, &fe_err, &_bus6);
143 }
144 #endif
145 
146 #endif
147 
148 #if USE_UART7
149 struct uart_periph uart7;
150 
151 #if PERIODIC_TELEMETRY
152 static void send_uart7_err(struct transport_tx *trans, struct link_device *dev)
153 {
154  uint16_t ore = uart7.ore;
155  uint16_t ne_err = uart7.ne_err;
156  uint16_t fe_err = uart7.fe_err;
157  uint8_t _bus7 = 7;
158  pprz_msg_send_UART_ERRORS(trans, dev, AC_ID,
159  &ore, &ne_err, &fe_err, &_bus7);
160 }
161 #endif
162 #endif
163 
164 #if USE_UART8
165 struct uart_periph uart8;
166 
167 #if PERIODIC_TELEMETRY
168 static void send_uart8_err(struct transport_tx *trans, struct link_device *dev)
169 {
170  uint16_t ore = uart8.ore;
171  uint16_t ne_err = uart8.ne_err;
172  uint16_t fe_err = uart8.fe_err;
173  uint8_t _bus8 = 8;
174  pprz_msg_send_UART_ERRORS(trans, dev, AC_ID,
175  &ore, &ne_err, &fe_err, &_bus8);
176 }
177 #endif
178 #endif
179 
180 #if PERIODIC_TELEMETRY
181 static void send_uart_err(struct transport_tx *trans __attribute__ ((unused)),
182  struct link_device *dev __attribute__ ((unused)))
183 {
184  static uint8_t uart_nb_cnt = 0;
185  switch (uart_nb_cnt) {
186 #if USE_UART0
187  case 0:
188  send_uart0_err(trans, dev); break;
189 #endif
190 #if USE_UART1
191  case 1:
192  send_uart1_err(trans, dev); break;
193 #endif
194 #if USE_UART2
195  case 2:
196  send_uart2_err(trans, dev); break;
197 #endif
198 #if USE_UART3
199  case 3:
200  send_uart3_err(trans, dev); break;
201 #endif
202 #if USE_UART4
203  case 4:
204  send_uart4_err(trans, dev); break;
205 #endif
206 #if USE_UART5
207  case 5:
208  send_uart5_err(trans, dev); break;
209 #endif
210 #if USE_UART6
211  case 6:
212  send_uart6_err(trans, dev); break;
213 #endif
214 #if USE_UART7
215  case 7:
216  send_uart7_err(trans, dev); break;
217 #endif
218 #if USE_UART8
219  case 8:
220  send_uart8_err(trans, dev); break;
221 #endif
222  default: break;
223  }
224  uart_nb_cnt++;
225  if (uart_nb_cnt == 9) {
226  uart_nb_cnt = 0;
227  }
228 }
229 #endif
230 
232 {
233  p->rx_insert_idx = 0;
234  p->rx_extract_idx = 0;
235  p->tx_insert_idx = 0;
236  p->tx_extract_idx = 0;
237  p->tx_running = false;
238  p->ore = 0;
239  p->ne_err = 0;
240  p->fe_err = 0;
241  p->device.periph = (void *)p;
242  p->device.check_free_space = (check_free_space_t) uart_check_free_space;
243  p->device.put_byte = (put_byte_t) uart_put_byte;
244  p->device.put_buffer = (put_buffer_t) uart_put_buffer;
245  p->device.send_message = (send_message_t) uart_send_message;
246  p->device.char_available = (char_available_t) uart_char_available;
247  p->device.get_byte = (get_byte_t) uart_getch;
248  p->device.set_baudrate = (set_baudrate_t) uart_periph_set_baudrate;
249 
250 #if PERIODIC_TELEMETRY
251  // the first to register do it for the others
252  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_UART_ERRORS, send_uart_err);
253 #endif
254 }
255 
256 int WEAK uart_check_free_space(struct uart_periph *p, long *fd __attribute__((unused)), uint16_t len)
257 {
258  int space = p->tx_extract_idx - p->tx_insert_idx - 1;
259  if (space < 0) {
260  space += UART_TX_BUFFER_SIZE;
261  }
262  return space >= len ? space : 0;
263 }
264 
265 // Weak implementation of put_buffer, byte by byte
266 void WEAK uart_put_buffer(struct uart_periph *p, long fd, const uint8_t *data, uint16_t len)
267 {
268  int i = 0;
269  for (i = 0; i < len; i++) {
270  uart_put_byte(p, fd, data[i]);
271  }
272 }
273 
274 // Weak implementation of send_message, not needed for stream operation
275 void WEAK uart_send_message(struct uart_periph *p __attribute__((unused)), long fd __attribute__((unused)))
276 {
277 }
278 
280 {
281  uint8_t ret = p->rx_buf[p->rx_extract_idx];
283  return ret;
284 }
285 
286 int WEAK uart_char_available(struct uart_periph *p)
287 {
288  int available = p->rx_insert_idx - p->rx_extract_idx;
289  if (available < 0) {
290  available += UART_RX_BUFFER_SIZE;
291  }
292  return available;
293 }
294 
295 void WEAK uart_arch_init(void)
296 {
297 }
298 
299 void WEAK uart_periph_invert_data_logic(struct uart_periph *p __attribute__((unused)), bool invert_rx __attribute__((unused)), bool invert_tx __attribute__((unused)))
300 {
301 }
302 
unsigned short uint16_t
Definition: types.h:16
uint8_t rx_buf[UART_RX_BUFFER_SIZE]
Receive buffer.
Definition: uart.h:72
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
void uart_periph_set_baudrate(struct uart_periph *p, uint32_t baud)
Set baudrate.
Definition: uart_arch.c:883
volatile uint8_t tx_running
Definition: uart.h:79
Periodic telemetry system header (includes downlink utility and generated code).
void WEAK uart_arch_init(void)
Definition: uart.c:295
void WEAK uart_periph_invert_data_logic(struct uart_periph *p, bool invert_rx, bool invert_tx)
Definition: uart.c:299
struct link_device device
Generic device interface.
Definition: uart.h:92
#define UART_RX_BUFFER_SIZE
Definition: uart_arch.h:31
uint16_t rx_insert_idx
Definition: uart.h:73
UART peripheral.
Definition: uart.h:70
void WEAK uart_put_buffer(struct uart_periph *p, long fd, const uint8_t *data, uint16_t len)
Uart transmit buffer implementation.
Definition: uart.c:266
void uart_periph_init(struct uart_periph *p)
Definition: uart.c:231
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
int WEAK uart_char_available(struct uart_periph *p)
Check UART for available chars in receive buffer.
Definition: uart.c:286
uint8_t WEAK uart_getch(struct uart_periph *p)
Definition: uart.c:279
uint16_t rx_extract_idx
Definition: uart.h:74
int WEAK uart_check_free_space(struct uart_periph *p, long *fd, uint16_t len)
Definition: uart.c:256
volatile uint16_t ne_err
noise error counter
Definition: uart.h:89
volatile uint16_t fe_err
framing error counter
Definition: uart.h:90
uint16_t tx_insert_idx
Definition: uart.h:77
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
void uart_put_byte(struct uart_periph *p, long fd, uint8_t data)
Uart transmit implementation.
Definition: uart_arch.c:994
unsigned char uint8_t
Definition: types.h:14
int fd
Definition: serial.c:26
#define UART_TX_BUFFER_SIZE
Definition: uart_arch.h:34
static float p[2][2]
static void send_uart_err(struct transport_tx *trans, struct link_device *dev)
Definition: uart.c:181
void WEAK uart_send_message(struct uart_periph *p, long fd)
Definition: uart.c:275
volatile uint16_t ore
overrun error counter
Definition: uart.h:88
uint16_t tx_extract_idx
Definition: uart.h:78
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46