├── openocd ├── jlink.cfg └── stm32f1x.cfg ├── tiny-AES-c ├── origin.txt ├── unlicense.txt ├── aes.h └── README.md ├── keys ├── iv.bin ├── key.bin └── keygen.sh ├── stm ├── stm32f10x.h ├── STM32F10x_StdPeriph_Driver │ ├── src │ │ ├── stm32f10x_i2c.c │ │ ├── stm32f10x_flash.c │ │ ├── stm32f10x_usart.c │ │ ├── stm32f10x_crc.c │ │ ├── stm32f10x_iwdg.c │ │ ├── stm32f10x_dbgmcu.c │ │ ├── stm32f10x_wwdg.c │ │ ├── misc.c │ │ └── stm32f10x_exti.c │ └── inc │ │ ├── stm32f10x_crc.h │ │ ├── stm32f10x_wwdg.h │ │ ├── stm32f10x_dbgmcu.h │ │ ├── stm32f10x_iwdg.h │ │ ├── stm32f10x_rtc.h │ │ ├── stm32f10x_pwr.h │ │ ├── stm32f10x_cec.h │ │ ├── stm32f10x_exti.h │ │ └── stm32f10x_bkp.h ├── system_stm32f10x.h └── STM32_USB-FS-Device_Driver │ ├── inc │ ├── usb_int.h │ ├── usb_mem.h │ ├── usb_type.h │ ├── usb_sil.h │ ├── usb_lib.h │ ├── usb_init.h │ └── usb_def.h │ └── src │ ├── usb_init.c │ ├── usb_sil.c │ ├── usb_mem.c │ └── usb_int.c ├── linker └── stm32_flash.ld ├── test_app ├── stm32_app.ld ├── README.md ├── stm32f10x_conf.h ├── Makefile └── main.c ├── .gitignore ├── FreeRTOS-Plus-CLI ├── ReadMe.url ├── readme.txt ├── LICENSE_INFORMATION.txt ├── History.txt └── FreeRTOS_CLI.h ├── src ├── stm_hex.h ├── usb │ ├── hw_config.h │ ├── usb_conf.h │ └── hw_config.c ├── cli.h ├── bl_data.h ├── bl_data.c ├── stm_hex.c └── main.c ├── config ├── stubs │ ├── task.h │ └── FreeRTOS.h ├── bl_conf.h └── stm32f10x_conf.h ├── hex_parser ├── hex_parser.h └── hex_parser.c ├── LICENSE ├── .github └── workflows │ └── make_binaries.yml ├── encrypter ├── Makefile └── main.c └── README.md /openocd/jlink.cfg: -------------------------------------------------------------------------------- 1 | adapter driver jlink 2 | adapter speed 12000 3 | transport select swd 4 | -------------------------------------------------------------------------------- /tiny-AES-c/origin.txt: -------------------------------------------------------------------------------- 1 | Original source location: 2 | https://github.com/kokke/tiny-AES-c 3 | -------------------------------------------------------------------------------- /keys/iv.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andriy-bilynskyy/STM32-AES-Bootloader/HEAD/keys/iv.bin -------------------------------------------------------------------------------- /keys/key.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andriy-bilynskyy/STM32-AES-Bootloader/HEAD/keys/key.bin -------------------------------------------------------------------------------- /stm/stm32f10x.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andriy-bilynskyy/STM32-AES-Bootloader/HEAD/stm/stm32f10x.h -------------------------------------------------------------------------------- /linker/stm32_flash.ld: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andriy-bilynskyy/STM32-AES-Bootloader/HEAD/linker/stm32_flash.ld -------------------------------------------------------------------------------- /test_app/stm32_app.ld: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andriy-bilynskyy/STM32-AES-Bootloader/HEAD/test_app/stm32_app.ld -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/*.o 2 | **/*.O 3 | **/*.d 4 | **/*.elf 5 | **/*.hex 6 | **/*.map 7 | **/*.enc 8 | encrypter/hex-encrypter 9 | -------------------------------------------------------------------------------- /FreeRTOS-Plus-CLI/ReadMe.url: -------------------------------------------------------------------------------- 1 | [InternetShortcut] 2 | URL=http://www.freertos.org/cli 3 | IDList= 4 | [{000214A0-0000-0000-C000-000000000046}] 5 | Prop3=19,2 6 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/src/stm32f10x_i2c.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andriy-bilynskyy/STM32-AES-Bootloader/HEAD/stm/STM32F10x_StdPeriph_Driver/src/stm32f10x_i2c.c -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/src/stm32f10x_flash.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andriy-bilynskyy/STM32-AES-Bootloader/HEAD/stm/STM32F10x_StdPeriph_Driver/src/stm32f10x_flash.c -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/src/stm32f10x_usart.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andriy-bilynskyy/STM32-AES-Bootloader/HEAD/stm/STM32F10x_StdPeriph_Driver/src/stm32f10x_usart.c -------------------------------------------------------------------------------- /FreeRTOS-Plus-CLI/readme.txt: -------------------------------------------------------------------------------- 1 | Contains source and header files that implement FreeRTOS+CLI. See 2 | http://www.FreeRTOS.org/cli for documentation and license information. 3 | 4 | -------------------------------------------------------------------------------- /keys/keygen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #**************************************************************************** 3 | #* 4 | #* (C) 2020 Andrii Bilynskyi 5 | #* 6 | #* This code is licensed under the MIT. 7 | #* 8 | #**************************************************************************** 9 | 10 | dd if=/dev/urandom of=iv.bin bs=1 count=16 11 | dd if=/dev/urandom of=key.bin bs=1 count=16 12 | -------------------------------------------------------------------------------- /src/stm_hex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ***************************************************************************** 3 | ** 4 | ** (C) 2023 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ***************************************************************************** 9 | */ 10 | 11 | #ifndef __STM_HEX_H 12 | #define __STM_HEX_H 13 | 14 | #include 15 | #include 16 | 17 | 18 | typedef bool (*stm_hex_on_word_t)(uint32_t addr, uint16_t data); 19 | 20 | 21 | bool stm_hex_feed(const char *str); 22 | void stm_hex_set_callback(stm_hex_on_word_t on_word); 23 | 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /config/stubs/task.h: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************** 3 | ** 4 | ** (C) 2023 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ******************************************************************************** 9 | */ 10 | 11 | 12 | #ifndef __FREE_RTOS_TASK_STUB_H 13 | #define __FREE_RTOS_TASK_STUB_H 14 | 15 | 16 | #include 17 | #include 18 | 19 | 20 | #define configASSERT(expr) assert_param(expr) 21 | #define pvPortMalloc(size) malloc(size) 22 | #define taskENTER_CRITICAL() 23 | #define taskEXIT_CRITICAL() 24 | 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /hex_parser/hex_parser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ***************************************************************************** 3 | ** 4 | ** (C) 2023 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ***************************************************************************** 9 | */ 10 | 11 | #ifndef __HEX_PARSER_H 12 | #define __HEX_PARSER_H 13 | 14 | #include 15 | #include 16 | 17 | 18 | typedef bool (*hex_parser_on_data_t)(uint32_t addr, uint8_t data[], uint16_t len); 19 | 20 | 21 | bool hex_parser_feed(char *str, bool data_upd); 22 | void hex_parser_set_callback(hex_parser_on_data_t on_data); 23 | 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/usb/hw_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************** 3 | ** 4 | ** (C) 2023 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ******************************************************************************** 9 | */ 10 | 11 | #ifndef __HW_CONFIG_H 12 | #define __HW_CONFIG_H 13 | 14 | 15 | #include 16 | 17 | void usb_cdc_start(void); 18 | void usb_cdc_stop(void); 19 | uint8_t usb_cdc_is_ready(void); 20 | uint8_t usb_cdc_rx_avail(void); 21 | int usb_cdc_read(uint8_t data[], uint16_t len); 22 | int usb_cdc_write(const uint8_t data[], uint16_t len); 23 | char usb_cdc_get_char(void); 24 | void usb_cdc_put_char(char ch); 25 | void usb_cdc_put_string(const char *str); 26 | 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /config/stubs/FreeRTOS.h: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************** 3 | ** 4 | ** (C) 2020 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ******************************************************************************** 9 | */ 10 | 11 | 12 | #ifndef __FREE_RTOS_STUB_H 13 | #define __FREE_RTOS_STUB_H 14 | 15 | 16 | #include 17 | 18 | 19 | /* CLI output buffer size */ 20 | #define configCOMMAND_INT_MAX_OUTPUT_SIZE (523) 21 | 22 | 23 | /* type definitions to compile freeRTOS CLI */ 24 | typedef long BaseType_t; 25 | typedef unsigned long UBaseType_t; 26 | 27 | #define pdFALSE ( ( BaseType_t ) 0 ) 28 | #define pdTRUE ( ( BaseType_t ) 1 ) 29 | #define pdPASS ( pdTRUE ) 30 | #define pdFAIL ( pdFALSE ) 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /src/cli.h: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************** 3 | ** 4 | ** (C) 2023 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ******************************************************************************** 9 | */ 10 | 11 | #ifndef __CLI_H 12 | #define __CLI_H 13 | 14 | 15 | #include 16 | #include 17 | 18 | 19 | void cli_init(void); 20 | uint8_t cli_process(const char *input, char *output, uint32_t output_size); 21 | uint32_t app_adderess(void); 22 | 23 | #if defined(LED_PORT) && defined(LED_PIN) 24 | void led_init(void); 25 | void led_deinit(void); 26 | void led_state(uint8_t on); 27 | 28 | #define LED_INIT() led_init() 29 | #define LED_DEINIT() led_deinit() 30 | #define LED_STATE(on) led_state(on) 31 | #else 32 | #define LED_INIT() 33 | #define LED_DEINIT() 34 | #define LED_STATE(on) 35 | #endif 36 | 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /config/bl_conf.h: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************** 3 | ** 4 | ** (C) 2020 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ******************************************************************************** 9 | */ 10 | 11 | #ifndef __BL_CONF_H 12 | #define __BL_CONF_H 13 | 14 | /* bootloader jumper BL/app# */ 15 | #define BL_PIN GPIO_Pin_2 16 | #define BL_PORT GPIOB 17 | 18 | /* bootloader LED */ 19 | #define LED_PIN GPIO_Pin_13 20 | #define LED_PORT GPIOC 21 | /* control LED from CLI */ 22 | //#define LED_CLI_CTL 23 | 24 | 25 | #ifdef DEBUG 26 | #include 27 | 28 | extern void initialise_monitor_handles(void); 29 | 30 | #define DBG_INIT() initialise_monitor_handles() 31 | #define DBG_OUT(fmt, ...) \ 32 | printf("%s:%04u " fmt "\n", __FILE__, (unsigned)__LINE__, ##__VA_ARGS__) 33 | #else 34 | #define DBG_INIT() 35 | #define DBG_OUT(fmt, ...) 36 | #endif 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /test_app/README.md: -------------------------------------------------------------------------------- 1 | # Bootloader test application 2 | 3 | ## Just a stupid application to demonstrate bootloader functionality 4 | 5 | The application is blinking LED using about 1S period. 6 | 7 | ## Important! 8 | 9 | Bootloader application during compilation estimates its size. 10 | So application flash section is dynamically calculated. 11 | *If you made any changes in bootloader execute bootloader and issue command **info** in bootloader console.* 12 | 13 | info 14 | Boot Loader: 0x8000000 - 0x8003a03 (14852 bytes) 15 | Application: 0x8003c00 - 0x800ffff (50176 bytes) 16 | Page size: 1024 bytes 17 | 18 | *It shows the application address and its size. In sample **Application: 0x8003c00 - 0x800ffff (50176 bytes)** 19 | Based on this data application linker script should be updated. So open file **stm32_app.ld** and change FLASH area according to this data.* 20 | 21 | FLASH (rx) : ORIGIN = 0x08003c00, LENGTH = 49K 22 | 23 | Actually its only one difference from usual (without bootloader) STM32 application. :( Some FLASH space is reserved by bootloader. 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Andrii Bilynskyi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/bl_data.h: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************** 3 | ** 4 | ** (C) 2020 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ******************************************************************************** 9 | */ 10 | 11 | #ifndef __BL_DATA_H 12 | #define __BL_DATA_H 13 | 14 | 15 | #include 16 | #include 17 | 18 | 19 | /* ST store MCU flash size in KB at this address */ 20 | #define FLASH_SIZE_ADDR (0x1FFFF7E0) 21 | #define FLASH_TOTAL_SIZE ((uint32_t)(*(const uint16_t *)FLASH_SIZE_ADDR) << 10) 22 | #if !defined (STM32F10X_LD) || defined (STM32F10X_MD) 23 | #define FLASH_PAGE_SIZE (1024) 24 | #elif defined (STM32F10X_HD) || defined (STM32F10X_XL) || defined (STM32F10X_CL) 25 | #define FLASH_PAGE_SIZE (2048) 26 | #else 27 | #error Unknown STM device line. 28 | #endif 29 | 30 | uint32_t bl_data_get_first_addr(void); 31 | uint32_t bl_data_get_last_addr(void); 32 | uint32_t bl_data_get_app_first_addr(void); 33 | uint32_t bl_data_get_app_last_addr(void); 34 | bool bl_data_is_page_clean(uint32_t addr); 35 | 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /FreeRTOS-Plus-CLI/LICENSE_INFORMATION.txt: -------------------------------------------------------------------------------- 1 | FreeRTOS+CLI is released under the following MIT license. 2 | 3 | Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | this software and associated documentation files (the "Software"), to deal in 6 | the Software without restriction, including without limitation the rights to 7 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | the Software, and to permit persons to whom the Software is furnished to do so, 9 | subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /tiny-AES-c/unlicense.txt: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /src/bl_data.c: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************** 3 | ** 4 | ** (C) 2023 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ******************************************************************************** 9 | */ 10 | 11 | #include "bl_data.h" 12 | #include 13 | 14 | 15 | extern char _sidata; 16 | extern char _edata; 17 | extern char _sdata; 18 | 19 | 20 | uint32_t bl_data_get_first_addr(void) 21 | { 22 | return FLASH_BASE; 23 | } 24 | 25 | uint32_t bl_data_get_last_addr(void) 26 | { 27 | return (uint32_t)&_sidata + ((uint32_t)&_edata - (uint32_t)&_sdata) - 1; 28 | } 29 | 30 | uint32_t bl_data_get_app_first_addr(void) 31 | { 32 | return ((bl_data_get_last_addr() + FLASH_PAGE_SIZE) / FLASH_PAGE_SIZE) * FLASH_PAGE_SIZE; 33 | } 34 | 35 | uint32_t bl_data_get_app_last_addr(void) 36 | { 37 | return FLASH_BASE + FLASH_TOTAL_SIZE - 1; 38 | } 39 | 40 | bool bl_data_is_page_clean(uint32_t addr) 41 | { 42 | bool result = true; 43 | uint32_t saddr = (addr / FLASH_PAGE_SIZE) * FLASH_PAGE_SIZE; 44 | uint32_t eaddr = saddr + FLASH_PAGE_SIZE - 1; 45 | for (uint32_t i = saddr; i <= eaddr; i += sizeof(uint32_t)) { 46 | if (*(const uint32_t *)i != 0xFFFFFFFF) { 47 | result = false; 48 | break; 49 | } 50 | } 51 | return result; 52 | } 53 | -------------------------------------------------------------------------------- /.github/workflows/make_binaries.yml: -------------------------------------------------------------------------------- 1 | name: Make binaries 2 | 3 | on: workflow_dispatch 4 | 5 | jobs: 6 | bootloader_mk_bin: 7 | runs-on: ubuntu-latest 8 | name: Make binaries 9 | steps: 10 | 11 | - name: Install toolchain 12 | run: | 13 | mkdir ~/arm_toolchain 14 | wget -q -P ~/arm_toolchain https://developer.arm.com/-/media/Files/downloads/gnu/12.2.mpacbti-rel1/binrel/arm-gnu-toolchain-12.2.mpacbti-rel1-x86_64-arm-none-eabi.tar.xz 15 | sudo tar xf ~/arm_toolchain/arm-gnu-toolchain-12.2.mpacbti-rel1-x86_64-arm-none-eabi.tar.xz -C /opt 16 | rm -rf ~/arm_toolchain 17 | 18 | - name: Checkout sources 19 | uses: actions/checkout@v3 20 | with: 21 | ref: ${{ github.ref }} 22 | fetch-depth: 0 23 | 24 | - name: Build (bootloader, encrypter and encrypted test application) 25 | run: | 26 | (cd keys/ && ./keygen.sh) 27 | make clean build 28 | (cd test_app/ && make clean build) 29 | (cd encrypter/ && make clean build run) 30 | 31 | - name: Collect artifacts 32 | run: | 33 | mkdir build_artifacts 34 | cp stm32-bl.hex build_artifacts 35 | cp test_app/stm32-app.hex.enc build_artifacts 36 | cp encrypter/hex-encrypter build_artifacts 37 | 38 | - name: Publish artifacts 39 | uses: actions/upload-artifact@v3 40 | with: 41 | path: | 42 | build_artifacts/* 43 | -------------------------------------------------------------------------------- /FreeRTOS-Plus-CLI/History.txt: -------------------------------------------------------------------------------- 1 | Changes between V1.0.3 and V1.0.4 released 2 | 3 | + Update to use stdint and the FreeRTOS specific typedefs that were 4 | introduced in FreeRTOS V8.0.0. 5 | 6 | Changes between V1.0.2 and V1.0.3 released 7 | 8 | + Previously, and in line with good software engineering practice, the 9 | FreeRTOS coding standard did not permit the use of char types that were 10 | not explicitly qualified as either signed or unsigned. As a result char 11 | pointers used to reference strings required casts, as did the use of any 12 | standard string handling functions. The casts ensured compiler warnings 13 | were not generated by compilers that defaulted unqualified char types to 14 | be signed or compilers that defaulted unqualified char types to be 15 | unsigned. As it has in later MISRA standards, this rule has now been 16 | relaxed, and unqualified char types are now permitted, but only when: 17 | 1) The char is used to point to a human readable text string. 18 | 2) The char is used to hold a single ASCII character. 19 | 20 | Changes between V1.0.1 and V1.0.2 released 14/10/2013 21 | 22 | + Changed double quotes (") to single quotes (') in the help string to 23 | allow the strings to be used with JSON in FreeRTOS+Nabto. 24 | 25 | Changes between V1.0.0 and V1.0.1 released 05/07/2012 26 | 27 | + Change the name of the structure used to map a function that implements 28 | a CLI command to the string used to call the command from 29 | xCommandLineInput to CLI_Command_Definition_t, as it was always intended 30 | to be. A #define was added to map the old name to the new name for 31 | reasons of backward compatibility. 32 | 33 | -------------------------------------------------------------------------------- /encrypter/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | #* 3 | #* (C) 2020 Andrii Bilynskyi 4 | #* 5 | #* This code is licensed under the MIT. 6 | #* 7 | #**************************************************************************** 8 | 9 | 10 | CC := gcc 11 | LD := ld 12 | 13 | PROJECT ?= hex-encrypter 14 | HEXFILE ?= ../test_app/stm32-app.hex 15 | 16 | PROJECT_CSRC := \ 17 | ${wildcard *.c} \ 18 | ${wildcard ../tiny-AES-c/*.c} \ 19 | ${wildcard ../hex_parser/*.c} \ 20 | ${wildcard ../keys/*.bin} \ 21 | 22 | INCLUDE_PATH := \ 23 | ./ \ 24 | ../tiny-AES-c/ \ 25 | ../hex_parser/ \ 26 | 27 | LDLIBS := \ 28 | 29 | OBJECTS := ${addsuffix .O, ${PROJECT_CSRC}} 30 | 31 | CFLAGS := -g3 -O1 -Wall -std=gnu99 -fdata-sections -ffunction-sections 32 | 33 | LDFLAGS := -Wl,--gc-sections -static 34 | 35 | BINFLAGS := -r -b binary 36 | 37 | CFLAGS += ${addprefix -I, ${INCLUDE_PATH}} 38 | LDFLAGS += ${addprefix -l, ${LDLIBS}} 39 | 40 | .PHONY: all build clean run 41 | 42 | all: ${PROJECT} 43 | 44 | build: ${PROJECT} 45 | 46 | ${PROJECT}: ${OBJECTS} 47 | ${CC} $^ -o $@ ${LDFLAGS} 48 | 49 | %.c.O: %.c 50 | ${CC} ${CFLAGS} -MD -c $< -o $@ 51 | 52 | %.bin.O: %.bin 53 | ${LD} ${BINFLAGS} $< -o $@ 54 | 55 | %.bin: %.bin.o 56 | @: 57 | 58 | clean: 59 | rm -f ${OBJECTS} $(OBJECTS:.O=.d) ${PROJECT} 60 | 61 | run: 62 | @if [ -f "${PROJECT}" ]; \ 63 | then \ 64 | ./${PROJECT} $(HEXFILE); \ 65 | else \ 66 | echo "Output executable file doesn't exist. Run 'make all' before!"; \ 67 | fi 68 | 69 | help: 70 | $(info Help) 71 | $(info ---------------------------------------------------------------------) 72 | $(info make help - this help text) 73 | $(info make build - create executable) 74 | $(info make all - create executable) 75 | $(info make clean - remove generated files) 76 | $(info make run - run executable) 77 | $(info ---------------------------------------------------------------------) 78 | 79 | -include $(OBJECTS:.O=.d) 80 | -------------------------------------------------------------------------------- /src/usb/usb_conf.h: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************** 3 | ** 4 | ** (C) 2020 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ******************************************************************************** 9 | */ 10 | 11 | #ifndef __USB_CONF_H 12 | #define __USB_CONF_H 13 | 14 | 15 | /* defines how many endpoints are used by the device */ 16 | #define USB_CDC_RX_TX_PACKET (64) 17 | /******************************************************************************/ 18 | 19 | 20 | /* defines how many endpoints are used by the device */ 21 | #define USB_CDC_EP_CNT (4) /* Total CDC endpoints include 0. */ 22 | #define USB_CDC_EP0_PACK (64) 23 | /* For CDC EP1: MCU TX */ 24 | #define USB_CDC_EP1_PACK (USB_CDC_RX_TX_PACKET) 25 | /* For CDC EP2: MCU serial config */ 26 | #define USB_CDC_EP2_PACK (8) 27 | /* For CDC EP3: MCU RX */ 28 | #define USB_CDC_EP3_PACK (USB_CDC_RX_TX_PACKET) 29 | /* For CDC maximum packet */ 30 | #define USB_CDC_MAX_PACK (USB_CDC_EP0_PACK > USB_CDC_EP1_PACK ? \ 31 | USB_CDC_EP0_PACK > USB_CDC_EP2_PACK ? \ 32 | USB_CDC_EP0_PACK > USB_CDC_EP3_PACK ? \ 33 | USB_CDC_EP0_PACK : USB_CDC_EP3_PACK : \ 34 | USB_CDC_EP2_PACK > USB_CDC_EP3_PACK ? \ 35 | USB_CDC_EP2_PACK : USB_CDC_EP3_PACK : \ 36 | USB_CDC_EP1_PACK > USB_CDC_EP2_PACK ? \ 37 | USB_CDC_EP1_PACK > USB_CDC_EP3_PACK ? \ 38 | USB_CDC_EP1_PACK : USB_CDC_EP3_PACK : \ 39 | USB_CDC_EP2_PACK > USB_CDC_EP3_PACK ? \ 40 | USB_CDC_EP2_PACK : USB_CDC_EP3_PACK) 41 | 42 | 43 | /* mask defining which events has to be handled */ 44 | #define IMR_MSK (CNTR_CTRM | CNTR_RESETM) 45 | 46 | /* calculate endpoint address */ 47 | #define BTABLE_ADDRESS (0x00) /* align to 8 */ 48 | #define __ALIGN_8(x) ((((x) + 7) >> 3) << 3) 49 | 50 | #define ENDP0_RXADDR __ALIGN_8(BTABLE_ADDRESS + (USB_CDC_EP_CNT << 3)) 51 | #define ENDP0_TXADDR __ALIGN_8(ENDP0_RXADDR + USB_CDC_EP0_PACK) 52 | #define ENDP1_TXADDR __ALIGN_8(ENDP0_TXADDR + USB_CDC_EP0_PACK) 53 | #define ENDP2_TXADDR __ALIGN_8(ENDP1_TXADDR + USB_CDC_EP1_PACK) 54 | #define ENDP3_RXADDR __ALIGN_8(ENDP2_TXADDR + USB_CDC_EP2_PACK) 55 | 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /openocd/stm32f1x.cfg: -------------------------------------------------------------------------------- 1 | # script for stm32f1x family 2 | 3 | # 4 | # stm32 devices support both JTAG and SWD transports. 5 | # 6 | source [find target/swj-dp.tcl] 7 | source [find mem_helper.tcl] 8 | 9 | if { [info exists CHIPNAME] } { 10 | set _CHIPNAME $CHIPNAME 11 | } else { 12 | set _CHIPNAME stm32f1x 13 | } 14 | 15 | set _ENDIAN little 16 | 17 | # Work-area is a space in RAM used for flash programming 18 | # By default use 4kB (as found on some STM32F100s) 19 | if { [info exists WORKAREASIZE] } { 20 | set _WORKAREASIZE $WORKAREASIZE 21 | } else { 22 | set _WORKAREASIZE 0x1000 23 | } 24 | 25 | # Allow overriding the Flash bank size 26 | if { [info exists FLASH_SIZE] } { 27 | set _FLASH_SIZE $FLASH_SIZE 28 | } else { 29 | # autodetect size 30 | set _FLASH_SIZE 0 31 | } 32 | 33 | #jtag scan chain 34 | if { [info exists CPUTAPID] } { 35 | set _CPUTAPID $CPUTAPID 36 | } else { 37 | if { [using_jtag] } { 38 | # See STM Document RM0008 Section 26.6.3 39 | set _CPUTAPID 0x3ba00477 40 | } { 41 | # this is the SW-DP tap id not the jtag tap id 42 | set _CPUTAPID 0x1ba01477 43 | } 44 | } 45 | 46 | swj_newdap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID 47 | dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu 48 | 49 | if {[using_jtag]} { 50 | jtag newtap $_CHIPNAME bs -irlen 5 51 | } 52 | 53 | set _TARGETNAME $_CHIPNAME.cpu 54 | target create $_TARGETNAME cortex_m -endian $_ENDIAN -dap $_CHIPNAME.dap 55 | 56 | $_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0 57 | 58 | # flash size will be probed 59 | set _FLASHNAME $_CHIPNAME.flash 60 | flash bank $_FLASHNAME stm32f1x 0x08000000 $_FLASH_SIZE 0 0 $_TARGETNAME 61 | 62 | # JTAG speed should be <= F_CPU/6. F_CPU after reset is 8MHz, so use F_JTAG = 1MHz 63 | adapter speed 1000 64 | 65 | adapter srst delay 100 66 | if {[using_jtag]} { 67 | jtag_ntrst_delay 100 68 | } 69 | 70 | reset_config srst_nogate 71 | 72 | if {![using_hla]} { 73 | # if srst is not fitted use SYSRESETREQ to 74 | # perform a soft reset 75 | cortex_m reset_config sysresetreq 76 | } 77 | 78 | $_TARGETNAME configure -event examine-end { 79 | # DBGMCU_CR |= DBG_WWDG_STOP | DBG_IWDG_STOP | 80 | # DBG_STANDBY | DBG_STOP | DBG_SLEEP 81 | mmw 0xE0042004 0x00000307 0 82 | } 83 | 84 | $_TARGETNAME configure -event trace-config { 85 | # Set TRACE_IOEN; TRACE_MODE is set to async; when using sync 86 | # change this value accordingly to configure trace pins 87 | # assignment 88 | mmw 0xE0042004 0x00000020 0 89 | } 90 | -------------------------------------------------------------------------------- /stm/system_stm32f10x.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file system_stm32f10x.h 4 | * @author MCD Application Team 5 | * @version V3.6.4 6 | * @date 22-September-2016 7 | * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Header File. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2016 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /** @addtogroup CMSIS 29 | * @{ 30 | */ 31 | 32 | /** @addtogroup stm32f10x_system 33 | * @{ 34 | */ 35 | 36 | /** 37 | * @brief Define to prevent recursive inclusion 38 | */ 39 | #ifndef __SYSTEM_STM32F10X_H 40 | #define __SYSTEM_STM32F10X_H 41 | 42 | #ifdef __cplusplus 43 | extern "C" { 44 | #endif 45 | 46 | /** @addtogroup STM32F10x_System_Includes 47 | * @{ 48 | */ 49 | 50 | /** 51 | * @} 52 | */ 53 | 54 | 55 | /** @addtogroup STM32F10x_System_Exported_types 56 | * @{ 57 | */ 58 | 59 | extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */ 60 | 61 | /** 62 | * @} 63 | */ 64 | 65 | /** @addtogroup STM32F10x_System_Exported_Constants 66 | * @{ 67 | */ 68 | 69 | /** 70 | * @} 71 | */ 72 | 73 | /** @addtogroup STM32F10x_System_Exported_Macros 74 | * @{ 75 | */ 76 | 77 | /** 78 | * @} 79 | */ 80 | 81 | /** @addtogroup STM32F10x_System_Exported_Functions 82 | * @{ 83 | */ 84 | 85 | extern void SystemInit(void); 86 | extern void SystemCoreClockUpdate(void); 87 | extern void SystemHseFailed(void); 88 | /** 89 | * @} 90 | */ 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif 95 | 96 | #endif /*__SYSTEM_STM32F10X_H */ 97 | 98 | /** 99 | * @} 100 | */ 101 | 102 | /** 103 | * @} 104 | */ 105 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 106 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/inc/stm32f10x_crc.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_crc.h 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file contains all the functions prototypes for the CRC firmware 8 | * library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | *

