altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / stm / ao_led_stm.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20
21 #if LED_PER_LED
22 #error LED_PER_LED support is in ao_led.c now
23 #endif
24
25 void
26 ao_led_on(AO_LED_TYPE colors)
27 {
28 #ifdef LED_PORT
29         LED_PORT->bsrr = (colors & LEDS_AVAILABLE);
30 #else
31 #ifdef LED_PORT_0
32         LED_PORT_0->bsrr = ((colors & LEDS_AVAILABLE) & LED_PORT_0_MASK) << LED_PORT_0_SHIFT;
33 #endif
34 #ifdef LED_PORT_1
35         LED_PORT_1->bsrr = ((colors & LEDS_AVAILABLE) & LED_PORT_1_MASK) << LED_PORT_1_SHIFT;
36 #endif
37 #ifdef LED_PORT_2
38         LED_PORT_2->bsrr = ((colors & LEDS_AVAILABLE) & LED_PORT_2_MASK) << LED_PORT_2_SHIFT;
39 #endif
40 #endif
41 }
42
43 void
44 ao_led_off(AO_LED_TYPE colors)
45 {
46 #ifdef LED_PORT
47         LED_PORT->bsrr = (uint32_t) (colors & LEDS_AVAILABLE) << 16;
48 #else
49 #ifdef LED_PORT_0
50         LED_PORT_0->bsrr = ((uint32_t) (colors & LEDS_AVAILABLE) & LED_PORT_0_MASK) << (LED_PORT_0_SHIFT + 16);
51 #endif
52 #ifdef LED_PORT_1
53         LED_PORT_1->bsrr = ((uint32_t) (colors & LEDS_AVAILABLE) & LED_PORT_1_MASK) << (LED_PORT_1_SHIFT + 16);
54 #endif
55 #ifdef LED_PORT_2
56         LED_PORT_2->bsrr = ((uint32_t) (colors & LEDS_AVAILABLE) & LED_PORT_2_MASK) << (LED_PORT_2_SHIFT + 16);
57 #endif
58 #endif
59 }
60
61 void
62 ao_led_set(AO_LED_TYPE colors)
63 {
64         AO_LED_TYPE     on = colors & LEDS_AVAILABLE;
65         AO_LED_TYPE     off = ~colors & LEDS_AVAILABLE;
66
67         ao_led_off(off);
68         ao_led_on(on);
69 }
70
71 void
72 ao_led_for(AO_LED_TYPE colors, AO_TICK_TYPE ticks) 
73 {
74         ao_led_on(colors);
75         ao_delay(ticks);
76         ao_led_off(colors);
77 }
78
79 #define init_led_pin(port, bit) do { \
80                 stm_moder_set(port, bit, STM_MODER_OUTPUT);             \
81                 stm_otyper_set(port, bit, STM_OTYPER_PUSH_PULL);        \
82         } while (0)
83
84 void
85 ao_led_init(void)
86 {
87         AO_LED_TYPE     bit;
88
89 #ifdef LED_PORT
90         stm_rcc.ahbenr |= (1 << LED_PORT_ENABLE);
91         LED_PORT->odr &= ~LEDS_AVAILABLE;
92 #else
93 #ifdef LED_PORT_0
94         stm_rcc.ahbenr |= (1 << LED_PORT_0_ENABLE);
95         LED_PORT_0->odr &= (uint32_t) ~(LEDS_AVAILABLE & LED_PORT_0_MASK) << LED_PORT_0_SHIFT;
96 #endif
97 #ifdef LED_PORT_1
98         stm_rcc.ahbenr |= (1 << LED_PORT_1_ENABLE);
99         LED_PORT_1->odr &= (uint32_t) ~(LEDS_AVAILABLE & LED_PORT_1_MASK) << LED_PORT_1_SHIFT;
100 #endif
101 #ifdef LED_PORT_2
102         stm_rcc.ahbenr |= (1 << LED_PORT_2_ENABLE);
103         LED_PORT_2->odr &= (uint32_t) ~(LEDS_AVAILABLE & LED_PORT_2_MASK) << LED_PORT_2_SHIFT;
104 #endif
105 #endif
106         for (bit = 0; bit < sizeof (AO_LED_TYPE) * 8; bit++) {
107                 if (LEDS_AVAILABLE & (1 << bit)) {
108 #ifdef LED_PORT
109                         init_led_pin(LED_PORT, bit);
110 #else
111 #ifdef LED_PORT_0
112                         if (LED_PORT_0_MASK & (1 << bit))
113                                 init_led_pin(LED_PORT_0, bit + LED_PORT_0_SHIFT);
114 #endif
115 #ifdef LED_PORT_1
116                         if (LED_PORT_1_MASK & (1 << bit))
117                                 init_led_pin(LED_PORT_1, bit + LED_PORT_1_SHIFT);
118 #endif
119 #ifdef LED_PORT_2
120                         if (LED_PORT_2_MASK & (1 << bit))
121                                 init_led_pin(LED_PORT_2, bit + LED_PORT_2_SHIFT);
122 #endif
123 #endif
124                 }
125         }
126 }