Program to demonstrate the use of the LowPowerSleep class on the u-blox C030 platform.
Dependencies: gnss low-power-sleep
Revision 1:633244b5186b, committed 2017-06-06
- Comitter:
- rob.meades@u-blox.com
- Date:
- Tue Jun 06 22:28:33 2017 +0100
- Parent:
- 0:84e316af0e0c
- Child:
- 2:c66d83bb6590
- Commit message:
- Flesh out example.
Changed in this revision
gnss.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/gnss.lib Tue Jun 06 08:34:35 2017 +0000 +++ b/gnss.lib Tue Jun 06 22:28:33 2017 +0100 @@ -1,1 +1,1 @@ -https://mbed.org/teams/ublox/code/gnss/#2a1cd49ead85 +https://mbed.org/teams/ublox/code/gnss/#82308d600690
--- a/main.cpp Tue Jun 06 08:34:35 2017 +0000 +++ b/main.cpp Tue Jun 06 22:28:33 2017 +0100 @@ -21,7 +21,7 @@ #define GNSS_WAIT_TIME_SECONDS 120 #define STOP_TIME_SECONDS 5 #define STANDBY_TIME_SECONDS 5 -#define _CHECK_TALKER(s) ((buffer[3] == s[0]) && (buffer[4] == s[1]) && (buffer[5] == s[2])) +#define CHECK_TALKER(s) ((buffer[3] == s[0]) && (buffer[4] == s[1]) && (buffer[5] == s[2])) #define BACKUP_SRAM_STRING "Back from Standby mode!" // Put the time at the start of back-up SRAM @@ -45,114 +45,119 @@ /* This example program for the u-blox C030 board demonstrates the use of * the low power driver. It waits for GPS to obtain the time, entering Stop - * mode while waiting for GPS results and, once the time has been obtained, - * it enters Standby mode for some time, putting stored information (the time - * and a string) into backup SRAM where they will be kept until the end of - * Standby mode. Progress may be monitored with a serial terminal - * running at 9600 baud. The LED on the C030 board will turn green when this - * program is operating correctly, flash blue when GNSS time is received, - * and turn red if GNSS time was not received or there is a failure. + * mode while waiting for the GPS to return results and, once the time has + * been obtained, it enters Standby mode, putting some stored information + * into backup SRAM where it will be kept until the end of Standby mode. + * Progress may be monitored with a serial terminal running at 9600 baud. + * The LED on the C030 board will turn green when this program is operating + * correctly, flash white when GNSS time is received, and turn red if GNSS + * time was not received or there is a failure. */ +// Get the time from GNSS +static bool gnssGetTime(GnssSerial *gnss) +{ + char buffer[256]; + bool gotTime = false; + int gnssReturnCode; + int32_t length; + + while ((gnssReturnCode = gnss->getMessage(buffer, sizeof(buffer))) > 0) { + length = LENGTH(gnssReturnCode); + + if ((PROTOCOL(gnssReturnCode) == GnssParser::NMEA) && (length > 6)) { + // Talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GNSS + if ((buffer[0] == '$') || buffer[1] == 'G') { + if (CHECK_TALKER("GGA") || CHECK_TALKER("GNS")) { + const char *timeString = NULL; + // Retrieve the time + timeString = gnss->findNmeaItemPos(1, buffer, buffer + length); + if (timeString != NULL) { + gotTime = true; + printf("\nGNSS: time is %.6s (UTC).\n", timeString); + ledBlue = 0; + ledGreen = 0; + ledRed = 0; + wait_ms(1000); + } + } + } + } + } + + return gotTime; +} + +// Main int main() { GnssSerial gnss; LowPower lowPower; - time_t timeNow; - int gnssReturnCode; - char buffer[256]; bool gotTime = false; // First exit Debug mode on the chip, otherwise it will not be // able to enter Standby mode lowPower.exitDebugMode(); + ledGreen = 0; + ledRed = 1; + ledBlue = 1; + // Initialise GNSS - gnss.init(); - - if (time(NULL) != 0) { - // If the RTC is running, we must have been awake previously - printf ("Awake from Standby mode after %d second(s).\n", - (int) (time(NULL) - timeNow)); - printf ("Backup RAM contains \"%.*s\".\n", sizeof(BACKUP_SRAM_STRING), - backupSram); - ledGreen = 0; - ledRed = 1; - ledBlue = 1; - - } else { - printf("\n\nStarting up from a cold start.\n"); - printf("IMPORTANT: this code puts the STM32F4xx chip into its lowest power state.\n"); - printf("The ability to do this is affected by the state of the debug chip on the C030\n"); - printf("board. To be sure that this code executes correctly, you must completely power\n"); - printf("off the board after downloading code, and power it back on again.\n\n"); - } + if (gnss.init()) { + if (time(NULL) != 0) { + // If the RTC is running, we must have been awake previously + printf ("Awake from Standby mode after %d second(s).\n", + (int) (time(NULL) - timeNow)); + printf ("Backup RAM contains \"%.*s\".\n\n", sizeof(BACKUP_SRAM_STRING), + backupSram); + } else { + printf("\n\nStarting up from a cold start.\n"); + printf("IMPORTANT: this code puts the STM32F4xx chip into its lowest power state.\n"); + printf("The ability to do this is affected by the state of the debug chip on the C030\n"); + printf("board. To be sure that this code executes correctly, you must completely power\n"); + printf("off the board after downloading code, and power it back on again.\n\n"); + } - printf ("Waiting up to %d second(s) for GNSS to receive the time...\n", - GNSS_WAIT_TIME_SECONDS); - for (uint32_t x = 0; (x < GNSS_WAIT_TIME_SECONDS) && !gotTime; - x+= STOP_TIME_SECONDS) { - while ((gnssReturnCode = gnss.getMessage(buffer, sizeof(buffer))) > 0) { - int32_t length = LENGTH(gnssReturnCode); - - if ((PROTOCOL(gnssReturnCode) == GnssParser::NMEA) && (length > 6)) { - // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GNSS - if ((buffer[0] == '$') || buffer[1] == 'G') { - if (_CHECK_TALKER("GGA") || _CHECK_TALKER("GNS")) { - const char *timeString = NULL; + printf ("Waiting up to %d second(s) for GNSS to receive the time...\n", GNSS_WAIT_TIME_SECONDS); + for (uint32_t x = 0; (x < GNSS_WAIT_TIME_SECONDS) && !gotTime; x+= STOP_TIME_SECONDS) { + gotTime = gnssGetTime(&gnss); - // Retrieve the time - timeString = gnss.findNmeaItemPos(1, buffer, - buffer + length); - if (timeString != NULL) { - gotTime = true; - printf("GNSS: time is %.6s.\n",timeString); - ledGreen = 1; - ledRed = 1; - ledBlue = 0; - wait_ms(1000); - } - } - } + if (!gotTime) { + printf (" Entering Stop mode for %d second(s) while waiting...\n", STOP_TIME_SECONDS); + // Let the printf leave the building + wait_ms(100); + time_t y = time(NULL); + lowPower.enterStop(STOP_TIME_SECONDS * 1000); + printf (" Awake from Stop mode after %d second(s).\n", (int) (time(NULL) - y)); } } + ledGreen = 1; + ledRed = 1; + ledBlue = 1; if (!gotTime) { - printf (" Entering Stop mode for %d second(s) while waiting...\n", - STOP_TIME_SECONDS); - // Let the printf leave the building - wait_ms(100); - timeNow = time(NULL); - lowPower.enterStop(STOP_TIME_SECONDS * 1000); - printf (" Awake from Stop mode after %d second(s).\n", - (int) (time(NULL) - timeNow)); + ledRed = 0; } - } + + printf ("\nPutting \"%s\" into BKPSRAM...\n", BACKUP_SRAM_STRING); + memcpy (backupSram, BACKUP_SRAM_STRING, sizeof(BACKUP_SRAM_STRING)); - ledGreen = 1; - ledRed = 1; - ledBlue = 1; - if (!gotTime) { - ledRed = 0; + printf ("Entering Standby mode, losing all RAM contents, for %d second(s)...\n\n", STANDBY_TIME_SECONDS); + // Let the printf leave the building + wait_ms(100); + // We will return at the start of main() when the Standby time expires + timeNow = time(NULL); + lowPower.enterStandby(STANDBY_TIME_SECONDS * 1000); + } else { + printf("Unable to initialise GNSS.\n"); } - printf ("\nPutting \"%s\" into BKPSRAM...\n", BACKUP_SRAM_STRING); - memcpy (backupSram, BACKUP_SRAM_STRING, sizeof(BACKUP_SRAM_STRING)); - - printf ("Entering Standby mode for %d second(s)...\n", STANDBY_TIME_SECONDS); - // Let the printf leave the building - wait_ms(100); - // We will return at the start of main() when the Standby time expires - lowPower.enterStandby(STANDBY_TIME_SECONDS * 1000); - + ledRed = 0; ledGreen = 1; - ledRed = 0; ledBlue = 1; - printf("Should never get here.\n"); MBED_ASSERT(false); - - return 0; } // End Of File \ No newline at end of file