© COPYRIGHT 2012 STMicroelectronics

13 | * 14 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 15 | * You may not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at: 17 | * 18 | * http://www.st.com/software_license_agreement_liberty_v2 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, 22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | ****************************************************************************** 27 | */ 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __STM32F10x_CRC_H 31 | #define __STM32F10x_CRC_H 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | /* Includes ------------------------------------------------------------------*/ 38 | #include "stm32f10x.h" 39 | 40 | /** @addtogroup STM32F10x_StdPeriph_Driver 41 | * @{ 42 | */ 43 | 44 | /** @addtogroup CRC 45 | * @{ 46 | */ 47 | 48 | /** @defgroup CRC_Exported_Types 49 | * @{ 50 | */ 51 | 52 | /** 53 | * @} 54 | */ 55 | 56 | /** @defgroup CRC_Exported_Constants 57 | * @{ 58 | */ 59 | 60 | /** 61 | * @} 62 | */ 63 | 64 | /** @defgroup CRC_Exported_Macros 65 | * @{ 66 | */ 67 | 68 | /** 69 | * @} 70 | */ 71 | 72 | /** @defgroup CRC_Exported_Functions 73 | * @{ 74 | */ 75 | 76 | void CRC_ResetDR(void); 77 | uint32_t CRC_CalcCRC(uint32_t Data); 78 | uint32_t CRC_CalcBlockCRC(uint32_t pBuffer[], uint32_t BufferLength); 79 | uint32_t CRC_GetCRC(void); 80 | void CRC_SetIDRegister(uint8_t IDValue); 81 | uint8_t CRC_GetIDRegister(void); 82 | 83 | #ifdef __cplusplus 84 | } 85 | #endif 86 | 87 | #endif /* __STM32F10x_CRC_H */ 88 | /** 89 | * @} 90 | */ 91 | 92 | /** 93 | * @} 94 | */ 95 | 96 | /** 97 | * @} 98 | */ 99 | 100 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 101 | -------------------------------------------------------------------------------- /stm/STM32_USB-FS-Device_Driver/inc/usb_int.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_int.h 4 | * @author MCD Application Team 5 | * @version V4.1.0 6 | * @date 26-May-2017 7 | * @brief Endpoint CTR (Low and High) interrupt's service routines prototypes 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT(c) 2017 STMicroelectronics

12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software 22 | * without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | ****************************************************************************** 36 | */ 37 | 38 | 39 | /* Define to prevent recursive inclusion -------------------------------------*/ 40 | #ifndef __USB_INT_H 41 | #define __USB_INT_H 42 | 43 | /* Includes ------------------------------------------------------------------*/ 44 | /* Exported types ------------------------------------------------------------*/ 45 | /* Exported constants --------------------------------------------------------*/ 46 | /* Exported macro ------------------------------------------------------------*/ 47 | /* Exported functions ------------------------------------------------------- */ 48 | void CTR_LP(void); 49 | void CTR_HP(void); 50 | 51 | /* External variables --------------------------------------------------------*/ 52 | 53 | #endif /* __USB_INT_H */ 54 | 55 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 56 | -------------------------------------------------------------------------------- /src/stm_hex.c: -------------------------------------------------------------------------------- 1 | /* 2 | ***************************************************************************** 3 | ** 4 | ** (C) 2023 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ***************************************************************************** 9 | */ 10 | 11 | #include "stm_hex.h" 12 | #include 13 | #include "hex_parser.h" 14 | #include "aes.h" 15 | 16 | 17 | extern const uint8_t _binary_keys_iv_bin_start[]; 18 | extern const uint8_t _binary_keys_iv_bin_end[]; 19 | extern const uint8_t _binary_keys_key_bin_start[]; 20 | extern const uint8_t _binary_keys_key_bin_end[]; 21 | 22 | 23 | static stm_hex_on_word_t stm_hex_on_word = NULL; 24 | static struct AES_ctx ctx; 25 | 26 | 27 | static bool new_data(uint32_t addr, uint8_t data[], uint16_t len); 28 | static bool hex_parser_on_data(bool finish, uint32_t addr, uint8_t data); 29 | 30 | 31 | bool stm_hex_feed(const char *str) 32 | { 33 | return hex_parser_feed((char *)str, false); 34 | } 35 | 36 | void stm_hex_set_callback(stm_hex_on_word_t on_word) 37 | { 38 | stm_hex_on_word = on_word; 39 | hex_parser_set_callback(new_data); 40 | AES_init_ctx_iv(&ctx, _binary_keys_key_bin_start, _binary_keys_iv_bin_start); 41 | } 42 | 43 | 44 | static bool new_data(uint32_t addr, uint8_t data[], uint16_t len) 45 | { 46 | bool result = true; 47 | if (data) { 48 | AES_CTR_xcrypt_buffer(&ctx, data, len); 49 | for (uint16_t i = 0; i < len && result; i++) { 50 | result = hex_parser_on_data(false, addr + i, data[i]); 51 | } 52 | } else { 53 | result = hex_parser_on_data(true, 0, 0); 54 | } 55 | return result; 56 | } 57 | 58 | static bool hex_parser_on_data(bool finish, uint32_t addr, uint8_t data) 59 | { 60 | static bool even_valid = false; 61 | static uint32_t even_addr = 0; 62 | static uint8_t even_data = 0; 63 | bool result = true; 64 | if (finish) { 65 | if (even_valid && stm_hex_on_word) { 66 | result = stm_hex_on_word(even_addr, (uint16_t)0xFF00 | even_data); 67 | } 68 | even_valid = false; 69 | } else if (addr & 0x01) { 70 | if (stm_hex_on_word) { 71 | if (even_valid) { 72 | if (addr - even_addr == 1) { 73 | result = stm_hex_on_word(even_addr, ((uint16_t)data << 8) | even_data); 74 | } else { 75 | result = stm_hex_on_word(even_addr, (uint16_t)0xFF00 | even_data); 76 | if (result) { 77 | result = stm_hex_on_word(addr - 1, ((uint16_t)data << 8) | 0xFF); 78 | } 79 | } 80 | } else { 81 | result = stm_hex_on_word(addr - 1, ((uint16_t)data << 8) | 0xFF); 82 | } 83 | } 84 | even_valid = false; 85 | } else { 86 | if (even_valid && stm_hex_on_word) { 87 | result = stm_hex_on_word(even_addr, (uint16_t)0xFF00 | even_data); 88 | } 89 | even_valid = true; 90 | even_addr = addr; 91 | even_data = data; 92 | } 93 | return result; 94 | } 95 | -------------------------------------------------------------------------------- /tiny-AES-c/aes.h: -------------------------------------------------------------------------------- 1 | #ifndef _AES_H_ 2 | #define _AES_H_ 3 | 4 | #include 5 | 6 | // #define the macros below to 1/0 to enable/disable the mode of operation. 7 | // 8 | // CBC enables AES encryption in CBC-mode of operation. 9 | // CTR enables encryption in counter-mode. 10 | // ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously. 11 | 12 | // The #ifndef-guard allows it to be configured before #include'ing or at compile time. 13 | #ifndef CBC 14 | #define CBC 1 15 | #endif 16 | 17 | #ifndef ECB 18 | #define ECB 1 19 | #endif 20 | 21 | #ifndef CTR 22 | #define CTR 1 23 | #endif 24 | 25 | 26 | #define AES128 1 27 | //#define AES192 1 28 | //#define AES256 1 29 | 30 | #define AES_BLOCKLEN 16 //Block length in bytes AES is 128b block only 31 | 32 | #if defined(AES256) && (AES256 == 1) 33 | #define AES_KEYLEN 32 34 | #define AES_keyExpSize 240 35 | #elif defined(AES192) && (AES192 == 1) 36 | #define AES_KEYLEN 24 37 | #define AES_keyExpSize 208 38 | #else 39 | #define AES_KEYLEN 16 // Key length in bytes 40 | #define AES_keyExpSize 176 41 | #endif 42 | 43 | struct AES_ctx 44 | { 45 | uint8_t RoundKey[AES_keyExpSize]; 46 | #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) 47 | uint8_t Iv[AES_BLOCKLEN]; 48 | #endif 49 | }; 50 | 51 | void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key); 52 | #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) 53 | void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv); 54 | void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv); 55 | #endif 56 | 57 | #if defined(ECB) && (ECB == 1) 58 | // buffer size is exactly AES_BLOCKLEN bytes; 59 | // you need only AES_init_ctx as IV is not used in ECB 60 | // NB: ECB is considered insecure for most uses 61 | void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf); 62 | void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf); 63 | 64 | #endif // #if defined(ECB) && (ECB == !) 65 | 66 | 67 | #if defined(CBC) && (CBC == 1) 68 | // buffer size MUST be mutile of AES_BLOCKLEN; 69 | // Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme 70 | // NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv() 71 | // no IV should ever be reused with the same key 72 | void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 73 | void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 74 | 75 | #endif // #if defined(CBC) && (CBC == 1) 76 | 77 | 78 | #if defined(CTR) && (CTR == 1) 79 | 80 | // Same function for encrypting as for decrypting. 81 | // IV is incremented for every block, and used after encryption as XOR-compliment for output 82 | // Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme 83 | // NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv() 84 | // no IV should ever be reused with the same key 85 | void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 86 | 87 | #endif // #if defined(CTR) && (CTR == 1) 88 | 89 | 90 | #endif //_AES_H_ 91 | -------------------------------------------------------------------------------- /stm/STM32_USB-FS-Device_Driver/inc/usb_mem.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_mem.h 4 | * @author MCD Application Team 5 | * @version V4.1.0 6 | * @date 26-May-2017 7 | * @brief Utility prototypes functions for memory/PMA transfers 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT(c) 2017 STMicroelectronics

12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software 22 | * without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | ****************************************************************************** 36 | */ 37 | 38 | 39 | /* Define to prevent recursive inclusion -------------------------------------*/ 40 | #ifndef __USB_MEM_H 41 | #define __USB_MEM_H 42 | 43 | /* Includes ------------------------------------------------------------------*/ 44 | /* Exported types ------------------------------------------------------------*/ 45 | /* Exported constants --------------------------------------------------------*/ 46 | /* Exported macro ------------------------------------------------------------*/ 47 | /* Exported functions ------------------------------------------------------- */ 48 | void UserToPMABufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes); 49 | void PMAToUserBufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes); 50 | /* External variables --------------------------------------------------------*/ 51 | 52 | #endif /*__USB_MEM_H*/ 53 | 54 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 55 | -------------------------------------------------------------------------------- /stm/STM32_USB-FS-Device_Driver/inc/usb_type.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_type.h 4 | * @author MCD Application Team 5 | * @version V4.1.0 6 | * @date 26-May-2017 7 | * @brief Type definitions used by the USB Library 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT(c) 2017 STMicroelectronics

12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software 22 | * without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | ****************************************************************************** 36 | */ 37 | 38 | 39 | /* Define to prevent recursive inclusion -------------------------------------*/ 40 | #ifndef __USB_TYPE_H 41 | #define __USB_TYPE_H 42 | 43 | /* Includes ------------------------------------------------------------------*/ 44 | #include "usb_conf.h" 45 | 46 | /* Exported types ------------------------------------------------------------*/ 47 | /* Exported constants --------------------------------------------------------*/ 48 | #ifndef NULL 49 | #define NULL ((void *)0) 50 | #endif 51 | 52 | typedef enum 53 | { 54 | FALSE = 0, TRUE = !FALSE 55 | } 56 | bool; 57 | 58 | /* Exported macro ------------------------------------------------------------*/ 59 | /* Exported functions ------------------------------------------------------- */ 60 | /* External variables --------------------------------------------------------*/ 61 | 62 | #endif /* __USB_TYPE_H */ 63 | 64 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 65 | -------------------------------------------------------------------------------- /stm/STM32_USB-FS-Device_Driver/inc/usb_sil.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_sil.h 4 | * @author MCD Application Team 5 | * @version V4.1.0 6 | * @date 26-May-2017 7 | * @brief Simplified Interface Layer function prototypes. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT(c) 2017 STMicroelectronics

12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software 22 | * without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | ****************************************************************************** 36 | */ 37 | 38 | 39 | /* Define to prevent recursive inclusion -------------------------------------*/ 40 | #ifndef __USB_SIL_H 41 | #define __USB_SIL_H 42 | 43 | /* Includes ------------------------------------------------------------------*/ 44 | /* Exported types ------------------------------------------------------------*/ 45 | /* Exported constants --------------------------------------------------------*/ 46 | /* Exported macro ------------------------------------------------------------*/ 47 | /* Exported functions ------------------------------------------------------- */ 48 | 49 | uint32_t USB_SIL_Init(void); 50 | uint32_t USB_SIL_Write(uint8_t bEpAddr, uint8_t* pBufferPointer, uint32_t wBufferSize); 51 | uint32_t USB_SIL_Read(uint8_t bEpAddr, uint8_t* pBufferPointer); 52 | 53 | /* External variables --------------------------------------------------------*/ 54 | 55 | #endif /* __USB_SIL_H */ 56 | 57 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 58 | -------------------------------------------------------------------------------- /config/stm32f10x_conf.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_conf.h 4 | * @author MCD Application Team 5 | * @version V4.1.0 6 | * @date 26-May-2017 (Modified 2023) 7 | * @brief Library configuration file. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT(c) 2017 STMicroelectronics

12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software 22 | * without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | ****************************************************************************** 36 | */ 37 | 38 | 39 | #ifndef __STM32F10x_CONF_H 40 | #define __STM32F10x_CONF_H 41 | 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include 57 | #include 58 | #include 59 | #include 60 | #include 61 | #include 62 | #include 63 | #include 64 | 65 | #ifdef USE_FULL_ASSERT 66 | 67 | #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) 68 | void assert_failed(uint8_t *file, uint32_t line); 69 | 70 | #else 71 | 72 | #define assert_param(expr) ((void)0) 73 | 74 | #endif 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /test_app/stm32f10x_conf.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_conf.h 4 | * @author MCD Application Team 5 | * @version V4.1.0 6 | * @date 26-May-2017 (Modified 2023) 7 | * @brief Library configuration file. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT(c) 2017 STMicroelectronics

12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software 22 | * without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | ****************************************************************************** 36 | */ 37 | 38 | 39 | #ifndef __STM32F10x_CONF_H 40 | #define __STM32F10x_CONF_H 41 | 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include 57 | #include 58 | #include 59 | #include 60 | #include 61 | #include 62 | #include 63 | #include 64 | 65 | #ifdef USE_FULL_ASSERT 66 | 67 | #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) 68 | void assert_failed(uint8_t *file, uint32_t line); 69 | 70 | #else 71 | 72 | #define assert_param(expr) ((void)0) 73 | 74 | #endif 75 | 76 | #endif 77 | 78 | -------------------------------------------------------------------------------- /stm/STM32_USB-FS-Device_Driver/inc/usb_lib.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_lib.h 4 | * @author MCD Application Team 5 | * @version V4.1.0 6 | * @date 26-May-2017 7 | * @brief USB library include files 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT(c) 2017 STMicroelectronics

12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software 22 | * without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | ****************************************************************************** 36 | */ 37 | 38 | 39 | /* Define to prevent recursive inclusion -------------------------------------*/ 40 | #ifndef __USB_LIB_H 41 | #define __USB_LIB_H 42 | 43 | /* Includes ------------------------------------------------------------------*/ 44 | #include "hw_config.h" 45 | #include "usb_type.h" 46 | #include "usb_regs.h" 47 | #include "usb_def.h" 48 | #include "usb_core.h" 49 | #include "usb_init.h" 50 | #include "usb_sil.h" 51 | #include "usb_mem.h" 52 | #include "usb_int.h" 53 | 54 | /* Exported types ------------------------------------------------------------*/ 55 | /* Exported constants --------------------------------------------------------*/ 56 | /* Exported macro ------------------------------------------------------------*/ 57 | /* Exported functions ------------------------------------------------------- */ 58 | /* External variables --------------------------------------------------------*/ 59 | 60 | #endif /* __USB_LIB_H */ 61 | 62 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 63 | -------------------------------------------------------------------------------- /test_app/Makefile: -------------------------------------------------------------------------------- 1 | #**************************************************************************** 2 | #* 3 | #* (C) 2020 Andrii Bilynskyi 4 | #* 5 | #* This code is licensed under the MIT. 6 | #* 7 | #**************************************************************************** 8 | 9 | 10 | TOOLCHAIN_PATH := /opt/arm-gnu-toolchain-12.2.mpacbti-rel1-x86_64-arm-none-eabi 11 | 12 | 13 | CC := ${TOOLCHAIN_PATH}/bin/arm-none-eabi-gcc 14 | CXX := ${TOOLCHAIN_PATH}/bin/arm-none-eabi-g++ 15 | AS := ${TOOLCHAIN_PATH}/bin/arm-none-eabi-gcc 16 | OBJCPY := ${TOOLCHAIN_PATH}/bin/arm-none-eabi-objcopy 17 | SIZE := ${TOOLCHAIN_PATH}/bin/arm-none-eabi-size 18 | GDB := ${TOOLCHAIN_PATH}/bin/arm-none-eabi-gdb 19 | 20 | 21 | PROJECT ?= stm32-app 22 | 23 | 24 | PROJECT_CSRC := \ 25 | ${wildcard ../stm/*.c} \ 26 | ${wildcard ../stm/*.s} \ 27 | ${wildcard ../stm/STM32F10x_StdPeriph_Driver/src/*.c} \ 28 | ${wildcard *.c} \ 29 | 30 | INCLUDE_PATH := \ 31 | ../cm \ 32 | ../stm \ 33 | ../stm/STM32F10x_StdPeriph_Driver/inc \ 34 | ./ \ 35 | 36 | LDLIBS := \ 37 | stdc++ \ 38 | 39 | LD_SCRIPT := stm32_app.ld 40 | 41 | OBJECTS := ${addsuffix .o, ${PROJECT_CSRC}} 42 | 43 | CFLAGS := -Wall -std=c99 -fdata-sections -ffunction-sections 44 | CFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m3 45 | CFLAGS += -DSTM32F10X_MD -DHSE_VALUE=8000000u -DUSE_STDPERIPH_DRIVER 46 | 47 | ASFLAGS := -Wall -std=c99 48 | ASFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m3 49 | ASFLAGS += -DSTM32F10X_MD -DHSE_VALUE=8000000u -DUSE_STDPERIPH_DRIVER 50 | 51 | CXXFLAGS := -Wall -fno-exceptions -fdata-sections -ffunction-sections 52 | CXXFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m3 53 | CXXFLAGS += -DSTM32F10X_MD -DHSE_VALUE=8000000u -DUSE_STDPERIPH_DRIVER 54 | 55 | LDFLAGS := -T${LD_SCRIPT} 56 | LDFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m3 57 | LDFLAGS += -Wl,--gc-sections 58 | LDFLAGS += -Xlinker -Map=${PROJECT}.map 59 | LDFLAGS += ${addprefix -l, ${LDLIBS}} 60 | 61 | 62 | CFLAGS += -g0 -O3 63 | ASFLAGS += -g0 -O3 64 | CXXFLAGS += -g0 -O3 65 | LDFLAGS += --specs=nosys.specs 66 | 67 | 68 | CFLAGS += ${addprefix -I, ${INCLUDE_PATH}} 69 | CXXFLAGS += ${addprefix -I, ${INCLUDE_PATH}} 70 | 71 | .PHONY: all build clean help 72 | 73 | all: ${PROJECT}.elf 74 | 75 | build: ${PROJECT}.elf 76 | 77 | ${PROJECT}.elf: ${OBJECTS} 78 | $(info $(BUILD) BUILD) 79 | ${CC} $^ -o $@ ${LDFLAGS} 80 | ${OBJCPY} -O ihex $@ ${PROJECT}.hex 81 | ${SIZE} --format=berkeley $@ 82 | 83 | %.c.o: %.c 84 | ${CC} ${CFLAGS} -MD -c $< -o $@ 85 | 86 | %.s.o: %.s 87 | ${CC} ${ASFLAGS} -MD -c $< -o $@ 88 | 89 | %.cpp.o: %.cpp 90 | ${CXX} ${CXXFLAGS} -MD -c $< -o $@ 91 | 92 | clean: 93 | rm -f ${OBJECTS} $(OBJECTS:.o=.d) ${PROJECT}.elf ${PROJECT}.hex ${PROJECT}.map 94 | 95 | help: 96 | $(info Help) 97 | $(info ---------------------------------------------------------------------) 98 | $(info make help - this help text) 99 | $(info make build - create binary files (.elf, .hex)) 100 | $(info make all - create binary files (.elf, .hex). same as build) 101 | $(info make clean - remove generated files) 102 | $(info ---------------------------------------------------------------------) 103 | 104 | -include $(OBJECTS:.o=.d) 105 | -------------------------------------------------------------------------------- /encrypter/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | ***************************************************************************** 3 | ** 4 | ** (C) 2023 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ***************************************************************************** 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "hex_parser.h" 16 | #include "aes.h" 17 | 18 | 19 | //#define SHOW_SECRET 20 | //#define SHOW_ORIGIN 21 | //#define SHOW_ENCRYPTED 22 | 23 | 24 | #define OUT_FILE_EXT ".enc" 25 | 26 | extern const uint8_t _binary____keys_iv_bin_start[]; 27 | extern const uint8_t _binary____keys_iv_bin_end[]; 28 | extern const uint8_t _binary____keys_key_bin_start[]; 29 | extern const uint8_t _binary____keys_key_bin_end[]; 30 | 31 | 32 | static struct AES_ctx ctx; 33 | 34 | 35 | bool hex_parser_new_data(uint32_t addr, uint8_t data[], uint16_t len) 36 | { 37 | if (data) { 38 | #ifdef SHOW_ORIGIN 39 | printf("%08X:", addr); 40 | for (uint16_t i = 0; i < len; i++) { 41 | printf(" %02X", data[i]); 42 | } 43 | printf("\n"); 44 | #endif 45 | AES_CTR_xcrypt_buffer(&ctx, data, len); 46 | #ifdef SHOW_ENCRYPTED 47 | printf("%08X:", addr); 48 | for (uint16_t i = 0; i < len; i++) { 49 | printf(" %02X", data[i]); 50 | } 51 | printf("\n"); 52 | #endif 53 | } 54 | return true; 55 | } 56 | 57 | 58 | int main(int argc, char *argv[]) 59 | { 60 | printf("HEX file AES encrypter\n"); 61 | #ifdef SHOW_SECRET 62 | printf("IV: "); 63 | for (const uint8_t *bt = _binary____keys_iv_bin_start; bt != _binary____keys_iv_bin_end; ++bt) { 64 | printf(" %02X", *bt); 65 | } 66 | printf("\n"); 67 | printf("KEY:"); 68 | for (const uint8_t *bt = _binary____keys_key_bin_start; bt != _binary____keys_key_bin_end; ++bt) { 69 | printf(" %02X", *bt); 70 | } 71 | printf("\n"); 72 | #endif 73 | if (argc != 2) { 74 | printf("Usage %s \n", argv[0]); 75 | return 1; 76 | } 77 | FILE *f_input; 78 | f_input = fopen(argv[1], "r"); 79 | if (!f_input) { 80 | printf("Hex file %s not found\n", argv[1]); 81 | return 1; 82 | } 83 | char *out_name = malloc(strlen(OUT_FILE_EXT) + strlen(argv[1]) + 1); 84 | if (!out_name) { 85 | fclose(f_input); 86 | printf("Memory allocation error\n"); 87 | return 1; 88 | } 89 | strcpy(out_name, argv[1]); 90 | strcpy(&out_name[strlen(out_name)], OUT_FILE_EXT); 91 | FILE *f_output; 92 | f_output = fopen(out_name, "w"); 93 | free(out_name); 94 | if (!f_output) { 95 | fclose(f_input); 96 | printf("Hex file %s not found\n", argv[1]); 97 | return 1; 98 | } 99 | hex_parser_set_callback(hex_parser_new_data); 100 | AES_init_ctx_iv(&ctx, _binary____keys_key_bin_start, _binary____keys_iv_bin_start); 101 | char *line = NULL; 102 | size_t len = 0; 103 | while (getline(&line, &len, f_input) != -1) { 104 | if (strlen(line) >= 2) { 105 | line[strlen(line) - 2] = '\0'; 106 | if (!hex_parser_feed(line, true)) { 107 | printf("Unparsed HEX line: %s\n", line); 108 | } else { 109 | fprintf(f_output, "%s\r\n", line); 110 | } 111 | } 112 | } 113 | if (line) { 114 | free(line); 115 | } 116 | fclose(f_output); 117 | fclose(f_input); 118 | printf("All done\n"); 119 | return 0; 120 | } 121 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/inc/stm32f10x_wwdg.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_wwdg.h 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file contains all the functions prototypes for the WWDG firmware 8 | * library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | *

