main.h 5.1 KB

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