Skip to content

Commit 07eb7d6

Browse files
authored
Merge pull request #3042 from kamtom480/gnss
Add gnss module
2 parents 3abdf74 + d768d4d commit 07eb7d6

17 files changed

+896
-1
lines changed

locale/circuitpython.pot

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-06-01 17:10-0700\n"
11+
"POT-Creation-Date: 2020-06-24 11:10+0200\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -572,6 +572,10 @@ msgstr ""
572572
msgid "Corrupt raw code"
573573
msgstr ""
574574

575+
#: ports/cxd56/common-hal/gnss/GNSS.c
576+
msgid "Could not initialize GNSS"
577+
msgstr ""
578+
575579
#: ports/atmel-samd/common-hal/busio/UART.c ports/cxd56/common-hal/busio/UART.c
576580
msgid "Could not initialize UART"
577581
msgstr ""
@@ -1425,6 +1429,10 @@ msgstr ""
14251429
msgid "Supply at least one UART pin"
14261430
msgstr ""
14271431

1432+
#: shared-bindings/gnss/GNSS.c
1433+
msgid "System entry must be gnss.SatelliteSystem"
1434+
msgstr ""
1435+
14281436
#: ports/stm/common-hal/microcontroller/Processor.c
14291437
msgid "Temperature read timed out"
14301438
msgstr ""
@@ -1611,6 +1619,7 @@ msgid ""
16111619
msgstr ""
16121620

16131621
#: ports/atmel-samd/common-hal/busio/I2C.c ports/cxd56/common-hal/busio/I2C.c
1622+
#: ports/stm/common-hal/busio/I2C.c
16141623
msgid "Unsupported baudrate"
16151624
msgstr ""
16161625

ports/cxd56/common-hal/gnss/GNSS.c

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright 2020 Sony Semiconductor Solutions Corporation
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <unistd.h>
28+
#include <fcntl.h>
29+
#include <string.h>
30+
#include <sys/ioctl.h>
31+
32+
#include "py/runtime.h"
33+
34+
#include "shared-bindings/gnss/GNSS.h"
35+
36+
typedef struct {
37+
const char* devpath;
38+
int fd;
39+
} gnss_dev_t;
40+
41+
STATIC gnss_dev_t gnss_dev = {"/dev/gps", -1};
42+
43+
static gnss_positionfix_t fix_to_positionfix_type(uint8_t fix) {
44+
switch (fix) {
45+
case CXD56_GNSS_PVT_POSFIX_2D:
46+
return POSITIONFIX_2D;
47+
case CXD56_GNSS_PVT_POSFIX_3D:
48+
return POSITIONFIX_3D;
49+
case CXD56_GNSS_PVT_POSFIX_INVALID:
50+
default:
51+
return POSITIONFIX_INVALID;
52+
}
53+
}
54+
55+
void common_hal_gnss_construct(gnss_obj_t *self, unsigned long selection) {
56+
if (gnss_dev.fd < 0) {
57+
gnss_dev.fd = open(gnss_dev.devpath, O_RDONLY);
58+
if (gnss_dev.fd < 0) {
59+
mp_raise_ValueError(translate("Could not initialize GNSS"));
60+
}
61+
}
62+
63+
self->satellite_system = 0;
64+
self->fix = POSITIONFIX_INVALID;
65+
66+
unsigned long sel = 0;
67+
68+
if (selection & SATELLITESYSTEM_GPS) {
69+
sel |= CXD56_GNSS_SAT_GPS;
70+
} else if (selection & SATELLITESYSTEM_GLONASS) {
71+
sel |= CXD56_GNSS_SAT_GLONASS;
72+
} else if (selection & SATELLITESYSTEM_SBAS) {
73+
sel |= CXD56_GNSS_SAT_SBAS;
74+
} else if (selection & SATELLITESYSTEM_QZSS_L1CA) {
75+
sel |= CXD56_GNSS_SAT_QZ_L1CA;
76+
} else if (selection & SATELLITESYSTEM_QZSS_L1S) {
77+
sel |= CXD56_GNSS_SAT_QZ_L1S;
78+
}
79+
80+
ioctl(gnss_dev.fd, CXD56_GNSS_IOCTL_SELECT_SATELLITE_SYSTEM, sel);
81+
ioctl(gnss_dev.fd, CXD56_GNSS_IOCTL_START, CXD56_GNSS_STMOD_COLD);
82+
}
83+
84+
void common_hal_gnss_deinit(gnss_obj_t *self) {
85+
if (common_hal_gnss_deinited(self)) {
86+
return;
87+
}
88+
89+
close(gnss_dev.fd);
90+
gnss_dev.fd = -1;
91+
}
92+
93+
bool common_hal_gnss_deinited(gnss_obj_t *self) {
94+
return gnss_dev.fd < 0;
95+
}
96+
97+
void common_hal_gnss_update(gnss_obj_t *self) {
98+
struct cxd56_gnss_positiondata_s positiondata;
99+
100+
read(gnss_dev.fd, &positiondata, sizeof(struct cxd56_gnss_positiondata_s));
101+
102+
if (positiondata.receiver.pos_dataexist) {
103+
self->fix = positiondata.receiver.pos_fixmode;
104+
self->latitude = positiondata.receiver.latitude;
105+
self->longitude = positiondata.receiver.longitude;
106+
self->altitude = positiondata.receiver.altitude;
107+
memcpy(&self->date, &positiondata.receiver.date, sizeof(struct cxd56_gnss_date_s));
108+
memcpy(&self->time, &positiondata.receiver.time, sizeof(struct cxd56_gnss_time_s));
109+
}
110+
}
111+
112+
mp_float_t common_hal_gnss_get_latitude(gnss_obj_t *self) {
113+
return (mp_float_t) self->latitude;
114+
}
115+
116+
mp_float_t common_hal_gnss_get_longitude(gnss_obj_t *self) {
117+
return (mp_float_t) self->longitude;
118+
}
119+
120+
mp_float_t common_hal_gnss_get_altitude(gnss_obj_t *self) {
121+
return (mp_float_t) self->altitude;
122+
}
123+
124+
void common_hal_gnss_get_timestamp(gnss_obj_t *self, timeutils_struct_time_t *tm) {
125+
tm->tm_year = self->date.year;
126+
tm->tm_mon = self->date.month;
127+
tm->tm_mday = self->date.day;
128+
tm->tm_hour = self->time.hour;
129+
tm->tm_min = self->time.minute;
130+
tm->tm_sec = self->time.sec;
131+
}
132+
133+
gnss_positionfix_t common_hal_gnss_get_fix(gnss_obj_t *self) {
134+
return fix_to_positionfix_type(self->fix);
135+
}