© COPYRIGHT 2012 STMicroelectronics

13 | * 14 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 15 | * You may not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at: 17 | * 18 | * http://www.st.com/software_license_agreement_liberty_v2 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, 22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | ****************************************************************************** 27 | */ 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __STM32F10x_WWDG_H 31 | #define __STM32F10x_WWDG_H 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | /* Includes ------------------------------------------------------------------*/ 38 | #include "stm32f10x.h" 39 | 40 | /** @addtogroup STM32F10x_StdPeriph_Driver 41 | * @{ 42 | */ 43 | 44 | /** @addtogroup WWDG 45 | * @{ 46 | */ 47 | 48 | /** @defgroup WWDG_Exported_Types 49 | * @{ 50 | */ 51 | 52 | /** 53 | * @} 54 | */ 55 | 56 | /** @defgroup WWDG_Exported_Constants 57 | * @{ 58 | */ 59 | 60 | /** @defgroup WWDG_Prescaler 61 | * @{ 62 | */ 63 | 64 | #define WWDG_Prescaler_1 ((uint32_t)0x00000000) 65 | #define WWDG_Prescaler_2 ((uint32_t)0x00000080) 66 | #define WWDG_Prescaler_4 ((uint32_t)0x00000100) 67 | #define WWDG_Prescaler_8 ((uint32_t)0x00000180) 68 | #define IS_WWDG_PRESCALER(PRESCALER) (((PRESCALER) == WWDG_Prescaler_1) || \ 69 | ((PRESCALER) == WWDG_Prescaler_2) || \ 70 | ((PRESCALER) == WWDG_Prescaler_4) || \ 71 | ((PRESCALER) == WWDG_Prescaler_8)) 72 | #define IS_WWDG_WINDOW_VALUE(VALUE) ((VALUE) <= 0x7F) 73 | #define IS_WWDG_COUNTER(COUNTER) (((COUNTER) >= 0x40) && ((COUNTER) <= 0x7F)) 74 | 75 | /** 76 | * @} 77 | */ 78 | 79 | /** 80 | * @} 81 | */ 82 | 83 | /** @defgroup WWDG_Exported_Macros 84 | * @{ 85 | */ 86 | /** 87 | * @} 88 | */ 89 | 90 | /** @defgroup WWDG_Exported_Functions 91 | * @{ 92 | */ 93 | 94 | void WWDG_DeInit(void); 95 | void WWDG_SetPrescaler(uint32_t WWDG_Prescaler); 96 | void WWDG_SetWindowValue(uint8_t WindowValue); 97 | void WWDG_EnableIT(void); 98 | void WWDG_SetCounter(uint8_t Counter); 99 | void WWDG_Enable(uint8_t Counter); 100 | FlagStatus WWDG_GetFlagStatus(void); 101 | void WWDG_ClearFlag(void); 102 | 103 | #ifdef __cplusplus 104 | } 105 | #endif 106 | 107 | #endif /* __STM32F10x_WWDG_H */ 108 | 109 | /** 110 | * @} 111 | */ 112 | 113 | /** 114 | * @} 115 | */ 116 | 117 | /** 118 | * @} 119 | */ 120 | 121 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 122 | -------------------------------------------------------------------------------- /stm/STM32_USB-FS-Device_Driver/inc/usb_init.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_init.h 4 | * @author MCD Application Team 5 | * @version V4.1.0 6 | * @date 26-May-2017 7 | * @brief Initialization routines & global variables 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT(c) 2017 STMicroelectronics

12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software 22 | * without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | ****************************************************************************** 36 | */ 37 | 38 | 39 | /* Define to prevent recursive inclusion -------------------------------------*/ 40 | #ifndef __USB_INIT_H 41 | #define __USB_INIT_H 42 | 43 | /* Includes ------------------------------------------------------------------*/ 44 | /* Exported types ------------------------------------------------------------*/ 45 | /* Exported constants --------------------------------------------------------*/ 46 | /* Exported macro ------------------------------------------------------------*/ 47 | /* Exported functions ------------------------------------------------------- */ 48 | void USB_Init(void); 49 | 50 | /* External variables --------------------------------------------------------*/ 51 | /* The number of current endpoint, it will be used to specify an endpoint */ 52 | extern uint8_t EPindex; 53 | /* The number of current device, it is an index to the Device_Table */ 54 | /*extern uint8_t Device_no; */ 55 | /* Points to the DEVICE_INFO structure of current device */ 56 | /* The purpose of this register is to speed up the execution */ 57 | extern DEVICE_INFO* pInformation; 58 | /* Points to the DEVICE_PROP structure of current device */ 59 | /* The purpose of this register is to speed up the execution */ 60 | extern DEVICE_PROP* pProperty; 61 | /* Temporary save the state of Rx & Tx status. */ 62 | /* Whenever the Rx or Tx state is changed, its value is saved */ 63 | /* in this variable first and will be set to the EPRB or EPRA */ 64 | /* at the end of interrupt process */ 65 | extern USER_STANDARD_REQUESTS *pUser_Standard_Requests; 66 | 67 | extern uint16_t SaveState ; 68 | extern uint16_t wInterrupt_Mask; 69 | 70 | #endif /* __USB_INIT_H */ 71 | 72 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 73 | -------------------------------------------------------------------------------- /test_app/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************** 3 | ** 4 | ** (C) 2023 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ******************************************************************************** 9 | */ 10 | 11 | #include 12 | 13 | 14 | #define LED_PIN GPIO_Pin_13 15 | #define LED_PORT GPIOC 16 | 17 | 18 | #ifdef DEBUG 19 | #include 20 | 21 | extern void initialise_monitor_handles(void); 22 | 23 | #define DBG_INIT() initialise_monitor_handles() 24 | #define DBG_OUT(fmt, ...) \ 25 | printf("%s:%04u " fmt "\n", __FILE__, (unsigned)__LINE__, ##__VA_ARGS__) 26 | #else 27 | #define DBG_INIT() 28 | #define DBG_OUT(fmt, ...) 29 | #endif 30 | 31 | 32 | static void sleep_ms(unsigned int ms) 33 | { 34 | while (ms--) { 35 | volatile unsigned int tmp = 5971u; 36 | while (tmp--) { 37 | __asm("nop"); 38 | } 39 | } 40 | } 41 | 42 | 43 | int main(void) 44 | { 45 | DBG_INIT(); 46 | DBG_OUT("main() start"); 47 | NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); 48 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | \ 49 | RCC_APB2Periph_GPIOA | \ 50 | RCC_APB2Periph_GPIOB | \ 51 | RCC_APB2Periph_GPIOC | \ 52 | RCC_APB2Periph_GPIOD | \ 53 | RCC_APB2Periph_GPIOE | \ 54 | RCC_APB2Periph_GPIOF | \ 55 | RCC_APB2Periph_GPIOG, ENABLE); 56 | GPIO_InitTypeDef GPIO_InitStruct_LED; 57 | GPIO_InitStruct_LED.GPIO_Pin = LED_PIN; 58 | GPIO_InitStruct_LED.GPIO_Speed = GPIO_Speed_50MHz; 59 | GPIO_InitStruct_LED.GPIO_Mode = GPIO_Mode_Out_PP; 60 | GPIO_Init(LED_PORT, &GPIO_InitStruct_LED); 61 | for (;;) { 62 | GPIO_ResetBits(LED_PORT, LED_PIN); 63 | sleep_ms(500); 64 | GPIO_SetBits(LED_PORT, LED_PIN); 65 | sleep_ms(500); 66 | } 67 | GPIO_InitStruct_LED.GPIO_Pin = LED_PIN; 68 | GPIO_InitStruct_LED.GPIO_Speed = GPIO_Speed_10MHz; 69 | GPIO_InitStruct_LED.GPIO_Mode = GPIO_Mode_AIN; 70 | GPIO_Init(LED_PORT, &GPIO_InitStruct_LED); 71 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | \ 72 | RCC_APB2Periph_GPIOA | \ 73 | RCC_APB2Periph_GPIOB | \ 74 | RCC_APB2Periph_GPIOC | \ 75 | RCC_APB2Periph_GPIOD | \ 76 | RCC_APB2Periph_GPIOE | \ 77 | RCC_APB2Periph_GPIOF | \ 78 | RCC_APB2Periph_GPIOG, DISABLE); 79 | } 80 | 81 | 82 | #ifdef USE_FULL_ASSERT 83 | void assert_failed(uint8_t *file, uint32_t line) 84 | { 85 | DBG_OUT("assertion in %s:%04u", file, (unsigned int)line); 86 | for (;;); 87 | } 88 | #endif 89 | 90 | 91 | __attribute__((naked)) void HardFault_Handler(void) 92 | { 93 | __asm volatile 94 | ( 95 | " tst lr, #4 \n" 96 | " ite eq \n" 97 | " mrseq r0, msp \n" 98 | " mrsne r0, psp \n" 99 | " bl prvGetRegistersFromStack \n" 100 | ); 101 | for (;;); 102 | } 103 | 104 | 105 | void prvGetRegistersFromStack(unsigned int *pStack) 106 | { 107 | DBG_OUT("hard fault\n" 108 | "R0 = %08x\n" 109 | "R1 = %08x\n" 110 | "R2 = %08x\n" 111 | "R3 = %08x\n" 112 | "R12 = %08x\n" 113 | "LR = %08x\n" 114 | "PC = %08x\n" 115 | "PSR = %08x\n", 116 | pStack[0], pStack[1], pStack[2], pStack[3], 117 | pStack[4], pStack[5], pStack[6], pStack[7]); 118 | } 119 | 120 | 121 | void SystemHseFailed(void) 122 | { 123 | DBG_OUT("HSE failed"); 124 | for (;;); 125 | } 126 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/src/stm32f10x_crc.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_crc.c 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file provides all the CRC firmware functions. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm32f10x_crc.h" 30 | 31 | /** @addtogroup STM32F10x_StdPeriph_Driver 32 | * @{ 33 | */ 34 | 35 | /** @defgroup CRC 36 | * @brief CRC driver modules 37 | * @{ 38 | */ 39 | 40 | /** @defgroup CRC_Private_TypesDefinitions 41 | * @{ 42 | */ 43 | 44 | /** 45 | * @} 46 | */ 47 | 48 | /** @defgroup CRC_Private_Defines 49 | * @{ 50 | */ 51 | 52 | /** 53 | * @} 54 | */ 55 | 56 | /** @defgroup CRC_Private_Macros 57 | * @{ 58 | */ 59 | 60 | /** 61 | * @} 62 | */ 63 | 64 | /** @defgroup CRC_Private_Variables 65 | * @{ 66 | */ 67 | 68 | /** 69 | * @} 70 | */ 71 | 72 | /** @defgroup CRC_Private_FunctionPrototypes 73 | * @{ 74 | */ 75 | 76 | /** 77 | * @} 78 | */ 79 | 80 | /** @defgroup CRC_Private_Functions 81 | * @{ 82 | */ 83 | 84 | /** 85 | * @brief Resets the CRC Data register (DR). 86 | * @param None 87 | * @retval None 88 | */ 89 | void CRC_ResetDR(void) 90 | { 91 | /* Reset CRC generator */ 92 | CRC->CR = CRC_CR_RESET; 93 | } 94 | 95 | /** 96 | * @brief Computes the 32-bit CRC of a given data word(32-bit). 97 | * @param Data: data word(32-bit) to compute its CRC 98 | * @retval 32-bit CRC 99 | */ 100 | uint32_t CRC_CalcCRC(uint32_t Data) 101 | { 102 | CRC->DR = Data; 103 | 104 | return (CRC->DR); 105 | } 106 | 107 | /** 108 | * @brief Computes the 32-bit CRC of a given buffer of data word(32-bit). 109 | * @param pBuffer: pointer to the buffer containing the data to be computed 110 | * @param BufferLength: length of the buffer to be computed 111 | * @retval 32-bit CRC 112 | */ 113 | uint32_t CRC_CalcBlockCRC(uint32_t pBuffer[], uint32_t BufferLength) 114 | { 115 | uint32_t index = 0; 116 | 117 | for(index = 0; index < BufferLength; index++) 118 | { 119 | CRC->DR = pBuffer[index]; 120 | } 121 | return (CRC->DR); 122 | } 123 | 124 | /** 125 | * @brief Returns the current CRC value. 126 | * @param None 127 | * @retval 32-bit CRC 128 | */ 129 | uint32_t CRC_GetCRC(void) 130 | { 131 | return (CRC->DR); 132 | } 133 | 134 | /** 135 | * @brief Stores a 8-bit data in the Independent Data(ID) register. 136 | * @param IDValue: 8-bit value to be stored in the ID register 137 | * @retval None 138 | */ 139 | void CRC_SetIDRegister(uint8_t IDValue) 140 | { 141 | CRC->IDR = IDValue; 142 | } 143 | 144 | /** 145 | * @brief Returns the 8-bit data stored in the Independent Data(ID) register 146 | * @param None 147 | * @retval 8-bit value of the ID register 148 | */ 149 | uint8_t CRC_GetIDRegister(void) 150 | { 151 | return (CRC->IDR); 152 | } 153 | 154 | /** 155 | * @} 156 | */ 157 | 158 | /** 159 | * @} 160 | */ 161 | 162 | /** 163 | * @} 164 | */ 165 | 166 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 167 | -------------------------------------------------------------------------------- /tiny-AES-c/README.md: -------------------------------------------------------------------------------- 1 | ### Tiny AES in C 2 | 3 | This is a small and portable implementation of the AES [ECB](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_.28ECB.29), [CTR](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_.28CTR.29) and [CBC](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_Block_Chaining_.28CBC.29) encryption algorithms written in C. 4 | 5 | You can override the default key-size of 128 bit with 192 or 256 bit by defining the symbols AES192 or AES256 in `aes.h`. 6 | 7 | The API is very simple and looks like this (I am using C99 ``-style annotated types): 8 | 9 | ```C 10 | /* Initialize context calling one of: */ 11 | void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key); 12 | void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv); 13 | 14 | /* ... or reset IV at random point: */ 15 | void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv); 16 | 17 | /* Then start encrypting and decrypting with the functions below: */ 18 | void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf); 19 | void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf); 20 | 21 | void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 22 | void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 23 | 24 | /* Same function for encrypting as for decrypting in CTR mode */ 25 | void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 26 | ``` 27 | 28 | Note: 29 | * No padding is provided so for CBC and ECB all buffers should be multiples of 16 bytes. For padding [PKCS7](https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7) is recommendable. 30 | * ECB mode is considered unsafe for most uses and is not implemented in streaming mode. If you need this mode, call the function for every block of 16 bytes you need encrypted. See [wikipedia's article on ECB](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_(ECB)) for more details. 31 | 32 | You can choose to use any or all of the modes-of-operations, by defining the symbols CBC, CTR or ECB. See the header file for clarification. 33 | 34 | C++ users should `#include` [aes.hpp](https://github.com/kokke/tiny-AES-c/blob/master/aes.hpp) instead of [aes.h](https://github.com/kokke/tiny-AES-c/blob/master/aes.h) 35 | 36 | There is no built-in error checking or protection from out-of-bounds memory access errors as a result of malicious input. 37 | 38 | The module uses less than 200 bytes of RAM and 1-2K ROM when compiled for ARM, but YMMV depending on which modes are enabled. 39 | 40 | It is one of the smallest implementations in C I've seen yet, but do contact me if you know of something smaller (or have improvements to the code here). 41 | 42 | I've successfully used the code on 64bit x86, 32bit ARM and 8 bit AVR platforms. 43 | 44 | 45 | GCC size output when only CTR mode is compiled for ARM: 46 | 47 | $ arm-none-eabi-gcc -Os -DCBC=0 -DECB=0 -DCTR=1 -c aes.c 48 | $ size aes.o 49 | text data bss dec hex filename 50 | 1343 0 0 1343 53f aes.o 51 | 52 | .. and when compiling for the THUMB instruction set, we end up just below 1K in code size. 53 | 54 | $ arm-none-eabi-gcc -Os -mthumb -DCBC=0 -DECB=0 -DCTR=1 -c aes.c 55 | $ size aes.o 56 | text data bss dec hex filename 57 | 979 0 0 979 3d3 aes.o 58 | 59 | 60 | I am using the Free Software Foundation, ARM GCC compiler: 61 | 62 | $ arm-none-eabi-gcc --version 63 | arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 8-2018-q4-major) 8.2.1 20181213 (release) 64 | Copyright (C) 2018 Free Software Foundation, Inc. 65 | This is free software; see the source for copying conditions. There is NO 66 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 67 | 68 | 69 | 70 | This implementation is verified against the data in: 71 | 72 | [National Institute of Standards and Technology Special Publication 800-38A 2001 ED](http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf) Appendix F: Example Vectors for Modes of Operation of the AES. 73 | 74 | The other appendices in the document are valuable for implementation details on e.g. padding, generation of IVs and nonces in CTR-mode etc. 75 | 76 | 77 | A heartfelt thank-you to all the nice people out there who have contributed to this project. 78 | 79 | 80 | All material in this repository is in the public domain. 81 | -------------------------------------------------------------------------------- /stm/STM32_USB-FS-Device_Driver/inc/usb_def.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_def.h 4 | * @author MCD Application Team 5 | * @version V4.1.0 6 | * @date 26-May-2017 7 | * @brief Definitions related to USB Core 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT(c) 2017 STMicroelectronics

12 | * 13 | * Redistribution and use in source and binary forms, with or without modification, 14 | * are permitted provided that the following conditions are met: 15 | * 1. Redistributions of source code must retain the above copyright notice, 16 | * this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, 18 | * this list of conditions and the following disclaimer in the documentation 19 | * and/or other materials provided with the distribution. 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software 22 | * without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | * 35 | ****************************************************************************** 36 | */ 37 | 38 | /* Define to prevent recursive inclusion -------------------------------------*/ 39 | #ifndef __USB_DEF_H 40 | #define __USB_DEF_H 41 | 42 | /* Includes ------------------------------------------------------------------*/ 43 | /* Exported types ------------------------------------------------------------*/ 44 | typedef enum _RECIPIENT_TYPE 45 | { 46 | DEVICE_RECIPIENT, /* Recipient device */ 47 | INTERFACE_RECIPIENT, /* Recipient interface */ 48 | ENDPOINT_RECIPIENT, /* Recipient endpoint */ 49 | OTHER_RECIPIENT 50 | } RECIPIENT_TYPE; 51 | 52 | 53 | typedef enum _STANDARD_REQUESTS 54 | { 55 | GET_STATUS = 0, 56 | CLEAR_FEATURE, 57 | RESERVED1, 58 | SET_FEATURE, 59 | RESERVED2, 60 | SET_ADDRESS, 61 | GET_DESCRIPTOR, 62 | SET_DESCRIPTOR, 63 | GET_CONFIGURATION, 64 | SET_CONFIGURATION, 65 | GET_INTERFACE, 66 | SET_INTERFACE, 67 | TOTAL_sREQUEST, /* Total number of Standard request */ 68 | SYNCH_FRAME = 12 69 | } STANDARD_REQUESTS; 70 | 71 | /* Definition of "USBwValue" */ 72 | typedef enum _DESCRIPTOR_TYPE 73 | { 74 | DEVICE_DESCRIPTOR = 1, 75 | CONFIG_DESCRIPTOR, 76 | STRING_DESCRIPTOR, 77 | INTERFACE_DESCRIPTOR, 78 | ENDPOINT_DESCRIPTOR, 79 | DEVICE_BOS_DESCRIPTOR = 0xF 80 | } DESCRIPTOR_TYPE; 81 | 82 | /* Feature selector of a SET_FEATURE or CLEAR_FEATURE */ 83 | typedef enum _FEATURE_SELECTOR 84 | { 85 | ENDPOINT_STALL, 86 | DEVICE_REMOTE_WAKEUP 87 | } FEATURE_SELECTOR; 88 | 89 | /* Exported constants --------------------------------------------------------*/ 90 | /* Definition of "USBbmRequestType" */ 91 | #define REQUEST_TYPE 0x60 /* Mask to get request type */ 92 | #define STANDARD_REQUEST 0x00 /* Standard request */ 93 | #define CLASS_REQUEST 0x20 /* Class request */ 94 | #define VENDOR_REQUEST 0x40 /* Vendor request */ 95 | 96 | #define RECIPIENT 0x1F /* Mask to get recipient */ 97 | 98 | /* Exported macro ------------------------------------------------------------*/ 99 | /* Exported functions ------------------------------------------------------- */ 100 | 101 | #endif /* __USB_DEF_H */ 102 | 103 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 104 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/inc/stm32f10x_dbgmcu.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_dbgmcu.h 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file contains all the functions prototypes for the DBGMCU 8 | * firmware library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | *

© COPYRIGHT 2012 STMicroelectronics

