Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
syslink_dl.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019 Gautier Hattenberger <gautier.hattenberger@enac.fr>
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, see
18 * <http://www.gnu.org/licenses/>.
19 */
20
30#include "mcu_periph/uart.h"
31#include <string.h>
32#include "led.h"
34
36
39
43{
45 uint8_t buf[sizeof(syslink_message_t)+2];
46 buf[0] = syslink_stx[0];
47 buf[1] = syslink_stx[1];
48 buf[2] = msg->type;
49 buf[3] = msg->length;
50 for (int i = 0; i < msg->length; i++) {
51 buf[4+i] = msg->data[i];
52 }
53 buf[msg->length+4] = msg->cksum[0];
54 buf[msg->length+5] = msg->cksum[1];
55 uart_put_buffer(&(SYSLINK_DEV), 0, buf, msg->length+6);
56 //uart_put_buffer(&(SYSLINK_DEV), 0, (uint8_t*)syslink_stx, 2);
57 //uart_put_buffer(&(SYSLINK_DEV), 0, (uint8_t*)(&msg->type), sizeof(msg->type));
58 //uart_put_buffer(&(SYSLINK_DEV), 0, (uint8_t*)(&msg->length), sizeof(msg->length));
59 //uart_put_buffer(&(SYSLINK_DEV), 0, (uint8_t*)(&msg->data), msg->length);
60 //uart_put_buffer(&(SYSLINK_DEV), 0, (uint8_t*)(&msg->cksum), sizeof(msg->cksum));
61}
62
67{
68 if (msg->length != 9) {
69 return;
70 }
71
72 // check flag
73 uint8_t flags = msg->data[0];
74 syslink.charging = (bool) (flags & 1);
75 syslink.powered = (bool) (flags & 2);
76
77 // update voltage
78 float vbat;
79 memcpy(&vbat, &msg->data[1], sizeof(float));
80 if (syslink.powered && vbat > 3.f) {
81 electrical.vsupply = vbat; // remove 0 reading when powered on USB ?
82 } else if (!syslink.powered) {
83 electrical.vsupply = vbat; // running on battery
84 }
85}
86
91{
92 // This function doesn't actually do anything
93 // It is just here to return null responses to most standard messages
94 // Setup order from the cflib-python is
95 // - platform link source (name, protocol version)
96 // - log TOC
97 // - mem update
98 // - param TOC
99 // -> call connected callback and start getting data
100 // - update param if needed
101
102 crtp_message_t *c = (crtp_message_t *) &msg->length;
103
104 if (c->port == CRTP_PORT_LOG) {
105 if (c->channel == 0) { // Table of Contents Access
106 uint8_t cmd = c->data[0];
107 if (cmd == 0) { // GET_ITEM
108 //int id = c->data[1];
109 memset(&c->data[2], 0, 3);
110 c->data[2] = 1; // type
111 c->size = 1 + 5;
113 } else if (cmd == 1) { // GET_INFO
114 memset(&c->data[1], 0, 7);
115 c->size = 1 + 8;
117 }
118 }
119 else if (c->channel == 1) { // Log control
120 c->data[2] = 0; // Success
121 c->size = 3 + 1;
122 // resend message
124 }
125 else if (c->channel == 2) { // Log data
126 // nothing
127 }
128 }
129 else if (c->port == CRTP_PORT_MEM) {
130 if (c->channel == 0) { // Info
131 int cmd = c->data[0];
132 if (cmd == 1) { // GET_NBR_OF_MEMS
133 c->data[1] = 0;
134 c->size = 2 + 1;
135 // resend message
137 }
138 }
139 }
140 else if (c->port == CRTP_PORT_PARAM) {
141 if (c->channel == 0) { // TOC Access
142 c->data[1] = 0; // Last parameter (id = 0)
143 memset(&c->data[2], 0, 10);
144 c->size = 1 + 8;
146 }
147 else if (c->channel == 1) { // Param read
148 // 0 is ok
149 c->data[1] = 0; // value
150 c->size = 1 + 2;
152 }
153 }
154 else if (c->port == CRTP_PORT_LINK) {
155 if (c->channel == 0) { // Echo
157 }
158 else if (c->channel == 1) { // Reply platform name
161 strcpy((char *)c->data, "Bitcraze PPRZ");
163 }
164 }
165 else {
166 // TODO handle error ?
167 }
168}
169
187{
188 crtp_message_t *c = (crtp_message_t *) &msg->length;
189
190 if (CRTP_NULL(*c)) {
191 if (c->size >= 3) {
192 //handle_bootloader(sys);
193 }
194 }
195 else if (c->port == CRTP_PORT_COMMANDER) {
196 //crtp_commander *cmd = (crtp_commander *) &c->data[0];
197 // TODO set RC from cmd message
198 }
199 else if (c->port == CRTP_PORT_PPRZLINK) {
200 // fill rx buffer with CRTP data
202 uint16_t available = SYSLINK_RX_BUF_LEN - syslink.device.char_available(&syslink);
203 if (available > data_size) {
204 // enough room to add new bytes
206 // copy in one block
208 } else {
209 // copy in two parts
213 }
215 }
216 }
217 else {
219 }
220
221 // send next raw message if fifo is not empty
228 // move fifo indexes
232 }
234 }
235
236}
237
239{
240 if (msg->type == SYSLINK_RADIO_RSSI) {
241 uint8_t rssi = msg->data[0]; // Between 40 and 100 meaning -40 dBm to -100 dBm
242 syslink.rssi = 140 - rssi * 100 / (100 - 40);
243 }
244 else if (msg->type == SYSLINK_RADIO_CHANNEL) {
245 // ack radio channel
246 }
247 else if (msg->type == SYSLINK_RADIO_DATARATE) {
248 // ack radio datarate
249 }
250 else if (msg->type == SYSLINK_RADIO_ADDRESS) {
251 // ack radio address
252 }
253}
254
255
259{
260 if (msg->type == SYSLINK_PM_ONOFF_SWITCHOFF) {
261 // power button is hit
262 // don't do anything for now ?
263 }
264 else if (msg->type == SYSLINK_PM_BATTERY_STATE) {
266 }
267 else if (msg->type == SYSLINK_RADIO_RAW) {
269 }
270 else if ((msg->type & SYSLINK_GROUP) == SYSLINK_RADIO) {
272 }
273 else if ((msg->type & SYSLINK_GROUP) == SYSLINK_OW) {
274 // one-wire memory access
275 // don't do anything for now
276 }
277 else {
278 // handle errors ?
279 }
280}
281
282
287// check free space: nb of CRTP slots x space in a slot
288static int syslink_check_free_space(struct syslink_dl *s, long *fd UNUSED, uint16_t len)
289{
290 int slots = s->tx_extract_idx - s->tx_insert_idx;
291 if (slots <= 0) {
292 slots += CRTP_BUF_LEN;
293 }
294 int space = CRTP_MAX_DATA_SIZE * (slots - 1);
295 if (space >= len) {
297 return space;
298 }
299 return 0;
300}
301
302// implementation of put_buffer, fill CRTP slots
303static void syslink_put_buffer(struct syslink_dl *s, long fd UNUSED, const uint8_t *data, uint16_t len)
304{
305 uint16_t buf_rem = len;
306 // fill slots until they are full
307 // we assume that the available space have been check before
308 while (buf_rem > 0) {
309 // get current slot
310 crtp_message_t *c = &s->msg_tx[s->tx_insert_idx];
311 if (c->size < CRTP_MAX_DATA_SIZE) {
312 // fill current buffer
314 memcpy(&c->data[c->size - sizeof(c->header)], &data[len - buf_rem], data_size);
315 c->size += data_size;
317 }
318 else {
319 // start a new slot
320 uint8_t tmp = (s->tx_insert_idx + 1) % CRTP_BUF_LEN;
321 if (tmp == s->tx_extract_idx) {
322 // no more slots
323 // this should be be possible when check_free_space is called before
324 return;
325 }
326 crtp_message_t *c = &s->msg_tx[tmp];
328 memcpy(c->data, &data[len - buf_rem], data_size);
329 c->size = sizeof(c->header) + data_size;
331 s->tx_insert_idx = tmp;
332 }
333 }
334}
335
336// implementation of put_byte using put_buffer
337static void syslink_put_byte(struct syslink_dl *s, long fd, const uint8_t b)
338{
339 syslink_put_buffer(s, fd, &b, 1);
340}
341
342// send_message is not needed as messages are stored in a fifo
344{
345 pprz_mtx_lock(&syslink_tx_mtx); // release mutex
346}
347
349{
350 uint8_t ret = s->rx_buf[s->rx_extract_idx];
351 s->rx_extract_idx = (s->rx_extract_idx + 1) % SYSLINK_RX_BUF_LEN;
352 return ret;
353}
354
356{
357 int available = s->rx_insert_idx - s->rx_extract_idx;
358 if (available < 0) {
359 available += SYSLINK_RX_BUF_LEN;
360 }
361 return available;
362}
363
364
367{
371 syslink.rssi = 0;
372 syslink.charging = false;
373 syslink.powered = false;
374
375 for (int i = 0; i < CRTP_BUF_LEN; i++) {
376 // prepare raw pprzlink datalink headers
377 syslink.msg_tx[i].header = 0;
379 syslink.msg_tx[i].channel = i % 4;
380 syslink.msg_tx[i].size = sizeof(syslink.msg_tx[i].header);
381 }
382
383 // generic device
384 syslink.device.periph = (void *)(&syslink);
391
392 // init mutex if needed
394}
395
398{
399#ifdef CHARGING_LED
400 if (syslink.charging) {
402 }
403 else if (syslink.powered) {
405 }
406 else {
408 }
409#endif
410}
411
422
423
#define UNUSED(x)
static uint16_t rssi
Definition cc2500_rx.c:85
#define LED_ON(i)
Definition led_hw.h:51
#define LED_OFF(i)
Definition led_hw.h:52
#define LED_TOGGLE(i)
Definition led_hw.h:53
int pprz_mtx_unlock(pprz_mutex_t *mtx)
int pprz_mtx_init(pprz_mutex_t *mtx)
int pprz_mtx_lock(pprz_mutex_t *mtx)
#define CRTP_PORT_PPRZLINK
Definition crtp.h:40
#define CRTP_PORT_MEM
Definition crtp.h:37
#define CRTP_NULL(x)
Definition crtp.h:46
#define CRTP_PORT_LOG
Definition crtp.h:38
#define CRTP_PORT_PARAM
Definition crtp.h:35
#define CRTP_PORT_COMMANDER
Definition crtp.h:36
uint8_t data[CRTP_MAX_DATA_SIZE]
Definition crtp.h:63
#define CRTP_PORT_LINK
Definition crtp.h:44
uint8_t size
Definition crtp.h:53
#define CRTP_MAX_DATA_SIZE
Definition crtp.h:50
struct Electrical electrical
Definition electrical.c:92
Interface for electrical status: supply voltage, current, battery status, etc.
float vsupply
supply voltage in V
Definition electrical.h:45
#define Min(x, y)
Definition esc_dshot.c:109
uint8_t msg[10]
Buffer used for general comunication over SPI (out buffer)
arch independent LED (Light Emitting Diodes) API
static uint32_t s
int uart_char_available(struct uart_periph *p)
Check UART for available chars in receive buffer.
Definition uart_arch.c:357
uint8_t uart_getch(struct uart_periph *p)
Definition uart_arch.c:348
uint16_t foo
Definition main_demo5.c:58
static int split(microrl_t *pThis, int limit)
Definition microrl.c:204
int fd
Definition serial.c:26
void WEAK uart_put_buffer(struct uart_periph *p, long fd, const uint8_t *data, uint16_t len)
Definition uart.c:161
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
float b
Definition wedgebug.c:202