]> mj.ucw.cz Git - home-hw.git/blob - test-display/main.c
Display test...
[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 /*** USB ***/
174
175 static usbd_device *usbd_dev;
176
177 enum usb_string {
178         STR_MANUFACTURER = 1,
179         STR_PRODUCT,
180         STR_SERIAL,
181 };
182
183 static char usb_serial_number[13];
184
185 static const char *usb_strings[] = {
186         "United Computer Wizards",
187         "Test Gadget",
188         usb_serial_number,
189 };
190
191 static const struct usb_device_descriptor device = {
192         .bLength = USB_DT_DEVICE_SIZE,
193         .bDescriptorType = USB_DT_DEVICE,
194         .bcdUSB = 0x0200,
195         .bDeviceClass = 0xFF,
196         .bDeviceSubClass = 0,
197         .bDeviceProtocol = 0,
198         .bMaxPacketSize0 = 64,
199         .idVendor = 0x4242,
200         .idProduct = 0x0007,
201         .bcdDevice = 0x0000,
202         .iManufacturer = STR_MANUFACTURER,
203         .iProduct = STR_PRODUCT,
204         .iSerialNumber = STR_SERIAL,
205         .bNumConfigurations = 1,
206 };
207
208 static const struct usb_endpoint_descriptor endpoints[] = {{
209         // Bulk end-point for sending values to DMX
210         .bLength = USB_DT_ENDPOINT_SIZE,
211         .bDescriptorType = USB_DT_ENDPOINT,
212         .bEndpointAddress = 0x01,
213         .bmAttributes = USB_ENDPOINT_ATTR_BULK,
214         .wMaxPacketSize = 64,
215         .bInterval = 1,
216 }};
217
218 static const struct usb_interface_descriptor iface = {
219         .bLength = USB_DT_INTERFACE_SIZE,
220         .bDescriptorType = USB_DT_INTERFACE,
221         .bInterfaceNumber = 0,
222         .bAlternateSetting = 0,
223         .bNumEndpoints = 1,
224         .bInterfaceClass = 0xFF,
225         .bInterfaceSubClass = 0,
226         .bInterfaceProtocol = 0,
227         .iInterface = 0,
228         .endpoint = endpoints,
229 };
230
231 static const struct usb_dfu_descriptor dfu_function = {
232         .bLength = sizeof(struct usb_dfu_descriptor),
233         .bDescriptorType = DFU_FUNCTIONAL,
234         .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
235         .wDetachTimeout = 255,
236         .wTransferSize = 1024,
237         .bcdDFUVersion = 0x0100,
238 };
239
240 static const struct usb_interface_descriptor dfu_iface = {
241         .bLength = USB_DT_INTERFACE_SIZE,
242         .bDescriptorType = USB_DT_INTERFACE,
243         .bInterfaceNumber = 1,
244         .bAlternateSetting = 0,
245         .bNumEndpoints = 0,
246         .bInterfaceClass = 0xFE,
247         .bInterfaceSubClass = 1,
248         .bInterfaceProtocol = 1,
249         .iInterface = 0,
250
251         .extra = &dfu_function,
252         .extralen = sizeof(dfu_function),
253 };
254
255 static const struct usb_interface ifaces[] = {{
256         .num_altsetting = 1,
257         .altsetting = &iface,
258 }, {
259         .num_altsetting = 1,
260         .altsetting = &dfu_iface,
261 }};
262
263 static const struct usb_config_descriptor config = {
264         .bLength = USB_DT_CONFIGURATION_SIZE,
265         .bDescriptorType = USB_DT_CONFIGURATION,
266         .wTotalLength = 0,
267         .bNumInterfaces = 2,
268         .bConfigurationValue = 1,
269         .iConfiguration = 0,
270         .bmAttributes = 0x80,
271         .bMaxPower = 50,        // multiplied by 2 mA
272         .interface = ifaces,
273 };
274
275 static byte usb_configured;
276 static uint8_t usbd_control_buffer[64];
277
278 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
279 {
280         // Reset to bootloader, which implements the rest of DFU
281         debug_printf("Switching to DFU\n");
282         debug_flush();
283         scb_reset_core();
284 }
285
286 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
287         struct usb_setup_data *req,
288         uint8_t **buf UNUSED,
289         uint16_t *len UNUSED,
290         void (**complete)(usbd_device *dev, struct usb_setup_data *req))
291 {
292         if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
293                 return USBD_REQ_NOTSUPP;
294
295         *complete = dfu_detach_complete;
296         return USBD_REQ_HANDLED;
297 }
298
299 # if 0
300 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
301 {
302         // We received a frame from the USB host
303         uint len = usbd_ep_read_packet(dev, 0x01, dmx_next_packet, DMX_MAX_PACKET_SIZE);
304         debug_printf("USB: Host sent %u bytes\n", len);
305 }
306 #endif
307
308 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
309 {
310         usbd_register_control_callback(
311                 dev,
312                 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
313                 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
314                 dfu_control_cb);
315         // usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
316         usb_configured = 1;
317 }
318
319 static void reset_cb(void)
320 {
321         debug_printf("USB: Reset\n");
322         usb_configured = 0;
323 }
324
325 static volatile bool usb_event_pending;
326
327 void usb_lp_can_rx0_isr(void)
328 {
329         /*
330          *  We handle USB in the main loop to avoid race conditions between
331          *  USB interrupts and other code. However, we need an interrupt to
332          *  up the main loop from sleep.
333          *
334          *  We set up only the low-priority ISR, because high-priority ISR handles
335          *  only double-buffered bulk transfers and isochronous transfers.
336          */
337         nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
338         usb_event_pending = 1;
339 }
340
341 static void usb_init(void)
342 {
343         // Simulate USB disconnect
344         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
345         gpio_clear(GPIOA, GPIO11 | GPIO12);
346         delay_ms(100);
347
348         usbd_dev = usbd_init(
349                 &st_usbfs_v1_usb_driver,
350                 &device,
351                 &config,
352                 usb_strings,
353                 ARRAY_SIZE(usb_strings),
354                 usbd_control_buffer,
355                 sizeof(usbd_control_buffer)
356         );
357         usbd_register_reset_callback(usbd_dev, reset_cb);
358         usbd_register_set_config_callback(usbd_dev, set_config_cb);
359         usb_event_pending = 1;
360 }
361
362 /*** Main ***/
363
364 int main(void)
365 {
366         clock_init();
367         gpio_init();
368         tick_init();
369         usart_init();
370         desig_get_unique_id_as_dfu(usb_serial_number);
371
372         debug_printf("Hello, world!\n");
373
374         usb_init();
375         display_init();
376
377         u32 last_blink = 0;
378
379         for (;;) {
380                 if (ms_ticks - last_blink >= 500) {
381                         debug_led_toggle();
382                         last_blink = ms_ticks;
383                         display_test();
384                 }
385
386                 if (usb_event_pending) {
387                         usbd_poll(usbd_dev);
388                         usb_event_pending = 0;
389                         nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
390                         nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
391                 }
392
393                 wait_for_interrupt();
394         }
395
396         return 0;
397 }