13 | * 14 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 15 | * You may not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at: 17 | * 18 | * http://www.st.com/software_license_agreement_liberty_v2 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, 22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | ****************************************************************************** 27 | */ 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __STM32F10x_DBGMCU_H 31 | #define __STM32F10x_DBGMCU_H 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | /* Includes ------------------------------------------------------------------*/ 38 | #include "stm32f10x.h" 39 | 40 | /** @addtogroup STM32F10x_StdPeriph_Driver 41 | * @{ 42 | */ 43 | 44 | /** @addtogroup DBGMCU 45 | * @{ 46 | */ 47 | 48 | /** @defgroup DBGMCU_Exported_Types 49 | * @{ 50 | */ 51 | 52 | /** 53 | * @} 54 | */ 55 | 56 | /** @defgroup DBGMCU_Exported_Constants 57 | * @{ 58 | */ 59 | 60 | #define DBGMCU_SLEEP ((uint32_t)0x00000001) 61 | #define DBGMCU_STOP ((uint32_t)0x00000002) 62 | #define DBGMCU_STANDBY ((uint32_t)0x00000004) 63 | #define DBGMCU_IWDG_STOP ((uint32_t)0x00000100) 64 | #define DBGMCU_WWDG_STOP ((uint32_t)0x00000200) 65 | #define DBGMCU_TIM1_STOP ((uint32_t)0x00000400) 66 | #define DBGMCU_TIM2_STOP ((uint32_t)0x00000800) 67 | #define DBGMCU_TIM3_STOP ((uint32_t)0x00001000) 68 | #define DBGMCU_TIM4_STOP ((uint32_t)0x00002000) 69 | #define DBGMCU_CAN1_STOP ((uint32_t)0x00004000) 70 | #define DBGMCU_I2C1_SMBUS_TIMEOUT ((uint32_t)0x00008000) 71 | #define DBGMCU_I2C2_SMBUS_TIMEOUT ((uint32_t)0x00010000) 72 | #define DBGMCU_TIM8_STOP ((uint32_t)0x00020000) 73 | #define DBGMCU_TIM5_STOP ((uint32_t)0x00040000) 74 | #define DBGMCU_TIM6_STOP ((uint32_t)0x00080000) 75 | #define DBGMCU_TIM7_STOP ((uint32_t)0x00100000) 76 | #define DBGMCU_CAN2_STOP ((uint32_t)0x00200000) 77 | #define DBGMCU_TIM15_STOP ((uint32_t)0x00400000) 78 | #define DBGMCU_TIM16_STOP ((uint32_t)0x00800000) 79 | #define DBGMCU_TIM17_STOP ((uint32_t)0x01000000) 80 | #define DBGMCU_TIM12_STOP ((uint32_t)0x02000000) 81 | #define DBGMCU_TIM13_STOP ((uint32_t)0x04000000) 82 | #define DBGMCU_TIM14_STOP ((uint32_t)0x08000000) 83 | #define DBGMCU_TIM9_STOP ((uint32_t)0x10000000) 84 | #define DBGMCU_TIM10_STOP ((uint32_t)0x20000000) 85 | #define DBGMCU_TIM11_STOP ((uint32_t)0x40000000) 86 | 87 | #define IS_DBGMCU_PERIPH(PERIPH) ((((PERIPH) & 0x800000F8) == 0x00) && ((PERIPH) != 0x00)) 88 | /** 89 | * @} 90 | */ 91 | 92 | /** @defgroup DBGMCU_Exported_Macros 93 | * @{ 94 | */ 95 | 96 | /** 97 | * @} 98 | */ 99 | 100 | /** @defgroup DBGMCU_Exported_Functions 101 | * @{ 102 | */ 103 | 104 | uint32_t DBGMCU_GetREVID(void); 105 | uint32_t DBGMCU_GetDEVID(void); 106 | void DBGMCU_Config(uint32_t DBGMCU_Periph, FunctionalState NewState); 107 | 108 | #ifdef __cplusplus 109 | } 110 | #endif 111 | 112 | #endif /* __STM32F10x_DBGMCU_H */ 113 | /** 114 | * @} 115 | */ 116 | 117 | /** 118 | * @} 119 | */ 120 | 121 | /** 122 | * @} 123 | */ 124 | 125 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 126 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # STM32 AES Bootloader 2 | 3 | ## YouTube demo 4 | https://www.youtube.com/watch?v=qMqJ9Rkrz3s 5 | 6 | ## Software description 7 | 8 | That's very easy to protect commercial embedded software from copying - just set flash lock bit on your MCU. But there is open question during maintain: How to deliver software updates caused by implementing new features or bug fixing? In that case Bootloader is used, it's small application which is started after MCU start and somehow able to update flash with new main application and start running application. 9 | 10 | Actually at customer side exists 2 problem: 11 | 12 | - SW/HW equipment to make flash procedure 13 | - Show application to customer (flash lock was initially used) 14 | 15 | That software is solving both issues. 16 | 17 | - In case of flashing HW/SW is used internal MCU USB port which provides CDC (virtual serial port). In that case customer just needs USB cable and standard serial port terminal software for example minicom. 18 | 19 | - Flash .hex file is encrypted before delivery. Bootloader knows how to decrypt it and flash into MCU. (On MCU flash lock is used so for customer there is no access to bootloader code and decrypted application code). 20 | 21 | To encrypt application .hex file the **AES-128** algorithm is used in **CTR mode**. Encrypted file seems as valid .hex file but all memory content is encrypted so it's possible to flash it but it will do nothing. 22 | 23 | All software are compileable under Linux, see https://github.com/andriy-bilynskyy/STM32-Linux for details. 24 | 25 | For debug prints semihosting is used (SEGGER RTT seems better but bootloader is used for commercial software in that case I avoid using non fully free software.) 26 | 27 | For demonstration cheap **Blue-Pill** STM32 board is used https://www.aliexpress.com/item/32792513237.html. The software can be adopted to use another hardware (Just configuration for another stm32f1x MCUs, replacing HAL for another MCUs). 28 | 29 | ## Software components 30 | 31 | - STM32F10x standard peripheral library. https://www.st.com/en/embedded-software/stsw-stm32054.html 32 | 33 | - STM32F10x, STM32L1xx and STM32F3xx USB full speed device library. https://www.st.com/en/embedded-software/stsw-stm32121.html (Also includes standard peripheral library.) 34 | 35 | - tiny-AES-c. https://github.com/kokke/tiny-AES-c 36 | 37 | - FreeRTOS+CLI https://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_CLI/FreeRTOS_Plus_Command_Line_Interface.html (Used only cli files. To make it compileable created FreeRTOS stubs.) 38 | 39 | ## How to use 40 | 41 | Update AES secret (for testing you can use existing) 42 | 43 | (cd keys/ && ./keygen.sh) 44 | 45 | Compile bootloder 46 | 47 | make clean build 48 | 49 | Connect your board using j-tag and flash bootloder 50 | 51 | make erase 52 | make flash 53 | 54 | Disconnect j-tag and power cycle your board (!that's very important! Flash protection is set at this stage. STM has strange behavior when protection is set with inserted j-tag.) 55 | 56 | Enter bootloader: set BOOT1 pin to "1" (BOOT0 as usual to "0"), reset MCU, attach USB cable. Green LED on board should switch on which indicates CDC connection. On PC in /dev folder should appear new serial device **ttyACM0** (can be another in your case). 57 | 58 | Open serial terminal and check available commands by issue **help** command. 59 | 60 | minicom --device /dev/ttyACM0 61 | 62 | In terninal: 63 | 64 | help 65 | 66 | help: 67 | Lists all the registered commands 68 | 69 | info: 70 | Show chip info 71 | 72 | erase: 73 | Erase application 74 | 75 | program 76 | Program application 77 | 78 | status 79 | Program status 80 | 81 | info 82 | Boot Loader: 0x8000000 - 0x8003a03 (14852 bytes) 83 | Application: 0x8003c00 - 0x800ffff (50176 bytes) 84 | Page size: 1024 bytes 85 | 86 | *Exit terminal and start with test application downloading* 87 | 88 | Compile test application 89 | 90 | (cd test_app/ && make clean build) 91 | 92 | Encrypt application 93 | 94 | (cd encrypter/ && make clean build run) 95 | 96 | As result you have encrypted application **test_app/stm32-app.hex.enc** path from project root. 97 | 98 | Start serial terminal again and issue command **program** 99 | 100 | program 101 | --------------------------------------------- 102 | Now send your .hex file as ASCII. 103 | 104 | Send encrypted file (Ctrl+A S for minicom). 105 | 106 | When file sent check flashing status by command **status** 107 | 108 | status 109 | Program OK! 110 | 111 | Run application: set BOOT1 pin to "0" (BOOT0 as usual to "0"), reset MCU. Green LED on board should start blinking as described in simple test application. 112 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/inc/stm32f10x_iwdg.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_iwdg.h 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file contains all the functions prototypes for the IWDG 8 | * firmware library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | *

© COPYRIGHT 2012 STMicroelectronics

13 | * 14 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 15 | * You may not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at: 17 | * 18 | * http://www.st.com/software_license_agreement_liberty_v2 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, 22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | ****************************************************************************** 27 | */ 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __STM32F10x_IWDG_H 31 | #define __STM32F10x_IWDG_H 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | /* Includes ------------------------------------------------------------------*/ 38 | #include "stm32f10x.h" 39 | 40 | /** @addtogroup STM32F10x_StdPeriph_Driver 41 | * @{ 42 | */ 43 | 44 | /** @addtogroup IWDG 45 | * @{ 46 | */ 47 | 48 | /** @defgroup IWDG_Exported_Types 49 | * @{ 50 | */ 51 | 52 | /** 53 | * @} 54 | */ 55 | 56 | /** @defgroup IWDG_Exported_Constants 57 | * @{ 58 | */ 59 | 60 | /** @defgroup IWDG_WriteAccess 61 | * @{ 62 | */ 63 | 64 | #define IWDG_WriteAccess_Enable ((uint16_t)0x5555) 65 | #define IWDG_WriteAccess_Disable ((uint16_t)0x0000) 66 | #define IS_IWDG_WRITE_ACCESS(ACCESS) (((ACCESS) == IWDG_WriteAccess_Enable) || \ 67 | ((ACCESS) == IWDG_WriteAccess_Disable)) 68 | /** 69 | * @} 70 | */ 71 | 72 | /** @defgroup IWDG_prescaler 73 | * @{ 74 | */ 75 | 76 | #define IWDG_Prescaler_4 ((uint8_t)0x00) 77 | #define IWDG_Prescaler_8 ((uint8_t)0x01) 78 | #define IWDG_Prescaler_16 ((uint8_t)0x02) 79 | #define IWDG_Prescaler_32 ((uint8_t)0x03) 80 | #define IWDG_Prescaler_64 ((uint8_t)0x04) 81 | #define IWDG_Prescaler_128 ((uint8_t)0x05) 82 | #define IWDG_Prescaler_256 ((uint8_t)0x06) 83 | #define IS_IWDG_PRESCALER(PRESCALER) (((PRESCALER) == IWDG_Prescaler_4) || \ 84 | ((PRESCALER) == IWDG_Prescaler_8) || \ 85 | ((PRESCALER) == IWDG_Prescaler_16) || \ 86 | ((PRESCALER) == IWDG_Prescaler_32) || \ 87 | ((PRESCALER) == IWDG_Prescaler_64) || \ 88 | ((PRESCALER) == IWDG_Prescaler_128)|| \ 89 | ((PRESCALER) == IWDG_Prescaler_256)) 90 | /** 91 | * @} 92 | */ 93 | 94 | /** @defgroup IWDG_Flag 95 | * @{ 96 | */ 97 | 98 | #define IWDG_FLAG_PVU ((uint16_t)0x0001) 99 | #define IWDG_FLAG_RVU ((uint16_t)0x0002) 100 | #define IS_IWDG_FLAG(FLAG) (((FLAG) == IWDG_FLAG_PVU) || ((FLAG) == IWDG_FLAG_RVU)) 101 | #define IS_IWDG_RELOAD(RELOAD) ((RELOAD) <= 0xFFF) 102 | /** 103 | * @} 104 | */ 105 | 106 | /** 107 | * @} 108 | */ 109 | 110 | /** @defgroup IWDG_Exported_Macros 111 | * @{ 112 | */ 113 | 114 | /** 115 | * @} 116 | */ 117 | 118 | /** @defgroup IWDG_Exported_Functions 119 | * @{ 120 | */ 121 | 122 | void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess); 123 | void IWDG_SetPrescaler(uint8_t IWDG_Prescaler); 124 | void IWDG_SetReload(uint16_t Reload); 125 | void IWDG_ReloadCounter(void); 126 | void IWDG_Enable(void); 127 | FlagStatus IWDG_GetFlagStatus(uint16_t IWDG_FLAG); 128 | 129 | #ifdef __cplusplus 130 | } 131 | #endif 132 | 133 | #endif /* __STM32F10x_IWDG_H */ 134 | /** 135 | * @} 136 | */ 137 | 138 | /** 139 | * @} 140 | */ 141 | 142 | /** 143 | * @} 144 | */ 145 | 146 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 147 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/inc/stm32f10x_rtc.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_rtc.h 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file contains all the functions prototypes for the RTC firmware 8 | * library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | *

© COPYRIGHT 2012 STMicroelectronics

13 | * 14 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 15 | * You may not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at: 17 | * 18 | * http://www.st.com/software_license_agreement_liberty_v2 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, 22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | ****************************************************************************** 27 | */ 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __STM32F10x_RTC_H 31 | #define __STM32F10x_RTC_H 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | /* Includes ------------------------------------------------------------------*/ 38 | #include "stm32f10x.h" 39 | 40 | /** @addtogroup STM32F10x_StdPeriph_Driver 41 | * @{ 42 | */ 43 | 44 | /** @addtogroup RTC 45 | * @{ 46 | */ 47 | 48 | /** @defgroup RTC_Exported_Types 49 | * @{ 50 | */ 51 | 52 | /** 53 | * @} 54 | */ 55 | 56 | /** @defgroup RTC_Exported_Constants 57 | * @{ 58 | */ 59 | 60 | /** @defgroup RTC_interrupts_define 61 | * @{ 62 | */ 63 | 64 | #define RTC_IT_OW ((uint16_t)0x0004) /*!< Overflow interrupt */ 65 | #define RTC_IT_ALR ((uint16_t)0x0002) /*!< Alarm interrupt */ 66 | #define RTC_IT_SEC ((uint16_t)0x0001) /*!< Second interrupt */ 67 | #define IS_RTC_IT(IT) ((((IT) & (uint16_t)0xFFF8) == 0x00) && ((IT) != 0x00)) 68 | #define IS_RTC_GET_IT(IT) (((IT) == RTC_IT_OW) || ((IT) == RTC_IT_ALR) || \ 69 | ((IT) == RTC_IT_SEC)) 70 | /** 71 | * @} 72 | */ 73 | 74 | /** @defgroup RTC_interrupts_flags 75 | * @{ 76 | */ 77 | 78 | #define RTC_FLAG_RTOFF ((uint16_t)0x0020) /*!< RTC Operation OFF flag */ 79 | #define RTC_FLAG_RSF ((uint16_t)0x0008) /*!< Registers Synchronized flag */ 80 | #define RTC_FLAG_OW ((uint16_t)0x0004) /*!< Overflow flag */ 81 | #define RTC_FLAG_ALR ((uint16_t)0x0002) /*!< Alarm flag */ 82 | #define RTC_FLAG_SEC ((uint16_t)0x0001) /*!< Second flag */ 83 | #define IS_RTC_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0xFFF0) == 0x00) && ((FLAG) != 0x00)) 84 | #define IS_RTC_GET_FLAG(FLAG) (((FLAG) == RTC_FLAG_RTOFF) || ((FLAG) == RTC_FLAG_RSF) || \ 85 | ((FLAG) == RTC_FLAG_OW) || ((FLAG) == RTC_FLAG_ALR) || \ 86 | ((FLAG) == RTC_FLAG_SEC)) 87 | #define IS_RTC_PRESCALER(PRESCALER) ((PRESCALER) <= 0xFFFFF) 88 | 89 | /** 90 | * @} 91 | */ 92 | 93 | /** 94 | * @} 95 | */ 96 | 97 | /** @defgroup RTC_Exported_Macros 98 | * @{ 99 | */ 100 | 101 | /** 102 | * @} 103 | */ 104 | 105 | /** @defgroup RTC_Exported_Functions 106 | * @{ 107 | */ 108 | 109 | void RTC_ITConfig(uint16_t RTC_IT, FunctionalState NewState); 110 | void RTC_EnterConfigMode(void); 111 | void RTC_ExitConfigMode(void); 112 | uint32_t RTC_GetCounter(void); 113 | void RTC_SetCounter(uint32_t CounterValue); 114 | void RTC_SetPrescaler(uint32_t PrescalerValue); 115 | void RTC_SetAlarm(uint32_t AlarmValue); 116 | uint32_t RTC_GetDivider(void); 117 | void RTC_WaitForLastTask(void); 118 | void RTC_WaitForSynchro(void); 119 | FlagStatus RTC_GetFlagStatus(uint16_t RTC_FLAG); 120 | void RTC_ClearFlag(uint16_t RTC_FLAG); 121 | ITStatus RTC_GetITStatus(uint16_t RTC_IT); 122 | void RTC_ClearITPendingBit(uint16_t RTC_IT); 123 | 124 | #ifdef __cplusplus 125 | } 126 | #endif 127 | 128 | #endif /* __STM32F10x_RTC_H */ 129 | /** 130 | * @} 131 | */ 132 | 133 | /** 134 | * @} 135 | */ 136 | 137 | /** 138 | * @} 139 | */ 140 | 141 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 142 | -------------------------------------------------------------------------------- /stm/STM32_USB-FS-Device_Driver/src/usb_init.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_init.c 4 | * @author MCD Application Team 5 | * @version V4.1.0 6 | * @date 26-May-2017 7 | * @brief Initialization routines & global variables 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© Copyright (c) 2017 STMicroelectronics International N.V. 12 | * All rights reserved.

13 | * 14 | * Redistribution and use in source and binary forms, with or without 15 | * modification, are permitted, provided that the following conditions are met: 16 | * 17 | * 1. Redistribution of source code must retain the above copyright notice, 18 | * this list of conditions and the following disclaimer. 19 | * 2. Redistributions in binary form must reproduce the above copyright notice, 20 | * this list of conditions and the following disclaimer in the documentation 21 | * and/or other materials provided with the distribution. 22 | * 3. Neither the name of STMicroelectronics nor the names of other 23 | * contributors to this software may be used to endorse or promote products 24 | * derived from this software without specific written permission. 25 | * 4. This software, including modifications and/or derivative works of this 26 | * software, must execute solely and exclusively on microcontroller or 27 | * microprocessor devices manufactured by or for STMicroelectronics. 28 | * 5. Redistribution and use of this software other than as permitted under 29 | * this license is void and will automatically terminate your rights under 30 | * this license. 31 | * 32 | * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" 33 | * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT 34 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 35 | * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY 36 | * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT 37 | * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 38 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 39 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 40 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 41 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 42 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 43 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 44 | * 45 | ****************************************************************************** 46 | */ 47 | 48 | 49 | /* Includes ------------------------------------------------------------------*/ 50 | #include "usb_lib.h" 51 | 52 | /* Private typedef -----------------------------------------------------------*/ 53 | /* Private define ------------------------------------------------------------*/ 54 | /* Private macro -------------------------------------------------------------*/ 55 | /* Private variables ---------------------------------------------------------*/ 56 | /* The number of current endpoint, it will be used to specify an endpoint */ 57 | uint8_t EPindex; 58 | /* The number of current device, it is an index to the Device_Table */ 59 | /* uint8_t Device_no; */ 60 | /* Points to the DEVICE_INFO structure of current device */ 61 | /* The purpose of this register is to speed up the execution */ 62 | DEVICE_INFO *pInformation; 63 | /* Points to the DEVICE_PROP structure of current device */ 64 | /* The purpose of this register is to speed up the execution */ 65 | DEVICE_PROP *pProperty; 66 | /* Temporary save the state of Rx & Tx status. */ 67 | /* Whenever the Rx or Tx state is changed, its value is saved */ 68 | /* in this variable first and will be set to the EPRB or EPRA */ 69 | /* at the end of interrupt process */ 70 | uint16_t SaveState ; 71 | uint16_t wInterrupt_Mask; 72 | DEVICE_INFO Device_Info; 73 | USER_STANDARD_REQUESTS *pUser_Standard_Requests; 74 | 75 | /* Extern variables ----------------------------------------------------------*/ 76 | /* Private function prototypes -----------------------------------------------*/ 77 | /* Private functions ---------------------------------------------------------*/ 78 | 79 | /** 80 | * Function Name : USB_Init 81 | * Description : USB system initialization 82 | * Input : None. 83 | * Output : None. 84 | * Return : None. 85 | **/ 86 | void USB_Init(void) 87 | { 88 | pInformation = &Device_Info; 89 | pInformation->ControlState = 2; 90 | pProperty = &Device_Property; 91 | pUser_Standard_Requests = &User_Standard_Requests; 92 | /* Initialize devices one by one */ 93 | pProperty->Init(); 94 | } 95 | 96 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 97 | -------------------------------------------------------------------------------- /FreeRTOS-Plus-CLI/FreeRTOS_CLI.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS+CLI V1.0.4 3 | * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | #ifndef COMMAND_INTERPRETER_H 29 | #define COMMAND_INTERPRETER_H 30 | 31 | 32 | /* The prototype to which callback functions used to process command line 33 | commands must comply. pcWriteBuffer is a buffer into which the output from 34 | executing the command can be written, xWriteBufferLen is the length, in bytes of 35 | the pcWriteBuffer buffer, and pcCommandString is the entire string as input by 36 | the user (from which parameters can be extracted).*/ 37 | typedef BaseType_t (*pdCOMMAND_LINE_CALLBACK)( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString ); 38 | 39 | /* The structure that defines command line commands. A command line command 40 | should be defined by declaring a const structure of this type. */ 41 | typedef struct xCOMMAND_LINE_INPUT 42 | { 43 | const char * const pcCommand; /* The command that causes pxCommandInterpreter to be executed. For example "help". Must be all lower case. */ 44 | const char * const pcHelpString; /* String that describes how to use the command. Should start with the command itself, and end with "\r\n". For example "help: Returns a list of all the commands\r\n". */ 45 | const pdCOMMAND_LINE_CALLBACK pxCommandInterpreter; /* A pointer to the callback function that will return the output generated by the command. */ 46 | int8_t cExpectedNumberOfParameters; /* Commands expect a fixed number of parameters, which may be zero. */ 47 | } CLI_Command_Definition_t; 48 | 49 | /* For backward compatibility. */ 50 | #define xCommandLineInput CLI_Command_Definition_t 51 | 52 | /* 53 | * Register the command passed in using the pxCommandToRegister parameter. 54 | * Registering a command adds the command to the list of commands that are 55 | * handled by the command interpreter. Once a command has been registered it 56 | * can be executed from the command line. 57 | */ 58 | BaseType_t FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister ); 59 | 60 | /* 61 | * Runs the command interpreter for the command string "pcCommandInput". Any 62 | * output generated by running the command will be placed into pcWriteBuffer. 63 | * xWriteBufferLen must indicate the size, in bytes, of the buffer pointed to 64 | * by pcWriteBuffer. 65 | * 66 | * FreeRTOS_CLIProcessCommand should be called repeatedly until it returns pdFALSE. 67 | * 68 | * pcCmdIntProcessCommand is not reentrant. It must not be called from more 69 | * than one task - or at least - by more than one task at a time. 70 | */ 71 | BaseType_t FreeRTOS_CLIProcessCommand( const char * const pcCommandInput, char * pcWriteBuffer, size_t xWriteBufferLen ); 72 | 73 | /*-----------------------------------------------------------*/ 74 | 75 | /* 76 | * A buffer into which command outputs can be written is declared in the 77 | * main command interpreter, rather than in the command console implementation, 78 | * to allow application that provide access to the command console via multiple 79 | * interfaces to share a buffer, and therefore save RAM. Note, however, that 80 | * the command interpreter itself is not re-entrant, so only one command 81 | * console interface can be used at any one time. For that reason, no attempt 82 | * is made to provide any mutual exclusion mechanism on the output buffer. 83 | * 84 | * FreeRTOS_CLIGetOutputBuffer() returns the address of the output buffer. 85 | */ 86 | char *FreeRTOS_CLIGetOutputBuffer( void ); 87 | 88 | /* 89 | * Return a pointer to the xParameterNumber'th word in pcCommandString. 90 | */ 91 | const char *FreeRTOS_CLIGetParameter( const char *pcCommandString, UBaseType_t uxWantedParameter, BaseType_t *pxParameterStringLength ); 92 | 93 | #endif /* COMMAND_INTERPRETER_H */ 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/inc/stm32f10x_pwr.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_pwr.h 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file contains all the functions prototypes for the PWR firmware 8 | * library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | *

© COPYRIGHT 2012 STMicroelectronics

