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