1
0

main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*---------------------------------------------------------------*
  2. * Automotive GPS logger (C)ChaN, 2014 *
  3. * Modified k4be, 2022 *
  4. *---------------------------------------------------------------*/
  5. #include <avr/io.h>
  6. #include <avr/interrupt.h>
  7. #include <avr/sleep.h>
  8. #include <util/delay.h>
  9. #include <avr/pgmspace.h>
  10. #include <avr/wdt.h>
  11. #include <string.h>
  12. #include "main.h"
  13. #include "ff.h"
  14. #include "diskio.h"
  15. #include "uart0.h"
  16. #include "uart1.h"
  17. #include "xprintf.h"
  18. #include "stime.h"
  19. #include "ds18b20.h"
  20. #include "I2C.h"
  21. #include "expander.h"
  22. #include "HD44780-I2C.h"
  23. #include "gpx.h"
  24. /*FUSES = {0xFF, 0x11, 0xFE};*/ /* ATmega644PA fuses: Low, High, Extended.
  25. This is the fuse settings for this project. The fuse bits will be included
  26. in the output hex file with program code. However some old flash programmers
  27. cannot load the fuse bits from hex file. If it is the case, remove this line
  28. and use these values to program the fuse bits. */
  29. volatile struct system_s System;
  30. FATFS Fatfs; /* File system object for each logical drive */
  31. FIL gps_log; /* File object */
  32. FIL system_log; /* System log file */
  33. char Line[100]; /* Line buffer */
  34. time_t utc; /* current time */
  35. struct location_s location;
  36. void start_bootloader(void) {
  37. typedef void (*do_reboot_t)(void);
  38. const do_reboot_t do_reboot = (do_reboot_t)(0x7ffa >> 1);
  39. cli();
  40. LCD_Clear();
  41. LCD_GoTo(0,0);
  42. LCD_WriteTextP(PSTR("Aktualizacja"));
  43. LCD_GoTo(8,1);
  44. LCD_WriteTextP(PSTR("softu..."));
  45. TCCR0A = TCCR1A = TCCR2A = 0; // make sure interrupts are off and timers are reset.
  46. do_reboot();
  47. }
  48. /*---------------------------------------------------------*/
  49. /* 100Hz timer interrupt generated by OC1A */
  50. /*---------------------------------------------------------*/
  51. ISR(TIMER1_COMPA_vect)
  52. {
  53. static WORD ivt_sync;
  54. // static BYTE led;
  55. static unsigned int power_sw;
  56. unsigned int *volatile ctimer;
  57. unsigned char i;
  58. for(i=0; i<sizeof(System.timers)/sizeof(unsigned int); i++){ // decrement every variable from timers struct unless it's already zero
  59. ctimer = ((unsigned int *)&System.timers) + i;
  60. if(*ctimer)
  61. (*ctimer)--;
  62. }
  63. /* Sync interval */
  64. if (IVT_SYNC && ++ivt_sync >= IVT_SYNC * 100) {
  65. ivt_sync = 0;
  66. FLAGS |= F_SYNC;
  67. }
  68. /* Green LED drive */
  69. /* if (FLAGS & F_POW) {
  70. if ((FLAGS & F_GPSOK) || (++led & 0x20)) {
  71. LEDG_ON();
  72. } else {
  73. LEDG_OFF();
  74. }
  75. } else {
  76. LEDG_OFF();
  77. }*/
  78. /* MMC/SD timer procedure */
  79. disk_timerproc();
  80. /* Switch off */
  81. if (POWER_SW_PRESSED()) {
  82. if (++power_sw == POWER_SW_TIME){
  83. FLAGS |= F_POWEROFF;
  84. power_sw++;
  85. }
  86. } else {
  87. power_sw = 0;
  88. }
  89. if (uart1_test() && uart1_get() == '0' && uart1_get() == ' '){
  90. //if (!(PIND & _BV(PD5))) {
  91. LEDB_ON();
  92. start_bootloader();
  93. }
  94. }
  95. /* Power supply monitor */
  96. ISR(ADC_vect)
  97. {
  98. float bat_volt;
  99. static unsigned int adc_buf;
  100. static unsigned char adc_cnt;
  101. static BYTE lvt;
  102. adc_buf += ADC;
  103. if(++adc_cnt < 15)
  104. return;
  105. bat_volt = (float)adc_buf/(float)(adc_cnt)/VI_MULT;
  106. adc_buf = 0;
  107. adc_cnt = 0;
  108. System.bat_volt = bat_volt;
  109. if (FLAGS & F_LVD) {
  110. if (bat_volt > VI_LVH) {
  111. FLAGS &= ~F_LVD;
  112. lvt = 0;
  113. }
  114. } else {
  115. if (bat_volt < VI_LVL) {
  116. if (++lvt >= 3)
  117. FLAGS |= F_LVD;
  118. } else {
  119. lvt = 0;
  120. }
  121. }
  122. }
  123. /*---------------------------------------------------------*/
  124. /* User Provided Timer Function for FatFs module */
  125. /*---------------------------------------------------------*/
  126. DWORD get_fattime (void)
  127. {
  128. struct tm *stm;
  129. time_t localtime = utc + LOCALDIFF;
  130. stm = gmtime(&localtime);
  131. return (DWORD)(stm->tm_year - 80) << 25 |
  132. (DWORD)(stm->tm_mon + 1) << 21 |
  133. (DWORD)stm->tm_mday << 16 |
  134. (DWORD)stm->tm_hour << 11 |
  135. (DWORD)stm->tm_min << 5 |
  136. (DWORD)stm->tm_sec >> 1;
  137. }
  138. static void sleep(void) {
  139. set_sleep_mode(SLEEP_MODE_IDLE);
  140. sleep_enable();
  141. sleep_cpu();
  142. sleep_disable();
  143. }
  144. /*----------------------------------------------------*/
  145. /* Get a line received from GPS module */
  146. /*----------------------------------------------------*/
  147. UINT get_line ( /* 0:Brownout or timeout, >0: Number of bytes received. */
  148. char *buff,
  149. UINT sz_buf
  150. )
  151. {
  152. char c;
  153. UINT i = 0;
  154. set_timer(recv_timeout, 1000);
  155. for (;;) {
  156. if (FLAGS & (F_LVD | F_POWEROFF))
  157. return 0; /* A brownout is detected */
  158. if (timer_expired(recv_timeout))
  159. return 0; /* timeout; continue the main loop */
  160. if (!uart0_test()) {
  161. sleep();
  162. continue;
  163. }
  164. c = (char)uart0_get();
  165. uart1_put(c);
  166. if (i == 0 && c != '$')
  167. continue; /* Find start of line */
  168. buff[i++] = c;
  169. if (c == '\n')
  170. break; /* EOL */
  171. if (i >= sz_buf)
  172. i = 0; /* Buffer overflow (abort this line) */
  173. }
  174. return i;
  175. }
  176. /*--------------------------------------------------------------------------*/
  177. /* Controls */
  178. void beep (UINT len, BYTE cnt)
  179. {
  180. while (cnt--) {
  181. BEEP_ON();
  182. set_timer(beep, len);
  183. while(!timer_expired(beep)) {};
  184. BEEP_OFF();
  185. set_timer(beep, len);
  186. while(!timer_expired(beep)) {};
  187. }
  188. }
  189. /* Make ISO time string */
  190. const char *get_iso_time(time_t time) {
  191. static char output[32];
  192. struct tm *ct = gmtime(&time);
  193. xsprintf(output, PSTR("%4u-%02u-%02uT%02u:%02u:%02u.000Z"), ct->tm_year+1900, ct->tm_mon+1, ct->tm_mday, ct->tm_hour, ct->tm_min, ct->tm_sec);
  194. return output;
  195. }
  196. /* Compare sentence header string */
  197. BYTE gp_comp (const char *str1, __flash const char *str2)
  198. {
  199. char c;
  200. do {
  201. c = pgm_read_byte(str2++);
  202. } while (c && c == *str1++);
  203. return c;
  204. }
  205. #define FIELD_BUF_LEN 32
  206. /* Get a column item */
  207. static
  208. const char* gp_col ( /* Returns pointer to the item (returns a NULL when not found) */
  209. const char* buf, /* Pointer to the sentence */
  210. BYTE col /* Column number (0 is the 1st item) */
  211. ) {
  212. BYTE c;
  213. static char field_buf[FIELD_BUF_LEN];
  214. unsigned char length = 0;
  215. while (col) {
  216. do {
  217. c = *buf++;
  218. if (c <= ' ') return NULL;
  219. } while (c != ',');
  220. col--;
  221. }
  222. while (*buf && *buf != ',' && length < FIELD_BUF_LEN-1) {
  223. field_buf[length++] = *buf++;
  224. }
  225. field_buf[length] = '\0';
  226. return field_buf;
  227. }
  228. static
  229. BYTE gp_val2 (
  230. const char *db
  231. )
  232. {
  233. BYTE n, m;
  234. n = db[0] - '0';
  235. if (n >= 10)
  236. return 0;
  237. m = db[1] - '0';
  238. if (m >= 10)
  239. return 0;
  240. return n * 10 + m;
  241. }
  242. static
  243. UINT gp_val3 (
  244. const char *db
  245. )
  246. {
  247. BYTE n, m, l;
  248. n = db[0] - '0';
  249. if (n >= 10)
  250. return 0;
  251. m = db[1] - '0';
  252. if (m >= 10)
  253. return 0;
  254. l = db[2] - '0';
  255. if (l >= 10)
  256. return 0;
  257. return n * 100 + m * 10 + l;
  258. }
  259. static
  260. time_t gp_rmctime ( /* Get GPS status from RMC sentence */
  261. const char *str
  262. )
  263. {
  264. const char *p;
  265. struct tm tmc;
  266. double tmp;
  267. if (gp_comp(str, PSTR("$GPRMC")))
  268. return 0; /* Not the RMC */
  269. p = gp_col(str, 1); /* Get h:m:s */
  270. if (!p)
  271. return 0;
  272. tmc.tm_hour = gp_val2(p);
  273. tmc.tm_min = gp_val2(p+2);
  274. tmc.tm_sec = gp_val2(p+4);
  275. p = gp_col(str, 9); /* Get y:m:d */
  276. if (!p)
  277. return 0;
  278. tmc.tm_mday = gp_val2(p);
  279. tmc.tm_mon = gp_val2(p+2) - 1;
  280. tmc.tm_year = gp_val2(p+4) + 100;
  281. utc = mktime(&tmc); /* Check time validity */
  282. if (utc == -1)
  283. return 0;
  284. p = gp_col(str, 2); /* Get status */
  285. if (!p || *p != 'A') {
  286. FLAGS &= ~F_GPSOK;
  287. return 0; /* Return 0 even is time is valid (comes from module's internal RTC) */
  288. }
  289. FLAGS |= F_GPSOK;
  290. /* parse location */
  291. p = gp_col(str, 3); /* latitude */
  292. location.lat = gp_val2(p); /* degrees */
  293. p += 2;
  294. xatof(&p, &tmp); /* minutes */
  295. tmp /= 60; /* convert minutes to degrees */
  296. location.lat += tmp;
  297. p = gp_col(str, 4); /* N/S */
  298. if (*p != 'N')
  299. location.lat = -location.lat;
  300. p = gp_col(str, 5); /* longitude */
  301. location.lon = gp_val3(p); /* degrees */
  302. p += 3;
  303. xatof(&p, &tmp); /* minutes */
  304. tmp /= 60; /* convert minutes to degrees */
  305. location.lon += tmp;
  306. p = gp_col(str, 6); /* E/W */
  307. if (*p != 'E')
  308. location.lon = -location.lon;
  309. location.time = utc;
  310. return utc;
  311. }
  312. #define LOG_SIZE 300
  313. struct {
  314. char buf[LOG_SIZE+1];
  315. unsigned int len;
  316. } logbuf;
  317. void log_put(char c){
  318. UINT bw;
  319. uart1_put(c);
  320. logbuf.buf[logbuf.len++] = c;
  321. if (logbuf.len >= LOG_SIZE && (FLAGS & F_FILEOPEN)) {
  322. if(!f_write(&system_log, logbuf.buf, logbuf.len, &bw))
  323. logbuf.len = 0;
  324. }
  325. if (logbuf.len > LOG_SIZE) {
  326. logbuf.len--;
  327. }
  328. }
  329. static
  330. void ioinit (void)
  331. {
  332. POWER_ON_DDR |= POWER_ON;
  333. PORTA |= POWER_ON;
  334. BUZZER_DDR |= BUZZER;
  335. GPS_DIS_DDR |= GPS_DIS;
  336. LEDR_DDR |= LEDR;
  337. OCR1A = F_CPU/8/100-1; /* Timer1: 100Hz interval (OC1A) */
  338. TCCR1B = _BV(WGM12) | _BV(CS11);
  339. TIMSK1 = _BV(OCIE1A); /* Enable TC1.oca interrupt */
  340. /* ADC */
  341. // ADMUX = 0;
  342. ADMUX = 1; // FIXME only testing battery voltage
  343. ADCSRA = _BV(ADEN)|_BV(ADATE)|_BV(ADIE)|_BV(ADPS2)|_BV(ADPS1)|_BV(ADPS0);
  344. /* uart1 (debug) */
  345. uart1_init();
  346. I2C_init();
  347. expander_init(0, 0x00, 0x00); /* all as outputs */
  348. LCD_Initialize();
  349. LCD_Clear();
  350. sei();
  351. ADCSRA |= _BV(ADSC);
  352. /* unused pins */
  353. DDRA |= _BV(PA2) | _BV(PA4) | _BV(PA5);
  354. PRR0 |= _BV(PRTIM2) | _BV(PRTIM0);
  355. ACSR = _BV(ACD); /* Disable analog comp */
  356. }
  357. __flash const unsigned char battery_states[][8] = {
  358. {
  359. 0b01110,
  360. 0b11111,
  361. 0b11111,
  362. 0b11111,
  363. 0b11111,
  364. 0b11111,
  365. 0b11111,
  366. 0b11111,
  367. },
  368. {
  369. 0b01110,
  370. 0b11111,
  371. 0b10001,
  372. 0b11111,
  373. 0b11111,
  374. 0b11111,
  375. 0b11111,
  376. 0b11111,
  377. },
  378. {
  379. 0b01110,
  380. 0b11111,
  381. 0b10001,
  382. 0b10001,
  383. 0b10001,
  384. 0b11111,
  385. 0b11111,
  386. 0b11111,
  387. },
  388. {
  389. 0b01110,
  390. 0b11111,
  391. 0b10001,
  392. 0b10001,
  393. 0b10001,
  394. 0b10001,
  395. 0b10001,
  396. 0b11111,
  397. },
  398. };
  399. void battery_state_display(void) {
  400. unsigned char i;
  401. unsigned char index;
  402. if (System.bat_volt > 4.0)
  403. index = 0;
  404. else if (System.bat_volt > 3.7)
  405. index = 1;
  406. else if (System.bat_volt > 3.4)
  407. index = 2;
  408. else
  409. index = 3;
  410. LCD_WriteCommand(0x40 + 0); // 0x00
  411. for(i=0; i<8; i++){
  412. LCD_WriteData(battery_states[index][i]);
  413. };
  414. }
  415. __flash const char __open_msg[] = "Open %s\r\n";
  416. /*-----------------------------------------------------------------------*/
  417. /* Main */
  418. /*-----------------------------------------------------------------------*/
  419. int main (void)
  420. {
  421. UINT bw, len;
  422. static struct tm ct;
  423. time_t tmp_utc, localtime;
  424. FRESULT res;
  425. unsigned char prev_status;
  426. ioinit();
  427. xdev_out(log_put);
  428. xputs_P(PSTR("STARTUP\r\n"));
  429. LCD_GoTo(0,0);
  430. LCD_WriteTextP(PSTR("Uruchamianie... "));
  431. _delay_ms(3000);
  432. for (;;) {
  433. if (FLAGS & (F_POWEROFF | F_LVD)) {
  434. xputs_P(PSTR("POWEROFF\r\n"));
  435. LCD_GoTo(0,0);
  436. LCD_WriteTextP(PSTR("Wylaczanie... "));
  437. if (FLAGS & F_LVD) {
  438. LCD_GoTo(0,1);
  439. LCD_WriteTextP(PSTR("Bateria slaba! "));
  440. _delay_ms(500);
  441. }
  442. POWEROFF();
  443. while (POWER_SW_PRESSED());
  444. _delay_ms(2000); /* wait for switch off */
  445. FLAGS &= ~F_POWEROFF; /* restart if power is not lost */
  446. POWERON();
  447. xputs_P(PSTR("RESTART\r\n"));
  448. }
  449. LCD_GoTo(0,0);
  450. LCD_WriteTextP(PSTR("Start "));
  451. LCD_GoTo(0,1);
  452. switch(System.status){
  453. case STATUS_NO_POWER: case STATUS_OK: case STATUS_NO_GPS: LCD_WriteTextP(PSTR(" ")); break;
  454. case STATUS_NO_DISK: LCD_WriteTextP(PSTR("Brak karty! ")); break;
  455. case STATUS_DISK_ERROR: LCD_WriteTextP(PSTR("Blad karty! ")); break;
  456. case STATUS_FILE_WRITE_ERROR: LCD_WriteTextP(PSTR("Blad zapisu! ")); break;
  457. case STATUS_FILE_SYNC_ERROR: LCD_WriteTextP(PSTR("Blad zapisu FAT!")); break;
  458. case STATUS_FILE_CLOSE_ERROR: LCD_WriteTextP(PSTR("Blad zamk.pliku!")); break;
  459. case STATUS_FILE_OPEN_ERROR: LCD_WriteTextP(PSTR("Blad otw. pliku!")); break;
  460. }
  461. xprintf(PSTR("LOOP err=%u\r\n"), (unsigned int)System.status);
  462. utc = 0;
  463. localtime = 0;
  464. prev_status = System.status;
  465. System.status = STATUS_NO_POWER;
  466. beep(250, prev_status); /* Error beep */
  467. /* Wait for supply voltage stabled */
  468. _delay_ms(500);
  469. if (FLAGS & F_LVD)
  470. continue;
  471. /* report error here ( prev_status) */
  472. if (disk_status(0) & STA_NODISK) {
  473. System.status = STATUS_NO_DISK;
  474. continue;
  475. }
  476. res = f_mount(&Fatfs, "", 1);
  477. if (res != FR_OK) {
  478. xprintf(PSTR("FS error %u\r\n"), res);
  479. System.status = STATUS_DISK_ERROR;
  480. continue;
  481. }
  482. System.status = STATUS_NO_GPS;
  483. xputs(PSTR("FS Ok\r\n"));
  484. LCD_GoTo(0,0);
  485. LCD_WriteTextP(PSTR("Karta OK! "));
  486. LCD_WriteData(0); /* battery symbol */
  487. LCD_GoTo(0,1);
  488. LCD_WriteTextP(PSTR("Czekam na GPS..."));
  489. beep(50, 1); /* 1 beep */
  490. /* Initialize GPS receiver */
  491. GPS_ON(); /* GPS power on */
  492. FLAGS |= F_POW;
  493. _delay_ms(300); /* Delay */
  494. uart0_init(); /* Enable UART */
  495. // xfprintf(uart0_put, PSTR("$PSRF106,21*0F\r\n")); /* Send initialization command (depends on the receiver) */
  496. for (;;) { /* main loop */
  497. battery_state_display();
  498. gettemp();
  499. if (!(FLAGS & F_GPSOK))
  500. xputs_P(PSTR("Waiting for GPS\r\n"));
  501. len = get_line(Line, sizeof Line); /* Receive a line from GPS receiver */
  502. if (!len){
  503. if (FLAGS & (F_LVD | F_POWEROFF))
  504. break; /* brownout */
  505. continue;
  506. }
  507. tmp_utc = gp_rmctime(Line);
  508. if (tmp_utc) {
  509. localtime = tmp_utc + LOCALDIFF * 3600L; /* Local time */
  510. ct = *gmtime(&localtime);
  511. if (timer_expired(system_log)) {
  512. set_timer(system_log, 5000);
  513. xprintf(PSTR("Time: %u.%02u.%04u %u:%02u:%02u\r\n"), ct.tm_mday, ct.tm_mon + 1, ct.tm_year+1900, ct.tm_hour, ct.tm_min, ct.tm_sec);
  514. xprintf(PSTR("Bat volt: %.3f\r\n"), System.bat_volt);
  515. if (System.temperature_ok)
  516. xprintf(PSTR("Temp: %.2f\r\n"), System.temperature);
  517. else
  518. xputs_P(PSTR("Temperature unknown\r\n"));
  519. }
  520. LEDG_ON();
  521. _delay_ms(2);
  522. LEDG_OFF();
  523. }
  524. if (localtime && !(FLAGS & F_FILEOPEN)) {
  525. xsprintf(Line, PSTR("%04u-%02u-%02u_%02u-%02u-%02u.LOG"), ct.tm_year+1900, ct.tm_mon + 1, ct.tm_mday, ct.tm_hour, ct.tm_min, ct.tm_sec);
  526. xprintf(__open_msg, Line);
  527. if (f_open(&gps_log, Line, FA_WRITE | FA_OPEN_ALWAYS) /* Open log file */
  528. || f_lseek(&gps_log, f_size(&gps_log)) /* Append mode */
  529. || f_write(&gps_log, "\r\n", 2, &bw)) /* Put a blank line as start marker */
  530. {
  531. System.status = STATUS_FILE_OPEN_ERROR;
  532. break; /* Failed to start logging */
  533. }
  534. xsprintf(Line, PSTR("%04u-%02u-%02u_%02u-%02u-%02u.GPX"), ct.tm_year+1900, ct.tm_mon + 1, ct.tm_mday, ct.tm_hour, ct.tm_min, ct.tm_sec);
  535. xprintf(__open_msg, Line);
  536. if (f_open(&gpx_file, Line, FA_WRITE | FA_OPEN_ALWAYS) /* Open log file */
  537. || f_lseek(&gpx_file, f_size(&gpx_file)) /* Append mode */
  538. || gpx_init(&gpx_file)) /* Put a blank line as start marker */
  539. {
  540. f_close(&gpx_file);
  541. System.status = STATUS_FILE_OPEN_ERROR;
  542. break; /* Failed to start logging */
  543. }
  544. xsprintf(Line, PSTR("%04u-%02u-%02u_%02u-%02u-%02u-SYSTEM.LOG"), ct.tm_year+1900, ct.tm_mon + 1, ct.tm_mday, ct.tm_hour, ct.tm_min, ct.tm_sec);
  545. xprintf(__open_msg, Line);
  546. if (f_open(&system_log, Line, FA_WRITE | FA_OPEN_ALWAYS) /* Open log file */
  547. || f_lseek(&system_log, f_size(&system_log)) /* Append mode */
  548. || f_write(&system_log, "\r\n", 2, &bw)) /* Put a blank line as start marker */
  549. {
  550. f_close(&gpx_file);
  551. f_close(&gps_log);
  552. System.status = STATUS_FILE_OPEN_ERROR;
  553. break; /* Failed to start logging */
  554. }
  555. FLAGS |= F_FILEOPEN;
  556. System.status = STATUS_OK;
  557. beep(50, 2); /* Two beeps. Start logging. */
  558. LCD_GoTo(0,1);
  559. LCD_WriteTextP(PSTR("Zapis aktywny "));
  560. continue;
  561. }
  562. if (FLAGS & F_FILEOPEN) {
  563. f_write(&gps_log, Line, len, &bw);
  564. if (bw != len) {
  565. System.status = STATUS_FILE_WRITE_ERROR;
  566. break;
  567. }
  568. if(tmp_utc) /* a new point */
  569. gpx_process_point(&location, &gpx_file);
  570. if (FLAGS & F_SYNC) {
  571. if (f_sync(&gps_log)) {
  572. System.status = STATUS_FILE_SYNC_ERROR;
  573. break;
  574. }
  575. FLAGS &= ~F_SYNC;
  576. }
  577. }
  578. }
  579. /* Stop GPS receiver */
  580. uart0_deinit();
  581. GPS_OFF();
  582. FLAGS &= ~F_POW;
  583. /* Close file */
  584. if (FLAGS & F_FILEOPEN) {
  585. if (f_close(&gps_log))
  586. System.status = STATUS_FILE_CLOSE_ERROR;
  587. if (gpx_close(&gpx_file))
  588. System.status = STATUS_FILE_CLOSE_ERROR;
  589. if (logbuf.len && !f_write(&system_log, logbuf.buf, logbuf.len, &bw)) {
  590. logbuf.len = 0;
  591. }
  592. if (f_close(&system_log))
  593. System.status = STATUS_FILE_CLOSE_ERROR;
  594. xputs_P(PSTR("File closed\r\n"));
  595. LCD_GoTo(0,1);
  596. LCD_WriteTextP(PSTR("Pliki zamkniete "));
  597. }
  598. FLAGS &= ~F_FILEOPEN;
  599. disk_ioctl(0, CTRL_POWER, 0);
  600. if (System.status != STATUS_FILE_CLOSE_ERROR)
  601. beep(500, 1); /* Long beep on file close succeeded */
  602. }
  603. }