13 | * 14 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 15 | * You may not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at: 17 | * 18 | * http://www.st.com/software_license_agreement_liberty_v2 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, 22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | ****************************************************************************** 27 | */ 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __STM32F10x_PWR_H 31 | #define __STM32F10x_PWR_H 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | /* Includes ------------------------------------------------------------------*/ 38 | #include "stm32f10x.h" 39 | 40 | /** @addtogroup STM32F10x_StdPeriph_Driver 41 | * @{ 42 | */ 43 | 44 | /** @addtogroup PWR 45 | * @{ 46 | */ 47 | 48 | /** @defgroup PWR_Exported_Types 49 | * @{ 50 | */ 51 | 52 | /** 53 | * @} 54 | */ 55 | 56 | /** @defgroup PWR_Exported_Constants 57 | * @{ 58 | */ 59 | 60 | /** @defgroup PVD_detection_level 61 | * @{ 62 | */ 63 | 64 | #define PWR_PVDLevel_2V2 ((uint32_t)0x00000000) 65 | #define PWR_PVDLevel_2V3 ((uint32_t)0x00000020) 66 | #define PWR_PVDLevel_2V4 ((uint32_t)0x00000040) 67 | #define PWR_PVDLevel_2V5 ((uint32_t)0x00000060) 68 | #define PWR_PVDLevel_2V6 ((uint32_t)0x00000080) 69 | #define PWR_PVDLevel_2V7 ((uint32_t)0x000000A0) 70 | #define PWR_PVDLevel_2V8 ((uint32_t)0x000000C0) 71 | #define PWR_PVDLevel_2V9 ((uint32_t)0x000000E0) 72 | #define IS_PWR_PVD_LEVEL(LEVEL) (((LEVEL) == PWR_PVDLevel_2V2) || ((LEVEL) == PWR_PVDLevel_2V3)|| \ 73 | ((LEVEL) == PWR_PVDLevel_2V4) || ((LEVEL) == PWR_PVDLevel_2V5)|| \ 74 | ((LEVEL) == PWR_PVDLevel_2V6) || ((LEVEL) == PWR_PVDLevel_2V7)|| \ 75 | ((LEVEL) == PWR_PVDLevel_2V8) || ((LEVEL) == PWR_PVDLevel_2V9)) 76 | /** 77 | * @} 78 | */ 79 | 80 | /** @defgroup Regulator_state_is_STOP_mode 81 | * @{ 82 | */ 83 | 84 | #define PWR_Regulator_ON ((uint32_t)0x00000000) 85 | #define PWR_Regulator_LowPower ((uint32_t)0x00000001) 86 | #define IS_PWR_REGULATOR(REGULATOR) (((REGULATOR) == PWR_Regulator_ON) || \ 87 | ((REGULATOR) == PWR_Regulator_LowPower)) 88 | /** 89 | * @} 90 | */ 91 | 92 | /** @defgroup STOP_mode_entry 93 | * @{ 94 | */ 95 | 96 | #define PWR_STOPEntry_WFI ((uint8_t)0x01) 97 | #define PWR_STOPEntry_WFE ((uint8_t)0x02) 98 | #define IS_PWR_STOP_ENTRY(ENTRY) (((ENTRY) == PWR_STOPEntry_WFI) || ((ENTRY) == PWR_STOPEntry_WFE)) 99 | 100 | /** 101 | * @} 102 | */ 103 | 104 | /** @defgroup PWR_Flag 105 | * @{ 106 | */ 107 | 108 | #define PWR_FLAG_WU ((uint32_t)0x00000001) 109 | #define PWR_FLAG_SB ((uint32_t)0x00000002) 110 | #define PWR_FLAG_PVDO ((uint32_t)0x00000004) 111 | #define IS_PWR_GET_FLAG(FLAG) (((FLAG) == PWR_FLAG_WU) || ((FLAG) == PWR_FLAG_SB) || \ 112 | ((FLAG) == PWR_FLAG_PVDO)) 113 | 114 | #define IS_PWR_CLEAR_FLAG(FLAG) (((FLAG) == PWR_FLAG_WU) || ((FLAG) == PWR_FLAG_SB)) 115 | /** 116 | * @} 117 | */ 118 | 119 | /** 120 | * @} 121 | */ 122 | 123 | /** @defgroup PWR_Exported_Macros 124 | * @{ 125 | */ 126 | 127 | /** 128 | * @} 129 | */ 130 | 131 | /** @defgroup PWR_Exported_Functions 132 | * @{ 133 | */ 134 | 135 | void PWR_DeInit(void); 136 | void PWR_BackupAccessCmd(FunctionalState NewState); 137 | void PWR_PVDCmd(FunctionalState NewState); 138 | void PWR_PVDLevelConfig(uint32_t PWR_PVDLevel); 139 | void PWR_WakeUpPinCmd(FunctionalState NewState); 140 | void PWR_EnterSTOPMode(uint32_t PWR_Regulator, uint8_t PWR_STOPEntry); 141 | void PWR_EnterSTANDBYMode(void); 142 | FlagStatus PWR_GetFlagStatus(uint32_t PWR_FLAG); 143 | void PWR_ClearFlag(uint32_t PWR_FLAG); 144 | 145 | #ifdef __cplusplus 146 | } 147 | #endif 148 | 149 | #endif /* __STM32F10x_PWR_H */ 150 | /** 151 | * @} 152 | */ 153 | 154 | /** 155 | * @} 156 | */ 157 | 158 | /** 159 | * @} 160 | */ 161 | 162 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 163 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************** 3 | ** 4 | ** (C) 2023 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ******************************************************************************** 9 | */ 10 | 11 | #include 12 | #include "bl_conf.h" 13 | #include 14 | #include 15 | #include 16 | #include "cli.h" 17 | 18 | 19 | __attribute__((naked)) void jump_2_app(uint32_t addr) 20 | { 21 | __disable_irq(); 22 | SCB->VTOR = addr; 23 | __asm volatile( 24 | "ldr sp, [r0] \n" 25 | "ldr pc, [r0, #4] \n" 26 | ); 27 | } 28 | 29 | 30 | int main(void) 31 | { 32 | DBG_INIT(); 33 | DBG_OUT("main() start"); 34 | FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); 35 | FLASH_SetLatency(FLASH_Latency_2); 36 | #ifndef DEBUG 37 | if (FLASH_GetReadOutProtectionStatus() == RESET) { 38 | FLASH_Unlock(); 39 | FLASH_ReadOutProtection(ENABLE); 40 | FLASH_Lock(); 41 | } 42 | #endif 43 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | \ 44 | RCC_APB2Periph_GPIOA | \ 45 | RCC_APB2Periph_GPIOB | \ 46 | RCC_APB2Periph_GPIOC | \ 47 | RCC_APB2Periph_GPIOD | \ 48 | RCC_APB2Periph_GPIOE | \ 49 | RCC_APB2Periph_GPIOF | \ 50 | RCC_APB2Periph_GPIOG, ENABLE); 51 | GPIO_InitTypeDef GPIO_InitStruct_JMP; 52 | GPIO_InitStruct_JMP.GPIO_Pin = BL_PIN; 53 | GPIO_InitStruct_JMP.GPIO_Speed = GPIO_Speed_10MHz; 54 | GPIO_InitStruct_JMP.GPIO_Mode = GPIO_Mode_IN_FLOATING; 55 | GPIO_Init(BL_PORT, &GPIO_InitStruct_JMP); 56 | if (GPIO_ReadInputDataBit(BL_PORT, BL_PIN) == Bit_RESET) { 57 | GPIO_InitStruct_JMP.GPIO_Mode = GPIO_Mode_AIN; 58 | GPIO_Init(BL_PORT, &GPIO_InitStruct_JMP); 59 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | \ 60 | RCC_APB2Periph_GPIOA | \ 61 | RCC_APB2Periph_GPIOB | \ 62 | RCC_APB2Periph_GPIOC | \ 63 | RCC_APB2Periph_GPIOD | \ 64 | RCC_APB2Periph_GPIOE | \ 65 | RCC_APB2Periph_GPIOF | \ 66 | RCC_APB2Periph_GPIOG, DISABLE); 67 | jump_2_app(app_adderess()); 68 | } 69 | NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); 70 | usb_cdc_start(); 71 | LED_INIT(); 72 | cli_init(); 73 | char command_buf[523]; 74 | uint16_t command_buf_pos = 0; 75 | for (;;) { 76 | if (usb_cdc_is_ready()) { 77 | LED_STATE(1); 78 | char ch = usb_cdc_get_char(); 79 | if (ch) { 80 | switch (ch) { 81 | case '\r': 82 | command_buf[command_buf_pos ++] = '\0'; 83 | usb_cdc_put_string("\r\n"); 84 | uint8_t cmd = 1; 85 | do { 86 | cmd = cli_process(command_buf, 87 | FreeRTOS_CLIGetOutputBuffer(), 88 | configCOMMAND_INT_MAX_OUTPUT_SIZE); 89 | usb_cdc_put_string(FreeRTOS_CLIGetOutputBuffer()); 90 | } while (cmd); 91 | command_buf_pos = 0; 92 | break; 93 | case '\n': 94 | break; 95 | case '\b': 96 | if (command_buf_pos) { 97 | command_buf_pos --; 98 | usb_cdc_put_char(ch); 99 | } 100 | break; 101 | default: 102 | usb_cdc_put_char(ch); 103 | command_buf[command_buf_pos ++] = ch; 104 | break; 105 | } 106 | } 107 | } else { 108 | LED_STATE(0); 109 | } 110 | } 111 | LED_DEINIT(); 112 | usb_cdc_stop(); 113 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | \ 114 | RCC_APB2Periph_GPIOA | \ 115 | RCC_APB2Periph_GPIOB | \ 116 | RCC_APB2Periph_GPIOC | \ 117 | RCC_APB2Periph_GPIOD | \ 118 | RCC_APB2Periph_GPIOE | \ 119 | RCC_APB2Periph_GPIOF | \ 120 | RCC_APB2Periph_GPIOG, DISABLE); 121 | } 122 | 123 | 124 | #ifdef USE_FULL_ASSERT 125 | void assert_failed(uint8_t *file, uint32_t line) 126 | { 127 | DBG_OUT("assertion in %s:%04u", file, (unsigned int)line); 128 | for (;;); 129 | } 130 | #endif 131 | 132 | 133 | __attribute__((naked)) void HardFault_Handler(void) 134 | { 135 | __asm volatile 136 | ( 137 | " tst lr, #4 \n" 138 | " ite eq \n" 139 | " mrseq r0, msp \n" 140 | " mrsne r0, psp \n" 141 | " bl prvGetRegistersFromStack \n" 142 | ); 143 | for (;;); 144 | } 145 | 146 | 147 | void prvGetRegistersFromStack(unsigned int *pStack) 148 | { 149 | DBG_OUT("hard fault\n" 150 | "R0 = %08x\n" 151 | "R1 = %08x\n" 152 | "R2 = %08x\n" 153 | "R3 = %08x\n" 154 | "R12 = %08x\n" 155 | "LR = %08x\n" 156 | "PC = %08x\n" 157 | "PSR = %08x\n", 158 | pStack[0], pStack[1], pStack[2], pStack[3], 159 | pStack[4], pStack[5], pStack[6], pStack[7]); 160 | } 161 | 162 | 163 | void SystemHseFailed(void) 164 | { 165 | DBG_OUT("HSE failed"); 166 | for (;;); 167 | } 168 | -------------------------------------------------------------------------------- /hex_parser/hex_parser.c: -------------------------------------------------------------------------------- 1 | /* 2 | ***************************************************************************** 3 | ** 4 | ** (C) 2023 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ***************************************************************************** 9 | */ 10 | 11 | #include "hex_parser.h" 12 | #include 13 | #include 14 | 15 | 16 | #define IHEX_START_POS (0) 17 | #define IHEX_LENGTH_POS (1) 18 | #define IHEX_ADDR_POS (3) 19 | #define IHEX_RECT_POS (7) 20 | #define IHEX_DATA_POS (9) 21 | 22 | #define IHEX_START_BYTE (':') 23 | #define IHEX_UTIL_DATA (11) 24 | 25 | #define IHEX_RECT_DATA (00) 26 | #define IHEX_RECT_EOF (01) 27 | #define IHEX_RECT_ESA (02) 28 | #define IHEX_RECT_SSA (03) 29 | #define IHEX_RECT_ELA (04) 30 | #define IHEX_RECT_SLA (05) 31 | 32 | 33 | static hex_parser_on_data_t hex_parser_on_data = NULL; 34 | 35 | 36 | static bool str2byte(const char *str, uint8_t *bt); 37 | static void byte2str(char *str, uint8_t bt); 38 | 39 | 40 | bool hex_parser_feed(char *str, bool data_upd) 41 | { 42 | static uint32_t addr_offs = 0; 43 | bool result = false; 44 | do { 45 | if (str[IHEX_START_POS] != IHEX_START_BYTE) { 46 | break; 47 | } 48 | uint8_t len; 49 | if (!str2byte(&str[IHEX_LENGTH_POS], &len)) { 50 | break; 51 | } 52 | if (strlen(str) != ((uint16_t)len << 1) + IHEX_UTIL_DATA) { 53 | break; 54 | } 55 | uint8_t crc = 0, crc_beg = 0; 56 | for (uint16_t i = IHEX_LENGTH_POS; i < ((uint16_t)len << 1) + IHEX_UTIL_DATA; i += 2) { 57 | uint8_t bt; 58 | if (!str2byte(&str[i], &bt)) { 59 | crc = 1; 60 | break; 61 | } else { 62 | crc += bt; 63 | if (i < IHEX_DATA_POS) { 64 | crc_beg += bt; 65 | } 66 | } 67 | } 68 | if (crc) { 69 | break; 70 | } 71 | uint16_t addr; 72 | if (!str2byte(&str[IHEX_ADDR_POS], (uint8_t *)&addr + 1)) { 73 | break; 74 | } 75 | if (!str2byte(&str[IHEX_ADDR_POS + 2], (uint8_t *)&addr)) { 76 | break; 77 | } 78 | uint8_t rect; 79 | if (!str2byte(&str[IHEX_RECT_POS], &rect)) { 80 | break; 81 | } 82 | switch (rect) { 83 | case IHEX_RECT_DATA: 84 | result = true; 85 | uint8_t *data = malloc(len); 86 | if (data) { 87 | for (uint16_t i = 0; i < len && result; i++) { 88 | if (!str2byte(&str[IHEX_DATA_POS + (i << 1)], &data[i])) { 89 | result = false; 90 | } 91 | } 92 | if (result && hex_parser_on_data) { 93 | result = hex_parser_on_data(addr_offs + addr, data, len); 94 | if (result && data_upd) { 95 | for (uint16_t i = 0; i < len; i++) { 96 | crc_beg += data[i]; 97 | byte2str(&str[IHEX_DATA_POS + (i << 1)], data[i]); 98 | } 99 | crc_beg = (uint8_t)0x1 + (uint8_t)(~crc_beg); 100 | byte2str(&str[IHEX_DATA_POS + (len << 1)], crc_beg); 101 | } 102 | } 103 | free(data); 104 | } else { 105 | result = false; 106 | } 107 | break; 108 | case IHEX_RECT_EOF: 109 | result = hex_parser_on_data(0, NULL, 0); 110 | addr_offs = 0; 111 | break; 112 | case IHEX_RECT_ESA: { 113 | uint16_t tmp; 114 | if (!str2byte(&str[IHEX_DATA_POS], (uint8_t *)&tmp + 1)) { 115 | break; 116 | } 117 | if (!str2byte(&str[IHEX_DATA_POS + 2], (uint8_t *)&tmp)) { 118 | break; 119 | } 120 | addr_offs = (uint32_t)tmp << 4; 121 | result = true; 122 | } 123 | break; 124 | case IHEX_RECT_SSA: 125 | result = true; 126 | break; 127 | case IHEX_RECT_ELA: { 128 | uint16_t tmp; 129 | if (!str2byte(&str[IHEX_DATA_POS], (uint8_t *)&tmp + 1)) { 130 | break; 131 | } 132 | if (!str2byte(&str[IHEX_DATA_POS + 2], (uint8_t *)&tmp)) { 133 | break; 134 | } 135 | addr_offs = (uint32_t)tmp << 16; 136 | result = true; 137 | } 138 | break; 139 | case IHEX_RECT_SLA: 140 | result = true; 141 | break; 142 | default: 143 | break; 144 | } 145 | } while (0); 146 | return result; 147 | } 148 | 149 | void hex_parser_set_callback(hex_parser_on_data_t on_data) 150 | { 151 | hex_parser_on_data = on_data; 152 | } 153 | 154 | 155 | static bool str2byte(const char *str, uint8_t *bt) 156 | { 157 | bool result = true; 158 | *bt = 0; 159 | for (uint8_t i = 0; i < 2; i++) { 160 | *bt <<= 4; 161 | if (str[i] >= '0' && str[i] <= '9') { 162 | *bt |= str[i] - '0'; 163 | } else if (str[i] >= 'A' && str[i] <= 'F') { 164 | *bt |= str[i] - 'A' + 0xA; 165 | } else if (str[i] >= 'a' && str[i] <= 'f') { 166 | *bt |= str[i] - 'a' + 0xA; 167 | } else { 168 | result = false; 169 | break; 170 | } 171 | } 172 | return result; 173 | } 174 | 175 | static void byte2str(char *str, uint8_t bt) 176 | { 177 | static const char chr[] = {'0', '1', '2', '3', '4', '5', '6', '7', 178 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 179 | }; 180 | str[0] = chr[bt >> 4]; 181 | str[1] = chr[bt & 0xF]; 182 | } 183 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/src/stm32f10x_iwdg.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_iwdg.c 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file provides all the IWDG firmware functions. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm32f10x_iwdg.h" 30 | 31 | /** @addtogroup STM32F10x_StdPeriph_Driver 32 | * @{ 33 | */ 34 | 35 | /** @defgroup IWDG 36 | * @brief IWDG driver modules 37 | * @{ 38 | */ 39 | 40 | /** @defgroup IWDG_Private_TypesDefinitions 41 | * @{ 42 | */ 43 | 44 | /** 45 | * @} 46 | */ 47 | 48 | /** @defgroup IWDG_Private_Defines 49 | * @{ 50 | */ 51 | 52 | /* ---------------------- IWDG registers bit mask ----------------------------*/ 53 | 54 | /* KR register bit mask */ 55 | #define KR_KEY_Reload ((uint16_t)0xAAAA) 56 | #define KR_KEY_Enable ((uint16_t)0xCCCC) 57 | 58 | /** 59 | * @} 60 | */ 61 | 62 | /** @defgroup IWDG_Private_Macros 63 | * @{ 64 | */ 65 | 66 | /** 67 | * @} 68 | */ 69 | 70 | /** @defgroup IWDG_Private_Variables 71 | * @{ 72 | */ 73 | 74 | /** 75 | * @} 76 | */ 77 | 78 | /** @defgroup IWDG_Private_FunctionPrototypes 79 | * @{ 80 | */ 81 | 82 | /** 83 | * @} 84 | */ 85 | 86 | /** @defgroup IWDG_Private_Functions 87 | * @{ 88 | */ 89 | 90 | /** 91 | * @brief Enables or disables write access to IWDG_PR and IWDG_RLR registers. 92 | * @param IWDG_WriteAccess: new state of write access to IWDG_PR and IWDG_RLR registers. 93 | * This parameter can be one of the following values: 94 | * @arg IWDG_WriteAccess_Enable: Enable write access to IWDG_PR and IWDG_RLR registers 95 | * @arg IWDG_WriteAccess_Disable: Disable write access to IWDG_PR and IWDG_RLR registers 96 | * @retval None 97 | */ 98 | void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess) 99 | { 100 | /* Check the parameters */ 101 | assert_param(IS_IWDG_WRITE_ACCESS(IWDG_WriteAccess)); 102 | IWDG->KR = IWDG_WriteAccess; 103 | } 104 | 105 | /** 106 | * @brief Sets IWDG Prescaler value. 107 | * @param IWDG_Prescaler: specifies the IWDG Prescaler value. 108 | * This parameter can be one of the following values: 109 | * @arg IWDG_Prescaler_4: IWDG prescaler set to 4 110 | * @arg IWDG_Prescaler_8: IWDG prescaler set to 8 111 | * @arg IWDG_Prescaler_16: IWDG prescaler set to 16 112 | * @arg IWDG_Prescaler_32: IWDG prescaler set to 32 113 | * @arg IWDG_Prescaler_64: IWDG prescaler set to 64 114 | * @arg IWDG_Prescaler_128: IWDG prescaler set to 128 115 | * @arg IWDG_Prescaler_256: IWDG prescaler set to 256 116 | * @retval None 117 | */ 118 | void IWDG_SetPrescaler(uint8_t IWDG_Prescaler) 119 | { 120 | /* Check the parameters */ 121 | assert_param(IS_IWDG_PRESCALER(IWDG_Prescaler)); 122 | IWDG->PR = IWDG_Prescaler; 123 | } 124 | 125 | /** 126 | * @brief Sets IWDG Reload value. 127 | * @param Reload: specifies the IWDG Reload value. 128 | * This parameter must be a number between 0 and 0x0FFF. 129 | * @retval None 130 | */ 131 | void IWDG_SetReload(uint16_t Reload) 132 | { 133 | /* Check the parameters */ 134 | assert_param(IS_IWDG_RELOAD(Reload)); 135 | IWDG->RLR = Reload; 136 | } 137 | 138 | /** 139 | * @brief Reloads IWDG counter with value defined in the reload register 140 | * (write access to IWDG_PR and IWDG_RLR registers disabled). 141 | * @param None 142 | * @retval None 143 | */ 144 | void IWDG_ReloadCounter(void) 145 | { 146 | IWDG->KR = KR_KEY_Reload; 147 | } 148 | 149 | /** 150 | * @brief Enables IWDG (write access to IWDG_PR and IWDG_RLR registers disabled). 151 | * @param None 152 | * @retval None 153 | */ 154 | void IWDG_Enable(void) 155 | { 156 | IWDG->KR = KR_KEY_Enable; 157 | } 158 | 159 | /** 160 | * @brief Checks whether the specified IWDG flag is set or not. 161 | * @param IWDG_FLAG: specifies the flag to check. 162 | * This parameter can be one of the following values: 163 | * @arg IWDG_FLAG_PVU: Prescaler Value Update on going 164 | * @arg IWDG_FLAG_RVU: Reload Value Update on going 165 | * @retval The new state of IWDG_FLAG (SET or RESET). 166 | */ 167 | FlagStatus IWDG_GetFlagStatus(uint16_t IWDG_FLAG) 168 | { 169 | FlagStatus bitstatus = RESET; 170 | /* Check the parameters */ 171 | assert_param(IS_IWDG_FLAG(IWDG_FLAG)); 172 | if ((IWDG->SR & IWDG_FLAG) != (uint32_t)RESET) 173 | { 174 | bitstatus = SET; 175 | } 176 | else 177 | { 178 | bitstatus = RESET; 179 | } 180 | /* Return the flag status */ 181 | return bitstatus; 182 | } 183 | 184 | /** 185 | * @} 186 | */ 187 | 188 | /** 189 | * @} 190 | */ 191 | 192 | /** 193 | * @} 194 | */ 195 | 196 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 197 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/src/stm32f10x_dbgmcu.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_dbgmcu.c 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file provides all the DBGMCU firmware functions. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm32f10x_dbgmcu.h" 30 | 31 | /** @addtogroup STM32F10x_StdPeriph_Driver 32 | * @{ 33 | */ 34 | 35 | /** @defgroup DBGMCU 36 | * @brief DBGMCU driver modules 37 | * @{ 38 | */ 39 | 40 | /** @defgroup DBGMCU_Private_TypesDefinitions 41 | * @{ 42 | */ 43 | 44 | /** 45 | * @} 46 | */ 47 | 48 | /** @defgroup DBGMCU_Private_Defines 49 | * @{ 50 | */ 51 | 52 | #define IDCODE_DEVID_MASK ((uint32_t)0x00000FFF) 53 | /** 54 | * @} 55 | */ 56 | 57 | /** @defgroup DBGMCU_Private_Macros 58 | * @{ 59 | */ 60 | 61 | /** 62 | * @} 63 | */ 64 | 65 | /** @defgroup DBGMCU_Private_Variables 66 | * @{ 67 | */ 68 | 69 | /** 70 | * @} 71 | */ 72 | 73 | /** @defgroup DBGMCU_Private_FunctionPrototypes 74 | * @{ 75 | */ 76 | 77 | /** 78 | * @} 79 | */ 80 | 81 | /** @defgroup DBGMCU_Private_Functions 82 | * @{ 83 | */ 84 | 85 | /** 86 | * @brief Returns the device revision identifier. 87 | * @param None 88 | * @retval Device revision identifier 89 | */ 90 | uint32_t DBGMCU_GetREVID(void) 91 | { 92 | return(DBGMCU->IDCODE >> 16); 93 | } 94 | 95 | /** 96 | * @brief Returns the device identifier. 97 | * @param None 98 | * @retval Device identifier 99 | */ 100 | uint32_t DBGMCU_GetDEVID(void) 101 | { 102 | return(DBGMCU->IDCODE & IDCODE_DEVID_MASK); 103 | } 104 | 105 | /** 106 | * @brief Configures the specified peripheral and low power mode behavior 107 | * when the MCU under Debug mode. 108 | * @param DBGMCU_Periph: specifies the peripheral and low power mode. 109 | * This parameter can be any combination of the following values: 110 | * @arg DBGMCU_SLEEP: Keep debugger connection during SLEEP mode 111 | * @arg DBGMCU_STOP: Keep debugger connection during STOP mode 112 | * @arg DBGMCU_STANDBY: Keep debugger connection during STANDBY mode 113 | * @arg DBGMCU_IWDG_STOP: Debug IWDG stopped when Core is halted 114 | * @arg DBGMCU_WWDG_STOP: Debug WWDG stopped when Core is halted 115 | * @arg DBGMCU_TIM1_STOP: TIM1 counter stopped when Core is halted 116 | * @arg DBGMCU_TIM2_STOP: TIM2 counter stopped when Core is halted 117 | * @arg DBGMCU_TIM3_STOP: TIM3 counter stopped when Core is halted 118 | * @arg DBGMCU_TIM4_STOP: TIM4 counter stopped when Core is halted 119 | * @arg DBGMCU_CAN1_STOP: Debug CAN2 stopped when Core is halted 120 | * @arg DBGMCU_I2C1_SMBUS_TIMEOUT: I2C1 SMBUS timeout mode stopped when Core is halted 121 | * @arg DBGMCU_I2C2_SMBUS_TIMEOUT: I2C2 SMBUS timeout mode stopped when Core is halted 122 | * @arg DBGMCU_TIM5_STOP: TIM5 counter stopped when Core is halted 123 | * @arg DBGMCU_TIM6_STOP: TIM6 counter stopped when Core is halted 124 | * @arg DBGMCU_TIM7_STOP: TIM7 counter stopped when Core is halted 125 | * @arg DBGMCU_TIM8_STOP: TIM8 counter stopped when Core is halted 126 | * @arg DBGMCU_CAN2_STOP: Debug CAN2 stopped when Core is halted 127 | * @arg DBGMCU_TIM15_STOP: TIM15 counter stopped when Core is halted 128 | * @arg DBGMCU_TIM16_STOP: TIM16 counter stopped when Core is halted 129 | * @arg DBGMCU_TIM17_STOP: TIM17 counter stopped when Core is halted 130 | * @arg DBGMCU_TIM9_STOP: TIM9 counter stopped when Core is halted 131 | * @arg DBGMCU_TIM10_STOP: TIM10 counter stopped when Core is halted 132 | * @arg DBGMCU_TIM11_STOP: TIM11 counter stopped when Core is halted 133 | * @arg DBGMCU_TIM12_STOP: TIM12 counter stopped when Core is halted 134 | * @arg DBGMCU_TIM13_STOP: TIM13 counter stopped when Core is halted 135 | * @arg DBGMCU_TIM14_STOP: TIM14 counter stopped when Core is halted 136 | * @param NewState: new state of the specified peripheral in Debug mode. 137 | * This parameter can be: ENABLE or DISABLE. 138 | * @retval None 139 | */ 140 | void DBGMCU_Config(uint32_t DBGMCU_Periph, FunctionalState NewState) 141 | { 142 | /* Check the parameters */ 143 | assert_param(IS_DBGMCU_PERIPH(DBGMCU_Periph)); 144 | assert_param(IS_FUNCTIONAL_STATE(NewState)); 145 | 146 | if (NewState != DISABLE) 147 | { 148 | DBGMCU->CR |= DBGMCU_Periph; 149 | } 150 | else 151 | { 152 | DBGMCU->CR &= ~DBGMCU_Periph; 153 | } 154 | } 155 | 156 | /** 157 | * @} 158 | */ 159 | 160 | /** 161 | * @} 162 | */ 163 | 164 | /** 165 | * @} 166 | */ 167 | 168 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 169 | -------------------------------------------------------------------------------- /stm/STM32_USB-FS-Device_Driver/src/usb_sil.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_sil.c 4 | * @author MCD Application Team 5 | * @version V4.1.0 6 | * @date 26-May-2017 7 | * @brief Simplified Interface Layer for Global Initialization and Endpoint 8 | * Rea/Write operations. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | *

