Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: X_NUCLEO_IKS01A2 mbed-rtos mbed
Teseo-LIV3F.cpp
00001 /* 00002 * ------------------------------------------------------------------------- 00003 * Copyright (C) 2017 STMicroelectronics 00004 * Author: Francesco M. Virlinzi <francesco.virlinzi@st.com> 00005 * 00006 * May be copied or modified under the terms of the GNU General Public 00007 * License V.2 ONLY. See linux/COPYING for more information. 00008 * 00009 * ------------------------------------------------------------------------- 00010 */ 00011 #include "string.h" 00012 #include "Teseo-LIV3F.h" 00013 00014 00015 static char T3_name[] = "Teseo-LIV3F"; 00016 00017 char _OK[] = "OK"; 00018 char _Failed[] = "Failed"; 00019 char X_Nucleo_name[] = "X-Nucleo-GNSS1A1"; 00020 00021 struct teseo_cmd { 00022 char *cmd; 00023 }; 00024 00025 static struct teseo_cmd teseo_cmds[] = { 00026 [Teseo_LIV3F::GETSWVER] = { 00027 .cmd = "$PSTMGETSWVER,7", 00028 }, 00029 [Teseo_LIV3F::FORCESTANDBY] = { 00030 .cmd = "$PSTMFORCESTANDBY,00007", 00031 }, 00032 [Teseo_LIV3F::RFTESTON] = { 00033 .cmd = "$PSTMRFTESTON,16", 00034 }, 00035 [Teseo_LIV3F::RFTESTOFF] = { 00036 .cmd = "$PSTMRFTESTOFF\n\r", 00037 }, 00038 [Teseo_LIV3F::LOWPOWER] = { 00039 .cmd = "$PSTMLOWPOWERONOFF,1,0,000,05,0,1,000,1,00010,01,0,0,1,01", 00040 }, 00041 [Teseo_LIV3F::FWUPDATE] = { 00042 .cmd = "$PSTMFWUPGRADE", 00043 }, 00044 }; 00045 00046 00047 Teseo_LIV3F::Teseo_LIV3F(PinName reset_pin, PinName wakeup_pin, 00048 PinName pps_pin, PinName uart_tx_pin, PinName uart_rx_pin, 00049 Serial *serial_debug): 00050 _reset(reset_pin, 1), 00051 _pps(pps_pin), 00052 _wakeup(wakeup_pin, 0), 00053 _uart(uart_rx_pin, uart_tx_pin, SDT_UART_BAUD), 00054 _serial_debug(serial_debug) 00055 { 00056 wait_ms(POWERON_STABLE_SIGNAL_DELAY_MS); 00057 _uart.baud(SDT_UART_BAUD); 00058 _uart.format(8, SerialBase::None, 1); 00059 _i2c = NULL; 00060 _uart_interleaded = false; 00061 _uart_discard = false; 00062 } 00063 00064 Teseo_LIV3F::Teseo_LIV3F(PinName reset_pin, PinName wakeup_pin, 00065 PinName pps_pin, PinName uart_tx_pin, PinName uart_rx_pin, 00066 I2C *bus, Serial *serial_debug): 00067 _reset(reset_pin, 1), 00068 _pps(pps_pin), 00069 _wakeup(wakeup_pin, 0), 00070 _uart(uart_rx_pin, uart_tx_pin, SDT_UART_BAUD), 00071 _i2c(bus), 00072 _serial_debug(serial_debug) 00073 { 00074 wait_ms(POWERON_STABLE_SIGNAL_DELAY_MS); 00075 _uart.baud(SDT_UART_BAUD); 00076 _uart.format(8, SerialBase::None, 1); 00077 _uart_interleaded = false; 00078 _uart_discard = false; 00079 } 00080 00081 int Teseo_LIV3F::EnableLowPower() 00082 { 00083 SendCommand(LOWPOWER); 00084 return 0; 00085 } 00086 00087 void Teseo_LIV3F::Reset(Serial *serial_debug) 00088 { 00089 if (serial_debug) 00090 serial_debug->printf("%s: Resetting...", T3_name); 00091 00092 _reset.write(0); 00093 00094 wait_ms(50); 00095 00096 _reset.write(1); 00097 00098 wait_ms(70); 00099 00100 if (serial_debug) 00101 serial_debug->printf("Done...\n\r"); 00102 } 00103 00104 enum { 00105 TESEO_FLASHER_IDENTIFIER = 0, // 0xBCD501F4 00106 TESEO_FLASHER_SYNC, // 0x83984073 00107 DEVICE_START_COMMUNICATION, // 0xA3 00108 FLASHER_READY, // 0x4A 00109 ACK, // 0xCC 00110 NACK, 00111 }; 00112 00113 struct firmware_ctrl { 00114 char *cmd; 00115 unsigned char len; 00116 char *n; 00117 } ; 00118 00119 /* 00120 * #define FWUPG_IDENTIFIER 0xBC D5 01 F4 00121 * #define FWUPG_SYNC 0x83 98 40 73 00122 */ 00123 static struct firmware_ctrl fw_data[] = { 00124 [TESEO_FLASHER_IDENTIFIER] = { 00125 .cmd = (char *)(char[]){ 0xF4, 0x01, 0xD5, 0xBC}, 00126 .len = 4, 00127 .n = "TESEO_FLASHER_IDENTIFIER", 00128 }, 00129 [TESEO_FLASHER_SYNC] = { 00130 .cmd =(char *)(char[]){ 0x73, 0x40, 0x98, 0x83 }, 00131 .len = 4, 00132 .n = "TESEO_FLASHER_SYNC", 00133 }, 00134 [DEVICE_START_COMMUNICATION] = { 00135 .cmd = (char *)(char[]){0xA3}, 00136 .len = 1, 00137 .n = "DEVICE_START_COMMUNICATION", 00138 }, 00139 [FLASHER_READY] = { 00140 .cmd = (char *)(char[]){0x4A}, 00141 .len = 1, 00142 .n = "FLASHER_READY", 00143 }, 00144 [ACK] = { 00145 .cmd = (char *)(char[]){0xCC}, 00146 .len = 1, 00147 .n = "ACK", 00148 }, 00149 [NACK] = { 00150 .cmd = (char *)(char[]){0xDD}, 00151 .len = 1, 00152 .n = "NACK", 00153 }, 00154 }; 00155 00156 int Teseo_LIV3F::SendString(char *buf, int len) 00157 { 00158 for (int i = 0; i < len; ++i) { 00159 while (!_uart.writeable()); 00160 _uart.putc(buf[i]); 00161 } 00162 } 00163 00164 struct ImageOptions 00165 { 00166 unsigned char eraseNVM; 00167 unsigned char programOnly; 00168 unsigned char reserved; 00169 unsigned char baudRate; 00170 unsigned int firmwareSize; 00171 unsigned int firmwareCRC; 00172 unsigned int nvmAddressOffset; 00173 unsigned int nvmSize; 00174 } liv3f_img_option = { 00175 .eraseNVM = 1, 00176 .programOnly = 0, 00177 .reserved = 0, 00178 .baudRate = 1, 00179 .firmwareSize = 0, 00180 .firmwareCRC = 0, 00181 .nvmAddressOffset = 0x00100000, 00182 .nvmSize = 0x00100000, 00183 00184 }; 00185 00186 int Teseo_LIV3F::FwWaitAck() 00187 { 00188 while (!_uart.readable()); 00189 00190 char c = _uart.getc(); 00191 00192 if (fw_data[ACK].cmd[0] == c) { 00193 if (_serial_debug) 00194 _serial_debug->printf("%s (0x%x)\n\r", _OK, c); 00195 return 0; 00196 } 00197 00198 if (fw_data[NACK].cmd[0] == c) { 00199 if (_serial_debug) 00200 _serial_debug->printf("%s (%x)\n\r", _Failed, c); 00201 return -1; 00202 } 00203 00204 if (_serial_debug) 00205 _serial_debug->printf("%s - Char not allowed (%x)\n\r", _Failed, c); 00206 return -1; 00207 } 00208 00209 bool Teseo_LIV3F::FirmwareUpdate(bool is_recovery, char *data, 00210 unsigned int data_len, 00211 unsigned long crc, 00212 Serial *serial_debug) 00213 { 00214 unsigned int i; 00215 00216 char _buf[4] = { 0xff, 0xff, 0xff, 0xff }; 00217 00218 liv3f_img_option.firmwareSize = data_len; 00219 liv3f_img_option.firmwareCRC = crc; 00220 00221 if (data == NULL || !data_len) 00222 return false; 00223 00224 if (is_recovery) 00225 Reset(); 00226 00227 { 00228 00229 _uart.baud(FWU_UART_BAUD); 00230 00231 #if 1 00232 while (1) { 00233 /* send TESEO_FLASHER_IDENTIFIER */ 00234 /* 00235 * Device is under reset. Host sends continuously “TESEO2_FLASHER_IDENTIFIER� word. 00236 */ 00237 SendString(fw_data[TESEO_FLASHER_IDENTIFIER].cmd, fw_data[TESEO_FLASHER_IDENTIFIER].len); 00238 00239 /* try to read... TESEO_FLASHER_SYNC */ 00240 if (_uart.readable()) 00241 for (i = 0; i < fw_data[TESEO_FLASHER_SYNC].len; ) { 00242 00243 while (!_uart.readable()); 00244 00245 _buf[i] = _uart.getc(); 00246 00247 if (fw_data[TESEO_FLASHER_SYNC].cmd[i] == _buf[i]) { 00248 if (serial_debug) 00249 serial_debug->printf("-- %d -- 0x%x -- ok--\n\r", i, _buf[i]); 00250 i++; 00251 goto exit_step_1; /* FMV: WA to have firmware update working.... */ 00252 } else { 00253 i = 0; 00254 } 00255 } 00256 if (i == fw_data[TESEO_FLASHER_SYNC].len) 00257 goto exit_step_1; 00258 } 00259 00260 exit_step_1: 00261 00262 _uart.abort_read(); 00263 00264 if (serial_debug) 00265 serial_debug->printf("Got: %s from %s\n\r", fw_data[TESEO_FLASHER_SYNC].n, T3_name); 00266 00267 /* 00268 * Host sends “DEVICE_START_COMMUNICATION� word. 00269 */ 00270 serial_debug->printf("\n\r%s Step: %s ",T3_name, fw_data[DEVICE_START_COMMUNICATION].n); 00271 SendString(fw_data[DEVICE_START_COMMUNICATION].cmd, fw_data[DEVICE_START_COMMUNICATION].len); 00272 00273 FwWaitAck(); 00274 00275 /* 00276 * Host sends the binary image options. Both host and 00277 * device change UART baud rates. Host sends continuously 00278 * the “FLASHER_READY� word. 00279 */ 00280 if (serial_debug) 00281 serial_debug->printf("%s Step: Send ImageOption\n\r",T3_name); 00282 00283 SendString((char*)&liv3f_img_option, sizeof(ImageOptions)); 00284 00285 if (serial_debug) 00286 serial_debug->printf("%s Step: Send %s\n\r",T3_name, fw_data[FLASHER_READY].n); 00287 00288 while (1) { 00289 SendString(fw_data[FLASHER_READY].cmd, fw_data[FLASHER_READY].len); 00290 if (_uart.readable()) 00291 goto exit_step_3; 00292 } 00293 00294 exit_step_3: 00295 FwWaitAck(); 00296 00297 if (serial_debug) 00298 serial_debug->printf("%s Step: Erasing flash area ",T3_name); 00299 00300 00301 FwWaitAck(); 00302 00303 /* 00304 * Device is erasing flash program. Host is waiting for an “ACK�. 00305 */ 00306 00307 if (serial_debug) 00308 serial_debug->printf("%s Step: Erasing NVM ",T3_name); 00309 00310 while (!_uart.readable()); 00311 00312 FwWaitAck(); 00313 00314 if (serial_debug) 00315 serial_debug->printf("%s Step: Sending data... ",T3_name); 00316 00317 for (i = 0; i < (data_len / (16*1024)); ++i) { 00318 SendString(&data[i], 16*1024); 00319 00320 FwWaitAck(); 00321 } 00322 00323 serial_debug->printf("\n\r"); 00324 /* 00325 * send remaining data... 00326 */ 00327 if (data_len != (i * 16*1024)) { 00328 SendString(&data[i*16*1024], data_len-i*16*1024); 00329 FwWaitAck(); 00330 } 00331 /* 00332 * wait CRC ack 00333 */ 00334 FwWaitAck(); 00335 } 00336 00337 #else 00338 //_uart.format(8, SerialBase::Forced0, 1); 00339 while (1) 00340 { 00341 /* send TESEO_FLASHER_IDENTIFIER */ 00342 for (i = 0; i < fw_data[TESEO_FLASHER_IDENTIFIER].len; ++i) { 00343 while (!_uart.writeable()); 00344 _uart.putc(fw_data[TESEO_FLASHER_IDENTIFIER].cmd[i]); 00345 } 00346 00347 00348 /* try to read... TESEO_FLASHER_SYNC */ 00349 //while (!_uart.readable()); 00350 for (i = 0; i < fw_data[TESEO_FLASHER_SYNC].len && _uart.readable(); ) 00351 if (_uart.readable()) 00352 { 00353 char c = _uart.getc(); 00354 /* 00355 if (!c) 00356 break; 00357 */ 00358 if (serial_debug) 00359 serial_debug->printf("%x vs %x\n\r", c, fw_data[TESEO_FLASHER_SYNC].cmd[i]); 00360 00361 if (fw_data[TESEO_FLASHER_SYNC].cmd[i] == c) 00362 i++; 00363 else 00364 i = 0; 00365 } 00366 00367 if (i == fw_data[TESEO_FLASHER_SYNC].len && serial_debug) 00368 serial_debug->printf("Got %s from %s\n\r",fw_data[TESEO_FLASHER_SYNC].n, T3_name); 00369 } 00370 } 00371 #endif 00372 if (serial_debug) 00373 serial_debug->printf("END\n\r"); 00374 00375 return true; 00376 } 00377 00378 int Teseo_LIV3F::WakeUp() 00379 { 00380 wait_ms(100); 00381 00382 _wakeup.write(1); 00383 00384 wait_ms(500); 00385 00386 _wakeup.write(0); 00387 00388 return 0; 00389 } 00390 00391 bool Teseo_LIV3F::CheckPPSWorking() 00392 { 00393 int val_0, val_1; 00394 00395 wait_ms(500); 00396 00397 val_0 = _pps.read(); 00398 00399 wait_ms(500); 00400 val_1 = _pps.read(); 00401 00402 if (val_0 != val_1) 00403 return true; 00404 00405 return false; 00406 } 00407 00408 int Teseo_LIV3F::CRC_(char *buf, int size) 00409 { 00410 int i = 0, ch = 0; 00411 00412 if (buf[0] == '$') 00413 ++i; 00414 00415 if (size) 00416 for (; i < size; ++i) 00417 ch ^= buf[i]; 00418 else 00419 for (; buf[i] != 0; ++i) 00420 ch ^= buf[i]; 00421 00422 return ch; 00423 } 00424 00425 bool Teseo_LIV3F::WaitBooting(Timer *t, float timeout) 00426 { 00427 unsigned int now = t->read_ms();; 00428 while (1) { 00429 if (CheckPPSWorking() == true) 00430 return true; 00431 00432 if ((now + timeout*1000) < t->read_ms()) 00433 break; 00434 } 00435 00436 return false; 00437 } 00438 00439 void Teseo_LIV3F::SendCommand(enum Teseo_LIV3F::cmd_enum c) 00440 { 00441 char crc[5]; 00442 00443 sprintf(crc, "*%02X\n\r", CRC_(teseo_cmds[c].cmd, 0)); 00444 00445 _uart_mutex_lock(); 00446 _uart_interleaded = true; 00447 SendString(teseo_cmds[c].cmd, strlen(teseo_cmds[c].cmd)); 00448 SendString(crc, 5); 00449 _uart_mutex_unlock(); 00450 } 00451 00452 char *Teseo_LIV3F::DetectSentence(const char *cmd, char *buf, unsigned long len) 00453 { 00454 char *result = NULL; 00455 unsigned int i = 0; 00456 const unsigned long cmd_len = strlen(cmd); 00457 len -= strlen(cmd); 00458 00459 while (!result && i < len) { 00460 for (; buf[i] != '$' && i < len; ++i); /* 1. check '$' char */ 00461 if (i == len) 00462 break; /* no more char.... */ 00463 00464 ++i; /* to point to the char after '$' */ 00465 00466 if (strncmp(&buf[i], cmd, cmd_len) == 0) { 00467 result = &buf[i]; 00468 } 00469 } 00470 00471 if (result) { 00472 for (i = 0; result[i] != '*'; ++i); 00473 result[i] = 0; 00474 } 00475 #if 0 00476 if (_serial_debug) 00477 _serial_debug->printf("%s: %s: %s %s FOUND\n\r", T3_name, __FUNCTION__, cmd, result ? " " : "NOT"); 00478 #endif 00479 return result; 00480 } 00481 00482 00483 int Teseo_LIV3F::CheckI2C() 00484 { 00485 00486 if (!_i2c) 00487 return -1; 00488 00489 _i2c->start(); 00490 int res = _i2c->write((TESEO_I2C_ADDRESS << 1) | 1); 00491 _i2c->stop(); 00492 /* 00493 * @returns 00494 * '0' - NAK was received 00495 * '1' - ACK was received, 00496 * '2' - timeout 00497 */ 00498 return res == 1 ? 0 : -1; 00499 } 00500 00501 int Teseo_LIV3F::ReadMessage(char *buf, unsigned long len, Timer *t, float timeout) 00502 { 00503 memset(buf, 0, len); 00504 00505 for (unsigned int i = 0; i < len; ++i){ 00506 if (t) { 00507 unsigned int now = t->read_ms();; 00508 while (!_uart.readable() && (now + timeout*1000) > t->read_ms()); 00509 } else 00510 while (!_uart.readable()); 00511 if (_uart.readable()) 00512 buf[i] = _uart.getc();; 00513 } 00514 #if 0 00515 if (_serial_debug) { 00516 unsigned int i; 00517 _serial_debug->printf("\n\r---------------------\n\r"); 00518 for (i = 0; i < len ; ++i) 00519 _serial_debug->putc((int)buf[i]); 00520 _serial_debug->printf("\n\r---------------------\n\r"); 00521 } 00522 #endif 00523 return 0; 00524 } 00525 00526 void Teseo_LIV3F::RFTest(bool enable) 00527 { 00528 if (enable) 00529 SendCommand(Teseo_LIV3F::RFTESTON); 00530 else 00531 SendCommand(Teseo_LIV3F::RFTESTOFF); 00532 } 00533 00534 void Teseo_LIV3F::ReadLoop(Serial *serial_debug) 00535 { 00536 while (1) 00537 if (_uart.readable()) { 00538 int c = _uart.getc(); 00539 serial_debug->putc(c); 00540 } 00541 } 00542 00543 char *Teseo_LIV3F::ReadSentence(const char *cmd, char *buf, unsigned long len) 00544 { 00545 int ret = ReadMessage(buf, len); 00546 if (ret) 00547 return NULL; 00548 return DetectSentence(cmd, buf, len); 00549 } 00550 00551 struct ___msg { 00552 unsigned char len; 00553 char *str; 00554 }; 00555 00556 00557 static const struct ___msg teseo_msgs[] = { 00558 [ NMEA_GPGGA ] = { .len = 5, .str = "GPGGA", }, 00559 [ NMEA_GPGLL ] = { .len = 5, .str = "GPGLL", }, 00560 [ NMEA_GNGSA ] = { .len = 5, .str = "GNGSA", }, 00561 [ NMEA_GPTXT ] = { .len = 5, .str = "GPTXT", }, 00562 [ NMEA_GPVTG ] = { .len = 5, .str = "GPVTG", }, 00563 [ NMEA_GPRMC ] = { .len = 5, .str = "GPRMC", }, 00564 [ NMEA_PSTMCPU ] = { .len = 7, .str = "PSTMCPU", }, 00565 [ NMEA_PSTMVER ] = { .len = 7, .str = "PSTMVER", }, 00566 }; 00567 00568 00569 enum nmea_msg_id Teseo_LIV3F::MsgDetect(char *buf, int buf_len, Serial *serial_debug) 00570 { 00571 int i; 00572 00573 if (buf[0] == '$') 00574 ++buf; 00575 00576 for (i = 0; i < NMEA_END__; ++i) 00577 if (memcmp((void*)teseo_msgs[i].str, (void*)buf, teseo_msgs[i].len) == 0) 00578 return (enum nmea_msg_id) i; 00579 #if 0 00580 if (serial_debug) { 00581 serial_debug->puts("MESSAGE NOT FOUND: "); 00582 for (int i = 0; i < 5; ++i) 00583 serial_debug->putc(lbuf[i]); 00584 serial_debug->puts("\n\r"); 00585 } 00586 #endif 00587 return NMEA_END__; 00588 } 00589 00590 void Teseo_LIV3F::UARTStreamProcess(Serial *serial_debug) 00591 { 00592 enum nmea_msg_id id; 00593 char c; 00594 00595 struct teseo_msg *msg = mpool.alloc(); 00596 msg->len = 0; 00597 00598 while (true) { 00599 _uart_mutex_lock(); 00600 #if 0 00601 if (_uart_interleaded == true) { 00602 msg->len = 0; 00603 _uart_interleaded = false; 00604 _uart_discard = true; 00605 } 00606 #endif 00607 if (_uart.readable()) { 00608 c = _uart.getc(); 00609 _uart_mutex_unlock(); 00610 if (c == '$') { 00611 queue.put(msg); 00612 msg = mpool.alloc(); 00613 msg->len = 0; 00614 _uart_discard = false; 00615 } 00616 if (!_uart_discard) 00617 msg->buf[msg->len++] = c; 00618 } else { 00619 _uart_mutex_unlock(); 00620 wait_us(100); 00621 } 00622 } 00623 } 00624 00625 struct thr_data { 00626 Teseo_LIV3F *gnss; 00627 Serial *serial_debug; 00628 }; 00629 00630 static void Teseo_LIV3F_UARTStreamProcess(struct thr_data *data) 00631 { 00632 data->gnss->UARTStreamProcess(data->serial_debug); 00633 } 00634 00635 void Teseo_LIV3F::startListener(Serial *serial_debug) 00636 { 00637 if (serialStreamThread.get_state() == Thread::Running) 00638 return; 00639 00640 static struct thr_data data = { 00641 .gnss = this, 00642 .serial_debug = serial_debug, 00643 }; 00644 00645 serialStreamThread.start(Teseo_LIV3F_UARTStreamProcess, &data); 00646 } 00647 00648 void Teseo_LIV3F::stopListener(Serial *serial_debug) 00649 { 00650 if (serialStreamThread.get_state() != Thread::Running) 00651 return; 00652 serialStreamThread.terminate(); 00653 } 00654
Generated on Fri Jul 15 2022 01:05:54 by
1.7.2