]> mj.ucw.cz Git - home-hw.git/blob - test-sinclair/main.c
8de64a79ffc54fa7d509e9050e4b84f0d12eef99
[home-hw.git] / test-sinclair / main.c
1 /*
2  *      Testing Communication with Sinclair Air Conditioner
3  *
4  *      (c) 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/spi.h>
18 #include <libopencm3/stm32/exti.h>
19 #include <libopencm3/stm32/timer.h>
20 #include <libopencm3/usb/dfu.h>
21 #include <libopencm3/usb/usbd.h>
22
23 #include <string.h>
24
25 /*** Hardware init ***/
26
27 static void clock_init(void)
28 {
29         rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
30
31         rcc_periph_clock_enable(RCC_GPIOA);
32         rcc_periph_clock_enable(RCC_GPIOB);
33         rcc_periph_clock_enable(RCC_GPIOC);
34         rcc_periph_clock_enable(RCC_SPI2);
35         rcc_periph_clock_enable(RCC_USART1);
36         rcc_periph_clock_enable(RCC_USB);
37         rcc_periph_clock_enable(RCC_AFIO);
38         rcc_periph_clock_enable(RCC_TIM3);
39
40         rcc_periph_reset_pulse(RST_GPIOA);
41         rcc_periph_reset_pulse(RST_GPIOB);
42         rcc_periph_reset_pulse(RST_GPIOC);
43         rcc_periph_reset_pulse(RST_SPI2);
44         rcc_periph_reset_pulse(RST_USART1);
45         rcc_periph_reset_pulse(RST_USB);
46         rcc_periph_reset_pulse(RST_AFIO);
47         rcc_periph_reset_pulse(RST_TIM3);
48 }
49
50 static void gpio_init(void)
51 {
52         // PA9 = TXD1 for debugging console
53         // PA10 = RXD1 for debugging console
54         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
55         gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
56
57         // PC13 = BluePill LED
58         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
59         gpio_clear(GPIOC, GPIO13);
60
61         // PB12 = SS2 (but used as GP input)
62         // PB13 = SCK2
63         // PB15 = MOSI2
64         gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO12);
65         gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO13);
66         gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO15);
67 }
68
69 static void usart_init(void)
70 {
71         usart_set_baudrate(USART1, 115200);
72         usart_set_databits(USART1, 8);
73         usart_set_stopbits(USART1, USART_STOPBITS_1);
74         usart_set_mode(USART1, USART_MODE_TX);
75         usart_set_parity(USART1, USART_PARITY_NONE);
76         usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
77
78         usart_enable(USART1);
79 }
80
81 /*** System ticks ***/
82
83 static volatile u32 ms_ticks;
84
85 void sys_tick_handler(void)
86 {
87         ms_ticks++;
88 }
89
90 static void tick_init(void)
91 {
92         systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000);
93         systick_counter_enable();
94         systick_interrupt_enable();
95 }
96
97 static void delay_ms(uint ms)
98 {
99         u32 start_ticks = ms_ticks;
100         while (ms_ticks - start_ticks < ms)
101                 ;
102 }
103
104 /*** Emulated TM1618 LED Driver ***/
105
106 static void tm_init(void)
107 {
108         // Configure SPI2 to receive
109         spi_set_receive_only_mode(SPI2);
110         spi_enable_software_slave_management(SPI2);
111         spi_set_nss_low(SPI2);
112         spi_send_lsb_first(SPI2);
113         spi_set_clock_polarity_0(SPI2);
114         spi_set_clock_phase_1(SPI2);
115         spi_enable_rx_buffer_not_empty_interrupt(SPI2);
116         nvic_enable_irq(NVIC_SPI2_IRQ);
117         spi_enable(SPI2);
118
119 #if 0
120         // Since our optocouplers are negating, we cannot let STM32 to handle slave
121         // select in hardware. Instead, we let SS trigger an interrupt, which changes
122         // SPI state accordingly.
123         nvic_set_priority(NVIC_EXTI15_10_IRQ, 0);
124         nvic_enable_irq(NVIC_EXTI15_10_IRQ);
125         exti_set_trigger(EXTI12, EXTI_TRIGGER_BOTH);
126         exti_select_source(EXTI12, GPIOB);
127         exti_enable_request(EXTI12);
128 #endif
129
130 #if 1
131         timer_set_prescaler(TIM3, CPU_CLOCK_MHZ-1);     // 1 tick = 1 μs
132         timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_DOWN);
133         timer_update_on_overflow(TIM3);
134         timer_disable_preload(TIM3);
135         timer_one_shot_mode(TIM3);
136         timer_enable_irq(TIM3, TIM_DIER_UIE);
137         nvic_enable_irq(NVIC_TIM3_IRQ);
138         //timer_set_period(TIM3, 9999);
139         //timer_generate_event(TIM3, TIM_EGR_UG);
140         //timer_enable_counter(TIM3);
141 #endif
142 }
143
144 void exti15_10_isr(void)
145 {
146         // We require low latency here, so interaction with peripherals is open-coded.
147 #if 0
148         if (GPIO_IDR(GPIOB) & (1 << 12))
149                 SPI_CR1(SPI2) |= SPI_CR1_SSI;
150         else
151                 SPI_CR1(SPI2) &= ~SPI_CR1_SSI;
152 #else
153         SPI_CR1(SPI2) &= ~SPI_CR1_SSI;
154 #endif
155         EXTI_PR = EXTI12;
156 }
157
158 static volatile byte tm_data[8];
159 static volatile byte tm_overrun;
160
161 static volatile byte tm_buffer[256];
162 static volatile uint tm_len;
163
164 static volatile uint tm_timeouts;
165
166 void spi2_isr(void)
167 {
168         /*
169          *  The AC unit is sending a stream of commands like this:
170          *
171          *  00 - set mode: 4 grids, 8 segments
172          *  44 - will write to display memory, no auto-increment
173          *  Cx - set memory address to x
174          *  yy - data to write, two most-significant bits are always zero
175          *  8B - display ON, duty cycle 10/16
176          *
177          *  So the only byte which can have top 2 bits both set is the Cx command.
178          *  We make use of this to synchronize the stream.
179          */
180         if (SPI_SR(SPI2) & SPI_SR_OVR)
181                 tm_overrun = 1;
182         if (SPI_SR(SPI2) & SPI_SR_RXNE) {
183                 byte x = SPI_DR(SPI2) ^ 0xff;
184 #if 0
185                 if (tm_len < ARRAY_SIZE(tm_buffer))
186                         tm_buffer[tm_len++] = x;
187 #endif
188                 static byte tm_address;
189                 if (tm_address) {
190                         tm_data[tm_address & 7] = x;
191                         tm_address = 0;
192                 } else if ((x & 0xc0) == 0xc0) {
193                         tm_address = x;
194                 }
195                 timer_set_period(TIM3, 999);
196                 timer_generate_event(TIM3, TIM_EGR_UG);
197                 timer_enable_counter(TIM3);
198         }
199 }
200
201 void tim3_isr(void)
202 {
203         if (TIM_SR(TIM3) & TIM_SR_UIF) {
204                 TIM_SR(TIM3) &= ~TIM_SR_UIF;
205                 tm_timeouts++;
206                 spi_set_nss_high(SPI2);
207                 spi_set_nss_low(SPI2);
208         }
209 }
210
211 #if 0
212 static void tm_test(void)
213 {
214         u32 start_ticks = ms_ticks;
215         static byte tmbuf[256];
216         uint len = 0;
217
218         while (ms_ticks - start_ticks < 1000 && len < ARRAY_SIZE(tmbuf)) {
219                 if (SPI_SR(SPI2) & SPI_SR_RXNE) {
220                         tmbuf[len++] = SPI_DR(SPI2) ^ 0xff;
221                 }
222         }
223
224         for (uint i=0; i<len; i++) {
225                 debug_printf("%02x ", tmbuf[i]);
226                 if ((i % 5) == 4)
227                         debug_putc('\n');
228         }
229         debug_putc('\n');
230 }
231 #endif
232
233 static void tm_show(void)
234 {
235         debug_printf("TM:");
236         for (uint i=0; i<8; i++)
237                 debug_printf(" %02x", tm_data[i]);
238         debug_printf(" o=%d t=%d", tm_overrun, tm_timeouts);
239         tm_overrun = 0;
240         debug_putc('\n');
241
242 #if 0
243         static byte tm_dumped;
244         if (!tm_dumped && tm_len == ARRAY_SIZE(tm_buffer)) {
245                 for (uint i=0; i < tm_len; i++)
246                         debug_printf("%02x ", tm_buffer[i]);
247                 debug_putc('\n');
248                 // tm_dumped = 1;
249                 tm_len = 0;
250         }
251 #endif
252 }
253
254 /*** USB ***/
255
256 static usbd_device *usbd_dev;
257
258 enum usb_string {
259         STR_MANUFACTURER = 1,
260         STR_PRODUCT,
261         STR_SERIAL,
262 };
263
264 static char usb_serial_number[13];
265
266 static const char *usb_strings[] = {
267         "United Computer Wizards",
268         "Sinclair Air Conditioner",
269         usb_serial_number,
270 };
271
272 static const struct usb_device_descriptor device = {
273         .bLength = USB_DT_DEVICE_SIZE,
274         .bDescriptorType = USB_DT_DEVICE,
275         .bcdUSB = 0x0200,
276         .bDeviceClass = 0xFF,
277         .bDeviceSubClass = 0,
278         .bDeviceProtocol = 0,
279         .bMaxPacketSize0 = 64,
280         .idVendor = 0x4242,
281         .idProduct = 0x0007,
282         .bcdDevice = 0x0000,
283         .iManufacturer = STR_MANUFACTURER,
284         .iProduct = STR_PRODUCT,
285         .iSerialNumber = STR_SERIAL,
286         .bNumConfigurations = 1,
287 };
288
289 static const struct usb_endpoint_descriptor endpoints[] = {{
290         // Bulk end-point for sending values to the display
291         .bLength = USB_DT_ENDPOINT_SIZE,
292         .bDescriptorType = USB_DT_ENDPOINT,
293         .bEndpointAddress = 0x01,
294         .bmAttributes = USB_ENDPOINT_ATTR_BULK,
295         .wMaxPacketSize = 64,
296         .bInterval = 1,
297 }};
298
299 static const struct usb_interface_descriptor iface = {
300         .bLength = USB_DT_INTERFACE_SIZE,
301         .bDescriptorType = USB_DT_INTERFACE,
302         .bInterfaceNumber = 0,
303         .bAlternateSetting = 0,
304         .bNumEndpoints = 1,
305         .bInterfaceClass = 0xFF,
306         .bInterfaceSubClass = 0,
307         .bInterfaceProtocol = 0,
308         .iInterface = 0,
309         .endpoint = endpoints,
310 };
311
312 static const struct usb_dfu_descriptor dfu_function = {
313         .bLength = sizeof(struct usb_dfu_descriptor),
314         .bDescriptorType = DFU_FUNCTIONAL,
315         .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
316         .wDetachTimeout = 255,
317         .wTransferSize = 1024,
318         .bcdDFUVersion = 0x0100,
319 };
320
321 static const struct usb_interface_descriptor dfu_iface = {
322         .bLength = USB_DT_INTERFACE_SIZE,
323         .bDescriptorType = USB_DT_INTERFACE,
324         .bInterfaceNumber = 1,
325         .bAlternateSetting = 0,
326         .bNumEndpoints = 0,
327         .bInterfaceClass = 0xFE,
328         .bInterfaceSubClass = 1,
329         .bInterfaceProtocol = 1,
330         .iInterface = 0,
331
332         .extra = &dfu_function,
333         .extralen = sizeof(dfu_function),
334 };
335
336 static const struct usb_interface ifaces[] = {{
337         .num_altsetting = 1,
338         .altsetting = &iface,
339 }, {
340         .num_altsetting = 1,
341         .altsetting = &dfu_iface,
342 }};
343
344 static const struct usb_config_descriptor config = {
345         .bLength = USB_DT_CONFIGURATION_SIZE,
346         .bDescriptorType = USB_DT_CONFIGURATION,
347         .wTotalLength = 0,
348         .bNumInterfaces = 2,
349         .bConfigurationValue = 1,
350         .iConfiguration = 0,
351         .bmAttributes = 0x80,
352         .bMaxPower = 50,        // multiplied by 2 mA
353         .interface = ifaces,
354 };
355
356 static byte usb_configured;
357 static uint8_t usbd_control_buffer[64];
358
359 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
360 {
361         // Reset to bootloader, which implements the rest of DFU
362         debug_printf("Switching to DFU\n");
363         debug_flush();
364         scb_reset_core();
365 }
366
367 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
368         struct usb_setup_data *req,
369         uint8_t **buf UNUSED,
370         uint16_t *len UNUSED,
371         void (**complete)(usbd_device *dev, struct usb_setup_data *req))
372 {
373         if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
374                 return USBD_REQ_NOTSUPP;
375
376         *complete = dfu_detach_complete;
377         return USBD_REQ_HANDLED;
378 }
379
380 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
381 {
382         // We received a frame from the USB host
383         byte buf[8];
384         uint len = usbd_ep_read_packet(dev, 0x01, buf, 8);
385         debug_printf("USB: Host sent %u bytes\n", len);
386 }
387
388 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
389 {
390         usbd_register_control_callback(
391                 dev,
392                 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
393                 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
394                 dfu_control_cb);
395         usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
396         usb_configured = 1;
397 }
398
399 static void reset_cb(void)
400 {
401         debug_printf("USB: Reset\n");
402         usb_configured = 0;
403 }
404
405 static volatile bool usb_event_pending;
406
407 void usb_lp_can_rx0_isr(void)
408 {
409         /*
410          *  We handle USB in the main loop to avoid race conditions between
411          *  USB interrupts and other code. However, we need an interrupt to
412          *  up the main loop from sleep.
413          *
414          *  We set up only the low-priority ISR, because high-priority ISR handles
415          *  only double-buffered bulk transfers and isochronous transfers.
416          */
417         nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
418         usb_event_pending = 1;
419 }
420
421 static void usb_init(void)
422 {
423         // Simulate USB disconnect
424         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
425         gpio_clear(GPIOA, GPIO11 | GPIO12);
426         delay_ms(100);
427
428         usbd_dev = usbd_init(
429                 &st_usbfs_v1_usb_driver,
430                 &device,
431                 &config,
432                 usb_strings,
433                 ARRAY_SIZE(usb_strings),
434                 usbd_control_buffer,
435                 sizeof(usbd_control_buffer)
436         );
437         usbd_register_reset_callback(usbd_dev, reset_cb);
438         usbd_register_set_config_callback(usbd_dev, set_config_cb);
439         usb_event_pending = 1;
440 }
441
442 /*** Main ***/
443
444 int main(void)
445 {
446         clock_init();
447         gpio_init();
448         usart_init();
449
450         tick_init();
451         desig_get_unique_id_as_dfu(usb_serial_number);
452
453         debug_printf("Hello, world!\n");
454
455         tm_init();
456         usb_init();
457
458         u32 last_blink = 0;
459
460         for (;;) {
461                 if (ms_ticks - last_blink >= 1000) {
462                         debug_led_toggle();
463                         last_blink = ms_ticks;
464                         tm_show();
465                 }
466
467                 if (usb_event_pending) {
468                         usbd_poll(usbd_dev);
469                         usb_event_pending = 0;
470                         nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
471                         nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
472                 }
473
474                 wait_for_interrupt();
475         }
476
477         return 0;
478 }