© Copyright (c) 2017 STMicroelectronics International N.V. 13 | * All rights reserved.

14 | * 15 | * Redistribution and use in source and binary forms, with or without 16 | * modification, are permitted, provided that the following conditions are met: 17 | * 18 | * 1. Redistribution of source code must retain the above copyright notice, 19 | * this list of conditions and the following disclaimer. 20 | * 2. Redistributions in binary form must reproduce the above copyright notice, 21 | * this list of conditions and the following disclaimer in the documentation 22 | * and/or other materials provided with the distribution. 23 | * 3. Neither the name of STMicroelectronics nor the names of other 24 | * contributors to this software may be used to endorse or promote products 25 | * derived from this software without specific written permission. 26 | * 4. This software, including modifications and/or derivative works of this 27 | * software, must execute solely and exclusively on microcontroller or 28 | * microprocessor devices manufactured by or for STMicroelectronics. 29 | * 5. Redistribution and use of this software other than as permitted under 30 | * this license is void and will automatically terminate your rights under 31 | * this license. 32 | * 33 | * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" 34 | * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT 35 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 36 | * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY 37 | * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT 38 | * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 39 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 40 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 41 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 42 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 43 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 44 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 45 | * 46 | ****************************************************************************** 47 | */ 48 | 49 | 50 | /* Includes ------------------------------------------------------------------*/ 51 | #include "usb_lib.h" 52 | 53 | /* Private typedef -----------------------------------------------------------*/ 54 | /* Private define ------------------------------------------------------------*/ 55 | /* Private macro -------------------------------------------------------------*/ 56 | /* Private variables ---------------------------------------------------------*/ 57 | /* Extern variables ----------------------------------------------------------*/ 58 | /* Private function prototypes -----------------------------------------------*/ 59 | /* Private functions ---------------------------------------------------------*/ 60 | 61 | /** 62 | * Function Name : USB_SIL_Init 63 | * Description : Initialize the USB Device IP and the Endpoint 0. 64 | * Input : None. 65 | * Output : None. 66 | * Return : Status. 67 | **/ 68 | uint32_t USB_SIL_Init(void) 69 | { 70 | /* USB interrupts initialization */ 71 | /* clear pending interrupts */ 72 | _SetISTR(0); 73 | wInterrupt_Mask = IMR_MSK; 74 | /* set interrupts mask */ 75 | _SetCNTR(wInterrupt_Mask); 76 | return 0; 77 | } 78 | 79 | /** 80 | * Function Name : USB_SIL_Write 81 | * Description : Write a buffer of data to a selected endpoint. 82 | * Input : - bEpAddr: The address of the non control endpoint. 83 | * - pBufferPointer: The pointer to the buffer of data to be written 84 | * to the endpoint. 85 | * - wBufferSize: Number of data to be written (in bytes). 86 | * Output : None. 87 | * Return : Status. 88 | **/ 89 | uint32_t USB_SIL_Write(uint8_t bEpAddr, uint8_t* pBufferPointer, uint32_t wBufferSize) 90 | { 91 | /* Use the memory interface function to write to the selected endpoint */ 92 | UserToPMABufferCopy(pBufferPointer, GetEPTxAddr(bEpAddr & 0x7F), wBufferSize); 93 | 94 | /* Update the data length in the control register */ 95 | SetEPTxCount((bEpAddr & 0x7F), wBufferSize); 96 | 97 | return 0; 98 | } 99 | 100 | /** 101 | * Function Name : USB_SIL_Read 102 | * Description : Write a buffer of data to a selected endpoint. 103 | * Input : - bEpAddr: The address of the non control endpoint. 104 | * - pBufferPointer: The pointer to which will be saved the 105 | * received data buffer. 106 | * Output : None. 107 | * Return : Number of received data (in Bytes). 108 | **/ 109 | uint32_t USB_SIL_Read(uint8_t bEpAddr, uint8_t* pBufferPointer) 110 | { 111 | uint32_t DataLength = 0; 112 | 113 | /* Get the number of received data on the selected Endpoint */ 114 | DataLength = GetEPRxCount(bEpAddr & 0x7F); 115 | 116 | /* Use the memory interface function to write to the selected endpoint */ 117 | PMAToUserBufferCopy(pBufferPointer, GetEPRxAddr(bEpAddr & 0x7F), DataLength); 118 | 119 | /* Return the number of received data */ 120 | return DataLength; 121 | } 122 | 123 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 124 | -------------------------------------------------------------------------------- /stm/STM32_USB-FS-Device_Driver/src/usb_mem.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_mem.c 4 | * @author MCD Application Team 5 | * @version V4.1.0 6 | * @date 26-May-2017 7 | * @brief Utility functions for memory transfers to/from PMA 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© Copyright (c) 2017 STMicroelectronics International N.V. 12 | * All rights reserved.

13 | * 14 | * Redistribution and use in source and binary forms, with or without 15 | * modification, are permitted, provided that the following conditions are met: 16 | * 17 | * 1. Redistribution of source code must retain the above copyright notice, 18 | * this list of conditions and the following disclaimer. 19 | * 2. Redistributions in binary form must reproduce the above copyright notice, 20 | * this list of conditions and the following disclaimer in the documentation 21 | * and/or other materials provided with the distribution. 22 | * 3. Neither the name of STMicroelectronics nor the names of other 23 | * contributors to this software may be used to endorse or promote products 24 | * derived from this software without specific written permission. 25 | * 4. This software, including modifications and/or derivative works of this 26 | * software, must execute solely and exclusively on microcontroller or 27 | * microprocessor devices manufactured by or for STMicroelectronics. 28 | * 5. Redistribution and use of this software other than as permitted under 29 | * this license is void and will automatically terminate your rights under 30 | * this license. 31 | * 32 | * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" 33 | * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT 34 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 35 | * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY 36 | * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT 37 | * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 38 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 39 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 40 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 41 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 42 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 43 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 44 | * 45 | ****************************************************************************** 46 | */ 47 | 48 | /* Includes ------------------------------------------------------------------*/ 49 | #include "usb_lib.h" 50 | 51 | /* Private typedef -----------------------------------------------------------*/ 52 | /* Private define ------------------------------------------------------------*/ 53 | /* Private macro -------------------------------------------------------------*/ 54 | /* Private variables ---------------------------------------------------------*/ 55 | /* Extern variables ----------------------------------------------------------*/ 56 | /* Private function prototypes -----------------------------------------------*/ 57 | /* Private functions ---------------------------------------------------------*/ 58 | 59 | /** 60 | * Function Name : UserToPMABufferCopy 61 | * Description : Copy a buffer from user memory area to packet memory area (PMA) 62 | * Input : - pbUsrBuf: pointer to user memory area. 63 | * - wPMABufAddr: address into PMA. 64 | * - wNBytes: no. of bytes to be copied. 65 | * Output : None. 66 | * Return : None . 67 | **/ 68 | void UserToPMABufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes) 69 | { 70 | #if defined STM32F303xE || defined STM32F302x8 71 | uint32_t n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */ 72 | uint32_t i; 73 | uint16_t *pdwVal; 74 | pdwVal = (uint16_t *)(wPMABufAddr + PMAAddr); 75 | 76 | for (i = n; i != 0; i--) 77 | { 78 | *pdwVal++ = *(uint16_t*)pbUsrBuf++; 79 | pbUsrBuf++; 80 | } 81 | #else 82 | uint32_t n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */ 83 | uint32_t i, temp1, temp2; 84 | uint16_t *pdwVal; 85 | pdwVal = (uint16_t *)(wPMABufAddr * 2 + PMAAddr); 86 | for (i = n; i != 0; i--) 87 | { 88 | temp1 = (uint16_t) * pbUsrBuf; 89 | pbUsrBuf++; 90 | temp2 = temp1 | (uint16_t) * pbUsrBuf << 8; 91 | *pdwVal++ = temp2; 92 | pdwVal++; 93 | pbUsrBuf++; 94 | } 95 | #endif 96 | } 97 | 98 | /** 99 | * Function Name : PMAToUserBufferCopy 100 | * Description : Copy a buffer from user memory area to packet memory area (PMA) 101 | * Input : - pbUsrBuf = pointer to user memory area. 102 | * - wPMABufAddr = address into PMA. 103 | * - wNBytes = no. of bytes to be copied. 104 | * Output : None. 105 | * Return : None. 106 | **/ 107 | void PMAToUserBufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes) 108 | { 109 | #if defined STM32F303xE || defined STM32F302x8 110 | uint32_t n = (wNBytes + 1) >> 1;/* /2*/ 111 | uint32_t i; 112 | uint16_t *pdwVal; 113 | pdwVal = (uint16_t *)(wPMABufAddr + PMAAddr); 114 | for (i = n; i != 0; i--) 115 | { 116 | *(uint16_t*)pbUsrBuf++ = *pdwVal++; 117 | pbUsrBuf++; 118 | } 119 | #else 120 | uint32_t n = (wNBytes + 1) >> 1;/* /2*/ 121 | uint32_t i; 122 | uint32_t *pdwVal; 123 | pdwVal = (uint32_t *)(wPMABufAddr * 2 + PMAAddr); 124 | for (i = n; i != 0; i--) 125 | { 126 | *(uint16_t*)pbUsrBuf++ = *pdwVal++; 127 | pbUsrBuf++; 128 | } 129 | #endif 130 | } 131 | 132 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 133 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/src/stm32f10x_wwdg.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_wwdg.c 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file provides all the WWDG firmware functions. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm32f10x_wwdg.h" 30 | #include "stm32f10x_rcc.h" 31 | 32 | /** @addtogroup STM32F10x_StdPeriph_Driver 33 | * @{ 34 | */ 35 | 36 | /** @defgroup WWDG 37 | * @brief WWDG driver modules 38 | * @{ 39 | */ 40 | 41 | /** @defgroup WWDG_Private_TypesDefinitions 42 | * @{ 43 | */ 44 | 45 | /** 46 | * @} 47 | */ 48 | 49 | /** @defgroup WWDG_Private_Defines 50 | * @{ 51 | */ 52 | 53 | /* ----------- WWDG registers bit address in the alias region ----------- */ 54 | #define WWDG_OFFSET (WWDG_BASE - PERIPH_BASE) 55 | 56 | /* Alias word address of EWI bit */ 57 | #define CFR_OFFSET (WWDG_OFFSET + 0x04) 58 | #define EWI_BitNumber 0x09 59 | #define CFR_EWI_BB (PERIPH_BB_BASE + (CFR_OFFSET * 32) + (EWI_BitNumber * 4)) 60 | 61 | /* --------------------- WWDG registers bit mask ------------------------ */ 62 | 63 | /* CR register bit mask */ 64 | #define CR_WDGA_Set ((uint32_t)0x00000080) 65 | 66 | /* CFR register bit mask */ 67 | #define CFR_WDGTB_Mask ((uint32_t)0xFFFFFE7F) 68 | #define CFR_W_Mask ((uint32_t)0xFFFFFF80) 69 | #define BIT_Mask ((uint8_t)0x7F) 70 | 71 | /** 72 | * @} 73 | */ 74 | 75 | /** @defgroup WWDG_Private_Macros 76 | * @{ 77 | */ 78 | 79 | /** 80 | * @} 81 | */ 82 | 83 | /** @defgroup WWDG_Private_Variables 84 | * @{ 85 | */ 86 | 87 | /** 88 | * @} 89 | */ 90 | 91 | /** @defgroup WWDG_Private_FunctionPrototypes 92 | * @{ 93 | */ 94 | 95 | /** 96 | * @} 97 | */ 98 | 99 | /** @defgroup WWDG_Private_Functions 100 | * @{ 101 | */ 102 | 103 | /** 104 | * @brief Deinitializes the WWDG peripheral registers to their default reset values. 105 | * @param None 106 | * @retval None 107 | */ 108 | void WWDG_DeInit(void) 109 | { 110 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, ENABLE); 111 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, DISABLE); 112 | } 113 | 114 | /** 115 | * @brief Sets the WWDG Prescaler. 116 | * @param WWDG_Prescaler: specifies the WWDG Prescaler. 117 | * This parameter can be one of the following values: 118 | * @arg WWDG_Prescaler_1: WWDG counter clock = (PCLK1/4096)/1 119 | * @arg WWDG_Prescaler_2: WWDG counter clock = (PCLK1/4096)/2 120 | * @arg WWDG_Prescaler_4: WWDG counter clock = (PCLK1/4096)/4 121 | * @arg WWDG_Prescaler_8: WWDG counter clock = (PCLK1/4096)/8 122 | * @retval None 123 | */ 124 | void WWDG_SetPrescaler(uint32_t WWDG_Prescaler) 125 | { 126 | uint32_t tmpreg = 0; 127 | /* Check the parameters */ 128 | assert_param(IS_WWDG_PRESCALER(WWDG_Prescaler)); 129 | /* Clear WDGTB[1:0] bits */ 130 | tmpreg = WWDG->CFR & CFR_WDGTB_Mask; 131 | /* Set WDGTB[1:0] bits according to WWDG_Prescaler value */ 132 | tmpreg |= WWDG_Prescaler; 133 | /* Store the new value */ 134 | WWDG->CFR = tmpreg; 135 | } 136 | 137 | /** 138 | * @brief Sets the WWDG window value. 139 | * @param WindowValue: specifies the window value to be compared to the downcounter. 140 | * This parameter value must be lower than 0x80. 141 | * @retval None 142 | */ 143 | void WWDG_SetWindowValue(uint8_t WindowValue) 144 | { 145 | __IO uint32_t tmpreg = 0; 146 | 147 | /* Check the parameters */ 148 | assert_param(IS_WWDG_WINDOW_VALUE(WindowValue)); 149 | /* Clear W[6:0] bits */ 150 | 151 | tmpreg = WWDG->CFR & CFR_W_Mask; 152 | 153 | /* Set W[6:0] bits according to WindowValue value */ 154 | tmpreg |= WindowValue & (uint32_t) BIT_Mask; 155 | 156 | /* Store the new value */ 157 | WWDG->CFR = tmpreg; 158 | } 159 | 160 | /** 161 | * @brief Enables the WWDG Early Wakeup interrupt(EWI). 162 | * @param None 163 | * @retval None 164 | */ 165 | void WWDG_EnableIT(void) 166 | { 167 | *(__IO uint32_t *) CFR_EWI_BB = (uint32_t)ENABLE; 168 | } 169 | 170 | /** 171 | * @brief Sets the WWDG counter value. 172 | * @param Counter: specifies the watchdog counter value. 173 | * This parameter must be a number between 0x40 and 0x7F. 174 | * @retval None 175 | */ 176 | void WWDG_SetCounter(uint8_t Counter) 177 | { 178 | /* Check the parameters */ 179 | assert_param(IS_WWDG_COUNTER(Counter)); 180 | /* Write to T[6:0] bits to configure the counter value, no need to do 181 | a read-modify-write; writing a 0 to WDGA bit does nothing */ 182 | WWDG->CR = Counter & BIT_Mask; 183 | } 184 | 185 | /** 186 | * @brief Enables WWDG and load the counter value. 187 | * @param Counter: specifies the watchdog counter value. 188 | * This parameter must be a number between 0x40 and 0x7F. 189 | * @retval None 190 | */ 191 | void WWDG_Enable(uint8_t Counter) 192 | { 193 | /* Check the parameters */ 194 | assert_param(IS_WWDG_COUNTER(Counter)); 195 | WWDG->CR = CR_WDGA_Set | Counter; 196 | } 197 | 198 | /** 199 | * @brief Checks whether the Early Wakeup interrupt flag is set or not. 200 | * @param None 201 | * @retval The new state of the Early Wakeup interrupt flag (SET or RESET) 202 | */ 203 | FlagStatus WWDG_GetFlagStatus(void) 204 | { 205 | return (FlagStatus)(WWDG->SR); 206 | } 207 | 208 | /** 209 | * @brief Clears Early Wakeup interrupt flag. 210 | * @param None 211 | * @retval None 212 | */ 213 | void WWDG_ClearFlag(void) 214 | { 215 | WWDG->SR = (uint32_t)RESET; 216 | } 217 | 218 | /** 219 | * @} 220 | */ 221 | 222 | /** 223 | * @} 224 | */ 225 | 226 | /** 227 | * @} 228 | */ 229 | 230 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 231 | -------------------------------------------------------------------------------- /src/usb/hw_config.c: -------------------------------------------------------------------------------- 1 | /* 2 | ******************************************************************************** 3 | ** 4 | ** (C) 2023 Andrii Bilynskyi 5 | ** 6 | ** This code is licensed under the MIT. 7 | ** 8 | ******************************************************************************** 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | 15 | __IO bool usb_cdc_ready = FALSE; 16 | __IO uint16_t usb_cdc_rx_cnt = 0; 17 | __IO uint16_t usb_cdc_rx_idx = 0; 18 | __IO uint8_t usb_cdc_rx_data[USB_CDC_EP3_PACK]; 19 | __IO bool usb_cdc_tx_done = TRUE; 20 | 21 | 22 | void usb_cdc_start(void) 23 | { 24 | usb_cdc_ready = FALSE; 25 | usb_cdc_rx_cnt = 0; 26 | usb_cdc_rx_idx = 0; 27 | usb_cdc_tx_done = TRUE; 28 | GPIO_InitTypeDef GPIO_InitStructure; 29 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12; 30 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 31 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 32 | GPIO_Init(GPIOA, &GPIO_InitStructure); 33 | RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); 34 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); 35 | NVIC_InitTypeDef NVIC_InitStructure; 36 | NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; 37 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 38 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 39 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 40 | NVIC_Init(&NVIC_InitStructure); 41 | USB_Init(); 42 | } 43 | 44 | 45 | void usb_cdc_stop(void) 46 | { 47 | usb_cdc_ready = FALSE; 48 | /* disable all interrupts and force USB reset */ 49 | _SetCNTR(CNTR_FRES); 50 | /* clear interrupt status register */ 51 | _SetISTR(0); 52 | /* switch-off device */ 53 | _SetCNTR(CNTR_FRES + CNTR_PDWN); 54 | NVIC_InitTypeDef NVIC_InitStructure; 55 | NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; 56 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 57 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 58 | NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE; 59 | NVIC_Init(&NVIC_InitStructure); 60 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, DISABLE); 61 | GPIO_InitTypeDef GPIO_InitStructure; 62 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12; 63 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; 64 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; 65 | GPIO_Init(GPIOA, &GPIO_InitStructure); 66 | } 67 | 68 | 69 | uint8_t usb_cdc_is_ready(void) 70 | { 71 | return (uint8_t)usb_cdc_ready; 72 | } 73 | 74 | 75 | uint8_t usb_cdc_rx_avail(void) 76 | { 77 | uint8_t result = FALSE; 78 | if (usb_cdc_rx_cnt > usb_cdc_rx_idx) { 79 | result = TRUE; 80 | } 81 | return result; 82 | } 83 | 84 | 85 | int usb_cdc_read(uint8_t data[], uint16_t len) 86 | { 87 | int result = -1; 88 | if (usb_cdc_ready == TRUE && data) { 89 | if (len) { 90 | while (usb_cdc_rx_cnt <= usb_cdc_rx_idx); 91 | result = len < usb_cdc_rx_cnt - usb_cdc_rx_idx ? 92 | len : usb_cdc_rx_cnt - usb_cdc_rx_idx; 93 | memcpy(data, (const uint8_t *)&usb_cdc_rx_data[usb_cdc_rx_idx], 94 | (uint16_t)result); 95 | usb_cdc_rx_idx += (uint16_t)result; 96 | if (usb_cdc_rx_cnt <= usb_cdc_rx_idx) { 97 | SetEPRxValid(ENDP3); 98 | } 99 | } else { 100 | result = 0; 101 | } 102 | } 103 | return result; 104 | } 105 | 106 | 107 | int usb_cdc_write(const uint8_t data[], uint16_t len) 108 | { 109 | int result = -1; 110 | if (usb_cdc_ready == TRUE && data) { 111 | result = len < USB_CDC_EP1_PACK ? len : USB_CDC_EP1_PACK; 112 | if (result > 0) { 113 | while (usb_cdc_tx_done == FALSE); 114 | usb_cdc_tx_done = FALSE; 115 | UserToPMABufferCopy((uint8_t *)data, ENDP1_TXADDR, (uint16_t)result); 116 | SetEPTxCount(ENDP1, (uint16_t)result); 117 | SetEPTxValid(ENDP1); 118 | } 119 | } 120 | return result; 121 | } 122 | 123 | 124 | char usb_cdc_get_char(void) 125 | { 126 | char ch = 0; 127 | if (usb_cdc_ready == TRUE) { 128 | while (usb_cdc_rx_cnt <= usb_cdc_rx_idx); 129 | ch = usb_cdc_rx_data[usb_cdc_rx_idx]; 130 | usb_cdc_rx_idx ++; 131 | if (usb_cdc_rx_cnt <= usb_cdc_rx_idx) { 132 | SetEPRxValid(ENDP3); 133 | } 134 | } 135 | return ch; 136 | } 137 | 138 | 139 | void usb_cdc_put_char(char ch) 140 | { 141 | if (usb_cdc_ready == TRUE) { 142 | while (usb_cdc_tx_done == FALSE); 143 | usb_cdc_tx_done = FALSE; 144 | UserToPMABufferCopy((uint8_t *)&ch, ENDP1_TXADDR, sizeof(ch)); 145 | SetEPTxCount(ENDP1, sizeof(ch)); 146 | SetEPTxValid(ENDP1); 147 | } 148 | } 149 | 150 | 151 | void usb_cdc_put_string(const char *str) 152 | { 153 | uint16_t len = strlen(str); 154 | while (len) { 155 | int res = usb_cdc_write((const uint8_t *)str, len); 156 | if (res > 0) { 157 | str += res; 158 | len -= res; 159 | } else { 160 | break; 161 | } 162 | } 163 | } 164 | 165 | 166 | /******************************************************************************* 167 | * USB interrupt 168 | ******************************************************************************/ 169 | 170 | /* ISTR register last read value */ 171 | __IO uint16_t wIstr; 172 | 173 | 174 | /* function pointers to non-control endpoints service routines */ 175 | static void usb_cdc_on_tx_finish(void); 176 | static void usb_cdc_on_rx_data(void); 177 | 178 | void (*pEpInt_IN[7])(void) = { 179 | usb_cdc_on_tx_finish, 180 | NOP_Process, 181 | NOP_Process, 182 | NOP_Process, 183 | NOP_Process, 184 | NOP_Process, 185 | NOP_Process, 186 | }; 187 | 188 | void (*pEpInt_OUT[7])(void) = { 189 | NOP_Process, 190 | NOP_Process, 191 | usb_cdc_on_rx_data, 192 | NOP_Process, 193 | NOP_Process, 194 | NOP_Process, 195 | NOP_Process, 196 | }; 197 | 198 | 199 | void USB_LP_CAN1_RX0_IRQHandler(void) 200 | { 201 | wIstr = _GetISTR(); 202 | if (wIstr & ISTR_CTR & wInterrupt_Mask) { 203 | /* servicing of the endpoint correct transfer interrupt */ 204 | /* clear of the CTR flag into the sub */ 205 | CTR_LP(); 206 | } 207 | if (wIstr & ISTR_RESET & wInterrupt_Mask) { 208 | _SetISTR((uint16_t)CLR_RESET); 209 | Device_Property.Reset(); 210 | } 211 | } 212 | 213 | 214 | static void usb_cdc_on_tx_finish(void) 215 | { 216 | usb_cdc_tx_done = TRUE; 217 | } 218 | 219 | 220 | static void usb_cdc_on_rx_data(void) 221 | { 222 | usb_cdc_rx_cnt = GetEPRxCount(ENDP3); 223 | usb_cdc_rx_idx = 0; 224 | if (usb_cdc_rx_cnt) { 225 | PMAToUserBufferCopy((uint8_t *)usb_cdc_rx_data, ENDP3_RXADDR, 226 | usb_cdc_rx_cnt); 227 | } else { 228 | SetEPRxValid(ENDP3); 229 | } 230 | } 231 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/inc/stm32f10x_cec.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_cec.h 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file contains all the functions prototypes for the CEC firmware 8 | * library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | *

