├── .gitignore ├── gdb_oocd.cmds ├── libs ├── STM32_HAL │ ├── include │ │ ├── cmsis │ │ │ ├── device │ │ │ │ ├── stm32f0xx.h │ │ │ │ ├── stm32f042x6.h │ │ │ │ ├── stm32f072xb.h │ │ │ │ └── system_stm32f0xx.h │ │ │ ├── cmsis_device.h │ │ │ ├── cmsis_version.h │ │ │ ├── arm_const_structs.h │ │ │ ├── arm_common_tables.h │ │ │ └── cmsis_compiler.h │ │ └── stm32f0xx │ │ │ ├── stm32f0xx_hal_pcd_ex.h │ │ │ ├── stm32f0xx_hal_cortex.h │ │ │ ├── stm32f0xx_hal_def.h │ │ │ ├── stm32f0xx_ll_usb.h │ │ │ ├── stm32f0xx_hal_tim_ex.h │ │ │ └── stm32f0xx_hal_flash.h │ ├── CMakeLists.txt │ └── src │ │ └── stm32f0xx │ │ └── stm32f0xx_hal_pcd_ex.c └── STM32_USB_Device_Library │ ├── CMakeLists.txt │ ├── config │ └── usbd_conf.h │ └── Core │ ├── Inc │ ├── usbd_ctlreq.h │ ├── usbd_ioreq.h │ └── usbd_core.h │ └── Src │ └── usbd_ioreq.c ├── 70-candle-usb.rules ├── cmake ├── FindDFUSuffix.cmake └── gcc-arm-none-eabi-8-2019-q3-update.cmake ├── openocd.cfg ├── include ├── gpio.h ├── dfu.h ├── timer.h ├── usbd_desc.h ├── flash.h ├── queue.h ├── led.h ├── can.h ├── usbd_gs_can.h ├── util.h ├── config.h └── gs_usb.h ├── src └── startup.c ├── LICENSE.md ├── Src ├── util.c ├── timer.c ├── dfu.c ├── interrupts.c ├── gpio.c ├── flash.c ├── queue.c ├── led.c ├── usbd_desc.c ├── usbd_conf.c ├── main.c └── can.c ├── ldscripts ├── STM32F042X6.ld └── STM32F072XB.ld ├── README.md └── CMakeLists.txt /.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode/ 2 | /build/ 3 | *.o 4 | *.d 5 | *.bin 6 | *.asmo 7 | *.elf 8 | *~ 9 | *.bak 10 | -------------------------------------------------------------------------------- /gdb_oocd.cmds: -------------------------------------------------------------------------------- 1 | set remotetimeout 5 2 | target extended-remote | openocd -f openocd.cfg -c "gdb_port pipe; log_output oocd.log" 3 | break main -------------------------------------------------------------------------------- /libs/STM32_HAL/include/cmsis/device/stm32f0xx.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UsbCANConverter-UCCbasic/UCCB_GS_Embedded/HEAD/libs/STM32_HAL/include/cmsis/device/stm32f0xx.h -------------------------------------------------------------------------------- /libs/STM32_HAL/include/cmsis/device/stm32f042x6.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UsbCANConverter-UCCbasic/UCCB_GS_Embedded/HEAD/libs/STM32_HAL/include/cmsis/device/stm32f042x6.h -------------------------------------------------------------------------------- /libs/STM32_HAL/include/cmsis/device/stm32f072xb.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UsbCANConverter-UCCbasic/UCCB_GS_Embedded/HEAD/libs/STM32_HAL/include/cmsis/device/stm32f072xb.h -------------------------------------------------------------------------------- /libs/STM32_HAL/include/cmsis/cmsis_device.h: -------------------------------------------------------------------------------- 1 | #ifdef STM32F042x6 2 | #include 3 | #endif 4 | 5 | #ifdef STM32F072xB 6 | #include 7 | #endif 8 | -------------------------------------------------------------------------------- /70-candle-usb.rules: -------------------------------------------------------------------------------- 1 | # Copy this file to /etc/udev/rules.d/ 2 | 3 | # uses OpenMoko VID 4 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1d50", ATTRS{idProduct}=="606f", MODE="660", GROUP="users", TAG+="uaccess" 5 | 6 | -------------------------------------------------------------------------------- /cmake/FindDFUSuffix.cmake: -------------------------------------------------------------------------------- 1 | # find and set DFU_SUFFIX_EXECUTABLE 2 | 3 | find_program ( DFU_SUFFIX_EXECUTABLE 4 | NAMES dfu-suffix 5 | DOC "dfu-suffix executable" 6 | ) 7 | 8 | mark_as_advanced ( DFU_SUFFIX_EXECUTABLE ) 9 | 10 | -------------------------------------------------------------------------------- /openocd.cfg: -------------------------------------------------------------------------------- 1 | 2 | #source [find board/stm32f0discovery.cfg] 3 | 4 | source [find interface/jlink.cfg] 5 | # adapter_khz 20 6 | transport select swd 7 | source [find target/stm32f0x.cfg] 8 | 9 | set WORKAREASIZE 0x2000 10 | 11 | reset_config srst_only 12 | 13 | #init 14 | #reset init 15 | 16 | -------------------------------------------------------------------------------- /cmake/gcc-arm-none-eabi-8-2019-q3-update.cmake: -------------------------------------------------------------------------------- 1 | set(TOOLCHAIN gcc-arm-none-eabi-8-2019-q3-update) 2 | find_path( 3 | TOOLCHAIN_BIN_DIR 4 | arm-none-eabi-gcc 5 | HINTS 6 | $ENV{HOME}/bin/${TOOLCHAIN}/bin 7 | $ENV{HOME}/opt/${TOOLCHAIN}/bin 8 | /opt/${TOOLCHAIN}/bin 9 | /srv/${TOOLCHAIN}/bin 10 | /usr/local/${TOOLCHAIN}/bin 11 | ENV TOOLCHAIN_BIN_DIR 12 | ) 13 | 14 | set(CMAKE_SYSTEM_NAME Generic) 15 | set(CMAKE_SYSTEM_PROCESSOR arm) 16 | 17 | set(CMAKE_C_COMPILER "${TOOLCHAIN_BIN_DIR}/arm-none-eabi-gcc" CACHE INTERNAL "") 18 | set(CMAKE_CXX_COMPILER "${TOOLCHAIN_BIN_DIR}/arm-none-eabi-g++" CACHE INTERNAL "") 19 | set(CMAKE_EXE_LINKER_FLAGS "" CACHE INTERNAL "") 20 | 21 | set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 22 | set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 23 | set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) 24 | set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) 25 | 26 | SET (CMAKE_C_COMPILER_WORKS 1) 27 | SET (CMAKE_CXX_COMPILER_WORKS 1) -------------------------------------------------------------------------------- /libs/STM32_USB_Device_Library/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(STM32_USB_Device_Library) 2 | 3 | set(SOURCES 4 | Core/Inc/usbd_def.h 5 | Core/Inc/usbd_ctlreq.h 6 | Core/Src/usbd_ctlreq.c 7 | Core/Inc/usbd_ioreq.h 8 | Core/Src/usbd_ioreq.c 9 | Core/Inc/usbd_core.h 10 | Core/Src/usbd_core.c 11 | ) 12 | 13 | set(INCLUDE_DIRS 14 | Core/Inc/ 15 | config/ 16 | ) 17 | 18 | add_library(STM32_USB_Device_Library_STM32F042x6 STATIC ${SOURCES}) 19 | target_include_directories(STM32_USB_Device_Library_STM32F042x6 PUBLIC ${INCLUDE_DIRS}) 20 | target_compile_options(STM32_USB_Device_Library_STM32F042x6 PRIVATE -Wno-unused-parameter) 21 | target_link_libraries(STM32_USB_Device_Library_STM32F042x6 PRIVATE STM32_HAL_STM32F042x6) 22 | 23 | add_library(STM32_USB_Device_Library_STM32F072xB STATIC ${SOURCES}) 24 | target_include_directories(STM32_USB_Device_Library_STM32F072xB PUBLIC ${INCLUDE_DIRS}) 25 | target_compile_options(STM32_USB_Device_Library_STM32F072xB PRIVATE -Wno-unused-parameter) 26 | target_link_libraries(STM32_USB_Device_Library_STM32F072xB PRIVATE STM32_HAL_STM32F072xB) 27 | -------------------------------------------------------------------------------- /include/gpio.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #pragma once 28 | 29 | void gpio_init(); 30 | -------------------------------------------------------------------------------- /include/dfu.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #pragma once 28 | 29 | void dfu_run_bootloader(); 30 | -------------------------------------------------------------------------------- /include/timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #pragma once 28 | 29 | #include 30 | 31 | void timer_init(); 32 | uint32_t timer_get(); 33 | -------------------------------------------------------------------------------- /src/startup.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct _copy_table_t 5 | { 6 | uint32_t const* src; 7 | uint32_t* dest; 8 | uint32_t wlen; 9 | } copy_table_t; 10 | 11 | typedef struct _zero_table_t 12 | { 13 | uint32_t* dest; 14 | uint32_t wlen; 15 | } zero_table_t; 16 | 17 | extern const copy_table_t __copy_table_start__; 18 | extern const copy_table_t __copy_table_end__; 19 | extern const zero_table_t __zero_table_start__; 20 | extern const zero_table_t __zero_table_end__; 21 | 22 | void __initialize_hardware_early(); 23 | void _start() __attribute__((noreturn)); 24 | 25 | void Reset_Handler() 26 | { 27 | __initialize_hardware_early(); 28 | 29 | for (copy_table_t const* table = &__copy_table_start__; table < &__copy_table_end__; ++table) 30 | { 31 | for (size_t i=0; iwlen; ++i) 32 | { 33 | table->dest[i] = table->src[i]; 34 | } 35 | } 36 | 37 | for (zero_table_t const* table = &__zero_table_start__; table < &__zero_table_end__; ++table) 38 | { 39 | for (size_t i=0; iwlen; ++i) 40 | { 41 | table->dest[i] = 0u; 42 | } 43 | } 44 | 45 | _start(); 46 | } 47 | 48 | 49 | void _exit(int code) 50 | { 51 | (void) code; 52 | __asm__("BKPT"); 53 | while (1); 54 | } -------------------------------------------------------------------------------- /include/usbd_desc.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #pragma once 28 | 29 | #include "usbd_def.h" 30 | 31 | extern const USBD_DescriptorsTypeDef FS_Desc; 32 | extern uint8_t USBD_DescBuf[USBD_DESC_BUF_SIZE]; 33 | 34 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # Licenses 2 | 3 | ## candleLight Firmware Sources 4 | 5 | The MIT License (MIT) 6 | 7 | Copyright (c) 2016 Hubert Denkmair 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | 16 | The above copyright notice and this permission notice shall be included in 17 | all copies or substantial portions of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | THE SOFTWARE. 26 | 27 | ## STM32 HAL 28 | The HAL is distributed under a non-restrictive BSD (Berkeley Software 29 | Distribution) license. 30 | 31 | ## STM32 USB Library 32 | Code from the STM32 USB library is licensed 33 | under ST STMicroelectronics 34 | [Ultimate Liberty license SLA0044] (www.st.com/SLA0044) 35 | -------------------------------------------------------------------------------- /include/flash.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #pragma once 28 | 29 | #include 30 | #include 31 | 32 | void flash_load(); 33 | bool flash_set_user_id(uint8_t channel, uint32_t user_id); 34 | uint32_t flash_get_user_id(uint8_t channel); 35 | void flash_flush(); 36 | void flash_bootloaderSwitcher(); 37 | void flash_RebootToBootloader(); 38 | 39 | 40 | -------------------------------------------------------------------------------- /libs/STM32_USB_Device_Library/config/usbd_conf.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #pragma once 28 | 29 | #include "stm32f0xx_hal.h" 30 | 31 | #define USBD_MAX_NUM_INTERFACES 1 32 | #define USBD_MAX_NUM_CONFIGURATION 1 33 | #define USBD_DESC_BUF_SIZE 192 34 | #define USBD_SUPPORT_USER_STRING_DESC 1 35 | #define USBD_SELF_POWERED 0 36 | #define DEVICE_FS 0 37 | 38 | #define USBD_ErrLog(...) 39 | -------------------------------------------------------------------------------- /Src/util.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #include 28 | #include 29 | 30 | void hex32(char *out, uint32_t val) 31 | { 32 | char *p = out + 8; 33 | 34 | *p-- = 0; 35 | 36 | while (p >= out) { 37 | uint8_t nybble = val & 0x0F; 38 | 39 | if (nybble < 10) 40 | *p = '0' + nybble; 41 | else 42 | *p = 'A' + nybble - 10; 43 | 44 | val >>= 4; 45 | p--; 46 | } 47 | } 48 | 49 | void assert_failed(void) { 50 | /* for now, just halt and trigger debugger (if attached) */ 51 | __BKPT(0); 52 | } 53 | -------------------------------------------------------------------------------- /Src/timer.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #include "timer.h" 28 | #include "stm32f0xx_hal.h" 29 | 30 | void timer_init() 31 | { 32 | __HAL_RCC_TIM2_CLK_ENABLE(); 33 | 34 | TIM2->CR1 = 0; 35 | TIM2->CR2 = 0; 36 | TIM2->SMCR = 0; 37 | TIM2->DIER = 0; 38 | TIM2->CCMR1 = 0; 39 | TIM2->CCMR2 = 0; 40 | TIM2->CCER = 0; 41 | TIM2->PSC = 48-1; // run @48MHz/480 = 1MHz = 1us 42 | TIM2->ARR = 0xFFFFFFFF; 43 | TIM2->CR1 |= TIM_CR1_CEN; 44 | TIM2->EGR = TIM_EGR_UG; 45 | } 46 | 47 | uint32_t timer_get() 48 | { 49 | return TIM2->CNT; 50 | } 51 | -------------------------------------------------------------------------------- /libs/STM32_HAL/include/cmsis/cmsis_version.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file cmsis_version.h 3 | * @brief CMSIS Core(M) Version definitions 4 | * @version V5.0.2 5 | * @date 19. April 2017 6 | ******************************************************************************/ 7 | /* 8 | * Copyright (c) 2009-2017 ARM Limited. All rights reserved. 9 | * 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the License); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | */ 24 | 25 | #if defined ( __ICCARM__ ) 26 | #pragma system_include /* treat file as system include file for MISRA check */ 27 | #elif defined (__clang__) 28 | #pragma clang system_header /* treat file as system include file */ 29 | #endif 30 | 31 | #ifndef __CMSIS_VERSION_H 32 | #define __CMSIS_VERSION_H 33 | 34 | /* CMSIS Version definitions */ 35 | #define __CM_CMSIS_VERSION_MAIN ( 5U) /*!< [31:16] CMSIS Core(M) main version */ 36 | #define __CM_CMSIS_VERSION_SUB ( 1U) /*!< [15:0] CMSIS Core(M) sub version */ 37 | #define __CM_CMSIS_VERSION ((__CM_CMSIS_VERSION_MAIN << 16U) | \ 38 | __CM_CMSIS_VERSION_SUB ) /*!< CMSIS Core(M) version number */ 39 | #endif 40 | -------------------------------------------------------------------------------- /include/queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #pragma once 28 | 29 | #include 30 | #include 31 | 32 | typedef struct { 33 | unsigned max_elements; 34 | unsigned first; 35 | unsigned size; 36 | void **buf; 37 | } queue_t; 38 | 39 | queue_t *queue_create(unsigned max_elements); 40 | void queue_free(queue_t *q); 41 | 42 | unsigned queue_size(queue_t *q); 43 | bool queue_is_empty(queue_t *q); 44 | bool queue_push_back(queue_t *q, void *el); 45 | bool queue_push_front(queue_t *q, void *el); 46 | void *queue_pop_front(queue_t *q); 47 | 48 | unsigned queue_size_i(queue_t *q); 49 | bool queue_is_empty_i(queue_t *q); 50 | bool queue_push_back_i(queue_t *q, void *el); 51 | bool queue_push_front_i(queue_t *q, void *el); 52 | void *queue_pop_front_i(queue_t *q); 53 | -------------------------------------------------------------------------------- /include/led.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #pragma once 28 | 29 | #include 30 | #include 31 | 32 | typedef enum { 33 | led_mode_off, 34 | led_mode_normal, 35 | led_mode_warn, 36 | led_mode_error, 37 | led_mode_sequence 38 | } led_mode_t; 39 | 40 | typedef enum { 41 | led_1, 42 | led_2 43 | } led_num_t; 44 | 45 | typedef struct { 46 | uint8_t state; 47 | uint8_t time_in_10ms; 48 | } led_seq_step_t; 49 | 50 | typedef struct { 51 | void* port; 52 | uint16_t pin; 53 | bool is_active_high; 54 | uint32_t on_until; 55 | uint32_t off_until; 56 | } led_state_t; 57 | 58 | typedef struct { 59 | led_mode_t mode; 60 | led_mode_t last_mode; 61 | 62 | led_seq_step_t *sequence; 63 | uint32_t sequence_step; 64 | uint32_t t_sequence_next; 65 | int32_t seq_num_repeat; 66 | 67 | led_state_t led_state[2]; 68 | } led_data_t; 69 | 70 | 71 | void led_init( 72 | led_data_t *leds, 73 | void* led1_port, uint16_t led1_pin, bool led1_active_high, 74 | void* led2_port, uint16_t led2_pin, bool led2_active_high 75 | ); 76 | void led_set_mode(led_data_t *leds,led_mode_t mode); 77 | void led_run_sequence(led_data_t *leds, led_seq_step_t *sequence, int32_t num_repeat); 78 | void led_indicate_trx(led_data_t *leds, led_num_t num); 79 | void led_update(led_data_t *leds); 80 | -------------------------------------------------------------------------------- /include/can.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #pragma once 28 | 29 | #include 30 | #include 31 | #include "stm32f0xx_hal.h" 32 | #include 33 | 34 | typedef struct { 35 | CAN_TypeDef *instance; 36 | uint16_t brp; 37 | uint8_t phase_seg1; 38 | uint8_t phase_seg2; 39 | uint8_t sjw; 40 | } can_data_t; 41 | 42 | void can_init(can_data_t *hcan, CAN_TypeDef *instance); 43 | bool can_set_bittiming(can_data_t *hcan, uint16_t brp, uint8_t phase_seg1, uint8_t phase_seg2, uint8_t sjw); 44 | void can_enable(can_data_t *hcan, bool loop_back, bool listen_only, bool one_shot); 45 | void can_disable(can_data_t *hcan); 46 | bool can_is_enabled(can_data_t *hcan); 47 | 48 | bool can_receive(can_data_t *hcan, struct gs_host_frame *rx_frame); 49 | bool can_is_rx_pending(can_data_t *hcan); 50 | 51 | bool can_send(can_data_t *hcan, struct gs_host_frame *frame); 52 | 53 | /** return CAN->ESR register which contains tx/rx error counters and 54 | * LEC (last error code). 55 | */ 56 | uint32_t can_get_error_status(can_data_t *hcan); 57 | 58 | #define CAN_ERRCOUNT_THRESHOLD 15 /* send an error frame if tx/rx counters increase by more than this amount */ 59 | 60 | /** parse status value returned by can_get_error_status(). 61 | * @param frame : will hold the generated error frame 62 | * @return 1 when status changes (if any) need a new error frame sent 63 | */ 64 | bool can_parse_error_status(uint32_t err, uint32_t last_err, can_data_t *hcan, struct gs_host_frame *frame); 65 | -------------------------------------------------------------------------------- /libs/STM32_HAL/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(STM32_HAL) 2 | 3 | set(SOURCES 4 | config/stm32f0xx_hal_conf.h 5 | 6 | include/stm32f0xx/stm32f0xx_hal_def.h 7 | include/stm32f0xx/stm32f0xx_hal.h 8 | src/stm32f0xx/stm32f0xx_hal.c 9 | 10 | include/stm32f0xx/stm32f0xx_hal_can.h 11 | src/stm32f0xx/stm32f0xx_hal_can.c 12 | 13 | include/stm32f0xx/stm32f0xx_hal_cortex.h 14 | src/stm32f0xx/stm32f0xx_hal_cortex.c 15 | 16 | include/stm32f0xx/stm32f0xx_hal_flash_ex.h 17 | src/stm32f0xx/stm32f0xx_hal_flash_ex.c 18 | include/stm32f0xx/stm32f0xx_hal_flash.h 19 | src/stm32f0xx/stm32f0xx_hal_flash.c 20 | 21 | include/stm32f0xx/stm32f0xx_hal_gpio_ex.h 22 | include/stm32f0xx/stm32f0xx_hal_gpio.h 23 | src/stm32f0xx/stm32f0xx_hal_gpio.c 24 | 25 | include/stm32f0xx/stm32f0xx_hal_pcd_ex.h 26 | src/stm32f0xx/stm32f0xx_hal_pcd_ex.c 27 | include/stm32f0xx/stm32f0xx_hal_pcd.h 28 | src/stm32f0xx/stm32f0xx_hal_pcd.c 29 | 30 | include/stm32f0xx/stm32f0xx_hal_rcc.h 31 | src/stm32f0xx/stm32f0xx_hal_rcc.c 32 | include/stm32f0xx/stm32f0xx_hal_rcc_ex.h 33 | src/stm32f0xx/stm32f0xx_hal_rcc_ex.c 34 | 35 | include/stm32f0xx/stm32f0xx_hal_tim_ex.h 36 | src/stm32f0xx/stm32f0xx_hal_tim_ex.c 37 | include/stm32f0xx/stm32f0xx_hal_tim.h 38 | src/stm32f0xx/stm32f0xx_hal_tim.c 39 | 40 | include/stm32f0xx/stm32f0xx_ll_usb.h 41 | src/stm32f0xx/stm32f0xx_ll_usb.c 42 | 43 | src/cmsis/system_stm32f0xx.c 44 | include/stm32f0xx/Legacy/stm32_hal_legacy.h 45 | 46 | include/cmsis/arm_common_tables.h 47 | include/cmsis/arm_const_structs.h 48 | include/cmsis/arm_math.h 49 | include/cmsis/cmsis_compiler.h 50 | include/cmsis/cmsis_device.h 51 | include/cmsis/cmsis_gcc.h 52 | include/cmsis/cmsis_version.h 53 | include/cmsis/core_cm0.h 54 | include/cmsis/device/stm32f0xx.h 55 | include/cmsis/device/stm32f042x6.h 56 | include/cmsis/device/stm32f072xb.h 57 | include/cmsis/device/system_stm32f0xx.h 58 | ) 59 | 60 | set(INCLUDE_DIRS 61 | include/ 62 | include/cmsis 63 | include/stm32f0xx 64 | include/cmsis/device 65 | config/ 66 | ) 67 | 68 | add_library(STM32_HAL_STM32F042x6 STATIC ${SOURCES}) 69 | target_include_directories(STM32_HAL_STM32F042x6 PUBLIC ${INCLUDE_DIRS}) 70 | target_compile_options(STM32_HAL_STM32F042x6 PRIVATE -Wno-unused-parameter) 71 | target_compile_definitions(STM32_HAL_STM32F042x6 PUBLIC STM32F042x6) 72 | 73 | add_library(STM32_HAL_STM32F072xB STATIC ${SOURCES}) 74 | target_include_directories(STM32_HAL_STM32F072xB PUBLIC ${INCLUDE_DIRS}) 75 | target_compile_options(STM32_HAL_STM32F072xB PRIVATE -Wno-unused-parameter) 76 | target_compile_definitions(STM32_HAL_STM32F072xB PUBLIC STM32F072xB) 77 | -------------------------------------------------------------------------------- /include/usbd_gs_can.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #pragma once 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | /* Define these here so they can be referenced in other files */ 37 | 38 | #define CAN_DATA_MAX_PACKET_SIZE 32 /* Endpoint IN & OUT Packet size */ 39 | #define CAN_CMD_PACKET_SIZE 64 /* Control Endpoint Packet size */ 40 | #define USB_CAN_CONFIG_DESC_SIZ 50 41 | #define NUM_CAN_CHANNEL 1 42 | #define USBD_GS_CAN_VENDOR_CODE 0x20 43 | #define DFU_INTERFACE_NUM 1 44 | #define DFU_INTERFACE_STR_INDEX 0xE0 45 | 46 | extern USBD_ClassTypeDef USBD_GS_CAN; 47 | 48 | uint8_t USBD_GS_CAN_Init(USBD_HandleTypeDef *pdev, queue_t *q_frame_pool, queue_t *q_from_host, led_data_t *leds); 49 | void USBD_GS_CAN_SetChannel(USBD_HandleTypeDef *pdev, uint8_t channel, can_data_t* handle); 50 | bool USBD_GS_CAN_TxReady(USBD_HandleTypeDef *pdev); 51 | uint8_t USBD_GS_CAN_PrepareReceive(USBD_HandleTypeDef *pdev); 52 | bool USBD_GS_CAN_CustomDeviceRequest(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); 53 | bool USBD_GS_CAN_CustomInterfaceRequest(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); 54 | 55 | bool USBD_GS_CAN_DfuDetachRequested(USBD_HandleTypeDef *pdev); 56 | uint8_t USBD_GS_CAN_SendFrame(USBD_HandleTypeDef *pdev, struct gs_host_frame *frame); 57 | uint8_t USBD_GS_CAN_Transmit(USBD_HandleTypeDef *pdev, uint8_t *buf, uint16_t len); 58 | uint8_t USBD_GS_CAN_GetProtocolVersion(USBD_HandleTypeDef *pdev); 59 | uint8_t USBD_GS_CAN_GetPadPacketsToMaxPacketSize(USBD_HandleTypeDef *pdev); 60 | -------------------------------------------------------------------------------- /libs/STM32_USB_Device_Library/Core/Inc/usbd_ctlreq.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_req.h 4 | * @author MCD Application Team 5 | * @brief Header file for the usbd_req.c file 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2015 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under Ultimate Liberty license 13 | * SLA0044, the "License"; You may not use this file except in compliance with 14 | * the License. You may obtain a copy of the License at: 15 | * www.st.com/SLA0044 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __USB_REQUEST_H 22 | #define __USB_REQUEST_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "usbd_def.h" 30 | 31 | 32 | /** @addtogroup STM32_USB_DEVICE_LIBRARY 33 | * @{ 34 | */ 35 | 36 | /** @defgroup USBD_REQ 37 | * @brief header file for the usbd_req.c file 38 | * @{ 39 | */ 40 | 41 | /** @defgroup USBD_REQ_Exported_Defines 42 | * @{ 43 | */ 44 | /** 45 | * @} 46 | */ 47 | 48 | 49 | /** @defgroup USBD_REQ_Exported_Types 50 | * @{ 51 | */ 52 | /** 53 | * @} 54 | */ 55 | 56 | 57 | 58 | /** @defgroup USBD_REQ_Exported_Macros 59 | * @{ 60 | */ 61 | /** 62 | * @} 63 | */ 64 | 65 | /** @defgroup USBD_REQ_Exported_Variables 66 | * @{ 67 | */ 68 | /** 69 | * @} 70 | */ 71 | 72 | /** @defgroup USBD_REQ_Exported_FunctionsPrototype 73 | * @{ 74 | */ 75 | 76 | USBD_StatusTypeDef USBD_StdDevReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); 77 | USBD_StatusTypeDef USBD_StdItfReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); 78 | USBD_StatusTypeDef USBD_StdEPReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); 79 | 80 | 81 | void USBD_CtlError(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); 82 | 83 | void USBD_ParseSetupRequest(USBD_SetupReqTypedef *req, uint8_t *pdata); 84 | 85 | void USBD_GetString(uint8_t *desc, uint8_t *unicode, uint16_t *len); 86 | /** 87 | * @} 88 | */ 89 | 90 | #ifdef __cplusplus 91 | } 92 | #endif 93 | 94 | #endif /* __USB_REQUEST_H */ 95 | 96 | /** 97 | * @} 98 | */ 99 | 100 | /** 101 | * @} 102 | */ 103 | 104 | 105 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 106 | -------------------------------------------------------------------------------- /Src/dfu.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #include "dfu.h" 28 | #include 29 | #include "stm32f0xx_hal.h" 30 | 31 | #define RESET_TO_BOOTLOADER_MAGIC_CODE 0xDEADBEEF 32 | 33 | #define SYSMEM_STM32F042 0x1FFFC400 34 | #define SYSMEM_STM32F072 0x1FFFC800 35 | 36 | static uint32_t dfu_reset_to_bootloader_magic; 37 | 38 | //static void dfu_hack_boot_pin_f042(); 39 | //static void dfu_jump_to_bootloader(); 40 | 41 | void dfu_run_bootloader() 42 | { 43 | flash_RebootToBootloader(); 44 | dfu_reset_to_bootloader_magic = RESET_TO_BOOTLOADER_MAGIC_CODE; 45 | NVIC_SystemReset(); 46 | } 47 | 48 | void __initialize_hardware_early(void) 49 | { 50 | // if (dfu_reset_to_bootloader_magic == RESET_TO_BOOTLOADER_MAGIC_CODE) 51 | // { 52 | // switch (HAL_GetDEVID()) 53 | // { 54 | // 55 | // case 0x445: // STM32F04x 56 | // dfu_hack_boot_pin_f042(); 57 | // dfu_jump_to_bootloader(SYSMEM_STM32F042); 58 | // break; 59 | // 60 | // case 0x448: // STM32F07x 61 | // dfu_jump_to_bootloader(SYSMEM_STM32F072); 62 | // break; 63 | // 64 | // } 65 | // } 66 | 67 | SystemInit(); 68 | } 69 | 70 | //static void dfu_hack_boot_pin_f042() 71 | //{ 72 | // __HAL_RCC_GPIOF_CLK_ENABLE(); 73 | // GPIO_InitTypeDef GPIO_InitStruct; 74 | // GPIO_InitStruct.Pin = GPIO_PIN_11; 75 | // GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 76 | // GPIO_InitStruct.Pull = GPIO_PULLUP; 77 | // GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 78 | // HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); 79 | // HAL_GPIO_WritePin(GPIOF, GPIO_PIN_11, 1); 80 | //} 81 | // 82 | //static void dfu_jump_to_bootloader(uint32_t sysmem_base) 83 | //{ 84 | // void (*bootloader)(void) = (void (*)(void)) (*((uint32_t *) (sysmem_base + 4))); 85 | // 86 | // __set_MSP(*(__IO uint32_t*) sysmem_base); 87 | // bootloader(); 88 | // 89 | // while (42) 90 | // { 91 | // } 92 | //} 93 | -------------------------------------------------------------------------------- /libs/STM32_HAL/include/cmsis/device/system_stm32f0xx.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file system_stm32f0xx.h 4 | * @author MCD Application Team 5 | * @brief CMSIS Cortex-M0 Device System Source File for STM32F0xx devices. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2016 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /** @addtogroup CMSIS 21 | * @{ 22 | */ 23 | 24 | /** @addtogroup stm32f0xx_system 25 | * @{ 26 | */ 27 | 28 | /** 29 | * @brief Define to prevent recursive inclusion 30 | */ 31 | #ifndef __SYSTEM_STM32F0XX_H 32 | #define __SYSTEM_STM32F0XX_H 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | /** @addtogroup STM32F0xx_System_Includes 39 | * @{ 40 | */ 41 | 42 | /** 43 | * @} 44 | */ 45 | 46 | 47 | /** @addtogroup STM32F0xx_System_Exported_types 48 | * @{ 49 | */ 50 | /* This variable is updated in three ways: 51 | 1) by calling CMSIS function SystemCoreClockUpdate() 52 | 3) by calling HAL API function HAL_RCC_GetHCLKFreq() 53 | 3) by calling HAL API function HAL_RCC_ClockConfig() 54 | Note: If you use this function to configure the system clock; then there 55 | is no need to call the 2 first functions listed above, since SystemCoreClock 56 | variable is updated automatically. 57 | */ 58 | extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */ 59 | extern const uint8_t AHBPrescTable[16]; /*!< AHB prescalers table values */ 60 | extern const uint8_t APBPrescTable[8]; /*!< APB prescalers table values */ 61 | 62 | /** 63 | * @} 64 | */ 65 | 66 | /** @addtogroup STM32F0xx_System_Exported_Constants 67 | * @{ 68 | */ 69 | 70 | /** 71 | * @} 72 | */ 73 | 74 | /** @addtogroup STM32F0xx_System_Exported_Macros 75 | * @{ 76 | */ 77 | 78 | /** 79 | * @} 80 | */ 81 | 82 | /** @addtogroup STM32F0xx_System_Exported_Functions 83 | * @{ 84 | */ 85 | 86 | extern void SystemInit(void); 87 | extern void SystemCoreClockUpdate(void); 88 | /** 89 | * @} 90 | */ 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif 95 | 96 | #endif /*__SYSTEM_STM32F0XX_H */ 97 | 98 | /** 99 | * @} 100 | */ 101 | 102 | /** 103 | * @} 104 | */ 105 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 106 | -------------------------------------------------------------------------------- /libs/STM32_HAL/include/stm32f0xx/stm32f0xx_hal_pcd_ex.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f0xx_hal_pcd_ex.h 4 | * @author MCD Application Team 5 | * @brief Header file of PCD HAL Extension module. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2016 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef STM32F0xx_HAL_PCD_EX_H 22 | #define STM32F0xx_HAL_PCD_EX_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm32f0xx_hal_def.h" 30 | 31 | #if defined (USB) 32 | /** @addtogroup STM32F0xx_HAL_Driver 33 | * @{ 34 | */ 35 | 36 | /** @addtogroup PCDEx 37 | * @{ 38 | */ 39 | /* Exported types ------------------------------------------------------------*/ 40 | /* Exported constants --------------------------------------------------------*/ 41 | /* Exported macros -----------------------------------------------------------*/ 42 | /* Exported functions --------------------------------------------------------*/ 43 | /** @addtogroup PCDEx_Exported_Functions PCDEx Exported Functions 44 | * @{ 45 | */ 46 | /** @addtogroup PCDEx_Exported_Functions_Group1 Peripheral Control functions 47 | * @{ 48 | */ 49 | 50 | 51 | 52 | HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd, 53 | uint16_t ep_addr, 54 | uint16_t ep_kind, 55 | uint32_t pmaadress); 56 | 57 | 58 | HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd); 59 | HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd); 60 | 61 | 62 | HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd); 63 | HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd); 64 | void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd); 65 | 66 | void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg); 67 | void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg); 68 | 69 | /** 70 | * @} 71 | */ 72 | 73 | /** 74 | * @} 75 | */ 76 | 77 | /** 78 | * @} 79 | */ 80 | 81 | /** 82 | * @} 83 | */ 84 | #endif /* defined (USB) */ 85 | 86 | #ifdef __cplusplus 87 | } 88 | #endif 89 | 90 | 91 | #endif /* STM32F0xx_HAL_PCD_EX_H */ 92 | 93 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 94 | -------------------------------------------------------------------------------- /libs/STM32_HAL/include/cmsis/arm_const_structs.h: -------------------------------------------------------------------------------- 1 | /* ---------------------------------------------------------------------- 2 | * Project: CMSIS DSP Library 3 | * Title: arm_const_structs.h 4 | * Description: Constant structs that are initialized for user convenience. 5 | * For example, some can be given as arguments to the arm_cfft_f32() function. 6 | * 7 | * $Date: 27. January 2017 8 | * $Revision: V.1.5.1 9 | * 10 | * Target Processor: Cortex-M cores 11 | * -------------------------------------------------------------------- */ 12 | /* 13 | * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved. 14 | * 15 | * SPDX-License-Identifier: Apache-2.0 16 | * 17 | * Licensed under the Apache License, Version 2.0 (the License); you may 18 | * not use this file except in compliance with the License. 19 | * You may obtain a copy of the License at 20 | * 21 | * www.apache.org/licenses/LICENSE-2.0 22 | * 23 | * Unless required by applicable law or agreed to in writing, software 24 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 25 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 26 | * See the License for the specific language governing permissions and 27 | * limitations under the License. 28 | */ 29 | 30 | #ifndef _ARM_CONST_STRUCTS_H 31 | #define _ARM_CONST_STRUCTS_H 32 | 33 | #include "arm_math.h" 34 | #include "arm_common_tables.h" 35 | 36 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len16; 37 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len32; 38 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len64; 39 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len128; 40 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len256; 41 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len512; 42 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len1024; 43 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len2048; 44 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len4096; 45 | 46 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len16; 47 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len32; 48 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len64; 49 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len128; 50 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len256; 51 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len512; 52 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len1024; 53 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len2048; 54 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len4096; 55 | 56 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len16; 57 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len32; 58 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len64; 59 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len128; 60 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len256; 61 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len512; 62 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len1024; 63 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len2048; 64 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len4096; 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /libs/STM32_USB_Device_Library/Core/Inc/usbd_ioreq.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_ioreq.h 4 | * @author MCD Application Team 5 | * @brief Header file for the usbd_ioreq.c file 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2015 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under Ultimate Liberty license 13 | * SLA0044, the "License"; You may not use this file except in compliance with 14 | * the License. You may obtain a copy of the License at: 15 | * www.st.com/SLA0044 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __USBD_IOREQ_H 22 | #define __USBD_IOREQ_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "usbd_def.h" 30 | #include "usbd_core.h" 31 | 32 | /** @addtogroup STM32_USB_DEVICE_LIBRARY 33 | * @{ 34 | */ 35 | 36 | /** @defgroup USBD_IOREQ 37 | * @brief header file for the usbd_ioreq.c file 38 | * @{ 39 | */ 40 | 41 | /** @defgroup USBD_IOREQ_Exported_Defines 42 | * @{ 43 | */ 44 | /** 45 | * @} 46 | */ 47 | 48 | 49 | /** @defgroup USBD_IOREQ_Exported_Types 50 | * @{ 51 | */ 52 | 53 | 54 | /** 55 | * @} 56 | */ 57 | 58 | 59 | 60 | /** @defgroup USBD_IOREQ_Exported_Macros 61 | * @{ 62 | */ 63 | 64 | /** 65 | * @} 66 | */ 67 | 68 | /** @defgroup USBD_IOREQ_Exported_Variables 69 | * @{ 70 | */ 71 | 72 | /** 73 | * @} 74 | */ 75 | 76 | /** @defgroup USBD_IOREQ_Exported_FunctionsPrototype 77 | * @{ 78 | */ 79 | 80 | USBD_StatusTypeDef USBD_CtlSendData(USBD_HandleTypeDef *pdev, 81 | uint8_t *pbuf, 82 | uint16_t len); 83 | 84 | USBD_StatusTypeDef USBD_CtlContinueSendData(USBD_HandleTypeDef *pdev, 85 | uint8_t *pbuf, 86 | uint16_t len); 87 | 88 | USBD_StatusTypeDef USBD_CtlPrepareRx(USBD_HandleTypeDef *pdev, 89 | uint8_t *pbuf, 90 | uint16_t len); 91 | 92 | USBD_StatusTypeDef USBD_CtlContinueRx(USBD_HandleTypeDef *pdev, 93 | uint8_t *pbuf, 94 | uint16_t len); 95 | 96 | USBD_StatusTypeDef USBD_CtlSendStatus(USBD_HandleTypeDef *pdev); 97 | 98 | USBD_StatusTypeDef USBD_CtlReceiveStatus(USBD_HandleTypeDef *pdev); 99 | 100 | uint32_t USBD_GetRxCount(USBD_HandleTypeDef *pdev, uint8_t ep_addr); 101 | 102 | /** 103 | * @} 104 | */ 105 | 106 | #ifdef __cplusplus 107 | } 108 | #endif 109 | 110 | #endif /* __USBD_IOREQ_H */ 111 | 112 | /** 113 | * @} 114 | */ 115 | 116 | /** 117 | * @} 118 | */ 119 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 120 | -------------------------------------------------------------------------------- /Src/interrupts.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2019 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | void NMI_Handler(void) 33 | { 34 | __asm__("BKPT"); 35 | while (1); 36 | } 37 | 38 | void HardFault_Handler(void) 39 | { 40 | __asm__("BKPT"); 41 | while (1); 42 | } 43 | 44 | void SysTick_Handler(void) 45 | { 46 | HAL_IncTick(); 47 | HAL_SYSTICK_IRQHandler(); 48 | } 49 | 50 | extern PCD_HandleTypeDef hpcd_USB_FS; 51 | void USB_Handler(void) 52 | { 53 | HAL_PCD_IRQHandler(&hpcd_USB_FS); 54 | } 55 | 56 | void Default_Handler() 57 | { 58 | __asm__("BKPT"); 59 | while (1); 60 | } 61 | 62 | extern void Reset_Handler(); 63 | 64 | typedef void(*pFunc)(); 65 | extern uint32_t __StackTop; 66 | 67 | __attribute__((used, section(".vectors"))) 68 | const pFunc InterruptVectorTable[48] = 69 | { 70 | (pFunc)(&__StackTop), // initial stack pointer 71 | Reset_Handler, // reset handler 72 | NMI_Handler, // -14: NMI 73 | HardFault_Handler, // -13: HardFault 74 | 0, 75 | 0, 76 | 0, 77 | 0, 78 | 0, 79 | 0, 80 | 0, 81 | 0, // -5: SVCall 82 | 0, 83 | 0, 84 | 0, // -2: PendSV 85 | SysTick_Handler, // -1: SysTick 86 | 87 | 0, // int 0: WWDG 88 | 0, // int 1: PVD 89 | 0, // int 2: RTC 90 | 0, // int 3: FLASH 91 | 0, // int 4: RCC_CRS 92 | 0, // int 5: EXTI0_1 93 | 0, // int 6: EXTI2_3 94 | 0, // int 7: EXTI4_15 95 | 0, // int 8: TSC 96 | 0, // int 9: DMA_CH1 97 | 0, // int 10: DMA_CH2_3 98 | 0, // int 11: DMA_CH4_5_6_7 99 | 0, // int 12: ADC_COMP 100 | 0, // int 13: TIM1_BRK_UP_TRG_COM 101 | 0, // int 14: TIM1_CC 102 | 0, // int 15: TIM2 103 | 0, // int 16: TIM3 104 | 0, // int 17: TIM6_DAC 105 | 0, // int 18: TIM7 106 | 0, // int 19: TIM14 107 | 0, // int 20: TIM15 108 | 0, // int 21: TIM16 109 | 0, // int 22: TIM17 110 | 0, // int 23: I2C1 111 | 0, // int 24: I2C2 112 | 0, // int 25: SPI1 113 | 0, // int 26: SPI2 114 | 0, // int 27: USART1 115 | 0, // int 28: USART2 116 | 0, // int 29: USART3_4 117 | 0, // int 30: CEC_CAN 118 | USB_Handler, // int 31: USB 119 | }; 120 | -------------------------------------------------------------------------------- /ldscripts/STM32F042X6.ld: -------------------------------------------------------------------------------- 1 | __STACK_SIZE = 1024; 2 | __HEAP_SIZE = 2560; 3 | 4 | __FLASH_SIZE = 32K; 5 | __NVM_SIZE = 1K; 6 | 7 | MEMORY 8 | { 9 | FLASH (rx) : ORIGIN = 0x08000000, LENGTH = __FLASH_SIZE - __NVM_SIZE 10 | RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 6K 11 | DATA (xrw) : ORIGIN = 0x08000000 + __FLASH_SIZE - __NVM_SIZE, LENGTH = __NVM_SIZE 12 | } 13 | 14 | ENTRY(Reset_Handler) 15 | 16 | SECTIONS 17 | { 18 | .text : 19 | { 20 | KEEP(*(.vectors)) 21 | *(.text*) 22 | 23 | KEEP(*(.init)) 24 | KEEP(*(.fini)) 25 | 26 | /* .ctors */ 27 | *crtbegin.o(.ctors) 28 | *crtbegin?.o(.ctors) 29 | *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors) 30 | *(SORT(.ctors.*)) 31 | *(.ctors) 32 | 33 | /* .dtors */ 34 | *crtbegin.o(.dtors) 35 | *crtbegin?.o(.dtors) 36 | *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors) 37 | *(SORT(.dtors.*)) 38 | *(.dtors) 39 | 40 | *(.rodata*) 41 | 42 | KEEP(*(.eh_frame*)) 43 | } > FLASH 44 | 45 | .ARM.extab : 46 | { 47 | *(.ARM.extab* .gnu.linkonce.armextab.*) 48 | } > FLASH 49 | 50 | __exidx_start = .; 51 | .ARM.exidx : 52 | { 53 | *(.ARM.exidx* .gnu.linkonce.armexidx.*) 54 | } > FLASH 55 | __exidx_end = .; 56 | 57 | .copy.table : 58 | { 59 | . = ALIGN(4); 60 | __copy_table_start__ = .; 61 | LONG (__etext) 62 | LONG (__data_start__) 63 | LONG (__data_end__ - __data_start__) 64 | __copy_table_end__ = .; 65 | } > FLASH 66 | 67 | .zero.table : 68 | { 69 | . = ALIGN(4); 70 | __zero_table_start__ = .; 71 | __zero_table_end__ = .; 72 | } > FLASH 73 | 74 | __etext = ALIGN (4); 75 | 76 | .user_data : 77 | { 78 | . = ALIGN(4); 79 | *(.user_data) 80 | . = ALIGN(4); 81 | } > DATA 82 | 83 | .data : AT (__etext) 84 | { 85 | __data_start__ = .; 86 | *(vtable) 87 | *(.data) 88 | *(.data.*) 89 | 90 | . = ALIGN(4); 91 | /* preinit data */ 92 | PROVIDE_HIDDEN (__preinit_array_start = .); 93 | KEEP(*(.preinit_array)) 94 | PROVIDE_HIDDEN (__preinit_array_end = .); 95 | 96 | . = ALIGN(4); 97 | /* init data */ 98 | PROVIDE_HIDDEN (__init_array_start = .); 99 | KEEP(*(SORT(.init_array.*))) 100 | KEEP(*(.init_array)) 101 | PROVIDE_HIDDEN (__init_array_end = .); 102 | 103 | 104 | . = ALIGN(4); 105 | /* finit data */ 106 | PROVIDE_HIDDEN (__fini_array_start = .); 107 | KEEP(*(SORT(.fini_array.*))) 108 | KEEP(*(.fini_array)) 109 | PROVIDE_HIDDEN (__fini_array_end = .); 110 | 111 | KEEP(*(.jcr*)) 112 | . = ALIGN(4); 113 | /* All data end */ 114 | __data_end__ = .; 115 | 116 | } > RAM 117 | 118 | .bss : 119 | { 120 | . = ALIGN(4); 121 | __bss_start__ = .; 122 | *(.bss) 123 | *(.bss.*) 124 | *(COMMON) 125 | . = ALIGN(4); 126 | __bss_end__ = .; 127 | } > RAM AT > RAM 128 | 129 | .heap (COPY) : 130 | { 131 | . = ALIGN(8); 132 | __end__ = .; 133 | PROVIDE(end = .); 134 | . = . + __HEAP_SIZE; 135 | . = ALIGN(8); 136 | __HeapLimit = .; 137 | } > RAM 138 | 139 | .stack (ORIGIN(RAM) + LENGTH(RAM) - __STACK_SIZE) (COPY) : 140 | { 141 | . = ALIGN(8); 142 | __StackLimit = .; 143 | . = . + __STACK_SIZE; 144 | . = ALIGN(8); 145 | __StackTop = .; 146 | } > RAM 147 | PROVIDE(__stack = __StackTop); 148 | 149 | ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack") 150 | } 151 | -------------------------------------------------------------------------------- /ldscripts/STM32F072XB.ld: -------------------------------------------------------------------------------- 1 | __STACK_SIZE = 2K; 2 | __HEAP_SIZE = 10K; 3 | 4 | __FLASH_SIZE = 128K; 5 | __NVM_SIZE = 1K; 6 | 7 | MEMORY 8 | { 9 | FLASH (rx) : ORIGIN = 0x08000000, LENGTH = __FLASH_SIZE - __NVM_SIZE 10 | RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 16K 11 | DATA (xrw) : ORIGIN = 0x08000000 + __FLASH_SIZE - __NVM_SIZE, LENGTH = __NVM_SIZE 12 | } 13 | 14 | 15 | ENTRY(Reset_Handler) 16 | 17 | SECTIONS 18 | { 19 | .text : 20 | { 21 | KEEP(*(.vectors)) 22 | *(.text*) 23 | 24 | KEEP(*(.init)) 25 | KEEP(*(.fini)) 26 | 27 | /* .ctors */ 28 | *crtbegin.o(.ctors) 29 | *crtbegin?.o(.ctors) 30 | *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors) 31 | *(SORT(.ctors.*)) 32 | *(.ctors) 33 | 34 | /* .dtors */ 35 | *crtbegin.o(.dtors) 36 | *crtbegin?.o(.dtors) 37 | *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors) 38 | *(SORT(.dtors.*)) 39 | *(.dtors) 40 | 41 | *(.rodata*) 42 | 43 | KEEP(*(.eh_frame*)) 44 | } > FLASH 45 | 46 | .ARM.extab : 47 | { 48 | *(.ARM.extab* .gnu.linkonce.armextab.*) 49 | } > FLASH 50 | 51 | __exidx_start = .; 52 | .ARM.exidx : 53 | { 54 | *(.ARM.exidx* .gnu.linkonce.armexidx.*) 55 | } > FLASH 56 | __exidx_end = .; 57 | 58 | .copy.table : 59 | { 60 | . = ALIGN(4); 61 | __copy_table_start__ = .; 62 | LONG (__etext) 63 | LONG (__data_start__) 64 | LONG (__data_end__ - __data_start__) 65 | __copy_table_end__ = .; 66 | } > FLASH 67 | 68 | .zero.table : 69 | { 70 | . = ALIGN(4); 71 | __zero_table_start__ = .; 72 | __zero_table_end__ = .; 73 | } > FLASH 74 | 75 | __etext = ALIGN (4); 76 | 77 | .user_data : 78 | { 79 | . = ALIGN(4); 80 | *(.user_data) 81 | . = ALIGN(4); 82 | } > DATA 83 | 84 | .data : AT (__etext) 85 | { 86 | __data_start__ = .; 87 | *(vtable) 88 | *(.data) 89 | *(.data.*) 90 | 91 | . = ALIGN(4); 92 | /* preinit data */ 93 | PROVIDE_HIDDEN (__preinit_array_start = .); 94 | KEEP(*(.preinit_array)) 95 | PROVIDE_HIDDEN (__preinit_array_end = .); 96 | 97 | . = ALIGN(4); 98 | /* init data */ 99 | PROVIDE_HIDDEN (__init_array_start = .); 100 | KEEP(*(SORT(.init_array.*))) 101 | KEEP(*(.init_array)) 102 | PROVIDE_HIDDEN (__init_array_end = .); 103 | 104 | 105 | . = ALIGN(4); 106 | /* finit data */ 107 | PROVIDE_HIDDEN (__fini_array_start = .); 108 | KEEP(*(SORT(.fini_array.*))) 109 | KEEP(*(.fini_array)) 110 | PROVIDE_HIDDEN (__fini_array_end = .); 111 | 112 | KEEP(*(.jcr*)) 113 | . = ALIGN(4); 114 | /* All data end */ 115 | __data_end__ = .; 116 | 117 | } > RAM 118 | 119 | .bss : 120 | { 121 | . = ALIGN(4); 122 | __bss_start__ = .; 123 | *(.bss) 124 | *(.bss.*) 125 | *(COMMON) 126 | . = ALIGN(4); 127 | __bss_end__ = .; 128 | } > RAM AT > RAM 129 | 130 | .heap (COPY) : 131 | { 132 | . = ALIGN(8); 133 | __end__ = .; 134 | PROVIDE(end = .); 135 | . = . + __HEAP_SIZE; 136 | . = ALIGN(8); 137 | __HeapLimit = .; 138 | } > RAM 139 | 140 | .stack (ORIGIN(RAM) + LENGTH(RAM) - __STACK_SIZE) (COPY) : 141 | { 142 | . = ALIGN(8); 143 | __StackLimit = .; 144 | . = . + __STACK_SIZE; 145 | . = ALIGN(8); 146 | __StackTop = .; 147 | } > RAM 148 | PROVIDE(__stack = __StackTop); 149 | 150 | ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack") 151 | } 152 | -------------------------------------------------------------------------------- /include/util.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016, 2019 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #pragma once 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | void hex32(char *out, uint32_t val); 34 | 35 | // ARM's 36 | // "Application Note 321 ARM Cortex-M Programming Guide to Memory Barrier Instructions" 37 | // (from https://developer.arm.com/documentation/dai0321/latest) says that 38 | // the ISBs are actually necessary on Cortex-M0 to avoid a 2-instruction 39 | // delay in the effect of enabling and disabling interrupts. 40 | // That probably doesn't matter here, but it's hard to say what the compiler 41 | // will put in those 2 instructions so it's safer to leave it. The DSB isn't 42 | // necessary on Cortex-M0, but it's architecturally required so we'll 43 | // include it to be safe. 44 | // 45 | // The "memory" and "cc" clobbers tell GCC to avoid moving memory loads or 46 | // stores across the instructions. This is important when an interrupt and the 47 | // code calling disable_irq/enable_irq share memory. The fact that these are 48 | // non-inlined functions probably forces GCC to flush everything to memory 49 | // anyways, but trying to outsmart the compiler is a bad strategy (you never 50 | // know when somebody will turn on LTO or something). 51 | 52 | static inline bool is_irq_enabled(void) 53 | { 54 | return (__get_PRIMASK() & 1u) == 0u; // interrupts not prevented 55 | } 56 | 57 | static inline bool disable_irq(void) 58 | { 59 | bool was_enabled = is_irq_enabled(); 60 | __disable_irq(); 61 | __ISB(); 62 | return was_enabled; 63 | } 64 | 65 | static inline void enable_irq() 66 | { 67 | __enable_irq(); 68 | __ISB(); 69 | } 70 | 71 | /* Lightweight assert macro to replace standard assert() 72 | * 73 | * Standard assert() needs fprint, __FILE__ , __LINE__ string consts etc. 74 | * 75 | * stm32's library has "USE_FULL_ASSERT" to enable some checks, but they have 76 | * their own assert_param() macro that also passes in __FILE__, __LINE__ so 77 | * I'm not sure if (and how) it'll be possible to combine both. 78 | * 79 | * Interesting notes on 80 | * https://barrgroup.com/embedded-systems/how-to/define-assert-macro 81 | * 82 | */ 83 | #define assert_basic(exp) \ 84 | if (exp) { \ 85 | } else \ 86 | assert_failed() 87 | 88 | /** halt / set core to debug state with BKPT. 89 | * 90 | * TODO : save extra info somewhere in RAM, and let WDT auto-reset ? 91 | */ 92 | void assert_failed(void); 93 | -------------------------------------------------------------------------------- /Src/gpio.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #include "config.h" 28 | #include "stm32f0xx_hal.h" 29 | 30 | //must run before can_init 31 | void gpio_init() 32 | { 33 | GPIO_InitTypeDef GPIO_InitStruct; 34 | 35 | __HAL_RCC_GPIOA_CLK_ENABLE(); 36 | __HAL_RCC_GPIOB_CLK_ENABLE(); 37 | __HAL_RCC_GPIOC_CLK_ENABLE(); 38 | 39 | #ifdef CAN_S_Pin 40 | HAL_GPIO_WritePin(CAN_S_GPIO_Port, CAN_S_Pin, GPIO_PIN_SET); 41 | GPIO_InitStruct.Pin = CAN_S_Pin; 42 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 43 | GPIO_InitStruct.Pull = GPIO_NOPULL; 44 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 45 | HAL_GPIO_Init(CAN_S_GPIO_Port, &GPIO_InitStruct); 46 | #endif 47 | 48 | #if (LED1_Active_High == 1) 49 | HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET); 50 | #else 51 | // HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET); 52 | #endif 53 | // GPIO_InitStruct.Pin = LED1_Pin; 54 | // GPIO_InitStruct.Mode = LED1_Mode; 55 | // GPIO_InitStruct.Pull = GPIO_NOPULL; 56 | // GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 57 | // HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct); 58 | 59 | #ifdef LED2_Active_Low 60 | HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET); 61 | #else 62 | // HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET); 63 | #endif 64 | // GPIO_InitStruct.Pin = LED2_Pin; 65 | // GPIO_InitStruct.Mode = LED2_Mode; 66 | // GPIO_InitStruct.Pull = GPIO_NOPULL; 67 | // GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 68 | // HAL_GPIO_Init(LED2_GPIO_Port, &GPIO_InitStruct); 69 | 70 | #if BOARD == BOARD_cannette 71 | HAL_GPIO_WritePin(nCANSTBY_Port, nCANSTBY_Pin, GPIO_PIN_RESET); 72 | GPIO_InitStruct.Pin = nCANSTBY_Pin; 73 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 74 | GPIO_InitStruct.Pull = GPIO_NOPULL; 75 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 76 | HAL_GPIO_Init(nCANSTBY_Port, &GPIO_InitStruct); //xceiver standby. 77 | 78 | HAL_GPIO_WritePin(DCDCEN_Port, DCDCEN_Pin, GPIO_PIN_SET); 79 | GPIO_InitStruct.Pin = DCDCEN_Pin; 80 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 81 | GPIO_InitStruct.Pull = GPIO_NOPULL; 82 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 83 | HAL_GPIO_Init(DCDCEN_Port, &GPIO_InitStruct); //start DCDC (TODO : wait until enumerated) 84 | 85 | HAL_GPIO_WritePin(nSI86EN_Port, nSI86EN_Pin, GPIO_PIN_RESET); 86 | GPIO_InitStruct.Pin = nSI86EN_Pin; 87 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 88 | GPIO_InitStruct.Pull = GPIO_NOPULL; 89 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 90 | HAL_GPIO_Init(nSI86EN_Port, &GPIO_InitStruct); //enable si86 91 | 92 | #endif // BOARD_cannette 93 | } 94 | -------------------------------------------------------------------------------- /Src/flash.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | 28 | #include "flash.h" 29 | #include 30 | #include "stm32f0xx_hal_flash.h" 31 | 32 | #define NUM_CHANNEL 1 33 | 34 | typedef struct { 35 | uint32_t user_id[NUM_CHANNEL]; 36 | } flash_data_t; 37 | 38 | static flash_data_t flash_data_ram; 39 | __attribute__((__section__(".user_data"))) const flash_data_t flash_data_rom; 40 | 41 | 42 | void flash_load() 43 | { 44 | memcpy(&flash_data_ram, &flash_data_rom, sizeof(flash_data_t)); 45 | } 46 | 47 | bool flash_set_user_id(uint8_t channel, uint32_t user_id) 48 | { 49 | if (channel 28 | #include 29 | 30 | queue_t *queue_create(unsigned max_elements){ 31 | queue_t *q = calloc(1, sizeof(queue_t)); 32 | q->buf = calloc(max_elements, sizeof(void*)); 33 | q->max_elements = max_elements; 34 | return q; 35 | } 36 | 37 | void queue_destroy(queue_t *q) 38 | { 39 | free(q->buf); 40 | free(q); 41 | } 42 | 43 | unsigned queue_size(queue_t *q) 44 | { 45 | bool was_irq_enabled = disable_irq(); 46 | unsigned retval = q->size; 47 | if (was_irq_enabled) enable_irq(); 48 | return retval; 49 | } 50 | 51 | bool queue_is_empty(queue_t *q) 52 | { 53 | return queue_size(q)==0; 54 | } 55 | 56 | bool queue_push_back(queue_t *q, void *el) 57 | { 58 | bool retval = false; 59 | bool was_irq_enabled = disable_irq(); 60 | 61 | if (q->size < q->max_elements) { 62 | unsigned pos = (q->first + q->size) % q->max_elements; 63 | q->buf[pos] = el; 64 | q->size += 1; 65 | retval = true; 66 | } 67 | 68 | if (was_irq_enabled) enable_irq(); 69 | return retval; 70 | } 71 | 72 | bool queue_push_front(queue_t *q, void *el) 73 | { 74 | bool retval = false; 75 | bool was_irq_enabled = disable_irq(); 76 | if (q->size < q->max_elements) { 77 | if (q->first==0) { 78 | q->first = q->max_elements - 1; 79 | } else { 80 | q->first = q->first - 1; 81 | } 82 | q->buf[q->first] = el; 83 | q->size += 1; 84 | retval = true; 85 | } 86 | if (was_irq_enabled) enable_irq(); 87 | return retval; 88 | } 89 | 90 | void *queue_pop_front(queue_t *q) 91 | { 92 | bool was_irq_enabled = disable_irq(); 93 | void *el = 0; 94 | if (q->size > 0) { 95 | el = q->buf[q->first]; 96 | q->first = (q->first + 1) % q->max_elements; 97 | q->size -= 1; 98 | } 99 | if (was_irq_enabled) enable_irq(); 100 | return el; 101 | } 102 | 103 | unsigned queue_size_i(queue_t *q) 104 | { 105 | return q->size; 106 | } 107 | 108 | bool queue_is_empty_i(queue_t *q) 109 | { 110 | return queue_size_i(q)==0; 111 | } 112 | 113 | bool queue_push_back_i(queue_t *q, void *el) 114 | { 115 | bool retval = false; 116 | 117 | if (q->size < q->max_elements) { 118 | unsigned pos = (q->first + q->size) % q->max_elements; 119 | q->buf[pos] = el; 120 | q->size += 1; 121 | retval = true; 122 | } 123 | 124 | return retval; 125 | } 126 | 127 | bool queue_push_front_i(queue_t *q, void *el) 128 | { 129 | bool retval = false; 130 | if (q->size < q->max_elements) { 131 | if (q->first==0) { 132 | q->first = q->max_elements - 1; 133 | } else { 134 | q->first = q->first - 1; 135 | } 136 | q->buf[q->first] = el; 137 | q->size += 1; 138 | retval = true; 139 | } 140 | return retval; 141 | } 142 | 143 | void *queue_pop_front_i(queue_t *q) 144 | { 145 | void *el = 0; 146 | if (q->size > 0) { 147 | el = q->buf[q->first]; 148 | q->first = (q->first + 1) % q->max_elements; 149 | q->size -= 1; 150 | } 151 | return el; 152 | } 153 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # candleLight_gsusb 2 | [![Build Status](https://travis-ci.org/candle-usb/candleLight_fw.svg?branch=master)](https://travis-ci.org/candle-usb/candleLight_fw) 3 | 4 | This is firmware for certain STM32F042x/STM32F072xB-based USB-CAN adapters, notably: 5 | - candleLight: https://github.com/HubertD/candleLight (STM32F072xB) 6 | - cantact: http://linklayer.github.io/cantact/ (STM32F042C6) 7 | - canable (cantact clone): http://canable.io/ (STM32F042C6) 8 | - USB2CAN: https://github.com/roboterclubaachen/usb2can (STM32F042x6) 9 | - CANAlyze: https://kkuchera.github.io/canalyze/ (STM32F042C6) 10 | - VulCAN Gen1: https://shop.copperforge.cc/products/ac41 (STM32F042x6) 11 | - Entreé: https://github.com/tuna-f1sh/entree (STM32F042x6) 12 | 13 | Of important note is that the common STM32F103 will NOT work with this firmware because its hardware cannot use both USB and CAN simultaneously. 14 | Beware also the smaller packages in the F042 series which map a USB and CAN_TX signal on the same pin and are therefore unusable ! 15 | 16 | This implements the interface of the mainline linux gs_usb kernel module and 17 | works out-of-the-box with linux distros packaging this module, e.g. Ubuntu. 18 | 19 | ## Known issues 20 | 21 | Be aware that there is a bug in the gs_usb module in linux<4.5 that can crash the kernel on device removal. 22 | 23 | Here is a fixed version that should also work for older kernels: 24 | https://github.com/HubertD/socketcan_gs_usb 25 | 26 | The Firmware also implements WCID USB descriptors and thus can be used on recent Windows versions without installing a driver. 27 | 28 | ## Building 29 | 30 | Building requires arm-none-eabi-gcc toolchain. 31 | 32 | ```shell 33 | sudo apt-get install gcc-arm-none-eabi 34 | 35 | mkdir build 36 | cd build 37 | cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/gcc-arm-none-eabi-8-2019-q3-update.cmake 38 | 39 | # or, 40 | # cmake-gui .. 41 | # don't forget to specify the cmake toolchain file before configuring. 42 | # 43 | # compile all targets : 44 | 45 | make 46 | 47 | # OR, each board target is a cmake option and can be disabled before running 'make'; 48 | # OR, compile a single target , e.g. 49 | make cantact_fw 50 | 51 | # 52 | # to list possible targets : 53 | make help 54 | 55 | ``` 56 | 57 | ## Flashing 58 | 59 | Flashing candleLight on linux: (source: [https://wiki.linklayer.com/index.php/CandleLightFirmware](https://wiki.linklayer.com/index.php/CandleLightFirmware)) 60 | - Flashing requires the dfu-util tool. On Ubuntu, this can be installed with `sudo apt install dfu-util`. 61 | - compile as above, or download the current binary release: gsusb_cantact_8b2b2b4.bin 62 | - If dfu-util fails due to permission issues on Linux, you may need additional udev rules. Consult your distro's documentation and see `70-candle-usb.rules` provided here. 63 | 64 | ### recommended simple method 65 | - If compiling with cmake, `make flash-`, e.g. `make flash-canable_fw`, to invoke dfu-util. 66 | 67 | ### method for reflashing a specific device by serial 68 | - when multiple devices are connected, dfu-util may be unable to choose which one to flash. 69 | - Obtain device's serial # by looking at `dfu-util -l` 70 | - adapt the following command accordingly : 71 | `dfu-util -D CORRECT_FIRMWARE.bin -S "serial_number_here", -a 0 -s 0x08000000:leave` 72 | - note, the `:leave` suffix above may not be supported by older builds of dfu-util and is simply a convenient way to reboot into the normal firmware. 73 | 74 | ### fail-safe method (or if flashing a blank device) 75 | - Disconnect the USB connector from the CANtact, short the BOOT pins, then reconnect the USB connector. The device should enumerate as "STM32 BOOTLOADER". 76 | 77 | - invoke dfu-util manually with: `sudo dfu-util --dfuse-address -d 0483:df11 -c 1 -i 0 -a 0 -s 0x08000000 -D CORRECT_FIRWARE.bin` where CORRECT_FIRWARE is the name of the desired .bin. 78 | - Disconnect the USB connector, un-short the BOOT pins, and reconnect. 79 | 80 | 81 | 82 | 83 | ## Links to related projects 84 | * [Cangaroo](https://github.com/HubertD/cangaroo) open source can bus analyzer software 85 | * [Candle.NET](https://github.com/elliotwoods/Candle.NET) .NET wrapper for the candle API 86 | -------------------------------------------------------------------------------- /libs/STM32_HAL/include/stm32f0xx/stm32f0xx_hal_cortex.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f0xx_hal_cortex.h 4 | * @author MCD Application Team 5 | * @brief Header file of CORTEX HAL module. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2016 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __STM32F0xx_HAL_CORTEX_H 22 | #define __STM32F0xx_HAL_CORTEX_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm32f0xx_hal_def.h" 30 | 31 | /** @addtogroup STM32F0xx_HAL_Driver 32 | * @{ 33 | */ 34 | 35 | /** @addtogroup CORTEX CORTEX 36 | * @{ 37 | */ 38 | /* Exported types ------------------------------------------------------------*/ 39 | /* Exported constants --------------------------------------------------------*/ 40 | 41 | /** @defgroup CORTEX_Exported_Constants CORTEX Exported Constants 42 | * @{ 43 | */ 44 | 45 | /** @defgroup CORTEX_SysTick_clock_source CORTEX SysTick clock source 46 | * @{ 47 | */ 48 | #define SYSTICK_CLKSOURCE_HCLK_DIV8 (0x00000000U) 49 | #define SYSTICK_CLKSOURCE_HCLK (0x00000004U) 50 | 51 | /** 52 | * @} 53 | */ 54 | 55 | /** 56 | * @} 57 | */ 58 | 59 | /* Exported Macros -----------------------------------------------------------*/ 60 | 61 | /* Exported functions --------------------------------------------------------*/ 62 | /** @addtogroup CORTEX_Exported_Functions CORTEX Exported Functions 63 | * @{ 64 | */ 65 | /** @addtogroup CORTEX_Exported_Functions_Group1 Initialization and de-initialization functions 66 | * @brief Initialization and Configuration functions 67 | * @{ 68 | */ 69 | /* Initialization and de-initialization functions *******************************/ 70 | void HAL_NVIC_SetPriority(IRQn_Type IRQn,uint32_t PreemptPriority, uint32_t SubPriority); 71 | void HAL_NVIC_EnableIRQ(IRQn_Type IRQn); 72 | void HAL_NVIC_DisableIRQ(IRQn_Type IRQn); 73 | void HAL_NVIC_SystemReset(void); 74 | uint32_t HAL_SYSTICK_Config(uint32_t TicksNumb); 75 | /** 76 | * @} 77 | */ 78 | 79 | /** @addtogroup CORTEX_Exported_Functions_Group2 Peripheral Control functions 80 | * @brief Cortex control functions 81 | * @{ 82 | */ 83 | 84 | /* Peripheral Control functions *************************************************/ 85 | uint32_t HAL_NVIC_GetPriority(IRQn_Type IRQn); 86 | uint32_t HAL_NVIC_GetPendingIRQ(IRQn_Type IRQn); 87 | void HAL_NVIC_SetPendingIRQ(IRQn_Type IRQn); 88 | void HAL_NVIC_ClearPendingIRQ(IRQn_Type IRQn); 89 | void HAL_SYSTICK_CLKSourceConfig(uint32_t CLKSource); 90 | void HAL_SYSTICK_IRQHandler(void); 91 | void HAL_SYSTICK_Callback(void); 92 | /** 93 | * @} 94 | */ 95 | 96 | /** 97 | * @} 98 | */ 99 | 100 | /* Private types -------------------------------------------------------------*/ 101 | /* Private variables ---------------------------------------------------------*/ 102 | /* Private constants ---------------------------------------------------------*/ 103 | /* Private macros ------------------------------------------------------------*/ 104 | /** @defgroup CORTEX_Private_Macros CORTEX Private Macros 105 | * @{ 106 | */ 107 | #define IS_NVIC_PREEMPTION_PRIORITY(PRIORITY) ((PRIORITY) < 0x4) 108 | 109 | #define IS_NVIC_DEVICE_IRQ(IRQ) ((IRQ) >= 0x00) 110 | 111 | #define IS_SYSTICK_CLK_SOURCE(SOURCE) (((SOURCE) == SYSTICK_CLKSOURCE_HCLK) || \ 112 | ((SOURCE) == SYSTICK_CLKSOURCE_HCLK_DIV8)) 113 | /** 114 | * @} 115 | */ 116 | 117 | /** 118 | * @} 119 | */ 120 | 121 | /** 122 | * @} 123 | */ 124 | 125 | #ifdef __cplusplus 126 | } 127 | #endif 128 | 129 | #endif /* __STM32F0xx_HAL_CORTEX_H */ 130 | 131 | 132 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 133 | 134 | -------------------------------------------------------------------------------- /Src/led.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #include "led.h" 28 | #include 29 | #include "stm32f0xx_hal.h" 30 | #include "stm32f0xx_hal_gpio.h" 31 | 32 | void led_init( 33 | led_data_t *leds, 34 | void* led1_port, uint16_t led1_pin, bool led1_active_high, 35 | void* led2_port, uint16_t led2_pin, bool led2_active_high 36 | ) { 37 | memset(leds, 0, sizeof(led_data_t)); 38 | leds->led_state[0].port = led1_port; 39 | leds->led_state[0].pin = led1_pin; 40 | leds->led_state[0].is_active_high = led1_active_high; 41 | leds->led_state[1].port = led2_port; 42 | leds->led_state[1].pin = led2_pin; 43 | leds->led_state[1].is_active_high = led2_active_high; 44 | } 45 | 46 | void led_set_mode(led_data_t *leds,led_mode_t mode) 47 | { 48 | leds->mode = mode; 49 | led_update(leds); 50 | } 51 | 52 | static void led_set(led_state_t *led, bool state) 53 | { 54 | if (!led->is_active_high) { 55 | state = !state; 56 | } 57 | 58 | HAL_GPIO_WritePin(led->port, led->pin, state ? GPIO_PIN_SET : GPIO_PIN_RESET); 59 | } 60 | 61 | static uint32_t led_set_sequence_step(led_data_t *leds, uint32_t step_num) 62 | { 63 | led_seq_step_t *step = &leds->sequence[step_num]; 64 | leds->sequence_step = step_num; 65 | led_set(&leds->led_state[0], step->state & 0x01); 66 | led_set(&leds->led_state[1], step->state & 0x02); 67 | leds->t_sequence_next = HAL_GetTick() + 10*step->time_in_10ms; 68 | return 10 * step->time_in_10ms; 69 | } 70 | 71 | void led_run_sequence(led_data_t *leds, led_seq_step_t *sequence, int32_t num_repeat) 72 | { 73 | leds->last_mode = leds->mode; 74 | leds->mode = led_mode_sequence; 75 | leds->sequence = sequence; 76 | leds->seq_num_repeat = num_repeat; 77 | led_set_sequence_step(leds, 0); 78 | led_update(leds); 79 | } 80 | 81 | void led_indicate_trx(led_data_t *leds, led_num_t num) 82 | { 83 | uint32_t now = HAL_GetTick(); 84 | led_state_t *led = &leds->led_state[num]; 85 | 86 | if ( (led->on_until < now) && (led->off_until < now) ) { 87 | led->off_until = now + 30; 88 | led->on_until = now + 45; 89 | } 90 | 91 | led_update(leds); 92 | } 93 | 94 | static void led_update_normal_mode(led_state_t *led) 95 | { 96 | uint32_t now = HAL_GetTick(); 97 | led_set(led, led->off_until < now); 98 | } 99 | 100 | static void led_update_sequence(led_data_t *leds) 101 | { 102 | if (leds->sequence == NULL) { 103 | return; 104 | } 105 | 106 | uint32_t now = HAL_GetTick(); 107 | if (now > leds->t_sequence_next) { 108 | 109 | uint32_t t = led_set_sequence_step(leds, ++leds->sequence_step); 110 | 111 | if (t > 0) { // the saga continues 112 | 113 | leds->t_sequence_next = now + t; 114 | 115 | } else { // end of sequence 116 | 117 | if (leds->seq_num_repeat != 0) { 118 | 119 | if (leds->seq_num_repeat > 0) { 120 | leds->seq_num_repeat--; 121 | } 122 | 123 | led_set_sequence_step(leds, 0); 124 | 125 | } else { 126 | leds->sequence = NULL; 127 | } 128 | } 129 | } 130 | } 131 | 132 | void led_update(led_data_t *leds) 133 | { 134 | switch (leds->mode) { 135 | 136 | case led_mode_off: 137 | led_set(&leds->led_state[0], false); 138 | led_set(&leds->led_state[1], false); 139 | break; 140 | 141 | case led_mode_normal: 142 | led_update_normal_mode(&leds->led_state[0]); 143 | led_update_normal_mode(&leds->led_state[1]); 144 | break; 145 | 146 | case led_mode_sequence: 147 | led_update_sequence(leds); 148 | break; 149 | 150 | default: 151 | led_set(&leds->led_state[0], false); 152 | led_set(&leds->led_state[1], true); 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.13) 2 | project(candleLightFirmware C ASM) 3 | 4 | add_compile_options( 5 | -std=gnu11 6 | -mcpu=cortex-m0 -mthumb 7 | -Wall -Wextra 8 | -fmessage-length=0 9 | -fsigned-char 10 | -ffunction-sections -fdata-sections 11 | -ffreestanding 12 | -fno-move-loop-invariants 13 | -Os -g3 14 | -flto -ffat-lto-objects 15 | --specs=nano.specs 16 | --specs=nosys.specs 17 | ) 18 | 19 | add_link_options( 20 | -mcpu=cortex-m0 -mthumb 21 | -Wall -Wextra -g3 22 | -Xlinker --gc-sections 23 | --specs=nano.specs 24 | --specs=nosys.specs 25 | ) 26 | 27 | add_subdirectory(libs/STM32_HAL) 28 | add_subdirectory(libs/STM32_USB_Device_Library) 29 | set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake ) 30 | 31 | set( 32 | SOURCE_FILES 33 | include/config.h 34 | 35 | include/gs_usb.h 36 | include/usbd_desc.h src/usbd_desc.c 37 | include/usbd_gs_can.h src/usbd_gs_can.c 38 | src/usbd_conf.c 39 | 40 | include/can.h src/can.c 41 | include/dfu.h src/dfu.c 42 | include/flash.h src/flash.c 43 | include/gpio.h src/gpio.c 44 | include/led.h src/led.c 45 | include/queue.h src/queue.c 46 | include/timer.h src/timer.c 47 | include/util.h src/util.c 48 | 49 | src/startup.c 50 | src/main.c 51 | src/interrupts.c 52 | ) 53 | 54 | 55 | ####### some helpers to generate targets 56 | 57 | ## objcopy to produce .bin file 58 | function(make_bin_file target) 59 | add_custom_command( 60 | TARGET ${target} POST_BUILD 61 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} 62 | BYPRODUCTS ${target}.bin 63 | COMMAND ${CMAKE_OBJCOPY} -O binary ${target} ${target}.bin 64 | ) 65 | endfunction() 66 | 67 | ## report size 68 | function(show_object_size target) 69 | string(REPLACE "objcopy" "size" CMAKE_OBJSIZE "${CMAKE_OBJCOPY}") 70 | add_custom_command( 71 | TARGET ${target} POST_BUILD 72 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} 73 | COMMAND ${CMAKE_OBJSIZE} ${target} 74 | ) 75 | endfunction() 76 | 77 | find_package(DFUSuffix) 78 | ## run dfu-suffix to append DFU stuff and signature; generate relevant flash-* target 79 | # TODO ? : run with execute_proces(... OUTPUT_QUIET ) instead of '... 1>/dev/null' 80 | 81 | function(dfu_flash target) 82 | if (DFU_SUFFIX_EXECUTABLE) 83 | add_custom_command( TARGET ${target} 84 | BYPRODUCTS ${target}.dfu 85 | COMMAND ${CMAKE_OBJCOPY} -O binary ${target} ${target}.dfu 86 | COMMAND ${DFU_SUFFIX_EXECUTABLE} --add ${target}.dfu --vid 1d50 --pid 606f 1>/dev/null 87 | COMMENT "create and sign dfu bin file: ${TGTNAME}_fw" 88 | ) 89 | add_custom_target( flash-${target} 90 | dfu-util -a 0 -R -s 0x08000000 -D ${target}.dfu 91 | ) 92 | else() 93 | add_custom_target( flash-${target} 94 | dfu-util -d 1d50:606f -a 0 -R -s 0x08000000 -D ${target}.bin 95 | ) 96 | endif() 97 | endfunction() 98 | 99 | 100 | ######### commands for adding each target have a lot in common: make helper func. 101 | # Split into two categories, F042-based and F072-based. 102 | 103 | function(add_f042_target TGTNAME) 104 | add_executable(${TGTNAME}_fw ${SOURCE_FILES}) 105 | target_include_directories(${TGTNAME}_fw PRIVATE include/) 106 | target_compile_definitions(${TGTNAME}_fw PRIVATE BOARD=BOARD_${TGTNAME}) 107 | target_link_options(${TGTNAME}_fw PRIVATE -T ${CMAKE_SOURCE_DIR}/ldscripts/STM32F042X6.ld) 108 | target_link_libraries(${TGTNAME}_fw PRIVATE STM32_HAL_STM32F042x6 STM32_USB_Device_Library_STM32F042x6) 109 | make_bin_file(${TGTNAME}_fw) 110 | dfu_flash(${TGTNAME}_fw) 111 | show_object_size(${TGTNAME}_fw) 112 | endfunction() 113 | 114 | function(add_f072_target TGTNAME) 115 | add_executable(${TGTNAME}_fw ${SOURCE_FILES}) 116 | target_include_directories(${TGTNAME}_fw PRIVATE include/) 117 | target_compile_definitions(${TGTNAME}_fw PRIVATE BOARD=BOARD_${TGTNAME}) 118 | target_link_options(${TGTNAME}_fw PRIVATE -T ${CMAKE_SOURCE_DIR}/ldscripts/STM32F072XB.ld) 119 | target_link_libraries(${TGTNAME}_fw PRIVATE STM32_HAL_STM32F072xB STM32_USB_Device_Library_STM32F072xB) 120 | make_bin_file(${TGTNAME}_fw) 121 | dfu_flash(${TGTNAME}_fw) 122 | show_object_size(${TGTNAME}_fw) 123 | endfunction() 124 | 125 | ########## generate list of targets. 126 | # the "_fw" part is appended automatically 127 | set(TGT042_LIST "uccb") 128 | #set(TGT072_LIST "candleLight") 129 | 130 | 131 | foreach (TGTNAME IN LISTS TGT042_LIST) 132 | option(BUILD_${TGTNAME} "Build firmware for \"${TGTNAME}\" (default=yes)" ON) 133 | if (BUILD_${TGTNAME}) 134 | add_f042_target(${TGTNAME}) 135 | endif() 136 | endforeach() 137 | 138 | foreach (TGTNAME IN LISTS TGT072_LIST) 139 | option(BUILD_${TGTNAME} "Build firmware for \"${TGTNAME}\" (default=yes)" ON) 140 | if (BUILD_${TGTNAME}) 141 | add_f072_target(${TGTNAME}) 142 | endif() 143 | endforeach() 144 | 145 | message("*******************") 146 | message("You may now:\n\t-compile all targets ('make')\n\t-compile a single target (e.g. 'make cantact_fw'") 147 | message("\t-flash a device (e.g. 'make flash-cantact_fw'") 148 | -------------------------------------------------------------------------------- /Src/usbd_desc.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #include 28 | #include "usbd_core.h" 29 | #include "usbd_desc.h" 30 | #include "config.h" 31 | #include "util.h" 32 | 33 | uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); 34 | uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); 35 | uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); 36 | uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); 37 | uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); 38 | uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); 39 | uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); 40 | 41 | #ifdef USB_SUPPORT_USER_STRING_DESC 42 | uint8_t *USBD_FS_USRStringDesc(USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length); 43 | #endif /* USB_SUPPORT_USER_STRING_DESC */ 44 | 45 | const USBD_DescriptorsTypeDef FS_Desc = { 46 | USBD_FS_DeviceDescriptor, 47 | USBD_FS_LangIDStrDescriptor, 48 | USBD_FS_ManufacturerStrDescriptor, 49 | USBD_FS_ProductStrDescriptor, 50 | USBD_FS_SerialStrDescriptor, 51 | USBD_FS_ConfigStrDescriptor, 52 | USBD_FS_InterfaceStrDescriptor, 53 | }; 54 | 55 | __ALIGN_BEGIN uint8_t USBD_DescBuf[USBD_DESC_BUF_SIZE] __ALIGN_END; 56 | 57 | /* USB_DeviceDescriptor */ 58 | static const uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] = { 59 | 0x12, /* bLength */ 60 | USB_DESC_TYPE_DEVICE, /* bDescriptorType */ 61 | 0x00, /* bcdUSB */ 62 | 0x02, 63 | 0x00, /* bDeviceClass */ 64 | 0x00, /* bDeviceSubClass */ 65 | 0x00, /* bDeviceProtocol */ 66 | USB_MAX_EP0_SIZE, /* bMaxPacketSize */ 67 | LOBYTE(USBD_VID), /* idVendor */ 68 | HIBYTE(USBD_VID), 69 | LOBYTE(USBD_PID_FS), /* idProduct */ 70 | HIBYTE(USBD_PID_FS), 71 | 0x00, /* bcdDevice rel. 0.00 */ 72 | 0x00, 73 | USBD_IDX_MFC_STR, /* Index of manufacturer string */ 74 | USBD_IDX_PRODUCT_STR, /* Index of product string */ 75 | USBD_IDX_SERIAL_STR, /* Index of serial number string */ 76 | USBD_MAX_NUM_CONFIGURATION /* bNumConfigurations */ 77 | } ; 78 | 79 | /* USB Standard Device Descriptor */ 80 | static const uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] = 81 | { 82 | USB_LEN_LANGID_STR_DESC, 83 | USB_DESC_TYPE_STRING, 84 | LOBYTE(USBD_LANGID_STRING), 85 | HIBYTE(USBD_LANGID_STRING), 86 | }; 87 | 88 | uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) 89 | { 90 | UNUSED(speed); 91 | *length = sizeof(USBD_FS_DeviceDesc); 92 | memcpy(USBD_DescBuf, USBD_FS_DeviceDesc, sizeof(USBD_FS_DeviceDesc)); 93 | return USBD_DescBuf; 94 | } 95 | 96 | uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) 97 | { 98 | UNUSED(speed); 99 | *length = sizeof(USBD_LangIDDesc); 100 | memcpy(USBD_DescBuf, USBD_LangIDDesc, sizeof(USBD_LangIDDesc)); 101 | return USBD_DescBuf; 102 | } 103 | 104 | uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) 105 | { 106 | UNUSED(speed); 107 | USBD_GetString(USBD_PRODUCT_STRING_FS, USBD_DescBuf, length); 108 | return USBD_DescBuf; 109 | } 110 | 111 | uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) 112 | { 113 | UNUSED(speed); 114 | USBD_GetString (USBD_MANUFACTURER_STRING, USBD_DescBuf, length); 115 | return USBD_DescBuf; 116 | } 117 | 118 | uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) 119 | { 120 | char buf[25]; 121 | 122 | UNUSED(speed); 123 | 124 | hex32(buf , *(uint32_t*)(UID_BASE )); 125 | hex32(buf + 8, *(uint32_t*)(UID_BASE + 4)); 126 | hex32(buf + 16, *(uint32_t*)(UID_BASE + 8)); 127 | 128 | USBD_GetString((uint8_t*)buf, USBD_DescBuf, length); 129 | return USBD_DescBuf; 130 | } 131 | 132 | uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) 133 | { 134 | UNUSED(speed); 135 | USBD_GetString(USBD_CONFIGURATION_STRING_FS, USBD_DescBuf, length); 136 | return USBD_DescBuf; 137 | } 138 | 139 | uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) 140 | { 141 | UNUSED(speed); 142 | USBD_GetString(USBD_INTERFACE_STRING_FS, USBD_DescBuf, length); 143 | return USBD_DescBuf; 144 | } 145 | -------------------------------------------------------------------------------- /libs/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_ioreq.c 4 | * @author MCD Application Team 5 | * @brief This file provides the IO requests APIs for control endpoints. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2015 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under Ultimate Liberty license 13 | * SLA0044, the "License"; You may not use this file except in compliance with 14 | * the License. You may obtain a copy of the License at: 15 | * www.st.com/SLA0044 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Includes ------------------------------------------------------------------*/ 21 | #include "usbd_ioreq.h" 22 | 23 | /** @addtogroup STM32_USB_DEVICE_LIBRARY 24 | * @{ 25 | */ 26 | 27 | 28 | /** @defgroup USBD_IOREQ 29 | * @brief control I/O requests module 30 | * @{ 31 | */ 32 | 33 | /** @defgroup USBD_IOREQ_Private_TypesDefinitions 34 | * @{ 35 | */ 36 | /** 37 | * @} 38 | */ 39 | 40 | 41 | /** @defgroup USBD_IOREQ_Private_Defines 42 | * @{ 43 | */ 44 | 45 | /** 46 | * @} 47 | */ 48 | 49 | 50 | /** @defgroup USBD_IOREQ_Private_Macros 51 | * @{ 52 | */ 53 | /** 54 | * @} 55 | */ 56 | 57 | 58 | /** @defgroup USBD_IOREQ_Private_Variables 59 | * @{ 60 | */ 61 | 62 | /** 63 | * @} 64 | */ 65 | 66 | 67 | /** @defgroup USBD_IOREQ_Private_FunctionPrototypes 68 | * @{ 69 | */ 70 | /** 71 | * @} 72 | */ 73 | 74 | 75 | /** @defgroup USBD_IOREQ_Private_Functions 76 | * @{ 77 | */ 78 | 79 | /** 80 | * @brief USBD_CtlSendData 81 | * send data on the ctl pipe 82 | * @param pdev: device instance 83 | * @param buff: pointer to data buffer 84 | * @param len: length of data to be sent 85 | * @retval status 86 | */ 87 | USBD_StatusTypeDef USBD_CtlSendData(USBD_HandleTypeDef *pdev, 88 | uint8_t *pbuf, uint16_t len) 89 | { 90 | /* Set EP0 State */ 91 | pdev->ep0_state = USBD_EP0_DATA_IN; 92 | pdev->ep_in[0].total_length = len; 93 | pdev->ep_in[0].rem_length = len; 94 | 95 | /* Start the transfer */ 96 | USBD_LL_Transmit(pdev, 0x00U, pbuf, len); 97 | 98 | return USBD_OK; 99 | } 100 | 101 | /** 102 | * @brief USBD_CtlContinueSendData 103 | * continue sending data on the ctl pipe 104 | * @param pdev: device instance 105 | * @param buff: pointer to data buffer 106 | * @param len: length of data to be sent 107 | * @retval status 108 | */ 109 | USBD_StatusTypeDef USBD_CtlContinueSendData(USBD_HandleTypeDef *pdev, 110 | uint8_t *pbuf, uint16_t len) 111 | { 112 | /* Start the next transfer */ 113 | USBD_LL_Transmit(pdev, 0x00U, pbuf, len); 114 | 115 | return USBD_OK; 116 | } 117 | 118 | /** 119 | * @brief USBD_CtlPrepareRx 120 | * receive data on the ctl pipe 121 | * @param pdev: device instance 122 | * @param buff: pointer to data buffer 123 | * @param len: length of data to be received 124 | * @retval status 125 | */ 126 | USBD_StatusTypeDef USBD_CtlPrepareRx(USBD_HandleTypeDef *pdev, 127 | uint8_t *pbuf, uint16_t len) 128 | { 129 | /* Set EP0 State */ 130 | pdev->ep0_state = USBD_EP0_DATA_OUT; 131 | pdev->ep_out[0].total_length = len; 132 | pdev->ep_out[0].rem_length = len; 133 | 134 | /* Start the transfer */ 135 | USBD_LL_PrepareReceive(pdev, 0U, pbuf, len); 136 | 137 | return USBD_OK; 138 | } 139 | 140 | /** 141 | * @brief USBD_CtlContinueRx 142 | * continue receive data on the ctl pipe 143 | * @param pdev: device instance 144 | * @param buff: pointer to data buffer 145 | * @param len: length of data to be received 146 | * @retval status 147 | */ 148 | USBD_StatusTypeDef USBD_CtlContinueRx(USBD_HandleTypeDef *pdev, 149 | uint8_t *pbuf, uint16_t len) 150 | { 151 | USBD_LL_PrepareReceive(pdev, 0U, pbuf, len); 152 | 153 | return USBD_OK; 154 | } 155 | 156 | /** 157 | * @brief USBD_CtlSendStatus 158 | * send zero lzngth packet on the ctl pipe 159 | * @param pdev: device instance 160 | * @retval status 161 | */ 162 | USBD_StatusTypeDef USBD_CtlSendStatus(USBD_HandleTypeDef *pdev) 163 | { 164 | /* Set EP0 State */ 165 | pdev->ep0_state = USBD_EP0_STATUS_IN; 166 | 167 | /* Start the transfer */ 168 | USBD_LL_Transmit(pdev, 0x00U, NULL, 0U); 169 | 170 | return USBD_OK; 171 | } 172 | 173 | /** 174 | * @brief USBD_CtlReceiveStatus 175 | * receive zero lzngth packet on the ctl pipe 176 | * @param pdev: device instance 177 | * @retval status 178 | */ 179 | USBD_StatusTypeDef USBD_CtlReceiveStatus(USBD_HandleTypeDef *pdev) 180 | { 181 | /* Set EP0 State */ 182 | pdev->ep0_state = USBD_EP0_STATUS_OUT; 183 | 184 | /* Start the transfer */ 185 | USBD_LL_PrepareReceive(pdev, 0U, NULL, 0U); 186 | 187 | return USBD_OK; 188 | } 189 | 190 | /** 191 | * @brief USBD_GetRxCount 192 | * returns the received data length 193 | * @param pdev: device instance 194 | * @param ep_addr: endpoint address 195 | * @retval Rx Data blength 196 | */ 197 | uint32_t USBD_GetRxCount(USBD_HandleTypeDef *pdev, uint8_t ep_addr) 198 | { 199 | return USBD_LL_GetRxDataSize(pdev, ep_addr); 200 | } 201 | 202 | /** 203 | * @} 204 | */ 205 | 206 | 207 | /** 208 | * @} 209 | */ 210 | 211 | 212 | /** 213 | * @} 214 | */ 215 | 216 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 217 | -------------------------------------------------------------------------------- /libs/STM32_USB_Device_Library/Core/Inc/usbd_core.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_core.h 4 | * @author MCD Application Team 5 | * @brief Header file for usbd_core.c file 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2015 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under Ultimate Liberty license 13 | * SLA0044, the "License"; You may not use this file except in compliance with 14 | * the License. You may obtain a copy of the License at: 15 | * www.st.com/SLA0044 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __USBD_CORE_H 22 | #define __USBD_CORE_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "usbd_conf.h" 30 | #include "usbd_def.h" 31 | #include "usbd_ioreq.h" 32 | #include "usbd_ctlreq.h" 33 | 34 | /** @addtogroup STM32_USB_DEVICE_LIBRARY 35 | * @{ 36 | */ 37 | 38 | /** @defgroup USBD_CORE 39 | * @brief This file is the Header file for usbd_core.c file 40 | * @{ 41 | */ 42 | 43 | 44 | /** @defgroup USBD_CORE_Exported_Defines 45 | * @{ 46 | */ 47 | #ifndef USBD_DEBUG_LEVEL 48 | #define USBD_DEBUG_LEVEL 0U 49 | #endif /* USBD_DEBUG_LEVEL */ 50 | /** 51 | * @} 52 | */ 53 | 54 | 55 | /** @defgroup USBD_CORE_Exported_TypesDefinitions 56 | * @{ 57 | */ 58 | 59 | 60 | /** 61 | * @} 62 | */ 63 | 64 | 65 | 66 | /** @defgroup USBD_CORE_Exported_Macros 67 | * @{ 68 | */ 69 | 70 | /** 71 | * @} 72 | */ 73 | 74 | /** @defgroup USBD_CORE_Exported_Variables 75 | * @{ 76 | */ 77 | #define USBD_SOF USBD_LL_SOF 78 | /** 79 | * @} 80 | */ 81 | 82 | /** @defgroup USBD_CORE_Exported_FunctionsPrototype 83 | * @{ 84 | */ 85 | USBD_StatusTypeDef USBD_Init(USBD_HandleTypeDef *pdev, USBD_DescriptorsTypeDef *pdesc, uint8_t id); 86 | USBD_StatusTypeDef USBD_DeInit(USBD_HandleTypeDef *pdev); 87 | USBD_StatusTypeDef USBD_Start(USBD_HandleTypeDef *pdev); 88 | USBD_StatusTypeDef USBD_Stop(USBD_HandleTypeDef *pdev); 89 | USBD_StatusTypeDef USBD_RegisterClass(USBD_HandleTypeDef *pdev, USBD_ClassTypeDef *pclass); 90 | 91 | USBD_StatusTypeDef USBD_RunTestMode(USBD_HandleTypeDef *pdev); 92 | USBD_StatusTypeDef USBD_SetClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx); 93 | USBD_StatusTypeDef USBD_ClrClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx); 94 | 95 | USBD_StatusTypeDef USBD_LL_SetupStage(USBD_HandleTypeDef *pdev, uint8_t *psetup); 96 | USBD_StatusTypeDef USBD_LL_DataOutStage(USBD_HandleTypeDef *pdev, uint8_t epnum, uint8_t *pdata); 97 | USBD_StatusTypeDef USBD_LL_DataInStage(USBD_HandleTypeDef *pdev, uint8_t epnum, uint8_t *pdata); 98 | 99 | USBD_StatusTypeDef USBD_LL_Reset(USBD_HandleTypeDef *pdev); 100 | USBD_StatusTypeDef USBD_LL_SetSpeed(USBD_HandleTypeDef *pdev, USBD_SpeedTypeDef speed); 101 | USBD_StatusTypeDef USBD_LL_Suspend(USBD_HandleTypeDef *pdev); 102 | USBD_StatusTypeDef USBD_LL_Resume(USBD_HandleTypeDef *pdev); 103 | 104 | USBD_StatusTypeDef USBD_LL_SOF(USBD_HandleTypeDef *pdev); 105 | USBD_StatusTypeDef USBD_LL_IsoINIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum); 106 | USBD_StatusTypeDef USBD_LL_IsoOUTIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum); 107 | 108 | USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef *pdev); 109 | USBD_StatusTypeDef USBD_LL_DevDisconnected(USBD_HandleTypeDef *pdev); 110 | 111 | /* USBD Low Level Driver */ 112 | USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev); 113 | USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev); 114 | USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev); 115 | USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev); 116 | USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, 117 | uint8_t ep_addr, 118 | uint8_t ep_type, 119 | uint16_t ep_mps); 120 | 121 | USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr); 122 | USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr); 123 | USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr); 124 | USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr); 125 | uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr); 126 | USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr); 127 | USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, 128 | uint8_t ep_addr, 129 | uint8_t *pbuf, 130 | uint16_t size); 131 | 132 | USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, 133 | uint8_t ep_addr, 134 | uint8_t *pbuf, 135 | uint16_t size); 136 | 137 | uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr); 138 | void USBD_LL_Delay(uint32_t Delay); 139 | 140 | /** 141 | * @} 142 | */ 143 | 144 | #ifdef __cplusplus 145 | } 146 | #endif 147 | 148 | #endif /* __USBD_CORE_H */ 149 | 150 | /** 151 | * @} 152 | */ 153 | 154 | /** 155 | * @} 156 | */ 157 | 158 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 159 | 160 | 161 | 162 | -------------------------------------------------------------------------------- /libs/STM32_HAL/include/cmsis/arm_common_tables.h: -------------------------------------------------------------------------------- 1 | /* ---------------------------------------------------------------------- 2 | * Project: CMSIS DSP Library 3 | * Title: arm_common_tables.h 4 | * Description: Extern declaration for common tables 5 | * 6 | * $Date: 27. January 2017 7 | * $Revision: V.1.5.1 8 | * 9 | * Target Processor: Cortex-M cores 10 | * -------------------------------------------------------------------- */ 11 | /* 12 | * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved. 13 | * 14 | * SPDX-License-Identifier: Apache-2.0 15 | * 16 | * Licensed under the Apache License, Version 2.0 (the License); you may 17 | * not use this file except in compliance with the License. 18 | * You may obtain a copy of the License at 19 | * 20 | * www.apache.org/licenses/LICENSE-2.0 21 | * 22 | * Unless required by applicable law or agreed to in writing, software 23 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 25 | * See the License for the specific language governing permissions and 26 | * limitations under the License. 27 | */ 28 | 29 | #ifndef _ARM_COMMON_TABLES_H 30 | #define _ARM_COMMON_TABLES_H 31 | 32 | #include "arm_math.h" 33 | 34 | extern const uint16_t armBitRevTable[1024]; 35 | extern const q15_t armRecipTableQ15[64]; 36 | extern const q31_t armRecipTableQ31[64]; 37 | extern const float32_t twiddleCoef_16[32]; 38 | extern const float32_t twiddleCoef_32[64]; 39 | extern const float32_t twiddleCoef_64[128]; 40 | extern const float32_t twiddleCoef_128[256]; 41 | extern const float32_t twiddleCoef_256[512]; 42 | extern const float32_t twiddleCoef_512[1024]; 43 | extern const float32_t twiddleCoef_1024[2048]; 44 | extern const float32_t twiddleCoef_2048[4096]; 45 | extern const float32_t twiddleCoef_4096[8192]; 46 | #define twiddleCoef twiddleCoef_4096 47 | extern const q31_t twiddleCoef_16_q31[24]; 48 | extern const q31_t twiddleCoef_32_q31[48]; 49 | extern const q31_t twiddleCoef_64_q31[96]; 50 | extern const q31_t twiddleCoef_128_q31[192]; 51 | extern const q31_t twiddleCoef_256_q31[384]; 52 | extern const q31_t twiddleCoef_512_q31[768]; 53 | extern const q31_t twiddleCoef_1024_q31[1536]; 54 | extern const q31_t twiddleCoef_2048_q31[3072]; 55 | extern const q31_t twiddleCoef_4096_q31[6144]; 56 | extern const q15_t twiddleCoef_16_q15[24]; 57 | extern const q15_t twiddleCoef_32_q15[48]; 58 | extern const q15_t twiddleCoef_64_q15[96]; 59 | extern const q15_t twiddleCoef_128_q15[192]; 60 | extern const q15_t twiddleCoef_256_q15[384]; 61 | extern const q15_t twiddleCoef_512_q15[768]; 62 | extern const q15_t twiddleCoef_1024_q15[1536]; 63 | extern const q15_t twiddleCoef_2048_q15[3072]; 64 | extern const q15_t twiddleCoef_4096_q15[6144]; 65 | extern const float32_t twiddleCoef_rfft_32[32]; 66 | extern const float32_t twiddleCoef_rfft_64[64]; 67 | extern const float32_t twiddleCoef_rfft_128[128]; 68 | extern const float32_t twiddleCoef_rfft_256[256]; 69 | extern const float32_t twiddleCoef_rfft_512[512]; 70 | extern const float32_t twiddleCoef_rfft_1024[1024]; 71 | extern const float32_t twiddleCoef_rfft_2048[2048]; 72 | extern const float32_t twiddleCoef_rfft_4096[4096]; 73 | 74 | /* floating-point bit reversal tables */ 75 | #define ARMBITREVINDEXTABLE_16_TABLE_LENGTH ((uint16_t)20) 76 | #define ARMBITREVINDEXTABLE_32_TABLE_LENGTH ((uint16_t)48) 77 | #define ARMBITREVINDEXTABLE_64_TABLE_LENGTH ((uint16_t)56) 78 | #define ARMBITREVINDEXTABLE_128_TABLE_LENGTH ((uint16_t)208) 79 | #define ARMBITREVINDEXTABLE_256_TABLE_LENGTH ((uint16_t)440) 80 | #define ARMBITREVINDEXTABLE_512_TABLE_LENGTH ((uint16_t)448) 81 | #define ARMBITREVINDEXTABLE_1024_TABLE_LENGTH ((uint16_t)1800) 82 | #define ARMBITREVINDEXTABLE_2048_TABLE_LENGTH ((uint16_t)3808) 83 | #define ARMBITREVINDEXTABLE_4096_TABLE_LENGTH ((uint16_t)4032) 84 | 85 | extern const uint16_t armBitRevIndexTable16[ARMBITREVINDEXTABLE_16_TABLE_LENGTH]; 86 | extern const uint16_t armBitRevIndexTable32[ARMBITREVINDEXTABLE_32_TABLE_LENGTH]; 87 | extern const uint16_t armBitRevIndexTable64[ARMBITREVINDEXTABLE_64_TABLE_LENGTH]; 88 | extern const uint16_t armBitRevIndexTable128[ARMBITREVINDEXTABLE_128_TABLE_LENGTH]; 89 | extern const uint16_t armBitRevIndexTable256[ARMBITREVINDEXTABLE_256_TABLE_LENGTH]; 90 | extern const uint16_t armBitRevIndexTable512[ARMBITREVINDEXTABLE_512_TABLE_LENGTH]; 91 | extern const uint16_t armBitRevIndexTable1024[ARMBITREVINDEXTABLE_1024_TABLE_LENGTH]; 92 | extern const uint16_t armBitRevIndexTable2048[ARMBITREVINDEXTABLE_2048_TABLE_LENGTH]; 93 | extern const uint16_t armBitRevIndexTable4096[ARMBITREVINDEXTABLE_4096_TABLE_LENGTH]; 94 | 95 | /* fixed-point bit reversal tables */ 96 | #define ARMBITREVINDEXTABLE_FIXED_16_TABLE_LENGTH ((uint16_t)12) 97 | #define ARMBITREVINDEXTABLE_FIXED_32_TABLE_LENGTH ((uint16_t)24) 98 | #define ARMBITREVINDEXTABLE_FIXED_64_TABLE_LENGTH ((uint16_t)56) 99 | #define ARMBITREVINDEXTABLE_FIXED_128_TABLE_LENGTH ((uint16_t)112) 100 | #define ARMBITREVINDEXTABLE_FIXED_256_TABLE_LENGTH ((uint16_t)240) 101 | #define ARMBITREVINDEXTABLE_FIXED_512_TABLE_LENGTH ((uint16_t)480) 102 | #define ARMBITREVINDEXTABLE_FIXED_1024_TABLE_LENGTH ((uint16_t)992) 103 | #define ARMBITREVINDEXTABLE_FIXED_2048_TABLE_LENGTH ((uint16_t)1984) 104 | #define ARMBITREVINDEXTABLE_FIXED_4096_TABLE_LENGTH ((uint16_t)4032) 105 | 106 | extern const uint16_t armBitRevIndexTable_fixed_16[ARMBITREVINDEXTABLE_FIXED_16_TABLE_LENGTH]; 107 | extern const uint16_t armBitRevIndexTable_fixed_32[ARMBITREVINDEXTABLE_FIXED_32_TABLE_LENGTH]; 108 | extern const uint16_t armBitRevIndexTable_fixed_64[ARMBITREVINDEXTABLE_FIXED_64_TABLE_LENGTH]; 109 | extern const uint16_t armBitRevIndexTable_fixed_128[ARMBITREVINDEXTABLE_FIXED_128_TABLE_LENGTH]; 110 | extern const uint16_t armBitRevIndexTable_fixed_256[ARMBITREVINDEXTABLE_FIXED_256_TABLE_LENGTH]; 111 | extern const uint16_t armBitRevIndexTable_fixed_512[ARMBITREVINDEXTABLE_FIXED_512_TABLE_LENGTH]; 112 | extern const uint16_t armBitRevIndexTable_fixed_1024[ARMBITREVINDEXTABLE_FIXED_1024_TABLE_LENGTH]; 113 | extern const uint16_t armBitRevIndexTable_fixed_2048[ARMBITREVINDEXTABLE_FIXED_2048_TABLE_LENGTH]; 114 | extern const uint16_t armBitRevIndexTable_fixed_4096[ARMBITREVINDEXTABLE_FIXED_4096_TABLE_LENGTH]; 115 | 116 | /* Tables for Fast Math Sine and Cosine */ 117 | extern const float32_t sinTable_f32[FAST_MATH_TABLE_SIZE + 1]; 118 | extern const q31_t sinTable_q31[FAST_MATH_TABLE_SIZE + 1]; 119 | extern const q15_t sinTable_q15[FAST_MATH_TABLE_SIZE + 1]; 120 | 121 | #endif /* ARM_COMMON_TABLES_H */ 122 | -------------------------------------------------------------------------------- /include/config.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #pragma once 28 | 29 | #define CAN_QUEUE_SIZE 64 30 | 31 | #define USBD_VID 0x1d50 32 | #define USBD_PID_FS 0x606f 33 | #define USBD_LANGID_STRING 1033 34 | #define USBD_CONFIGURATION_STRING_FS (uint8_t*) "gs_usb config" 35 | #define USBD_INTERFACE_STRING_FS (uint8_t*) "gs_usb interface" 36 | 37 | #define BOARD_candleLight 1 38 | #define BOARD_cantact 2 39 | #define BOARD_canable 3 40 | #define BOARD_usb2can 4 41 | #define BOARD_canalyze 5 42 | #define BOARD_cannette 6 43 | #define BOARD_UCCB 7 44 | 45 | #define BOARD BOARD_UCCB 46 | 47 | #if BOARD == BOARD_candleLight 48 | #define USBD_PRODUCT_STRING_FS (uint8_t*) "candleLight USB to CAN adapter" 49 | #define USBD_MANUFACTURER_STRING (uint8_t*) "bytewerk" 50 | #define DFU_INTERFACE_STRING_FS (uint8_t*) "candleLight firmware upgrade interface" 51 | #define CAN_S_Pin GPIO_PIN_13 52 | #define CAN_S_GPIO_Port GPIOC 53 | 54 | #define LED1_Pin GPIO_PIN_0 55 | #define LED1_Mode GPIO_MODE_OUTPUT_OD 56 | #define LED1_GPIO_Port GPIOA 57 | #define LED1_Active_High 0 58 | 59 | #define LED2_GPIO_Port GPIOA 60 | #define LED2_Pin GPIO_PIN_1 61 | #define LED2_Mode GPIO_MODE_OUTPUT_OD 62 | #define LED2_Active_High 0 63 | 64 | #elif BOARD == BOARD_cantact 65 | #define USBD_PRODUCT_STRING_FS (uint8_t*) "cantact gs_usb" 66 | #define USBD_MANUFACTURER_STRING (uint8_t*) "cantact.io" 67 | #define DFU_INTERFACE_STRING_FS (uint8_t*) "cantact firmware upgrade interface" 68 | 69 | // SILENT pin not connected 70 | 71 | #define LED1_GPIO_Port GPIOB 72 | #define LED1_Pin GPIO_PIN_0 /* green */ 73 | #define LED1_Mode GPIO_MODE_OUTPUT_PP 74 | #define LED1_Active_High 1 75 | 76 | #define LED2_GPIO_Port GPIOB 77 | #define LED2_Pin GPIO_PIN_1 /* red */ 78 | #define LED2_Mode GPIO_MODE_OUTPUT_PP 79 | #define LED2_Active_High 1 80 | 81 | #elif BOARD == BOARD_canable 82 | #define USBD_PRODUCT_STRING_FS (uint8_t*) "canable gs_usb" 83 | #define USBD_MANUFACTURER_STRING (uint8_t*) "canable.io" 84 | #define DFU_INTERFACE_STRING_FS (uint8_t*) "canable firmware upgrade interface" 85 | 86 | // SILENT pin not connected 87 | 88 | #define LED1_GPIO_Port GPIOB 89 | #define LED1_Pin GPIO_PIN_0 /* green */ 90 | #define LED1_Mode GPIO_MODE_OUTPUT_PP 91 | #define LED1_Active_High 1 92 | 93 | #define LED2_GPIO_Port GPIOB 94 | #define LED2_Pin GPIO_PIN_1 /* blue */ 95 | #define LED2_Mode GPIO_MODE_OUTPUT_PP 96 | #define LED2_Active_High 0 97 | 98 | #elif BOARD == BOARD_usb2can 99 | #define USBD_PRODUCT_STRING_FS (uint8_t*) "USB2CAN RCA gs_usb" 100 | #define USBD_MANUFACTURER_STRING (uint8_t*) "Roboter Club Aachen" 101 | #define DFU_INTERFACE_STRING_FS (uint8_t*) "usb2can firmware upgrade interface" 102 | 103 | // SILENT pin not connected 104 | 105 | #define LED4_GPIO_Port GPIOA 106 | #define LED4_Pin GPIO_PIN_0 /* white */ 107 | #define LED4_Mode GPIO_MODE_OUTPUT_OD 108 | #define LED4_Active_Low 109 | 110 | #define LED2_GPIO_Port GPIOA 111 | #define LED2_Pin GPIO_PIN_1 /* blue */ 112 | #define LED2_Mode GPIO_MODE_OUTPUT_OD 113 | #define LED2_Active_High 0 114 | 115 | #define LED3_GPIO_Port GPIOA 116 | #define LED3_Pin GPIO_PIN_2 /* red */ 117 | #define LED3_Mode GPIO_MODE_OUTPUT_OD 118 | #define LED3_Active_Low 119 | 120 | #define LED1_GPIO_Port GPIOB 121 | #define LED1_Pin GPIO_PIN_3 /* green */ 122 | #define LED1_Mode GPIO_MODE_OUTPUT_OD 123 | #define LED1_Active_High 0 124 | #elif BOARD == BOARD_canalyze 125 | #define USBD_PRODUCT_STRING_FS (uint8_t*) "CANAlyze gs_usb" 126 | #define USBD_MANUFACTURER_STRING (uint8_t*) "STMicroelectronics" 127 | #define DFU_INTERFACE_STRING_FS (uint8_t*) "CANAlyze firmware upgrade interface" 128 | 129 | // SILENT pin not connected 130 | 131 | #define LED1_GPIO_Port GPIOB 132 | #define LED1_Pin GPIO_PIN_0 /* green */ 133 | #define LED1_Mode GPIO_MODE_OUTPUT_PP 134 | #define LED1_Active_High 1 135 | 136 | #define LED2_GPIO_Port GPIOB 137 | #define LED2_Pin GPIO_PIN_1 /* red */ 138 | #define LED2_Mode GPIO_MODE_OUTPUT_PP 139 | #define LED2_Active_High 1 140 | 141 | #elif BOARD == BOARD_cannette 142 | #define USBD_PRODUCT_STRING_FS (uint8_t*) "cannette gs_usb" 143 | #define USBD_MANUFACTURER_STRING (uint8_t*) "chacaltech" 144 | #define DFU_INTERFACE_STRING_FS (uint8_t*) "cannette firmware upgrade interface" 145 | 146 | // SILENT pin not connected 147 | 148 | #define LED1_GPIO_Port GPIOA 149 | #define LED1_Pin GPIO_PIN_9 /* RX: green */ 150 | #define LED1_Mode GPIO_MODE_OUTPUT_OD 151 | #define LED1_Active_High 0 152 | 153 | #define LED2_GPIO_Port GPIOA 154 | #define LED2_Pin GPIO_PIN_8 /* TX: red */ 155 | #define LED2_Mode GPIO_MODE_OUTPUT_OD 156 | #define LED2_Active_High 0 157 | 158 | #define nCANSTBY_Port GPIOC 159 | #define nCANSTBY_Pin GPIO_PIN_14 /* control xceiver standby, active low */ 160 | 161 | #define nSI86EN_Port GPIOC 162 | #define nSI86EN_Pin GPIO_PIN_13 /* enable power to Si86xx isolater, active low */ 163 | 164 | #define DCDCEN_Port GPIOC 165 | #define DCDCEN_Pin GPIO_PIN_15 /* activate DCDC converter, active high */ 166 | #elif BOARD == BOARD_UCCB 167 | #define USBD_PRODUCT_STRING_FS (uint8_t*) "UCCB USB to CAN adapter" 168 | #define USBD_MANUFACTURER_STRING (uint8_t*) "ucandevices.github.io" 169 | #define DFU_INTERFACE_STRING_FS (uint8_t*) "UCCB firmware upgrade interface" 170 | #define CAN_S_Pin GPIO_PIN_4 171 | #define CAN_S_GPIO_Port GPIOB 172 | #warning "UCCB Compilation" 173 | 174 | #else 175 | 176 | #error please define BOARD 177 | #endif 178 | -------------------------------------------------------------------------------- /libs/STM32_HAL/include/stm32f0xx/stm32f0xx_hal_def.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f0xx_hal_def.h 4 | * @author MCD Application Team 5 | * @brief This file contains HAL common defines, enumeration, macros and 6 | * structures definitions. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | *

