]> mj.ucw.cz Git - home-hw.git/blob - test-display2/main.c
3c8049869c7abefc182d595129327af8507d7f2c
[home-hw.git] / test-display2 / main.c
1 /*
2  *      Workshop Clock
3  *
4  *      (c) 2020-2023 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/stm32/timer.h>
19 #include <libopencm3/usb/dfu.h>
20 #include <libopencm3/usb/usbd.h>
21
22 #include <string.h>
23
24 /*** Hardware init ***/
25
26 static void clock_init(void)
27 {
28         rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
29
30         rcc_periph_clock_enable(RCC_GPIOA);
31         rcc_periph_clock_enable(RCC_GPIOB);
32         rcc_periph_clock_enable(RCC_GPIOC);
33         rcc_periph_clock_enable(RCC_I2C1);
34         rcc_periph_clock_enable(RCC_USART1);
35         rcc_periph_clock_enable(RCC_USB);
36         rcc_periph_clock_enable(RCC_TIM1);
37
38         rcc_periph_reset_pulse(RST_GPIOA);
39         rcc_periph_reset_pulse(RST_GPIOB);
40         rcc_periph_reset_pulse(RST_GPIOC);
41         rcc_periph_reset_pulse(RST_I2C1);
42         rcc_periph_reset_pulse(RST_USART1);
43         rcc_periph_reset_pulse(RST_USB);
44         rcc_periph_reset_pulse(RST_TIM1);
45 }
46
47 static void gpio_init(void)
48 {
49         // PA9 = TXD1 for debugging console
50         // PA10 = RXD1 for debugging console
51         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
52         gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
53
54         // PC13 = BluePill LED
55         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
56         gpio_clear(GPIOC, GPIO13);
57
58         // PB7 = SDA for display controller
59         // PB6 = SCL for display controller
60         gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO6 | GPIO7);
61
62         // PA8 = SFH5110 output (5V tolerant) connected to TIM1_CH1
63         gpio_set_mode(GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO8);
64 }
65
66 static void usart_init(void)
67 {
68         usart_set_baudrate(USART1, 115200);
69         usart_set_databits(USART1, 8);
70         usart_set_stopbits(USART1, USART_STOPBITS_1);
71         usart_set_mode(USART1, USART_MODE_TX);
72         usart_set_parity(USART1, USART_PARITY_NONE);
73         usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
74
75         usart_enable(USART1);
76 }
77
78 /*** System ticks ***/
79
80 static volatile u32 ms_ticks;
81
82 void sys_tick_handler(void)
83 {
84         ms_ticks++;
85 }
86
87 static void tick_init(void)
88 {
89         systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000);
90         systick_counter_enable();
91         systick_interrupt_enable();
92 }
93
94 static void delay_ms(uint ms)
95 {
96         u32 start_ticks = ms_ticks;
97         while (ms_ticks - start_ticks < ms)
98                 ;
99 }
100
101 /*** Display ***/
102
103 /*
104  *      Display digits:
105  *
106  *                 ---- 40 ----
107  *               |              |
108  *               |              |
109  *              20              80
110  *               |              |
111  *               |              |
112  *                 ---- 10 ----
113  *               |              |
114  *               |              |
115  *              08              02
116  *               |              |
117  *               |              |
118  *                 ---- 04 ----
119  *                                      (01)
120  */
121
122 static byte disp[4];
123 static byte ctrl = 0x56;
124
125 static void display_update(void)
126 {
127         // debug_puts("Display update\n");
128         byte cmds[4];
129         cmds[0] = 0;
130         cmds[1] = ctrl;
131         cmds[2] = ((disp[1] & 0x88) >> 3) | ((disp[1] & 0x44) >> 1) | ((disp[1] & 0x22) << 1) | ((disp[1] & 0x11) << 3);
132         cmds[3] = disp[0];
133         i2c_transfer7(I2C1, 0x76/2, (byte *) cmds, sizeof(cmds), NULL, 0);
134
135         cmds[2] = ((disp[3] & 0x88) >> 3) | ((disp[3] & 0x44) >> 1) | ((disp[3] & 0x22) << 1) | ((disp[3] & 0x11) << 3);
136         cmds[3] = disp[2];
137         i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
138
139         // debug_puts("Update done\n");
140 }
141
142 static void display_init(void)
143 {
144         debug_puts("I2C init\n");
145         i2c_peripheral_disable(I2C1);
146         i2c_set_speed(I2C1, i2c_speed_sm_100k, rcc_apb1_frequency / 1000000);
147         i2c_peripheral_enable(I2C1);
148
149         disp[0] = 0x82;
150         disp[1] = 0xdc;
151         disp[2] = 0xd6;
152         disp[3] = 0xb2;
153         display_update();
154 }
155
156 static void display_test(void)
157 {
158         static byte mode;
159
160         disp[0] ^= 0x01;
161         display_update();
162
163 #if 0
164         byte cmds[] = { 0x00, mode ? 0x77 : 0x77 };
165         i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
166 #endif
167
168 #if 0
169         byte disp[] = { 0xff, 0xff, mode ? 0xff : 0x00, mode ? 0xff : 0xff };
170         byte cmds[] = { 0x00, 0x77, 0, 0, 0, 0 };
171         cmds[2] = (disp[0] & 0xf0) | (disp[2] >> 4);
172         cmds[3] = (disp[1] & 0xf0) | (disp[3] >> 4);
173         cmds[4] = (disp[2] & 0x0f) | (disp[0] << 4);
174         cmds[5] = (disp[3] & 0x0f) | (disp[1] << 4);
175         i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
176 #endif
177
178         mode = !mode;
179 }
180
181 static const byte lcd_font[] = {
182         [0] = 0xee,
183         [1] = 0x82,
184         [2] = 0xdc,
185         [3] = 0xd6,
186         [4] = 0xb2,
187         [5] = 0x76,
188         [6] = 0x7e,
189         [7] = 0xc2,
190         [8] = 0xfe,
191         [9] = 0xf6,
192         [10] = 0xea,
193         [11] = 0x3e,
194         [12] = 0x6c,
195         [13] = 0x9e,
196         [14] = 0x7c,
197         [15] = 0x78,
198 };
199
200 /*** Infrared Remote Control ***/
201
202 static void ir_init(void)
203 {
204         debug_puts("IR init\n");
205
206         timer_set_prescaler(TIM1, 71);          // 72 MHz / 72 = 1 MHz
207         timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
208         timer_set_period(TIM1, 65535);
209         timer_update_on_overflow(TIM1);
210
211         // IC1 will trigger on TI1 (TIM1_CH1) falling edge
212         timer_ic_set_input(TIM1, TIM_IC1, TIM_IC_IN_TI1);
213         // timer_ic_set_filter(TIM1, TIM_IC1, TIM_IC_OFF);
214         timer_set_oc_polarity_low(TIM1, TIM_OC1);       // OC functions affect IC, too
215
216         // IC2 will trigger on TI1 (TIM1_CH1) rising edge
217         timer_ic_set_input(TIM1, TIM_IC2, TIM_IC_IN_TI1);
218         timer_set_oc_polarity_high(TIM1, TIM_OC2);
219
220         // CH3 will trigger on a break longer than 50 ms
221         timer_set_oc_mode(TIM1, TIM_OC3, TIM_OCM_ACTIVE);
222         timer_set_oc_value(TIM1, TIM_OC3, 30000);
223
224         // Program slave controller to reset the timer on IC1
225         timer_slave_set_trigger(TIM1, TIM_SMCR_TS_TI1FP1);
226         timer_slave_set_mode(TIM1, TIM_SMCR_SMS_RM);
227
228         // Request interrupts
229         timer_enable_irq(TIM1, TIM_DIER_CC1IE | TIM_DIER_CC2IE | TIM_DIER_CC3IE);
230         nvic_enable_irq(NVIC_TIM1_CC_IRQ);
231
232         // Enable ICs and OCs
233         timer_enable_oc_output(TIM1, TIM_OC1);
234         timer_enable_oc_output(TIM1, TIM_OC2);
235         timer_enable_oc_output(TIM1, TIM_OC3);
236
237         timer_enable_counter(TIM1);
238 }
239
240 #define IR_MAX_PULSES 64
241 static u16 ir_pulses[IR_MAX_PULSES];
242 static uint ir_num_pulses;
243 static u16 ir_last_pulse;
244
245 void tim1_cc_isr(void)
246 {
247         if (TIM_SR(TIM1) & TIM_SR_CC1IF) {
248                 TIM_SR(TIM1) &= ~TIM_SR_CC1IF;
249                 u16 now = TIM_CCR1(TIM1);
250                 if (ir_last_pulse) {
251                         ir_pulses[ir_num_pulses++] = ir_last_pulse;
252                         ir_pulses[ir_num_pulses++] = now - ir_last_pulse;
253                         ir_last_pulse = 0;
254                 }
255         }
256         if (TIM_SR(TIM1) & TIM_SR_CC2IF) {
257                 TIM_SR(TIM1) &= ~TIM_SR_CC2IF;
258                 ir_last_pulse = TIM_CCR2(TIM1);
259         }
260         if (TIM_SR(TIM1) & TIM_SR_CC3IF) {
261                 TIM_SR(TIM1) &= ~TIM_SR_CC3IF;
262                 if (ir_last_pulse) {
263                         ir_pulses[ir_num_pulses++] = ir_last_pulse;
264                         ir_pulses[ir_num_pulses++] = 0xffff;
265                 }
266                 ir_last_pulse = 0;
267         }
268 }
269
270 /*** USB ***/
271
272 static usbd_device *usbd_dev;
273
274 enum usb_string {
275         STR_MANUFACTURER = 1,
276         STR_PRODUCT,
277         STR_SERIAL,
278 };
279
280 static char usb_serial_number[13];
281
282 static const char *usb_strings[] = {
283         "United Computer Wizards",
284         "Workshop Clock",
285         usb_serial_number,
286 };
287
288 static const struct usb_device_descriptor device = {
289         .bLength = USB_DT_DEVICE_SIZE,
290         .bDescriptorType = USB_DT_DEVICE,
291         .bcdUSB = 0x0200,
292         .bDeviceClass = 0xFF,
293         .bDeviceSubClass = 0,
294         .bDeviceProtocol = 0,
295         .bMaxPacketSize0 = 64,
296         .idVendor = 0x4242,
297         .idProduct = 0x0007,
298         .bcdDevice = 0x0000,
299         .iManufacturer = STR_MANUFACTURER,
300         .iProduct = STR_PRODUCT,
301         .iSerialNumber = STR_SERIAL,
302         .bNumConfigurations = 1,
303 };
304
305 static const struct usb_endpoint_descriptor endpoints[] = {{
306         // Bulk end-point for sending values to the display
307         .bLength = USB_DT_ENDPOINT_SIZE,
308         .bDescriptorType = USB_DT_ENDPOINT,
309         .bEndpointAddress = 0x01,
310         .bmAttributes = USB_ENDPOINT_ATTR_BULK,
311         .wMaxPacketSize = 64,
312         .bInterval = 1,
313 }};
314
315 static const struct usb_interface_descriptor iface = {
316         .bLength = USB_DT_INTERFACE_SIZE,
317         .bDescriptorType = USB_DT_INTERFACE,
318         .bInterfaceNumber = 0,
319         .bAlternateSetting = 0,
320         .bNumEndpoints = 1,
321         .bInterfaceClass = 0xFF,
322         .bInterfaceSubClass = 0,
323         .bInterfaceProtocol = 0,
324         .iInterface = 0,
325         .endpoint = endpoints,
326 };
327
328 static const struct usb_dfu_descriptor dfu_function = {
329         .bLength = sizeof(struct usb_dfu_descriptor),
330         .bDescriptorType = DFU_FUNCTIONAL,
331         .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
332         .wDetachTimeout = 255,
333         .wTransferSize = 1024,
334         .bcdDFUVersion = 0x0100,
335 };
336
337 static const struct usb_interface_descriptor dfu_iface = {
338         .bLength = USB_DT_INTERFACE_SIZE,
339         .bDescriptorType = USB_DT_INTERFACE,
340         .bInterfaceNumber = 1,
341         .bAlternateSetting = 0,
342         .bNumEndpoints = 0,
343         .bInterfaceClass = 0xFE,
344         .bInterfaceSubClass = 1,
345         .bInterfaceProtocol = 1,
346         .iInterface = 0,
347
348         .extra = &dfu_function,
349         .extralen = sizeof(dfu_function),
350 };
351
352 static const struct usb_interface ifaces[] = {{
353         .num_altsetting = 1,
354         .altsetting = &iface,
355 }, {
356         .num_altsetting = 1,
357         .altsetting = &dfu_iface,
358 }};
359
360 static const struct usb_config_descriptor config = {
361         .bLength = USB_DT_CONFIGURATION_SIZE,
362         .bDescriptorType = USB_DT_CONFIGURATION,
363         .wTotalLength = 0,
364         .bNumInterfaces = 2,
365         .bConfigurationValue = 1,
366         .iConfiguration = 0,
367         .bmAttributes = 0x80,
368         .bMaxPower = 50,        // multiplied by 2 mA
369         .interface = ifaces,
370 };
371
372 static byte usb_configured;
373 static uint8_t usbd_control_buffer[64];
374
375 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
376 {
377         // Reset to bootloader, which implements the rest of DFU
378         debug_printf("Switching to DFU\n");
379         debug_flush();
380         scb_reset_core();
381 }
382
383 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
384         struct usb_setup_data *req,
385         uint8_t **buf UNUSED,
386         uint16_t *len UNUSED,
387         void (**complete)(usbd_device *dev, struct usb_setup_data *req))
388 {
389         if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
390                 return USBD_REQ_NOTSUPP;
391
392         *complete = dfu_detach_complete;
393         return USBD_REQ_HANDLED;
394 }
395
396 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
397 {
398         // We received a frame from the USB host
399         byte buf[8];
400         uint len = usbd_ep_read_packet(dev, 0x01, buf, 8);
401         debug_printf("USB: Host sent %u bytes\n", len);
402         if (len >= 5) {
403                 for (uint i=0; i<4; i++) {
404                         if (buf[i] < 16)
405                                 disp[i] = lcd_font[buf[i]];
406                         else
407                                 disp[i] = 0;
408                 }
409                 if (buf[4])
410                         disp[1] |= 1;
411                 display_update();
412         }
413 }
414
415 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
416 {
417         usbd_register_control_callback(
418                 dev,
419                 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
420                 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
421                 dfu_control_cb);
422         usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
423         usb_configured = 1;
424 }
425
426 static void reset_cb(void)
427 {
428         debug_printf("USB: Reset\n");
429         usb_configured = 0;
430 }
431
432 static volatile bool usb_event_pending;
433
434 void usb_lp_can_rx0_isr(void)
435 {
436         /*
437          *  We handle USB in the main loop to avoid race conditions between
438          *  USB interrupts and other code. However, we need an interrupt to
439          *  up the main loop from sleep.
440          *
441          *  We set up only the low-priority ISR, because high-priority ISR handles
442          *  only double-buffered bulk transfers and isochronous transfers.
443          */
444         nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
445         usb_event_pending = 1;
446 }
447
448 static void usb_init(void)
449 {
450         // Simulate USB disconnect
451         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
452         gpio_clear(GPIOA, GPIO11 | GPIO12);
453         delay_ms(100);
454
455         usbd_dev = usbd_init(
456                 &st_usbfs_v1_usb_driver,
457                 &device,
458                 &config,
459                 usb_strings,
460                 ARRAY_SIZE(usb_strings),
461                 usbd_control_buffer,
462                 sizeof(usbd_control_buffer)
463         );
464         usbd_register_reset_callback(usbd_dev, reset_cb);
465         usbd_register_set_config_callback(usbd_dev, set_config_cb);
466         usb_event_pending = 1;
467 }
468
469 /*** Main ***/
470
471 int main(void)
472 {
473         clock_init();
474         gpio_init();
475         usart_init();
476
477         tick_init();
478         desig_get_unique_id_as_dfu(usb_serial_number);
479
480         debug_printf("Hello, world!\n");
481
482         usb_init();
483         display_init();
484         ir_init();
485
486         u32 last_blink = 0;
487
488         for (;;) {
489                 if (ms_ticks - last_blink >= 500) {
490                         debug_led_toggle();
491                         last_blink = ms_ticks;
492                         display_test();
493                 }
494
495                 if (usb_event_pending) {
496                         usbd_poll(usbd_dev);
497                         usb_event_pending = 0;
498                         nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
499                         nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
500                 }
501
502                 static u16 pulses[IR_MAX_PULSES];
503                 uint np;
504
505                 cm_disable_interrupts();
506                 np = ir_num_pulses;
507                 memcpy(pulses, ir_pulses, 2*np);
508                 ir_num_pulses = 0;
509                 cm_enable_interrupts();
510
511                 if (np) {
512                         debug_printf("IR:");
513                         for (uint i=0; i < np; i++)
514                                 debug_printf(" %u", pulses[i]);
515                         debug_putc('\n');
516                 }
517
518                 wait_for_interrupt();
519         }
520
521         return 0;
522 }