main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 <stdlib.h>
  13. #include "main.h"
  14. #include "ff.h"
  15. #include "diskio.h"
  16. #include "uart0.h"
  17. #include "uart1.h"
  18. #include "xprintf.h"
  19. #include "stime.h"
  20. #include "ds18b20.h"
  21. #include "I2C.h"
  22. #include "expander.h"
  23. #include "gpx.h"
  24. #include "display.h"
  25. #include "working_modes.h"
  26. #include "timec.h"
  27. #include "nmea.h"
  28. #include "settings.h"
  29. #include "menu.h"
  30. /*FUSES = {0xFF, 0x11, 0xFE};*/ /* ATmega644PA fuses: Low, High, Extended.
  31. This is the fuse settings for this project. The fuse bits will be included
  32. in the output hex file with program code. However some old flash programmers
  33. cannot load the fuse bits from hex file. If it is the case, remove this line
  34. and use these values to program the fuse bits. */
  35. volatile struct system_s System;
  36. FATFS Fatfs; /* File system object for each logical drive */
  37. FIL gps_log; /* File object */
  38. FIL system_log; /* System log file */
  39. char Line[100]; /* Line buffer */
  40. time_t utc; /* current time */
  41. struct location_s location;
  42. struct auto_pause_s auto_pause;
  43. void start_bootloader(void) {
  44. typedef void (*do_reboot_t)(void);
  45. const do_reboot_t do_reboot = (do_reboot_t)((FLASHEND - 1023) >> 1);
  46. cli();
  47. uart1_deinit();
  48. /* close_files(0); // FIXME not working
  49. display_event(DISPLAY_EVENT_BOOTLOADER);*/
  50. LCD_Clear(); /* Do not call display.c here! */
  51. LCD_GoTo(0,0);
  52. LCD_WriteTextP(PSTR("Aktualizacja"));
  53. LCD_GoTo(8,1);
  54. LCD_WriteTextP(PSTR("softu..."));
  55. TCCR0A = TCCR1A = TCCR2A = 0; // make sure interrupts are off and timers are reset.
  56. do_reboot();
  57. wdt_enable(WDTO_15MS);
  58. while(1);
  59. }
  60. /*---------------------------------------------------------*/
  61. /* 100Hz timer interrupt generated by OC1A */
  62. /*---------------------------------------------------------*/
  63. ISR(TIMER1_COMPA_vect)
  64. {
  65. static WORD ivt_sync;
  66. // static BYTE led;
  67. static unsigned int power_sw;
  68. unsigned int *volatile ctimer;
  69. unsigned char i;
  70. unsigned char k;
  71. static unsigned char oldk;
  72. for(i=0; i<sizeof(System.timers)/sizeof(unsigned int); i++){ // decrement every variable from timers struct unless it's already zero
  73. ctimer = ((unsigned int *)&System.timers) + i;
  74. if(*ctimer)
  75. (*ctimer)--;
  76. }
  77. /* Sync interval */
  78. if (IVT_SYNC && ++ivt_sync >= IVT_SYNC * 100) {
  79. ivt_sync = 0;
  80. FLAGS |= F_SYNC;
  81. }
  82. /* Green LED drive */
  83. /* if (FLAGS & F_POW) {
  84. if ((FLAGS & F_GPSOK) || (++led & 0x20)) {
  85. LEDG_ON();
  86. } else {
  87. LEDG_OFF();
  88. }
  89. } else {
  90. LEDG_OFF();
  91. }*/
  92. /* MMC/SD timer procedure */
  93. disk_timerproc();
  94. /* Switch off */
  95. if (POWER_SW_PRESSED()) {
  96. if (++power_sw == POWER_SW_TIME){
  97. FLAGS |= F_POWEROFF;
  98. power_sw++;
  99. }
  100. System.timers.backlight = ms(BACKLIGHT_TIME);
  101. } else {
  102. power_sw = 0;
  103. }
  104. if (uart1_test() && uart1_get() == '0' && uart1_get() == ' '){
  105. LEDB_ON();
  106. start_bootloader();
  107. }
  108. /* keyboard */
  109. k = ((~PIND) >> 4) & 0x0f;
  110. if (POWER_SW_PRESSED())
  111. k |= K_POWER;
  112. if (k && k != oldk) {
  113. System.keypress = k;
  114. oldk = k;
  115. }
  116. if (!k)
  117. oldk = 0;
  118. }
  119. unsigned char getkey(void) {
  120. unsigned char key = System.keypress;
  121. System.keypress = 0;
  122. return key;
  123. }
  124. /* Power supply monitor */
  125. ISR(ADC_vect)
  126. {
  127. float bat_volt;
  128. static unsigned int adc_buf;
  129. static unsigned char adc_cnt;
  130. static BYTE lvt;
  131. adc_buf += ADC;
  132. if(++adc_cnt < 15)
  133. return;
  134. bat_volt = (float)adc_buf/(float)(adc_cnt)/VI_MULT;
  135. adc_buf = 0;
  136. adc_cnt = 0;
  137. System.bat_volt = bat_volt;
  138. if (FLAGS & F_LVD) {
  139. if (bat_volt > VI_LVH) {
  140. FLAGS &= ~F_LVD;
  141. lvt = 0;
  142. }
  143. } else {
  144. if (bat_volt < VI_LVL) {
  145. if (++lvt >= 3)
  146. FLAGS |= F_LVD;
  147. } else {
  148. lvt = 0;
  149. }
  150. }
  151. }
  152. void sleep(void) {
  153. set_sleep_mode(SLEEP_MODE_IDLE);
  154. sleep_enable();
  155. sleep_cpu();
  156. sleep_disable();
  157. }
  158. /*--------------------------------------------------------------------------*/
  159. /* Controls */
  160. void beep (UINT len, BYTE cnt)
  161. {
  162. while (cnt--) {
  163. BEEP_ON();
  164. set_timer(beep, len);
  165. while(!timer_expired(beep)) {};
  166. BEEP_OFF();
  167. set_timer(beep, len);
  168. while(!timer_expired(beep)) {};
  169. }
  170. }
  171. #define LOG_SIZE 300
  172. struct {
  173. char buf[LOG_SIZE+1];
  174. unsigned int len;
  175. } logbuf;
  176. void log_put(int c){
  177. UINT bw;
  178. uart1_put(c);
  179. logbuf.buf[logbuf.len++] = c;
  180. if (logbuf.len >= LOG_SIZE && (FLAGS & F_FILEOPEN)) {
  181. if(!f_write(&system_log, logbuf.buf, logbuf.len, &bw))
  182. logbuf.len = 0;
  183. }
  184. if (logbuf.len > LOG_SIZE) {
  185. logbuf.len--;
  186. }
  187. }
  188. static
  189. void ioinit (void)
  190. {
  191. wdt_enable(WDTO_4S);
  192. MCUSR = 0;
  193. POWER_ON_DDR |= POWER_ON;
  194. PORTA |= POWER_ON;
  195. BUZZER_DDR |= BUZZER;
  196. GPS_OFF();
  197. GPS_DIS_DDR |= GPS_DIS;
  198. LEDR_DDR |= LEDR;
  199. OCR1A = F_CPU/8/100-1; /* Timer1: 100Hz interval (OC1A) */
  200. TCCR1B = _BV(WGM12) | _BV(CS11);
  201. TIMSK1 = _BV(OCIE1A); /* Enable TC1.oca interrupt */
  202. /* ADC */
  203. // ADMUX = 0;
  204. ADMUX = 1; // FIXME only testing battery voltage
  205. ADCSRA = _BV(ADEN)|_BV(ADATE)|_BV(ADIE)|_BV(ADPS2)|_BV(ADPS1)|_BV(ADPS0);
  206. /* uart1 (debug) */
  207. uart1_init();
  208. I2C_init();
  209. expander_init(0, 0x00, 0x00); /* all as outputs */
  210. LCD_Initialize();
  211. LCD_Clear();
  212. LCD_GoTo(0,0);
  213. wdt_reset();
  214. sei();
  215. ADCSRA |= _BV(ADSC);
  216. /* unused pins */
  217. DDRA |= _BV(PA2) | _BV(PA4) | _BV(PA5);
  218. PRR0 |= _BV(PRTIM2) | _BV(PRTIM0);
  219. ACSR = _BV(ACD); /* Disable analog comp */
  220. }
  221. void close_files(unsigned char flush_logs) {
  222. UINT bw;
  223. if (FLAGS & F_FILEOPEN) {
  224. if (f_close(&gps_log))
  225. System.status = STATUS_FILE_CLOSE_ERROR;
  226. if (gpx_close(&gpx_file))
  227. System.status = STATUS_FILE_CLOSE_ERROR;
  228. if (flush_logs && logbuf.len && !f_write(&system_log, logbuf.buf, logbuf.len, &bw)) {
  229. logbuf.len = 0;
  230. }
  231. if (f_close(&system_log))
  232. System.status = STATUS_FILE_CLOSE_ERROR;
  233. xputs_P(PSTR("File closed\r\n"));
  234. display_event(DISPLAY_EVENT_FILE_CLOSED);
  235. }
  236. FLAGS &= ~F_FILEOPEN;
  237. disk_ioctl(0, CTRL_POWER, 0);
  238. }
  239. static inline void auto_unpause(void) {
  240. if (!System.tracking_auto_paused)
  241. return;
  242. System.tracking_auto_paused = 0;
  243. beep(50, 4);
  244. }
  245. static inline void auto_pause_activate(void) {
  246. System.tracking_auto_paused = 1;
  247. beep(50, 3);
  248. }
  249. static inline void auto_pause_process(void) {
  250. if (System.tracking_paused || !get_flag(CONFFLAG_AUTO_PAUSE)) { /* remove auto-pause */
  251. System.tracking_auto_paused = 0;
  252. auto_pause.prev_distance = System.distance;
  253. auto_pause.point_counter = 0;
  254. return;
  255. }
  256. if (System.speed >= System.conf.auto_pause_speed) { /* immediately unpause when set speed is exceeded */
  257. auto_pause.point_counter = 0;
  258. auto_unpause();
  259. return;
  260. }
  261. if (++auto_pause.point_counter < System.conf.auto_pause_time)
  262. return;
  263. auto_pause.point_counter = 0;
  264. if ((System.distance - auto_pause.prev_distance)/100 > System.conf.auto_pause_dist) {
  265. if (System.tracking_auto_paused)
  266. auto_unpause(); /* unpause when distance exceeded */
  267. } else {
  268. if (!System.tracking_auto_paused)
  269. auto_pause_activate(); /* pause otherwise */
  270. }
  271. auto_pause.prev_distance = System.distance;
  272. }
  273. void reset_counters(void) {
  274. System.distance = 0;
  275. System.time_start = 0;
  276. System.current_pause_start = 0;
  277. System.pause_time = 0;
  278. }
  279. time_t get_pause_time(void) {
  280. time_t res = System.pause_time;
  281. if (System.current_pause_start < System.time_start)
  282. System.current_pause_start = System.time_start; /* disallow negative pause time */
  283. if (is_paused() && System.current_pause_start)
  284. res += utc - System.current_pause_start;
  285. return res;
  286. }
  287. unsigned int get_logging_time(void) {
  288. if (!utc || !System.time_start)
  289. return 0;
  290. return utc - System.time_start - get_pause_time();
  291. }
  292. __flash const char __open_msg[] = "Open %s\r\n";
  293. /*-----------------------------------------------------------------------*/
  294. /* Main */
  295. /*-----------------------------------------------------------------------*/
  296. int main (void)
  297. {
  298. UINT bw, len;
  299. static struct tm ct;
  300. time_t tmp_utc, localtime;
  301. FRESULT res;
  302. unsigned char prev_status;
  303. unsigned char already_logging = 0;
  304. ioinit();
  305. xdev_out(log_put);
  306. xputs_P(PSTR("STARTUP\r\n"));
  307. disp_init();
  308. display_event(DISPLAY_EVENT_STARTUP);
  309. settings_load();
  310. menu_push(default_menu);
  311. for (;;) {
  312. wdt_reset();
  313. if (FLAGS & (F_POWEROFF | F_LVD)) {
  314. xputs_P(PSTR("POWEROFF\r\n"));
  315. display_event(DISPLAY_EVENT_POWEROFF);
  316. if (FLAGS & F_LVD) {
  317. display_event(DISPLAY_EVENT_LOW_BATTERY);
  318. _delay_ms(500);
  319. }
  320. POWEROFF();
  321. while (POWER_SW_PRESSED());
  322. _delay_ms(2000); /* wait for switch off */
  323. FLAGS &= ~F_POWEROFF; /* restart if power is not lost */
  324. POWERON();
  325. xputs_P(PSTR("RESTART\r\n"));
  326. }
  327. display_event(DISPLAY_EVENT_INITIALIZED);
  328. xprintf(PSTR("LOOP err=%u\r\n"), (unsigned int)System.status);
  329. utc = 0;
  330. localtime = 0;
  331. prev_status = System.status;
  332. System.status = STATUS_NO_POWER;
  333. beep(250, prev_status); /* Error beep */
  334. /* Wait for supply voltage stabled */
  335. _delay_ms(500);
  336. if (FLAGS & F_LVD)
  337. continue;
  338. /* report error here ( prev_status) */
  339. if (disk_status(0) & STA_NODISK) {
  340. System.status = STATUS_NO_DISK;
  341. continue;
  342. }
  343. res = f_mount(&Fatfs, "", 1);
  344. if (res != FR_OK) {
  345. xprintf(PSTR("FS error %u\r\n"), res);
  346. System.status = STATUS_DISK_ERROR;
  347. continue;
  348. }
  349. System.status = STATUS_NO_GPS;
  350. xputs(PSTR("FS Ok\r\n"));
  351. display_event(DISPLAY_EVENT_CARD_INITIALIZED);
  352. beep(50, 1); /* 1 beep */
  353. /* Initialize GPS receiver */
  354. GPS_ON(); /* GPS power on */
  355. FLAGS |= F_POW;
  356. _delay_ms(1);
  357. uart0_init(); /* Enable UART */
  358. _delay_ms(300); /* Delay */
  359. System.gps_initialized = 0;
  360. if (!already_logging && !get_flag(CONFFLAG_LOGGING_AFTER_BOOT)) {
  361. tracking_pause(TRACKING_PAUSE_CMD_PAUSE, 0);
  362. }
  363. for (;;) { /* main loop */
  364. wdt_reset();
  365. gettemp();
  366. menu();
  367. display_refresh(0);
  368. if (no_menu())
  369. menu_push(default_menu); /* returned from top-level */
  370. if (System.timers.backlight)
  371. LEDW_ON();
  372. else
  373. LEDW_OFF();
  374. if (!(FLAGS & F_GPSOK))
  375. xputs_P(PSTR("Waiting for GPS\r\n"));
  376. len = get_line(Line, sizeof Line); /* Receive a line from GPS receiver */
  377. if (!len){
  378. if (FLAGS & (F_LVD | F_POWEROFF))
  379. break; /* brownout */
  380. continue;
  381. }
  382. tmp_utc = gps_parse(Line);
  383. if (tmp_utc) {
  384. localtime = tmp_utc + local_time_diff(tmp_utc) * 3600L; /* Local time */
  385. ct = *gmtime(&localtime);
  386. if (timer_expired(system_log)) {
  387. set_timer(system_log, 5000);
  388. 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);
  389. xprintf(PSTR("Bat volt: %.3f\r\n"), System.bat_volt);
  390. if (System.temperature_ok)
  391. xprintf(PSTR("Temp: %.2f\r\n"), System.temperature);
  392. else
  393. xputs_P(PSTR("Temperature unknown\r\n"));
  394. if (System.sbas)
  395. xputs_P(PSTR("SBAS (DGPS) active\r\n"));
  396. else
  397. xputs_P(PSTR("SBAS inactive\r\n"));
  398. xputs_P(PSTR("Using GNSS: "));
  399. xputs_P(gnss_names[System.conf.gnss_mode]);
  400. xputs_P(PSTR("\r\n"));
  401. }
  402. LEDG_ON();
  403. _delay_ms(2);
  404. LEDG_OFF();
  405. }
  406. if (FLAGS & F_FILEOPEN) {
  407. f_write(&gps_log, Line, len-1, &bw);
  408. if (bw != len-1) {
  409. System.status = STATUS_FILE_WRITE_ERROR;
  410. break;
  411. }
  412. if (System.location_valid == LOC_VALID_NEW) { /* a new point */
  413. gpx_process_point(&location, &gpx_file);
  414. auto_pause_process();
  415. }
  416. wdt_reset();
  417. if (FLAGS & F_SYNC) {
  418. if (f_sync(&gps_log)) {
  419. System.status = STATUS_FILE_SYNC_ERROR;
  420. break;
  421. }
  422. FLAGS &= ~F_SYNC;
  423. }
  424. if (System.open_new_file) {
  425. close_files(1);
  426. System.open_new_file = 0;
  427. if (!System.tracking_paused)
  428. tracking_pause(TRACKING_PAUSE_CMD_PAUSE, 0);
  429. if (get_flag(CONFFLAG_RESET_ON_NEW_FILE))
  430. reset_counters();
  431. }
  432. if (!System.tracking_paused)
  433. already_logging = 1; /* to avoid pausing on any reset caused by some error */
  434. }
  435. if (System.location_valid == LOC_VALID_NEW) {
  436. System.location_valid = LOC_VALID;
  437. }
  438. if (localtime && !(FLAGS & F_FILEOPEN)) {
  439. char *time = get_iso_time(utc, 1);
  440. iso_time_to_filename(time);
  441. xsprintf(Line, PSTR("%s-NMEA.LOG"), time);
  442. xprintf(__open_msg, Line);
  443. wdt_disable();
  444. if (f_open(&gps_log, Line, FA_WRITE | FA_OPEN_ALWAYS) /* Open log file */
  445. || f_lseek(&gps_log, f_size(&gps_log)) /* Append mode */
  446. || f_write(&gps_log, "\r\n", 2, &bw)) /* Put a blank line as start marker */
  447. {
  448. System.status = STATUS_FILE_OPEN_ERROR;
  449. break; /* Failed to start logging */
  450. }
  451. xsprintf(Line, PSTR("%s.GPX"), time);
  452. xprintf(__open_msg, Line);
  453. if (f_open(&gpx_file, Line, FA_WRITE | FA_OPEN_ALWAYS) /* Open log file */
  454. || f_lseek(&gpx_file, f_size(&gpx_file)) /* Append mode */
  455. || gpx_init(&gpx_file)) /* Put a blank line as start marker */
  456. {
  457. f_close(&gpx_file);
  458. System.status = STATUS_FILE_OPEN_ERROR;
  459. break; /* Failed to start logging */
  460. }
  461. xsprintf(Line, PSTR("%s-SYSTEM.LOG"), time);
  462. xprintf(__open_msg, Line);
  463. if (f_open(&system_log, Line, FA_WRITE | FA_OPEN_ALWAYS) /* Open log file */
  464. || f_lseek(&system_log, f_size(&system_log)) /* Append mode */
  465. || f_write(&system_log, "\r\n", 2, &bw)) /* Put a blank line as start marker */
  466. {
  467. f_close(&gpx_file);
  468. f_close(&gps_log);
  469. System.status = STATUS_FILE_OPEN_ERROR;
  470. break; /* Failed to start logging */
  471. }
  472. wdt_enable(WDTO_4S);
  473. FLAGS |= F_FILEOPEN;
  474. System.status = STATUS_OK;
  475. beep(50, System.tracking_paused?8:2); /* Two beeps. Start logging. */
  476. display_event(DISPLAY_EVENT_FILE_OPEN);
  477. continue;
  478. }
  479. }
  480. wdt_enable(WDTO_4S);
  481. /* Stop GPS receiver */
  482. uart0_deinit();
  483. GPS_OFF();
  484. FLAGS &= ~F_POW;
  485. /* Close file */
  486. close_files(1);
  487. if (System.status != STATUS_FILE_CLOSE_ERROR)
  488. beep(500, 1); /* Long beep on file close succeeded */
  489. settings_store(); /* save eeprom data */
  490. }
  491. }