trennfix/sw: Was not as beautiful as expected. Now it's better!
[eisenbahn.git] / trennfix / sw / config / trennfix_0.4.h
1 #ifndef __HARDWARE_H
2 #define __HARDWARE_H
3
4 #include <pin_magic.h>
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)
10 #define PIN_TRIGGER _PIN(PORTB, PB0)
11 #define BTN_PRESSED (!PINVAL(PIN_BTN))
12
13 #define STOP_TIMER1 ({TCCR1 = 0; TCNT1 = 1;})
14
15 #define PWM_CYCLE 21
16
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);
27 OUTPUT_PIN(PIN_TRIGGER);
28 setpin(PIN_BTN, 1); /* Need pullup */
29
30 GIMSK |= _BV(PCIE); /* Enable pin change interrupt for sense port */
31 PCMSK |= _BV(PCINT4); /* PB4, Rail sense input */
32
33 /* Change interrupt for button */
34 PCMSK |= _BV(PCINT1); /* PB1 */
35
36 /* Setup timer 0, used for mm_switch */
37 TCCR0A = 0; /* Normal mode */
38 TCCR0B = 2; /* Prescaler 8 */
39 TIMSK |= _BV(OCIE0A); /* Get a match interrupt */
40 OCR0A = 100; TCCR0B = 2;
41
42 #ifdef WITH_PWM
43 /* Timer 1 as PWM */
44 OCR1C = PWM_CYCLE;
45 OCR1B = PWM_CYCLE;
46 TCCR1 = 0x5;
47
48 GTCCR |= _BV(PWM1B) | _BV(COM1B0);
49 #endif
50
51 }
52
53 /*
54 * Function to trigger an oscilloscope on the drive pin
55 *
56 * Can be altered to use the LED.
57 *
58 */
59 static inline void __attribute__((unused)) trigger(void)
60 {
61 setpin(PIN_TRIGGER, 1);
62 setpin(PIN_TRIGGER, 0);
63 }
64
65
66 static inline void __attribute__((unused)) trigger_on(void)
67 {
68 setpin(PIN_TRIGGER, 1);
69 }
70
71 static inline void __attribute__((unused)) trigger_off(void)
72 {
73 setpin(PIN_TRIGGER, 0);
74 }
75
76 /*
77 * Configuration for mm receiver code.
78 */
79 #define MM_SENSE (!PINVAL(PIN_SENSE))
80 #define MM_TIMER_INT_VECT TIMER0_COMPA_vect
81
82 #define MM_TSTART { \
83 GTCCR |= _BV(TSM) | _BV(PSR0); \
84 TCNT0 = 0; \
85 TIFR |= _BV(OCF0A); \
86 GTCCR &= ~_BV(TSM); \
87 }
88
89
90 #ifdef USE_REGISTER_VARS
91 uint8_t register drive_on asm("r8");
92 #endif
93
94 #endif