© Copyright (c) 2016 STMicroelectronics. 11 | * All rights reserved.

12 | * 13 | * This software component is licensed by ST under BSD 3-Clause license, 14 | * the "License"; You may not use this file except in compliance with the 15 | * License. You may obtain a copy of the License at: 16 | * opensource.org/licenses/BSD-3-Clause 17 | * 18 | ****************************************************************************** 19 | */ 20 | 21 | /* Define to prevent recursive inclusion -------------------------------------*/ 22 | #ifndef __STM32F0xx_HAL_DEF 23 | #define __STM32F0xx_HAL_DEF 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | /* Includes ------------------------------------------------------------------*/ 30 | #include "stm32f0xx.h" 31 | #if defined(USE_HAL_LEGACY) 32 | #include "Legacy/stm32_hal_legacy.h" 33 | #endif 34 | #include 35 | 36 | /* Exported types ------------------------------------------------------------*/ 37 | 38 | /** 39 | * @brief HAL Status structures definition 40 | */ 41 | typedef enum 42 | { 43 | HAL_OK = 0x00U, 44 | HAL_ERROR = 0x01U, 45 | HAL_BUSY = 0x02U, 46 | HAL_TIMEOUT = 0x03U 47 | } HAL_StatusTypeDef; 48 | 49 | /** 50 | * @brief HAL Lock structures definition 51 | */ 52 | typedef enum 53 | { 54 | HAL_UNLOCKED = 0x00U, 55 | HAL_LOCKED = 0x01U 56 | } HAL_LockTypeDef; 57 | 58 | /* Exported macro ------------------------------------------------------------*/ 59 | 60 | #define HAL_MAX_DELAY 0xFFFFFFFFU 61 | 62 | #define HAL_IS_BIT_SET(REG, BIT) (((REG) & (BIT)) == (BIT)) 63 | #define HAL_IS_BIT_CLR(REG, BIT) (((REG) & (BIT)) == 0U) 64 | 65 | #define __HAL_LINKDMA(__HANDLE__, __PPP_DMA_FIELD_, __DMA_HANDLE_) \ 66 | do{ \ 67 | (__HANDLE__)->__PPP_DMA_FIELD_ = &(__DMA_HANDLE_); \ 68 | (__DMA_HANDLE_).Parent = (__HANDLE__); \ 69 | } while(0) 70 | 71 | #define UNUSED(X) (void)X /* To avoid gcc/g++ warnings */ 72 | 73 | /** @brief Reset the Handle's State field. 74 | * @param __HANDLE__ specifies the Peripheral Handle. 75 | * @note This macro can be used for the following purpose: 76 | * - When the Handle is declared as local variable; before passing it as parameter 77 | * to HAL_PPP_Init() for the first time, it is mandatory to use this macro 78 | * to set to 0 the Handle's "State" field. 79 | * Otherwise, "State" field may have any random value and the first time the function 80 | * HAL_PPP_Init() is called, the low level hardware initialization will be missed 81 | * (i.e. HAL_PPP_MspInit() will not be executed). 82 | * - When there is a need to reconfigure the low level hardware: instead of calling 83 | * HAL_PPP_DeInit() then HAL_PPP_Init(), user can make a call to this macro then HAL_PPP_Init(). 84 | * In this later function, when the Handle's "State" field is set to 0, it will execute the function 85 | * HAL_PPP_MspInit() which will reconfigure the low level hardware. 86 | * @retval None 87 | */ 88 | #define __HAL_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = 0) 89 | 90 | #if (USE_RTOS == 1) 91 | #error " USE_RTOS should be 0 in the current HAL release " 92 | #else 93 | #define __HAL_LOCK(__HANDLE__) \ 94 | do{ \ 95 | if((__HANDLE__)->Lock == HAL_LOCKED) \ 96 | { \ 97 | return HAL_BUSY; \ 98 | } \ 99 | else \ 100 | { \ 101 | (__HANDLE__)->Lock = HAL_LOCKED; \ 102 | } \ 103 | }while (0) 104 | 105 | #define __HAL_UNLOCK(__HANDLE__) \ 106 | do{ \ 107 | (__HANDLE__)->Lock = HAL_UNLOCKED; \ 108 | }while (0) 109 | #endif /* USE_RTOS */ 110 | 111 | #if defined ( __GNUC__ ) 112 | #ifndef __weak 113 | #define __weak __attribute__((weak)) 114 | #endif /* __weak */ 115 | #ifndef __packed 116 | #define __packed __attribute__((__packed__)) 117 | #endif /* __packed */ 118 | #endif /* __GNUC__ */ 119 | 120 | 121 | /* Macro to get variable aligned on 4-bytes, for __ICCARM__ the directive "#pragma data_alignment=4" must be used instead */ 122 | #if defined (__GNUC__) /* GNU Compiler */ 123 | #ifndef __ALIGN_END 124 | #define __ALIGN_END __attribute__ ((aligned (4))) 125 | #endif /* __ALIGN_END */ 126 | #ifndef __ALIGN_BEGIN 127 | #define __ALIGN_BEGIN 128 | #endif /* __ALIGN_BEGIN */ 129 | #else 130 | #ifndef __ALIGN_END 131 | #define __ALIGN_END 132 | #endif /* __ALIGN_END */ 133 | #ifndef __ALIGN_BEGIN 134 | #if defined (__CC_ARM) /* ARM Compiler */ 135 | #define __ALIGN_BEGIN __align(4) 136 | #elif defined (__ICCARM__) /* IAR Compiler */ 137 | #define __ALIGN_BEGIN 138 | #endif /* __CC_ARM */ 139 | #endif /* __ALIGN_BEGIN */ 140 | #endif /* __GNUC__ */ 141 | 142 | /** 143 | * @brief __NOINLINE definition 144 | */ 145 | #if defined ( __CC_ARM ) || defined ( __GNUC__ ) 146 | /* ARM & GNUCompiler 147 | ---------------- 148 | */ 149 | #define __NOINLINE __attribute__ ( (noinline) ) 150 | 151 | #elif defined ( __ICCARM__ ) 152 | /* ICCARM Compiler 153 | --------------- 154 | */ 155 | #define __NOINLINE _Pragma("optimize = no_inline") 156 | 157 | #endif 158 | 159 | #ifdef __cplusplus 160 | } 161 | #endif 162 | 163 | #endif /* ___STM32F0xx_HAL_DEF */ 164 | 165 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 166 | 167 | -------------------------------------------------------------------------------- /Src/usbd_conf.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #include 28 | #include "usbd_core.h" 29 | #include "usbd_gs_can.h" 30 | 31 | PCD_HandleTypeDef hpcd_USB_FS; 32 | 33 | void HAL_PCD_MspInit(PCD_HandleTypeDef* hpcd) 34 | { 35 | if(hpcd->Instance==USB) { 36 | __HAL_RCC_USB_CLK_ENABLE(); 37 | HAL_NVIC_SetPriority(USB_IRQn, 1, 0); 38 | HAL_NVIC_EnableIRQ(USB_IRQn); 39 | } 40 | } 41 | 42 | void HAL_PCD_MspDeInit(PCD_HandleTypeDef* hpcd) 43 | { 44 | if(hpcd->Instance==USB) { 45 | __HAL_RCC_USB_CLK_DISABLE(); 46 | HAL_NVIC_DisableIRQ(USB_IRQn); 47 | } 48 | } 49 | 50 | void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) 51 | { 52 | USBD_HandleTypeDef *pdev = (USBD_HandleTypeDef*)hpcd->pData; 53 | USBD_ParseSetupRequest((USBD_SetupReqTypedef*)&pdev->request, (uint8_t*)hpcd->Setup); 54 | 55 | bool request_was_handled = false; 56 | 57 | if ((pdev->request.bmRequest & 0x1F) == USB_REQ_RECIPIENT_DEVICE ) { // device request 58 | request_was_handled = USBD_GS_CAN_CustomDeviceRequest(pdev, &pdev->request); 59 | } 60 | 61 | if ((pdev->request.bmRequest & 0x1F) == USB_REQ_RECIPIENT_INTERFACE ) { // interface request 62 | request_was_handled = USBD_GS_CAN_CustomInterfaceRequest(pdev, &pdev->request); 63 | } 64 | 65 | if (!request_was_handled) { 66 | USBD_LL_SetupStage((USBD_HandleTypeDef*)hpcd->pData, (uint8_t *)hpcd->Setup); 67 | } 68 | } 69 | 70 | void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) 71 | { 72 | USBD_LL_DataOutStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff); 73 | } 74 | 75 | void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) 76 | { 77 | USBD_LL_DataInStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff); 78 | } 79 | 80 | void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) 81 | { 82 | USBD_LL_SOF((USBD_HandleTypeDef*)hpcd->pData); 83 | } 84 | 85 | void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd) 86 | { 87 | USBD_LL_SetSpeed((USBD_HandleTypeDef*)hpcd->pData, USBD_SPEED_FULL); 88 | USBD_LL_Reset((USBD_HandleTypeDef*)hpcd->pData); 89 | } 90 | 91 | void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd) 92 | { 93 | USBD_LL_Suspend((USBD_HandleTypeDef*)hpcd->pData); 94 | } 95 | 96 | void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd) 97 | { 98 | USBD_LL_Resume((USBD_HandleTypeDef*) hpcd->pData); 99 | } 100 | 101 | USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev) 102 | { 103 | /* Init USB_IP */ 104 | /* Link The driver to the stack */ 105 | hpcd_USB_FS.pData = pdev; 106 | pdev->pData = &hpcd_USB_FS; 107 | 108 | hpcd_USB_FS.Instance = USB; 109 | hpcd_USB_FS.Init.dev_endpoints = 5; 110 | hpcd_USB_FS.Init.speed = PCD_SPEED_FULL; 111 | hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64; 112 | hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED; 113 | hpcd_USB_FS.Init.low_power_enable = DISABLE; 114 | hpcd_USB_FS.Init.lpm_enable = DISABLE; 115 | HAL_PCD_Init(&hpcd_USB_FS); 116 | 117 | /* 118 | * PMA layout 119 | * 0x00 - 0x17 (24 bytes) metadata? 120 | * 0x18 - 0x57 (64 bytes) EP0 OUT 121 | * 0x58 - 0x97 (64 bytes) EP0 IN 122 | * 0x98 - 0xD7 (64 bytes) EP1 IN 123 | * 0xD8 - 0x157 (128 bytes) EP1 OUT (buffer 1) 124 | * 0x158 - 0x1D7 (128 bytes) EP1 OUT (buffer 2) 125 | */ 126 | HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x00 , PCD_SNG_BUF, 24); 127 | HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x80 , PCD_SNG_BUF, 0x58); 128 | HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x81 , PCD_SNG_BUF, 0x98); 129 | HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x02 , PCD_DBL_BUF, 0x00D80158); 130 | 131 | return USBD_OK; 132 | } 133 | 134 | USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev) 135 | { 136 | HAL_PCD_DeInit((PCD_HandleTypeDef*)pdev->pData); 137 | return USBD_OK; 138 | } 139 | 140 | USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev) 141 | { 142 | HAL_PCD_Start((PCD_HandleTypeDef*)pdev->pData); 143 | return USBD_OK; 144 | } 145 | 146 | USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev) 147 | { 148 | HAL_PCD_Stop((PCD_HandleTypeDef*) pdev->pData); 149 | return USBD_OK; 150 | } 151 | 152 | USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t ep_type, uint16_t ep_mps) 153 | { 154 | HAL_PCD_EP_Open((PCD_HandleTypeDef*) pdev->pData, ep_addr, ep_mps, ep_type); 155 | return USBD_OK; 156 | } 157 | 158 | USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) 159 | { 160 | HAL_PCD_EP_Close((PCD_HandleTypeDef*) pdev->pData, ep_addr); 161 | return USBD_OK; 162 | } 163 | 164 | USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) 165 | { 166 | HAL_PCD_EP_Flush((PCD_HandleTypeDef*) pdev->pData, ep_addr); 167 | return USBD_OK; 168 | } 169 | 170 | USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) 171 | { 172 | HAL_PCD_EP_SetStall((PCD_HandleTypeDef*) pdev->pData, ep_addr); 173 | return USBD_OK; 174 | } 175 | 176 | USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) 177 | { 178 | HAL_PCD_EP_ClrStall((PCD_HandleTypeDef*) pdev->pData, ep_addr); 179 | return USBD_OK; 180 | } 181 | 182 | uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) 183 | { 184 | PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef*) pdev->pData; 185 | return ((ep_addr & 0x80) == 0x80) 186 | ? hpcd->IN_ep[ep_addr & 0x7F].is_stall 187 | : hpcd->OUT_ep[ep_addr & 0x7F].is_stall; 188 | } 189 | 190 | USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr) 191 | { 192 | HAL_PCD_SetAddress((PCD_HandleTypeDef*) pdev->pData, dev_addr); 193 | return USBD_OK; 194 | } 195 | 196 | USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint16_t size) 197 | { 198 | HAL_PCD_EP_Transmit((PCD_HandleTypeDef*) pdev->pData, ep_addr, pbuf, size); 199 | return USBD_OK; 200 | } 201 | 202 | USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint16_t size) 203 | { 204 | HAL_PCD_EP_Receive((PCD_HandleTypeDef*) pdev->pData, ep_addr, pbuf, size); 205 | return USBD_OK; 206 | } 207 | 208 | uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr) 209 | { 210 | return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef*) pdev->pData, ep_addr); 211 | } 212 | -------------------------------------------------------------------------------- /Src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #include 28 | #include 29 | 30 | #include "config.h" 31 | #include "stm32f0xx_hal.h" 32 | #include "usbd_def.h" 33 | #include "usbd_desc.h" 34 | #include "usbd_core.h" 35 | #include "usbd_gs_can.h" 36 | #include "gpio.h" 37 | #include "queue.h" 38 | #include "gs_usb.h" 39 | #include "can.h" 40 | #include "led.h" 41 | #include "dfu.h" 42 | #include "timer.h" 43 | #include "flash.h" 44 | #include "util.h" 45 | 46 | void HAL_MspInit(void); 47 | void SystemClock_Config(void); 48 | static bool send_to_host_or_enqueue(struct gs_host_frame *frame); 49 | static void send_to_host(); 50 | 51 | can_data_t hCAN; 52 | USBD_HandleTypeDef hUSB; 53 | led_data_t hLED; 54 | 55 | queue_t *q_frame_pool; 56 | queue_t *q_from_host; 57 | queue_t *q_to_host; 58 | 59 | uint32_t received_count=0; 60 | 61 | 62 | int main(void) 63 | { 64 | uint32_t last_can_error_status = 0; 65 | 66 | flash_bootloaderSwitcher(); 67 | 68 | HAL_Init(); 69 | SystemClock_Config(); 70 | 71 | flash_load(); 72 | 73 | gpio_init(); 74 | 75 | //led_init(&hLED, LED1_GPIO_Port, LED1_Pin, LED1_Active_High, LED2_GPIO_Port, LED2_Pin, LED2_Active_High); 76 | 77 | /* nice wake-up pattern */ 78 | for(uint8_t i=0; i<10; i++) 79 | { 80 | // HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin); 81 | // HAL_Delay(50); 82 | // HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin); 83 | } 84 | 85 | //led_set_mode(&hLED, led_mode_off); 86 | timer_init(); 87 | 88 | can_init(&hCAN, CAN); 89 | can_disable(&hCAN); 90 | 91 | 92 | q_frame_pool = queue_create(CAN_QUEUE_SIZE); 93 | q_from_host = queue_create(CAN_QUEUE_SIZE); 94 | q_to_host = queue_create(CAN_QUEUE_SIZE); 95 | assert_basic(q_frame_pool && q_from_host && q_to_host); 96 | 97 | struct gs_host_frame *msgbuf = calloc(CAN_QUEUE_SIZE, sizeof(struct gs_host_frame)); 98 | assert_basic(msgbuf); 99 | 100 | for (unsigned i=0; itimestamp_us = timer_get(); 120 | send_to_host_or_enqueue(frame); 121 | 122 | // led_indicate_trx(&hLED, led_2); 123 | } else { 124 | queue_push_front(q_from_host, frame); // retry later 125 | } 126 | } 127 | 128 | if (USBD_GS_CAN_TxReady(&hUSB)) { 129 | send_to_host(); 130 | } 131 | 132 | if (can_is_rx_pending(&hCAN)) { 133 | struct gs_host_frame *frame = queue_pop_front(q_frame_pool); 134 | if (frame != 0) 135 | { 136 | if (can_receive(&hCAN, frame)) { 137 | received_count++; 138 | 139 | frame->timestamp_us = timer_get(); 140 | frame->echo_id = 0xFFFFFFFF; // not a echo frame 141 | frame->channel = 0; 142 | frame->flags = 0; 143 | frame->reserved = 0; 144 | 145 | send_to_host_or_enqueue(frame); 146 | 147 | // led_indicate_trx(&hLED, led_1); 148 | } 149 | else 150 | { 151 | queue_push_back(q_frame_pool, frame); 152 | } 153 | } 154 | // If there are frames to receive, don't report any error frames. The 155 | // best we can localize the errors to is "after the last successfully 156 | // received frame", so wait until we get there. LEC will hold some error 157 | // to report even if multiple pass by. 158 | } else { 159 | uint32_t can_err = can_get_error_status(&hCAN); 160 | struct gs_host_frame *frame = queue_pop_front(q_frame_pool); 161 | if (frame != 0) { 162 | frame->timestamp_us = timer_get(); 163 | if (can_parse_error_status(can_err, last_can_error_status, &hCAN, frame)) { 164 | send_to_host_or_enqueue(frame); 165 | last_can_error_status = can_err; 166 | } else { 167 | queue_push_back(q_frame_pool, frame); 168 | } 169 | } 170 | } 171 | 172 | //led_update(&hLED); 173 | 174 | if (USBD_GS_CAN_DfuDetachRequested(&hUSB)) { 175 | dfu_run_bootloader(); 176 | } 177 | 178 | } 179 | } 180 | 181 | void HAL_MspInit(void) 182 | { 183 | __HAL_RCC_SYSCFG_CLK_ENABLE(); 184 | HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); 185 | } 186 | 187 | void SystemClock_Config(void) 188 | { 189 | RCC_OscInitTypeDef RCC_OscInitStruct; 190 | RCC_ClkInitTypeDef RCC_ClkInitStruct; 191 | RCC_PeriphCLKInitTypeDef PeriphClkInit; 192 | RCC_CRSInitTypeDef RCC_CRSInitStruct; 193 | 194 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48; 195 | RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; 196 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; 197 | HAL_RCC_OscConfig(&RCC_OscInitStruct); 198 | 199 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK 200 | |RCC_CLOCKTYPE_PCLK1; 201 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48; 202 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 203 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; 204 | HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1); 205 | 206 | PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB; 207 | PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48; 208 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); 209 | 210 | __HAL_RCC_CRS_CLK_ENABLE(); 211 | 212 | RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1; 213 | RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_USB; 214 | RCC_CRSInitStruct.Polarity = RCC_CRS_SYNC_POLARITY_RISING; 215 | RCC_CRSInitStruct.ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000,1000); 216 | RCC_CRSInitStruct.ErrorLimitValue = 34; 217 | RCC_CRSInitStruct.HSI48CalibrationValue = 32; 218 | HAL_RCCEx_CRSConfig(&RCC_CRSInitStruct); 219 | 220 | HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); 221 | 222 | HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); 223 | 224 | /* SysTick_IRQn interrupt configuration */ 225 | HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); 226 | } 227 | 228 | bool send_to_host_or_enqueue(struct gs_host_frame *frame) 229 | { 230 | queue_push_back(q_to_host, frame); 231 | return true; 232 | } 233 | 234 | void send_to_host() 235 | { 236 | struct gs_host_frame *frame = queue_pop_front(q_to_host); 237 | 238 | if(!frame) 239 | return; 240 | 241 | if (USBD_GS_CAN_SendFrame(&hUSB, frame) == USBD_OK) { 242 | queue_push_back(q_frame_pool, frame); 243 | } else { 244 | queue_push_front(q_to_host, frame); 245 | } 246 | } 247 | 248 | -------------------------------------------------------------------------------- /include/gs_usb.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #pragma once 28 | 29 | #define u32 uint32_t 30 | #define u8 uint8_t 31 | 32 | #define GSUSB_ENDPOINT_IN 0x81 33 | #define GSUSB_ENDPOINT_OUT 0x02 34 | 35 | 36 | #define GS_CAN_MODE_NORMAL 0 37 | #define GS_CAN_MODE_LISTEN_ONLY (1<<0) 38 | #define GS_CAN_MODE_LOOP_BACK (1<<1) 39 | #define GS_CAN_MODE_TRIPLE_SAMPLE (1<<2) 40 | #define GS_CAN_MODE_ONE_SHOT (1<<3) 41 | #define GS_CAN_MODE_HW_TIMESTAMP (1<<4) 42 | 43 | #define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE (1<<7) 44 | 45 | #define GS_CAN_FEATURE_LISTEN_ONLY (1<<0) 46 | #define GS_CAN_FEATURE_LOOP_BACK (1<<1) 47 | #define GS_CAN_FEATURE_TRIPLE_SAMPLE (1<<2) 48 | #define GS_CAN_FEATURE_ONE_SHOT (1<<3) 49 | #define GS_CAN_FEATURE_HW_TIMESTAMP (1<<4) 50 | #define GS_CAN_FEATURE_IDENTIFY (1<<5) 51 | #define GS_CAN_FEATURE_USER_ID (1<<6) 52 | 53 | #define GS_CAN_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE (1<<7) 54 | 55 | #define GS_CAN_FLAG_OVERFLOW 1 56 | 57 | #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */ 58 | #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */ 59 | #define CAN_ERR_FLAG 0x20000000U /* error message frame */ 60 | 61 | #define CAN_ERR_DLC 8 /* dlc for error message frames */ 62 | 63 | /* error class (mask) in can_id */ 64 | #define CAN_ERR_TX_TIMEOUT 0x00000001U /* TX timeout (by netdevice driver) */ 65 | #define CAN_ERR_LOSTARB 0x00000002U /* lost arbitration / data[0] */ 66 | #define CAN_ERR_CRTL 0x00000004U /* controller problems / data[1] */ 67 | #define CAN_ERR_PROT 0x00000008U /* protocol violations / data[2..3] */ 68 | #define CAN_ERR_TRX 0x00000010U /* transceiver status / data[4] */ 69 | #define CAN_ERR_ACK 0x00000020U /* received no ACK on transmission */ 70 | #define CAN_ERR_BUSOFF 0x00000040U /* bus off */ 71 | #define CAN_ERR_BUSERROR 0x00000080U /* bus error (may flood!) */ 72 | #define CAN_ERR_RESTARTED 0x00000100U /* controller restarted */ 73 | 74 | /* arbitration lost in bit ... / data[0] */ 75 | #define CAN_ERR_LOSTARB_UNSPEC 0x00 /* unspecified */ 76 | /* else bit number in bitstream */ 77 | 78 | /* error status of CAN-controller / data[1] */ 79 | #define CAN_ERR_CRTL_UNSPEC 0x00 /* unspecified */ 80 | #define CAN_ERR_CRTL_RX_OVERFLOW 0x01 /* RX buffer overflow */ 81 | #define CAN_ERR_CRTL_TX_OVERFLOW 0x02 /* TX buffer overflow */ 82 | #define CAN_ERR_CRTL_RX_WARNING 0x04 /* reached warning level for RX errors */ 83 | #define CAN_ERR_CRTL_TX_WARNING 0x08 /* reached warning level for TX errors */ 84 | #define CAN_ERR_CRTL_RX_PASSIVE 0x10 /* reached error passive status RX */ 85 | #define CAN_ERR_CRTL_TX_PASSIVE 0x20 /* reached error passive status TX */ 86 | /* (at least one error counter exceeds */ 87 | /* the protocol-defined level of 127) */ 88 | #define CAN_ERR_CRTL_ACTIVE 0x40 /* recovered to error active state */ 89 | 90 | /* error in CAN protocol (type) / data[2] */ 91 | #define CAN_ERR_PROT_UNSPEC 0x00 /* unspecified */ 92 | #define CAN_ERR_PROT_BIT 0x01 /* single bit error */ 93 | #define CAN_ERR_PROT_FORM 0x02 /* frame format error */ 94 | #define CAN_ERR_PROT_STUFF 0x04 /* bit stuffing error */ 95 | #define CAN_ERR_PROT_BIT0 0x08 /* unable to send dominant bit */ 96 | #define CAN_ERR_PROT_BIT1 0x10 /* unable to send recessive bit */ 97 | #define CAN_ERR_PROT_OVERLOAD 0x20 /* bus overload */ 98 | #define CAN_ERR_PROT_ACTIVE 0x40 /* active error announcement */ 99 | #define CAN_ERR_PROT_TX 0x80 /* error occurred on transmission */ 100 | 101 | /* error in CAN protocol (location) / data[3] */ 102 | #define CAN_ERR_PROT_LOC_UNSPEC 0x00 /* unspecified */ 103 | #define CAN_ERR_PROT_LOC_SOF 0x03 /* start of frame */ 104 | #define CAN_ERR_PROT_LOC_ID28_21 0x02 /* ID bits 28 - 21 (SFF: 10 - 3) */ 105 | #define CAN_ERR_PROT_LOC_ID20_18 0x06 /* ID bits 20 - 18 (SFF: 2 - 0 )*/ 106 | #define CAN_ERR_PROT_LOC_SRTR 0x04 /* substitute RTR (SFF: RTR) */ 107 | #define CAN_ERR_PROT_LOC_IDE 0x05 /* identifier extension */ 108 | #define CAN_ERR_PROT_LOC_ID17_13 0x07 /* ID bits 17-13 */ 109 | #define CAN_ERR_PROT_LOC_ID12_05 0x0F /* ID bits 12-5 */ 110 | #define CAN_ERR_PROT_LOC_ID04_00 0x0E /* ID bits 4-0 */ 111 | #define CAN_ERR_PROT_LOC_RTR 0x0C /* RTR */ 112 | #define CAN_ERR_PROT_LOC_RES1 0x0D /* reserved bit 1 */ 113 | #define CAN_ERR_PROT_LOC_RES0 0x09 /* reserved bit 0 */ 114 | #define CAN_ERR_PROT_LOC_DLC 0x0B /* data length code */ 115 | #define CAN_ERR_PROT_LOC_DATA 0x0A /* data section */ 116 | #define CAN_ERR_PROT_LOC_CRC_SEQ 0x08 /* CRC sequence */ 117 | #define CAN_ERR_PROT_LOC_CRC_DEL 0x18 /* CRC delimiter */ 118 | #define CAN_ERR_PROT_LOC_ACK 0x19 /* ACK slot */ 119 | #define CAN_ERR_PROT_LOC_ACK_DEL 0x1B /* ACK delimiter */ 120 | #define CAN_ERR_PROT_LOC_EOF 0x1A /* end of frame */ 121 | #define CAN_ERR_PROT_LOC_INTERM 0x12 /* intermission */ 122 | 123 | /* error status of CAN-transceiver / data[4] */ 124 | /* CANH CANL */ 125 | #define CAN_ERR_TRX_UNSPEC 0x00 /* 0000 0000 */ 126 | #define CAN_ERR_TRX_CANH_NO_WIRE 0x04 /* 0000 0100 */ 127 | #define CAN_ERR_TRX_CANH_SHORT_TO_BAT 0x05 /* 0000 0101 */ 128 | #define CAN_ERR_TRX_CANH_SHORT_TO_VCC 0x06 /* 0000 0110 */ 129 | #define CAN_ERR_TRX_CANH_SHORT_TO_GND 0x07 /* 0000 0111 */ 130 | #define CAN_ERR_TRX_CANL_NO_WIRE 0x40 /* 0100 0000 */ 131 | #define CAN_ERR_TRX_CANL_SHORT_TO_BAT 0x50 /* 0101 0000 */ 132 | #define CAN_ERR_TRX_CANL_SHORT_TO_VCC 0x60 /* 0110 0000 */ 133 | #define CAN_ERR_TRX_CANL_SHORT_TO_GND 0x70 /* 0111 0000 */ 134 | #define CAN_ERR_TRX_CANL_SHORT_TO_CANH 0x80 /* 1000 0000 */ 135 | 136 | 137 | enum gs_usb_breq { 138 | GS_USB_BREQ_HOST_FORMAT = 0, 139 | GS_USB_BREQ_BITTIMING, 140 | GS_USB_BREQ_MODE, 141 | GS_USB_BREQ_BERR, 142 | GS_USB_BREQ_BT_CONST, 143 | GS_USB_BREQ_DEVICE_CONFIG, 144 | GS_USB_BREQ_TIMESTAMP, 145 | GS_USB_BREQ_IDENTIFY, 146 | GS_USB_BREQ_GET_USER_ID, 147 | GS_USB_BREQ_SET_USER_ID, 148 | }; 149 | 150 | enum gs_can_mode { 151 | /* reset a channel. turns it off */ 152 | GS_CAN_MODE_RESET = 0, 153 | /* starts a channel */ 154 | GS_CAN_MODE_START 155 | }; 156 | 157 | enum gs_can_state { 158 | GS_CAN_STATE_ERROR_ACTIVE = 0, 159 | GS_CAN_STATE_ERROR_WARNING, 160 | GS_CAN_STATE_ERROR_PASSIVE, 161 | GS_CAN_STATE_BUS_OFF, 162 | GS_CAN_STATE_STOPPED, 163 | GS_CAN_STATE_SLEEPING 164 | }; 165 | 166 | /* data types passed between host and device */ 167 | struct gs_host_config { 168 | u32 byte_order; 169 | } __packed; 170 | /* All data exchanged between host and device is exchanged in host byte order, 171 | * thanks to the struct gs_host_config byte_order member, which is sent first 172 | * to indicate the desired byte order. 173 | */ 174 | 175 | struct gs_device_config { 176 | u8 reserved1; 177 | u8 reserved2; 178 | u8 reserved3; 179 | u8 icount; 180 | u32 sw_version; 181 | u32 hw_version; 182 | } __packed; 183 | 184 | struct gs_device_mode { 185 | u32 mode; 186 | u32 flags; 187 | } __packed; 188 | 189 | struct gs_device_state { 190 | u32 state; 191 | u32 rxerr; 192 | u32 txerr; 193 | } __packed; 194 | 195 | struct gs_device_bittiming { 196 | u32 prop_seg; 197 | u32 phase_seg1; 198 | u32 phase_seg2; 199 | u32 sjw; 200 | u32 brp; 201 | } __packed; 202 | 203 | struct gs_device_bt_const { 204 | u32 feature; 205 | u32 fclk_can; 206 | u32 tseg1_min; 207 | u32 tseg1_max; 208 | u32 tseg2_min; 209 | u32 tseg2_max; 210 | u32 sjw_max; 211 | u32 brp_min; 212 | u32 brp_max; 213 | u32 brp_inc; 214 | } __packed; 215 | 216 | struct gs_host_frame { 217 | u32 echo_id; 218 | u32 can_id; 219 | 220 | u8 can_dlc; 221 | u8 channel; 222 | u8 flags; 223 | u8 reserved; 224 | 225 | u8 data[8]; 226 | 227 | u32 timestamp_us; 228 | 229 | } __packed; 230 | 231 | struct gs_tx_context { 232 | struct gs_can *dev; 233 | unsigned int echo_id; 234 | }; 235 | 236 | -------------------------------------------------------------------------------- /libs/STM32_HAL/include/stm32f0xx/stm32f0xx_ll_usb.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f0xx_ll_usb.h 4 | * @author MCD Application Team 5 | * @brief Header file of USB Low Layer HAL module. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2016 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef STM32F0xx_LL_USB_H 22 | #define STM32F0xx_LL_USB_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm32f0xx_hal_def.h" 30 | 31 | #if defined (USB) 32 | /** @addtogroup STM32F0xx_HAL_Driver 33 | * @{ 34 | */ 35 | 36 | /** @addtogroup USB_LL 37 | * @{ 38 | */ 39 | 40 | /* Exported types ------------------------------------------------------------*/ 41 | 42 | /** 43 | * @brief USB Mode definition 44 | */ 45 | 46 | 47 | 48 | typedef enum 49 | { 50 | USB_DEVICE_MODE = 0 51 | } USB_ModeTypeDef; 52 | 53 | /** 54 | * @brief USB Initialization Structure definition 55 | */ 56 | typedef struct 57 | { 58 | uint32_t dev_endpoints; /*!< Device Endpoints number. 59 | This parameter depends on the used USB core. 60 | This parameter must be a number between Min_Data = 1 and Max_Data = 15 */ 61 | 62 | uint32_t speed; /*!< USB Core speed. 63 | This parameter can be any value of @ref USB_Core_Speed */ 64 | 65 | uint32_t ep0_mps; /*!< Set the Endpoint 0 Max Packet size. */ 66 | 67 | uint32_t phy_itface; /*!< Select the used PHY interface. 68 | This parameter can be any value of @ref USB_Core_PHY */ 69 | 70 | uint32_t Sof_enable; /*!< Enable or disable the output of the SOF signal. */ 71 | 72 | uint32_t low_power_enable; /*!< Enable or disable Low Power mode */ 73 | 74 | uint32_t lpm_enable; /*!< Enable or disable Battery charging. */ 75 | 76 | uint32_t battery_charging_enable; /*!< Enable or disable Battery charging. */ 77 | } USB_CfgTypeDef; 78 | 79 | typedef struct 80 | { 81 | uint8_t num; /*!< Endpoint number 82 | This parameter must be a number between Min_Data = 1 and Max_Data = 15 */ 83 | 84 | uint8_t is_in; /*!< Endpoint direction 85 | This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ 86 | 87 | uint8_t is_stall; /*!< Endpoint stall condition 88 | This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ 89 | 90 | uint8_t type; /*!< Endpoint type 91 | This parameter can be any value of @ref USB_EP_Type */ 92 | 93 | uint8_t data_pid_start; /*!< Initial data PID 94 | This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ 95 | 96 | uint16_t pmaadress; /*!< PMA Address 97 | This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ 98 | 99 | uint16_t pmaaddr0; /*!< PMA Address0 100 | This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ 101 | 102 | uint16_t pmaaddr1; /*!< PMA Address1 103 | This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ 104 | 105 | uint8_t doublebuffer; /*!< Double buffer enable 106 | This parameter can be 0 or 1 */ 107 | 108 | uint16_t tx_fifo_num; /*!< This parameter is not required by USB Device FS peripheral, it is used 109 | only by USB OTG FS peripheral 110 | This parameter is added to ensure compatibility across USB peripherals */ 111 | 112 | uint32_t maxpacket; /*!< Endpoint Max packet size 113 | This parameter must be a number between Min_Data = 0 and Max_Data = 64KB */ 114 | 115 | uint8_t *xfer_buff; /*!< Pointer to transfer buffer */ 116 | 117 | uint32_t xfer_len; /*!< Current transfer length */ 118 | 119 | uint32_t xfer_count; /*!< Partial transfer length in case of multi packet transfer */ 120 | 121 | } USB_EPTypeDef; 122 | 123 | 124 | /* Exported constants --------------------------------------------------------*/ 125 | 126 | /** @defgroup PCD_Exported_Constants PCD Exported Constants 127 | * @{ 128 | */ 129 | 130 | 131 | /** @defgroup USB_LL_EP0_MPS USB Low Layer EP0 MPS 132 | * @{ 133 | */ 134 | #define DEP0CTL_MPS_64 0U 135 | #define DEP0CTL_MPS_32 1U 136 | #define DEP0CTL_MPS_16 2U 137 | #define DEP0CTL_MPS_8 3U 138 | /** 139 | * @} 140 | */ 141 | 142 | /** @defgroup USB_LL_EP_Type USB Low Layer EP Type 143 | * @{ 144 | */ 145 | #define EP_TYPE_CTRL 0U 146 | #define EP_TYPE_ISOC 1U 147 | #define EP_TYPE_BULK 2U 148 | #define EP_TYPE_INTR 3U 149 | #define EP_TYPE_MSK 3U 150 | /** 151 | * @} 152 | */ 153 | 154 | /** @defgroup USB_LL Device Speed 155 | * @{ 156 | */ 157 | #define USBD_FS_SPEED 2U 158 | /** 159 | * @} 160 | */ 161 | 162 | #define BTABLE_ADDRESS 0x000U 163 | #define PMA_ACCESS 1U 164 | 165 | #define EP_ADDR_MSK 0x7U 166 | /** 167 | * @} 168 | */ 169 | 170 | /* Exported macro ------------------------------------------------------------*/ 171 | /** 172 | * @} 173 | */ 174 | 175 | /* Exported functions --------------------------------------------------------*/ 176 | /** @addtogroup USB_LL_Exported_Functions USB Low Layer Exported Functions 177 | * @{ 178 | */ 179 | 180 | 181 | HAL_StatusTypeDef USB_CoreInit(USB_TypeDef *USBx, USB_CfgTypeDef cfg); 182 | HAL_StatusTypeDef USB_DevInit(USB_TypeDef *USBx, USB_CfgTypeDef cfg); 183 | HAL_StatusTypeDef USB_EnableGlobalInt(USB_TypeDef *USBx); 184 | HAL_StatusTypeDef USB_DisableGlobalInt(USB_TypeDef *USBx); 185 | HAL_StatusTypeDef USB_SetCurrentMode(USB_TypeDef *USBx, USB_ModeTypeDef mode); 186 | HAL_StatusTypeDef USB_SetDevSpeed(USB_TypeDef *USBx, uint8_t speed); 187 | HAL_StatusTypeDef USB_FlushRxFifo(USB_TypeDef *USBx); 188 | HAL_StatusTypeDef USB_FlushTxFifo(USB_TypeDef *USBx, uint32_t num); 189 | HAL_StatusTypeDef USB_ActivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep); 190 | HAL_StatusTypeDef USB_DeactivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep); 191 | HAL_StatusTypeDef USB_EPStartXfer(USB_TypeDef *USBx, USB_EPTypeDef *ep); 192 | HAL_StatusTypeDef USB_WritePacket(USB_TypeDef *USBx, uint8_t *src, uint8_t ch_ep_num, uint16_t len); 193 | void *USB_ReadPacket(USB_TypeDef *USBx, uint8_t *dest, uint16_t len); 194 | HAL_StatusTypeDef USB_EPSetStall(USB_TypeDef *USBx, USB_EPTypeDef *ep); 195 | HAL_StatusTypeDef USB_EPClearStall(USB_TypeDef *USBx, USB_EPTypeDef *ep); 196 | HAL_StatusTypeDef USB_SetDevAddress(USB_TypeDef *USBx, uint8_t address); 197 | HAL_StatusTypeDef USB_DevConnect(USB_TypeDef *USBx); 198 | HAL_StatusTypeDef USB_DevDisconnect(USB_TypeDef *USBx); 199 | HAL_StatusTypeDef USB_StopDevice(USB_TypeDef *USBx); 200 | HAL_StatusTypeDef USB_EP0_OutStart(USB_TypeDef *USBx, uint8_t *psetup); 201 | uint32_t USB_ReadInterrupts(USB_TypeDef *USBx); 202 | uint32_t USB_ReadDevAllOutEpInterrupt(USB_TypeDef *USBx); 203 | uint32_t USB_ReadDevOutEPInterrupt(USB_TypeDef *USBx, uint8_t epnum); 204 | uint32_t USB_ReadDevAllInEpInterrupt(USB_TypeDef *USBx); 205 | uint32_t USB_ReadDevInEPInterrupt(USB_TypeDef *USBx, uint8_t epnum); 206 | void USB_ClearInterrupts(USB_TypeDef *USBx, uint32_t interrupt); 207 | 208 | HAL_StatusTypeDef USB_ActivateRemoteWakeup(USB_TypeDef *USBx); 209 | HAL_StatusTypeDef USB_DeActivateRemoteWakeup(USB_TypeDef *USBx); 210 | void USB_WritePMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes); 211 | void USB_ReadPMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes); 212 | 213 | /** 214 | * @} 215 | */ 216 | 217 | /** 218 | * @} 219 | */ 220 | 221 | /** 222 | * @} 223 | */ 224 | 225 | /** 226 | * @} 227 | */ 228 | #endif /* defined (USB) */ 229 | 230 | #ifdef __cplusplus 231 | } 232 | #endif 233 | 234 | 235 | #endif /* STM32F0xx_LL_USB_H */ 236 | 237 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 238 | -------------------------------------------------------------------------------- /libs/STM32_HAL/include/cmsis/cmsis_compiler.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file cmsis_compiler.h 3 | * @brief CMSIS compiler generic header file 4 | * @version V5.0.4 5 | * @date 10. January 2018 6 | ******************************************************************************/ 7 | /* 8 | * Copyright (c) 2009-2018 Arm Limited. All rights reserved. 9 | * 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the License); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | */ 24 | 25 | #ifndef __CMSIS_COMPILER_H 26 | #define __CMSIS_COMPILER_H 27 | 28 | #include 29 | 30 | /* 31 | * Arm Compiler 4/5 32 | */ 33 | #if defined ( __CC_ARM ) 34 | #include "cmsis_armcc.h" 35 | 36 | 37 | /* 38 | * Arm Compiler 6 (armclang) 39 | */ 40 | #elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 41 | #include "cmsis_armclang.h" 42 | 43 | 44 | /* 45 | * GNU Compiler 46 | */ 47 | #elif defined ( __GNUC__ ) 48 | #include "cmsis_gcc.h" 49 | 50 | 51 | /* 52 | * IAR Compiler 53 | */ 54 | #elif defined ( __ICCARM__ ) 55 | #include 56 | 57 | 58 | /* 59 | * TI Arm Compiler 60 | */ 61 | #elif defined ( __TI_ARM__ ) 62 | #include 63 | 64 | #ifndef __ASM 65 | #define __ASM __asm 66 | #endif 67 | #ifndef __INLINE 68 | #define __INLINE inline 69 | #endif 70 | #ifndef __STATIC_INLINE 71 | #define __STATIC_INLINE static inline 72 | #endif 73 | #ifndef __STATIC_FORCEINLINE 74 | #define __STATIC_FORCEINLINE __STATIC_INLINE 75 | #endif 76 | #ifndef __NO_RETURN 77 | #define __NO_RETURN __attribute__((noreturn)) 78 | #endif 79 | #ifndef __USED 80 | #define __USED __attribute__((used)) 81 | #endif 82 | #ifndef __WEAK 83 | #define __WEAK __attribute__((weak)) 84 | #endif 85 | #ifndef __PACKED 86 | #define __PACKED __attribute__((packed)) 87 | #endif 88 | #ifndef __PACKED_STRUCT 89 | #define __PACKED_STRUCT struct __attribute__((packed)) 90 | #endif 91 | #ifndef __PACKED_UNION 92 | #define __PACKED_UNION union __attribute__((packed)) 93 | #endif 94 | #ifndef __UNALIGNED_UINT32 /* deprecated */ 95 | struct __attribute__((packed)) T_UINT32 { uint32_t v; }; 96 | #define __UNALIGNED_UINT32(x) (((struct T_UINT32 *)(x))->v) 97 | #endif 98 | #ifndef __UNALIGNED_UINT16_WRITE 99 | __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; }; 100 | #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void*)(addr))->v) = (val)) 101 | #endif 102 | #ifndef __UNALIGNED_UINT16_READ 103 | __PACKED_STRUCT T_UINT16_READ { uint16_t v; }; 104 | #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v) 105 | #endif 106 | #ifndef __UNALIGNED_UINT32_WRITE 107 | __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; }; 108 | #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val)) 109 | #endif 110 | #ifndef __UNALIGNED_UINT32_READ 111 | __PACKED_STRUCT T_UINT32_READ { uint32_t v; }; 112 | #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v) 113 | #endif 114 | #ifndef __ALIGNED 115 | #define __ALIGNED(x) __attribute__((aligned(x))) 116 | #endif 117 | #ifndef __RESTRICT 118 | #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored. 119 | #define __RESTRICT 120 | #endif 121 | 122 | 123 | /* 124 | * TASKING Compiler 125 | */ 126 | #elif defined ( __TASKING__ ) 127 | /* 128 | * The CMSIS functions have been implemented as intrinsics in the compiler. 129 | * Please use "carm -?i" to get an up to date list of all intrinsics, 130 | * Including the CMSIS ones. 131 | */ 132 | 133 | #ifndef __ASM 134 | #define __ASM __asm 135 | #endif 136 | #ifndef __INLINE 137 | #define __INLINE inline 138 | #endif 139 | #ifndef __STATIC_INLINE 140 | #define __STATIC_INLINE static inline 141 | #endif 142 | #ifndef __STATIC_FORCEINLINE 143 | #define __STATIC_FORCEINLINE __STATIC_INLINE 144 | #endif 145 | #ifndef __NO_RETURN 146 | #define __NO_RETURN __attribute__((noreturn)) 147 | #endif 148 | #ifndef __USED 149 | #define __USED __attribute__((used)) 150 | #endif 151 | #ifndef __WEAK 152 | #define __WEAK __attribute__((weak)) 153 | #endif 154 | #ifndef __PACKED 155 | #define __PACKED __packed__ 156 | #endif 157 | #ifndef __PACKED_STRUCT 158 | #define __PACKED_STRUCT struct __packed__ 159 | #endif 160 | #ifndef __PACKED_UNION 161 | #define __PACKED_UNION union __packed__ 162 | #endif 163 | #ifndef __UNALIGNED_UINT32 /* deprecated */ 164 | struct __packed__ T_UINT32 { uint32_t v; }; 165 | #define __UNALIGNED_UINT32(x) (((struct T_UINT32 *)(x))->v) 166 | #endif 167 | #ifndef __UNALIGNED_UINT16_WRITE 168 | __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; }; 169 | #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val)) 170 | #endif 171 | #ifndef __UNALIGNED_UINT16_READ 172 | __PACKED_STRUCT T_UINT16_READ { uint16_t v; }; 173 | #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v) 174 | #endif 175 | #ifndef __UNALIGNED_UINT32_WRITE 176 | __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; }; 177 | #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val)) 178 | #endif 179 | #ifndef __UNALIGNED_UINT32_READ 180 | __PACKED_STRUCT T_UINT32_READ { uint32_t v; }; 181 | #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v) 182 | #endif 183 | #ifndef __ALIGNED 184 | #define __ALIGNED(x) __align(x) 185 | #endif 186 | #ifndef __RESTRICT 187 | #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored. 188 | #define __RESTRICT 189 | #endif 190 | 191 | 192 | /* 193 | * COSMIC Compiler 194 | */ 195 | #elif defined ( __CSMC__ ) 196 | #include 197 | 198 | #ifndef __ASM 199 | #define __ASM _asm 200 | #endif 201 | #ifndef __INLINE 202 | #define __INLINE inline 203 | #endif 204 | #ifndef __STATIC_INLINE 205 | #define __STATIC_INLINE static inline 206 | #endif 207 | #ifndef __STATIC_FORCEINLINE 208 | #define __STATIC_FORCEINLINE __STATIC_INLINE 209 | #endif 210 | #ifndef __NO_RETURN 211 | // NO RETURN is automatically detected hence no warning here 212 | #define __NO_RETURN 213 | #endif 214 | #ifndef __USED 215 | #warning No compiler specific solution for __USED. __USED is ignored. 216 | #define __USED 217 | #endif 218 | #ifndef __WEAK 219 | #define __WEAK __weak 220 | #endif 221 | #ifndef __PACKED 222 | #define __PACKED @packed 223 | #endif 224 | #ifndef __PACKED_STRUCT 225 | #define __PACKED_STRUCT @packed struct 226 | #endif 227 | #ifndef __PACKED_UNION 228 | #define __PACKED_UNION @packed union 229 | #endif 230 | #ifndef __UNALIGNED_UINT32 /* deprecated */ 231 | @packed struct T_UINT32 { uint32_t v; }; 232 | #define __UNALIGNED_UINT32(x) (((struct T_UINT32 *)(x))->v) 233 | #endif 234 | #ifndef __UNALIGNED_UINT16_WRITE 235 | __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; }; 236 | #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val)) 237 | #endif 238 | #ifndef __UNALIGNED_UINT16_READ 239 | __PACKED_STRUCT T_UINT16_READ { uint16_t v; }; 240 | #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v) 241 | #endif 242 | #ifndef __UNALIGNED_UINT32_WRITE 243 | __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; }; 244 | #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val)) 245 | #endif 246 | #ifndef __UNALIGNED_UINT32_READ 247 | __PACKED_STRUCT T_UINT32_READ { uint32_t v; }; 248 | #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v) 249 | #endif 250 | #ifndef __ALIGNED 251 | #warning No compiler specific solution for __ALIGNED. __ALIGNED is ignored. 252 | #define __ALIGNED(x) 253 | #endif 254 | #ifndef __RESTRICT 255 | #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored. 256 | #define __RESTRICT 257 | #endif 258 | 259 | 260 | #else 261 | #error Unknown compiler. 262 | #endif 263 | 264 | 265 | #endif /* __CMSIS_COMPILER_H */ 266 | 267 | -------------------------------------------------------------------------------- /libs/STM32_HAL/src/stm32f0xx/stm32f0xx_hal_pcd_ex.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f0xx_hal_pcd_ex.c 4 | * @author MCD Application Team 5 | * @brief PCD Extended HAL module driver. 6 | * This file provides firmware functions to manage the following 7 | * functionalities of the USB Peripheral Controller: 8 | * + Extended features functions 9 | * 10 | ****************************************************************************** 11 | * @attention 12 | * 13 | *

