├── .gitignore ├── CREDITS ├── Makefile ├── README ├── README.md ├── binaries ├── cc3d.bin ├── dso138_boot20.bin ├── gd32f1_frankenmaple.bin ├── gd32f1_generic_boot20_pc13.bin ├── generic-none_bootloader.bin ├── generic_boot20_hytiny.bin ├── generic_boot20_pa1.bin ├── generic_boot20_pa1_button_pa8.bin ├── generic_boot20_pa9.bin ├── generic_boot20_pb0.bin ├── generic_boot20_pb12.bin ├── generic_boot20_pb7.bin ├── generic_boot20_pb9.bin ├── generic_boot20_pc13.bin ├── generic_boot20_pc13_fastboot.bin ├── generic_boot20_pd1.bin ├── generic_boot20_pd2.bin ├── generic_boot20_pe2.bin ├── generic_boot20_pe5.bin ├── generic_boot20_pe5_button_pa0.bin ├── generic_boot20_pg15.bin ├── maple_mini_boot20.bin ├── maple_rev3_boot20.bin ├── maple_rev5_boot20.bin ├── naze32_boot20.bin ├── smart-v2.bin └── stbee_boot20.bin ├── bootloader_only_binaries ├── cc3d.bin ├── dso138_boot20.bin ├── gd32f1_frankenmaple.bin ├── gd32f1_generic_boot20_pc13.bin ├── generic-none_bootloader.bin ├── generic_boot20_hytiny.bin ├── generic_boot20_pa1.bin ├── generic_boot20_pa1_button_pa8.bin ├── generic_boot20_pa9.bin ├── generic_boot20_pb0.bin ├── generic_boot20_pb12.bin ├── generic_boot20_pb7.bin ├── generic_boot20_pb9.bin ├── generic_boot20_pc13.bin ├── generic_boot20_pc13_fastboot.bin ├── generic_boot20_pd1.bin ├── generic_boot20_pd2.bin ├── generic_boot20_pe2.bin ├── generic_boot20_pe5.bin ├── generic_boot20_pe5_button_pa0.bin ├── generic_boot20_pg15.bin ├── maple_mini_boot20.bin ├── maple_rev3_boot20.bin ├── maple_rev5_boot20.bin ├── naze32_boot20.bin ├── smart-v2.bin └── stbee_boot20.bin ├── common.h ├── config.h ├── dfu.c ├── dfu.h ├── flash ├── debug.cfg ├── flash.cfg ├── openocd.cfg ├── stm32.cfg └── stm32loader.py ├── hardware.c ├── hardware.h ├── main.c ├── make_all.bat ├── merge_sketch_and_bootloaders.bat ├── platformio.ini ├── sketch_combiner ├── bin_merge_tool │ ├── main.c │ └── mergesketch.exe └── congratulations_sketch │ ├── congratulations_sketch.ino │ ├── congratulations_sketch.ino.generic_gd32f103c.bin │ ├── congratulations_sketch.ino.generic_stm32f103c.bin │ └── congratulations_sketch.ino.maple_mini.bin ├── stm32_lib ├── c_only_md.ld ├── c_only_md_RAM.ld ├── c_only_md_high_density.ld ├── c_only_startup.s ├── c_only_startup_user.s ├── cortexm3_macro.h ├── cortexm3_macro.s └── stm32f10x_type.h ├── updater_gd32f1 └── updater_gd32f1.ino ├── updater_stm32f1 └── updater_stm32f1.ino ├── usb.c ├── usb.h ├── usb_callbacks.c ├── usb_descriptor.c ├── usb_descriptor.h ├── usb_descriptor_strings_util.html └── usb_lib ├── usb_conf.h ├── usb_core.c ├── usb_core.h ├── usb_def.h ├── usb_init.c ├── usb_init.h ├── usb_int.c ├── usb_int.h ├── usb_lib.h ├── usb_mem.c ├── usb_mem.h ├── usb_regs.c ├── usb_regs.h └── usb_type.h /.gitignore: -------------------------------------------------------------------------------- 1 | .dep 2 | TAGS 3 | tags 4 | cscope.out 5 | build 6 | *~ 7 | .pioenvs 8 | .piolibdeps 9 | .vscode/.browse.c_cpp.db* 10 | .vscode/c_cpp_properties.json 11 | .vscode/launch.json 12 | -------------------------------------------------------------------------------- /CREDITS: -------------------------------------------------------------------------------- 1 | This is at least a partial credits-file of people that have 2 | contributed to the Maple bootloader. It is formatted the same way the 3 | Linux kernel CREDITS file is structured: sorted by name and formatted 4 | for easy processing. 5 | 6 | The fields are: name (N), email (E), web-address (W), description (D). 7 | 8 | ---------- 9 | 10 | N: Tormod Volden 11 | E: debian.tormod@gmail.com 12 | D: Fixes for DFU compliance 13 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | 2 | FILES ------------------------------------------------------------------------- 3 | 4 | stm32lib/* 5 | - all the (possibly consolidated) stm32 lib and usb example code 6 | 7 | usb.c 8 | - USB-specific hardware setup. Interrupts, clocks, etc. handling USB when 9 | not "Attached". some low-level callbacks (low power mode, init, reset, 10 | resume, etc). 11 | 12 | usb_callbacks.c 13 | - aka endpoints: handling data transfer when "Configured". calls out to 14 | application specific callbacks (i.e. DFU). 15 | 16 | usb_descriptor.c 17 | - aka application descriptor; big static struct and callbacks for sending 18 | the descriptor. 19 | 20 | main.c 21 | - main loop and calling any hardware init stuff. timing hacks for EEPROM 22 | writes not to block usb interrupts. logic to handle 2 second timeout then 23 | jump to user code. 24 | 25 | hardware.c 26 | - init routines to setup clocks, interrupts, also destructor functions. 27 | does not include USB stuff. EEPROM read/write functions. 28 | 29 | dfu.c 30 | - mostly the giant FSM case switch, also some USB endpoint callbacks 31 | 32 | TODO -------------------------------------------------------------------------- 33 | 34 | * pack the structs 35 | * use sizeof() for usb application descriptor once structs are packed 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # STM32duino-bootloader 2 | 3 | Please Note: This code does not work with all STM32F103 Boards 4 | 5 | Also Note: Use GCC 4.8 (not 4.9 or newer, as those versions have more aggressive optimisation which cause hardware registers to be read incorrectly and consequently the bootloader does not work) 6 | 7 | 8 | Bootloader for STM32F103 boards, for use with the Arduino_STM32 repo and the Arduino IDE 9 | 10 | This repo is a derivation of https://github.com/jonatanolofsson/maple-bootloader (mini-boot branch) which is in turn a derivation of the maple-bootloader created by Leaflabs http://github.com/leaflabs/maple-bootloader 11 | 12 | The bootloader has been reworked so that all versions are contained in the Master branch, rather than 1 branch per version / board. 13 | 14 | Note. 15 | The upload to RAM option has effectively been removed, as it was useless and caused problems. 16 | The bootloader still has a DFU - AltID for RAM uploads, however it returns an error if the host attempts to uplaod to the AltID. The AltID was kept to allow backward compatibility with anyone still using the old Maple IDE which has a upload to RAM option. 17 | 18 | The makefile now has multple build targets including "maple-mini" (roughly equivalent to the old mini-boot branch), maple-rev3 etc etc. 19 | 20 | Additionally, the bootloader now works with "generic" STM32F103 boards that do not have the additional USB reset hardware which all Maple and Maple mini boards have. 21 | 22 | On "generic" boards, the USB reset (to force re-enumeration by the host), is triggered by reconfiguring the USB D+ line (PA12) into GPIO mode, and driving PA12 low for a short period, before setting the pin back to its USB operational mode. 23 | This USB reset method was written by @Victor_pv. 24 | Note: It is not guaranteed to work on all "generic" STM32 boards, and relies on PA12 having a pull-up resistor of around 1.5k - however most "generic" boards seem to have this. 25 | Its unclear if this method to reset the USB bus conforms precisely to the USB standard, but it seems to work fine on all PCs, Macs and Linux boxes on which it's been tested - and seems usable for hobby / non commericial / non-critical systems. 26 | 27 | 28 | There are multiple build targets for "generic" STM32F103 boards - because each vendor seems to have the "LED" on a different port/pin - and the bootloader flashes the LED to indicate the current operation / state. 29 | 30 | So if your board has an LED on pin PC13 the makefile target is "generic-pc13." At the time of writing, the following generic targets are available (more may be added without this readme being updated each time, so please check the makefile to see the latest list of build targets) 31 | 32 | generic-pc13 33 | generic-pg15 34 | generic-pd2 35 | generic-pd1 36 | generic-pa1 37 | generic-pb9 38 | 39 | In addition to the LED, differences in the build targets are: 40 | (a) whether the device has maple USB reset hardware. 41 | (b) Which pin the "button" is attached to. 42 | 43 | Note: Most "generic" STM32F103 boards only have a reset button, and not a user / test button. So the bootloader code always configures the Button input pin as PullDown. Hence, if a button is not present on the Button pin (Default is PC14), the pin should remain in a LOW state, and the bootloader will assume the Button is not being pressed. 44 | 45 | IMPORTANT! 46 | If your board has external hardware attached to pin PC12 and it pulls that pin HIGH, you will need to make a new build target for your board which uses a different pin for the Button, or change the code to make it ignore the Button. 47 | 48 | The configuration for each build target / board is defined in config.h 49 | 50 | The Makefile sets the board name as a #define e.g. #define TARGET_GENERIC_F103_PD2, and config.h contains a block of config defines for each board. 51 | 52 | e.g. 53 | 54 | ``` 55 | #define HAS_MAPLE_HARDWARE 1 56 | #define LED_BANK GPIOB 57 | #define LED_PIN 1 58 | #define LED_ON_STATE 1 59 | /* On the Mini, BUT is PB8 */ 60 | #define BUTTON_BANK GPIOB 61 | #define BUTTON 8 62 | /* USB Disc Pin Setup. USB DISC is PB9 */ 63 | #define USB_DISC_BANK GPIOB 64 | #define USB_DISC 9 65 | #define USER_CODE_RAM ((u32)0x20000C00) 66 | #define RAM_END ((u32)0x20005000) 67 | ``` 68 | 69 | Boards which have the Maple USB reset hardware need to defined HAS_MAPLE_HARDWARE 70 | 71 | There are definitions for LED_BANK (GPIO Port) 72 | LED pin (Pin in that port) 73 | LED_ON_STATE (Whether the LED is lit when the pin is high (1) or low (0) - Note: this is because some generic boards have the LED between Vcc and the pin, hence are on when the LED PIN is LOW) 74 | 75 | Button bank (port) and pin 76 | 77 | For boards with Maple hardware, the "Disconnect" pin USB_DISC need to be defined. 78 | On generic" boards, as this is always PA12 - as USB D+ is always on this pin on STM32F103 devices. 79 | (Note: For generic boards, this define could probably be moved to the usb code as it never changes) 80 | 81 | Note: 82 | USER_CODE_RAM and RAM_END are allways the same, and probably need to be consolidated, rather than defined for each board. 83 | 84 | There is no longer a need to configure FLASH SIZE, as it is detemined by reading the size from a register in the processor. 85 | The Flash page (and hence DFU upload block size) no longer needs to be defined either, as the flash page size is inferred from the total flash size. All devices up to 128kb have a 1k page size, and boards with more than 128k of flash have a 2k page size. 86 | 87 | Note the code directly links flash page size to DFU block size. It would probably be possible to decouple this and possibly increase upload speeds, but it has not been done yet. 88 | 89 | 90 | #####Other improvements on the original Maple bootloader 91 | 92 | 1. Smaller footprint - now only 8k (This became possible due to code changes by @jonatanolofsson which allowed the GCC optimisation for size flag to be used 93 | 2. Additional DFU AltID upload type was added, which allows the sketch to be loaded at 0x8002000 instead of 0x8005000 (due to the reduced size of the bootloader itself), 94 | Note: Upload to 0x8005000 was retained for backward compatibility. 95 | 96 | 97 | If you have questions about the bootloader please raise an issue and I will attempt to answer them. 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /binaries/cc3d.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/cc3d.bin -------------------------------------------------------------------------------- /binaries/dso138_boot20.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/dso138_boot20.bin -------------------------------------------------------------------------------- /binaries/gd32f1_frankenmaple.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/gd32f1_frankenmaple.bin -------------------------------------------------------------------------------- /binaries/gd32f1_generic_boot20_pc13.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/gd32f1_generic_boot20_pc13.bin -------------------------------------------------------------------------------- /binaries/generic-none_bootloader.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic-none_bootloader.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_hytiny.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_hytiny.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pa1.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pa1.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pa1_button_pa8.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pa1_button_pa8.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pa9.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pa9.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pb0.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pb0.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pb12.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pb12.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pb7.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pb7.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pb9.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pb9.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pc13.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pc13.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pc13_fastboot.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pc13_fastboot.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pd1.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pd1.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pd2.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pd2.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pe2.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pe2.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pe5.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pe5.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pe5_button_pa0.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pe5_button_pa0.bin -------------------------------------------------------------------------------- /binaries/generic_boot20_pg15.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/generic_boot20_pg15.bin -------------------------------------------------------------------------------- /binaries/maple_mini_boot20.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/maple_mini_boot20.bin -------------------------------------------------------------------------------- /binaries/maple_rev3_boot20.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/maple_rev3_boot20.bin -------------------------------------------------------------------------------- /binaries/maple_rev5_boot20.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/maple_rev5_boot20.bin -------------------------------------------------------------------------------- /binaries/naze32_boot20.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/naze32_boot20.bin -------------------------------------------------------------------------------- /binaries/smart-v2.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/smart-v2.bin -------------------------------------------------------------------------------- /binaries/stbee_boot20.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/binaries/stbee_boot20.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/cc3d.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/cc3d.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/dso138_boot20.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/dso138_boot20.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/gd32f1_frankenmaple.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/gd32f1_frankenmaple.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/gd32f1_generic_boot20_pc13.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/gd32f1_generic_boot20_pc13.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic-none_bootloader.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic-none_bootloader.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_hytiny.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_hytiny.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pa1.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pa1.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pa1_button_pa8.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pa1_button_pa8.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pa9.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pa9.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pb0.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pb0.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pb12.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pb12.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pb7.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pb7.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pb9.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pb9.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pc13.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pc13.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pc13_fastboot.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pc13_fastboot.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pd1.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pd1.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pd2.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pd2.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pe2.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pe2.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pe5.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pe5.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pe5_button_pa0.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pe5_button_pa0.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/generic_boot20_pg15.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/generic_boot20_pg15.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/maple_mini_boot20.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/maple_mini_boot20.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/maple_rev3_boot20.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/maple_rev3_boot20.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/maple_rev5_boot20.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/maple_rev5_boot20.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/naze32_boot20.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/naze32_boot20.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/smart-v2.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/smart-v2.bin -------------------------------------------------------------------------------- /bootloader_only_binaries/stbee_boot20.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/bootloader_only_binaries/stbee_boot20.bin -------------------------------------------------------------------------------- /common.h: -------------------------------------------------------------------------------- 1 | /* ***************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 LeafLabs LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ****************************************************************************/ 24 | 25 | /** 26 | * @file common.h 27 | * 28 | * @brief toplevel include for bootloader source files 29 | * 30 | * 31 | */ 32 | 33 | #ifndef __COMMON_H 34 | #define __COMMON_H 35 | 36 | #include "config.h" 37 | #include "hardware.h" 38 | #include "stm32f10x_type.h" 39 | #include "cortexm3_macro.h" 40 | #include "usb.h" 41 | 42 | typedef void (*FuncPtr)(void); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 LeafLabs LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | *****************************************************************************/ 24 | 25 | /** 26 | * @file config.h 27 | * 28 | * @brief bootloader settings and macro defines 29 | * 30 | * 31 | */ 32 | 33 | #ifndef __CONFIG_H 34 | #define __CONFIG_H 35 | 36 | #include "common.h" 37 | 38 | 39 | 40 | /* Speed controls for strobing the LED pin */ 41 | #define BLINK_FAST 0x50000 42 | #define BLINK_SLOW 0x100000 43 | 44 | 45 | // The USB clock is the same for all boards 46 | #define RCC_APB1ENR_USB_CLK 0x00800000 47 | 48 | // Clocks for the backup domain registers 49 | #define RCC_APB1ENR_PWR_CLK 0x10000000 50 | #define RCC_APB1ENR_BKP_CLK 0x08000000 51 | 52 | 53 | // Use the usb_description_strings_util.html to make new strngs for the next 3 arrays if you need to change the text. 54 | 55 | #define ALT0_STR_LEN 0x80 56 | #define ALT0_MSG_STR 'S',0,'T',0,'M',0,'3',0,'2',0,'d',0,'u',0,'i',0,'n',0,'o',0,' ',0,'b',0,'o',0,'o',0,'t',0,'l',0,'o',0,'a',0,'d',0,'e',0,'r',0,' ',0,'v',0,'1',0,'.',0,'0',0,' ',0,' ',0,'E',0,'R',0,'R',0,'O',0,'R',0,'.',0,' ',0,'U',0,'p',0,'l',0,'o',0,'a',0,'d',0,' ',0,'t',0,'o',0,' ',0,'R',0,'A',0,'M',0,' ',0,'n',0,'o',0,'t',0,' ',0,'s',0,'u',0,'p',0,'p',0,'o',0,'r',0,'t',0,'e',0,'d',0,'.',0 57 | 58 | #define ALT1_STR_LEN 0x6C 59 | #define ALT1_MSG_STR 'S',0,'T',0,'M',0,'3',0,'2',0,'d',0,'u',0,'i',0,'n',0,'o',0,' ',0,'b',0,'o',0,'o',0,'t',0,'l',0,'o',0,'a',0,'d',0,'e',0,'r',0,' ',0,'v',0,'1',0,'.',0,'0',0,' ',0,' ',0,'U',0,'p',0,'l',0,'o',0,'a',0,'d',0,' ',0,'t',0,'o',0,' ',0,'F',0,'l',0,'a',0,'s',0,'h',0,' ',0,'0',0,'x',0,'8',0,'0',0,'0',0,'5',0,'0',0,'0',0,'0',0 60 | 61 | #define ALT2_STR_LEN 0x6C 62 | #define ALT2_MSG_STR 'S',0,'T',0,'M',0,'3',0,'2',0,'d',0,'u',0,'i',0,'n',0,'o',0,' ',0,'b',0,'o',0,'o',0,'t',0,'l',0,'o',0,'a',0,'d',0,'e',0,'r',0,' ',0,'v',0,'1',0,'.',0,'0',0,' ',0,' ',0,'U',0,'p',0,'l',0,'o',0,'a',0,'d',0,' ',0,'t',0,'o',0,' ',0,'F',0,'l',0,'a',0,'s',0,'h',0,' ',0,'0',0,'x',0,'8',0,'0',0,'0',0,'2',0,'0',0,'0',0,'0',0 63 | 64 | // Jump locations for legacy bootloader (0x8005000) and new / smaller bootloder (0x8002000) 65 | #define USER_CODE_FLASH0X8005000 ((u32)0x08005000) 66 | #define USER_CODE_FLASH0X8002000 ((u32)0x08002000) 67 | 68 | #define LARGEST_FLASH_PAGE_SIZE 0x800 69 | 70 | // Upload to RAM has been removed / depreacted so these values a not used any more 71 | #define USER_CODE_RAM ((u32)0x20000C00) 72 | 73 | // RAM_END, set ram end to the end of ram on the device wth the least RAM (STM32F103C) 74 | #define RAM_END ((u32)0x20005000) 75 | 76 | /* Porting information Please read. 77 | 78 | These defineds are use to setup the hardware of the GPIO. 79 | See http://www.st.com/web/en/resource/technical/document/reference_manual/CD00171190.pdf 80 | 81 | Two GPIO pins need to be defined, the LED and the Button. 82 | 83 | For each pin, the following is required 84 | 85 | LED_BANK, this is the GPIO port, e.g. GPIOA,GPIOB, GPIOC etc etc etc 86 | LED_PIN, this is the pin number e.g PB1 = 1 87 | LED_ON_STATE is whether the pin needs to be 1 or 0 for the LED to be lit, this is needed because on some boards the led is wired between Vcc and the Pin 88 | instead of from the pin to GND 89 | 90 | BUTTON_BANK Port name of the pin used for the button 91 | #define BUTTON_PIN 92 | #define BUTTON_PRESSED_STATE 1 93 | */ 94 | 95 | #if defined TARGET_MAPLE_MINI 96 | 97 | #define HAS_MAPLE_HARDWARE 1 98 | 99 | #define LED_BANK GPIOB 100 | #define LED_PIN 1 101 | #define LED_ON_STATE 1 102 | 103 | /* On the Mini, BUT is PB8 */ 104 | #define BUTTON_BANK GPIOB 105 | #define BUTTON_PIN 8 106 | #define BUTTON_PRESSED_STATE 1 107 | 108 | /* USB Disc Pin Setup. USB DISC is PB9 */ 109 | #define USB_DISC_BANK GPIOB 110 | #define USB_DISC_PIN 9 111 | 112 | #elif defined TARGET_MAPLE_REV3 113 | 114 | #warning "Target MAPLE_REV3" 115 | 116 | // Flag that this type of board has the custom maple disconnect hardware 117 | #define HAS_MAPLE_HARDWARE 1 118 | 119 | #define LED_BANK GPIOB 120 | #define LED_PIN 1 121 | #define LED_ON_STATE 1 122 | 123 | #define BUTTON_BANK GPIOB 124 | #define BUTTON_PIN 8 125 | #define BUTTON_PRESSED_STATE 1 126 | 127 | /* USB Disc Pin Setup. USB DISC is PC12 */ 128 | #define USB_DISC_BANK GPIOC 129 | #define USB_DISC_PIN 12 130 | 131 | #elif defined TARGET_MAPLE_REV5 132 | 133 | // Flag that this type of board has the custom maple disconnect hardware 134 | #define HAS_MAPLE_HARDWARE 1 135 | 136 | #define LED_BANK GPIOA 137 | #define LED_PIN 5 138 | #define LED_ON_STATE 0 139 | 140 | /* On the Mini, BUT is PB8 */ 141 | #define BUTTON_BANK GPIOC 142 | #define BUTTON_PIN 9 143 | #define BUTTON_PRESSED_STATE 1 144 | 145 | /* USB Disc Pin Setup. USB DISC is PC12 */ 146 | #define USB_DISC_BANK GPIOC 147 | #define USB_DISC_PIN 12 148 | 149 | #elif defined TARGET_GENERIC_F103_PC13 150 | 151 | 152 | #define LED_BANK GPIOC 153 | #define LED_PIN 13 154 | #define LED_ON_STATE 0 155 | 156 | // Use Boot1 PB2 as the button, as hardly anyone uses this pin as GPIO 157 | // Need to set the button input mode to just CR_INPUT and not CR_INPUT_PU_PD because the external pullup on the jumplink is very weak 158 | #define BUTTON_INPUT_MODE CR_INPUT 159 | #define BUTTON_BANK GPIOB 160 | #define BUTTON_PIN 2 161 | #define BUTTON_PRESSED_STATE 1 162 | 163 | #elif defined TARGET_GENERIC_F103_PC13_FASTBOOT 164 | 165 | 166 | #define LED_BANK GPIOC 167 | #define LED_PIN 13 168 | #define LED_ON_STATE 0 169 | 170 | // Use Boot1 PB2 as the button, as hardly anyone uses this pin as GPIO 171 | // Need to set the button input mode to just CR_INPUT and not CR_INPUT_PU_PD because the external pullup on the jumplink is very weak 172 | #define BUTTON_INPUT_MODE CR_INPUT 173 | #define BUTTON_BANK GPIOB 174 | #define BUTTON_PIN 2 175 | #define BUTTON_PRESSED_STATE 1 176 | 177 | #define FASTBOOT 1 178 | 179 | #elif defined TARGET_GENERIC_F103_NONE 180 | 181 | 182 | #elif defined TARGET_GENERIC_F103_PG15 183 | 184 | #define LED_BANK GPIOG 185 | #define LED_PIN 15 186 | #define LED_ON_STATE 1 187 | 188 | // Button (if you have one) 189 | #define BUTTON_BANK GPIOC 190 | #define BUTTON_PIN 14 191 | #define BUTTON_PRESSED_STATE 1 192 | 193 | #elif defined TARGET_GENERIC_F103_PD2 194 | 195 | #define LED_BANK GPIOD 196 | #define LED_PIN 2 197 | #define LED_ON_STATE 1 198 | 199 | // Button (if you have one) 200 | #define BUTTON_BANK GPIOC 201 | #define BUTTON_PIN 14 202 | #define BUTTON_PRESSED_STATE 1 203 | 204 | #elif defined TARGET_GENERIC_F103_PD1 205 | 206 | #define LED_BANK GPIOD 207 | #define LED_PIN 1 208 | #define LED_ON_STATE 1 209 | 210 | // Button (if you have one) 211 | #define BUTTON_BANK GPIOC 212 | #define BUTTON_PIN 14 213 | #define BUTTON_PRESSED_STATE 1 214 | 215 | #elif defined TARGET_GENERIC_F103_PA1 216 | 217 | #define LED_BANK GPIOA 218 | #define LED_PIN 1 219 | #define LED_ON_STATE 1 220 | 221 | // Button (if you have one) 222 | #define BUTTON_BANK GPIOC 223 | #define BUTTON_PIN 14 224 | #define BUTTON_PRESSED_STATE 1 225 | 226 | #elif defined TARGET_GENERIC_F103_PA1_BUTTON_PA8 227 | 228 | #define LED_BANK GPIOA 229 | #define LED_PIN 1 230 | #define LED_ON_STATE 1 231 | 232 | #define BUTTON_BANK GPIOA 233 | #define BUTTON_PIN 8 234 | #define BUTTON_PRESSED_STATE 0 235 | 236 | #elif defined TARGET_GENERIC_F103_PB9 237 | 238 | #define LED_BANK GPIOB 239 | #define LED_PIN 9 240 | #define LED_ON_STATE 1 241 | 242 | // Button (if you have one) 243 | #define BUTTON_BANK GPIOC 244 | #define BUTTON_PIN 14 245 | #define BUTTON_PRESSED_STATE 1 246 | 247 | #elif defined TARGET_GENERIC_F103_PE2 248 | 249 | #define LED_BANK GPIOE 250 | #define LED_PIN 2 251 | #define LED_ON_STATE 1 252 | 253 | #elif defined TARGET_GENERIC_F103_PA9 254 | 255 | #define LED_BANK GPIOA 256 | #define LED_PIN 9 257 | #define LED_ON_STATE 1 258 | 259 | #elif defined TARGET_GENERIC_F103_PE5 260 | 261 | #define LED_BANK GPIOE 262 | #define LED_PIN 5 263 | #define LED_ON_STATE 1 264 | 265 | #define BUTTON_BANK GPIOD 266 | #define BUTTON_PIN 2 267 | #define BUTTON_PRESSED_STATE 1 268 | 269 | #elif defined TARGET_GENERIC_F103_PE5_BUTTON_PA0 270 | 271 | #define LED_BANK GPIOE 272 | #define LED_PIN 5 273 | #define LED_ON_STATE 1 274 | 275 | #define BUTTON_BANK GPIOA 276 | #define BUTTON_PIN 0 277 | #define BUTTON_PRESSED_STATE 1 278 | 279 | #elif defined TARGET_GENERIC_F103_PB7 280 | 281 | #define LED_BANK GPIOB 282 | #define LED_PIN 7 283 | #define LED_ON_STATE 1 284 | 285 | #elif defined TARGET_GENERIC_F103_PB0 286 | 287 | #define LED_BANK GPIOB 288 | #define LED_PIN 0 289 | #define LED_ON_STATE 1 290 | #define BOOTLOADER_WAIT 30 291 | 292 | #elif defined TARGET_STBEE 293 | 294 | #define HAS_MAPLE_HARDWARE 1 295 | 296 | #define LED_BANK GPIOD 297 | #define LED_PIN 4 298 | #define LED_ON_STATE 0 299 | 300 | /* BUTTON is PA0 (pull down) */ 301 | #define BUTTON_BANK GPIOA 302 | #define BUTTON_PIN 0 303 | #define BUTTON_PRESSED_STATE 1 304 | 305 | /* USB Disc Pin Setup. USB DISC is PD3 */ 306 | #define USB_DISC_BANK GPIOD 307 | #define USB_DISC_PIN 3 308 | 309 | /* CRISTAL 12MHz */ 310 | #define XTAL12M 1 311 | 312 | #elif defined TARGET_NAZE32 313 | 314 | #define LED_BANK GPIOB 315 | #define LED_PIN 3 316 | #define LED_ON_STATE 0 317 | 318 | #elif defined TARGET_GENERIC_F103_PB12 319 | 320 | #define LED_BANK GPIOB 321 | #define LED_PIN 12 322 | #define LED_ON_STATE 1 323 | #define BOOTLOADER_WAIT 10 324 | 325 | #define BUTTON_BANK GPIOB 326 | #define BUTTON_PIN 2 327 | #define BUTTON_PRESSED_STATE 1 328 | 329 | #elif defined TARGET_HYTINY_STM32F103T 330 | 331 | #define HAS_MAPLE_HARDWARE 1 332 | 333 | 334 | #define LED_BANK GPIOA 335 | #define LED_PIN 1 336 | #define LED_ON_STATE 0 337 | #define BOOTLOADER_WAIT 30 338 | 339 | /* USB Disc Pin Setup. USB DISC is PA0 */ 340 | #define USB_DISC_BANK GPIOA 341 | #define USB_DISC_PIN 0 342 | 343 | #elif defined TARGET_DSO138 344 | 345 | #define LED_BANK GPIOA 346 | #define LED_PIN 15 347 | #define LED_ON_STATE 0 348 | 349 | // "OK" Button 350 | #define BUTTON_BANK GPIOB 351 | #define BUTTON_PIN 15 352 | #define BUTTON_PRESSED_STATE 0 353 | 354 | #elif defined TARGET_GD32F1_FRANKENMAPLE 355 | 356 | #define HAS_MAPLE_HARDWARE 1 357 | 358 | #define LED_BANK GPIOC 359 | #define LED_PIN 13 360 | #define LED_ON_STATE 1 361 | 362 | 363 | /* 364 | #define BUTTON_BANK GPIOB 365 | #define BUTTON_PIN 8 366 | #define BUTTON_PRESSED_STATE 1 367 | */ 368 | 369 | /* USB Disc Pin Setup. USB DISC is PB9 */ 370 | #define USB_DISC_BANK GPIOB 371 | #define USB_DISC_PIN 9 372 | 373 | /* CRISTAL 12MHz */ 374 | #define XTAL12M 1 375 | 376 | #elif defined TARGET_GD32F1_GENERIC_F103_PC13 377 | 378 | #define LED_BANK GPIOC 379 | #define LED_PIN 13 380 | #define LED_ON_STATE 0 381 | 382 | // Button (if you have one) 383 | 384 | // #define BUTTON_BANK GPIOC 385 | // #define BUTTON_PIN 14 386 | // #define BUTTON_PRESSED_STATE 1 387 | 388 | /* CRISTAL 12MHz */ 389 | #define XTAL12M 1 390 | 391 | #elif defined TARGET_STM32_SMART_V20 392 | 393 | #define LED_BANK GPIOC 394 | #define LED_PIN 13 395 | #define LED_ON_STATE 1 396 | 397 | #define BUTTON_BANK GPIOA 398 | #define BUTTON_PIN 0 399 | #define BUTTON_PRESSED_STATE 1 400 | 401 | /* CRISTAL 8MHz */ 402 | 403 | #elif defined TARGET_CC3D 404 | 405 | #define LED_BANK GPIOB 406 | #define LED_PIN 3 407 | #define LED_ON_STATE 0 408 | 409 | 410 | #else 411 | #error "No config for this target" 412 | #endif 413 | 414 | // Check if button pulldown should be enabled 415 | // Default to True as this was the default prior to needing to disable it 416 | // in order to use the boot1 pin on the Blue Pill which has a very week pullup 417 | #ifndef BUTTON_INPUT_MODE 418 | #define BUTTON_INPUT_MODE CR_INPUT_PU_PD 419 | #endif 420 | 421 | #define STARTUP_BLINKS 5 422 | #ifndef BOOTLOADER_WAIT 423 | #ifdef BUTTON_BANK 424 | #define BOOTLOADER_WAIT 6 425 | #else 426 | #define BOOTLOADER_WAIT 30 427 | #endif 428 | #endif 429 | 430 | // defines for USB (DONT CHANGE) 431 | #define VEND_ID0 0xAF 432 | #define VEND_ID1 0x1E 433 | #define PROD_ID0 0x03 434 | #define PROD_ID1 0x00 435 | 436 | // Value to place in RTC backup register 10 for persistent bootloader mode 437 | #define RTC_BOOTLOADER_FLAG 0x424C 438 | #define RTC_BOOTLOADER_JUST_UPLOADED 0x424D 439 | #endif 440 | -------------------------------------------------------------------------------- /dfu.h: -------------------------------------------------------------------------------- 1 | /* ***************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 LeafLabs LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ****************************************************************************/ 24 | 25 | #ifndef __DFU_H 26 | #define __DFU_H 27 | 28 | #include "common.h" 29 | /* 30 | #define DFU_UPLOAD_NONE 0 31 | #define DFU_UPLOAD_RAM 1 32 | #define DFU_UPLOAD_FLASH_0X8005000 2 33 | #define DFU_UPLOAD_FLASH_0X8002000 3 34 | */ 35 | typedef enum {DFU_UPLOAD_NONE, DFU_UPLOAD_RAM, DFU_UPLOAD_FLASH_0X8005000,DFU_UPLOAD_FLASH_0X8002000} dfuUploadTypes_t; 36 | 37 | /* exposed types */ 38 | typedef u8 *(*ClassReqCB)(u16); 39 | 40 | /* exposed structs */ 41 | typedef struct _DFUStatus { 42 | u8 bStatus; 43 | u8 bwPollTimeout0; 44 | u8 bwPollTimeout1; 45 | u8 bwPollTimeout2; 46 | u8 bState; /* state of device at the time the host receives the message! */ 47 | u8 iString; 48 | } DFUStatus; 49 | 50 | typedef enum _PLOT { 51 | BEGINNING, 52 | MIDDLE, 53 | END, 54 | WAIT 55 | } PLOT; 56 | 57 | 58 | /*** DFU bRequest Values ******/ 59 | /* bmRequestType, wValue, wIndex, wLength, Data */ 60 | #define DFU_DETACH 0x00 /* 0x21, wTimeout, Interface, Zero, None */ 61 | #define DFU_DNLOAD 0x01 /* 0x21, wBlockNum, Interface, Length, Firmware */ 62 | #define DFU_UPLOAD 0x02 /* 0xA1, Zero, Interface, Length, Firmware */ 63 | #define DFU_GETSTATUS 0x03 /* 0xA1, Zero, Interface, 6, Status */ 64 | #define DFU_CLRSTATUS 0x04 /* 0x21, Zero, Interface, Zero, None */ 65 | #define DFU_GETSTATE 0x05 /* 0xA1, Zero, Interface, 1, State */ 66 | #define DFU_ABORT 0x06 /* 0x21, Zero, Interface, Zero, None */ 67 | 68 | /*** DFU Status Values ******/ 69 | #define OK 0x00 /* No error */ 70 | #define errTARGET 0x01 /* File is not appropriate for this device */ 71 | #define errFILE 0x02 /* File fails some vendor tests */ 72 | #define errWRITE 0x03 /* Device is unable to write memory */ 73 | #define errERASE 0x04 /* Memory erase failed */ 74 | #define errCHECK_ERASED 0x05 /* Memory erase check failed */ 75 | #define errPROG 0x06 /* Program memory function failed */ 76 | #define errVERIFY 0x07 /* Written program failed verification */ 77 | #define errADDRESS 0x08 /* address out of range */ 78 | #define errNOTDONE 0x09 /* received DNLOAD with wLength=0, but firmware seems incomplete */ 79 | #define errFIRMWARE 0x0A /* Runtime firmware corrupt, cannot return to non-dfu operations! */ 80 | #define errVENDOR 0x0B /* vendor specific error */ 81 | #define errUSBR 0x0C /* Unexpected usb reset! */ 82 | #define errPOR 0x0D /* Unexpected power on reset */ 83 | #define errUNKNOWN 0x0E /* Unknown error */ 84 | #define errSTALLEDPKT 0x0F /* device stalled unexpected request */ 85 | /***************************/ 86 | 87 | /*** DFU State Values **************/ 88 | #define appIDLE 0x00 89 | #define appDETACH 0x01 90 | #define dfuIDLE 0x02 91 | #define dfuDNLOAD_SYNC 0x03 92 | #define dfuDNBUSY 0x04 93 | #define dfuDNLOAD_IDLE 0x05 94 | #define dfuMANIFEST_SYNC 0x06 95 | #define dfuMANIFEST 0x07 96 | #define dfuMANIFEST_WAIT_RESET 0x08 97 | #define dfuUPLOAD_IDLE 0x09 98 | #define dfuERROR 0x0A 99 | /***********************************/ 100 | 101 | 102 | 103 | extern volatile bool dfuBusy; 104 | 105 | /* exposed functions */ 106 | void dfuInit(void); /* singleton dfu initializer */ 107 | 108 | /* should consume dfuEvent type, but for now we can use pInfo (see comment above) */ 109 | bool dfuUpdateByRequest(void); /* returns if new status is OK */ 110 | void dfuUpdateByReset(void); 111 | void dfuUpdateByTimeout(void); 112 | 113 | /* usb callbacks */ 114 | u8 *dfuCopyState(u16); 115 | u8 *dfuCopyStatus(u16); 116 | u8 *dfuCopyDNLOAD(u16); 117 | u8 *dfuCopyUPLOAD(u16); 118 | 119 | void dfuCopyBufferToExec(void); 120 | bool checkTestFile(void); 121 | 122 | u8 dfuGetState(void); 123 | void dfuSetState(u8); 124 | bool dfuUploadStarted(); 125 | void dfuFinishUpload(); 126 | 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /flash/debug.cfg: -------------------------------------------------------------------------------- 1 | # script for stm32 2 | 3 | interface ft2232 4 | ft2232_device_desc "Olimex OpenOCD JTAG" 5 | ft2232_layout olimex-jtag 6 | ft2232_vid_pid 0x15ba 0x0003 7 | 8 | if { [info exists CHIPNAME] } { 9 | set _CHIPNAME $CHIPNAME 10 | } else { 11 | set _CHIPNAME stm32 12 | } 13 | 14 | if { [info exists ENDIAN] } { 15 | set _ENDIAN $ENDIAN 16 | } else { 17 | set _ENDIAN little 18 | } 19 | 20 | # jtag speed 21 | jtag_khz 600 22 | 23 | #use combined on interfaces or targets that can't set TRST/SRST separately 24 | reset_config trst_and_srst 25 | 26 | #jtag scan chain 27 | if { [info exists CPUTAPID ] } { 28 | set _CPUTAPID $CPUTAPID 29 | } else { 30 | # See STM Document RM0008 31 | # Section 26.6.3 32 | set _CPUTAPID 0x3ba00477 33 | } 34 | jtag newtap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID 35 | 36 | if { [info exists BSTAPID ] } { 37 | set _BSTAPID $BSTAPID 38 | } else { 39 | # See STM Document RM0008 40 | # Section 26.6.2 41 | # Medium Density RevA 42 | set _BSTAPID 0x06410041 43 | # Rev B and Rev Z 44 | set _BSTAPID 0x16410041 45 | # High Density Devices, Rev A 46 | #set _BSTAPID 0x06414041 47 | } 48 | jtag newtap $_CHIPNAME bs -irlen 5 -ircapture 0x1 -irmask 0x1 -expected-id $_BSTAPID 49 | 50 | set _TARGETNAME [format "%s.cpu" $_CHIPNAME] 51 | target create $_TARGETNAME cortex_m3 -endian $_ENDIAN -chain-position $_TARGETNAME 52 | 53 | $_TARGETNAME configure -work-area-virt 0 -work-area-phys 0x20000000 -work-area-size 0x5000 -work-area-backup 0 54 | #$_TARGETNAME configure -event halted halt_handle 55 | 56 | #flash bank stm32x 0 0 0 0 0 57 | 58 | 59 | #target create cortex_m3 -endian little 60 | #run_and_halt_time 0 30 61 | 62 | #working_area 0 0x20000000 0x4000 nobackup 63 | 64 | flash bank stm32x 0x08000000 0x00010000 0 0 0 65 | 66 | # For more information about the configuration files, take a look at: 67 | # openocd.texi 68 | 69 | #script flash.script 70 | 71 | proc halt_handle {} { 72 | resume 73 | } 74 | 75 | proc flash_test {} { 76 | puts "Entering DEBUG wait" 77 | sleep 100 78 | # reset run 79 | # sleep 500 80 | } 81 | 82 | init 83 | flash_test 84 | -------------------------------------------------------------------------------- /flash/flash.cfg: -------------------------------------------------------------------------------- 1 | # script for stm32 2 | 3 | interface ft2232 4 | ft2232_device_desc "Olimex OpenOCD JTAG" 5 | ft2232_layout olimex-jtag 6 | ft2232_vid_pid 0x15ba 0x0003 7 | 8 | if { [info exists CHIPNAME] } { 9 | set _CHIPNAME $CHIPNAME 10 | } else { 11 | set _CHIPNAME stm32 12 | } 13 | 14 | if { [info exists ENDIAN] } { 15 | set _ENDIAN $ENDIAN 16 | } else { 17 | set _ENDIAN little 18 | } 19 | 20 | # jtag speed 21 | jtag_khz 600 22 | 23 | #use combined on interfaces or targets that can't set TRST/SRST separately 24 | reset_config trst_and_srst 25 | 26 | #jtag scan chain 27 | if { [info exists CPUTAPID ] } { 28 | set _CPUTAPID $CPUTAPID 29 | } else { 30 | # See STM Document RM0008 31 | # Section 26.6.3 32 | set _CPUTAPID 0x3ba00477 33 | } 34 | jtag newtap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID 35 | 36 | if { [info exists BSTAPID ] } { 37 | set _BSTAPID $BSTAPID 38 | } else { 39 | # See STM Document RM0008 40 | # Section 26.6.2 41 | # Medium Density RevA 42 | set _BSTAPID 0x06410041 43 | # Rev B and Rev Z 44 | set _BSTAPID 0x16410041 45 | # High Density Devices, Rev A 46 | #set _BSTAPID 0x06414041 47 | } 48 | jtag newtap $_CHIPNAME bs -irlen 5 -ircapture 0x1 -irmask 0x1 -expected-id $_BSTAPID 49 | 50 | set _TARGETNAME [format "%s.cpu" $_CHIPNAME] 51 | target create $_TARGETNAME cortex_m3 -endian $_ENDIAN -chain-position $_TARGETNAME 52 | 53 | $_TARGETNAME configure -work-area-virt 0 -work-area-phys 0x20000000 -work-area-size 0x5000 -work-area-backup 0 54 | #$_TARGETNAME configure -event halted halt_handle 55 | 56 | #flash bank stm32x 0 0 0 0 0 57 | 58 | 59 | #target create cortex_m3 -endian little 60 | #run_and_halt_time 0 30 61 | 62 | #working_area 0 0x20000000 0x4000 nobackup 63 | 64 | flash bank stm32x 0x08000000 0x00010000 0 0 0 65 | 66 | # For more information about the configuration files, take a look at: 67 | # openocd.texi 68 | 69 | #script flash.script 70 | 71 | proc halt_handle {} { 72 | resume 73 | } 74 | 75 | proc flash_test {} { 76 | puts "Trying to flash" 77 | sleep 100 78 | halt 79 | sleep 300 80 | stm32x mass_erase 0 81 | sleep 20 82 | flash write_bank 0 tmpflash.bin 0 83 | sleep 50 84 | # reset run 85 | # sleep 500 86 | reset run 87 | shutdown 88 | } 89 | 90 | init 91 | flash_test 92 | -------------------------------------------------------------------------------- /flash/openocd.cfg: -------------------------------------------------------------------------------- 1 | # script for stm32 2 | 3 | interface ft2232 4 | ft2232_device_desc "Olimex OpenOCD JTAG" 5 | ft2232_layout olimex-jtag 6 | ft2232_vid_pid 0x15ba 0x0003 7 | 8 | if { [info exists CHIPNAME] } { 9 | set _CHIPNAME $CHIPNAME 10 | } else { 11 | set _CHIPNAME stm32 12 | } 13 | 14 | if { [info exists ENDIAN] } { 15 | set _ENDIAN $ENDIAN 16 | } else { 17 | set _ENDIAN little 18 | } 19 | 20 | # jtag speed 21 | jtag_khz 500 22 | 23 | jtag_nsrst_delay 200 24 | jtag_ntrst_delay 200 25 | 26 | #use combined on interfaces or targets that can't set TRST/SRST separately 27 | reset_config trst_and_srst 28 | 29 | #jtag scan chain 30 | if { [info exists CPUTAPID ] } { 31 | set _CPUTAPID $CPUTAPID 32 | } else { 33 | # See STM Document RM0008 34 | # Section 26.6.3 35 | set _CPUTAPID 0x3ba00477 36 | } 37 | 38 | jtag newtap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID 39 | 40 | if { [info exists BSTAPID ] } { 41 | set _BSTAPID $BSTAPID 42 | } else { 43 | # See STM Document RM0008 44 | # Section 26.6.2 45 | # Medium Density RevA 46 | set _BSTAPID 0x06410041 47 | # Rev B and Rev Z 48 | set _BSTAPID 0x16410041 49 | # High Density Devices, Rev A 50 | #set _BSTAPID 0x06414041 51 | } 52 | 53 | jtag newtap $_CHIPNAME bs -irlen 5 -ircapture 0x1 -irmask 0x1 -expected-id $_BSTAPID 54 | 55 | set _TARGETNAME [format "%s.cpu" $_CHIPNAME] 56 | target create $_TARGETNAME cortex_m3 -endian $_ENDIAN -chain-position $_TARGETNAME 57 | 58 | $_TARGETNAME configure -work-area-virt 0 -work-area-phys 0x20000000 -work-area-size 0x5000 -work-area-backup 0 59 | 60 | flash bank stm32x 0x08000000 0x00010000 0 0 0 61 | 62 | init 63 | 64 | halt 65 | sleep 1000 66 | stm32x unlock 0 67 | flash erase_sector 0 0 0 68 | sleep 1000 69 | flash write_bank 0 tmpflash.bin 0 70 | sleep 3000 71 | reset 72 | sleep 3000 73 | shutdown 74 | -------------------------------------------------------------------------------- /flash/stm32.cfg: -------------------------------------------------------------------------------- 1 | # script for stm32 2 | 3 | interface ft2232 4 | ft2232_device_desc "Olimex OpenOCD JTAG" 5 | ft2232_layout olimex-jtag 6 | ft2232_vid_pid 0x15ba 0x0003 7 | 8 | if { [info exists CHIPNAME] } { 9 | set _CHIPNAME $CHIPNAME 10 | } else { 11 | set _CHIPNAME stm32 12 | } 13 | 14 | if { [info exists ENDIAN] } { 15 | set _ENDIAN $ENDIAN 16 | } else { 17 | set _ENDIAN little 18 | } 19 | 20 | # jtag speed 21 | jtag_khz 500 22 | 23 | jtag_nsrst_delay 100 24 | jtag_ntrst_delay 100 25 | 26 | #use combined on interfaces or targets that can't set TRST/SRST separately 27 | reset_config trst_and_srst 28 | 29 | #jtag scan chain 30 | if { [info exists CPUTAPID ] } { 31 | set _CPUTAPID $CPUTAPID 32 | } else { 33 | # See STM Document RM0008 34 | # Section 26.6.3 35 | set _CPUTAPID 0x3ba00477 36 | } 37 | jtag newtap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID 38 | 39 | if { [info exists BSTAPID ] } { 40 | set _BSTAPID $BSTAPID 41 | } else { 42 | # See STM Document RM0008 43 | # Section 26.6.2 44 | # Medium Density RevA 45 | set _BSTAPID 0x06410041 46 | # Rev B and Rev Z 47 | set _BSTAPID 0x16410041 48 | # High Density Devices, Rev A 49 | #set _BSTAPID 0x06414041 50 | } 51 | jtag newtap $_CHIPNAME bs -irlen 5 -ircapture 0x1 -irmask 0x1 -expected-id $_BSTAPID 52 | 53 | set _TARGETNAME [format "%s.cpu" $_CHIPNAME] 54 | target create $_TARGETNAME cortex_m3 -endian $_ENDIAN -chain-position $_TARGETNAME 55 | 56 | $_TARGETNAME configure -work-area-virt 0 -work-area-phys 0x20000000 -work-area-size 16384 -work-area-backup 0 57 | 58 | #flash bank stm32x 0 0 0 0 0 59 | 60 | 61 | target create cortex_m3 -endian little 62 | #run_and_halt_time 0 30 63 | 64 | #working_area 0 0x20000000 0x4000 nobackup 65 | 66 | #flash bank stm32x 0x08000000 0x00010000 0 0 0 67 | reset 68 | sleep 3000 69 | shutdown 70 | # For more information about the configuration files, take a look at: 71 | # openocd.texi 72 | 73 | -------------------------------------------------------------------------------- /flash/stm32loader.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # -*- coding: utf-8 -*- 4 | # vim: sw=4:ts=4:si:et:enc=utf-8 5 | 6 | # Author: Ivan A-R 7 | # Project page: http://tuxotronic.org/wiki/projects/stm32loader 8 | # 9 | # This file is part of stm32loader. 10 | # 11 | # stm32loader is free software; you can redistribute it and/or modify it under 12 | # the terms of the GNU General Public License as published by the Free 13 | # Software Foundation; either version 3, or (at your option) any later 14 | # version. 15 | # 16 | # stm32loader is distributed in the hope that it will be useful, but WITHOUT ANY 17 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or 18 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 19 | # for more details. 20 | # 21 | # You should have received a copy of the GNU General Public License 22 | # along with stm32loader; see the file COPYING3. If not see 23 | # . 24 | 25 | import sys, getopt 26 | import serial 27 | import time 28 | 29 | try: 30 | from progressbar import * 31 | usepbar = 1 32 | except: 33 | usepbar = 0 34 | 35 | # Verbose level 36 | QUIET = 20 37 | 38 | def mdebug(level, message): 39 | if(QUIET >= level): 40 | print >> sys.stderr , message 41 | 42 | 43 | class CmdException(Exception): 44 | pass 45 | 46 | class CommandInterface: 47 | def open(self, aport='/dev/tty.usbserial-FTD3TMCH', abaudrate=115200) : 48 | self.sp = serial.Serial( 49 | port=aport, 50 | baudrate=abaudrate, # baudrate 51 | bytesize=8, # number of databits 52 | parity=serial.PARITY_EVEN, 53 | stopbits=1, 54 | xonxoff=0, # enable software flow control 55 | rtscts=0, # disable RTS/CTS flow control 56 | timeout=5 # set a timeout value, None for waiting forever 57 | ) 58 | 59 | 60 | def _wait_for_ask(self, info = ""): 61 | # wait for ask 62 | try: 63 | ask = ord(self.sp.read()) 64 | except: 65 | raise CmdException("Can't read port or timeout") 66 | else: 67 | if ask == 0x79: 68 | # ACK 69 | return 1 70 | else: 71 | if ask == 0x1F: 72 | # NACK 73 | raise CmdException("NACK "+info) 74 | else: 75 | # Unknown response 76 | raise CmdException("Unknown response. "+info+": "+hex(ask)) 77 | 78 | 79 | def reset(self): 80 | self.sp.setDTR(0) 81 | time.sleep(0.1) 82 | self.sp.setDTR(1) 83 | time.sleep(0.5) 84 | 85 | def initChip(self): 86 | # Set boot 87 | self.sp.setRTS(0) 88 | self.reset() 89 | 90 | self.sp.write("\x7F") # Syncro 91 | return self._wait_for_ask("Syncro") 92 | 93 | def releaseChip(self): 94 | self.sp.setRTS(1) 95 | self.reset() 96 | 97 | def cmdGeneric(self, cmd): 98 | self.sp.write(chr(cmd)) 99 | self.sp.write(chr(cmd ^ 0xFF)) # Control byte 100 | return self._wait_for_ask(hex(cmd)) 101 | 102 | def cmdGet(self): 103 | if self.cmdGeneric(0x00): 104 | mdebug(10, "*** Get command"); 105 | len = ord(self.sp.read()) 106 | version = ord(self.sp.read()) 107 | mdebug(10, " Bootloader version: "+hex(version)) 108 | dat = map(lambda c: hex(ord(c)), self.sp.read(len)) 109 | mdebug(10, " Available commands: "+str(dat)) 110 | self._wait_for_ask("0x00 end") 111 | return version 112 | else: 113 | raise CmdException("Get (0x00) failed") 114 | 115 | def cmdGetVersion(self): 116 | if self.cmdGeneric(0x01): 117 | mdebug(10, "*** GetVersion command") 118 | version = ord(self.sp.read()) 119 | self.sp.read(2) 120 | self._wait_for_ask("0x01 end") 121 | mdebug(10, " Bootloader version: "+hex(version)) 122 | return version 123 | else: 124 | raise CmdException("GetVersion (0x01) failed") 125 | 126 | def cmdGetID(self): 127 | if self.cmdGeneric(0x02): 128 | mdebug(10, "*** GetID command") 129 | len = ord(self.sp.read()) 130 | id = self.sp.read(len+1) 131 | self._wait_for_ask("0x02 end") 132 | return id 133 | else: 134 | raise CmdException("GetID (0x02) failed") 135 | 136 | 137 | def _encode_addr(self, addr): 138 | byte3 = (addr >> 0) & 0xFF 139 | byte2 = (addr >> 8) & 0xFF 140 | byte1 = (addr >> 16) & 0xFF 141 | byte0 = (addr >> 24) & 0xFF 142 | crc = byte0 ^ byte1 ^ byte2 ^ byte3 143 | return (chr(byte0) + chr(byte1) + chr(byte2) + chr(byte3) + chr(crc)) 144 | 145 | 146 | def cmdReadMemory(self, addr, lng): 147 | assert(lng <= 256) 148 | if self.cmdGeneric(0x11): 149 | mdebug(10, "*** ReadMemory command") 150 | self.sp.write(self._encode_addr(addr)) 151 | self._wait_for_ask("0x11 address failed") 152 | N = (lng - 1) & 0xFF 153 | crc = N ^ 0xFF 154 | self.sp.write(chr(N) + chr(crc)) 155 | self._wait_for_ask("0x11 length failed") 156 | return map(lambda c: ord(c), self.sp.read(lng)) 157 | else: 158 | raise CmdException("ReadMemory (0x11) failed") 159 | 160 | 161 | def cmdGo(self, addr): 162 | if self.cmdGeneric(0x21): 163 | mdebug(10, "*** Go command") 164 | self.sp.write(self._encode_addr(addr)) 165 | self._wait_for_ask("0x21 go failed") 166 | else: 167 | raise CmdException("Go (0x21) failed") 168 | 169 | 170 | def cmdWriteMemory(self, addr, data): 171 | assert(len(data) <= 256) 172 | if self.cmdGeneric(0x31): 173 | mdebug(10, "*** Write memory command") 174 | self.sp.write(self._encode_addr(addr)) 175 | self._wait_for_ask("0x31 address failed") 176 | #map(lambda c: hex(ord(c)), data) 177 | lng = (len(data)-1) & 0xFF 178 | mdebug(10, " %s bytes to write" % [lng+1]); 179 | self.sp.write(chr(lng)) # len really 180 | crc = 0xFF 181 | for c in data: 182 | crc = crc ^ c 183 | self.sp.write(chr(c)) 184 | self.sp.write(chr(crc)) 185 | self._wait_for_ask("0x31 programming failed") 186 | mdebug(10, " Write memory done") 187 | else: 188 | raise CmdException("Write memory (0x31) failed") 189 | 190 | 191 | def cmdEraseMemory(self, sectors = None): 192 | if self.cmdGeneric(0x43): 193 | mdebug(10, "*** Erase memory command") 194 | if sectors is None: 195 | # Global erase 196 | self.sp.write(chr(0xFF)) 197 | self.sp.write(chr(0x00)) 198 | else: 199 | # Sectors erase 200 | self.sp.write(chr((len(sectors)-1) & 0xFF)) 201 | crc = 0xFF 202 | for c in sectors: 203 | crc = crc ^ c 204 | self.sp.write(chr(c)) 205 | self.sp.write(chr(crc)) 206 | self._wait_for_ask("0x43 erasing failed") 207 | mdebug(10, " Erase memory done") 208 | else: 209 | raise CmdException("Erase memory (0x43) failed") 210 | 211 | def cmdWriteProtect(self, sectors): 212 | if self.cmdGeneric(0x63): 213 | mdebug(10, "*** Write protect command") 214 | self.sp.write(chr((len(sectors)-1) & 0xFF)) 215 | crc = 0xFF 216 | for c in sectors: 217 | crc = crc ^ c 218 | self.sp.write(chr(c)) 219 | self.sp.write(chr(crc)) 220 | self._wait_for_ask("0x63 write protect failed") 221 | mdebug(10, " Write protect done") 222 | else: 223 | raise CmdException("Write Protect memory (0x63) failed") 224 | 225 | def cmdWriteUnprotect(self): 226 | if self.cmdGeneric(0x73): 227 | mdebug(10, "*** Write Unprotect command") 228 | self._wait_for_ask("0x73 write unprotect failed") 229 | self._wait_for_ask("0x73 write unprotect 2 failed") 230 | mdebug(10, " Write Unprotect done") 231 | else: 232 | raise CmdException("Write Unprotect (0x73) failed") 233 | 234 | def cmdReadoutProtect(self): 235 | if self.cmdGeneric(0x82): 236 | mdebug(10, "*** Readout protect command") 237 | self._wait_for_ask("0x82 readout protect failed") 238 | self._wait_for_ask("0x82 readout protect 2 failed") 239 | mdebug(10, " Read protect done") 240 | else: 241 | raise CmdException("Readout protect (0x82) failed") 242 | 243 | def cmdReadoutUnprotect(self): 244 | if self.cmdGeneric(0x92): 245 | mdebug(10, "*** Readout Unprotect command") 246 | self._wait_for_ask("0x92 readout unprotect failed") 247 | self._wait_for_ask("0x92 readout unprotect 2 failed") 248 | mdebug(10, " Read Unprotect done") 249 | else: 250 | raise CmdException("Readout unprotect (0x92) failed") 251 | 252 | 253 | # Complex commands section 254 | 255 | def readMemory(self, addr, lng): 256 | data = [] 257 | if usepbar: 258 | widgets = ['Reading: ', Percentage(),', ', ETA(), ' ', Bar()] 259 | pbar = ProgressBar(widgets=widgets,maxval=lng, term_width=79).start() 260 | 261 | while lng > 256: 262 | if usepbar: 263 | pbar.update(pbar.maxval-lng) 264 | else: 265 | mdebug(5, "Read %(len)d bytes at 0x%(addr)X" % {'addr': addr, 'len': 256}) 266 | data = data + self.cmdReadMemory(addr, 256) 267 | addr = addr + 256 268 | lng = lng - 256 269 | if usepbar: 270 | pbar.update(pbar.maxval-lng) 271 | pbar.finish() 272 | else: 273 | mdebug(5, "Read %(len)d bytes at 0x%(addr)X" % {'addr': addr, 'len': 256}) 274 | data = data + self.cmdReadMemory(addr, lng) 275 | return data 276 | 277 | def writeMemory(self, addr, data): 278 | lng = len(data) 279 | if usepbar: 280 | widgets = ['Writing: ', Percentage(),' ', ETA(), ' ', Bar()] 281 | pbar = ProgressBar(widgets=widgets, maxval=lng, term_width=79).start() 282 | 283 | offs = 0 284 | while lng > 256: 285 | if usepbar: 286 | pbar.update(pbar.maxval-lng) 287 | else: 288 | mdebug(5, "Write %(len)d bytes at 0x%(addr)X" % {'addr': addr, 'len': 256}) 289 | self.cmdWriteMemory(addr, data[offs:offs+256]) 290 | offs = offs + 256 291 | addr = addr + 256 292 | lng = lng - 256 293 | if usepbar: 294 | pbar.update(pbar.maxval-lng) 295 | pbar.finish() 296 | else: 297 | mdebug(5, "Write %(len)d bytes at 0x%(addr)X" % {'addr': addr, 'len': 256}) 298 | self.cmdWriteMemory(addr, data[offs:offs+lng] + ([0xFF] * (256-lng)) ) 299 | 300 | 301 | 302 | 303 | def __init__(self) : 304 | pass 305 | 306 | 307 | def usage(): 308 | print """Usage: %s [-hqVewvr] [-l length] [-p port] [-b baud] [-a addr] [file.bin] 309 | -h This help 310 | -q Quiet 311 | -V Verbose 312 | -e Erase 313 | -w Write 314 | -v Verify 315 | -r Read 316 | -l length Length of read 317 | -p port Serial port (default: /dev/tty.usbserial-ftCYPMYJ) 318 | -b baud Baud speed (default: 115200) 319 | -a addr Target address 320 | 321 | ./stm32loader.py -e -w -v example/main.bin 322 | 323 | """ % sys.argv[0] 324 | 325 | 326 | if __name__ == "__main__": 327 | 328 | # Import Psyco if available 329 | try: 330 | import psyco 331 | psyco.full() 332 | print "Using Psyco..." 333 | except ImportError: 334 | pass 335 | 336 | conf = { 337 | 'port': '/dev/tty.usbserial-FTD3TMCH', 338 | 'baud': 115200, 339 | 'address': 0x08000000, 340 | 'erase': 0, 341 | 'write': 0, 342 | 'verify': 0, 343 | 'read': 0, 344 | 'len': 1000, 345 | 'fname':'', 346 | } 347 | 348 | # http://www.python.org/doc/2.5.2/lib/module-getopt.html 349 | 350 | try: 351 | opts, args = getopt.getopt(sys.argv[1:], "hqVewvrp:b:a:l:") 352 | except getopt.GetoptError, err: 353 | # print help information and exit: 354 | print str(err) # will print something like "option -a not recognized" 355 | usage() 356 | sys.exit(2) 357 | 358 | QUIET = 5 359 | 360 | for o, a in opts: 361 | if o == '-V': 362 | QUIET = 10 363 | elif o == '-q': 364 | QUIET = 0 365 | elif o == '-h': 366 | usage() 367 | sys.exit(0) 368 | elif o == '-e': 369 | conf['erase'] = 1 370 | elif o == '-w': 371 | conf['write'] = 1 372 | elif o == '-v': 373 | conf['verify'] = 1 374 | elif o == '-r': 375 | conf['read'] = 1 376 | elif o == '-p': 377 | conf['port'] = a 378 | elif o == '-b': 379 | conf['baud'] = eval(a) 380 | elif o == '-a': 381 | conf['address'] = eval(a) 382 | elif o == '-l': 383 | conf['len'] = eval(a) 384 | # elif o == '-f': 385 | # conf['fname'] = a 386 | else: 387 | assert False, "unhandled option" 388 | 389 | cmd = CommandInterface() 390 | cmd.open(conf['port'], conf['baud']) 391 | mdebug(10, "Open port %(port)s, baud %(baud)d" % {'port':conf['port'], 'baud':conf['baud']}) 392 | try: 393 | try: 394 | cmd.initChip() 395 | except: 396 | print "Can't init. Ensure that BOOT0 is enabled and reset device" 397 | 398 | bootversion = cmd.cmdGet() 399 | mdebug(0, "Bootloader version %X" % bootversion) 400 | mdebug(0, "Chip id `%s'" % str(map(lambda c: hex(ord(c)), cmd.cmdGetID()))) 401 | # cmd.cmdGetVersion() 402 | # cmd.cmdGetID() 403 | # cmd.cmdReadoutUnprotect() 404 | # cmd.cmdWriteUnprotect() 405 | # cmd.cmdWriteProtect([0, 1]) 406 | 407 | if (conf['write'] or conf['verify']): 408 | data = map(lambda c: ord(c), file(args[0]).read()) 409 | 410 | if conf['erase']: 411 | cmd.cmdEraseMemory() 412 | 413 | if conf['write']: 414 | cmd.writeMemory(conf['address'], data) 415 | 416 | if conf['verify']: 417 | verify = cmd.readMemory(conf['address'], len(data)) 418 | if(data == verify): 419 | print "Verification OK" 420 | else: 421 | print "Verification FAILED" 422 | print str(len(data)) + ' vs ' + str(len(verify)) 423 | for i in xrange(0, len(data)): 424 | if data[i] != verify[i]: 425 | print hex(i) + ': ' + hex(data[i]) + ' vs ' + hex(verify[i]) 426 | 427 | if not conf['write'] and conf['read']: 428 | rdata = cmd.readMemory(conf['address'], conf['len']) 429 | # file(conf['fname'], 'wb').write(rdata) 430 | file(args[0], 'wb').write(''.join(map(chr,rdata))) 431 | 432 | # cmd.cmdGo(addr + 0x04) 433 | finally: 434 | cmd.releaseChip() 435 | 436 | -------------------------------------------------------------------------------- /hardware.c: -------------------------------------------------------------------------------- 1 | /* ***************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 LeafLabs LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ****************************************************************************/ 24 | 25 | /** 26 | * @file hardware.c 27 | * 28 | * @brief init routines to setup clocks, interrupts, also destructor functions. 29 | * does not include USB stuff. EEPROM read/write functions. 30 | * 31 | */ 32 | #include "common.h" 33 | #include "hardware.h" 34 | /* 35 | void setPin(u32 bank, u8 pin) { 36 | u32 pinMask = 0x1 << (pin); 37 | SET_REG(GPIO_BSRR(bank), pinMask); 38 | } 39 | 40 | void resetPin(u32 bank, u8 pin) { 41 | u32 pinMask = 0x1 << (16 + pin); 42 | SET_REG(GPIO_BSRR(bank), pinMask); 43 | } 44 | */ 45 | void gpio_write_bit(u32 bank, u8 pin, u8 val) { 46 | val = !val; // "set" bits are lower than "reset" bits 47 | SET_REG(GPIO_BSRR(bank), (1U << pin) << (16 * val)); 48 | } 49 | 50 | bool readPin(u32 bank, u8 pin) { 51 | // todo, implement read 52 | if (GET_REG(GPIO_IDR(bank)) & (0x01 << pin)) { 53 | return TRUE; 54 | } else { 55 | return FALSE; 56 | } 57 | } 58 | 59 | bool readButtonState() { 60 | // todo, implement read 61 | bool state=FALSE; 62 | #if defined(BUTTON_BANK) && defined (BUTTON_PIN) && defined (BUTTON_PRESSED_STATE) 63 | if (GET_REG(GPIO_IDR(BUTTON_BANK)) & (0x01 << BUTTON_PIN)) 64 | { 65 | state = TRUE; 66 | } 67 | 68 | if (BUTTON_PRESSED_STATE==0) 69 | { 70 | state=!state; 71 | } 72 | #endif 73 | return state; 74 | } 75 | 76 | void strobePin(u32 bank, u8 pin, u8 count, u32 rate,u8 onState) 77 | { 78 | gpio_write_bit( bank,pin,1-onState); 79 | 80 | u32 c; 81 | while (count-- > 0) 82 | { 83 | for (c = rate; c > 0; c--) 84 | { 85 | asm volatile("nop"); 86 | } 87 | 88 | gpio_write_bit( bank,pin,onState); 89 | 90 | for (c = rate; c > 0; c--) 91 | { 92 | asm volatile("nop"); 93 | } 94 | gpio_write_bit( bank,pin,1-onState); 95 | } 96 | } 97 | 98 | void systemReset(void) { 99 | SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x00000001); 100 | SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) & 0xF8FF0000); 101 | SET_REG(RCC_CR, GET_REG(RCC_CR) & 0xFEF6FFFF); 102 | SET_REG(RCC_CR, GET_REG(RCC_CR) & 0xFFFBFFFF); 103 | SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) & 0xFF80FFFF); 104 | 105 | SET_REG(RCC_CIR, 0x00000000); /* disable all RCC interrupts */ 106 | } 107 | 108 | void setupCLK(void) { 109 | unsigned int StartUpCounter=0; 110 | /* enable HSE */ 111 | SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x00010001); 112 | while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ 113 | 114 | /* enable flash prefetch buffer */ 115 | SET_REG(FLASH_ACR, 0x00000012); 116 | 117 | /* Configure PLL */ 118 | #if defined XTAL16M 119 | // 16 MHz crystal (using the Bit 17 PLLXTPRE=1 => HSE clock divided by 2 before PLL entry) 120 | SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x001F0400); /* pll=72Mhz(x9/2),APB1=36Mhz,AHB=72Mhz */ 121 | #elif defined XTAL12M 122 | // 12 MHz crystal 123 | SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x00110400); /* pll=72Mhz(x6),APB1=36Mhz,AHB=72Mhz */ 124 | #else 125 | // 8 MHz crystal default 126 | SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz(x9),APB1=36Mhz,AHB=72Mhz */ 127 | #endif 128 | 129 | SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x01000000); /* enable the pll */ 130 | 131 | 132 | #if !defined (HSE_STARTUP_TIMEOUT) 133 | #define HSE_STARTUP_TIMEOUT ((unsigned int)0x0500) /*!< Time out for HSE start up */ 134 | #endif /* HSE_STARTUP_TIMEOUT */ 135 | 136 | while ((GET_REG(RCC_CR) & 0x03000000) == 0 && StartUpCounter < HSE_STARTUP_TIMEOUT) 137 | { 138 | // StartUpCounter++; // This is commented out, so other changes can be committed. It will be uncommented at a later date 139 | } /* wait for it to come on */ 140 | 141 | if (StartUpCounter>=HSE_STARTUP_TIMEOUT) 142 | { 143 | // HSE has not started. Try restarting the processor 144 | systemHardReset(); 145 | } 146 | 147 | /* Set SYSCLK as PLL */ 148 | SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x00000002); 149 | while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ 150 | 151 | pRCC->APB2ENR |= 0B111111100;// Enable All GPIO channels (A to G) 152 | pRCC->APB1ENR |= RCC_APB1ENR_USB_CLK; 153 | } 154 | 155 | 156 | void setupLEDAndButton (void) { 157 | // SET_REG(AFIO_MAPR,(GET_REG(AFIO_MAPR) & ~AFIO_MAPR_SWJ_CFG) | AFIO_MAPR_SWJ_CFG_NO_JTAG_NO_SW);// Try to disable SWD AND JTAG so we can use those pins (not sure if this works). 158 | 159 | #if defined(BUTTON_BANK) && defined (BUTTON_PIN) && defined (BUTTON_PRESSED_STATE) 160 | SET_REG(GPIO_CR(BUTTON_BANK,BUTTON_PIN),(GET_REG(GPIO_CR(BUTTON_BANK,BUTTON_PIN)) & crMask(BUTTON_PIN)) | BUTTON_INPUT_MODE << CR_SHITF(BUTTON_PIN)); 161 | 162 | gpio_write_bit(BUTTON_BANK, BUTTON_PIN,1-BUTTON_PRESSED_STATE);// set pulldown resistor in case there is no button. 163 | #endif 164 | #if defined(LED_BANK) && defined(LED_PIN) && defined(LED_ON_STATE) 165 | SET_REG(GPIO_CR(LED_BANK,LED_PIN),(GET_REG(GPIO_CR(LED_BANK,LED_PIN)) & crMask(LED_PIN)) | CR_OUTPUT_PP << CR_SHITF(LED_PIN)); 166 | #endif 167 | } 168 | 169 | void setupFLASH() { 170 | /* configure the HSI oscillator */ 171 | if ((pRCC->CR & 0x01) == 0x00) { 172 | u32 rwmVal = pRCC->CR; 173 | rwmVal |= 0x01; 174 | pRCC->CR = rwmVal; 175 | } 176 | 177 | /* wait for it to come on */ 178 | while ((pRCC->CR & 0x02) == 0x00) {} 179 | } 180 | 181 | bool checkUserCode(u32 usrAddr) { 182 | u32 sp = *(vu32 *) usrAddr; 183 | 184 | if ((sp & 0x2FFE0000) == 0x20000000) { 185 | return (TRUE); 186 | } else { 187 | return (FALSE); 188 | } 189 | } 190 | 191 | void setMspAndJump(u32 usrAddr) { 192 | // Dedicated function with no call to any function (appart the last call) 193 | // This way, there is no manipulation of the stack here, ensuring that GGC 194 | // didn't insert any pop from the SP after having set the MSP. 195 | typedef void (*funcPtr)(void); 196 | u32 jumpAddr = *(vu32 *)(usrAddr + 0x04); /* reset ptr in vector table */ 197 | 198 | funcPtr usrMain = (funcPtr) jumpAddr; 199 | 200 | SET_REG(SCB_VTOR, (vu32) (usrAddr)); 201 | 202 | asm volatile("msr msp, %0"::"g"(*(volatile u32 *)usrAddr)); 203 | 204 | usrMain(); /* go! */ 205 | } 206 | 207 | 208 | void jumpToUser(u32 usrAddr) { 209 | 210 | /* tear down all the dfu related setup */ 211 | // disable usb interrupts, clear them, turn off usb, set the disc pin 212 | // todo pick exactly what we want to do here, now its just a conservative 213 | flashLock(); 214 | usbDsbISR(); 215 | nvicDisableInterrupts(); 216 | 217 | #ifndef HAS_MAPLE_HARDWARE 218 | usbDsbBus(); 219 | #endif 220 | 221 | // Does nothing, as PC12 is not connected on teh Maple mini according to the schemmatic setPin(GPIOC, 12); // disconnect usb from host. todo, macroize pin 222 | systemReset(); // resets clocks and periphs, not core regs 223 | 224 | setMspAndJump(usrAddr); 225 | } 226 | 227 | void bkp10Write(u16 value) 228 | { 229 | // Enable clocks for the backup domain registers 230 | pRCC->APB1ENR |= (RCC_APB1ENR_PWR_CLK | RCC_APB1ENR_BKP_CLK); 231 | 232 | // Disable backup register write protection 233 | pPWR->CR |= PWR_CR_DBP; 234 | 235 | // store value in pBK DR10 236 | pBKP->DR10 = value; 237 | 238 | // Re-enable backup register write protection 239 | pPWR->CR &=~ PWR_CR_DBP; 240 | } 241 | 242 | int checkAndClearBootloaderFlag() 243 | { 244 | bool flagSet = 0x00;// Flag not used 245 | 246 | // Enable clocks for the backup domain registers 247 | pRCC->APB1ENR |= (RCC_APB1ENR_PWR_CLK | RCC_APB1ENR_BKP_CLK); 248 | 249 | switch (pBKP->DR10) 250 | { 251 | case RTC_BOOTLOADER_FLAG: 252 | flagSet = 0x01; 253 | break; 254 | case RTC_BOOTLOADER_JUST_UPLOADED: 255 | flagSet = 0x02; 256 | break; 257 | } 258 | 259 | if (flagSet!=0x00) 260 | { 261 | bkp10Write(0x0000);// Clear the flag 262 | // Disable clocks 263 | pRCC->APB1ENR &= ~(RCC_APB1ENR_PWR_CLK | RCC_APB1ENR_BKP_CLK); 264 | } 265 | 266 | 267 | 268 | return flagSet; 269 | } 270 | 271 | 272 | 273 | void nvicInit(NVIC_InitTypeDef *NVIC_InitStruct) { 274 | u32 tmppriority = 0x00; 275 | u32 tmpreg = 0x00; 276 | u32 tmpmask = 0x00; 277 | u32 tmppre = 0; 278 | u32 tmpsub = 0x0F; 279 | 280 | SCB_TypeDef *rSCB = (SCB_TypeDef *) SCB_BASE; 281 | NVIC_TypeDef *rNVIC = (NVIC_TypeDef *) NVIC_BASE; 282 | 283 | 284 | /* Compute the Corresponding IRQ Priority --------------------------------*/ 285 | tmppriority = (0x700 - (rSCB->AIRCR & (u32)0x700)) >> 0x08; 286 | tmppre = (0x4 - tmppriority); 287 | tmpsub = tmpsub >> tmppriority; 288 | 289 | tmppriority = (u32)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre; 290 | tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub; 291 | 292 | tmppriority = tmppriority << 0x04; 293 | tmppriority = ((u32)tmppriority) << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08); 294 | 295 | tmpreg = rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)]; 296 | tmpmask = (u32)0xFF << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08); 297 | tmpreg &= ~tmpmask; 298 | tmppriority &= tmpmask; 299 | tmpreg |= tmppriority; 300 | 301 | rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)] = tmpreg; 302 | 303 | /* Enable the Selected IRQ Channels --------------------------------------*/ 304 | rNVIC->ISER[(NVIC_InitStruct->NVIC_IRQChannel >> 0x05)] = 305 | (u32)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (u8)0x1F); 306 | } 307 | 308 | void nvicDisableInterrupts() { 309 | NVIC_TypeDef *rNVIC = (NVIC_TypeDef *) NVIC_BASE; 310 | rNVIC->ICER[0] = 0xFFFFFFFF; 311 | rNVIC->ICER[1] = 0xFFFFFFFF; 312 | rNVIC->ICPR[0] = 0xFFFFFFFF; 313 | rNVIC->ICPR[1] = 0xFFFFFFFF; 314 | 315 | SET_REG(STK_CTRL, 0x04); /* disable the systick, which operates separately from nvic */ 316 | } 317 | 318 | void systemHardReset(void) { 319 | SCB_TypeDef *rSCB = (SCB_TypeDef *) SCB_BASE; 320 | 321 | /* Reset */ 322 | rSCB->AIRCR = (u32)AIRCR_RESET_REQ; 323 | 324 | /* should never get here */ 325 | while (1) { 326 | asm volatile("nop"); 327 | } 328 | } 329 | 330 | bool flashErasePage(u32 pageAddr) { 331 | u32 rwmVal = GET_REG(FLASH_CR); 332 | rwmVal = FLASH_CR_PER; 333 | SET_REG(FLASH_CR, rwmVal); 334 | 335 | while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} 336 | SET_REG(FLASH_AR, pageAddr); 337 | SET_REG(FLASH_CR, FLASH_CR_START | FLASH_CR_PER); 338 | while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} 339 | 340 | /* todo: verify the page was erased */ 341 | 342 | rwmVal = 0x00; 343 | SET_REG(FLASH_CR, rwmVal); 344 | 345 | return TRUE; 346 | } 347 | bool flashErasePages(u32 pageAddr, u16 n) { 348 | while (n-- > 0) { 349 | if (!flashErasePage(pageAddr + wTransferSize * n)) { 350 | return FALSE; 351 | } 352 | } 353 | 354 | return TRUE; 355 | } 356 | 357 | bool flashWriteWord(u32 addr, u32 word) { 358 | vu16 *flashAddr = (vu16 *)addr; 359 | vu32 lhWord = (vu32)word & 0x0000FFFF; 360 | vu32 hhWord = ((vu32)word & 0xFFFF0000) >> 16; 361 | 362 | u32 rwmVal = GET_REG(FLASH_CR); 363 | SET_REG(FLASH_CR, FLASH_CR_PG); 364 | 365 | /* apparently we need not write to FLASH_AR and can 366 | simply do a native write of a half word */ 367 | while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} 368 | *(flashAddr + 0x01) = (vu16)hhWord; 369 | while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} 370 | *(flashAddr) = (vu16)lhWord; 371 | while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} 372 | 373 | rwmVal &= 0xFFFFFFFE; 374 | SET_REG(FLASH_CR, rwmVal); 375 | 376 | /* verify the write */ 377 | if (*(vu32 *)addr != word) { 378 | return FALSE; 379 | } 380 | 381 | return TRUE; 382 | } 383 | 384 | void flashLock() { 385 | /* take down the HSI oscillator? it may be in use elsewhere */ 386 | 387 | /* ensure all FPEC functions disabled and lock the FPEC */ 388 | SET_REG(FLASH_CR, 0x00000080); 389 | } 390 | 391 | void flashUnlock() { 392 | /* unlock the flash */ 393 | SET_REG(FLASH_KEYR, FLASH_KEY1); 394 | SET_REG(FLASH_KEYR, FLASH_KEY2); 395 | } 396 | 397 | 398 | // Used to create the control register masking pattern, when setting control register mode. 399 | unsigned int crMask(int pin) 400 | { 401 | unsigned int mask; 402 | if (pin>=8) 403 | { 404 | pin-=8; 405 | } 406 | mask = 0x0F << (pin<<2); 407 | return ~mask; 408 | } 409 | 410 | #define FLASH_SIZE_REG 0x1FFFF7E0 411 | int getFlashEnd(void) 412 | { 413 | unsigned short *flashSize = (unsigned short *) (FLASH_SIZE_REG);// Address register 414 | return ((int)(*flashSize & 0xffff) * 1024) + 0x08000000; 415 | } 416 | 417 | int getFlashPageSize(void) 418 | { 419 | 420 | unsigned short *flashSize = (unsigned short *) (FLASH_SIZE_REG);// Address register 421 | if ((*flashSize & 0xffff) > 128) 422 | { 423 | return 0x800; 424 | } 425 | else 426 | { 427 | return 0x400; 428 | } 429 | } 430 | -------------------------------------------------------------------------------- /hardware.h: -------------------------------------------------------------------------------- 1 | /* ***************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 LeafLabs LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ****************************************************************************/ 24 | 25 | #ifndef __HARDWARE_H 26 | #define __HARDWARE_H 27 | 28 | #include "stm32f10x_type.h" 29 | #include "cortexm3_macro.h" 30 | #include "common.h" 31 | 32 | /* macro'd register and peripheral definitions */ 33 | #define RCC ((u32)0x40021000) 34 | #define FLASH ((u32)0x40022000) 35 | #define GPIOA ((u32)0x40010800) 36 | #define GPIOB ((u32)0x40010C00) 37 | #define GPIOC ((u32)0x40011000) 38 | #define GPIOD ((u32)0x40011400) 39 | #define GPIOE ((u32)0x40011800) 40 | #define GPIOF ((u32)0x40011C00) 41 | #define GPIOG ((u32)0x40012000) 42 | #define AFIO_BASE ((u32)0x40010000) 43 | #define AFIO_MAPR (AFIO_BASE + 0x04) 44 | 45 | 46 | #define RCC_CR RCC 47 | #define RCC_CFGR (RCC + 0x04) 48 | #define RCC_CIR (RCC + 0x08) 49 | #define RCC_AHBENR (RCC + 0x14) 50 | #define RCC_APB2ENR (RCC + 0x18) 51 | #define RCC_APB1ENR (RCC + 0x1C) 52 | 53 | #define FLASH_ACR (FLASH + 0x00) 54 | #define FLASH_KEYR (FLASH + 0x04) 55 | #define FLASH_OPTKEYR (FLASH + 0x08) 56 | #define FLASH_SR (FLASH + 0x0C) 57 | #define FLASH_CR (FLASH + 0x10) 58 | #define FLASH_AR (FLASH + 0x14) 59 | #define FLASH_OBR (FLASH + 0x1C) 60 | #define FLASH_WRPR (FLASH + 0x20) 61 | 62 | #define FLASH_KEY1 0x45670123 63 | #define FLASH_KEY2 0xCDEF89AB 64 | #define FLASH_RDPRT 0x00A5 65 | #define FLASH_SR_BSY 0x01 66 | #define FLASH_CR_PER 0x02 67 | #define FLASH_CR_PG 0x01 68 | #define FLASH_CR_START 0x40 69 | 70 | #define GPIO_CRL(port) port 71 | #define GPIO_CRH(port) (port+0x04) 72 | #define GPIO_IDR(port) (port+0x08) 73 | #define GPIO_ODR(port) (port+0x0c) 74 | #define GPIO_BSRR(port) (port+0x10) 75 | #define GPIO_CR(port,pin) (port + (0x04*(pin>7))) 76 | 77 | #define CR_OUTPUT_OD 0x05 78 | #define CR_OUTPUT_PP 0x01 79 | #define CR_INPUT 0x04 80 | #define CR_INPUT_PU_PD 0x08 81 | 82 | #define SCS_BASE ((u32)0xE000E000) 83 | #define NVIC_BASE (SCS_BASE + 0x0100) 84 | #define SCB_BASE (SCS_BASE + 0x0D00) 85 | 86 | 87 | #define SCS 0xE000E000 88 | #define NVIC (SCS+0x100) 89 | #define SCB (SCS+0xD00) 90 | #define STK (SCS+0x10) 91 | 92 | #define SCB_VTOR (SCB+0x08) 93 | #define STK_CTRL (STK+0x00) 94 | 95 | #define TIM1_APB2_ENB ((u32)0x00000800) 96 | #define TIM1 ((u32)0x40012C00) 97 | #define TIM1_PSC (TIM1+0x28) 98 | #define TIM1_ARR (TIM1+0x2C) 99 | #define TIM1_RCR (TIM1+0x30) 100 | #define TIM1_CR1 (TIM1+0x00) 101 | #define TIM1_CR2 (TIM1+0x04) 102 | #define TIM1_DIER (TIM1+0x0C) 103 | #define TIM1_UP_IRQ_Channel ((u8)0x19) 104 | 105 | #define USB_HP_IRQ ((u8)0x13) 106 | #define USB_LP_IRQ ((u8)0x14) 107 | #define TIM2_IRQ ((u8)0x1C) 108 | 109 | 110 | /* AIRCR */ 111 | #define AIRCR_RESET 0x05FA0000 112 | #define AIRCR_RESET_REQ (AIRCR_RESET | (u32)0x04); 113 | 114 | /* temporary copyage of example from kiel */ 115 | #define __VAL(__TIMCLK, __PERIOD) ((__TIMCLK/1000000UL)*__PERIOD) 116 | #define __PSC(__TIMCLK, __PERIOD) (((__VAL(__TIMCLK, __PERIOD)+49999UL)/50000UL) - 1) 117 | #define __ARR(__TIMCLK, __PERIOD) ((__VAL(__TIMCLK, __PERIOD)/(__PSC(__TIMCLK, __PERIOD)+1)) - 1) 118 | 119 | // SWD and JTAG DEBUGGING 120 | #define AFIO_MAPR_SWJ_CFG (0x7 << 24) 121 | #define AFIO_MAPR_SWJ_CFG_FULL_SWJ (0x0 << 24) 122 | #define AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_NJRST (0x1 << 24) 123 | #define AFIO_MAPR_SWJ_CFG_NO_JTAG_SW (0x2 << 24) 124 | #define AFIO_MAPR_SWJ_CFG_NO_JTAG_NO_SW (0x4 << 24) 125 | 126 | 127 | 128 | 129 | // more bit twiddling to set Control register bits 130 | #define CR_SHITF(pin) ((pin - 8*(pin>7))<<2) 131 | 132 | #define SET_REG(addr,val) do { *(vu32*)(addr)=val; } while(0) 133 | #define GET_REG(addr) (*(vu32*)(addr)) 134 | 135 | 136 | /* todo: there must be some major misunderstanding in how we access 137 | regs. The direct access approach (GET_REG) causes the usb init to 138 | fail upon trying to activate RCC_APB1 |= 0x00800000. However, using 139 | the struct approach from ST, it works fine...temporarily switching 140 | to that approach */ 141 | typedef struct { 142 | vu32 CR; 143 | vu32 CFGR; 144 | vu32 CIR; 145 | vu32 APB2RSTR; 146 | vu32 APB1RSTR; 147 | vu32 AHBENR; 148 | vu32 APB2ENR; 149 | vu32 APB1ENR; 150 | vu32 BDCR; 151 | vu32 CSR; 152 | } RCC_RegStruct; 153 | #define pRCC ((RCC_RegStruct *) RCC) 154 | 155 | typedef struct { 156 | vu32 ISER[2]; 157 | u32 RESERVED0[30]; 158 | vu32 ICER[2]; 159 | u32 RSERVED1[30]; 160 | vu32 ISPR[2]; 161 | u32 RESERVED2[30]; 162 | vu32 ICPR[2]; 163 | u32 RESERVED3[30]; 164 | vu32 IABR[2]; 165 | u32 RESERVED4[62]; 166 | vu32 IPR[15]; 167 | } NVIC_TypeDef; 168 | 169 | typedef struct { 170 | u8 NVIC_IRQChannel; 171 | u8 NVIC_IRQChannelPreemptionPriority; 172 | u8 NVIC_IRQChannelSubPriority; 173 | bool NVIC_IRQChannelCmd; /* TRUE for enable */ 174 | } NVIC_InitTypeDef; 175 | 176 | typedef struct { 177 | vuc32 CPUID; 178 | vu32 ICSR; 179 | vu32 VTOR; 180 | vu32 AIRCR; 181 | vu32 SCR; 182 | vu32 CCR; 183 | vu32 SHPR[3]; 184 | vu32 SHCSR; 185 | vu32 CFSR; 186 | vu32 HFSR; 187 | vu32 DFSR; 188 | vu32 MMFAR; 189 | vu32 BFAR; 190 | vu32 AFSR; 191 | } SCB_TypeDef; 192 | 193 | 194 | /** Power interface register map. */ 195 | typedef struct pwr_reg_map { 196 | vu32 CR; /**< Control register */ 197 | vu32 CSR; /**< Control and status register */ 198 | } pwr_reg_map; 199 | 200 | /** Power peripheral register map base pointer. */ 201 | #define pPWR ((struct pwr_reg_map*)0x40007000) 202 | 203 | /** Disable backup domain write protection bit */ 204 | #define PWR_CR_DBP (1 << 8) 205 | 206 | 207 | /** Backup peripheral register map type. */ 208 | typedef struct bkp_reg_map { 209 | const u32 RESERVED1; ///< Reserved 210 | vu16 DR1; ///< Data register 1 211 | const u16 RESERVED2; 212 | vu16 DR2; ///< Data register 2 213 | const u16 RESERVED3; 214 | vu16 DR3; ///< Data register 3 215 | const u16 RESERVED4; 216 | vu16 DR4; ///< Data register 4 217 | const u16 RESERVED5; 218 | vu16 DR5; ///< Data register 5 219 | const u16 RESERVED6; 220 | vu16 DR6; ///< Data register 6 221 | const u16 RESERVED7; 222 | vu16 DR7; ///< Data register 7 223 | const u16 RESERVED8; 224 | vu16 DR8; ///< Data register 8 225 | const u16 RESERVED9; 226 | vu16 DR9; ///< Data register 9 227 | const u16 RESERVED10; 228 | vu16 DR10; ///< Data register 10 229 | const u16 RESERVED11; 230 | vu32 RTCCR; ///< RTC control register 231 | vu32 CR; ///< Control register 232 | vu32 CSR; ///< Control and status register 233 | } bkp_reg_map; 234 | 235 | /** Backup peripheral register map base pointer. */ 236 | #define pBKP ((struct bkp_reg_map*)0x40006C00) 237 | 238 | void bkp10Write(u16 value); 239 | 240 | //void setPin(u32 bank, u8 pin); 241 | //void resetPin(u32 bank, u8 pin); 242 | void gpio_write_bit(u32 bank, u8 pin, u8 val); 243 | unsigned int crMask(int pin); 244 | 245 | bool readPin(u32 bank, u8 pin); 246 | bool readButtonState(); 247 | void strobePin(u32 bank, u8 pin, u8 count, u32 rate,u8 onState); 248 | 249 | void systemHardReset(void); 250 | void systemReset(void); 251 | void setupCLK(void); 252 | void setupLEDAndButton(void); 253 | void setupFLASH(void); 254 | bool checkUserCode(u32 usrAddr); 255 | void jumpToUser(u32 usrAddr); 256 | int checkAndClearBootloaderFlag(); 257 | 258 | bool flashWriteWord(u32 addr, u32 word); 259 | bool flashErasePage(u32 addr); 260 | bool flashErasePages(u32 addr, u16 n); 261 | void flashLock(void); 262 | void flashUnlock(void); 263 | void nvicInit(NVIC_InitTypeDef *); 264 | void nvicDisableInterrupts(void); 265 | 266 | int getFlashEnd(void); 267 | int getFlashPageSize(void); 268 | 269 | #endif 270 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | /* ***************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 LeafLabs LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ****************************************************************************/ 24 | 25 | /** 26 | * @file main.c 27 | * 28 | * @brief main loop and calling any hardware init stuff. timing hacks for EEPROM 29 | * writes not to block usb interrupts. logic to handle 2 second timeout then 30 | * jump to user code. 31 | * 32 | */ 33 | 34 | #include "common.h" 35 | #include "dfu.h" 36 | extern volatile dfuUploadTypes_t userUploadType; 37 | 38 | int main() 39 | { 40 | bool no_user_jump = FALSE; 41 | bool dont_wait=FALSE; 42 | 43 | systemReset(); // peripherals but not PC 44 | setupCLK(); 45 | setupLEDAndButton(); 46 | setupUSB(); 47 | setupFLASH(); 48 | 49 | switch(checkAndClearBootloaderFlag()) 50 | { 51 | case 0x01: 52 | no_user_jump = TRUE; 53 | #if defined(LED_BANK) && defined(LED_PIN) && defined(LED_ON_STATE) 54 | strobePin(LED_BANK, LED_PIN, STARTUP_BLINKS, BLINK_FAST,LED_ON_STATE); 55 | #endif 56 | break; 57 | case 0x02: 58 | dont_wait=TRUE; 59 | break; 60 | default: 61 | #ifdef FASTBOOT 62 | dont_wait=TRUE; 63 | #else 64 | #if defined(LED_BANK) && defined(LED_PIN) && defined(LED_ON_STATE) 65 | strobePin(LED_BANK, LED_PIN, STARTUP_BLINKS, BLINK_FAST,LED_ON_STATE); 66 | #endif 67 | #endif 68 | 69 | if (!checkUserCode(USER_CODE_FLASH0X8005000) && !checkUserCode(USER_CODE_FLASH0X8002000)) 70 | { 71 | no_user_jump = TRUE; 72 | } 73 | else if (readButtonState()) 74 | { 75 | no_user_jump = TRUE; 76 | #ifdef FASTBOOT 77 | dont_wait=FALSE; 78 | #endif 79 | } 80 | break; 81 | } 82 | 83 | if (!dont_wait) 84 | { 85 | int delay_count = 0; 86 | 87 | while ((delay_count++ < BOOTLOADER_WAIT) || no_user_jump) 88 | { 89 | #if defined(LED_BANK) && defined(LED_PIN) && defined(LED_ON_STATE) 90 | strobePin(LED_BANK, LED_PIN, 1, BLINK_SLOW,LED_ON_STATE); 91 | #endif 92 | if (dfuUploadStarted()) 93 | { 94 | dfuFinishUpload(); // systemHardReset from DFU once done 95 | } 96 | } 97 | } 98 | 99 | if (checkUserCode(USER_CODE_FLASH0X8002000)) 100 | { 101 | jumpToUser(USER_CODE_FLASH0X8002000); 102 | } 103 | else 104 | { 105 | if (checkUserCode(USER_CODE_FLASH0X8005000)) 106 | { 107 | jumpToUser(USER_CODE_FLASH0X8005000); 108 | } 109 | else 110 | { 111 | // Nothing to execute in either Flash or RAM 112 | #if defined(LED_BANK) && defined(LED_PIN) && defined(LED_ON_STATE) 113 | strobePin(LED_BANK, LED_PIN, 5, BLINK_FAST,LED_ON_STATE); 114 | #endif 115 | systemHardReset(); 116 | } 117 | } 118 | 119 | return 0;// Added to please the compiler 120 | } 121 | -------------------------------------------------------------------------------- /make_all.bat: -------------------------------------------------------------------------------- 1 | make maple-mini 2 | make maple-rev3 3 | make maple-rev5 4 | make generic-none 5 | make generic-pc13 6 | make generic-pg15 7 | make generic-pd2 8 | make generic-pd1 9 | make generic-pa1 10 | make generic-pa1-button-pa8 11 | make generic-pb9 12 | make generic-pe2 13 | make generic-pa9 14 | make generic-pe5 15 | make generic-pe5-button-pa0 16 | make generic-pb7 17 | make generic-pb0 18 | make stbee 19 | make naze32 20 | make generic-pb12 21 | make hytiny-stm32f103t 22 | make dso138 23 | make gd32f1-generic-pc13 24 | make gd32f1-frankenmaple 25 | make cc3d 26 | make generic-pc13-fastboot 27 | make smart-v2 28 | -------------------------------------------------------------------------------- /merge_sketch_and_bootloaders.bat: -------------------------------------------------------------------------------- 1 | '@echo off 2 | setlocal disableDelayedExpansion 3 | for /f "delims=" %%A in ('forfiles /p .\bootloader_only_binaries /s /m *.bin /c "cmd /c echo @relpath"') do ( 4 | set "file=%%~A" 5 | setlocal enableDelayedExpansion 6 | .\sketch_combiner\bin_merge_tool\mergesketch.exe .\bootloader_only_binaries\!file:~2! .\sketch_combiner\congratulations_sketch\congratulations_sketch.ino.generic_stm32f103c.bin .\binaries\!file:~2! 7 | endlocal 8 | ) 9 | 10 | 11 | 12 | setlocal disableDelayedExpansion 13 | for /f "delims=" %%A in ('forfiles /p .\bootloader_only_binaries /s /m maple*.bin /c "cmd /c echo @relpath"') do ( 14 | set "file=%%~A" 15 | setlocal enableDelayedExpansion 16 | .\sketch_combiner\bin_merge_tool\mergesketch.exe .\bootloader_only_binaries\!file:~2! .\sketch_combiner\congratulations_sketch\congratulations_sketch.ino.maple_mini.bin .\binaries\!file:~2! 17 | endlocal 18 | ) 19 | 20 | .\sketch_combiner\bin_merge_tool\mergesketch.exe .\bootloader_only_binaries\gd32f1_generic_boot20_pc13.bin .\sketch_combiner\congratulations_sketch\congratulations_sketch.ino.generic_gd32f103c.bin .\binaries\gd32f1_generic_boot20_pc13.bin 21 | .\sketch_combiner\bin_merge_tool\mergesketch.exe .\bootloader_only_binaries\gd32f1_frankenmaple.bin .\sketch_combiner\congratulations_sketch\congratulations_sketch.ino.generic_gd32f103c.bin .\binaries\gd32f1_frankenmaple.bin 22 | 23 | -------------------------------------------------------------------------------- /platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; http://docs.platformio.org/page/projectconf.html 10 | 11 | [platformio] 12 | src_dir = . 13 | 14 | [env:maple-mini] 15 | platform = ststm32 16 | board = genericSTM32F103C8 17 | build_flags = -DTARGET_MAPLE_MINI -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 18 | src_filter = +<*> - - 19 | 20 | [env:maple-rev3] 21 | platform = ststm32 22 | board = genericSTM32F103C8 23 | build_flags = -DTARGET_MAPLE_REV3 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 24 | src_filter = +<*> - - 25 | 26 | [env:maple-rev5] 27 | platform = ststm32 28 | board = genericSTM32F103C8 29 | build_flags = -DTARGET_MAPLE_REV5 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 30 | src_filter = +<*> - - 31 | 32 | [env:generic-pc13] 33 | platform = ststm32 34 | board = genericSTM32F103C8 35 | build_flags = -DTARGET_GENERIC_F103_PC13 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 36 | src_filter = +<*> - - 37 | 38 | [env:generic-pg15] 39 | platform = ststm32 40 | board = genericSTM32F103C8 41 | build_flags = -DTARGET_GENERIC_F103_PG15 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 42 | src_filter = +<*> - - 43 | 44 | [env:generic-pd2] 45 | platform = ststm32 46 | board = genericSTM32F103C8 47 | build_flags = -DTARGET_GENERIC_F103_PD2 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 48 | src_filter = +<*> - - 49 | 50 | [env:generic-pd1] 51 | platform = ststm32 52 | board = genericSTM32F103C8 53 | build_flags = -DTARGET_GENERIC_F103_PD1 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 54 | src_filter = +<*> - - 55 | 56 | [env:generic-pa1] 57 | platform = ststm32 58 | board = genericSTM32F103C8 59 | build_flags = -DTARGET_GENERIC_F103_PA1 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 60 | src_filter = +<*> - - 61 | 62 | [env:generic-pa1-button-pa8] 63 | platform = ststm32 64 | board = genericSTM32F103C8 65 | build_flags = -DTARGET_GENERIC_F103_PA1_BUTTON_PA8 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 66 | src_filter = +<*> - - 67 | 68 | [env:generic-pb9] 69 | platform = ststm32 70 | board = genericSTM32F103C8 71 | build_flags = -DTARGET_GENERIC_F103_PB9 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 72 | src_filter = +<*> - - 73 | 74 | [env:generic-pe2] 75 | platform = ststm32 76 | board = genericSTM32F103C8 77 | build_flags = -DTARGET_GENERIC_F103_PE2 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 78 | src_filter = +<*> - - 79 | 80 | [env:generic-pa9] 81 | platform = ststm32 82 | board = genericSTM32F103C8 83 | build_flags = -DTARGET_GENERIC_F103_PA9 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 84 | src_filter = +<*> - - 85 | 86 | [env:generic-pe5] 87 | platform = ststm32 88 | board = genericSTM32F103C8 89 | build_flags = -DTARGET_GENERIC_F103_PE5 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 90 | src_filter = +<*> - - 91 | 92 | [env:generic-pe5-button-pa0] 93 | platform = ststm32 94 | board = genericSTM32F103C8 95 | build_flags = -DTARGET_GENERIC_F103_PE5_BUTTON_PA0 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 96 | src_filter = +<*> - - 97 | 98 | [env:generic-pb7] 99 | platform = ststm32 100 | board = genericSTM32F103C8 101 | build_flags = -DTARGET_GENERIC_F103_PB7 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 102 | src_filter = +<*> - - 103 | 104 | [env:generic-pb0] 105 | platform = ststm32 106 | board = genericSTM32F103C8 107 | build_flags = -DTARGET_GENERIC_F103_PB0 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 108 | src_filter = +<*> - - 109 | 110 | [env:stbee] 111 | platform = ststm32 112 | board = genericSTM32F103C8 113 | build_flags = -DTARGET_STBEE -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 114 | src_filter = +<*> - - 115 | 116 | [env:naze32] 117 | platform = ststm32 118 | board = genericSTM32F103C8 119 | build_flags = -DTARGET_NAZE32 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 120 | src_filter = +<*> - - 121 | 122 | [env:generic-pb12] 123 | platform = ststm32 124 | board = genericSTM32F103C8 125 | build_flags = -DTARGET_GENERIC_F103_PB12 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 126 | src_filter = +<*> - - 127 | 128 | [env:hytiny-stm32f103t] 129 | platform = ststm32 130 | board = genericSTM32F103C8 131 | build_flags = -DTARGET_HYTINY_STM32F103T -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 132 | src_filter = +<*> - - 133 | 134 | [env:dso138] 135 | platform = ststm32 136 | board = genericSTM32F103C8 137 | build_flags = -DTARGET_DSO138 -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 138 | src_filter = +<*> - - 139 | 140 | [env:gd32f1-frankenmaple] 141 | platform = ststm32 142 | board = genericSTM32F103C8 143 | build_flags = -DTARGET_GD32F1_FRANKENMAPLE -Istm32_lib -Iusb_lib -Wl,-Tstm32_lib/c_only_md_high_density.ld 144 | src_filter = +<*> - - 145 | 146 | -------------------------------------------------------------------------------- /sketch_combiner/bin_merge_tool/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char **argv) 6 | { 7 | FILE *fpBootloader; 8 | FILE *fpSketch; 9 | FILE *fpOutput; 10 | int sketchSize; 11 | int bootloaderSize; 12 | uint8_t *buf; 13 | const int SKETCH_OFFSET = 0x2000; 14 | 15 | 16 | if (argc!=4) 17 | { 18 | printf("\n%s Usage. mergebin bootloader.bin sketch.bin output.bin - Incorrect number of args\n\n",argv[0]); 19 | return -1; 20 | } 21 | 22 | fpBootloader = fopen (argv[1], "rb"); 23 | if (fpBootloader==NULL) 24 | { 25 | printf("Unable to open bootloader binary file %s\n",argv[1]); 26 | return -2; 27 | } 28 | 29 | fseek(fpBootloader, 0, SEEK_END); // seek to send of bootloader to determine its length 30 | bootloaderSize = ftell (fpBootloader); 31 | fseek(fpBootloader, 0, SEEK_SET); // go back to start, so we can read from the beginning 32 | printf("Opened bootloader file %s : size is %d\n",argv[1],bootloaderSize); 33 | 34 | fpSketch = fopen (argv[2], "rb"); 35 | if (fpSketch==NULL) 36 | { 37 | fclose(fpBootloader); 38 | printf("Unable to open sketch binary file %s\n",argv[2]); 39 | return -2; 40 | } 41 | 42 | 43 | fseek(fpSketch, 0, SEEK_END); // seek to send of sketch to determine its length 44 | sketchSize = ftell (fpSketch); 45 | fseek(fpSketch, 0, SEEK_SET); // go back to start, so we can read from the beginning 46 | printf("Opened sketch file %s : size is %d\n",argv[2],sketchSize); 47 | 48 | buf=malloc(sketchSize + SKETCH_OFFSET); 49 | if (buf==NULL) 50 | { 51 | fclose(fpBootloader); 52 | fclose(fpSketch); 53 | printf("Unable to open allocate memory\n"); 54 | return -3; 55 | } 56 | 57 | printf("Allocated %d bytes for buffer\n",sketchSize + SKETCH_OFFSET); 58 | 59 | fread(buf,bootloaderSize,1,fpBootloader); 60 | fclose(fpBootloader); 61 | 62 | fread(buf + SKETCH_OFFSET,sketchSize,1,fpSketch); 63 | fclose(fpSketch); 64 | 65 | fpOutput = fopen (argv[3], "wb"); 66 | if (fpOutput==NULL) 67 | { 68 | fclose(fpBootloader); 69 | fclose(fpSketch); 70 | printf("Unable to open output file %s for writing \n",argv[3]); 71 | return -2; 72 | } 73 | 74 | fwrite(buf,sketchSize+SKETCH_OFFSET,1,fpOutput); 75 | 76 | fclose(fpOutput); 77 | printf("Wrote combined file to %s \n",argv[3]); 78 | 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /sketch_combiner/bin_merge_tool/mergesketch.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/sketch_combiner/bin_merge_tool/mergesketch.exe -------------------------------------------------------------------------------- /sketch_combiner/congratulations_sketch/congratulations_sketch.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void setup() { 4 | Serial.begin(); 5 | } 6 | void loop() { 7 | Serial.println("Congratulations, you have installed the STM32duino bootloader"); 8 | Serial.println("See https://github.com/rogerclarkmelbourne/STM32duino-bootloader\n\n"); 9 | Serial.println("For more information about Arduino on STM32"); 10 | Serial.println("See https://www.stm32duino.com\n\n\n"); 11 | delay(1000); 12 | } 13 | -------------------------------------------------------------------------------- /sketch_combiner/congratulations_sketch/congratulations_sketch.ino.generic_gd32f103c.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/sketch_combiner/congratulations_sketch/congratulations_sketch.ino.generic_gd32f103c.bin -------------------------------------------------------------------------------- /sketch_combiner/congratulations_sketch/congratulations_sketch.ino.generic_stm32f103c.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/sketch_combiner/congratulations_sketch/congratulations_sketch.ino.generic_stm32f103c.bin -------------------------------------------------------------------------------- /sketch_combiner/congratulations_sketch/congratulations_sketch.ino.maple_mini.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rogerclarkmelbourne/STM32duino-bootloader/df689808b6030280480c0d151ee9c552ecf6b405/sketch_combiner/congratulations_sketch/congratulations_sketch.ino.maple_mini.bin -------------------------------------------------------------------------------- /stm32_lib/c_only_md.ld: -------------------------------------------------------------------------------- 1 | /* 2 | Default linker script for STM32F10x_128K_20K 3 | Original Copyright RAISONANCE S.A.S. 2008 4 | Modified P Harrison May 2009 5 | */ 6 | 7 | /* 8 | * Default stack sizes. 9 | * 10 | * These are used by the startup in order to allocate stacks for the different modes. 11 | * PROVIDE" allows to easily override these values from an object file or the commmand line. 12 | */ 13 | 14 | __Stack_Size = 1024 ; 15 | PROVIDE ( _Stack_Size = __Stack_Size ) ; 16 | __Stack_Init = _estack - __Stack_Size ; 17 | PROVIDE ( _Stack_Init = __Stack_Init ) ; 18 | 19 | /* 20 | *There will be a link error if there is not this amount of RAM free at the end. 21 | */ 22 | _Minimum_Stack_Size = 0x100 ; 23 | 24 | 25 | MEMORY 26 | { 27 | RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 3K 28 | FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 8K 29 | } 30 | 31 | /* higher address of the user mode stack */ 32 | _estack = 0x20002800; 33 | 34 | SECTIONS 35 | { 36 | /* 37 | * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, 38 | * which goes to FLASH 39 | */ 40 | .isr_vector : 41 | { 42 | . = ALIGN(4); 43 | KEEP(*(.isr_vector)) /* Startup code */ 44 | . = ALIGN(4); 45 | } >FLASH 46 | 47 | /* 48 | * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, 49 | * which goes to FLASH 50 | */ 51 | .flashtext : 52 | { 53 | . = ALIGN(4); 54 | KEEP (*(.flashtext)) /* Startup code */ 55 | . = ALIGN(4); 56 | } >FLASH 57 | 58 | /* 59 | * the program code is stored in the .text section, which goes to Flash 60 | */ 61 | .text : 62 | { 63 | . = ALIGN(4); 64 | *(.text) /* remaining code */ 65 | *(.text.*) /* remaining code */ 66 | *(.rodata) /* read-only data (constants) */ 67 | *(.rodata*) 68 | *(.glue_7) 69 | *(.glue_7t) 70 | . = ALIGN(4); 71 | _etext = .; 72 | _sidata = _etext; 73 | } >FLASH 74 | 75 | /* 76 | * This is the initialized data section. It is stored in RAM but the initial values 77 | * are held in flash and copied to RAM by the startup code 78 | */ 79 | 80 | /* we copy the important program globals vector in RAM as well, so that users can fool with it */ 81 | .data : AT ( _sidata ) /* AT makes the LMA follow on in the binary image */ 82 | { 83 | . = ALIGN(4); 84 | _sdata = . ; /* Used by the startup in order to initialize the .data section */ 85 | KEEP( *(.data) ) 86 | KEEP( *(.data.*) ) 87 | . = ALIGN(4); 88 | _edata = . ; /* Used by the startup in order to initialize the .data section */ 89 | } >RAM 90 | 91 | 92 | 93 | /* 94 | * This is the uninitialized data section. Date here is stored in RAM and will be 95 | * set to zero by the startup code. 96 | */ 97 | .bss : 98 | { 99 | . = ALIGN(4); 100 | _sbss = .; /* Used by the startup in order to initialize the .bss section */ 101 | *(.bss) 102 | *(COMMON) 103 | . = ALIGN(4); 104 | _ebss = . ; /* Used by the startup in order to initialize the .bss section */ 105 | } >RAM 106 | 107 | PROVIDE ( end = _ebss ); 108 | PROVIDE ( _end = _ebss ); 109 | 110 | /* 111 | * This is the user stack section 112 | * This is just to check that there is enough RAM left for the User mode stack 113 | * It should generate an error if it's full. 114 | */ 115 | ._usrstack : 116 | { 117 | . = ALIGN(4); 118 | _susrstack = . ; 119 | . = . + _Minimum_Stack_Size ; 120 | . = ALIGN(4); 121 | _eusrstack = . ; 122 | } >RAM 123 | 124 | /* 125 | * after that it's only debugging information. 126 | */ 127 | 128 | /* remove the debugging information from the standard libraries */ 129 | DISCARD : 130 | { 131 | libc.a ( * ) 132 | libm.a ( * ) 133 | libgcc.a ( * ) 134 | } 135 | 136 | /* Stabs debugging sections. */ 137 | .stab 0 : { *(.stab) } 138 | .stabstr 0 : { *(.stabstr) } 139 | .stab.excl 0 : { *(.stab.excl) } 140 | .stab.exclstr 0 : { *(.stab.exclstr) } 141 | .stab.index 0 : { *(.stab.index) } 142 | .stab.indexstr 0 : { *(.stab.indexstr) } 143 | .comment 0 : { *(.comment) } 144 | /* 145 | * DWARF debug sections. 146 | * Symbols in the DWARF debugging sections are relative to the beginning 147 | * of the section so we begin them at 0. 148 | */ 149 | 150 | /* DWARF 1 */ 151 | .debug 0 : { *(.debug) } 152 | .line 0 : { *(.line) } 153 | /* GNU DWARF 1 extensions */ 154 | .debug_srcinfo 0 : { *(.debug_srcinfo) } 155 | .debug_sfnames 0 : { *(.debug_sfnames) } 156 | /* DWARF 1.1 and DWARF 2 */ 157 | .debug_aranges 0 : { *(.debug_aranges) } 158 | .debug_pubnames 0 : { *(.debug_pubnames) } 159 | /* DWARF 2 */ 160 | .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } 161 | .debug_abbrev 0 : { *(.debug_abbrev) } 162 | .debug_line 0 : { *(.debug_line) } 163 | .debug_frame 0 : { *(.debug_frame) } 164 | .debug_str 0 : { *(.debug_str) } 165 | .debug_loc 0 : { *(.debug_loc) } 166 | .debug_macinfo 0 : { *(.debug_macinfo) } 167 | /* SGI/MIPS DWARF 2 extensions */ 168 | .debug_weaknames 0 : { *(.debug_weaknames) } 169 | .debug_funcnames 0 : { *(.debug_funcnames) } 170 | .debug_typenames 0 : { *(.debug_typenames) } 171 | .debug_varnames 0 : { *(.debug_varnames) } 172 | } 173 | -------------------------------------------------------------------------------- /stm32_lib/c_only_md_RAM.ld: -------------------------------------------------------------------------------- 1 | /* 2 | Default linker script for STM32F10x_128K_20K 3 | Original Copyright RAISONANCE S.A.S. 2008 4 | Modified P Harrison May 2009 5 | */ 6 | 7 | /* 8 | * Default stack sizes. 9 | * 10 | * These are used by the startup in order to allocate stacks for the different modes. 11 | * PROVIDE" allows to easily override these values from an object file or the commmand line. 12 | */ 13 | 14 | __Stack_Size = 1024 ; 15 | PROVIDE ( _Stack_Size = __Stack_Size ) ; 16 | __Stack_Init = _estack - __Stack_Size ; 17 | PROVIDE ( _Stack_Init = __Stack_Init ) ; 18 | 19 | /* 20 | *There will be a link error if there is not this amount of RAM free at the end. 21 | */ 22 | _Minimum_Stack_Size = 0x100 ; 23 | 24 | 25 | MEMORY 26 | { 27 | RAM (xrw) : ORIGIN = 0x20000C00, LENGTH = 17K 28 | } 29 | 30 | /* higher address of the user mode stack */ 31 | _estack = 0x20005000; 32 | _magicRate = 0x5000; 33 | SECTIONS 34 | { 35 | /* 36 | * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, 37 | * which goes to FLASH 38 | */ 39 | .isr_vector : 40 | { 41 | . = ALIGN(4); 42 | KEEP(*(.isr_vector)) /* Startup code */ 43 | . = ALIGN(4); 44 | } >RAM 45 | 46 | /* 47 | * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, 48 | * which goes to FLASH 49 | */ 50 | .flashtext : 51 | { 52 | . = ALIGN(4); 53 | KEEP (*(.flashtext)) /* Startup code */ 54 | . = ALIGN(4); 55 | } >RAM 56 | 57 | /* 58 | * the program code is stored in the .text section, which goes to Flash 59 | */ 60 | .text : 61 | { 62 | . = ALIGN(4); 63 | *(.text) /* remaining code */ 64 | *(.text.*) /* remaining code */ 65 | *(.rodata) /* read-only data (constants) */ 66 | *(.rodata*) 67 | *(.glue_7) 68 | *(.glue_7t) 69 | . = ALIGN(4); 70 | _etext = .; 71 | _sidata = _etext; /* Uused by the startup in order to initialize the .data secion */ 72 | } >RAM 73 | 74 | /* 75 | * This is the initialized data section. It is stored in RAM but the initial values 76 | * are held in flash and copied to RAM by the startup code 77 | */ 78 | .data : AT ( _sidata ) /* AT makes the LMA follow on in the binary image */ 79 | { 80 | . = ALIGN(4); 81 | _sdata = . ; /* Used by the startup in order to initialize the .data section */ 82 | KEEP( *(.data) ) 83 | KEEP( *(.data.*) ) 84 | . = ALIGN(4); 85 | _edata = . ; /* Used by the startup in order to initialize the .data section */ 86 | } >RAM 87 | 88 | 89 | 90 | /* 91 | * This is the uninitialized data section. Date here is stored in RAM and will be 92 | * set to zero by the startup code. 93 | */ 94 | .bss : 95 | { 96 | . = ALIGN(4); 97 | _sbss = .; /* Used by the startup in order to initialize the .bss section */ 98 | *(.bss) 99 | *(COMMON) 100 | . = ALIGN(4); 101 | _ebss = . ; /* Used by the startup in order to initialize the .bss section */ 102 | } >RAM 103 | 104 | PROVIDE ( end = _ebss ); 105 | PROVIDE ( _end = _ebss ); 106 | 107 | /* 108 | * This is the user stack section 109 | * This is just to check that there is enough RAM left for the User mode stack 110 | * It should generate an error if it's full. 111 | */ 112 | ._usrstack : 113 | { 114 | . = ALIGN(4); 115 | _susrstack = . ; 116 | . = . + _Minimum_Stack_Size ; 117 | . = ALIGN(4); 118 | _eusrstack = . ; 119 | } >RAM 120 | 121 | /* 122 | * after that it's only debugging information. 123 | */ 124 | 125 | /* remove the debugging information from the standard libraries */ 126 | DISCARD : 127 | { 128 | libc.a ( * ) 129 | libm.a ( * ) 130 | libgcc.a ( * ) 131 | } 132 | 133 | /* Stabs debugging sections. */ 134 | .stab 0 : { *(.stab) } 135 | .stabstr 0 : { *(.stabstr) } 136 | .stab.excl 0 : { *(.stab.excl) } 137 | .stab.exclstr 0 : { *(.stab.exclstr) } 138 | .stab.index 0 : { *(.stab.index) } 139 | .stab.indexstr 0 : { *(.stab.indexstr) } 140 | .comment 0 : { *(.comment) } 141 | /* 142 | * DWARF debug sections. 143 | * Symbols in the DWARF debugging sections are relative to the beginning 144 | * of the section so we begin them at 0. 145 | */ 146 | 147 | /* DWARF 1 */ 148 | .debug 0 : { *(.debug) } 149 | .line 0 : { *(.line) } 150 | /* GNU DWARF 1 extensions */ 151 | .debug_srcinfo 0 : { *(.debug_srcinfo) } 152 | .debug_sfnames 0 : { *(.debug_sfnames) } 153 | /* DWARF 1.1 and DWARF 2 */ 154 | .debug_aranges 0 : { *(.debug_aranges) } 155 | .debug_pubnames 0 : { *(.debug_pubnames) } 156 | /* DWARF 2 */ 157 | .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } 158 | .debug_abbrev 0 : { *(.debug_abbrev) } 159 | .debug_line 0 : { *(.debug_line) } 160 | .debug_frame 0 : { *(.debug_frame) } 161 | .debug_str 0 : { *(.debug_str) } 162 | .debug_loc 0 : { *(.debug_loc) } 163 | .debug_macinfo 0 : { *(.debug_macinfo) } 164 | /* SGI/MIPS DWARF 2 extensions */ 165 | .debug_weaknames 0 : { *(.debug_weaknames) } 166 | .debug_funcnames 0 : { *(.debug_funcnames) } 167 | .debug_typenames 0 : { *(.debug_typenames) } 168 | .debug_varnames 0 : { *(.debug_varnames) } 169 | } 170 | -------------------------------------------------------------------------------- /stm32_lib/c_only_md_high_density.ld: -------------------------------------------------------------------------------- 1 | /* 2 | Default linker script for STM32F10x_128K_20K 3 | Original Copyright RAISONANCE S.A.S. 2008 4 | Modified P Harrison May 2009 5 | */ 6 | 7 | /* 8 | * Default stack sizes. 9 | * 10 | * These are used by the startup in order to allocate stacks for the different modes. 11 | * PROVIDE" allows to easily override these values from an object file or the commmand line. 12 | */ 13 | 14 | __Stack_Size = 1024 ; 15 | PROVIDE ( _Stack_Size = __Stack_Size ) ; 16 | __Stack_Init = _estack - __Stack_Size ; 17 | PROVIDE ( _Stack_Init = __Stack_Init ) ; 18 | 19 | /* 20 | *There will be a link error if there is not this amount of RAM free at the end. 21 | */ 22 | _Minimum_Stack_Size = 0x100 ; 23 | 24 | 25 | MEMORY 26 | { 27 | RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 4K 28 | FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 8K 29 | } 30 | 31 | /* higher address of the user mode stack */ 32 | _estack = 0x20002800; 33 | 34 | SECTIONS 35 | { 36 | /* 37 | * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, 38 | * which goes to FLASH 39 | */ 40 | .isr_vector : 41 | { 42 | . = ALIGN(4); 43 | KEEP(*(.isr_vector)) /* Startup code */ 44 | . = ALIGN(4); 45 | } >FLASH 46 | 47 | /* 48 | * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, 49 | * which goes to FLASH 50 | */ 51 | .flashtext : 52 | { 53 | . = ALIGN(4); 54 | KEEP (*(.flashtext)) /* Startup code */ 55 | . = ALIGN(4); 56 | } >FLASH 57 | 58 | /* 59 | * the program code is stored in the .text section, which goes to Flash 60 | */ 61 | .text : 62 | { 63 | . = ALIGN(4); 64 | *(.text) /* remaining code */ 65 | *(.text.*) /* remaining code */ 66 | *(.rodata) /* read-only data (constants) */ 67 | *(.rodata*) 68 | *(.glue_7) 69 | *(.glue_7t) 70 | . = ALIGN(4); 71 | _etext = .; 72 | _sidata = _etext; 73 | } >FLASH 74 | 75 | /* 76 | * This is the initialized data section. It is stored in RAM but the initial values 77 | * are held in flash and copied to RAM by the startup code 78 | */ 79 | 80 | /* we copy the important program globals vector in RAM as well, so that users can fool with it */ 81 | .data : AT ( _sidata ) /* AT makes the LMA follow on in the binary image */ 82 | { 83 | . = ALIGN(4); 84 | _sdata = . ; /* Used by the startup in order to initialize the .data section */ 85 | KEEP( *(.data) ) 86 | KEEP( *(.data.*) ) 87 | . = ALIGN(4); 88 | _edata = . ; /* Used by the startup in order to initialize the .data section */ 89 | } >RAM 90 | 91 | 92 | 93 | /* 94 | * This is the uninitialized data section. Date here is stored in RAM and will be 95 | * set to zero by the startup code. 96 | */ 97 | .bss : 98 | { 99 | . = ALIGN(4); 100 | _sbss = .; /* Used by the startup in order to initialize the .bss section */ 101 | *(.bss) 102 | *(COMMON) 103 | . = ALIGN(4); 104 | _ebss = . ; /* Used by the startup in order to initialize the .bss section */ 105 | } >RAM 106 | 107 | PROVIDE ( end = _ebss ); 108 | PROVIDE ( _end = _ebss ); 109 | 110 | /* 111 | * This is the user stack section 112 | * This is just to check that there is enough RAM left for the User mode stack 113 | * It should generate an error if it's full. 114 | */ 115 | ._usrstack : 116 | { 117 | . = ALIGN(4); 118 | _susrstack = . ; 119 | . = . + _Minimum_Stack_Size ; 120 | . = ALIGN(4); 121 | _eusrstack = . ; 122 | } >RAM 123 | 124 | /* 125 | * after that it's only debugging information. 126 | */ 127 | 128 | /* remove the debugging information from the standard libraries */ 129 | DISCARD : 130 | { 131 | libc.a ( * ) 132 | libm.a ( * ) 133 | libgcc.a ( * ) 134 | } 135 | 136 | /* Stabs debugging sections. */ 137 | .stab 0 : { *(.stab) } 138 | .stabstr 0 : { *(.stabstr) } 139 | .stab.excl 0 : { *(.stab.excl) } 140 | .stab.exclstr 0 : { *(.stab.exclstr) } 141 | .stab.index 0 : { *(.stab.index) } 142 | .stab.indexstr 0 : { *(.stab.indexstr) } 143 | .comment 0 : { *(.comment) } 144 | /* 145 | * DWARF debug sections. 146 | * Symbols in the DWARF debugging sections are relative to the beginning 147 | * of the section so we begin them at 0. 148 | */ 149 | 150 | /* DWARF 1 */ 151 | .debug 0 : { *(.debug) } 152 | .line 0 : { *(.line) } 153 | /* GNU DWARF 1 extensions */ 154 | .debug_srcinfo 0 : { *(.debug_srcinfo) } 155 | .debug_sfnames 0 : { *(.debug_sfnames) } 156 | /* DWARF 1.1 and DWARF 2 */ 157 | .debug_aranges 0 : { *(.debug_aranges) } 158 | .debug_pubnames 0 : { *(.debug_pubnames) } 159 | /* DWARF 2 */ 160 | .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } 161 | .debug_abbrev 0 : { *(.debug_abbrev) } 162 | .debug_line 0 : { *(.debug_line) } 163 | .debug_frame 0 : { *(.debug_frame) } 164 | .debug_str 0 : { *(.debug_str) } 165 | .debug_loc 0 : { *(.debug_loc) } 166 | .debug_macinfo 0 : { *(.debug_macinfo) } 167 | /* SGI/MIPS DWARF 2 extensions */ 168 | .debug_weaknames 0 : { *(.debug_weaknames) } 169 | .debug_funcnames 0 : { *(.debug_funcnames) } 170 | .debug_typenames 0 : { *(.debug_typenames) } 171 | .debug_varnames 0 : { *(.debug_varnames) } 172 | } 173 | -------------------------------------------------------------------------------- /stm32_lib/c_only_startup.s: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file startup_stm32f10x_md.s 4 | * @author MCD Application Team 5 | * @version V3.1.0 6 | * @date 06/19/2009 7 | * @brief STM32F10x Medium Density Devices vector table for RIDE7 toolchain. 8 | * This module performs: 9 | * - Set the initial SP 10 | * - Set the initial PC == Reset_Handler, 11 | * - Set the vector table entries with the exceptions ISR address 12 | * - Branches to main in the C library (which eventually 13 | * calls main()). 14 | * After Reset the Cortex-M3 processor is in Thread mode, 15 | * priority is Privileged, and the Stack is set to Main. 16 | ******************************************************************************* 17 | * @copy 18 | * 19 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 20 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 21 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 22 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 23 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 24 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 25 | * 26 | *

