main.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #define ms(x) (x/10)
  104. struct timers {
  105. unsigned int owire;
  106. unsigned int beep;
  107. unsigned int recv_timeout;
  108. unsigned int system_log;
  109. unsigned int lcd;
  110. unsigned int backlight;
  111. };
  112. struct system_s {
  113. struct timers timers;
  114. struct config_s conf;
  115. unsigned int global_error;
  116. unsigned char status;
  117. float bat_volt;
  118. float temperature;
  119. unsigned char display_state;
  120. unsigned char keypress;
  121. unsigned char working_mode;
  122. unsigned long int distance; // cm
  123. unsigned char speed; // km/h
  124. time_t time_start;
  125. unsigned temperature_ok:1;
  126. unsigned satellites_used:5;
  127. unsigned location_valid:2;
  128. unsigned sbas:1;
  129. unsigned gps_initialized:1;
  130. unsigned gps_only:1;
  131. unsigned tracking_paused:1;
  132. unsigned tracking_auto_paused:1;
  133. unsigned open_new_file:1;
  134. unsigned sat_count_low:1;
  135. };
  136. struct location_s {
  137. float lon;
  138. float lat;
  139. float alt;
  140. time_t time;
  141. };
  142. struct auto_pause_s {
  143. unsigned char point_counter;
  144. unsigned long int prev_distance;
  145. };
  146. extern volatile struct system_s System;
  147. extern struct location_s location;
  148. extern time_t utc;
  149. static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value) __attribute__((always_inline));
  150. static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value){
  151. cli();
  152. *data = value;
  153. sei();
  154. }
  155. static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data) __attribute__((always_inline));
  156. static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data){
  157. unsigned int ret;
  158. cli();
  159. ret = *data;
  160. sei();
  161. return ret;
  162. }
  163. #define set_timer(timer,val) set_timer_counts(timer, ms(val))
  164. #define set_timer_counts(timer,val) atomic_set_uint(&System.timers.timer, val)
  165. #define timer_expired(timer) !atomic_get_uint(&System.timers.timer)
  166. void disk_timerproc (void); /* mmc.h */
  167. char *get_iso_time(time_t time, unsigned char local);
  168. void close_files(unsigned char flush_logs);
  169. unsigned char getkey(void);
  170. void sleep(void);