]> mj.ucw.cz Git - home-hw.git/blob - test-shutters/main.c
test-shutters: Init
[home-hw.git] / test-shutters / main.c
1 /*
2  *      Testing Shutter Controller
3  *
4  *      (c) 2023 Martin Mareš <mj@ucw.cz>
5  */
6
7 #include "util.h"
8
9 #include <libopencm3/cm3/cortex.h>
10 #include <libopencm3/cm3/nvic.h>
11 #include <libopencm3/cm3/systick.h>
12 #include <libopencm3/cm3/scb.h>
13 #include <libopencm3/stm32/rcc.h>
14 #include <libopencm3/stm32/desig.h>
15 #include <libopencm3/stm32/gpio.h>
16 #include <libopencm3/stm32/usart.h>
17 #include <libopencm3/stm32/spi.h>
18 #include <libopencm3/stm32/timer.h>
19 #include <libopencm3/usb/dfu.h>
20 #include <libopencm3/usb/usbd.h>
21
22 #include <string.h>
23
24 /*** Hardware init ***/
25
26 static void clock_init(void)
27 {
28         rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
29
30         rcc_periph_clock_enable(RCC_GPIOA);
31         rcc_periph_clock_enable(RCC_GPIOB);
32         rcc_periph_clock_enable(RCC_GPIOC);
33         rcc_periph_clock_enable(RCC_SPI2);
34         rcc_periph_clock_enable(RCC_USART1);
35         rcc_periph_clock_enable(RCC_USB);
36
37         rcc_periph_reset_pulse(RST_GPIOA);
38         rcc_periph_reset_pulse(RST_GPIOB);
39         rcc_periph_reset_pulse(RST_GPIOC);
40         rcc_periph_reset_pulse(RST_USART1);
41         rcc_periph_reset_pulse(RST_USB);
42 }
43
44 static void gpio_init(void)
45 {
46         // PA9 = TXD1 for debugging console
47         // PA10 = RXD1 for debugging console
48         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
49         gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
50
51         // PC13 = BluePill LED
52         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
53         gpio_clear(GPIOC, GPIO13);
54
55         // PB6 = SSR (active low)
56         gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO6);
57         gpio_set(GPIOB, GPIO6);
58
59         // PA0 = current sense
60         gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO0);
61         gpio_set(GPIOA, GPIO0);
62 }
63
64 static void usart_init(void)
65 {
66         usart_set_baudrate(USART1, 115200);
67         usart_set_databits(USART1, 8);
68         usart_set_stopbits(USART1, USART_STOPBITS_1);
69         usart_set_mode(USART1, USART_MODE_TX_RX);
70         usart_set_parity(USART1, USART_PARITY_NONE);
71         usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
72
73         usart_enable(USART1);
74 }
75
76 /*** System ticks ***/
77
78 static volatile u32 ms_ticks;
79
80 void sys_tick_handler(void)
81 {
82         ms_ticks++;
83 }
84
85 static void tick_init(void)
86 {
87         systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000);
88         systick_counter_enable();
89         systick_interrupt_enable();
90 }
91
92 static void delay_ms(uint ms)
93 {
94         u32 start_ticks = ms_ticks;
95         while (ms_ticks - start_ticks < ms)
96                 ;
97 }
98
99 /*** USB ***/
100
101 static usbd_device *usbd_dev;
102
103 enum usb_string {
104         STR_MANUFACTURER = 1,
105         STR_PRODUCT,
106         STR_SERIAL,
107 };
108
109 static char usb_serial_number[13];
110
111 static const char *usb_strings[] = {
112         "United Computer Wizards",
113         "Shutter Control Test",
114         usb_serial_number,
115 };
116
117 static const struct usb_device_descriptor device = {
118         .bLength = USB_DT_DEVICE_SIZE,
119         .bDescriptorType = USB_DT_DEVICE,
120         .bcdUSB = 0x0200,
121         .bDeviceClass = 0xFF,
122         .bDeviceSubClass = 0,
123         .bDeviceProtocol = 0,
124         .bMaxPacketSize0 = 64,
125         .idVendor = 0x4242,
126         .idProduct = 0x0007,
127         .bcdDevice = 0x0000,
128         .iManufacturer = STR_MANUFACTURER,
129         .iProduct = STR_PRODUCT,
130         .iSerialNumber = STR_SERIAL,
131         .bNumConfigurations = 1,
132 };
133
134 static const struct usb_endpoint_descriptor endpoints[] = {{
135         // Bulk end-point for sending values to the display
136         .bLength = USB_DT_ENDPOINT_SIZE,
137         .bDescriptorType = USB_DT_ENDPOINT,
138         .bEndpointAddress = 0x01,
139         .bmAttributes = USB_ENDPOINT_ATTR_BULK,
140         .wMaxPacketSize = 64,
141         .bInterval = 1,
142 }};
143
144 static const struct usb_interface_descriptor iface = {
145         .bLength = USB_DT_INTERFACE_SIZE,
146         .bDescriptorType = USB_DT_INTERFACE,
147         .bInterfaceNumber = 0,
148         .bAlternateSetting = 0,
149         .bNumEndpoints = 1,
150         .bInterfaceClass = 0xFF,
151         .bInterfaceSubClass = 0,
152         .bInterfaceProtocol = 0,
153         .iInterface = 0,
154         .endpoint = endpoints,
155 };
156
157 static const struct usb_dfu_descriptor dfu_function = {
158         .bLength = sizeof(struct usb_dfu_descriptor),
159         .bDescriptorType = DFU_FUNCTIONAL,
160         .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
161         .wDetachTimeout = 255,
162         .wTransferSize = 1024,
163         .bcdDFUVersion = 0x0100,
164 };
165
166 static const struct usb_interface_descriptor dfu_iface = {
167         .bLength = USB_DT_INTERFACE_SIZE,
168         .bDescriptorType = USB_DT_INTERFACE,
169         .bInterfaceNumber = 1,
170         .bAlternateSetting = 0,
171         .bNumEndpoints = 0,
172         .bInterfaceClass = 0xFE,
173         .bInterfaceSubClass = 1,
174         .bInterfaceProtocol = 1,
175         .iInterface = 0,
176
177         .extra = &dfu_function,
178         .extralen = sizeof(dfu_function),
179 };
180
181 static const struct usb_interface ifaces[] = {{
182         .num_altsetting = 1,
183         .altsetting = &iface,
184 }, {
185         .num_altsetting = 1,
186         .altsetting = &dfu_iface,
187 }};
188
189 static const struct usb_config_descriptor config = {
190         .bLength = USB_DT_CONFIGURATION_SIZE,
191         .bDescriptorType = USB_DT_CONFIGURATION,
192         .wTotalLength = 0,
193         .bNumInterfaces = 2,
194         .bConfigurationValue = 1,
195         .iConfiguration = 0,
196         .bmAttributes = 0x80,
197         .bMaxPower = 50,        // multiplied by 2 mA
198         .interface = ifaces,
199 };
200
201 static byte usb_configured;
202 static uint8_t usbd_control_buffer[64];
203
204 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
205 {
206         // Reset to bootloader, which implements the rest of DFU
207         debug_printf("Switching to DFU\n");
208         debug_flush();
209         scb_reset_core();
210 }
211
212 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
213         struct usb_setup_data *req,
214         uint8_t **buf UNUSED,
215         uint16_t *len UNUSED,
216         void (**complete)(usbd_device *dev, struct usb_setup_data *req))
217 {
218         if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
219                 return USBD_REQ_NOTSUPP;
220
221         *complete = dfu_detach_complete;
222         return USBD_REQ_HANDLED;
223 }
224
225 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
226 {
227         // We received a frame from the USB host
228         byte buf[8];
229         uint len = usbd_ep_read_packet(dev, 0x01, buf, 8);
230         debug_printf("USB: Host sent %u bytes\n", len);
231 }
232
233 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
234 {
235         usbd_register_control_callback(
236                 dev,
237                 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
238                 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
239                 dfu_control_cb);
240         usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
241         usb_configured = 1;
242 }
243
244 static void reset_cb(void)
245 {
246         debug_printf("USB: Reset\n");
247         usb_configured = 0;
248 }
249
250 static volatile bool usb_event_pending;
251
252 void usb_lp_can_rx0_isr(void)
253 {
254         /*
255          *  We handle USB in the main loop to avoid race conditions between
256          *  USB interrupts and other code. However, we need an interrupt to
257          *  up the main loop from sleep.
258          *
259          *  We set up only the low-priority ISR, because high-priority ISR handles
260          *  only double-buffered bulk transfers and isochronous transfers.
261          */
262         nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
263         usb_event_pending = 1;
264 }
265
266 static void usb_init(void)
267 {
268         // Simulate USB disconnect
269         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
270         gpio_clear(GPIOA, GPIO11 | GPIO12);
271         delay_ms(100);
272
273         usbd_dev = usbd_init(
274                 &st_usbfs_v1_usb_driver,
275                 &device,
276                 &config,
277                 usb_strings,
278                 ARRAY_SIZE(usb_strings),
279                 usbd_control_buffer,
280                 sizeof(usbd_control_buffer)
281         );
282         usbd_register_reset_callback(usbd_dev, reset_cb);
283         usbd_register_set_config_callback(usbd_dev, set_config_cb);
284         usb_event_pending = 1;
285 }
286
287 /*** Main ***/
288
289 int main(void)
290 {
291         clock_init();
292         gpio_init();
293         usart_init();
294
295         tick_init();
296         desig_get_unique_id_as_dfu(usb_serial_number);
297
298         debug_printf("Hello, world!\n");
299
300         usb_init();
301
302         u32 last_blink = 0;
303         uint csense[2] = { 0, 0 };
304
305         for (;;) {
306                 if (ms_ticks - last_blink >= 1000) {
307                         debug_led_toggle();
308                         last_blink = ms_ticks;
309                         debug_printf("C: %u %u\n", csense[0], csense[1]);
310                         csense[0] = csense[1] = 0;
311                 }
312
313                 csense[gpio_get(GPIOA, GPIO0)]++;
314
315                 if (usart_get_flag(USART1, USART_SR_RXNE)) {
316                         uint ch = usart_recv(USART1);
317                         if (ch == '1') {
318                                 gpio_clear(GPIOB, GPIO6);
319                         } else if (ch == '0') {
320                                 gpio_set(GPIOB, GPIO6);
321                         } else {
322                                 debug_putc(ch);
323                         }
324                 }
325
326                 if (usb_event_pending) {
327                         usbd_poll(usbd_dev);
328                         usb_event_pending = 0;
329                         nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
330                         nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
331                 }
332
333                 wait_for_interrupt();
334         }
335
336         return 0;
337 }