main.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #pragma once
  2. #define __PROG_TYPES_COMPAT__
  3. /* Standard library includes */
  4. #include <inttypes.h>
  5. #include <avr/io.h>
  6. #include <avr/pgmspace.h>
  7. #include <avr/interrupt.h>
  8. #include <avr/sleep.h>
  9. #include <avr/wdt.h>
  10. #include <avr/eeprom.h>
  11. #include <util/delay.h>
  12. #include <util/crc16.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stddef.h>
  16. #include <ctype.h>
  17. #include <stdarg.h>
  18. #include <math.h>
  19. /* Project includes - basic headers without struct dependencies */
  20. #include "stime.h"
  21. #include "expander.h"
  22. #include "settings.h"
  23. #include "ff.h"
  24. #include "diskio.h"
  25. #include "uart0.h"
  26. #include "uart1.h"
  27. #include "xprintf.h"
  28. #include "ds18b20.h"
  29. #include "I2C.h"
  30. /* LCD driver includes */
  31. #if defined(LCD_GRAPHIC)
  32. #include "UC1601S-I2C.h"
  33. #elif defined(LCD_ALNUM)
  34. #include "HD44780-I2C.h"
  35. #else
  36. #error No LCD type defined
  37. #endif
  38. #define IVT_SYNC 180 /* f_sync() interval (0:no periodic sync) [sec] */
  39. #define POWER_SW_TIME 300 /* power switch hold time to power off [10ms] */
  40. #define BACKLIGHT_TIME 10000
  41. //#define VI_LVL 4.2 /* Blackout threshold [volt] */
  42. //#define VI_LVH 4.8 /* Recharge threshold [volt] */
  43. #define VI_LVL 3.1 /* Blackout threshold [volt] */
  44. #define VI_LVH 3.4 /* Recharge threshold [volt] */
  45. #define VI_MULT (3.3 / 6.6 / 2.495 * 1024)
  46. /* pin definitions */
  47. #define BUZZER_PORT PORTA
  48. #define BUZZER _BV(PA7)
  49. #define BUZZER_DDR DDRA
  50. #define GPS_DIS_PORT PORTC
  51. #define GPS_DIS _BV(PC6)
  52. #define GPS_DIS_DDR DDRC
  53. #define LEDR_PORT PORTA
  54. #define LEDR _BV(PA6)
  55. #define LEDR_DDR DDRA
  56. #define SD_CS_PORT PORTB
  57. #define SD_CS _BV(PB4)
  58. #define SD_CS_DDR DDRB
  59. #define SD_PWROFF_PORT PORTB
  60. #define SD_PWROFF _BV(PB3)
  61. #define SD_PWROFF_DDR DDRB
  62. #define SD_PWROFF_PIN PINB
  63. #define SD_CD _BV(PB1)
  64. #define SD_CD_PIN PINB
  65. #define SD_WP _BV(PB2)
  66. #define SD_WP_PIN PINB
  67. #define POWER_ON _BV(PA3)
  68. #define POWER_ON_PORT PORTA
  69. #define POWER_ON_DDR DDRA
  70. #define POWER_SW _BV(PC7)
  71. #define POWER_SW_PIN PINC
  72. #define LEDG_PORT 1 /* expander */
  73. #define LEDG _BV(4)
  74. #define LEDB_PORT 1 /* expander */
  75. #define LEDB _BV(5)
  76. #define LEDW_PORT 1 /* expander */
  77. #define LEDW _BV(7)
  78. /* on/off macros */
  79. #define BEEP_ON() {BUZZER_PORT |= BUZZER;}
  80. #define BEEP_OFF() {BUZZER_PORT &= ~BUZZER;}
  81. #define LEDR_ON() {LEDR_PORT |= LEDR;}
  82. #define LEDR_OFF() {LEDR_PORT &= ~LEDR;}
  83. #define LEDG_ON() expander_set_bit(LEDG_PORT, LEDG, 1)
  84. #define LEDG_OFF() expander_set_bit(LEDG_PORT, LEDG, 0)
  85. #define LEDB_ON() expander_set_bit(LEDB_PORT, LEDB, 1)
  86. #define LEDB_OFF() expander_set_bit(LEDB_PORT, LEDB, 0)
  87. #define LEDW_ON() expander_set_bit(LEDW_PORT, LEDW, 1)
  88. #define LEDW_OFF() expander_set_bit(LEDW_PORT, LEDW, 0)
  89. #define GPS_ON() {GPS_DIS_PORT &= ~GPS_DIS;}
  90. #define GPS_OFF() {GPS_DIS_PORT |= GPS_DIS;}
  91. #define POWEROFF() {POWER_ON_PORT &= ~POWER_ON;}
  92. #define POWERON() {POWER_ON_PORT |= POWER_ON;}
  93. #define POWER_SW_PRESSED() (POWER_SW_PIN & POWER_SW)
  94. #define FLAGS GPIOR0 /* Status flags and bit definitions */
  95. #define F_POW 0x80 /* Power */
  96. #define F_POWEROFF 0x10 /* In process of switching off */
  97. #define F_GPSOK 0x08 /* GPS data valid */
  98. #define F_SYNC 0x04 /* Sync request */
  99. #define F_FILEOPEN 0x02 /* File is open, logging in progress */
  100. #define F_LVD 0x01 /* Low battery detect */
  101. /* System.global_error vals */
  102. #define ERROR_NO 0
  103. #define ERROR_I2C 1
  104. #define ERROR_I2C_TIMEOUT 2
  105. #define ERROR_TEMPERATURE 4
  106. #define ERROR_DISK 8
  107. #define ERROR_FILE_WRITE 16
  108. #define ERROR_FILE_SYNC 32
  109. #define ERROR_FILE_CLOSE 64
  110. #define ERROR_FILE_OPEN 128
  111. /* System.status vals */
  112. #define STATUS_NO_POWER 0
  113. #define STATUS_NO_DISK 1
  114. #define STATUS_NO_GPS 2
  115. #define STATUS_OK 3
  116. #define STATUS_DISK_ERROR 4
  117. #define STATUS_FILE_WRITE_ERROR 5
  118. #define STATUS_FILE_SYNC_ERROR 6
  119. #define STATUS_FILE_CLOSE_ERROR 7
  120. #define STATUS_FILE_OPEN_ERROR 8
  121. /* Keyboard */
  122. #define K_UP _BV(1)
  123. #define K_DOWN _BV(3)
  124. #define K_LEFT _BV(2)
  125. #define K_RIGHT _BV(0)
  126. #define K_POWER _BV(4)
  127. /* System.location_valid values */
  128. #define LOC_INVALID 0
  129. #define LOC_VALID 1
  130. #define LOC_VALID_NEW 2
  131. /* System.gps_initialized values */
  132. #define GPS_INIT_NOT_INITIALIZED 0
  133. #define GPS_INIT_QUERY_SENT 1
  134. #define GPS_INIT_INITIALIZED 2
  135. #define ms(x) (x/10)
  136. struct timers {
  137. unsigned int owire;
  138. unsigned int beep;
  139. unsigned int recv_timeout;
  140. unsigned int status_log;
  141. unsigned int lcd;
  142. unsigned int backlight;
  143. };
  144. /* System.fix_type values (from GSA) */
  145. #define FIX_TYPE_UNKNOWN 0
  146. #define FIX_TYPE_2D 2
  147. #define FIX_TYPE_3D 3
  148. struct system_s {
  149. struct timers timers;
  150. struct config_s conf;
  151. unsigned int global_error;
  152. unsigned char status;
  153. float bat_volt;
  154. float temperature;
  155. unsigned char display_state;
  156. unsigned char keypress;
  157. unsigned char working_mode;
  158. unsigned long int distance; // cm
  159. unsigned long int elevation_gain; // dm (decimeters, 0.1m resolution); filtered (dead-band+smoothed)
  160. unsigned long int elevation_loss; // dm; filtered (dead-band+smoothed)
  161. unsigned long int elevation_gain_raw; // dm; unfiltered, for log comparison only
  162. unsigned long int elevation_loss_raw; // dm; unfiltered, for log comparison only
  163. unsigned long int alt_max; // dm; highest smoothed altitude seen this session
  164. unsigned long int speed_accum_x100; // sum of instantaneous NMEA speed samples (km/h * 100) while not paused
  165. unsigned long int speed_sample_count; // number of samples in speed_accum_x100
  166. unsigned long int points_written; // count of accepted/written track points this session
  167. unsigned long int rejected_count; // count of rejected candidate points this session (unsigned long: a poor-fix session can reject far more than 65535 times)
  168. unsigned long int points_paused; // count of candidate points seen while tracking_is_paused(), this session
  169. unsigned long int points_skipped; // count of candidate points dropped by the skip_points warm-up (after boot or a fix gap), this session
  170. unsigned long int points_accepted; // count of candidate points that passed every reject check, this session; with filters enabled, points_accepted - points_written*AVG_COUNT is "averaged out, not yet flushed".
  171. // points_paused + points_skipped + rejected_count + points_accepted may fall a couple short of the true candidate-point total: the sliding window used for the distance/altitude spike checks needs a couple of points to refill after each reset (boot, or a fix gap) before it can even decide reject-vs-accept, and those few points aren't tallied anywhere.
  172. unsigned int hdop_x100; // HDOP * 100, from GSA
  173. unsigned char fix_type; // FIX_TYPE_* from GSA
  174. unsigned char speed; // km/h
  175. time_t time_start;
  176. time_t current_pause_start;
  177. time_t pause_time;
  178. float bat_volt_min; // lowest battery voltage seen this session
  179. unsigned temperature_ok:1;
  180. unsigned satellites_used:5;
  181. unsigned location_valid:2;
  182. unsigned dgps:1; /* GGA reports a DGPS-quality fix - the receiver's own doing, not CONFFLAG_ENABLE_SBAS (our SBAS-search request) */
  183. unsigned gps_initialized:2;
  184. unsigned gps_only:1;
  185. unsigned tracking_paused:1;
  186. unsigned tracking_auto_paused:1;
  187. unsigned open_new_file:1;
  188. unsigned sat_count_low:1;
  189. };
  190. struct location_s {
  191. float lon;
  192. float lat;
  193. float alt;
  194. time_t time;
  195. };
  196. extern volatile struct system_s System;
  197. extern struct location_s location;
  198. extern time_t utc;
  199. extern volatile unsigned long int uptime_ms;
  200. extern volatile unsigned char reset_cause;
  201. void beep(unsigned int len, unsigned char cnt);
  202. /* Project includes - headers that depend on struct definitions above */
  203. #include "gpx.h"
  204. #include "display.h"
  205. #include "working_modes.h"
  206. #include "timec.h"
  207. #include "nmea.h"
  208. #include "menu.h"
  209. #include "syslog.h"
  210. #include "autopause.h"
  211. static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value) __attribute__((always_inline));
  212. static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value){
  213. cli();
  214. *data = value;
  215. sei();
  216. }
  217. static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data) __attribute__((always_inline));
  218. static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data){
  219. unsigned int ret;
  220. cli();
  221. ret = *data;
  222. sei();
  223. return ret;
  224. }
  225. static inline unsigned long int atomic_get_ulong(volatile unsigned long int *volatile data) __attribute__((always_inline));
  226. static inline unsigned long int atomic_get_ulong(volatile unsigned long int *volatile data){
  227. unsigned long int ret;
  228. cli();
  229. ret = *data;
  230. sei();
  231. return ret;
  232. }
  233. #define get_uptime_ms() atomic_get_ulong(&uptime_ms)
  234. #define set_timer(timer,val) set_timer_counts(timer, ms(val))
  235. #define set_timer_counts(timer,val) atomic_set_uint(&System.timers.timer, val)
  236. #define timer_expired(timer) !atomic_get_uint(&System.timers.timer)
  237. void disk_timerproc (void); /* mmc.h */
  238. char *get_iso_time(time_t time, unsigned char local);
  239. void close_files(unsigned char flush_logs);
  240. unsigned char getkey(void);
  241. void sleep(void);
  242. void reset_counters(void);
  243. time_t get_pause_time(void);
  244. unsigned int get_logging_time(void);
  245. unsigned char tracking_is_paused(void);
  246. void pause_time_start(void);
  247. void pause_time_end(void);
  248. unsigned char fix_gap_detected(time_t point_time);
  249. unsigned int get_avg_speed_x100(void);
  250. unsigned int get_dist_avg_speed_x100(void);
  251. /* SD card I/O timing: longest single f_write()/f_sync() call observed since
  252. * the last time the status line reset the counter (io_get_and_reset_max). */
  253. void io_mark(unsigned long int start_ms);
  254. unsigned int io_get_and_reset_max(void);
  255. /* Request that the next main-loop pass sync all open log files immediately,
  256. * instead of waiting for the periodic IVT_SYNC timer - used around events
  257. * (boot, low battery, fix lost, errors, shutdown) whose log lines must not be
  258. * lost to a brownout before the next periodic sync. */
  259. void request_immediate_sync(void);
  260. /* Stack headroom: bytes of the stack-painted region between the end of
  261. * .bss/.noinit and RAMEND that are still untouched. Falls only, monotonically,
  262. * as the deepest call/interrupt nesting grows over the session. */
  263. unsigned int get_stack_free_bytes(void);