trennfix/sw: Nice, keep! Good!
[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_DRIVE PIN_LED
10 #define PIN_BTN _PIN(PORTB, PB1)
11
12 #define BTN_PRESSED (!PINVAL(PIN_BTN))
13
14 #define START_TIMER0 ({TCNT0 = 0; TCCR0B = 2;})
15 #define START_TIMER1 ({TCNT1 = 1; TCCR1 = 7; GTCCR |= 2;})
16 #define STOP_TIMER1 ({TCCR1 = 0; TCNT1 = 1;})
17
18 static inline void setup_hw(void)
19 {
20 /* Turn off the 1/8 clock prescaler - now running at 16MHz*/
21 CLKPR = (1 << CLKPCE);
22 CLKPR = 0;
23
24 INPUT_PIN(PIN_SENSE);
25 INPUT_PIN(PIN_BTN);
26 OUTPUT_PIN(PIN_DRIVE);
27 OUTPUT_PIN(PIN_LED);
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 = 0; /* Timer off */
39 TIMSK |= _BV(OCIE0A); /* Get a match interrupt */
40
41 #ifdef WITH_PWM
42 /* Timer 1 as PWM */
43 OCR1C = 14;
44 OCR1B = 14;
45 TCCR1 = 0x6;
46 GTCCR |= _BV(PWM1B) | _BV(COM1B0);
47 #endif
48
49 }
50
51 /*
52 * Function to trigger an oscilloscope on the drive pin
53 *
54 * Can be altered to use the LED.
55 *
56 */
57 static void __attribute__((unused)) trigger(void)
58 {
59 setpin(PIN_DRIVE, 1);
60 _delay_us(4);
61 setpin(PIN_DRIVE, 0);
62 _delay_us(4);
63 }
64
65 /*
66 * Configuration for mm receiver code.
67 */
68
69 #define MM_SENSE (!PINVAL(PIN_SENSE))
70 #define MM_TSTART ({TCNT0 = 0; GTCCR |= 1; OCR0A = 115; TCCR0B = 2;})
71 #define MM_TIMER_INT_VECT TIMER0_COMPA_vect
72
73 /* Costs 38 bytes program memory */
74 #define MM_FILTER_REPEATED
75 #define USE_EEPROM_UPDATE
76
77 #ifdef USE_REGISTER_VARS
78 uint8_t register drive_on asm("r8");
79 #endif
80
81 #endif