main.h 4.9 KB

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