Library 27- Read GPS data on STM32F4 devices

Recently I got a cheap GPS module from ebay, NEO-6M. They said that this is flight controller, but position fix is good only on really flat area. When I made a library, I’ve connected my SDcard to STM32F4 to save data, result are here.

To communicate with STM32F4 device, USART is in use. Most GPS modules have by default 9600 baudrate and 1Hz refresh rate of sending data. I got my GPS configured in 115200 baudrate and 5Hz refresh rate, so when you try example, make sure that you change your baudrate to correct value.

When you plug your GPS to power, it can take some time to get first valid signal from satellites. This time can be up to one minute, according to where you are trying to get signal.

If you are interested in HAL version of the same library, click here.

GPS Library

Features

  • Parse GPS data
  • Supported NMEA 0183 standard
  • Works with GPGGA, GPRMC, GPGSA, GPGSV NMEA statements
  • Version 1.1 – August 22, 2014
    • Calculate distance between 2 coordinates
    • Calculate bearing between 2 coordinates
  • Allows you to select custom NMEA statements

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx USART
    • MISC
  • TM
    • TM USART
    • TM GPIO
    • attributes.h
    • defines.h
GPS Default pinout to STM32F4 device
GPS module STM32F4 Description
RX PB6 Receive line for GPS, actually not needed
TX PB7 Transmit line for GPS, this is needed

This pins are for USART1, if you want your custom USART, add this line below in your defines.h file and edit them:

My library basically checks to get all data that are need to display all possible data which library supports. For that, it needs 4 NMEA statements to be returned from GPS module:

  • GPGGA: Global Positioning System Fix Data
  • GPRMC: Recommended minimum specific GPS/Transit data
  • GPGSA: GPS DOP and Active Satellites
  • GPGSV: GPS Satellites in view

GPGGA and GPRMC should be returned from all GPS receivers, but others 2 are maybe not. Everything depends on GPS configuration. There are some tools on net, to change GPS module’s settings, like one here.

If for some reason your GPS module does not provide all of your statements, you will not be able to get all possible data. To disable any of your statements, copy statements below in your defines.h file and uncomment line you don’t want to use:

When you enable these defines, my library will ignore statements, which are disabled and response like we have new data.

Distance and bearing

These two options were added in version 1.1.

Distance between 2 points can not be provided from GPS receiver, so we have to calculate it. This is useful, if you have let’s say quadcopter and you want to know, how far you are from quad. FIrst coordinate will be quad’s current position from GPS and second point will be set before you start with your flight.

If you want to make let’s say “Return home” function on quadcopter, bearing comes to be very useful. GPS module tells you your direction (included in library) where you are currently moving. Still, first coordinate is from quad’s GPS receiver, and second is where quad started with flight. When you activate “Return home” function, quad will know how far he is from “home” and in which direction (bearing) he has to move according to the north. Then, you just have to compare your calculated bearing with actual direction provided from GPS. And you will know, if he needs to go more left, right, etc.

Custom GPS statements

Library supports by default 4 statements. It assumes that every GPS module should output these 4 statements. But it may happen, that your module outputs data that you need, but are not available with my library by default.

Here is why custom GPS statements come handy. It is possible to define custom statements which will be parsed as strings from GPS receiver directly to user.

For example, let’s say that your module outputs $GPRMB sentence (navigation). From that statement, you want to get “Steering value”, which can be “L” or “R” for Left or Right. Steering value is part 3 inside GPRMB sentence. If you want to include this into your output, you can do this:

This statement is now added into main GPS_Data structure and also, pointer to small GPRMB.3 structure is returned.

You just have to make sure, that in this case, GPRMB statement is really returned from module. If module will not return this statement, you will not be able to read anything from library because it will never happen that everything will be updated before new data are available.

How to handle that data, you should look at bottom example about custom GPS statements.

Functions, structs and enumerations

Example

  • GPS data are displayed to to user on USART3 at 115200 bauds
    • TX pin is PB10

Example below produces something similar to:

Example 2

  • Works on STM32F429-Discovery with included LCD
  • Default baudrate for GPS module is 115200
    • PA10 is used for receive data

Example 3

  • Shows how to use custom NMEA statements for your GPS module

GPS Custom result output

GPS Custom result output



View projects at Github, download library below.

Icon
TM STM32F4 GPS Library

