LP-311 Remove basic/advanced stabilization tab auto-switch (autotune/txpid lock issues)
[librepilot.git] / flight / modules / gpsp / gps9gpshandler.c
blob8e0e82cccc10340804df4ffdc1c82127c2ab8ebb
1 /**
2 ******************************************************************************
4 * @file gps9gpshandler.c
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2014.
6 * @brief handler for GPSV9 onboard ubx gps module.
7 * --
8 * @see The GNU Public License (GPL) Version 3
10 *****************************************************************************/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <openpilot.h>
28 #include <pios_struct_helper.h>
29 #include <pios_helpers.h>
30 #include <ubx_utils.h>
31 #include <pios_ubx_ddc.h>
33 #include "gps9gpshandler.h"
34 #include "gps9protocol.h"
36 uint32_t lastUnsentData = 0;
37 uint8_t gpsBuffer[GPS_BUFFER_SIZE];
38 uint8_t fcBuffer[FC_BUFFER_SIZE];
40 void handleGPS()
42 bool completeSentenceInBuffer = false;
43 int8_t maxCount = 2;
45 do {
46 int32_t datacounter = PIOS_UBX_DDC_GetAvailableBytes(PIOS_I2C_GPS);
47 if (datacounter > 0) {
48 uint8_t toRead = (uint32_t)datacounter > GPS_BUFFER_SIZE - lastUnsentData ? GPS_BUFFER_SIZE - lastUnsentData : (uint8_t)datacounter;
49 uint8_t toSend = toRead + lastUnsentData;
50 PIOS_UBX_DDC_ReadData(PIOS_I2C_GPS, gpsBuffer + lastUnsentData, toRead);
52 uint8_t *lastSentence;
53 uint16_t lastSentenceLength;
54 completeSentenceInBuffer = ubx_getLastSentence(gpsBuffer, toSend, &lastSentence, &lastSentenceLength);
55 if (completeSentenceInBuffer) {
56 toSend = (uint8_t)(lastSentence - gpsBuffer + lastSentenceLength);
57 PIOS_COM_SendBuffer(pios_com_main_id, gpsBuffer, toSend);
58 // move unsent data to the beginning of gpsBuffer to be sent next time
59 lastUnsentData = lastUnsentData > toSend ? lastUnsentData - toSend : 0;
60 if (lastUnsentData > 0) {
61 memcpy(gpsBuffer, (gpsBuffer + toSend), lastUnsentData);
63 } else {
64 // toss the buffer contents if it's full and there are no complete
65 // ublox sentences in it. This happens if the module is sending NMEA
66 lastUnsentData = toSend < GPS_BUFFER_SIZE ? toSend : 0;
70 datacounter = PIOS_COM_ReceiveBuffer(pios_com_main_id, fcBuffer, FC_BUFFER_SIZE, 0);
71 if (datacounter > 0) {
72 PIOS_UBX_DDC_WriteData(PIOS_I2C_GPS, fcBuffer, datacounter);
74 if (maxCount) {
75 // Note: this delay is needed as querying too quickly the UBX module's I2C(DDC)
76 // port causes a lot of weird issues (it stops sending nav sentences)
77 vTaskDelay(2 * configTICK_RATE_HZ / 1000);
79 } while (maxCount--);
82 typedef struct {
83 uint8_t size;
84 const uint8_t *sentence;
85 } ubx_init_sentence;
88 void setupGPS()
90 CfgPrtPkt cfgprt;
92 cfgprt.fragments.data.portID = CFG_PRT_DATA_PORTID_DDC;
93 cfgprt.fragments.data.reserved0 = 0;
94 cfgprt.fragments.data.txReady = CFG_PRT_DATA_TXREADI_DISABLED;
95 cfgprt.fragments.data.mode = CFG_PRT_DATA_MODE_ADDR;
96 cfgprt.fragments.data.reserved3 = 0;
97 cfgprt.fragments.data.inProtoMask = CFG_PRT_DATA_PROTO_UBX | CFG_PRT_DATA_PROTO_NMEA | CFG_PRT_DATA_PROTO_RTCM;
98 cfgprt.fragments.data.outProtoMask = CFG_PRT_DATA_PROTO_UBX;
99 cfgprt.fragments.data.flags = 0;
100 cfgprt.fragments.data.reserved5 = 0;
102 ubx_buildPacket(&cfgprt.packet, UBX_CFG_CLASS, UBX_CFG_PRT, sizeof(CfgPrtData));
103 PIOS_UBX_DDC_WriteData(PIOS_I2C_GPS, cfgprt.packet.binarystream, sizeof(CfgPrtPkt));