2 * Testing Shutter Controller
4 * (c) 2023 Martin Mareš <mj@ucw.cz>
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>
24 /*** Hardware init ***/
26 static void clock_init(void)
28 rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
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);
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);
44 static void gpio_init(void)
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);
51 // PC13 = BluePill LED
52 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
53 gpio_clear(GPIOC, GPIO13);
55 // PB6, PB7 = SSR (active low)
56 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO6 | GPIO7);
57 gpio_set(GPIOB, GPIO6 | GPIO7);
59 // PA0 = current sense
60 gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO0);
61 gpio_set(GPIOA, GPIO0);
64 static void usart_init(void)
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);
76 /*** System ticks ***/
78 static volatile u32 ms_ticks;
80 void sys_tick_handler(void)
85 static void tick_init(void)
87 systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000);
88 systick_counter_enable();
89 systick_interrupt_enable();
92 static void delay_ms(uint ms)
94 u32 start_ticks = ms_ticks;
95 while (ms_ticks - start_ticks < ms)
101 static usbd_device *usbd_dev;
104 STR_MANUFACTURER = 1,
109 static char usb_serial_number[13];
111 static const char *usb_strings[] = {
112 "United Computer Wizards",
113 "Shutter Control Test",
117 static const struct usb_device_descriptor device = {
118 .bLength = USB_DT_DEVICE_SIZE,
119 .bDescriptorType = USB_DT_DEVICE,
121 .bDeviceClass = 0xFF,
122 .bDeviceSubClass = 0,
123 .bDeviceProtocol = 0,
124 .bMaxPacketSize0 = 64,
128 .iManufacturer = STR_MANUFACTURER,
129 .iProduct = STR_PRODUCT,
130 .iSerialNumber = STR_SERIAL,
131 .bNumConfigurations = 1,
134 static const struct usb_interface_descriptor iface = {
135 .bLength = USB_DT_INTERFACE_SIZE,
136 .bDescriptorType = USB_DT_INTERFACE,
137 .bInterfaceNumber = 0,
138 .bAlternateSetting = 0,
140 .bInterfaceClass = 0xFF,
141 .bInterfaceSubClass = 0,
142 .bInterfaceProtocol = 0,
147 static const struct usb_dfu_descriptor dfu_function = {
148 .bLength = sizeof(struct usb_dfu_descriptor),
149 .bDescriptorType = DFU_FUNCTIONAL,
150 .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
151 .wDetachTimeout = 255,
152 .wTransferSize = 1024,
153 .bcdDFUVersion = 0x0100,
156 static const struct usb_interface_descriptor dfu_iface = {
157 .bLength = USB_DT_INTERFACE_SIZE,
158 .bDescriptorType = USB_DT_INTERFACE,
159 .bInterfaceNumber = 1,
160 .bAlternateSetting = 0,
162 .bInterfaceClass = 0xFE,
163 .bInterfaceSubClass = 1,
164 .bInterfaceProtocol = 1,
167 .extra = &dfu_function,
168 .extralen = sizeof(dfu_function),
171 static const struct usb_interface ifaces[] = {{
173 .altsetting = &iface,
176 .altsetting = &dfu_iface,
179 static const struct usb_config_descriptor config = {
180 .bLength = USB_DT_CONFIGURATION_SIZE,
181 .bDescriptorType = USB_DT_CONFIGURATION,
184 .bConfigurationValue = 1,
186 .bmAttributes = 0x80,
187 .bMaxPower = 50, // multiplied by 2 mA
191 static byte usb_configured;
192 static uint8_t usbd_control_buffer[64];
194 uint csense_counters[2] = { 0, 0 };
196 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
198 // Reset to bootloader, which implements the rest of DFU
199 debug_printf("Switching to DFU\n");
204 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
205 struct usb_setup_data *req,
206 uint8_t **buf UNUSED,
207 uint16_t *len UNUSED,
208 void (**complete)(usbd_device *dev, struct usb_setup_data *req))
210 if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
211 return USBD_REQ_NOTSUPP;
213 *complete = dfu_detach_complete;
214 return USBD_REQ_HANDLED;
217 static enum usbd_request_return_codes control_cb(
218 usbd_device *dev UNUSED,
219 struct usb_setup_data *req,
222 void (**complete)(usbd_device *dev, struct usb_setup_data *req) UNUSED)
224 uint index = req->wIndex;
225 uint value = req->wValue;
227 if (req->bmRequestType == (USB_REQ_TYPE_IN | USB_REQ_TYPE_VENDOR | USB_REQ_TYPE_DEVICE)) {
228 debug_printf("Control request IN %02x (index=%d, len=%d)\n", req->bRequest, index, *len);
230 const byte *reply = NULL;
233 switch (req->bRequest) {
235 reply = (const byte *) &csense_counters;
236 reply_len = sizeof(csense_counters);
239 return USBD_REQ_NOTSUPP;
242 uint n = MIN(*len, reply_len);
243 memcpy(*buf, reply, n);
245 return USBD_REQ_HANDLED;
246 } else if (req->bmRequestType == (USB_REQ_TYPE_OUT | USB_REQ_TYPE_VENDOR | USB_REQ_TYPE_DEVICE)) {
247 debug_printf("Control request OUT %02x (index=%d, val=%d, len=%d)\n", req->bRequest, index, value, *len);
249 switch (req->bRequest) {
252 return USBD_REQ_NOTSUPP;
254 gpio_clear(GPIOB, GPIO6);
256 gpio_set(GPIOB, GPIO6);
258 gpio_clear(GPIOB, GPIO7);
260 gpio_set(GPIOB, GPIO7);
263 return USBD_REQ_NOTSUPP;
266 return USBD_REQ_HANDLED;
268 return USBD_REQ_NOTSUPP;
272 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
274 usbd_register_control_callback(
279 usbd_register_control_callback(
281 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
282 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
287 static void reset_cb(void)
289 debug_printf("USB: Reset\n");
293 static volatile bool usb_event_pending;
295 void usb_lp_can_rx0_isr(void)
298 * We handle USB in the main loop to avoid race conditions between
299 * USB interrupts and other code. However, we need an interrupt to
300 * up the main loop from sleep.
302 * We set up only the low-priority ISR, because high-priority ISR handles
303 * only double-buffered bulk transfers and isochronous transfers.
305 nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
306 usb_event_pending = 1;
309 static void usb_init(void)
311 // Simulate USB disconnect
312 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
313 gpio_clear(GPIOA, GPIO11 | GPIO12);
316 usbd_dev = usbd_init(
317 &st_usbfs_v1_usb_driver,
321 ARRAY_SIZE(usb_strings),
323 sizeof(usbd_control_buffer)
325 usbd_register_reset_callback(usbd_dev, reset_cb);
326 usbd_register_set_config_callback(usbd_dev, set_config_cb);
327 usb_event_pending = 1;
339 desig_get_unique_id_as_dfu(usb_serial_number);
341 debug_printf("Hello, world!\n");
348 if (ms_ticks - last_blink >= 1000) {
350 last_blink = ms_ticks;
351 debug_printf("C: %u %u\n", csense_counters[0], csense_counters[1]);
354 csense_counters[gpio_get(GPIOA, GPIO0)]++;
356 if (usart_get_flag(USART1, USART_SR_RXNE)) {
357 uint ch = usart_recv(USART1);
359 gpio_clear(GPIOB, GPIO6);
360 } else if (ch == '0') {
361 gpio_set(GPIOB, GPIO6);
367 if (usb_event_pending) {
369 usb_event_pending = 0;
370 nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
371 nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
374 wait_for_interrupt();