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