|
| 1 | +#ifdef PANDA |
| 2 | + |
| 3 | +int relay_control = 0; // True if relay is controlled through l-line |
| 4 | + |
| 5 | +/* Conrol a relay connected to l-line pin */ |
| 6 | + |
| 7 | +// 160us cycles, 1 high, 25 low |
| 8 | + |
| 9 | +volatile int turn_on_relay = 0; |
| 10 | +volatile int on_cycles = 25; |
| 11 | + |
| 12 | +//5s timeout |
| 13 | +#define LLINE_TIMEOUT_CYCLES 31250 |
| 14 | +volatile int timeout_cycles = LLINE_TIMEOUT_CYCLES; |
| 15 | + |
| 16 | +void TIM5_IRQHandler(void) { |
| 17 | + if (TIM5->SR & TIM_SR_UIF) { |
| 18 | + on_cycles--; |
| 19 | + timeout_cycles--; |
| 20 | + if (timeout_cycles == 0) { |
| 21 | + turn_on_relay = 0; |
| 22 | + } |
| 23 | + if (on_cycles > 0) { |
| 24 | + if (turn_on_relay) { |
| 25 | + set_gpio_output(GPIOC, 10, 0); |
| 26 | + } |
| 27 | + } |
| 28 | + else { |
| 29 | + set_gpio_output(GPIOC, 10, 1); |
| 30 | + on_cycles = 25; |
| 31 | + } |
| 32 | + } |
| 33 | + TIM5->ARR = 160-1; |
| 34 | + TIM5->SR = 0; |
| 35 | +} |
| 36 | + |
| 37 | +void lline_relay_init (void) { |
| 38 | + set_lline_output(0); |
| 39 | + relay_control = 1; |
| 40 | + set_gpio_output(GPIOC, 10, 1); |
| 41 | + |
| 42 | + // setup |
| 43 | + TIM5->PSC = 48-1; // tick on 1 us |
| 44 | + TIM5->CR1 = TIM_CR1_CEN; // enable |
| 45 | + TIM5->ARR = 50-1; // 50 us |
| 46 | + TIM5->DIER = TIM_DIER_UIE; // update interrupt |
| 47 | + TIM5->CNT = 0; |
| 48 | + |
| 49 | + NVIC_EnableIRQ(TIM5_IRQn); |
| 50 | + |
| 51 | +#ifdef DEBUG |
| 52 | + puts("INIT LLINE\n"); |
| 53 | + puts(" SR "); |
| 54 | + putui(TIM5->SR); |
| 55 | + puts(" PSC "); |
| 56 | + putui(TIM5->PSC); |
| 57 | + puts(" CR1 "); |
| 58 | + putui(TIM5->CR1); |
| 59 | + puts(" ARR "); |
| 60 | + putui(TIM5->ARR); |
| 61 | + puts(" DIER "); |
| 62 | + putui(TIM5->DIER); |
| 63 | + puts(" SR "); |
| 64 | + putui(TIM5->SR); |
| 65 | + puts(" CNT "); |
| 66 | + putui(TIM5->CNT); |
| 67 | + puts("\n"); |
| 68 | +#endif |
| 69 | +} |
| 70 | + |
| 71 | +void lline_relay_release (void) { |
| 72 | + set_lline_output(0); |
| 73 | + relay_control = 0; |
| 74 | + puts("RELEASE LLINE\n"); |
| 75 | + set_gpio_alternate(GPIOC, 10, GPIO_AF7_USART3); |
| 76 | + NVIC_DisableIRQ(TIM5_IRQn); |
| 77 | +} |
| 78 | + |
| 79 | +void set_lline_output(int to_set) { |
| 80 | + timeout_cycles = LLINE_TIMEOUT_CYCLES; |
| 81 | + turn_on_relay = to_set; |
| 82 | +} |
| 83 | + |
| 84 | +int get_lline_status() { |
| 85 | + return turn_on_relay; |
| 86 | +} |
| 87 | + |
| 88 | +#endif |
0 commit comments