Firmware
syslink_main.h
1 /****************************************************************************
2  *
3  * Copyright (c) 2016 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 #pragma once
35 
36 #include <stdint.h>
37 
38 #include <battery/battery.h>
39 
40 #include <drivers/device/device.h>
42 
43 #include <uORB/uORB.h>
44 
45 #include "syslink.h"
46 #include "crtp.h"
47 
48 #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
49 
50 
51 typedef enum {
52  BAT_DISCHARGING = 0,
53  BAT_CHARGING = 1,
54  BAT_CHARGED = 2
55 } battery_state;
56 
57 
58 class SyslinkBridge;
59 class SyslinkMemory;
60 
61 class Syslink
62 {
63 public:
64  Syslink();
65 
66  int start();
67 
68  int set_datarate(uint8_t datarate);
69  int set_channel(uint8_t channel);
70  int set_address(uint64_t addr);
71 
72  int is_good(int i) { return _params_ack[i] != 0; }
73 
74  int pktrate;
75  int nullrate;
76  int rxrate;
77  int txrate;
78 
79 private:
80 
81  friend class SyslinkBridge;
82  friend class SyslinkMemory;
83 
84  int open_serial(const char *dev);
85 
86  void handle_message(syslink_message_t *msg);
87  void handle_raw(syslink_message_t *sys);
88  void handle_radio(syslink_message_t *sys);
89  void handle_bootloader(syslink_message_t *sys);
90 
91  // Handles other types of messages that we don't really care about, but
92  // will be maintained with the bare minimum implementation for supporting
93  // other crazyflie libraries
94  void handle_raw_other(syslink_message_t *sys);
95 
96  int send_bytes(const void *data, size_t len);
97 
98  // Checksums and transmits a syslink message
99  int send_message(syslink_message_t *msg);
100 
101  int send_queued_raw_message();
102 
103  void update_params(bool force_set);
104 
105  int _syslink_task;
106  bool _task_running;
107  bool _bootloader_mode;
108 
109  int _count;
110  int _null_count;
111  int _count_in;
112  int _count_out;
113  hrt_abstime _lasttime;
114  hrt_abstime _lasttxtime; // Last time a radio message was sent
115  hrt_abstime _lastrxtime; // Last time a radio message was recieved
116 
117  int _fd;
118 
119  // For receiving raw syslink messages to send from other processes
120  ringbuffer::RingBuffer _queue;
121 
122  // Stores data that was needs to be written as a raw message
123  ringbuffer::RingBuffer _writebuffer;
124  SyslinkBridge *_bridge;
125  SyslinkMemory *_memory;
126 
127  int _params_sub;
128 
129  // Current parameter values
130  int32_t _channel, _rate;
131  uint64_t _addr;
132  hrt_abstime _params_update[3]; // Time at which the parameters were updated
133  hrt_abstime _params_ack[3]; // Time at which the parameters were acknowledged by the nrf module
134 
135  orb_advert_t _battery_pub;
136  orb_advert_t _rc_pub;
137  orb_advert_t _cmd_pub;
138 
139  struct battery_status_s _battery_status;
140 
141  Battery _battery;
142 
143  int32_t _rssi;
144  battery_state _bstate;
145 
146  px4_sem_t memory_sem;
147 
148  syslink_message_t memory_msg;
149 
150  static int task_main_trampoline(int argc, char *argv[]);
151 
152  void task_main();
153 
154 };
155 
156 
157 class SyslinkBridge : public cdev::CDev
158 {
159 
160 public:
161  SyslinkBridge(Syslink *link);
162  virtual ~SyslinkBridge() = default;
163 
164  virtual int init();
165 
166  virtual ssize_t read(struct file *filp, char *buffer, size_t buflen);
167  virtual ssize_t write(struct file *filp, const char *buffer, size_t buflen);
168  virtual int ioctl(struct file *filp, int cmd, unsigned long arg);
169 
170  // Makes the message available for reading to processes reading from the bridge
171  void pipe_message(crtp_message_t *msg);
172 
173 protected:
174 
175  virtual pollevent_t poll_state(struct file *filp);
176 
177 private:
178 
179  Syslink *_link;
180 
181  // Stores data that was received from syslink but not yet read by another driver
182  ringbuffer::RingBuffer _readbuffer;
183 
184  crtp_message_t _msg_to_send;
185  int _msg_to_send_size_remaining;
186 
187 };
188 
189 
190 class SyslinkMemory : public cdev::CDev
191 {
192 
193 public:
194  SyslinkMemory(Syslink *link);
195  virtual ~SyslinkMemory() = default;
196 
197  virtual int init();
198 
199  virtual ssize_t read(struct file *filp, char *buffer, size_t buflen);
200  virtual ssize_t write(struct file *filp, const char *buffer, size_t buflen);
201  virtual int ioctl(struct file *filp, int cmd, unsigned long arg);
202 
203 private:
204  friend class Syslink;
205 
206  Syslink *_link;
207 
208  int _activeI;
209 
210  syslink_message_t msgbuf;
211 
212  uint8_t scan();
213  void getinfo(int i);
214 
215  int read(int i, uint16_t addr, char *buf, int length);
216  int write(int i, uint16_t addr, const char *buf, int length);
217 
218  void sendAndWait();
219 
220 };
A flexible ringbuffer class.
Definition: syslink_main.h:190
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
Library calls for battery functionality.
API for the uORB lightweight object broker.
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
Abstract class for any character device.
Definition: CDev.hpp:58
Definition: video_device.h:50
Definition: syslink_main.h:157
Definition: battery.h:49