2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file neo6m.cpp
|
|
|
|
* @brief Ublox 6M hardware UART driver
|
|
|
|
*
|
|
|
|
* http://www.u-blox.com/en/gps-modules/pvt-modules/previous-generations/neo-6-family.html
|
|
|
|
*
|
|
|
|
* Technically any UART GPS should work with this driver since NMEA protocol is pretty common anyway
|
|
|
|
*
|
|
|
|
* @date Dec 28, 2013
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
* Kot_dnz 2014
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
// todo: MISRA does not like time.h
|
|
|
|
#include <time.h>
|
2018-09-16 19:26:57 -07:00
|
|
|
#include "global.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-04-12 17:52:51 -07:00
|
|
|
#if EFI_UART_GPS
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#include "console_io.h"
|
|
|
|
#include "eficonsole.h"
|
|
|
|
#include "pin_repository.h"
|
|
|
|
#include "nmea.h"
|
|
|
|
#include "neo6m.h"
|
|
|
|
#include "rtc_helper.h"
|
2019-01-09 19:16:30 -08:00
|
|
|
#include "engine.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static SerialConfig GPSserialConfig = { GPS_SERIAL_SPEED, 0, USART_CR2_STOP1_BITS | USART_CR2_LINEN, 0 };
|
|
|
|
static THD_WORKING_AREA(gpsThreadStack, UTILITY_THREAD_STACK_SIZE);
|
|
|
|
|
|
|
|
// this field holds our current state
|
|
|
|
static loc_t GPSdata;
|
|
|
|
|
|
|
|
static int gpsMesagesCount = 0;
|
|
|
|
static int uartErrors = 0;
|
|
|
|
|
|
|
|
// todo: some data structure for coordinates location
|
|
|
|
// todo:
|
|
|
|
float getCurrentSpeed(void) {
|
|
|
|
return GPSdata.speed;
|
|
|
|
}
|
|
|
|
|
2019-01-09 19:16:30 -08:00
|
|
|
EXTERN_ENGINE;
|
|
|
|
|
|
|
|
static void printGpsInfo(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2021-04-19 05:11:59 -07:00
|
|
|
efiPrintf("GPS RX %s", hwPortname(CONFIG(gps_rx_pin)));
|
|
|
|
efiPrintf("GPS TX %s", hwPortname(CONFIG(gps_tx_pin)));
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-04-19 05:11:59 -07:00
|
|
|
efiPrintf("m=%d,e=%d: vehicle speed = %.2f", gpsMesagesCount, uartErrors, getCurrentSpeed());
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
float sec = currentTimeMillis() / 1000.0;
|
2021-04-19 05:11:59 -07:00
|
|
|
efiPrintf("communication speed: %.2f", gpsMesagesCount / sec);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-01-23 09:05:14 -08:00
|
|
|
print("GPS latitude = %.2f\r\n", GPSdata.latitude);
|
|
|
|
print("GPS longitude = %.2f\r\n", GPSdata.longitude);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct tm curTm;
|
|
|
|
|
|
|
|
static void onGpsMessage(char *buffer) {
|
|
|
|
|
|
|
|
gps_location(&GPSdata, buffer);
|
|
|
|
date_get_tm(&curTm);
|
|
|
|
|
|
|
|
if (GPSdata.quality == 4 && GPSdata.GPStm.tm_year > 0 && GPSdata.GPStm.tm_sec != curTm.tm_sec) {
|
|
|
|
// quality =4 (valis GxRMC), year > 0, and difference more then second
|
|
|
|
date_set_tm(&GPSdata.GPStm); // set GPS time
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
gpsMesagesCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we do not want this on stack, right?
|
|
|
|
static char gps_str[GPS_MAX_STRING];
|
|
|
|
|
2016-01-15 20:01:43 -08:00
|
|
|
static THD_FUNCTION(GpsThreadEntryPoint, arg) {
|
2015-07-10 06:01:56 -07:00
|
|
|
(void) arg;
|
|
|
|
chRegSetThreadName("GPS thread");
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
while (true) {
|
2018-01-23 18:17:30 -08:00
|
|
|
msg_t charbuf = streamGet(GPS_SERIAL_DEVICE);
|
2015-07-10 06:01:56 -07:00
|
|
|
if (charbuf == 10 || count == GPS_MAX_STRING) { // if 0xD,0xA or limit
|
|
|
|
if (count >= 1)
|
|
|
|
gps_str[--count] = '\0'; // delete 0xD
|
|
|
|
|
|
|
|
// scheduleMsg(&logger, "got GPS [%s]", gps_str);
|
|
|
|
|
|
|
|
// 'gps_str' string completed
|
|
|
|
onGpsMessage(gps_str);
|
|
|
|
memset(&gps_str, '\0', GPS_MAX_STRING); // clear buffer
|
|
|
|
count = 0;
|
|
|
|
} else {
|
|
|
|
gps_str[count++] = charbuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-27 10:02:00 -08:00
|
|
|
static bool isGpsEnabled() {
|
2021-01-08 17:01:26 -08:00
|
|
|
return (isBrainPinValid(CONFIG(gps_rx_pin)) &&
|
|
|
|
isBrainPinValid(CONFIG(gps_tx_pin)));
|
2016-12-27 10:02:00 -08:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void initGps(void) {
|
2018-02-25 18:17:59 -08:00
|
|
|
if (!isGpsEnabled())
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
sdStart(GPS_SERIAL_DEVICE, &GPSserialConfig);
|
|
|
|
// GPS we have USART1: PB7 -> USART1_RX and PB6 -> USART1_TX
|
2019-12-11 14:48:55 -08:00
|
|
|
efiSetPadMode("GPS tx", CONFIG(gps_tx_pin), PAL_MODE_ALTERNATE(7));
|
|
|
|
efiSetPadMode("GPS rx", CONFIG(gps_rx_pin), PAL_MODE_ALTERNATE(7));
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
// todo: add a thread which would save location. If the GPS 5Hz - we should save the location each 200 ms
|
2018-12-27 06:40:40 -08:00
|
|
|
chThdCreateStatic(gpsThreadStack, sizeof(gpsThreadStack), LOWPRIO, (tfunc_t)(void*) GpsThreadEntryPoint, NULL);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
addConsoleAction("gpsinfo", &printGpsInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* EFI_UART_GPS */
|