/
main.c
200 lines (168 loc) · 5.58 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* This file is part of the stfuboot project.
*
* Copyright (C) 2012 Innovative Converged Devices (ICD)
*
* Author(s):
* Andrey Smirnov <andrey.smirnov@convergeddevices.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <alloca.h>
#include <libopencm3/cm3/scb.h>
#include <libopencm3/stm32/f1/rcc.h>
#include <libopencm3/stm32/f1/flash.h>
#include <libopencm3/stm32/f1/gpio.h>
#include <libopencm3/stm32/f1/flash.h>
#include <libopencm3/stm32/desig.h>
#include <libopencm3/stm32/crc.h>
#include <libopencm3/usb/usbd.h>
#include "dfu.h"
#include "uart.h"
#include "printf.h"
#define MIN(a, b) ((a)<(b) ? (a) : (b))
/* We need a special large control buffer for this device: */
u8 usbd_control_buffer[2048];
const struct usb_device_descriptor stfub_dev_descr = {
.bLength = USB_DT_DEVICE_SIZE,
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = 0x0200,
.bDeviceClass = 0,
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.bMaxPacketSize0 = 64,
.idVendor = 0x0483,
.idProduct = 0xDF11,
.bcdDevice = 0x0200,
.iManufacturer = 0,
.iProduct = 1,
.iSerialNumber = 2,
.bNumConfigurations = 1,
};
const struct usb_dfu_descriptor stfub_dfu_descr = {
.bLength = sizeof(struct usb_dfu_descriptor),
.bDescriptorType = DFU_FUNCTIONAL,
.bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_CAN_UPLOAD | USB_DFU_WILL_DETACH,
.wDetachTimeout = 255,
.wTransferSize = 2048, /* STM32 flash page size */
.bcdDFUVersion = 0x0110,
};
#define STFUB_AS_ISTRING(x) ((x) + 3)
#define STFUB_DFU_INTERFACE(num, func) \
{ \
.bLength = USB_DT_INTERFACE_SIZE, \
.bDescriptorType = USB_DT_INTERFACE, \
.bInterfaceNumber = 0, \
.bAlternateSetting = num, \
.bNumEndpoints = 0, \
.bInterfaceClass = 0xFE, \
.bInterfaceSubClass = 1, \
.bInterfaceProtocol = 2, \
.iInterface = STFUB_AS_ISTRING(num), \
.extra = &func, \
.extralen = sizeof(func), \
}
const struct usb_interface_descriptor stfub_interface_descriptors[] ={
STFUB_DFU_INTERFACE(STFUB_AS_MAIN_MEMORY, stfub_dfu_descr),
STFUB_DFU_INTERFACE(STFUB_AS_SYSTEM_MEMORY, stfub_dfu_descr),
STFUB_DFU_INTERFACE(STFUB_AS_OPTION_BYTES, stfub_dfu_descr),
};
struct usb_interface stfub_interfaces[] = {
{
.num_altsetting = 3,
.altsetting = stfub_interface_descriptors,
},
};
struct usb_config_descriptor config = {
.bLength = USB_DT_CONFIGURATION_SIZE,
.bDescriptorType = USB_DT_CONFIGURATION,
.wTotalLength = 0,
.bNumInterfaces = 1,
.bConfigurationValue = 1,
.iConfiguration = 0,
.bmAttributes = 0xC0,
.bMaxPower = 0x32,
.interface = stfub_interfaces,
};
static char serial_number_string[30];
static const char *usb_strings[] = {
"Device with STFUBoot",
serial_number_string,
/* This string is used by ST Microelectronics' DfuSe utility. */
"Main Memory [0x08004800 - 0x08040000]",
"System Memory [0x08001000 - 0x08004800]",
"Option Bytes [0x1FFFF800 - 0x1FFFF810]",
};
static void stfub_clocks_init(void)
{
/*
TODO: For some reason the device would not be able to
initialize PLL after exiting the factory
bootloader (not due to flashing). Power-cycling the board will solve the issue.
Using HSE instead doesn't have this issuer
*/
rcc_clock_setup_in_hsi_out_48mhz();
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPDEN);
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_OTGFSEN);
}
static void stfub_gpio_init(void)
{
gpio_set_mode(GPIOD, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO5);
gpio_set(GPIOD, GPIO5);
gpio_primary_remap(AFIO_MAPR_SWJ_CFG_FULL_SWJ, AFIO_MAPR_USART2_REMAP);
}
static usbd_device *stfub_usb_init(void)
{
static usbd_device *usbddev;
desig_get_unique_id_as_string(serial_number_string,
sizeof(serial_number_string));
stfub_dfu_init(&stfub_dfu_descr);
usbddev = usbd_init(&stm32f107_usb_driver, &stfub_dev_descr,
&config, usb_strings,
(sizeof(usb_strings) / sizeof(usb_strings[0])));
usbd_set_control_buffer_size(usbddev, sizeof(usbd_control_buffer));
usbd_register_set_altsetting_callback(usbddev,
stfub_dfu_switch_altsetting);
usbd_register_control_callback(usbddev,
USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
stfub_dfu_handle_control_request);
return usbddev;
}
int main(void)
{
static usbd_device *usbddev;
stfub_clocks_init();
stfub_gpio_init();
stfub_uart_init();
/*
TODO: For some reason he first character of this banner is
lost, this has to be investigated further
*/
stfub_printf("=========================================\n");
stfub_printf("= stfuboot -- Insert smart tagline here =\n");
stfub_printf("=========================================\n");
usbddev = stfub_usb_init();
while (1) {
usbd_poll(usbddev);
stfub_dfu_tick();
}
}