1
0

main.h 4.8 KB

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