main.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 <string.h>
  10. #include "main.h"
  11. #include "ff.h"
  12. #include "diskio.h"
  13. #include "uart0.h"
  14. #include "uart1.h"
  15. #include "xitoa.h"
  16. #include "stime.h"
  17. #include "ds18b20.h"
  18. #include "I2C.h"
  19. #include "expander.h"
  20. #include "HD44780-I2C.h"
  21. FUSES = {0xFF, 0x11, 0xFE}; /* ATmega644PA fuses: Low, High, Extended.
  22. This is the fuse settings for this project. The fuse bits will be included
  23. in the output hex file with program code. However some old flash programmers
  24. cannot load the fuse bits from hex file. If it is the case, remove this line
  25. and use these values to program the fuse bits. */
  26. volatile struct system_s System;
  27. FATFS Fatfs; /* File system object for each logical drive */
  28. FIL File1; /* File object */
  29. char Line[100]; /* Line buffer */
  30. /*---------------------------------------------------------*/
  31. /* 100Hz timer interrupt generated by OC1A */
  32. /*---------------------------------------------------------*/
  33. ISR(TIMER1_COMPA_vect)
  34. {
  35. static WORD ivt_sync;
  36. static BYTE led;
  37. unsigned int *volatile ctimer;
  38. unsigned char i;
  39. for(i=0; i<sizeof(System.timers)/sizeof(unsigned int); i++){ // decrement every variable from timers struct unless it's already zero
  40. ctimer = ((unsigned int *)&System.timers) + i;
  41. if(*ctimer)
  42. (*ctimer)--;
  43. }
  44. /* Sync interval */
  45. if (IVT_SYNC && ++ivt_sync >= IVT_SYNC * 100) {
  46. ivt_sync = 0;
  47. FLAGS |= F_SYNC;
  48. }
  49. /* Green LED drive */
  50. if (FLAGS & F_POW) {
  51. if ((FLAGS & F_GPSOK) || (++led & 0x20)) {
  52. LEDG_ON();
  53. } else {
  54. LEDG_OFF();
  55. }
  56. } else {
  57. LEDG_OFF();
  58. }
  59. /* MMC/SD timer procedure */
  60. disk_timerproc();
  61. }
  62. /* Power supply monitor */
  63. ISR(ADC_vect)
  64. {
  65. WORD ad;
  66. static BYTE lvt;
  67. ad = ADC * 100;
  68. if (FLAGS & F_LVD) {
  69. if (ad > (WORD)(VI_LVH * VI_MULT) * 100) {
  70. FLAGS &= ~F_LVD;
  71. lvt = 0;
  72. }
  73. } else {
  74. if (ad < (WORD)(VI_LVL * VI_MULT) * 100) {
  75. if (++lvt >= 10)
  76. FLAGS |= F_LVD;
  77. } else {
  78. lvt = 0;
  79. }
  80. }
  81. }
  82. /*---------------------------------------------------------*/
  83. /* User Provided Timer Function for FatFs module */
  84. /*---------------------------------------------------------*/
  85. DWORD get_fattime (void)
  86. {
  87. return 0;
  88. }
  89. /*----------------------------------------------------*/
  90. /* Get a line received from GPS module */
  91. /*----------------------------------------------------*/
  92. UINT get_line ( /* 0:Brownout, >0: Number of bytes received. */
  93. char *buff,
  94. UINT sz_buf
  95. )
  96. {
  97. char c;
  98. UINT i = 0;
  99. set_timer(recv_timeout, 1000);
  100. for (;;) {
  101. if (FLAGS & F_LVD)
  102. return 0; /* A brownout is detected */
  103. if (timer_expired(recv_timeout))
  104. return 0; /* timeout; continue the main loop */
  105. if (!uart0_test())
  106. continue;
  107. c = (char)uart0_get();
  108. xputc(c);
  109. if (i == 0 && c != '$')
  110. continue; /* Find start of line */
  111. buff[i++] = c;
  112. if (c == '\n')
  113. break; /* EOL */
  114. if (i >= sz_buf)
  115. i = 0; /* Buffer overflow (abort this line) */
  116. }
  117. return i;
  118. }
  119. /*--------------------------------------------------------------------------*/
  120. /* Controls */
  121. void beep (UINT len, BYTE cnt)
  122. {
  123. while (cnt--) {
  124. BEEP_ON();
  125. set_timer(beep, len);
  126. while(!timer_expired(beep)) {};
  127. BEEP_OFF();
  128. set_timer(beep, len);
  129. while(!timer_expired(beep)) {};
  130. }
  131. }
  132. /* Compare sentence header string */
  133. BYTE gp_comp (const char *str1, const prog_char *str2)
  134. {
  135. char c;
  136. do {
  137. c = pgm_read_byte(str2++);
  138. } while (c && c == *str1++);
  139. return c;
  140. }
  141. /* Get a column item */
  142. static
  143. BYTE* gp_col ( /* Returns pointer to the item (returns a NULL when not found) */
  144. const char* buf, /* Pointer to the sentence */
  145. BYTE col /* Column number (0 is the 1st item) */
  146. ) {
  147. BYTE c;
  148. while (col) {
  149. do {
  150. c = *buf++;
  151. if (c <= ' ') return NULL;
  152. } while (c != ',');
  153. col--;
  154. }
  155. return (BYTE*)buf;
  156. }
  157. static
  158. BYTE gp_val2 (
  159. const BYTE *db
  160. )
  161. {
  162. BYTE n, m;
  163. n = db[0] - '0';
  164. if (n >= 10)
  165. return 0;
  166. m = db[1] - '0';
  167. if (m >= 10)
  168. return 0;
  169. return n * 10 + m;
  170. }
  171. static
  172. time_t gp_rmctime ( /* Get GPS status from RMC sentence */
  173. const char *str
  174. )
  175. {
  176. const BYTE *p;
  177. struct tm tmc;
  178. time_t utc;
  179. if (gp_comp(str, PSTR("$GPRMC")))
  180. return 0; /* Not the RMC */
  181. p = gp_col(str, 2); /* Get status */
  182. if (!p || *p != 'A') {
  183. FLAGS &= ~F_GPSOK;
  184. return 0;
  185. }
  186. p = gp_col(str, 1); /* Get h:m:s */
  187. if (!p)
  188. return 0;
  189. tmc.tm_hour = gp_val2(p);
  190. tmc.tm_min = gp_val2(p+2);
  191. tmc.tm_sec = gp_val2(p+4);
  192. p = gp_col(str, 9); /* Get y:m:d */
  193. if (!p)
  194. return 0;
  195. tmc.tm_mday = gp_val2(p);
  196. tmc.tm_mon = gp_val2(p+2) - 1;
  197. tmc.tm_year = gp_val2(p+4) + 100;
  198. utc = mktime(&tmc); /* Check time validity */
  199. if (utc == -1)
  200. return 0;
  201. FLAGS |= F_GPSOK;
  202. return utc;
  203. }
  204. static
  205. void ioinit (void)
  206. {
  207. BUZZER_DDR |= BUZZER;
  208. GPS_DIS_DDR |= GPS_DIS;
  209. LEDR_DDR |= LEDR;
  210. OCR1A = F_CPU/8/100-1; /* Timer1: 100Hz interval (OC1A) */
  211. TCCR1B = _BV(WGM12) | _BV(CS11);
  212. TIMSK1 = _BV(OCIE1A); /* Enable TC1.oca interrupt */
  213. ACSR = _BV(ACD); /* Disable analog comp */
  214. /* ADC */
  215. ADMUX = 0;
  216. ADCSRA = _BV(ADEN)|_BV(ADSC)|_BV(ADPS2)|_BV(ADPS1)|_BV(ADPS0);
  217. /* uart1 (debug) */
  218. uart1_init();
  219. I2C_init();
  220. expander_init(0, 0x00, 0x00); /* all as outputs */
  221. LCD_Initialize();
  222. LCD_Clear();
  223. sei();
  224. }
  225. /*-----------------------------------------------------------------------*/
  226. /* Main */
  227. /*-----------------------------------------------------------------------*/
  228. int main (void)
  229. {
  230. UINT bw, len, err;
  231. struct tm *ct;
  232. time_t utc;
  233. FRESULT res;
  234. ioinit();
  235. xdev_out(uart1_put);
  236. err = 0;
  237. for (;;) {
  238. beep(250, err); /* Error beep */
  239. err = 0;
  240. /* Wait for supply voltage stabled */
  241. while (FLAGS & F_LVD) {};
  242. _delay_ms(500);
  243. if (FLAGS & F_LVD)
  244. continue;
  245. if (disk_status(0) & STA_NODISK)
  246. continue;
  247. res = f_mount(&Fatfs, "", 1);
  248. if (res != FR_OK) {
  249. xprintf(PSTR("FS error %u\r\n"), res);
  250. err = 2;
  251. continue;
  252. }
  253. xputs(PSTR("FS Ok\r\n"));
  254. beep(50, 1); /* 1 beep */
  255. /* Initialize GPS receiver */
  256. GPS_ON(); /* GPS power on */
  257. FLAGS |= F_POW;
  258. _delay_ms(300); /* Delay */
  259. uart0_init(); /* Enable UART */
  260. // xfprintf(uart0_put, PSTR("$PSRF106,21*0F\r\n")); /* Send initialization command (depends on the receiver) */
  261. utc = 0;
  262. for (;;) { /* main loop */
  263. gettemp();
  264. len = get_line(Line, sizeof Line); /* Receive a line from GPS receiver */
  265. if (!len) continue;
  266. if (FLAGS & F_LVD) break; /* brownout */
  267. if (!utc) {
  268. utc = gp_rmctime(Line); /* Get time in UTC from a valid RMC sentence */
  269. if (utc) {
  270. utc += LOCALDIFF * 3600L; /* Local time */
  271. ct = gmtime(&utc);
  272. xsprintf(Line, PSTR("%02u%02u%02u.LOG"), ct->tm_year % 100, ct->tm_mon + 1, ct->tm_mday);
  273. xprintf(PSTR("Open %s\r\n"), Line);
  274. if (f_open(&File1, Line, FA_WRITE | FA_OPEN_ALWAYS) /* Open log file */
  275. || f_lseek(&File1, f_size(&File1)) /* Append mode */
  276. || f_write(&File1, "\r\n", 2, &bw)) /* Put a blank line as start marker */
  277. {
  278. utc = 0;
  279. err = 2;
  280. break; /* Failed to start logging */
  281. }
  282. beep(50, 2); /* Two beeps. Start logging. */
  283. }
  284. } else {
  285. gp_rmctime(Line);
  286. f_write(&File1, Line, len, &bw);
  287. if (bw != len) {
  288. err = 3;
  289. break;
  290. }
  291. if (FLAGS & F_SYNC) {
  292. if (f_sync(&File1)) {
  293. err = 2;
  294. break;
  295. }
  296. FLAGS &= ~F_SYNC;
  297. }
  298. }
  299. }
  300. /* Stop GPS receiver */
  301. uart0_deinit();
  302. GPS_OFF();
  303. FLAGS &= ~F_POW;
  304. /* Close file */
  305. if (utc && f_close(&File1))
  306. err = 2;
  307. disk_ioctl(0, CTRL_POWER_OFF, 0);
  308. if (!err)
  309. beep(500, 1); /* Long beep on file close succeeded */
  310. }
  311. }