e3562afc7a2b8d79de07139e7718f7f00706d7c9
[fw/altos] / src / csm84clio-v0.1 / ao_csm84clio.c
1 /*
2  * Copyright © 2023 Bdale Garbee <bdale@gag.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 /*
22  * define the FET outputs
23  */
24
25 typedef struct 
26 {
27         struct stm_gpio *       port;
28         int                     pin;
29 } fet;
30
31 fet outputs[] = {
32         {&stm_gpioe, 9},        /* step_0 */
33         {&stm_gpioe, 13},       /* step_1 */
34         {&stm_gpiob, 11},       /* step_2 */
35         {&stm_gpioe, 8},        /* step_3 */
36         {&stm_gpioe, 12},       /* step_4 */
37         {&stm_gpioe, 10},       /* step_5 */
38         {&stm_gpioe, 14},       /* step_6 */
39         {&stm_gpiob, 10},       /* step_7 */
40         {&stm_gpioe, 11},       /* step_8 */
41         {&stm_gpioe, 15},       /* step_9 */
42         {&stm_gpioa, 5},        /* step_10 */
43         {&stm_gpioc, 5},        /* step_11 */
44         {&stm_gpioe, 7},        /* step_12 */
45         {&stm_gpioa, 6},        /* step_13 */
46         {&stm_gpiob, 0},        /* step_14 */
47         {&stm_gpioa, 3},        /* step_15 */
48         {&stm_gpioa, 7},        /* step_16 */
49         {&stm_gpiob, 1},        /* step_17 */
50         {&stm_gpioa, 4},        /* step_18 */
51         {&stm_gpioc, 4},        /* step_19 */
52         {&stm_gpioe, 4},        /* step_20 */
53         {&stm_gpioc, 0},        /* step_21 */
54         {&stm_gpioc, 3},        /* step_22 */
55         {&stm_gpioe, 5},        /* step_23 */
56         {&stm_gpioc, 1},        /* step_24 */
57         {&stm_gpioe, 2},        /* step_25 */
58         {&stm_gpioc, 14},       /* step_26 */
59         {&stm_gpioc, 2},        /* step_27 */
60         {&stm_gpioe, 3},        /* step_28 */
61         {&stm_gpioc, 15},       /* step_29 */
62         {&stm_gpiob, 3},        /* step_30 */
63         {&stm_gpiob, 5},        /* step_31 */
64         {&stm_gpiob, 9},        /* step_32 */
65         {&stm_gpiob, 4},        /* step_33 */
66         {&stm_gpiob, 6},        /* step_34 */
67         {&stm_gpiod, 7},        /* step_35 */
68         {&stm_gpiob, 7},        /* step_36 */
69         {&stm_gpioe, 0},        /* step_37 */
70         {&stm_gpiod, 6},        /* step_38 */
71         {&stm_gpiob, 8},        /* step_39 */
72         {&stm_gpioc, 7},        /* step_40 */
73         {&stm_gpiod, 1},        /* step_41 */
74         {&stm_gpiod, 4},        /* step_42 */
75         {&stm_gpioc, 6},        /* step_43 */
76         {&stm_gpiod, 0},        /* step_44 */
77         {&stm_gpioc, 8},        /* step_45 */
78         {&stm_gpiod, 2},        /* step_46 */
79         {&stm_gpiod, 5},        /* step_47 */
80         {&stm_gpioc, 9},        /* step_48 */
81         {&stm_gpiod, 3}};       /* step_49 */
82
83 static void
84 ao_fet_control(uint32_t output, uint8_t value)
85 {
86         /* map output 1-50 to corresponding GPIO port and pin, set to value */
87         ao_gpio_set(outputs[output - 1].port, outputs[output - 1].pin, value);
88 }
89
90 static void
91 ao_fet_init(void)
92 {
93         int i;
94
95         /* initialize GPIO outputs and turn them all off */
96         for (i = 0; i < 50; i++) 
97         {
98                 ao_enable_output(outputs[i].port, outputs[i].pin, 0);
99         }
100 }
101
102 static void
103 ao_fet_on(void)
104 {
105         uint32_t output;
106
107         output = ao_cmd_decimal();
108         if (ao_cmd_status != ao_cmd_success)
109                 return;
110         if ((output < 1) || (output > 50))
111                 printf ("Invalid FET selection %lu, must be 1..50\n", output);
112         else {
113                 ao_fet_control(output, 1);
114                 ao_led_on(AO_LED_RED);
115         }
116 }
117
118 static void
119 ao_fet_off(void)
120 {
121         uint32_t output;
122
123         output = ao_cmd_decimal();
124         if (ao_cmd_status != ao_cmd_success)
125                 return;
126         if ((output < 1) || (output > 50))
127                 printf ("Invalid FET selection %lu, must be 1..50\n", output);
128         else {
129                 ao_fet_control(output, 0);
130                 ao_led_off(AO_LED_RED);
131         }
132 }
133
134 static const struct ao_cmds ao_fet_cmds[] = {
135         { ao_fet_on,  "S <output>\0Set (turn on) FET" },
136         { ao_fet_off, "R <output>\0Reset (turn off) FET" },
137         { 0, NULL }
138 };
139
140 int
141 main(void)
142 {
143         ao_fet_init();          /* turn all outputs off ASAP */
144         ao_clock_init();
145
146         ao_task_init();
147         ao_led_init();
148
149         /* both LEDs on briefly as system test */
150         ao_led_on(LEDS_AVAILABLE);
151
152         ao_serial_init();
153         ao_timer_init();
154         ao_dma_init();
155         ao_adc_init();
156         ao_cmd_init();
157         ao_usb_init();
158
159         ao_cmd_register(ao_fet_cmds);
160
161         /* turn red off, leave green on as a "power indicator" */
162         ao_led_off(AO_LED_RED);
163
164         ao_start_scheduler();
165 }