tilz0R

Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • cuong nguyen

    Hi! how can you upload this result to google server ? thanks !

  • Kaiser

    Hi I want to Build this Project of STMF4 Discovery I have downloaded all your Files but two files are missing
    “tm_stm32f4_delay.h”
    “tm_stm32f4_ili9341.h”

    Can you please either list them or email these thanks in advance

  • Kaiser

    Hi
    “tm_stm32f4_gpio.h”
    “attributes.h”

    also missing please add these many thanks

    • Search for GPIO library,
      Search for attributes.h,
      Search for DELAY library,
      Search for ILI9341 library.

      Everything on my site, use search.

  • Kaiser

    Hi Thanks for your help I found them

    But i am getting errors when building may be i am doing something wrong can you please help many thanks in advance

    compiling system_stm32f4xx.c…
    linking…
    .ObjectsGPS-EXAMPLE-3.axf: Error: L6218E: Undefined symbol TM_DELAY_Init (referred from main.o).
    .ObjectsGPS-EXAMPLE-3.axf: Error: L6218E: Undefined symbol TM_GPS_AddCustom (referred from main.o).
    .ObjectsGPS-EXAMPLE-3.axf: Error: L6218E: Undefined symbol TM_GPS_Init (referred from main.o).
    .ObjectsGPS-EXAMPLE-3.axf: Error: L6218E: Undefined symbol TM_GPS_Update (referred from main.o).
    .ObjectsGPS-EXAMPLE-3.axf: Error: L6218E: Undefined symbol TM_Time (referred from main.o).
    .ObjectsGPS-EXAMPLE-3.axf: Error: L6218E: Undefined symbol TM_USART_Putc (referred from main.o).
    Not enough information to list image symbols.
    Finished: 1 information, 0 warning and 6 error messages.
    “.ObjectsGPS-EXAMPLE-3.axf” – 6 Error(s), 0 Warning(s).
    Target not created.

    • If you have coocox, send me project.

      V pon., 13. apr. 2015 17.56 je oseba Disqus napisala:

  • Kaiser

    Hi Thank you for your Reply Unfortunately i am using Keil 5.1.4.0.0 .
    i don’t have coocox will this work in Keil ?

    • Yes, this was done using keil uvision.

      Download my entire Github repository and run keil project directly from there. It will work for you for sure.

  • Kaiser

    Hi I tried to find your GPS repository on Github but i can find it .

    I have found your GPS projects but no repository download anywhere . i am sorry i am new to this . sorry for the inconvenience if you can send me your GPS project repository download link i will be grateful .
    Thank you in advance

  • Kaiser

    Hi Thanks i have built your GPS project . But i am getting some errors I can send you the project zip file how do i attach it here ? thanks

  • jasen

    I have compiled.What are the pins in example 2?
    I tried what you wrote.But it didn’t work.
    rx—>stmboard tx—->stmboard

  • samit

    Hello Sir,
    I Want to take data from RGB sensor and display it on the terminal.I am not getting how to do it.

    • Hi,

      Well GPS has nothing to do with your RGb sensor. I also dont know how to transfer rgb data, but for data transfer from stm to computer, you can use USART.

      • samit

        Exactly Sir,
        but I am not getting how to read digital 12bit data from sensor.

        • Me neighter, because you didnt tell which sensor you have.

          • samit

            sunrom1185

          • You checked datasheet?
            Use email for further communication!!

  • Shailesh Joshi

    Hi,
    My C Library repository does not have any of the c library we used in one of the example. (stdio, math, tim etc.). Some site suggested me to download Sourcery g++ Lite but it is not available now. Well what should I do to get the code running?

    • STDIO, MATH are standard C libraries. You can get it by downloading GCC compiler for ARM for example.
      I don’t know what you mean with TIM, but this is STM32F4xx peripheral library provided from ST.com

      • Shailesh Joshi

        Thank you. Actually it shows the underline and “?” under those libraries, but when I compile I get no error related to these libraries.
        And it was supposed to be ‘time’ there. Anyways the code seems to work. Thank you again.

      • Shailesh Joshi

        Thanks. I think it worked. But now I have a problem that my math.h has no underline or “?” on it as it was earlier which should mean that IDE recognizes the file, still it shows error of “undefined reference to sin(cos/tan…)”. Also when I hover over these function in your library it shows its definition in math.h. How come it still not working?

  • Qfnnn

    Hi,
    I have used your gps lib in my project. My board is stm32f4-discovery and the gps module is as same as the one you used.
    I tested the module via u-center and it showed that module works properly. However, the stm board cannot get any new data after every update even I tried different baud rates (I tried the code example 2 on your post). I also checked the wire connection many times so it might not be the reason.
    I believe your lib works well and I used the libs of bmp180, rtc… and they are all very great. Is there any possible reason that the board cannot get new gps data?
    Thanks.

    • If you connect your module to USB->UART transmitter, can you show response from module?
      It should output 4 statements described above in post.

      Possibilities for not receiving data are:
      1. Module is not on correct baudrate
      2. Module does not output all statements required
      3. Pinout is not correct. Pins PA9/PA10 won’t work on STM32F4-Discovery board for USART. These 2 pins have low-pass filters for USB there and will block communication. Use PB6/PB7 instead. Open GPS library and set new USART settings in defines for GPS library. Or use defines.h file to overwrite USART settings for GPS library.

      • Qfnnn

        Yep, it did show the four NMEA statements. I will try other pins to do troubleshooting.
        Thank you very much.