diff --git a/meson.build b/meson.build
index 86a20fe4..f3fce9ba 100644
--- a/meson.build
+++ b/meson.build
@@ -426,7 +426,8 @@ mduv3x0_def += openrtx_def + stm32f405_def
##
md9600_src = ['platform/targets/MD-9600/platform.c',
'platform/targets/MD-9600/hwconfig.c',
- 'platform/drivers/GPS/GPS_MDx.cpp',
+ 'platform/drivers/GPS/gps_stm32.cpp',
+ 'platform/drivers/GPS/nmea_rbuf.c',
'platform/drivers/display/ST7567_MD9600.c',
'platform/drivers/keyboard/keyboard_MD9600.c',
'platform/drivers/backlight/backlight_MDx.c',
diff --git a/platform/drivers/GPS/GPS_MDx.cpp b/platform/drivers/GPS/GPS_MDx.cpp
deleted file mode 100644
index e84bb619..00000000
--- a/platform/drivers/GPS/GPS_MDx.cpp
+++ /dev/null
@@ -1,243 +0,0 @@
-/***************************************************************************
- * Copyright (C) 2021 - 2025 by Federico Amedeo Izzo IU2NUO, *
- * Niccolò Izzo IU2KIN *
- * Frederik Saraci IU2NRO *
- * Silvano Seva IU2KWO *
- * *
- * 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, see *
- ***************************************************************************/
-
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-
-static int8_t detectStatus = -1;
-static size_t bufPos = 0;
-static size_t maxPos = 0;
-static char *dataBuf;
-static bool receiving = false;
-
-using namespace miosix;
-static Thread *gpsWaiting = 0;
-
-#ifdef PLATFORM_MD3x0
-#define PORT USART3
-#else
-#define PORT USART1
-#endif
-
-
-void __attribute__((used)) GpsUsartImpl()
-{
- if(PORT->SR & USART_SR_RXNE)
- {
- char value = PORT->DR;
-
- if((receiving == false) && (value == '$') && (bufPos == 0))
- {
- receiving = true;
- }
-
- if(receiving)
- {
- if(bufPos == maxPos)
- {
- receiving = false;
- }
-
- char prevChar = dataBuf[bufPos - 1];
- dataBuf[bufPos] = value;
- bufPos += 1;
-
- if((prevChar == '\r') && (value == '\n'))
- {
- receiving = false;
- bufPos -= 1;
- }
- }
-
- if((receiving == false) && (bufPos != 0))
- {
- // NMEA sentence received, turn off serial port
- PORT->CR1 &= ~USART_CR1_UE;
-
- if(gpsWaiting)
- {
- gpsWaiting->IRQwakeup();
- if(gpsWaiting->IRQgetPriority()>
- Thread::IRQgetCurrentThread()->IRQgetPriority())
- Scheduler::IRQfindNextThread();
- gpsWaiting = 0;
- }
- }
- }
-
- PORT->SR = 0;
-}
-
-#ifdef PLATFORM_MD3x0
-void __attribute__((naked)) USART3_IRQHandler()
-#else
-void __attribute__((naked)) USART1_IRQHandler()
-#endif
-{
- saveContext();
- #if defined(PLATFORM_MD3x0) && defined(MD3x0_ENABLE_DBG)
- asm volatile("bl _Z13usart3irqImplv");
- #else
- asm volatile("bl _Z12GpsUsartImplv");
- #endif
- restoreContext();
-}
-
-
-void gps_init(const uint16_t baud)
-{
- gpio_setMode(GPS_EN, OUTPUT);
- gpio_setMode(GPS_DATA, ALTERNATE | ALTERNATE_FUNC(7));
-
- #ifdef PLATFORM_MD3x0
- const unsigned int quot = 2*42000000/baud; /* APB1 clock is 42MHz */
- RCC->APB1ENR |= RCC_APB1ENR_USART3EN;
- #else
- const unsigned int quot = 2*84000000/baud; /* APB2 clock is 84MHz */
- RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
- #endif
- __DSB();
-
- PORT->BRR = quot/2 + (quot & 1);
- PORT->CR3 |= USART_CR3_ONEBIT;
- PORT->CR1 = USART_CR1_RE
- | USART_CR1_RXNEIE;
-
- #ifdef PLATFORM_MD3x0
- NVIC_SetPriority(USART3_IRQn, 14);
- #else
- NVIC_SetPriority(USART1_IRQn, 14);
- #endif
-}
-
-void gps_terminate()
-{
- gps_disable();
-
- #ifdef PLATFORM_MD3x0
- RCC->APB1ENR &= ~RCC_APB1ENR_USART3EN;
- #else
- RCC->APB2ENR &= ~RCC_APB2ENR_USART1EN;
- #endif
-}
-
-void gps_enable()
-{
- gpio_setPin(GPS_EN);
-
- // Enable IRQ
- #ifdef PLATFORM_MD3x0
- NVIC_ClearPendingIRQ(USART3_IRQn);
- NVIC_EnableIRQ(USART3_IRQn);
- #else
- NVIC_ClearPendingIRQ(USART1_IRQn);
- NVIC_EnableIRQ(USART1_IRQn);
- #endif
-}
-
-void gps_disable()
-{
- gpio_clearPin(GPS_EN);
- PORT->CR1 &= ~USART_CR1_UE;
-
- #ifdef PLATFORM_MD3x0
- NVIC_DisableIRQ(USART3_IRQn);
- #else
- NVIC_DisableIRQ(USART1_IRQn);
- #endif
-
- receiving = false;
- bufPos = 0;
-}
-
-bool gps_detect(uint16_t timeout)
-{
- if(detectStatus == -1)
- {
- gpio_setMode(GPS_DATA, INPUT_PULL_DOWN);
- gpio_setMode(GPS_EN, OUTPUT);
- gpio_setPin(GPS_EN);
-
- while((gpio_readPin(GPS_DATA) == 0) && (timeout > 0))
- {
- delayMs(1);
- timeout--;
- }
-
- gpio_clearPin(GPS_EN);
- gpio_setMode(GPS_EN, INPUT);
-
- if(timeout > 0)
- {
- detectStatus = 1;
- }
- else
- {
- detectStatus = 0;
- }
- }
-
- return (detectStatus == 1) ? true : false;
-}
-
-int gps_getNmeaSentence(char *buf, const size_t maxLength)
-{
- if(detectStatus != 1) return -1;
-
- memset(buf, 0x00, maxLength);
- bufPos = 0;
- maxPos = maxLength;
- dataBuf = buf;
-
- // Enable serial port
- PORT->CR1 |= USART_CR1_UE;
-
- return 0;
-}
-
-bool gps_nmeaSentenceReady()
-{
- return (receiving == false) && (bufPos > 0);
-}
-
-void gps_waitForNmeaSentence()
-{
- /*
- * Put the calling thread in waiting status until a complete sentence is ready.
- */
- {
- FastInterruptDisableLock dLock;
- gpsWaiting = Thread::IRQgetCurrentThread();
- do
- {
- Thread::IRQwait();
- {
- FastInterruptEnableLock eLock(dLock);
- Thread::yield();
- }
- }
- while(gpsWaiting);
- }
-}
diff --git a/platform/targets/MD-9600/hwconfig.c b/platform/targets/MD-9600/hwconfig.c
index b4ff33ec..d48610e7 100644
--- a/platform/targets/MD-9600/hwconfig.c
+++ b/platform/targets/MD-9600/hwconfig.c
@@ -18,13 +18,36 @@
* along with this program; if not, see *
***************************************************************************/
+#include
#include
#include
#include
+#include
#include
+#include
+
+static void gpsEnable(void *priv)
+{
+ gpio_setPin(GPS_EN);
+ gpsStm32_enable(priv);
+}
+
+static void gpsDisable(void *priv)
+{
+ gpio_clearPin(GPS_EN);
+ gpsStm32_disable(priv);
+}
static pthread_mutex_t spi2Mutex;
static pthread_mutex_t adcMutex;
SPI_STM32_DEVICE_DEFINE(spi2, SPI2, &spi2Mutex)
ADC_STM32_DEVICE_DEFINE(adc1, ADC1, &adcMutex, ADC_COUNTS_TO_UV(3300000, 12))
+
+const struct gpsDevice gps =
+{
+ .priv = NULL,
+ .enable = gpsEnable,
+ .disable = gpsDisable,
+ .getSentence = gpsStm32_getNmeaSentence
+};
diff --git a/platform/targets/MD-9600/hwconfig.h b/platform/targets/MD-9600/hwconfig.h
index ff04ba49..2e8ac3ec 100644
--- a/platform/targets/MD-9600/hwconfig.h
+++ b/platform/targets/MD-9600/hwconfig.h
@@ -40,6 +40,7 @@ enum adcChannel
ADC_HTEMP_CH = 15
};
+extern const struct gpsDevice gps;
extern const struct spiDevice spi2;
extern const struct Adc adc1;
@@ -48,6 +49,8 @@ extern const struct Adc adc1;
/* Device supports an optional GPS chip */
#define CONFIG_GPS
+#define CONFIG_GPS_STM32_USART1
+#define CONFIG_NMEA_RBUF_SIZE 128
/* Screen dimensions */
#define CONFIG_SCREEN_WIDTH 128
diff --git a/platform/targets/MD-9600/platform.c b/platform/targets/MD-9600/platform.c
index fac6bea1..6e607faa 100644
--- a/platform/targets/MD-9600/platform.c
+++ b/platform/targets/MD-9600/platform.c
@@ -31,7 +31,9 @@
#include
#include
#include
+#include
#include
+#include
/* TODO: Hardcoded hwInfo until we implement reading from flash */
static const hwInfo_t hwInfo =
@@ -85,6 +87,7 @@ void platform_terminate()
{
/* Shut down all the modules */
adcStm32_terminate(&adc1);
+ gpsStm32_terminate();
toneGen_terminate();
chSelector_terminate();
audio_terminate();
@@ -229,6 +232,31 @@ const hwInfo_t *platform_getHwInfo()
return &hwInfo;
}
+const struct gpsDevice *platform_initGps()
+{
+ const struct gpsDevice *dev = NULL;
+
+ // Turn on the GPS and check if there is voltage on the RXD pin
+ gpio_setMode(GPS_DATA, INPUT_PULL_DOWN);
+ gpio_setMode(GPS_EN, OUTPUT);
+ gpio_setPin(GPS_EN);
+
+ for(size_t i = 0; i < 50; i++) {
+ if(gpio_readPin(GPS_DATA) != 0) {
+ dev = &gps;
+ gpsStm32_init(9600);
+ break;
+ }
+
+ sleepFor(0, 1);
+ }
+
+ gpio_clearPin(GPS_EN);
+ gpio_setMode(GPS_DATA, ALTERNATE | ALTERNATE_FUNC(7));
+
+ return dev;
+}
+
/*
* NOTE: implementation of this API function is provided in
* platform/drivers/chSelector/chSelector_MD9600.c