main.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 LOCALDIFF +1 /* Time difference from UTC [hours] */
  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 VI_LVL 4.2 /* Blackout threshold [volt] */
  12. //#define VI_LVH 4.8 /* Recharge threshold [volt] */
  13. #define VI_LVL 2.9 /* Blackout threshold [volt] */
  14. #define VI_LVH 3.4 /* Recharge threshold [volt] */
  15. #define VI_MULT (3.3 / 6.6 / 2.495 * 1024)
  16. /* pin definitions */
  17. #define BUZZER_PORT PORTA
  18. #define BUZZER _BV(PA7)
  19. #define BUZZER_DDR DDRA
  20. #define GPS_DIS_PORT PORTC
  21. #define GPS_DIS _BV(PC6)
  22. #define GPS_DIS_DDR DDRC
  23. #define LEDR_PORT PORTA
  24. #define LEDR _BV(PA6)
  25. #define LEDR_DDR DDRA
  26. #define SD_CS_PORT PORTB
  27. #define SD_CS _BV(PB4)
  28. #define SD_CS_DDR DDRB
  29. #define SD_PWROFF_PORT PORTB
  30. #define SD_PWROFF _BV(PB3)
  31. #define SD_PWROFF_DDR DDRB
  32. #define SD_PWROFF_PIN PINB
  33. #define SD_CD _BV(PB1)
  34. #define SD_CD_PIN PINB
  35. #define SD_WP _BV(PB2)
  36. #define SD_WP_PIN PINB
  37. #define POWER_ON _BV(PA3)
  38. #define POWER_ON_PORT PORTA
  39. #define POWER_ON_DDR DDRA
  40. #define POWER_SW _BV(PC7)
  41. #define POWER_SW_PIN PINC
  42. #define LEDG_PORT 1 /* expander */
  43. #define LEDG _BV(4)
  44. /* on/off macros */
  45. #define BEEP_ON() {BUZZER_PORT |= BUZZER;}
  46. #define BEEP_OFF() {BUZZER_PORT &= ~BUZZER;}
  47. #define LEDR_ON() {LEDR_PORT |= LEDR;}
  48. #define LEDR_OFF() {LEDR_PORT &= ~LEDR;}
  49. #define LEDG_ON() expander_set_bit(LEDG_PORT, LEDG, 1)
  50. #define LEDG_OFF() expander_set_bit(LEDG_PORT, LEDG, 0)
  51. #define GPS_ON() {GPS_DIS_PORT &= ~GPS_DIS;}
  52. #define GPS_OFF() {GPS_DIS_PORT |= GPS_DIS;}
  53. #define POWEROFF() {POWER_ON_PORT &= ~POWER_ON;}
  54. #define POWERON() {POWER_ON_PORT |= POWER_ON;}
  55. #define POWER_SW_PRESSED() (POWER_SW_PIN & POWER_SW)
  56. #define FLAGS GPIOR0 /* Status flags and bit definitions */
  57. #define F_POW 0x80 /* Power */
  58. #define F_POWEROFF 0x10 /* In process of switching off */
  59. #define F_GPSOK 0x08 /* GPS data valid */
  60. #define F_SYNC 0x04 /* Sync request */
  61. #define F_FILEOPEN 0x02 /* File is open, logging in progress */
  62. #define F_LVD 0x01 /* Low battery detect */
  63. /* System.global_error vals */
  64. #define ERROR_NO 0
  65. #define ERROR_I2C 1
  66. #define ERROR_I2C_TIMEOUT 2
  67. /* System.status vals */
  68. #define STATUS_NO_POWER 0
  69. #define STATUS_NO_DISK 1
  70. #define STATUS_NO_GPS 2
  71. #define STATUS_OK 3
  72. #define STATUS_DISK_ERROR 4
  73. #define STATUS_FILE_WRITE_ERROR 5
  74. #define STATUS_FILE_SYNC_ERROR 6
  75. #define STATUS_FILE_CLOSE_ERROR 7
  76. #define STATUS_FILE_OPEN_ERROR 8
  77. #define ms(x) (x/10)
  78. struct timers {
  79. unsigned int owire;
  80. unsigned int beep;
  81. unsigned int recv_timeout;
  82. };
  83. struct system_s {
  84. struct timers timers;
  85. unsigned int global_error;
  86. unsigned char status;
  87. };
  88. struct location_s {
  89. float lon;
  90. float lat;
  91. float alt;
  92. time_t time;
  93. };
  94. extern volatile struct system_s System;
  95. extern struct location_s location;
  96. extern time_t utc;
  97. static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value) __attribute__((always_inline));
  98. static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value){
  99. cli();
  100. *data = value;
  101. sei();
  102. }
  103. static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data) __attribute__((always_inline));
  104. static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data){
  105. unsigned int ret;
  106. cli();
  107. ret = *data;
  108. sei();
  109. return ret;
  110. }
  111. #define set_timer(timer,val) set_timer_counts(timer, ms(val))
  112. #define set_timer_counts(timer,val) atomic_set_uint(&System.timers.timer, val)
  113. #define timer_expired(timer) !atomic_get_uint(&System.timers.timer)
  114. void disk_timerproc (void); /* mmc.h */
  115. const char *get_iso_time(time_t time);