forked from u-blox/c030-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
222 lines (187 loc) · 6.67 KB
/
main.cpp
File metadata and controls
222 lines (187 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* 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 "battery_charger_bq24295.h"
#include "battery_gauge_bq27441.h"
#include "low_power.h"
#include "gnss.h"
/**
* @file main.cpp
* main() for testing C030 stuff.
*
* IMPORTANT: this code puts the C030 MCU chip into
* its lowest power state. The ability to do this
* is affected by the state of the debug chip on the 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.
*/
// Pin-out
#define PIN_I2C_SDA PC_9
#define PIN_I2C_SCL PA_8
#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]))
// Backup SRAM stuff
BACKUP_SRAM
static time_t gTimeNow;
BACKUP_SRAM
static char gBackupSram[BACKUP_SRAM_SIZE - sizeof (gTimeNow)];
#define BACKUP_SRAM_STRING "Back from the dead!"
// LEDs
DigitalOut gLedRed(LED1);
DigitalOut gLedGreen(LED2);
DigitalOut gLedBlue(LED3);
// ----------------------------------------------------------------
// PRIVATE FUNCTIONS
// ----------------------------------------------------------------
static void signalGood(void)
{
gLedGreen = 0;
gLedRed = 1;
gLedBlue = 1;
wait_ms(1000);
gLedGreen = 1;
}
static void signalBad(void)
{
gLedRed = 0;
gLedGreen = 1;
gLedBlue = 1;
wait_ms(1000);
gLedRed = 1;
}
static void signalEvent(void)
{
gLedBlue = 0;
gLedGreen = 1;
gLedRed = 1;
wait_ms(1000);
gLedBlue = 1;
}
static void signalOff(void)
{
gLedGreen = 1;
gLedRed = 1;
gLedBlue = 1;
}
// ----------------------------------------------------------------
// PUBLIC FUNCTIONS: MAIN
// ----------------------------------------------------------------
#if 0
int main()
{
GnssSerial *pGnss = new GnssSerial();
LowPower *pLowPower = new LowPower();
BatteryChargerBq24295 *pBatteryCharger = NULL;
BatteryGaugeBq27441 *pBatteryGauge = NULL;
I2C *pI2C = NULL;
time_t timeNow;
int gnssReturnCode;
char buffer[256];
bool gotTime = false;
// Have to exit Debug mode on the chip if we want to go into Standby mode
pLowPower->exitDebugMode();
// Power up GNSS
pGnss->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) - gTimeNow));
printf ("Backup RAM contains \"%.*s\".\n", sizeof(BACKUP_SRAM_STRING), gBackupSram);
} 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");
signalOff();
}
pI2C = new I2C(PIN_I2C_SDA, PIN_I2C_SCL);
if (pI2C != NULL) {
pBatteryCharger = new BatteryChargerBq24295();
if (pBatteryCharger != NULL) {
if (pBatteryCharger->init(pI2C)) {
printf ("BQ24295 battery charger ready.\n");
signalGood();
} else {
printf ("Unable to initialise BQ24295 charger chip.\n");
signalBad();
}
} else {
printf("Unable to instantiate BQ24295 charger chip.\n");
signalBad();
}
pBatteryGauge = new BatteryGaugeBq27441();
if (pBatteryGauge != NULL) {
if (pBatteryGauge->init(pI2C)) {
printf ("BQ27441 battery gauge ready.\n");
signalGood();
} else {
printf ("Unable to initialise BQ27441 battery gauge chip.\n");
signalBad();
}
} else {
printf("Unable to instantiate BQ27441 battery gauge chip.\n");
signalBad();
}
} else {
printf("Unable to instantiate I2C.\n");
signalBad();
}
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 = pGnss->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 *pTimeString = NULL;
// Retrieve the time
pTimeString = pGnss->findNmeaItemPos(1, buffer, buffer + length);
if (pTimeString != NULL) {
gotTime = true;
printf("GNSS: time is %.6s.\n", pTimeString);
}
}
}
}
}
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);
signalEvent();
timeNow = time(NULL);
pLowPower->enterStop(STOP_TIME_SECONDS * 1000);
printf (" Awake from Stop mode after %d second(s).\n", (int) (time(NULL) - timeNow));
}
}
printf ("\nPutting \"%s\" into BKPSRAM...\n", BACKUP_SRAM_STRING);
memcpy (gBackupSram, 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);
signalEvent();
gTimeNow = time(NULL);
pLowPower->enterStandby(STANDBY_TIME_SECONDS * 1000);
printf("Should never get here.\n");
MBED_ASSERT(false);
return 0;
}
#endif
// End Of File