© COPYRIGHT 2012 STMicroelectronics

13 | * 14 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 15 | * You may not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at: 17 | * 18 | * http://www.st.com/software_license_agreement_liberty_v2 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, 22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | ****************************************************************************** 27 | */ 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __STM32F10x_CEC_H 31 | #define __STM32F10x_CEC_H 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | /* Includes ------------------------------------------------------------------*/ 38 | #include "stm32f10x.h" 39 | 40 | /** @addtogroup STM32F10x_StdPeriph_Driver 41 | * @{ 42 | */ 43 | 44 | /** @addtogroup CEC 45 | * @{ 46 | */ 47 | 48 | 49 | /** @defgroup CEC_Exported_Types 50 | * @{ 51 | */ 52 | 53 | /** 54 | * @brief CEC Init structure definition 55 | */ 56 | typedef struct 57 | { 58 | uint16_t CEC_BitTimingMode; /*!< Configures the CEC Bit Timing Error Mode. 59 | This parameter can be a value of @ref CEC_BitTiming_Mode */ 60 | uint16_t CEC_BitPeriodMode; /*!< Configures the CEC Bit Period Error Mode. 61 | This parameter can be a value of @ref CEC_BitPeriod_Mode */ 62 | }CEC_InitTypeDef; 63 | 64 | /** 65 | * @} 66 | */ 67 | 68 | /** @defgroup CEC_Exported_Constants 69 | * @{ 70 | */ 71 | 72 | /** @defgroup CEC_BitTiming_Mode 73 | * @{ 74 | */ 75 | #define CEC_BitTimingStdMode ((uint16_t)0x00) /*!< Bit timing error Standard Mode */ 76 | #define CEC_BitTimingErrFreeMode CEC_CFGR_BTEM /*!< Bit timing error Free Mode */ 77 | 78 | #define IS_CEC_BIT_TIMING_ERROR_MODE(MODE) (((MODE) == CEC_BitTimingStdMode) || \ 79 | ((MODE) == CEC_BitTimingErrFreeMode)) 80 | /** 81 | * @} 82 | */ 83 | 84 | /** @defgroup CEC_BitPeriod_Mode 85 | * @{ 86 | */ 87 | #define CEC_BitPeriodStdMode ((uint16_t)0x00) /*!< Bit period error Standard Mode */ 88 | #define CEC_BitPeriodFlexibleMode CEC_CFGR_BPEM /*!< Bit period error Flexible Mode */ 89 | 90 | #define IS_CEC_BIT_PERIOD_ERROR_MODE(MODE) (((MODE) == CEC_BitPeriodStdMode) || \ 91 | ((MODE) == CEC_BitPeriodFlexibleMode)) 92 | /** 93 | * @} 94 | */ 95 | 96 | 97 | /** @defgroup CEC_interrupts_definition 98 | * @{ 99 | */ 100 | #define CEC_IT_TERR CEC_CSR_TERR 101 | #define CEC_IT_TBTRF CEC_CSR_TBTRF 102 | #define CEC_IT_RERR CEC_CSR_RERR 103 | #define CEC_IT_RBTF CEC_CSR_RBTF 104 | #define IS_CEC_GET_IT(IT) (((IT) == CEC_IT_TERR) || ((IT) == CEC_IT_TBTRF) || \ 105 | ((IT) == CEC_IT_RERR) || ((IT) == CEC_IT_RBTF)) 106 | /** 107 | * @} 108 | */ 109 | 110 | 111 | /** @defgroup CEC_Own_Address 112 | * @{ 113 | */ 114 | #define IS_CEC_ADDRESS(ADDRESS) ((ADDRESS) < 0x10) 115 | /** 116 | * @} 117 | */ 118 | 119 | /** @defgroup CEC_Prescaler 120 | * @{ 121 | */ 122 | #define IS_CEC_PRESCALER(PRESCALER) ((PRESCALER) <= 0x3FFF) 123 | 124 | /** 125 | * @} 126 | */ 127 | 128 | /** @defgroup CEC_flags_definition 129 | * @{ 130 | */ 131 | 132 | /** 133 | * @brief ESR register flags 134 | */ 135 | #define CEC_FLAG_BTE ((uint32_t)0x10010000) 136 | #define CEC_FLAG_BPE ((uint32_t)0x10020000) 137 | #define CEC_FLAG_RBTFE ((uint32_t)0x10040000) 138 | #define CEC_FLAG_SBE ((uint32_t)0x10080000) 139 | #define CEC_FLAG_ACKE ((uint32_t)0x10100000) 140 | #define CEC_FLAG_LINE ((uint32_t)0x10200000) 141 | #define CEC_FLAG_TBTFE ((uint32_t)0x10400000) 142 | 143 | /** 144 | * @brief CSR register flags 145 | */ 146 | #define CEC_FLAG_TEOM ((uint32_t)0x00000002) 147 | #define CEC_FLAG_TERR ((uint32_t)0x00000004) 148 | #define CEC_FLAG_TBTRF ((uint32_t)0x00000008) 149 | #define CEC_FLAG_RSOM ((uint32_t)0x00000010) 150 | #define CEC_FLAG_REOM ((uint32_t)0x00000020) 151 | #define CEC_FLAG_RERR ((uint32_t)0x00000040) 152 | #define CEC_FLAG_RBTF ((uint32_t)0x00000080) 153 | 154 | #define IS_CEC_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFFFF03) == 0x00) && ((FLAG) != 0x00)) 155 | 156 | #define IS_CEC_GET_FLAG(FLAG) (((FLAG) == CEC_FLAG_BTE) || ((FLAG) == CEC_FLAG_BPE) || \ 157 | ((FLAG) == CEC_FLAG_RBTFE) || ((FLAG)== CEC_FLAG_SBE) || \ 158 | ((FLAG) == CEC_FLAG_ACKE) || ((FLAG) == CEC_FLAG_LINE) || \ 159 | ((FLAG) == CEC_FLAG_TBTFE) || ((FLAG) == CEC_FLAG_TEOM) || \ 160 | ((FLAG) == CEC_FLAG_TERR) || ((FLAG) == CEC_FLAG_TBTRF) || \ 161 | ((FLAG) == CEC_FLAG_RSOM) || ((FLAG) == CEC_FLAG_REOM) || \ 162 | ((FLAG) == CEC_FLAG_RERR) || ((FLAG) == CEC_FLAG_RBTF)) 163 | 164 | /** 165 | * @} 166 | */ 167 | 168 | /** 169 | * @} 170 | */ 171 | 172 | /** @defgroup CEC_Exported_Macros 173 | * @{ 174 | */ 175 | 176 | /** 177 | * @} 178 | */ 179 | 180 | /** @defgroup CEC_Exported_Functions 181 | * @{ 182 | */ 183 | void CEC_DeInit(void); 184 | void CEC_Init(CEC_InitTypeDef* CEC_InitStruct); 185 | void CEC_Cmd(FunctionalState NewState); 186 | void CEC_ITConfig(FunctionalState NewState); 187 | void CEC_OwnAddressConfig(uint8_t CEC_OwnAddress); 188 | void CEC_SetPrescaler(uint16_t CEC_Prescaler); 189 | void CEC_SendDataByte(uint8_t Data); 190 | uint8_t CEC_ReceiveDataByte(void); 191 | void CEC_StartOfMessage(void); 192 | void CEC_EndOfMessageCmd(FunctionalState NewState); 193 | FlagStatus CEC_GetFlagStatus(uint32_t CEC_FLAG); 194 | void CEC_ClearFlag(uint32_t CEC_FLAG); 195 | ITStatus CEC_GetITStatus(uint8_t CEC_IT); 196 | void CEC_ClearITPendingBit(uint16_t CEC_IT); 197 | 198 | #ifdef __cplusplus 199 | } 200 | #endif 201 | 202 | #endif /* __STM32F10x_CEC_H */ 203 | 204 | /** 205 | * @} 206 | */ 207 | 208 | /** 209 | * @} 210 | */ 211 | 212 | /** 213 | * @} 214 | */ 215 | 216 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 217 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/inc/stm32f10x_exti.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_exti.h 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file contains all the functions prototypes for the EXTI firmware 8 | * library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | *

© COPYRIGHT 2012 STMicroelectronics

13 | * 14 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 15 | * You may not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at: 17 | * 18 | * http://www.st.com/software_license_agreement_liberty_v2 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, 22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | ****************************************************************************** 27 | */ 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __STM32F10x_EXTI_H 31 | #define __STM32F10x_EXTI_H 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | /* Includes ------------------------------------------------------------------*/ 38 | #include "stm32f10x.h" 39 | 40 | /** @addtogroup STM32F10x_StdPeriph_Driver 41 | * @{ 42 | */ 43 | 44 | /** @addtogroup EXTI 45 | * @{ 46 | */ 47 | 48 | /** @defgroup EXTI_Exported_Types 49 | * @{ 50 | */ 51 | 52 | /** 53 | * @brief EXTI mode enumeration 54 | */ 55 | 56 | typedef enum 57 | { 58 | EXTI_Mode_Interrupt = 0x00, 59 | EXTI_Mode_Event = 0x04 60 | }EXTIMode_TypeDef; 61 | 62 | #define IS_EXTI_MODE(MODE) (((MODE) == EXTI_Mode_Interrupt) || ((MODE) == EXTI_Mode_Event)) 63 | 64 | /** 65 | * @brief EXTI Trigger enumeration 66 | */ 67 | 68 | typedef enum 69 | { 70 | EXTI_Trigger_Rising = 0x08, 71 | EXTI_Trigger_Falling = 0x0C, 72 | EXTI_Trigger_Rising_Falling = 0x10 73 | }EXTITrigger_TypeDef; 74 | 75 | #define IS_EXTI_TRIGGER(TRIGGER) (((TRIGGER) == EXTI_Trigger_Rising) || \ 76 | ((TRIGGER) == EXTI_Trigger_Falling) || \ 77 | ((TRIGGER) == EXTI_Trigger_Rising_Falling)) 78 | /** 79 | * @brief EXTI Init Structure definition 80 | */ 81 | 82 | typedef struct 83 | { 84 | uint32_t EXTI_Line; /*!< Specifies the EXTI lines to be enabled or disabled. 85 | This parameter can be any combination of @ref EXTI_Lines */ 86 | 87 | EXTIMode_TypeDef EXTI_Mode; /*!< Specifies the mode for the EXTI lines. 88 | This parameter can be a value of @ref EXTIMode_TypeDef */ 89 | 90 | EXTITrigger_TypeDef EXTI_Trigger; /*!< Specifies the trigger signal active edge for the EXTI lines. 91 | This parameter can be a value of @ref EXTITrigger_TypeDef */ 92 | 93 | FunctionalState EXTI_LineCmd; /*!< Specifies the new state of the selected EXTI lines. 94 | This parameter can be set either to ENABLE or DISABLE */ 95 | }EXTI_InitTypeDef; 96 | 97 | /** 98 | * @} 99 | */ 100 | 101 | /** @defgroup EXTI_Exported_Constants 102 | * @{ 103 | */ 104 | 105 | /** @defgroup EXTI_Lines 106 | * @{ 107 | */ 108 | 109 | #define EXTI_Line0 ((uint32_t)0x00001) /*!< External interrupt line 0 */ 110 | #define EXTI_Line1 ((uint32_t)0x00002) /*!< External interrupt line 1 */ 111 | #define EXTI_Line2 ((uint32_t)0x00004) /*!< External interrupt line 2 */ 112 | #define EXTI_Line3 ((uint32_t)0x00008) /*!< External interrupt line 3 */ 113 | #define EXTI_Line4 ((uint32_t)0x00010) /*!< External interrupt line 4 */ 114 | #define EXTI_Line5 ((uint32_t)0x00020) /*!< External interrupt line 5 */ 115 | #define EXTI_Line6 ((uint32_t)0x00040) /*!< External interrupt line 6 */ 116 | #define EXTI_Line7 ((uint32_t)0x00080) /*!< External interrupt line 7 */ 117 | #define EXTI_Line8 ((uint32_t)0x00100) /*!< External interrupt line 8 */ 118 | #define EXTI_Line9 ((uint32_t)0x00200) /*!< External interrupt line 9 */ 119 | #define EXTI_Line10 ((uint32_t)0x00400) /*!< External interrupt line 10 */ 120 | #define EXTI_Line11 ((uint32_t)0x00800) /*!< External interrupt line 11 */ 121 | #define EXTI_Line12 ((uint32_t)0x01000) /*!< External interrupt line 12 */ 122 | #define EXTI_Line13 ((uint32_t)0x02000) /*!< External interrupt line 13 */ 123 | #define EXTI_Line14 ((uint32_t)0x04000) /*!< External interrupt line 14 */ 124 | #define EXTI_Line15 ((uint32_t)0x08000) /*!< External interrupt line 15 */ 125 | #define EXTI_Line16 ((uint32_t)0x10000) /*!< External interrupt line 16 Connected to the PVD Output */ 126 | #define EXTI_Line17 ((uint32_t)0x20000) /*!< External interrupt line 17 Connected to the RTC Alarm event */ 127 | #define EXTI_Line18 ((uint32_t)0x40000) /*!< External interrupt line 18 Connected to the USB Device/USB OTG FS 128 | Wakeup from suspend event */ 129 | #define EXTI_Line19 ((uint32_t)0x80000) /*!< External interrupt line 19 Connected to the Ethernet Wakeup event */ 130 | 131 | #define IS_EXTI_LINE(LINE) ((((LINE) & (uint32_t)0xFFF00000) == 0x00) && ((LINE) != (uint16_t)0x00)) 132 | #define IS_GET_EXTI_LINE(LINE) (((LINE) == EXTI_Line0) || ((LINE) == EXTI_Line1) || \ 133 | ((LINE) == EXTI_Line2) || ((LINE) == EXTI_Line3) || \ 134 | ((LINE) == EXTI_Line4) || ((LINE) == EXTI_Line5) || \ 135 | ((LINE) == EXTI_Line6) || ((LINE) == EXTI_Line7) || \ 136 | ((LINE) == EXTI_Line8) || ((LINE) == EXTI_Line9) || \ 137 | ((LINE) == EXTI_Line10) || ((LINE) == EXTI_Line11) || \ 138 | ((LINE) == EXTI_Line12) || ((LINE) == EXTI_Line13) || \ 139 | ((LINE) == EXTI_Line14) || ((LINE) == EXTI_Line15) || \ 140 | ((LINE) == EXTI_Line16) || ((LINE) == EXTI_Line17) || \ 141 | ((LINE) == EXTI_Line18) || ((LINE) == EXTI_Line19)) 142 | 143 | 144 | /** 145 | * @} 146 | */ 147 | 148 | /** 149 | * @} 150 | */ 151 | 152 | /** @defgroup EXTI_Exported_Macros 153 | * @{ 154 | */ 155 | 156 | /** 157 | * @} 158 | */ 159 | 160 | /** @defgroup EXTI_Exported_Functions 161 | * @{ 162 | */ 163 | 164 | void EXTI_DeInit(void); 165 | void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct); 166 | void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct); 167 | void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line); 168 | FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line); 169 | void EXTI_ClearFlag(uint32_t EXTI_Line); 170 | ITStatus EXTI_GetITStatus(uint32_t EXTI_Line); 171 | void EXTI_ClearITPendingBit(uint32_t EXTI_Line); 172 | 173 | #ifdef __cplusplus 174 | } 175 | #endif 176 | 177 | #endif /* __STM32F10x_EXTI_H */ 178 | /** 179 | * @} 180 | */ 181 | 182 | /** 183 | * @} 184 | */ 185 | 186 | /** 187 | * @} 188 | */ 189 | 190 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 191 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/src/misc.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file misc.c 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file provides all the miscellaneous firmware functions (add-on 8 | * to CMSIS functions). 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | *

© COPYRIGHT 2012 STMicroelectronics

