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