main.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. /* struct system_s/location_s/config_s/auto_pause_s and the value constants
  22. * their fields hold are in common.h - shared byte-for-byte with the PC test
  23. * tool, since gpx.c/nmea.c/syslog.c/autopause.c compile unmodified into both.
  24. * Needs time_t (stime.h, just above) already defined. */
  25. #include "common.h"
  26. #include "expander.h"
  27. #include "settings.h"
  28. #include "ff.h"
  29. #include "diskio.h"
  30. #include "uart0.h"
  31. #include "uart1.h"
  32. #include "xprintf.h"
  33. #include "ds18b20.h"
  34. #include "I2C.h"
  35. /* LCD driver includes */
  36. #if defined(LCD_GRAPHIC)
  37. #include "UC1601S-I2C.h"
  38. #elif defined(LCD_ALNUM)
  39. #include "HD44780-I2C.h"
  40. #else
  41. #error No LCD type defined
  42. #endif
  43. #define IVT_SYNC 180 /* f_sync() interval (0:no periodic sync) [sec] */
  44. #define POWER_SW_TIME 300 /* power switch hold time to power off [10ms] */
  45. #define BACKLIGHT_TIME 10000
  46. //#define VI_LVL 4.2 /* Blackout threshold [volt] */
  47. //#define VI_LVH 4.8 /* Recharge threshold [volt] */
  48. #define VI_LVL 3.1 /* Blackout threshold [volt] */
  49. #define VI_LVH 3.4 /* Recharge threshold [volt] */
  50. #define VI_MULT (3.3 / 6.6 / 2.495 * 1024)
  51. /* pin definitions */
  52. #define BUZZER_PORT PORTA
  53. #define BUZZER _BV(PA7)
  54. #define BUZZER_DDR DDRA
  55. #define GPS_DIS_PORT PORTC
  56. #define GPS_DIS _BV(PC6)
  57. #define GPS_DIS_DDR DDRC
  58. #define LEDR_PORT PORTA
  59. #define LEDR _BV(PA6)
  60. #define LEDR_DDR DDRA
  61. #define SD_CS_PORT PORTB
  62. #define SD_CS _BV(PB4)
  63. #define SD_CS_DDR DDRB
  64. #define SD_PWROFF_PORT PORTB
  65. #define SD_PWROFF _BV(PB3)
  66. #define SD_PWROFF_DDR DDRB
  67. #define SD_PWROFF_PIN PINB
  68. #define SD_CD _BV(PB1)
  69. #define SD_CD_PIN PINB
  70. #define SD_WP _BV(PB2)
  71. #define SD_WP_PIN PINB
  72. #define POWER_ON _BV(PA3)
  73. #define POWER_ON_PORT PORTA
  74. #define POWER_ON_DDR DDRA
  75. #define POWER_SW _BV(PC7)
  76. #define POWER_SW_PIN PINC
  77. #define LEDG_PORT 1 /* expander */
  78. #define LEDG _BV(4)
  79. #define LEDB_PORT 1 /* expander */
  80. #define LEDB _BV(5)
  81. #define LEDW_PORT 1 /* expander */
  82. #define LEDW _BV(7)
  83. /* on/off macros */
  84. #define BEEP_ON() {BUZZER_PORT |= BUZZER;}
  85. #define BEEP_OFF() {BUZZER_PORT &= ~BUZZER;}
  86. #define LEDR_ON() {LEDR_PORT |= LEDR;}
  87. #define LEDR_OFF() {LEDR_PORT &= ~LEDR;}
  88. #define LEDG_ON() expander_set_bit(LEDG_PORT, LEDG, 1)
  89. #define LEDG_OFF() expander_set_bit(LEDG_PORT, LEDG, 0)
  90. #define LEDB_ON() expander_set_bit(LEDB_PORT, LEDB, 1)
  91. #define LEDB_OFF() expander_set_bit(LEDB_PORT, LEDB, 0)
  92. #define LEDW_ON() expander_set_bit(LEDW_PORT, LEDW, 1)
  93. #define LEDW_OFF() expander_set_bit(LEDW_PORT, LEDW, 0)
  94. #define GPS_ON() {GPS_DIS_PORT &= ~GPS_DIS;}
  95. #define GPS_OFF() {GPS_DIS_PORT |= GPS_DIS;}
  96. #define POWEROFF() {POWER_ON_PORT &= ~POWER_ON;}
  97. #define POWERON() {POWER_ON_PORT |= POWER_ON;}
  98. #define POWER_SW_PRESSED() (POWER_SW_PIN & POWER_SW)
  99. #define FLAGS GPIOR0 /* Status flags and bit definitions */
  100. #define F_POW 0x80 /* Power */
  101. #define F_POWEROFF 0x10 /* In process of switching off */
  102. #define F_GPSOK 0x08 /* GPS data valid */
  103. #define F_SYNC 0x04 /* Sync request */
  104. #define F_FILEOPEN 0x02 /* File is open, logging in progress */
  105. #define F_LVD 0x01 /* Low battery detect */
  106. /* System.global_error vals */
  107. #define ERROR_NO 0
  108. #define ERROR_I2C 1
  109. #define ERROR_I2C_TIMEOUT 2
  110. #define ERROR_TEMPERATURE 4
  111. #define ERROR_DISK 8
  112. #define ERROR_FILE_WRITE 16
  113. #define ERROR_FILE_SYNC 32
  114. #define ERROR_FILE_CLOSE 64
  115. #define ERROR_FILE_OPEN 128
  116. /* System.status vals */
  117. #define STATUS_NO_POWER 0
  118. #define STATUS_NO_DISK 1
  119. #define STATUS_NO_GPS 2
  120. #define STATUS_OK 3
  121. #define STATUS_DISK_ERROR 4
  122. #define STATUS_FILE_WRITE_ERROR 5
  123. #define STATUS_FILE_SYNC_ERROR 6
  124. #define STATUS_FILE_CLOSE_ERROR 7
  125. #define STATUS_FILE_OPEN_ERROR 8
  126. /* Keyboard */
  127. #define K_UP _BV(1)
  128. #define K_DOWN _BV(3)
  129. #define K_LEFT _BV(2)
  130. #define K_RIGHT _BV(0)
  131. #define K_POWER _BV(4)
  132. /* System.gps_initialized values */
  133. #define GPS_INIT_NOT_INITIALIZED 0
  134. #define GPS_INIT_QUERY_SENT 1
  135. #define GPS_INIT_INITIALIZED 2
  136. #define ms(x) (x/10)
  137. extern volatile struct system_s System;
  138. extern struct location_s location;
  139. extern time_t utc;
  140. extern volatile unsigned long int uptime_ms;
  141. extern volatile unsigned char reset_cause;
  142. void beep(unsigned int len, unsigned char cnt);
  143. /* Project includes - headers that depend on struct definitions above */
  144. #include "gpx.h"
  145. #include "display.h"
  146. #include "working_modes.h"
  147. #include "timec.h"
  148. #include "nmea.h"
  149. #include "menu.h"
  150. #include "syslog.h"
  151. #include "autopause.h"
  152. static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value) __attribute__((always_inline));
  153. static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value){
  154. cli();
  155. *data = value;
  156. sei();
  157. }
  158. static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data) __attribute__((always_inline));
  159. static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data){
  160. unsigned int ret;
  161. cli();
  162. ret = *data;
  163. sei();
  164. return ret;
  165. }
  166. static inline unsigned long int atomic_get_ulong(volatile unsigned long int *volatile data) __attribute__((always_inline));
  167. static inline unsigned long int atomic_get_ulong(volatile unsigned long int *volatile data){
  168. unsigned long int ret;
  169. cli();
  170. ret = *data;
  171. sei();
  172. return ret;
  173. }
  174. #define get_uptime_ms() atomic_get_ulong(&uptime_ms)
  175. #define set_timer(timer,val) set_timer_counts(timer, ms(val))
  176. #define set_timer_counts(timer,val) atomic_set_uint(&System.timers.timer, val)
  177. #define timer_expired(timer) !atomic_get_uint(&System.timers.timer)
  178. void disk_timerproc (void); /* mmc.h */
  179. char *get_iso_time(time_t time, unsigned char local);
  180. void close_files(unsigned char flush_logs);
  181. unsigned char getkey(void);
  182. void sleep(void);
  183. void reset_counters(void);
  184. time_t get_pause_time(void);
  185. unsigned int get_logging_time(void);
  186. unsigned char tracking_is_paused(void);
  187. void pause_time_start(void);
  188. void pause_time_end(void);
  189. unsigned char fix_gap_detected(time_t point_time);
  190. unsigned int get_avg_speed_x100(void);
  191. unsigned int get_dist_avg_speed_x100(void);
  192. /* SD card I/O timing: longest single f_write()/f_sync() call observed since
  193. * the last time the status line reset the counter (io_get_and_reset_max). */
  194. void io_mark(unsigned long int start_ms);
  195. unsigned int io_get_and_reset_max(void);
  196. /* Request that the next main-loop pass sync all open log files immediately,
  197. * instead of waiting for the periodic IVT_SYNC timer - used around events
  198. * (boot, low battery, fix lost, errors, shutdown) whose log lines must not be
  199. * lost to a brownout before the next periodic sync. */
  200. void request_immediate_sync(void);
  201. /* Stack headroom: bytes of the stack-painted region between the end of
  202. * .bss/.noinit and RAMEND that are still untouched. Falls only, monotonically,
  203. * as the deepest call/interrupt nesting grows over the session. */
  204. unsigned int get_stack_free_bytes(void);