main.h 6.4 KB

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