From ae54b81f1ca0f271186a9a6d56677beea4230716 Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Sun, 4 Sep 2022 08:56:15 +0200 Subject: [PATCH] sensors: gps data --- src/main.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/src/main.c b/src/main.c index 6215a6d..4130dc9 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ #include "fatfs/tf_card.h" #include #include "gd32vf103_i2c.h" +#include "minmea.h" //#define HZ2MS(X) ((uint64_t)((X * 53000.0)/(SystemCoreClock))) @@ -33,6 +34,32 @@ void init_uart0(void) usart_interrupt_enable(USART0, USART_INT_RBNE); } +void init_uart1(void) +{ + /* enable GPIO clock */ + rcu_periph_clock_enable(RCU_GPIOA); + /* enable USART clock */ + rcu_periph_clock_enable(RCU_USART1); + + /* connect port to USARTx_Tx */ + /* gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9); */ + /* connect port to USARTx_Rx */ + gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_3); + + /* USART configure */ + usart_deinit(USART1); + usart_baudrate_set(USART1, 9600); + usart_word_length_set(USART1, USART_WL_8BIT); + usart_stop_bit_set(USART1, USART_STB_1BIT); + usart_parity_config(USART1, USART_PM_NONE); + usart_hardware_flow_rts_config(USART1, USART_RTS_DISABLE); + usart_hardware_flow_cts_config(USART1, USART_CTS_DISABLE); + usart_receive_config(USART1, USART_RECEIVE_ENABLE); + usart_transmit_config(USART1, USART_TRANSMIT_DISABLE); + usart_enable(USART1); + + usart_interrupt_enable(USART1, USART_INT_RBNE); +} #define MAX_ROW 20 #define MAX_NMEA 192 @@ -297,6 +324,51 @@ static uint32_t bmp180_read_P(void) return (uint32_t)((data_xlsb + (data_lsb << 8) + (data_msb << 16)) >> (8 - BMP180_OSS)); } +static char uart_data_receive_blocking(uint32_t usart_periph) +{ + while (usart_flag_get(usart_periph, USART_FLAG_RBNE) != SET) { + continue; + } + + return usart_data_receive(usart_periph); +} + +static void get_nmea_line(char *buf) +{ + int i = 0; + + while (uart_data_receive_blocking(USART1) != '$') { + /* sync with the beginning of the line */ + } + buf[i++] = '$'; + while(true) { + buf[i] = uart_data_receive_blocking(USART1); + if (buf[i] == '\n' || i >= MAX_NMEA) + break; + i++; + } + buf[i] = 0; +} + +static int read_gps(struct minmea_sentence_gga *frame) +{ + char nmea_buf[MAX_NMEA]; + int ret; + + do { + get_nmea_line(nmea_buf); + } while(strncmp(nmea_buf, "$GPGGA", strlen("$GPGGA")) != 0); + + ret = minmea_parse_gga(frame, nmea_buf); + if (!ret) { + LCD_ShowString(0, 0, (u8 *)("PARSING ERROR"), RED); + LCD_ShowString(0, 32, (u8 *)nmea_buf, RED); + delay_1ms(2000); + } + + return 0; +} + int main(void) { uint8_t mount_is_ok = 1; /* 0: mount successful ; 1: mount failed */ @@ -310,6 +382,7 @@ int main(void) uint32_t up; uint32_t pressure; int32_t temperature; + struct minmea_sentence_gga gps_data = { 0 }; SystemInit(); /* Init GPIOs */ @@ -326,6 +399,7 @@ int main(void) /* HW init: uart, ADC, Display */ init_uart0(); + init_uart1(); Lcd_Init(); /* I2C init */ @@ -339,6 +413,14 @@ int main(void) while (1) { + read_gps(&gps_data); + LCD_Clear(BLACK); + snprintf(gps, MAX_ROW-1, "%d:%d:%d", gps_data.time.hours, gps_data.time.minutes, gps_data.time.seconds); + LCD_ShowString (0, 0, (u8 *)gps, GREEN); + snprintf(gps, MAX_ROW-1, "%d,%d", gps_data.latitude.value, gps_data.longitude.value); + LCD_ShowString (0, 16, (u8 *)gps, GREEN); + snprintf(gps, MAX_ROW-1, "H:%d", gps_data.altitude.value/gps_data.altitude.scale); + LCD_ShowString (0, 32, (u8 *)gps, GREEN); ut = bmp180_read_T(); up = bmp180_read_P();