Program to demonstrate the use of the LowPowerSleep class on the u-blox C030 platform.
Dependencies: gnss low-power-sleep
Diff: main.cpp
- Revision:
- 0:84e316af0e0c
- Child:
- 1:633244b5186b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Jun 06 08:34:35 2017 +0000 @@ -0,0 +1,158 @@ +/* mbed Microcontroller Library + * Copyright (c) 2017 u-blox + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "low_power.h" +#include "gnss.h" + +#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 BACKUP_SRAM_STRING "Back from Standby mode!" + +// Put the time at the start of back-up SRAM +BACKUP_SRAM +static time_t timeNow; + +// The rest of backup SRAM +BACKUP_SRAM +static char backupSram[BACKUP_SRAM_SIZE - sizeof (timeNow)]; + +// LEDs +DigitalOut ledRed(LED1, 1); +DigitalOut ledGreen(LED2, 1); +DigitalOut ledBlue(LED3, 1); + +/* IMPORTANT: this code puts the STM32F4xx chip into its lowest power state. + * The ability to do this is affected by the state of the debug chip on the C030 + * board. To be sure that this code executes correctly, you MUST completely + * power off the board after downloading code, and power it back on again. + */ + +/* 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. + */ + +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(); + + // 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"); + } + + 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; + + // 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); + timeNow = time(NULL); + lowPower.enterStop(STOP_TIME_SECONDS * 1000); + printf (" Awake from Stop mode after %d second(s).\n", + (int) (time(NULL) - timeNow)); + } + } + + ledGreen = 1; + ledRed = 1; + ledBlue = 1; + if (!gotTime) { + ledRed = 0; + } + + 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); + + 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