1
0

main.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include <avr/interrupt.h>
  3. #include "expander.h"
  4. #define LOCALDIFF +1 /* Time difference from UTC [hours] */
  5. #define IVT_SYNC 180 /* f_sync() interval (0:no periodic sync) [sec] */
  6. #define VI_LVL 4.2 /* Blackout threshold [volt] */
  7. #define VI_LVH 4.8 /* Recharge threshold [volt] */
  8. #define VI_MULT (3.3 / 6.6 / 2.495 * 1024)
  9. /* pin definitions */
  10. #define BUZZER_PORT PORTA
  11. #define BUZZER _BV(PA7)
  12. #define BUZZER_DDR DDRA
  13. #define GPS_DIS_PORT PORTC
  14. #define GPS_DIS _BV(PC6)
  15. #define GPS_DIS_DDR DDRC
  16. #define LEDR_PORT PORTA
  17. #define LEDR _BV(PA6)
  18. #define LEDR_DDR DDRA
  19. #define SD_CS_PORT PORTB
  20. #define SD_CS _BV(PB4)
  21. #define SD_CS_DDR DDRB
  22. #define SD_PWROFF_PORT PORTB
  23. #define SD_PWROFF _BV(PB3)
  24. #define SD_PWROFF_DDR DDRB
  25. #define SD_PWROFF_PIN PINB
  26. #define SD_CD _BV(PB1)
  27. #define SD_CD_PIN PINB
  28. #define SD_WP _BV(PB2)
  29. #define SD_WP_PIN PINB
  30. #define LEDG_PORT 1 /* expander */
  31. #define LEDG _BV(4)
  32. /* on/off macros */
  33. #define BEEP_ON() {BUZZER_PORT |= BUZZER;}
  34. #define BEEP_OFF() {BUZZER_PORT &= ~BUZZER;}
  35. #define LEDR_ON() {LEDR_PORT |= LEDR;}
  36. #define LEDR_OFF() {LEDR_PORT &= ~LEDR;}
  37. #define LEDG_ON() expander_set_bit(LEDG_PORT, LEDG, 1)
  38. #define LEDG_OFF() expander_set_bit(LEDG_PORT, LEDG, 0)
  39. #define GPS_ON() {GPS_DIS_PORT &= ~GPS_DIS;}
  40. #define GPS_OFF() {GPS_DIS_PORT |= GPS_DIS;}
  41. #define FLAGS GPIOR0 /* Status flags and bit definitions */
  42. #define F_POW 0x80 /* Power */
  43. #define F_GPSOK 0x08 /* GPS data valid */
  44. #define F_SYNC 0x04 /* Sync request */
  45. #define F_FILEOPEN 0x02 /* File is open, logging in progress */
  46. #define F_LVD 0x01 /* Low battery detect */
  47. /* System.global_error vals */
  48. #define ERROR_NO 0
  49. #define ERROR_I2C 1
  50. #define ERROR_I2C_TIMEOUT 2
  51. /* System.status vals */
  52. #define STATUS_NO_POWER 0
  53. #define STATUS_NO_DISK 1
  54. #define STATUS_NO_GPS 2
  55. #define STATUS_OK 3
  56. #define STATUS_DISK_ERROR 4
  57. #define STATUS_FILE_WRITE_ERROR 5
  58. #define STATUS_FILE_SYNC_ERROR 6
  59. #define STATUS_FILE_CLOSE_ERROR 7
  60. #define STATUS_FILE_OPEN_ERROR 8
  61. #define ms(x) (x/10)
  62. struct timers {
  63. unsigned int owire;
  64. unsigned int beep;
  65. unsigned int recv_timeout;
  66. };
  67. struct system_s {
  68. struct timers timers;
  69. unsigned int global_error;
  70. unsigned char status;
  71. };
  72. extern volatile struct system_s System;
  73. static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value) __attribute__((always_inline));
  74. static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value){
  75. cli();
  76. *data = value;
  77. sei();
  78. }
  79. static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data) __attribute__((always_inline));
  80. static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data){
  81. unsigned int ret;
  82. cli();
  83. ret = *data;
  84. sei();
  85. return ret;
  86. }
  87. #define set_timer(timer,val) set_timer_counts(timer, ms(val))
  88. #define set_timer_counts(timer,val) atomic_set_uint(&System.timers.timer, val)
  89. #define timer_expired(timer) !atomic_get_uint(&System.timers.timer)