© COPYRIGHT 2009 STMicroelectronics

27 | */ 28 | 29 | .syntax unified 30 | .cpu cortex-m3 31 | .fpu softvfp 32 | .thumb 33 | 34 | .global g_pfnVectors 35 | .global SystemInit_ExtMemCtl_Dummy 36 | .global Default_Handler 37 | 38 | /* start address for the initialization values of the .data section. 39 | defined in linker script */ 40 | .word _sidata 41 | /* start address for the .data section. defined in linker script */ 42 | .word _sdata 43 | /* end address for the .data section. defined in linker script */ 44 | .word _edata 45 | /* start address for the .bss section. defined in linker script */ 46 | .word _sbss 47 | /* end address for the .bss section. defined in linker script */ 48 | .word _ebss 49 | 50 | .equ BootRAM, 0xF108F85F 51 | /** 52 | * @brief This is the code that gets called when the processor first 53 | * starts execution following a reset event. Only the absolutely 54 | * necessary set is performed, after which the application 55 | * supplied main() routine is called. 56 | * @param None 57 | * @retval : None 58 | */ 59 | 60 | .section .text.Reset_Handler 61 | .weak Reset_Handler 62 | .type Reset_Handler, %function 63 | Reset_Handler: 64 | 65 | /* Copy the data segment initializers from flash to SRAM */ 66 | movs r1, #0 67 | b LoopCopyDataInit 68 | 69 | CopyDataInit: 70 | ldr r3, =_sidata 71 | ldr r3, [r3, r1] 72 | str r3, [r0, r1] 73 | adds r1, r1, #4 74 | 75 | LoopCopyDataInit: 76 | ldr r0, =_sdata 77 | ldr r3, =_edata 78 | adds r2, r0, r1 79 | cmp r2, r3 80 | bcc CopyDataInit 81 | ldr r2, =_sbss 82 | b LoopFillZerobss 83 | /* Zero fill the bss segment. */ 84 | FillZerobss: 85 | movs r3, #0 86 | str r3, [r2], #4 87 | 88 | LoopFillZerobss: 89 | ldr r3, = _ebss 90 | cmp r2, r3 91 | bcc FillZerobss 92 | /* Call the application's entry point.*/ 93 | bl main 94 | bx lr 95 | .size Reset_Handler, .-Reset_Handler 96 | 97 | /** 98 | * @brief This is the code that gets called when the processor receives an 99 | * unexpected interrupt. This simply enters an infinite loop, preserving 100 | * the system state for examination by a debugger. 101 | * 102 | * @param None 103 | * @retval : None 104 | */ 105 | .section .text.Default_Handler,"ax",%progbits 106 | Default_Handler: 107 | Infinite_Loop: 108 | b Infinite_Loop 109 | .size Default_Handler, .-Default_Handler 110 | /****************************************************************************** 111 | * 112 | * The minimal vector table for a Cortex M3. Note that the proper constructs 113 | * must be placed on this to ensure that it ends up at physical address 114 | * 0x0000.0000. 115 | * 116 | ******************************************************************************/ 117 | .section .isr_vector,"a",%progbits 118 | .type g_pfnVectors, %object 119 | .size g_pfnVectors, .-g_pfnVectors 120 | 121 | 122 | g_pfnVectors: 123 | .word _estack 124 | .word Reset_Handler 125 | .word NMI_Handler 126 | .word HardFault_Handler 127 | .word MemManage_Handler 128 | .word BusFault_Handler 129 | .word UsageFault_Handler 130 | .word 0 131 | .word 0 132 | .word 0 133 | .word 0 134 | .word SVC_Handler 135 | .word DebugMon_Handler 136 | .word 0 137 | .word PendSV_Handler 138 | .word SysTick_Handler 139 | .word WWDG_IRQHandler 140 | .word PVD_IRQHandler 141 | .word TAMPER_IRQHandler 142 | .word RTC_IRQHandler 143 | .word FLASH_IRQHandler 144 | .word RCC_IRQHandler 145 | .word EXTI0_IRQHandler 146 | .word EXTI1_IRQHandler 147 | .word EXTI2_IRQHandler 148 | .word EXTI3_IRQHandler 149 | .word EXTI4_IRQHandler 150 | .word DMA1_Channel1_IRQHandler 151 | .word DMA1_Channel2_IRQHandler 152 | .word DMA1_Channel3_IRQHandler 153 | .word DMA1_Channel4_IRQHandler 154 | .word DMA1_Channel5_IRQHandler 155 | .word DMA1_Channel6_IRQHandler 156 | .word DMA1_Channel7_IRQHandler 157 | .word ADC1_2_IRQHandler 158 | .word USB_HP_CAN1_TX_IRQHandler 159 | .word USB_LP_CAN1_RX0_IRQHandler 160 | .word CAN1_RX1_IRQHandler 161 | .word CAN1_SCE_IRQHandler 162 | .word EXTI9_5_IRQHandler 163 | .word TIM1_BRK_IRQHandler 164 | .word TIM1_UP_IRQHandler 165 | .word TIM1_TRG_COM_IRQHandler 166 | .word TIM1_CC_IRQHandler 167 | .word TIM2_IRQHandler 168 | .word TIM3_IRQHandler 169 | .word TIM4_IRQHandler 170 | .word I2C1_EV_IRQHandler 171 | .word I2C1_ER_IRQHandler 172 | .word I2C2_EV_IRQHandler 173 | .word I2C2_ER_IRQHandler 174 | .word SPI1_IRQHandler 175 | .word SPI2_IRQHandler 176 | .word USART1_IRQHandler 177 | .word USART2_IRQHandler 178 | .word USART3_IRQHandler 179 | .word EXTI15_10_IRQHandler 180 | .word RTCAlarm_IRQHandler 181 | .word USBWakeUp_IRQHandler 182 | /* 183 | .word TIM8_BRK 184 | .word TIM8_UP 185 | .word TIM8_TRG_COM 186 | .word TIM8_CC 187 | .word ADC3 188 | .word FSMC 189 | .word SDIO 190 | .word TIM5 191 | .word SPI3 192 | .word UART4 193 | .word UART5 194 | .word TIM6 195 | .word TIM7 196 | .word DMA2_Channel1 197 | .word DMA2_Channel2 198 | .word DMA2_Channel3 199 | .word DMA2_Channel5 200 | */ 201 | .word BootRAM /* @0x108. This is for boot in RAM mode for 202 | STM32F10x Medium Density devices. */ 203 | 204 | /******************************************************************************* 205 | * 206 | * Provide weak aliases for each Exception handler to the Default_Handler. 207 | * As they are weak aliases, any function with the same name will override 208 | * this definition. 209 | * 210 | *******************************************************************************/ 211 | 212 | .weak NMI_Handler 213 | .thumb_set NMI_Handler,Default_Handler 214 | 215 | .weak HardFault_Handler 216 | .thumb_set HardFault_Handler,Default_Handler 217 | 218 | .weak MemManage_Handler 219 | .thumb_set MemManage_Handler,Default_Handler 220 | 221 | .weak BusFault_Handler 222 | .thumb_set BusFault_Handler,Default_Handler 223 | 224 | .weak UsageFault_Handler 225 | .thumb_set UsageFault_Handler,Default_Handler 226 | 227 | .weak SVC_Handler 228 | .thumb_set SVC_Handler,Default_Handler 229 | 230 | .weak DebugMon_Handler 231 | .thumb_set DebugMon_Handler,Default_Handler 232 | 233 | .weak PendSV_Handler 234 | .thumb_set PendSV_Handler,Default_Handler 235 | 236 | .weak SysTick_Handler 237 | .thumb_set SysTick_Handler,Default_Handler 238 | 239 | .weak WWDG_IRQHandler 240 | .thumb_set WWDG_IRQHandler,Default_Handler 241 | 242 | .weak PVD_IRQHandler 243 | .thumb_set PVD_IRQHandler,Default_Handler 244 | 245 | .weak TAMPER_IRQHandler 246 | .thumb_set TAMPER_IRQHandler,Default_Handler 247 | 248 | .weak RTC_IRQHandler 249 | .thumb_set RTC_IRQHandler,Default_Handler 250 | 251 | .weak FLASH_IRQHandler 252 | .thumb_set FLASH_IRQHandler,Default_Handler 253 | 254 | .weak RCC_IRQHandler 255 | .thumb_set RCC_IRQHandler,Default_Handler 256 | 257 | .weak EXTI0_IRQHandler 258 | .thumb_set EXTI0_IRQHandler,Default_Handler 259 | 260 | .weak EXTI1_IRQHandler 261 | .thumb_set EXTI1_IRQHandler,Default_Handler 262 | 263 | .weak EXTI2_IRQHandler 264 | .thumb_set EXTI2_IRQHandler,Default_Handler 265 | 266 | .weak EXTI3_IRQHandler 267 | .thumb_set EXTI3_IRQHandler,Default_Handler 268 | 269 | .weak EXTI4_IRQHandler 270 | .thumb_set EXTI4_IRQHandler,Default_Handler 271 | 272 | .weak DMA1_Channel1_IRQHandler 273 | .thumb_set DMA1_Channel1_IRQHandler,Default_Handler 274 | 275 | .weak DMA1_Channel2_IRQHandler 276 | .thumb_set DMA1_Channel2_IRQHandler,Default_Handler 277 | 278 | .weak DMA1_Channel3_IRQHandler 279 | .thumb_set DMA1_Channel3_IRQHandler,Default_Handler 280 | 281 | .weak DMA1_Channel4_IRQHandler 282 | .thumb_set DMA1_Channel4_IRQHandler,Default_Handler 283 | 284 | .weak DMA1_Channel5_IRQHandler 285 | .thumb_set DMA1_Channel5_IRQHandler,Default_Handler 286 | 287 | .weak DMA1_Channel6_IRQHandler 288 | .thumb_set DMA1_Channel6_IRQHandler,Default_Handler 289 | 290 | .weak DMA1_Channel7_IRQHandler 291 | .thumb_set DMA1_Channel7_IRQHandler,Default_Handler 292 | 293 | .weak ADC1_2_IRQHandler 294 | .thumb_set ADC1_2_IRQHandler,Default_Handler 295 | 296 | .weak USB_HP_CAN1_TX_IRQHandler 297 | .thumb_set USB_HP_CAN1_TX_IRQHandler,Default_Handler 298 | 299 | .weak USB_LP_CAN1_RX0_IRQHandler 300 | .thumb_set USB_LP_CAN1_RX0_IRQHandler,Default_Handler 301 | 302 | .weak CAN1_RX1_IRQHandler 303 | .thumb_set CAN1_RX1_IRQHandler,Default_Handler 304 | 305 | .weak CAN1_SCE_IRQHandler 306 | .thumb_set CAN1_SCE_IRQHandler,Default_Handler 307 | 308 | .weak EXTI9_5_IRQHandler 309 | .thumb_set EXTI9_5_IRQHandler,Default_Handler 310 | 311 | .weak TIM1_BRK_IRQHandler 312 | .thumb_set TIM1_BRK_IRQHandler,Default_Handler 313 | 314 | .weak TIM1_UP_IRQHandler 315 | .thumb_set TIM1_UP_IRQHandler,Default_Handler 316 | 317 | .weak TIM1_TRG_COM_IRQHandler 318 | .thumb_set TIM1_TRG_COM_IRQHandler,Default_Handler 319 | 320 | .weak TIM1_CC_IRQHandler 321 | .thumb_set TIM1_CC_IRQHandler,Default_Handler 322 | 323 | .weak TIM2_IRQHandler 324 | .thumb_set TIM2_IRQHandler,Default_Handler 325 | 326 | .weak TIM3_IRQHandler 327 | .thumb_set TIM3_IRQHandler,Default_Handler 328 | 329 | .weak TIM4_IRQHandler 330 | .thumb_set TIM4_IRQHandler,Default_Handler 331 | 332 | .weak I2C1_EV_IRQHandler 333 | .thumb_set I2C1_EV_IRQHandler,Default_Handler 334 | 335 | .weak I2C1_ER_IRQHandler 336 | .thumb_set I2C1_ER_IRQHandler,Default_Handler 337 | 338 | .weak I2C2_EV_IRQHandler 339 | .thumb_set I2C2_EV_IRQHandler,Default_Handler 340 | 341 | .weak I2C2_ER_IRQHandler 342 | .thumb_set I2C2_ER_IRQHandler,Default_Handler 343 | 344 | .weak SPI1_IRQHandler 345 | .thumb_set SPI1_IRQHandler,Default_Handler 346 | 347 | .weak SPI2_IRQHandler 348 | .thumb_set SPI2_IRQHandler,Default_Handler 349 | 350 | .weak USART1_IRQHandler 351 | .thumb_set USART1_IRQHandler,Default_Handler 352 | 353 | .weak USART2_IRQHandler 354 | .thumb_set USART2_IRQHandler,Default_Handler 355 | 356 | .weak USART3_IRQHandler 357 | .thumb_set USART3_IRQHandler,Default_Handler 358 | 359 | .weak EXTI15_10_IRQHandler 360 | .thumb_set EXTI15_10_IRQHandler,Default_Handler 361 | 362 | .weak RTCAlarm_IRQHandler 363 | .thumb_set RTCAlarm_IRQHandler,Default_Handler 364 | 365 | .weak USBWakeUp_IRQHandler 366 | .thumb_set USBWakeUp_IRQHandler,Default_Handler 367 | 368 | -------------------------------------------------------------------------------- /stm32_lib/c_only_startup_user.s: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file startup_stm32f10x_md.s 4 | * @author MCD Application Team 5 | * @version V3.1.0 6 | * @date 06/19/2009 7 | * @brief STM32F10x Medium Density Devices vector table for RIDE7 toolchain. 8 | * This module performs: 9 | * - Set the initial SP 10 | * - Set the initial PC == Reset_Handler, 11 | * - Set the vector table entries with the exceptions ISR address 12 | * - Branches to main in the C library (which eventually 13 | * calls main()). 14 | * After Reset the Cortex-M3 processor is in Thread mode, 15 | * priority is Privileged, and the Stack is set to Main. 16 | ******************************************************************************* 17 | * @copy 18 | * 19 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 20 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 21 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 22 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 23 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 24 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 25 | * 26 | *

