├── .gitmodules ├── BSP_STM32F0xx ├── bsp_system.h ├── bsp_usb.h ├── xpd_config.h ├── bsp_io.c ├── bsp_io.h ├── bsp_usb.c └── bsp_system.c ├── BSP_STM32F4xx ├── bsp_usb.c ├── bsp_system.h ├── bsp_usb.h ├── bsp_io.c ├── xpd_config.h ├── bsp_io.h └── bsp_system.c ├── BSP_STM32F3xx ├── xpd_config.h ├── bsp_system.h ├── bsp_usb.h ├── bsp_io.h ├── bsp_io.c ├── bsp_system.c └── bsp_usb.c ├── BSP_STM32L4xx ├── bsp_system.h ├── bsp_usb.h ├── xpd_config.h ├── bsp_io.c ├── bsp_io.h ├── bsp_usb.c └── bsp_system.c ├── Main ├── flash_if.h ├── usbd_config.h ├── main.c ├── stm32_cmn_flash.ld ├── stm32_cm0_flash.ld ├── stm32_cm0_startup.s ├── stm32_cmn_startup.s └── flash_if.c ├── .travis.yml ├── README.md ├── Makefile └── LICENSE /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "USBDevice"] 2 | path = USBDevice 3 | url = https://github.com/IntergatedCircuits/USBDevice 4 | [submodule "STM32_XPD"] 5 | path = STM32_XPD 6 | url = https://github.com/IntergatedCircuits/STM32_XPD 7 | -------------------------------------------------------------------------------- /BSP_STM32F0xx/bsp_system.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_system.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for system clocking 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __BSP_SYSTEM_H_ 24 | #define __BSP_SYSTEM_H_ 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | extern void SystemClock_Config(void); 32 | 33 | #ifdef __cplusplus 34 | } 35 | #endif 36 | 37 | #endif /* __BSP_SYSTEM_H_ */ 38 | -------------------------------------------------------------------------------- /BSP_STM32F0xx/bsp_usb.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_usb.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for USB communication 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __BSP_USB_H_ 24 | #define __BSP_USB_H_ 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | #include 32 | 33 | void BSP_USB_Bind(USB_HandleType *dev); 34 | 35 | #ifdef __cplusplus 36 | } 37 | #endif 38 | 39 | #endif /* __BSP_USB_H_ */ 40 | -------------------------------------------------------------------------------- /BSP_STM32F4xx/bsp_usb.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_usb.c 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for USB communication 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #include 24 | #include 25 | 26 | void BSP_USB_Bind(USB_HandleType *dev) 27 | { 28 | GPIO_vInitPin(USB_DM_PIN, USB_DM_CFG); 29 | GPIO_vInitPin(USB_DP_PIN, USB_DP_CFG); 30 | USB_INST2HANDLE(dev, USB_OTG_FS); 31 | } 32 | -------------------------------------------------------------------------------- /BSP_STM32F0xx/xpd_config.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file xpd_config.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader STM32_XPD configuration header 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef XPD_CONFIG_H_ 24 | #define XPD_CONFIG_H_ 25 | 26 | /* specify device header */ 27 | #include STM32_TARGET_HEADER 28 | 29 | #ifndef VDD_VALUE_mV 30 | #define VDD_VALUE_mV 2000 31 | #endif 32 | 33 | #endif /* XPD_CONFIG_H_ */ 34 | -------------------------------------------------------------------------------- /BSP_STM32F3xx/xpd_config.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file xpd_config.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader STM32_XPD configuration header 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef XPD_CONFIG_H_ 24 | #define XPD_CONFIG_H_ 25 | 26 | /* specify device header */ 27 | #include STM32_TARGET_HEADER 28 | 29 | #ifndef VDD_VALUE_mV 30 | #define VDD_VALUE_mV 2000 31 | #endif 32 | 33 | #endif /* XPD_CONFIG_H_ */ 34 | -------------------------------------------------------------------------------- /BSP_STM32F3xx/bsp_system.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_system.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for system clocking 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __BSP_SYSTEM_H_ 24 | #define __BSP_SYSTEM_H_ 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | extern void SystemClock_Config(void); 32 | 33 | #ifdef __cplusplus 34 | } 35 | #endif 36 | 37 | #endif /* __BSP_SYSTEM_H_ */ 38 | -------------------------------------------------------------------------------- /BSP_STM32F4xx/bsp_system.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_system.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for system clocking 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __BSP_SYSTEM_H_ 24 | #define __BSP_SYSTEM_H_ 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | extern void SystemClock_Config(void); 32 | 33 | #ifdef __cplusplus 34 | } 35 | #endif 36 | 37 | #endif /* __BSP_SYSTEM_H_ */ 38 | -------------------------------------------------------------------------------- /BSP_STM32L4xx/bsp_system.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_system.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for system clocking 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __BSP_SYSTEM_H_ 24 | #define __BSP_SYSTEM_H_ 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | extern void SystemClock_Config(void); 32 | 33 | #ifdef __cplusplus 34 | } 35 | #endif 36 | 37 | #endif /* __BSP_SYSTEM_H_ */ 38 | -------------------------------------------------------------------------------- /BSP_STM32F3xx/bsp_usb.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_usb.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for USB communication 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __BSP_USB_H_ 24 | #define __BSP_USB_H_ 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | #include 32 | 33 | void BSP_USB_Bind(USB_HandleType *dev); 34 | 35 | #ifdef __cplusplus 36 | } 37 | #endif 38 | 39 | #endif /* __BSP_USB_H_ */ 40 | -------------------------------------------------------------------------------- /BSP_STM32F4xx/bsp_usb.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_usb.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for USB communication 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __BSP_USB_H_ 24 | #define __BSP_USB_H_ 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | #include 32 | 33 | void BSP_USB_Bind(USB_HandleType *dev); 34 | 35 | #ifdef __cplusplus 36 | } 37 | #endif 38 | 39 | #endif /* __BSP_USB_H_ */ 40 | -------------------------------------------------------------------------------- /BSP_STM32L4xx/bsp_usb.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_usb.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for USB communication 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __BSP_USB_H_ 24 | #define __BSP_USB_H_ 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | #include 32 | 33 | void BSP_USB_Bind(USB_HandleType *dev); 34 | 35 | #ifdef __cplusplus 36 | } 37 | #endif 38 | 39 | #endif /* __BSP_USB_H_ */ 40 | -------------------------------------------------------------------------------- /BSP_STM32F0xx/bsp_io.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_io.c 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for I/O pins 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #include 24 | 25 | const GPIO_InitType BSP_IOCfg[] = 26 | { 27 | /* USB pins */ 28 | { 29 | .Mode = GPIO_MODE_ALTERNATE, 30 | .Pull = GPIO_PULL_FLOAT, 31 | .Output.Type = GPIO_OUTPUT_PUSHPULL, 32 | .Output.Speed = VERY_HIGH, 33 | .AlternateMap = GPIO_USB_AF2 34 | }, 35 | }; 36 | -------------------------------------------------------------------------------- /BSP_STM32F4xx/bsp_io.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_io.c 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for I/O pins 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #include 24 | 25 | const GPIO_InitType BSP_IOCfg[] = 26 | { 27 | /* USB pins */ 28 | { 29 | .Mode = GPIO_MODE_ALTERNATE, 30 | .Pull = GPIO_PULL_FLOAT, 31 | .Output.Type = GPIO_OUTPUT_PUSHPULL, 32 | .Output.Speed = VERY_HIGH, 33 | .AlternateMap = GPIO_OTG_FS_AF10 34 | }, 35 | }; 36 | -------------------------------------------------------------------------------- /BSP_STM32F4xx/xpd_config.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file xpd_config.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader STM32_XPD configuration header 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __XPD_CONFIG_H_ 24 | #define __XPD_CONFIG_H_ 25 | 26 | /* specify device header */ 27 | #include STM32_TARGET_HEADER 28 | 29 | /* VDD value is used to adjust the flash programming width */ 30 | #ifndef VDD_VALUE_mV 31 | #define VDD_VALUE_mV 2000 32 | #endif 33 | 34 | /* TODO specify oscillator parameters */ 35 | 36 | #endif /* __XPD_CONFIG_H_ */ 37 | -------------------------------------------------------------------------------- /BSP_STM32L4xx/xpd_config.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file xpd_config.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader STM32_XPD configuration header 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __XPD_CONFIG_H_ 24 | #define __XPD_CONFIG_H_ 25 | 26 | /* specify device header */ 27 | #include STM32_TARGET_HEADER 28 | 29 | /* VDD value is used to adjust the flash programming width */ 30 | #ifndef VDD_VALUE_mV 31 | #define VDD_VALUE_mV 2000 32 | #endif 33 | 34 | /* TODO specify oscillator parameters */ 35 | 36 | #endif /* __XPD_CONFIG_H_ */ 37 | -------------------------------------------------------------------------------- /Main/flash_if.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file flash_if.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader flash memory interface header 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __FLASH_IF_H_ 24 | #define __FLASH_IF_H_ 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | #include 32 | 33 | #define FLASH_VALID_SYMBOL ((uint32_t)0xCA11AB1E) /* Callable */ 34 | 35 | extern const USBD_DFU_AppType* const flash_if; 36 | 37 | int FlashIf_ApplicationPresent(void); 38 | 39 | #ifdef __cplusplus 40 | } 41 | #endif 42 | 43 | #endif /* __FLASH_IF_H_ */ 44 | -------------------------------------------------------------------------------- /BSP_STM32F0xx/bsp_io.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_io.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for I/O pins 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __BSP_IO_H_ 24 | #define __BSP_IO_H_ 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | #include 32 | 33 | #define USB_DP_PIN PA12 34 | #define USB_DM_PIN PA11 35 | #define USB_DP_CFG (&BSP_IOCfg[0]) 36 | #define USB_DM_CFG (&BSP_IOCfg[0]) 37 | 38 | extern const GPIO_InitType BSP_IOCfg[]; 39 | 40 | #ifdef __cplusplus 41 | } 42 | #endif 43 | 44 | #endif /* __BSP_IO_H_ */ 45 | -------------------------------------------------------------------------------- /BSP_STM32L4xx/bsp_io.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_io.c 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for I/O pins 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #include 24 | 25 | const GPIO_InitType BSP_IOCfg[] = 26 | { 27 | /* USB pins */ 28 | { 29 | .Mode = GPIO_MODE_ALTERNATE, 30 | .Pull = GPIO_PULL_FLOAT, 31 | .Output.Type = GPIO_OUTPUT_PUSHPULL, 32 | .Output.Speed = VERY_HIGH, 33 | #if defined(USB) 34 | .AlternateMap = GPIO_USB_AF10, 35 | #elif defined(USB_OTG_FS) 36 | .AlternateMap = GPIO_OTG_FS_AF10, 37 | #endif 38 | }, 39 | }; 40 | -------------------------------------------------------------------------------- /BSP_STM32L4xx/bsp_io.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_io.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for I/O pins 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __BSP_IO_H_ 24 | #define __BSP_IO_H_ 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | #include 32 | 33 | #define USB_DP_PIN PA12 34 | #define USB_DM_PIN PA11 35 | #define USB_DP_CFG (&BSP_IOCfg[0]) 36 | #define USB_DM_CFG (&BSP_IOCfg[0]) 37 | 38 | extern const GPIO_InitType BSP_IOCfg[]; 39 | 40 | #ifdef __cplusplus 41 | } 42 | #endif 43 | 44 | #endif /* __BSP_IO_H_ */ 45 | -------------------------------------------------------------------------------- /BSP_STM32F0xx/bsp_usb.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_usb.c 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for USB communication 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #include 24 | #include 25 | 26 | void BSP_USB_Bind(USB_HandleType *dev) 27 | { 28 | /* Only necessary for low pin count devices */ 29 | GPIO_PIN_REMAP(PA11_PA12); 30 | 31 | #ifdef RCC_HSI48_SUPPORT 32 | USB_vClockConfig(USB_CLOCKSOURCE_HSI48); 33 | #else 34 | USB_vClockConfig(USB_CLOCKSOURCE_PLL); 35 | #endif 36 | 37 | GPIO_vInitPin(USB_DM_PIN, USB_DM_CFG); 38 | GPIO_vInitPin(USB_DP_PIN, USB_DP_CFG); 39 | USB_INST2HANDLE(dev, USB); 40 | } 41 | -------------------------------------------------------------------------------- /BSP_STM32F3xx/bsp_io.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_io.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for I/O pins 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __BSP_IO_H_ 24 | #define __BSP_IO_H_ 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | #include 32 | 33 | #define USB_DP_PIN PA12 34 | #define USB_DM_PIN PA11 35 | #define USB_DP_CFG (&BSP_IOCfg[0]) 36 | #define USB_DM_CFG (&BSP_IOCfg[0]) 37 | #define USB_CONNECT_CFG (&BSP_IOCfg[1]) 38 | 39 | extern const GPIO_InitType BSP_IOCfg[]; 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | #endif /* __BSP_IO_H_ */ 46 | -------------------------------------------------------------------------------- /BSP_STM32F4xx/bsp_io.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_io.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for I/O pins 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __BSP_IO_H_ 24 | #define __BSP_IO_H_ 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | #include 32 | 33 | #define USB_DP_PIN PA12 34 | #define USB_DM_PIN PA11 35 | #define USB_VBUS_PIN PA9 36 | #define USB_DP_CFG (&BSP_IOCfg[0]) 37 | #define USB_DM_CFG (&BSP_IOCfg[0]) 38 | #define USB_VBUS_CFG (&BSP_IOCfg[0]) 39 | 40 | extern const GPIO_InitType BSP_IOCfg[]; 41 | 42 | #ifdef __cplusplus 43 | } 44 | #endif 45 | 46 | #endif /* __BSP_IO_H_ */ 47 | -------------------------------------------------------------------------------- /BSP_STM32F3xx/bsp_io.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_io.c 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for I/O pins 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #include 24 | 25 | const GPIO_InitType BSP_IOCfg[] = 26 | { 27 | /* USB pins */ 28 | { 29 | .Mode = GPIO_MODE_ALTERNATE, 30 | .Pull = GPIO_PULL_FLOAT, 31 | .Output.Type = GPIO_OUTPUT_PUSHPULL, 32 | .Output.Speed = VERY_HIGH, 33 | .AlternateMap = GPIO_USB_AF14 34 | }, 35 | /* GPIO outputs */ 36 | { 37 | .Mode = GPIO_MODE_OUTPUT, 38 | .Pull = GPIO_PULL_FLOAT, 39 | .Output.Type = GPIO_OUTPUT_PUSHPULL, 40 | .Output.Speed = HIGH, 41 | }, 42 | }; 43 | -------------------------------------------------------------------------------- /BSP_STM32L4xx/bsp_usb.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_usb.c 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for USB communication 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #include 24 | #include 25 | #include 26 | 27 | void BSP_USB_Bind(USB_HandleType *dev) 28 | { 29 | PWR_vVddUSB(ENABLE); 30 | GPIO_vInitPin(USB_DM_PIN, USB_DM_CFG); 31 | GPIO_vInitPin(USB_DP_PIN, USB_DP_CFG); 32 | 33 | #ifdef RCC_HSI48_SUPPORT 34 | USB_vClockConfig(USB_CLOCKSOURCE_HSI48); 35 | #else 36 | USB_vClockConfig(USB_CLOCKSOURCE_MSI); 37 | #endif 38 | #if defined(USB) 39 | USB_INST2HANDLE(dev, USB); 40 | #elif defined(USB_OTG_FS) 41 | USB_INST2HANDLE(dev, USB_OTG_FS); 42 | #endif 43 | } 44 | -------------------------------------------------------------------------------- /Main/usbd_config.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_config.h 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader USB Device library configuration 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #ifndef __USBD_CONFIG_H_ 24 | #define __USBD_CONFIG_H_ 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | /** @addtogroup USBD 32 | * @{ */ 33 | 34 | /** @addtogroup USBD_Exported_Macros USBD Exported Macros 35 | * @{ */ 36 | 37 | #define USBD_MAX_IF_COUNT 8 /* TODO must always match with application's setting */ 38 | 39 | #define USBD_EP0_BUFFER_SIZE 1024 40 | 41 | #define USBD_DFU_ALTSETTINGS 0 42 | 43 | #define USBD_DFU_MANIFEST_TOLERANT 0 44 | 45 | /** @} */ 46 | 47 | /** @} */ 48 | 49 | #ifdef __cplusplus 50 | } 51 | #endif 52 | 53 | #endif /* __USBD_CONFIG_H_ */ 54 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Credits for the build setup go to https://github.com/Jumperr-labs 2 | sudo: required 3 | 4 | language: ruby 5 | 6 | services: 7 | - docker 8 | 9 | before_install: 10 | - docker pull jumperio/vlab-gcc-arm:latest 11 | 12 | script: 13 | - docker run -v $PWD:/my_files_in_docker --entrypoint /usr/bin/make jumperio/vlab-gcc-arm -C my_files_in_docker TARGET_HEADER="\" SERIES=STM32F0 APP_ADDRESS=0x08002000 APP_SIZE=120*1024 TOTAL_ERASE_MS=4000 VID=FFFF PID=F072 14 | - docker run -v $PWD:/my_files_in_docker --entrypoint /usr/bin/make jumperio/vlab-gcc-arm -C my_files_in_docker TARGET_HEADER="\" SERIES=STM32F3 APP_ADDRESS=0x08002000 APP_SIZE=120*1024 TOTAL_ERASE_MS=4000 VID=FFFF PID=F302 HSE_HZ=12000000 DP_PIN="PA9" 15 | # the following targets only fit in 8kB flash when compiled with PREFIX=arm-atollic-eabi- 16 | #- docker run -v $PWD:/my_files_in_docker --entrypoint /usr/bin/make jumperio/vlab-gcc-arm -C my_files_in_docker TARGET_HEADER="\" SERIES=STM32F4 APP_ADDRESS=0x08004000 APP_SIZE=1008*1024 TOTAL_ERASE_MS=16000 VID=FFFF PID=F407 VDD_MV=3000 HSE_HZ=8000000 17 | - docker run -v $PWD:/my_files_in_docker --entrypoint /usr/bin/make jumperio/vlab-gcc-arm -C my_files_in_docker TARGET_HEADER="\" SERIES=STM32L4 APP_ADDRESS=0x08002000 APP_SIZE=1016*1024 TOTAL_ERASE_MS=12000 VID=FFFF PID=1476 18 | - docker run -v $PWD:/my_files_in_docker --entrypoint /usr/bin/make jumperio/vlab-gcc-arm -C my_files_in_docker TARGET_HEADER="\" SERIES=STM32L4 APP_ADDRESS=0x08002000 APP_SIZE=120*1024 TOTAL_ERASE_MS=4000 VID=FFFF PID=1432 19 | -------------------------------------------------------------------------------- /BSP_STM32F3xx/bsp_system.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_system.c 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for system clocking 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #include 24 | 25 | #include 26 | 27 | static const RCC_PLL_InitType pllconf = { 28 | .State = ENABLE, 29 | .Source = HSE, 30 | .Predivider = 1, 31 | .Multiplier = 72000000 / HSE_VALUE_Hz /* SYSCLK = 72000000 */ 32 | }; 33 | 34 | /* System clocks configuration */ 35 | void SystemClock_Config(void) 36 | { 37 | RCC_eHSE_Config(OSC_ON); 38 | RCC_ePLL_Config(&pllconf); 39 | 40 | /* System clocks configuration */ 41 | RCC_eHCLK_Config(PLL, CLK_DIV1, 2); 42 | 43 | RCC_vPCLK1_Config(CLK_DIV2); 44 | RCC_vPCLK2_Config(CLK_DIV1); 45 | } 46 | -------------------------------------------------------------------------------- /BSP_STM32F3xx/bsp_usb.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_usb.c 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for USB communication 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #include 24 | #include 25 | 26 | /* This STM32 line doesn't have built-in DP pull-up, 27 | * therefore an external 1.5kOhm resistor must be placed between USB DP 28 | * and a GPIO pin. 29 | */ 30 | static void usbAttachDetach(FunctionalState state) 31 | { 32 | GPIO_vWritePin(USB_CONNECT_PIN, state); 33 | } 34 | 35 | void BSP_USB_Bind(USB_HandleType *dev) 36 | { 37 | GPIO_vInitPin(USB_DM_PIN, USB_DM_CFG); 38 | GPIO_vInitPin(USB_DP_PIN, USB_DP_CFG); 39 | GPIO_vInitPin(USB_CONNECT_PIN, USB_CONNECT_CFG); 40 | USB_INST2HANDLE(dev, USB); 41 | dev->Callbacks.ConnectCtrl = usbAttachDetach; 42 | } 43 | -------------------------------------------------------------------------------- /BSP_STM32F4xx/bsp_system.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_system.c 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for system clocking 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #include 24 | #include 25 | 26 | static const RCC_PLL_InitType pllconf = { 27 | .State = ENABLE, 28 | .Source = HSE, 29 | .M = HSE_VALUE_Hz / 1000000, 30 | .N = 336, 31 | .P = 4, /* 1 * 336 / 4 = PLLP = 84000000 -> SYSCLK */ 32 | .Q = 7 /* 1 * 336 / 7 = PLLQ = 48000000 -> USB */ 33 | }; 34 | 35 | /* System clocks configuration */ 36 | void SystemClock_Config(void) 37 | { 38 | RCC_eHSE_Config(OSC_ON); 39 | RCC_ePLL_Config(&pllconf); 40 | 41 | /* System clocks configuration */ 42 | RCC_eHCLK_Config(PLL, CLK_DIV1, 3); 43 | 44 | RCC_vPCLK1_Config(CLK_DIV2); 45 | RCC_vPCLK2_Config(CLK_DIV1); 46 | } 47 | -------------------------------------------------------------------------------- /BSP_STM32L4xx/bsp_system.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_system.c 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for system clocking 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #include 24 | #include 25 | #ifdef RCC_HSI48_SUPPORT 26 | #include 27 | 28 | static const CRS_InitType crsSetup = { 29 | .Source = CRS_SYNC_SOURCE_USB, 30 | .ErrorLimit = CRS_ERRORLIMIT_DEFAULT, 31 | }; 32 | #endif 33 | 34 | static const RCC_MSI_InitType msiconf = { 35 | .ClockFreq = MSI_48MHz, 36 | .State = ENABLE 37 | }; 38 | 39 | /* System clocks configuration */ 40 | void SystemClock_Config(void) 41 | { 42 | RCC_eMSI_Config(&msiconf); 43 | 44 | #if (LSE_VALUE_Hz == 32768) 45 | /* Use LSE to synchronize MSI */ 46 | RCC_eLSE_Config(OSC_ON); 47 | #endif 48 | 49 | #ifdef RCC_HSI48_SUPPORT 50 | /* HSI48 configuration */ 51 | RCC_eHSI48_Enable(); 52 | CRS_vInit(&crsSetup); 53 | #endif 54 | 55 | /* System clocks configuration is the default, optimize calls */ 56 | #if 0 57 | RCC_eHCLK_Config(MSI, CLK_DIV1, 2); 58 | 59 | RCC_vPCLK1_Config(CLK_DIV1); 60 | RCC_vPCLK2_Config(CLK_DIV1); 61 | #endif 62 | } 63 | -------------------------------------------------------------------------------- /BSP_STM32F0xx/bsp_system.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file bsp_system.c 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader BSP for system clocking 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #include 24 | #include 25 | #ifdef RCC_HSI48_SUPPORT 26 | #include 27 | 28 | static const CRS_InitType crsSetup = { 29 | .Source = CRS_SYNC_SOURCE_USB, 30 | .ErrorLimit = CRS_ERRORLIMIT_DEFAULT, 31 | }; 32 | #else 33 | 34 | static const RCC_PLL_InitType pllconf = { 35 | .State = ENABLE, 36 | .Source = HSE, 37 | .Predivider = 1, 38 | .Multiplier = 48000000 / HSE_VALUE_Hz 39 | }; 40 | #endif 41 | 42 | /* System clocks configuration */ 43 | void SystemClock_Config(void) 44 | { 45 | #ifdef RCC_HSI48_SUPPORT 46 | /* HSI48 configuration */ 47 | RCC_eHSI48_Enable(); 48 | CRS_vInit(&crsSetup); 49 | 50 | /* System clocks configuration */ 51 | RCC_eHCLK_Config(HSI48, CLK_DIV1, 1); 52 | #else 53 | RCC_eHSE_Config(OSC_ON); 54 | RCC_ePLL_Config(&pllconf); 55 | 56 | /* System clocks configuration */ 57 | RCC_eHCLK_Config(PLL, CLK_DIV1, 1); 58 | #endif 59 | 60 | /* System clocks configuration is the default, optimize calls */ 61 | #if 0 62 | RCC_vPCLK1_Config(CLK_DIV1); 63 | #endif 64 | } 65 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DFU Bootloader [![Build Status](https://travis-ci.org/IntergatedCircuits/DfuBootloader.svg?branch=master)](https://travis-ci.org/IntergatedCircuits/DfuBootloader) 2 | 3 | This repository contains a generic USB device bootloader firmware for STM32 controllers. 4 | 5 | ## Features 6 | 7 | * Fully-featured DFU class (with support to STMicroelectronics protocol Extension) USB device 8 | * Small footprint: consumes only 8kB flash with `-Os` (50% smaller than the *STM32Cube* solution) 9 | * The DFU interface is allocated to a fixed address, so it can be mounted on the application's USB device, 10 | allowing convenient entry to firmware update mode 11 | * Easy to port on virtually any STM32 device that is supported by [STM32_XPD][STM32_XPD] 12 | * C# CLI application available for performing the update: [LibUsbDfu][LibUsbDfu] 13 | 14 | ## How to build 15 | 16 | Simply run `make` with the following arguments: 17 | 18 | | Symbol Name | Use 19 | | :----------------- | :------------------------ 20 | | `SERIES` | The series of the STM32 device 21 | | `TARGET_HEADER` | String of the device-specific *STM32_XPD* header 22 | | `APP_ADDRESS` | Value of the application's flash start address 23 | | `APP_SIZE` | The total available flash space for the application in bytes 24 | | `TOTAL_ERASE_MS` | Average erase time of the application flash area 25 | | `VID` | Vendor ID of the USB device in hexadecimal format (no prefix) 26 | | `PID` | Product ID of the USB device in hexadecimal format (no prefix) 27 | | `DFUSE` | Set to `1` if [DFUSE][DFUSE] protocol is desired 28 | | `DESC_STR` | DFUSE specific flash layout descriptor string 29 | | `BINPATH` | Path to build toolchain binaries (/usr/bin by default) 30 | 31 | An example for an STM32F042K6 device with (VID,PID)={FFFF,FFFF}: 32 | 33 | `make TARGET_HEADER="\" SERIES=STM32F0 APP_ADDRESS=0x08002000 APP_SIZE=24568 TOTAL_ERASE_MS=480 VID=FFFF PID=FFFF` 34 | 35 | Further examples can be found in `.travis.yml`. 36 | 37 | Built with GCC. 38 | (arm-atollic-eabi- is preferred over arm-none-eabi- as it provides better size optimization) 39 | 40 | [STM32_XPD]: https://github.com/IntergatedCircuits/STM32_XPD 41 | [USBDevice]: https://github.com/IntergatedCircuits/USBDevice 42 | [DFUSE]: www.st.com/resource/en/application_note/cd00264379.pdf 43 | [LibUsbDfu]: https://github.com/IntergatedCircuits/LibUsbDfu 44 | -------------------------------------------------------------------------------- /Main/main.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file main.c 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader main function 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #include 24 | #include 25 | #include 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | #include 33 | 34 | const USBD_DescriptionType hdev_cfg = { 35 | .Vendor = { 36 | .Name = "IntergatedCircuits", 37 | .ID = USBD_VID, 38 | }, 39 | .Product = { 40 | .Name = "DFU Bootloader", 41 | .ID = USBD_PID, 42 | .Version.bcd = 0x0100, 43 | }, 44 | .SerialNumber = (USBD_SerialNumberType*)DEVICE_ID_REG, 45 | .Config = { 46 | .Name = "DFU Bootloader", 47 | .MaxCurrent_mA = 100, 48 | .RemoteWakeup = 0, 49 | .SelfPowered = 0, 50 | }, 51 | }, *const dev_cfg = &hdev_cfg; 52 | 53 | uint32_t SystemCoreClock; 54 | USBD_HandleType husbd, *const usbd = &husbd; 55 | 56 | USBD_DFU_IfHandleType __attribute__((section (".dfuSharedSection"))) hdfu_if; 57 | USBD_DFU_IfHandleType *const dfu_if = &hdfu_if; 58 | 59 | /** 60 | * @brief This function resets the device to known initial state, 61 | * determines and starts the boot target (bootloader / application) 62 | * @return It doesn't 63 | */ 64 | int main(void) 65 | { 66 | /* Reset hardware to default state */ 67 | XPD_vDeinit(); 68 | RCC_vDeinit(); 69 | 70 | /* As the DFU interface is used by the application as well, 71 | * initialize it before going anywhere */ 72 | USBD_DFU_BootInit(dfu_if, (USBD_DFU_RebootCbkType)NVIC_SystemReset, flash_if, 1); 73 | 74 | /* Enter App when DFU isn't requested and App is present */ 75 | if (!USBD_DFU_IsRequested(dfu_if) && FlashIf_ApplicationPresent()) 76 | { 77 | void (*runApplication)(void) = *((const void **)(FLASH_APP_ADDRESS + 4)); 78 | 79 | /* Set the main stack pointer */ 80 | __set_MSP(*((uint32_t *)FLASH_APP_ADDRESS)); 81 | 82 | /* Jump to application */ 83 | runApplication(); 84 | } 85 | 86 | /* Otherwise start bootloader */ 87 | XPD_vInit(); 88 | 89 | FLASH_vPrefetchBuffer(ENABLE); 90 | 91 | BSP_USB_Bind(usbd); 92 | 93 | SystemClock_Config(); 94 | 95 | /* Mount the interface to the device */ 96 | USBD_DFU_MountInterface(dfu_if, usbd); 97 | 98 | /* Initialize the device */ 99 | USBD_Init(usbd, dev_cfg); 100 | 101 | USBD_Connect(usbd); 102 | 103 | while(1) 104 | { 105 | /* No interrupts are used, USB events are handled from here */ 106 | USB_vIRQHandler(usbd); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /Main/stm32_cmn_flash.ld: -------------------------------------------------------------------------------- 1 | 2 | /* Entry Point */ 3 | ENTRY(Reset_Handler) 4 | 5 | /* Highest address of the user mode stack */ 6 | _estack = 0x20001000;/* end of 4K RAM */ 7 | 8 | /* Generate a link error if heap and stack don't fit into RAM */ 9 | _Min_Heap_Size = 0; /* required amount of heap */ 10 | _Min_Stack_Size = 0x100; /* required amount of stack */ 11 | 12 | /* Specify the memory areas */ 13 | MEMORY 14 | { 15 | FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 8K 16 | RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 4K 17 | } 18 | 19 | /* Define output sections */ 20 | SECTIONS 21 | { 22 | /* The startup code goes first into FLASH */ 23 | .isr_vector : 24 | { 25 | . = ALIGN(4); 26 | KEEP(*(.isr_vector)) /* Startup code */ 27 | . = ALIGN(4); 28 | } >FLASH 29 | 30 | /* The program code and other data goes into FLASH */ 31 | .text : 32 | { 33 | . = ALIGN(4); 34 | *(.text) /* .text sections (code) */ 35 | *(.text*) /* .text* sections (code) */ 36 | *(.glue_7) /* glue arm to thumb code */ 37 | *(.glue_7t) /* glue thumb to arm code */ 38 | *(.eh_frame) 39 | 40 | KEEP (*(.init)) 41 | KEEP (*(.fini)) 42 | 43 | . = ALIGN(4); 44 | _etext = .; /* define a global symbols at end of code */ 45 | } >FLASH 46 | 47 | /* Constant data goes into FLASH */ 48 | .rodata : 49 | { 50 | . = ALIGN(4); 51 | *(.rodata) /* .rodata sections (constants, strings, etc.) */ 52 | *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ 53 | . = ALIGN(4); 54 | } >FLASH 55 | 56 | .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH 57 | .ARM : { 58 | __exidx_start = .; 59 | *(.ARM.exidx*) 60 | __exidx_end = .; 61 | } >FLASH 62 | 63 | .preinit_array : 64 | { 65 | PROVIDE_HIDDEN (__preinit_array_start = .); 66 | KEEP (*(.preinit_array*)) 67 | PROVIDE_HIDDEN (__preinit_array_end = .); 68 | } >FLASH 69 | .init_array : 70 | { 71 | PROVIDE_HIDDEN (__init_array_start = .); 72 | KEEP (*(SORT(.init_array.*))) 73 | KEEP (*(.init_array*)) 74 | PROVIDE_HIDDEN (__init_array_end = .); 75 | } >FLASH 76 | .fini_array : 77 | { 78 | PROVIDE_HIDDEN (__fini_array_start = .); 79 | KEEP (*(SORT(.fini_array.*))) 80 | KEEP (*(.fini_array*)) 81 | PROVIDE_HIDDEN (__fini_array_end = .); 82 | } >FLASH 83 | 84 | /* RAM variables for DFU */ 85 | .shared_ram 0x20000000 (NOLOAD): 86 | { 87 | KEEP(*(.dfuSharedSection)) 88 | } >RAM 89 | 90 | /* used by the startup to initialize data */ 91 | _sidata = LOADADDR(.data); 92 | 93 | /* Initialized data sections goes into RAM, load LMA copy after code */ 94 | .data : 95 | { 96 | . = ALIGN(4); 97 | _sdata = .; /* create a global symbol at data start */ 98 | *(.data) /* .data sections */ 99 | *(.data*) /* .data* sections */ 100 | 101 | . = ALIGN(4); 102 | _edata = .; /* define a global symbol at data end */ 103 | } >RAM AT> FLASH 104 | 105 | /* Uninitialized data section */ 106 | . = ALIGN(4); 107 | .bss : 108 | { 109 | /* This is used by the startup in order to initialize the .bss secion */ 110 | _sbss = .; /* define a global symbol at bss start */ 111 | __bss_start__ = _sbss; 112 | *(.bss) 113 | *(.bss*) 114 | *(COMMON) 115 | 116 | . = ALIGN(4); 117 | _ebss = .; /* define a global symbol at bss end */ 118 | __bss_end__ = _ebss; 119 | } >RAM 120 | 121 | /* User_heap_stack section, used to check that there is enough RAM left */ 122 | ._user_heap_stack : 123 | { 124 | . = ALIGN(4); 125 | PROVIDE ( end = . ); 126 | PROVIDE ( _end = . ); 127 | . = . + _Min_Heap_Size; 128 | . = . + _Min_Stack_Size; 129 | . = ALIGN(4); 130 | } >RAM 131 | 132 | /* Remove information from the standard libraries */ 133 | /DISCARD/ : 134 | { 135 | libc.a ( * ) 136 | libm.a ( * ) 137 | libgcc.a ( * ) 138 | } 139 | 140 | .ARM.attributes 0 : { *(.ARM.attributes) } 141 | } 142 | -------------------------------------------------------------------------------- /Main/stm32_cm0_flash.ld: -------------------------------------------------------------------------------- 1 | /* Use this linker for Cortex M0 core devices without SCB->VTOR register, 2 | * where the application's vector table has to be relocated to SRAM */ 3 | 4 | /* Entry Point */ 5 | ENTRY(Reset_Handler) 6 | 7 | /* Highest address of the user mode stack */ 8 | _estack = 0x20001000;/* end of 4K RAM */ 9 | 10 | /* Generate a link error if heap and stack don't fit into RAM */ 11 | _Min_Heap_Size = 0; /* required amount of heap */ 12 | _Min_Stack_Size = 0x100; /* required amount of stack */ 13 | 14 | /* Specify the memory areas */ 15 | MEMORY 16 | { 17 | FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 8K 18 | RAM (xrw) : ORIGIN = 0x200000C0, LENGTH = 3904 19 | } 20 | 21 | /* Define output sections */ 22 | SECTIONS 23 | { 24 | /* The startup code goes first into FLASH */ 25 | .isr_vector : 26 | { 27 | . = ALIGN(4); 28 | KEEP(*(.isr_vector)) /* Startup code */ 29 | . = ALIGN(4); 30 | } >FLASH 31 | 32 | /* The program code and other data goes into FLASH */ 33 | .text : 34 | { 35 | . = ALIGN(4); 36 | *(.text) /* .text sections (code) */ 37 | *(.text*) /* .text* sections (code) */ 38 | *(.glue_7) /* glue arm to thumb code */ 39 | *(.glue_7t) /* glue thumb to arm code */ 40 | *(.eh_frame) 41 | 42 | KEEP (*(.init)) 43 | KEEP (*(.fini)) 44 | 45 | . = ALIGN(4); 46 | _etext = .; /* define a global symbols at end of code */ 47 | } >FLASH 48 | 49 | /* Constant data goes into FLASH */ 50 | .rodata : 51 | { 52 | . = ALIGN(4); 53 | *(.rodata) /* .rodata sections (constants, strings, etc.) */ 54 | *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ 55 | . = ALIGN(4); 56 | } >FLASH 57 | 58 | .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH 59 | .ARM : { 60 | __exidx_start = .; 61 | *(.ARM.exidx*) 62 | __exidx_end = .; 63 | } >FLASH 64 | 65 | .preinit_array : 66 | { 67 | PROVIDE_HIDDEN (__preinit_array_start = .); 68 | KEEP (*(.preinit_array*)) 69 | PROVIDE_HIDDEN (__preinit_array_end = .); 70 | } >FLASH 71 | .init_array : 72 | { 73 | PROVIDE_HIDDEN (__init_array_start = .); 74 | KEEP (*(SORT(.init_array.*))) 75 | KEEP (*(.init_array*)) 76 | PROVIDE_HIDDEN (__init_array_end = .); 77 | } >FLASH 78 | .fini_array : 79 | { 80 | PROVIDE_HIDDEN (__fini_array_start = .); 81 | KEEP (*(SORT(.fini_array.*))) 82 | KEEP (*(.fini_array*)) 83 | PROVIDE_HIDDEN (__fini_array_end = .); 84 | } >FLASH 85 | 86 | /* RAM variables for DFU */ 87 | .shared_ram 0x200000C0 (NOLOAD): 88 | { 89 | KEEP(*(.dfuSharedSection)) 90 | } >RAM 91 | 92 | /* used by the startup to initialize data */ 93 | _sidata = LOADADDR(.data); 94 | 95 | /* Initialized data sections goes into RAM, load LMA copy after code */ 96 | .data : 97 | { 98 | . = ALIGN(4); 99 | _sdata = .; /* create a global symbol at data start */ 100 | *(.data) /* .data sections */ 101 | *(.data*) /* .data* sections */ 102 | 103 | . = ALIGN(4); 104 | _edata = .; /* define a global symbol at data end */ 105 | } >RAM AT> FLASH 106 | 107 | /* Uninitialized data section */ 108 | . = ALIGN(4); 109 | .bss : 110 | { 111 | /* This is used by the startup in order to initialize the .bss secion */ 112 | _sbss = .; /* define a global symbol at bss start */ 113 | __bss_start__ = _sbss; 114 | *(.bss) 115 | *(.bss*) 116 | *(COMMON) 117 | 118 | . = ALIGN(4); 119 | _ebss = .; /* define a global symbol at bss end */ 120 | __bss_end__ = _ebss; 121 | } >RAM 122 | 123 | /* User_heap_stack section, used to check that there is enough RAM left */ 124 | ._user_heap_stack : 125 | { 126 | . = ALIGN(4); 127 | PROVIDE ( end = . ); 128 | PROVIDE ( _end = . ); 129 | . = . + _Min_Heap_Size; 130 | . = . + _Min_Stack_Size; 131 | . = ALIGN(4); 132 | } >RAM 133 | 134 | /* Remove information from the standard libraries */ 135 | /DISCARD/ : 136 | { 137 | libc.a ( * ) 138 | libm.a ( * ) 139 | libgcc.a ( * ) 140 | } 141 | 142 | .ARM.attributes 0 : { *(.ARM.attributes) } 143 | } 144 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ########++++++++++++++++++++++++++++++++++++++######## 2 | ##++---- Makefile for DfuBootloader project ----++## 3 | ########++++++++++++++++++++++++++++++++++++++######## 4 | 5 | # optimization 6 | OPT = -Os 7 | DEBUG = 1 8 | 9 | TARGET = DfuBootloader 10 | 11 | # Build path 12 | BUILD_DIR = build_$(VID)_$(PID) 13 | 14 | ##++---- Target configuration ----++## 15 | 16 | ifeq ($(SERIES),STM32L0) 17 | CORE = m0plus 18 | else ifeq ($(SERIES),STM32F0) 19 | CORE = m0 20 | PROGRAM_US = 30 21 | ERASE_MS = 30 22 | else ifeq ($(SERIES),STM32F1) 23 | CORE = m3 24 | else ifeq ($(SERIES),STM32L1) 25 | CORE = m3 26 | else ifeq ($(SERIES),STM32F2) 27 | CORE = m3 28 | else ifeq ($(SERIES),STM32F3) 29 | CORE = m4 30 | PROGRAM_US = 30 31 | ERASE_MS = 30 32 | else ifeq ($(SERIES),STM32F4) 33 | CORE = m4 34 | PROGRAM_US = 25 35 | ERASE_MS = 2000 36 | else ifeq ($(SERIES),STM32L4) 37 | CORE = m4 38 | PROGRAM_US = 10 39 | ERASE_MS = 25 40 | else ifeq ($(SERIES),STM32F7) 41 | CORE = m7 42 | else ifeq ($(SERIES),STM32H7) 43 | CORE = m7 44 | endif 45 | 46 | ifeq ($(CORE),m0) 47 | DEVICE_GROUP = stm32_cm0 48 | else 49 | DEVICE_GROUP = stm32_cmn 50 | endif 51 | 52 | # safe default values 53 | BSP = BSP_$(SERIES)xx 54 | VDD_MV = 2000 55 | 56 | C_DEFS = \ 57 | -DFLASH_APP_ADDRESS=$(APP_ADDRESS) \ 58 | -DFLASH_APP_SIZE=$(APP_SIZE) \ 59 | -DFLASH_BYTE_PROGRAM_TIME_us=$(PROGRAM_US) \ 60 | -DUSBD_VID=0x$(VID) \ 61 | -DUSBD_PID=0x$(PID) \ 62 | -DVDD_VALUE_mV=$(VDD_MV) \ 63 | -DSTM32_TARGET_HEADER=$(TARGET_HEADER) 64 | 65 | ifdef HSE_HZ 66 | C_DEFS += -DHSE_VALUE_Hz=$(HSE_HZ) 67 | endif 68 | 69 | ifdef DP_PIN 70 | C_DEFS += -DUSB_CONNECT_PIN=$(DP_PIN) 71 | endif 72 | 73 | ifdef DFUSE 74 | C_DEFS += \ 75 | -DUSBD_DFU_ST_EXTENSION=$(DFUSE) \ 76 | -DFLASH_ERASE_TIME_ms=$(ERASE_MS) \ 77 | -DSE_FLASH_DESC_STR=$(DESC_STR) 78 | else 79 | C_DEFS += -DFLASH_TOTAL_ERASE_TIME_ms=$(TOTAL_ERASE_MS) 80 | endif 81 | 82 | ##++---- Build tool binaries ----++## 83 | BINPATH = /usr/bin 84 | PREFIX = arm-none-eabi- 85 | CC = $(BINPATH)/$(PREFIX)gcc 86 | AS = $(BINPATH)/$(PREFIX)gcc -x assembler-with-cpp 87 | CP = $(BINPATH)/$(PREFIX)objcopy 88 | AR = $(BINPATH)/$(PREFIX)ar 89 | SZ = $(BINPATH)/$(PREFIX)size 90 | HEX = $(CP) -O ihex 91 | BIN = $(CP) -O binary -S 92 | 93 | 94 | ##++---- MCU config ----++## 95 | CPU = -mcpu=cortex-$(CORE) 96 | MCU = $(CPU) -mthumb -mfloat-abi=soft 97 | 98 | 99 | ##++---- Assembler ----++## 100 | AS_DEFS = 101 | AS_INCLUDES = 102 | # assembly sources 103 | AS_SOURCES = Main/$(DEVICE_GROUP)_startup.s 104 | 105 | # assembler flags 106 | ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections 107 | 108 | 109 | ##++---- Compiler ----++## 110 | C_STANDARD = -std=gnu11 111 | 112 | XPD_DIR = STM32_XPD 113 | USBD_DIR = USBDevice 114 | 115 | # All USBDevice classes are built, but only DFU is used 116 | # C includes 117 | C_INCLUDES = \ 118 | -I$(BSP) \ 119 | -IMain \ 120 | -I$(USBD_DIR)/Include \ 121 | -I$(USBD_DIR)/PDs/STM32_XPD \ 122 | -I$(XPD_DIR)/CMSIS/Include \ 123 | -I$(XPD_DIR)/CMSIS/Device/ST/$(SERIES)xx/Include \ 124 | -I$(XPD_DIR)/$(SERIES)_XPD/inc 125 | 126 | # C sources 127 | C_SOURCES = \ 128 | $(wildcard $(BSP)/*.c) \ 129 | $(wildcard Main/*.c) \ 130 | $(wildcard $(USBD_DIR)/Device/*.c) \ 131 | $(wildcard $(USBD_DIR)/Class/DFU/*.c) \ 132 | $(wildcard $(XPD_DIR)/$(SERIES)_XPD/src/*.c) 133 | 134 | # compiler flags 135 | CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections $(C_STANDARD) 136 | 137 | ifeq ($(DEBUG), 1) 138 | CFLAGS += -g -gdwarf-2 139 | endif 140 | 141 | # Generate dependency information 142 | CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" 143 | 144 | 145 | ##++---- Linker ----++## 146 | # link script 147 | LDSCRIPT = Main/$(DEVICE_GROUP)_flash.ld 148 | 149 | # libraries 150 | LIBS = -lc -lm -lnosys 151 | LIBDIR = 152 | LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections 153 | 154 | 155 | ##++---- Build the application ----++## 156 | # default action: build all 157 | all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex 158 | 159 | # list of objects 160 | OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o))) 161 | vpath %.c $(sort $(dir $(C_SOURCES))) 162 | # list of ASM program objects 163 | OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(AS_SOURCES:.s=.o))) 164 | vpath %.s $(sort $(dir $(AS_SOURCES))) 165 | 166 | $(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 167 | $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ 168 | 169 | $(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR) 170 | $(AS) -c $(CFLAGS) $< -o $@ 171 | 172 | $(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile 173 | $(CC) $(OBJECTS) $(LDFLAGS) -o $@ 174 | $(SZ) $@ 175 | 176 | $(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR) 177 | $(HEX) $< $@ 178 | 179 | $(BUILD_DIR): 180 | mkdir $@ 181 | 182 | ##++---- Clean ----++## 183 | clean: 184 | -rm -fR .dep $(BUILD_DIR) 185 | 186 | 187 | ##++---- Dependencies ----++## 188 | -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) 189 | 190 | # *** EOF *** 191 | -------------------------------------------------------------------------------- /Main/stm32_cm0_startup.s: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32_cm0_startup.s 4 | * @author MCD Application Team 5 | * @version V2.3.0 6 | * @date 27-May-2016 7 | * @brief STM32 with Cortex M0 core startup code for GCC based toolchains. 8 | * This module performs: 9 | * - Set the initial SP 10 | * - Set the initial PC == Reset_Handler, 11 | * - Set the vector table entries with the exceptions ISR address 12 | * - Branches to main in the C library (which eventually 13 | * calls main()). 14 | * After Reset the Cortex-M0 processor is in Thread mode, 15 | * priority is Privileged, and the Stack is set to Main. 16 | ****************************************************************************** 17 | * 18 | * Redistribution and use in source and binary forms, with or without modification, 19 | * are permitted provided that the following conditions are met: 20 | * 1. Redistributions of source code must retain the above copyright notice, 21 | * this list of conditions and the following disclaimer. 22 | * 2. Redistributions in binary form must reproduce the above copyright notice, 23 | * this list of conditions and the following disclaimer in the documentation 24 | * and/or other materials provided with the distribution. 25 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 26 | * may be used to endorse or promote products derived from this software 27 | * without specific prior written permission. 28 | * 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 30 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 32 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 33 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 36 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 37 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 38 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39 | * 40 | ****************************************************************************** 41 | */ 42 | 43 | .syntax unified 44 | .cpu cortex-m0 45 | .fpu softvfp 46 | .thumb 47 | 48 | .global g_pfnVectors 49 | .global Default_Handler 50 | 51 | /* start address for the initialization values of the .data section. 52 | defined in linker script */ 53 | .word _sidata 54 | /* start address for the .data section. defined in linker script */ 55 | .word _sdata 56 | /* end address for the .data section. defined in linker script */ 57 | .word _edata 58 | /* start address for the .bss section. defined in linker script */ 59 | .word _sbss 60 | /* end address for the .bss section. defined in linker script */ 61 | .word _ebss 62 | 63 | .section .text.Reset_Handler 64 | .weak Reset_Handler 65 | .type Reset_Handler, %function 66 | Reset_Handler: 67 | ldr r0, =_estack 68 | mov sp, r0 /* set stack pointer */ 69 | 70 | /* Copy the data segment initializers from flash to SRAM */ 71 | movs r1, #0 72 | b LoopCopyDataInit 73 | 74 | CopyDataInit: 75 | ldr r3, =_sidata 76 | ldr r3, [r3, r1] 77 | str r3, [r0, r1] 78 | adds r1, r1, #4 79 | 80 | LoopCopyDataInit: 81 | ldr r0, =_sdata 82 | ldr r3, =_edata 83 | adds r2, r0, r1 84 | cmp r2, r3 85 | bcc CopyDataInit 86 | ldr r2, =_sbss 87 | b LoopFillZerobss 88 | /* Zero fill the bss segment. */ 89 | FillZerobss: 90 | movs r3, #0 91 | str r3, [r2] 92 | adds r2, r2, #4 93 | 94 | 95 | LoopFillZerobss: 96 | ldr r3, = _ebss 97 | cmp r2, r3 98 | bcc FillZerobss 99 | 100 | /* Call static constructors */ 101 | bl __libc_init_array 102 | /* Call the application's entry point.*/ 103 | bl main 104 | 105 | LoopForever: 106 | b LoopForever 107 | 108 | 109 | .size Reset_Handler, .-Reset_Handler 110 | 111 | /** 112 | * @brief This is the code that gets called when the processor receives an 113 | * unexpected interrupt. This simply enters an infinite loop, preserving 114 | * the system state for examination by a debugger. 115 | * 116 | * @param None 117 | * @retval : None 118 | */ 119 | .section .text.Default_Handler,"ax",%progbits 120 | Default_Handler: 121 | Infinite_Loop: 122 | b Infinite_Loop 123 | .size Default_Handler, .-Default_Handler 124 | /****************************************************************************** 125 | * 126 | * The minimal vector table for a Cortex M0. Note that the proper constructs 127 | * must be placed on this to ensure that it ends up at physical address 128 | * 0x0000.0000. 129 | * 130 | ******************************************************************************/ 131 | .section .isr_vector,"a",%progbits 132 | .type g_pfnVectors, %object 133 | .size g_pfnVectors, .-g_pfnVectors 134 | 135 | 136 | g_pfnVectors: 137 | .word _estack 138 | .word Reset_Handler 139 | .word NMI_Handler 140 | .word HardFault_Handler 141 | .word 0 142 | .word 0 143 | .word 0 144 | .word 0 145 | .word 0 146 | .word 0 147 | .word 0 148 | .word SVC_Handler 149 | .word 0 150 | .word 0 151 | .word PendSV_Handler 152 | .word SysTick_Handler 153 | 154 | /******************************************************************************* 155 | * 156 | * Provide weak aliases for each Exception handler to the Default_Handler. 157 | * As they are weak aliases, any function with the same name will override 158 | * this definition. 159 | * 160 | *******************************************************************************/ 161 | 162 | .weak NMI_Handler 163 | .thumb_set NMI_Handler,Default_Handler 164 | 165 | .weak HardFault_Handler 166 | .thumb_set HardFault_Handler,Default_Handler 167 | 168 | .weak SVC_Handler 169 | .thumb_set SVC_Handler,Default_Handler 170 | 171 | .weak PendSV_Handler 172 | .thumb_set PendSV_Handler,Default_Handler 173 | 174 | .weak SysTick_Handler 175 | .thumb_set SysTick_Handler,Default_Handler 176 | -------------------------------------------------------------------------------- /Main/stm32_cmn_startup.s: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32_cmn_startup.s 4 | * @author MCD Application Team 5 | * @version V2.4.2 6 | * @date 13-November-2015 7 | * @brief STM32 with Cortex M<0 core startup code for GCC based toolchains. 8 | * This module performs: 9 | * - Set the initial SP 10 | * - Set the initial PC == Reset_Handler, 11 | * - Set the vector table entries with the exceptions ISR address 12 | * - Branches to main in the C library (which eventually 13 | * calls main()). 14 | * After Reset the Cortex-M4 processor is in Thread mode, 15 | * priority is Privileged, and the Stack is set to Main. 16 | ****************************************************************************** 17 | * @attention 18 | * 19 | *

