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