PX4 Firmware
PX4 Autopilot Software http://px4.io
px4muorb_KraitRpcWrapper.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2016 Ramakrishna Kintada. 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 #include <time.h>
35 #include <unistd.h>
36 #include <fcntl.h>
38 #include <rpcmem.h>
39 #include <px4muorb.h>
40 #include <px4_platform_common/log.h>
41 #include <px4_platform_common/shmem.h>
42 #include <drivers/drv_hrt.h>
43 #include <string.h>
44 
45 using namespace px4muorb;
46 
47 /* Flags applied to the allocation of the shared memory for RPC */
48 #define MUORB_KRAIT_FASTRPC_MEM_FLAGS 0
49 
50 /* The ID of the HEAP to be used when allocating shared memory */
51 //TODO This heap id is used for test purposes. We need to find out the correct one.
52 #define MUORB_KRAIT_FASTRPC_HEAP_ID 22
53 
54 static char *_TopicNameBuffer = 0;
55 static const int32_t _MAX_TOPIC_NAME_BUFFER = 256;
56 
57 static uint8_t *_DataBuffer = 0;
58 static const uint32_t _MAX_DATA_BUFFER_SIZE = 2048;
59 
60 static bool _Initialized = false;
61 
62 // These numbers are based off the fact each fastrpc call for 64K packet is 94us.
63 // hence we are trying to allocation 64K of byte buffers.
64 static const uint32_t _MAX_TOPIC_DATA_BUFFER_SIZE = 1024;
65 static const uint32_t _MAX_TOPICS = 64;
66 static const uint32_t _MAX_BULK_TRANSFER_BUFFER_SIZE =
68 static uint8_t *_BulkTransferBuffer = 0;
69 
70 unsigned char *adsp_changed_index = 0;
71 
72 // The DSP timer can be read from this file.
73 #define DSP_TIMER_FILE "/sys/kernel/boot_adsp/qdsp_qtimer"
74 
75 /**
76  * Helper function to get timer difference between time on DSP and appsproc side.
77  * Usually the DSP gets started around 2s before the appsproc (Linux) side and
78  * therefore the clocks are not in sync. We change the clock on the DSP side but
79  * for this we need to find the offset first and then tell code on the DSP side.
80  *
81  * @param time_diff_us: pointer to time offset to set.
82  * @return: 0 on success, < 0 on error.
83  */
84 int calc_timer_diff_to_dsp_us(int32_t *time_diff_us);
85 
86 int calc_timer_diff_to_dsp_us(int32_t *time_diff_us)
87 {
88 #if defined(__PX4_POSIX_EXCELSIOR)
89  *time_diff_us = 0;
90  return 0;
91 #endif
92  int fd = open(DSP_TIMER_FILE, O_RDONLY);
93 
94  if (fd < 0) {
95  PX4_ERR("Could not open DSP timer file %s.", DSP_TIMER_FILE);
96  return -1;
97  }
98 
99  char buffer[21] {};
100  int bytes_read = read(fd, buffer, sizeof(buffer));
101 
102  if (bytes_read < 0) {
103  PX4_ERR("Could not read DSP timer file %s.", DSP_TIMER_FILE);
104  close(fd);
105  return -2;
106  }
107 
108  // Do this call right after reading to avoid latency here.
109  timespec ts;
110  clock_gettime(CLOCK_MONOTONIC, &ts);
111  uint64_t time_appsproc = ((uint64_t) ts.tv_sec) * 1000000llu
112  + (ts.tv_nsec / 1000);
113 
114  close(fd);
115 
116  uint64_t time_dsp;
117  int ret = sscanf(buffer, "%llx", &time_dsp);
118 
119  if (ret < 0) {
120  PX4_ERR("Could not parse DSP timer.");
121  return -3;
122  }
123 
124  // The clock count needs to get converted to us.
125  // The magic value of 19.2 was provided by Qualcomm.
126  time_dsp /= 19.2;
127 
128  // Before casting to in32_t, check if it fits.
129  uint64_t abs_diff =
130  (time_appsproc > time_dsp) ?
131  (time_appsproc - time_dsp) : (time_dsp - time_appsproc);
132 
133  if (abs_diff > INT32_MAX) {
134  PX4_ERR("Timer difference too big");
135  return -4;
136  }
137 
138  *time_diff_us = time_appsproc - time_dsp;
139 
140  PX4_DEBUG("found time_dsp: %llu us, time_appsproc: %llu us",
141  time_dsp, time_appsproc);
142  PX4_DEBUG("found time_diff: %li us, %.6f s",
143  *time_diff_us, ((double)*time_diff_us) / 1e6);
144 
145  return 0;
146 }
147 
149 {
150  bool rc = true;
151 
152  PX4_DEBUG("%s Now calling rpcmem_init...", __FUNCTION__);
153  rpcmem_init();
154 
155  PX4_DEBUG("%s Now calling rpcmem_alloc...", __FUNCTION__);
156 
157  _BulkTransferBuffer = (uint8_t *) rpcmem_alloc(MUORB_KRAIT_FASTRPC_HEAP_ID,
159  _MAX_BULK_TRANSFER_BUFFER_SIZE * sizeof(uint8_t));
160  rc = (_BulkTransferBuffer != NULL) ? true : false;
161 
162  if (!rc) {
163  PX4_ERR("%s rpcmem_alloc failed! for bulk transfer buffers",
164  __FUNCTION__);
165  return rc;
166 
167  } else {
168  PX4_DEBUG(
169  "%s rpcmem_alloc passed for Bulk transfer buffers buffer_size: %d addr: %p",
170  __FUNCTION__, (_MAX_BULK_TRANSFER_BUFFER_SIZE * sizeof(uint8_t)), _BulkTransferBuffer);
171  }
172 
173  _TopicNameBuffer = (char *) rpcmem_alloc(MUORB_KRAIT_FASTRPC_HEAP_ID,
175  _MAX_TOPIC_NAME_BUFFER * sizeof(char));
176 
177  rc = (_TopicNameBuffer != NULL) ? true : false;
178 
179  if (!rc) {
180  PX4_ERR("%s rpcmem_alloc failed! for topic_name_buffer", __FUNCTION__);
181  rpcmem_free(_BulkTransferBuffer);
182  return rc;
183 
184  } else {
185  PX4_DEBUG("%s rpcmem_alloc passed for topic_name_buffer", __FUNCTION__);
186  }
187 
188  // now allocate the data buffer.
189  _DataBuffer = (uint8_t *) rpcmem_alloc(MUORB_KRAIT_FASTRPC_HEAP_ID,
191  _MAX_DATA_BUFFER_SIZE * sizeof(uint8_t));
192 
193  rc = (_DataBuffer != NULL) ? true : false;
194 
195  if (!rc) {
196  PX4_ERR("%s rpcmem_alloc failed! for DataBuffer", __FUNCTION__);
197  // free the topic name buffer;
198  rpcmem_free(_BulkTransferBuffer);
199  rpcmem_free(_TopicNameBuffer);
200  _TopicNameBuffer = 0;
201  return rc;
202 
203  } else {
204  PX4_DEBUG("%s rpcmem_alloc passed for data_buffer", __FUNCTION__);
205  }
206 
207  adsp_changed_index = (uint8_t *) rpcmem_alloc(MUORB_KRAIT_FASTRPC_HEAP_ID,
208  MUORB_KRAIT_FASTRPC_MEM_FLAGS, PARAM_BUFFER_SIZE * sizeof(uint8_t));
209 
210  rc = (adsp_changed_index != NULL) ? true : false;
211 
212  if (!rc) {
213  PX4_ERR("%s rpcmem_alloc failed! for adsp_changed_index", __FUNCTION__);
214 
215  } else {
216  memset(adsp_changed_index, 0, PARAM_BUFFER_SIZE * sizeof(uint8_t));
217  }
218 
219  int32_t time_diff_us;
220 
221  if (calc_timer_diff_to_dsp_us(&time_diff_us) != 0) {
222  rc = false;
223  return rc;
224  }
225 
226  // call muorb initialize routine.
227  if (px4muorb_orb_initialize() != 0) {
228  PX4_ERR("%s Error calling the uorb fastrpc initalize method..",
229  __FUNCTION__);
230  rc = false;
231  return rc;
232  }
233 
234  // TODO FIXME: remove this check or make it less verbose later
235  px4muorb_set_absolute_time_offset(time_diff_us);
236 
237  uint64_t time_dsp;
238  px4muorb_get_absolute_time(&time_dsp);
239 
240  uint64_t time_appsproc = hrt_absolute_time();
241 
242  int diff = (time_dsp - time_appsproc);
243 
244  PX4_DEBUG("time_dsp: %llu us, time appsproc: %llu us, diff: %d us",
245  time_dsp, time_appsproc, diff);
246 
247  _Initialized = true;
248  return rc;
249 }
250 
252 {
253  if (_BulkTransferBuffer != NULL) {
254  rpcmem_free(_BulkTransferBuffer);
256  }
257 
258  if (_TopicNameBuffer != NULL) {
259  rpcmem_free(_TopicNameBuffer);
260  _TopicNameBuffer = 0;
261  }
262 
263  if (_DataBuffer != NULL) {
264  rpcmem_free(_DataBuffer);
265  _DataBuffer = 0;
266  }
267 
268  if (adsp_changed_index != NULL) {
269  rpcmem_free(adsp_changed_index);
270  adsp_changed_index = 0;
271  }
272 
273  _Initialized = false;
274  return true;
275 }
276 
278 {
279  return ((_Initialized) ? px4muorb_topic_advertised(topic) : -1);
280 }
281 
283 {
284  return ((_Initialized) ? px4muorb_topic_unadvertised(topic) : -1);
285 }
286 
288 {
289  return ((_Initialized) ? px4muorb_add_subscriber(topic) : -1);
290 }
291 
293 {
294  return (_Initialized ? px4muorb_remove_subscriber(topic) : -1);
295 }
296 
298  int32_t *status)
299 {
300  return (_Initialized ? px4muorb_is_subscriber_present(topic, status) : -1);
301 }
302 
303 int32_t px4muorb::KraitRpcWrapper::SendData(const char *topic,
304  int32_t length_in_bytes, const uint8_t *data)
305 {
306  return (_Initialized ?
307  px4muorb_send_topic_data(topic, data, length_in_bytes) : -1);
308 }
309 
310 int32_t px4muorb::KraitRpcWrapper::ReceiveData(int32_t *msg_type, char **topic,
311  int32_t *length_in_bytes, uint8_t **data)
312 {
313  int32_t rc = -1;
314 
315  if (_Initialized) {
316  rc = px4muorb_receive_msg(msg_type, _TopicNameBuffer,
318  length_in_bytes);
319 
320  if (rc == 0) {
321  *topic = _TopicNameBuffer;
322  *data = _DataBuffer;
323 
324  } else {
325  PX4_ERR("ERROR: Getting data from fastRPC link");
326  }
327 
328  } else {
329  PX4_ERR("ERROR: FastRpcWrapper Not Initialized");
330  }
331 
332  return rc;
333 }
334 
335 int32_t px4muorb::KraitRpcWrapper::ReceiveBulkData(uint8_t **bulk_data,
336  int32_t *length_in_bytes, int32_t *topic_count)
337 {
338  int32_t rc = -1;
339 
340  if (_Initialized) {
341  //rc = px4muorb_receive_msg( msg_type, _TopicNameBuffer, _MAX_TOPIC_NAME_BUFFER, _DataBuffer, _MAX_DATA_BUFFER_SIZE, length_in_bytes );
343  _MAX_BULK_TRANSFER_BUFFER_SIZE, length_in_bytes, topic_count);
344 
345  if (rc == 0) {
346  *bulk_data = _BulkTransferBuffer;
347 
348  } else {
349  PX4_ERR("ERROR: Getting Bulk data from fastRPC link");
350  }
351 
352  } else {
353  PX4_ERR("ERROR: FastRpcWrapper Not Initialized");
354  }
355 
356  return rc;
357 }
358 
360 {
361  return (_Initialized ? px4muorb_unblock_recieve_msg() : -1);
362 }
bool Initialize()
Initiatizes the rpc channel px4 muorb.
static struct vehicle_status_s status
Definition: Commander.cpp:138
int px4muorb_add_subscriber(const char *name)
Definition: px4muorb.cpp:190
int px4muorb_receive_bulk_data(uint8_t *bulk_transfer_buffer, int max_size_in_bytes, int *returned_length_in_bytes, int *topic_count)
Definition: px4muorb.cpp:275
int px4muorb_topic_advertised(const char *topic_name)
Definition: px4muorb.cpp:156
int32_t AddSubscriber(const char *topic)
High-resolution timer with callouts and timekeeping.
bool Terminate()
Terminate to clean up the resources.
int px4muorb_is_subscriber_present(const char *topic_name, int *status)
Definition: px4muorb.cpp:246
int32_t TopicAdvertised(const char *topic)
Muorb related functions to pub/sub of orb topic from krait to adsp.
static void read(bootloader_app_shared_t *pshared)
int calc_timer_diff_to_dsp_us(int32_t *time_diff_us)
Helper function to get timer difference between time on DSP and appsproc side.
int px4muorb_get_absolute_time(uint64_t *time_us)
Definition: px4muorb.cpp:76
int px4muorb_topic_unadvertised(const char *topic_name)
Definition: px4muorb.cpp:173
#define MUORB_KRAIT_FASTRPC_MEM_FLAGS
#define DSP_TIMER_FILE
__END_DECLS int px4muorb_orb_initialize()
Definition: px4muorb.cpp:51
int px4muorb_unblock_recieve_msg(void)
Definition: px4muorb.cpp:290
uint8_t * data
Definition: dataman.cpp:149
static uint8_t * _DataBuffer
#define MUORB_KRAIT_FASTRPC_HEAP_ID
int fd
Definition: dataman.cpp:146
int32_t TopicUnadvertised(const char *topic)
int px4muorb_send_topic_data(const char *name, const uint8_t *data, int data_len_in_bytes)
Definition: px4muorb.cpp:228
int px4muorb_remove_subscriber(const char *name)
Definition: px4muorb.cpp:211
int px4muorb_set_absolute_time_offset(int32_t time_diff_us)
Definition: px4muorb.cpp:71
int px4muorb_receive_msg(int *msg_type, char *topic_name, int topic_name_len, uint8_t *data, int data_len_in_bytes, int *bytes_returned)
Definition: px4muorb.cpp:260
static const uint32_t _MAX_TOPIC_DATA_BUFFER_SIZE
static const int32_t _MAX_TOPIC_NAME_BUFFER
unsigned char * adsp_changed_index
static char * _TopicNameBuffer
int32_t RemoveSubscriber(const char *topic)
int32_t IsSubscriberPresent(const char *topic, int32_t *status)
int32_t ReceiveBulkData(uint8_t **bulk_data, int32_t *length_in_bytes, int32_t *topic_count)
static uint8_t * _BulkTransferBuffer
int32_t SendData(const char *topic, int32_t length_in_bytes, const uint8_t *data)
int32_t ReceiveData(int32_t *msg_type, char **topic, int32_t *length_in_bytes, uint8_t **data)
static const uint32_t _MAX_TOPICS
static const uint32_t _MAX_DATA_BUFFER_SIZE
static bool _Initialized
static const uint32_t _MAX_BULK_TRANSFER_BUFFER_SIZE
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).