main.c 13 KB

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