]> mj.ucw.cz Git - home-hw.git/blob - clock/firmware/main.c
burrow-bsbd: Removed surplus newline
[home-hw.git] / clock / firmware / main.c
1 /*
2  *      Clock with Barometer
3  *
4  *      (c) 2018--2019 Martin Mareš <mj@ucw.cz>
5  */
6
7 FIXME: Partial code...
8
9 #include "util.h"
10
11 #include <libopencm3/cm3/nvic.h>
12 #include <libopencm3/cm3/systick.h>
13 #include <libopencm3/stm32/rcc.h>
14 #include <libopencm3/stm32/gpio.h>
15 #include <libopencm3/stm32/timer.h>
16 #include <libopencm3/stm32/usart.h>
17
18 #include <string.h>
19
20 static void clock_init(void)
21 {
22         rcc_clock_setup_in_hse_8mhz_out_72mhz();
23
24         rcc_periph_clock_enable(RCC_GPIOB);
25         rcc_periph_clock_enable(RCC_GPIOC);
26         rcc_periph_clock_enable(RCC_USART1);
27         rcc_periph_clock_enable(RCC_USB);
28
29         rcc_periph_reset_pulse(RST_GPIOB);
30         rcc_periph_reset_pulse(RST_GPIOC);
31         rcc_periph_reset_pulse(RST_USART1);
32         rcc_periph_reset_pulse(RST_USB);
33 }
34
35 static void gpio_init(void)
36 {
37         // Switch JTAG off to free up pins
38         gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON, 0);
39
40         // PA9 = TXD1 for debugging console
41         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
42
43         // PA10 = RXD1 for debugging console
44         gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
45
46         // PC13 = BluePill LED
47         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
48         gpio_clear(GPIOC, GPIO13);
49 }
50
51 static volatile u32 ms_ticks;
52
53 void sys_tick_handler(void)
54 {
55         ms_ticks++;
56 }
57
58 static void tick_init(void)
59 {
60         systick_set_frequency(1000, 72000000);
61         systick_counter_enable();
62         systick_interrupt_enable();
63 }
64
65 #if 0
66 static void delay_ms(uint ms)
67 {
68         u32 start_ticks = ms_ticks;
69         while (ms_ticks - start_ticks < ms)
70                 ;
71 }
72 #endif
73
74 static void usart_init(void)
75 {
76         usart_set_baudrate(USART1, 115200);
77         usart_set_databits(USART1, 8);
78         usart_set_stopbits(USART1, USART_STOPBITS_1);
79         usart_set_mode(USART1, USART_MODE_TX_RX);
80         usart_set_parity(USART1, USART_PARITY_NONE);
81         usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
82
83         usart_enable(USART1);
84 }
85
86 static const struct usb_device_descriptor dev = {
87         .bLength = USB_DT_DEVICE_SIZE,
88         .bDescriptorType = USB_DT_DEVICE,
89         .bcdUSB = 0x0200,
90         .bDeviceClass = 0xFF,
91         .bDeviceSubClass = 0,
92         .bDeviceProtocol = 0,
93         .bMaxPacketSize0 = 64,
94         .idVendor = 0xCAFE,
95         .idProduct = 0xCAFE,
96         .bcdDevice = 0x0200,
97         .iManufacturer = 1,
98         .iProduct = 2,
99         .iSerialNumber = 3,
100         .bNumConfigurations = 1,
101 };
102
103 static const struct usb_endpoint_descriptor endpoints[] = {{
104         .bLength = USB_DT_ENDPOINT_SIZE,
105         .bDescriptorType = USB_DT_ENDPOINT,
106         .bEndpointAddress = 0x81,
107         .bmAttributes = USB_ENDPOINT_ATTR_BULK,
108         .wMaxPacketSize = 64,
109         .bInterval = 1,
110 }};
111
112 static const struct usb_interface_descriptor iface = {
113         .bLength = USB_DT_INTERFACE_SIZE,
114         .bDescriptorType = USB_DT_INTERFACE,
115         .bInterfaceNumber = 0,
116         .bAlternateSetting = 0,
117         .bNumEndpoints = 1,
118         .bInterfaceClass = 0xFF,
119         .bInterfaceSubClass = 0,
120         .bInterfaceProtocol = 0,
121         .iInterface = 0,
122         .endpoint = endpoints,
123 };
124
125 static const struct usb_interface ifaces[] = {{
126         .num_altsetting = 1,
127         .altsetting = &iface,
128 }};
129
130 static const struct usb_config_descriptor config = {
131         .bLength = USB_DT_CONFIGURATION_SIZE,
132         .bDescriptorType = USB_DT_CONFIGURATION,
133         .wTotalLength = 0,
134         .bNumInterfaces = 1,
135         .bConfigurationValue = 1,
136         .iConfiguration = 0,
137         .bmAttributes = 0x80,
138         .bMaxPower = 50,        // multiplied by 2 mA
139         .interface = ifaces,
140 };
141
142 static const char *usb_strings[] = {
143         "Hippo Computing Inc.",
144         "Space Alert Thermometer",
145         "42",
146 };
147
148 uint8_t usbd_control_buffer[64];
149
150 static void ep81_cb(usbd_device *usbd_dev, uint8_t ep UNUSED)
151 {
152         byte buf[4];
153         if (ds_sensors[0].address[0] && ds_sensors[0].current_temp != DS_TEMP_UNKNOWN) {
154                 put_u32_be(buf, (u32) ds_sensors[0].current_temp);
155         } else {
156                 put_u32_be(buf, 0x80000000);
157         }
158         usbd_ep_write_packet(usbd_dev, 0x81, buf, sizeof(buf));
159 }
160
161 static void set_config_cb(usbd_device *usbd_dev, uint16_t wValue UNUSED)
162 {
163         usbd_ep_setup(usbd_dev, 0x81, USB_ENDPOINT_ATTR_BULK, 64, ep81_cb);
164         ep81_cb(usbd_dev, 0);
165 }
166
167 int main(void)
168 {
169         clock_init();
170         gpio_init();
171         tick_init();
172         usart_init();
173
174         // Simulate USB disconnect
175         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
176         gpio_clear(GPIOA, GPIO11 | GPIO12);
177         delay_ms(1000);
178
179         usbd_device *usbd_dev = usbd_init(&st_usbfs_v1_usb_driver, &dev, &config, usb_strings, 4, usbd_control_buffer, sizeof(usbd_control_buffer));
180         usbd_register_set_config_callback(usbd_dev, set_config_cb);
181         u32 last_ds_step = 0;
182
183         for (;;) {
184                 if (ms_ticks - last_ds_step >= 100) {
185                         debug_led_toggle();
186                         ds_step();
187                         last_ds_step = ms_ticks;
188                 }
189
190                 // XXX: libopencm3 usbd does not use interrupts at the moment, need to poll...
191                 // wait_for_interrupt();
192                 usbd_poll(usbd_dev);
193         }
194
195         return 0;
196 }