rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
gps_uart.cpp
Go to the documentation of this file.
1/**
2 * @file gps_uart.cpp
3 * @brief GPS receiver hardware UART driver
4 *
5 * Tested and developed for NEO-6M
6 * http://www.u-blox.com/en/gps-modules/pvt-modules/previous-generations/neo-6-family.html
7 * Technically any UART GPS should work with this driver since NMEA protocol is pretty common anyway
8 *
9 * @date Dec 28, 2013
10 * @author Andrey Belomutskiy, (c) 2012-2020
11 * Kot_dnz 2014
12 */
13
14#include "pch.h"
15
16#if EFI_UART_GPS
17#include <string.h>
18
19#include "rusefi_types.h"
20#include "console_io.h"
21#include "eficonsole.h"
22#include "nmea.h"
23#include "gps_uart.h"
24#include "rtc_helper.h"
25
26static SerialConfig GPSserialConfig = { GPS_SERIAL_SPEED, 0, USART_CR2_STOP1_BITS | USART_CR2_LINEN, 0 };
27static THD_WORKING_AREA(gpsThreadStack, UTILITY_THREAD_STACK_SIZE);
28
29// this field holds our current state
32
33static int gpsMessageCount = 0;
34static int uartErrors = 0;
35
36// TODO: some data structure for coordinates location
37
38float getCurrentSpeed(void) {
39 // TODO: ???
40 return GPSdata.speed;
41}
42
43static void printGpsInfo() {
44 efiPrintf("GPS RX %s", hwPortname(engineConfiguration->gps_rx_pin));
45 efiPrintf("GPS TX %s", hwPortname(engineConfiguration->gps_tx_pin));
46
47 efiPrintf("m=%d,e=%d: vehicle speed = %.2f", gpsMessageCount, uartErrors, getCurrentSpeed());
48
49 float sec = getTimeNowMs() / 1000.0;
50 efiPrintf("communication speed: %.2f", gpsMessageCount / sec);
51
52 efiPrintf("GPS latitude = %.2f\r\n", GPSdata.latitude);
53 efiPrintf("GPS longitude = %.2f\r\n", GPSdata.longitude);
54}
55
56static void onGpsMessage(const char * const buffer) {
58
59 if (GPSdata.quality == 4 && GPSdata.time.year > 0) {
61 if (GPSdata.time.second != dateTime.second) {
62 // quality =4 (valid GxRMC), year > 0, and difference more than second
64 }
65 }
66
68}
69
70// we do not want this on stack, right?
71static char gps_str[GPS_MAX_STRING];
72
73static THD_FUNCTION(GpsThreadEntryPoint, arg) {
74 (void) arg;
75 chRegSetThreadName("GPS thread");
76
77 int count = 0;
78
79 while (true) {
80 msg_t charbuf = streamGet(GPS_SERIAL_DEVICE);
81 if (charbuf == 10 || count == GPS_MAX_STRING) { // if 0xD,0xA or limit
82 if (count >= 1)
83 gps_str[--count] = '\0'; // delete 0xD
84
85 // scheduleMsg(&logger, "got GPS [%s]", gps_str);
86
87 // 'gps_str' string completed
89 memset(&gps_str, '\0', GPS_MAX_STRING); // clear buffer
90 count = 0;
91 } else {
92 gps_str[count++] = charbuf;
93 }
94 }
95}
96
97static bool isGpsEnabled() {
98 return (isBrainPinValid(engineConfiguration->gps_rx_pin) &&
100}
101
102void initGps(void) {
103 if (!isGpsEnabled())
104 return;
105
106
107 sdStart(GPS_SERIAL_DEVICE, &GPSserialConfig);
108// GPS we have USART1: PB7 -> USART1_RX and PB6 -> USART1_TX
109 efiSetPadMode("GPS tx", engineConfiguration->gps_tx_pin, PAL_MODE_ALTERNATE(7));
110 efiSetPadMode("GPS rx", engineConfiguration->gps_rx_pin, PAL_MODE_ALTERNATE(7));
111
112// todo: add a thread which would save location. If the GPS 5Hz - we should save the location each 200 ms
113 chThdCreateStatic(gpsThreadStack, sizeof(gpsThreadStack), LOWPRIO, (tfunc_t)(void*) GpsThreadEntryPoint, NULL);
114
115 addConsoleAction("gpsinfo", &printGpsInfo);
116}
117
118#endif // EFI_UART_GPS
void efiSetPadMode(const char *msg, brain_pin_e brainPin, iomode_t mode)
void addConsoleAction(const char *token, Void callback)
Register console action without parameters.
Console package entry point header.
efitimems_t getTimeNowMs()
Returns the 32 bit number of milliseconds since the board initialization.
Definition efitime.cpp:34
static constexpr engine_configuration_s * engineConfiguration
static char gps_str[GPS_MAX_STRING]
Definition gps_uart.cpp:71
static SerialConfig GPSserialConfig
Definition gps_uart.cpp:26
static bool isGpsEnabled()
Definition gps_uart.cpp:97
static int uartErrors
Definition gps_uart.cpp:34
static THD_WORKING_AREA(gpsThreadStack, UTILITY_THREAD_STACK_SIZE)
static int gpsMessageCount
Definition gps_uart.cpp:33
static THD_FUNCTION(GpsThreadEntryPoint, arg)
Definition gps_uart.cpp:73
static loc_t GPSdata
Definition gps_uart.cpp:30
static efidatetime_t lastDateTime
Definition gps_uart.cpp:31
static void printGpsInfo()
Definition gps_uart.cpp:43
void initGps(void)
Definition gps_uart.cpp:102
float getCurrentSpeed(void)
Definition gps_uart.cpp:38
static void onGpsMessage(const char *const buffer)
Definition gps_uart.cpp:56
void gps_location(loc_t *coord, char const *const buffer)
Definition nmea.cpp:298
const char * hwPortname(brain_pin_e brainPin)
bool isBrainPinValid(brain_pin_e brainPin)
void setRtcDateTime(efidatetime_t const *const dateTime)
efidatetime_t getRtcDateTime()
Real Time Clock helper.
float latitude
Definition nmea.h:28
efidatetime_t time
Definition nmea.h:33
float longitude
Definition nmea.h:29
int quality
Definition nmea.h:35
float speed
Definition nmea.h:30
uint32_t year
static BigBufferHandle buffer
uint16_t count
Definition tunerstudio.h:1