]> mj.ucw.cz Git - home-hw.git/blob - test-display/main.c
Merge branch 'master' of ssh://git.ucw.cz/home/mj/GIT/home-hw
[home-hw.git] / test-display / main.c
1 /*
2  *      Test Gadget
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         // PB7 = SDA for display controller
56         // PB6 = SCL for display controller
57         gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO6 | GPIO7);
58 }
59
60 static void usart_init(void)
61 {
62         usart_set_baudrate(USART1, 115200);
63         usart_set_databits(USART1, 8);
64         usart_set_stopbits(USART1, USART_STOPBITS_1);
65         usart_set_mode(USART1, USART_MODE_TX);
66         usart_set_parity(USART1, USART_PARITY_NONE);
67         usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
68
69         usart_enable(USART1);
70 }
71
72 /*** System ticks ***/
73
74 static volatile u32 ms_ticks;
75
76 void sys_tick_handler(void)
77 {
78         ms_ticks++;
79 }
80
81 static void tick_init(void)
82 {
83         systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000);
84         systick_counter_enable();
85         systick_interrupt_enable();
86 }
87
88 static void delay_ms(uint ms)
89 {
90         u32 start_ticks = ms_ticks;
91         while (ms_ticks - start_ticks < ms)
92                 ;
93 }
94
95 /*** Display ***/
96
97 /*
98  *      Display digits:
99  *
100  *                 ---- 40 ----
101  *               |              |
102  *               |              |
103  *              20              80
104  *               |              |
105  *               |              |
106  *                 ---- 10 ----
107  *               |              |
108  *               |              |
109  *              08              02
110  *               |              |
111  *               |              |
112  *                 ---- 04 ----
113  *                                      (01)
114  */
115
116 static byte disp[4];
117 static byte ctrl = 0x56;
118
119 static void display_update(void)
120 {
121         debug_puts("Display pdate\n");
122         byte cmds[4];
123         cmds[0] = 0;
124         cmds[1] = ctrl;
125         cmds[2] = ((disp[1] & 0x88) >> 3) | ((disp[1] & 0x44) >> 1) | ((disp[1] & 0x22) << 1) | ((disp[1] & 0x11) << 3);
126         cmds[3] = disp[0];
127         i2c_transfer7(I2C1, 0x76/2, (byte *) cmds, sizeof(cmds), NULL, 0);
128
129         cmds[2] = ((disp[3] & 0x88) >> 3) | ((disp[3] & 0x44) >> 1) | ((disp[3] & 0x22) << 1) | ((disp[3] & 0x11) << 3);
130         cmds[3] = disp[2];
131         i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
132
133         debug_puts("Update done\n");
134 }
135
136 static void display_init(void)
137 {
138         debug_puts("I2C init\n");
139         i2c_peripheral_disable(I2C1);
140         i2c_set_speed(I2C1, i2c_speed_sm_100k, rcc_apb1_frequency / 1000000);
141         i2c_peripheral_enable(I2C1);
142
143         disp[0] = 0x82;
144         disp[1] = 0xdc;
145         disp[2] = 0xd6;
146         disp[3] = 0xb2;
147         display_update();
148 }
149
150 static void display_test(void)
151 {
152         static byte mode;
153
154         disp[0] ^= 0x01;
155         display_update();
156
157 #if 0
158         byte cmds[] = { 0x00, mode ? 0x77 : 0x77 };
159         i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
160 #endif
161
162 #if 0
163         byte disp[] = { 0xff, 0xff, mode ? 0xff : 0x00, mode ? 0xff : 0xff };
164         byte cmds[] = { 0x00, 0x77, 0, 0, 0, 0 };
165         cmds[2] = (disp[0] & 0xf0) | (disp[2] >> 4);
166         cmds[3] = (disp[1] & 0xf0) | (disp[3] >> 4);
167         cmds[4] = (disp[2] & 0x0f) | (disp[0] << 4);
168         cmds[5] = (disp[3] & 0x0f) | (disp[1] << 4);
169         i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
170 #endif
171
172         mode = !mode;
173 }
174
175 static const byte lcd_font[] = {
176         [0] = 0xee,
177         [1] = 0x82,
178         [2] = 0xdc,
179         [3] = 0xd6,
180         [4] = 0xb2,
181         [5] = 0x76,
182         [6] = 0x7e,
183         [7] = 0xc2,
184         [8] = 0xfe,
185         [9] = 0xf6,
186         [10] = 0xea,
187         [11] = 0x3e,
188         [12] = 0x6c,
189         [13] = 0x9e,
190         [14] = 0x7c,
191         [15] = 0x78,
192 };
193
194 /*** USB ***/
195
196 static usbd_device *usbd_dev;
197
198 enum usb_string {
199         STR_MANUFACTURER = 1,
200         STR_PRODUCT,
201         STR_SERIAL,
202 };
203
204 static char usb_serial_number[13];
205
206 static const char *usb_strings[] = {
207         "United Computer Wizards",
208         "Test Gadget",
209         usb_serial_number,
210 };
211
212 static const struct usb_device_descriptor device = {
213         .bLength = USB_DT_DEVICE_SIZE,
214         .bDescriptorType = USB_DT_DEVICE,
215         .bcdUSB = 0x0200,
216         .bDeviceClass = 0xFF,
217         .bDeviceSubClass = 0,
218         .bDeviceProtocol = 0,
219         .bMaxPacketSize0 = 64,
220         .idVendor = 0x4242,
221         .idProduct = 0x0007,
222         .bcdDevice = 0x0000,
223         .iManufacturer = STR_MANUFACTURER,
224         .iProduct = STR_PRODUCT,
225         .iSerialNumber = STR_SERIAL,
226         .bNumConfigurations = 1,
227 };
228
229 static const struct usb_endpoint_descriptor endpoints[] = {{
230         // Bulk end-point for sending values to DMX
231         .bLength = USB_DT_ENDPOINT_SIZE,
232         .bDescriptorType = USB_DT_ENDPOINT,
233         .bEndpointAddress = 0x01,
234         .bmAttributes = USB_ENDPOINT_ATTR_BULK,
235         .wMaxPacketSize = 64,
236         .bInterval = 1,
237 }};
238
239 static const struct usb_interface_descriptor iface = {
240         .bLength = USB_DT_INTERFACE_SIZE,
241         .bDescriptorType = USB_DT_INTERFACE,
242         .bInterfaceNumber = 0,
243         .bAlternateSetting = 0,
244         .bNumEndpoints = 1,
245         .bInterfaceClass = 0xFF,
246         .bInterfaceSubClass = 0,
247         .bInterfaceProtocol = 0,
248         .iInterface = 0,
249         .endpoint = endpoints,
250 };
251
252 static const struct usb_dfu_descriptor dfu_function = {
253         .bLength = sizeof(struct usb_dfu_descriptor),
254         .bDescriptorType = DFU_FUNCTIONAL,
255         .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
256         .wDetachTimeout = 255,
257         .wTransferSize = 1024,
258         .bcdDFUVersion = 0x0100,
259 };
260
261 static const struct usb_interface_descriptor dfu_iface = {
262         .bLength = USB_DT_INTERFACE_SIZE,
263         .bDescriptorType = USB_DT_INTERFACE,
264         .bInterfaceNumber = 1,
265         .bAlternateSetting = 0,
266         .bNumEndpoints = 0,
267         .bInterfaceClass = 0xFE,
268         .bInterfaceSubClass = 1,
269         .bInterfaceProtocol = 1,
270         .iInterface = 0,
271
272         .extra = &dfu_function,
273         .extralen = sizeof(dfu_function),
274 };
275
276 static const struct usb_interface ifaces[] = {{
277         .num_altsetting = 1,
278         .altsetting = &iface,
279 }, {
280         .num_altsetting = 1,
281         .altsetting = &dfu_iface,
282 }};
283
284 static const struct usb_config_descriptor config = {
285         .bLength = USB_DT_CONFIGURATION_SIZE,
286         .bDescriptorType = USB_DT_CONFIGURATION,
287         .wTotalLength = 0,
288         .bNumInterfaces = 2,
289         .bConfigurationValue = 1,
290         .iConfiguration = 0,
291         .bmAttributes = 0x80,
292         .bMaxPower = 50,        // multiplied by 2 mA
293         .interface = ifaces,
294 };
295
296 static byte usb_configured;
297 static uint8_t usbd_control_buffer[64];
298
299 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
300 {
301         // Reset to bootloader, which implements the rest of DFU
302         debug_printf("Switching to DFU\n");
303         debug_flush();
304         scb_reset_core();
305 }
306
307 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
308         struct usb_setup_data *req,
309         uint8_t **buf UNUSED,
310         uint16_t *len UNUSED,
311         void (**complete)(usbd_device *dev, struct usb_setup_data *req))
312 {
313         if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
314                 return USBD_REQ_NOTSUPP;
315
316         *complete = dfu_detach_complete;
317         return USBD_REQ_HANDLED;
318 }
319
320 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
321 {
322         // We received a frame from the USB host
323         byte buf[8];
324         uint len = usbd_ep_read_packet(dev, 0x01, buf, 8);
325         debug_printf("USB: Host sent %u bytes\n", len);
326         if (len >= 5) {
327                 for (uint i=0; i<4; i++) {
328                         if (buf[i] < 16)
329                                 disp[i] = lcd_font[buf[i]];
330                         else
331                                 disp[i] = 0;
332                 }
333                 if (buf[4])
334                         disp[1] |= 1;
335                 display_update();
336         }
337 }
338
339 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
340 {
341         usbd_register_control_callback(
342                 dev,
343                 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
344                 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
345                 dfu_control_cb);
346         usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
347         usb_configured = 1;
348 }
349
350 static void reset_cb(void)
351 {
352         debug_printf("USB: Reset\n");
353         usb_configured = 0;
354 }
355
356 static volatile bool usb_event_pending;
357
358 void usb_lp_can_rx0_isr(void)
359 {
360         /*
361          *  We handle USB in the main loop to avoid race conditions between
362          *  USB interrupts and other code. However, we need an interrupt to
363          *  up the main loop from sleep.
364          *
365          *  We set up only the low-priority ISR, because high-priority ISR handles
366          *  only double-buffered bulk transfers and isochronous transfers.
367          */
368         nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
369         usb_event_pending = 1;
370 }
371
372 static void usb_init(void)
373 {
374         // Simulate USB disconnect
375         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
376         gpio_clear(GPIOA, GPIO11 | GPIO12);
377         delay_ms(100);
378
379         usbd_dev = usbd_init(
380                 &st_usbfs_v1_usb_driver,
381                 &device,
382                 &config,
383                 usb_strings,
384                 ARRAY_SIZE(usb_strings),
385                 usbd_control_buffer,
386                 sizeof(usbd_control_buffer)
387         );
388         usbd_register_reset_callback(usbd_dev, reset_cb);
389         usbd_register_set_config_callback(usbd_dev, set_config_cb);
390         usb_event_pending = 1;
391 }
392
393 /*** Main ***/
394
395 int main(void)
396 {
397         clock_init();
398         gpio_init();
399         tick_init();
400         usart_init();
401         desig_get_unique_id_as_dfu(usb_serial_number);
402
403         debug_printf("Hello, world!\n");
404
405         usb_init();
406         display_init();
407
408         u32 last_blink = 0;
409
410         for (;;) {
411                 if (ms_ticks - last_blink >= 500) {
412                         debug_led_toggle();
413                         last_blink = ms_ticks;
414                         display_test();
415                 }
416
417                 if (usb_event_pending) {
418                         usbd_poll(usbd_dev);
419                         usb_event_pending = 0;
420                         nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
421                         nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
422                 }
423
424                 wait_for_interrupt();
425         }
426
427         return 0;
428 }