kh

Dependencies:   Adafruit_SGP30_mbed mbed CCS811 mbed-rtos ALPHASENSE Adafruit_ADS1015_ BME680

Committer:
etiene32
Date:
Wed Mar 06 14:03:41 2019 +0000
Revision:
0:43070b2dfc87
,hb

Who changed what in which revision?

UserRevisionLine numberNew contents of line
etiene32 0:43070b2dfc87 1 /*
etiene32 0:43070b2dfc87 2 * -------------------------------------------------------------------------
etiene32 0:43070b2dfc87 3 * Copyright (C) 2017 STMicroelectronics
etiene32 0:43070b2dfc87 4 * Author: Francesco M. Virlinzi <francesco.virlinzi@st.com>
etiene32 0:43070b2dfc87 5 *
etiene32 0:43070b2dfc87 6 * May be copied or modified under the terms of the GNU General Public
etiene32 0:43070b2dfc87 7 * License V.2 ONLY. See linux/COPYING for more information.
etiene32 0:43070b2dfc87 8 *
etiene32 0:43070b2dfc87 9 * -------------------------------------------------------------------------
etiene32 0:43070b2dfc87 10 */
etiene32 0:43070b2dfc87 11 #include "string.h"
etiene32 0:43070b2dfc87 12 #include "Teseo-LIV3F.h"
etiene32 0:43070b2dfc87 13
etiene32 0:43070b2dfc87 14
etiene32 0:43070b2dfc87 15 static char T3_name[] = "Teseo-LIV3F";
etiene32 0:43070b2dfc87 16
etiene32 0:43070b2dfc87 17 char _OK[] = "OK";
etiene32 0:43070b2dfc87 18 char _Failed[] = "Failed";
etiene32 0:43070b2dfc87 19 char X_Nucleo_name[] = "X-Nucleo-GNSS1A1";
etiene32 0:43070b2dfc87 20
etiene32 0:43070b2dfc87 21 struct teseo_cmd {
etiene32 0:43070b2dfc87 22 char *cmd;
etiene32 0:43070b2dfc87 23 };
etiene32 0:43070b2dfc87 24
etiene32 0:43070b2dfc87 25 static struct teseo_cmd teseo_cmds[] = {
etiene32 0:43070b2dfc87 26 [Teseo_LIV3F::GETSWVER] = {
etiene32 0:43070b2dfc87 27 .cmd = "$PSTMGETSWVER,7",
etiene32 0:43070b2dfc87 28 },
etiene32 0:43070b2dfc87 29 [Teseo_LIV3F::FORCESTANDBY] = {
etiene32 0:43070b2dfc87 30 .cmd = "$PSTMFORCESTANDBY,00007",
etiene32 0:43070b2dfc87 31 },
etiene32 0:43070b2dfc87 32 [Teseo_LIV3F::RFTESTON] = {
etiene32 0:43070b2dfc87 33 .cmd = "$PSTMRFTESTON,16",
etiene32 0:43070b2dfc87 34 },
etiene32 0:43070b2dfc87 35 [Teseo_LIV3F::RFTESTOFF] = {
etiene32 0:43070b2dfc87 36 .cmd = "$PSTMRFTESTOFF\n\r",
etiene32 0:43070b2dfc87 37 },
etiene32 0:43070b2dfc87 38 [Teseo_LIV3F::LOWPOWER] = {
etiene32 0:43070b2dfc87 39 .cmd = "$PSTMLOWPOWERONOFF,1,0,000,05,0,1,000,1,00010,01,0,0,1,01",
etiene32 0:43070b2dfc87 40 },
etiene32 0:43070b2dfc87 41 [Teseo_LIV3F::FWUPDATE] = {
etiene32 0:43070b2dfc87 42 .cmd = "$PSTMFWUPGRADE",
etiene32 0:43070b2dfc87 43 },
etiene32 0:43070b2dfc87 44 };
etiene32 0:43070b2dfc87 45
etiene32 0:43070b2dfc87 46
etiene32 0:43070b2dfc87 47 Teseo_LIV3F::Teseo_LIV3F(PinName reset_pin, PinName wakeup_pin,
etiene32 0:43070b2dfc87 48 PinName pps_pin, PinName uart_tx_pin, PinName uart_rx_pin,
etiene32 0:43070b2dfc87 49 Serial *serial_debug):
etiene32 0:43070b2dfc87 50 _reset(reset_pin, 1),
etiene32 0:43070b2dfc87 51 _pps(pps_pin),
etiene32 0:43070b2dfc87 52 _wakeup(wakeup_pin, 0),
etiene32 0:43070b2dfc87 53 _uart(uart_rx_pin, uart_tx_pin, SDT_UART_BAUD),
etiene32 0:43070b2dfc87 54 _serial_debug(serial_debug)
etiene32 0:43070b2dfc87 55 {
etiene32 0:43070b2dfc87 56 wait_ms(POWERON_STABLE_SIGNAL_DELAY_MS);
etiene32 0:43070b2dfc87 57 _uart.baud(SDT_UART_BAUD);
etiene32 0:43070b2dfc87 58 _uart.format(8, SerialBase::None, 1);
etiene32 0:43070b2dfc87 59 _i2c = NULL;
etiene32 0:43070b2dfc87 60 _uart_interleaded = false;
etiene32 0:43070b2dfc87 61 _uart_discard = false;
etiene32 0:43070b2dfc87 62 }
etiene32 0:43070b2dfc87 63
etiene32 0:43070b2dfc87 64 Teseo_LIV3F::Teseo_LIV3F(PinName reset_pin, PinName wakeup_pin,
etiene32 0:43070b2dfc87 65 PinName pps_pin, PinName uart_tx_pin, PinName uart_rx_pin,
etiene32 0:43070b2dfc87 66 I2C *bus, Serial *serial_debug):
etiene32 0:43070b2dfc87 67 _reset(reset_pin, 1),
etiene32 0:43070b2dfc87 68 _pps(pps_pin),
etiene32 0:43070b2dfc87 69 _wakeup(wakeup_pin, 0),
etiene32 0:43070b2dfc87 70 _uart(uart_rx_pin, uart_tx_pin, SDT_UART_BAUD),
etiene32 0:43070b2dfc87 71 _i2c(bus),
etiene32 0:43070b2dfc87 72 _serial_debug(serial_debug)
etiene32 0:43070b2dfc87 73 {
etiene32 0:43070b2dfc87 74 wait_ms(POWERON_STABLE_SIGNAL_DELAY_MS);
etiene32 0:43070b2dfc87 75 _uart.baud(SDT_UART_BAUD);
etiene32 0:43070b2dfc87 76 _uart.format(8, SerialBase::None, 1);
etiene32 0:43070b2dfc87 77 _uart_interleaded = false;
etiene32 0:43070b2dfc87 78 _uart_discard = false;
etiene32 0:43070b2dfc87 79 }
etiene32 0:43070b2dfc87 80
etiene32 0:43070b2dfc87 81 int Teseo_LIV3F::EnableLowPower()
etiene32 0:43070b2dfc87 82 {
etiene32 0:43070b2dfc87 83 SendCommand(LOWPOWER);
etiene32 0:43070b2dfc87 84 return 0;
etiene32 0:43070b2dfc87 85 }
etiene32 0:43070b2dfc87 86
etiene32 0:43070b2dfc87 87 void Teseo_LIV3F::Reset(Serial *serial_debug)
etiene32 0:43070b2dfc87 88 {
etiene32 0:43070b2dfc87 89 if (serial_debug)
etiene32 0:43070b2dfc87 90 serial_debug->printf("%s: Resetting...", T3_name);
etiene32 0:43070b2dfc87 91
etiene32 0:43070b2dfc87 92 _reset.write(0);
etiene32 0:43070b2dfc87 93
etiene32 0:43070b2dfc87 94 wait_ms(50);
etiene32 0:43070b2dfc87 95
etiene32 0:43070b2dfc87 96 _reset.write(1);
etiene32 0:43070b2dfc87 97
etiene32 0:43070b2dfc87 98 wait_ms(70);
etiene32 0:43070b2dfc87 99
etiene32 0:43070b2dfc87 100 if (serial_debug)
etiene32 0:43070b2dfc87 101 serial_debug->printf("Done...\n\r");
etiene32 0:43070b2dfc87 102 }
etiene32 0:43070b2dfc87 103
etiene32 0:43070b2dfc87 104 enum {
etiene32 0:43070b2dfc87 105 TESEO_FLASHER_IDENTIFIER = 0, // 0xBCD501F4
etiene32 0:43070b2dfc87 106 TESEO_FLASHER_SYNC, // 0x83984073
etiene32 0:43070b2dfc87 107 DEVICE_START_COMMUNICATION, // 0xA3
etiene32 0:43070b2dfc87 108 FLASHER_READY, // 0x4A
etiene32 0:43070b2dfc87 109 ACK, // 0xCC
etiene32 0:43070b2dfc87 110 NACK,
etiene32 0:43070b2dfc87 111 };
etiene32 0:43070b2dfc87 112
etiene32 0:43070b2dfc87 113 struct firmware_ctrl {
etiene32 0:43070b2dfc87 114 char *cmd;
etiene32 0:43070b2dfc87 115 unsigned char len;
etiene32 0:43070b2dfc87 116 char *n;
etiene32 0:43070b2dfc87 117 } ;
etiene32 0:43070b2dfc87 118
etiene32 0:43070b2dfc87 119 /*
etiene32 0:43070b2dfc87 120 * #define FWUPG_IDENTIFIER 0xBC D5 01 F4
etiene32 0:43070b2dfc87 121 * #define FWUPG_SYNC 0x83 98 40 73
etiene32 0:43070b2dfc87 122 */
etiene32 0:43070b2dfc87 123 static struct firmware_ctrl fw_data[] = {
etiene32 0:43070b2dfc87 124 [TESEO_FLASHER_IDENTIFIER] = {
etiene32 0:43070b2dfc87 125 .cmd = (char *)(char[]){ 0xF4, 0x01, 0xD5, 0xBC},
etiene32 0:43070b2dfc87 126 .len = 4,
etiene32 0:43070b2dfc87 127 .n = "TESEO_FLASHER_IDENTIFIER",
etiene32 0:43070b2dfc87 128 },
etiene32 0:43070b2dfc87 129 [TESEO_FLASHER_SYNC] = {
etiene32 0:43070b2dfc87 130 .cmd =(char *)(char[]){ 0x73, 0x40, 0x98, 0x83 },
etiene32 0:43070b2dfc87 131 .len = 4,
etiene32 0:43070b2dfc87 132 .n = "TESEO_FLASHER_SYNC",
etiene32 0:43070b2dfc87 133 },
etiene32 0:43070b2dfc87 134 [DEVICE_START_COMMUNICATION] = {
etiene32 0:43070b2dfc87 135 .cmd = (char *)(char[]){0xA3},
etiene32 0:43070b2dfc87 136 .len = 1,
etiene32 0:43070b2dfc87 137 .n = "DEVICE_START_COMMUNICATION",
etiene32 0:43070b2dfc87 138 },
etiene32 0:43070b2dfc87 139 [FLASHER_READY] = {
etiene32 0:43070b2dfc87 140 .cmd = (char *)(char[]){0x4A},
etiene32 0:43070b2dfc87 141 .len = 1,
etiene32 0:43070b2dfc87 142 .n = "FLASHER_READY",
etiene32 0:43070b2dfc87 143 },
etiene32 0:43070b2dfc87 144 [ACK] = {
etiene32 0:43070b2dfc87 145 .cmd = (char *)(char[]){0xCC},
etiene32 0:43070b2dfc87 146 .len = 1,
etiene32 0:43070b2dfc87 147 .n = "ACK",
etiene32 0:43070b2dfc87 148 },
etiene32 0:43070b2dfc87 149 [NACK] = {
etiene32 0:43070b2dfc87 150 .cmd = (char *)(char[]){0xDD},
etiene32 0:43070b2dfc87 151 .len = 1,
etiene32 0:43070b2dfc87 152 .n = "NACK",
etiene32 0:43070b2dfc87 153 },
etiene32 0:43070b2dfc87 154 };
etiene32 0:43070b2dfc87 155
etiene32 0:43070b2dfc87 156 int Teseo_LIV3F::SendString(char *buf, int len)
etiene32 0:43070b2dfc87 157 {
etiene32 0:43070b2dfc87 158 for (int i = 0; i < len; ++i) {
etiene32 0:43070b2dfc87 159 while (!_uart.writeable());
etiene32 0:43070b2dfc87 160 _uart.putc(buf[i]);
etiene32 0:43070b2dfc87 161 }
etiene32 0:43070b2dfc87 162
etiene32 0:43070b2dfc87 163 }
etiene32 0:43070b2dfc87 164
etiene32 0:43070b2dfc87 165 struct ImageOptions
etiene32 0:43070b2dfc87 166 {
etiene32 0:43070b2dfc87 167 unsigned char eraseNVM;
etiene32 0:43070b2dfc87 168 unsigned char programOnly;
etiene32 0:43070b2dfc87 169 unsigned char reserved;
etiene32 0:43070b2dfc87 170 unsigned char baudRate;
etiene32 0:43070b2dfc87 171 unsigned int firmwareSize;
etiene32 0:43070b2dfc87 172 unsigned int firmwareCRC;
etiene32 0:43070b2dfc87 173 unsigned int nvmAddressOffset;
etiene32 0:43070b2dfc87 174 unsigned int nvmSize;
etiene32 0:43070b2dfc87 175 } liv3f_img_option = {
etiene32 0:43070b2dfc87 176 .eraseNVM = 1,
etiene32 0:43070b2dfc87 177 .programOnly = 0,
etiene32 0:43070b2dfc87 178 .reserved = 0,
etiene32 0:43070b2dfc87 179 .baudRate = 1,
etiene32 0:43070b2dfc87 180 .firmwareSize = 0,
etiene32 0:43070b2dfc87 181 .firmwareCRC = 0,
etiene32 0:43070b2dfc87 182 .nvmAddressOffset = 0x00100000,
etiene32 0:43070b2dfc87 183 .nvmSize = 0x00100000,
etiene32 0:43070b2dfc87 184
etiene32 0:43070b2dfc87 185 };
etiene32 0:43070b2dfc87 186
etiene32 0:43070b2dfc87 187 int Teseo_LIV3F::FwWaitAck()
etiene32 0:43070b2dfc87 188 {
etiene32 0:43070b2dfc87 189 while (!_uart.readable());
etiene32 0:43070b2dfc87 190
etiene32 0:43070b2dfc87 191 char c = _uart.getc();
etiene32 0:43070b2dfc87 192
etiene32 0:43070b2dfc87 193 if (fw_data[ACK].cmd[0] == c) {
etiene32 0:43070b2dfc87 194 if (_serial_debug)
etiene32 0:43070b2dfc87 195 _serial_debug->printf("%s (0x%x)\n\r", _OK, c);
etiene32 0:43070b2dfc87 196 return 0;
etiene32 0:43070b2dfc87 197 }
etiene32 0:43070b2dfc87 198
etiene32 0:43070b2dfc87 199 if (fw_data[NACK].cmd[0] == c) {
etiene32 0:43070b2dfc87 200 if (_serial_debug)
etiene32 0:43070b2dfc87 201 _serial_debug->printf("%s (%x)\n\r", _Failed, c);
etiene32 0:43070b2dfc87 202 return -1;
etiene32 0:43070b2dfc87 203 }
etiene32 0:43070b2dfc87 204
etiene32 0:43070b2dfc87 205 if (_serial_debug)
etiene32 0:43070b2dfc87 206 _serial_debug->printf("%s - Char not allowed (%x)\n\r", _Failed, c);
etiene32 0:43070b2dfc87 207 return -1;
etiene32 0:43070b2dfc87 208 }
etiene32 0:43070b2dfc87 209
etiene32 0:43070b2dfc87 210 bool Teseo_LIV3F::FirmwareUpdate(bool is_recovery, char *data,
etiene32 0:43070b2dfc87 211 unsigned int data_len,
etiene32 0:43070b2dfc87 212 unsigned long crc,
etiene32 0:43070b2dfc87 213 Serial *serial_debug)
etiene32 0:43070b2dfc87 214 {
etiene32 0:43070b2dfc87 215 unsigned int i;
etiene32 0:43070b2dfc87 216
etiene32 0:43070b2dfc87 217 char _buf[4] = { 0xff, 0xff, 0xff, 0xff };
etiene32 0:43070b2dfc87 218
etiene32 0:43070b2dfc87 219 liv3f_img_option.firmwareSize = data_len;
etiene32 0:43070b2dfc87 220 liv3f_img_option.firmwareCRC = crc;
etiene32 0:43070b2dfc87 221
etiene32 0:43070b2dfc87 222 if (data == NULL || !data_len)
etiene32 0:43070b2dfc87 223 return false;
etiene32 0:43070b2dfc87 224
etiene32 0:43070b2dfc87 225 if (is_recovery)
etiene32 0:43070b2dfc87 226 Reset();
etiene32 0:43070b2dfc87 227
etiene32 0:43070b2dfc87 228 {
etiene32 0:43070b2dfc87 229
etiene32 0:43070b2dfc87 230 _uart.baud(FWU_UART_BAUD);
etiene32 0:43070b2dfc87 231
etiene32 0:43070b2dfc87 232 #if 1
etiene32 0:43070b2dfc87 233 while (1) {
etiene32 0:43070b2dfc87 234 /* send TESEO_FLASHER_IDENTIFIER */
etiene32 0:43070b2dfc87 235 /*
etiene32 0:43070b2dfc87 236 * Device is under reset. Host sends continuously “TESEO2_FLASHER_IDENTIFIER� word.
etiene32 0:43070b2dfc87 237 */
etiene32 0:43070b2dfc87 238 SendString(fw_data[TESEO_FLASHER_IDENTIFIER].cmd, fw_data[TESEO_FLASHER_IDENTIFIER].len);
etiene32 0:43070b2dfc87 239
etiene32 0:43070b2dfc87 240 /* try to read... TESEO_FLASHER_SYNC */
etiene32 0:43070b2dfc87 241 if (_uart.readable())
etiene32 0:43070b2dfc87 242 for (i = 0; i < fw_data[TESEO_FLASHER_SYNC].len; ) {
etiene32 0:43070b2dfc87 243
etiene32 0:43070b2dfc87 244 while (!_uart.readable());
etiene32 0:43070b2dfc87 245
etiene32 0:43070b2dfc87 246 _buf[i] = _uart.getc();
etiene32 0:43070b2dfc87 247
etiene32 0:43070b2dfc87 248 if (fw_data[TESEO_FLASHER_SYNC].cmd[i] == _buf[i]) {
etiene32 0:43070b2dfc87 249 if (serial_debug)
etiene32 0:43070b2dfc87 250 serial_debug->printf("-- %d -- 0x%x -- ok--\n\r", i, _buf[i]);
etiene32 0:43070b2dfc87 251 i++;
etiene32 0:43070b2dfc87 252 goto exit_step_1; /* FMV: WA to have firmware update working.... */
etiene32 0:43070b2dfc87 253 } else {
etiene32 0:43070b2dfc87 254 i = 0;
etiene32 0:43070b2dfc87 255 }
etiene32 0:43070b2dfc87 256 }
etiene32 0:43070b2dfc87 257 if (i == fw_data[TESEO_FLASHER_SYNC].len)
etiene32 0:43070b2dfc87 258 goto exit_step_1;
etiene32 0:43070b2dfc87 259 }
etiene32 0:43070b2dfc87 260
etiene32 0:43070b2dfc87 261 exit_step_1:
etiene32 0:43070b2dfc87 262
etiene32 0:43070b2dfc87 263 _uart.abort_read();
etiene32 0:43070b2dfc87 264
etiene32 0:43070b2dfc87 265 if (serial_debug)
etiene32 0:43070b2dfc87 266 serial_debug->printf("Got: %s from %s\n\r", fw_data[TESEO_FLASHER_SYNC].n, T3_name);
etiene32 0:43070b2dfc87 267
etiene32 0:43070b2dfc87 268 /*
etiene32 0:43070b2dfc87 269 * Host sends “DEVICE_START_COMMUNICATION� word.
etiene32 0:43070b2dfc87 270 */
etiene32 0:43070b2dfc87 271 serial_debug->printf("\n\r%s Step: %s ",T3_name, fw_data[DEVICE_START_COMMUNICATION].n);
etiene32 0:43070b2dfc87 272 SendString(fw_data[DEVICE_START_COMMUNICATION].cmd, fw_data[DEVICE_START_COMMUNICATION].len);
etiene32 0:43070b2dfc87 273
etiene32 0:43070b2dfc87 274 FwWaitAck();
etiene32 0:43070b2dfc87 275
etiene32 0:43070b2dfc87 276 /*
etiene32 0:43070b2dfc87 277 * Host sends the binary image options. Both host and
etiene32 0:43070b2dfc87 278 * device change UART baud rates. Host sends continuously
etiene32 0:43070b2dfc87 279 * the “FLASHER_READY� word.
etiene32 0:43070b2dfc87 280 */
etiene32 0:43070b2dfc87 281 if (serial_debug)
etiene32 0:43070b2dfc87 282 serial_debug->printf("%s Step: Send ImageOption\n\r",T3_name);
etiene32 0:43070b2dfc87 283
etiene32 0:43070b2dfc87 284 SendString((char*)&liv3f_img_option, sizeof(ImageOptions));
etiene32 0:43070b2dfc87 285
etiene32 0:43070b2dfc87 286 if (serial_debug)
etiene32 0:43070b2dfc87 287 serial_debug->printf("%s Step: Send %s\n\r",T3_name, fw_data[FLASHER_READY].n);
etiene32 0:43070b2dfc87 288
etiene32 0:43070b2dfc87 289 while (1) {
etiene32 0:43070b2dfc87 290 SendString(fw_data[FLASHER_READY].cmd, fw_data[FLASHER_READY].len);
etiene32 0:43070b2dfc87 291 if (_uart.readable())
etiene32 0:43070b2dfc87 292 goto exit_step_3;
etiene32 0:43070b2dfc87 293 }
etiene32 0:43070b2dfc87 294
etiene32 0:43070b2dfc87 295 exit_step_3:
etiene32 0:43070b2dfc87 296 FwWaitAck();
etiene32 0:43070b2dfc87 297
etiene32 0:43070b2dfc87 298 if (serial_debug)
etiene32 0:43070b2dfc87 299 serial_debug->printf("%s Step: Erasing flash area ",T3_name);
etiene32 0:43070b2dfc87 300
etiene32 0:43070b2dfc87 301
etiene32 0:43070b2dfc87 302 FwWaitAck();
etiene32 0:43070b2dfc87 303
etiene32 0:43070b2dfc87 304 /*
etiene32 0:43070b2dfc87 305 * Device is erasing flash program. Host is waiting for an “ACK�.
etiene32 0:43070b2dfc87 306 */
etiene32 0:43070b2dfc87 307
etiene32 0:43070b2dfc87 308 if (serial_debug)
etiene32 0:43070b2dfc87 309 serial_debug->printf("%s Step: Erasing NVM ",T3_name);
etiene32 0:43070b2dfc87 310
etiene32 0:43070b2dfc87 311 while (!_uart.readable());
etiene32 0:43070b2dfc87 312
etiene32 0:43070b2dfc87 313 FwWaitAck();
etiene32 0:43070b2dfc87 314
etiene32 0:43070b2dfc87 315 if (serial_debug)
etiene32 0:43070b2dfc87 316 serial_debug->printf("%s Step: Sending data... ",T3_name);
etiene32 0:43070b2dfc87 317
etiene32 0:43070b2dfc87 318 for (i = 0; i < (data_len / (16*1024)); ++i) {
etiene32 0:43070b2dfc87 319 SendString(&data[i], 16*1024);
etiene32 0:43070b2dfc87 320
etiene32 0:43070b2dfc87 321 FwWaitAck();
etiene32 0:43070b2dfc87 322 }
etiene32 0:43070b2dfc87 323
etiene32 0:43070b2dfc87 324 serial_debug->printf("\n\r");
etiene32 0:43070b2dfc87 325 /*
etiene32 0:43070b2dfc87 326 * send remaining data...
etiene32 0:43070b2dfc87 327 */
etiene32 0:43070b2dfc87 328 if (data_len != (i * 16*1024)) {
etiene32 0:43070b2dfc87 329 SendString(&data[i*16*1024], data_len-i*16*1024);
etiene32 0:43070b2dfc87 330 FwWaitAck();
etiene32 0:43070b2dfc87 331 }
etiene32 0:43070b2dfc87 332 /*
etiene32 0:43070b2dfc87 333 * wait CRC ack
etiene32 0:43070b2dfc87 334 */
etiene32 0:43070b2dfc87 335 FwWaitAck();
etiene32 0:43070b2dfc87 336 }
etiene32 0:43070b2dfc87 337
etiene32 0:43070b2dfc87 338 #else
etiene32 0:43070b2dfc87 339 //_uart.format(8, SerialBase::Forced0, 1);
etiene32 0:43070b2dfc87 340 while (1)
etiene32 0:43070b2dfc87 341 {
etiene32 0:43070b2dfc87 342 /* send TESEO_FLASHER_IDENTIFIER */
etiene32 0:43070b2dfc87 343 for (i = 0; i < fw_data[TESEO_FLASHER_IDENTIFIER].len; ++i) {
etiene32 0:43070b2dfc87 344 while (!_uart.writeable());
etiene32 0:43070b2dfc87 345 _uart.putc(fw_data[TESEO_FLASHER_IDENTIFIER].cmd[i]);
etiene32 0:43070b2dfc87 346 }
etiene32 0:43070b2dfc87 347
etiene32 0:43070b2dfc87 348
etiene32 0:43070b2dfc87 349 /* try to read... TESEO_FLASHER_SYNC */
etiene32 0:43070b2dfc87 350 //while (!_uart.readable());
etiene32 0:43070b2dfc87 351 for (i = 0; i < fw_data[TESEO_FLASHER_SYNC].len && _uart.readable(); )
etiene32 0:43070b2dfc87 352 if (_uart.readable())
etiene32 0:43070b2dfc87 353 {
etiene32 0:43070b2dfc87 354 char c = _uart.getc();
etiene32 0:43070b2dfc87 355 /*
etiene32 0:43070b2dfc87 356 if (!c)
etiene32 0:43070b2dfc87 357 break;
etiene32 0:43070b2dfc87 358 */
etiene32 0:43070b2dfc87 359 if (serial_debug)
etiene32 0:43070b2dfc87 360 serial_debug->printf("%x vs %x\n\r", c, fw_data[TESEO_FLASHER_SYNC].cmd[i]);
etiene32 0:43070b2dfc87 361
etiene32 0:43070b2dfc87 362 if (fw_data[TESEO_FLASHER_SYNC].cmd[i] == c)
etiene32 0:43070b2dfc87 363 i++;
etiene32 0:43070b2dfc87 364 else
etiene32 0:43070b2dfc87 365 i = 0;
etiene32 0:43070b2dfc87 366 }
etiene32 0:43070b2dfc87 367
etiene32 0:43070b2dfc87 368 if (i == fw_data[TESEO_FLASHER_SYNC].len && serial_debug)
etiene32 0:43070b2dfc87 369 serial_debug->printf("Got %s from %s\n\r",fw_data[TESEO_FLASHER_SYNC].n, T3_name);
etiene32 0:43070b2dfc87 370 }
etiene32 0:43070b2dfc87 371 }
etiene32 0:43070b2dfc87 372 #endif
etiene32 0:43070b2dfc87 373 if (serial_debug)
etiene32 0:43070b2dfc87 374 serial_debug->printf("END\n\r");
etiene32 0:43070b2dfc87 375
etiene32 0:43070b2dfc87 376 return true;
etiene32 0:43070b2dfc87 377 }
etiene32 0:43070b2dfc87 378
etiene32 0:43070b2dfc87 379 int Teseo_LIV3F::WakeUp()
etiene32 0:43070b2dfc87 380 {
etiene32 0:43070b2dfc87 381 wait_ms(100);
etiene32 0:43070b2dfc87 382
etiene32 0:43070b2dfc87 383 _wakeup.write(1);
etiene32 0:43070b2dfc87 384
etiene32 0:43070b2dfc87 385 wait_ms(500);
etiene32 0:43070b2dfc87 386
etiene32 0:43070b2dfc87 387 _wakeup.write(0);
etiene32 0:43070b2dfc87 388
etiene32 0:43070b2dfc87 389 return 0;
etiene32 0:43070b2dfc87 390 }
etiene32 0:43070b2dfc87 391
etiene32 0:43070b2dfc87 392 bool Teseo_LIV3F::CheckPPSWorking()
etiene32 0:43070b2dfc87 393 {
etiene32 0:43070b2dfc87 394 int val_0, val_1;
etiene32 0:43070b2dfc87 395
etiene32 0:43070b2dfc87 396 wait_ms(500);
etiene32 0:43070b2dfc87 397
etiene32 0:43070b2dfc87 398 val_0 = _pps.read();
etiene32 0:43070b2dfc87 399
etiene32 0:43070b2dfc87 400 wait_ms(500);
etiene32 0:43070b2dfc87 401 val_1 = _pps.read();
etiene32 0:43070b2dfc87 402
etiene32 0:43070b2dfc87 403 if (val_0 != val_1)
etiene32 0:43070b2dfc87 404 return true;
etiene32 0:43070b2dfc87 405
etiene32 0:43070b2dfc87 406 return false;
etiene32 0:43070b2dfc87 407 }
etiene32 0:43070b2dfc87 408
etiene32 0:43070b2dfc87 409 int Teseo_LIV3F::CRC_(char *buf, int size)
etiene32 0:43070b2dfc87 410 {
etiene32 0:43070b2dfc87 411 int i = 0, ch = 0;
etiene32 0:43070b2dfc87 412
etiene32 0:43070b2dfc87 413 if (buf[0] == '$')
etiene32 0:43070b2dfc87 414 ++i;
etiene32 0:43070b2dfc87 415
etiene32 0:43070b2dfc87 416 if (size)
etiene32 0:43070b2dfc87 417 for (; i < size; ++i)
etiene32 0:43070b2dfc87 418 ch ^= buf[i];
etiene32 0:43070b2dfc87 419 else
etiene32 0:43070b2dfc87 420 for (; buf[i] != 0; ++i)
etiene32 0:43070b2dfc87 421 ch ^= buf[i];
etiene32 0:43070b2dfc87 422
etiene32 0:43070b2dfc87 423 return ch;
etiene32 0:43070b2dfc87 424 }
etiene32 0:43070b2dfc87 425
etiene32 0:43070b2dfc87 426 bool Teseo_LIV3F::WaitBooting(Timer *t, float timeout)
etiene32 0:43070b2dfc87 427 {
etiene32 0:43070b2dfc87 428 unsigned int now = t->read_ms();;
etiene32 0:43070b2dfc87 429 while (1) {
etiene32 0:43070b2dfc87 430 if (CheckPPSWorking() == true)
etiene32 0:43070b2dfc87 431 return true;
etiene32 0:43070b2dfc87 432
etiene32 0:43070b2dfc87 433 if ((now + timeout*1000) < t->read_ms())
etiene32 0:43070b2dfc87 434 break;
etiene32 0:43070b2dfc87 435 }
etiene32 0:43070b2dfc87 436
etiene32 0:43070b2dfc87 437 return false;
etiene32 0:43070b2dfc87 438 }
etiene32 0:43070b2dfc87 439
etiene32 0:43070b2dfc87 440 void Teseo_LIV3F::SendCommand(enum Teseo_LIV3F::cmd_enum c)
etiene32 0:43070b2dfc87 441 {
etiene32 0:43070b2dfc87 442 char crc[5];
etiene32 0:43070b2dfc87 443
etiene32 0:43070b2dfc87 444 sprintf(crc, "*%02X\n\r", CRC_(teseo_cmds[c].cmd, 0));
etiene32 0:43070b2dfc87 445
etiene32 0:43070b2dfc87 446 _uart_mutex_lock();
etiene32 0:43070b2dfc87 447 _uart_interleaded = true;
etiene32 0:43070b2dfc87 448 SendString(teseo_cmds[c].cmd, strlen(teseo_cmds[c].cmd));
etiene32 0:43070b2dfc87 449 SendString(crc, 5);
etiene32 0:43070b2dfc87 450 _uart_mutex_unlock();
etiene32 0:43070b2dfc87 451 }
etiene32 0:43070b2dfc87 452
etiene32 0:43070b2dfc87 453 char *Teseo_LIV3F::DetectSentence(const char *cmd, char *buf, unsigned long len)
etiene32 0:43070b2dfc87 454 {
etiene32 0:43070b2dfc87 455 char *result = NULL;
etiene32 0:43070b2dfc87 456 unsigned int i = 0;
etiene32 0:43070b2dfc87 457 const unsigned long cmd_len = strlen(cmd);
etiene32 0:43070b2dfc87 458 len -= strlen(cmd);
etiene32 0:43070b2dfc87 459
etiene32 0:43070b2dfc87 460 while (!result && i < len) {
etiene32 0:43070b2dfc87 461 for (; buf[i] != '$' && i < len; ++i); /* 1. check '$' char */
etiene32 0:43070b2dfc87 462 if (i == len)
etiene32 0:43070b2dfc87 463 break; /* no more char.... */
etiene32 0:43070b2dfc87 464
etiene32 0:43070b2dfc87 465 ++i; /* to point to the char after '$' */
etiene32 0:43070b2dfc87 466
etiene32 0:43070b2dfc87 467 if (strncmp(&buf[i], cmd, cmd_len) == 0) {
etiene32 0:43070b2dfc87 468 result = &buf[i];
etiene32 0:43070b2dfc87 469 }
etiene32 0:43070b2dfc87 470 }
etiene32 0:43070b2dfc87 471
etiene32 0:43070b2dfc87 472 if (result) {
etiene32 0:43070b2dfc87 473 for (i = 0; result[i] != '*'; ++i);
etiene32 0:43070b2dfc87 474 result[i] = 0;
etiene32 0:43070b2dfc87 475 }
etiene32 0:43070b2dfc87 476 #if 0
etiene32 0:43070b2dfc87 477 if (_serial_debug)
etiene32 0:43070b2dfc87 478 _serial_debug->printf("%s: %s: %s %s FOUND\n\r", T3_name, __FUNCTION__, cmd, result ? " " : "NOT");
etiene32 0:43070b2dfc87 479 #endif
etiene32 0:43070b2dfc87 480 return result;
etiene32 0:43070b2dfc87 481 }
etiene32 0:43070b2dfc87 482
etiene32 0:43070b2dfc87 483
etiene32 0:43070b2dfc87 484 int Teseo_LIV3F::CheckI2C()
etiene32 0:43070b2dfc87 485 {
etiene32 0:43070b2dfc87 486
etiene32 0:43070b2dfc87 487 if (!_i2c)
etiene32 0:43070b2dfc87 488 return -1;
etiene32 0:43070b2dfc87 489
etiene32 0:43070b2dfc87 490 _i2c->start();
etiene32 0:43070b2dfc87 491 int res = _i2c->write((TESEO_I2C_ADDRESS << 1) | 1);
etiene32 0:43070b2dfc87 492 _i2c->stop();
etiene32 0:43070b2dfc87 493 /*
etiene32 0:43070b2dfc87 494 * @returns
etiene32 0:43070b2dfc87 495 * '0' - NAK was received
etiene32 0:43070b2dfc87 496 * '1' - ACK was received,
etiene32 0:43070b2dfc87 497 * '2' - timeout
etiene32 0:43070b2dfc87 498 */
etiene32 0:43070b2dfc87 499 return res == 1 ? 0 : -1;
etiene32 0:43070b2dfc87 500 }
etiene32 0:43070b2dfc87 501
etiene32 0:43070b2dfc87 502 int Teseo_LIV3F::ReadMessage(char *buf, unsigned long len, Timer *t, float timeout)
etiene32 0:43070b2dfc87 503 {
etiene32 0:43070b2dfc87 504 memset(buf, 0, len);
etiene32 0:43070b2dfc87 505
etiene32 0:43070b2dfc87 506 for (unsigned int i = 0; i < len; ++i){
etiene32 0:43070b2dfc87 507 if (t) {
etiene32 0:43070b2dfc87 508 unsigned int now = t->read_ms();;
etiene32 0:43070b2dfc87 509 while (!_uart.readable() && (now + timeout*1000) > t->read_ms());
etiene32 0:43070b2dfc87 510 } else
etiene32 0:43070b2dfc87 511 while (!_uart.readable());
etiene32 0:43070b2dfc87 512 if (_uart.readable())
etiene32 0:43070b2dfc87 513 buf[i] = _uart.getc();;
etiene32 0:43070b2dfc87 514 }
etiene32 0:43070b2dfc87 515 #if 0
etiene32 0:43070b2dfc87 516 if (_serial_debug) {
etiene32 0:43070b2dfc87 517 unsigned int i;
etiene32 0:43070b2dfc87 518 _serial_debug->printf("\n\r---------------------\n\r");
etiene32 0:43070b2dfc87 519 for (i = 0; i < len ; ++i)
etiene32 0:43070b2dfc87 520 _serial_debug->putc((int)buf[i]);
etiene32 0:43070b2dfc87 521 _serial_debug->printf("\n\r---------------------\n\r");
etiene32 0:43070b2dfc87 522 }
etiene32 0:43070b2dfc87 523 #endif
etiene32 0:43070b2dfc87 524 return 0;
etiene32 0:43070b2dfc87 525 }
etiene32 0:43070b2dfc87 526
etiene32 0:43070b2dfc87 527 void Teseo_LIV3F::RFTest(bool enable)
etiene32 0:43070b2dfc87 528 {
etiene32 0:43070b2dfc87 529 if (enable)
etiene32 0:43070b2dfc87 530 SendCommand(Teseo_LIV3F::RFTESTON);
etiene32 0:43070b2dfc87 531 else
etiene32 0:43070b2dfc87 532 SendCommand(Teseo_LIV3F::RFTESTOFF);
etiene32 0:43070b2dfc87 533 }
etiene32 0:43070b2dfc87 534
etiene32 0:43070b2dfc87 535 void Teseo_LIV3F::ReadLoop(Serial *serial_debug)
etiene32 0:43070b2dfc87 536 {
etiene32 0:43070b2dfc87 537 while (1)
etiene32 0:43070b2dfc87 538 if (_uart.readable()) {
etiene32 0:43070b2dfc87 539 int c = _uart.getc();
etiene32 0:43070b2dfc87 540 serial_debug->putc(c);
etiene32 0:43070b2dfc87 541 }
etiene32 0:43070b2dfc87 542 }
etiene32 0:43070b2dfc87 543
etiene32 0:43070b2dfc87 544 char *Teseo_LIV3F::ReadSentence(const char *cmd, char *buf, unsigned long len)
etiene32 0:43070b2dfc87 545 {
etiene32 0:43070b2dfc87 546 int ret = ReadMessage(buf, len);
etiene32 0:43070b2dfc87 547 if (ret)
etiene32 0:43070b2dfc87 548 return NULL;
etiene32 0:43070b2dfc87 549 return DetectSentence(cmd, buf, len);
etiene32 0:43070b2dfc87 550 }
etiene32 0:43070b2dfc87 551
etiene32 0:43070b2dfc87 552 struct ___msg {
etiene32 0:43070b2dfc87 553 unsigned char len;
etiene32 0:43070b2dfc87 554 char *str;
etiene32 0:43070b2dfc87 555 };
etiene32 0:43070b2dfc87 556
etiene32 0:43070b2dfc87 557
etiene32 0:43070b2dfc87 558 static const struct ___msg teseo_msgs[] = {
etiene32 0:43070b2dfc87 559 [ NMEA_GPGGA ] = { .len = 5, .str = "GPGGA", },
etiene32 0:43070b2dfc87 560 [ NMEA_GPGLL ] = { .len = 5, .str = "GPGLL", },
etiene32 0:43070b2dfc87 561 [ NMEA_GNGSA ] = { .len = 5, .str = "GNGSA", },
etiene32 0:43070b2dfc87 562 [ NMEA_GPTXT ] = { .len = 5, .str = "GPTXT", },
etiene32 0:43070b2dfc87 563 [ NMEA_GPVTG ] = { .len = 5, .str = "GPVTG", },
etiene32 0:43070b2dfc87 564 [ NMEA_GPRMC ] = { .len = 5, .str = "GPRMC", },
etiene32 0:43070b2dfc87 565 [ NMEA_PSTMCPU ] = { .len = 7, .str = "PSTMCPU", },
etiene32 0:43070b2dfc87 566 [ NMEA_PSTMVER ] = { .len = 7, .str = "PSTMVER", },
etiene32 0:43070b2dfc87 567 };
etiene32 0:43070b2dfc87 568
etiene32 0:43070b2dfc87 569
etiene32 0:43070b2dfc87 570 enum nmea_msg_id Teseo_LIV3F::MsgDetect(char *buf, int buf_len, Serial *serial_debug)
etiene32 0:43070b2dfc87 571 {
etiene32 0:43070b2dfc87 572 int i;
etiene32 0:43070b2dfc87 573
etiene32 0:43070b2dfc87 574 if (buf[0] == '$')
etiene32 0:43070b2dfc87 575 ++buf;
etiene32 0:43070b2dfc87 576
etiene32 0:43070b2dfc87 577 for (i = 0; i < NMEA_END__; ++i)
etiene32 0:43070b2dfc87 578 if (memcmp((void*)teseo_msgs[i].str, (void*)buf, teseo_msgs[i].len) == 0)
etiene32 0:43070b2dfc87 579 return (enum nmea_msg_id) i;
etiene32 0:43070b2dfc87 580 #if 0
etiene32 0:43070b2dfc87 581 if (serial_debug) {
etiene32 0:43070b2dfc87 582 serial_debug->puts("MESSAGE NOT FOUND: ");
etiene32 0:43070b2dfc87 583 for (int i = 0; i < 5; ++i)
etiene32 0:43070b2dfc87 584 serial_debug->putc(lbuf[i]);
etiene32 0:43070b2dfc87 585 serial_debug->puts("\n\r");
etiene32 0:43070b2dfc87 586 }
etiene32 0:43070b2dfc87 587 #endif
etiene32 0:43070b2dfc87 588 return NMEA_END__;
etiene32 0:43070b2dfc87 589 }
etiene32 0:43070b2dfc87 590
etiene32 0:43070b2dfc87 591 void Teseo_LIV3F::UARTStreamProcess(Serial *serial_debug)
etiene32 0:43070b2dfc87 592 {
etiene32 0:43070b2dfc87 593 enum nmea_msg_id id;
etiene32 0:43070b2dfc87 594 char c;
etiene32 0:43070b2dfc87 595
etiene32 0:43070b2dfc87 596 struct teseo_msg *msg = mpool.alloc();
etiene32 0:43070b2dfc87 597 msg->len = 0;
etiene32 0:43070b2dfc87 598
etiene32 0:43070b2dfc87 599 while (true) {
etiene32 0:43070b2dfc87 600 _uart_mutex_lock();
etiene32 0:43070b2dfc87 601 #if 0
etiene32 0:43070b2dfc87 602 if (_uart_interleaded == true) {
etiene32 0:43070b2dfc87 603 msg->len = 0;
etiene32 0:43070b2dfc87 604 _uart_interleaded = false;
etiene32 0:43070b2dfc87 605 _uart_discard = true;
etiene32 0:43070b2dfc87 606 }
etiene32 0:43070b2dfc87 607 #endif
etiene32 0:43070b2dfc87 608 if (_uart.readable()) {
etiene32 0:43070b2dfc87 609 c = _uart.getc();
etiene32 0:43070b2dfc87 610 _uart_mutex_unlock();
etiene32 0:43070b2dfc87 611 if (c == '$') {
etiene32 0:43070b2dfc87 612 queue.put(msg);
etiene32 0:43070b2dfc87 613 msg = mpool.alloc();
etiene32 0:43070b2dfc87 614 msg->len = 0;
etiene32 0:43070b2dfc87 615 _uart_discard = false;
etiene32 0:43070b2dfc87 616 }
etiene32 0:43070b2dfc87 617 if (!_uart_discard)
etiene32 0:43070b2dfc87 618 msg->buf[msg->len++] = c;
etiene32 0:43070b2dfc87 619 } else {
etiene32 0:43070b2dfc87 620 _uart_mutex_unlock();
etiene32 0:43070b2dfc87 621 wait_us(100);
etiene32 0:43070b2dfc87 622 }
etiene32 0:43070b2dfc87 623 }
etiene32 0:43070b2dfc87 624 }
etiene32 0:43070b2dfc87 625
etiene32 0:43070b2dfc87 626 struct thr_data {
etiene32 0:43070b2dfc87 627 Teseo_LIV3F *gnss;
etiene32 0:43070b2dfc87 628 Serial *serial_debug;
etiene32 0:43070b2dfc87 629 };
etiene32 0:43070b2dfc87 630
etiene32 0:43070b2dfc87 631 static void Teseo_LIV3F_UARTStreamProcess(struct thr_data *data)
etiene32 0:43070b2dfc87 632 {
etiene32 0:43070b2dfc87 633 data->gnss->UARTStreamProcess(data->serial_debug);
etiene32 0:43070b2dfc87 634 }
etiene32 0:43070b2dfc87 635
etiene32 0:43070b2dfc87 636 void Teseo_LIV3F::startListener(Serial *serial_debug)
etiene32 0:43070b2dfc87 637 {
etiene32 0:43070b2dfc87 638 if (serialStreamThread.get_state() == Thread::Running)
etiene32 0:43070b2dfc87 639 return;
etiene32 0:43070b2dfc87 640
etiene32 0:43070b2dfc87 641 static struct thr_data data = {
etiene32 0:43070b2dfc87 642 .gnss = this,
etiene32 0:43070b2dfc87 643 .serial_debug = serial_debug,
etiene32 0:43070b2dfc87 644 };
etiene32 0:43070b2dfc87 645
etiene32 0:43070b2dfc87 646 serialStreamThread.start(Teseo_LIV3F_UARTStreamProcess, &data);
etiene32 0:43070b2dfc87 647 }
etiene32 0:43070b2dfc87 648
etiene32 0:43070b2dfc87 649 void Teseo_LIV3F::stopListener(Serial *serial_debug)
etiene32 0:43070b2dfc87 650 {
etiene32 0:43070b2dfc87 651 if (serialStreamThread.get_state() != Thread::Running)
etiene32 0:43070b2dfc87 652 return;
etiene32 0:43070b2dfc87 653 serialStreamThread.terminate();
etiene32 0:43070b2dfc87 654 }