© Copyright (c) 2016 STMicroelectronics. 14 | * All rights reserved.

15 | * 16 | * This software component is licensed by ST under BSD 3-Clause license, 17 | * the "License"; You may not use this file except in compliance with the 18 | * License. You may obtain a copy of the License at: 19 | * opensource.org/licenses/BSD-3-Clause 20 | * 21 | ****************************************************************************** 22 | */ 23 | 24 | /* Includes ------------------------------------------------------------------*/ 25 | #include "stm32f0xx_hal.h" 26 | 27 | /** @addtogroup STM32F0xx_HAL_Driver 28 | * @{ 29 | */ 30 | 31 | /** @defgroup PCDEx PCDEx 32 | * @brief PCD Extended HAL module driver 33 | * @{ 34 | */ 35 | 36 | #ifdef HAL_PCD_MODULE_ENABLED 37 | 38 | #if defined (USB) 39 | /* Private types -------------------------------------------------------------*/ 40 | /* Private variables ---------------------------------------------------------*/ 41 | /* Private constants ---------------------------------------------------------*/ 42 | /* Private macros ------------------------------------------------------------*/ 43 | /* Private functions ---------------------------------------------------------*/ 44 | /* Exported functions --------------------------------------------------------*/ 45 | 46 | /** @defgroup PCDEx_Exported_Functions PCDEx Exported Functions 47 | * @{ 48 | */ 49 | 50 | /** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions 51 | * @brief PCDEx control functions 52 | * 53 | @verbatim 54 | =============================================================================== 55 | ##### Extended features functions ##### 56 | =============================================================================== 57 | [..] This section provides functions allowing to: 58 | (+) Update FIFO configuration 59 | 60 | @endverbatim 61 | * @{ 62 | */ 63 | 64 | /** 65 | * @brief Configure PMA for EP 66 | * @param hpcd Device instance 67 | * @param ep_addr endpoint address 68 | * @param ep_kind endpoint Kind 69 | * USB_SNG_BUF: Single Buffer used 70 | * USB_DBL_BUF: Double Buffer used 71 | * @param pmaadress: EP address in The PMA: In case of single buffer endpoint 72 | * this parameter is 16-bit value providing the address 73 | * in PMA allocated to endpoint. 74 | * In case of double buffer endpoint this parameter 75 | * is a 32-bit value providing the endpoint buffer 0 address 76 | * in the LSB part of 32-bit value and endpoint buffer 1 address 77 | * in the MSB part of 32-bit value. 78 | * @retval HAL status 79 | */ 80 | 81 | HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd, 82 | uint16_t ep_addr, 83 | uint16_t ep_kind, 84 | uint32_t pmaadress) 85 | { 86 | PCD_EPTypeDef *ep; 87 | 88 | /* initialize ep structure*/ 89 | if ((0x80U & ep_addr) == 0x80U) 90 | { 91 | ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK]; 92 | } 93 | else 94 | { 95 | ep = &hpcd->OUT_ep[ep_addr]; 96 | } 97 | 98 | /* Here we check if the endpoint is single or double Buffer*/ 99 | if (ep_kind == PCD_SNG_BUF) 100 | { 101 | /* Single Buffer */ 102 | ep->doublebuffer = 0U; 103 | /* Configure the PMA */ 104 | ep->pmaadress = (uint16_t)pmaadress; 105 | } 106 | else /* USB_DBL_BUF */ 107 | { 108 | /* Double Buffer Endpoint */ 109 | ep->doublebuffer = 1U; 110 | /* Configure the PMA */ 111 | ep->pmaaddr0 = (uint16_t)(pmaadress & 0xFFFFU); 112 | ep->pmaaddr1 = (uint16_t)((pmaadress & 0xFFFF0000U) >> 16); 113 | } 114 | 115 | return HAL_OK; 116 | } 117 | 118 | /** 119 | * @brief Activate BatteryCharging feature. 120 | * @param hpcd PCD handle 121 | * @retval HAL status 122 | */ 123 | HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd) 124 | { 125 | USB_TypeDef *USBx = hpcd->Instance; 126 | hpcd->battery_charging_active = 1U; 127 | 128 | /* Enable BCD feature */ 129 | USBx->BCDR |= USB_BCDR_BCDEN; 130 | 131 | /* Enable DCD : Data Contact Detect */ 132 | USBx->BCDR &= ~(USB_BCDR_PDEN); 133 | USBx->BCDR &= ~(USB_BCDR_SDEN); 134 | USBx->BCDR |= USB_BCDR_DCDEN; 135 | 136 | return HAL_OK; 137 | } 138 | 139 | /** 140 | * @brief Deactivate BatteryCharging feature. 141 | * @param hpcd PCD handle 142 | * @retval HAL status 143 | */ 144 | HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd) 145 | { 146 | USB_TypeDef *USBx = hpcd->Instance; 147 | hpcd->battery_charging_active = 0U; 148 | 149 | /* Disable BCD feature */ 150 | USBx->BCDR &= ~(USB_BCDR_BCDEN); 151 | 152 | return HAL_OK; 153 | } 154 | 155 | /** 156 | * @brief Handle BatteryCharging Process. 157 | * @param hpcd PCD handle 158 | * @retval HAL status 159 | */ 160 | void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd) 161 | { 162 | USB_TypeDef *USBx = hpcd->Instance; 163 | uint32_t tickstart = HAL_GetTick(); 164 | 165 | /* Wait Detect flag or a timeout is happen*/ 166 | while ((USBx->BCDR & USB_BCDR_DCDET) == 0U) 167 | { 168 | /* Check for the Timeout */ 169 | if ((HAL_GetTick() - tickstart) > 1000U) 170 | { 171 | #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U) 172 | hpcd->BCDCallback(hpcd, PCD_BCD_ERROR); 173 | #else 174 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR); 175 | #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */ 176 | 177 | return; 178 | } 179 | } 180 | 181 | HAL_Delay(200U); 182 | 183 | /* Data Pin Contact ? Check Detect flag */ 184 | if ((USBx->BCDR & USB_BCDR_DCDET) == USB_BCDR_DCDET) 185 | { 186 | #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U) 187 | hpcd->BCDCallback(hpcd, PCD_BCD_CONTACT_DETECTION); 188 | #else 189 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION); 190 | #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */ 191 | } 192 | /* Primary detection: checks if connected to Standard Downstream Port 193 | (without charging capability) */ 194 | USBx->BCDR &= ~(USB_BCDR_DCDEN); 195 | HAL_Delay(50U); 196 | USBx->BCDR |= (USB_BCDR_PDEN); 197 | HAL_Delay(50U); 198 | 199 | /* If Charger detect ? */ 200 | if ((USBx->BCDR & USB_BCDR_PDET) == USB_BCDR_PDET) 201 | { 202 | /* Start secondary detection to check connection to Charging Downstream 203 | Port or Dedicated Charging Port */ 204 | USBx->BCDR &= ~(USB_BCDR_PDEN); 205 | HAL_Delay(50U); 206 | USBx->BCDR |= (USB_BCDR_SDEN); 207 | HAL_Delay(50U); 208 | 209 | /* If CDP ? */ 210 | if ((USBx->BCDR & USB_BCDR_SDET) == USB_BCDR_SDET) 211 | { 212 | /* Dedicated Downstream Port DCP */ 213 | #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U) 214 | hpcd->BCDCallback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT); 215 | #else 216 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT); 217 | #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */ 218 | } 219 | else 220 | { 221 | /* Charging Downstream Port CDP */ 222 | #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U) 223 | hpcd->BCDCallback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT); 224 | #else 225 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT); 226 | #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */ 227 | } 228 | } 229 | else /* NO */ 230 | { 231 | /* Standard Downstream Port */ 232 | #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U) 233 | hpcd->BCDCallback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT); 234 | #else 235 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT); 236 | #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */ 237 | } 238 | 239 | /* Battery Charging capability discovery finished Start Enumeration */ 240 | (void)HAL_PCDEx_DeActivateBCD(hpcd); 241 | #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U) 242 | hpcd->BCDCallback(hpcd, PCD_BCD_DISCOVERY_COMPLETED); 243 | #else 244 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED); 245 | #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */ 246 | } 247 | 248 | 249 | /** 250 | * @brief Activate LPM feature. 251 | * @param hpcd PCD handle 252 | * @retval HAL status 253 | */ 254 | HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd) 255 | { 256 | 257 | USB_TypeDef *USBx = hpcd->Instance; 258 | hpcd->lpm_active = 1U; 259 | hpcd->LPM_State = LPM_L0; 260 | 261 | USBx->LPMCSR |= USB_LPMCSR_LMPEN; 262 | USBx->LPMCSR |= USB_LPMCSR_LPMACK; 263 | 264 | return HAL_OK; 265 | } 266 | 267 | /** 268 | * @brief Deactivate LPM feature. 269 | * @param hpcd PCD handle 270 | * @retval HAL status 271 | */ 272 | HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd) 273 | { 274 | USB_TypeDef *USBx = hpcd->Instance; 275 | 276 | hpcd->lpm_active = 0U; 277 | 278 | USBx->LPMCSR &= ~(USB_LPMCSR_LMPEN); 279 | USBx->LPMCSR &= ~(USB_LPMCSR_LPMACK); 280 | 281 | return HAL_OK; 282 | } 283 | 284 | 285 | 286 | /** 287 | * @brief Send LPM message to user layer callback. 288 | * @param hpcd PCD handle 289 | * @param msg LPM message 290 | * @retval HAL status 291 | */ 292 | __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg) 293 | { 294 | /* Prevent unused argument(s) compilation warning */ 295 | UNUSED(hpcd); 296 | UNUSED(msg); 297 | 298 | /* NOTE : This function should not be modified, when the callback is needed, 299 | the HAL_PCDEx_LPM_Callback could be implemented in the user file 300 | */ 301 | } 302 | 303 | /** 304 | * @brief Send BatteryCharging message to user layer callback. 305 | * @param hpcd PCD handle 306 | * @param msg LPM message 307 | * @retval HAL status 308 | */ 309 | __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg) 310 | { 311 | /* Prevent unused argument(s) compilation warning */ 312 | UNUSED(hpcd); 313 | UNUSED(msg); 314 | 315 | /* NOTE : This function should not be modified, when the callback is needed, 316 | the HAL_PCDEx_BCD_Callback could be implemented in the user file 317 | */ 318 | } 319 | 320 | /** 321 | * @} 322 | */ 323 | 324 | /** 325 | * @} 326 | */ 327 | #endif /* defined (USB) */ 328 | #endif /* HAL_PCD_MODULE_ENABLED */ 329 | 330 | /** 331 | * @} 332 | */ 333 | 334 | /** 335 | * @} 336 | */ 337 | 338 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 339 | -------------------------------------------------------------------------------- /libs/STM32_HAL/include/stm32f0xx/stm32f0xx_hal_tim_ex.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f0xx_hal_tim_ex.h 4 | * @author MCD Application Team 5 | * @brief Header file of TIM HAL Extended module. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2016 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef STM32F0xx_HAL_TIM_EX_H 22 | #define STM32F0xx_HAL_TIM_EX_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm32f0xx_hal_def.h" 30 | 31 | /** @addtogroup STM32F0xx_HAL_Driver 32 | * @{ 33 | */ 34 | 35 | /** @addtogroup TIMEx 36 | * @{ 37 | */ 38 | 39 | /* Exported types ------------------------------------------------------------*/ 40 | /** @defgroup TIMEx_Exported_Types TIM Extended Exported Types 41 | * @{ 42 | */ 43 | 44 | /** 45 | * @brief TIM Hall sensor Configuration Structure definition 46 | */ 47 | 48 | typedef struct 49 | { 50 | uint32_t IC1Polarity; /*!< Specifies the active edge of the input signal. 51 | This parameter can be a value of @ref TIM_Input_Capture_Polarity */ 52 | 53 | uint32_t IC1Prescaler; /*!< Specifies the Input Capture Prescaler. 54 | This parameter can be a value of @ref TIM_Input_Capture_Prescaler */ 55 | 56 | uint32_t IC1Filter; /*!< Specifies the input capture filter. 57 | This parameter can be a number between Min_Data = 0x0 and Max_Data = 0xF */ 58 | 59 | uint32_t Commutation_Delay; /*!< Specifies the pulse value to be loaded into the Capture Compare Register. 60 | This parameter can be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF */ 61 | } TIM_HallSensor_InitTypeDef; 62 | /** 63 | * @} 64 | */ 65 | /* End of exported types -----------------------------------------------------*/ 66 | 67 | /* Exported constants --------------------------------------------------------*/ 68 | /** @defgroup TIMEx_Exported_Constants TIM Extended Exported Constants 69 | * @{ 70 | */ 71 | 72 | /** @defgroup TIMEx_Remap TIM Extended Remapping 73 | * @{ 74 | */ 75 | #define TIM_TIM14_GPIO (0x00000000U) /*!< TIM14 TI1 is connected to GPIO */ 76 | #define TIM_TIM14_RTC (0x00000001U) /*!< TIM14 TI1 is connected to RTC_clock */ 77 | #define TIM_TIM14_HSE (0x00000002U) /*!< TIM14 TI1 is connected to HSE/32U */ 78 | #define TIM_TIM14_MCO (0x00000003U) /*!< TIM14 TI1 is connected to MCO */ 79 | /** 80 | * @} 81 | */ 82 | 83 | /** 84 | * @} 85 | */ 86 | /* End of exported constants -------------------------------------------------*/ 87 | 88 | /* Exported macro ------------------------------------------------------------*/ 89 | /** @defgroup TIMEx_Exported_Macros TIM Extended Exported Macros 90 | * @{ 91 | */ 92 | 93 | /** 94 | * @} 95 | */ 96 | /* End of exported macro -----------------------------------------------------*/ 97 | 98 | /* Private macro -------------------------------------------------------------*/ 99 | /** @defgroup TIMEx_Private_Macros TIM Extended Private Macros 100 | * @{ 101 | */ 102 | #define IS_TIM_REMAP(__INSTANCE__, __REMAP__) \ 103 | (((__INSTANCE__) == TIM14) && (((__REMAP__) & 0xFFFFFFFCU) == 0x00000000U)) 104 | 105 | /** 106 | * @} 107 | */ 108 | /* End of private macro ------------------------------------------------------*/ 109 | 110 | /* Exported functions --------------------------------------------------------*/ 111 | /** @addtogroup TIMEx_Exported_Functions TIM Extended Exported Functions 112 | * @{ 113 | */ 114 | 115 | /** @addtogroup TIMEx_Exported_Functions_Group1 Extended Timer Hall Sensor functions 116 | * @brief Timer Hall Sensor functions 117 | * @{ 118 | */ 119 | /* Timer Hall Sensor functions **********************************************/ 120 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, TIM_HallSensor_InitTypeDef *sConfig); 121 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim); 122 | 123 | void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef *htim); 124 | void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim); 125 | 126 | /* Blocking mode: Polling */ 127 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim); 128 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim); 129 | /* Non-Blocking mode: Interrupt */ 130 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim); 131 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim); 132 | /* Non-Blocking mode: DMA */ 133 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length); 134 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim); 135 | /** 136 | * @} 137 | */ 138 | 139 | /** @addtogroup TIMEx_Exported_Functions_Group2 Extended Timer Complementary Output Compare functions 140 | * @brief Timer Complementary Output Compare functions 141 | * @{ 142 | */ 143 | /* Timer Complementary Output Compare functions *****************************/ 144 | /* Blocking mode: Polling */ 145 | HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel); 146 | HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel); 147 | 148 | /* Non-Blocking mode: Interrupt */ 149 | HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel); 150 | HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel); 151 | 152 | /* Non-Blocking mode: DMA */ 153 | HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length); 154 | HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel); 155 | /** 156 | * @} 157 | */ 158 | 159 | /** @addtogroup TIMEx_Exported_Functions_Group3 Extended Timer Complementary PWM functions 160 | * @brief Timer Complementary PWM functions 161 | * @{ 162 | */ 163 | /* Timer Complementary PWM functions ****************************************/ 164 | /* Blocking mode: Polling */ 165 | HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel); 166 | HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel); 167 | 168 | /* Non-Blocking mode: Interrupt */ 169 | HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel); 170 | HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel); 171 | /* Non-Blocking mode: DMA */ 172 | HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length); 173 | HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel); 174 | /** 175 | * @} 176 | */ 177 | 178 | /** @addtogroup TIMEx_Exported_Functions_Group4 Extended Timer Complementary One Pulse functions 179 | * @brief Timer Complementary One Pulse functions 180 | * @{ 181 | */ 182 | /* Timer Complementary One Pulse functions **********************************/ 183 | /* Blocking mode: Polling */ 184 | HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel); 185 | HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel); 186 | 187 | /* Non-Blocking mode: Interrupt */ 188 | HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel); 189 | HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel); 190 | /** 191 | * @} 192 | */ 193 | 194 | /** @addtogroup TIMEx_Exported_Functions_Group5 Extended Peripheral Control functions 195 | * @brief Peripheral Control functions 196 | * @{ 197 | */ 198 | /* Extended Control functions ************************************************/ 199 | HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger, 200 | uint32_t CommutationSource); 201 | HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger, 202 | uint32_t CommutationSource); 203 | HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger, 204 | uint32_t CommutationSource); 205 | HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim, 206 | TIM_MasterConfigTypeDef *sMasterConfig); 207 | HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim, 208 | TIM_BreakDeadTimeConfigTypeDef *sBreakDeadTimeConfig); 209 | HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap); 210 | /** 211 | * @} 212 | */ 213 | 214 | /** @addtogroup TIMEx_Exported_Functions_Group6 Extended Callbacks functions 215 | * @brief Extended Callbacks functions 216 | * @{ 217 | */ 218 | /* Extended Callback **********************************************************/ 219 | void HAL_TIMEx_CommutCallback(TIM_HandleTypeDef *htim); 220 | void HAL_TIMEx_CommutHalfCpltCallback(TIM_HandleTypeDef *htim); 221 | void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim); 222 | /** 223 | * @} 224 | */ 225 | 226 | /** @addtogroup TIMEx_Exported_Functions_Group7 Extended Peripheral State functions 227 | * @brief Extended Peripheral State functions 228 | * @{ 229 | */ 230 | /* Extended Peripheral State functions ***************************************/ 231 | HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(TIM_HandleTypeDef *htim); 232 | /** 233 | * @} 234 | */ 235 | 236 | /** 237 | * @} 238 | */ 239 | /* End of exported functions -------------------------------------------------*/ 240 | 241 | /* Private functions----------------------------------------------------------*/ 242 | /** @addtogroup TIMEx_Private_Functions TIMEx Private Functions 243 | * @{ 244 | */ 245 | void TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma); 246 | void TIMEx_DMACommutationHalfCplt(DMA_HandleTypeDef *hdma); 247 | /** 248 | * @} 249 | */ 250 | /* End of private functions --------------------------------------------------*/ 251 | 252 | /** 253 | * @} 254 | */ 255 | 256 | /** 257 | * @} 258 | */ 259 | 260 | #ifdef __cplusplus 261 | } 262 | #endif 263 | 264 | 265 | #endif /* STM32F0xx_HAL_TIM_EX_H */ 266 | 267 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 268 | -------------------------------------------------------------------------------- /Src/can.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2016 Hubert Denkmair 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #include "can.h" 28 | #include "config.h" 29 | 30 | void can_init(can_data_t *hcan, CAN_TypeDef *instance) 31 | { 32 | __HAL_RCC_CAN1_CLK_ENABLE(); 33 | 34 | GPIO_InitTypeDef itd; 35 | itd.Pin = GPIO_PIN_8|GPIO_PIN_9; 36 | itd.Mode = GPIO_MODE_AF_PP; 37 | itd.Pull = GPIO_NOPULL; 38 | itd.Speed = GPIO_SPEED_FREQ_HIGH; 39 | itd.Alternate = GPIO_AF4_CAN; 40 | HAL_GPIO_Init(GPIOB, &itd); 41 | 42 | hcan->instance = instance; 43 | hcan->brp = 6; 44 | hcan->phase_seg1 = 13; 45 | hcan->phase_seg2 = 2; 46 | hcan->sjw = 1; 47 | } 48 | 49 | bool can_set_bittiming(can_data_t *hcan, uint16_t brp, uint8_t phase_seg1, uint8_t phase_seg2, uint8_t sjw) 50 | { 51 | if ( (brp>0) && (brp<=1024) 52 | && (phase_seg1>0) && (phase_seg1<=16) 53 | && (phase_seg2>0) && (phase_seg2<=8) 54 | && (sjw>0) && (sjw<=4) 55 | ) { 56 | hcan->brp = brp & 0x3FF; 57 | hcan->phase_seg1 = phase_seg1; 58 | hcan->phase_seg2 = phase_seg2; 59 | hcan->sjw = sjw; 60 | return true; 61 | } else { 62 | return false; 63 | } 64 | } 65 | 66 | void can_enable(can_data_t *hcan, bool loop_back, bool listen_only, bool one_shot) 67 | { 68 | CAN_TypeDef *can = hcan->instance; 69 | 70 | uint32_t mcr = CAN_MCR_INRQ 71 | | CAN_MCR_ABOM 72 | | CAN_MCR_TXFP 73 | | (one_shot ? CAN_MCR_NART : 0); 74 | 75 | uint32_t btr = ((uint32_t)(hcan->sjw-1)) << 24 76 | | ((uint32_t)(hcan->phase_seg1-1)) << 16 77 | | ((uint32_t)(hcan->phase_seg2-1)) << 20 78 | | (hcan->brp - 1) 79 | | (loop_back ? CAN_MODE_LOOPBACK : 0) 80 | | (listen_only ? CAN_MODE_SILENT : 0); 81 | 82 | 83 | // Reset CAN peripheral 84 | can->MCR |= CAN_MCR_RESET; 85 | while((can->MCR & CAN_MCR_RESET) != 0); // reset bit is set to zero after reset 86 | while((can->MSR & CAN_MSR_SLAK) == 0); // should be in sleep mode after reset 87 | 88 | can->MCR |= CAN_MCR_INRQ ; 89 | while((can->MSR & CAN_MSR_INAK) == 0); 90 | 91 | can->MCR = mcr; 92 | can->BTR = btr; 93 | 94 | can->MCR &= ~CAN_MCR_INRQ; 95 | while((can->MSR & CAN_MSR_INAK) != 0); 96 | 97 | uint32_t filter_bit = 0x00000001; 98 | can->FMR |= CAN_FMR_FINIT; 99 | can->FMR &= ~CAN_FMR_CAN2SB; 100 | can->FA1R &= ~filter_bit; // disable filter 101 | can->FS1R |= filter_bit; // set to single 32-bit filter mode 102 | can->FM1R &= ~filter_bit; // set filter mask mode for filter 0 103 | can->sFilterRegister[0].FR1 = 0; // filter ID = 0 104 | can->sFilterRegister[0].FR2 = 0; // filter Mask = 0 105 | can->FFA1R &= ~filter_bit; // assign filter 0 to FIFO 0 106 | can->FA1R |= filter_bit; // enable filter 107 | can->FMR &= ~CAN_FMR_FINIT; 108 | 109 | #ifdef nCANSTBY_Pin 110 | HAL_GPIO_WritePin(nCANSTBY_Port, nCANSTBY_Pin, GPIO_PIN_SET); 111 | #endif 112 | } 113 | 114 | void can_disable(can_data_t *hcan) 115 | { 116 | CAN_TypeDef *can = hcan->instance; 117 | #ifdef nCANSTBY_Pin 118 | HAL_GPIO_WritePin(nCANSTBY_Port, nCANSTBY_Pin, GPIO_PIN_RESET); 119 | #endif 120 | can->MCR |= CAN_MCR_INRQ ; // send can controller into initialization mode 121 | } 122 | 123 | bool can_is_enabled(can_data_t *hcan) 124 | { 125 | CAN_TypeDef *can = hcan->instance; 126 | return (can->MCR & CAN_MCR_INRQ) == 0; 127 | } 128 | 129 | bool can_is_rx_pending(can_data_t *hcan) 130 | { 131 | CAN_TypeDef *can = hcan->instance; 132 | return ((can->RF0R & CAN_RF0R_FMP0) != 0); 133 | } 134 | 135 | bool can_receive(can_data_t *hcan, struct gs_host_frame *rx_frame) 136 | { 137 | CAN_TypeDef *can = hcan->instance; 138 | 139 | if (can_is_rx_pending(hcan)) { 140 | CAN_FIFOMailBox_TypeDef *fifo = &can->sFIFOMailBox[0]; 141 | 142 | if (fifo->RIR & CAN_RI0R_IDE) { 143 | rx_frame->can_id = CAN_EFF_FLAG | ((fifo->RIR >> 3) & 0x1FFFFFFF); 144 | } else { 145 | rx_frame->can_id = (fifo->RIR >> 21) & 0x7FF; 146 | } 147 | 148 | if (fifo->RIR & CAN_RI0R_RTR) { 149 | rx_frame->can_id |= CAN_RTR_FLAG; 150 | } 151 | 152 | rx_frame->can_dlc = fifo->RDTR & CAN_RDT0R_DLC; 153 | 154 | rx_frame->data[0] = (fifo->RDLR >> 0) & 0xFF; 155 | rx_frame->data[1] = (fifo->RDLR >> 8) & 0xFF; 156 | rx_frame->data[2] = (fifo->RDLR >> 16) & 0xFF; 157 | rx_frame->data[3] = (fifo->RDLR >> 24) & 0xFF; 158 | rx_frame->data[4] = (fifo->RDHR >> 0) & 0xFF; 159 | rx_frame->data[5] = (fifo->RDHR >> 8) & 0xFF; 160 | rx_frame->data[6] = (fifo->RDHR >> 16) & 0xFF; 161 | rx_frame->data[7] = (fifo->RDHR >> 24) & 0xFF; 162 | 163 | can->RF0R |= CAN_RF0R_RFOM0; // release FIFO 164 | 165 | return true; 166 | } else { 167 | return false; 168 | } 169 | } 170 | 171 | static CAN_TxMailBox_TypeDef *can_find_free_mailbox(can_data_t *hcan) 172 | { 173 | CAN_TypeDef *can = hcan->instance; 174 | 175 | uint32_t tsr = can->TSR; 176 | if ( tsr & CAN_TSR_TME0 ) { 177 | return &can->sTxMailBox[0]; 178 | } else if ( tsr & CAN_TSR_TME1 ) { 179 | return &can->sTxMailBox[1]; 180 | } else if ( tsr & CAN_TSR_TME2 ) { 181 | return &can->sTxMailBox[2]; 182 | } else { 183 | return 0; 184 | } 185 | } 186 | 187 | bool can_send(can_data_t *hcan, struct gs_host_frame *frame) 188 | { 189 | CAN_TxMailBox_TypeDef *mb = can_find_free_mailbox(hcan); 190 | if (mb != 0) { 191 | 192 | /* first, clear transmission request */ 193 | mb->TIR &= CAN_TI0R_TXRQ; 194 | 195 | if (frame->can_id & CAN_EFF_FLAG) { // extended id 196 | mb->TIR = CAN_ID_EXT | (frame->can_id & 0x1FFFFFFF) << 3; 197 | } else { 198 | mb->TIR = (frame->can_id & 0x7FF) << 21; 199 | } 200 | 201 | if (frame->can_id & CAN_RTR_FLAG) { 202 | mb->TIR |= CAN_RTR_REMOTE; 203 | } 204 | 205 | mb->TDTR &= 0xFFFFFFF0; 206 | mb->TDTR |= frame->can_dlc & 0x0F; 207 | 208 | mb->TDLR = 209 | ( frame->data[3] << 24 ) 210 | | ( frame->data[2] << 16 ) 211 | | ( frame->data[1] << 8 ) 212 | | ( frame->data[0] << 0 ); 213 | 214 | mb->TDHR = 215 | ( frame->data[7] << 24 ) 216 | | ( frame->data[6] << 16 ) 217 | | ( frame->data[5] << 8 ) 218 | | ( frame->data[4] << 0 ); 219 | 220 | /* request transmission */ 221 | mb->TIR |= CAN_TI0R_TXRQ; 222 | 223 | return true; 224 | } else { 225 | return false; 226 | } 227 | } 228 | 229 | uint32_t can_get_error_status(can_data_t *hcan) 230 | { 231 | CAN_TypeDef *can = hcan->instance; 232 | return can->ESR; 233 | } 234 | 235 | static bool status_is_active(uint32_t err) 236 | { 237 | return !(err & (CAN_ESR_BOFF | CAN_ESR_EPVF)); 238 | } 239 | 240 | bool can_parse_error_status(uint32_t err, uint32_t last_err, can_data_t *hcan, struct gs_host_frame *frame) 241 | { 242 | /* We build up the detailed error information at the same time as we decide 243 | * whether there's anything worth sending. This variable tracks that final 244 | * result. */ 245 | bool should_send = false; 246 | 247 | frame->echo_id = 0xFFFFFFFF; 248 | frame->can_id = CAN_ERR_FLAG | CAN_ERR_CRTL; 249 | frame->can_dlc = CAN_ERR_DLC; 250 | frame->data[0] = CAN_ERR_LOSTARB_UNSPEC; 251 | frame->data[1] = CAN_ERR_CRTL_UNSPEC; 252 | frame->data[2] = CAN_ERR_PROT_UNSPEC; 253 | frame->data[3] = CAN_ERR_PROT_LOC_UNSPEC; 254 | frame->data[4] = CAN_ERR_TRX_UNSPEC; 255 | frame->data[5] = 0; 256 | frame->data[6] = 0; 257 | frame->data[7] = 0; 258 | 259 | if (err & CAN_ESR_BOFF) { 260 | frame->can_id |= CAN_ERR_BUSOFF; 261 | if (!(last_err & CAN_ESR_BOFF)) { 262 | /* We transitioned to bus-off. */ 263 | should_send = true; 264 | } 265 | } else if (last_err & CAN_ESR_BOFF) { 266 | /* We transitioned out of bus-off. */ 267 | should_send = true; 268 | } 269 | 270 | /* We transitioned from passive/bus-off to active, so report the edge. */ 271 | if (!status_is_active(last_err) && status_is_active(err)) { 272 | frame->data[1] |= CAN_ERR_CRTL_ACTIVE; 273 | should_send = true; 274 | } 275 | 276 | uint8_t tx_error_cnt = (err>>16) & 0xFF; 277 | uint8_t rx_error_cnt = (err>>24) & 0xFF; 278 | /* The Linux sja1000 driver puts these counters here. Seems like as good a 279 | * place as any. */ 280 | frame->data[6] = tx_error_cnt; 281 | frame->data[7] = rx_error_cnt; 282 | 283 | uint8_t last_tx_error_cnt = (last_err>>16) & 0xFF; 284 | uint8_t last_rx_error_cnt = (last_err>>24) & 0xFF; 285 | /* If either error counter transitioned to/from 0. */ 286 | if ((tx_error_cnt == 0) != (last_tx_error_cnt == 0)) { 287 | should_send = true; 288 | } 289 | if ((rx_error_cnt == 0) != (last_rx_error_cnt == 0)) { 290 | should_send = true; 291 | } 292 | /* If either error counter increased by 15. */ 293 | if (((int)last_tx_error_cnt + CAN_ERRCOUNT_THRESHOLD) < tx_error_cnt) { 294 | should_send = true; 295 | } 296 | if (((int)last_rx_error_cnt + CAN_ERRCOUNT_THRESHOLD) < rx_error_cnt) { 297 | should_send = true; 298 | } 299 | 300 | if (err & CAN_ESR_EPVF) { 301 | frame->data[1] |= CAN_ERR_CRTL_RX_PASSIVE | CAN_ERR_CRTL_TX_PASSIVE; 302 | if (!(last_err & CAN_ESR_EPVF)) { 303 | should_send = true; 304 | } 305 | } else if (err & CAN_ESR_EWGF) { 306 | frame->data[1] |= CAN_ERR_CRTL_RX_WARNING | CAN_ERR_CRTL_TX_WARNING; 307 | if (!(last_err & CAN_ESR_EWGF)) { 308 | should_send = true; 309 | } 310 | } else if (last_err & (CAN_ESR_EPVF | CAN_ESR_EWGF)) { 311 | should_send = true; 312 | } 313 | 314 | uint8_t lec = (err>>4) & 0x07; 315 | switch (lec) { 316 | case 0x01: /* stuff error */ 317 | frame->can_id |= CAN_ERR_PROT; 318 | frame->data[2] |= CAN_ERR_PROT_STUFF; 319 | should_send = true; 320 | break; 321 | case 0x02: /* form error */ 322 | frame->can_id |= CAN_ERR_PROT; 323 | frame->data[2] |= CAN_ERR_PROT_FORM; 324 | should_send = true; 325 | break; 326 | case 0x03: /* ack error */ 327 | frame->can_id |= CAN_ERR_ACK; 328 | should_send = true; 329 | break; 330 | case 0x04: /* bit recessive error */ 331 | frame->can_id |= CAN_ERR_PROT; 332 | frame->data[2] |= CAN_ERR_PROT_BIT1; 333 | should_send = true; 334 | break; 335 | case 0x05: /* bit dominant error */ 336 | frame->can_id |= CAN_ERR_PROT; 337 | frame->data[2] |= CAN_ERR_PROT_BIT0; 338 | should_send = true; 339 | break; 340 | case 0x06: /* CRC error */ 341 | frame->can_id |= CAN_ERR_PROT; 342 | frame->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ; 343 | should_send = true; 344 | break; 345 | default: /* 0=no error, 7=no change */ 346 | break; 347 | } 348 | 349 | CAN_TypeDef *can = hcan->instance; 350 | /* Write 7 to LEC so we know if it gets set to the same thing again */ 351 | can->ESR = 7<<4; 352 | 353 | return should_send; 354 | } 355 | -------------------------------------------------------------------------------- /libs/STM32_HAL/include/stm32f0xx/stm32f0xx_hal_flash.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f0xx_hal_flash.h 4 | * @author MCD Application Team 5 | * @brief Header file of Flash HAL module. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2016 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __STM32F0xx_HAL_FLASH_H 22 | #define __STM32F0xx_HAL_FLASH_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm32f0xx_hal_def.h" 30 | 31 | /** @addtogroup STM32F0xx_HAL_Driver 32 | * @{ 33 | */ 34 | 35 | /** @addtogroup FLASH 36 | * @{ 37 | */ 38 | 39 | /** @addtogroup FLASH_Private_Constants 40 | * @{ 41 | */ 42 | #define FLASH_TIMEOUT_VALUE (50000U) /* 50 s */ 43 | /** 44 | * @} 45 | */ 46 | 47 | /** @addtogroup FLASH_Private_Macros 48 | * @{ 49 | */ 50 | 51 | #define IS_FLASH_TYPEPROGRAM(VALUE) (((VALUE) == FLASH_TYPEPROGRAM_HALFWORD) || \ 52 | ((VALUE) == FLASH_TYPEPROGRAM_WORD) || \ 53 | ((VALUE) == FLASH_TYPEPROGRAM_DOUBLEWORD)) 54 | 55 | #define IS_FLASH_LATENCY(__LATENCY__) (((__LATENCY__) == FLASH_LATENCY_0) || \ 56 | ((__LATENCY__) == FLASH_LATENCY_1)) 57 | 58 | /** 59 | * @} 60 | */ 61 | 62 | /* Exported types ------------------------------------------------------------*/ 63 | /** @defgroup FLASH_Exported_Types FLASH Exported Types 64 | * @{ 65 | */ 66 | 67 | /** 68 | * @brief FLASH Procedure structure definition 69 | */ 70 | typedef enum 71 | { 72 | FLASH_PROC_NONE = 0U, 73 | FLASH_PROC_PAGEERASE = 1U, 74 | FLASH_PROC_MASSERASE = 2U, 75 | FLASH_PROC_PROGRAMHALFWORD = 3U, 76 | FLASH_PROC_PROGRAMWORD = 4U, 77 | FLASH_PROC_PROGRAMDOUBLEWORD = 5U 78 | } FLASH_ProcedureTypeDef; 79 | 80 | /** 81 | * @brief FLASH handle Structure definition 82 | */ 83 | typedef struct 84 | { 85 | __IO FLASH_ProcedureTypeDef ProcedureOnGoing; /*!< Internal variable to indicate which procedure is ongoing or not in IT context */ 86 | 87 | __IO uint32_t DataRemaining; /*!< Internal variable to save the remaining pages to erase or half-word to program in IT context */ 88 | 89 | __IO uint32_t Address; /*!< Internal variable to save address selected for program or erase */ 90 | 91 | __IO uint64_t Data; /*!< Internal variable to save data to be programmed */ 92 | 93 | HAL_LockTypeDef Lock; /*!< FLASH locking object */ 94 | 95 | __IO uint32_t ErrorCode; /*!< FLASH error code 96 | This parameter can be a value of @ref FLASH_Error_Codes */ 97 | } FLASH_ProcessTypeDef; 98 | 99 | /** 100 | * @} 101 | */ 102 | 103 | /* Exported constants --------------------------------------------------------*/ 104 | /** @defgroup FLASH_Exported_Constants FLASH Exported Constants 105 | * @{ 106 | */ 107 | 108 | /** @defgroup FLASH_Error_Codes FLASH Error Codes 109 | * @{ 110 | */ 111 | 112 | #define HAL_FLASH_ERROR_NONE 0x00U /*!< No error */ 113 | #define HAL_FLASH_ERROR_PROG 0x01U /*!< Programming error */ 114 | #define HAL_FLASH_ERROR_WRP 0x02U /*!< Write protection error */ 115 | 116 | /** 117 | * @} 118 | */ 119 | 120 | /** @defgroup FLASH_Type_Program FLASH Type Program 121 | * @{ 122 | */ 123 | #define FLASH_TYPEPROGRAM_HALFWORD (0x01U) /*!ACR = (FLASH->ACR&(~FLASH_ACR_LATENCY)) | (__LATENCY__)) 186 | 187 | 188 | /** 189 | * @brief Get the FLASH Latency. 190 | * @retval FLASH Latency 191 | * The value of this parameter depend on device used within the same series 192 | */ 193 | #define __HAL_FLASH_GET_LATENCY() (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY)) 194 | 195 | /** 196 | * @} 197 | */ 198 | 199 | /** @defgroup FLASH_Prefetch FLASH Prefetch 200 | * @brief macros to handle FLASH Prefetch buffer 201 | * @{ 202 | */ 203 | /** 204 | * @brief Enable the FLASH prefetch buffer. 205 | * @retval None 206 | */ 207 | #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE() (FLASH->ACR |= FLASH_ACR_PRFTBE) 208 | 209 | /** 210 | * @brief Disable the FLASH prefetch buffer. 211 | * @retval None 212 | */ 213 | #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE() (FLASH->ACR &= (~FLASH_ACR_PRFTBE)) 214 | 215 | /** 216 | * @} 217 | */ 218 | 219 | /** @defgroup FLASH_Interrupt FLASH Interrupts 220 | * @brief macros to handle FLASH interrupts 221 | * @{ 222 | */ 223 | 224 | /** 225 | * @brief Enable the specified FLASH interrupt. 226 | * @param __INTERRUPT__ FLASH interrupt 227 | * This parameter can be any combination of the following values: 228 | * @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt 229 | * @arg @ref FLASH_IT_ERR Error Interrupt 230 | * @retval none 231 | */ 232 | #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__) SET_BIT((FLASH->CR), (__INTERRUPT__)) 233 | 234 | /** 235 | * @brief Disable the specified FLASH interrupt. 236 | * @param __INTERRUPT__ FLASH interrupt 237 | * This parameter can be any combination of the following values: 238 | * @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt 239 | * @arg @ref FLASH_IT_ERR Error Interrupt 240 | * @retval none 241 | */ 242 | #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__) CLEAR_BIT((FLASH->CR), (uint32_t)(__INTERRUPT__)) 243 | 244 | /** 245 | * @brief Get the specified FLASH flag status. 246 | * @param __FLAG__ specifies the FLASH flag to check. 247 | * This parameter can be one of the following values: 248 | * @arg @ref FLASH_FLAG_BSY FLASH Busy flag 249 | * @arg @ref FLASH_FLAG_EOP FLASH End of Operation flag 250 | * @arg @ref FLASH_FLAG_WRPERR FLASH Write protected error flag 251 | * @arg @ref FLASH_FLAG_PGERR FLASH Programming error flag 252 | * @retval The new state of __FLAG__ (SET or RESET). 253 | */ 254 | #define __HAL_FLASH_GET_FLAG(__FLAG__) (((FLASH->SR) & (__FLAG__)) == (__FLAG__)) 255 | 256 | /** 257 | * @brief Clear the specified FLASH flag. 258 | * @param __FLAG__ specifies the FLASH flags to clear. 259 | * This parameter can be any combination of the following values: 260 | * @arg @ref FLASH_FLAG_EOP FLASH End of Operation flag 261 | * @arg @ref FLASH_FLAG_WRPERR FLASH Write protected error flag 262 | * @arg @ref FLASH_FLAG_PGERR FLASH Programming error flag 263 | * @retval none 264 | */ 265 | #define __HAL_FLASH_CLEAR_FLAG(__FLAG__) ((FLASH->SR) = (__FLAG__)) 266 | 267 | /** 268 | * @} 269 | */ 270 | 271 | /** 272 | * @} 273 | */ 274 | 275 | /* Include FLASH HAL Extended module */ 276 | #include "stm32f0xx_hal_flash_ex.h" 277 | 278 | /* Exported functions --------------------------------------------------------*/ 279 | /** @addtogroup FLASH_Exported_Functions 280 | * @{ 281 | */ 282 | 283 | /** @addtogroup FLASH_Exported_Functions_Group1 284 | * @{ 285 | */ 286 | /* IO operation functions *****************************************************/ 287 | HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data); 288 | HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data); 289 | 290 | /* FLASH IRQ handler function */ 291 | void HAL_FLASH_IRQHandler(void); 292 | /* Callbacks in non blocking modes */ 293 | void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue); 294 | void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue); 295 | 296 | /** 297 | * @} 298 | */ 299 | 300 | /** @addtogroup FLASH_Exported_Functions_Group2 301 | * @{ 302 | */ 303 | /* Peripheral Control functions ***********************************************/ 304 | HAL_StatusTypeDef HAL_FLASH_Unlock(void); 305 | HAL_StatusTypeDef HAL_FLASH_Lock(void); 306 | HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void); 307 | HAL_StatusTypeDef HAL_FLASH_OB_Lock(void); 308 | HAL_StatusTypeDef HAL_FLASH_OB_Launch(void); 309 | 310 | /** 311 | * @} 312 | */ 313 | 314 | /** @addtogroup FLASH_Exported_Functions_Group3 315 | * @{ 316 | */ 317 | /* Peripheral State and Error functions ***************************************/ 318 | uint32_t HAL_FLASH_GetError(void); 319 | 320 | /** 321 | * @} 322 | */ 323 | 324 | /** 325 | * @} 326 | */ 327 | 328 | /* Private function -------------------------------------------------*/ 329 | /** @addtogroup FLASH_Private_Functions 330 | * @{ 331 | */ 332 | HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout); 333 | 334 | /** 335 | * @} 336 | */ 337 | 338 | /** 339 | * @} 340 | */ 341 | 342 | /** 343 | * @} 344 | */ 345 | 346 | #ifdef __cplusplus 347 | } 348 | #endif 349 | 350 | #endif /* __STM32F0xx_HAL_FLASH_H */ 351 | 352 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 353 | 354 | --------------------------------------------------------------------------------