Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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(void) {
34  uint16_t ore = uart0.ore;
35  uint16_t ne_err = uart0.ne_err;
36  uint16_t fe_err = uart0.fe_err;
37  const uint8_t _bus0 = 0;
38  DOWNLINK_SEND_UART_ERRORS(DefaultChannel, DefaultDevice,
39  &ore, &ne_err, &fe_err, &_bus0);
40 }
41 #endif
42 
43 #endif
44 
45 #if USE_UART1
46 struct uart_periph uart1;
47 
48 #if PERIODIC_TELEMETRY
49 static void send_uart1_err(void) {
50  uint16_t ore = uart1.ore;
51  uint16_t ne_err = uart1.ne_err;
52  uint16_t fe_err = uart1.fe_err;
53  const uint8_t _bus1 = 1;
54  DOWNLINK_SEND_UART_ERRORS(DefaultChannel, DefaultDevice,
55  &ore, &ne_err, &fe_err, &_bus1);
56 }
57 #endif
58 
59 #endif
60 
61 #if USE_UART2
62 struct uart_periph uart2;
63 
64 #if PERIODIC_TELEMETRY
65 static void send_uart2_err(void) {
66  uint16_t ore = uart2.ore;
67  uint16_t ne_err = uart2.ne_err;
68  uint16_t fe_err = uart2.fe_err;
69  const uint8_t _bus2 = 2;
70  DOWNLINK_SEND_UART_ERRORS(DefaultChannel, DefaultDevice,
71  &ore, &ne_err, &fe_err, &_bus2);
72 }
73 #endif
74 
75 #endif
76 
77 #if USE_UART3
78 struct uart_periph uart3;
79 
80 #if PERIODIC_TELEMETRY
81 static void send_uart3_err(void) {
82  uint16_t ore = uart3.ore;
83  uint16_t ne_err = uart3.ne_err;
84  uint16_t fe_err = uart3.fe_err;
85  const uint8_t _bus3 = 3;
86  DOWNLINK_SEND_UART_ERRORS(DefaultChannel, DefaultDevice,
87  &ore, &ne_err, &fe_err, &_bus3);
88 }
89 #endif
90 
91 #endif
92 
93 #if USE_UART4
94 struct uart_periph uart4;
95 
96 #if PERIODIC_TELEMETRY
97 static void send_uart4_err(void) {
98  uint16_t ore = uart4.ore;
99  uint16_t ne_err = uart4.ne_err;
100  uint16_t fe_err = uart4.fe_err;
101  const uint8_t _bus4 = 4;
102  DOWNLINK_SEND_UART_ERRORS(DefaultChannel, DefaultDevice,
103  &ore, &ne_err, &fe_err, &_bus4);
104 }
105 #endif
106 
107 #endif
108 
109 #if USE_UART5
110 struct uart_periph uart5;
111 
112 #if PERIODIC_TELEMETRY
113 static void send_uart5_err(void) {
114  uint16_t ore = uart5.ore;
115  uint16_t ne_err = uart5.ne_err;
116  uint16_t fe_err = uart5.fe_err;
117  const uint8_t _bus5 = 5;
118  DOWNLINK_SEND_UART_ERRORS(DefaultChannel, DefaultDevice,
119  &ore, &ne_err, &fe_err, &_bus5);
120 }
121 #endif
122 
123 #endif
124 
125 #if USE_UART6
126 struct uart_periph uart6;
127 
128 #if PERIODIC_TELEMETRY
129 static void send_uart6_err(void) {
130  const uint8_t _bus6 = 6;
131  uint16_t ore = uart6.ore;
132  uint16_t ne_err = uart6.ne_err;
133  uint16_t fe_err = uart6.fe_err;
134  DOWNLINK_SEND_UART_ERRORS(DefaultChannel, DefaultDevice,
135  &ore, &ne_err, &fe_err, &_bus6);
136 }
137 #endif
138 
139 #endif
140 
141 #if PERIODIC_TELEMETRY
142 static void send_uart_err(void) {
143  static uint8_t uart_nb_cnt = 0;
144  switch (uart_nb_cnt) {
145 #if USE_UART0
146  case 0:
147  send_uart0_err(); break;
148 #endif
149 #if USE_UART1
150  case 1:
151  send_uart1_err(); break;
152 #endif
153 #if USE_UART2
154  case 2:
155  send_uart2_err(); break;
156 #endif
157 #if USE_UART3
158  case 3:
159  send_uart3_err(); break;
160 #endif
161 #if USE_UART4
162  case 4:
163  send_uart4_err(); break;
164 #endif
165 #if USE_UART5
166  case 5:
167  send_uart5_err(); break;
168 #endif
169 #if USE_UART6
170  case 6:
171  send_uart6_err(); break;
172 #endif
173  default: break;
174  }
175  uart_nb_cnt++;
176  if (uart_nb_cnt == 7)
177  uart_nb_cnt = 0;
178 }
179 #endif
180 
182  p->rx_insert_idx = 0;
183  p->rx_extract_idx = 0;
184  p->tx_insert_idx = 0;
185  p->tx_extract_idx = 0;
186  p->tx_running = FALSE;
187  p->ore = 0;
188  p->ne_err = 0;
189  p->fe_err = 0;
190 
191 #if PERIODIC_TELEMETRY
192  // the first to register do it for the others
193  register_periodic_telemetry(DefaultPeriodic, "UART_ERRORS", send_uart_err);
194 #endif
195 }
196 
198  int16_t space = p->tx_extract_idx - p->tx_insert_idx;
199  if (space <= 0)
200  space += UART_TX_BUFFER_SIZE;
201  return (uint16_t)(space - 1) >= len;
202 }
203 
205  uint8_t ret = p->rx_buf[p->rx_extract_idx];
207  return ret;
208 }
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
uint8_t uart_getch(struct uart_periph *p)
Definition: uart.c:204
Periodic telemetry system header (includes downlink utility and generated code).
volatile uint16_t ore
overrun error counter
Definition: uart.h:69
volatile uint16_t fe_err
framing error counter
Definition: uart.h:71
uint8_t tx_running
Definition: uart.h:64
volatile uint16_t ne_err
noise error counter
Definition: uart.h:70
uint16_t tx_extract_idx
Definition: uart.h:63
bool_t register_periodic_telemetry(struct pprz_telemetry *_pt, const char *_msg, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:38
#define FALSE
Definition: imu_chimu.h:141
UART peripheral.
Definition: uart.h:55
#define UART_TX_BUFFER_SIZE
Definition: uart.h:35
void uart_periph_init(struct uart_periph *p)
Definition: uart.c:181
signed short int16_t
Definition: types.h:17
uint16_t rx_insert_idx
Definition: uart.h:58
bool_t uart_check_free_space(struct uart_periph *p, uint8_t len)
Definition: uart.c:197
unsigned char uint8_t
Definition: types.h:14
uint16_t tx_insert_idx
Definition: uart.h:62
uint8_t rx_buf[UART_RX_BUFFER_SIZE]
Receive buffer.
Definition: uart.h:57
static float p[2][2]
uint16_t rx_extract_idx
Definition: uart.h:59
#define UART_RX_BUFFER_SIZE
Definition: uart.h:34