Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
downlink.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Pascal Brisset, Antoine Drouin
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 
30 #include "generated/airframe.h" // AC_ID is required
31 
32 #if (defined DATALINK) || PERIODIC_TELEMETRY
34 #endif
35 
36 #if USE_HARD_FAULT_RECOVERY
37 #include "mcu.h"
38 #endif
39 
40 #if defined SITL && !USE_NPS
41 struct ivy_transport ivy_tp;
42 #endif
43 
44 #if DATALINK == PPRZ || DATALINK == SUPERBITRF || DATALINK == W5100 || DATALINK == BLUEGIGA
45 struct pprz_transport pprz_tp;
46 #endif
47 #if DATALINK == XBEE
48 struct xbee_transport xbee_tp;
49 #endif
50 #if USE_PPRZLOG
51 struct pprzlog_transport pprzlog_tp;
52 #endif
53 
54 #if PERIODIC_TELEMETRY
56 #include "mcu_periph/sys_time.h"
57 
58 static uint32_t last_down_nb_bytes = 0; // previous number of bytes sent
59 static uint32_t last_up_nb_msgs = 0; // previous number of received messages
60 static uint32_t last_ts = 0; // timestamp in usec when last message was send
61 
62 static void send_downlink(struct transport_tx *trans, struct link_device *dev)
63 {
64  // current timestamp
65  uint32_t now_ts = get_sys_time_msec();
66  // compute downlink byte rate
67  if (now_ts > last_ts) {
68  uint16_t down_rate = (1000 * ((uint32_t)dev->nb_bytes - last_down_nb_bytes)) / (now_ts - last_ts);
69  uint16_t up_rate = (1000 * ((uint32_t)datalink_nb_msgs - last_up_nb_msgs)) / (now_ts - last_ts);
70 
71  last_ts = now_ts;
72 #if defined DATALINK || defined SITL
73  last_down_nb_bytes = dev->nb_bytes;
75 #else
77  last_up_nb_msgs = 0;
78 #endif
79 
80  pprz_msg_send_DATALINK_REPORT(trans, dev, AC_ID, &datalink_time, &datalink_nb_msgs, &dev->nb_msgs, &down_rate,
81  &up_rate, &dev->nb_ovrn);
82  }
83 }
84 #endif
85 
86 void downlink_init(void)
87 {
88  // Set initial counters
89  (DefaultDevice).device.nb_ovrn = 0;
90  (DefaultDevice).device.nb_bytes = 0;
91  (DefaultDevice).device.nb_msgs = 0;
92 
93 #if defined DATALINK
94 
95  datalink_nb_msgs = 0;
96 
97 #if DATALINK == PPRZ || DATALINK == SUPERBITRF || DATALINK == W5100 || DATALINK == BLUEGIGA
98  pprz_transport_init(&pprz_tp);
99 #endif
100 #if DATALINK == XBEE
101 #ifndef XBEE_TYPE
102 #define XBEE_TYPE XBEE_24
103 #endif
104 #ifndef XBEE_INIT
105 #define XBEE_INIT ""
106 #endif
107 #if USE_HARD_FAULT_RECOVERY
108  if (recovering_from_hard_fault)
109  // in case of hardfault recovery, we want to skip xbee init which as an active wait
110  xbee_transport_init(&xbee_tp, &((DefaultDevice).device), AC_ID, XBEE_TYPE, XBEE_BAUD, NULL, XBEE_INIT);
111  else
112 #endif
113  xbee_transport_init(&xbee_tp, &((DefaultDevice).device), AC_ID, XBEE_TYPE, XBEE_BAUD, sys_time_usleep, XBEE_INIT);
114 #endif
115 #if DATALINK == W5100
116  w5100_init();
117 #endif
118 #if DATALINK == BLUEGIGA
120 #endif
121 
122 #endif
123 
124 #if USE_PPRZLOG
125  pprzlog_transport_init(&pprzlog_tp, get_sys_time_usec);
126 #endif
127 
128 #if SITL && !USE_NPS
129  ivy_transport_init(&ivy_tp);
130 #endif
131 
132 #if PERIODIC_TELEMETRY
133  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_DATALINK_REPORT, send_downlink);
134 #endif
135 }
136 
unsigned short uint16_t
Definition: types.h:16
Periodic telemetry system header (includes downlink utility and generated code).
uint32_t get_sys_time_msec(void)
Definition: sys_time_arch.c:78
void w5100_init(void)
Definition: w5100.c:184
struct bluegiga_periph bluegiga_p
Definition: bluegiga.c:61
void bluegiga_init(struct bluegiga_periph *p)
Definition: bluegiga.c:148
uint16_t datalink_nb_msgs
Definition: sim_ap.c:43
void sys_time_usleep(uint32_t us)
sys_time_usleep(uint32_t us)
Definition: sys_time_arch.c:95
Architecture independent timing functions.
unsigned long uint32_t
Definition: types.h:18
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
uint16_t datalink_time
Definition: sim_ap.c:42
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
Arch independent mcu ( Micro Controller Unit ) utilities.
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46