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

Dependencies:   gnss low-power-sleep

Committer:
RobMeades
Date:
Tue Jun 06 08:34:35 2017 +0000
Revision:
0:84e316af0e0c
Child:
1:633244b5186b
Initial revision.

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
RobMeades 0:84e316af0e0c 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
RobMeades 0:84e316af0e0c 48 * mode while waiting for GPS results and, once the time has been obtained,
RobMeades 0:84e316af0e0c 49 * it enters Standby mode for some time, putting stored information (the time
RobMeades 0:84e316af0e0c 50 * and a string) into backup SRAM where they will be kept until the end of
RobMeades 0:84e316af0e0c 51 * Standby mode. Progress may be monitored with a serial terminal
RobMeades 0:84e316af0e0c 52 * running at 9600 baud. The LED on the C030 board will turn green when this
RobMeades 0:84e316af0e0c 53 * program is operating correctly, flash blue when GNSS time is received,
RobMeades 0:84e316af0e0c 54 * and turn red if GNSS time was not received or there is a failure.
RobMeades 0:84e316af0e0c 55 */
RobMeades 0:84e316af0e0c 56
RobMeades 0:84e316af0e0c 57 int main()
RobMeades 0:84e316af0e0c 58 {
RobMeades 0:84e316af0e0c 59 GnssSerial gnss;
RobMeades 0:84e316af0e0c 60 LowPower lowPower;
RobMeades 0:84e316af0e0c 61 time_t timeNow;
RobMeades 0:84e316af0e0c 62 int gnssReturnCode;
RobMeades 0:84e316af0e0c 63 char buffer[256];
RobMeades 0:84e316af0e0c 64 bool gotTime = false;
RobMeades 0:84e316af0e0c 65
RobMeades 0:84e316af0e0c 66 // First exit Debug mode on the chip, otherwise it will not be
RobMeades 0:84e316af0e0c 67 // able to enter Standby mode
RobMeades 0:84e316af0e0c 68 lowPower.exitDebugMode();
RobMeades 0:84e316af0e0c 69
RobMeades 0:84e316af0e0c 70 // Initialise GNSS
RobMeades 0:84e316af0e0c 71 gnss.init();
RobMeades 0:84e316af0e0c 72
RobMeades 0:84e316af0e0c 73 if (time(NULL) != 0) {
RobMeades 0:84e316af0e0c 74 // If the RTC is running, we must have been awake previously
RobMeades 0:84e316af0e0c 75 printf ("Awake from Standby mode after %d second(s).\n",
RobMeades 0:84e316af0e0c 76 (int) (time(NULL) - timeNow));
RobMeades 0:84e316af0e0c 77 printf ("Backup RAM contains \"%.*s\".\n", sizeof(BACKUP_SRAM_STRING),
RobMeades 0:84e316af0e0c 78 backupSram);
RobMeades 0:84e316af0e0c 79 ledGreen = 0;
RobMeades 0:84e316af0e0c 80 ledRed = 1;
RobMeades 0:84e316af0e0c 81 ledBlue = 1;
RobMeades 0:84e316af0e0c 82
RobMeades 0:84e316af0e0c 83 } else {
RobMeades 0:84e316af0e0c 84 printf("\n\nStarting up from a cold start.\n");
RobMeades 0:84e316af0e0c 85 printf("IMPORTANT: this code puts the STM32F4xx chip into its lowest power state.\n");
RobMeades 0:84e316af0e0c 86 printf("The ability to do this is affected by the state of the debug chip on the C030\n");
RobMeades 0:84e316af0e0c 87 printf("board. To be sure that this code executes correctly, you must completely power\n");
RobMeades 0:84e316af0e0c 88 printf("off the board after downloading code, and power it back on again.\n\n");
RobMeades 0:84e316af0e0c 89 }
RobMeades 0:84e316af0e0c 90
RobMeades 0:84e316af0e0c 91 printf ("Waiting up to %d second(s) for GNSS to receive the time...\n",
RobMeades 0:84e316af0e0c 92 GNSS_WAIT_TIME_SECONDS);
RobMeades 0:84e316af0e0c 93 for (uint32_t x = 0; (x < GNSS_WAIT_TIME_SECONDS) && !gotTime;
RobMeades 0:84e316af0e0c 94 x+= STOP_TIME_SECONDS) {
RobMeades 0:84e316af0e0c 95 while ((gnssReturnCode = gnss.getMessage(buffer, sizeof(buffer))) > 0) {
RobMeades 0:84e316af0e0c 96 int32_t length = LENGTH(gnssReturnCode);
RobMeades 0:84e316af0e0c 97
RobMeades 0:84e316af0e0c 98 if ((PROTOCOL(gnssReturnCode) == GnssParser::NMEA) && (length > 6)) {
RobMeades 0:84e316af0e0c 99 // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GNSS
RobMeades 0:84e316af0e0c 100 if ((buffer[0] == '$') || buffer[1] == 'G') {
RobMeades 0:84e316af0e0c 101 if (_CHECK_TALKER("GGA") || _CHECK_TALKER("GNS")) {
RobMeades 0:84e316af0e0c 102 const char *timeString = NULL;
RobMeades 0:84e316af0e0c 103
RobMeades 0:84e316af0e0c 104 // Retrieve the time
RobMeades 0:84e316af0e0c 105 timeString = gnss.findNmeaItemPos(1, buffer,
RobMeades 0:84e316af0e0c 106 buffer + length);
RobMeades 0:84e316af0e0c 107 if (timeString != NULL) {
RobMeades 0:84e316af0e0c 108 gotTime = true;
RobMeades 0:84e316af0e0c 109 printf("GNSS: time is %.6s.\n",timeString);
RobMeades 0:84e316af0e0c 110 ledGreen = 1;
RobMeades 0:84e316af0e0c 111 ledRed = 1;
RobMeades 0:84e316af0e0c 112 ledBlue = 0;
RobMeades 0:84e316af0e0c 113 wait_ms(1000);
RobMeades 0:84e316af0e0c 114 }
RobMeades 0:84e316af0e0c 115 }
RobMeades 0:84e316af0e0c 116 }
RobMeades 0:84e316af0e0c 117 }
RobMeades 0:84e316af0e0c 118 }
RobMeades 0:84e316af0e0c 119
RobMeades 0:84e316af0e0c 120 if (!gotTime) {
RobMeades 0:84e316af0e0c 121 printf (" Entering Stop mode for %d second(s) while waiting...\n",
RobMeades 0:84e316af0e0c 122 STOP_TIME_SECONDS);
RobMeades 0:84e316af0e0c 123 // Let the printf leave the building
RobMeades 0:84e316af0e0c 124 wait_ms(100);
RobMeades 0:84e316af0e0c 125 timeNow = time(NULL);
RobMeades 0:84e316af0e0c 126 lowPower.enterStop(STOP_TIME_SECONDS * 1000);
RobMeades 0:84e316af0e0c 127 printf (" Awake from Stop mode after %d second(s).\n",
RobMeades 0:84e316af0e0c 128 (int) (time(NULL) - timeNow));
RobMeades 0:84e316af0e0c 129 }
RobMeades 0:84e316af0e0c 130 }
RobMeades 0:84e316af0e0c 131
RobMeades 0:84e316af0e0c 132 ledGreen = 1;
RobMeades 0:84e316af0e0c 133 ledRed = 1;
RobMeades 0:84e316af0e0c 134 ledBlue = 1;
RobMeades 0:84e316af0e0c 135 if (!gotTime) {
RobMeades 0:84e316af0e0c 136 ledRed = 0;
RobMeades 0:84e316af0e0c 137 }
RobMeades 0:84e316af0e0c 138
RobMeades 0:84e316af0e0c 139 printf ("\nPutting \"%s\" into BKPSRAM...\n", BACKUP_SRAM_STRING);
RobMeades 0:84e316af0e0c 140 memcpy (backupSram, BACKUP_SRAM_STRING, sizeof(BACKUP_SRAM_STRING));
RobMeades 0:84e316af0e0c 141
RobMeades 0:84e316af0e0c 142 printf ("Entering Standby mode for %d second(s)...\n", STANDBY_TIME_SECONDS);
RobMeades 0:84e316af0e0c 143 // Let the printf leave the building
RobMeades 0:84e316af0e0c 144 wait_ms(100);
RobMeades 0:84e316af0e0c 145 // We will return at the start of main() when the Standby time expires
RobMeades 0:84e316af0e0c 146 lowPower.enterStandby(STANDBY_TIME_SECONDS * 1000);
RobMeades 0:84e316af0e0c 147
RobMeades 0:84e316af0e0c 148 ledGreen = 1;
RobMeades 0:84e316af0e0c 149 ledRed = 0;
RobMeades 0:84e316af0e0c 150 ledBlue = 1;
RobMeades 0:84e316af0e0c 151
RobMeades 0:84e316af0e0c 152 printf("Should never get here.\n");
RobMeades 0:84e316af0e0c 153 MBED_ASSERT(false);
RobMeades 0:84e316af0e0c 154
RobMeades 0:84e316af0e0c 155 return 0;
RobMeades 0:84e316af0e0c 156 }
RobMeades 0:84e316af0e0c 157
RobMeades 0:84e316af0e0c 158 // End Of File