4 * (c) 2018--2019 Martin Mareš <mj@ucw.cz>
11 #include <libopencm3/cm3/nvic.h>
12 #include <libopencm3/cm3/systick.h>
13 #include <libopencm3/stm32/rcc.h>
14 #include <libopencm3/stm32/gpio.h>
15 #include <libopencm3/stm32/timer.h>
16 #include <libopencm3/stm32/usart.h>
20 static void clock_init(void)
22 rcc_clock_setup_in_hse_8mhz_out_72mhz();
24 rcc_periph_clock_enable(RCC_GPIOB);
25 rcc_periph_clock_enable(RCC_GPIOC);
26 rcc_periph_clock_enable(RCC_USART1);
27 rcc_periph_clock_enable(RCC_USB);
29 rcc_periph_reset_pulse(RST_GPIOB);
30 rcc_periph_reset_pulse(RST_GPIOC);
31 rcc_periph_reset_pulse(RST_USART1);
32 rcc_periph_reset_pulse(RST_USB);
35 static void gpio_init(void)
37 // Switch JTAG off to free up pins
38 gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON, 0);
40 // PA9 = TXD1 for debugging console
41 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
43 // PA10 = RXD1 for debugging console
44 gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
46 // PC13 = BluePill LED
47 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
48 gpio_clear(GPIOC, GPIO13);
51 static volatile u32 ms_ticks;
53 void sys_tick_handler(void)
58 static void tick_init(void)
60 systick_set_frequency(1000, 72000000);
61 systick_counter_enable();
62 systick_interrupt_enable();
66 static void delay_ms(uint ms)
68 u32 start_ticks = ms_ticks;
69 while (ms_ticks - start_ticks < ms)
74 static void usart_init(void)
76 usart_set_baudrate(USART1, 115200);
77 usart_set_databits(USART1, 8);
78 usart_set_stopbits(USART1, USART_STOPBITS_1);
79 usart_set_mode(USART1, USART_MODE_TX_RX);
80 usart_set_parity(USART1, USART_PARITY_NONE);
81 usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
86 static const struct usb_device_descriptor dev = {
87 .bLength = USB_DT_DEVICE_SIZE,
88 .bDescriptorType = USB_DT_DEVICE,
93 .bMaxPacketSize0 = 64,
100 .bNumConfigurations = 1,
103 static const struct usb_endpoint_descriptor endpoints[] = {{
104 .bLength = USB_DT_ENDPOINT_SIZE,
105 .bDescriptorType = USB_DT_ENDPOINT,
106 .bEndpointAddress = 0x81,
107 .bmAttributes = USB_ENDPOINT_ATTR_BULK,
108 .wMaxPacketSize = 64,
112 static const struct usb_interface_descriptor iface = {
113 .bLength = USB_DT_INTERFACE_SIZE,
114 .bDescriptorType = USB_DT_INTERFACE,
115 .bInterfaceNumber = 0,
116 .bAlternateSetting = 0,
118 .bInterfaceClass = 0xFF,
119 .bInterfaceSubClass = 0,
120 .bInterfaceProtocol = 0,
122 .endpoint = endpoints,
125 static const struct usb_interface ifaces[] = {{
127 .altsetting = &iface,
130 static const struct usb_config_descriptor config = {
131 .bLength = USB_DT_CONFIGURATION_SIZE,
132 .bDescriptorType = USB_DT_CONFIGURATION,
135 .bConfigurationValue = 1,
137 .bmAttributes = 0x80,
138 .bMaxPower = 50, // multiplied by 2 mA
142 static const char *usb_strings[] = {
143 "Hippo Computing Inc.",
144 "Space Alert Thermometer",
148 uint8_t usbd_control_buffer[64];
150 static void ep81_cb(usbd_device *usbd_dev, uint8_t ep UNUSED)
153 if (ds_sensors[0].address[0] && ds_sensors[0].current_temp != DS_TEMP_UNKNOWN) {
154 put_u32_be(buf, (u32) ds_sensors[0].current_temp);
156 put_u32_be(buf, 0x80000000);
158 usbd_ep_write_packet(usbd_dev, 0x81, buf, sizeof(buf));
161 static void set_config_cb(usbd_device *usbd_dev, uint16_t wValue UNUSED)
163 usbd_ep_setup(usbd_dev, 0x81, USB_ENDPOINT_ATTR_BULK, 64, ep81_cb);
164 ep81_cb(usbd_dev, 0);
174 // Simulate USB disconnect
175 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
176 gpio_clear(GPIOA, GPIO11 | GPIO12);
179 usbd_device *usbd_dev = usbd_init(&st_usbfs_v1_usb_driver, &dev, &config, usb_strings, 4, usbd_control_buffer, sizeof(usbd_control_buffer));
180 usbd_register_set_config_callback(usbd_dev, set_config_cb);
181 u32 last_ds_step = 0;
184 if (ms_ticks - last_ds_step >= 100) {
187 last_ds_step = ms_ticks;
190 // XXX: libopencm3 usbd does not use interrupts at the moment, need to poll...
191 // wait_for_interrupt();