diff --git a/flight/libraries/inc/ubx_utils.h b/flight/libraries/inc/ubx_utils.h new file mode 100644 index 000000000..387b0f7ea --- /dev/null +++ b/flight/libraries/inc/ubx_utils.h @@ -0,0 +1,62 @@ +/** + ****************************************************************************** + * + * @file ubx_utils.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2014. + * @brief UBX Protocol utilities + * -- + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef UBX_UTILS_H_ +#define UBX_UTILS_H_ + +#include +#include + +typedef struct { + uint8_t syn1; + uint8_t syn2; + uint8_t class; + uint8_t id; + uint16_t len; +} __attribute__((packed)) UBXHeader_t; + +typedef struct { + uint8_t chk1; + uint8_t chk2; +} __attribute__((packed)) UBXFooter_t; + +typedef union { + uint8_t bynarystream[0]; + struct { + UBXHeader_t header; + uint8_t payload[0]; + } packet; +} UBXPacket_t; + +#define UBX_HEADER_LEN (sizeof(UBXHeader_t)) + +#define UBX_SYN1 0xB5 +#define UBX_SYN2 0x62 + +bool ubx_getLastSentence(uint8_t *data, uint16_t bufferCount, uint8_t * *lastSentence, uint16_t *lenght); +void ubx_appendChecksum(UBXPacket_t *pkt); +void ubx_buildPacket(UBXPacket_t *pkt, uint8_t packetClass, uint8_t packetId, uint16_t len); + +#endif /* UBX_UTILS_H_ */ diff --git a/flight/libraries/ubx_utils.c b/flight/libraries/ubx_utils.c new file mode 100644 index 000000000..ac33285cc --- /dev/null +++ b/flight/libraries/ubx_utils.c @@ -0,0 +1,77 @@ +/** + ****************************************************************************** + * + * @file ubx_utils.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2014. + * @brief UBX Protocol utilities. + * -- + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include +bool ubx_getLastSentence(uint8_t *data, uint16_t bufferCount, uint8_t * *lastSentence, uint16_t *lenght) +{ + const uint8_t packet_overhead = UBX_HEADER_LEN + 2; + uint8_t *current = data + bufferCount - packet_overhead; + + while (current >= data) { + // look for a ubx a sentence + if (current[0] == UBX_SYN1 && current[1] == UBX_SYN2) { + // check whether it fits the current buffer (whole sentence is into buffer) + uint16_t len = current[4] + (current[5] << 8); + if (len + packet_overhead + current <= data + bufferCount) { + *lastSentence = current; + *lenght = len + packet_overhead; + return true; + } + } + current--; + } + // no complete sentence found + return false; +} + +void ubx_buildPacket(UBXPacket_t *pkt, uint8_t packetClass, uint8_t packetId, uint16_t len) +{ + pkt->packet.header.syn1 = UBX_SYN1; + pkt->packet.header.syn2 = UBX_SYN2; + + // don't make any assumption on alignments... + ((uint8_t *)&pkt->packet.header.len)[0] = len & 0xFF; + ((uint8_t *)&pkt->packet.header.len)[1] = (len >> 8) & 0xFF; + + pkt->packet.header.class = packetClass; + pkt->packet.header.id = packetId; + ubx_appendChecksum(pkt); +} + +void ubx_appendChecksum(UBXPacket_t *pkt) +{ + uint8_t chkA = 0; + uint8_t chkB = 0; + uint16_t len = ((uint8_t *)&pkt->packet.header.len)[0] | ((uint8_t *)&pkt->packet.header.len)[1] << 8; + + // From class field to the end of payload + for (uint8_t i = 2; i < len + UBX_HEADER_LEN; i++) { + chkA += pkt->bynarystream[i]; + chkB += chkA; + } + ; + pkt->packet.payload[len] = chkA; + pkt->packet.payload[len + 1] = chkB; +} diff --git a/flight/targets/boards/gpsplatinum/firmware/Makefile b/flight/targets/boards/gpsplatinum/firmware/Makefile index 86799241b..3c6bd7a29 100644 --- a/flight/targets/boards/gpsplatinum/firmware/Makefile +++ b/flight/targets/boards/gpsplatinum/firmware/Makefile @@ -50,6 +50,7 @@ SRC += $(CMSIS_DEVICEDIR)/system_stm32f0xx.c ## Application Core SRC += $(OPSYSTEM)/gpsp.c SRC += $(OPSYSTEM)/pios_board.c +SRC += $(FLIGHTLIB)/ubx_utils.c #SRC += $(FLIGHTLIB)/alarms.c #SRC += $(OPUAVOBJ)/uavobjectmanager.c #SRC += $(OPUAVOBJ)/eventdispatcher.c