Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
igrr committed Dec 16, 2017
0 parents commit 4ff5d23
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.settings
.cproject
.project
example/build
example/sdkconfig
.DS_Store
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "libnmea"]
path = libnmea
url = https://github.com/jacketizer/libnmea
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Jack Engqvist Johansson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# NMEA parser component for ESP32 ESP-IDF

This is a wrapper around [libnmea](https://github.com/jacketizer/libnmea),
in the form of an [ESP-IDF](https://github.com/espressif/esp-idf) component.

To use, clone the component into the `components` directory of your project,
or add it as a submodule.
See [libnmea documentation](https://github.com/jacketizer/libnmea#how-to-use-it)
for more details.


## Example

Example project is provided inside `example` directory. It works the same way
as `parse_stdin.c` example from libnmea, except that it reads NMEA sentences
from UART. Connect TXD pin of GPS receiver to GPIO21 of ESP32, build and
flash the example. Decoded NMEA messages will be displayed in the console.

## Known issues

`strptime` function in newlib is less smart than the one in Glibc, and can not
parse format strings such as '%H%M%S' (i.e. when there are no separators
between numeric fields). Therefore date and time fields of NMEA messages
are not parsed.

## License

[libnmea](https://github.com/jacketizer/libnmea), this component, and the
example project are licensed under MIT License.
36 changes: 36 additions & 0 deletions component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
COMPONENT_SUBMODULES := libnmea
COMPONENT_SRCDIRS := libnmea/src/nmea libnmea/src/parsers
COMPONENT_ADD_INCLUDEDIRS := libnmea/src/nmea libnmea/src/parsers
PARSER_OBJS := $(addprefix libnmea/src/parsers/,\
gpgga.o \
gpgll.o \
gprmc.o \
)

COMPONENT_OBJS := $(addprefix libnmea/src/,\
nmea/nmea.o \
nmea/parser_static.o \
parsers/parse.o \
) \
$(PARSER_OBJS)

define RENAME_SYMBOLS
$(OBJCOPY) \
--redefine-sym init=nmea_$(1)_init \
--redefine-sym parse=nmea_$(1)_parse \
--redefine-sym set_default=nmea_$(1)_set_default \
--redefine-sym allocate_data=nmea_$(1)_allocate_data \
--redefine-sym free_data=nmea_$(1)_free_data \
libnmea/src/parsers/$(1).o
endef

$(COMPONENT_LIBRARY): | rename_symbols

.PHONY: rename_symbols

rename_symbols: | $(PARSER_OBJS)
$(call RENAME_SYMBOLS,gpgga)
$(call RENAME_SYMBOLS,gpgll)
$(call RENAME_SYMBOLS,gprmc)

CPPFLAGS += -DENABLE_GPGLL=1 -DENABLE_GPGGA=1 -DENABLE_GPRMC=1 -DPARSER_COUNT=3
6 changes: 6 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PROJECT_NAME := nmea_example

EXTRA_COMPONENT_DIRS := $(realpath ..)

include $(IDF_PATH)/make/project.mk

Empty file added example/main/component.mk
Empty file.
143 changes: 143 additions & 0 deletions example/main/nmea_example_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/* NMEA parsing example for ESP32.
* Based on "parse_stdin.c" example from libnmea.
* Copyright (c) 2015 Jack Engqvist Johansson.
* Additions Copyright (c) 2017 Ivan Grokhotkov.
* See "LICENSE" file in libnmea directory for license.
*/

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "nmea.h"
#include "gpgll.h"
#include "gpgga.h"
#include "gprmc.h"

#define UART_NUM UART_NUM_2
#define UART_RX_PIN 21
#define UART_RX_BUF_SIZE (1024)

static void uart_setup();
static void read_and_parse_nmea();

void app_main()
{
uart_setup();
read_and_parse_nmea();
}

static void uart_setup()
{
uart_config_t uart_config = {
.baud_rate = 9600,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
uart_param_config(UART_NUM, &uart_config);
uart_set_pin(UART_NUM,
UART_PIN_NO_CHANGE, 21,
UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(UART_NUM, UART_RX_BUF_SIZE * 2, 0, 0, NULL, 0);
}


static void read_and_parse_nmea()
{
// Configure a temporary buffer for the incoming data
char *buffer = (char*) malloc(UART_RX_BUF_SIZE + 1);
char fmt_buf[32];
size_t total_bytes = 0;
while (1) {
// Read data from the UART
int read_bytes = uart_read_bytes(UART_NUM,
(uint8_t*) buffer + total_bytes,
UART_RX_BUF_SIZE - total_bytes, 100 / portTICK_RATE_MS);
if (read_bytes <= 0) {
continue;
}

nmea_s *data;
total_bytes += read_bytes;

/* find start (a dollar sign) */
char* start = memchr(buffer, '$', total_bytes);
if (start == NULL) {
total_bytes = 0;
continue;
}

/* find end of line */
char* end = memchr(start, '\r', total_bytes - (start - buffer));
if (NULL == end || '\n' != *(++end)) {
continue;
}
end[-1] = NMEA_END_CHAR_1;
end[0] = NMEA_END_CHAR_2;

/* handle data */
data = nmea_parse(start, end - start + 1, 0);
if (data != NULL) {
if (data->errors != 0) {
printf("WARN: The sentence struct contains parse errors!\n");
}

if (NMEA_GPGGA == data->type) {
printf("GPGGA sentence\n");
nmea_gpgga_s *gpgga = (nmea_gpgga_s *) data;
printf("Number of satellites: %d\n", gpgga->n_satellites);
printf("Altitude: %d %c\n", gpgga->altitude,
gpgga->altitude_unit);
}

if (NMEA_GPGLL == data->type) {
printf("GPGLL sentence\n");
nmea_gpgll_s *pos = (nmea_gpgll_s *) data;
printf("Longitude:\n");
printf(" Degrees: %d\n", pos->longitude.degrees);
printf(" Minutes: %f\n", pos->longitude.minutes);
printf(" Cardinal: %c\n", (char) pos->longitude.cardinal);
printf("Latitude:\n");
printf(" Degrees: %d\n", pos->latitude.degrees);
printf(" Minutes: %f\n", pos->latitude.minutes);
printf(" Cardinal: %c\n", (char) pos->latitude.cardinal);
strftime(fmt_buf, sizeof(fmt_buf), "%H:%M:%S", &pos->time);
printf("Time: %s\n", fmt_buf);
}

if (NMEA_GPRMC == data->type) {
printf("GPRMC sentence\n");
nmea_gprmc_s *pos = (nmea_gprmc_s *) data;
printf("Longitude:\n");
printf(" Degrees: %d\n", pos->longitude.degrees);
printf(" Minutes: %f\n", pos->longitude.minutes);
printf(" Cardinal: %c\n", (char) pos->longitude.cardinal);
printf("Latitude:\n");
printf(" Degrees: %d\n", pos->latitude.degrees);
printf(" Minutes: %f\n", pos->latitude.minutes);
printf(" Cardinal: %c\n", (char) pos->latitude.cardinal);
strftime(fmt_buf, sizeof(fmt_buf), "%H:%M:%S", &pos->time);
printf("Time: %s\n", fmt_buf);
}

nmea_free(data);
}

/* buffer empty? */
if (end == buffer + total_bytes) {
total_bytes = 0;
continue;
}

/* copy rest of buffer to beginning */
if (buffer != memmove(buffer, end, total_bytes - (end - buffer))) {
total_bytes = 0;
continue;
}

total_bytes -= end - buffer;
}
free(buffer);
}
1 change: 1 addition & 0 deletions libnmea
Submodule libnmea added at 527ba7

0 comments on commit 4ff5d23

Please sign in to comment.