main.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #pragma once
  2. #define __PROG_TYPES_COMPAT__
  3. #include <inttypes.h>
  4. #include <avr/pgmspace.h>
  5. #include <avr/interrupt.h>
  6. #include "stime.h"
  7. #include "expander.h"
  8. #include "settings.h"
  9. #if defined(LCD_GRAPHIC)
  10. #include "UC1601S-I2C.h"
  11. #elif defined(LCD_ALNUM)
  12. #include "HD44780-I2C.h"
  13. #else
  14. #error No LCD type defined
  15. #endif
  16. #define IVT_SYNC 180 /* f_sync() interval (0:no periodic sync) [sec] */
  17. #define POWER_SW_TIME 300 /* power switch hold time to power off [10ms] */
  18. #define BACKLIGHT_TIME 10000
  19. //#define VI_LVL 4.2 /* Blackout threshold [volt] */
  20. //#define VI_LVH 4.8 /* Recharge threshold [volt] */
  21. #define VI_LVL 3.1 /* Blackout threshold [volt] */
  22. #define VI_LVH 3.4 /* Recharge threshold [volt] */
  23. #define VI_MULT (3.3 / 6.6 / 2.495 * 1024)
  24. /* pin definitions */
  25. #define BUZZER_PORT PORTA
  26. #define BUZZER _BV(PA7)
  27. #define BUZZER_DDR DDRA
  28. #define GPS_DIS_PORT PORTC
  29. #define GPS_DIS _BV(PC6)
  30. #define GPS_DIS_DDR DDRC
  31. #define LEDR_PORT PORTA
  32. #define LEDR _BV(PA6)
  33. #define LEDR_DDR DDRA
  34. #define SD_CS_PORT PORTB
  35. #define SD_CS _BV(PB4)
  36. #define SD_CS_DDR DDRB
  37. #define SD_PWROFF_PORT PORTB
  38. #define SD_PWROFF _BV(PB3)
  39. #define SD_PWROFF_DDR DDRB
  40. #define SD_PWROFF_PIN PINB
  41. #define SD_CD _BV(PB1)
  42. #define SD_CD_PIN PINB
  43. #define SD_WP _BV(PB2)
  44. #define SD_WP_PIN PINB
  45. #define POWER_ON _BV(PA3)
  46. #define POWER_ON_PORT PORTA
  47. #define POWER_ON_DDR DDRA
  48. #define POWER_SW _BV(PC7)
  49. #define POWER_SW_PIN PINC
  50. #define LEDG_PORT 1 /* expander */
  51. #define LEDG _BV(4)
  52. #define LEDB_PORT 1 /* expander */
  53. #define LEDB _BV(5)
  54. #define LEDW_PORT 1 /* expander */
  55. #define LEDW _BV(7)
  56. /* on/off macros */
  57. #define BEEP_ON() {BUZZER_PORT |= BUZZER;}
  58. #define BEEP_OFF() {BUZZER_PORT &= ~BUZZER;}
  59. #define LEDR_ON() {LEDR_PORT |= LEDR;}
  60. #define LEDR_OFF() {LEDR_PORT &= ~LEDR;}
  61. #define LEDG_ON() expander_set_bit(LEDG_PORT, LEDG, 1)
  62. #define LEDG_OFF() expander_set_bit(LEDG_PORT, LEDG, 0)
  63. #define LEDB_ON() expander_set_bit(LEDB_PORT, LEDB, 1)
  64. #define LEDB_OFF() expander_set_bit(LEDB_PORT, LEDB, 0)
  65. #define LEDW_ON() expander_set_bit(LEDW_PORT, LEDW, 1)
  66. #define LEDW_OFF() expander_set_bit(LEDW_PORT, LEDW, 0)
  67. #define GPS_ON() {GPS_DIS_PORT &= ~GPS_DIS;}
  68. #define GPS_OFF() {GPS_DIS_PORT |= GPS_DIS;}
  69. #define POWEROFF() {POWER_ON_PORT &= ~POWER_ON;}
  70. #define POWERON() {POWER_ON_PORT |= POWER_ON;}
  71. #define POWER_SW_PRESSED() (POWER_SW_PIN & POWER_SW)
  72. #define FLAGS GPIOR0 /* Status flags and bit definitions */
  73. #define F_POW 0x80 /* Power */
  74. #define F_POWEROFF 0x10 /* In process of switching off */
  75. #define F_GPSOK 0x08 /* GPS data valid */
  76. #define F_SYNC 0x04 /* Sync request */
  77. #define F_FILEOPEN 0x02 /* File is open, logging in progress */
  78. #define F_LVD 0x01 /* Low battery detect */
  79. /* System.global_error vals */
  80. #define ERROR_NO 0
  81. #define ERROR_I2C 1
  82. #define ERROR_I2C_TIMEOUT 2
  83. /* System.status vals */
  84. #define STATUS_NO_POWER 0
  85. #define STATUS_NO_DISK 1
  86. #define STATUS_NO_GPS 2
  87. #define STATUS_OK 3
  88. #define STATUS_DISK_ERROR 4
  89. #define STATUS_FILE_WRITE_ERROR 5
  90. #define STATUS_FILE_SYNC_ERROR 6
  91. #define STATUS_FILE_CLOSE_ERROR 7
  92. #define STATUS_FILE_OPEN_ERROR 8
  93. /* Keyboard */
  94. #define K_UP _BV(1)
  95. #define K_DOWN _BV(3)
  96. #define K_LEFT _BV(2)
  97. #define K_RIGHT _BV(0)
  98. #define K_POWER _BV(4)
  99. /* System.location_valid values */
  100. #define LOC_INVALID 0
  101. #define LOC_VALID 1
  102. #define LOC_VALID_NEW 2
  103. /* System.gps_initialized values */
  104. #define GPS_INIT_NOT_INITIALIZED 0
  105. #define GPS_INIT_QUERY_SENT 1
  106. #define GPS_INIT_INITIALIZED 2
  107. #define ms(x) (x/10)
  108. struct timers {
  109. unsigned int owire;
  110. unsigned int beep;
  111. unsigned int recv_timeout;
  112. unsigned int system_log;
  113. unsigned int lcd;
  114. unsigned int backlight;
  115. };
  116. struct system_s {
  117. struct timers timers;
  118. struct config_s conf;
  119. unsigned int global_error;
  120. unsigned char status;
  121. float bat_volt;
  122. float temperature;
  123. unsigned char display_state;
  124. unsigned char keypress;
  125. unsigned char working_mode;
  126. unsigned long int distance; // cm
  127. unsigned char speed; // km/h
  128. time_t time_start;
  129. time_t current_pause_start;
  130. time_t pause_time;
  131. unsigned temperature_ok:1;
  132. unsigned satellites_used:5;
  133. unsigned location_valid:2;
  134. unsigned sbas:1;
  135. unsigned gps_initialized:2;
  136. unsigned gps_only:1;
  137. unsigned tracking_paused:1;
  138. unsigned tracking_auto_paused:1;
  139. unsigned open_new_file:1;
  140. unsigned sat_count_low:1;
  141. };
  142. struct location_s {
  143. float lon;
  144. float lat;
  145. float alt;
  146. time_t time;
  147. };
  148. struct auto_pause_s {
  149. unsigned char point_counter;
  150. unsigned long int prev_distance;
  151. };
  152. extern volatile struct system_s System;
  153. extern struct location_s location;
  154. extern time_t utc;
  155. static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value) __attribute__((always_inline));
  156. static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value){
  157. cli();
  158. *data = value;
  159. sei();
  160. }
  161. static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data) __attribute__((always_inline));
  162. static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data){
  163. unsigned int ret;
  164. cli();
  165. ret = *data;
  166. sei();
  167. return ret;
  168. }
  169. #define set_timer(timer,val) set_timer_counts(timer, ms(val))
  170. #define set_timer_counts(timer,val) atomic_set_uint(&System.timers.timer, val)
  171. #define timer_expired(timer) !atomic_get_uint(&System.timers.timer)
  172. void disk_timerproc (void); /* mmc.h */
  173. char *get_iso_time(time_t time, unsigned char local);
  174. void close_files(unsigned char flush_logs);
  175. unsigned char getkey(void);
  176. void sleep(void);
  177. void reset_counters(void);
  178. time_t get_pause_time(void);
  179. unsigned int get_logging_time(void);