© COPYRIGHT 2015 STMicroelectronics

20 | * 21 | * Redistribution and use in source and binary forms, with or without modification, 22 | * are permitted provided that the following conditions are met: 23 | * 1. Redistributions of source code must retain the above copyright notice, 24 | * this list of conditions and the following disclaimer. 25 | * 2. Redistributions in binary form must reproduce the above copyright notice, 26 | * this list of conditions and the following disclaimer in the documentation 27 | * and/or other materials provided with the distribution. 28 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 29 | * may be used to endorse or promote products derived from this software 30 | * without specific prior written permission. 31 | * 32 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 33 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 35 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 36 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 38 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 39 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 40 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 41 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 | * 43 | ****************************************************************************** 44 | */ 45 | 46 | .syntax unified 47 | .thumb 48 | 49 | .global g_pfnVectors 50 | .global Default_Handler 51 | 52 | /* start address for the initialization values of the .data section. 53 | defined in linker script */ 54 | .word _sidata 55 | /* start address for the .data section. defined in linker script */ 56 | .word _sdata 57 | /* end address for the .data section. defined in linker script */ 58 | .word _edata 59 | /* start address for the .bss section. defined in linker script */ 60 | .word _sbss 61 | /* end address for the .bss section. defined in linker script */ 62 | .word _ebss 63 | /* stack used for SystemInit_ExtMemCtl; always internal RAM used */ 64 | 65 | /** 66 | * @brief This is the code that gets called when the processor first 67 | * starts execution following a reset event. Only the absolutely 68 | * necessary set is performed, after which the application 69 | * supplied main() routine is called. 70 | * @param None 71 | * @retval : None 72 | */ 73 | 74 | .section .text.Reset_Handler 75 | .weak Reset_Handler 76 | .type Reset_Handler, %function 77 | Reset_Handler: 78 | ldr sp, =_estack /* set stack pointer */ 79 | 80 | /* Copy the data segment initializers from flash to SRAM */ 81 | movs r1, #0 82 | b LoopCopyDataInit 83 | 84 | CopyDataInit: 85 | ldr r3, =_sidata 86 | ldr r3, [r3, r1] 87 | str r3, [r0, r1] 88 | adds r1, r1, #4 89 | 90 | LoopCopyDataInit: 91 | ldr r0, =_sdata 92 | ldr r3, =_edata 93 | adds r2, r0, r1 94 | cmp r2, r3 95 | bcc CopyDataInit 96 | ldr r2, =_sbss 97 | b LoopFillZerobss 98 | /* Zero fill the bss segment. */ 99 | FillZerobss: 100 | movs r3, #0 101 | str r3, [r2], #4 102 | 103 | LoopFillZerobss: 104 | ldr r3, = _ebss 105 | cmp r2, r3 106 | bcc FillZerobss 107 | 108 | /* Call static constructors */ 109 | bl __libc_init_array 110 | /* Call the application's entry point.*/ 111 | bl main 112 | bx lr 113 | .size Reset_Handler, .-Reset_Handler 114 | 115 | /** 116 | * @brief This is the code that gets called when the processor receives an 117 | * unexpected interrupt. This simply enters an infinite loop, preserving 118 | * the system state for examination by a debugger. 119 | * @param None 120 | * @retval None 121 | */ 122 | .section .text.Default_Handler,"ax",%progbits 123 | Default_Handler: 124 | Infinite_Loop: 125 | b Infinite_Loop 126 | .size Default_Handler, .-Default_Handler 127 | /****************************************************************************** 128 | * 129 | * The minimal vector table for a Cortex M3. Note that the proper constructs 130 | * must be placed on this to ensure that it ends up at physical address 131 | * 0x0000.0000. 132 | * 133 | *******************************************************************************/ 134 | .section .isr_vector,"a",%progbits 135 | .type g_pfnVectors, %object 136 | .size g_pfnVectors, .-g_pfnVectors 137 | 138 | 139 | g_pfnVectors: 140 | .word _estack 141 | .word Reset_Handler 142 | .word NMI_Handler 143 | .word HardFault_Handler 144 | .word MemManage_Handler 145 | .word BusFault_Handler 146 | .word UsageFault_Handler 147 | .word 0 148 | .word 0 149 | .word 0 150 | .word 0 151 | .word SVC_Handler 152 | .word DebugMon_Handler 153 | .word 0 154 | .word PendSV_Handler 155 | .word SysTick_Handler 156 | 157 | /******************************************************************************* 158 | * 159 | * Provide weak aliases for each Exception handler to the Default_Handler. 160 | * As they are weak aliases, any function with the same name will override 161 | * this definition. 162 | * 163 | *******************************************************************************/ 164 | .weak NMI_Handler 165 | .thumb_set NMI_Handler,Default_Handler 166 | 167 | .weak HardFault_Handler 168 | .thumb_set HardFault_Handler,Default_Handler 169 | 170 | .weak MemManage_Handler 171 | .thumb_set MemManage_Handler,Default_Handler 172 | 173 | .weak BusFault_Handler 174 | .thumb_set BusFault_Handler,Default_Handler 175 | 176 | .weak UsageFault_Handler 177 | .thumb_set UsageFault_Handler,Default_Handler 178 | 179 | .weak SVC_Handler 180 | .thumb_set SVC_Handler,Default_Handler 181 | 182 | .weak DebugMon_Handler 183 | .thumb_set DebugMon_Handler,Default_Handler 184 | 185 | .weak PendSV_Handler 186 | .thumb_set PendSV_Handler,Default_Handler 187 | 188 | .weak SysTick_Handler 189 | .thumb_set SysTick_Handler,Default_Handler 190 | -------------------------------------------------------------------------------- /Main/flash_if.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file flash_if.c 4 | * @author Benedek Kupper 5 | * @version 0.1 6 | * @date 2018-05-21 7 | * @brief DfuBootloader flash memory interface 8 | * 9 | * Copyright (c) 2018 Benedek Kupper 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); 12 | * you may not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | */ 23 | #include 24 | #include 25 | 26 | static const struct 27 | { 28 | uint32_t dummy; /* Write double word for STM32L4 support */ 29 | uint32_t symbol; 30 | }manifSign = { 0xFFFFFFFF, FLASH_VALID_SYMBOL }; 31 | 32 | #if (USBD_DFU_ST_EXTENSION != 0) 33 | /** 34 | * @brief DFUSE layout descriptor strings 35 | * 36 | * DFUSE string descriptors structure: 37 | * @Name /StartAddress/Count*SizeProperty,Count*SizeProperty 38 | * 39 | * Property options: 40 | * @arg a: R 41 | * @arg g: REW 42 | * @arg e: RW 43 | * 44 | * Example strings: 45 | * @arg "@Internal Flash /0x08000000/4*2Ka,12*2Kg" 46 | * @arg "@OTP Memory /0x1FFF7800/1*512e,1*16e" 47 | * @arg "@Option Bytes /0x1FFFC000/1*16e" 48 | * @arg "@Device Feature /0xFFFF0000/1*4e" 49 | */ 50 | 51 | /** 52 | * @brief Erase one block at the specified address. 53 | * @param addr: start address of the block to erase 54 | * @return Result of the flash erase operation 55 | */ 56 | static USBD_DFU_StatusType SE_FlashIf_Erase(uint8_t *addr) 57 | { 58 | /* Erase flash memory block from the start address */ 59 | return (FLASH_eErase(addr, 1) == XPD_OK) ? 60 | DFU_ERROR_NONE : DFU_ERROR_ERASE; 61 | } 62 | 63 | /** 64 | * @brief Get approximating timeout for the upcoming flash operation. 65 | * @param addr: start address of the following operation 66 | * @param len: (byte) length of the operation 67 | * @return Expected time of next operation 68 | */ 69 | static uint16_t SE_FlashIf_GetTimeout_ms(uint8_t *addr, uint32_t len) 70 | { 71 | if (len == 5) /* Erase one block */ 72 | { 73 | return FLASH_ERASE_TIME_ms; 74 | } 75 | else /* Write single block */ 76 | { 77 | return ((FLASH_BYTE_PROGRAM_TIME_us * len) + 1000) / 1000; 78 | } 79 | } 80 | 81 | #else 82 | #define FLASH_ERASE_SIZE_kB ((FLASH_APP_SIZE) >> 10) /* Entire application (in kB) */ 83 | 84 | /** 85 | * @brief Erase entire application firmware 86 | * @param addr: start address of application 87 | * @return Result of the flash erase operation 88 | */ 89 | static USBD_DFU_StatusType FlashIf_Erase(uint8_t *addr) 90 | { 91 | return (FLASH_eErase(addr, FLASH_ERASE_SIZE_kB) == XPD_OK) ? 92 | DFU_ERROR_NONE : DFU_ERROR_ERASE; 93 | } 94 | 95 | /** 96 | * @brief Get approximating timeout for the upcoming flash operation(s). 97 | * @param addr: start address of the following operation(s) 98 | * @param len: (byte) length of the operation(s) 99 | * @return Expected time of next operation(s) 100 | */ 101 | static uint16_t FlashIf_GetTimeout_ms(uint8_t *addr, uint32_t len) 102 | { 103 | /* Erase whole FW, write first block */ 104 | if (addr == (uint8_t*)FLASH_APP_ADDRESS) 105 | { 106 | return FLASH_TOTAL_ERASE_TIME_ms + FLASH_BYTE_PROGRAM_TIME_us; 107 | } 108 | #if 0 109 | /* Manifestation */ 110 | else if (!(addr < ((uint8_t*)FLASH_APP_ADDRESS + FLASH_APP_SIZE))) 111 | { 112 | return ((FLASH_BYTE_PROGRAM_TIME_us * len) + 1000) / 1000; 113 | } 114 | #endif 115 | /* Write single block */ 116 | else 117 | { 118 | return ((FLASH_BYTE_PROGRAM_TIME_us * len) + 1000) / 1000; 119 | } 120 | } 121 | #endif 122 | 123 | /** 124 | * @brief Writes the passed data to the specified flash address. 125 | * @param addr: target address to write to 126 | * @param data: flash contents to write 127 | * @param len: amount of bytes to write 128 | * @return Result of the flash program operation 129 | */ 130 | static USBD_DFU_StatusType FlashIf_Write(uint8_t *addr, uint8_t *data, uint32_t len) 131 | { 132 | return (FLASH_eProgram(addr, data, len) == XPD_OK) ? 133 | DFU_ERROR_NONE : DFU_ERROR_WRITE; 134 | } 135 | 136 | /** 137 | * @brief Reads from the flash memory. 138 | * @param addr: source pointer 139 | * @param data: destination pointer 140 | * @param len: amount of bytes to read 141 | */ 142 | static void FlashIf_Read(uint8_t *addr, uint8_t *data, uint32_t len) 143 | { 144 | while (len-- > 0) 145 | *data++ = *addr++; 146 | } 147 | 148 | /** 149 | * @brief Check if the application header is correct 150 | * (word 0 = end of stack, word 1 = reset vector). 151 | * @return TRUE if application header is valid, FALSE otherwise 152 | */ 153 | static int FlashIf_AppHeaderValid(void) 154 | { 155 | /* End of stack is the first word of the application, 156 | * it has a limited valid range */ 157 | uint32_t stackEnd = *( (uint32_t*)FLASH_APP_ADDRESS); 158 | /* The reset vector should point to a valid flash area */ 159 | uint32_t resetVector = *(((uint32_t*)FLASH_APP_ADDRESS) + 1); 160 | 161 | return (((stackEnd & 0xFFF007FF) == 0x20000000) && 162 | (resetVector > (FLASH_APP_ADDRESS)) && 163 | (resetVector < (FLASH_BASE + FLASH_APP_SIZE - sizeof(manifSign)))); 164 | } 165 | 166 | /** 167 | * @brief Checks if the application manifest signature is valid. 168 | * @return TRUE if application signature is valid, FALSE otherwise 169 | */ 170 | static int FlashIf_AppSignValid(void) 171 | { 172 | /* The last word of the application is set to fixed value during manifestation */ 173 | uint32_t* appEnd = ((uint32_t*)(FLASH_APP_ADDRESS + FLASH_APP_SIZE 174 | - sizeof(FLASH_VALID_SYMBOL))); 175 | 176 | return (*appEnd == FLASH_VALID_SYMBOL); 177 | } 178 | 179 | /** 180 | * @brief Writes a validity signature to the end of the flash to indicate upgrade success. 181 | * @return Result of the flash program operation 182 | */ 183 | static USBD_DFU_StatusType FlashIf_Manifest(void) 184 | { 185 | USBD_DFU_StatusType retval = DFU_ERROR_NONE; 186 | uint32_t* pmanif = ((uint32_t*)(FLASH_APP_ADDRESS + FLASH_APP_SIZE 187 | - sizeof(manifSign))); 188 | 189 | if (!FlashIf_AppHeaderValid()) 190 | { 191 | retval = DFU_ERROR_VERIFY; 192 | } 193 | else if (FlashIf_AppSignValid()) 194 | { 195 | /* Skip rewrite of manifest */ 196 | } 197 | else if (XPD_OK != FLASH_eProgram(pmanif, 198 | (const uint8_t*)&manifSign, sizeof(manifSign))) 199 | { 200 | retval = DFU_ERROR_PROG; 201 | } 202 | return retval; 203 | } 204 | 205 | /** 206 | * @brief Determines if the application is present in the flash medium. 207 | * @return TRUE if application is available for execution, FALSE otherwise 208 | */ 209 | int FlashIf_ApplicationPresent(void) 210 | { 211 | return (FlashIf_AppHeaderValid() && FlashIf_AppSignValid()); 212 | } 213 | 214 | const USBD_DFU_AppType hflash_if = { 215 | .Firmware.Address = FLASH_APP_ADDRESS, 216 | .Firmware.TotalSize = FLASH_APP_SIZE - sizeof(manifSign), 217 | 218 | .Init = FLASH_vUnlock, 219 | .Deinit = FLASH_vLock, 220 | .Write = FlashIf_Write, 221 | .Read = FlashIf_Read, 222 | .Manifest = FlashIf_Manifest, 223 | 224 | #if (USBD_DFU_ST_EXTENSION != 0) 225 | .Erase = SE_FlashIf_Erase, 226 | .GetTimeout_ms = SE_FlashIf_GetTimeout_ms, 227 | .Name = SE_FLASH_DESC_STR, 228 | #else 229 | .Erase = FlashIf_Erase, 230 | .GetTimeout_ms = FlashIf_GetTimeout_ms, 231 | .Name = "DFU Bootloader", 232 | #endif 233 | }, *const flash_if = &hflash_if; 234 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------