DesignSpark Electrical Logolinkedin
Menu Search
Ask a Question

Building a Spresense Powered Smart Security Device Part 4: GNSS and Storage

This series of posts demonstrates how the advanced capabilities of the Sony Spresense can be put to use in creating a low power security device for asset monitoring and tracking.

In the first post, we introduced the project, the second covered SDK setup, and the previous explored the Sensor Control Unit (SCU) via SDK examples. In this post, we will now take a look at the Global Navigation Satellite System (GNSS) receiver and integrated SPI flash storage. When an event occurs these will allow us to identify the current position and log this along with event details.


GNSS is the generic term used for navigation systems such as GPS, GLONASS, Galileo and BeiDu, which along with positioning, provide a high accuracy time reference which is derived from the atomic clocks onboard the satellites delivering the service. The CXD5602’s embedded GNSS receiver supports all the aforementioned four systems, plus also satellite-based augmentation systems (SBAS) which are used to further improve accuracy and reliability.

Key features of the receiver include:

  • Asynchronous position processing and notification
  • Geofencing (detecting whether the receiver is leaving a specified area)
  • Ability to store up to 170 logs in the GNSS core

Geofencing transition notification

As with the SCU and sensor processing, such features free up the application processor to focus on the main task at hand, while significantly reducing energy consumption.

Spresense SDK GNSS features include:

  • /dev/gps interface plus ioctl commands
  • Library to convert output to standard NMEA format
  • Example data for development and testing indoors, plus a command emulator
  • LNA power control and GNSS sleep mode
  • Ability to backup data for faster positioning via hot start
  • Hardware-based position notification and geofencing support

The GNSS interface can be opened and read like a regular file and data structures are provided which give access to things such as the satellite status, position data and accuracy etc.

SPI flash

The Spresense module is equipped with 8MB (8M x 8) quad-SPI flash, which is used to store programs along with data, such as GNSS backup data for faster positioning start-up.

NuttX provides a Memory Technology Device (MTD) driver for accessing devices such as flash and EEPROM. The Spresense SDK uses this to access the flash storage and can configure support for NXFFS — the NuttX wear-leveling flash filesystem — or alternatively, the SMART flash filesystem from Ken Pettit.

PVTlog example

PVTlog is a function that logs position, velocity, and time information in the GNSS core. Up to 170 logs can be stored in the GNSS core and when the threshold is reached the application is notified. The cxd56_pvtlog_s structure is used for this and this can be seen in the above diagram.

The log store is started and stopped with CXD56_GNSS_IOCTL_PVTLOG_START and CXD56_GNSS_IOCTL_PVTLOG_STOP. The former takes a parameter pointing to the cxd56_pvtlog_setting_s structure, which configures the log interval and notification threshold.

The SDK includes a PVTlog example which allows us to:

  1. Start positioning, cache position in the GNSS core and then notify when the threshold is exceeded, following which write the log to a file
  2. Read the saved file and output this to the console
  3. Delete the saved file
  4. Execute all of 1-3 above

So let’s take a look at the key parts involved for positioning and writing the log. For complete details see the source file.

Once the GPS device file is opened and we have a file descriptor we can start the log function.

  pvtlog_setting.cycle     = TEST_RECORDING_CYCLE;
  pvtlog_setting.threshold = TEST_NOTIFY_THRESHOLD;
  ret = ioctl(fd, CXD56_GNSS_IOCTL_PVTLOG_START,
              (unsigned long)&pvtlog_setting);

Then start positioning.


We now wait for signals that were configured to tell us when positioning data is available and when the pvtlog threshold has been reached. In the case of the former, we print this out, and with the latter we write this to a file.

  sig_id = sigwaitinfo(&mask, NULL);

  switch (sig_id)
    case MY_GNSS_SIG0:
      /* Read and print POS data. */


    case MY_GNSS_SIG1:
      /* Receive pvtlog signal */


The writefile function constructs a filename and opens this write-only with:

fd_write = open(filename, O_WRONLY | O_CREAT | O_BINARY);

And then writes to it with:

write(fd_write, &pvtlogdat, sizeof(struct cxd56_pvtlog_s)

Following which it closes the file. Error handling is also included and once again, see the source for complete details.

We can configure the SDK example with:

$ tools/ examples/gnss_pvtlog

Following which we can build the example, flash it to the Spresense and run it by entering “gnss_pvtlog” at the NuttShell prompt.

$ make buildkernel

$ make

$ tools/ -c /dev/ttyUSB3 nuttx.spk

$ screen /dev/ttyUSB3 115200

After output from initial start-up, we will see “No Positioning...” messages until the receiver obtains a fix, following which the date and time plus lat/long will be output.

Note that the default if no argument is given to gnss_pvtlog is the write mode as described earlier. In this second run, we passed the “r” argument to retrieve the log file written in the first run.

Thereby also demonstrating reading from flash storage.

Prototype solution

Now that we have explored the SCU and GNSS domains, in the next post we will combine the use of these two capabilities with a program that configures the event detector, then reads sensor input and if this exceeds threshold, logs event details along with the current position to flash storage. Our prototype solution!

Andrew Back

Open source (hardware and software!) advocate, Treasurer and Director of the Free and Open Source Silicon Foundation, organiser of Wuthering Bytes technology festival and founder of the Open Source Hardware User Group.

16 Jan 2020, 11:25