communications.cpp
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright 2014-2019 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT, IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #include <asf.h>
14 #include "../../../src/ISUtilities.h"
15 #include "../../../src/ISLogger.h"
16 #include "../../../hw-libs/misc/bootloaderApp.h"
17 #include "../drivers/d_quadEnc.h"
18 #include "ISLogFileFatFs.h"
19 #include "xbee.h"
20 #include "wifi.h"
21 #include "sd_card_logger.h"
22 #include "CAN.h"
23 #include "../hw-libs/communications/CAN_comm.h"
24 #include "../src/protocol_nmea.h"
25 #include "user_interface.h"
26 #include "communications.h"
27 
28 typedef struct
29 {
30  // Comm instances
32 
33  // Comm instance data buffer
34  uint8_t comm_buffer[PKT_BUF_SIZE];
35 
37 
38 
39 #define COM_RX_PORT_COUNT (EVB2_PORT_COUNT-1) // exclude CAN port
41 static uint8_t s_rxDecodeBuffer[PKT_BUF_SIZE] = {};
42 
44 
48 
51 
52 
53 int comWrite(int serialNum, const unsigned char *buf, int size, uint32_t ledPin )
54 {
55  int len;
57  {
58  len = spiTouINS_serWrite(buf, size);
59  }
60  else
61  {
62  len = serWrite(serialNum, buf, size);
63  }
64 
65  if(len)
66  {
67  LED_ON(ledPin);
68  }
69  return len;
70 }
71 
72 int comRead(int serialNum, unsigned char *buf, int bufSize, uint32_t ledPin)
73 {
74  int len = 0;
75 
77  {
78  len = spiTouINS_serRead(buf, bufSize);
79  }
80  else
81  {
82 #if 0 // Wait for end of data. Check Rx buffer has data and hasn't change over 1ms.
83  static int usedLast[EVB2_PORT_COUNT] = {0};
84  int used = serRxUsed(serialNum);
85  if(used!=0 && used==usedLast[serialNum])
86  { // Data in Rx buffer and amount hasn't changed.
87  len = serRead(serialNum, buf, bufSize, 0);
88  }
89  usedLast[serialNum] = used;
90 #else
91  // Normal read
92  len = serRead(serialNum, buf, bufSize);
93 #endif
94  }
95 
96  if(len)
97  {
98  LED_ON(ledPin);
99  }
100  return len;
101 }
102 
104 {
105  UNUSED(port);
106 #if 0
107  uint32_t stopbits, parity, databits;
108 // uint32_t imr;
109 
110  switch (cfg->bCharFormat)
111  {
112  case CDC_STOP_BITS_2:
113  stopbits = US_MR_NBSTOP_2_BIT;
114  break;
115  case CDC_STOP_BITS_1_5:
116  stopbits = US_MR_NBSTOP_1_5_BIT;
117  break;
118  case CDC_STOP_BITS_1:
119  default:
120  // Default stop bit = 1 stop bit
121  stopbits = US_MR_NBSTOP_1_BIT;
122  break;
123  }
124 
125  switch (cfg->bParityType)
126  {
127  case CDC_PAR_EVEN:
128  parity = US_MR_PAR_EVEN;
129  break;
130  case CDC_PAR_ODD:
131  parity = US_MR_PAR_ODD;
132  break;
133  case CDC_PAR_MARK:
134  parity = US_MR_PAR_MARK;
135  break;
136  case CDC_PAR_SPACE:
137  parity = US_MR_PAR_SPACE;
138  break;
139  default:
140  case CDC_PAR_NONE:
141  parity = US_MR_PAR_NO;
142  break;
143  }
144 
145  switch(cfg->bDataBits)
146  {
147  case 5:
148  case 6:
149  case 7:
150  databits = cfg->bDataBits - 5;
151  break;
152  default:
153  case 8:
154  databits = US_MR_CHRL_8_BIT;
155  break;
156  }
157 
158  // Options for USART. This gets called when USB is first connected AND each time the USB CDC serial port is opened by the host.
159  // sam_usart_opt_t usart_options;
160  // usart_options.baudrate = LE32_TO_CPU(cfg->dwDTERate);
161  // usart_options.char_length = databits;
162  // usart_options.parity_type = parity;
163  // usart_options.stop_bits = stopbits;
164  // usart_options.channel_mode = US_MR_CHMODE_NORMAL;
165 #endif
166 
167  uint32_t baudrate = LE32_TO_CPU(cfg->dwDTERate);
168  if (comManagerValidateBaudRate(baudrate)==0)
169  {
170  // Set baudrate based on USB CDC baudrate
172  {
173  serSetBaudRate(EVB2_PORT_UINS0, baudrate);
174  }
175 
178  {
179  serSetBaudRate(EVB2_PORT_UINS1, baudrate);
180  }
181  }
182 
183  if (comManagerValidateBaudRate(baudrate)==0 || baudrate==9600)
184  {
185  // if(g_flashCfg->cbf[EVB2_PORT_USB] & (1<<EVB2_PORT_XBEE))
186  // {
187  // serSetBaudRate(EVB2_PORT_XBEE, baudrate);
188  // }
190  {
191  serSetBaudRate(EVB2_PORT_XRADIO, baudrate);
192  }
194  {
195  serSetBaudRate(EVB2_PORT_BLE, baudrate);
196  }
198  {
199  serSetBaudRate(EVB2_PORT_SP330, baudrate);
200  }
202  {
204  }
205  }
206 
207 }
208 
209 void callback_cdc_set_dtr(uint8_t port, bool b_enable)
210 {
211  if (b_enable)
212  { // Host terminal has open COM
214  }
215  else
216  { // Host terminal has close COM
218  }
219 
221  {
222  if (b_enable)
223  { // Assert (LOW) DTR
226  }
227  else
228  { // De-assert (HIGH) DTR
231  }
232  }
233 }
234 
235 // This function never gets called because of a bug in Atmel CDC driver. Bummer.
236 // void callback_cdc_set_rts(uint8_t port, bool b_enable)
237 // {
238 // switch(g_msg.evb.comBridgeCfg)
239 // {
240 // case EVB2_CBC_USBxXBEE:
241 // if (b_enable)
242 // {
243 // ioport_set_pin_level(UART_XBEE_N_RTS_PIN, IOPORT_PIN_LEVEL_HIGH);
244 // }
245 // else
246 // {
247 // ioport_set_pin_level(UART_XBEE_N_RTS_PIN, IOPORT_PIN_LEVEL_LOW);
248 // }
249 // break;
250 //
251 // case EVB2_CBC_RS232xUSB:
252 // case EVB2_CBC_RS422xUSB:
253 // break;
254 // }
255 // }
256 
257 
259 {
260  int len = is_comm_stop_broadcasts_current_port(&g_commTx);
262 }
263 
265 {
266  int len;
267 
268  len = is_comm_get_data(&g_commTx, DID_INS_2, 0, 0, 10); // 20 x 4ms = 40ms
270 
271  len = is_comm_get_data(&g_commTx, DID_DEV_INFO, 0, 0, 500); // 500ms
273 }
274 
276 {
277  rmc_t rmc;
279  rmc.options = 0;
280  int len = is_comm_set_data(&g_commTx, DID_RMC, 0, sizeof(rmc_t), &rmc);
282 
283 // len = is_comm_get_data(&comm, DID_INS_2, 0, 0, 1); // 1 x 4ms = 4ms
284 // comWrite(EVB2_PORT_UINS0, comm.buffer, len, LED_INS_TXD_PIN);
285 }
286 
287 
288 void handle_data_from_uINS(p_data_hdr_t &dataHdr, uint8_t *data)
289 {
290  uDatasets d;
291 
292  if( copyDataPToStructP2(&d, &dataHdr, data, sizeof(uDatasets)) )
293  { // Invalid
294  return;
295  }
296 
297  // Save uINS data to global variables
298  switch(dataHdr.id)
299  {
300  case DID_DEV_INFO:
301  copyDataPToStructP2(&g_msg.uInsInfo, &dataHdr, data, sizeof(dev_info_t));
302  break;
303 
304  case DID_INS_1:
305  if(dataHdr.size+dataHdr.offset > sizeof(ins_1_t)){ /* Invalid */ return; }
306  g_status.week = d.ins1.week;
307  g_status.timeOfWeekMs = (uint32_t)(d.ins1.timeOfWeek*1000);
308  break;
309 
310  case DID_INS_2:
311  if(dataHdr.size+dataHdr.offset > sizeof(ins_2_t)){ /* Invalid */ return; }
312  g_msg.ins2 = d.ins2;
314  g_status.timeOfWeekMs = (uint32_t)(d.ins2.timeOfWeek*1000);
315  break;
316 
317  case DID_INS_3:
318  if(dataHdr.size+dataHdr.offset > sizeof(ins_3_t)){ /* Invalid */ return; }
319  g_status.week = d.ins1.week;
320  g_status.timeOfWeekMs = (uint32_t)(d.ins3.timeOfWeek*1000);
321  break;
322 
323  case DID_INS_4:
324  if(dataHdr.size+dataHdr.offset > sizeof(ins_4_t)){ /* Invalid */ return; }
325  g_status.week = d.ins4.week;
326  g_status.timeOfWeekMs = (uint32_t)(d.ins4.timeOfWeek*1000);
327  break;
328  }
329 
330  // Pass to callback
332  {
333  s_pfnHandleUinsData(dataHdr, d);
334  }
335 }
336 
337 
339 {
340  is_evb_log_stream stm = {};
341  uint8_t data[MAX_DATASET_SIZE];
342  p_data_hdr_t dataHdr = {};
343 
344  while (xStreamBufferReceive(g_xStreamBufferUINS, (void*)&stm, sizeof(is_evb_log_stream), 0) == sizeof(is_evb_log_stream))
345  {
346  if (stm.marker==DATA_CHUNK_MARKER)
347  {
348  switch (stm.ptype)
349  {
351  if (xStreamBufferReceive(g_xStreamBufferUINS, (void*)&dataHdr, sizeof(p_data_hdr_t), 0) == sizeof(p_data_hdr_t))
352  {
353  if (dataHdr.id < DID_COUNT && dataHdr.size < MAX_DATASET_SIZE)
354  {
355  if (xStreamBufferReceive(g_xStreamBufferUINS, (void*)data, dataHdr.size, 0) == dataHdr.size)
356  {
357  // Log Inertial Sense Binary data to SD Card
358  if(g_loggerEnabled)
359  {
360  logger.LogData(0, &dataHdr, data);
361  }
362  }
363  }
364  }
365  break;
366 
367  case _PTYPE_ASCII_NMEA:
368  break;
369 
370  case _PTYPE_UBLOX:
371 #if UBLOX_LOG_ENABLE
372  if (xStreamBufferReceive(g_xStreamBufferUINS, (void*)&data, stm.size, 0) == stm.size)
373  {
374 extern void log_ublox_raw_to_SD(cISLogger& logger, uint8_t *dataPtr, uint32_t dataSize);
375  log_ublox_raw_to_SD(logger, data, stm.size);
376  }
377 #endif
378  break;
379  }
380  }
381  }
382 }
383 
384 
386 {
387  if(error_check_config(&newCfg))
388  {
389  return;
390  }
391 
392  // Data is identical, no changes.
393  if( !memcmp(&newCfg, g_flashCfg, sizeof(evb_flash_cfg_t)) )
394  {
395  return;
396  }
397 
398  bool initCbPreset = false;
399  bool initIOconfig = false;
400  bool reinitXBee = false;
401  bool reinitWiFi = false;
402 
403  // Detect changes
404  if (newCfg.cbPreset != g_flashCfg->cbPreset ||
406  {
407  initCbPreset = true;
408  initIOconfig = true;
409  }
410  if (newCfg.radioPID != g_flashCfg->radioPID ||
411  newCfg.radioNID != g_flashCfg->radioNID ||
413  {
414  reinitXBee = true;
415  }
418  { // WiFi or TCP server preset changed
419  reinitWiFi = true;
420  }
421  int i = EVB_CFG_BITS_IDX_WIFI(newCfg.bits);
422  if (strncmp((const char*)(newCfg.wifi[i].ssid), (const char*)(g_flashCfg->wifi[i].ssid), WIFI_SSID_PSK_SIZE)!=0 ||
423  strncmp((const char*)(newCfg.wifi[i].psk), (const char*)(g_flashCfg->wifi[i].psk), WIFI_SSID_PSK_SIZE)!=0 ||
424  newCfg.server[i].ipAddr.u32 != g_flashCfg->server[i].ipAddr.u32 ||
425  newCfg.server[i].port != g_flashCfg->server[i].port)
426  { // WiFi or TCP settings changed
427  reinitWiFi = true;
428  }
429  i = EVB_CFG_BITS_IDX_SERVER(newCfg.bits);
430  if (newCfg.server[i].ipAddr.u32 != g_flashCfg->server[i].ipAddr.u32 ||
431  newCfg.server[i].port != g_flashCfg->server[i].port)
432  { // TCP settings changed
433  reinitWiFi = true;
434  }
440  {
441  initIOconfig = true;
442  }
443  if (g_flashCfg->CANbaud_kbps != newCfg.CANbaud_kbps)
444  {
446  CAN_init();
447  }
449  {
451  CAN_init();
452  }
453 
454  // Copy data from message to working location
455  *g_flashCfg = newCfg;
456 
457  // Apply changes
458  if(initCbPreset)
459  {
461  }
462  if(initIOconfig)
463  { // Update EVB IO config
464  board_IO_config();
465  }
466  if(reinitXBee)
467  {
468  xbee_init();
469  }
470  if(reinitWiFi)
471  {
472  wifi_reinit();
473  }
475 
476  // Enable flash write
479 }
480 
481 
482 void handle_data_from_host(is_comm_instance_t *comm, protocol_type_t ptype, uint32_t srcPort)
483 {
484  uint8_t *dataPtr = comm->dataPtr + comm->dataHdr.offset;
485 
486  switch(ptype)
487  {
489  switch(comm->dataHdr.id)
490  { // From host to EVB
491  case DID_EVB_STATUS:
493  break;
494 
495  case DID_EVB_FLASH_CFG:
496  evb_flash_cfg_t newCfg;
497  newCfg = *g_flashCfg;
498  is_comm_copy_to_struct(&newCfg, comm, sizeof(evb_flash_cfg_t));
499 
500  update_flash_cfg(newCfg);
501  break;
502 
503  case DID_EVB_DEBUG_ARRAY:
505  break;
506  }
507 
508  // Disable uINS bootloader if host sends IS binary data
510  break;
511 
513  if(comm->pkt.hdr.pid == PID_GET_DATA)
514  {
515  int n=0;
516  switch(comm->dataHdr.id)
517  {
518  case DID_DEV_INFO: n = is_comm_data(&g_commTx, DID_EVB_DEV_INFO, 0, sizeof(dev_info_t), (void*)&(g_evbDevInfo)); break;
519  case DID_EVB_STATUS: n = is_comm_data(&g_commTx, DID_EVB_STATUS, 0, sizeof(evb_status_t), (void*)&(g_status)); break;
520  case DID_EVB_FLASH_CFG: n = is_comm_data(&g_commTx, DID_EVB_FLASH_CFG, 0, sizeof(evb_flash_cfg_t), (void*)(g_flashCfg)); break;
521  case DID_EVB_DEBUG_ARRAY: n = is_comm_data(&g_commTx, DID_EVB_DEBUG_ARRAY, 0, sizeof(debug_array_t), (void*)&(g_debug)); break;
522  case DID_EVB_RTOS_INFO: n = is_comm_data(&g_commTx, DID_EVB_RTOS_INFO, 0, sizeof(evb_rtos_info_t), (void*)&(g_rtos)); g_enRtosStats = true; break;
523  }
524  if(n>0)
525  {
526  serWrite(srcPort, g_commTx.buf.start, n);
527  }
528 
529  // Disable uINS bootloader mode if host sends IS binary command
531  }
532  break;
533 
534  case _PTYPE_ASCII_NMEA:
535  {
536  uint32_t messageIdUInt = ASCII_MESSAGEID_TO_UINT(&(dataPtr[1]));
537  if(comm->dataHdr.size == 10)
538  { // 4 character commands (i.e. "$STPB*14\r\n")
539  switch (messageIdUInt)
540  {
541  case ASCII_MSG_ID_BLEN: // Enable bootloader (uINS)
543  break;
544 
545  case ASCII_MSG_ID_EBLE: // Enable bootloader (EVB)
546  // Disable uINS bootloader if host enables EVB bootloader
548 
550  break;
551  }
552  break;
553  }
554  else
555  { // General ASCII
556  switch (messageIdUInt)
557  {
558  case ASCII_MSG_ID_NELB: // SAM bootloader assistant (SAM-BA) enable
559  if (comm->dataHdr.size == 22 &&
560 // (pHandle == EVB2_PORT_USB) &&
561  strncmp((const char*)(&(comm->buf.start[6])), "!!SAM-BA!!", 6) == 0)
562  { // 16 character commands (i.e. "$NELB,!!SAM-BA!!\0*58\r\n")
564  }
565  break;
566 
567  default:
568  // Disable uINS bootloader if host sends larger ASCII sentence
570  break;
571  }
572  }
573  }
574  break;
575  }
576 
577  // Pass to callback
579  {
580  s_pfnHandleHostData(comm, ptype, srcPort);
581  }
582 }
583 
584 
585 #define CAN_COM_PORT 1
586 #define CAN_HDR 0xFC;
587 #define CAN_FTR 0xFE;
588 
589 void sendRadio(uint8_t *data, int dataSize, bool sendXbee, bool sendXrad)
590 {
592  { // Filter RTK Base Messages
593  protocol_type_t ptype;
594 
595  static is_comm_instance_t comm = {};
596  static uint8_t buffer[PKT_BUF_SIZE];
597  if (comm.buf.start == NULL)
598  { // Init buffer
599  is_comm_init(&comm, buffer, sizeof(buffer));
600  }
601 
602  for(uint8_t *ptr=data; dataSize>0; )
603  {
604  // Number of bytes to copy
605  int n = _MIN(dataSize, is_comm_free(&comm));
606 
607  // Copy data into buffer
608  memcpy(comm.buf.tail, ptr, n);
609  comm.buf.tail += n;
610  dataSize -= n;
611  ptr += n;
612 
613  while((ptype = is_comm_parse(&comm)) != _PTYPE_NONE)
614  {
615  // Parse Data
616  switch(ptype)
617  {
618  case _PTYPE_UBLOX:
619  case _PTYPE_RTCM3:
620  case _PTYPE_ASCII_NMEA:
621  if(sendXbee){ comWrite(EVB2_PORT_XBEE, comm.dataPtr, comm.dataHdr.size, LED_XBEE_TXD_PIN); }
622  if(sendXrad){ comWrite(EVB2_PORT_XRADIO, comm.dataPtr, comm.dataHdr.size, 0); }
623  break;
624  }
625  }
626  }
627  }
628  else
629  {
630  if(sendXbee){ comWrite(EVB2_PORT_XBEE, data, dataSize, LED_XBEE_TXD_PIN); }
631  if(sendXrad){ comWrite(EVB2_PORT_XRADIO, data, dataSize, 0); }
632  }
633 }
634 
635 
636 // This function only forwards data after complete valid packets are received
637 void com_bridge_smart_forward(uint32_t srcPort, uint32_t ledPin)
638 {
639  if(srcPort>=COM_RX_PORT_COUNT)
640  {
641  return;
642  }
643 
644  is_comm_instance_t &comm = g_comRxPort[srcPort].comm;
645 
646  // Get available size of comm buffer
647  int n = is_comm_free(&comm);
648 
649  if ((n = comRead(srcPort, comm.buf.tail, n, ledPin)) > 0)
650  {
652  { // When uINS bootloader is enabled forwarding is disabled below is_comm_parse(), so forward bootloader data here.
653  switch (srcPort)
654  {
656  case EVB2_PORT_UINS0: comWrite(EVB2_PORT_USB, comm.buf.tail, n, 0); break;
657  }
658  }
659 
660  // Update comm buffer tail pointer
661  comm.buf.tail += n;
662 
663  // Search comm buffer for valid packets
664  protocol_type_t ptype;
665  while((ptype = is_comm_parse(&comm)) != _PTYPE_NONE)
666  {
667  switch(srcPort)
668  {
669  case EVB2_PORT_USB:
670  case EVB2_PORT_XBEE:
671  case EVB2_PORT_SP330:
672  handle_data_from_host(&comm, ptype, srcPort);
673  break;
674  }
675 
676  if (ptype!=_PTYPE_NONE &&
677  ptype!=_PTYPE_PARSE_ERROR &&
679  { // Forward data
680  uint32_t pktSize = _MIN(comm.buf.scan - comm.pktPtr, PKT_BUF_SIZE);
681  com_bridge_forward(srcPort, comm.pktPtr, pktSize);
682 
683  // Send uINS data to Logging task
684  if (srcPort == g_flashCfg->uinsComPort)
685  {
686  is_evb_log_stream stm;
688  stm.ptype = ptype;
689  switch (ptype)
690  {
693 
694  stm.size = sizeof(p_data_hdr_t) + comm.dataHdr.size;
695  xStreamBufferSend(g_xStreamBufferUINS, (void*)&(stm), sizeof(is_evb_log_stream), 0);
696  xStreamBufferSend(g_xStreamBufferUINS, (void*)&(comm.dataHdr), sizeof(p_data_hdr_t), 0);
697  xStreamBufferSend(g_xStreamBufferUINS, (void*)(comm.dataPtr), comm.dataHdr.size, 0);
698  break;
699  case _PTYPE_UBLOX:
700 #if UBLOX_LOG_ENABLE
701  stm.size = pktSize;
702  xStreamBufferSend(g_xStreamBufferUINS, (void*)&(stm), sizeof(is_evb_log_stream), 0);
703  xStreamBufferSend(g_xStreamBufferUINS, (void*)(comm.pktPtr), pktSize, 0);
704 #endif
705  break;
706  }
707  }
708  }
709  }
710  }
711 }
712 
713 
714 void com_bridge_smart_forward_xstream(uint32_t srcPort, StreamBufferHandle_t xStreamBuffer);
715 void com_bridge_smart_forward_xstream(uint32_t srcPort, StreamBufferHandle_t xStreamBuffer)
716 {
717  if(srcPort>=COM_RX_PORT_COUNT)
718  {
719  return;
720  }
721 
722  is_comm_instance_t &comm = g_comRxPort[srcPort].comm;
723 
724  // Get available size of comm buffer
725  int n = is_comm_free(&comm);
726 
727  if ((n = xStreamBufferReceive(xStreamBuffer, comm.buf.tail, n, 0)) > 0)
728  {
729  // Update comm buffer tail pointer
730  comm.buf.tail += n;
731 
732  // Search comm buffer for valid packets
733  protocol_type_t ptype;
734  while ((ptype = is_comm_parse(&comm)) != _PTYPE_NONE)
735  {
736  if (srcPort == EVB2_PORT_USB)
737  {
738  handle_data_from_host(&comm, ptype, srcPort);
739  }
740 
741  if (ptype!=_PTYPE_NONE &&
742  ptype!=_PTYPE_PARSE_ERROR &&
744  { // Forward data
745  uint32_t pktSize = _MIN(comm.buf.scan - comm.pktPtr, PKT_BUF_SIZE);
746  com_bridge_forward(srcPort, comm.pktPtr, pktSize);
747  }
748  }
749  }
750 }
751 
752 
753 void com_bridge_forward(uint32_t srcPort, uint8_t *buf, int len)
754 {
755  uint32_t dstCbf = g_flashCfg->cbf[srcPort];
756 
757  if(dstCbf == 0 || len==0) // None
758  {
759  return;
760  }
761 
763  { // uINS bootloader mode enabled - don't allow forwarding on these ports
764  if(dstCbf & (1<<EVB2_PORT_USB))
765  {
766  comWrite(EVB2_PORT_USB, buf, len, 0);
767  }
768 
769  if(dstCbf & (1<<EVB2_PORT_UINS0))
770  {
772  }
773  }
774 
775  if(dstCbf & (1<<EVB2_PORT_UINS1))
776  {
778  }
779 
780  bool sendXbee = dstCbf & (1<<EVB2_PORT_XBEE) && xbee_runtime_mode(); // Disable XBee communications when being configured
781  bool sendXradio = dstCbf & (1<<EVB2_PORT_XRADIO);
782  if(sendXbee || sendXradio)
783  {
784  sendRadio(buf, len, sendXbee, sendXradio);
785  }
786 
787  if(dstCbf & (1<<EVB2_PORT_BLE))
788  {
790  }
791 
792  if(dstCbf & (1<<EVB2_PORT_SP330))
793  {
795  }
796 
797  if(dstCbf & (1<<EVB2_PORT_GPIO_H8))
798  {
800  }
801 
802 #if 0 // Disabled when forwarding data directly in wifi task
803  if(dstCbf & (1<<EVB2_PORT_WIFI))
804  {
805  xStreamBufferSend(g_xStreamBufferWiFiTx, (void*)buf, len, 0);
806  }
807 #endif
808 }
809 
810 
812 {
813  // USB CDC Rx =======================================================
815 
816  // uINS Ser0 Rx =======================================================
818 
819  // uINS Ser1 (TTL or SPI) Rx =======================================================
821 
822 #ifdef CONF_BOARD_SERIAL_XBEE // XBee Rx =======================================================
823  xbee_step(&comm);
824 #endif
825 
826 #ifdef CONF_BOARD_SERIAL_EXT_RADIO // External Radio Rx =======================================================
828 #endif
829 
830 #ifdef CONF_BOARD_SERIAL_ATWINC_BLE // ATWINC BLE Rx =======================================================
832 #endif
833 
834 #ifdef CONF_BOARD_SERIAL_SP330 // SP330 RS232/RS422 converter Rx =======================================================
836 #endif
837 
838 #ifdef CONF_BOARD_SERIAL_GPIO_H8 // H8 Header GPIO TTL Rx =======================================================
840 #endif
841 
842 #ifdef CONF_BOARD_SPI_ATWINC_WIFI // WiFi Rx =======================================================
844 #endif
845 
846 #ifdef CONF_BOARD_CAN1
847  uint8_t can_rx_message[CONF_MCAN_ELEMENT_DATA_SIZE];
848  uint32_t id;
849  uint8_t lenCAN;
850  is_can_message msg;
851  msg.startByte = CAN_HDR;
852  msg.endByte = CAN_FTR;
853  if((lenCAN = mcan_receive_message(&id, can_rx_message)) > 0)// && --timeout > 0))
854  {
855  msg.address = id;
856  msg.payload = *(is_can_payload*)can_rx_message;
857  msg.dlc = lenCAN;
858  com_bridge_forward(EVB2_PORT_CAN,(uint8_t*)&msg, sizeof(is_can_message));
859  }
860  /*Test CAN*/
861  //static uint8_t tx_message_1[8] = { 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87};
862  //mcan_send_message(0x100000A5, tx_message_1, CONF_MCAN_ELEMENT_DATA_SIZE);
863 #endif
864 }
865 
866 
868 {
869  s_pfnHandleUinsData = pfn;
870 }
871 
872 
874 {
875  s_pfnHandleHostData = pfn;
876 }
877 
878 
880 {
881  const size_t xTriggerLevel = 1;
885 
886  for(int i=0; i<COM_RX_PORT_COUNT; i++)
887  {
888  is_comm_init(&(g_comRxPort[i].comm), g_comRxPort[i].comm_buffer, PKT_BUF_SIZE);
889 
890  // Use alternate decode buffer on EVB so we can preserve and forward original packet received.
891  g_comRxPort[i].comm.altDecodeBuf = s_rxDecodeBuffer;
892  }
893 }
StreamBufferHandle_t g_xStreamBufferWiFiTx
uint32_t bits
Definition: data_sets.h:3154
#define _MIN(a, b)
Definition: ISConstants.h:298
d
is_comm_instance_t g_commTx
#define LED_ON(led)
Definition: user_board.h:236
StreamBufferHandle_t g_xStreamBufferWiFiRx
static uint8_t s_rxDecodeBuffer[PKT_BUF_SIZE]
uint32_t options
Definition: data_sets.h:1152
void enable_bootloader(int pHandle)
Definition: bootloaderApp.c:82
uint32_t h3sp330BaudRate
Definition: data_sets.h:3193
uint8_t dlc
ins_2_t ins2
Definition: data_sets.h:3529
uint32_t address
#define DID_EVB_DEBUG_ARRAY
Definition: data_sets.h:116
dev_info_t uInsInfo
Definition: globals.h:45
int serSetBaudRate(int serialNum, int baudrate)
Change USART baudrate. 0 on success, -1 on failure.
Definition: d_usartDMA.c:995
uint32_t cbf[EVB2_PORT_COUNT]
Definition: data_sets.h:3148
#define UNUSED(v)
Marking v as a unused parameter or value.
Definition: compiler.h:86
void(* pfnHandleUinsData)(p_data_hdr_t &dataHdr, uDatasets &data)
int comManagerValidateBaudRate(unsigned int baudRate)
Definition: com_manager.c:1587
void com_bridge_smart_forward(uint32_t srcPort, uint32_t ledPin)
static void ioport_set_pin_level(ioport_pin_t pin, bool level)
Set an IOPORT pin to a specified logical value.
Definition: ioport.h:275
#define xStreamBufferCreate(xBufferSizeBytes, xTriggerLevelBytes)
int comWrite(int serialNum, const unsigned char *buf, int size, uint32_t ledPin)
No parity.
#define US_MR_PAR_EVEN
(US_MR) Even parity
uint32_t used
Definition: USBD.h:95
#define US_MR_NBSTOP_1_BIT
(US_MR) 1 stop bit
#define LED_WIFI_RXD_PIN
Definition: user_board.h:285
ins_1_t ins1
Definition: data_sets.h:3528
int xbee_runtime_mode(void)
Definition: xbee.cpp:364
void CAN_init(void)
Definition: CAN.cpp:26
#define UART_XBEE_N_DTR_PIN
Definition: user_board.h:184
uint32_t id
Definition: ISComm.h:376
#define DID_INS_1
Definition: data_sets.h:38
uint32_t radioNID
Definition: data_sets.h:3160
uint32_t size
Definition: ISComm.h:379
void(* pfnHandleHostData)(is_comm_instance_t *comm, protocol_type_t ptype, uint32_t srcPort)
void xbee_step(is_comm_instance_t *comm)
Definition: xbee.cpp:246
is_can_payload payload
evb_rtos_info_t g_rtos
Definition: globals.c:27
size_t xStreamBufferReceive(StreamBufferHandle_t xStreamBuffer, void *pvRxData, size_t xBufferLengthBytes, TickType_t xTicksToWait) PRIVILEGED_FUNCTION
#define LE32_TO_CPU(x)
Definition: compiler.h:904
uint64_t bits
Definition: data_sets.h:1359
void xbee_init(void)
Definition: xbee.cpp:236
Odd parity.
bool d_usartDMA_callback_cdc_enable(void)
Definition: d_usartDMA.c:160
bool g_enRtosStats
Definition: globals.c:35
#define PKT_BUF_SIZE
Definition: ISComm.h:98
static pfnHandleUinsData s_pfnHandleUinsData
void com_bridge_forward(uint32_t srcPort, uint8_t *buf, int len)
debug_array_t g_debug
Definition: globals.c:26
#define DID_COUNT
Definition: data_sets.h:138
packet_hdr_t hdr
Definition: ISComm.h:358
#define DID_DEV_INFO
Definition: data_sets.h:35
#define DID_RMC
Definition: data_sets.h:43
#define DID_INS_2
Definition: data_sets.h:39
int is_comm_get_data(is_comm_instance_t *instance, uint32_t dataId, uint32_t offset, uint32_t size, uint32_t periodMultiple)
Definition: ISComm.c:588
#define NULL
Definition: nm_bsp.h:52
StreamBufferHandle_t g_xStreamBufferUINS
void evbUiRefreshLedCfg()
uint32_t h4xRadioBaudRate
Definition: data_sets.h:3196
int serRead(int serialNum, unsigned char *buf, int size)
Read data on USART. Returns number of bytes read. With use of the PDCA ring buffer, buffer overrun may occur if bytes are received is greater than bytes read plus buffer size.
Definition: d_usartDMA.c:782
#define LED_INS_TXD_PIN
Definition: user_board.h:280
char is_comm_copy_to_struct(void *sptr, const is_comm_instance_t *instance, const unsigned int maxsize)
Definition: ISComm.c:1008
void d_usartDMA_callback_cdc_disable(void)
Definition: d_usartDMA.c:166
#define LED_WIFI_TXD_PIN
Definition: user_board.h:286
#define WIFI_SSID_PSK_SIZE
Definition: data_sets.h:3085
void uINS_stream_enable_PPD(void)
static pfnHandleHostData s_pfnHandleHostData
void sendRadio(uint8_t *data, int dataSize, bool sendXbee, bool sendXrad)
char psk[WIFI_SSID_PSK_SIZE]
Definition: data_sets.h:3093
uint32_t port
Definition: data_sets.h:3106
int error_check_config(evb_flash_cfg_t *cfg)
Definition: globals.c:407
#define US_MR_PAR_NO
(US_MR) No parity
void communications_init(void)
ins_2_t ins2
Definition: globals.h:46
uint32_t flash_write_needed
Definition: globals.h:51
uint32_t timeOfWeekMs
Definition: data_sets.h:3063
uint32_t week
Definition: data_sets.h:3060
uint32_t week
Definition: CAN_comm.h:26
uint8_t startByte
uint32_t u32
Definition: data_sets.h:3101
#define CONF_MCAN_ELEMENT_DATA_SIZE
Definition: conf_mcan.h:77
Parity forced to 1 (mark)
comm_rx_port_t g_comRxPort[COM_RX_PORT_COUNT]
uint32_t radioPowerLevel
Definition: data_sets.h:3163
#define NULLPTR
Definition: ISConstants.h:426
ins_3_t ins3
Definition: data_sets.h:3530
#define LED_XBEE_TXD_PIN
Definition: user_board.h:283
double timeOfWeek
Definition: data_sets.h:417
void com_bridge_apply_preset(evb_flash_cfg_t *cfg)
Definition: globals.c:147
void callback_cdc_set_config(uint8_t port, usb_cdc_line_coding_t *cfg)
#define US_MR_NBSTOP_2_BIT
(US_MR) 2 stop bits
bool LogData(unsigned int device, p_data_hdr_t *dataHdr, const uint8_t *dataBuf)
Definition: ISLogger.cpp:348
is_comm_buffer_t buf
Definition: ISComm.h:489
int spiTouINS_serWrite(const unsigned char *buf, int size)
Definition: spiTouINS.c:393
int is_comm_set_data(is_comm_instance_t *instance, uint32_t dataId, uint32_t offset, uint32_t size, void *data)
Definition: ISComm.c:634
void comunications_set_uins_data_callback(pfnHandleUinsData pfn)
#define UART_XBEE_N_RTS_PIN
Definition: user_board.h:181
#define STREAM_BUFFER_SIZE
Definition: globals.h:82
#define US_MR_PAR_MARK
(US_MR) Parity forced to 1 (Mark)
int is_comm_free(is_comm_instance_t *instance)
Definition: ISComm.c:462
uint8_t * pktPtr
Definition: ISComm.h:513
evb_status_t g_status
Definition: globals.c:21
uint32_t cbOptions
Definition: data_sets.h:3151
#define PID_GET_DATA
Definition: ISComm.h:183
uint8_t cbPreset
Definition: data_sets.h:3142
#define US_MR_CHRL_8_BIT
(US_MR) Character length is 8 bits
#define US_MR_PAR_ODD
(US_MR) Odd parity
uint8_t * start
Definition: ISComm.h:448
uint32_t g_uInsBootloaderEnableTimeMs
Definition: globals.c:34
ins_4_t ins4
Definition: data_sets.h:3531
uint32_t g_comm_time_ms
Definition: globals.c:32
uint8_t * tail
Definition: ISComm.h:460
uint8_t endByte
#define DATA_CHUNK_MARKER
Definition: DataChunk.h:23
void wifi_reinit(void)
Definition: wifi.c:252
void callback_cdc_set_dtr(uint8_t port, bool b_enable)
int comRead(int serialNum, unsigned char *buf, int bufSize, uint32_t ledPin)
dev_info_t g_evbDevInfo
Definition: globals.c:19
void uINS_stream_stop_all(void)
void uINS_stream_enable_std(void)
USBInterfaceDescriptor data
uint8_t * scan
Definition: ISComm.h:463
uint8_t pid
Definition: ISComm.h:323
union evb_server_t::@40 ipAddr
Even parity.
evb_server_t server[NUM_WIFI_PRESETS]
Definition: data_sets.h:3169
#define DID_EVB_DEV_INFO
Definition: data_sets.h:127
protocol_type_t ptype
#define CAN_FTR
void is_comm_init(is_comm_instance_t *instance, uint8_t *buffer, int bufferSize)
Definition: ISComm.c:185
#define DID_EVB_FLASH_CFG
Definition: data_sets.h:115
void comunications_set_host_data_callback(pfnHandleHostData pfn)
void update_flash_cfg(evb_flash_cfg_t &newCfg)
char ssid[WIFI_SSID_PSK_SIZE]
Definition: data_sets.h:3090
#define DID_EVB_STATUS
Definition: data_sets.h:114
#define EVB_CFG_BITS_IDX_SERVER(bits)
Definition: data_sets.h:3125
uint32_t marker
#define DID_INS_3
Definition: data_sets.h:99
evb_flash_cfg_t * g_flashCfg
Definition: globals.c:22
p_data_hdr_t dataHdr
Definition: ISComm.h:507
void enable_bootloader_assistant(void)
#define LED_INS_RXD_PIN
Definition: user_board.h:279
uint32_t flash_write_enable
Definition: globals.h:53
#define ASCII_MESSAGEID_TO_UINT(c4)
Definition: ISComm.h:242
1.5 stop bits
#define PORT_SEL_USB
bool g_loggerEnabled
Definition: globals.c:33
#define CAN_HDR
size_t xStreamBufferSend(StreamBufferHandle_t xStreamBuffer, const void *pvTxData, size_t xDataLengthBytes, TickType_t xTicksToWait) PRIVILEGED_FUNCTION
uint8_t mcan_receive_message(uint32_t *id_value, uint8_t *data)
Definition: CAN.cpp:78
void handle_data_from_host(is_comm_instance_t *comm, protocol_type_t ptype, uint32_t srcPort)
void step_com_bridge(is_comm_instance_t &comm)
void * StreamBufferHandle_t
Definition: stream_buffer.h:65
protocol_type_t
Definition: ISComm.h:75
uint8_t * altDecodeBuf
Definition: ISComm.h:516
protocol_type_t is_comm_parse(is_comm_instance_t *instance)
Definition: ISComm.c:514
int is_comm_data(is_comm_instance_t *instance, uint32_t dataId, uint32_t offset, uint32_t size, void *data)
Definition: ISComm.c:639
packet_t pkt
Definition: ISComm.h:522
Autogenerated API include file for the Atmel Software Framework (ASF)
uint32_t portOptions
Definition: data_sets.h:3190
#define RMC_PRESET_PPD_BITS
Definition: data_sets.h:1340
Parity forced to 0 (space)
void board_IO_config(void)
Definition: init.c:376
nvr_manage_t g_nvr_manage_config
Definition: globals.c:23
int spiTouINS_serRead(unsigned char *buf, int size)
Definition: spiTouINS.c:517
void log_ublox_raw_to_SD(cISLogger &logger, uint8_t *dataPtr, uint32_t dataSize)
#define US_MR_PAR_SPACE
(US_MR) Parity forced to 0 (Space)
#define COM_RX_PORT_COUNT
uint32_t offset
Definition: ISComm.h:382
void handle_data_from_uINS(p_data_hdr_t &dataHdr, uint8_t *data)
int is_comm_stop_broadcasts_current_port(is_comm_instance_t *instance)
Definition: ISComm.c:654
#define MAX_DATASET_SIZE
Definition: ISComm.h:91
uint32_t radioPID
Definition: data_sets.h:3157
uint8_t uinsComPort
Definition: data_sets.h:3181
evb_msg_t g_msg
Definition: globals.c:25
#define DID_INS_4
Definition: data_sets.h:100
int serRxUsed(int serialNum)
Returns number of bytes used in Rx buffer.
Definition: d_usartDMA.c:301
uint32_t can_receive_address
Definition: data_sets.h:3178
Line Coding structure.
#define EVB_CFG_BITS_IDX_WIFI(bits)
Definition: data_sets.h:3124
uint32_t size
evb_wifi_t wifi[NUM_WIFI_PRESETS]
Definition: data_sets.h:3166
#define US_MR_NBSTOP_1_5_BIT
(US_MR) 1.5 stop bit (SYNC = 0) or reserved (SYNC = 1)
void log_uINS_data(cISLogger &logger, is_comm_instance_t &comm)
char copyDataPToStructP2(void *sptr, const p_data_hdr_t *dataHdr, const uint8_t *dataBuf, const unsigned int maxsize)
Definition: ISComm.c:989
uint32_t h8gpioBaudRate
Definition: data_sets.h:3199
uint32_t CANbaud_kbps
Definition: data_sets.h:3175
#define DID_EVB_RTOS_INFO
Definition: data_sets.h:117
void com_bridge_smart_forward_xstream(uint32_t srcPort, StreamBufferHandle_t xStreamBuffer)
is_comm_instance_t comm
int serWrite(int serialNum, const unsigned char *buf, int size)
Write data on USART. Returns number of bytes written.
Definition: d_usartDMA.c:443
uint8_t * dataPtr
Definition: ISComm.h:510


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:57