├── F1 ├── Readme ├── binary │ └── STM32F1-CDC-Bootloader.bin └── eclipse_project │ ├── .cproject │ ├── .project │ ├── .settings │ └── language.settings.xml │ ├── LinkerScript.ld │ └── src │ ├── cdc.h │ ├── libmaple │ ├── bit_constants.h │ ├── bitband.h │ ├── bits.h │ ├── bkp.h │ ├── board.h │ ├── boards.h │ ├── delay.h │ ├── exti.h │ ├── flash.c │ ├── flash.h │ ├── gpio.c │ ├── gpio.h │ ├── libmaple.h │ ├── libmaple_types.h │ ├── nvic.c │ ├── nvic.h │ ├── pwr.h │ ├── rcc.c │ ├── rcc.h │ ├── rcc_f1.c │ ├── rcc_private.h │ ├── scb.h │ ├── series │ │ ├── flash.h │ │ ├── gpio.h │ │ ├── nvic.h │ │ ├── pwr.h │ │ ├── rcc.h │ │ ├── stm32.h │ │ └── timer.h │ ├── stm32.h │ ├── stm32_private.h │ ├── syscfg.h │ ├── systick.c │ ├── systick.h │ ├── util.h │ ├── util │ │ └── atomic.h │ ├── wirish_time.h │ └── wirish_types.h │ ├── main.c │ ├── startup_stm32.S │ ├── system.c │ ├── usb.c │ ├── usb_def.h │ ├── usb_desc.c │ ├── usb_desc.h │ ├── usb_func.h │ └── usbstd.h ├── F3 ├── Readme.md ├── binary │ └── STM32F3-CDC-Bootloader.bin └── eclipse_project │ ├── .cproject │ ├── .project │ ├── CMSIS │ └── core │ │ ├── arm_common_tables.h │ │ ├── arm_const_structs.h │ │ ├── arm_math.h │ │ ├── cmsis_armcc.h │ │ ├── cmsis_armcc_V6.h │ │ ├── cmsis_gcc.h │ │ ├── core_cm0.h │ │ ├── core_cm0plus.h │ │ ├── core_cm3.h │ │ ├── core_cm4.h │ │ ├── core_cm7.h │ │ ├── core_cmFunc.h │ │ ├── core_cmInstr.h │ │ ├── core_cmSimd.h │ │ ├── core_sc000.h │ │ └── core_sc300.h │ ├── LinkerScript.ld │ ├── dbg │ ├── ring_buffer.c │ ├── ring_buffer.h │ ├── usart.c │ ├── usart.h │ ├── usart_f1.c │ ├── usart_private.h │ ├── usb_trx.c │ └── usb_trx.h │ └── src │ ├── cdc.h │ ├── libmaple │ ├── bitband.h │ ├── bkp.h │ ├── board.h │ ├── delay.h │ ├── exti.h │ ├── flash.c │ ├── flash.h │ ├── gpio.c │ ├── gpio.h │ ├── libmaple.h │ ├── libmaple_types.h │ ├── nvic.c │ ├── nvic.h │ ├── pwr.c │ ├── pwr.h │ ├── rcc.c │ ├── rcc.h │ ├── rcc_f3.c │ ├── rcc_private.h │ ├── stm32.h │ ├── stm32_private.h │ ├── syscfg.c │ ├── syscfg.h │ ├── system.c │ ├── systick.c │ ├── systick.h │ └── util.h │ ├── main.c │ ├── startup_stm32.S │ ├── stm32f303xc.h │ ├── stm32f3xx.h │ ├── usb.c │ ├── usb_def.h │ ├── usb_desc.c │ ├── usb_desc.h │ ├── usb_func.h │ └── usb_std.h └── README.md /F1/Readme: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /F1/binary/STM32F1-CDC-Bootloader.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stevstrong/STM32-CDC-Bootloader/d53886de2380a4812abd720e6777ede4b293cc57/F1/binary/STM32F1-CDC-Bootloader.bin -------------------------------------------------------------------------------- /F1/eclipse_project/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | STM32F1-CDC-Bootloader 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 24 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 25 | 26 | 27 | -------------------------------------------------------------------------------- /F1/eclipse_project/.settings/language.settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /F1/eclipse_project/LinkerScript.ld: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stevstrong/STM32-CDC-Bootloader/d53886de2380a4812abd720e6777ede4b293cc57/F1/eclipse_project/LinkerScript.ld -------------------------------------------------------------------------------- /F1/eclipse_project/src/cdc.h: -------------------------------------------------------------------------------- 1 | /** 2 | * usb_cdc_defines USB CDC Type Definitions 3 | * 4 | */ 5 | #ifndef __CDC_H 6 | #define __CDC_H 7 | 8 | /* Definitions of Communications Device Class from 9 | * "Universal Serial Bus Class Definitions for Communications Devices 10 | * Revision 1.2" 11 | */ 12 | 13 | /* Table 2: Communications Device Class Code */ 14 | #define USB_CLASS_CDC 0x02 15 | 16 | /* Table 4: Class Subclass Code */ 17 | #define USB_CDC_SUBCLASS_DLCM 0x01 18 | #define USB_CDC_SUBCLASS_ACM 0x02 19 | /* ... */ 20 | 21 | /* Table 5 Communications Interface Class Control Protocol Codes */ 22 | #define USB_CDC_PROTOCOL_NONE 0x00 23 | #define USB_CDC_PROTOCOL_AT 0x01 24 | /* ... */ 25 | 26 | /* Table 6: Data Interface Class Code */ 27 | #define USB_CLASS_DATA 0x0A 28 | 29 | /* Table 12: Type Values for the bDescriptorType Field */ 30 | #define CS_INTERFACE 0x24 31 | #define CS_ENDPOINT 0x25 32 | 33 | /* Table 13: bDescriptor SubType in Communications Class Functional 34 | * Descriptors */ 35 | #define USB_CDC_TYPE_HEADER 0x00 36 | #define USB_CDC_TYPE_CALL_MANAGEMENT 0x01 37 | #define USB_CDC_TYPE_ACM 0x02 38 | /* ... */ 39 | #define USB_CDC_TYPE_UNION 0x06 40 | /* ... */ 41 | 42 | /* Table 15: Class-Specific Descriptor Header Format */ 43 | typedef struct usb_cdc_header_descriptor { 44 | uint8_t bFunctionLength; 45 | uint8_t bDescriptorType; 46 | uint8_t bDescriptorSubtype; 47 | uint16_t bcdCDC; 48 | } __attribute((packed)) usb_cdc_header_descriptor; 49 | 50 | /* Table 16: Union Interface Functional Descriptor */ 51 | typedef struct usb_cdc_union_descriptor { 52 | uint8_t bFunctionLength; 53 | uint8_t bDescriptorType; 54 | uint8_t bDescriptorSubtype; 55 | uint8_t bControlInterface; 56 | uint8_t bSubordinateInterface0; 57 | /* ... */ 58 | } __attribute((packed)) usb_cdc_union_descriptor; 59 | 60 | 61 | /* Definitions for Abstract Control Model devices from: 62 | * "Universal Serial Bus Communications Class Subclass Specification for 63 | * PSTN Devices" 64 | */ 65 | 66 | /* Table 3: Call Management Functional Descriptor */ 67 | typedef struct usb_cdc_call_management_descriptor { 68 | uint8_t bFunctionLength; 69 | uint8_t bDescriptorType; 70 | uint8_t bDescriptorSubtype; 71 | uint8_t bmCapabilities; 72 | uint8_t bDataInterface; 73 | } __attribute((packed)) usb_cdc_call_management_descriptor; 74 | 75 | /* Table 4: Abstract Control Management Functional Descriptor */ 76 | typedef struct usb_cdc_acm_descriptor { 77 | uint8_t bFunctionLength; 78 | uint8_t bDescriptorType; 79 | uint8_t bDescriptorSubtype; 80 | uint8_t bmCapabilities; 81 | } __attribute((packed)) usb_cdc_acm_descriptor; 82 | 83 | /* Table 13: Class-Specific Request Codes for PSTN subclasses */ 84 | /* ... */ 85 | #define USB_CDC_REQ_SET_LINE_CODING 0x20 86 | #define USB_CDC_REQ_GET_LINE_CODING 0x21 87 | #define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22 88 | #define USB_CDC_REQ_SEND_BREAK 0x23 89 | 90 | 91 | /* Table 17: Line Coding Structure */ 92 | typedef struct usb_cdc_line_coding { 93 | uint32_t baudRate; // dwDTERate; 94 | uint8_t stopBits; //bCharFormat; 95 | uint8_t parityType; //bParityType; 96 | uint8_t dataBits; //bDataBits; 97 | } __attribute((packed)) usb_cdc_line_coding; 98 | 99 | enum usb_cdc_line_coding_bCharFormat { 100 | USB_CDC_1_STOP_BITS = 0, 101 | USB_CDC_1_5_STOP_BITS = 1, 102 | USB_CDC_2_STOP_BITS = 2, 103 | }; 104 | 105 | enum usb_cdc_line_coding_bParityType { 106 | USB_CDC_NO_PARITY = 0, 107 | USB_CDC_ODD_PARITY = 1, 108 | USB_CDC_EVEN_PARITY = 2, 109 | USB_CDC_MARK_PARITY = 3, 110 | USB_CDC_SPACE_PARITY = 4, 111 | }; 112 | 113 | /* Table 30: Class-Specific Notification Codes for PSTN subclasses */ 114 | #define USB_CDC_NOTIFY_SERIAL_STATE 0x20 115 | 116 | 117 | /* Notification Structure */ 118 | struct usb_cdc_notification { 119 | uint8_t bmRequestType; 120 | uint8_t bNotification; 121 | uint16_t wValue; 122 | uint16_t wIndex; 123 | uint16_t wLength; 124 | } __attribute__((packed)); 125 | 126 | #endif 127 | 128 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/bitband.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2011 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/bitband.h 29 | * 30 | * @brief Bit-banding utility functions 31 | */ 32 | 33 | #ifndef _LIBMAPLE_BITBAND_H_ 34 | #define _LIBMAPLE_BITBAND_H_ 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | #include "libmaple_types.h" 41 | 42 | #define BB_SRAM_REF 0x20000000 43 | #define BB_SRAM_BASE 0x22000000 44 | #define BB_PERI_REF 0x40000000 45 | #define BB_PERI_BASE 0x42000000 46 | 47 | static inline volatile uint32* __bb_addr(volatile void*, 48 | uint32, 49 | uint32, 50 | uint32); 51 | 52 | /** 53 | * @brief Obtain a pointer to the bit-band address corresponding to a 54 | * bit in a volatile SRAM address. 55 | * @param address Address in the bit-banded SRAM region 56 | * @param bit Bit in address to bit-band 57 | */ 58 | static inline volatile uint32* bb_sramp(volatile void *address, uint32 bit) { 59 | return __bb_addr(address, bit, BB_SRAM_BASE, BB_SRAM_REF); 60 | } 61 | 62 | /** 63 | * @brief Get a bit from an address in the SRAM bit-band region. 64 | * @param address Address in the SRAM bit-band region to read from 65 | * @param bit Bit in address to read 66 | * @return bit's value in address. 67 | */ 68 | static inline uint8 bb_sram_get_bit(volatile void *address, uint32 bit) { 69 | return *bb_sramp(address, bit); 70 | } 71 | 72 | /** 73 | * @brief Set a bit in an address in the SRAM bit-band region. 74 | * @param address Address in the SRAM bit-band region to write to 75 | * @param bit Bit in address to write to 76 | * @param val Value to write for bit, either 0 or 1. 77 | */ 78 | static inline void bb_sram_set_bit(volatile void *address, 79 | uint32 bit, 80 | uint8 val) { 81 | *bb_sramp(address, bit) = val; 82 | } 83 | 84 | /** 85 | * @brief Obtain a pointer to the bit-band address corresponding to a 86 | * bit in a peripheral address. 87 | * @param address Address in the bit-banded peripheral region 88 | * @param bit Bit in address to bit-band 89 | */ 90 | static inline volatile uint32* bb_perip(volatile void *address, uint32 bit) { 91 | return __bb_addr(address, bit, BB_PERI_BASE, BB_PERI_REF); 92 | } 93 | 94 | /** 95 | * @brief Get a bit from an address in the peripheral bit-band region. 96 | * @param address Address in the peripheral bit-band region to read from 97 | * @param bit Bit in address to read 98 | * @return bit's value in address. 99 | */ 100 | static inline uint8 bb_peri_get_bit(volatile void *address, uint32 bit) { 101 | return *bb_perip(address, bit); 102 | } 103 | 104 | /** 105 | * @brief Set a bit in an address in the peripheral bit-band region. 106 | * @param address Address in the peripheral bit-band region to write to 107 | * @param bit Bit in address to write to 108 | * @param val Value to write for bit, either 0 or 1. 109 | */ 110 | static inline void bb_peri_set_bit(volatile void *address, 111 | uint32 bit, 112 | uint8 val) { 113 | *bb_perip(address, bit) = val; 114 | } 115 | 116 | static inline volatile uint32* __bb_addr(volatile void *address, 117 | uint32 bit, 118 | uint32 bb_base, 119 | uint32 bb_ref) { 120 | return (volatile uint32*)(bb_base + ((uint32)address - bb_ref) * 32 + 121 | bit * 4); 122 | } 123 | 124 | #ifdef __cplusplus 125 | } 126 | #endif 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/bits.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 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /* Note: Use of this header file is deprecated. Use bit_constants.h 28 | instead. */ 29 | 30 | #ifndef _WIRISH_BITS_H_ 31 | #define _WIRISH_BITS_H_ 32 | 33 | #include 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/board.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2011 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file wirish/boards/maple_mini/include/board/board.h 29 | * @author Marti Bolivar 30 | * @brief Maple Mini board header. 31 | * 32 | * See wirish/boards/maple/include/board/board.h for more information 33 | * on these definitions. 34 | */ 35 | 36 | #ifndef _BOARD_GENERIC_STM32F103C_H_ 37 | #define _BOARD_GENERIC_STM32F103C_H_ 38 | 39 | #define CYCLES_PER_MICROSECOND (F_CPU / 1000000U) 40 | #define SYSTICK_RELOAD_VAL (F_CPU/1000) - 1 /* takes a cycle to reload */ 41 | 42 | #define BOARD_NR_USARTS 3 43 | #define BOARD_USART1_TX_PIN PA9 44 | #define BOARD_USART1_RX_PIN PA10 45 | #define BOARD_USART2_TX_PIN PA2 46 | #define BOARD_USART2_RX_PIN PA3 47 | #define BOARD_USART3_TX_PIN PB10 48 | #define BOARD_USART3_RX_PIN PB11 49 | 50 | #define BOARD_NR_SPI 2 51 | #define BOARD_SPI1_NSS_PIN PA4 52 | #define BOARD_SPI1_MOSI_PIN PA7 53 | #define BOARD_SPI1_MISO_PIN PA6 54 | #define BOARD_SPI1_SCK_PIN PA5 55 | 56 | #define BOARD_SPI1_ALT_NSS_PIN PA15 57 | #define BOARD_SPI1_ALT_MOSI_PIN PB5 58 | #define BOARD_SPI1_ALT_MISO_PIN PB4 59 | #define BOARD_SPI1_ALT_SCK_PIN PB3 60 | 61 | #define BOARD_SPI2_NSS_PIN PB12 62 | #define BOARD_SPI2_MOSI_PIN PB15 63 | #define BOARD_SPI2_MISO_PIN PB14 64 | #define BOARD_SPI2_SCK_PIN PB13 65 | 66 | #define BOARD_NR_GPIO_PINS 35 67 | #define BOARD_NR_PWM_PINS 12 68 | #define BOARD_NR_ADC_PINS 9 69 | #define BOARD_NR_USED_PINS 4 70 | 71 | 72 | #define BOARD_JTMS_SWDIO_PIN 22 73 | #define BOARD_JTCK_SWCLK_PIN 21 74 | #define BOARD_JTDI_PIN 20 75 | #define BOARD_JTDO_PIN 19 76 | #define BOARD_NJTRST_PIN 18 77 | 78 | #define BOARD_USB_DISC_DEV NULL 79 | #define BOARD_USB_DISC_BIT NULL 80 | 81 | #define LED_BUILTIN PC13 82 | 83 | // Note this needs to match with the PIN_MAP array in board.cpp 84 | enum { 85 | PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7, PA8, PA9, PA10, PA11, PA12, PA13,PA14,PA15, 86 | PB0, PB1, PB2, PB3, PB4, PB5, PB6, PB7, PB8, PB9, PB10, PB11, PB12, PB13,PB14,PB15, 87 | PC13 = (32+13), PC14,PC15 88 | }; 89 | 90 | #endif 91 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/delay.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * Copyright (c) 2011 LeafLabs, LLC. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/include/libmaple/delay.h 30 | * @brief Delay implementation 31 | */ 32 | 33 | #ifndef _LIBMAPLE_DELAY_H_ 34 | #define _LIBMAPLE_DELAY_H_ 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | #include "libmaple_types.h" 41 | #include "stm32.h" 42 | 43 | /** 44 | * @brief Delay the given number of microseconds. 45 | * 46 | * @param us Number of microseconds to delay. 47 | */ 48 | static inline void delay_us(uint32 us) { 49 | if (us==0) 50 | { 51 | return; 52 | } 53 | us *= STM32_DELAY_US_MULT; 54 | 55 | /* fudge for function call overhead */ 56 | us--; 57 | asm volatile(" mov r0, %[us] \n\t" 58 | "1: subs r0, #1 \n\t" 59 | " bhi 1b \n\t" 60 | : 61 | : [us] "r" (us) 62 | : "r0"); 63 | } 64 | 65 | /** 66 | * Delay for at least the given number of microseconds. 67 | * 68 | * Interrupts, etc. may cause the actual number of microseconds to 69 | * exceed us. However, this function will return no less than us 70 | * microseconds from the time it is called. 71 | * 72 | * @param us the number of microseconds to delay. 73 | * @see delay() 74 | */ 75 | static inline void delayMicroseconds(uint32 us) { 76 | delay_us(us); 77 | } 78 | 79 | /** 80 | * Delay for at least the given number of milliseconds. 81 | * 82 | * Interrupts, etc. may cause the actual number of milliseconds to 83 | * exceed ms. However, this function will return no less than ms 84 | * milliseconds from the time it is called. 85 | * 86 | * @param ms the number of milliseconds to delay. 87 | * @see delayMicroseconds() 88 | */ 89 | void delay(uint32 ms); 90 | 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/flash.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * Copyright (c) 2011, 2012 LeafLabs, LLC. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/flash.c 30 | * @brief Flash management functions 31 | */ 32 | 33 | #include "libmaple_types.h" 34 | #include "flash.h" 35 | #include "nvic.h" 36 | 37 | /** 38 | * @brief Set flash wait states 39 | * 40 | * Note that not all wait states are available on every MCU. See the 41 | * Flash programming manual for your MCU for restrictions on the 42 | * allowed value of wait_states for a given system clock (SYSCLK) 43 | * frequency. 44 | * 45 | * @param wait_states number of wait states (one of 46 | * FLASH_WAIT_STATE_0, FLASH_WAIT_STATE_1, 47 | * ..., FLASH_WAIT_STATE_7). 48 | */ 49 | void flash_set_latency(uint32 wait_states) 50 | { 51 | uint32 val = FLASH->ACR; 52 | 53 | val &= ~FLASH_ACR_LATENCY; 54 | val |= wait_states; 55 | 56 | FLASH->ACR = val; 57 | } 58 | 59 | //----------------------------------------------------------------------------- 60 | // Reference: RM0008 chap. 3.3.3. 61 | //----------------------------------------------------------------------------- 62 | /* 63 | * The right sequence is : 64 | 65 | ERASE: 66 | - check busy flag (wait till it is reset) 67 | - Set PER bit to enable the erase 68 | - write the address of the page to be erased into FLASH_AR register (the entire page containing the address will be erased). 69 | - Set STRT bit to trigger the erase 70 | - Wait till Busy flag or STRT flag is cleared by hardware (it takes a time) 71 | - Read and verify that the location is erased (should have 0xFFFF) 72 | 73 | WRITE: 74 | - Unlock the flash 75 | - Wait on BUSY 76 | - Set the PG bit 77 | - Write to destination (16-bit in a half-word aligned address) 78 | - Wait on BUSY 79 | */ 80 | //----------------------------------------------------------------------------- 81 | void flash_erase_page(uint16_t *page) 82 | { 83 | // Unlock Flash with magic keys 84 | flash_unlock(); 85 | flash_wait_for_ready(); 86 | 87 | // Erase page 88 | flash_set_cr(FLASH_CR_PER); // erase page flag 89 | flash_set_page((uint32_t) page); 90 | flash_start(); 91 | 92 | flash_wait_for_ready(); 93 | } 94 | 95 | //----------------------------------------------------------------------------- 96 | void flash_write_data(uint16_t *page, uint16_t *data, uint16_t size) 97 | { 98 | flash_set_cr(FLASH_CR_PG); // erase program flag 99 | 100 | while (size--) 101 | { 102 | *page++ = *data++; 103 | 104 | flash_wait_for_ready(); 105 | } 106 | } 107 | 108 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/flash.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/flash.h 29 | * @brief Flash support. 30 | */ 31 | 32 | #ifndef _LIBMAPLE_FLASH_H_ 33 | #define _LIBMAPLE_FLASH_H_ 34 | 35 | #ifdef __cplusplus 36 | extern "C"{ 37 | #endif 38 | 39 | #include "libmaple_types.h" 40 | 41 | #define FLASH_KEY1 0x45670123U 42 | #define FLASH_KEY2 0xCDEF89ABU 43 | 44 | #define FLASH_WAIT_STATE_0 0x0 45 | #define FLASH_WAIT_STATE_1 0x1 46 | #define FLASH_WAIT_STATE_2 0x2 47 | #define FLASH_WAIT_STATE_3 0x3 48 | #define FLASH_WAIT_STATE_4 0x4 49 | #define FLASH_WAIT_STATE_5 0x5 50 | #define FLASH_WAIT_STATE_6 0x6 51 | #define FLASH_WAIT_STATE_7 0x7 52 | 53 | /* The series header must define: 54 | * 55 | * - FLASH_SAFE_WAIT_STATES, the smallest number of wait states that 56 | * it is safe to use when SYSCLK is at its fastest documented rate 57 | * and the MCU is powered at 3.3V (i.e. this doesn't consider 58 | * overclocking or low voltage operation). 59 | * 60 | * - The following bit flags, for flash_enable_features(): 61 | * 62 | * -- FLASH_PREFETCH: prefetcher 63 | * -- FLASH_ICACHE: instruction cache 64 | * -- FLASH_DCACHE: data cache 65 | * 66 | * See that function's Doxygen for more restrictions. 67 | */ 68 | #include "series/flash.h" 69 | 70 | #ifdef __DOXYGEN__ 71 | /** Flash register map base pointer. */ 72 | #define FLASH 73 | #endif 74 | 75 | 76 | /* 77 | * Flash routines 78 | */ 79 | 80 | extern void flash_set_latency(uint32 wait_states); 81 | void flash_erase_page(uint16_t *page); 82 | extern void flash_write_data(uint16_t *page, uint16_t *data, uint16_t size); 83 | 84 | /** 85 | * @brief Enable Flash memory features 86 | * 87 | * If the target MCU doesn't provide a feature (e.g. instruction and 88 | * data caches on the STM32F1), the flag will be ignored. This allows 89 | * using these flags unconditionally, with the desired effect taking 90 | * place on targets that support them. 91 | * 92 | * @param feature_flags Bitwise OR of the following: 93 | * FLASH_PREFETCH (turns on prefetcher), 94 | * FLASH_ICACHE (turns on instruction cache), 95 | * FLASH_DCACHE (turns on data cache). 96 | */ 97 | static inline void flash_enable_features(uint32 feature_flags) { 98 | FLASH->ACR |= feature_flags; 99 | } 100 | 101 | /** 102 | * @brief Deprecated. Use flash_enable_features(FLASH_PREFETCH) instead. 103 | */ 104 | static inline void flash_enable_prefetch(void) { 105 | flash_enable_features(FLASH_PREFETCH); 106 | } 107 | 108 | static inline int flash_locked() { 109 | return (FLASH->CR & FLASH_CR_LOCK); 110 | } 111 | 112 | static inline void flash_lock() { 113 | FLASH->CR = FLASH_CR_LOCK; 114 | } 115 | 116 | static inline void flash_unlock(void) { 117 | // Unlock Flash with magic keys 118 | FLASH->KEYR = FLASH_KEY1; 119 | FLASH->KEYR = FLASH_KEY2; 120 | } 121 | 122 | static inline void flash_wait_for_ready(void) { 123 | while (FLASH->SR & FLASH_SR_BSY); 124 | } 125 | 126 | static inline void flash_set_cr(int cr) 127 | { 128 | FLASH->CR = cr; 129 | } 130 | 131 | static inline void flash_set_page(uint32 page) { 132 | FLASH->AR = page; 133 | } 134 | 135 | static inline void flash_start() { 136 | FLASH->CR |= FLASH_CR_STRT; 137 | } 138 | 139 | 140 | #ifdef __cplusplus 141 | } 142 | #endif 143 | 144 | #endif 145 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/gpio.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/stm32f1/gpio.c 29 | * @brief STM32F1 GPIO support. 30 | */ 31 | 32 | #include "gpio.h" 33 | 34 | 35 | /* 36 | * GPIO devices 37 | */ 38 | 39 | /** GPIO port A device. */ 40 | gpio_dev gpioa = { 41 | .regs = GPIOA_BASE, 42 | .clk_id = RCC_GPIOA, 43 | .exti_port = EXTI_PA, 44 | }; 45 | 46 | /** GPIO port B device. */ 47 | gpio_dev gpiob = { 48 | .regs = GPIOB_BASE, 49 | .clk_id = RCC_GPIOB, 50 | .exti_port = EXTI_PB, 51 | }; 52 | 53 | /** GPIO port C device. */ 54 | gpio_dev gpioc = { 55 | .regs = GPIOC_BASE, 56 | .clk_id = RCC_GPIOC, 57 | .exti_port = EXTI_PC, 58 | }; 59 | 60 | #if STM32_NR_GPIO_PORTS > 3 61 | /** GPIO port D device. */ 62 | gpio_dev gpiod = { 63 | .regs = GPIOD_BASE, 64 | .clk_id = RCC_GPIOD, 65 | .exti_port = EXTI_PD, 66 | }; 67 | #endif 68 | #if STM32_NR_GPIO_PORTS > 4 69 | gpio_dev gpioe = { 70 | .regs = GPIOE_BASE, 71 | .clk_id = RCC_GPIOE, 72 | .exti_port = EXTI_PE, 73 | }; 74 | /** GPIO port E device. */ 75 | gpio_dev* const GPIOE = &gpioe; 76 | 77 | gpio_dev gpiof = { 78 | .regs = GPIOF_BASE, 79 | .clk_id = RCC_GPIOF, 80 | .exti_port = EXTI_PF, 81 | }; 82 | /** GPIO port F device. */ 83 | gpio_dev* const GPIOF = &gpiof; 84 | 85 | gpio_dev gpiog = { 86 | .regs = GPIOG_BASE, 87 | .clk_id = RCC_GPIOG, 88 | .exti_port = EXTI_PG, 89 | }; 90 | /** GPIO port G device. */ 91 | gpio_dev* const GPIOG = &gpiog; 92 | #endif 93 | 94 | const gpio_dev* const gpio_devs[3] = {GPIOA, GPIOB, GPIOC}; 95 | /* 96 | * GPIO routines 97 | */ 98 | 99 | /** 100 | * Initialize and reset all available GPIO devices. 101 | */ 102 | void gpio_init_all(void) 103 | { 104 | gpio_init(GPIOA); 105 | gpio_init(GPIOB); 106 | gpio_init(GPIOC); 107 | #if STM32_NR_GPIO_PORTS > 3 108 | gpio_init(GPIOD); 109 | #endif 110 | #if STM32_NR_GPIO_PORTS > 4 111 | gpio_init(GPIOE); 112 | gpio_init(GPIOF); 113 | gpio_init(GPIOG); 114 | #endif 115 | } 116 | 117 | void gpio_deinit_all(void) 118 | { 119 | gpio_deinit(GPIOA); 120 | gpio_deinit(GPIOB); 121 | gpio_deinit(GPIOC); 122 | #if STM32_NR_GPIO_PORTS > 3 123 | gpio_init(GPIOD); 124 | #endif 125 | #if STM32_NR_GPIO_PORTS > 4 126 | gpio_deinit(GPIOE); 127 | gpio_deinit(GPIOF); 128 | gpio_deinit(GPIOG); 129 | #endif 130 | } 131 | 132 | /** 133 | * Set the mode of a GPIO pin. 134 | * 135 | * @param dev GPIO device. 136 | * @param pin Pin on the device whose mode to set, 0--15. 137 | * @param mode General purpose or alternate function mode to set the pin to. 138 | * @see gpio_pin_mode 139 | */ 140 | void gpio_set_mode(const gpio_dev *dev, uint8 bit, gpio_pin_mode mode) { 141 | gpio_reg_map *regs = dev->regs; 142 | __IO uint32 *cr = ®s->CRL + (bit >> 3); 143 | uint32 shift = (bit & 0x7) * 4; 144 | uint32 tmp = *cr; 145 | 146 | tmp &= ~(0xF << shift); 147 | tmp |= (mode == GPIO_INPUT_PU ? GPIO_INPUT_PD : mode) << shift; 148 | *cr = tmp; 149 | 150 | if (mode == GPIO_INPUT_PD) { 151 | regs->ODR &= ~(1U << bit); 152 | } else if (mode == GPIO_INPUT_PU) { 153 | regs->ODR |= (1U << bit); 154 | } 155 | } 156 | //----------------------------------------------------------------------------- 157 | gpio_pin_mode gpio_get_mode(const gpio_dev *dev, uint8 pin) { 158 | gpio_reg_map *regs = dev->regs; 159 | __IO uint32 *cr = ®s->CRL + (pin >> 3); 160 | uint32 shift = (pin & 0x7) * 4; 161 | 162 | uint32 crMode = (*cr>>shift) & 0x0F; 163 | 164 | // could be pull up or pull down. Nee to check the ODR 165 | if (crMode==GPIO_INPUT_PD && ((regs->ODR >> pin) & 0x01) !=0 ) 166 | { 167 | crMode = GPIO_INPUT_PU; 168 | } 169 | 170 | return(crMode); 171 | } 172 | 173 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/gpio.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * Copyright (c) 2011, 2012 LeafLabs, LLC. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/include/libmaple/gpio.h 30 | * @brief General Purpose I/O (GPIO) interace. 31 | */ 32 | 33 | #ifndef _LIBMAPLE_GPIO_H_ 34 | #define _LIBMAPLE_GPIO_H_ 35 | 36 | #ifdef __cplusplus 37 | extern "C"{ 38 | #endif 39 | 40 | /* 41 | * Note: Series header must define: 42 | * - enum gpio_pin_mode (TODO think harder about portability here) 43 | */ 44 | 45 | #include "series/gpio.h" 46 | #include "libmaple_types.h" 47 | #include "exti.h" 48 | #include "rcc.h" 49 | 50 | /* 51 | * Device type 52 | */ 53 | 54 | 55 | extern const gpio_dev * const gpio_devs[3]; 56 | /* 57 | * Portable routines 58 | */ 59 | static inline void enableDebugPorts() { afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY); } 60 | 61 | inline void gpio_init(const gpio_dev *dev) { 62 | rcc_clk_enable(dev->clk_id); 63 | rcc_reset_dev(dev->clk_id); 64 | } 65 | 66 | inline void gpio_deinit(const gpio_dev *dev) { 67 | rcc_clk_disable(dev->clk_id); 68 | } 69 | 70 | void gpio_init_all(void); 71 | void gpio_deinit_all(void); 72 | 73 | /* TODO flags argument version? */ 74 | void gpio_set_mode(const gpio_dev *dev, uint8 bit, gpio_pin_mode mode); 75 | gpio_pin_mode gpio_get_mode(const gpio_dev *dev, uint8 bit); 76 | 77 | static inline void gpio_set_pin_mode(uint8 pin, gpio_pin_mode mode) { 78 | gpio_set_mode(gpio_devs[pin/16], pin%16, mode); 79 | } 80 | /** 81 | * @brief Get a GPIO port's corresponding EXTI port configuration. 82 | * @param dev GPIO port whose exti_cfg to return. 83 | */ 84 | static inline exti_cfg gpio_exti_port(gpio_dev *dev) { 85 | return (exti_cfg)(EXTI_PA + (dev->clk_id - RCC_GPIOA)); 86 | } 87 | 88 | /** 89 | * Set or reset a GPIO pin. 90 | * 91 | * Pin must have previously been configured to output mode. 92 | * 93 | * @param dev GPIO device whose pin to set. 94 | * @param pin Pin on to set or reset 95 | * @param val If true, set the pin. If false, reset the pin. 96 | */ 97 | static inline void gpio_write_bit(const gpio_dev *dev, uint8 bit, uint8 val) { 98 | val = !val; /* "set" bits are lower than "reset" bits */ 99 | dev->regs->BSRR = (1U << bit) << (16 * val); 100 | } 101 | 102 | static inline void gpio_write_pin(uint8 pin, uint8 val) { 103 | gpio_write_bit(gpio_devs[pin/16], pin%16, val); 104 | } 105 | 106 | /** 107 | * Determine whether or not a GPIO pin is set. 108 | * 109 | * Pin must have previously been configured to input mode. 110 | * 111 | * @param dev GPIO device whose pin to test. 112 | * @param pin Pin on dev to test. 113 | * @return True if the pin is set, false otherwise. 114 | */ 115 | static inline uint32 gpio_read_bit(const gpio_dev *dev, uint8 pin) { 116 | return dev->regs->IDR & (1U << pin); 117 | } 118 | 119 | static inline uint32 gpio_read_pin(uint8 pin) { 120 | return gpio_read_bit(gpio_devs[pin/16], pin%16); 121 | } 122 | 123 | /** 124 | * Toggle a pin configured as output push-pull. 125 | * @param dev GPIO device. 126 | * @param pin Pin on dev to toggle. 127 | */ 128 | static inline void gpio_toggle_bit(gpio_dev *dev, uint8 pin) { 129 | dev->regs->ODR = dev->regs->ODR ^ (1U << pin); 130 | } 131 | 132 | #ifdef __cplusplus 133 | } 134 | #endif 135 | 136 | #endif 137 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/libmaple.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/libmaple.h 29 | * @brief General include file for libmaple 30 | */ 31 | 32 | #ifndef _LIBMAPLE_LIBMAPLE_H_ 33 | #define _LIBMAPLE_LIBMAPLE_H_ 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | #include "libmaple_types.h" 40 | #include "stm32.h" 41 | #include "util.h" 42 | #include "delay.h" 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/libmaple_types.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/libmaple_types.h 29 | * 30 | * @brief libmaple's types, and operations on types. 31 | */ 32 | 33 | #ifndef _LIBMAPLE_LIBMAPLE_TYPES_H_ 34 | #define _LIBMAPLE_LIBMAPLE_TYPES_H_ 35 | 36 | #include 37 | #include 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | typedef unsigned char uint8; 44 | typedef unsigned short uint16; 45 | typedef uint32_t uint32; 46 | typedef unsigned long long uint64; 47 | 48 | typedef signed char int8; 49 | typedef short int16; 50 | typedef int int32; 51 | typedef long long int64; 52 | 53 | typedef void (*voidFuncPtr)(void); 54 | typedef void (*voidArgumentFuncPtr)(void *); 55 | 56 | #define __IO volatile 57 | #define __attr_flash __attribute__((section (".USER_FLASH"))) 58 | #define __packed __attribute__((__packed__)) 59 | #define __deprecated __attribute__((__deprecated__)) 60 | #define __weak __attribute__((weak)) 61 | #ifndef __unused 62 | #define __unused __attribute__((unused)) 63 | #endif 64 | 65 | #ifndef __always_inline 66 | #define __always_inline inline __attribute__((always_inline)) 67 | #endif 68 | 69 | #ifndef NULL 70 | #define NULL 0 71 | #endif 72 | #ifndef true 73 | #define true 1 74 | #endif 75 | #ifndef false 76 | #define false 0 77 | #endif 78 | #ifndef offsetof 79 | #define offsetof(type, member) __builtin_offsetof(type, member) 80 | #endif 81 | 82 | #ifdef __cplusplus 83 | } 84 | #endif 85 | 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/nvic.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * Copyright (c) 2011 LeafLabs, LLC. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/nvic.c 30 | * @brief Nested vector interrupt controller support. 31 | */ 32 | 33 | #include "nvic.h" 34 | #include "scb.h" 35 | #include "stm32.h" 36 | 37 | /** 38 | * @brief Set interrupt priority for an interrupt line 39 | * 40 | * Note: The STM32 only implements 4 bits of priority, ignoring the 41 | * lower 4 bits. This means there are only 16 levels of priority. 42 | * Bits[3:0] read as zero and ignore writes. 43 | * 44 | * @param irqn device to set 45 | * @param priority Priority to set, 0 being highest priority and 15 46 | * being lowest. 47 | */ 48 | void nvic_irq_set_priority(nvic_irq_num irqn, uint8 priority) { 49 | if (irqn < 0) { 50 | /* This interrupt is in the system handler block */ 51 | SCB_BASE->SHP[((uint32)irqn & 0xF) - 4] = (priority & 0xF) << 4; 52 | } else { 53 | NVIC_BASE->IP[irqn] = (priority & 0xF) << 4; 54 | } 55 | } 56 | 57 | /** 58 | * @brief Initialize the NVIC, setting interrupts to a default priority. 59 | */ 60 | void nvic_init(uint32 address, uint32 offset) { 61 | uint32 i; 62 | 63 | nvic_set_vector_table(address, offset); 64 | 65 | /* 66 | * Lower priority level for all peripheral interrupts to lowest 67 | * possible. 68 | */ 69 | for (i = 0; i < STM32_NR_INTERRUPTS; i++) { 70 | nvic_irq_set_priority((nvic_irq_num)i, 0xF); 71 | } 72 | 73 | /* Lower systick interrupt priority to lowest level */ 74 | nvic_irq_set_priority(NVIC_SYSTICK, 0xF); 75 | } 76 | 77 | /** 78 | * @brief Set the vector table base address. 79 | * 80 | * For stand-alone products, the vector table base address is normally 81 | * the start of Flash (0x08000000). 82 | * 83 | * @param address Vector table base address. 84 | * @param offset Offset from address. Some restrictions apply to the 85 | * use of nonzero offsets; see the ARM Cortex M3 86 | * Technical Reference Manual. 87 | */ 88 | void nvic_set_vector_table(uint32 address, uint32 offset) { 89 | SCB_BASE->VTOR = address | (offset & 0x1FFFFF80); 90 | } 91 | 92 | /** 93 | * @brief Force a system reset. 94 | * 95 | * Resets all major system components, excluding debug. 96 | */ 97 | void nvic_sys_reset() { 98 | uint32 prigroup = SCB_BASE->AIRCR & SCB_AIRCR_PRIGROUP; 99 | SCB_BASE->AIRCR = SCB_AIRCR_VECTKEY | SCB_AIRCR_SYSRESETREQ | prigroup; 100 | asm volatile("dsb"); 101 | while (1) 102 | ; 103 | } 104 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/nvic.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/nvic.h 29 | * @brief Nested vectored interrupt controller support. 30 | * 31 | * Basic usage: 32 | * 33 | * @code 34 | * // Initialise the interrupt controller and point to the vector 35 | * // table at the start of flash. 36 | * nvic_init(0x08000000, 0); 37 | * // Bind in a timer interrupt handler 38 | * timer_attach_interrupt(TIMER_CC1_INTERRUPT, handler); 39 | * // Optionally set the priority 40 | * nvic_irq_set_priority(NVIC_TIMER1_CC, 5); 41 | * // All done, enable all interrupts 42 | * nvic_globalirq_enable(); 43 | * @endcode 44 | */ 45 | 46 | #ifndef _LIBMAPLE_NVIC_H_ 47 | #define _LIBMAPLE_NVIC_H_ 48 | 49 | #ifdef __cplusplus 50 | extern "C"{ 51 | #endif 52 | 53 | #include "libmaple_types.h" 54 | #include "util.h" 55 | 56 | /** NVIC register map type. */ 57 | typedef struct nvic_reg_map { 58 | __IO uint32 ISER[8]; /**< Interrupt Set Enable Registers */ 59 | /** Reserved */ 60 | uint32 RESERVED0[24]; 61 | 62 | __IO uint32 ICER[8]; /**< Interrupt Clear Enable Registers */ 63 | /** Reserved */ 64 | uint32 RESERVED1[24]; 65 | 66 | __IO uint32 ISPR[8]; /**< Interrupt Set Pending Registers */ 67 | /** Reserved */ 68 | uint32 RESERVED2[24]; 69 | 70 | __IO uint32 ICPR[8]; /**< Interrupt Clear Pending Registers */ 71 | /** Reserved */ 72 | uint32 RESERVED3[24]; 73 | 74 | __IO uint32 IABR[8]; /**< Interrupt Active bit Registers */ 75 | /** Reserved */ 76 | uint32 RESERVED4[56]; 77 | 78 | __IO uint8 IP[240]; /**< Interrupt Priority Registers */ 79 | /** Reserved */ 80 | uint32 RESERVED5[644]; 81 | 82 | __IO uint32 STIR; /**< Software Trigger Interrupt Registers */ 83 | } nvic_reg_map; 84 | 85 | /** NVIC register map base pointer. */ 86 | #define NVIC_BASE ((struct nvic_reg_map*)0xE000E100) 87 | 88 | /* 89 | * Note: The series header must define enum nvic_irq_num, which gives 90 | * descriptive names to the interrupts and exceptions from NMI (-14) 91 | * to the largest interrupt available in the series, where the value 92 | * for nonnegative enumerators corresponds to its position in the 93 | * vector table. 94 | * 95 | * It also must define a static inline nvic_irq_disable_all(), which 96 | * writes 0xFFFFFFFF to all ICE registers available in the series. (We 97 | * place the include here to give the series header access to 98 | * NVIC_BASE, in order to let it do so). 99 | */ 100 | 101 | #include "series/nvic.h" 102 | 103 | void nvic_init(uint32 address, uint32 offset); 104 | void nvic_set_vector_table(uint32 address, uint32 offset); 105 | void nvic_irq_set_priority(nvic_irq_num irqn, uint8 priority); 106 | void nvic_sys_reset(); 107 | 108 | /** 109 | * Enables interrupts and configurable fault handlers (clear PRIMASK). 110 | */ 111 | inline void nvic_globalirq_enable() { asm volatile("cpsie i"); } 112 | 113 | /** 114 | * Disable interrupts and configurable fault handlers (set PRIMASK). 115 | */ 116 | inline void nvic_globalirq_disable() { asm volatile("cpsid i"); } 117 | 118 | inline void interrupts() { nvic_globalirq_enable(); } 119 | 120 | inline void noInterrupts() { nvic_globalirq_disable(); } 121 | 122 | /** 123 | * @brief Enable interrupt irq_num 124 | * @param irq_num Interrupt to enable 125 | */ 126 | static inline void nvic_irq_enable(nvic_irq_num irq_num) { 127 | if (irq_num < 0) { 128 | return; 129 | } 130 | NVIC_BASE->ISER[irq_num / 32] = BIT(irq_num % 32); 131 | } 132 | 133 | /** 134 | * @brief Disable interrupt irq_num 135 | * @param irq_num Interrupt to disable 136 | */ 137 | inline void nvic_irq_disable(nvic_irq_num irq_num) { 138 | if (irq_num < 0) { 139 | return; 140 | } 141 | NVIC_BASE->ICER[irq_num / 32] = BIT(irq_num % 32); 142 | } 143 | 144 | /** 145 | * @brief Quickly disable all interrupts. 146 | * 147 | * Calling this function is significantly faster than calling 148 | * nvic_irq_disable() in a loop. 149 | */ 150 | inline void nvic_irq_disable_all(void); 151 | 152 | #ifdef __cplusplus 153 | } 154 | #endif 155 | 156 | #endif 157 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/pwr.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 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/pwr.h 29 | * @brief Power control (PWR). 30 | */ 31 | 32 | #ifndef _LIBMAPLE_PWR_H_ 33 | #define _LIBMAPLE_PWR_H_ 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | #include "libmaple.h" 40 | #include "series/pwr.h" 41 | 42 | 43 | /** Power interface register map. */ 44 | typedef struct pwr_reg_map { 45 | __IO uint32 CR; /**< Control register */ 46 | __IO uint32 CSR; /**< Control and status register */ 47 | } pwr_reg_map; 48 | 49 | /** Power peripheral register map base pointer. */ 50 | #define PWR ((struct pwr_reg_map*)0x40007000) 51 | 52 | /* 53 | * Register bit definitions 54 | */ 55 | 56 | /* Control register */ 57 | 58 | /** Disable backup domain write protection bit */ 59 | #define PWR_CR_DBP_BIT 8 60 | /** Power voltage detector enable bit */ 61 | #define PWR_CR_PVDE_BIT 4 62 | /** Clear standby flag bit */ 63 | #define PWR_CR_CSBF_BIT 3 64 | /** Clear wakeup flag bit */ 65 | #define PWR_CR_CWUF_BIT 2 66 | /** Power down deepsleep bit */ 67 | #define PWR_CR_PDDS_BIT 1 68 | /** Low-power deepsleep bit */ 69 | #define PWR_CR_LPDS_BIT 0 70 | 71 | /** Disable backup domain write protection */ 72 | #define PWR_CR_DBP (1U << PWR_CR_DBP_BIT) 73 | /** Power voltage detector (PVD) level selection */ 74 | #define PWR_CR_PLS (0x7 << 5) 75 | /** Power voltage detector enable */ 76 | #define PWR_CR_PVDE (1U << PWR_CR_PVDE_BIT) 77 | /** Clear standby flag */ 78 | #define PWR_CR_CSBF (1U << PWR_CR_CSBF_BIT) 79 | /** Clear wakeup flag */ 80 | #define PWR_CR_CWUF (1U << PWR_CR_CWUF_BIT) 81 | /** Power down deepsleep */ 82 | #define PWR_CR_PDDS (1U << PWR_CR_PDDS_BIT) 83 | /** Low-power deepsleep */ 84 | #define PWR_CR_LPDS (1U << PWR_CR_LPDS_BIT) 85 | 86 | /* Control and status register */ 87 | 88 | /** Enable wakeup pin bit */ 89 | #define PWR_CSR_EWUP_BIT 8 90 | /** PVD output bit */ 91 | #define PWR_CSR_PVDO_BIT 2 92 | /** Standby flag bit */ 93 | #define PWR_CSR_SBF_BIT 1 94 | /** Wakeup flag bit */ 95 | #define PWR_CSR_WUF_BIT 0 96 | 97 | /** Enable wakeup pin */ 98 | #define PWR_CSR_EWUP (1U << PWR_CSR_EWUP_BIT) 99 | /** PVD output */ 100 | #define PWR_CSR_PVDO (1U << PWR_CSR_PVDO_BIT) 101 | /** Standby flag */ 102 | #define PWR_CSR_SBF (1U << PWR_CSR_SBF_BIT) 103 | /** Wakeup flag */ 104 | #define PWR_CSR_WUF (1U << PWR_CSR_WUF_BIT) 105 | 106 | /* 107 | * Convenience functions 108 | */ 109 | 110 | void pwr_init(void); 111 | 112 | #ifdef __cplusplus 113 | } 114 | #endif 115 | 116 | #endif 117 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/rcc_private.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2011 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /* 28 | * RCC private header. 29 | */ 30 | 31 | #ifndef _LIBMAPLE_PRIVATE_RCC_H_ 32 | #define _LIBMAPLE_PRIVATE_RCC_H_ 33 | 34 | #include "bitband.h" 35 | 36 | struct rcc_dev_info { 37 | const rcc_clk_domain clk_domain; 38 | const uint8 line_num; 39 | }; 40 | 41 | extern void rcc_do_clk_enable(__IO uint32** enable_regs, rcc_clk_id id); 42 | extern void rcc_do_reset_dev(__IO uint32** reset_regs, rcc_clk_id id); 43 | extern void rcc_do_set_prescaler(const uint32 *masks, rcc_prescaler prescaler, uint32 divider); 44 | extern void rcc_do_clk_disable(__IO uint32** enable_regs, rcc_clk_id id); 45 | 46 | extern const struct rcc_dev_info rcc_dev_table[]; 47 | 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/series/pwr.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2012 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/stm32f1/include/series/pwr.h 29 | * @author Marti Bolivar 30 | * @brief STM32F1 Power control (PWR) support. 31 | */ 32 | 33 | #ifndef _LIBMAPLE_STM32F1_PWR_H_ 34 | #define _LIBMAPLE_STM32F1_PWR_H_ 35 | 36 | /* 37 | * Register bit definitions 38 | */ 39 | 40 | /* Control register */ 41 | 42 | /* PVD level selection */ 43 | #define PWR_CR_PLS_2_2V (0x0 << 5) 44 | #define PWR_CR_PLS_2_3V (0x1 << 5) 45 | #define PWR_CR_PLS_2_4V (0x2 << 5) 46 | #define PWR_CR_PLS_2_5V (0x3 << 5) 47 | #define PWR_CR_PLS_2_6V (0x4 << 5) 48 | #define PWR_CR_PLS_2_7V (0x5 << 5) 49 | #define PWR_CR_PLS_2_8V (0x6 << 5) 50 | #define PWR_CR_PLS_2_9V (0x7 << 5) 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/stm32_private.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2012 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | #ifndef _LIBMAPLE_STM32_PRIVATE_H_ 28 | #define _LIBMAPLE_STM32_PRIVATE_H_ 29 | 30 | typedef enum stm32_mem_block_purpose { 31 | STM32_BLOCK_CODE, 32 | STM32_BLOCK_SRAM, 33 | STM32_BLOCK_PERIPH, 34 | STM32_BLOCK_FSMC_1_2, 35 | STM32_BLOCK_FSMC_3_4, 36 | STM32_BLOCK_FSMC_REG, 37 | STM32_BLOCK_UNUSED, 38 | STM32_BLOCK_CORTEX_INTERNAL, 39 | } stm32_mem_block_purpose; 40 | 41 | static inline stm32_mem_block_purpose stm32_block_purpose(void *addr) { 42 | return (stm32_mem_block_purpose)((unsigned)addr >> 29); 43 | } 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/syscfg.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2012 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/syscfg.h 29 | * @brief System configuration controller (SYSCFG) 30 | * 31 | * Availability: STM32F2, STM32F4. 32 | */ 33 | 34 | #ifndef _LIBMAPLE_SYSCFG_H_ 35 | #define _LIBMAPLE_SYSCFG_H_ 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | #include 42 | 43 | /* 44 | * Register map and base pointer 45 | */ 46 | 47 | /** 48 | * @brief SYSCFG register map type. 49 | */ 50 | typedef struct syscfg_reg_map { 51 | __IO uint32 MEMRMP; /**< Memory remap register */ 52 | __IO uint32 PMC; /**< Peripheral mode configuration */ 53 | __IO uint32 EXTICR[4]; /**< External interrupt configuration registers */ 54 | const uint32 RESERVED1; 55 | const uint32 RESERVED2; 56 | __IO uint32 CMPCR; /**< Compensation cell control register */ 57 | } syscfg_reg_map; 58 | 59 | /** SYSCFG register map base pointer */ 60 | #define SYSCFG_BASE ((struct syscfg_reg_map*)0x40013800) 61 | 62 | /* 63 | * Register bit definitions 64 | */ 65 | 66 | /* Memory remap register */ 67 | 68 | #define SYSCFG_MEMRMP_MEM_MODE 0x3 69 | #define SYSCFG_MEMRMP_MEM_MODE_FLASH 0x0 70 | #define SYSCFG_MEMRMP_MEM_MODE_SYS_FLASH 0x1 71 | #define SYSCFG_MEMRMP_MEM_MODE_FSMC_1 0x2 72 | #define SYSCFG_MEMRMP_MEM_MODE_EMB_SRAM 0x3 73 | 74 | /* Peripheral mode configuration register */ 75 | 76 | #define SYSCFG_PMC_MII_RMII_SEL_BIT 23 77 | 78 | #define SYSCFG_PMC_MII_RMII_SEL (1U << SYSCFG_PMC_MII_RMII_SEL_BIT) 79 | #define SYSCFG_PMC_MII_RMII_SEL_MII (0U << SYSCFG_PMC_MII_RMII_SEL_BIT) 80 | #define SYSCFG_PMC_MII_RMII_SEL_RMII (1U << SYSCFG_PMC_MII_RMII_SEL_BIT) 81 | 82 | /* External interrupt configuration register 1 */ 83 | 84 | #define SYSCFG_EXTICR1_EXTI0 0xF 85 | #define SYSCFG_EXTICR1_EXTI1 0xF0 86 | #define SYSCFG_EXTICR1_EXTI2 0xF00 87 | #define SYSCFG_EXTICR1_EXTI3 0xF000 88 | 89 | /* External interrupt configuration register 2 */ 90 | 91 | #define SYSCFG_EXTICR2_EXTI4 0xF 92 | #define SYSCFG_EXTICR2_EXTI5 0xF0 93 | #define SYSCFG_EXTICR2_EXTI6 0xF00 94 | #define SYSCFG_EXTICR2_EXTI7 0xF000 95 | 96 | /* External interrupt configuration register 3 */ 97 | 98 | #define SYSCFG_EXTICR3_EXTI8 0xF 99 | #define SYSCFG_EXTICR3_EXTI9 0xF0 100 | #define SYSCFG_EXTICR3_EXTI10 0xF00 101 | #define SYSCFG_EXTICR3_EXTI11 0xF000 102 | 103 | /* External interrupt configuration register 4 */ 104 | 105 | #define SYSCFG_EXTICR4_EXTI12 0xF 106 | #define SYSCFG_EXTICR4_EXTI13 0xF0 107 | #define SYSCFG_EXTICR4_EXTI14 0xF00 108 | #define SYSCFG_EXTICR4_EXTI15 0xF000 109 | 110 | /* Compensation cell control register */ 111 | 112 | #define SYSCFG_CMPCR_READY_BIT 8 113 | #define SYSCFG_CMPCR_CMP_PD_BIT 0 114 | 115 | #define SYSCFG_CMPCR_READY (1U << SYSCFG_CMPCR_READY_BIT) 116 | #define SYSCFG_CMPCR_CMP_PD (1U << SYSCFG_CMPCR_CMP_PD_BIT) 117 | #define SYSCFG_CMPCR_CMP_PD_PDWN (0U << SYSCFG_CMPCR_CMP_PD_BIT) 118 | #define SYSCFG_CMPCR_CMP_PD_ENABLE (1U << SYSCFG_CMPCR_CMP_PD_BIT) 119 | 120 | /* 121 | * Routines 122 | */ 123 | 124 | void syscfg_init(void); 125 | 126 | void syscfg_enable_io_compensation(void); 127 | void syscfg_disable_io_compensation(void); 128 | 129 | /** 130 | * @brief System memory mode 131 | * These values specify what memory to map to address 0x00000000. 132 | * @see syscfg_set_mem_mode 133 | */ 134 | typedef enum syscfg_mem_mode { 135 | /** Main flash memory is mapped at 0x0. */ 136 | SYCFG_MEM_MODE_FLASH = SYSCFG_MEMRMP_MEM_MODE_FLASH, 137 | /** System flash (i.e. ST's baked-in bootloader) is mapped at 0x0. */ 138 | SYCFG_MEM_MODE_SYSTEM_FLASH = SYSCFG_MEMRMP_MEM_MODE_SYS_FLASH, 139 | /** FSMC bank 1 (NOR/PSRAM 1 and 2) is mapped at 0x0. */ 140 | SYCFG_MEM_MODE_FSMC_BANK_1 = SYSCFG_MEMRMP_MEM_MODE_FSMC_1, 141 | /** Embedded SRAM (i.e., not backup SRAM) is mapped at 0x0. */ 142 | SYCFG_MEM_MODE_SRAM = SYSCFG_MEMRMP_MEM_MODE_EMB_SRAM, 143 | } syscfg_mem_mode; 144 | 145 | void syscfg_set_mem_mode(syscfg_mem_mode); 146 | 147 | #ifdef __cplusplus 148 | } 149 | #endif 150 | 151 | #endif 152 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/systick.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * Copyright (c) 2010, 2011 LeafLabs, LLC. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/systick.c 30 | * @brief System timer (SysTick). 31 | */ 32 | 33 | #include "systick.h" 34 | #include "delay.h" 35 | 36 | volatile uint32 systick_uptime_millis; 37 | void (*systick_user_callback)(void); 38 | 39 | 40 | 41 | /* 42 | * SysTick ISR 43 | */ 44 | 45 | __weak void __exc_systick(void) { 46 | systick_uptime_millis++; 47 | if (systick_user_callback) { 48 | systick_user_callback(); 49 | } 50 | } 51 | //----------------------------------------------------------------------------- 52 | __weak void yield() {} 53 | //----------------------------------------------------------------------------- 54 | void delay(uint32 ms) 55 | { 56 | uint32 start = micros(); 57 | while (ms > 0) 58 | { 59 | yield(); 60 | while ( (ms > 0) && ((micros() - start) >= 1000) ) 61 | { 62 | ms--; 63 | start += 1000; 64 | } 65 | } 66 | } 67 | 68 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/util.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/util.h 29 | * @brief Miscellaneous utility macros and procedures. 30 | */ 31 | 32 | #ifndef _LIBMAPLE_UTIL_H_ 33 | #define _LIBMAPLE_UTIL_H_ 34 | 35 | #ifdef __cplusplus 36 | extern "C"{ 37 | #endif 38 | 39 | #include "libmaple_types.h" 40 | 41 | 42 | /* 43 | * Bit manipulation 44 | */ 45 | 46 | /** 1UL shifted left by 'shift' */ 47 | #define BIT(shift) (1UL << (shift)) 48 | /** 'Mask' shifted left by 'shift' */ 49 | #define BIT_MASK_SHIFT(mask, shift) ((mask) << (shift)) 50 | /** Bits m to n of x */ 51 | #define GET_BITS(x, m, n) ((((uint32)x) << (31 - (n))) >> ((31 - (n)) + (m))) 52 | /** True iff v is a power of two (1, 2, 4, 8, ...) */ 53 | #define IS_POWER_OF_TWO(v) ((v) && !((v) & ((v) - 1))) 54 | 55 | 56 | 57 | /* 58 | * Failure routines 59 | */ 60 | 61 | void __error(void); 62 | void _fail(const char*, int, const char*); 63 | void throb(void); 64 | 65 | /* 66 | * Asserts and debug levels 67 | */ 68 | 69 | #define DEBUG_NONE 0 70 | #define DEBUG_FAULT 1 71 | #define DEBUG_ALL 2 72 | 73 | /** 74 | * \def DEBUG_LEVEL 75 | * 76 | * Controls the level of assertion checking. 77 | * 78 | * The higher the debug level, the more assertions will be compiled 79 | * in. This increases the amount of debugging information, but slows 80 | * down (and increases the size of) the binary. 81 | * 82 | * The debug levels, from lowest to highest, are DEBUG_NONE, 83 | * DEBUG_FAULT, and DEBUG_ALL. The default level is DEBUG_ALL. 84 | */ 85 | 86 | #ifndef DEBUG_LEVEL 87 | #define DEBUG_LEVEL DEBUG_ALL 88 | #endif 89 | 90 | #if DEBUG_LEVEL >= DEBUG_ALL 91 | #define ASSERT(exp) \ 92 | if (exp) { \ 93 | } else { \ 94 | _fail(__FILE__, __LINE__, #exp); \ 95 | } 96 | #else 97 | #define ASSERT(exp) (void)((0)) 98 | #endif 99 | 100 | #if DEBUG_LEVEL >= DEBUG_FAULT 101 | #define ASSERT_FAULT(exp) \ 102 | if (exp) { \ 103 | } else { \ 104 | _fail(__FILE__, __LINE__, #exp); \ 105 | } 106 | #else 107 | #define ASSERT_FAULT(exp) (void)((0)) 108 | #endif 109 | 110 | #ifdef __cplusplus 111 | } // extern "C" 112 | #endif 113 | 114 | #endif 115 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/util/atomic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This is port of Dean Camera's ATOMIC_BLOCK macros for AVR to ARM Cortex M3 3 | * v1.0 4 | * Mark Pendrith, Nov 27, 2012. 5 | * 6 | * From Mark: 7 | * >When I ported the macros I emailed Dean to ask what attribution would be 8 | * >appropriate, and here is his response: 9 | * > 10 | * >>Mark, 11 | * >>I think it's great that you've ported the macros; consider them 12 | * >>public domain, to do with whatever you wish. I hope you find them useful. 13 | * >> 14 | * >>Cheers! 15 | * >>- Dean 16 | */ 17 | 18 | #ifndef _CORTEX_M3_ATOMIC_H_ 19 | #define _CORTEX_M3_ATOMIC_H_ 20 | 21 | static __inline__ uint32_t __get_primask(void) \ 22 | { uint32_t primask = 0; \ 23 | __asm__ volatile ("MRS %[result], PRIMASK\n\t":[result]"=r"(primask)::); \ 24 | return primask; } // returns 0 if interrupts enabled, 1 if disabled 25 | 26 | static __inline__ void __set_primask(uint32_t setval) \ 27 | { __asm__ volatile ("MSR PRIMASK, %[value]\n\t""dmb\n\t""dsb\n\t""isb\n\t"::[value]"r"(setval):); \ 28 | __asm__ volatile ("" ::: "memory");} 29 | 30 | static __inline__ uint32_t __iSeiRetVal(void) \ 31 | { __asm__ volatile ("CPSIE i\n\t""dmb\n\t""dsb\n\t""isb\n\t"); \ 32 | __asm__ volatile ("" ::: "memory"); return 1; } 33 | 34 | static __inline__ uint32_t __iCliRetVal(void) \ 35 | { __asm__ volatile ("CPSID i\n\t""dmb\n\t""dsb\n\t""isb\n\t"); \ 36 | __asm__ volatile ("" ::: "memory"); return 1; } 37 | 38 | static __inline__ void __iSeiParam(const uint32_t *__s) \ 39 | { __asm__ volatile ("CPSIE i\n\t""dmb\n\t""dsb\n\t""isb\n\t"); \ 40 | __asm__ volatile ("" ::: "memory"); (void)__s; } 41 | 42 | static __inline__ void __iCliParam(const uint32_t *__s) \ 43 | { __asm__ volatile ("CPSID i\n\t""dmb\n\t""dsb\n\t""isb\n\t"); \ 44 | __asm__ volatile ("" ::: "memory"); (void)__s; } 45 | 46 | static __inline__ void __iRestore(const uint32_t *__s) \ 47 | { __set_primask(*__s); __asm__ volatile ("dmb\n\t""dsb\n\t""isb\n\t"); \ 48 | __asm__ volatile ("" ::: "memory"); } 49 | 50 | 51 | #define ATOMIC_BLOCK(type) \ 52 | for ( type, __ToDo = __iCliRetVal(); __ToDo ; __ToDo = 0 ) 53 | 54 | #define ATOMIC_RESTORESTATE \ 55 | uint32_t primask_save __attribute__((__cleanup__(__iRestore))) = __get_primask() 56 | 57 | #define ATOMIC_FORCEON \ 58 | uint32_t primask_save __attribute__((__cleanup__(__iSeiParam))) = 0 59 | 60 | #define NONATOMIC_BLOCK(type) \ 61 | for ( type, __ToDo = __iSeiRetVal(); __ToDo ; __ToDo = 0 ) 62 | 63 | #define NONATOMIC_RESTORESTATE \ 64 | uint32_t primask_save __attribute__((__cleanup__(__iRestore))) = __get_primask() 65 | 66 | #define NONATOMIC_FORCEOFF \ 67 | uint32_t primask_save __attribute__((__cleanup__(__iCliParam))) = 0 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/wirish_time.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file wirish/include/wirish/wirish_time.h 29 | * @brief Timing and delay functions. 30 | */ 31 | 32 | #ifndef _WIRISH_WIRISH_TIME_H_ 33 | #define _WIRISH_WIRISH_TIME_H_ 34 | 35 | #include 36 | #include 37 | 38 | #include 39 | 40 | /** 41 | * Returns time (in milliseconds) since the beginning of program 42 | * execution. On overflow, restarts at 0. 43 | * @see micros() 44 | */ 45 | static inline uint32 millis(void) { 46 | return systick_uptime(); 47 | } 48 | 49 | /** 50 | * Returns time (in microseconds) since the beginning of program 51 | * execution. On overflow, restarts at 0. 52 | * @see millis() 53 | */ 54 | static inline uint32 micros(void) { 55 | uint32 ms; 56 | uint32 cycle_cnt; 57 | 58 | do { 59 | ms = millis(); 60 | cycle_cnt = systick_get_count(); 61 | asm volatile("nop"); //allow interrupt to fire 62 | asm volatile("nop"); 63 | } while (ms != millis()); 64 | 65 | #define US_PER_MS 1000 66 | /* SYSTICK_RELOAD_VAL is 1 less than the number of cycles it 67 | * actually takes to complete a SysTick reload */ 68 | return ((ms * US_PER_MS) + 69 | (SYSTICK_RELOAD_VAL + 1 - cycle_cnt) / CYCLES_PER_MICROSECOND); 70 | #undef US_PER_MS 71 | } 72 | 73 | /** 74 | * Delay for at least the given number of milliseconds. 75 | * 76 | * Interrupts, etc. may cause the actual number of milliseconds to 77 | * exceed ms. However, this function will return no less than ms 78 | * milliseconds from the time it is called. 79 | * 80 | * @param ms the number of milliseconds to delay. 81 | * @see delayMicroseconds() 82 | */ 83 | void delay(unsigned long ms); 84 | 85 | /** 86 | * Delay for at least the given number of microseconds. 87 | * 88 | * Interrupts, etc. may cause the actual number of microseconds to 89 | * exceed us. However, this function will return no less than us 90 | * microseconds from the time it is called. 91 | * 92 | * @param us the number of microseconds to delay. 93 | * @see delay() 94 | */ 95 | void delayMicroseconds(uint32 us); 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/libmaple/wirish_types.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2011 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file wirish/include/wirish/wirish_types.h 29 | * @author Marti Bolivar 30 | * @brief Wirish library type definitions. 31 | */ 32 | 33 | #ifndef _WIRISH_WIRISH_TYPES_H_ 34 | #define _WIRISH_WIRISH_TYPES_H_ 35 | 36 | #include "gpio.h" 37 | #include "timer.h" 38 | 39 | 40 | /** 41 | * Variable attribute, instructs the linker to place the marked 42 | * variable in Flash instead of RAM. */ 43 | #define __FLASH__ __attr_flash 44 | 45 | typedef bool boolean; 46 | typedef uint8 byte; 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/system.c: -------------------------------------------------------------------------------- 1 | /* 2 | * system.c 3 | * 4 | * Created on: Apr 25, 2020 5 | * Author: stevestrong 6 | */ 7 | 8 | 9 | #include "flash.h" 10 | #include "rcc.h" 11 | #include "gpio.h" 12 | #include "board.h" 13 | 14 | 15 | // Allow boards to provide a PLL multiplier. This is useful for 16 | // e.g. STM32F100 value line MCUs, which use slower multipliers. 17 | // (We're leaving the default to RCC_PLLMUL_9 for now, since that 18 | // works for F103 performance line MCUs, which is all that LeafLabs 19 | // currently officially supports). 20 | #ifndef BOARD_RCC_PLLMUL 21 | #if !USE_HSI_CLOCK 22 | #if F_CPU==128000000 23 | #define BOARD_RCC_PLLMUL RCC_PLLMUL_16 24 | #elif F_CPU==72000000 25 | #define BOARD_RCC_PLLMUL RCC_PLLMUL_9 26 | #elif F_CPU==48000000 27 | #define BOARD_RCC_PLLMUL RCC_PLLMUL_6 28 | #elif F_CPU==16000000 29 | #define BOARD_RCC_PLLMUL RCC_PLLMUL_2 30 | #endif 31 | #else 32 | #define BOARD_RCC_PLLMUL RCC_PLLMUL_16 33 | #endif 34 | #endif 35 | 36 | 37 | static stm32f1_rcc_pll_data pll_data = {BOARD_RCC_PLLMUL}; 38 | #if !USE_HSI_CLOCK 39 | __weak rcc_pll_cfg w_board_pll_cfg = {RCC_PLLSRC_HSE, &pll_data}; 40 | #else 41 | __weak rcc_pll_cfg w_board_pll_cfg = {RCC_PLLSRC_HSI_DIV_2, &pll_data}; 42 | #endif 43 | 44 | void board_setup_clock_prescalers(void) { 45 | rcc_set_prescaler(RCC_PRESCALER_AHB, RCC_AHB_SYSCLK_DIV_1); 46 | rcc_set_prescaler(RCC_PRESCALER_APB1, RCC_APB1_HCLK_DIV_2); 47 | rcc_set_prescaler(RCC_PRESCALER_APB2, RCC_APB2_HCLK_DIV_1); 48 | rcc_clk_disable(RCC_USB); 49 | #if F_CPU == 72000000 50 | rcc_set_prescaler(RCC_PRESCALER_USB, RCC_USB_SYSCLK_DIV_1_5); 51 | #elif F_CPU == 48000000 52 | rcc_set_prescaler(RCC_PRESCALER_USB, RCC_USB_SYSCLK_DIV_1); 53 | #endif 54 | } 55 | 56 | //----------------------------------------------------------------------------- 57 | void Setup_flash(void) 58 | { 59 | // Turn on as many Flash "go faster" features as 60 | // possible. flash_enable_features() just ignores any flags it 61 | // can't support. 62 | flash_enable_features(FLASH_PREFETCH | FLASH_ICACHE | FLASH_DCACHE); 63 | // Configure the wait states, assuming we're operating at "close 64 | // enough" to 3.3V. 65 | flash_set_latency(FLASH_SAFE_WAIT_STATES); 66 | } 67 | //----------------------------------------------------------------------------- 68 | void Setup_clocks(void) 69 | { 70 | #if 1 71 | // Turn on HSI. We'll switch to and run off of this while we're 72 | // setting up the main PLL. 73 | rcc_turn_on_clk(RCC_CLK_HSI); 74 | 75 | // Turn off and reset the clock subsystems we'll be using, as well 76 | // as the clock security subsystem (CSS). Note that resetting CFGR 77 | // to its default value of 0 implies a switch to HSI for SYSCLK. 78 | RCC->CFGR = 0x00000000; 79 | rcc_disable_css(); 80 | rcc_turn_off_clk(RCC_CLK_PLL); 81 | rcc_turn_off_clk(RCC_CLK_HSE); 82 | // Clear clock readiness interrupt flags and turn off clock 83 | // readiness interrupts. 84 | RCC->CIR = 0x00000000; 85 | #if !USE_HSI_CLOCK 86 | // Enable HSE, and wait until it's ready. 87 | rcc_turn_on_clk(RCC_CLK_HSE); 88 | while (!rcc_is_clk_ready(RCC_CLK_HSE)) 89 | ; 90 | #endif 91 | // Configure AHBx, APBx, etc. prescalers and the main PLL. 92 | board_setup_clock_prescalers(); 93 | rcc_configure_pll(&w_board_pll_cfg); 94 | 95 | // Enable the PLL, and wait until it's ready. 96 | rcc_turn_on_clk(RCC_CLK_PLL); 97 | while(!rcc_is_clk_ready(RCC_CLK_PLL)) 98 | ; 99 | 100 | // Finally, switch to the now-ready PLL as the main clock source. 101 | rcc_switch_sysclk(RCC_CLKSRC_PLL); 102 | #else 103 | /* Enable HSE */ 104 | SET_BIT(RCC->CR, RCC_CR_HSEON); 105 | 106 | /* Wait until HSE is ready */ 107 | while (READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0) { 108 | ; 109 | } 110 | 111 | /* Enable Prefetch Buffer & set Flash access to 2 wait states */ 112 | // SET_BIT(FLASH->ACR, FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY_2); 113 | 114 | /* SYSCLK = PCLK2 = HCLK */ 115 | /* PCLK1 = HCLK / 2 */ 116 | /* PLLCLK = HSE * 9 = 72 MHz */ 117 | SET_BIT(RCC->CFGR, 118 | RCC_CFGR_HPRE_DIV1 | RCC_CFGR_PPRE2_DIV1 | RCC_CFGR_PPRE1_DIV2 | 119 | RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9); 120 | 121 | /* Enable PLL */ 122 | SET_BIT(RCC->CR, RCC_CR_PLLON); 123 | 124 | /* Wait until PLL is ready */ 125 | while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0) { 126 | ; 127 | } 128 | 129 | /* Select PLL as system clock source */ 130 | SET_BIT(RCC->CFGR, RCC_CFGR_SW_PLL); 131 | 132 | /* Wait until PLL is used as system clock source */ 133 | while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS_1) == 0) { 134 | ; 135 | } 136 | #endif 137 | } 138 | 139 | 140 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/usb_def.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stevstrong/STM32-CDC-Bootloader/d53886de2380a4812abd720e6777ede4b293cc57/F1/eclipse_project/src/usb_def.h -------------------------------------------------------------------------------- /F1/eclipse_project/src/usb_desc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * usb_desc.h 3 | * 4 | * Created on: Apr 6, 2020 5 | * Author: Zo 6 | */ 7 | 8 | #ifndef USB_DESC_H_ 9 | #define USB_DESC_H_ 10 | 11 | 12 | #if 1 13 | #define USB_VID 0x1EAF // 0x0416 /* Vendor ID (von RealTek) */ 14 | #define USB_PID 0x0002 // 0x5011 /* Product ID */ 15 | #else 16 | #define USB_VID 0xDEAD 17 | #define USB_PID 0xBEEF 18 | #endif 19 | 20 | // assignment of the USB EP numbers - bEndpointAddress 21 | enum { EP_CTRL, EP_DATA, EP_COMM, EP_MAX }; 22 | 23 | #define NUM_IFACES 2 // COMM + DATA 24 | 25 | #define EP_CTRL_ADDR_IN USB_EP_ADDR_IN(EP_CTRL) 26 | #define EP_CTRL_ADDR_OUT USB_EP_ADDR_OUT(EP_CTRL) 27 | #define EP_COMM_ADDR_IN USB_EP_ADDR_IN(EP_COMM) 28 | #define EP_COMM_ADDR_OUT USB_EP_ADDR_OUT(EP_COMM) 29 | #define EP_DATA_ADDR_IN USB_EP_ADDR_IN(EP_DATA) 30 | #define EP_DATA_ADDR_OUT USB_EP_ADDR_OUT(EP_DATA) 31 | 32 | 33 | #endif /* USB_DESC_H_ */ 34 | -------------------------------------------------------------------------------- /F1/eclipse_project/src/usb_func.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef USB_FUNC_H 3 | #define USB_FUNC_H 4 | 5 | #include 6 | #include "usb_def.h" 7 | #include "usb_desc.h" 8 | #include "cdc.h" 9 | #include "gpio.h" 10 | #include "flash.h" 11 | 12 | /******* Struktur des Setup-Paketes *****/ 13 | typedef struct 14 | { 15 | uint8_t bmRequestType; /* siehe oben */ 16 | uint8_t bRequest; /* siehe Request-Tafel in USB-Doku */ 17 | uint16_t wValue; /* je nach Request */ 18 | uint16_t wIndex; /* je nach Request */ 19 | uint16_t wLength; /* Anzahl Bytes, wenn Data-Stage vorhanden ist */ 20 | } setupPaket_t; 21 | 22 | /******* Struktur des Kommando- und Org-Datenblockes *******************/ 23 | typedef struct 24 | { 25 | setupPaket_t setupPacket; /* das jeweils letzte Setup-Paket */ 26 | int transferLen; /* noch zum Host zu sendende Anzahl Bytes */ 27 | int packetLen; /* wie lang das Paket zum Host sein darf */ 28 | uint8_t* transferPtr; /* zeigt auf die noch zu sendenden Bytes */ 29 | 30 | bool remoteWakeup; 31 | bool selfPowered; 32 | int configuration; 33 | } command_t; 34 | 35 | /* Line coding structure 36 | 0-3 BaudRate Data terminal rate (baudrate), in bits per second 37 | 4 bCharFormat Stop bits: 0 - 1 Stop bit, 1 - 1.5 Stop bits, 2 - 2 Stop bits 38 | 5 bParityType Parity: 0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space 39 | 6 bDataBits Data bits: 5, 6, 7, 8, 16 40 | */ 41 | 42 | #define lineCoding_t usb_cdc_line_coding 43 | 44 | extern command_t CMD; 45 | extern lineCoding_t lineCoding; 46 | extern uint16_t Dtr_Rts; 47 | extern const uint8_t ZERO; 48 | 49 | extern void Class_Start(void); 50 | extern void EnableUsbIRQ(); 51 | extern void Setup_flash(); 52 | extern void Setup_clocks(); 53 | 54 | extern int DataBeginTransmit(); 55 | extern void DataBeginReceive(); // called when Rx data can be processed again 56 | extern int ReadControlBlock(uint8_t* pBuffer, int maxlen); 57 | extern int SendData(int ep, uint8_t* pBuffer, int count); 58 | //-------------------------------------------------------------------------- 59 | static inline void ACK(void) 60 | { 61 | SendData(EP_CTRL, (uint8_t*) &ZERO, 0); 62 | trace("-ACK\n"); 63 | } 64 | 65 | //----------------------------------------------------------------------------- 66 | static inline void USB_SetAddress(uint8_t adr) 67 | { 68 | strace(buf,"setAddr adr=%i\n",adr); 69 | USB_DADDR = 0x80 | adr; 70 | } 71 | 72 | /* Request-Typ im Setup-Packet testen (Standard, Class, Vendor) */ 73 | //-------------------------------------------------------------------------- 74 | static inline bool IsStandardRequest(void) 75 | { 76 | return (CMD.setupPacket.bmRequestType & 0x60) == 0; 77 | } 78 | 79 | //-------------------------------------------------------------------------- 80 | static inline bool IsClassRequest(void) 81 | { 82 | return (CMD.setupPacket.bmRequestType & 0x60) == 0x20; 83 | } 84 | 85 | //-------------------------------------------------------------------------- 86 | static inline bool IsVendorRequest(void) 87 | { 88 | return (CMD.setupPacket.bmRequestType & 0x60) == 0x40; 89 | } 90 | 91 | 92 | #define LED_ON gpio_write_pin(LED_BUILTIN, 0) 93 | #define LED_OFF gpio_write_pin(LED_BUILTIN, 1) 94 | 95 | //----------------------------------------------------------------------------- 96 | #define FLASH_BASE (0x08000000) 97 | #define SRAM_BASE (0x20000000) 98 | // Bootloader size 99 | #define BOOTLOADER_SIZE (4 * 1024) 100 | 101 | // SRAM size 102 | #define SRAM_SIZE (20 * 1024) 103 | 104 | // SRAM end (bottom of stack) 105 | #define SRAM_END (SRAM_BASE + SRAM_SIZE) 106 | 107 | // CDC Bootloader takes 4 kb flash. 108 | #define USER_PROGRAM (FLASH_BASE + BOOTLOADER_SIZE) 109 | //----------------------------------------------------------------------------- 110 | typedef union cmd_t { 111 | uint8_t data[8]; 112 | struct { 113 | uint16_t start; // 0x41BE 114 | uint8_t id; 115 | uint8_t page; 116 | uint16_t data_len; 117 | uint16_t crc; 118 | }; 119 | } __attribute((packed)) cmd_t; 120 | extern cmd_t cmd; 121 | 122 | #define PAGE_SIZE 1024 123 | extern int Check_CRC(uint8_t * buff, int len); 124 | 125 | #define BAUD_RATE 230400 126 | 127 | typedef enum {BUF_EMPTY, BUF_RECEIVING, BUF_FULL, BUF_SENDING} buf_status_t; 128 | typedef enum { 129 | NO_ERROR, 130 | NO_SETUP, 131 | NO_FREE_BUFFER, 132 | DATA_OVERFLOW, 133 | DATA_UNDEFLOW, 134 | CMD_WRONG_LENGTH, 135 | CMD_WRONG_CRC, 136 | CMD_WRONG_ID 137 | } error_t; 138 | 139 | typedef struct buf_params_t { 140 | buf_status_t status; 141 | int wr_index; 142 | int len; 143 | } buf_params_t; 144 | extern buf_params_t buf_params[2]; 145 | 146 | extern int num_pages; // number of total pages to flash 147 | extern int crt_page, page_offset; 148 | extern int header_ok; 149 | //----------------------------------------------------------------------------- 150 | 151 | 152 | 153 | #endif // USB_FUNC_H 154 | -------------------------------------------------------------------------------- /F3/Readme.md: -------------------------------------------------------------------------------- 1 | ## How to use with Arduino IDE: 2 | 3 | Add to boards.txt: 4 | ``` 5 | generic_f303cc.menu.upload_method.CDCMethod=CDC bootloader 6 | generic_f303cc.menu.upload_method.CDCMethod.upload.protocol=cdc_upload 7 | generic_f303cc.menu.upload_method.CDCMethod.upload.tool=cdc_upload 8 | generic_f303cc.menu.upload_method.CDCMethod.build.vect_flags=-DUSER_ADDR_ROM=0x08001800 9 | generic_f303cc.menu.upload_method.CDCMethod.build.ldscript=ld/cdc_upload.ld 10 | ``` 11 | Add to platform.txt: 12 | ``` 13 | # cdc upload 14 | tools.cdc_upload.cmd=cdc_upload 15 | tools.cdc_upload.cmd.windows=cdc_upload.bat 16 | tools.cdc_upload.path.windows={runtime.hardware.path}/tools/win 17 | #tools.cdc_upload.path.macosx={runtime.hardware.path}/tools/macosx 18 | #tools.cdc_upload.path.linux={runtime.hardware.path}/tools/linux 19 | #tools.cdc_upload.path.linux64={runtime.hardware.path}/tools/linux64 20 | tools.cdc_upload.upload.params.verbose= 21 | tools.cdc_upload.upload.params.quiet= 22 | tools.cdc_upload.upload.pattern="{path}/{cmd}" "{build.path}/{build.project_name}.bin" {serial.port.file} 2 23 | ``` 24 | Copy to variant directory the linker file: 25 | - https://github.com/stevstrong/Arduino_STM32/blob/master/STM32F3/variants/generic_f303cc/ld/cdc_upload.ld 26 | 27 | Copy to tools/win directory the files: 28 | - https://github.com/stevstrong/Arduino_STM32/blob/master/tools/win/cdc_flasher.exe 29 | - https://github.com/stevstrong/Arduino_STM32/blob/master/tools/win/cdc_upload.bat 30 | 31 | -------------------------------------------------------------------------------- /F3/binary/STM32F3-CDC-Bootloader.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stevstrong/STM32-CDC-Bootloader/d53886de2380a4812abd720e6777ede4b293cc57/F3/binary/STM32F3-CDC-Bootloader.bin -------------------------------------------------------------------------------- /F3/eclipse_project/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | STM32F1-CDC-Bootloader 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 24 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 25 | 26 | 27 | -------------------------------------------------------------------------------- /F3/eclipse_project/CMSIS/core/arm_const_structs.h: -------------------------------------------------------------------------------- 1 | /* ---------------------------------------------------------------------- 2 | * Copyright (C) 2010-2014 ARM Limited. All rights reserved. 3 | * 4 | * $Date: 19. March 2015 5 | * $Revision: V.1.4.5 6 | * 7 | * Project: CMSIS DSP Library 8 | * Title: arm_const_structs.h 9 | * 10 | * Description: This file has constant structs that are initialized for 11 | * user convenience. For example, some can be given as 12 | * arguments to the arm_cfft_f32() function. 13 | * 14 | * Target Processor: Cortex-M4/Cortex-M3 15 | * 16 | * Redistribution and use in source and binary forms, with or without 17 | * modification, are permitted provided that the following conditions 18 | * are met: 19 | * - Redistributions of source code must retain the above copyright 20 | * notice, this list of conditions and the following disclaimer. 21 | * - Redistributions in binary form must reproduce the above copyright 22 | * notice, this list of conditions and the following disclaimer in 23 | * the documentation and/or other materials provided with the 24 | * distribution. 25 | * - Neither the name of ARM LIMITED nor the names of its contributors 26 | * may be used to endorse or promote products derived from this 27 | * software without specific prior written permission. 28 | * 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 32 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 33 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 34 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 35 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 36 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 37 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 39 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 40 | * POSSIBILITY OF SUCH DAMAGE. 41 | * -------------------------------------------------------------------- */ 42 | 43 | #ifndef _ARM_CONST_STRUCTS_H 44 | #define _ARM_CONST_STRUCTS_H 45 | 46 | #include "arm_math.h" 47 | #include "arm_common_tables.h" 48 | 49 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len16; 50 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len32; 51 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len64; 52 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len128; 53 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len256; 54 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len512; 55 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len1024; 56 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len2048; 57 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len4096; 58 | 59 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len16; 60 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len32; 61 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len64; 62 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len128; 63 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len256; 64 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len512; 65 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len1024; 66 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len2048; 67 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len4096; 68 | 69 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len16; 70 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len32; 71 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len64; 72 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len128; 73 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len256; 74 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len512; 75 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len1024; 76 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len2048; 77 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len4096; 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /F3/eclipse_project/CMSIS/core/core_cmFunc.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file core_cmFunc.h 3 | * @brief CMSIS Cortex-M Core Function Access Header File 4 | * @version V4.30 5 | * @date 20. October 2015 6 | ******************************************************************************/ 7 | /* Copyright (c) 2009 - 2015 ARM LIMITED 8 | 9 | All rights reserved. 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | - Redistributions of source code must retain the above copyright 13 | notice, this list of conditions and the following disclaimer. 14 | - Redistributions in binary form must reproduce the above copyright 15 | notice, this list of conditions and the following disclaimer in the 16 | documentation and/or other materials provided with the distribution. 17 | - Neither the name of ARM nor the names of its contributors may be used 18 | to endorse or promote products derived from this software without 19 | specific prior written permission. 20 | * 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE 25 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | POSSIBILITY OF SUCH DAMAGE. 32 | ---------------------------------------------------------------------------*/ 33 | 34 | 35 | #if defined ( __ICCARM__ ) 36 | #pragma system_include /* treat file as system include file for MISRA check */ 37 | #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 38 | #pragma clang system_header /* treat file as system include file */ 39 | #endif 40 | 41 | #ifndef __CORE_CMFUNC_H 42 | #define __CORE_CMFUNC_H 43 | 44 | 45 | /* ########################### Core Function Access ########################### */ 46 | /** \ingroup CMSIS_Core_FunctionInterface 47 | \defgroup CMSIS_Core_RegAccFunctions CMSIS Core Register Access Functions 48 | @{ 49 | */ 50 | 51 | /*------------------ RealView Compiler -----------------*/ 52 | #if defined ( __CC_ARM ) 53 | #include "cmsis_armcc.h" 54 | 55 | /*------------------ ARM Compiler V6 -------------------*/ 56 | #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 57 | #include "cmsis_armcc_V6.h" 58 | 59 | /*------------------ GNU Compiler ----------------------*/ 60 | #elif defined ( __GNUC__ ) 61 | #include "cmsis_gcc.h" 62 | 63 | /*------------------ ICC Compiler ----------------------*/ 64 | #elif defined ( __ICCARM__ ) 65 | #include 66 | 67 | /*------------------ TI CCS Compiler -------------------*/ 68 | #elif defined ( __TMS470__ ) 69 | #include 70 | 71 | /*------------------ TASKING Compiler ------------------*/ 72 | #elif defined ( __TASKING__ ) 73 | /* 74 | * The CMSIS functions have been implemented as intrinsics in the compiler. 75 | * Please use "carm -?i" to get an up to date list of all intrinsics, 76 | * Including the CMSIS ones. 77 | */ 78 | 79 | /*------------------ COSMIC Compiler -------------------*/ 80 | #elif defined ( __CSMC__ ) 81 | #include 82 | 83 | #endif 84 | 85 | /*@} end of CMSIS_Core_RegAccFunctions */ 86 | 87 | #endif /* __CORE_CMFUNC_H */ 88 | -------------------------------------------------------------------------------- /F3/eclipse_project/CMSIS/core/core_cmInstr.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file core_cmInstr.h 3 | * @brief CMSIS Cortex-M Core Instruction Access Header File 4 | * @version V4.30 5 | * @date 20. October 2015 6 | ******************************************************************************/ 7 | /* Copyright (c) 2009 - 2015 ARM LIMITED 8 | 9 | All rights reserved. 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | - Redistributions of source code must retain the above copyright 13 | notice, this list of conditions and the following disclaimer. 14 | - Redistributions in binary form must reproduce the above copyright 15 | notice, this list of conditions and the following disclaimer in the 16 | documentation and/or other materials provided with the distribution. 17 | - Neither the name of ARM nor the names of its contributors may be used 18 | to endorse or promote products derived from this software without 19 | specific prior written permission. 20 | * 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE 25 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | POSSIBILITY OF SUCH DAMAGE. 32 | ---------------------------------------------------------------------------*/ 33 | 34 | 35 | #if defined ( __ICCARM__ ) 36 | #pragma system_include /* treat file as system include file for MISRA check */ 37 | #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 38 | #pragma clang system_header /* treat file as system include file */ 39 | #endif 40 | 41 | #ifndef __CORE_CMINSTR_H 42 | #define __CORE_CMINSTR_H 43 | 44 | 45 | /* ########################## Core Instruction Access ######################### */ 46 | /** \defgroup CMSIS_Core_InstructionInterface CMSIS Core Instruction Interface 47 | Access to dedicated instructions 48 | @{ 49 | */ 50 | 51 | /*------------------ RealView Compiler -----------------*/ 52 | #if defined ( __CC_ARM ) 53 | #include "cmsis_armcc.h" 54 | 55 | /*------------------ ARM Compiler V6 -------------------*/ 56 | #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 57 | #include "cmsis_armcc_V6.h" 58 | 59 | /*------------------ GNU Compiler ----------------------*/ 60 | #elif defined ( __GNUC__ ) 61 | #include "cmsis_gcc.h" 62 | 63 | /*------------------ ICC Compiler ----------------------*/ 64 | #elif defined ( __ICCARM__ ) 65 | #include 66 | 67 | /*------------------ TI CCS Compiler -------------------*/ 68 | #elif defined ( __TMS470__ ) 69 | #include 70 | 71 | /*------------------ TASKING Compiler ------------------*/ 72 | #elif defined ( __TASKING__ ) 73 | /* 74 | * The CMSIS functions have been implemented as intrinsics in the compiler. 75 | * Please use "carm -?i" to get an up to date list of all intrinsics, 76 | * Including the CMSIS ones. 77 | */ 78 | 79 | /*------------------ COSMIC Compiler -------------------*/ 80 | #elif defined ( __CSMC__ ) 81 | #include 82 | 83 | #endif 84 | 85 | /*@}*/ /* end of group CMSIS_Core_InstructionInterface */ 86 | 87 | #endif /* __CORE_CMINSTR_H */ 88 | -------------------------------------------------------------------------------- /F3/eclipse_project/CMSIS/core/core_cmSimd.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file core_cmSimd.h 3 | * @brief CMSIS Cortex-M SIMD Header File 4 | * @version V4.30 5 | * @date 20. October 2015 6 | ******************************************************************************/ 7 | /* Copyright (c) 2009 - 2015 ARM LIMITED 8 | 9 | All rights reserved. 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | - Redistributions of source code must retain the above copyright 13 | notice, this list of conditions and the following disclaimer. 14 | - Redistributions in binary form must reproduce the above copyright 15 | notice, this list of conditions and the following disclaimer in the 16 | documentation and/or other materials provided with the distribution. 17 | - Neither the name of ARM nor the names of its contributors may be used 18 | to endorse or promote products derived from this software without 19 | specific prior written permission. 20 | * 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE 25 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | POSSIBILITY OF SUCH DAMAGE. 32 | ---------------------------------------------------------------------------*/ 33 | 34 | 35 | #if defined ( __ICCARM__ ) 36 | #pragma system_include /* treat file as system include file for MISRA check */ 37 | #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 38 | #pragma clang system_header /* treat file as system include file */ 39 | #endif 40 | 41 | #ifndef __CORE_CMSIMD_H 42 | #define __CORE_CMSIMD_H 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | 49 | /* ################### Compiler specific Intrinsics ########################### */ 50 | /** \defgroup CMSIS_SIMD_intrinsics CMSIS SIMD Intrinsics 51 | Access to dedicated SIMD instructions 52 | @{ 53 | */ 54 | 55 | /*------------------ RealView Compiler -----------------*/ 56 | #if defined ( __CC_ARM ) 57 | #include "cmsis_armcc.h" 58 | 59 | /*------------------ ARM Compiler V6 -------------------*/ 60 | #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 61 | #include "cmsis_armcc_V6.h" 62 | 63 | /*------------------ GNU Compiler ----------------------*/ 64 | #elif defined ( __GNUC__ ) 65 | #include "cmsis_gcc.h" 66 | 67 | /*------------------ ICC Compiler ----------------------*/ 68 | #elif defined ( __ICCARM__ ) 69 | #include 70 | 71 | /*------------------ TI CCS Compiler -------------------*/ 72 | #elif defined ( __TMS470__ ) 73 | #include 74 | 75 | /*------------------ TASKING Compiler ------------------*/ 76 | #elif defined ( __TASKING__ ) 77 | /* 78 | * The CMSIS functions have been implemented as intrinsics in the compiler. 79 | * Please use "carm -?i" to get an up to date list of all intrinsics, 80 | * Including the CMSIS ones. 81 | */ 82 | 83 | /*------------------ COSMIC Compiler -------------------*/ 84 | #elif defined ( __CSMC__ ) 85 | #include 86 | 87 | #endif 88 | 89 | /*@} end of group CMSIS_SIMD_intrinsics */ 90 | 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif 95 | 96 | #endif /* __CORE_CMSIMD_H */ 97 | -------------------------------------------------------------------------------- /F3/eclipse_project/LinkerScript.ld: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stevstrong/STM32-CDC-Bootloader/d53886de2380a4812abd720e6777ede4b293cc57/F3/eclipse_project/LinkerScript.ld -------------------------------------------------------------------------------- /F3/eclipse_project/dbg/ring_buffer.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2011 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file ring_buffer.c 29 | * @brief Simple circular buffer 30 | * 31 | * This implementation is not thread-safe. In particular, none of 32 | * these functions is guaranteed re-entrant. 33 | */ 34 | 35 | #include "ring_buffer.h" 36 | 37 | 38 | // returns the count of used slots 39 | uint16_t rb_read_available(ring_buffer_t * rb) 40 | { 41 | register rb_ptrs_t temp = { rb->ptrs.both }; 42 | return (temp.head - temp.tail) & rb->CAPACITY; 43 | } 44 | 45 | // return the count of free slots 46 | uint16_t rb_write_available(ring_buffer_t * rb) 47 | { 48 | register rb_ptrs_t temp = { rb->ptrs.both }; 49 | return ((temp.tail - temp.head - 1) & rb->CAPACITY); // overall free space 50 | } 51 | 52 | // Adds an element to the end of the queue 53 | // does not check for free space. Use together with rb_write_available() 54 | //--------------------------------------------------------------------------- 55 | void rb_write(ring_buffer_t * rb, const uint8_t element) 56 | { 57 | register uint16_t temp_head = rb->ptrs.head; 58 | rb->buffer[temp_head++] = element; 59 | rb->ptrs.head = temp_head & rb->CAPACITY; 60 | } 61 | 62 | // Adds an element to the end of the queue 63 | // It does check for free space. returns false if no space available 64 | //--------------------------------------------------------------------------- 65 | int rb_write_safe(ring_buffer_t * rb, const uint8_t element) 66 | { 67 | register rb_ptrs_t temp = { rb->ptrs.both }; 68 | rb->buffer[temp.head++] = element; // safe to write because it does not overwrite active data 69 | temp.head &= rb->CAPACITY; 70 | if ( temp.head==temp.tail ) return false; 71 | rb->ptrs.head = temp.head; 72 | return true; 73 | } 74 | 75 | // Adds multiple elements to the end of the queue 76 | // checks for available free space 77 | //--------------------------------------------------------------------------- 78 | uint32 rb_write_safe_n(ring_buffer_t * rb, const uint8_t * buf, uint16_t len) 79 | { 80 | register rb_ptrs_t temp = { rb->ptrs.both }; 81 | register uint16 cap = rb->CAPACITY; 82 | uint32 txd = 0; 83 | while ( txd < len ) { 84 | rb->buffer[temp.head++] = *buf++; // safe to write because it does not overwrite active data 85 | temp.head &= cap; 86 | if (temp.head==temp.tail) 87 | break; 88 | ++txd; 89 | } 90 | rb->ptrs.head = temp.head; // update volatile variable 91 | return txd; 92 | } 93 | 94 | // does not check for available free space !! 95 | void rb_write_n(ring_buffer_t * rb, const uint8_t * buf, uint16_t len) 96 | { 97 | register uint16_t temp_head = rb->ptrs.head; 98 | register uint16 cap = rb->CAPACITY; 99 | while (len--) { 100 | rb->buffer[temp_head++] = *buf++; 101 | temp_head &= cap; 102 | } 103 | rb->ptrs.head = temp_head; 104 | } 105 | 106 | // affects tail, reads head 107 | int rb_read_safe(ring_buffer_t * rb) 108 | { 109 | register rb_ptrs_t temp = { rb->ptrs.both }; 110 | if ( (temp.head == temp.tail) ) 111 | return -1; 112 | int elem = rb->buffer[temp.tail++]; 113 | rb->ptrs.tail = temp.tail & rb->CAPACITY; 114 | return elem; 115 | } 116 | 117 | // does not check for available data !! 118 | uint8_t rb_read(ring_buffer_t * rb) 119 | { 120 | register uint16_t temp_tail = rb->ptrs.tail; 121 | uint8_t elem = rb->buffer[temp_tail++]; 122 | rb->ptrs.tail = temp_tail & rb->CAPACITY; 123 | return elem; 124 | } 125 | 126 | // does not check for available data !! 127 | uint32 rb_read_n(ring_buffer_t * rb, uint8_t* buf, uint16 len) 128 | { 129 | uint32 rd = 0; 130 | register rb_ptrs_t temp = {rb->ptrs.both}; 131 | register uint16 tmp_tail = temp.tail; 132 | register uint16 cap = rb->CAPACITY; 133 | while ( (tmp_tail!=temp.head) && ((rd++)buffer[tmp_tail++]; 135 | tmp_tail &= cap; 136 | } 137 | rb->ptrs.tail = tmp_tail; // update volatile variable 138 | return rd; 139 | } 140 | 141 | uint32 rb_peek_n(ring_buffer_t * rb, uint8_t* buf, uint16 len) 142 | { 143 | uint32 peeked = 0; 144 | register rb_ptrs_t temp = {rb->ptrs.both}; 145 | register uint16 tmp_tail = temp.tail; 146 | register uint16 cap = rb->CAPACITY; 147 | while ( (tmp_tail!=temp.head) && ((peeked++)buffer[tmp_tail++]; 149 | tmp_tail &= cap; 150 | } 151 | // no need to update volatile tail 152 | return peeked; 153 | } 154 | 155 | -------------------------------------------------------------------------------- /F3/eclipse_project/dbg/ring_buffer.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2011 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/ring_buffer.h 29 | * @brief Simple circular buffer 30 | * 31 | * This implementation is not thread-safe. In particular, none of 32 | * these functions is guaranteed re-entrant. 33 | */ 34 | 35 | #ifndef _LIBMAPLE_RING_BUFFER_H_ 36 | #define _LIBMAPLE_RING_BUFFER_H_ 37 | 38 | #ifdef __cplusplus 39 | extern "C"{ 40 | #endif 41 | 42 | #include "libmaple_types.h" 43 | 44 | 45 | /** 46 | A union containing 16 bit head and tail offsets into the ring buffer. 47 | The union allows the c code to grab both values with one assembler instruction access. 48 | */ 49 | typedef union rb_ptrs_t { 50 | // access as 32 bit 51 | uint32_t both; 52 | // -- or as 2 16 bit values -- 53 | struct { 54 | uint16_t head; 55 | uint16_t tail; 56 | }; 57 | } rb_ptrs_t; 58 | 59 | typedef struct ring_buffer_t { 60 | volatile rb_ptrs_t ptrs; 61 | const int CAPACITY; // should be set to SIZE-1 62 | uint8_t buffer[]; 63 | } ring_buffer_t; 64 | 65 | #define RING_BUFFER(name, size) ring_buffer_t (name) = { \ 66 | .ptrs.both = 0, \ 67 | .CAPACITY = (size-1), \ 68 | .buffer[size] = 0 } 69 | 70 | // enum { CAPACITY = SIZE - 1 }; // leave one slot open 71 | 72 | // --- public methods --- 73 | 74 | // write access zeros out head and tail 75 | static inline void rb_reset(ring_buffer_t * rb) { rb->ptrs.both = 0; } 76 | 77 | // return the size of the buffer 78 | static inline int rb_size(ring_buffer_t * rb) { return rb->CAPACITY; } 79 | 80 | uint16_t rb_read_available(ring_buffer_t * rb); // return the count of used slots 81 | 82 | uint16_t rb_write_available(ring_buffer_t * rb); // return the count of free slots 83 | 84 | // returns true when there is no used slots 85 | inline bool rb_is_empty(ring_buffer_t * rb) { 86 | register rb_ptrs_t temp = { rb->ptrs.both }; 87 | return (temp.head == temp.tail); 88 | } 89 | 90 | // returns true when all slots are used 91 | inline int rb_is_full(ring_buffer_t * rb) { 92 | return (rb_read_available(rb) == (uint16)rb->CAPACITY); 93 | } 94 | 95 | // write() - adds an element to the end of the queue 96 | // Note: affects head, reads tail, element ignored if overflow ~300 ns @72MHz 97 | //--------------------------------------------------------------------------- 98 | int rb_write_safe(ring_buffer_t * rb, const uint8_t element); 99 | uint32 rb_write_safe_n(ring_buffer_t * rb, const uint8_t * buf, uint16_t len); 100 | 101 | // does not check for available free space !! 102 | void rb_write_n(ring_buffer_t * rb, const uint8_t * buf, uint16_t len); 103 | /* 104 | static inline uint8_t * rb_write_ptr(ring_buffer_t * rb) { 105 | return &rb->buffer[rb->ptrs.head]; 106 | } 107 | */ 108 | inline void rb_write_finish(ring_buffer_t * rb, uint16_t n) { 109 | register uint16_t temp_head = (rb->ptrs.head + n) & rb->CAPACITY; 110 | rb->ptrs.head = temp_head; 111 | } 112 | /* 113 | inline uint8_t * rb_read_ptr(ring_buffer_t * rb) { 114 | return &rb->buffer[rb->ptrs.tail]; 115 | } 116 | */ 117 | inline int rb_peek(ring_buffer_t * rb) { 118 | return rb->buffer[rb->ptrs.tail]; 119 | } 120 | 121 | uint32 rb_peek_n(ring_buffer_t * rb, uint8_t* buf, uint16 len); 122 | uint32 rb_read_n(ring_buffer_t * rb, uint8_t* buf, uint16 len); 123 | 124 | int rb_read_safe(ring_buffer_t * rb); 125 | uint8_t rb_read(ring_buffer_t * rb); 126 | 127 | 128 | 129 | #ifdef __cplusplus 130 | } // extern "C" 131 | #endif 132 | 133 | #endif 134 | -------------------------------------------------------------------------------- /F3/eclipse_project/dbg/usart.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * Copyright (c) 2011 LeafLabs, LLC. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/usart.c 30 | * @author Marti Bolivar , 31 | * Perry Hung 32 | * @brief Portable USART routines 33 | */ 34 | #include "stm32f3xx.h" 35 | #include "usart.h" 36 | 37 | 38 | 39 | /** 40 | * @brief Initialize a serial port. 41 | * @param dev Serial port to be initialized 42 | */ 43 | void usart_init(const usart_dev *dev) { 44 | rb_reset(dev->rb); 45 | rb_reset(dev->wb); 46 | rcc_clk_enable(dev->clk_id); 47 | nvic_irq_enable(dev->irq_num); 48 | } 49 | 50 | /** 51 | * @brief Enable a serial port. 52 | * 53 | * USART is enabled in single buffer transmission mode, multibuffer 54 | * receiver mode, 8n1. 55 | * 56 | * Serial port must have a baud rate configured to work properly. 57 | * 58 | * @param dev Serial port to enable. 59 | * @see usart_set_baud_rate() 60 | */ 61 | void usart_enable(const usart_dev *dev) { 62 | usart_reg_def *regs = dev->regs; 63 | regs->CR1 |= (USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE| USART_CR1_UE); 64 | } 65 | 66 | /** 67 | * @brief Turn off a serial port. 68 | * @param dev Serial port to be disabled 69 | */ 70 | void usart_disable(const usart_dev *dev) { 71 | /* FIXME this misbehaves (on F1) if you try to use PWM on TX afterwards */ 72 | usart_reg_def *regs = dev->regs; 73 | 74 | while(!rb_is_empty(dev->wb)) 75 | ; // wait for TX completed 76 | /* TC bit must be high before disabling the USART */ 77 | while((regs->CR1 & USART_CR1_UE) && !(regs->ISR & USART_ISR_TC_Pos)) 78 | ; 79 | 80 | /* Disable UE */ 81 | regs->CR1 &= ~USART_CR1_UE; 82 | 83 | // Clean up buffer 84 | rb_reset(dev->rb); 85 | rb_reset(dev->wb); 86 | } 87 | 88 | /** 89 | * @brief Nonblocking USART transmit 90 | * @param dev Serial port to transmit over 91 | * @param buf Buffer to transmit 92 | * @param len Maximum number of bytes to transmit 93 | * @return Number of bytes transmitted 94 | */ 95 | uint16 usart_tx(const usart_dev *dev, const uint8 *buf, uint16 len) 96 | { 97 | uint16 txed = 0; 98 | while ( txed < len ) 99 | { 100 | while ( !rb_write_safe(dev->wb, *buf) ) // checks for buffer full 101 | ;//break; 102 | buf++; 103 | txed++; 104 | } 105 | dev->regs->CR1 |= USART_CR1_TXEIE; // enable Tx IRQ 106 | 107 | return txed; 108 | } 109 | 110 | /** 111 | * @brief Nonblocking USART receive. 112 | * @param dev Serial port to receive bytes from 113 | * @param buf Buffer to store received bytes into 114 | * @param len Maximum number of bytes to store 115 | * @return Number of bytes received 116 | */ 117 | uint16 usart_rx(const usart_dev *dev, uint8 *buf, uint16 len) { 118 | uint16 rxed = 0; 119 | while (usart_rx_available(dev) && rxed < len) { 120 | *buf++ = usart_getc(dev); 121 | rxed++; 122 | } 123 | return rxed; 124 | } 125 | 126 | /** 127 | * @brief Transmit an unsigned integer to the specified serial port in 128 | * decimal format. 129 | * 130 | * This function blocks until the integer's digits have been 131 | * completely transmitted. 132 | * 133 | * @param dev Serial port to send on 134 | * @param val Number to print 135 | */ 136 | void usart_putudec(const usart_dev *dev, uint32 val) { 137 | char digits[12]; 138 | int i = 0; 139 | 140 | do { 141 | digits[i++] = val % 10 + '0'; 142 | val /= 10; 143 | } while (val > 0); 144 | 145 | while (--i >= 0) { 146 | usart_putc(dev, digits[i]); 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /F3/eclipse_project/dbg/usart_f1.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2012 LeafLabs, LLC. 5 | * Copyright (c) 2010 Perry Hung. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/stm32f1/usart.c 30 | * @author Marti Bolivar , 31 | * Perry Hung 32 | * @brief STM32F1 USART. 33 | */ 34 | 35 | #include "libmaple_types.h" 36 | #include "usart.h" 37 | #include "gpio.h" 38 | #include "usart_private.h" 39 | 40 | /* 41 | * Devices 42 | */ 43 | 44 | static RING_BUFFER(usart1_rb, USART_RX_BUF_SIZE); 45 | static RING_BUFFER(usart1_wb, USART_TX_BUF_SIZE); 46 | /** USART1 device */ 47 | const usart_dev usart1 = { 48 | .regs = USART1_REGS, 49 | .rb = &usart1_rb, 50 | .wb = &usart1_wb, 51 | .clk_id = RCC_USART1, 52 | .irq_num = NVIC_USART1, 53 | }; 54 | #if 0 55 | static RING_BUFFER(usart2_rb, SERIAL_RX_BUFFER_SIZE); 56 | static RING_BUFFER(usart2_wb, SERIAL_TX_BUFFER_SIZE); 57 | /** USART2 device */ 58 | const usart_dev usart2 = { 59 | .regs = USART2_BASE, 60 | .rb = &usart2_rb, 61 | .wb = &usart2_wb, 62 | .clk_id = RCC_USART2, 63 | .irq_num = NVIC_USART2, 64 | }; 65 | 66 | static RING_BUFFER(usart3_rb, SERIAL_RX_BUFFER_SIZE); 67 | static RING_BUFFER(usart3_wb, SERIAL_TX_BUFFER_SIZE); 68 | /** USART3 device */ 69 | const usart_dev usart3 = { 70 | .regs = USART3_BASE, 71 | .rb = &usart3_rb, 72 | .wb = &usart3_wb, 73 | .clk_id = RCC_USART3, 74 | .irq_num = NVIC_USART3, 75 | }; 76 | #endif 77 | #if defined(STM32_HIGH_DENSITY) || defined(STM32_XL_DENSITY) 78 | static ring_buffer uart4_rb; 79 | static ring_buffer uart4_wb; 80 | static usart_dev uart4 = { 81 | .regs = UART4_BASE, 82 | .rb = &uart4_rb, 83 | .wb = &uart4_wb, 84 | .max_baud = 2250000UL, 85 | .clk_id = RCC_UART4, 86 | .irq_num = NVIC_UART4, 87 | }; 88 | /** UART4 device */ 89 | usart_dev *UART4 = &uart4; 90 | 91 | static ring_buffer uart5_rb; 92 | static ring_buffer uart5_wb; 93 | static usart_dev uart5 = { 94 | .regs = UART5_BASE, 95 | .rb = &uart5_rb, 96 | .wb = &uart5_wb, 97 | .max_baud = 2250000UL, 98 | .clk_id = RCC_UART5, 99 | .irq_num = NVIC_UART5, 100 | }; 101 | /** UART5 device */ 102 | usart_dev *UART5 = &uart5; 103 | #endif 104 | 105 | /* 106 | * Routines 107 | */ 108 | 109 | 110 | void usart_config(const usart_dev *udev, uint8 rx, uint8 tx, uint8 flags) 111 | { 112 | /* 113 | CR1 bit 12 Word length 0=8 1=9 114 | CR1 bit 11 wake (default value is 0) we can safely set this value to 0 (zero) each time 115 | CR1 bit 10 parity enable (1 = enabled) 116 | CR1 bit 9 Parity selection 0 = Even 1 = Odd 117 | CR2 bits 13 and 12 stop bits: 00 = 1 01 = 0.5 10 = 2 11 = 1.5 118 | Not all USARTs support 1.5 or 0.5 bits so its best to avoid them. 119 | When parity enabled the word length must be increased (CR1 bit 12 set). 120 | Word length of 9 bit with parity is not supported. 121 | */ 122 | udev->regs->CR1 = (udev->regs->CR1 & 0B1110000111111111) | ((uint32_t)(flags&0x0F)<<9); 123 | udev->regs->CR2 = (udev->regs->CR2 & 0B1100111111111111) | ((uint32_t)(flags&0x30)<<8); 124 | gpio_set_mode(rx, GPIO_AF_INPUT); 125 | gpio_set_mode(tx, GPIO_AF_OUTPUT); 126 | gpio_set_af(rx, GPIO_AF_USART1_2_3); 127 | gpio_set_af(tx, GPIO_AF_USART1_2_3); 128 | } 129 | 130 | void usart_set_baud_rate(const usart_dev *dev, uint32 baud) 131 | { 132 | uint32 clock_speed = _usart_clock_freq(dev); 133 | 134 | /* Convert desired baud rate to baud rate register setting. */ 135 | uint32 integer_part = (25 * clock_speed) / (4 * baud); 136 | uint32 tmp = (integer_part / 100) << 4; 137 | uint32 fractional_part = integer_part - (100 * (tmp >> 4)); 138 | tmp |= (((fractional_part * 16) + 50) / 100) & ((uint8)0x0F); 139 | 140 | dev->regs->BRR = (uint16)tmp; 141 | } 142 | 143 | /** 144 | * @brief Call a function on each USART. 145 | * @param fn Function to call. 146 | */ 147 | void usart_foreach(void (*fn)(const usart_dev*)) { 148 | fn(USART1); 149 | #if 0 150 | fn(USART2); 151 | fn(USART3); 152 | #endif 153 | #ifdef STM32_HIGH_DENSITY 154 | fn(UART4); 155 | fn(UART5); 156 | #endif 157 | } 158 | 159 | /* 160 | * Interrupt handlers. 161 | */ 162 | 163 | __weak void __irq_usart1(void) { 164 | usart_irq(USART1); 165 | } 166 | 167 | __weak void __irq_usart2(void) { 168 | usart_irq(USART2); 169 | } 170 | 171 | __weak void __irq_usart3(void) { 172 | usart_irq(USART3); 173 | } 174 | 175 | #ifdef STM32_HIGH_DENSITY 176 | __weak void __irq_uart4(void) { 177 | usart_irq(UART4); 178 | } 179 | 180 | __weak void __irq_uart5(void) { 181 | usart_irq(UART5); 182 | } 183 | #endif 184 | -------------------------------------------------------------------------------- /F3/eclipse_project/dbg/usart_private.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2012 LeafLabs, LLC. 5 | * Copyright (c) 2010 Perry Hung. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/usart_private.h 30 | * @author Marti Bolivar 31 | * @brief Private USART header. 32 | */ 33 | 34 | #ifndef _LIBMAPLE_USART_PRIVATE_H_ 35 | #define _LIBMAPLE_USART_PRIVATE_H_ 36 | 37 | #include "ring_buffer.h" 38 | #include "usart.h" 39 | 40 | 41 | static inline uint32 _usart_clock_freq(const usart_dev *dev) { 42 | rcc_clk_domain domain = rcc_dev_clk(dev->clk_id); 43 | return (domain == RCC_APB1 ? STM32_PCLK1 : 44 | (domain == RCC_APB2 ? STM32_PCLK2 : 0)); 45 | } 46 | 47 | 48 | static inline void usart_irq(const usart_dev * udev) 49 | { 50 | /* Handling RXNEIE and TXEIE interrupts. 51 | * See table 198 (sec 27.4, p809) in STM document RM0008 rev 15. 52 | * We enable RXNEIE. */ 53 | 54 | register usart_reg_def * regs = udev->regs; 55 | 56 | // Receive part. RXNE signifies availability of a byte in DR. 57 | if (regs->CR1 & USART_CR1_RXNEIE) 58 | { // check error flags 59 | if ( regs->ISR & (USART_ISR_ORE | USART_ISR_FE | USART_ISR_PE) ) 60 | { // overrun, framing or parity error: clear the error flags 61 | regs->ICR = (USART_ICR_ORECF | USART_ICR_FECF | USART_ICR_PECF); 62 | } 63 | if (regs->ISR & USART_ISR_RXNE) 64 | { // push bytes around in the ring buffer 65 | rb_write_safe(udev->rb, (uint8) regs->RDR); 66 | } 67 | } 68 | // Transmit part. TXE signifies readiness to send a byte to DR 69 | if ((regs->CR1 & USART_CR1_TXEIE) && (regs->ISR & USART_ISR_TXE)) 70 | { 71 | register int val = rb_read_safe(udev->wb); 72 | if (val!=-1) 73 | regs->TDR = (uint8_t)val; 74 | else 75 | regs->CR1 &= ~((uint32)USART_CR1_TXEIE); // disable TXEIE 76 | } 77 | } 78 | 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /F3/eclipse_project/dbg/usb_trx.c: -------------------------------------------------------------------------------- 1 | 2 | #include "usb_def.h" 3 | #include "usb_trx.h" 4 | #include "usb_func.h" 5 | 6 | // ring buffers for USB In und Out data 7 | RING_BUFFER(rxBuf, 256); 8 | RING_BUFFER(txBuf, 256); 9 | 10 | 11 | #ifdef USB_DEBUG 12 | #define DBG_SIZE 10000 13 | char dBuf[DBG_SIZE]; 14 | int dLen = 0; 15 | #define DIGITS_SIZE 10 16 | char digits[DIGITS_SIZE]; 17 | 18 | void DoTrace(char * s, int len) 19 | { 20 | if ( (dLen+len)>DBG_SIZE ) 21 | return; 22 | 23 | char* dest = &dBuf[dLen]; 24 | dLen += len; 25 | const char* src = s; 26 | while (len--) 27 | *dest++ = *src++; 28 | } 29 | void NrTrace(int val, int nl) 30 | { 31 | int i = DIGITS_SIZE-1; 32 | if (nl) 33 | digits[i--] = '\n'; 34 | do { 35 | digits[i--] = val % 10 + '0'; 36 | val /= 10; 37 | } while (val > 0 && i>=0); 38 | i++; 39 | DoTrace(&digits[i], DIGITS_SIZE-i); 40 | } 41 | #endif 42 | 43 | // USB serial related routines 44 | // These functions are used on main level 45 | 46 | // Returns the number of available bytes received by USB 47 | int UsbRxAvail(void) 48 | { 49 | return rb_read_available(&rxBuf); 50 | } 51 | 52 | // Reads a character from the USB receive buffer. 53 | // Returns -1 if none received 54 | int UsbGetChar(void) 55 | { 56 | return rb_read_safe(&rxBuf); 57 | } 58 | 59 | // Returns true if the USB has been started and configured. 60 | // Data transmission is only possible this returns true. 61 | // The main level application should wait for this condition before performing any communication. 62 | bool UsbActive (void) 63 | { 64 | status_t state = { usb_state.both }; // read status with one instruction 65 | return ( state.configured && !state.suspended ); 66 | } 67 | 68 | // Returns the number of available free slots in the transmit buffer 69 | uint16 UsbTxFree(void) 70 | { 71 | return rb_write_available(&txBuf); 72 | } 73 | 74 | // Returns true if there is not data to send 75 | bool UsbTxEmpty(void) 76 | { 77 | return rb_is_empty(&txBuf); 78 | } 79 | /* 80 | // send everything from the Tx buffer 81 | void UsbTxFlush (void) 82 | { 83 | DataBeginTransmit (); 84 | } 85 | 86 | // Transmits a character over the USB serial interface 87 | bool UsbPutChar(char c) 88 | { 89 | if ( !rb_write_safe(&txBuf, c) ) 90 | return false; 91 | 92 | DataBeginTransmit (); 93 | 94 | return true; 95 | } 96 | */ 97 | // Transmits a string over the USB serial interface 98 | void UsbPutStr(char* S) 99 | { 100 | while (*S && UsbPutChar(*S++)); 101 | } 102 | 103 | -------------------------------------------------------------------------------- /F3/eclipse_project/dbg/usb_trx.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stevstrong/STM32-CDC-Bootloader/d53886de2380a4812abd720e6777ede4b293cc57/F3/eclipse_project/dbg/usb_trx.h -------------------------------------------------------------------------------- /F3/eclipse_project/src/cdc.h: -------------------------------------------------------------------------------- 1 | /** 2 | * usb_cdc_defines USB CDC Type Definitions 3 | * 4 | */ 5 | #ifndef __CDC_H 6 | #define __CDC_H 7 | 8 | /* Definitions of Communications Device Class from 9 | * "Universal Serial Bus Class Definitions for Communications Devices 10 | * Revision 1.2" 11 | */ 12 | 13 | /* Table 2: Communications Device Class Code */ 14 | #define USB_CLASS_CDC 0x02 15 | 16 | /* Table 4: Class Subclass Code */ 17 | #define USB_CDC_SUBCLASS_DLCM 0x01 18 | #define USB_CDC_SUBCLASS_ACM 0x02 19 | /* ... */ 20 | 21 | /* Table 5 Communications Interface Class Control Protocol Codes */ 22 | #define USB_CDC_PROTOCOL_NONE 0x00 23 | #define USB_CDC_PROTOCOL_AT 0x01 24 | /* ... */ 25 | 26 | /* Table 6: Data Interface Class Code */ 27 | #define USB_CLASS_DATA 0x0A 28 | 29 | /* Table 12: Type Values for the bDescriptorType Field */ 30 | #define CS_INTERFACE 0x24 31 | #define CS_ENDPOINT 0x25 32 | 33 | /* Table 13: bDescriptor SubType in Communications Class Functional 34 | * Descriptors */ 35 | #define USB_CDC_TYPE_HEADER 0x00 36 | #define USB_CDC_TYPE_CALL_MANAGEMENT 0x01 37 | #define USB_CDC_TYPE_ACM 0x02 38 | /* ... */ 39 | #define USB_CDC_TYPE_UNION 0x06 40 | /* ... */ 41 | 42 | /* Table 15: Class-Specific Descriptor Header Format */ 43 | typedef struct usb_cdc_header_descriptor { 44 | uint8_t bFunctionLength; 45 | uint8_t bDescriptorType; 46 | uint8_t bDescriptorSubtype; 47 | uint16_t bcdCDC; 48 | } __attribute((packed)) usb_cdc_header_descriptor; 49 | 50 | /* Table 16: Union Interface Functional Descriptor */ 51 | typedef struct usb_cdc_union_descriptor { 52 | uint8_t bFunctionLength; 53 | uint8_t bDescriptorType; 54 | uint8_t bDescriptorSubtype; 55 | uint8_t bControlInterface; 56 | uint8_t bSubordinateInterface0; 57 | /* ... */ 58 | } __attribute((packed)) usb_cdc_union_descriptor; 59 | 60 | 61 | /* Definitions for Abstract Control Model devices from: 62 | * "Universal Serial Bus Communications Class Subclass Specification for 63 | * PSTN Devices" 64 | */ 65 | 66 | /* Table 3: Call Management Functional Descriptor */ 67 | typedef struct usb_cdc_call_management_descriptor { 68 | uint8_t bFunctionLength; 69 | uint8_t bDescriptorType; 70 | uint8_t bDescriptorSubtype; 71 | uint8_t bmCapabilities; 72 | uint8_t bDataInterface; 73 | } __attribute((packed)) usb_cdc_call_management_descriptor; 74 | 75 | /* Table 4: Abstract Control Management Functional Descriptor */ 76 | typedef struct usb_cdc_acm_descriptor { 77 | uint8_t bFunctionLength; 78 | uint8_t bDescriptorType; 79 | uint8_t bDescriptorSubtype; 80 | uint8_t bmCapabilities; 81 | } __attribute((packed)) usb_cdc_acm_descriptor; 82 | 83 | /* Table 13: Class-Specific Request Codes for PSTN subclasses */ 84 | /* ... */ 85 | #define USB_CDC_REQ_SET_LINE_CODING 0x20 86 | #define USB_CDC_REQ_GET_LINE_CODING 0x21 87 | #define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22 88 | #define USB_CDC_REQ_SEND_BREAK 0x23 89 | 90 | 91 | /* Table 17: Line Coding Structure */ 92 | typedef struct usb_cdc_line_coding { 93 | uint32_t baudRate; // dwDTERate; 94 | uint8_t stopBits; //bCharFormat; 95 | uint8_t parityType; //bParityType; 96 | uint8_t dataBits; //bDataBits; 97 | } __attribute((packed)) usb_cdc_line_coding; 98 | 99 | enum usb_cdc_line_coding_bCharFormat { 100 | USB_CDC_1_STOP_BITS = 0, 101 | USB_CDC_1_5_STOP_BITS = 1, 102 | USB_CDC_2_STOP_BITS = 2, 103 | }; 104 | 105 | enum usb_cdc_line_coding_bParityType { 106 | USB_CDC_NO_PARITY = 0, 107 | USB_CDC_ODD_PARITY = 1, 108 | USB_CDC_EVEN_PARITY = 2, 109 | USB_CDC_MARK_PARITY = 3, 110 | USB_CDC_SPACE_PARITY = 4, 111 | }; 112 | 113 | /* Table 30: Class-Specific Notification Codes for PSTN subclasses */ 114 | #define USB_CDC_NOTIFY_SERIAL_STATE 0x20 115 | 116 | 117 | /* Notification Structure */ 118 | struct usb_cdc_notification { 119 | uint8_t bmRequestType; 120 | uint8_t bNotification; 121 | uint16_t wValue; 122 | uint16_t wIndex; 123 | uint16_t wLength; 124 | } __attribute__((packed)); 125 | 126 | #endif 127 | 128 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/bitband.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2011 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/bitband.h 29 | * 30 | * @brief Bit-banding utility functions 31 | */ 32 | 33 | #ifndef _LIBMAPLE_BITBAND_H_ 34 | #define _LIBMAPLE_BITBAND_H_ 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | #include "libmaple_types.h" 41 | 42 | #define BB_SRAM_REF 0x20000000 43 | #define BB_SRAM_BASE 0x22000000 44 | #define BB_PERI_REF 0x40000000 45 | #define BB_PERI_BASE 0x42000000 46 | 47 | static inline volatile uint32* __bb_addr(volatile void*, 48 | uint32, 49 | uint32, 50 | uint32); 51 | 52 | /** 53 | * @brief Obtain a pointer to the bit-band address corresponding to a 54 | * bit in a volatile SRAM address. 55 | * @param address Address in the bit-banded SRAM region 56 | * @param bit Bit in address to bit-band 57 | */ 58 | static inline volatile uint32* bb_sramp(volatile void *address, uint32 bit) { 59 | return __bb_addr(address, bit, BB_SRAM_BASE, BB_SRAM_REF); 60 | } 61 | 62 | /** 63 | * @brief Get a bit from an address in the SRAM bit-band region. 64 | * @param address Address in the SRAM bit-band region to read from 65 | * @param bit Bit in address to read 66 | * @return bit's value in address. 67 | */ 68 | static inline uint8 bb_sram_get_bit(volatile void *address, uint32 bit) { 69 | return *bb_sramp(address, bit); 70 | } 71 | 72 | /** 73 | * @brief Set a bit in an address in the SRAM bit-band region. 74 | * @param address Address in the SRAM bit-band region to write to 75 | * @param bit Bit in address to write to 76 | * @param val Value to write for bit, either 0 or 1. 77 | */ 78 | static inline void bb_sram_set_bit(volatile void *address, 79 | uint32 bit, 80 | uint8 val) { 81 | *bb_sramp(address, bit) = val; 82 | } 83 | 84 | /** 85 | * @brief Obtain a pointer to the bit-band address corresponding to a 86 | * bit in a peripheral address. 87 | * @param address Address in the bit-banded peripheral region 88 | * @param bit Bit in address to bit-band 89 | */ 90 | static inline volatile uint32* bb_perip(volatile void *address, uint32 bit) { 91 | return __bb_addr(address, bit, BB_PERI_BASE, BB_PERI_REF); 92 | } 93 | 94 | /** 95 | * @brief Get a bit from an address in the peripheral bit-band region. 96 | * @param address Address in the peripheral bit-band region to read from 97 | * @param bit Bit in address to read 98 | * @return bit's value in address. 99 | */ 100 | static inline uint8 bb_peri_get_bit(volatile void *address, uint32 bit) { 101 | return *bb_perip(address, bit); 102 | } 103 | 104 | /** 105 | * @brief Set a bit in an address in the peripheral bit-band region. 106 | * @param address Address in the peripheral bit-band region to write to 107 | * @param bit Bit in address to write to 108 | * @param val Value to write for bit, either 0 or 1. 109 | */ 110 | static inline void bb_peri_set_bit(volatile void *address, 111 | uint32 bit, 112 | uint8 val) { 113 | *bb_perip(address, bit) = val; 114 | } 115 | 116 | static inline volatile uint32* __bb_addr(volatile void *address, 117 | uint32 bit, 118 | uint32 bb_base, 119 | uint32 bb_ref) { 120 | return (volatile uint32*)(bb_base + ((uint32)address - bb_ref) * 32 + 121 | bit * 4); 122 | } 123 | 124 | #ifdef __cplusplus 125 | } 126 | #endif 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/bkp.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 LeafLabs, LLC. 5 | * Copyright (c) 2013 OpenMusicKontrollers. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/stm32f3/include/series/bkp.h 30 | * @author F3-port by Hanspeter Portner 31 | * @brief STM32F3 backup register support. 32 | */ 33 | 34 | #ifndef _LIBMAPLE_STM32F3_BKP_H_ 35 | #define _LIBMAPLE_STM32F3_BKP_H_ 36 | 37 | 38 | #ifdef __cplusplus 39 | extern "C"{ 40 | #endif 41 | 42 | 43 | #include "libmaple.h" 44 | #include "pwr.h" 45 | 46 | 47 | #define BKP_NR_DATA_REGS 16 48 | 49 | /** Backup peripheral register map type. */ 50 | typedef struct bkp_reg_map { 51 | __IO uint32 DR0; ///< Data register 0 52 | __IO uint32 DR1; ///< Data register 1 53 | __IO uint32 DR2; ///< Data register 2 54 | __IO uint32 DR3; ///< Data register 3 55 | __IO uint32 DR4; ///< Data register 4 56 | __IO uint32 DR5; ///< Data register 5 57 | __IO uint32 DR6; ///< Data register 6 58 | __IO uint32 DR7; ///< Data register 7 59 | __IO uint32 DR8; ///< Data register 8 60 | __IO uint32 DR9; ///< Data register 9 61 | __IO uint32 DR10; ///< Data register 10 62 | __IO uint32 DR11; ///< Data register 11 63 | __IO uint32 DR12; ///< Data register 12 64 | __IO uint32 DR13; ///< Data register 13 65 | __IO uint32 DR14; ///< Data register 14 66 | __IO uint32 DR15; ///< Data register 15 67 | } bkp_reg_map; 68 | 69 | /** Backup peripheral register map base pointer. */ 70 | #define BKP_BASE ((struct bkp_reg_map*)0x40002850) //is part of RTC memory map 71 | 72 | /** Backup peripheral device type. */ 73 | typedef struct bkp_dev { 74 | bkp_reg_map *regs; /**< Register map */ 75 | } bkp_dev; 76 | 77 | extern const bkp_dev bkp_device; 78 | #define BKP (&bkp_device) 79 | 80 | 81 | /** 82 | * @brief Initialize backup interface. 83 | * 84 | * Enables the power and backup interface clocks, and resets the 85 | * backup device. 86 | */ 87 | static inline void bkp_init(void) { 88 | // Don't call pwr_init(), or you'll reset the device. We just need the clock. 89 | rcc_clk_enable(RCC_PWR); 90 | } 91 | //----------------------------------------------------------------------------- 92 | static inline void bkp_deinit(void) { 93 | rcc_clk_disable(RCC_PWR); 94 | } 95 | 96 | /** 97 | * Enable write access to the backup registers. Backup interface must 98 | * be initialized for subsequent register writes to work. 99 | * @see bkp_init() 100 | */ 101 | static inline void bkp_enable_writes(void) { 102 | PWR->CR |= PWR_CR_DBP; 103 | } 104 | 105 | /** 106 | * Disable write access to the backup registers. 107 | */ 108 | static inline void bkp_disable_writes(void) { 109 | PWR->CR &= PWR_CR_DBP; 110 | } 111 | 112 | /* 113 | * this function needs to be implemented for each series separately 114 | */ 115 | static inline __IO uint32* bkp_data_register(uint8 reg) { 116 | return (uint32*)BKP_BASE + (reg-1); // regs are accessed from 1-16 117 | } 118 | 119 | /** 120 | * Read a value from given backup data register. 121 | * @param reg Data register to read, from 1 to BKP_NR_DATA_REGS (10 on 122 | * medium-density devices, 42 on high-density devices). 123 | */ 124 | static inline uint16 bkp_read(uint8 reg) { 125 | if (reg < 1 || reg > BKP_NR_DATA_REGS) 126 | return 0; 127 | __IO uint32* dr = bkp_data_register(reg); 128 | return (uint16)*dr; 129 | } 130 | 131 | /** 132 | * @brief Write a value to given data register. 133 | * 134 | * Write access to backup registers must be enabled. 135 | * 136 | * @param reg Data register to write, from 1 to BKP_NR_DATA_REGS (10 137 | * on medium-density devices, 42 on high-density devices). 138 | * @param val Value to write into the register. 139 | * @see bkp_enable_writes() 140 | */ 141 | static inline void bkp_write(uint8 reg, uint16 val) { 142 | if (reg < 1 || reg > BKP_NR_DATA_REGS) 143 | return; 144 | __IO uint32* dr = bkp_data_register(reg); 145 | *dr = (uint32)val; 146 | } 147 | 148 | 149 | 150 | #ifdef __cplusplus 151 | } 152 | #endif 153 | 154 | 155 | #endif 156 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/board.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2013 OpenMusicKontrollers. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file board/board.h 29 | * @author F3-port: Hanspeter Portner 30 | * @brief F303xx board header (F303CB, F303CC, F303RB, F303RC, F303VB, F303VC). 31 | * 32 | * See wirish/boards/maple/include/board/board.h for more information 33 | * on these definitions. 34 | */ 35 | 36 | #ifndef _BOARD_F303xx_H_ 37 | #define _BOARD_F303xx_H_ 38 | 39 | #include "stm32.h" 40 | 41 | #define LED_BUILTIN BOARD_LED_PIN 42 | 43 | #define CYCLES_PER_MICROSECOND (F_CPU / 1000000U) 44 | #define SYSTICK_RELOAD_VAL (F_CPU/1000) - 1 /* takes a cycle to reload */ 45 | 46 | enum { 47 | PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7, PA8, PA9, PA10, PA11, PA12, PA13, PA14, PA15, 48 | PB0, PB1, PB2, PB3, PB4, PB5, PB6, PB7, PB8, PB9, PB10, PB11, PB12, PB13, PB14, PB15, 49 | PC13 = 45, PC14, PC15, LAST_GPIO_PIN 50 | }; 51 | 52 | #define OSC_IN PC14 53 | #define OSC_OUT PC15 54 | 55 | #define USB_DM PA11 56 | #define USB_DP PA12 57 | 58 | #define BOARD_USART1_TX_PIN PA9 /* also PB6 */ 59 | #define BOARD_USART1_RX_PIN PA10 /* also PB7 */ 60 | 61 | #define BOARD_USART2_TX_PIN PA2 /* also PA14, PB3 */ 62 | #define BOARD_USART2_RX_PIN PA3 /* also PA15, PB4 */ 63 | 64 | #define BOARD_USART3_TX_PIN PB10 65 | #define BOARD_USART3_RX_PIN PB11 66 | 67 | #if defined(STM32_HIGH_DENSITY) || defined(STM32_XL_DENSITY) 68 | # define BOARD_UART4_TX_PIN PC10 69 | # define BOARD_UART4_RX_PIN PC11 70 | 71 | # define BOARD_UART5_TX_PIN PC12 72 | # define BOARD_UART5_RX_PIN PD2 73 | #endif 74 | 75 | #define BOARD_NR_SPI 3 76 | 77 | #define BOARD_SPI1_NSS_PIN PA4 78 | #define BOARD_SPI1_SCK_PIN PA5 79 | #define BOARD_SPI1_MISO_PIN PA6 80 | #define BOARD_SPI1_MOSI_PIN PA7 81 | 82 | #define BOARD_SPI2_NSS_PIN PB12 83 | #define BOARD_SPI2_SCK_PIN PB13 84 | #define BOARD_SPI2_MISO_PIN PB14 85 | #define BOARD_SPI2_MOSI_PIN PB15 86 | 87 | #define BOARD_SPI3_NSS_PIN PA15 88 | #define BOARD_SPI3_SCK_PIN PB3 89 | #define BOARD_SPI3_MISO_PIN PB4 90 | #define BOARD_SPI3_MOSI_PIN PB5 91 | 92 | #define BOARD_JTMS_SWDIO_PIN PA13 93 | #define BOARD_JTCK_SWCLK_PIN PA14 94 | #define BOARD_JTDI_PIN PA15 95 | #define BOARD_JTDO_PIN PB3 96 | #define BOARD_NJTRST_PIN PB4 97 | 98 | #if defined(STM32_MEDIUM_DENSITY) 99 | # define BOARD_NR_USARTS 3 100 | # define BOARD_NR_GPIO_PINS 35 //37 101 | //# define BOARD_NR_PWM_PINS 28 102 | //# define BOARD_NR_ADC_PINS 15 103 | # define BOARD_NR_USED_PINS 4 104 | #elif defined(STM32_HIGH_DENSITY) 105 | # define BOARD_NR_USARTS 5 106 | # define BOARD_NR_GPIO_PINS 52 107 | # define BOARD_NR_PWM_PINS 32 108 | # define BOARD_NR_ADC_PINS 22 109 | # define BOARD_NR_USED_PINS 4 110 | #elif defined(STM32_XL_DENSITY) 111 | # define BOARD_NR_USARTS 5 112 | # define BOARD_NR_GPIO_PINS 87 113 | # define BOARD_NR_PWM_PINS 53 114 | # define BOARD_NR_ADC_PINS 39 115 | # define BOARD_NR_USED_PINS 7 116 | #endif 117 | 118 | /* redefine the following ones to match your hardware design */ 119 | //#define BOARD_BUTTON_PIN PA0 120 | #define BOARD_LED_PIN PC13 121 | 122 | #define BOARD_USB_DISC_DEV GPIOA 123 | #define BOARD_USB_DISC_BIT 13 124 | 125 | #endif 126 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/delay.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * Copyright (c) 2011 LeafLabs, LLC. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/include/libmaple/delay.h 30 | * @brief Delay implementation 31 | */ 32 | 33 | #ifndef _LIBMAPLE_DELAY_H_ 34 | #define _LIBMAPLE_DELAY_H_ 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | #include "libmaple_types.h" 41 | #include "stm32.h" 42 | #include "systick.h" 43 | #include "board.h" 44 | 45 | /** 46 | * Delay for at least the given number of milliseconds. 47 | * 48 | * Interrupts, etc. may cause the actual number of milliseconds to 49 | * exceed ms. However, this function will return no less than ms 50 | * milliseconds from the time it is called. 51 | * 52 | * @param ms the number of milliseconds to delay. 53 | * @see delayMicroseconds() 54 | */ 55 | void delay(uint32 ms); 56 | 57 | /** 58 | * Delay for at least the given number of microseconds. 59 | * 60 | * Interrupts, etc. may cause the actual number of microseconds to 61 | * exceed us. However, this function will return no less than us 62 | * microseconds from the time it is called. 63 | * 64 | * @param us the number of microseconds to delay. 65 | * @see delay() 66 | */ 67 | void delayMicroseconds(uint32 us); 68 | 69 | 70 | /** 71 | * Returns time (in microseconds) since the beginning of program 72 | * execution. On overflow, restarts at 0. 73 | * @see millis() 74 | */ 75 | static inline uint32 micros(void) { 76 | uint32 ms; 77 | uint32 cycle_cnt; 78 | 79 | do { 80 | ms = millis(); 81 | cycle_cnt = systick_get_count(); 82 | asm volatile("nop"); //allow interrupt to fire 83 | asm volatile("nop"); 84 | } while (ms != millis()); 85 | 86 | #define US_PER_MS 1000 87 | /* SYSTICK_RELOAD_VAL is 1 less than the number of cycles it 88 | * actually takes to complete a SysTick reload */ 89 | return ((ms * US_PER_MS) + 90 | (SYSTICK_RELOAD_VAL + 1 - cycle_cnt) / CYCLES_PER_MICROSECOND); 91 | #undef US_PER_MS 92 | } 93 | 94 | /** 95 | * @brief Delay the given number of microseconds. 96 | * 97 | * @param us Number of microseconds to delay. 98 | */ 99 | static inline void delay_us(uint32 us) { 100 | if (us==0) 101 | { 102 | return; 103 | } 104 | us *= STM32_DELAY_US_MULT; 105 | 106 | /* fudge for function call overhead */ 107 | us--; 108 | asm volatile(" mov r0, %[us] \n\t" 109 | "1: subs r0, #1 \n\t" 110 | " bhi 1b \n\t" 111 | : 112 | : [us] "r" (us) 113 | : "r0"); 114 | } 115 | 116 | 117 | #ifdef __cplusplus 118 | } 119 | #endif 120 | 121 | #endif 122 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/exti.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2012 LeafLabs, LLC. 5 | * Copyright (c) 2013 OpenMusicKontrollers. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/stm32f3/include/series/exti.h 30 | * @author F3-port by Hanspeter Portner 31 | * @brief STM32F3 external interrupts 32 | */ 33 | 34 | #ifndef _LIBMAPLE_STM32F3_EXTI_H_ 35 | #define _LIBMAPLE_STM32F3_EXTI_H_ 36 | 37 | #ifdef __cpluspus 38 | extern "C" { 39 | #endif 40 | 41 | #include "libmaple_types.h" 42 | #include "syscfg.h" 43 | 44 | /* 45 | * Register map and base pointer. 46 | */ 47 | 48 | /** EXTI register map type */ 49 | typedef struct exti_reg_map { 50 | __IO uint32 IMR; /**< Interrupt mask register */ 51 | __IO uint32 EMR; /**< Event mask register */ 52 | __IO uint32 RTSR; /**< Rising trigger selection register */ 53 | __IO uint32 FTSR; /**< Falling trigger selection register */ 54 | __IO uint32 SWIER; /**< Software interrupt event register */ 55 | __IO uint32 PR; /**< Pending register */ 56 | } exti_reg_map; 57 | 58 | 59 | #define EXTI_RTC_ALARM BIT(17) 60 | #define EXTI_RTC_WAKEUP BIT(20) 61 | #define EXTI_RTC_TAMPSTAMP BIT(19) 62 | 63 | 64 | 65 | /* 66 | * Types: exti_num, exti_cfg, exti_trigger_mode. 67 | * 68 | * A combination of these three specifies an external interrupt 69 | * configuration (see exti_attach_interrupt()). 70 | */ 71 | 72 | /** EXTI line. */ 73 | typedef enum exti_num { 74 | EXTI0, /**< EXTI line 0 */ 75 | EXTI1, /**< EXTI line 1 */ 76 | EXTI2, /**< EXTI line 2 */ 77 | EXTI3, /**< EXTI line 3 */ 78 | EXTI4, /**< EXTI line 4 */ 79 | EXTI5, /**< EXTI line 5 */ 80 | EXTI6, /**< EXTI line 6 */ 81 | EXTI7, /**< EXTI line 7 */ 82 | EXTI8, /**< EXTI line 8 */ 83 | EXTI9, /**< EXTI line 9 */ 84 | EXTI10, /**< EXTI line 10 */ 85 | EXTI11, /**< EXTI line 11 */ 86 | EXTI12, /**< EXTI line 12 */ 87 | EXTI13, /**< EXTI line 13 */ 88 | EXTI14, /**< EXTI line 14 */ 89 | EXTI15, /**< EXTI line 15 */ 90 | } exti_num; 91 | 92 | /** 93 | * @brief EXTI port configuration 94 | * 95 | * These specify which GPIO port an external interrupt line should be 96 | * connected to. 97 | */ 98 | typedef enum exti_cfg { 99 | EXTI_PA, /**< Use PAx pin */ 100 | EXTI_PB, /**< Use PBx pin */ 101 | EXTI_PC, /**< Use PCx pin */ 102 | } exti_cfg; 103 | 104 | /** External interrupt trigger mode */ 105 | typedef enum exti_trigger_mode { 106 | EXTI_RISING, /**< Trigger on the rising edge */ 107 | EXTI_FALLING, /**< Trigger on the falling edge */ 108 | EXTI_RISING_FALLING /**< Trigger on both the rising and falling edges */ 109 | } exti_trigger_mode; 110 | 111 | /* 112 | * Routines 113 | */ 114 | 115 | void exti_attach_callback(exti_num num, 116 | exti_cfg port, 117 | voidArgumentFuncPtr handler, 118 | void *arg, 119 | exti_trigger_mode mode); 120 | 121 | static inline void exti_attach_interrupt(exti_num num, 122 | exti_cfg port, 123 | voidFuncPtr handler, 124 | exti_trigger_mode mode) { 125 | // Call callback version with arg being null 126 | exti_attach_callback(num, port, (voidArgumentFuncPtr)handler, NULL, mode); 127 | } 128 | 129 | void exti_detach_interrupt(exti_num num); 130 | 131 | void exti_do_select(__IO uint32_t *exti_cr, exti_num num, exti_cfg port); 132 | 133 | /** 134 | * @brief Set the GPIO port for an EXTI line. 135 | * 136 | * This is a low-level routine that most users will not 137 | * need. exti_attach_interrupt() handles calling this function 138 | * appropriately. 139 | * 140 | * @param num EXTI line 141 | * @param port EXTI configuration for GPIO port to connect to num. 142 | * @see exti_num 143 | * @see exti_cfg 144 | */ 145 | static inline void exti_select(exti_num num, exti_cfg cfg) { 146 | exti_do_select(&SYSCFG->EXTICR[num / 4], num, cfg); 147 | } 148 | 149 | 150 | 151 | #ifdef __cpluspus 152 | } 153 | #endif 154 | 155 | #endif 156 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/flash.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * Copyright (c) 2011, 2012 LeafLabs, LLC. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/flash.c 30 | * @brief Flash management functions 31 | */ 32 | 33 | #include "libmaple_types.h" 34 | #include "flash.h" 35 | #include "nvic.h" 36 | 37 | /** 38 | * @brief Set flash wait states 39 | * 40 | * Note that not all wait states are available on every MCU. See the 41 | * Flash programming manual for your MCU for restrictions on the 42 | * allowed value of wait_states for a given system clock (SYSCLK) 43 | * frequency. 44 | * 45 | * @param wait_states number of wait states (one of 46 | * FLASH_WAIT_STATE_0, FLASH_WAIT_STATE_1, 47 | * ..., FLASH_WAIT_STATE_7). 48 | */ 49 | void flash_set_latency(uint32 wait_states) 50 | { 51 | uint32 val = FLASH->ACR; 52 | 53 | val &= ~FLASH_ACR_LATENCY; 54 | val |= wait_states; 55 | 56 | FLASH->ACR = val; 57 | } 58 | 59 | //----------------------------------------------------------------------------- 60 | // Reference: RM0008 chap. 3.3.3. 61 | //----------------------------------------------------------------------------- 62 | /* 63 | * The right sequence is : 64 | 65 | ERASE: 66 | - check busy flag (wait till it is reset) 67 | - Set PER bit to enable the erase 68 | - write the address of the page to be erased into FLASH_AR register (the entire page containing the address will be erased). 69 | - Set STRT bit to trigger the erase 70 | - Wait till Busy flag or STRT flag is cleared by hardware (it takes a time) 71 | - Read and verify that the location is erased (should have 0xFFFF) 72 | 73 | WRITE: 74 | - Unlock the flash 75 | - Wait on BUSY 76 | - Set the PG bit 77 | - Write to destination (16-bit in a half-word aligned address) 78 | - Wait on BUSY 79 | */ 80 | //----------------------------------------------------------------------------- 81 | void flash_erase_page(uint16_t *page) 82 | { 83 | // Unlock Flash with magic keys 84 | flash_unlock(); 85 | 86 | // Erase page 87 | flash_set_cr(FLASH_CR_PER); // erase page flag 88 | flash_set_page((uint32_t) page); 89 | flash_start(); 90 | } 91 | 92 | //----------------------------------------------------------------------------- 93 | void flash_write_data(uint16_t *page, uint16_t *data, uint16_t size) 94 | { 95 | flash_set_cr(FLASH_CR_PG); // erase program flag 96 | 97 | while (size--) 98 | { 99 | *page++ = *data++; 100 | 101 | flash_wait_for_ready(); 102 | } 103 | } 104 | 105 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/flash.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * Copyright (c) 2013 OpenMusicKontrollers. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/stm32f3/include/series/flash.h 30 | * @author F3-port by Hanspeter Portner 31 | * @brief STM32F3 Flash header. 32 | * 33 | * Provides register map, base pointer, and register bit definitions 34 | * for the Flash controller on the STM32F3 line, along with 35 | * series-specific configuration values. 36 | */ 37 | 38 | #ifndef _LIBMAPLE_STM32F3_FLASH_H_ 39 | #define _LIBMAPLE_STM32F3_FLASH_H_ 40 | 41 | #ifdef __cplusplus 42 | extern "C"{ 43 | #endif 44 | 45 | #include "libmaple_types.h" 46 | #include "stm32f3xx.h" 47 | 48 | 49 | 50 | 51 | 52 | /* 53 | * Series-specific configuration values. 54 | */ 55 | 56 | #define FLASH_SAFE_WAIT_STATES FLASH_WAIT_STATE_2 57 | 58 | /* Flash memory features available via ACR */ 59 | enum { 60 | FLASH_PREFETCH = FLASH_ACR_PRFTBE, 61 | FLASH_HALF_CYCLE = 0x8, 62 | FLASH_ICACHE = 0x0, /* Not available on STM32F3 */ 63 | FLASH_DCACHE = 0x0, /* Not available on STM32F3 */ 64 | }; 65 | 66 | /* TODO add routines for option byte handling, e.g. nBoot1 */ 67 | 68 | #define FLASH_WAIT_STATE_0 0x0 69 | #define FLASH_WAIT_STATE_1 0x1 70 | #define FLASH_WAIT_STATE_2 0x2 71 | #define FLASH_WAIT_STATE_3 0x3 72 | #define FLASH_WAIT_STATE_4 0x4 73 | #define FLASH_WAIT_STATE_5 0x5 74 | #define FLASH_WAIT_STATE_6 0x6 75 | #define FLASH_WAIT_STATE_7 0x7 76 | 77 | /* The series header must define: 78 | * 79 | * - FLASH_SAFE_WAIT_STATES, the smallest number of wait states that 80 | * it is safe to use when SYSCLK is at its fastest documented rate 81 | * and the MCU is powered at 3.3V (i.e. this doesn't consider 82 | * overclocking or low voltage operation). 83 | * 84 | * - The following bit flags, for flash_enable_features(): 85 | * 86 | * -- FLASH_PREFETCH: prefetcher 87 | * -- FLASH_ICACHE: instruction cache 88 | * -- FLASH_DCACHE: data cache 89 | * 90 | * See that function's Doxygen for more restrictions. 91 | */ 92 | 93 | #ifdef __DOXYGEN__ 94 | /** Flash register map base pointer. */ 95 | #define FLASH_BASE 96 | #endif 97 | 98 | 99 | 100 | /* 101 | * Flash routines 102 | */ 103 | 104 | extern void flash_set_latency(uint32 wait_states); 105 | void flash_erase_page(uint16_t *page); 106 | extern void flash_write_data(uint16_t *page, uint16_t *data, uint16_t size); 107 | 108 | /** 109 | * @brief Enable Flash memory features 110 | * 111 | * If the target MCU doesn't provide a feature (e.g. instruction and 112 | * data caches on the STM32F1), the flag will be ignored. This allows 113 | * using these flags unconditionally, with the desired effect taking 114 | * place on targets that support them. 115 | * 116 | * @param feature_flags Bitwise OR of the following: 117 | * FLASH_PREFETCH (turns on prefetcher), 118 | * FLASH_ICACHE (turns on instruction cache), 119 | * FLASH_DCACHE (turns on data cache). 120 | */ 121 | static inline void flash_enable_features(uint32 feature_flags) { 122 | FLASH->ACR |= feature_flags; 123 | } 124 | 125 | /** 126 | * @brief Deprecated. Use flash_enable_features(FLASH_PREFETCH) instead. 127 | */ 128 | static inline void flash_enable_prefetch(void) { 129 | flash_enable_features(FLASH_PREFETCH); 130 | } 131 | 132 | static inline void flash_wait_for_ready(void) { 133 | while (FLASH->SR & FLASH_SR_BSY); 134 | } 135 | 136 | static inline int flash_locked() { 137 | return (FLASH->CR & FLASH_CR_LOCK); 138 | } 139 | 140 | static inline void flash_lock() { 141 | FLASH->CR = FLASH_CR_LOCK; 142 | flash_wait_for_ready(); 143 | } 144 | 145 | static inline void flash_unlock(void) { 146 | // Unlock Flash with magic keys 147 | FLASH->KEYR = FLASH_KEY1; 148 | FLASH->KEYR = FLASH_KEY2; 149 | flash_wait_for_ready(); 150 | } 151 | 152 | static inline void flash_start() { 153 | FLASH->CR |= FLASH_CR_STRT; 154 | flash_wait_for_ready(); 155 | } 156 | 157 | static inline void flash_set_cr(int cr) 158 | { 159 | FLASH->CR = cr; 160 | } 161 | 162 | static inline void flash_set_page(uint32 page) { 163 | FLASH->AR = page; 164 | } 165 | 166 | 167 | #ifdef __cplusplus 168 | } 169 | #endif 170 | 171 | #endif 172 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/libmaple.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/libmaple.h 29 | * @brief General include file for libmaple 30 | */ 31 | 32 | #ifndef _LIBMAPLE_LIBMAPLE_H_ 33 | #define _LIBMAPLE_LIBMAPLE_H_ 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | #include "libmaple_types.h" 40 | #include "stm32.h" 41 | #include "util.h" 42 | #include "delay.h" 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/libmaple_types.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/libmaple_types.h 29 | * 30 | * @brief libmaple's types, and operations on types. 31 | */ 32 | 33 | #ifndef _LIBMAPLE_LIBMAPLE_TYPES_H_ 34 | #define _LIBMAPLE_LIBMAPLE_TYPES_H_ 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | #include 41 | #include 42 | #include 43 | 44 | typedef unsigned char uint8; 45 | typedef unsigned short uint16; 46 | typedef unsigned int uint32; 47 | typedef unsigned long long uint64; 48 | 49 | typedef signed char int8; 50 | typedef short int16; 51 | typedef int int32; 52 | typedef long long int64; 53 | 54 | typedef void (*voidFuncPtr)(void); 55 | typedef void (*voidArgumentFuncPtr)(void *); 56 | 57 | #define __IO volatile 58 | #define __attr_flash __attribute__((section (".USER_FLASH"))) 59 | #define __packed __attribute__((__packed__)) 60 | #define __deprecated __attribute__((__deprecated__)) 61 | #define __weak __attribute__((weak)) 62 | 63 | #ifndef NULL 64 | #define NULL 0 65 | #endif 66 | #ifndef true 67 | #define true 1 68 | #endif 69 | #ifndef false 70 | #define false 0 71 | #endif 72 | #ifndef offsetof 73 | #define __builtin_offsetof(type, member) __offsetof(type, member) 74 | #define offsetof(type, member) __builtin_offsetof(type, member) 75 | #endif 76 | 77 | #ifdef __cplusplus 78 | } 79 | #endif 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/nvic.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * Copyright (c) 2011 LeafLabs, LLC. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/nvic.c 30 | * @brief Nested vector interrupt controller support. 31 | */ 32 | 33 | #include "nvic.h" 34 | 35 | /** 36 | * @brief Set interrupt priority for an interrupt line 37 | * 38 | * Note: The STM32 only implements 4 bits of priority, ignoring the 39 | * lower 4 bits. This means there are only 16 levels of priority. 40 | * Bits[3:0] read as zero and ignore writes. 41 | * 42 | * @param irqn device to set 43 | * @param priority Priority to set, 0 being highest priority and 15 44 | * being lowest. 45 | */ 46 | void nvic_irq_set_priority(nvic_irq_num irqn, uint8 priority) { 47 | if (irqn < 0) { 48 | /* This interrupt is in the system handler block */ 49 | SCB->SHP[((uint32)irqn & 0xF) - 4] = (priority & 0xF) << 4; 50 | } else { 51 | NVIC->IP[irqn] = (priority & 0xF) << 4; 52 | } 53 | } 54 | 55 | /** 56 | * @brief Initialize the NVIC, setting interrupts to a default priority. 57 | */ 58 | void nvic_init(uint32 address, uint32 offset) { 59 | uint32 i; 60 | 61 | nvic_set_vector_table(address, offset); 62 | 63 | /* 64 | * Lower priority level for all peripheral interrupts to lowest 65 | * possible. 66 | */ 67 | for (i = 0; i < STM32_NR_INTERRUPTS; i++) { 68 | nvic_irq_set_priority((nvic_irq_num)i, 0xF); 69 | } 70 | 71 | /* Lower systick interrupt priority to lowest level */ 72 | nvic_irq_set_priority(NVIC_SYSTICK, 0xF); 73 | } 74 | 75 | /** 76 | * @brief Force a system reset. 77 | * 78 | * Resets all major system components, excluding debug. 79 | */ 80 | #define SCB_AIRCR_VECTKEY (0x5FA << 16) 81 | 82 | void nvic_sys_reset() { 83 | uint32 prigroup = SCB->AIRCR & SCB_AIRCR_PRIGROUP_Msk; 84 | SCB->AIRCR = SCB_AIRCR_VECTKEY | SCB_AIRCR_SYSRESETREQ_Msk | prigroup; 85 | asm volatile("dsb"); 86 | while (1) 87 | ; 88 | } 89 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/pwr.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2011 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/pwr.c 29 | * @brief Power control (PWR) support. 30 | */ 31 | 32 | #include "pwr.h" 33 | #include "rcc.h" 34 | 35 | /** 36 | * Enables the power interface clock, and resets the power device. 37 | */ 38 | void pwr_init(void) { 39 | rcc_clk_enable(RCC_PWR); 40 | rcc_reset_dev(RCC_PWR); 41 | } 42 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/pwr.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 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/pwr.h 29 | * @brief Power control (PWR). 30 | */ 31 | 32 | #ifndef _LIBMAPLE_PWR_H_ 33 | #define _LIBMAPLE_PWR_H_ 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | #include "libmaple.h" 40 | #include "stm32f3xx.h" 41 | 42 | 43 | /* 44 | * Convenience functions 45 | */ 46 | 47 | void pwr_init(void); 48 | 49 | #ifdef __cplusplus 50 | } 51 | #endif 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/rcc_private.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2011 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /* 28 | * RCC private header. 29 | */ 30 | 31 | #ifndef _LIBMAPLE_PRIVATE_RCC_H_ 32 | #define _LIBMAPLE_PRIVATE_RCC_H_ 33 | 34 | #include "bitband.h" 35 | 36 | struct rcc_dev_info { 37 | const rcc_clk_domain clk_domain; 38 | const uint8 line_num; 39 | }; 40 | 41 | extern const struct rcc_dev_info rcc_dev_table[]; 42 | 43 | /** 44 | * @brief Get a peripheral's clock domain 45 | * @param id Clock ID of the peripheral whose clock domain to return 46 | * @return Clock source for the given clock ID 47 | */ 48 | static inline rcc_clk_domain rcc_dev_clk(rcc_clk_id id) { 49 | return rcc_dev_table[id].clk_domain; 50 | } 51 | 52 | extern void rcc_do_clk_set(rcc_clk_id id, int en); 53 | extern void rcc_do_set_prescaler(rcc_prescaler prescaler, uint32 divider); 54 | 55 | static inline void rcc_clk_enable(rcc_clk_id id) { 56 | rcc_do_clk_set(id, 1); 57 | } 58 | 59 | static inline void rcc_clk_disable(rcc_clk_id id) { 60 | rcc_do_clk_set(id, 0); 61 | } 62 | 63 | 64 | void rcc_do_reset_dev(rcc_clk_id id); 65 | 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/stm32_private.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2012 LeafLabs, LLC. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | #ifndef _LIBMAPLE_STM32_PRIVATE_H_ 28 | #define _LIBMAPLE_STM32_PRIVATE_H_ 29 | 30 | typedef enum stm32_mem_block_purpose { 31 | STM32_BLOCK_CODE, 32 | STM32_BLOCK_SRAM, 33 | STM32_BLOCK_PERIPH, 34 | STM32_BLOCK_FSMC_1_2, 35 | STM32_BLOCK_FSMC_3_4, 36 | STM32_BLOCK_FSMC_REG, 37 | STM32_BLOCK_UNUSED, 38 | STM32_BLOCK_CORTEX_INTERNAL, 39 | } stm32_mem_block_purpose; 40 | 41 | static inline stm32_mem_block_purpose stm32_block_purpose(void *addr) { 42 | return (stm32_mem_block_purpose)((unsigned)addr >> 29); 43 | } 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/syscfg.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2012 LeafLabs, LLC. 5 | * Copyright (c) 2013 OpenMusicKontrollers. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/stm32f3/syscfg.c 30 | * @author F3-port by Hanspeter Portner 31 | * @brief SYSCFG routines. 32 | */ 33 | 34 | #include "syscfg.h" 35 | #include "bitband.h" 36 | #include "rcc.h" 37 | #include "syscfg.h" 38 | 39 | 40 | void syscfg_init(void) { 41 | rcc_clk_enable(RCC_SYSCFG); 42 | rcc_reset_dev(RCC_SYSCFG); 43 | } 44 | 45 | void syscfg_set_mem_mode(syscfg_mem_mode mode) { 46 | __IO uint32_t memrmp = SYSCFG->CFGR1; 47 | memrmp &= ~SYSCFG_CFGR1_MEM_MODE; 48 | memrmp |= (uint32)mode; 49 | SYSCFG->CFGR1 = memrmp; 50 | } 51 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/syscfg.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2013 OpenMusicKontrollers. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/stm32f3/include/series/syscfg.h 29 | * @author F3-port by Hanspeter Portner 30 | * @brief System configuration controller (SYSCFG) 31 | */ 32 | 33 | #ifndef _LIBMAPLE_STM32F3_SYSCFG_H_ 34 | #define _LIBMAPLE_STM32F3_SYSCFG_H_ 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | #include "libmaple_types.h" 41 | #include "stm32f3xx.h" 42 | 43 | 44 | /* 45 | * Register bit definitions 46 | */ 47 | 48 | #include "rcc.h" 49 | 50 | 51 | /* 52 | * Routines 53 | */ 54 | 55 | /** 56 | * @brief Initialize the SYSCFG peripheral. 57 | */ 58 | void syscfg_init(void); 59 | 60 | /** 61 | * @brief System memory mode 62 | * These values specify what memory to map to address 0x00000000. 63 | * @see syscfg_set_mem_mode 64 | */ 65 | typedef enum syscfg_mem_mode { 66 | /** Main flash memory is mapped at 0x0. */ 67 | SYSCFG_MEM_MODE_FLASH = 0x0, 68 | /** System flash (i.e. ST's baked-in bootloader) is mapped at 0x0. */ 69 | SYSCFG_MEM_MODE_SYSTEM_FLASH = 0x1, 70 | /** FSMC bank 1 (NOR/PSRAM 1 and 2) is mapped at 0x0. */ 71 | SYSCFG_MEM_MODE_FSMC_BANK_1 = 0x2, 72 | /** Embedded SRAM (i.e., not backup SRAM) is mapped at 0x0. */ 73 | SYSCFG_MEM_MODE_SRAM = 0x3, 74 | } syscfg_mem_mode; 75 | 76 | 77 | /** 78 | * @brief Set the memory to be mapped at address 0x00000000. 79 | * 80 | * This function can be used to override the BOOT pin 81 | * configuration. Some restrictions apply; see your chip's reference 82 | * manual for the details. 83 | * 84 | * @param mode Mode to set 85 | * @see syscfg_mem_mode 86 | */ 87 | void syscfg_set_mem_mode(syscfg_mem_mode); 88 | 89 | 90 | 91 | #ifdef __cplusplus 92 | } 93 | #endif 94 | 95 | #endif 96 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/system.c: -------------------------------------------------------------------------------- 1 | /* 2 | * system.c 3 | * 4 | * Created on: Apr 25, 2020 5 | * Author: stevestrong 6 | */ 7 | 8 | 9 | #include "flash.h" 10 | #include "rcc.h" 11 | #include "gpio.h" 12 | #include "board.h" 13 | 14 | 15 | // Allow boards to provide a PLL multiplier. This is useful for 16 | // e.g. STM32F100 value line MCUs, which use slower multipliers. 17 | // (We're leaving the default to RCC_PLLMUL_9 for now, since that 18 | // works for F103 performance line MCUs, which is all that LeafLabs 19 | // currently officially supports). 20 | #ifndef BOARD_RCC_PLLMUL 21 | #if !USE_HSI_CLOCK 22 | #if F_CPU==128000000 23 | #define BOARD_RCC_PLLMUL RCC_PLLMUL_16 24 | #elif F_CPU==72000000 25 | #define BOARD_RCC_PLLMUL RCC_PLLMUL_9 26 | #elif F_CPU==48000000 27 | #define BOARD_RCC_PLLMUL RCC_PLLMUL_6 28 | #elif F_CPU==16000000 29 | #define BOARD_RCC_PLLMUL RCC_PLLMUL_2 30 | #endif 31 | #else 32 | #define BOARD_RCC_PLLMUL RCC_PLLMUL_16 33 | #endif 34 | #endif 35 | 36 | 37 | static stm32f3_rcc_pll_data pll_data = {.pll_mul=BOARD_RCC_PLLMUL, .pclk_prediv=RCC_PREDIV_PCLK_DIV_1}; 38 | __weak rcc_pll_cfg w_board_pll_cfg = {RCC_PLLSRC_HSE, &pll_data}; 39 | 40 | void board_setup_clock_prescalers(void) { 41 | rcc_set_prescaler(RCC_PRESCALER_AHB, RCC_AHB_SYSCLK_DIV_1); 42 | rcc_set_prescaler(RCC_PRESCALER_APB1, RCC_APB1_HCLK_DIV_2); 43 | rcc_set_prescaler(RCC_PRESCALER_APB2, RCC_APB2_HCLK_DIV_1); 44 | rcc_clk_disable(RCC_USB); 45 | #if F_CPU == 72000000 46 | rcc_set_prescaler(RCC_PRESCALER_USB, RCC_USB_SYSCLK_DIV_1_5); 47 | #elif F_CPU == 48000000 48 | rcc_set_prescaler(RCC_PRESCALER_USB, RCC_USB_SYSCLK_DIV_1); 49 | #endif 50 | } 51 | 52 | //----------------------------------------------------------------------------- 53 | void Setup_flash(void) 54 | { 55 | // Turn on as many Flash "go faster" features as 56 | // possible. flash_enable_features() just ignores any flags it 57 | // can't support. 58 | flash_enable_features(FLASH_PREFETCH | FLASH_ICACHE | FLASH_DCACHE); 59 | // Configure the wait states, assuming we're operating at "close 60 | // enough" to 3.3V. 61 | flash_set_latency(FLASH_SAFE_WAIT_STATES); 62 | } 63 | //----------------------------------------------------------------------------- 64 | void Setup_clocks(void) 65 | { 66 | // Turn on HSI. We'll switch to and run off of this while we're 67 | // setting up the main PLL. 68 | rcc_turn_on_clk(RCC_CLK_HSI); 69 | while (!rcc_is_clk_ready(RCC_CLK_HSI)) 70 | ; 71 | 72 | // Turn off and reset the clock subsystems we'll be using, as well 73 | // as the clock security subsystem (CSS). Note that resetting CFGR 74 | // to its default value of 0 implies a switch to HSI for SYSCLK. 75 | RCC_REGS->CFGR = 0x00000000; 76 | rcc_disable_css(); 77 | rcc_turn_off_clk(RCC_CLK_PLL); 78 | rcc_turn_off_clk(RCC_CLK_HSE); 79 | // Clear clock readiness interrupt flags and turn off clock 80 | // readiness interrupts. 81 | RCC_REGS->CIR = 0x00000000; 82 | 83 | // Enable HSE, and wait until it's ready. 84 | rcc_turn_on_clk(RCC_CLK_HSE); 85 | while (!rcc_is_clk_ready(RCC_CLK_HSE)) 86 | ; 87 | 88 | // Configure AHBx, APBx, etc. prescalers and the main PLL. 89 | board_setup_clock_prescalers(); 90 | rcc_configure_pll(&w_board_pll_cfg); 91 | 92 | // Enable the PLL, and wait until it's ready. 93 | rcc_turn_on_clk(RCC_CLK_PLL); 94 | while(!rcc_is_clk_ready(RCC_CLK_PLL)) 95 | ; 96 | 97 | // Finally, switch to the now-ready PLL as the main clock source. 98 | rcc_switch_sysclk(RCC_CLKSRC_PLL); 99 | 100 | } 101 | 102 | 103 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/systick.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * Copyright (c) 2010, 2011 LeafLabs, LLC. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, copy, 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies 12 | * of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | *****************************************************************************/ 27 | 28 | /** 29 | * @file libmaple/systick.c 30 | * @brief System timer (SysTick). 31 | */ 32 | 33 | #include "systick.h" 34 | #include "delay.h" 35 | 36 | volatile uint32 systick_uptime_millis; 37 | static void (*systick_user_callback)(void); 38 | 39 | /** 40 | * @brief Initialize and enable SysTick. 41 | * 42 | * Clocks the system timer with the core clock, turns it on, and 43 | * enables interrupts. 44 | * 45 | * @param reload_val Appropriate reload counter to tick every 1 ms. 46 | */ 47 | void systick_init() { 48 | SYSTICK_REGS->RVR = SYSTICK_RELOAD_VAL; 49 | systick_enable(); 50 | } 51 | 52 | /** 53 | * Clock the system timer with the core clock, but don't turn it 54 | * on or enable interrupt. 55 | */ 56 | void systick_disable() { 57 | SYSTICK_REGS->CSR = SYSTICK_CSR_CLKSOURCE_CORE; 58 | } 59 | 60 | /** 61 | * Clock the system timer with the core clock and turn it on; 62 | * interrupt every 1 ms, for systick_timer_millis. 63 | */ 64 | void systick_enable() { 65 | /* re-enables init registers without changing reload val */ 66 | SYSTICK_REGS->CSR = (SYSTICK_CSR_CLKSOURCE_CORE | 67 | SYSTICK_CSR_ENABLE | 68 | SYSTICK_CSR_TICKINT_PEND); 69 | } 70 | 71 | /** 72 | * @brief Attach a callback to be called from the SysTick exception handler. 73 | * 74 | * To detach a callback, call this function again with a null argument. 75 | */ 76 | void systick_attach_callback(voidFuncPtr callback) { 77 | systick_user_callback = callback; 78 | } 79 | 80 | /* 81 | * SysTick ISR 82 | */ 83 | 84 | __weak void __exc_systick(void) { 85 | systick_uptime_millis++; 86 | if (systick_user_callback) { 87 | systick_user_callback(); 88 | } 89 | } 90 | //----------------------------------------------------------------------------- 91 | __weak void yield() {} 92 | //----------------------------------------------------------------------------- 93 | void delay(uint32 ms) 94 | { 95 | uint32 start = micros(); 96 | while (ms > 0) 97 | { 98 | yield(); 99 | while ( (ms > 0) && ((micros() - start) >= 1000) ) 100 | { 101 | ms--; 102 | start += 1000; 103 | } 104 | } 105 | } 106 | 107 | //----------------------------------------------------------------------------- 108 | void delayMicroseconds(uint32 us) { 109 | delay_us(us); 110 | } 111 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/systick.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/include/libmaple/systick.h 29 | * @brief System timer definitions 30 | */ 31 | 32 | #ifndef _LIBMAPLE_SYSTICK_H_ 33 | #define _LIBMAPLE_SYSTICK_H_ 34 | 35 | #ifdef __cplusplus 36 | extern "C"{ 37 | #endif 38 | 39 | #include "libmaple_types.h" 40 | #include "util.h" 41 | 42 | /** SysTick register map type */ 43 | typedef struct systick_reg_map { 44 | __IO uint32 CSR; /**< Control and status register */ 45 | __IO uint32 RVR; /**< Reload value register */ 46 | __IO uint32 CNT; /**< Current value register ("count") */ 47 | __IO uint32 CVR; /**< Calibration value register */ 48 | } systick_reg_map; 49 | 50 | /** SysTick register map base pointer */ 51 | #define SYSTICK_REGS ((struct systick_reg_map*)0xE000E010) 52 | 53 | /* 54 | * Register bit definitions. 55 | */ 56 | 57 | /* Control and status register */ 58 | 59 | #define SYSTICK_CSR_COUNTFLAG BIT(16) 60 | #define SYSTICK_CSR_CLKSOURCE BIT(2) 61 | #define SYSTICK_CSR_CLKSOURCE_EXTERNAL 0 62 | #define SYSTICK_CSR_CLKSOURCE_CORE BIT(2) 63 | #define SYSTICK_CSR_TICKINT BIT(1) 64 | #define SYSTICK_CSR_TICKINT_PEND BIT(1) 65 | #define SYSTICK_CSR_TICKINT_NO_PEND 0 66 | #define SYSTICK_CSR_ENABLE BIT(0) 67 | #define SYSTICK_CSR_ENABLE_MULTISHOT BIT(0) 68 | #define SYSTICK_CSR_ENABLE_DISABLED 0 69 | 70 | /* Calibration value register */ 71 | 72 | #define SYSTICK_CVR_NOREF BIT(31) 73 | #define SYSTICK_CVR_SKEW BIT(30) 74 | #define SYSTICK_CVR_TENMS 0xFFFFFF 75 | 76 | /** System elapsed time, in milliseconds */ 77 | extern volatile uint32 systick_uptime_millis; 78 | 79 | 80 | /** 81 | * @brief Returns the system uptime, in milliseconds. 82 | */ 83 | static inline uint32 systick_uptime(void) { 84 | return systick_uptime_millis; 85 | } 86 | 87 | /** 88 | * Returns time (in milliseconds) since the beginning of program 89 | * execution. On overflow, restarts at 0. 90 | * @see micros() 91 | */ 92 | static inline uint32 millis(void) { 93 | return systick_uptime(); 94 | } 95 | 96 | void __exc_systick(void); 97 | void systick_init(); 98 | void systick_disable(); 99 | void systick_enable(); 100 | 101 | /** 102 | * @brief Returns the current value of the SysTick counter. 103 | */ 104 | static inline uint32 systick_get_count(void) { 105 | return SYSTICK_REGS->CNT; 106 | } 107 | 108 | /** 109 | * @brief Check for underflow. 110 | * 111 | * This function returns 1 if the SysTick timer has counted to 0 since 112 | * the last time it was called. However, any reads of any part of the 113 | * SysTick Control and Status Register SYSTICK_BASE->CSR will 114 | * interfere with this functionality. See the ARM Cortex M3 Technical 115 | * Reference Manual for more details (e.g. Table 8-3 in revision r1p1). 116 | */ 117 | static inline uint32 systick_check_underflow(void) { 118 | return SYSTICK_REGS->CSR & SYSTICK_CSR_COUNTFLAG; 119 | } 120 | 121 | /** 122 | * @brief Attach a callback to be called from the SysTick exception handler. 123 | * 124 | * To detach a callback, call this function again with a null argument. 125 | */ 126 | void systick_attach_callback(voidFuncPtr callback); 127 | 128 | #ifdef __cplusplus 129 | } // extern "C" 130 | #endif 131 | 132 | #endif 133 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/libmaple/util.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2010 Perry Hung. 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | *****************************************************************************/ 26 | 27 | /** 28 | * @file libmaple/util.h 29 | * @brief Miscellaneous utility macros and procedures. 30 | */ 31 | 32 | #ifndef _LIBMAPLE_UTIL_H_ 33 | #define _LIBMAPLE_UTIL_H_ 34 | 35 | #ifdef __cplusplus 36 | extern "C"{ 37 | #endif 38 | 39 | #include "libmaple_types.h" 40 | 41 | 42 | /* 43 | * Bit manipulation 44 | */ 45 | 46 | /** 1UL shifted left by 'shift' */ 47 | #define BIT(shift) (1UL << (shift)) 48 | /** 'Mask' shifted left by 'shift' */ 49 | #define BIT_MASK_SHIFT(mask, shift) ((mask) << (shift)) 50 | /** Bits m to n of x */ 51 | #define GET_BITS(x, m, n) ((((uint32)x) << (31 - (n))) >> ((31 - (n)) + (m))) 52 | /** True iff v is a power of two (1, 2, 4, 8, ...) */ 53 | #define IS_POWER_OF_TWO(v) ((v) && !((v) & ((v) - 1))) 54 | 55 | /* 56 | * Failure routines 57 | */ 58 | 59 | void __error(void); 60 | void _fail(const char*, int, const char*); 61 | void throb(void); 62 | 63 | /* 64 | * Asserts and debug levels 65 | */ 66 | 67 | #define DEBUG_NONE 0 68 | #define DEBUG_FAULT 1 69 | #define DEBUG_ALL 2 70 | 71 | /** 72 | * \def DEBUG_LEVEL 73 | * 74 | * Controls the level of assertion checking. 75 | * 76 | * The higher the debug level, the more assertions will be compiled 77 | * in. This increases the amount of debugging information, but slows 78 | * down (and increases the size of) the binary. 79 | * 80 | * The debug levels, from lowest to highest, are DEBUG_NONE, 81 | * DEBUG_FAULT, and DEBUG_ALL. The default level is DEBUG_ALL. 82 | */ 83 | 84 | #ifndef DEBUG_LEVEL 85 | #define DEBUG_LEVEL DEBUG_NONE //DEBUG_ALL 86 | #endif 87 | 88 | #if DEBUG_LEVEL >= DEBUG_ALL 89 | #define ASSERT(exp) \ 90 | if (exp) { \ 91 | } else { \ 92 | _fail(__FILE__, __LINE__, #exp); \ 93 | } 94 | #else 95 | #define ASSERT(exp) (void)((0)) 96 | #endif 97 | 98 | #if DEBUG_LEVEL >= DEBUG_FAULT 99 | #define ASSERT_FAULT(exp) \ 100 | if (exp) { \ 101 | } else { \ 102 | _fail(__FILE__, __LINE__, #exp); \ 103 | } 104 | #else 105 | #define ASSERT_FAULT(exp) (void)((0)) 106 | #endif 107 | 108 | #ifdef __cplusplus 109 | } // extern "C" 110 | #endif 111 | 112 | #endif 113 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/usb_def.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stevstrong/STM32-CDC-Bootloader/d53886de2380a4812abd720e6777ede4b293cc57/F3/eclipse_project/src/usb_def.h -------------------------------------------------------------------------------- /F3/eclipse_project/src/usb_desc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * usb_desc.h 3 | * 4 | * Created on: Apr 6, 2020 5 | * Author: Zo 6 | */ 7 | 8 | #ifndef USB_DESC_H_ 9 | #define USB_DESC_H_ 10 | 11 | 12 | #if 1 13 | #define USB_VID 0x1EAF // 0x0416 /* Vendor ID (von RealTek) */ 14 | #define USB_PID 0x0002 // 0x5011 /* Product ID */ 15 | #else 16 | #define USB_VID 0xDEAD 17 | #define USB_PID 0xBEEF 18 | #endif 19 | 20 | // assignment of the USB EP numbers - bEndpointAddress 21 | enum { EP_CTRL, EP_DATA, EP_COMM, EP_LAST }; 22 | 23 | #define NUM_IFACES 2 // COMM + DATA 24 | 25 | #define EP_CTRL_ADDR_IN USB_EP_ADDR_IN(EP_CTRL) 26 | #define EP_CTRL_ADDR_OUT USB_EP_ADDR_OUT(EP_CTRL) 27 | #define EP_COMM_ADDR_IN USB_EP_ADDR_IN(EP_COMM) 28 | #define EP_COMM_ADDR_OUT USB_EP_ADDR_OUT(EP_COMM) 29 | #define EP_DATA_ADDR_IN USB_EP_ADDR_IN(EP_DATA) 30 | #define EP_DATA_ADDR_OUT USB_EP_ADDR_OUT(EP_DATA) 31 | 32 | 33 | #endif /* USB_DESC_H_ */ 34 | -------------------------------------------------------------------------------- /F3/eclipse_project/src/usb_func.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef USB_FUNC_H 3 | #define USB_FUNC_H 4 | 5 | #include 6 | #include "usb_def.h" 7 | #include "usb_desc.h" 8 | #include "cdc.h" 9 | #include "gpio.h" 10 | #include "flash.h" 11 | #include "nvic.h" 12 | 13 | 14 | /******* Struktur des Setup-Paketes *****/ 15 | typedef struct 16 | { 17 | uint8_t bmRequestType; /* siehe oben */ 18 | uint8_t bRequest; /* siehe Request-Tafel in USB-Doku */ 19 | uint16_t wValue; /* je nach Request */ 20 | uint16_t wIndex; /* je nach Request */ 21 | uint16_t wLength; /* Anzahl Bytes, wenn Data-Stage vorhanden ist */ 22 | } setupPaket_t; 23 | 24 | /******* Struktur des Kommando- und Org-Datenblockes *******************/ 25 | typedef struct 26 | { 27 | setupPaket_t setupPacket; /* das jeweils letzte Setup-Paket */ 28 | int transferLen; /* noch zum Host zu sendende Anzahl Bytes */ 29 | int packetLen; /* wie lang das Paket zum Host sein darf */ 30 | uint8_t* transferPtr; /* zeigt auf die noch zu sendenden Bytes */ 31 | 32 | bool remoteWakeup; 33 | bool selfPowered; 34 | int configuration; 35 | } command_t; 36 | 37 | typedef union status_t { 38 | // access as 16 bit in one read instruction 39 | uint16_t both; 40 | // -- or as separate 8 bit values -- 41 | struct { 42 | uint8_t suspended; 43 | uint8_t configured; 44 | }; 45 | } status_t; 46 | 47 | /* Line coding structure 48 | 0-3 BaudRate Data terminal rate (baudrate), in bits per second 49 | 4 bCharFormat Stop bits: 0 - 1 Stop bit, 1 - 1.5 Stop bits, 2 - 2 Stop bits 50 | 5 bParityType Parity: 0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space 51 | 6 bDataBits Data bits: 5, 6, 7, 8, 16 52 | */ 53 | 54 | #define lineCoding_t usb_cdc_line_coding 55 | 56 | extern command_t CMD; 57 | extern lineCoding_t lineCoding; 58 | extern uint16_t Dtr_Rts; 59 | extern const uint8_t ZERO; 60 | 61 | extern void Class_Start(void); 62 | extern void Setup_flash(); 63 | extern void Setup_clocks(); 64 | 65 | extern int DataBeginTransmit(); 66 | extern void DataBeginReceive(); // called when Rx data can be processed again 67 | extern int ReadControlBlock(uint8_t* pBuffer, int maxlen); 68 | extern int SendData(int ep, uint8_t* pBuffer, int count); 69 | //-------------------------------------------------------------------------- 70 | static inline void EnableUsbIRQ (void) 71 | { 72 | nvic_irq_enable(NVIC_USB_LP_CAN_RX0); 73 | } 74 | //-------------------------------------------------------------------------- 75 | static inline void DisableUsbIRQ (void) 76 | { 77 | nvic_irq_disable(NVIC_USB_LP_CAN_RX0); 78 | } 79 | //-------------------------------------------------------------------------- 80 | static inline void ACK(void) 81 | { 82 | SendData(EP_CTRL, (uint8_t*) &ZERO, 0); 83 | trace("-ACK\n"); 84 | } 85 | 86 | //----------------------------------------------------------------------------- 87 | static inline void USB_SetAddress(uint8_t adr) 88 | { 89 | strace(buf,"setAddr adr=%i\n",adr); 90 | USB_DADDR = 0x80 | adr; 91 | } 92 | 93 | /* Request-Typ im Setup-Packet testen (Standard, Class, Vendor) */ 94 | //-------------------------------------------------------------------------- 95 | static inline bool IsStandardRequest(void) 96 | { 97 | return (CMD.setupPacket.bmRequestType & 0x60) == 0; 98 | } 99 | 100 | //-------------------------------------------------------------------------- 101 | static inline bool IsClassRequest(void) 102 | { 103 | return (CMD.setupPacket.bmRequestType & 0x60) == 0x20; 104 | } 105 | 106 | //-------------------------------------------------------------------------- 107 | static inline bool IsVendorRequest(void) 108 | { 109 | return (CMD.setupPacket.bmRequestType & 0x60) == 0x40; 110 | } 111 | 112 | 113 | #define LED_ON gpio_write_pin(LED_BUILTIN, 0) 114 | #define LED_OFF gpio_write_pin(LED_BUILTIN, 1) 115 | 116 | //----------------------------------------------------------------------------- 117 | #define PAGE_SIZE (2*1024) 118 | // Bootloader size 119 | #define BOOTLOADER_SIZE (3 * PAGE_SIZE) 120 | 121 | // SRAM size 122 | #define SRAM_SIZE (40 * 1024) // the 8kB CDC RAM is not used 123 | 124 | // SRAM end (bottom of stack) 125 | #define SRAM_END (SRAM_BASE + SRAM_SIZE) 126 | 127 | // CDC Bootloader takes 4 kb flash. 128 | #define USER_PROGRAM (FLASH_BASE + BOOTLOADER_SIZE) 129 | //----------------------------------------------------------------------------- 130 | typedef union cmd_t { 131 | uint8_t data[8]; 132 | struct { 133 | uint16_t start; // 0x41BE 134 | uint8_t id; 135 | uint8_t page; 136 | uint16_t data_len; 137 | uint16_t crc; 138 | }; 139 | } __attribute((packed)) cmd_t; 140 | extern cmd_t cmd; 141 | 142 | extern int Check_CRC(uint8_t * buff, int len); 143 | 144 | #define BAUD_RATE 230400 145 | 146 | typedef enum {BUF_EMPTY, BUF_RECEIVING, BUF_FULL, BUF_SENDING} buf_status_t; 147 | typedef enum { 148 | NO_ERROR, 149 | NO_SETUP, 150 | NO_FREE_BUFFER, 151 | DATA_OVERFLOW, 152 | DATA_UNDEFLOW, 153 | CMD_WRONG_LENGTH, 154 | CMD_WRONG_CRC, 155 | CMD_WRONG_ID 156 | } error_t; 157 | 158 | extern int num_pages; // number of total pages to flash 159 | extern int crt_page, page_offset; 160 | extern int header_ok; 161 | //----------------------------------------------------------------------------- 162 | 163 | 164 | 165 | #endif // USB_FUNC_H 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # STM32-CDC-Bootloader 2 | 3 | A bootloader based on USB CDC protocol for STM32 F1, F3 and F4 family MCUs. 4 | 5 | 6 | 7 | For each family the repository contains a respective Eclipse project. 8 | 9 | ### Features: 10 | - no special drive installation: the device with this bootloader will enumerate as a serial COM port. 11 | - the source files are partially based on libmaple core files, also included in the repository. 12 | - additonally, some CMSIS files (slightly modified) are also used. 13 | - in order to upload a program with the bootloader, a special utility program is needed, see [CDC flasher](https://github.com/stevstrong/CDC-flasher). 14 | --------------------------------------------------------------------------------