This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathu_device_private_gnss.c
444 lines (406 loc) · 18.9 KB
/
u_device_private_gnss.c
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* Copyright 2019-2024 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.
*/
/** @file
* @brief Functions associated with a GNSS device.
*/
#ifdef U_CFG_OVERRIDE
# include "u_cfg_override.h" // For a customer's configuration override
#endif
#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdbool.h"
#include "string.h" // for memset()
#include "u_cfg_os_platform_specific.h" // For U_CFG_OS_CLIB_LEAKS
#include "u_error_common.h"
#include "u_port_os.h"
#include "u_port_heap.h"
#include "u_port_uart.h"
#include "u_port_i2c.h"
#include "u_port_spi.h"
#include "u_device.h"
#include "u_device_shared.h"
#include "u_device_serial.h"
#include "u_at_client.h"
#include "u_cell_module_type.h"
#include "u_cell.h" // For uCellAtClientHandleGet()
#include "u_cell_loc.h" // For uCellLocSetPinGnssPwr()/uCellLocSetPinGnssDataReady()
#include "u_short_range_module_type.h"
#include "u_short_range.h" // For uShortRangeAtClientHandleGet()
#include "u_gnss_module_type.h"
#include "u_gnss_type.h"
#include "u_gnss.h"
#include "u_gnss_pwr.h"
#include "u_gnss_cfg.h" // For uGnssCfgSetProtocolOut()
#include "u_gnss_msg.h" // For uGnssMsgSetDataReady()
#include "u_device_private.h"
#include "u_device_shared_gnss.h"
#include "u_device_private_gnss.h"
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
/* ----------------------------------------------------------------
* STATIC VARIABLES
* -------------------------------------------------------------- */
/** Table to convert device transport type to GNSS transport type.
*/
const uGnssTransportType_t gDeviceToGnssTransportType[] = {
U_GNSS_TRANSPORT_NONE, // U_DEVICE_TRANSPORT_TYPE_NONE,
U_GNSS_TRANSPORT_UART, // U_DEVICE_TRANSPORT_TYPE_UART or U_DEVICE_TRANSPORT_TYPE_UART_1
U_GNSS_TRANSPORT_I2C, // U_DEVICE_TRANSPORT_TYPE_I2C
U_GNSS_TRANSPORT_SPI, // U_DEVICE_TRANSPORT_TYPE_SPI
U_GNSS_TRANSPORT_VIRTUAL_SERIAL, // U_DEVICE_TRANSPORT_TYPE_VIRTUAL_SERIAL
U_GNSS_TRANSPORT_UART_2, // U_DEVICE_TRANSPORT_TYPE_UART_2
U_GNSS_TRANSPORT_USB // U_DEVICE_TRANSPORT_TYPE_UART_USB
};
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */
// Populate the GNSS device context
static void populateContext(uDeviceGnssInstance_t *pContext,
uGnssTransportHandle_t gnssTransportHandle,
uDeviceTransportType_t deviceTransportType,
bool powerOffToBackup)
{
switch (deviceTransportType) {
case U_DEVICE_TRANSPORT_TYPE_UART:
// fall-through
case U_DEVICE_TRANSPORT_TYPE_UART_USB:
// fall-through
case U_DEVICE_TRANSPORT_TYPE_UART_2:
pContext->transportHandle.int32Handle = gnssTransportHandle.uart;
break;
case U_DEVICE_TRANSPORT_TYPE_I2C:
pContext->transportHandle.int32Handle = gnssTransportHandle.i2c;
break;
case U_DEVICE_TRANSPORT_TYPE_SPI:
pContext->transportHandle.int32Handle = gnssTransportHandle.spi;
break;
case U_DEVICE_TRANSPORT_TYPE_VIRTUAL_SERIAL:
pContext->transportHandle.pDeviceSerial = gnssTransportHandle.pDeviceSerial;
break;
default:
break;
}
pContext->deviceTransportType = deviceTransportType;
pContext->powerOffToBackup = powerOffToBackup;
}
// Do all the leg-work to remove a GNSS device.
static int32_t removeDevice(uDeviceHandle_t devHandle, bool powerOff)
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
uDeviceGnssInstance_t *pContext = (uDeviceGnssInstance_t *) U_DEVICE_INSTANCE(devHandle)->pContext;
if (pContext != NULL) {
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
if (powerOff) {
if (pContext->powerOffToBackup) {
errorCode = uGnssPwrOffBackup(devHandle);
} else {
errorCode = uGnssPwrOff(devHandle);
}
if (errorCode == 0) {
// This will destroy the instance
uGnssRemove(devHandle);
uPortFree(pContext);
}
} else {
uGnssRemove(devHandle);
uPortFree(pContext);
}
}
return errorCode;
}
// Do all the leg-work to add a GNSS device.
static int32_t addDevice(uGnssTransportHandle_t gnssTransportHandle,
uDeviceTransportType_t deviceTransportType,
const uDeviceCfgGnss_t *pCfgGnss,
uDeviceHandle_t *pDeviceHandle)
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_NO_MEMORY;
uGnssTransportType_t gnssTransportType = U_GNSS_TRANSPORT_NONE;
uDeviceGnssInstance_t *pContext;
// Populate gnssTransportType
if ((deviceTransportType >= 0) &&
(deviceTransportType < sizeof(gDeviceToGnssTransportType) / sizeof(
gDeviceToGnssTransportType[0]))) {
gnssTransportType = gDeviceToGnssTransportType[deviceTransportType];
}
pContext = (uDeviceGnssInstance_t *) pUPortMalloc(sizeof(uDeviceGnssInstance_t));
if (pContext != NULL) {
memset(pContext, 0, sizeof(*pContext));
populateContext(pContext, gnssTransportHandle, deviceTransportType,
pCfgGnss->powerOffToBackup);
// Add the GNSS instance, which actually creates pDeviceHandle
errorCode = uGnssAdd((uGnssModuleType_t) pCfgGnss->moduleType,
gnssTransportType, gnssTransportHandle,
pCfgGnss->pinEnablePower, false,
pDeviceHandle);
if (errorCode == 0) {
if (pCfgGnss->i2cAddress > 0) {
uGnssSetI2cAddress(*pDeviceHandle, pCfgGnss->i2cAddress);
}
#if !U_CFG_OS_CLIB_LEAKS
// Set printing of commands sent to the GNSS chip,
// which can be useful while debugging, but
// only if the C library doesn't leak.
uGnssSetUbxMessagePrint(*pDeviceHandle, true);
#endif
// Attach the context
U_DEVICE_INSTANCE(*pDeviceHandle)->pContext = pContext;
// Power on the GNSS chip
errorCode = uGnssPwrOn(*pDeviceHandle);
if ((errorCode == 0) && (pCfgGnss->pinDataReady >= 0)) {
// A data ready pin has been specified; set it up
errorCode = uGnssMsgSetDataReady(*pDeviceHandle,
pCfgGnss->pinDataReady,
pCfgGnss->devicePioDataReady,
-1, -1, NULL, NULL);
if (errorCode < 0) {
// Return to off state if data ready set-up has failed
uGnssPwrOff(*pDeviceHandle);
}
}
if (errorCode < 0) {
// If we failed to power on, clean up
removeDevice(*pDeviceHandle, false);
}
} else {
uPortFree(pContext);
}
}
return errorCode;
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS: WORKAROUND FOR LINKER ISSUE
* -------------------------------------------------------------- */
void uDevicePrivateGnssLink()
{
//dummy
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */
// Initialise GNSS.
int32_t uDevicePrivateGnssInit()
{
return uGnssInit();
}
// Deinitialise GNSS.
void uDevicePrivateGnssDeinit()
{
uGnssDeinit();
}
// Power up a GNSS device, making it available for configuration.
int32_t uDevicePrivateGnssAdd(const uDeviceCfg_t *pDevCfg,
uDeviceHandle_t *pDeviceHandle)
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
uGnssTransportHandle_t gnssTransportHandle;
int32_t x;
const uDeviceCfgUart_t *pCfgUart;
const uDeviceCfgI2c_t *pCfgI2c;
const uDeviceCfgSpi_t *pCfgSpi;
const uDeviceCfgVirtualSerial_t *pCfgVirtualSerial;
const uDeviceCfgGnss_t *pCfgGnss;
uDeviceSerial_t *pDeviceSerial;
if ((pDevCfg != NULL) && (pDeviceHandle != NULL)) {
pCfgGnss = &(pDevCfg->deviceCfg.cfgGnss);
if (pCfgGnss->version == 0) {
// If a data ready pin is provided then the
// device PIO must also be provided
if ((pCfgGnss->pinDataReady < 0) ||
(pCfgGnss->devicePioDataReady >= 0)) {
switch (pDevCfg->transportType) {
case U_DEVICE_TRANSPORT_TYPE_UART:
// fall-through
case U_DEVICE_TRANSPORT_TYPE_UART_USB:
// fall-through
case U_DEVICE_TRANSPORT_TYPE_UART_2:
pCfgUart = &(pDevCfg->transportCfg.cfgUart);
if (pCfgUart->pPrefix != NULL) {
uPortUartPrefix(pCfgUart->pPrefix);
}
if (0 == pCfgUart->baudRate) {
// Negotiate baud rate
const unsigned baudRates[] = { 1200, 2400, 4800, 9600, 14400,
19200, 38400, 57600, 115200,
230400, 460800, 921600
};
for (int32_t i = ((sizeof(baudRates) / sizeof(baudRates[0])) - 1); i >= 0; --i) {
errorCode = uPortUartOpen(pCfgUart->uart,
baudRates[i], NULL,
U_GNSS_UART_BUFFER_LENGTH_BYTES,
pCfgUart->pinTxd,
pCfgUart->pinRxd,
pCfgUart->pinCts,
pCfgUart->pinRts);
if (errorCode >= 0) {
gnssTransportHandle.uart = errorCode;
errorCode = addDevice(gnssTransportHandle,
pDevCfg->transportType,
pCfgGnss, pDeviceHandle);
if (errorCode < 0) {
// Clean up on error
uPortUartClose(gnssTransportHandle.uart);
} else {
// Found acceptable baudrate
break;
}
}
}
} else {
// Open a UART with the recommended buffer length
// and default baud rate.
errorCode = uPortUartOpen(pCfgUart->uart,
pCfgUart->baudRate, NULL,
U_GNSS_UART_BUFFER_LENGTH_BYTES,
pCfgUart->pinTxd,
pCfgUart->pinRxd,
pCfgUart->pinCts,
pCfgUart->pinRts);
if (errorCode >= 0) {
gnssTransportHandle.uart = errorCode;
errorCode = addDevice(gnssTransportHandle,
pDevCfg->transportType,
pCfgGnss, pDeviceHandle);
if (errorCode < 0) {
// Clean up on error
uPortUartClose(gnssTransportHandle.uart);
}
}
}
break;
case U_DEVICE_TRANSPORT_TYPE_I2C:
pCfgI2c = &(pDevCfg->transportCfg.cfgI2c);
// Open the I2C instance.
errorCode = uDevicePrivateI2cOpen(pCfgI2c);
if (errorCode >= 0) {
gnssTransportHandle.i2c = errorCode;
errorCode = addDevice(gnssTransportHandle,
pDevCfg->transportType,
pCfgGnss, pDeviceHandle);
if (errorCode >= 0) {
// Log that the device is using the given I2C HW
x = uDevicePrivateI2cIsUsedBy(*pDeviceHandle, pCfgI2c);
if (x < 0) {
errorCode = x;
// Clean up if there's no room
removeDevice(*pDeviceHandle, true);
}
} else {
// Clean up on error
uDevicePrivateI2cCloseCfgI2c(pCfgI2c);
}
}
break;
case U_DEVICE_TRANSPORT_TYPE_SPI:
pCfgSpi = &(pDevCfg->transportCfg.cfgSpi);
// Open SPI.
errorCode = uPortSpiOpen(pCfgSpi->spi,
pCfgSpi->pinMosi,
pCfgSpi->pinMiso,
pCfgSpi->pinClk,
true);
if (errorCode >= 0) {
gnssTransportHandle.spi = errorCode;
if (pCfgSpi->maxSegmentSize > 0) {
if (uPortSpiSetMaxSegmentSize(gnssTransportHandle.spi,
pCfgSpi->maxSegmentSize) < 0) {
// Return a meaningful error code
errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
}
}
if (errorCode >= 0) {
errorCode = uPortSpiControllerSetDevice(gnssTransportHandle.spi,
&(pCfgSpi->device));
if (errorCode == 0) {
errorCode = addDevice(gnssTransportHandle,
pDevCfg->transportType,
pCfgGnss, pDeviceHandle);
}
}
if (errorCode < 0) {
// Clean up on error
uPortSpiClose(gnssTransportHandle.spi);
}
}
break;
case U_DEVICE_TRANSPORT_TYPE_VIRTUAL_SERIAL:
pCfgVirtualSerial = &(pDevCfg->transportCfg.cfgVirtualSerial);
pDeviceSerial = pCfgVirtualSerial->pDevice;
// Open a virtual serial port with the recommended buffer length
errorCode = pDeviceSerial->open(pDeviceSerial, NULL,
U_GNSS_UART_BUFFER_LENGTH_BYTES);
if (errorCode == 0) {
gnssTransportHandle.pDeviceSerial = pDeviceSerial;
errorCode = addDevice(gnssTransportHandle,
pDevCfg->transportType,
pCfgGnss, pDeviceHandle);
if (errorCode < 0) {
// Clean up on error
pDeviceSerial->close(pDeviceSerial);
}
}
break;
default:
break;
}
}
}
}
return errorCode;
}
// Remove a GNSS device.
int32_t uDevicePrivateGnssRemove(uDeviceHandle_t devHandle,
bool powerOff)
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
uDeviceGnssInstance_t *pContext = (uDeviceGnssInstance_t *) U_DEVICE_INSTANCE(devHandle)->pContext;
uDeviceTransportType_t deviceTransportType;
uDeviceGnssTransportHandle_t transportHandle;
if (pContext != NULL) {
deviceTransportType = pContext->deviceTransportType;
transportHandle = pContext->transportHandle;
errorCode = removeDevice(devHandle, powerOff);
if (errorCode == 0) {
// Having removed the device, close the transport
switch (deviceTransportType) {
case U_DEVICE_TRANSPORT_TYPE_UART:
// fall-through
case U_DEVICE_TRANSPORT_TYPE_UART_USB:
// fall-through
case U_DEVICE_TRANSPORT_TYPE_UART_2:
uPortUartClose(transportHandle.int32Handle);
break;
case U_DEVICE_TRANSPORT_TYPE_I2C:
uDevicePrivateI2cCloseDevHandle(devHandle);
break;
case U_DEVICE_TRANSPORT_TYPE_SPI:
uPortSpiClose(transportHandle.int32Handle);
break;
case U_DEVICE_TRANSPORT_TYPE_VIRTUAL_SERIAL:
pContext->transportHandle.pDeviceSerial->close(transportHandle.pDeviceSerial);
break;
default:
break;
}
}
}
return errorCode;
}
// End of file