main.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_LVD 0x01 /* Low battery detect */
  46. #define ERROR_NO 0
  47. #define ERROR_I2C 1
  48. #define ERROR_I2C_TIMEOUT 2
  49. #define ms(x) (x/100)
  50. struct timers {
  51. unsigned int owire;
  52. unsigned int beep;
  53. unsigned int recv_timeout;
  54. };
  55. struct system_s {
  56. struct timers timers;
  57. unsigned int global_error;
  58. };
  59. extern volatile struct system_s System;
  60. static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value) __attribute__((always_inline));
  61. static inline void atomic_set_uint(volatile unsigned int *volatile data, unsigned int value){
  62. cli();
  63. *data = value;
  64. sei();
  65. }
  66. static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data) __attribute__((always_inline));
  67. static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data){
  68. unsigned int ret;
  69. cli();
  70. ret = *data;
  71. sei();
  72. return ret;
  73. }
  74. #define set_timer(timer,val) set_timer_counts(timer, ms(val))
  75. #define set_timer_counts(timer,val) atomic_set_uint(&System.timers.timer, val)
  76. #define timer_expired(timer) !atomic_get_uint(&System.timers.timer)