Program to demonstrate the use of the LowPowerSleep class on the u-blox C030 platform.

Dependencies:   gnss low-power-sleep

Committer:
rob.meades@u-blox.com
Date:
Tue Jun 06 22:28:33 2017 +0100
Revision:
1:633244b5186b
Parent:
0:84e316af0e0c
Child:
5:65094452a0c5
Flesh out example.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RobMeades 0:84e316af0e0c 1 /* mbed Microcontroller Library
RobMeades 0:84e316af0e0c 2 * Copyright (c) 2017 u-blox
RobMeades 0:84e316af0e0c 3 *
RobMeades 0:84e316af0e0c 4 * Licensed under the Apache License, Version 2.0 (the "License");
RobMeades 0:84e316af0e0c 5 * you may not use this file except in compliance with the License.
RobMeades 0:84e316af0e0c 6 * You may obtain a copy of the License at
RobMeades 0:84e316af0e0c 7 *
RobMeades 0:84e316af0e0c 8 * http://www.apache.org/licenses/LICENSE-2.0
RobMeades 0:84e316af0e0c 9 *
RobMeades 0:84e316af0e0c 10 * Unless required by applicable law or agreed to in writing, software
RobMeades 0:84e316af0e0c 11 * distributed under the License is distributed on an "AS IS" BASIS,
RobMeades 0:84e316af0e0c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
RobMeades 0:84e316af0e0c 13 * See the License for the specific language governing permissions and
RobMeades 0:84e316af0e0c 14 * limitations under the License.
RobMeades 0:84e316af0e0c 15 */
RobMeades 0:84e316af0e0c 16
RobMeades 0:84e316af0e0c 17 #include "mbed.h"
RobMeades 0:84e316af0e0c 18 #include "low_power.h"
RobMeades 0:84e316af0e0c 19 #include "gnss.h"
RobMeades 0:84e316af0e0c 20
RobMeades 0:84e316af0e0c 21 #define GNSS_WAIT_TIME_SECONDS 120
RobMeades 0:84e316af0e0c 22 #define STOP_TIME_SECONDS 5
RobMeades 0:84e316af0e0c 23 #define STANDBY_TIME_SECONDS 5
rob.meades@u-blox.com 1:633244b5186b 24 #define CHECK_TALKER(s) ((buffer[3] == s[0]) && (buffer[4] == s[1]) && (buffer[5] == s[2]))
RobMeades 0:84e316af0e0c 25 #define BACKUP_SRAM_STRING "Back from Standby mode!"
RobMeades 0:84e316af0e0c 26
RobMeades 0:84e316af0e0c 27 // Put the time at the start of back-up SRAM
RobMeades 0:84e316af0e0c 28 BACKUP_SRAM
RobMeades 0:84e316af0e0c 29 static time_t timeNow;
RobMeades 0:84e316af0e0c 30
RobMeades 0:84e316af0e0c 31 // The rest of backup SRAM
RobMeades 0:84e316af0e0c 32 BACKUP_SRAM
RobMeades 0:84e316af0e0c 33 static char backupSram[BACKUP_SRAM_SIZE - sizeof (timeNow)];
RobMeades 0:84e316af0e0c 34
RobMeades 0:84e316af0e0c 35 // LEDs
RobMeades 0:84e316af0e0c 36 DigitalOut ledRed(LED1, 1);
RobMeades 0:84e316af0e0c 37 DigitalOut ledGreen(LED2, 1);
RobMeades 0:84e316af0e0c 38 DigitalOut ledBlue(LED3, 1);
RobMeades 0:84e316af0e0c 39
RobMeades 0:84e316af0e0c 40 /* IMPORTANT: this code puts the STM32F4xx chip into its lowest power state.
RobMeades 0:84e316af0e0c 41 * The ability to do this is affected by the state of the debug chip on the C030
RobMeades 0:84e316af0e0c 42 * board. To be sure that this code executes correctly, you MUST completely
RobMeades 0:84e316af0e0c 43 * power off the board after downloading code, and power it back on again.
RobMeades 0:84e316af0e0c 44 */
RobMeades 0:84e316af0e0c 45
RobMeades 0:84e316af0e0c 46 /* This example program for the u-blox C030 board demonstrates the use of
RobMeades 0:84e316af0e0c 47 * the low power driver. It waits for GPS to obtain the time, entering Stop
rob.meades@u-blox.com 1:633244b5186b 48 * mode while waiting for the GPS to return results and, once the time has
rob.meades@u-blox.com 1:633244b5186b 49 * been obtained, it enters Standby mode, putting some stored information
rob.meades@u-blox.com 1:633244b5186b 50 * into backup SRAM where it will be kept until the end of Standby mode.
rob.meades@u-blox.com 1:633244b5186b 51 * Progress may be monitored with a serial terminal running at 9600 baud.
rob.meades@u-blox.com 1:633244b5186b 52 * The LED on the C030 board will turn green when this program is operating
rob.meades@u-blox.com 1:633244b5186b 53 * correctly, flash white when GNSS time is received, and turn red if GNSS
rob.meades@u-blox.com 1:633244b5186b 54 * time was not received or there is a failure.
RobMeades 0:84e316af0e0c 55 */
RobMeades 0:84e316af0e0c 56
rob.meades@u-blox.com 1:633244b5186b 57 // Get the time from GNSS
rob.meades@u-blox.com 1:633244b5186b 58 static bool gnssGetTime(GnssSerial *gnss)
rob.meades@u-blox.com 1:633244b5186b 59 {
rob.meades@u-blox.com 1:633244b5186b 60 char buffer[256];
rob.meades@u-blox.com 1:633244b5186b 61 bool gotTime = false;
rob.meades@u-blox.com 1:633244b5186b 62 int gnssReturnCode;
rob.meades@u-blox.com 1:633244b5186b 63 int32_t length;
rob.meades@u-blox.com 1:633244b5186b 64
rob.meades@u-blox.com 1:633244b5186b 65 while ((gnssReturnCode = gnss->getMessage(buffer, sizeof(buffer))) > 0) {
rob.meades@u-blox.com 1:633244b5186b 66 length = LENGTH(gnssReturnCode);
rob.meades@u-blox.com 1:633244b5186b 67
rob.meades@u-blox.com 1:633244b5186b 68 if ((PROTOCOL(gnssReturnCode) == GnssParser::NMEA) && (length > 6)) {
rob.meades@u-blox.com 1:633244b5186b 69 // Talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GNSS
rob.meades@u-blox.com 1:633244b5186b 70 if ((buffer[0] == '$') || buffer[1] == 'G') {
rob.meades@u-blox.com 1:633244b5186b 71 if (CHECK_TALKER("GGA") || CHECK_TALKER("GNS")) {
rob.meades@u-blox.com 1:633244b5186b 72 const char *timeString = NULL;
rob.meades@u-blox.com 1:633244b5186b 73 // Retrieve the time
rob.meades@u-blox.com 1:633244b5186b 74 timeString = gnss->findNmeaItemPos(1, buffer, buffer + length);
rob.meades@u-blox.com 1:633244b5186b 75 if (timeString != NULL) {
rob.meades@u-blox.com 1:633244b5186b 76 gotTime = true;
rob.meades@u-blox.com 1:633244b5186b 77 printf("\nGNSS: time is %.6s (UTC).\n", timeString);
rob.meades@u-blox.com 1:633244b5186b 78 ledBlue = 0;
rob.meades@u-blox.com 1:633244b5186b 79 ledGreen = 0;
rob.meades@u-blox.com 1:633244b5186b 80 ledRed = 0;
rob.meades@u-blox.com 1:633244b5186b 81 wait_ms(1000);
rob.meades@u-blox.com 1:633244b5186b 82 }
rob.meades@u-blox.com 1:633244b5186b 83 }
rob.meades@u-blox.com 1:633244b5186b 84 }
rob.meades@u-blox.com 1:633244b5186b 85 }
rob.meades@u-blox.com 1:633244b5186b 86 }
rob.meades@u-blox.com 1:633244b5186b 87
rob.meades@u-blox.com 1:633244b5186b 88 return gotTime;
rob.meades@u-blox.com 1:633244b5186b 89 }
rob.meades@u-blox.com 1:633244b5186b 90
rob.meades@u-blox.com 1:633244b5186b 91 // Main
RobMeades 0:84e316af0e0c 92 int main()
RobMeades 0:84e316af0e0c 93 {
RobMeades 0:84e316af0e0c 94 GnssSerial gnss;
RobMeades 0:84e316af0e0c 95 LowPower lowPower;
RobMeades 0:84e316af0e0c 96 bool gotTime = false;
RobMeades 0:84e316af0e0c 97
RobMeades 0:84e316af0e0c 98 // First exit Debug mode on the chip, otherwise it will not be
RobMeades 0:84e316af0e0c 99 // able to enter Standby mode
RobMeades 0:84e316af0e0c 100 lowPower.exitDebugMode();
RobMeades 0:84e316af0e0c 101
rob.meades@u-blox.com 1:633244b5186b 102 ledGreen = 0;
rob.meades@u-blox.com 1:633244b5186b 103 ledRed = 1;
rob.meades@u-blox.com 1:633244b5186b 104 ledBlue = 1;
rob.meades@u-blox.com 1:633244b5186b 105
RobMeades 0:84e316af0e0c 106 // Initialise GNSS
rob.meades@u-blox.com 1:633244b5186b 107 if (gnss.init()) {
rob.meades@u-blox.com 1:633244b5186b 108 if (time(NULL) != 0) {
rob.meades@u-blox.com 1:633244b5186b 109 // If the RTC is running, we must have been awake previously
rob.meades@u-blox.com 1:633244b5186b 110 printf ("Awake from Standby mode after %d second(s).\n",
rob.meades@u-blox.com 1:633244b5186b 111 (int) (time(NULL) - timeNow));
rob.meades@u-blox.com 1:633244b5186b 112 printf ("Backup RAM contains \"%.*s\".\n\n", sizeof(BACKUP_SRAM_STRING),
rob.meades@u-blox.com 1:633244b5186b 113 backupSram);
rob.meades@u-blox.com 1:633244b5186b 114 } else {
rob.meades@u-blox.com 1:633244b5186b 115 printf("\n\nStarting up from a cold start.\n");
rob.meades@u-blox.com 1:633244b5186b 116 printf("IMPORTANT: this code puts the STM32F4xx chip into its lowest power state.\n");
rob.meades@u-blox.com 1:633244b5186b 117 printf("The ability to do this is affected by the state of the debug chip on the C030\n");
rob.meades@u-blox.com 1:633244b5186b 118 printf("board. To be sure that this code executes correctly, you must completely power\n");
rob.meades@u-blox.com 1:633244b5186b 119 printf("off the board after downloading code, and power it back on again.\n\n");
rob.meades@u-blox.com 1:633244b5186b 120 }
RobMeades 0:84e316af0e0c 121
rob.meades@u-blox.com 1:633244b5186b 122 printf ("Waiting up to %d second(s) for GNSS to receive the time...\n", GNSS_WAIT_TIME_SECONDS);
rob.meades@u-blox.com 1:633244b5186b 123 for (uint32_t x = 0; (x < GNSS_WAIT_TIME_SECONDS) && !gotTime; x+= STOP_TIME_SECONDS) {
rob.meades@u-blox.com 1:633244b5186b 124 gotTime = gnssGetTime(&gnss);
RobMeades 0:84e316af0e0c 125
rob.meades@u-blox.com 1:633244b5186b 126 if (!gotTime) {
rob.meades@u-blox.com 1:633244b5186b 127 printf (" Entering Stop mode for %d second(s) while waiting...\n", STOP_TIME_SECONDS);
rob.meades@u-blox.com 1:633244b5186b 128 // Let the printf leave the building
rob.meades@u-blox.com 1:633244b5186b 129 wait_ms(100);
rob.meades@u-blox.com 1:633244b5186b 130 time_t y = time(NULL);
rob.meades@u-blox.com 1:633244b5186b 131 lowPower.enterStop(STOP_TIME_SECONDS * 1000);
rob.meades@u-blox.com 1:633244b5186b 132 printf (" Awake from Stop mode after %d second(s).\n", (int) (time(NULL) - y));
RobMeades 0:84e316af0e0c 133 }
RobMeades 0:84e316af0e0c 134 }
RobMeades 0:84e316af0e0c 135
rob.meades@u-blox.com 1:633244b5186b 136 ledGreen = 1;
rob.meades@u-blox.com 1:633244b5186b 137 ledRed = 1;
rob.meades@u-blox.com 1:633244b5186b 138 ledBlue = 1;
RobMeades 0:84e316af0e0c 139 if (!gotTime) {
rob.meades@u-blox.com 1:633244b5186b 140 ledRed = 0;
RobMeades 0:84e316af0e0c 141 }
rob.meades@u-blox.com 1:633244b5186b 142
rob.meades@u-blox.com 1:633244b5186b 143 printf ("\nPutting \"%s\" into BKPSRAM...\n", BACKUP_SRAM_STRING);
rob.meades@u-blox.com 1:633244b5186b 144 memcpy (backupSram, BACKUP_SRAM_STRING, sizeof(BACKUP_SRAM_STRING));
RobMeades 0:84e316af0e0c 145
rob.meades@u-blox.com 1:633244b5186b 146 printf ("Entering Standby mode, losing all RAM contents, for %d second(s)...\n\n", STANDBY_TIME_SECONDS);
rob.meades@u-blox.com 1:633244b5186b 147 // Let the printf leave the building
rob.meades@u-blox.com 1:633244b5186b 148 wait_ms(100);
rob.meades@u-blox.com 1:633244b5186b 149 // We will return at the start of main() when the Standby time expires
rob.meades@u-blox.com 1:633244b5186b 150 timeNow = time(NULL);
rob.meades@u-blox.com 1:633244b5186b 151 lowPower.enterStandby(STANDBY_TIME_SECONDS * 1000);
rob.meades@u-blox.com 1:633244b5186b 152 } else {
rob.meades@u-blox.com 1:633244b5186b 153 printf("Unable to initialise GNSS.\n");
RobMeades 0:84e316af0e0c 154 }
RobMeades 0:84e316af0e0c 155
rob.meades@u-blox.com 1:633244b5186b 156 ledRed = 0;
RobMeades 0:84e316af0e0c 157 ledGreen = 1;
RobMeades 0:84e316af0e0c 158 ledBlue = 1;
RobMeades 0:84e316af0e0c 159 printf("Should never get here.\n");
RobMeades 0:84e316af0e0c 160 MBED_ASSERT(false);
RobMeades 0:84e316af0e0c 161 }
RobMeades 0:84e316af0e0c 162
RobMeades 0:84e316af0e0c 163 // End Of File