main.c 12 KB

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