13 | * 14 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 15 | * You may not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at: 17 | * 18 | * http://www.st.com/software_license_agreement_liberty_v2 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, 22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | ****************************************************************************** 27 | */ 28 | 29 | /* Includes ------------------------------------------------------------------*/ 30 | #include "misc.h" 31 | 32 | /** @addtogroup STM32F10x_StdPeriph_Driver 33 | * @{ 34 | */ 35 | 36 | /** @defgroup MISC 37 | * @brief MISC driver modules 38 | * @{ 39 | */ 40 | 41 | /** @defgroup MISC_Private_TypesDefinitions 42 | * @{ 43 | */ 44 | 45 | /** 46 | * @} 47 | */ 48 | 49 | /** @defgroup MISC_Private_Defines 50 | * @{ 51 | */ 52 | 53 | #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000) 54 | /** 55 | * @} 56 | */ 57 | 58 | /** @defgroup MISC_Private_Macros 59 | * @{ 60 | */ 61 | 62 | /** 63 | * @} 64 | */ 65 | 66 | /** @defgroup MISC_Private_Variables 67 | * @{ 68 | */ 69 | 70 | /** 71 | * @} 72 | */ 73 | 74 | /** @defgroup MISC_Private_FunctionPrototypes 75 | * @{ 76 | */ 77 | 78 | /** 79 | * @} 80 | */ 81 | 82 | /** @defgroup MISC_Private_Functions 83 | * @{ 84 | */ 85 | 86 | /** 87 | * @brief Configures the priority grouping: pre-emption priority and subpriority. 88 | * @param NVIC_PriorityGroup: specifies the priority grouping bits length. 89 | * This parameter can be one of the following values: 90 | * @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority 91 | * 4 bits for subpriority 92 | * @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority 93 | * 3 bits for subpriority 94 | * @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority 95 | * 2 bits for subpriority 96 | * @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority 97 | * 1 bits for subpriority 98 | * @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority 99 | * 0 bits for subpriority 100 | * @retval None 101 | */ 102 | void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup) 103 | { 104 | /* Check the parameters */ 105 | assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup)); 106 | 107 | /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */ 108 | SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup; 109 | } 110 | 111 | /** 112 | * @brief Initializes the NVIC peripheral according to the specified 113 | * parameters in the NVIC_InitStruct. 114 | * @param NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains 115 | * the configuration information for the specified NVIC peripheral. 116 | * @retval None 117 | */ 118 | void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct) 119 | { 120 | uint32_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F; 121 | 122 | /* Check the parameters */ 123 | assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd)); 124 | assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority)); 125 | assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority)); 126 | 127 | if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE) 128 | { 129 | /* Compute the Corresponding IRQ Priority --------------------------------*/ 130 | tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08; 131 | tmppre = (0x4 - tmppriority); 132 | tmpsub = tmpsub >> tmppriority; 133 | 134 | tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre; 135 | tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub; 136 | tmppriority = tmppriority << 0x04; 137 | 138 | NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority; 139 | 140 | /* Enable the Selected IRQ Channels --------------------------------------*/ 141 | NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] = 142 | (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F); 143 | } 144 | else 145 | { 146 | /* Disable the Selected IRQ Channels -------------------------------------*/ 147 | NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] = 148 | (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F); 149 | } 150 | } 151 | 152 | /** 153 | * @brief Sets the vector table location and Offset. 154 | * @param NVIC_VectTab: specifies if the vector table is in RAM or FLASH memory. 155 | * This parameter can be one of the following values: 156 | * @arg NVIC_VectTab_RAM 157 | * @arg NVIC_VectTab_FLASH 158 | * @param Offset: Vector Table base offset field. This value must be a multiple 159 | * of 0x200. 160 | * @retval None 161 | */ 162 | void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset) 163 | { 164 | /* Check the parameters */ 165 | assert_param(IS_NVIC_VECTTAB(NVIC_VectTab)); 166 | assert_param(IS_NVIC_OFFSET(Offset)); 167 | 168 | SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80); 169 | } 170 | 171 | /** 172 | * @brief Selects the condition for the system to enter low power mode. 173 | * @param LowPowerMode: Specifies the new mode for the system to enter low power mode. 174 | * This parameter can be one of the following values: 175 | * @arg NVIC_LP_SEVONPEND 176 | * @arg NVIC_LP_SLEEPDEEP 177 | * @arg NVIC_LP_SLEEPONEXIT 178 | * @param NewState: new state of LP condition. This parameter can be: ENABLE or DISABLE. 179 | * @retval None 180 | */ 181 | void NVIC_SystemLPConfig(uint8_t LowPowerMode, FunctionalState NewState) 182 | { 183 | /* Check the parameters */ 184 | assert_param(IS_NVIC_LP(LowPowerMode)); 185 | assert_param(IS_FUNCTIONAL_STATE(NewState)); 186 | 187 | if (NewState != DISABLE) 188 | { 189 | SCB->SCR |= LowPowerMode; 190 | } 191 | else 192 | { 193 | SCB->SCR &= (uint32_t)(~(uint32_t)LowPowerMode); 194 | } 195 | } 196 | 197 | /** 198 | * @brief Configures the SysTick clock source. 199 | * @param SysTick_CLKSource: specifies the SysTick clock source. 200 | * This parameter can be one of the following values: 201 | * @arg SysTick_CLKSource_HCLK_Div8: AHB clock divided by 8 selected as SysTick clock source. 202 | * @arg SysTick_CLKSource_HCLK: AHB clock selected as SysTick clock source. 203 | * @retval None 204 | */ 205 | void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource) 206 | { 207 | /* Check the parameters */ 208 | assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource)); 209 | if (SysTick_CLKSource == SysTick_CLKSource_HCLK) 210 | { 211 | SysTick->CTRL |= SysTick_CLKSource_HCLK; 212 | } 213 | else 214 | { 215 | SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8; 216 | } 217 | } 218 | 219 | /** 220 | * @} 221 | */ 222 | 223 | /** 224 | * @} 225 | */ 226 | 227 | /** 228 | * @} 229 | */ 230 | 231 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 232 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/src/stm32f10x_exti.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_exti.c 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file provides all the EXTI firmware functions. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm32f10x_exti.h" 30 | 31 | /** @addtogroup STM32F10x_StdPeriph_Driver 32 | * @{ 33 | */ 34 | 35 | /** @defgroup EXTI 36 | * @brief EXTI driver modules 37 | * @{ 38 | */ 39 | 40 | /** @defgroup EXTI_Private_TypesDefinitions 41 | * @{ 42 | */ 43 | 44 | /** 45 | * @} 46 | */ 47 | 48 | /** @defgroup EXTI_Private_Defines 49 | * @{ 50 | */ 51 | 52 | #define EXTI_LINENONE ((uint32_t)0x00000) /* No interrupt selected */ 53 | 54 | /** 55 | * @} 56 | */ 57 | 58 | /** @defgroup EXTI_Private_Macros 59 | * @{ 60 | */ 61 | 62 | /** 63 | * @} 64 | */ 65 | 66 | /** @defgroup EXTI_Private_Variables 67 | * @{ 68 | */ 69 | 70 | /** 71 | * @} 72 | */ 73 | 74 | /** @defgroup EXTI_Private_FunctionPrototypes 75 | * @{ 76 | */ 77 | 78 | /** 79 | * @} 80 | */ 81 | 82 | /** @defgroup EXTI_Private_Functions 83 | * @{ 84 | */ 85 | 86 | /** 87 | * @brief Deinitializes the EXTI peripheral registers to their default reset values. 88 | * @param None 89 | * @retval None 90 | */ 91 | void EXTI_DeInit(void) 92 | { 93 | EXTI->IMR = 0x00000000; 94 | EXTI->EMR = 0x00000000; 95 | EXTI->RTSR = 0x00000000; 96 | EXTI->FTSR = 0x00000000; 97 | EXTI->PR = 0x000FFFFF; 98 | } 99 | 100 | /** 101 | * @brief Initializes the EXTI peripheral according to the specified 102 | * parameters in the EXTI_InitStruct. 103 | * @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure 104 | * that contains the configuration information for the EXTI peripheral. 105 | * @retval None 106 | */ 107 | void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct) 108 | { 109 | uint32_t tmp = 0; 110 | 111 | /* Check the parameters */ 112 | assert_param(IS_EXTI_MODE(EXTI_InitStruct->EXTI_Mode)); 113 | assert_param(IS_EXTI_TRIGGER(EXTI_InitStruct->EXTI_Trigger)); 114 | assert_param(IS_EXTI_LINE(EXTI_InitStruct->EXTI_Line)); 115 | assert_param(IS_FUNCTIONAL_STATE(EXTI_InitStruct->EXTI_LineCmd)); 116 | 117 | tmp = (uint32_t)EXTI_BASE; 118 | 119 | if (EXTI_InitStruct->EXTI_LineCmd != DISABLE) 120 | { 121 | /* Clear EXTI line configuration */ 122 | EXTI->IMR &= ~EXTI_InitStruct->EXTI_Line; 123 | EXTI->EMR &= ~EXTI_InitStruct->EXTI_Line; 124 | 125 | tmp += EXTI_InitStruct->EXTI_Mode; 126 | 127 | *(__IO uint32_t *) tmp |= EXTI_InitStruct->EXTI_Line; 128 | 129 | /* Clear Rising Falling edge configuration */ 130 | EXTI->RTSR &= ~EXTI_InitStruct->EXTI_Line; 131 | EXTI->FTSR &= ~EXTI_InitStruct->EXTI_Line; 132 | 133 | /* Select the trigger for the selected external interrupts */ 134 | if (EXTI_InitStruct->EXTI_Trigger == EXTI_Trigger_Rising_Falling) 135 | { 136 | /* Rising Falling edge */ 137 | EXTI->RTSR |= EXTI_InitStruct->EXTI_Line; 138 | EXTI->FTSR |= EXTI_InitStruct->EXTI_Line; 139 | } 140 | else 141 | { 142 | tmp = (uint32_t)EXTI_BASE; 143 | tmp += EXTI_InitStruct->EXTI_Trigger; 144 | 145 | *(__IO uint32_t *) tmp |= EXTI_InitStruct->EXTI_Line; 146 | } 147 | } 148 | else 149 | { 150 | tmp += EXTI_InitStruct->EXTI_Mode; 151 | 152 | /* Disable the selected external lines */ 153 | *(__IO uint32_t *) tmp &= ~EXTI_InitStruct->EXTI_Line; 154 | } 155 | } 156 | 157 | /** 158 | * @brief Fills each EXTI_InitStruct member with its reset value. 159 | * @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure which will 160 | * be initialized. 161 | * @retval None 162 | */ 163 | void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct) 164 | { 165 | EXTI_InitStruct->EXTI_Line = EXTI_LINENONE; 166 | EXTI_InitStruct->EXTI_Mode = EXTI_Mode_Interrupt; 167 | EXTI_InitStruct->EXTI_Trigger = EXTI_Trigger_Falling; 168 | EXTI_InitStruct->EXTI_LineCmd = DISABLE; 169 | } 170 | 171 | /** 172 | * @brief Generates a Software interrupt. 173 | * @param EXTI_Line: specifies the EXTI lines to be enabled or disabled. 174 | * This parameter can be any combination of EXTI_Linex where x can be (0..19). 175 | * @retval None 176 | */ 177 | void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line) 178 | { 179 | /* Check the parameters */ 180 | assert_param(IS_EXTI_LINE(EXTI_Line)); 181 | 182 | EXTI->SWIER |= EXTI_Line; 183 | } 184 | 185 | /** 186 | * @brief Checks whether the specified EXTI line flag is set or not. 187 | * @param EXTI_Line: specifies the EXTI line flag to check. 188 | * This parameter can be: 189 | * @arg EXTI_Linex: External interrupt line x where x(0..19) 190 | * @retval The new state of EXTI_Line (SET or RESET). 191 | */ 192 | FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line) 193 | { 194 | FlagStatus bitstatus = RESET; 195 | /* Check the parameters */ 196 | assert_param(IS_GET_EXTI_LINE(EXTI_Line)); 197 | 198 | if ((EXTI->PR & EXTI_Line) != (uint32_t)RESET) 199 | { 200 | bitstatus = SET; 201 | } 202 | else 203 | { 204 | bitstatus = RESET; 205 | } 206 | return bitstatus; 207 | } 208 | 209 | /** 210 | * @brief Clears the EXTI's line pending flags. 211 | * @param EXTI_Line: specifies the EXTI lines flags to clear. 212 | * This parameter can be any combination of EXTI_Linex where x can be (0..19). 213 | * @retval None 214 | */ 215 | void EXTI_ClearFlag(uint32_t EXTI_Line) 216 | { 217 | /* Check the parameters */ 218 | assert_param(IS_EXTI_LINE(EXTI_Line)); 219 | 220 | EXTI->PR = EXTI_Line; 221 | } 222 | 223 | /** 224 | * @brief Checks whether the specified EXTI line is asserted or not. 225 | * @param EXTI_Line: specifies the EXTI line to check. 226 | * This parameter can be: 227 | * @arg EXTI_Linex: External interrupt line x where x(0..19) 228 | * @retval The new state of EXTI_Line (SET or RESET). 229 | */ 230 | ITStatus EXTI_GetITStatus(uint32_t EXTI_Line) 231 | { 232 | ITStatus bitstatus = RESET; 233 | uint32_t enablestatus = 0; 234 | /* Check the parameters */ 235 | assert_param(IS_GET_EXTI_LINE(EXTI_Line)); 236 | 237 | enablestatus = EXTI->IMR & EXTI_Line; 238 | if (((EXTI->PR & EXTI_Line) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET)) 239 | { 240 | bitstatus = SET; 241 | } 242 | else 243 | { 244 | bitstatus = RESET; 245 | } 246 | return bitstatus; 247 | } 248 | 249 | /** 250 | * @brief Clears the EXTI's line pending bits. 251 | * @param EXTI_Line: specifies the EXTI lines to clear. 252 | * This parameter can be any combination of EXTI_Linex where x can be (0..19). 253 | * @retval None 254 | */ 255 | void EXTI_ClearITPendingBit(uint32_t EXTI_Line) 256 | { 257 | /* Check the parameters */ 258 | assert_param(IS_EXTI_LINE(EXTI_Line)); 259 | 260 | EXTI->PR = EXTI_Line; 261 | } 262 | 263 | /** 264 | * @} 265 | */ 266 | 267 | /** 268 | * @} 269 | */ 270 | 271 | /** 272 | * @} 273 | */ 274 | 275 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 276 | -------------------------------------------------------------------------------- /stm/STM32_USB-FS-Device_Driver/src/usb_int.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_int.c 4 | * @author MCD Application Team 5 | * @version V4.1.0 6 | * @date 26-May-2017 7 | * @brief Endpoint CTR (Low and High) interrupt's service routines 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© Copyright (c) 2017 STMicroelectronics International N.V. 12 | * All rights reserved.

13 | * 14 | * Redistribution and use in source and binary forms, with or without 15 | * modification, are permitted, provided that the following conditions are met: 16 | * 17 | * 1. Redistribution of source code must retain the above copyright notice, 18 | * this list of conditions and the following disclaimer. 19 | * 2. Redistributions in binary form must reproduce the above copyright notice, 20 | * this list of conditions and the following disclaimer in the documentation 21 | * and/or other materials provided with the distribution. 22 | * 3. Neither the name of STMicroelectronics nor the names of other 23 | * contributors to this software may be used to endorse or promote products 24 | * derived from this software without specific written permission. 25 | * 4. This software, including modifications and/or derivative works of this 26 | * software, must execute solely and exclusively on microcontroller or 27 | * microprocessor devices manufactured by or for STMicroelectronics. 28 | * 5. Redistribution and use of this software other than as permitted under 29 | * this license is void and will automatically terminate your rights under 30 | * this license. 31 | * 32 | * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" 33 | * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT 34 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 35 | * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY 36 | * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT 37 | * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 38 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 39 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 40 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 41 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 42 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 43 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 44 | * 45 | ****************************************************************************** 46 | */ 47 | 48 | /* Includes ------------------------------------------------------------------*/ 49 | #include "usb_lib.h" 50 | 51 | /* Private typedef -----------------------------------------------------------*/ 52 | /* Private define ------------------------------------------------------------*/ 53 | /* Private macro -------------------------------------------------------------*/ 54 | /* Private variables ---------------------------------------------------------*/ 55 | __IO uint16_t SaveRState; 56 | __IO uint16_t SaveTState; 57 | 58 | /* Extern variables ----------------------------------------------------------*/ 59 | extern void (*pEpInt_IN[7])(void); /* Handles IN interrupts */ 60 | extern void (*pEpInt_OUT[7])(void); /* Handles OUT interrupts */ 61 | 62 | /* Private function prototypes -----------------------------------------------*/ 63 | /* Private functions ---------------------------------------------------------*/ 64 | 65 | /** 66 | * Function Name : CTR_LP. 67 | * Description : Low priority Endpoint Correct Transfer interrupt's service 68 | * routine. 69 | * Input : None. 70 | * Output : None. 71 | * Return : None. 72 | **/ 73 | void CTR_LP(void) 74 | { 75 | __IO uint16_t wEPVal = 0; 76 | /* stay in loop while pending interrupts */ 77 | while (((wIstr = _GetISTR()) & ISTR_CTR) != 0) 78 | { 79 | /* extract highest priority endpoint number */ 80 | EPindex = (uint8_t)(wIstr & ISTR_EP_ID); 81 | if (EPindex == 0) 82 | { 83 | /* Decode and service control endpoint interrupt */ 84 | /* calling related service routine */ 85 | /* (Setup0_Process, In0_Process, Out0_Process) */ 86 | 87 | /* save RX & TX status */ 88 | /* and set both to NAK */ 89 | 90 | SaveRState = _GetENDPOINT(ENDP0); 91 | SaveTState = SaveRState & EPTX_STAT; 92 | SaveRState &= EPRX_STAT; 93 | 94 | _SetEPRxTxStatus(ENDP0,EP_RX_NAK,EP_TX_NAK); 95 | 96 | /* DIR bit = origin of the interrupt */ 97 | 98 | if ((wIstr & ISTR_DIR) == 0) 99 | { 100 | /* DIR = 0 */ 101 | 102 | /* DIR = 0 => IN int */ 103 | /* DIR = 0 implies that (EP_CTR_TX = 1) always */ 104 | 105 | _ClearEP_CTR_TX(ENDP0); 106 | In0_Process(); 107 | 108 | /* before terminate set Tx & Rx status */ 109 | 110 | _SetEPRxTxStatus(ENDP0,SaveRState,SaveTState); 111 | return; 112 | } 113 | else 114 | { 115 | /* DIR = 1 */ 116 | 117 | /* DIR = 1 & CTR_RX => SETUP or OUT int */ 118 | /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */ 119 | 120 | wEPVal = _GetENDPOINT(ENDP0); 121 | 122 | if ((wEPVal &EP_SETUP) != 0) 123 | { 124 | _ClearEP_CTR_RX(ENDP0); /* SETUP bit kept frozen while CTR_RX = 1 */ 125 | Setup0_Process(); 126 | /* before terminate set Tx & Rx status */ 127 | 128 | _SetEPRxTxStatus(ENDP0,SaveRState,SaveTState); 129 | return; 130 | } 131 | 132 | else if ((wEPVal & EP_CTR_RX) != 0) 133 | { 134 | _ClearEP_CTR_RX(ENDP0); 135 | Out0_Process(); 136 | /* before terminate set Tx & Rx status */ 137 | 138 | _SetEPRxTxStatus(ENDP0,SaveRState,SaveTState); 139 | return; 140 | } 141 | } 142 | }/* if(EPindex == 0) */ 143 | else 144 | { 145 | /* Decode and service non control endpoints interrupt */ 146 | 147 | /* process related endpoint register */ 148 | wEPVal = _GetENDPOINT(EPindex); 149 | if ((wEPVal & EP_CTR_RX) != 0) 150 | { 151 | /* clear int flag */ 152 | _ClearEP_CTR_RX(EPindex); 153 | 154 | /* call OUT service function */ 155 | (*pEpInt_OUT[EPindex-1])(); 156 | 157 | } /* if((wEPVal & EP_CTR_RX) */ 158 | 159 | if ((wEPVal & EP_CTR_TX) != 0) 160 | { 161 | /* clear int flag */ 162 | _ClearEP_CTR_TX(EPindex); 163 | 164 | /* call IN service function */ 165 | (*pEpInt_IN[EPindex-1])(); 166 | } /* if((wEPVal & EP_CTR_TX) != 0) */ 167 | 168 | }/* if(EPindex == 0) else */ 169 | 170 | }/* while(...) */ 171 | } 172 | 173 | /** 174 | * Function Name : CTR_HP. 175 | * Description : High Priority Endpoint Correct Transfer interrupt's service 176 | * routine. 177 | * Input : None. 178 | * Output : None. 179 | * Return : None. 180 | **/ 181 | void CTR_HP(void) 182 | { 183 | uint32_t wEPVal = 0; 184 | 185 | while (((wIstr = _GetISTR()) & ISTR_CTR) != 0) 186 | { 187 | _SetISTR((uint16_t)CLR_CTR); /* clear CTR flag */ 188 | /* extract highest priority endpoint number */ 189 | EPindex = (uint8_t)(wIstr & ISTR_EP_ID); 190 | /* process related endpoint register */ 191 | wEPVal = _GetENDPOINT(EPindex); 192 | if ((wEPVal & EP_CTR_RX) != 0) 193 | { 194 | /* clear int flag */ 195 | _ClearEP_CTR_RX(EPindex); 196 | 197 | /* call OUT service function */ 198 | (*pEpInt_OUT[EPindex-1])(); 199 | 200 | } /* if((wEPVal & EP_CTR_RX) */ 201 | else if ((wEPVal & EP_CTR_TX) != 0) 202 | { 203 | /* clear int flag */ 204 | _ClearEP_CTR_TX(EPindex); 205 | 206 | /* call IN service function */ 207 | (*pEpInt_IN[EPindex-1])(); 208 | 209 | 210 | } /* if((wEPVal & EP_CTR_TX) != 0) */ 211 | 212 | }/* while(...) */ 213 | } 214 | 215 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 216 | -------------------------------------------------------------------------------- /stm/STM32F10x_StdPeriph_Driver/inc/stm32f10x_bkp.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_bkp.h 4 | * @author MCD Application Team 5 | * @version V3.6.1 6 | * @date 05-March-2012 7 | * @brief This file contains all the functions prototypes for the BKP firmware 8 | * library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | *

© COPYRIGHT 2012 STMicroelectronics

13 | * 14 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 15 | * You may not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at: 17 | * 18 | * http://www.st.com/software_license_agreement_liberty_v2 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, 22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | ****************************************************************************** 27 | */ 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __STM32F10x_BKP_H 31 | #define __STM32F10x_BKP_H 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | /* Includes ------------------------------------------------------------------*/ 38 | #include "stm32f10x.h" 39 | 40 | /** @addtogroup STM32F10x_StdPeriph_Driver 41 | * @{ 42 | */ 43 | 44 | /** @addtogroup BKP 45 | * @{ 46 | */ 47 | 48 | /** @defgroup BKP_Exported_Types 49 | * @{ 50 | */ 51 | 52 | /** 53 | * @} 54 | */ 55 | 56 | /** @defgroup BKP_Exported_Constants 57 | * @{ 58 | */ 59 | 60 | /** @defgroup Tamper_Pin_active_level 61 | * @{ 62 | */ 63 | 64 | #define BKP_TamperPinLevel_High ((uint16_t)0x0000) 65 | #define BKP_TamperPinLevel_Low ((uint16_t)0x0001) 66 | #define IS_BKP_TAMPER_PIN_LEVEL(LEVEL) (((LEVEL) == BKP_TamperPinLevel_High) || \ 67 | ((LEVEL) == BKP_TamperPinLevel_Low)) 68 | /** 69 | * @} 70 | */ 71 | 72 | /** @defgroup RTC_output_source_to_output_on_the_Tamper_pin 73 | * @{ 74 | */ 75 | 76 | #define BKP_RTCOutputSource_None ((uint16_t)0x0000) 77 | #define BKP_RTCOutputSource_CalibClock ((uint16_t)0x0080) 78 | #define BKP_RTCOutputSource_Alarm ((uint16_t)0x0100) 79 | #define BKP_RTCOutputSource_Second ((uint16_t)0x0300) 80 | #define IS_BKP_RTC_OUTPUT_SOURCE(SOURCE) (((SOURCE) == BKP_RTCOutputSource_None) || \ 81 | ((SOURCE) == BKP_RTCOutputSource_CalibClock) || \ 82 | ((SOURCE) == BKP_RTCOutputSource_Alarm) || \ 83 | ((SOURCE) == BKP_RTCOutputSource_Second)) 84 | /** 85 | * @} 86 | */ 87 | 88 | /** @defgroup Data_Backup_Register 89 | * @{ 90 | */ 91 | 92 | #define BKP_DR1 ((uint16_t)0x0004) 93 | #define BKP_DR2 ((uint16_t)0x0008) 94 | #define BKP_DR3 ((uint16_t)0x000C) 95 | #define BKP_DR4 ((uint16_t)0x0010) 96 | #define BKP_DR5 ((uint16_t)0x0014) 97 | #define BKP_DR6 ((uint16_t)0x0018) 98 | #define BKP_DR7 ((uint16_t)0x001C) 99 | #define BKP_DR8 ((uint16_t)0x0020) 100 | #define BKP_DR9 ((uint16_t)0x0024) 101 | #define BKP_DR10 ((uint16_t)0x0028) 102 | #define BKP_DR11 ((uint16_t)0x0040) 103 | #define BKP_DR12 ((uint16_t)0x0044) 104 | #define BKP_DR13 ((uint16_t)0x0048) 105 | #define BKP_DR14 ((uint16_t)0x004C) 106 | #define BKP_DR15 ((uint16_t)0x0050) 107 | #define BKP_DR16 ((uint16_t)0x0054) 108 | #define BKP_DR17 ((uint16_t)0x0058) 109 | #define BKP_DR18 ((uint16_t)0x005C) 110 | #define BKP_DR19 ((uint16_t)0x0060) 111 | #define BKP_DR20 ((uint16_t)0x0064) 112 | #define BKP_DR21 ((uint16_t)0x0068) 113 | #define BKP_DR22 ((uint16_t)0x006C) 114 | #define BKP_DR23 ((uint16_t)0x0070) 115 | #define BKP_DR24 ((uint16_t)0x0074) 116 | #define BKP_DR25 ((uint16_t)0x0078) 117 | #define BKP_DR26 ((uint16_t)0x007C) 118 | #define BKP_DR27 ((uint16_t)0x0080) 119 | #define BKP_DR28 ((uint16_t)0x0084) 120 | #define BKP_DR29 ((uint16_t)0x0088) 121 | #define BKP_DR30 ((uint16_t)0x008C) 122 | #define BKP_DR31 ((uint16_t)0x0090) 123 | #define BKP_DR32 ((uint16_t)0x0094) 124 | #define BKP_DR33 ((uint16_t)0x0098) 125 | #define BKP_DR34 ((uint16_t)0x009C) 126 | #define BKP_DR35 ((uint16_t)0x00A0) 127 | #define BKP_DR36 ((uint16_t)0x00A4) 128 | #define BKP_DR37 ((uint16_t)0x00A8) 129 | #define BKP_DR38 ((uint16_t)0x00AC) 130 | #define BKP_DR39 ((uint16_t)0x00B0) 131 | #define BKP_DR40 ((uint16_t)0x00B4) 132 | #define BKP_DR41 ((uint16_t)0x00B8) 133 | #define BKP_DR42 ((uint16_t)0x00BC) 134 | 135 | #define IS_BKP_DR(DR) (((DR) == BKP_DR1) || ((DR) == BKP_DR2) || ((DR) == BKP_DR3) || \ 136 | ((DR) == BKP_DR4) || ((DR) == BKP_DR5) || ((DR) == BKP_DR6) || \ 137 | ((DR) == BKP_DR7) || ((DR) == BKP_DR8) || ((DR) == BKP_DR9) || \ 138 | ((DR) == BKP_DR10) || ((DR) == BKP_DR11) || ((DR) == BKP_DR12) || \ 139 | ((DR) == BKP_DR13) || ((DR) == BKP_DR14) || ((DR) == BKP_DR15) || \ 140 | ((DR) == BKP_DR16) || ((DR) == BKP_DR17) || ((DR) == BKP_DR18) || \ 141 | ((DR) == BKP_DR19) || ((DR) == BKP_DR20) || ((DR) == BKP_DR21) || \ 142 | ((DR) == BKP_DR22) || ((DR) == BKP_DR23) || ((DR) == BKP_DR24) || \ 143 | ((DR) == BKP_DR25) || ((DR) == BKP_DR26) || ((DR) == BKP_DR27) || \ 144 | ((DR) == BKP_DR28) || ((DR) == BKP_DR29) || ((DR) == BKP_DR30) || \ 145 | ((DR) == BKP_DR31) || ((DR) == BKP_DR32) || ((DR) == BKP_DR33) || \ 146 | ((DR) == BKP_DR34) || ((DR) == BKP_DR35) || ((DR) == BKP_DR36) || \ 147 | ((DR) == BKP_DR37) || ((DR) == BKP_DR38) || ((DR) == BKP_DR39) || \ 148 | ((DR) == BKP_DR40) || ((DR) == BKP_DR41) || ((DR) == BKP_DR42)) 149 | 150 | #define IS_BKP_CALIBRATION_VALUE(VALUE) ((VALUE) <= 0x7F) 151 | /** 152 | * @} 153 | */ 154 | 155 | /** 156 | * @} 157 | */ 158 | 159 | /** @defgroup BKP_Exported_Macros 160 | * @{ 161 | */ 162 | 163 | /** 164 | * @} 165 | */ 166 | 167 | /** @defgroup BKP_Exported_Functions 168 | * @{ 169 | */ 170 | 171 | void BKP_DeInit(void); 172 | void BKP_TamperPinLevelConfig(uint16_t BKP_TamperPinLevel); 173 | void BKP_TamperPinCmd(FunctionalState NewState); 174 | void BKP_ITConfig(FunctionalState NewState); 175 | void BKP_RTCOutputConfig(uint16_t BKP_RTCOutputSource); 176 | void BKP_SetRTCCalibrationValue(uint8_t CalibrationValue); 177 | void BKP_WriteBackupRegister(uint16_t BKP_DR, uint16_t Data); 178 | uint16_t BKP_ReadBackupRegister(uint16_t BKP_DR); 179 | FlagStatus BKP_GetFlagStatus(void); 180 | void BKP_ClearFlag(void); 181 | ITStatus BKP_GetITStatus(void); 182 | void BKP_ClearITPendingBit(void); 183 | 184 | #ifdef __cplusplus 185 | } 186 | #endif 187 | 188 | #endif /* __STM32F10x_BKP_H */ 189 | /** 190 | * @} 191 | */ 192 | 193 | /** 194 | * @} 195 | */ 196 | 197 | /** 198 | * @} 199 | */ 200 | 201 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 202 | --------------------------------------------------------------------------------