-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathx_module_common.c
294 lines (210 loc) · 7.76 KB
/
x_module_common.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
/*
* Copyright 2022 u-blox Ltd
*
* 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 This file contains the implementation of the API to handle ublox
* modules common fuctions.
*/
#include "x_module_common.h"
// Zephyr/SDK Related
#include <drivers/uart.h>
#include <hal/nrf_gpio.h>
#include <hal/nrf_uarte.h>
#include <logging/log.h>
#include <logging/log_ctrl.h> //log_process
// Ubxlib related
//#include "u_port.h"
#include "ubxlib.h"
// Application related
#include "x_logging.h"
#include "x_wifi_ninaW156.h"
#include "x_cell_saraR5.h"
#include "x_pos_maxm10s.h"
#include "x_wifi_mqtt.h"
#include "x_cell_mqttsn.h"
#include "x_shell_conf.h"
#include "x_led.h"
#include "x_system_conf.h"
#include "x_pin_conf.h"
/* ----------------------------------------------------------------
* STATIC FUNCTION DECLARATION
* -------------------------------------------------------------- */
/** Thread called by xCommonUPortDeinit and deinitializes Zephyr port
* for ubxlib
*/
void xCommonUPortDeinitThread(void);
/* ----------------------------------------------------------------
* ZEPHYR RELATED DEFINITIONS/DECLARATIONS
* -------------------------------------------------------------- */
LOG_MODULE_REGISTER( LOGMOD_NAME_UBLMOD_COMMON, LOG_LEVEL_DBG );
// Semaphore definition
K_SEM_DEFINE(xCommonUPortDeinit_semaphore, 0, 1);
// Thread definition
K_THREAD_DEFINE(xCommonUPortDeinitThreadId, C210_UPORT_STACK_SIZE, xCommonUPortDeinitThread, NULL, NULL, NULL,
C210_UPORT_PRIORITY, 0, 0);
/* ----------------------------------------------------------------
* GLOBALS
* -------------------------------------------------------------- */
/** Flag signaling if ubxlib Port is initialized
*/
static bool gubxlibPortInitialized = false;
/** Signals which module (SARA or NINA) is configured in the common UART
* peripheral. Default is none
*/
static xCommonUart_t gCommonActiveUart = COMMON_UART_NONE;
/** Structure holding the pin configuration in case NINA is configured
* to work with the common UART share by NINA and SARA
*/
UARTE_PSEL_Type NINA ={
NINA_UART_RTS,
NINA_UART_TX,
NINA_UART_CTS,
NINA_UART_RX
};
/** Structure holding the pin configuration in case SARA is configured
* to work with the common UART share by NINA and SARA
*/
UARTE_PSEL_Type SARA ={
SARA_SEC_UART_RTS,
SARA_SEC_UART_TX,
SARA_SEC_UART_CTS,
SARA_SEC_UART_RX
};
/* ----------------------------------------------------------------
* STATIC FUNCTION IMPLEMENTATION
* -------------------------------------------------------------- */
void xCommonUPortDeinitThread(void){
// needed to avoid thread overflows when using ubxlib functions within a thread
k_thread_system_pool_assign(k_current_get());
while(1){
// Semaphore given by xCommonUPortDeinit
k_sem_take( &xCommonUPortDeinit_semaphore, K_FOREVER );
LOG_INF("uPort Deinitialize request \r\n");
// no need to do something
if( !gubxlibPortInitialized ){
//operation_result = success
continue;
}
// get Max module status and close
xPosMaxM10Status_t maxM10status = xPosMaxM10GetModuleStatus();
if( maxM10status.isUbxInit){
xPosMaxM10Deinit();
}
// get WiFi network initialization status and close if necessary
// also handle mqtt client
xWifiNinaStatus_t ninaStatus = xWifiNinaGetModuleStatus();
if( ninaStatus.uStatus >= uDeviceApiInitialized ){
xWifiNinaDeviceClose();
}
// get Cellular network initialization status and close (SARA)
// if necessary. Also handles MQTT-SN client
xCellSaraStatus_t saraStatus = xCellSaraGetModuleStatus();
if( saraStatus.uStatus >= uDeviceApiInitialized ){
xCellSaraDeviceClose();
while( ( saraStatus.uStatus >= uDeviceApiInitialized ) ){
k_sleep(K_MSEC(1000));
saraStatus = xCellSaraGetModuleStatus();
//we do not check errors here
//operation_result = xCellSaraGetLastOperationResult();
}
}
// port deinit optical indication
// both cell and wifi have the same color in deactivation
xLedFade(WIFI_DEACTIVATING_LEDCOL,WIFI_ACTIVATING_LED_DELAY_ON, WIFI_ACTIVATING_LED_DELAY_ON, 0);
//give time for pending logging messages (if any)
k_sleep(K_MSEC(1000));
// Process any pending logs from sensor modules prior to disabling
// (this could cause problems in very fast sampling rates? < 100ms )
while( log_process(false) ){
;
}
// save logger state (in order to restore it after shell is deinitialized
// and then reinitialized). If is not done, logger state resets to default settings
xLogSaveState();
// shell is uninitialized before uPort deinitialization. If uPortDeinit
// is called with shell enabled, the console uart input crashes.
xShellDeinit();
// wait for shell to deinitialize
//this does not work...
//while( !xShellDeinitIsComplete() ){
//wait
//}
// a wait is used instead
k_sleep(K_MSEC(1000));
// finally close uPort
uPortDeinit();
// reinitilize shell and restore logger settings
xShellReinitTrigger();
k_sleep(K_MSEC(1000));
xLogRestoreState();
LOG_INF("Port DeInitialized\r\n");
xLedOff();
gubxlibPortInitialized = false;
//operation_result = SUCCESS
}
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTION IMPLEMENTATION
* -------------------------------------------------------------- */
bool xCommonUPortIsInit(){
return gubxlibPortInitialized;
}
void xCommonUPortDeinit(void){
k_sem_give( &xCommonUPortDeinit_semaphore );
}
void xCommonUartCfg(xCommonUart_t type){
UARTE_PSEL_Type pins;
if( (type == COMMON_UART_SARA) && (gCommonActiveUart != COMMON_UART_SARA) ){
pins = SARA;
}
else if( (type == COMMON_UART_NINA) && (gCommonActiveUart != COMMON_UART_NINA) ){
pins = NINA;
}
else{
return;
}
nrf_uarte_disable(NRF_UARTE2_S);
// Set up TX and RX pins.
nrf_gpio_pin_set(pins.TXD);
nrf_gpio_cfg_output(pins.TXD);
nrf_gpio_cfg_input(pins.RXD, NRF_GPIO_PIN_NOPULL);
nrf_uarte_txrx_pins_set(NRF_UARTE2_S, pins.TXD, pins.RXD);
// Set up CTS and RTS pins.
nrf_gpio_cfg_input(pins.CTS, NRF_GPIO_PIN_NOPULL);
nrf_gpio_pin_set(pins.RTS);
nrf_gpio_cfg_output(pins.RTS);
nrf_uarte_hwfc_pins_set(NRF_UARTE2_S, pins.RTS, pins.CTS);
// enable UARTE
nrf_uarte_enable(NRF_UARTE2_S);
// kick-start Rx
nrf_uarte_task_trigger(NRF_UARTE2_S, NRF_UARTE_TASK_STARTRX);
gCommonActiveUart = type;
}
err_code xCommonUPortInit(void){
err_code err;
if(gubxlibPortInitialized){
// de-initialize previous ubxlib configuration, before initializing a new one
LOG_INF("uPort Already Initialized \r\n");
return X_ERR_INVALID_STATE;
}
if( ( err = uPortInit() ) == 0){
gubxlibPortInitialized = true;
LOG_INF("uPort Initialized \r\n");
}
else{
LOG_ERR("uPortInit failed \r\n");
}
return err;
}