ports/cxd56/common-hal/gnss/GNSS.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright 2020 Sony Semiconductor Solutions Corporation
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_GNSS_GNSS_H
28+
#define MICROPY_INCLUDED_CXD56_COMMON_HAL_GNSS_GNSS_H
29+
30+
#include <arch/chip/gnss.h>
31+
32+
#include "py/obj.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
unsigned long satellite_system;
37+
uint8_t fix;
38+
double latitude;
39+
double longitude;
40+
double altitude;
41+
struct cxd56_gnss_date_s date;
42+
struct cxd56_gnss_time_s time;
43+
} gnss_obj_t;
44+
45+
#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_GNSS_GNSS_H

ports/cxd56/common-hal/gnss/PositionFix.c

Whitespace-only changes.

ports/cxd56/common-hal/gnss/SatelliteSystem.c

Whitespace-only changes.

ports/cxd56/common-hal/gnss/__init__.c

Whitespace-only changes.

ports/cxd56/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CIRCUITPY_COUNTIO = 0
1313
CIRCUITPY_DISPLAYIO = 0
1414
CIRCUITPY_FREQUENCYIO = 0
1515
CIRCUITPY_GAMEPAD = 0
16+
CIRCUITPY_GNSS = 1
1617
CIRCUITPY_I2CSLAVE = 0
1718
CIRCUITPY_NEOPIXEL_WRITE = 0
1819
CIRCUITPY_NVM = 0

py/circuitpy_defns.mk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ endif
162162
ifeq ($(CIRCUITPY_GAMEPADSHIFT),1)
163163
SRC_PATTERNS += gamepadshift/%
164164
endif
165+
ifeq ($(CIRCUITPY_GNSS),1)
166+
SRC_PATTERNS += gnss/%
167+
endif
165168
ifeq ($(CIRCUITPY_I2CSLAVE),1)
166169
SRC_PATTERNS += i2cslave/%
167170
endif
@@ -283,6 +286,10 @@ SRC_COMMON_HAL_ALL = \
283286
displayio/ParallelBus.c \
284287
frequencyio/FrequencyIn.c \
285288
frequencyio/__init__.c \
289+
gnss/__init__.c \
290+
gnss/GNSS.c \
291+
gnss/PositionFix.c \
292+
gnss/SatelliteSystem.c \
286293
i2cslave/I2CSlave.c \
287294
i2cslave/__init__.c \
288295
microcontroller/Pin.c \

py/circuitpy_mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,13 @@ extern const struct _mp_obj_module_t gamepadshift_module;
401401
#define GAMEPAD_ROOT_POINTERS
402402
#endif
403403

404+
#if CIRCUITPY_GNSS
405+
extern const struct _mp_obj_module_t gnss_module;
406+
#define GNSS_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_gnss), (mp_obj_t)&gnss_module },
407+
#else
408+
#define GNSS_MODULE
409+
#endif
410+
404411
#if CIRCUITPY_I2CSLAVE
405412
extern const struct _mp_obj_module_t i2cslave_module;
406413
#define I2CSLAVE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_i2cslave), (mp_obj_t)&i2cslave_module },
@@ -682,6 +689,7 @@ extern const struct _mp_obj_module_t watchdog_module;
682689
FREQUENCYIO_MODULE \
683690
GAMEPAD_MODULE \
684691
GAMEPADSHIFT_MODULE \
692+
GNSS_MODULE \
685693
I2CSLAVE_MODULE \
686694
JSON_MODULE \
687695
MATH_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ CFLAGS += -DCIRCUITPY_GAMEPAD=$(CIRCUITPY_GAMEPAD)
109109
CIRCUITPY_GAMEPADSHIFT ?= 0
110110
CFLAGS += -DCIRCUITPY_GAMEPADSHIFT=$(CIRCUITPY_GAMEPADSHIFT)
111111

112+
CIRCUITPY_GNSS ?= 0
113+
CFLAGS += -DCIRCUITPY_GNSS=$(CIRCUITPY_GNSS)
114+
112115
CIRCUITPY_I2CSLAVE ?= $(CIRCUITPY_FULL_BUILD)
113116
CFLAGS += -DCIRCUITPY_I2CSLAVE=$(CIRCUITPY_I2CSLAVE)
114117

0 commit comments

Comments
 (0)