Commit | Line | Data |
---|---|---|
70095677 PH |
1 | #ifndef __HARDWARE_H |
2 | #define __HARDWARE_H | |
3 | ||
93cb14d4 | 4 | #include <pin_magic.h> |
70095677 PH |
5 | |
6 | #define PIN_LED _PIN(PORTB, PORTB2) | |
7 | #define PIN_SENSE _PIN(PORTB, PORTB4) | |
8 | #define PIN_DRIVE _PIN(PORTB, PORTB3) | |
9 | #define PIN_BTN _PIN(PORTB, PB1) | |
d0047978 | 10 | #define PIN_TRIGGER _PIN(PORTB, PB0) |
70095677 PH |
11 | #define BTN_PRESSED (!PINVAL(PIN_BTN)) |
12 | ||
70095677 PH |
13 | #define STOP_TIMER1 ({TCCR1 = 0; TCNT1 = 1;}) |
14 | ||
d0047978 PH |
15 | #define PWM_CYCLE 21 |
16 | ||
70095677 PH |
17 | static inline void setup_hw(void) |
18 | { | |
19 | /* Turn off the 1/8 clock prescaler - now running at 16MHz*/ | |
20 | CLKPR = (1 << CLKPCE); | |
21 | CLKPR = 0; | |
22 | ||
23 | INPUT_PIN(PIN_SENSE); | |
24 | INPUT_PIN(PIN_BTN); | |
25 | OUTPUT_PIN(PIN_DRIVE); | |
26 | OUTPUT_PIN(PIN_LED); | |
d0047978 | 27 | OUTPUT_PIN(PIN_TRIGGER); |
b1a7e65e PH |
28 | setpin(PIN_LED, 0); |
29 | setpin(PIN_TRIGGER, 0); | |
30 | ||
70095677 PH |
31 | setpin(PIN_BTN, 1); /* Need pullup */ |
32 | ||
33 | GIMSK |= _BV(PCIE); /* Enable pin change interrupt for sense port */ | |
34 | PCMSK |= _BV(PCINT4); /* PB4, Rail sense input */ | |
35 | ||
36 | /* Change interrupt for button */ | |
37 | PCMSK |= _BV(PCINT1); /* PB1 */ | |
38 | ||
39 | /* Setup timer 0, used for mm_switch */ | |
40 | TCCR0A = 0; /* Normal mode */ | |
d0047978 | 41 | TCCR0B = 2; /* Prescaler 8 */ |
b1a7e65e PH |
42 | // TIMSK |= _BV(OCIE0A); /* Get a match interrupt */ |
43 | TIMSK |= _BV(TOIE0); | |
44 | OCR0A = 100; | |
45 | TCCR0B = 2; | |
70095677 | 46 | |
dc41eb22 PH |
47 | #ifdef WITH_PWM |
48 | /* Timer 1 as PWM */ | |
d0047978 PH |
49 | OCR1C = PWM_CYCLE; |
50 | OCR1B = PWM_CYCLE; | |
51 | TCCR1 = 0x5; | |
52 | ||
dc41eb22 PH |
53 | GTCCR |= _BV(PWM1B) | _BV(COM1B0); |
54 | #endif | |
55 | ||
70095677 PH |
56 | } |
57 | ||
93cb14d4 PH |
58 | /* |
59 | * Function to trigger an oscilloscope on the drive pin | |
60 | * | |
61 | * Can be altered to use the LED. | |
62 | * | |
63 | */ | |
d0047978 PH |
64 | static inline void __attribute__((unused)) trigger(void) |
65 | { | |
66 | setpin(PIN_TRIGGER, 1); | |
67 | setpin(PIN_TRIGGER, 0); | |
68 | } | |
69 | ||
70 | ||
71 | static inline void __attribute__((unused)) trigger_on(void) | |
72 | { | |
73 | setpin(PIN_TRIGGER, 1); | |
74 | } | |
75 | ||
76 | static inline void __attribute__((unused)) trigger_off(void) | |
93cb14d4 | 77 | { |
d0047978 | 78 | setpin(PIN_TRIGGER, 0); |
93cb14d4 | 79 | } |
70095677 | 80 | |
93cb14d4 PH |
81 | /* |
82 | * Configuration for mm receiver code. | |
83 | */ | |
93cb14d4 | 84 | #define MM_SENSE (!PINVAL(PIN_SENSE)) |
b1a7e65e | 85 | #define MM_TIMER_INT_VECT TIMER0_OVF_vect |
70095677 | 86 | |
d0047978 PH |
87 | #define MM_TSTART { \ |
88 | GTCCR |= _BV(TSM) | _BV(PSR0); \ | |
89 | TCNT0 = 0; \ | |
d0047978 PH |
90 | GTCCR &= ~_BV(TSM); \ |
91 | } | |
92 | ||
b1a7e65e PH |
93 | #define MM_RESETFLAG TIFR |= _BV(OVF0) |
94 | ||
dc41eb22 PH |
95 | |
96 | #ifdef USE_REGISTER_VARS | |
b1a7e65e PH |
97 | uint8_t register drive_on asm("r12"); |
98 | uint8_t register slope asm("r13"); | |
dc41eb22 | 99 | #endif |
70095677 PH |
100 | |
101 | #endif |