© COPYRIGHT 2009 STMicroelectronics

27 | */ 28 | 29 | .syntax unified 30 | .cpu cortex-m3 31 | .fpu softvfp 32 | .thumb 33 | 34 | .global g_pfnVectors 35 | .global SystemInit_ExtMemCtl_Dummy 36 | .global Default_Handler 37 | 38 | /* start address for the initialization values of the .data section. 39 | defined in linker script */ 40 | .word _sidata 41 | /* start address for the .data section. defined in linker script */ 42 | .word _sdata 43 | /* end address for the .data section. defined in linker script */ 44 | .word _edata 45 | /* start address for the .bss section. defined in linker script */ 46 | .word _sbss 47 | /* end address for the .bss section. defined in linker script */ 48 | .word _ebss 49 | .word _magicRate 50 | 51 | .equ BootRAM, 0xF108F85F 52 | /** 53 | * @brief This is the code that gets called when the processor first 54 | * starts execution following a reset event. Only the absolutely 55 | * necessary set is performed, after which the application 56 | * supplied main() routine is called. 57 | * @param None 58 | * @retval : None 59 | */ 60 | 61 | .section .text.Reset_Handler 62 | .weak Reset_Handler 63 | .type Reset_Handler, %function 64 | Reset_Handler: 65 | 66 | /* Copy the data segment initializers from flash to SRAM */ 67 | movs r1, #0 68 | b LoopCopyDataInit 69 | 70 | CopyDataInit: 71 | ldr r3, =_sidata 72 | ldr r3, [r3, r1] 73 | str r3, [r0, r1] 74 | adds r1, r1, #4 75 | 76 | LoopCopyDataInit: 77 | ldr r0, =_sdata 78 | ldr r3, =_edata 79 | adds r2, r0, r1 80 | cmp r2, r3 81 | bcc CopyDataInit 82 | ldr r2, =_sbss 83 | b LoopFillZerobss 84 | /* Zero fill the bss segment. */ 85 | FillZerobss: 86 | movs r3, #0 87 | str r3, [r2], #4 88 | 89 | LoopFillZerobss: 90 | ldr r3, = _ebss 91 | cmp r2, r3 92 | bcc FillZerobss 93 | /* Call the application's entry point.*/ 94 | bl main 95 | bx lr 96 | .size Reset_Handler, .-Reset_Handler 97 | 98 | /** 99 | * @brief This is the code that gets called when the processor receives an 100 | * unexpected interrupt. This simply enters an infinite loop, preserving 101 | * the system state for examination by a debugger. 102 | * 103 | * @param None 104 | * @retval : None 105 | */ 106 | .section .text.Default_Handler,"ax",%progbits 107 | Default_Handler: 108 | Infinite_Loop: 109 | b Infinite_Loop 110 | .size Default_Handler, .-Default_Handler 111 | /****************************************************************************** 112 | * 113 | * The minimal vector table for a Cortex M3. Note that the proper constructs 114 | * must be placed on this to ensure that it ends up at physical address 115 | * 0x0000.0000. 116 | * 117 | ******************************************************************************/ 118 | .section .isr_vector,"a",%progbits 119 | .type g_pfnVectors, %object 120 | .size g_pfnVectors, .-g_pfnVectors 121 | 122 | 123 | g_pfnVectors: 124 | .word _estack 125 | .word Reset_Handler 126 | .word _magicRate 127 | 128 | 129 | -------------------------------------------------------------------------------- /stm32_lib/cortexm3_macro.h: -------------------------------------------------------------------------------- 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 2 | * File Name : cortexm3_macro.h 3 | * Author : MCD Application Team 4 | * Version : V2.0.3 5 | * Date : 09/22/2008 6 | * Description : Header file for cortexm3_macro.s. 7 | ******************************************************************************** 8 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 9 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 10 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 11 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 12 | * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 13 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 14 | *******************************************************************************/ 15 | 16 | /* Define to prevent recursive inclusion -------------------------------------*/ 17 | #ifndef __CORTEXM3_MACRO_H 18 | #define __CORTEXM3_MACRO_H 19 | 20 | /* Includes ------------------------------------------------------------------*/ 21 | #include "stm32f10x_type.h" 22 | 23 | /* Exported types ------------------------------------------------------------*/ 24 | /* Exported constants --------------------------------------------------------*/ 25 | /* Exported macro ------------------------------------------------------------*/ 26 | /* Exported functions ------------------------------------------------------- */ 27 | void __WFI(void); 28 | void __WFE(void); 29 | void __SEV(void); 30 | void __ISB(void); 31 | void __DSB(void); 32 | void __DMB(void); 33 | void __SVC(void); 34 | u32 __MRS_CONTROL(void); 35 | void __MSR_CONTROL(u32 Control); 36 | u32 __MRS_PSP(void); 37 | void __MSR_PSP(u32 TopOfProcessStack); 38 | u32 __MRS_MSP(void); 39 | void __MSR_MSP(u32 TopOfMainStack); 40 | void __RESETPRIMASK(void); 41 | void __SETPRIMASK(void); 42 | u32 __READ_PRIMASK(void); 43 | void __RESETFAULTMASK(void); 44 | void __SETFAULTMASK(void); 45 | u32 __READ_FAULTMASK(void); 46 | void __BASEPRICONFIG(u32 NewPriority); 47 | u32 __GetBASEPRI(void); 48 | u16 __REV_HalfWord(u16 Data); 49 | u32 __REV_Word(u32 Data); 50 | 51 | #endif /* __CORTEXM3_MACRO_H */ 52 | 53 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ 54 | -------------------------------------------------------------------------------- /stm32_lib/cortexm3_macro.s: -------------------------------------------------------------------------------- 1 | # 1 "./stm32_lib/cortexm3_macro.S" 2 | # 1 "" 3 | # 1 "" 4 | # 1 "./stm32_lib/cortexm3_macro.S" 5 | # 16 "./stm32_lib/cortexm3_macro.S" 6 | .cpu cortex-m3 7 | .fpu softvfp 8 | .syntax unified 9 | .thumb 10 | .text 11 | 12 | 13 | .globl __WFI 14 | .globl __WFE 15 | .globl __SEV 16 | .globl __ISB 17 | .globl __DSB 18 | .globl __DMB 19 | .globl __SVC 20 | .globl __MRS_CONTROL 21 | .globl __MSR_CONTROL 22 | .globl __MRS_PSP 23 | .globl __MSR_PSP 24 | .globl __MRS_MSP 25 | .globl __MSR_MSP 26 | .globl __RESETPRIMASK 27 | .globl __SETPRIMASK 28 | .globl __READ_PRIMASK 29 | .globl __RESETFAULTMASK 30 | .globl __SETFAULTMASK 31 | .globl __READ_FAULTMASK 32 | .globl __BASEPRICONFIG 33 | .globl __GetBASEPRI 34 | .globl __REV_HalfWord 35 | .globl __REV_Word 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | .thumb_func 44 | __WFI: 45 | 46 | WFI 47 | BX r14 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | .thumb_func 56 | __WFE: 57 | 58 | WFE 59 | BX r14 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | .thumb_func 68 | __SEV: 69 | 70 | SEV 71 | BX r14 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | .thumb_func 80 | __ISB: 81 | 82 | ISB 83 | BX r14 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | .thumb_func 92 | __DSB: 93 | 94 | DSB 95 | BX r14 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | .thumb_func 104 | __DMB: 105 | 106 | DMB 107 | BX r14 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | .thumb_func 116 | __SVC: 117 | 118 | SVC 0x01 119 | BX r14 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | .thumb_func 128 | __MRS_CONTROL: 129 | 130 | MRS r0,control 131 | BX r14 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | .thumb_func 140 | __MSR_CONTROL: 141 | 142 | MSR control, r0 143 | ISB 144 | BX r14 145 | 146 | 147 | 148 | 149 | 150 | 151 | .thumb_func 152 | __MRS_PSP: 153 | 154 | MRS r0, psp 155 | BX r14 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | .thumb_func 164 | __MSR_PSP: 165 | 166 | MSR psp, r0 167 | BX r14 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | .thumb_func 176 | __MRS_MSP: 177 | 178 | MRS r0, msp 179 | BX r14 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | .thumb_func 188 | __MSR_MSP: 189 | 190 | MSR msp, r0 191 | BX r14 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | .thumb_func 200 | __RESETPRIMASK: 201 | 202 | CPSIE i 203 | BX r14 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | .thumb_func 212 | __SETPRIMASK: 213 | 214 | CPSID i 215 | BX r14 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | .thumb_func 224 | __READ_PRIMASK: 225 | 226 | MRS r0, PRIMASK 227 | BX r14 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | .thumb_func 236 | __RESETFAULTMASK: 237 | 238 | CPSIE f 239 | BX r14 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | .thumb_func 248 | __SETFAULTMASK: 249 | 250 | CPSID f 251 | BX r14 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | .thumb_func 260 | __READ_FAULTMASK: 261 | 262 | MRS r0, FAULTMASK 263 | BX r14 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | .thumb_func 272 | __BASEPRICONFIG: 273 | 274 | MSR basepri, r0 275 | BX r14 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | .thumb_func 284 | __GetBASEPRI: 285 | 286 | MRS r0, basepri_max 287 | BX r14 288 | 289 | 290 | 291 | 292 | 293 | 294 | .thumb_func 295 | __REV_HalfWord: 296 | 297 | REV16 r0, r0 298 | BX r14 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | .thumb_func 307 | __REV_Word: 308 | 309 | REV r0, r0 310 | BX r14 311 | 312 | .end 313 | -------------------------------------------------------------------------------- /stm32_lib/stm32f10x_type.h: -------------------------------------------------------------------------------- 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 2 | * File Name : stm32f10x_type.h 3 | * Author : MCD Application Team 4 | * Version : V2.0.3 5 | * Date : 09/22/2008 6 | * Description : This file contains all the common data types used for the 7 | * STM32F10x firmware library. 8 | ******************************************************************************** 9 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 10 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 11 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 12 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 13 | * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 14 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 15 | *******************************************************************************/ 16 | 17 | /* Define to prevent recursive inclusion -------------------------------------*/ 18 | #ifndef __STM32F10x_TYPE_H 19 | #define __STM32F10x_TYPE_H 20 | 21 | /* Includes ------------------------------------------------------------------*/ 22 | /* Exported types ------------------------------------------------------------*/ 23 | typedef signed long s32; 24 | typedef signed short s16; 25 | typedef signed char s8; 26 | 27 | typedef signed long const sc32; /* Read Only */ 28 | typedef signed short const sc16; /* Read Only */ 29 | typedef signed char const sc8; /* Read Only */ 30 | 31 | typedef volatile signed long vs32; 32 | typedef volatile signed short vs16; 33 | typedef volatile signed char vs8; 34 | 35 | typedef volatile signed long const vsc32; /* Read Only */ 36 | typedef volatile signed short const vsc16; /* Read Only */ 37 | typedef volatile signed char const vsc8; /* Read Only */ 38 | 39 | typedef unsigned long u32; 40 | typedef unsigned short u16; 41 | typedef unsigned char u8; 42 | 43 | typedef unsigned long const uc32; /* Read Only */ 44 | typedef unsigned short const uc16; /* Read Only */ 45 | typedef unsigned char const uc8; /* Read Only */ 46 | 47 | typedef volatile unsigned long vu32; 48 | typedef volatile unsigned short vu16; 49 | typedef volatile unsigned char vu8; 50 | 51 | typedef volatile unsigned long const vuc32; /* Read Only */ 52 | typedef volatile unsigned short const vuc16; /* Read Only */ 53 | typedef volatile unsigned char const vuc8; /* Read Only */ 54 | 55 | typedef enum {FALSE = 0, TRUE = !FALSE} bool; 56 | 57 | typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus; 58 | 59 | typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState; 60 | #define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE)) 61 | 62 | typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus; 63 | 64 | #define U8_MAX ((u8)255) 65 | #define S8_MAX ((s8)127) 66 | #define S8_MIN ((s8)-128) 67 | #define U16_MAX ((u16)65535u) 68 | #define S16_MAX ((s16)32767) 69 | #define S16_MIN ((s16)-32768) 70 | #define U32_MAX ((u32)4294967295uL) 71 | #define S32_MAX ((s32)2147483647) 72 | #define S32_MIN ((s32)-2147483648) 73 | 74 | /* Exported constants --------------------------------------------------------*/ 75 | /* Exported macro ------------------------------------------------------------*/ 76 | /* Exported functions ------------------------------------------------------- */ 77 | 78 | #endif /* __STM32F10x_TYPE_H */ 79 | 80 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ 81 | -------------------------------------------------------------------------------- /updater_gd32f1/updater_gd32f1.ino: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Maple Mini bootloader transition sketch. 4 | * Based on sketch from Gregwar for Robotis OpenCM9.04, which is based on Maple bootloader code. 5 | * 6 | * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 7 | * WARNING WARNING 8 | * WARNING This comes with NO WARRANTY, you have to be perfectly sure WARNING 9 | * WARNING and aware of what you are doing. WARNING 10 | * WARNING WARNING 11 | * WARNING Running this sketch will erase your bootloader to put the WARNING 12 | * WARNING New Maple Mini one. This is still a BETA version WARNING 13 | * WARNING WARNING 14 | * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 15 | * 16 | */ 17 | 18 | #include "maple_mini_boot20.h" 19 | #define BOARD_LED_PIN PB1 20 | 21 | 22 | typedef struct { 23 | volatile uint32 CR; 24 | volatile uint32 CFGR; 25 | volatile uint32 CIR; 26 | volatile uint32 APB2RSTR; 27 | volatile uint32 APB1RSTR; 28 | volatile uint32 AHBENR; 29 | volatile uint32 APB2ENR; 30 | volatile uint32 APB1ENR; 31 | volatile uint32 BDCR; 32 | volatile uint32 CSR; 33 | } 34 | RCC_RegStruct; 35 | 36 | typedef enum { 37 | RESET = 0, SET = !RESET } 38 | FlagStatus, ITStatus; 39 | 40 | typedef enum { 41 | DISABLE = 0, ENABLE = !DISABLE} 42 | FunctionalState; 43 | 44 | typedef enum { 45 | ERROR = 0, SUCCESS = !ERROR} 46 | ErrorStatus; 47 | 48 | #define BOOTLOADER_FLASH ((uint32)0x08000000) 49 | #define PAGE_SIZE 1024 50 | 51 | #define SET_REG(addr,val) do { *(volatile uint32*)(addr)=val; } while(0) 52 | #define GET_REG(addr) (*(volatile uint32*)(addr)) 53 | 54 | 55 | #define RCC ((uint32)0x40021000) 56 | #define FLASH ((uint32)0x40022000) 57 | 58 | #define RCC_CR RCC 59 | #define RCC_CFGR (RCC + 0x04) 60 | #define RCC_CIR (RCC + 0x08) 61 | #define RCC_AHBENR (RCC + 0x14) 62 | #define RCC_APB2ENR (RCC + 0x18) 63 | #define RCC_APB1ENR (RCC + 0x1C) 64 | 65 | #define FLASH_ACR (FLASH + 0x00) 66 | #define FLASH_KEYR (FLASH + 0x04) 67 | #define FLASH_OPTKEYR (FLASH + 0x08) 68 | #define FLASH_SR (FLASH + 0x0C) 69 | #define FLASH_CR (FLASH + 0x10) 70 | #define FLASH_AR (FLASH + 0x14) 71 | #define FLASH_OBR (FLASH + 0x1C) 72 | #define FLASH_WRPR (FLASH + 0x20) 73 | 74 | #define FLASH_KEY1 0x45670123 75 | #define FLASH_KEY2 0xCDEF89AB 76 | #define FLASH_RDPRT 0x00A5 77 | #define FLASH_SR_BSY 0x01 78 | #define FLASH_CR_PER 0x02 79 | #define FLASH_CR_PG 0x01 80 | #define FLASH_CR_START 0x40 81 | 82 | #define pRCC ((RCC_RegStruct *) RCC) 83 | 84 | char * bootloader; 85 | 86 | bool flashErasePage(uint32 pageAddr) { 87 | uint32 rwmVal = GET_REG(FLASH_CR); 88 | rwmVal = FLASH_CR_PER; 89 | SET_REG(FLASH_CR, rwmVal); 90 | 91 | while (GET_REG(FLASH_SR) & FLASH_SR_BSY) { 92 | } 93 | SET_REG(FLASH_AR, pageAddr); 94 | SET_REG(FLASH_CR, FLASH_CR_START | FLASH_CR_PER); 95 | while (GET_REG(FLASH_SR) & FLASH_SR_BSY) { 96 | } 97 | 98 | /* todo: verify the page was erased */ 99 | 100 | rwmVal = 0x00; 101 | SET_REG(FLASH_CR, rwmVal); 102 | 103 | return true; 104 | } 105 | bool flashErasePages(uint32 pageAddr, uint16 n) { 106 | while (n-- > 0) { 107 | if (!flashErasePage(pageAddr + 0x400 * n)) { 108 | return false; 109 | } 110 | } 111 | 112 | return true; 113 | } 114 | 115 | bool flashWriteWord(uint32 addr, uint32 word) { 116 | volatile uint16 *flashAddr = (volatile uint16 *)addr; 117 | volatile uint32 lhWord = (volatile uint32)word & 0x0000FFFF; 118 | volatile uint32 hhWord = ((volatile uint32)word & 0xFFFF0000) >> 16; 119 | 120 | uint32 rwmVal = GET_REG(FLASH_CR); 121 | SET_REG(FLASH_CR, FLASH_CR_PG); 122 | 123 | /* apparently we need not write to FLASH_AR and can 124 | simply do a native write of a half word */ 125 | while (GET_REG(FLASH_SR) & FLASH_SR_BSY) { 126 | } 127 | *(flashAddr + 0x01) = (volatile uint16)hhWord; 128 | while (GET_REG(FLASH_SR) & FLASH_SR_BSY) { 129 | } 130 | *(flashAddr) = (volatile uint16)lhWord; 131 | while (GET_REG(FLASH_SR) & FLASH_SR_BSY) { 132 | } 133 | 134 | rwmVal &= 0xFFFFFFFE; 135 | SET_REG(FLASH_CR, rwmVal); 136 | 137 | /* verify the write */ 138 | if (*(volatile uint32 *)addr != word) { 139 | return false; 140 | } 141 | 142 | return true; 143 | } 144 | 145 | void flashLock() { 146 | /* take down the HSI oscillator? it may be in use elsewhere */ 147 | 148 | /* ensure all FPEC functions disabled and lock the FPEC */ 149 | SET_REG(FLASH_CR, 0x00000080); 150 | } 151 | 152 | void flashUnlock() { 153 | /* unlock the flash */ 154 | SET_REG(FLASH_KEYR, FLASH_KEY1); 155 | SET_REG(FLASH_KEYR, FLASH_KEY2); 156 | } 157 | 158 | /* Minimum_Source*/ 159 | 160 | void setupFLASH() { 161 | /* configure the HSI oscillator */ 162 | if ((pRCC->CR & 0x01) == 0x00) { 163 | uint32 rwmVal = pRCC->CR; 164 | rwmVal |= 0x01; 165 | pRCC->CR = rwmVal; 166 | } 167 | 168 | /* wait for it to come on */ 169 | while ((pRCC->CR & 0x02) == 0x00) { 170 | } 171 | } 172 | 173 | bool writeChunk(uint32 *ptr, int size, const char *data) 174 | { 175 | flashErasePage((uint32)(ptr)); 176 | 177 | for (int i = 0; i(maple_mini_boot20); 219 | 220 | 221 | setupFLASH(); 222 | flashUnlock(); 223 | 224 | int success = 1; 225 | int n = sizeof(bootloader); 226 | for (int i=0; i PAGE_SIZE) size = PAGE_SIZE; 232 | 233 | if (!writeChunk(chunk, size, &bootloader[i])) { 234 | Serial.println (); 235 | Serial.println ("WARNING, Update Failed!! The sketch will restart in 3 seconds and you can try to flash again"); 236 | delay (3000); 237 | success = 0; 238 | break; 239 | } 240 | } 241 | 242 | if (success){ 243 | flashLock(); 244 | Serial.println (); 245 | Serial.println ("Update completed successfully. Reboot now and replace this sketch"); 246 | while (1){ 247 | digitalWrite(BOARD_LED_PIN, LOW); 248 | delay(500); 249 | digitalWrite(BOARD_LED_PIN, HIGH); 250 | delay(500); 251 | } 252 | } 253 | } 254 | 255 | -------------------------------------------------------------------------------- /usb.h: -------------------------------------------------------------------------------- 1 | /* ***************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 LeafLabs LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ****************************************************************************/ 24 | #ifndef __USB_H 25 | #define __USB_H 26 | 27 | #include "common.h" 28 | #include "usb_lib.h" 29 | #include "usb_descriptor.h" 30 | #include "config.h" 31 | 32 | 33 | /* USB configuration params */ 34 | #define BTABLE_ADDRESS 0x00 35 | #define ENDP0_RXADDR 0x40 36 | #define ENDP0_TXADDR 0x80 /* gives 64 bytes i/o buflen */ 37 | #define ENDP1_TXADDR 0xC0 38 | #define ENDP2_TXADDR 0x100 39 | #define ENDP3_RXADDR 0x110 40 | 41 | #define bMaxPacketSize 0x40 /* 64B, maximum for usb FS devices */ 42 | //#define wTransferSize FLASH_PAGE_SIZE /* This is important, because transfers have to match with the flash page size, otherwise it erases a page before its finished copying to that page */ 43 | #define dummyTransferSize 0x800 44 | int wTransferSize; 45 | 46 | #define NUM_ENDPTS 0x01 47 | 48 | /* do we gracefully implement usb suspend? */ 49 | #define F_SUSPEND_ENABLED 1 50 | 51 | /* defines which interrupts are handled */ 52 | #define ISR_MSK (CNTR_CTRM | \ 53 | CNTR_WKUPM | \ 54 | CNTR_SUSPM | \ 55 | CNTR_ERRM | \ 56 | CNTR_SOFM | \ 57 | CNTR_ESOFM | \ 58 | CNTR_RESETM \ 59 | ) 60 | 61 | typedef enum _RESUME_STATE { 62 | RESUME_EXTERNAL, 63 | RESUME_INTERNAL, 64 | RESUME_LATER, 65 | RESUME_WAIT, 66 | RESUME_START, 67 | RESUME_ON, 68 | RESUME_OFF, 69 | RESUME_ESOF 70 | } RESUME_STATE; 71 | 72 | typedef enum _DEVICE_STATE { 73 | UNCONNECTED, 74 | ATTACHED, 75 | POWERED, 76 | SUSPENDED, 77 | ADDRESSED, 78 | CONFIGURED 79 | } DEVICE_STATE; 80 | 81 | void setupUSB(void); 82 | void usbDsbBus(void); 83 | void usbAppInit(void); /* singleton usb initializer */ 84 | 85 | void usbSuspend(void); 86 | void usbResumeInit(void); 87 | void usbResume(RESUME_STATE state); 88 | RESULT usbPowerOn(void); 89 | RESULT usbPowerOff(void); 90 | 91 | /* internal functions (as per the usb_core pProperty structure) */ 92 | void usbInit(void); 93 | void usbReset(void); 94 | void usbStatusIn(void); 95 | void usbStatusOut(void); 96 | 97 | RESULT usbDataSetup(u8 request); 98 | RESULT usbNoDataSetup(u8 request); 99 | RESULT usbGetInterfaceSetting(u8, u8); 100 | 101 | u8 *usbGetDeviceDescriptor(u16 length); 102 | u8 *usbGetConfigDescriptor(u16 length); 103 | u8 *usbGetStringDescriptor(u16 length); 104 | u8 *usbGetFunctionalDescriptor(u16 length); 105 | 106 | /* internal callbacks to respond to standard requests */ 107 | void usbGetConfiguration(void); 108 | void usbSetConfiguration(void); 109 | void usbGetInterface(void); 110 | void usbSetInterface(void); 111 | void usbGetStatus(void); 112 | void usbClearFeature(void); 113 | void usbSetEndpointFeature(void); 114 | void usbSetDeviceFeature(void); 115 | void usbSetDeviceAddress(void); 116 | 117 | /* the small number of comm emulator functions to 118 | eventually be migrated into their own usart sources 119 | */ 120 | u8 *vcomGetLineCoding(u16 length); 121 | u8 *vcomSetLineCoding(u16 length); 122 | void vcomEp1In(void); 123 | void vcomEp3Out(void); 124 | 125 | /* Interrupt setup/handling exposed only so that 126 | its obvious from main what interrupts are overloaded 127 | from c_only_startup.s (see the top of main.c) */ 128 | void usbDsbISR(void); 129 | void usbEnbISR(void); 130 | 131 | /* override the weakly defined isr in linker */ 132 | void USB_LP_CAN1_RX0_IRQHandler(void); 133 | 134 | 135 | void nothingProc(void); 136 | 137 | #endif 138 | -------------------------------------------------------------------------------- /usb_callbacks.c: -------------------------------------------------------------------------------- 1 | /* ***************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 LeafLabs LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ****************************************************************************/ 24 | 25 | /** 26 | * @file usb_callbacks.c 27 | * 28 | * @brief aka endpoints: handling data transfer when "Configured". calls out to 29 | * application specific callbacks (eg DFU) 30 | * 31 | */ 32 | -------------------------------------------------------------------------------- /usb_descriptor.c: -------------------------------------------------------------------------------- 1 | /* ***************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 LeafLabs LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ****************************************************************************/ 24 | 25 | 26 | /** 27 | * @file usb_descriptor.c 28 | * 29 | * @brief aka application descriptor; big static struct and callbacks for sending 30 | * the descriptor. 31 | * 32 | */ 33 | 34 | 35 | #include "usb_descriptor.h" 36 | 37 | u8 u8_usbDeviceDescriptorDFU[18] = { 38 | 0x12, /* bLength */ 39 | 0x01, /* bDescriptorType */ 40 | 0x00, /* bcdUSB, version 1.00 */ 41 | 0x01, 42 | 0x00, /* bDeviceClass : See interface */ 43 | 0x00, /* bDeviceSubClass : See interface*/ 44 | 0x00, /* bDeviceProtocol : See interface */ 45 | bMaxPacketSize, /* bMaxPacketSize0 0x40 = 64 */ 46 | VEND_ID0, /* idVendor (0110) */ 47 | VEND_ID1, 48 | 49 | PROD_ID0, /* idProduct (0x1001 or 1002) */ 50 | PROD_ID1, 51 | 52 | 0x01, /* bcdDevice*/ 53 | 0x02, 54 | 0x01, /* iManufacturer : index of string Manufacturer */ 55 | 0x02, /* iProduct : index of string descriptor of product*/ 56 | 0x03, /* iSerialNumber : index of string serial number*/ 57 | 0x01 /*bNumConfigurations */ 58 | }; 59 | 60 | ONE_DESCRIPTOR usbDeviceDescriptorDFU = { 61 | u8_usbDeviceDescriptorDFU, 62 | 0x12 63 | }; 64 | 65 | u8 u8_usbFunctionalDescriptor[9] = { 66 | /******************** DFU Functional Descriptor********************/ 67 | 0x09, /*blength = 7 Bytes*/ 68 | 0x21, /* DFU Functional Descriptor*/ 69 | 0x03, /*bmAttributes, bitCanDnload | bitCanUpload */ 70 | 0xFF, /*DetachTimeOut= 255 ms*/ 71 | 0x00, 72 | (dummyTransferSize & 0x00FF), 73 | (dummyTransferSize & 0xFF00) >> 8, /* TransferSize = 1024 Byte*/ 74 | 0x10, /* bcdDFUVersion = 1.1 */ 75 | 0x01 76 | }; 77 | 78 | ONE_DESCRIPTOR usbFunctionalDescriptor = { 79 | u8_usbFunctionalDescriptor, 80 | 0x09 81 | }; 82 | 83 | #define u8_usbConfigDescriptorDFU_LENGTH 45 84 | u8 u8_usbConfigDescriptorDFU[u8_usbConfigDescriptorDFU_LENGTH] = { 85 | 0x09, /* bLength: Configuation Descriptor size */ 86 | 0x02, /* bDescriptorType: Configuration */ 87 | u8_usbConfigDescriptorDFU_LENGTH, /* wTotalLength: Bytes returned */ 88 | 0x00, 89 | 0x01, /* bNumInterfaces: 1 interface */ 90 | 0x01, /* bConfigurationValue: */ 91 | 0x00, /* iConfiguration: */ 92 | 0x80, /* bmAttributes: */ 93 | 0x32, /* MaxPower 100 mA */ 94 | /* 09 */ 95 | 96 | /************ Descriptor of DFU interface 0 Alternate setting 0 *********/ 97 | 0x09, /* bLength: Interface Descriptor size */ 98 | 0x04, /* bDescriptorType: */ 99 | 0x00, /* bInterfaceNumber: Number of Interface */ 100 | 0x00, /* bAlternateSetting: Alternate setting */ 101 | 0x00, /* bNumEndpoints*/ 102 | 0xFE, /* bInterfaceClass: DFU */ 103 | 0x01, /* bInterfaceSubClass */ 104 | 105 | 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ 106 | 107 | 0x04, /* iInterface: */ 108 | 109 | /************ Descriptor of DFU interface 0 Alternate setting 1 *********/ 110 | 0x09, /* bLength: Interface Descriptor size */ 111 | 0x04, /* bDescriptorType: */ 112 | 0x00, /* bInterfaceNumber: Number of Interface */ 113 | 0x01, /* bAlternateSetting: Alternate setting */ 114 | 0x00, /* bNumEndpoints*/ 115 | 0xFE, /* bInterfaceClass: DFU */ 116 | 0x01, /* bInterfaceSubClass */ 117 | 118 | 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ 119 | 120 | 0x05, /* iInterface: */ 121 | 122 | /************ Descriptor of DFU interface 0 Alternate setting 2 *********/ 123 | 0x09, /* bLength: Interface Descriptor size */ 124 | 0x04, /* bDescriptorType: */ 125 | 0x00, /* bInterfaceNumber: Number of Interface */ 126 | 0x02, /* bAlternateSetting: Alternate setting */ 127 | 0x00, /* bNumEndpoints*/ 128 | 0xFE, /* bInterfaceClass: DFU */ 129 | 0x01, /* bInterfaceSubClass */ 130 | 131 | 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ 132 | 133 | 0x06, /* iInterface: */ 134 | 135 | 136 | /******************** DFU Functional Descriptor********************/ 137 | 0x09, /*blength = 7 Bytes*/ 138 | 0x21, /* DFU Functional Descriptor*/ 139 | 0x03, /*bmAttributes, bitCanDnload | bitCanUpload */ 140 | 0xFF, /*DetachTimeOut= 255 ms*/ 141 | 0x00, 142 | (dummyTransferSize & 0x00FF), 143 | (dummyTransferSize & 0xFF00) >> 8, /* TransferSize = 1024 Byte*/ 144 | 0x10, /* bcdDFUVersion = 1.1 */ 145 | 0x01 146 | /***********************************************************/ 147 | /*36*/ 148 | }; 149 | 150 | ONE_DESCRIPTOR usbConfigDescriptorDFU = { 151 | u8_usbConfigDescriptorDFU, 152 | u8_usbConfigDescriptorDFU_LENGTH 153 | }; 154 | 155 | #define USB_STR_LANG_ID_LEN 0x04 156 | u8 u8_usbStringLangId[USB_STR_LANG_ID_LEN] = { 157 | USB_STR_LANG_ID_LEN, 158 | 0x03, 159 | 0x09, 160 | 0x04 /* LangID = 0x0409: U.S. English */ 161 | }; 162 | #define USB_VENDOR_STR_LEN 0x12 163 | u8 u8_usbStringVendor[USB_VENDOR_STR_LEN] = { 164 | USB_VENDOR_STR_LEN, 165 | 0x03, 166 | 'L', 0, 'e', 0, 'a', 0, 'f', 0, 'L', 0, 'a', 0, 'b', 0, 's', 0 167 | }; 168 | #define USB_PRODUCT_STR_LEN 0x14 169 | u8 u8_usbStringProduct[USB_PRODUCT_STR_LEN] = { 170 | USB_PRODUCT_STR_LEN, 171 | 0x03, 172 | 'M', 0, 'a', 0, 'p', 0, 'l', 0, 'e', 0, ' ', 0, '0', 0, '0', 0, '3', 0 173 | }; 174 | #define USB_SERIAL_STR_LEN 0x10 175 | u8 u8_usbStringSerial[USB_SERIAL_STR_LEN] = { 176 | USB_SERIAL_STR_LEN, 177 | 0x03, 178 | 'L', 0, 'L', 0, 'M', 0, ' ', 0, '0', 0, '0', 0, '3', 0 179 | }; 180 | 181 | u8 u8_usbStringAlt0[ALT0_STR_LEN] = { 182 | ALT0_STR_LEN, 183 | 0x03, 184 | ALT0_MSG_STR 185 | }; 186 | 187 | 188 | u8 u8_usbStringAlt1[ALT1_STR_LEN] = { 189 | ALT1_STR_LEN, 190 | 0x03, 191 | ALT1_MSG_STR 192 | }; 193 | 194 | 195 | u8 u8_usbStringAlt2[ALT2_STR_LEN] = { 196 | ALT2_STR_LEN, 197 | 0x03, 198 | ALT2_MSG_STR 199 | }; 200 | 201 | u8 u8_usbStringInterface = NULL; 202 | 203 | ONE_DESCRIPTOR usbStringDescriptor[STR_DESC_LEN] = { 204 | { (u8 *)u8_usbStringLangId, USB_STR_LANG_ID_LEN }, 205 | { (u8 *)u8_usbStringVendor, USB_VENDOR_STR_LEN }, 206 | { (u8 *)u8_usbStringProduct, USB_PRODUCT_STR_LEN }, 207 | { (u8 *)u8_usbStringSerial, USB_SERIAL_STR_LEN }, 208 | { (u8 *)u8_usbStringAlt0, ALT0_STR_LEN }, 209 | { (u8 *)u8_usbStringAlt1, ALT1_STR_LEN }, 210 | { (u8 *)u8_usbStringAlt2, ALT2_STR_LEN } 211 | }; 212 | 213 | -------------------------------------------------------------------------------- /usb_descriptor.h: -------------------------------------------------------------------------------- 1 | /* ***************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 LeafLabs LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * ****************************************************************************/ 24 | 25 | #ifndef __MAPLE_USB_DESC_H 26 | #define __MAPLE_USB_DESC_H 27 | 28 | #include "common.h" 29 | #include "usb_lib.h" 30 | #include "usb.h" 31 | 32 | #define NUM_ALT_SETTINGS 3 33 | #define STR_DESC_LEN 7 34 | 35 | extern ONE_DESCRIPTOR usbDeviceDescriptorDFU; 36 | extern ONE_DESCRIPTOR usbConfigDescriptorDFU; 37 | extern ONE_DESCRIPTOR usbStringDescriptor[STR_DESC_LEN]; 38 | extern ONE_DESCRIPTOR usbFunctionalDescriptor; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /usb_descriptor_strings_util.html: -------------------------------------------------------------------------------- 1 | 2 | 22 | 23 | -------------------------------------------------------------------------------- /usb_lib/usb_conf.h: -------------------------------------------------------------------------------- 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 2 | * File Name : usb_conf.h 3 | * Author : MCD Application Team 4 | * Version : V2.2.1 5 | * Date : 09/22/2008 6 | * Description : Device Firmware Upgrade (DFU) configuration file 7 | ******************************************************************************** 8 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 9 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 10 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 11 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 12 | * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 13 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 14 | *******************************************************************************/ 15 | 16 | /* Define to prevent recursive inclusion -------------------------------------*/ 17 | #ifndef __USB_CONF_H 18 | #define __USB_CONF_H 19 | 20 | /* Includes ------------------------------------------------------------------*/ 21 | /* Exported types ------------------------------------------------------------*/ 22 | /* Exported constants --------------------------------------------------------*/ 23 | /* Exported macro ------------------------------------------------------------*/ 24 | /* Exported functions ------------------------------------------------------- */ 25 | /* External variables --------------------------------------------------------*/ 26 | /*-------------------------------------------------------------*/ 27 | /* EP_NUM */ 28 | /* defines how many endpoints are used by the device */ 29 | /*-------------------------------------------------------------*/ 30 | #define EP_NUM (1) 31 | 32 | /*-------------------------------------------------------------*/ 33 | /* -------------- Buffer Description Table -----------------*/ 34 | /*-------------------------------------------------------------*/ 35 | /* buffer table base address */ 36 | /* buffer table base address */ 37 | #define BTABLE_ADDRESS (0x00) 38 | 39 | /* EP0 */ 40 | /* rx/tx buffer base address */ 41 | #define ENDP0_RXADDR (0x10) 42 | #define ENDP0_TXADDR (0x50) 43 | 44 | 45 | /*-------------------------------------------------------------*/ 46 | /* ------------------- ISTR events -------------------------*/ 47 | /*-------------------------------------------------------------*/ 48 | /* IMR_MSK */ 49 | /* mask defining which events has to be handled */ 50 | /* by the device application software */ 51 | #define IMR_MSK (CNTR_CTRM | \ 52 | CNTR_WKUPM | \ 53 | CNTR_SUSPM | \ 54 | CNTR_ERRM | \ 55 | CNTR_SOFM | \ 56 | CNTR_ESOFM | \ 57 | CNTR_RESETM \ 58 | ) 59 | 60 | /* CTR service routines */ 61 | /* associated to defined endpoints */ 62 | #define EP1_IN_Callback NOP_Process 63 | #define EP2_IN_Callback NOP_Process 64 | #define EP3_IN_Callback NOP_Process 65 | #define EP4_IN_Callback NOP_Process 66 | #define EP5_IN_Callback NOP_Process 67 | #define EP6_IN_Callback NOP_Process 68 | #define EP7_IN_Callback NOP_Process 69 | 70 | 71 | #define EP1_OUT_Callback NOP_Process 72 | #define EP2_OUT_Callback NOP_Process 73 | #define EP3_OUT_Callback NOP_Process 74 | #define EP4_OUT_Callback NOP_Process 75 | #define EP5_OUT_Callback NOP_Process 76 | #define EP6_OUT_Callback NOP_Process 77 | #define EP7_OUT_Callback NOP_Process 78 | 79 | 80 | #endif /*__USB_CONF_H*/ 81 | 82 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /usb_lib/usb_core.h: -------------------------------------------------------------------------------- 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 2 | * File Name : usb_core.h 3 | * Author : MCD Application Team 4 | * Version : V2.2.1 5 | * Date : 09/22/2008 6 | * Description : Standard protocol processing functions prototypes 7 | ******************************************************************************** 8 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 9 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 10 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 11 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 12 | * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 13 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 14 | *******************************************************************************/ 15 | 16 | /* Define to prevent recursive inclusion -------------------------------------*/ 17 | #ifndef __USB_CORE_H 18 | #define __USB_CORE_H 19 | 20 | /* Includes ------------------------------------------------------------------*/ 21 | /* Exported types ------------------------------------------------------------*/ 22 | typedef enum _CONTROL_STATE 23 | { 24 | WAIT_SETUP, /* 0 */ 25 | SETTING_UP, /* 1 */ 26 | IN_DATA, /* 2 */ 27 | OUT_DATA, /* 3 */ 28 | LAST_IN_DATA, /* 4 */ 29 | LAST_OUT_DATA, /* 5 */ 30 | WAIT_STATUS_IN, /* 7 */ 31 | WAIT_STATUS_OUT, /* 8 */ 32 | STALLED, /* 9 */ 33 | PAUSE /* 10 */ 34 | } CONTROL_STATE; /* The state machine states of a control pipe */ 35 | 36 | typedef struct OneDescriptor 37 | { 38 | u8 *Descriptor; 39 | u16 Descriptor_Size; 40 | } 41 | ONE_DESCRIPTOR, *PONE_DESCRIPTOR; 42 | /* All the request process routines return a value of this type 43 | If the return value is not SUCCESS or NOT_READY, 44 | the software will STALL the correspond endpoint */ 45 | typedef enum _RESULT 46 | { 47 | USB_SUCCESS = 0, /* Process sucessfully */ 48 | USB_ERROR, 49 | USB_UNSUPPORT, 50 | USB_NOT_READY /* The process has not been finished, endpoint will be 51 | NAK to further rquest */ 52 | } RESULT; 53 | 54 | 55 | /*-*-*-*-*-*-*-*-*-*-* Definitions for endpoint level -*-*-*-*-*-*-*-*-*-*-*-*/ 56 | typedef struct _ENDPOINT_INFO 57 | { 58 | /* When send data out of the device, 59 | CopyData() is used to get data buffer 'Length' bytes data 60 | if Length is 0, 61 | CopyData() returns the total length of the data 62 | if the request is not supported, returns 0 63 | (NEW Feature ) 64 | if CopyData() returns -1, the calling routine should not proceed 65 | further and will resume the SETUP process by the class device 66 | if Length is not 0, 67 | CopyData() returns a pointer to indicate the data location 68 | Usb_wLength is the data remain to be sent, 69 | Usb_wOffset is the Offset of original data 70 | When receive data from the host, 71 | CopyData() is used to get user data buffer which is capable 72 | of Length bytes data to copy data from the endpoint buffer. 73 | if Length is 0, 74 | CopyData() returns the available data length, 75 | if Length is not 0, 76 | CopyData() returns user buffer address 77 | Usb_rLength is the data remain to be received, 78 | Usb_rPointer is the Offset of data buffer 79 | */ 80 | u16 Usb_wLength; 81 | u16 Usb_wOffset; 82 | u16 PacketSize; 83 | u8 *(*CopyData)(u16 Length); 84 | }ENDPOINT_INFO; 85 | 86 | /*-*-*-*-*-*-*-*-*-*-*-* Definitions for device level -*-*-*-*-*-*-*-*-*-*-*-*/ 87 | 88 | typedef struct _DEVICE 89 | { 90 | u8 Total_Endpoint; /* Number of endpoints that are used */ 91 | u8 Total_Configuration;/* Number of configuration available */ 92 | } 93 | DEVICE; 94 | 95 | typedef union 96 | { 97 | u16 w; 98 | struct BW 99 | { 100 | /* Little Endian */ 101 | u8 bb0; 102 | u8 bb1; 103 | } 104 | bw; 105 | } u16_u8; 106 | 107 | typedef struct _DEVICE_INFO 108 | { 109 | u8 USBbmRequestType; /* bmRequestType */ 110 | u8 USBbRequest; /* bRequest */ 111 | u16_u8 USBwValues; /* wValue */ 112 | u16_u8 USBwIndexs; /* wIndex */ 113 | u16_u8 USBwLengths; /* wLength */ 114 | 115 | u8 ControlState; /* of type CONTROL_STATE */ 116 | u8 Current_Feature; 117 | u8 Current_Configuration; /* Selected configuration */ 118 | u8 Current_Interface; /* Selected interface of current configuration */ 119 | u8 Current_AlternateSetting;/* Selected Alternate Setting of current 120 | interface*/ 121 | 122 | ENDPOINT_INFO Ctrl_Info; 123 | }DEVICE_INFO; 124 | 125 | typedef struct _DEVICE_PROP 126 | { 127 | void (*Init)(void); /* Initialize the device */ 128 | void (*Reset)(void); /* Reset routine of this device */ 129 | 130 | /* Device dependent process after the status stage */ 131 | void (*Process_Status_IN)(void); 132 | void (*Process_Status_OUT)(void); 133 | 134 | /* Procedure of process on setup stage of a class specified request with data stage */ 135 | /* All class specified requests with data stage are processed in Class_Data_Setup 136 | Class_Data_Setup() 137 | responses to check all special requests and fills ENDPOINT_INFO 138 | according to the request 139 | If IN tokens are expected, then wLength & wOffset will be filled 140 | with the total transferring bytes and the starting position 141 | If OUT tokens are expected, then rLength & rOffset will be filled 142 | with the total expected bytes and the starting position in the buffer 143 | 144 | If the request is valid, Class_Data_Setup returns SUCCESS, else UNSUPPORT 145 | 146 | CAUTION: 147 | Since GET_CONFIGURATION & GET_INTERFACE are highly related to 148 | the individual classes, they will be checked and processed here. 149 | */ 150 | RESULT (*Class_Data_Setup)(u8 RequestNo); 151 | 152 | /* Procedure of process on setup stage of a class specified request without data stage */ 153 | /* All class specified requests without data stage are processed in Class_NoData_Setup 154 | Class_NoData_Setup 155 | responses to check all special requests and perform the request 156 | 157 | CAUTION: 158 | Since SET_CONFIGURATION & SET_INTERFACE are highly related to 159 | the individual classes, they will be checked and processed here. 160 | */ 161 | RESULT (*Class_NoData_Setup)(u8 RequestNo); 162 | 163 | /*Class_Get_Interface_Setting 164 | This function is used by the file usb_core.c to test if the selected Interface 165 | and Alternate Setting (u8 Interface, u8 AlternateSetting) are supported by 166 | the application. 167 | This function is writing by user. It should return "SUCCESS" if the Interface 168 | and Alternate Setting are supported by the application or "UNSUPPORT" if they 169 | are not supported. */ 170 | 171 | RESULT (*Class_Get_Interface_Setting)(u8 Interface, u8 AlternateSetting); 172 | 173 | u8* (*GetDeviceDescriptor)(u16 Length); 174 | u8* (*GetConfigDescriptor)(u16 Length); 175 | u8* (*GetStringDescriptor)(u16 Length); 176 | u8* (*GetFunctionalDescriptor)(u16 Length); 177 | 178 | u8* RxEP_buffer; 179 | u8 MaxPacketSize; 180 | 181 | }DEVICE_PROP; 182 | 183 | typedef struct _USER_STANDARD_REQUESTS 184 | { 185 | void (*User_GetConfiguration)(void); /* Get Configuration */ 186 | void (*User_SetConfiguration)(void); /* Set Configuration */ 187 | void (*User_GetInterface)(void); /* Get Interface */ 188 | void (*User_SetInterface)(void); /* Set Interface */ 189 | void (*User_GetStatus)(void); /* Get Status */ 190 | void (*User_ClearFeature)(void); /* Clear Feature */ 191 | void (*User_SetEndPointFeature)(void); /* Set Endpoint Feature */ 192 | void (*User_SetDeviceFeature)(void); /* Set Device Feature */ 193 | void (*User_SetDeviceAddress)(void); /* Set Device Address */ 194 | } 195 | USER_STANDARD_REQUESTS; 196 | 197 | /* Exported constants --------------------------------------------------------*/ 198 | #define Type_Recipient (pInformation->USBbmRequestType & (REQUEST_TYPE | RECIPIENT)) 199 | 200 | #define Usb_rLength Usb_wLength 201 | #define Usb_rOffset Usb_wOffset 202 | 203 | #define USBwValue USBwValues.w 204 | #define USBwValue0 USBwValues.bw.bb0 205 | #define USBwValue1 USBwValues.bw.bb1 206 | #define USBwIndex USBwIndexs.w 207 | #define USBwIndex0 USBwIndexs.bw.bb0 208 | #define USBwIndex1 USBwIndexs.bw.bb1 209 | #define USBwLength USBwLengths.w 210 | #define USBwLength0 USBwLengths.bw.bb0 211 | #define USBwLength1 USBwLengths.bw.bb1 212 | #define StatusInfo0 StatusInfo.bw.bb0 213 | #define StatusInfo1 StatusInfo.bw.bb1 214 | 215 | /* Exported macro ------------------------------------------------------------*/ 216 | /* Exported functions ------------------------------------------------------- */ 217 | u8 Setup0_Process(void); 218 | u8 Post0_Process(void); 219 | u8 Out0_Process(void); 220 | u8 In0_Process(void); 221 | 222 | RESULT Standard_SetEndPointFeature(void); 223 | RESULT Standard_SetDeviceFeature(void); 224 | 225 | u8 *Standard_GetConfiguration(u16 Length); 226 | RESULT Standard_SetConfiguration(void); 227 | u8 *Standard_GetInterface(u16 Length); 228 | RESULT Standard_SetInterface(void); 229 | u8 *Standard_GetDescriptorData(u16 Length, PONE_DESCRIPTOR pDesc); 230 | 231 | u8 *Standard_GetStatus(u16 Length); 232 | RESULT Standard_ClearFeature(void); 233 | void SetDeviceAddress(u8); 234 | void NOP_Process(void); 235 | 236 | extern DEVICE_PROP Device_Property; 237 | extern USER_STANDARD_REQUESTS User_Standard_Requests; 238 | extern DEVICE Device_Table; 239 | extern DEVICE_INFO Device_Info; 240 | 241 | /* cells saving status during interrupt servicing */ 242 | extern u16 SaveRState; 243 | extern u16 SaveTState; 244 | 245 | #endif /* __USB_CORE_H */ 246 | 247 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ 248 | -------------------------------------------------------------------------------- /usb_lib/usb_def.h: -------------------------------------------------------------------------------- 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 2 | * File Name : usb_def.h 3 | * Author : MCD Application Team 4 | * Version : V2.2.1 5 | * Date : 09/22/2008 6 | * Description : Definitions related to USB Core 7 | ******************************************************************************** 8 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 9 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 10 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 11 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 12 | * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 13 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 14 | *******************************************************************************/ 15 | 16 | /* Define to prevent recursive inclusion -------------------------------------*/ 17 | #ifndef __USB_DEF_H 18 | #define __USB_DEF_H 19 | 20 | /* Includes ------------------------------------------------------------------*/ 21 | /* Exported types ------------------------------------------------------------*/ 22 | typedef enum _RECIPIENT_TYPE 23 | { 24 | DEVICE_RECIPIENT, /* Recipient device */ 25 | INTERFACE_RECIPIENT, /* Recipient interface */ 26 | ENDPOINT_RECIPIENT, /* Recipient endpoint */ 27 | OTHER_RECIPIENT 28 | } RECIPIENT_TYPE; 29 | 30 | 31 | typedef enum _STANDARD_REQUESTS 32 | { 33 | GET_STATUS = 0, 34 | CLEAR_FEATURE, 35 | RESERVED1, 36 | SET_FEATURE, 37 | RESERVED2, 38 | SET_ADDRESS, 39 | GET_DESCRIPTOR, 40 | SET_DESCRIPTOR, 41 | GET_CONFIGURATION, 42 | SET_CONFIGURATION, 43 | GET_INTERFACE, 44 | SET_INTERFACE, 45 | TOTAL_sREQUEST, /* Total number of Standard request */ 46 | SYNCH_FRAME = 12 47 | } STANDARD_REQUESTS; 48 | 49 | /* Definition of "USBwValue" */ 50 | typedef enum _DESCRIPTOR_TYPE 51 | { 52 | DEVICE_DESCRIPTOR = 1, 53 | CONFIG_DESCRIPTOR, 54 | STRING_DESCRIPTOR, 55 | INTERFACE_DESCRIPTOR, 56 | ENDPOINT_DESCRIPTOR 57 | } DESCRIPTOR_TYPE; 58 | 59 | /* Feature selector of a SET_FEATURE or CLEAR_FEATURE */ 60 | typedef enum _FEATURE_SELECTOR 61 | { 62 | ENDPOINT_STALL, 63 | DEVICE_REMOTE_WAKEUP 64 | } FEATURE_SELECTOR; 65 | 66 | /* Exported constants --------------------------------------------------------*/ 67 | /* Definition of "USBbmRequestType" */ 68 | #define REQUEST_TYPE 0x60 /* Mask to get request type */ 69 | #define STANDARD_REQUEST 0x00 /* Standard request */ 70 | #define CLASS_REQUEST 0x20 /* Class request */ 71 | #define VENDOR_REQUEST 0x40 /* Vendor request */ 72 | 73 | #define RECIPIENT 0x1F /* Mask to get recipient */ 74 | 75 | /* Exported macro ------------------------------------------------------------*/ 76 | /* Exported functions ------------------------------------------------------- */ 77 | 78 | #endif /* __USB_DEF_H */ 79 | 80 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ 81 | -------------------------------------------------------------------------------- /usb_lib/usb_init.c: -------------------------------------------------------------------------------- 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 2 | * File Name : usb_init.c 3 | * Author : MCD Application Team 4 | * Version : V2.2.1 5 | * Date : 09/22/2008 6 | * Description : Initialization routines & global variables 7 | ******************************************************************************** 8 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 9 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 10 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 11 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 12 | * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 13 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 14 | *******************************************************************************/ 15 | 16 | /* Includes ------------------------------------------------------------------*/ 17 | #include "usb_lib.h" 18 | 19 | /* Private typedef -----------------------------------------------------------*/ 20 | /* Private define ------------------------------------------------------------*/ 21 | /* Private macro -------------------------------------------------------------*/ 22 | /* Private variables ---------------------------------------------------------*/ 23 | /* The number of current endpoint, it will be used to specify an endpoint */ 24 | u8 EPindex; 25 | /* The number of current device, it is an index to the Device_Table */ 26 | /* u8 Device_no; */ 27 | /* Points to the DEVICE_INFO structure of current device */ 28 | /* The purpose of this register is to speed up the execution */ 29 | DEVICE_INFO *pInformation; 30 | /* Points to the DEVICE_PROP structure of current device */ 31 | /* The purpose of this register is to speed up the execution */ 32 | DEVICE_PROP *pProperty; 33 | /* Temporary save the state of Rx & Tx status. */ 34 | /* Whenever the Rx or Tx state is changed, its value is saved */ 35 | /* in this variable first and will be set to the EPRB or EPRA */ 36 | /* at the end of interrupt process */ 37 | u16 SaveState ; 38 | u16 wInterrupt_Mask; 39 | DEVICE_INFO Device_Info; 40 | USER_STANDARD_REQUESTS *pUser_Standard_Requests; 41 | 42 | /* Extern variables ----------------------------------------------------------*/ 43 | /* Private function prototypes -----------------------------------------------*/ 44 | /* Private functions ---------------------------------------------------------*/ 45 | 46 | /******************************************************************************* 47 | * Function Name : USB_Init 48 | * Description : USB system initialization 49 | * Input : None. 50 | * Output : None. 51 | * Return : None. 52 | *******************************************************************************/ 53 | void USB_Init(void) 54 | { 55 | pInformation = &Device_Info; 56 | pInformation->ControlState = 2; 57 | pProperty = &Device_Property; 58 | pUser_Standard_Requests = &User_Standard_Requests; 59 | /* Initialize devices one by one */ 60 | 61 | pProperty->Init(); 62 | } 63 | 64 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ 65 | -------------------------------------------------------------------------------- /usb_lib/usb_init.h: -------------------------------------------------------------------------------- 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 2 | * File Name : usb_init.h 3 | * Author : MCD Application Team 4 | * Version : V2.2.1 5 | * Date : 09/22/2008 6 | * Description : Initialization routines & global variables 7 | ******************************************************************************** 8 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 9 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 10 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 11 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 12 | * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 13 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 14 | *******************************************************************************/ 15 | 16 | /* Define to prevent recursive inclusion -------------------------------------*/ 17 | #ifndef __USB_INIT_H 18 | #define __USB_INIT_H 19 | 20 | /* Includes ------------------------------------------------------------------*/ 21 | /* Exported types ------------------------------------------------------------*/ 22 | /* Exported constants --------------------------------------------------------*/ 23 | /* Exported macro ------------------------------------------------------------*/ 24 | /* Exported functions ------------------------------------------------------- */ 25 | void USB_Init(void); 26 | 27 | /* External variables --------------------------------------------------------*/ 28 | /* The number of current endpoint, it will be used to specify an endpoint */ 29 | extern u8 EPindex; 30 | /* The number of current device, it is an index to the Device_Table */ 31 | /*extern u8 Device_no; */ 32 | /* Points to the DEVICE_INFO structure of current device */ 33 | /* The purpose of this register is to speed up the execution */ 34 | extern DEVICE_INFO* pInformation; 35 | /* Points to the DEVICE_PROP structure of current device */ 36 | /* The purpose of this register is to speed up the execution */ 37 | extern DEVICE_PROP* pProperty; 38 | /* Temporary save the state of Rx & Tx status. */ 39 | /* Whenever the Rx or Tx state is changed, its value is saved */ 40 | /* in this variable first and will be set to the EPRB or EPRA */ 41 | /* at the end of interrupt process */ 42 | extern USER_STANDARD_REQUESTS *pUser_Standard_Requests; 43 | 44 | extern u16 SaveState ; 45 | extern u16 wInterrupt_Mask; 46 | 47 | #endif /* __USB_INIT_H */ 48 | 49 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ 50 | -------------------------------------------------------------------------------- /usb_lib/usb_int.c: -------------------------------------------------------------------------------- 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 2 | * File Name : usb_int.c 3 | * Author : MCD Application Team 4 | * Version : V2.2.1 5 | * Date : 09/22/2008 6 | * Description : Endpoint CTR (Low and High) interrupt's service routines 7 | ******************************************************************************** 8 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 9 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 10 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 11 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 12 | * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 13 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 14 | *******************************************************************************/ 15 | 16 | /* Includes ------------------------------------------------------------------*/ 17 | #include "usb_lib.h" 18 | 19 | /* Private typedef -----------------------------------------------------------*/ 20 | /* Private define ------------------------------------------------------------*/ 21 | /* Private macro -------------------------------------------------------------*/ 22 | /* Private variables ---------------------------------------------------------*/ 23 | u16 SaveRState; 24 | u16 SaveTState; 25 | 26 | /* Extern variables ----------------------------------------------------------*/ 27 | extern void (*pEpInt_IN[7])(void); /* Handles IN interrupts */ 28 | extern void (*pEpInt_OUT[7])(void); /* Handles OUT interrupts */ 29 | 30 | /* Private function prototypes -----------------------------------------------*/ 31 | /* Private functions ---------------------------------------------------------*/ 32 | 33 | /******************************************************************************* 34 | * Function Name : CTR_LP. 35 | * Description : Low priority Endpoint Correct Transfer interrupt's service 36 | * routine. 37 | * Input : None. 38 | * Output : None. 39 | * Return : None. 40 | *******************************************************************************/ 41 | void CTR_LP(void) 42 | { 43 | u32 wEPVal = 0; 44 | /* stay in loop while pending ints */ 45 | while (((wIstr = _GetISTR()) & ISTR_CTR) != 0) 46 | { 47 | _SetISTR((u16)CLR_CTR); /* clear CTR flag */ 48 | /* extract highest priority endpoint number */ 49 | EPindex = (u8)(wIstr & ISTR_EP_ID); 50 | if (EPindex == 0) 51 | { 52 | /* Decode and service control endpoint interrupt */ 53 | /* calling related service routine */ 54 | /* (Setup0_Process, In0_Process, Out0_Process) */ 55 | 56 | /* save RX & TX status */ 57 | /* and set both to NAK */ 58 | SaveRState = _GetEPRxStatus(ENDP0); 59 | SaveTState = _GetEPTxStatus(ENDP0); 60 | _SetEPRxStatus(ENDP0, EP_RX_NAK); 61 | _SetEPTxStatus(ENDP0, EP_TX_NAK); 62 | 63 | 64 | /* DIR bit = origin of the interrupt */ 65 | 66 | if ((wIstr & ISTR_DIR) == 0) 67 | { 68 | /* DIR = 0 */ 69 | 70 | /* DIR = 0 => IN int */ 71 | /* DIR = 0 implies that (EP_CTR_TX = 1) always */ 72 | 73 | 74 | _ClearEP_CTR_TX(ENDP0); 75 | In0_Process(); 76 | 77 | /* before terminate set Tx & Rx status */ 78 | _SetEPRxStatus(ENDP0, SaveRState); 79 | _SetEPTxStatus(ENDP0, SaveTState); 80 | return; 81 | } 82 | else 83 | { 84 | /* DIR = 1 */ 85 | 86 | /* DIR = 1 & CTR_RX => SETUP or OUT int */ 87 | /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */ 88 | 89 | wEPVal = _GetENDPOINT(ENDP0); 90 | if ((wEPVal & EP_CTR_TX) != 0) 91 | { 92 | _ClearEP_CTR_TX(ENDP0); 93 | In0_Process(); 94 | /* before terminate set Tx & Rx status */ 95 | _SetEPRxStatus(ENDP0, SaveRState); 96 | _SetEPTxStatus(ENDP0, SaveTState); 97 | return; 98 | } 99 | else if ((wEPVal &EP_SETUP) != 0) 100 | { 101 | _ClearEP_CTR_RX(ENDP0); /* SETUP bit kept frozen while CTR_RX = 1 */ 102 | Setup0_Process(); 103 | /* before terminate set Tx & Rx status */ 104 | _SetEPRxStatus(ENDP0, SaveRState); 105 | _SetEPTxStatus(ENDP0, SaveTState); 106 | return; 107 | } 108 | 109 | else if ((wEPVal & EP_CTR_RX) != 0) 110 | { 111 | _ClearEP_CTR_RX(ENDP0); 112 | Out0_Process(); 113 | /* before terminate set Tx & Rx status */ 114 | _SetEPRxStatus(ENDP0, SaveRState); 115 | _SetEPTxStatus(ENDP0, SaveTState); 116 | return; 117 | } 118 | } 119 | }/* if(EPindex == 0) */ 120 | else 121 | { 122 | /* Decode and service non control endpoints interrupt */ 123 | 124 | /* process related endpoint register */ 125 | wEPVal = _GetENDPOINT(EPindex); 126 | if ((wEPVal & EP_CTR_RX) != 0) 127 | { 128 | /* clear int flag */ 129 | _ClearEP_CTR_RX(EPindex); 130 | 131 | /* call OUT service function */ 132 | (*pEpInt_OUT[EPindex-1])(); 133 | 134 | } /* if((wEPVal & EP_CTR_RX) */ 135 | 136 | if ((wEPVal & EP_CTR_TX) != 0) 137 | { 138 | /* clear int flag */ 139 | _ClearEP_CTR_TX(EPindex); 140 | 141 | /* call IN service function */ 142 | (*pEpInt_IN[EPindex-1])(); 143 | } /* if((wEPVal & EP_CTR_TX) != 0) */ 144 | 145 | }/* if(EPindex == 0) else */ 146 | 147 | }/* while(...) */ 148 | } 149 | 150 | /******************************************************************************* 151 | * Function Name : CTR_HP. 152 | * Description : High Priority Endpoint Correct Transfer interrupt's service 153 | * routine. 154 | * Input : None. 155 | * Output : None. 156 | * Return : None. 157 | *******************************************************************************/ 158 | void CTR_HP(void) 159 | { 160 | u32 wEPVal = 0; 161 | 162 | while (((wIstr = _GetISTR()) & ISTR_CTR) != 0) 163 | { 164 | _SetISTR((u16)CLR_CTR); /* clear CTR flag */ 165 | /* extract highest priority endpoint number */ 166 | EPindex = (u8)(wIstr & ISTR_EP_ID); 167 | /* process related endpoint register */ 168 | wEPVal = _GetENDPOINT(EPindex); 169 | if ((wEPVal & EP_CTR_RX) != 0) 170 | { 171 | /* clear int flag */ 172 | _ClearEP_CTR_RX(EPindex); 173 | 174 | /* call OUT service function */ 175 | (*pEpInt_OUT[EPindex-1])(); 176 | 177 | } /* if((wEPVal & EP_CTR_RX) */ 178 | else if ((wEPVal & EP_CTR_TX) != 0) 179 | { 180 | /* clear int flag */ 181 | _ClearEP_CTR_TX(EPindex); 182 | 183 | /* call IN service function */ 184 | (*pEpInt_IN[EPindex-1])(); 185 | 186 | 187 | } /* if((wEPVal & EP_CTR_TX) != 0) */ 188 | 189 | }/* while(...) */ 190 | } 191 | 192 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ 193 | -------------------------------------------------------------------------------- /usb_lib/usb_int.h: -------------------------------------------------------------------------------- 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 2 | * File Name : usb_int.h 3 | * Author : MCD Application Team 4 | * Version : V2.2.1 5 | * Date : 09/22/2008 6 | * Description : Endpoint CTR (Low and High) interrupt's service routines 7 | * prototypes 8 | ******************************************************************************** 9 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 10 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 11 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 12 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 13 | * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 14 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 15 | *******************************************************************************/ 16 | 17 | /* Define to prevent recursive inclusion -------------------------------------*/ 18 | #ifndef __USB_INT_H 19 | #define __USB_INT_H 20 | 21 | /* Includes ------------------------------------------------------------------*/ 22 | /* Exported types ------------------------------------------------------------*/ 23 | /* Exported constants --------------------------------------------------------*/ 24 | /* Exported macro ------------------------------------------------------------*/ 25 | /* Exported functions ------------------------------------------------------- */ 26 | void CTR_LP(void); 27 | void CTR_HP(void); 28 | 29 | /* External variables --------------------------------------------------------*/ 30 | 31 | #endif /* __USB_INT_H */ 32 | 33 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ 34 | -------------------------------------------------------------------------------- /usb_lib/usb_lib.h: -------------------------------------------------------------------------------- 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 2 | * File Name : usb_lib.h 3 | * Author : MCD Application Team 4 | * Version : V2.2.1 5 | * Date : 09/22/2008 6 | * Description : USB library include files 7 | ******************************************************************************** 8 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 9 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 10 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 11 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 12 | * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 13 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 14 | *******************************************************************************/ 15 | 16 | /* Define to prevent recursive inclusion -------------------------------------*/ 17 | #ifndef __USB_LIB_H 18 | #define __USB_LIB_H 19 | 20 | /* Includes ------------------------------------------------------------------*/ 21 | #include "usb_type.h" 22 | #include "usb_regs.h" 23 | #include "usb_def.h" 24 | #include "usb_core.h" 25 | #include "usb_init.h" 26 | #include "usb_mem.h" 27 | #include "usb_int.h" 28 | 29 | /* Exported types ------------------------------------------------------------*/ 30 | /* Exported constants --------------------------------------------------------*/ 31 | /* Exported macro ------------------------------------------------------------*/ 32 | /* Exported functions ------------------------------------------------------- */ 33 | /* External variables --------------------------------------------------------*/ 34 | 35 | #endif /* __USB_LIB_H */ 36 | 37 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ 38 | -------------------------------------------------------------------------------- /usb_lib/usb_mem.c: -------------------------------------------------------------------------------- 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 2 | * File Name : usb_mem.c 3 | * Author : MCD Application Team 4 | * Version : V2.2.1 5 | * Date : 09/22/2008 6 | * Description : Utility functions for memory transfers to/from PMA 7 | ******************************************************************************** 8 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 9 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 10 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 11 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 12 | * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 13 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 14 | *******************************************************************************/ 15 | 16 | /* Includes ------------------------------------------------------------------*/ 17 | #include "usb_lib.h" 18 | 19 | /* Private typedef -----------------------------------------------------------*/ 20 | /* Private define ------------------------------------------------------------*/ 21 | /* Private macro -------------------------------------------------------------*/ 22 | /* Private variables ---------------------------------------------------------*/ 23 | /* Extern variables ----------------------------------------------------------*/ 24 | /* Private function prototypes -----------------------------------------------*/ 25 | /* Private functions ---------------------------------------------------------*/ 26 | /******************************************************************************* 27 | * Function Name : UserToPMABufferCopy 28 | * Description : Copy a buffer from user memory area to packet memory area (PMA) 29 | * Input : - pbUsrBuf: pointer to user memory area. 30 | * - wPMABufAddr: address into PMA. 31 | * - wNBytes: no. of bytes to be copied. 32 | * Output : None. 33 | * Return : None . 34 | *******************************************************************************/ 35 | void UserToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes) 36 | { 37 | u32 n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */ 38 | u32 i, temp1, temp2; 39 | u16 *pdwVal; 40 | pdwVal = (u16 *)(wPMABufAddr * 2 + PMAAddr); 41 | for (i = n; i != 0; i--) 42 | { 43 | temp1 = (u16) * pbUsrBuf; 44 | pbUsrBuf++; 45 | temp2 = temp1 | (u16) * pbUsrBuf << 8; 46 | *pdwVal++ = temp2; 47 | pdwVal++; 48 | pbUsrBuf++; 49 | } 50 | } 51 | /******************************************************************************* 52 | * Function Name : PMAToUserBufferCopy 53 | * Description : Copy a buffer from user memory area to packet memory area (PMA) 54 | * Input : - pbUsrBuf = pointer to user memory area. 55 | * - wPMABufAddr = address into PMA. 56 | * - wNBytes = no. of bytes to be copied. 57 | * Output : None. 58 | * Return : None. 59 | *******************************************************************************/ 60 | void PMAToUserBufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes) 61 | { 62 | u32 n = (wNBytes + 1) >> 1;/* /2*/ 63 | u32 i; 64 | u32 *pdwVal; 65 | pdwVal = (u32 *)(wPMABufAddr * 2 + PMAAddr); 66 | for (i = n; i != 0; i--) 67 | { 68 | *(u16*)pbUsrBuf++ = *pdwVal++; 69 | pbUsrBuf++; 70 | } 71 | } 72 | 73 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ 74 | -------------------------------------------------------------------------------- /usb_lib/usb_mem.h: -------------------------------------------------------------------------------- 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 2 | * File Name : usb_mem.h 3 | * Author : MCD Application Team 4 | * Version : V2.2.1 5 | * Date : 09/22/2008 6 | * Description : Utility prototypes functions for memory/PMA transfers 7 | ******************************************************************************** 8 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 9 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 10 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 11 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 12 | * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 13 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 14 | *******************************************************************************/ 15 | 16 | /* Define to prevent recursive inclusion -------------------------------------*/ 17 | #ifndef __USB_MEM_H 18 | #define __USB_MEM_H 19 | 20 | /* Includes ------------------------------------------------------------------*/ 21 | /* Exported types ------------------------------------------------------------*/ 22 | /* Exported constants --------------------------------------------------------*/ 23 | /* Exported macro ------------------------------------------------------------*/ 24 | /* Exported functions ------------------------------------------------------- */ 25 | void UserToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes); 26 | void PMAToUserBufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes); 27 | 28 | /* External variables --------------------------------------------------------*/ 29 | 30 | #endif /*__USB_MEM_H*/ 31 | 32 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ 33 | -------------------------------------------------------------------------------- /usb_lib/usb_type.h: -------------------------------------------------------------------------------- 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 2 | * File Name : usb_type.h 3 | * Author : MCD Application Team 4 | * Version : V2.2.1 5 | * Date : 09/22/2008 6 | * Description : Type definitions used by the USB Library 7 | ******************************************************************************** 8 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 9 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 10 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 11 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 12 | * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 13 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 14 | *******************************************************************************/ 15 | 16 | /* Define to prevent recursive inclusion -------------------------------------*/ 17 | #ifndef __USB_TYPE_H 18 | #define __USB_TYPE_H 19 | 20 | /* Exported types ------------------------------------------------------------*/ 21 | /* Exported constants --------------------------------------------------------*/ 22 | #ifndef NULL 23 | #define NULL ((void *)0) 24 | #endif 25 | 26 | #ifndef __STM32F10x_TYPE_H 27 | 28 | typedef signed long s32; 29 | typedef signed short s16; 30 | typedef signed char s8; 31 | 32 | typedef volatile signed long vs32; 33 | typedef volatile signed short vs16; 34 | typedef volatile signed char vs8; 35 | 36 | typedef unsigned long u32; 37 | typedef unsigned short u16; 38 | typedef unsigned char u8; 39 | 40 | typedef unsigned long const uc32; /* Read Only */ 41 | typedef unsigned short const uc16; /* Read Only */ 42 | typedef unsigned char const uc8; /* Read Only */ 43 | 44 | typedef volatile unsigned long vu32; 45 | typedef volatile unsigned short vu16; 46 | typedef volatile unsigned char vu8; 47 | 48 | typedef volatile unsigned long const vuc32; /* Read Only */ 49 | typedef volatile unsigned short const vuc16; /* Read Only */ 50 | typedef volatile unsigned char const vuc8; /* Read Only */ 51 | 52 | 53 | typedef enum 54 | { 55 | FALSE = 0, TRUE = !FALSE 56 | } 57 | bool; 58 | 59 | typedef enum { RESET = 0, SET = !RESET } FlagStatus, ITStatus; 60 | 61 | typedef enum { DISABLE = 0, ENABLE = !DISABLE} FunctionalState; 62 | 63 | typedef enum { ERROR = 0, SUCCESS = !ERROR} ErrorStatus; 64 | #endif 65 | 66 | /* Exported macro ------------------------------------------------------------*/ 67 | /* Exported functions ------------------------------------------------------- */ 68 | /* External variables --------------------------------------------------------*/ 69 | 70 | #endif /* __USB_TYPE_H */ 71 | 72 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ 73 | --------------------------------------------------------------------------------