main.c 7.2 KB

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