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