├── .cproject ├── .gitignore ├── .mxproject ├── .project ├── .settings ├── language.settings.xml └── stm32cubeide.project.prefs ├── Connect ├── connect.py ├── connect.spec └── web │ ├── index.html │ └── js │ └── app.js ├── Core ├── Inc │ ├── as5600.h │ ├── bldc.h │ ├── main.h │ ├── pid.h │ ├── stm32f1xx_hal_conf.h │ ├── stm32f1xx_it.h │ └── usb_comm.h ├── Src │ ├── as5600.c │ ├── bldc.c │ ├── main.c │ ├── pid.c │ ├── stm32f1xx_hal_msp.c │ ├── stm32f1xx_it.c │ ├── syscalls.c │ ├── sysmem.c │ ├── system_stm32f1xx.c │ └── usb_comm.c └── Startup │ └── startup_stm32f103c8tx.s ├── Drivers ├── CMSIS │ ├── Device │ │ └── ST │ │ │ └── STM32F1xx │ │ │ ├── Include │ │ │ ├── stm32f103xb.h │ │ │ ├── stm32f1xx.h │ │ │ └── system_stm32f1xx.h │ │ │ └── License.md │ ├── Include │ │ ├── cmsis_armcc.h │ │ ├── cmsis_armclang.h │ │ ├── cmsis_compiler.h │ │ ├── cmsis_gcc.h │ │ ├── cmsis_iccarm.h │ │ ├── cmsis_version.h │ │ ├── core_armv8mbl.h │ │ ├── core_armv8mml.h │ │ ├── core_cm0.h │ │ ├── core_cm0plus.h │ │ ├── core_cm1.h │ │ ├── core_cm23.h │ │ ├── core_cm3.h │ │ ├── core_cm33.h │ │ ├── core_cm4.h │ │ ├── core_cm7.h │ │ ├── core_sc000.h │ │ ├── core_sc300.h │ │ ├── mpu_armv7.h │ │ ├── mpu_armv8.h │ │ └── tz_context.h │ └── LICENSE.txt └── STM32F1xx_HAL_Driver │ ├── Inc │ ├── Legacy │ │ └── stm32_hal_legacy.h │ ├── stm32f1xx_hal.h │ ├── stm32f1xx_hal_adc.h │ ├── stm32f1xx_hal_adc_ex.h │ ├── stm32f1xx_hal_cortex.h │ ├── stm32f1xx_hal_def.h │ ├── stm32f1xx_hal_dma.h │ ├── stm32f1xx_hal_dma_ex.h │ ├── stm32f1xx_hal_exti.h │ ├── stm32f1xx_hal_flash.h │ ├── stm32f1xx_hal_flash_ex.h │ ├── stm32f1xx_hal_gpio.h │ ├── stm32f1xx_hal_gpio_ex.h │ ├── stm32f1xx_hal_i2c.h │ ├── stm32f1xx_hal_pcd.h │ ├── stm32f1xx_hal_pcd_ex.h │ ├── stm32f1xx_hal_pwr.h │ ├── stm32f1xx_hal_rcc.h │ ├── stm32f1xx_hal_rcc_ex.h │ ├── stm32f1xx_hal_tim.h │ ├── stm32f1xx_hal_tim_ex.h │ ├── stm32f1xx_ll_adc.h │ ├── stm32f1xx_ll_bus.h │ ├── stm32f1xx_ll_cortex.h │ ├── stm32f1xx_ll_dma.h │ ├── stm32f1xx_ll_exti.h │ ├── stm32f1xx_ll_gpio.h │ ├── stm32f1xx_ll_i2c.h │ ├── stm32f1xx_ll_pwr.h │ ├── stm32f1xx_ll_rcc.h │ ├── stm32f1xx_ll_system.h │ ├── stm32f1xx_ll_tim.h │ ├── stm32f1xx_ll_usb.h │ └── stm32f1xx_ll_utils.h │ ├── License.md │ └── Src │ ├── stm32f1xx_hal.c │ ├── stm32f1xx_hal_adc.c │ ├── stm32f1xx_hal_adc_ex.c │ ├── stm32f1xx_hal_cortex.c │ ├── stm32f1xx_hal_dma.c │ ├── stm32f1xx_hal_exti.c │ ├── stm32f1xx_hal_flash.c │ ├── stm32f1xx_hal_flash_ex.c │ ├── stm32f1xx_hal_gpio.c │ ├── stm32f1xx_hal_gpio_ex.c │ ├── stm32f1xx_hal_i2c.c │ ├── stm32f1xx_hal_pcd.c │ ├── stm32f1xx_hal_pcd_ex.c │ ├── stm32f1xx_hal_pwr.c │ ├── stm32f1xx_hal_rcc.c │ ├── stm32f1xx_hal_rcc_ex.c │ ├── stm32f1xx_hal_tim.c │ ├── stm32f1xx_hal_tim_ex.c │ └── stm32f1xx_ll_usb.c ├── Middlewares └── ST │ └── STM32_USB_Device_Library │ ├── Class │ └── CDC │ │ ├── Inc │ │ └── usbd_cdc.h │ │ └── Src │ │ └── usbd_cdc.c │ └── Core │ ├── Inc │ ├── usbd_core.h │ ├── usbd_ctlreq.h │ ├── usbd_def.h │ └── usbd_ioreq.h │ └── Src │ ├── usbd_core.c │ ├── usbd_ctlreq.c │ └── usbd_ioreq.c ├── STM32F103C8TX_FLASH.ld ├── STM32F103_BLDC_Driver (1).launch ├── STM32F103_BLDC_Driver.ioc ├── STM32F103_BLDC_Driver.launch ├── USB_DEVICE ├── App │ ├── usb_device.c │ ├── usb_device.h │ ├── usbd_cdc_if.c │ ├── usbd_cdc_if.h │ ├── usbd_desc.c │ └── usbd_desc.h └── Target │ ├── usbd_conf.c │ └── usbd_conf.h ├── projectbldc.png └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | /Debug/ 2 | /Release/ 3 | 4 | /dist/ 5 | /build/ 6 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | STM32F103_BLDC_Driver 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 | com.st.stm32cube.ide.mcu.MCUProjectNature 23 | com.st.stm32cube.ide.mcu.MCUCubeProjectNature 24 | org.eclipse.cdt.core.cnature 25 | com.st.stm32cube.ide.mcu.MCUCubeIdeServicesRevAev2ProjectNature 26 | com.st.stm32cube.ide.mcu.MCUAdvancedStructureProjectNature 27 | com.st.stm32cube.ide.mcu.MCUSingleCpuProjectNature 28 | com.st.stm32cube.ide.mcu.MCURootProjectNature 29 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 30 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 31 | 32 | 33 | -------------------------------------------------------------------------------- /.settings/language.settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /.settings/stm32cubeide.project.prefs: -------------------------------------------------------------------------------- 1 | 66BE74F758C12D739921AEA421D593D3=1 2 | 8DF89ED150041C4CBC7CB9A9CAA90856=3C032167BDC971B9B2B0655F6BF40811 3 | DC22A860405A8BF2F2C095E5B6529F12=3C032167BDC971B9B2B0655F6BF40811 4 | eclipse.preferences.version=1 5 | -------------------------------------------------------------------------------- /Connect/connect.py: -------------------------------------------------------------------------------- 1 | import serial, io, time, threading, eel, keyboard, os 2 | import serial.tools.list_ports 3 | from sys import exit 4 | 5 | runFlag = True 6 | 7 | @eel.expose 8 | def get_python_variable(): 9 | return connect.get_motor_data() 10 | 11 | @eel.expose 12 | def set_motor_position(motorPos): 13 | connect.set_new_position(motorPos) 14 | 15 | @eel.expose 16 | def set_motor_power(motorPower): 17 | connect.set_new_power(motorPower) 18 | 19 | @eel.expose 20 | def set_mode(mode): 21 | connect.set_mode(mode) 22 | 23 | def close_callback(route, websockets): 24 | global runFlag 25 | 26 | runFlag = False 27 | if not websockets: 28 | exit() 29 | 30 | class Connect: 31 | motorData = '' 32 | 33 | def __init__(self): 34 | self.bldcPort = '' 35 | self.bldcPort = self.find_device() 36 | if(self.bldcPort == -1 or self.bldcPort == None): 37 | print('device not found!') 38 | exit() 39 | else: 40 | time.sleep(.1) 41 | rxData = '' 42 | try: 43 | self.serialConn = serial.Serial(self.bldcPort, 115200, timeout=0, parity=serial.PARITY_EVEN, rtscts=1) 44 | except: 45 | print("device not found") 46 | exit() 47 | 48 | def find_device(self): 49 | print("searching device...") 50 | ports = serial.tools.list_ports.comports() 51 | for p in ports: 52 | self.serialConn = serial.Serial(p.device, 115200, timeout=0, parity=serial.PARITY_EVEN, rtscts=1) 53 | self.serialConn.write(str("?").encode('ascii')) 54 | time.sleep(.1) 55 | self.rxData = self.readData() 56 | self.serialConn.close() 57 | if self.rxData.count("$") > 0: 58 | deviceName = self.rxData[self.rxData.index("$")+1 : self.rxData.index("/")] 59 | if(deviceName == "BLDCController"): 60 | print("device found on {}".format(p.device)) 61 | return p.device 62 | break 63 | else: 64 | return -1 65 | 66 | 67 | def read_thread(self): 68 | while runFlag == True: 69 | time.sleep(.1) 70 | self.rxData = self.readData() 71 | self.parse_data(self.rxData) 72 | 73 | def get_motor_data(self): 74 | return self.motorData 75 | 76 | def send_data(self, data): 77 | self.serialConn.write(str(data).encode('ascii')) 78 | 79 | def set_mode(self, mode): 80 | msg = '#mod{}/'.format(mode) 81 | self.send_data(msg) 82 | 83 | def set_new_position(self, newPos): 84 | msg = '#deg{}0/'.format(newPos) 85 | self.send_data(msg) 86 | 87 | def set_new_power(self, newPow): 88 | msg = '#pwr{}/'.format(newPow) 89 | self.send_data(msg) 90 | 91 | def parse_data(self, data): 92 | if data != None: 93 | if data.count("#") > 0: 94 | self.motorData = data[data.index("#")+1 : data.index("/")] 95 | 96 | def connect(self): 97 | self.serialConn.isOpen() 98 | 99 | def readData(self): 100 | rx = '' 101 | while self.serialConn.inWaiting() > 0: 102 | rx += self.serialConn.read(1).decode('utf-8') 103 | if rx != '': 104 | return rx 105 | 106 | def __del__(self): 107 | print("connection destruction") 108 | try: 109 | self.serialConn.close() 110 | except AttributeError: 111 | print("port closed") 112 | 113 | class Gui: 114 | def __init__(self): 115 | print("GUI init") 116 | self.eel = eel 117 | self.eel.init(f'{os.path.dirname(os.path.realpath(__file__))}/web', allowed_extensions=['.js', '.html']) 118 | self.name = 'eel test' 119 | self.create_window() 120 | 121 | def create_window(self): 122 | eel.start('index.html', size=(800, 1100), block = False, close_callback=close_callback) 123 | while True: 124 | eel.sleep(.1) 125 | 126 | class Main: 127 | def __init__(self) -> None: 128 | print("main init") 129 | 130 | def main_thread(self): 131 | global runFlag 132 | 133 | while runFlag == True: 134 | time.sleep(1) 135 | 136 | if __name__ == "__main__": 137 | connect = Connect() 138 | main = Main() 139 | 140 | readTH = threading.Thread(target=connect.read_thread) 141 | readTH.start() 142 | mainTH = threading.Thread(target=main.main_thread) 143 | mainTH.start() 144 | 145 | gui = Gui() -------------------------------------------------------------------------------- /Connect/connect.spec: -------------------------------------------------------------------------------- 1 | # -*- mode: python ; coding: utf-8 -*- 2 | 3 | 4 | block_cipher = None 5 | 6 | 7 | a = Analysis( 8 | ['connect.py'], 9 | pathex=[], 10 | binaries=[], 11 | datas=[('C:\\Users\\kosik\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\eel\\eel.js', 'eel'), ('web', 'web')], 12 | hiddenimports=['bottle_websocket'], 13 | hookspath=[], 14 | hooksconfig={}, 15 | runtime_hooks=[], 16 | excludes=[], 17 | win_no_prefer_redirects=False, 18 | win_private_assemblies=False, 19 | cipher=block_cipher, 20 | noarchive=False, 21 | ) 22 | pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) 23 | 24 | exe = EXE( 25 | pyz, 26 | a.scripts, 27 | a.binaries, 28 | a.zipfiles, 29 | a.datas, 30 | [], 31 | name='connect', 32 | debug=False, 33 | bootloader_ignore_signals=False, 34 | strip=False, 35 | upx=True, 36 | upx_exclude=[], 37 | runtime_tmpdir=None, 38 | console=True, 39 | disable_windowed_traceback=False, 40 | argv_emulation=False, 41 | target_arch=None, 42 | codesign_identity=None, 43 | entitlements_file=None, 44 | ) 45 | -------------------------------------------------------------------------------- /Connect/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | BLDC Driver 5 | 6 | 7 | 8 | 9 |
10 |

BLDC Driver connect

11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 26 | PWM U 27 | 0 28 | PWM V 29 | 0 30 | PWM W 31 | 0 32 | 33 |
34 | 35 | 36 | 45 | 46 | 47 | 48 | 0 49 | 0 50 | 51 | 52 |
53 |
One point mode
54 |
Switch mode
55 |
56 | 57 | 58 |
59 |
60 | 61 | 62 | 63 | 69 | expected position: 70 | 0 71 | 72 |
73 |
74 | 75 |
76 |
77 | 78 | 79 | 80 | 81 | 82 | 88 | expected power: 89 | 0 90 | 91 |
92 |
93 | 94 | 95 | 215 | 216 | 217 | -------------------------------------------------------------------------------- /Connect/web/js/app.js: -------------------------------------------------------------------------------- 1 | function timedRefresh() { 2 | var timer = setInterval(function() { 3 | eel.get_python_variable()(call_Back); 4 | }, 100); 5 | }; 6 | timedRefresh(); 7 | 8 | const svgMotorPos = document.getElementById("bldcSetPosition"); 9 | const svgMotorPower = document.getElementById("bldcSetPower"); 10 | 11 | function call_Back(output){ 12 | var partsArray = output.split(','); 13 | 14 | if(partsArray[0] == 0){ 15 | document.getElementById("buttonMode1").style.backgroundColor="#52754a"; 16 | document.getElementById("buttonMode2").style.backgroundColor="#803b3b"; 17 | } else if (partsArray[0] == 1){ 18 | document.getElementById("buttonMode1").style.backgroundColor="#803b3b"; 19 | document.getElementById("buttonMode2").style.backgroundColor="#52754a"; 20 | } 21 | 22 | var bldcAngle = Math.round(partsArray[1] / 11.406); 23 | document.getElementById("bldcPosition").innerHTML = bldcAngle + "°"; 24 | var msg = 'rotate(' + bldcAngle + 'deg)' 25 | document.getElementById("motorPosition").style.transformOrigin = "50% 50%"; 26 | document.getElementById("motorPosition").style.transform = msg; 27 | document.getElementById("bldcPwmU").innerHTML = partsArray[2]; 28 | pwmU = Math.round(partsArray[2]) / 10; 29 | document.getElementById("pwmU").style.transform = "scale(" + pwmU + "%, 100%)"; 30 | document.getElementById("bldcPwmV").innerHTML = partsArray[3]; 31 | pwmV = Math.round(partsArray[3]) / 10; 32 | document.getElementById("pwmV").style.transform = "scale(" + pwmV + "%, 100%)"; 33 | document.getElementById("bldcPwmW").innerHTML = partsArray[4]; 34 | pwmW = Math.round(partsArray[4]) / 10; 35 | document.getElementById("pwmW").style.transform = "scale(" + pwmW + "%, 100%)"; 36 | 37 | var bldcExpectedAngle = Math.round(partsArray[5] / 11.406); 38 | var msg = 'rotate(' + bldcExpectedAngle + 'deg)' 39 | document.getElementById("motorExpectedPosition").style.transformOrigin = "50% 50%"; 40 | document.getElementById("motorExpectedPosition").style.transform = msg; 41 | 42 | var rect = svgMotorPos.getBoundingClientRect(); 43 | var pointerPos = Math.round((rect.width / 4095) * partsArray[5]); 44 | document.getElementById("posPointer").style.transform = "translate(" + pointerPos + "px, 0px)"; 45 | document.getElementById("bldcExpectedPositionLabel").innerHTML = bldcExpectedAngle + "°"; 46 | 47 | var rect2 = svgMotorPower.getBoundingClientRect(); 48 | var pointerPower = Math.round((rect2.width / 100) * partsArray[6]); 49 | document.getElementById("powerPointer").style.transform = "translate(" + pointerPower + "px, 0px)"; 50 | document.getElementById("bldcExpectedPowerLabel").innerHTML = partsArray[6] + "%"; 51 | 52 | actualPower = Math.round(partsArray[7]); 53 | document.getElementById("actualPower").style.transform = "scale(" + actualPower + "%, 100%)"; 54 | 55 | console.log(partsArray); 56 | document.getElementById("temperature").innerHTML = partsArray[8] + "°C"; 57 | if(partsArray[8] > 30){ 58 | document.getElementById("temperature").style.fill = "#992626"; 59 | } else { 60 | document.getElementById("temperature").style.fill = "#162818"; 61 | } 62 | } 63 | 64 | 65 | const positionSvgHandler = (svgMotorPos, x, y) => { //position 66 | let p = new DOMPoint(x, y); 67 | return p.matrixTransform(svgMotorPos.getScreenCTM().inverse()); 68 | }; 69 | svgMotorPos.addEventListener('click', e => { 70 | let p = positionSvgHandler(e.target, e.clientX, e.clientY); 71 | var rect = svgMotorPos.getBoundingClientRect(); 72 | var newMotorPos = Math.round((359 / rect.width) * p.x); 73 | eel.set_motor_position(newMotorPos)(); 74 | }); 75 | 76 | 77 | const powerSvgHandler = (svgMotorPower, x, y) => { //power 78 | let p = new DOMPoint(x, y); 79 | return p.matrixTransform(svgMotorPower.getScreenCTM().inverse()); 80 | }; 81 | svgMotorPower.addEventListener('click', e => { 82 | let p = powerSvgHandler(e.target, e.clientX, e.clientY); 83 | var rect = svgMotorPower.getBoundingClientRect(); 84 | var newMotorPower = Math.round((100 / rect.width) * p.x); 85 | eel.set_motor_power(newMotorPower)(); 86 | }); 87 | 88 | function set_mode(mode){ 89 | eel.set_mode(mode)(); 90 | } 91 | -------------------------------------------------------------------------------- /Core/Inc/as5600.h: -------------------------------------------------------------------------------- 1 | /* 2 | * as5600.h 3 | * 4 | * Created on: Mar 5, 2022 5 | * Author: KoSik 6 | */ 7 | 8 | #ifndef INC_AS5600_H_ 9 | #define INC_AS5600_H_ 10 | 11 | #define AS5600_I2C_ADDRESS 0x36 << 1 12 | 13 | 14 | HAL_StatusTypeDef as5600_WriteCommand(I2C_HandleTypeDef *hi2c, uint8_t conf_register_addr, uint8_t setting); 15 | HAL_StatusTypeDef as5600_Init(I2C_HandleTypeDef *hi2c); 16 | HAL_StatusTypeDef as5600_ReadPosition(I2C_HandleTypeDef *hi2c, uint16_t *angle); 17 | HAL_StatusTypeDef as5600_ReadRawPosition(I2C_HandleTypeDef *hi2c, uint16_t *angle); 18 | HAL_StatusTypeDef as5600_ReadConfig(I2C_HandleTypeDef *hi2c, uint16_t *config); 19 | uint8_t as5600_StatusMagnet(I2C_HandleTypeDef *hi2c); 20 | 21 | #endif /* INC_AS5600_H_ */ 22 | -------------------------------------------------------------------------------- /Core/Inc/bldc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * bldc.h 3 | * 4 | * Created on: Apr 9, 2022 5 | * Author: KoSik 6 | * e-mail: kosik84@gmail.com 7 | * 8 | * version: 1.0 9 | */ 10 | 11 | #ifndef INC_BLDC_H_ 12 | #define INC_BLDC_H_ 13 | 14 | struct BLDCMotorSt { 15 | uint32_t pwmU; 16 | uint32_t pwmV; 17 | uint32_t pwmW; 18 | uint16_t expectedPosition; // expected motor position 19 | double speed; 20 | //double accelerate; 21 | double expectedPower; 22 | double actualPower; 23 | //double expectedAcceleration; 24 | double actualAcceleration; 25 | double actualSpeed; 26 | double expectedSpeed; 27 | double actualSpeedARRReg; //timer 2 28 | //uint16_t expectedDeadZone; // dead zone 29 | double distance; // distance from the expected point to the current point 30 | uint8_t direction; // direction 0-L 1-R 31 | int16_t fieldPosition; //magnetic field position for complete rotation - 4095 pozycji 32 | int16_t fieldLastPosition; 33 | }; 34 | 35 | struct BLDCencoderSt { 36 | uint8_t status; 37 | uint16_t angle; //encoder position RAW 38 | uint16_t calculatedAngle; // calculated encoder position 39 | uint8_t magnetStatus; 40 | uint16_t offset; // position offset 41 | }; 42 | 43 | #define REST_DELAY 9 44 | #define PWM_MAX 1023 45 | #define MOTOR_RESOLUTION 819.0 // 5 x 819 = 4095 46 | 47 | #define ARR_MIN 400 48 | #define ACCELERATION_MAX 25 // the number of magnetic field steps that can be jumped in one step 49 | 50 | void bldc_Calculate(TIM_HandleTypeDef *tim); 51 | HAL_StatusTypeDef bldc_Init(TIM_HandleTypeDef *tim, uint32_t channel1, 52 | uint32_t channel2, uint32_t channel3); 53 | HAL_StatusTypeDef bldc_SyncWithEncoder(uint8_t divide); 54 | void bldc_Set_NewPosition(uint16_t deadZone, uint16_t stepsToChange); 55 | uint8_t bldcCheckReachedPosition(uint16_t zeroPoint); 56 | void bldcHapticSwitch(uint16_t point1, uint16_t point2); 57 | void bldc_Set_MotorPosition(uint16_t position, float power); 58 | uint16_t bldc_Create_FocPoint(uint16_t punktX); 59 | void bldc_Create_FocArray(uint16_t *tab); 60 | void set_field(int16_t pozycja_silnika, float moc_silnika); 61 | void delay_Us(uint32_t delay_Us); 62 | 63 | #endif /* INC_BLDC_H_ */ 64 | -------------------------------------------------------------------------------- /Core/Inc/main.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : main.h 5 | * @brief : Header for main.c file. 6 | * This file contains the common defines of the application. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2022 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | 21 | /* Define to prevent recursive inclusion -------------------------------------*/ 22 | #ifndef __MAIN_H 23 | #define __MAIN_H 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | /* Includes ------------------------------------------------------------------*/ 30 | #include "stm32f1xx_hal.h" 31 | 32 | /* Private includes ----------------------------------------------------------*/ 33 | /* USER CODE BEGIN Includes */ 34 | 35 | /* USER CODE END Includes */ 36 | 37 | /* Exported types ------------------------------------------------------------*/ 38 | /* USER CODE BEGIN ET */ 39 | 40 | /* USER CODE END ET */ 41 | 42 | /* Exported constants --------------------------------------------------------*/ 43 | /* USER CODE BEGIN EC */ 44 | 45 | /* USER CODE END EC */ 46 | 47 | /* Exported macro ------------------------------------------------------------*/ 48 | /* USER CODE BEGIN EM */ 49 | 50 | /* USER CODE END EM */ 51 | 52 | void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); 53 | 54 | /* Exported functions prototypes ---------------------------------------------*/ 55 | void Error_Handler(void); 56 | 57 | /* USER CODE BEGIN EFP */ 58 | 59 | /* USER CODE END EFP */ 60 | 61 | /* Private defines -----------------------------------------------------------*/ 62 | #define LEDR_Pin GPIO_PIN_13 63 | #define LEDR_GPIO_Port GPIOC 64 | #define LEDG_Pin GPIO_PIN_14 65 | #define LEDG_GPIO_Port GPIOC 66 | #define HALL1_Pin GPIO_PIN_0 67 | #define HALL1_GPIO_Port GPIOA 68 | #define HALL2_Pin GPIO_PIN_1 69 | #define HALL2_GPIO_Port GPIOA 70 | #define HALL3_Pin GPIO_PIN_2 71 | #define HALL3_GPIO_Port GPIOA 72 | #define HALL4_Pin GPIO_PIN_3 73 | #define HALL4_GPIO_Port GPIOA 74 | #define TEMP_Pin GPIO_PIN_4 75 | #define TEMP_GPIO_Port GPIOA 76 | /* USER CODE BEGIN Private defines */ 77 | 78 | /* USER CODE END Private defines */ 79 | 80 | #ifdef __cplusplus 81 | } 82 | #endif 83 | 84 | #endif /* __MAIN_H */ 85 | -------------------------------------------------------------------------------- /Core/Inc/pid.h: -------------------------------------------------------------------------------- 1 | /* 2 | ------------------------------------------------------------------------------ 3 | ~ File : pid.h 4 | ~ Author : Majid Derhambakhsh 5 | ~ Version: V1.0.0 6 | ~ Created: 02/11/2021 03:43:00 AM 7 | ~ Brief : 8 | ~ Support: 9 | E-Mail : Majid.do16@gmail.com (subject : Embedded Library Support) 10 | 11 | Github : https://github.com/Majid-Derhambakhsh 12 | ------------------------------------------------------------------------------ 13 | ~ Description: 14 | 15 | ~ Attention : 16 | 17 | ~ Changes : 18 | ------------------------------------------------------------------------------ 19 | */ 20 | 21 | #ifndef __PID_H_ 22 | #define __PID_H_ 23 | 24 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Include ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 25 | #include 26 | #include 27 | 28 | /* ------------------------------------------------------------------ */ 29 | 30 | #ifdef __CODEVISIONAVR__ /* Check compiler */ 31 | 32 | #pragma warn_unref_func- /* Disable 'unused function' warning */ 33 | 34 | /* ------------------------------------------------------------------ */ 35 | 36 | #elif defined(__GNUC__) && !defined(USE_HAL_DRIVER) /* Check compiler */ 37 | 38 | #pragma GCC diagnostic ignored "-Wunused-function" /* Disable 'unused function' warning */ 39 | 40 | /* ------------------------------------------------------------------ */ 41 | 42 | #elif defined(USE_HAL_DRIVER) /* Check driver */ 43 | 44 | #include "main.h" 45 | 46 | /* --------------- Check Mainstream series --------------- */ 47 | 48 | #ifdef STM32F0 49 | #include "stm32f0xx_hal.h" /* Import HAL library */ 50 | #elif defined(STM32F1) 51 | #include "stm32f1xx_hal.h" /* Import HAL library */ 52 | #elif defined(STM32F2) 53 | #include "stm32f2xx_hal.h" /* Import HAL library */ 54 | #elif defined(STM32F3) 55 | #include "stm32f3xx_hal.h" /* Import HAL library */ 56 | #elif defined(STM32F4) 57 | #include "stm32f4xx_hal.h" /* Import HAL library */ 58 | #elif defined(STM32F7) 59 | #include "stm32f7xx_hal.h" /* Import HAL library */ 60 | #elif defined(STM32G0) 61 | #include "stm32g0xx_hal.h" /* Import HAL library */ 62 | #elif defined(STM32G4) 63 | #include "stm32g4xx_hal.h" /* Import HAL library */ 64 | 65 | /* ------------ Check High Performance series ------------ */ 66 | 67 | #elif defined(STM32H7) 68 | #include "stm32h7xx_hal.h" /* Import HAL library */ 69 | 70 | /* ------------ Check Ultra low power series ------------- */ 71 | 72 | #elif defined(STM32L0) 73 | #include "stm32l0xx_hal.h" /* Import HAL library */ 74 | #elif defined(STM32L1) 75 | #include "stm32l1xx_hal.h" /* Import HAL library */ 76 | #elif defined(STM32L5) 77 | #include "stm32l5xx_hal.h" /* Import HAL library */ 78 | #elif defined(STM32L4) 79 | #include "stm32l4xx_hal.h" /* Import HAL library */ 80 | #elif defined(STM32H7) 81 | #include "stm32h7xx_hal.h" /* Import HAL library */ 82 | #else 83 | #endif /* STM32F1 */ 84 | 85 | /* ------------------------------------------------------- */ 86 | 87 | #if defined ( __ICCARM__ ) /* ICCARM Compiler */ 88 | 89 | #pragma diag_suppress=Pe177 /* Disable 'unused function' warning */ 90 | 91 | #elif defined ( __GNUC__ ) /* GNU Compiler */ 92 | 93 | //#pragma diag_suppress 177 /* Disable 'unused function' warning */ 94 | 95 | #endif /* __ICCARM__ */ 96 | 97 | /* ------------------------------------------------------------------ */ 98 | 99 | #else /* Compiler not found */ 100 | 101 | #error Chip or Library not supported /* Send error */ 102 | 103 | #endif /* __CODEVISIONAVR__ */ 104 | 105 | /* ------------------------------------------------------------------ */ 106 | 107 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Defines ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 108 | /* ------------------------ Library ------------------------ */ 109 | #define _PID_LIBRARY_VERSION 1.0.0 110 | 111 | /* ------------------------ Public ------------------------- */ 112 | #define _PID_8BIT_PWM_MAX UINT8_MAX 113 | #define _PID_SAMPLE_TIME_MS_DEF 100 114 | 115 | #ifndef _FALSE 116 | 117 | #define _FALSE 0 118 | 119 | #endif 120 | 121 | #ifndef _TRUE 122 | 123 | #define _TRUE 1 124 | 125 | #endif 126 | 127 | /* ---------------------- By compiler ---------------------- */ 128 | #ifndef GetTime 129 | 130 | /* ---------------------- By compiler ---------------------- */ 131 | 132 | #ifdef __CODEVISIONAVR__ /* Check compiler */ 133 | 134 | #define GetTime() 0 135 | 136 | /* ------------------------------------------------------------------ */ 137 | 138 | #elif defined(__GNUC__) && !defined(USE_HAL_DRIVER) /* Check compiler */ 139 | 140 | #define GetTime() 0 141 | 142 | /* ------------------------------------------------------------------ */ 143 | 144 | #elif defined(USE_HAL_DRIVER) /* Check driver */ 145 | 146 | #define GetTime() HAL_GetTick() 147 | 148 | /* ------------------------------------------------------------------ */ 149 | 150 | #else 151 | #endif /* __CODEVISIONAVR__ */ 152 | /* ------------------------------------------------------------------ */ 153 | 154 | #endif 155 | 156 | /* --------------------------------------------------------- */ 157 | 158 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Types ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 159 | /* PID Mode */ 160 | typedef enum 161 | { 162 | 163 | _PID_MODE_MANUAL = 0, 164 | _PID_MODE_AUTOMATIC = 1 165 | 166 | }PIDMode_TypeDef; 167 | 168 | /* PID P On x */ 169 | typedef enum 170 | { 171 | 172 | _PID_P_ON_M = 0, /* Proportional on Measurement */ 173 | _PID_P_ON_E = 1 174 | 175 | }PIDPON_TypeDef; 176 | 177 | /* PID Control direction */ 178 | typedef enum 179 | { 180 | 181 | _PID_CD_DIRECT = 0, 182 | _PID_CD_REVERSE = 1 183 | 184 | }PIDCD_TypeDef; 185 | 186 | /* PID Structure */ 187 | typedef struct 188 | { 189 | 190 | PIDPON_TypeDef POnE; 191 | PIDMode_TypeDef InAuto; 192 | 193 | PIDPON_TypeDef POn; 194 | PIDCD_TypeDef ControllerDirection; 195 | 196 | uint32_t LastTime; 197 | uint32_t SampleTime; 198 | 199 | double DispKp; 200 | double DispKi; 201 | double DispKd; 202 | 203 | double Kp; 204 | double Ki; 205 | double Kd; 206 | 207 | double *MyInput; 208 | double *MyOutput; 209 | double *MySetpoint; 210 | 211 | double OutputSum; 212 | double LastInput; 213 | 214 | double OutMin; 215 | double OutMax; 216 | 217 | }PID_TypeDef; 218 | 219 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 220 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Enum ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 221 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Struct ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 222 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Class ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 223 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Prototype ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 224 | /* :::::::::::::: Init ::::::::::::: */ 225 | void PID_Init(PID_TypeDef *uPID); 226 | 227 | void PID(PID_TypeDef *uPID, double *Input, double *Output, double *Setpoint, double Kp, double Ki, double Kd, PIDPON_TypeDef POn, PIDCD_TypeDef ControllerDirection); 228 | void PID2(PID_TypeDef *uPID, double *Input, double *Output, double *Setpoint, double Kp, double Ki, double Kd, PIDCD_TypeDef ControllerDirection); 229 | 230 | /* ::::::::::: Computing ::::::::::: */ 231 | uint8_t PID_Compute(PID_TypeDef *uPID); 232 | 233 | /* ::::::::::: PID Mode :::::::::::: */ 234 | void PID_SetMode(PID_TypeDef *uPID, PIDMode_TypeDef Mode); 235 | PIDMode_TypeDef PID_GetMode(PID_TypeDef *uPID); 236 | 237 | /* :::::::::: PID Limits ::::::::::: */ 238 | void PID_SetOutputLimits(PID_TypeDef *uPID, double Min, double Max); 239 | 240 | /* :::::::::: PID Tunings :::::::::: */ 241 | void PID_SetTunings(PID_TypeDef *uPID, double Kp, double Ki, double Kd); 242 | void PID_SetTunings2(PID_TypeDef *uPID, double Kp, double Ki, double Kd, PIDPON_TypeDef POn); 243 | 244 | /* ::::::::: PID Direction ::::::::: */ 245 | void PID_SetControllerDirection(PID_TypeDef *uPID, PIDCD_TypeDef Direction); 246 | PIDCD_TypeDef PID_GetDirection(PID_TypeDef *uPID); 247 | 248 | /* ::::::::: PID Sampling :::::::::: */ 249 | void PID_SetSampleTime(PID_TypeDef *uPID, int32_t NewSampleTime); 250 | 251 | /* ::::::: Get Tunings Param ::::::: */ 252 | double PID_GetKp(PID_TypeDef *uPID); 253 | double PID_GetKi(PID_TypeDef *uPID); 254 | double PID_GetKd(PID_TypeDef *uPID); 255 | 256 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End of the program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 257 | 258 | #endif /* __PID_H_ */ 259 | -------------------------------------------------------------------------------- /Core/Inc/stm32f1xx_it.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file stm32f1xx_it.h 5 | * @brief This file contains the headers of the interrupt handlers. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __STM32F1xx_IT_H 22 | #define __STM32F1xx_IT_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Private includes ----------------------------------------------------------*/ 29 | /* USER CODE BEGIN Includes */ 30 | 31 | /* USER CODE END Includes */ 32 | 33 | /* Exported types ------------------------------------------------------------*/ 34 | /* USER CODE BEGIN ET */ 35 | 36 | /* USER CODE END ET */ 37 | 38 | /* Exported constants --------------------------------------------------------*/ 39 | /* USER CODE BEGIN EC */ 40 | 41 | /* USER CODE END EC */ 42 | 43 | /* Exported macro ------------------------------------------------------------*/ 44 | /* USER CODE BEGIN EM */ 45 | 46 | /* USER CODE END EM */ 47 | 48 | /* Exported functions prototypes ---------------------------------------------*/ 49 | void NMI_Handler(void); 50 | void HardFault_Handler(void); 51 | void MemManage_Handler(void); 52 | void BusFault_Handler(void); 53 | void UsageFault_Handler(void); 54 | void SVC_Handler(void); 55 | void DebugMon_Handler(void); 56 | void PendSV_Handler(void); 57 | void SysTick_Handler(void); 58 | void PVD_IRQHandler(void); 59 | void FLASH_IRQHandler(void); 60 | void RCC_IRQHandler(void); 61 | void DMA1_Channel1_IRQHandler(void); 62 | void DMA1_Channel2_IRQHandler(void); 63 | void DMA1_Channel3_IRQHandler(void); 64 | void DMA1_Channel6_IRQHandler(void); 65 | void USB_HP_CAN1_TX_IRQHandler(void); 66 | void USB_LP_CAN1_RX0_IRQHandler(void); 67 | void TIM2_IRQHandler(void); 68 | void TIM3_IRQHandler(void); 69 | void TIM4_IRQHandler(void); 70 | void I2C2_EV_IRQHandler(void); 71 | /* USER CODE BEGIN EFP */ 72 | 73 | /* USER CODE END EFP */ 74 | 75 | #ifdef __cplusplus 76 | } 77 | #endif 78 | 79 | #endif /* __STM32F1xx_IT_H */ 80 | -------------------------------------------------------------------------------- /Core/Inc/usb_comm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * usb_comm.h 3 | * 4 | * Created on: 18 paź 2022 5 | * Author: KoSik 6 | * e-mail: kosik84@gmail.com 7 | * 8 | * version: 1.0 9 | */ 10 | 11 | #ifndef INC_USB_COMM_H_ 12 | #define INC_USB_COMM_H_ 13 | 14 | uint8_t read_variable(uint8_t *Data, char sparam[3], uint16_t *param, uint8_t array_size); 15 | void decode_message(uint8_t *Data, uint8_t array_size); 16 | 17 | #endif /* INC_USB_COMM_H_ */ 18 | -------------------------------------------------------------------------------- /Core/Src/as5600.c: -------------------------------------------------------------------------------- 1 | /* 2 | * as5600.c 3 | * 4 | * Created on: Mar 5, 2022 5 | * Author: KoSik 6 | */ 7 | 8 | #include "main.h" 9 | #include "as5600.h" 10 | 11 | HAL_StatusTypeDef as5600_WriteCommand(I2C_HandleTypeDef *hi2c, 12 | uint8_t conf_register_addr, uint8_t setting) { 13 | return HAL_I2C_Mem_Write(hi2c, AS5600_I2C_ADDRESS, conf_register_addr, 1, 14 | &setting, 1, 100); 15 | } 16 | 17 | HAL_StatusTypeDef as5600_Init(I2C_HandleTypeDef *hi2c) { 18 | uint8_t status; 19 | HAL_I2C_Init(hi2c); 20 | status = as5600_WriteCommand(hi2c, 0x08, 0x01); //wlacz enkoder 21 | if (!status) { 22 | HAL_Delay(50); 23 | status = as5600_WriteCommand(hi2c, 0x08, 0x01); //wlacz enkoder 24 | } 25 | status = as5600_WriteCommand(hi2c, 0x07, 0x00); //slow filter x16 26 | HAL_Delay(10); 27 | return status; 28 | } 29 | 30 | HAL_StatusTypeDef as5600_ReadPosition(I2C_HandleTypeDef *hi2c, uint16_t *angle) { 31 | uint8_t status = 0; 32 | uint8_t temp[2]; 33 | 34 | status = HAL_I2C_Mem_Read(hi2c, AS5600_I2C_ADDRESS, 0x0E, 1, temp, 2, 100); 35 | *angle = (temp[0] << 8) + temp[1]; 36 | 37 | return status; 38 | } 39 | 40 | HAL_StatusTypeDef as5600_ReadRawPosition(I2C_HandleTypeDef *hi2c, 41 | uint16_t *angle) { 42 | uint8_t status = 0; 43 | uint8_t temp[2]; 44 | 45 | status = HAL_I2C_Mem_Read(hi2c, AS5600_I2C_ADDRESS, 0x0C, 1, temp, 2, 100); 46 | *angle = (temp[0] << 8) + temp[1]; 47 | 48 | return status; 49 | } 50 | 51 | HAL_StatusTypeDef as5600_ReadConfig(I2C_HandleTypeDef *hi2c, uint16_t *config) { 52 | uint8_t temp_posision = 0; 53 | uint8_t status = 0; 54 | 55 | status = HAL_I2C_Mem_Read(hi2c, AS5600_I2C_ADDRESS, 0x07, 1, &temp_posision, 1, 56 | 100); 57 | *config = temp_posision << 8; 58 | if (status == 0) { 59 | HAL_I2C_Mem_Read(hi2c, AS5600_I2C_ADDRESS, 0x08, 1, &temp_posision, 1, 60 | 100); 61 | *config += temp_posision; 62 | } 63 | 64 | return status; 65 | } 66 | 67 | /* Magnet Status 68 | * Bits |MD|ML|MH| 69 | * 70 | * MD - magnet was detected 71 | * ML - magnet too weak 72 | * MH - magnet too strong 73 | * */ 74 | uint8_t as5600_StatusMagnet(I2C_HandleTypeDef *hi2c) { 75 | uint8_t status = 0; 76 | 77 | HAL_I2C_Mem_Read(hi2c, AS5600_I2C_ADDRESS, 0x0B, 1, &status, 1, 100); 78 | return status >> 3; 79 | } 80 | 81 | -------------------------------------------------------------------------------- /Core/Src/bldc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * bldc.h 3 | * 4 | * Created on: Apr 9, 2022 5 | * Author: KoSik 6 | * e-mail: kosik84@gmail.com 7 | * 8 | * version: 1.0 9 | */ 10 | 11 | #include "main.h" 12 | #include "bldc.h" 13 | #include "stdlib.h" 14 | 15 | struct BLDCMotorSt bldcMotor; 16 | struct BLDCencoderSt bldcEncoder; 17 | 18 | uint16_t focArray[(int)MOTOR_RESOLUTION]; 19 | uint8_t initPhase = 0; 20 | //uint16_t centerPoint, centerPoint2, bufPoint; 21 | 22 | 23 | 24 | HAL_StatusTypeDef bldc_Init(TIM_HandleTypeDef *tim, uint32_t channel1, uint32_t channel2, uint32_t channel3){ 25 | HAL_StatusTypeDef status; 26 | 27 | status = HAL_TIM_PWM_Start_DMA(tim, channel1, (uint32_t*)&bldcMotor.pwmU, 1); 28 | HAL_TIMEx_PWMN_Start(tim, channel1); 29 | HAL_TIM_PWM_Start_DMA(tim, channel2, (uint32_t*)&bldcMotor.pwmV, 1); 30 | HAL_TIMEx_PWMN_Start(tim, channel2); 31 | HAL_TIM_PWM_Start_DMA(tim, channel3, (uint32_t*)&bldcMotor.pwmW, 1); 32 | HAL_TIMEx_PWMN_Start(tim, channel3); 33 | 34 | 35 | initPhase = 1; 36 | 37 | for (bldcMotor.fieldPosition = 4095; bldcMotor.fieldPosition > 0; bldcMotor.fieldPosition--) { 38 | bldc_Set_MotorPosition(bldcMotor.fieldPosition, 20); 39 | delay_Us(30); 40 | if(bldcEncoder.angle < 819){ 41 | break; 42 | } 43 | } 44 | for (bldcMotor.fieldPosition = 819; bldcMotor.fieldPosition > 0; bldcMotor.fieldPosition--) { 45 | bldc_Set_MotorPosition(bldcMotor.fieldPosition, 20); 46 | delay_Us(300); 47 | } 48 | HAL_Delay(100); 49 | bldcEncoder.offset = bldcEncoder.angle; 50 | initPhase = 0; 51 | 52 | 53 | return status; 54 | } 55 | 56 | 57 | /* 58 | * Update field position with encoder position 59 | * tested with 1kHz update 60 | * */ 61 | HAL_StatusTypeDef bldc_SyncWithEncoder(uint8_t divide){ 62 | if(abs(bldcMotor.fieldPosition - bldcEncoder.calculatedAngle) > 50){ //korekta pozycji silnika wzgledem enkodera 63 | if(abs(bldcMotor.fieldPosition - bldcEncoder.calculatedAngle) > 3000){ //jesli przeskok przez 0 64 | if(bldcMotor.fieldPosition > bldcEncoder.calculatedAngle){ 65 | bldcMotor.fieldPosition = bldcEncoder.calculatedAngle - ((bldcMotor.fieldPosition - (4095 + bldcEncoder.calculatedAngle)) / divide); 66 | } else { 67 | bldcMotor.fieldPosition = bldcEncoder.calculatedAngle + (((bldcMotor.fieldPosition + 4095) - bldcEncoder.calculatedAngle) / divide); 68 | } 69 | } else { 70 | bldcMotor.fieldPosition = bldcEncoder.calculatedAngle + ((bldcMotor.fieldPosition - bldcEncoder.calculatedAngle) / divide); 71 | } 72 | } 73 | return HAL_OK; 74 | } 75 | 76 | void bldc_Set_NewPosition(uint16_t deadZone, uint16_t stepsToChange) { 77 | if (bldcMotor.distance >= stepsToChange) { 78 | if (bldcMotor.direction == 0) { 79 | bldcMotor.fieldPosition = bldcMotor.fieldPosition + stepsToChange; 80 | if (bldcMotor.fieldPosition > 4095) { 81 | bldcMotor.fieldPosition = bldcMotor.fieldPosition - 4095; 82 | } 83 | } else { 84 | bldcMotor.fieldPosition = bldcMotor.fieldPosition - stepsToChange; 85 | if(bldcMotor.fieldPosition < 0) { 86 | bldcMotor.fieldPosition = 4095 - bldcMotor.fieldPosition; 87 | } 88 | } 89 | } 90 | bldc_Set_MotorPosition(bldcMotor.fieldPosition, bldcMotor.actualPower); 91 | } 92 | 93 | /* 94 | * on/off switch simulation 95 | * call in timer 96 | * 97 | * 98 | * center point calculation error !!!!!!!!!!!!!!!!!!!!!!!!!!! 99 | * error when point2 < point1 100 | * */ 101 | void bldcHapticSwitch(uint16_t point1, uint16_t point2){ 102 | uint16_t centerPoint, centerPoint2, bufPoint; 103 | 104 | if(point1 > point2){ //odworcenie punktow 105 | bufPoint = point2; 106 | point2 = point1; 107 | point1 = bufPoint; 108 | } 109 | centerPoint = ((point2 - point1)/2) + point1; 110 | 111 | centerPoint2 = centerPoint + 2047; 112 | if(centerPoint2 > 4095){ 113 | centerPoint2 = centerPoint2 - 4095; 114 | } 115 | 116 | if (bldcEncoder.calculatedAngle >= centerPoint && bldcEncoder.calculatedAngle < centerPoint2) { 117 | bldcMotor.expectedPosition = point2; 118 | } else { 119 | bldcMotor.expectedPosition = point1; 120 | } 121 | } 122 | 123 | 124 | /* 125 | * calculation of power and next position 126 | * interrupt 100Hz 127 | * */ 128 | void bldc_Calculate(TIM_HandleTypeDef *tim){ 129 | uint16_t calcR, calcL; 130 | 131 | // ANGLE 132 | if((uint16_t)bldcEncoder.angle > bldcEncoder.offset){ 133 | bldcEncoder.calculatedAngle = (uint16_t)bldcEncoder.angle - bldcEncoder.offset; 134 | } else { 135 | bldcEncoder.calculatedAngle = (4095 + (uint16_t)bldcEncoder.angle) - bldcEncoder.offset; 136 | } 137 | // DISTANCE + DIRECTION 138 | if(bldcMotor.expectedPosition >= bldcMotor.fieldPosition){ 139 | calcL = bldcMotor.expectedPosition - bldcMotor.fieldPosition; 140 | calcR = (4095 - bldcMotor.expectedPosition) + bldcMotor.fieldPosition; 141 | } else { 142 | calcL = (4095 - bldcMotor.fieldPosition) + bldcMotor.expectedPosition; 143 | calcR = bldcMotor.fieldPosition - bldcMotor.expectedPosition; 144 | } 145 | 146 | if(calcR > calcL){ 147 | bldcMotor.distance = calcL; 148 | bldcMotor.direction = 0; 149 | } else { 150 | bldcMotor.distance = calcR; 151 | bldcMotor.direction = 1; 152 | } 153 | // SPEED 154 | if(bldcMotor.fieldPosition >= bldcMotor.fieldLastPosition){ 155 | calcL = bldcMotor.fieldPosition - bldcMotor.fieldLastPosition; 156 | calcR = (4095 - bldcMotor.fieldPosition) + bldcMotor.fieldLastPosition; 157 | } else { 158 | calcL = (4095 - bldcMotor.fieldLastPosition) + bldcMotor.fieldPosition; 159 | calcR = bldcMotor.fieldLastPosition - bldcMotor.fieldPosition; 160 | } 161 | 162 | if(calcR > calcL){ 163 | bldcMotor.speed = calcL; 164 | } else { 165 | bldcMotor.speed = calcR; 166 | } 167 | bldcMotor.fieldLastPosition = bldcMotor.fieldPosition; 168 | 169 | // ACCELERATION 170 | if(bldcMotor.speed < bldcMotor.expectedSpeed && bldcMotor.distance > 100){ 171 | if(__HAL_TIM_GET_AUTORELOAD(tim) > ARR_MIN){ 172 | bldcMotor.actualSpeedARRReg = bldcMotor.actualSpeedARRReg - 1; 173 | __HAL_TIM_SET_AUTORELOAD(tim, bldcMotor.actualSpeedARRReg); 174 | } else if(__HAL_TIM_GET_AUTORELOAD(tim) == ARR_MIN && bldcMotor.actualAcceleration < ACCELERATION_MAX){ 175 | bldcMotor.actualAcceleration+=0.008; 176 | } 177 | } 178 | // BRAKING 179 | if(bldcMotor.distance < 100){ 180 | bldcMotor.actualSpeedARRReg = 400 + (bldcMotor.distance * 6); 181 | __HAL_TIM_SET_AUTORELOAD(tim, bldcMotor.actualSpeedARRReg); 182 | } 183 | 184 | } 185 | 186 | 187 | 188 | /* 189 | * interrupt 1kHz 190 | * */ 191 | void bldc_Set_MotorPosition(uint16_t position, float power){ 192 | uint16_t pos = position; 193 | 194 | for(uint8_t i=0; i<20; i++){ 195 | if(pos>=819){ 196 | pos=pos-819; 197 | } else { 198 | break; 199 | } 200 | } 201 | set_field(pos, power); 202 | } 203 | 204 | uint16_t bldc_Create_FocPoint(uint16_t pointX){ 205 | float a, b; 206 | 207 | if(pointX > (MOTOR_RESOLUTION-1)){ 208 | pointX -= (MOTOR_RESOLUTION-1); 209 | } 210 | 211 | if(pointX >= 0 && pointX < REST_DELAY){ 212 | return PWM_MAX; 213 | } else if(pointX >= REST_DELAY && pointX < (MOTOR_RESOLUTION/2)){ 214 | a = -PWM_MAX / ((MOTOR_RESOLUTION/2) - REST_DELAY); 215 | b = PWM_MAX - (a * REST_DELAY); 216 | return (int)((a * pointX) + b); 217 | } else if (pointX >= (MOTOR_RESOLUTION/2) && pointX < ((MOTOR_RESOLUTION/2) + REST_DELAY)){ 218 | return 0; 219 | } else if (pointX >= (MOTOR_RESOLUTION/2) + REST_DELAY && pointX < MOTOR_RESOLUTION){ 220 | a = ((PWM_MAX * 1.0) / (MOTOR_RESOLUTION - ((MOTOR_RESOLUTION/2) + REST_DELAY))); 221 | b = -(a * ((MOTOR_RESOLUTION/2) + REST_DELAY)); 222 | return (int)((a * pointX) + b); 223 | } else { 224 | return 0; 225 | } 226 | } 227 | 228 | void bldc_Create_FocArray(uint16_t *tab){ 229 | for(uint16_t q=0; q MOTOR_RESOLUTION - shift1){ 245 | actualPosition = motorPosition - MOTOR_RESOLUTION + shift1; 246 | } else { 247 | actualPosition = motorPosition + shift1; 248 | } 249 | bldcMotor.pwmV = (uint16_t)(focArray[actualPosition] * moc_silnika / 100); 250 | 251 | if(motorPosition > MOTOR_RESOLUTION - shift2){ 252 | actualPosition = motorPosition - MOTOR_RESOLUTION + shift2; 253 | } else { 254 | actualPosition = motorPosition + shift2; 255 | } 256 | bldcMotor.pwmW = (uint16_t)(focArray[actualPosition] * moc_silnika / 100); 257 | 258 | } 259 | 260 | void delay_Us(uint32_t delay_Us){ 261 | volatile unsigned int num; 262 | volatile unsigned int t; 263 | 264 | 265 | for (num = 0; num < delay_Us; num++) 266 | { 267 | t = 11; 268 | while (t != 0) 269 | { 270 | t--; 271 | } 272 | } 273 | } 274 | -------------------------------------------------------------------------------- /Core/Src/pid.c: -------------------------------------------------------------------------------- 1 |  /* 2 | ------------------------------------------------------------------------------ 3 | ~ File : pid.c 4 | ~ Author : Majid Derhambakhsh 5 | ~ Version: V1.0.0 6 | ~ Created: 02/11/2021 03:43:00 AM 7 | ~ Brief : 8 | ~ Support: 9 | E-Mail : Majid.do16@gmail.com (subject : Embedded Library Support) 10 | 11 | Github : https://github.com/Majid-Derhambakhsh 12 | ------------------------------------------------------------------------------ 13 | ~ Description: 14 | 15 | ~ Attention : 16 | 17 | ~ Changes : 18 | ------------------------------------------------------------------------------ 19 | */ 20 | 21 | #include "pid.h" 22 | 23 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Functions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 24 | /* ~~~~~~~~~~~~~~~~~ Initialize ~~~~~~~~~~~~~~~~ */ 25 | void PID_Init(PID_TypeDef *uPID) 26 | { 27 | /* ~~~~~~~~~~ Set parameter ~~~~~~~~~~ */ 28 | uPID->OutputSum = *uPID->MyOutput; 29 | uPID->LastInput = *uPID->MyInput; 30 | 31 | if (uPID->OutputSum > uPID->OutMax) 32 | { 33 | uPID->OutputSum = uPID->OutMax; 34 | } 35 | else if (uPID->OutputSum < uPID->OutMin) 36 | { 37 | uPID->OutputSum = uPID->OutMin; 38 | } 39 | else { } 40 | 41 | } 42 | 43 | void PID(PID_TypeDef *uPID, double *Input, double *Output, double *Setpoint, double Kp, double Ki, double Kd, PIDPON_TypeDef POn, PIDCD_TypeDef ControllerDirection) 44 | { 45 | /* ~~~~~~~~~~ Set parameter ~~~~~~~~~~ */ 46 | uPID->MyOutput = Output; 47 | uPID->MyInput = Input; 48 | uPID->MySetpoint = Setpoint; 49 | uPID->InAuto = (PIDMode_TypeDef)_FALSE; 50 | 51 | PID_SetOutputLimits(uPID, 0, _PID_8BIT_PWM_MAX); 52 | 53 | uPID->SampleTime = _PID_SAMPLE_TIME_MS_DEF; /* default Controller Sample Time is 0.1 seconds */ 54 | 55 | PID_SetControllerDirection(uPID, ControllerDirection); 56 | PID_SetTunings2(uPID, Kp, Ki, Kd, POn); 57 | 58 | uPID->LastTime = GetTime() - uPID->SampleTime; 59 | 60 | } 61 | 62 | void PID2(PID_TypeDef *uPID, double *Input, double *Output, double *Setpoint, double Kp, double Ki, double Kd, PIDCD_TypeDef ControllerDirection) 63 | { 64 | PID(uPID, Input, Output, Setpoint, Kp, Ki, Kd, _PID_P_ON_E, ControllerDirection); 65 | } 66 | 67 | /* ~~~~~~~~~~~~~~~~~ Computing ~~~~~~~~~~~~~~~~~ */ 68 | uint8_t PID_Compute(PID_TypeDef *uPID) 69 | { 70 | 71 | uint32_t now; 72 | uint32_t timeChange; 73 | 74 | double input; 75 | double error; 76 | double dInput; 77 | double output; 78 | 79 | /* ~~~~~~~~~~ Check PID mode ~~~~~~~~~~ */ 80 | if (!uPID->InAuto) 81 | { 82 | return _FALSE; 83 | } 84 | 85 | /* ~~~~~~~~~~ Calculate time ~~~~~~~~~~ */ 86 | now = GetTime(); 87 | timeChange = (now - uPID->LastTime); 88 | 89 | if (timeChange >= uPID->SampleTime) 90 | { 91 | /* ..... Compute all the working error variables ..... */ 92 | input = *uPID->MyInput; 93 | error = *uPID->MySetpoint - input; 94 | dInput = (input - uPID->LastInput); 95 | 96 | uPID->OutputSum += (uPID->Ki * error); 97 | 98 | /* ..... Add Proportional on Measurement, if P_ON_M is specified ..... */ 99 | if (!uPID->POnE) 100 | { 101 | uPID->OutputSum -= uPID->Kp * dInput; 102 | } 103 | 104 | if (uPID->OutputSum > uPID->OutMax) 105 | { 106 | uPID->OutputSum = uPID->OutMax; 107 | } 108 | else if (uPID->OutputSum < uPID->OutMin) 109 | { 110 | uPID->OutputSum = uPID->OutMin; 111 | } 112 | else { } 113 | 114 | /* ..... Add Proportional on Error, if P_ON_E is specified ..... */ 115 | if (uPID->POnE) 116 | { 117 | output = uPID->Kp * error; 118 | } 119 | else 120 | { 121 | output = 0; 122 | } 123 | 124 | /* ..... Compute Rest of PID Output ..... */ 125 | output += uPID->OutputSum - uPID->Kd * dInput; 126 | 127 | if (output > uPID->OutMax) 128 | { 129 | output = uPID->OutMax; 130 | } 131 | else if (output < uPID->OutMin) 132 | { 133 | output = uPID->OutMin; 134 | } 135 | else { } 136 | 137 | *uPID->MyOutput = output; 138 | 139 | /* ..... Remember some variables for next time ..... */ 140 | uPID->LastInput = input; 141 | uPID->LastTime = now; 142 | 143 | return _TRUE; 144 | 145 | } 146 | else 147 | { 148 | return _FALSE; 149 | } 150 | 151 | } 152 | 153 | /* ~~~~~~~~~~~~~~~~~ PID Mode ~~~~~~~~~~~~~~~~~~ */ 154 | void PID_SetMode(PID_TypeDef *uPID, PIDMode_TypeDef Mode) 155 | { 156 | 157 | uint8_t newAuto = (Mode == _PID_MODE_AUTOMATIC); 158 | 159 | /* ~~~~~~~~~~ Initialize the PID ~~~~~~~~~~ */ 160 | if (newAuto && !uPID->InAuto) 161 | { 162 | PID_Init(uPID); 163 | } 164 | 165 | uPID->InAuto = (PIDMode_TypeDef)newAuto; 166 | 167 | } 168 | PIDMode_TypeDef PID_GetMode(PID_TypeDef *uPID) 169 | { 170 | return uPID->InAuto ? _PID_MODE_AUTOMATIC : _PID_MODE_MANUAL; 171 | } 172 | 173 | /* ~~~~~~~~~~~~~~~~ PID Limits ~~~~~~~~~~~~~~~~~ */ 174 | void PID_SetOutputLimits(PID_TypeDef *uPID, double Min, double Max) 175 | { 176 | /* ~~~~~~~~~~ Check value ~~~~~~~~~~ */ 177 | if (Min >= Max) 178 | { 179 | return; 180 | } 181 | 182 | uPID->OutMin = Min; 183 | uPID->OutMax = Max; 184 | 185 | /* ~~~~~~~~~~ Check PID Mode ~~~~~~~~~~ */ 186 | if (uPID->InAuto) 187 | { 188 | 189 | /* ..... Check out value ..... */ 190 | if (*uPID->MyOutput > uPID->OutMax) 191 | { 192 | *uPID->MyOutput = uPID->OutMax; 193 | } 194 | else if (*uPID->MyOutput < uPID->OutMin) 195 | { 196 | *uPID->MyOutput = uPID->OutMin; 197 | } 198 | else { } 199 | 200 | /* ..... Check out value ..... */ 201 | if (uPID->OutputSum > uPID->OutMax) 202 | { 203 | uPID->OutputSum = uPID->OutMax; 204 | } 205 | else if (uPID->OutputSum < uPID->OutMin) 206 | { 207 | uPID->OutputSum = uPID->OutMin; 208 | } 209 | else { } 210 | 211 | } 212 | 213 | } 214 | 215 | /* ~~~~~~~~~~~~~~~~ PID Tunings ~~~~~~~~~~~~~~~~ */ 216 | void PID_SetTunings(PID_TypeDef *uPID, double Kp, double Ki, double Kd) 217 | { 218 | PID_SetTunings2(uPID, Kp, Ki, Kd, uPID->POn); 219 | } 220 | void PID_SetTunings2(PID_TypeDef *uPID, double Kp, double Ki, double Kd, PIDPON_TypeDef POn) 221 | { 222 | 223 | double SampleTimeInSec; 224 | 225 | /* ~~~~~~~~~~ Check value ~~~~~~~~~~ */ 226 | if (Kp < 0 || Ki < 0 || Kd < 0) 227 | { 228 | return; 229 | } 230 | 231 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ 232 | uPID->POn = POn; 233 | uPID->POnE = (PIDPON_TypeDef)(POn == _PID_P_ON_E); 234 | 235 | uPID->DispKp = Kp; 236 | uPID->DispKi = Ki; 237 | uPID->DispKd = Kd; 238 | 239 | /* ~~~~~~~~~ Calculate time ~~~~~~~~ */ 240 | SampleTimeInSec = ((double)uPID->SampleTime) / 1000; 241 | 242 | uPID->Kp = Kp; 243 | uPID->Ki = Ki * SampleTimeInSec; 244 | uPID->Kd = Kd / SampleTimeInSec; 245 | 246 | /* ~~~~~~~~ Check direction ~~~~~~~~ */ 247 | if (uPID->ControllerDirection == _PID_CD_REVERSE) 248 | { 249 | 250 | uPID->Kp = (0 - uPID->Kp); 251 | uPID->Ki = (0 - uPID->Ki); 252 | uPID->Kd = (0 - uPID->Kd); 253 | 254 | } 255 | 256 | } 257 | 258 | /* ~~~~~~~~~~~~~~~ PID Direction ~~~~~~~~~~~~~~~ */ 259 | void PID_SetControllerDirection(PID_TypeDef *uPID, PIDCD_TypeDef Direction) 260 | { 261 | /* ~~~~~~~~~~ Check parameters ~~~~~~~~~~ */ 262 | if ((uPID->InAuto) && (Direction !=uPID->ControllerDirection)) 263 | { 264 | 265 | uPID->Kp = (0 - uPID->Kp); 266 | uPID->Ki = (0 - uPID->Ki); 267 | uPID->Kd = (0 - uPID->Kd); 268 | 269 | } 270 | 271 | uPID->ControllerDirection = Direction; 272 | 273 | } 274 | PIDCD_TypeDef PID_GetDirection(PID_TypeDef *uPID) 275 | { 276 | return uPID->ControllerDirection; 277 | } 278 | 279 | /* ~~~~~~~~~~~~~~~ PID Sampling ~~~~~~~~~~~~~~~~ */ 280 | void PID_SetSampleTime(PID_TypeDef *uPID, int32_t NewSampleTime) 281 | { 282 | 283 | double ratio; 284 | 285 | /* ~~~~~~~~~~ Check value ~~~~~~~~~~ */ 286 | if (NewSampleTime > 0) 287 | { 288 | 289 | ratio = (double)NewSampleTime / (double)uPID->SampleTime; 290 | 291 | uPID->Ki *= ratio; 292 | uPID->Kd /= ratio; 293 | uPID->SampleTime = (uint32_t)NewSampleTime; 294 | 295 | } 296 | 297 | } 298 | 299 | /* ~~~~~~~~~~~~~ Get Tunings Param ~~~~~~~~~~~~~ */ 300 | double PID_GetKp(PID_TypeDef *uPID) 301 | { 302 | return uPID->DispKp; 303 | } 304 | double PID_GetKi(PID_TypeDef *uPID) 305 | { 306 | return uPID->DispKi; 307 | } 308 | double PID_GetKd(PID_TypeDef *uPID) 309 | { 310 | return uPID->DispKd; 311 | } 312 | -------------------------------------------------------------------------------- /Core/Src/syscalls.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file syscalls.c 4 | * @author Auto-generated by STM32CubeIDE 5 | * @brief STM32CubeIDE Minimal System calls file 6 | * 7 | * For more information about which c-functions 8 | * need which of these lowlevel functions 9 | * please consult the Newlib libc-manual 10 | ****************************************************************************** 11 | * @attention 12 | * 13 | * Copyright (c) 2022 STMicroelectronics. 14 | * All rights reserved. 15 | * 16 | * This software is licensed under terms that can be found in the LICENSE file 17 | * in the root directory of this software component. 18 | * If no LICENSE file comes with this software, it is provided AS-IS. 19 | * 20 | ****************************************************************************** 21 | */ 22 | 23 | /* Includes */ 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | 34 | /* Variables */ 35 | extern int __io_putchar(int ch) __attribute__((weak)); 36 | extern int __io_getchar(void) __attribute__((weak)); 37 | 38 | 39 | char *__env[1] = { 0 }; 40 | char **environ = __env; 41 | 42 | 43 | /* Functions */ 44 | void initialise_monitor_handles() 45 | { 46 | } 47 | 48 | int _getpid(void) 49 | { 50 | return 1; 51 | } 52 | 53 | int _kill(int pid, int sig) 54 | { 55 | errno = EINVAL; 56 | return -1; 57 | } 58 | 59 | void _exit (int status) 60 | { 61 | _kill(status, -1); 62 | while (1) {} /* Make sure we hang here */ 63 | } 64 | 65 | __attribute__((weak)) int _read(int file, char *ptr, int len) 66 | { 67 | int DataIdx; 68 | 69 | for (DataIdx = 0; DataIdx < len; DataIdx++) 70 | { 71 | *ptr++ = __io_getchar(); 72 | } 73 | 74 | return len; 75 | } 76 | 77 | __attribute__((weak)) int _write(int file, char *ptr, int len) 78 | { 79 | int DataIdx; 80 | 81 | for (DataIdx = 0; DataIdx < len; DataIdx++) 82 | { 83 | __io_putchar(*ptr++); 84 | } 85 | return len; 86 | } 87 | 88 | int _close(int file) 89 | { 90 | return -1; 91 | } 92 | 93 | 94 | int _fstat(int file, struct stat *st) 95 | { 96 | st->st_mode = S_IFCHR; 97 | return 0; 98 | } 99 | 100 | int _isatty(int file) 101 | { 102 | return 1; 103 | } 104 | 105 | int _lseek(int file, int ptr, int dir) 106 | { 107 | return 0; 108 | } 109 | 110 | int _open(char *path, int flags, ...) 111 | { 112 | /* Pretend like we always fail */ 113 | return -1; 114 | } 115 | 116 | int _wait(int *status) 117 | { 118 | errno = ECHILD; 119 | return -1; 120 | } 121 | 122 | int _unlink(char *name) 123 | { 124 | errno = ENOENT; 125 | return -1; 126 | } 127 | 128 | int _times(struct tms *buf) 129 | { 130 | return -1; 131 | } 132 | 133 | int _stat(char *file, struct stat *st) 134 | { 135 | st->st_mode = S_IFCHR; 136 | return 0; 137 | } 138 | 139 | int _link(char *old, char *new) 140 | { 141 | errno = EMLINK; 142 | return -1; 143 | } 144 | 145 | int _fork(void) 146 | { 147 | errno = EAGAIN; 148 | return -1; 149 | } 150 | 151 | int _execve(char *name, char **argv, char **env) 152 | { 153 | errno = ENOMEM; 154 | return -1; 155 | } 156 | -------------------------------------------------------------------------------- /Core/Src/sysmem.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file sysmem.c 4 | * @author Generated by STM32CubeIDE 5 | * @brief STM32CubeIDE System Memory calls file 6 | * 7 | * For more information about which C functions 8 | * need which of these lowlevel functions 9 | * please consult the newlib libc manual 10 | ****************************************************************************** 11 | * @attention 12 | * 13 | * Copyright (c) 2022 STMicroelectronics. 14 | * All rights reserved. 15 | * 16 | * This software is licensed under terms that can be found in the LICENSE file 17 | * in the root directory of this software component. 18 | * If no LICENSE file comes with this software, it is provided AS-IS. 19 | * 20 | ****************************************************************************** 21 | */ 22 | 23 | /* Includes */ 24 | #include 25 | #include 26 | 27 | /** 28 | * Pointer to the current high watermark of the heap usage 29 | */ 30 | static uint8_t *__sbrk_heap_end = NULL; 31 | 32 | /** 33 | * @brief _sbrk() allocates memory to the newlib heap and is used by malloc 34 | * and others from the C library 35 | * 36 | * @verbatim 37 | * ############################################################################ 38 | * # .data # .bss # newlib heap # MSP stack # 39 | * # # # # Reserved by _Min_Stack_Size # 40 | * ############################################################################ 41 | * ^-- RAM start ^-- _end _estack, RAM end --^ 42 | * @endverbatim 43 | * 44 | * This implementation starts allocating at the '_end' linker symbol 45 | * The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack 46 | * The implementation considers '_estack' linker symbol to be RAM end 47 | * NOTE: If the MSP stack, at any point during execution, grows larger than the 48 | * reserved size, please increase the '_Min_Stack_Size'. 49 | * 50 | * @param incr Memory size 51 | * @return Pointer to allocated memory 52 | */ 53 | void *_sbrk(ptrdiff_t incr) 54 | { 55 | extern uint8_t _end; /* Symbol defined in the linker script */ 56 | extern uint8_t _estack; /* Symbol defined in the linker script */ 57 | extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */ 58 | const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size; 59 | const uint8_t *max_heap = (uint8_t *)stack_limit; 60 | uint8_t *prev_heap_end; 61 | 62 | /* Initialize heap end at first call */ 63 | if (NULL == __sbrk_heap_end) 64 | { 65 | __sbrk_heap_end = &_end; 66 | } 67 | 68 | /* Protect heap from growing into the reserved MSP stack */ 69 | if (__sbrk_heap_end + incr > max_heap) 70 | { 71 | errno = ENOMEM; 72 | return (void *)-1; 73 | } 74 | 75 | prev_heap_end = __sbrk_heap_end; 76 | __sbrk_heap_end += incr; 77 | 78 | return (void *)prev_heap_end; 79 | } 80 | -------------------------------------------------------------------------------- /Core/Src/usb_comm.c: -------------------------------------------------------------------------------- 1 | /* 2 | * usb_comm.c 3 | * 4 | * Created on: 18 paź 2022 5 | * Author: KoSik 6 | * e-mail: kosik84@gmail.com 7 | * 8 | * version: 1.0 9 | */ 10 | #include 11 | #include 12 | #include 13 | 14 | uint16_t expectedDegree = 0; 15 | uint16_t expectedPosition = 0; 16 | uint16_t expectedPower = 30; 17 | uint8_t mode = 0; 18 | 19 | 20 | uint8_t read_variable(uint8_t *Data, char sparam[3], uint16_t *param, uint8_t array_size){ 21 | uint8_t buf[10]; 22 | uint8_t q; 23 | 24 | for (q = 0; q < sizeof(buf); q++) { // clean buffer 25 | buf[q] = 0; 26 | } 27 | if (Data[1] == sparam[0] && Data[2] == sparam[1] && Data[3] == sparam[2]) { 28 | for (q = 0; q < sizeof(Data); q++) { 29 | if (Data[4 + q] == '/') { 30 | break; 31 | } else { 32 | buf[q] = Data[4 + q]; 33 | } 34 | } 35 | *param = atoi((char*) buf); //variable save 36 | for (uint16_t i = 0; i < (array_size - (4 + q)); i++) { 37 | Data[i] = Data[4 + q + i]; 38 | } 39 | return 1; 40 | } else { 41 | return 0; 42 | } 43 | } 44 | 45 | void decode_message(uint8_t *Data, uint8_t array_size) { 46 | for (uint8_t j = 0; j < 3; j++) { 47 | if (Data[0] == '#') { 48 | if (!read_variable(Data, "mod", &mode, array_size)) { 49 | if (!read_variable(Data, "pwr", &expectedPower, array_size)) { 50 | if (!read_variable(Data, "pos", &expectedPosition, array_size)) { 51 | read_variable(Data, "deg", &expectedDegree, array_size); 52 | } 53 | } 54 | } 55 | //------------------------------------------------------------------------ 56 | 57 | else { 58 | break; 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xb.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KoSik-prog/STM32F103_BLDC_Driver/3d108960543fd65714011477147a4a047ca86acc/Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xb.h -------------------------------------------------------------------------------- /Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f1xx.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KoSik-prog/STM32F103_BLDC_Driver/3d108960543fd65714011477147a4a047ca86acc/Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f1xx.h -------------------------------------------------------------------------------- /Drivers/CMSIS/Device/ST/STM32F1xx/Include/system_stm32f1xx.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file system_stm32f10x.h 4 | * @author MCD Application Team 5 | * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Header File. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

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

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /** @addtogroup CMSIS 21 | * @{ 22 | */ 23 | 24 | /** @addtogroup stm32f10x_system 25 | * @{ 26 | */ 27 | 28 | /** 29 | * @brief Define to prevent recursive inclusion 30 | */ 31 | #ifndef __SYSTEM_STM32F10X_H 32 | #define __SYSTEM_STM32F10X_H 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | /** @addtogroup STM32F10x_System_Includes 39 | * @{ 40 | */ 41 | 42 | /** 43 | * @} 44 | */ 45 | 46 | 47 | /** @addtogroup STM32F10x_System_Exported_types 48 | * @{ 49 | */ 50 | 51 | extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */ 52 | extern const uint8_t AHBPrescTable[16U]; /*!< AHB prescalers table values */ 53 | extern const uint8_t APBPrescTable[8U]; /*!< APB prescalers table values */ 54 | 55 | /** 56 | * @} 57 | */ 58 | 59 | /** @addtogroup STM32F10x_System_Exported_Constants 60 | * @{ 61 | */ 62 | 63 | /** 64 | * @} 65 | */ 66 | 67 | /** @addtogroup STM32F10x_System_Exported_Macros 68 | * @{ 69 | */ 70 | 71 | /** 72 | * @} 73 | */ 74 | 75 | /** @addtogroup STM32F10x_System_Exported_Functions 76 | * @{ 77 | */ 78 | 79 | extern void SystemInit(void); 80 | extern void SystemCoreClockUpdate(void); 81 | /** 82 | * @} 83 | */ 84 | 85 | #ifdef __cplusplus 86 | } 87 | #endif 88 | 89 | #endif /*__SYSTEM_STM32F10X_H */ 90 | 91 | /** 92 | * @} 93 | */ 94 | 95 | /** 96 | * @} 97 | */ 98 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 99 | -------------------------------------------------------------------------------- /Drivers/CMSIS/Device/ST/STM32F1xx/License.md: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 10 | 11 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 12 | 13 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 14 | 15 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 16 | 17 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 18 | 19 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 20 | 21 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 22 | 23 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 24 | 25 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 26 | 27 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 28 | 29 | 2. Grant of Copyright License. 30 | 31 | Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 32 | 33 | 3. Grant of Patent License. 34 | 35 | Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 36 | 37 | 4. Redistribution. 38 | 39 | You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 40 | 1.You must give any other recipients of the Work or Derivative Works a copy of this License; and 41 | 2.You must cause any modified files to carry prominent notices stating that You changed the files; and 42 | 3.You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 43 | 4.If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. 44 | 45 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 46 | 47 | 5. Submission of Contributions. 48 | 49 | Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 50 | 51 | 6. Trademarks. 52 | 53 | This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 54 | 55 | 7. Disclaimer of Warranty. 56 | 57 | Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 58 | 59 | 8. Limitation of Liability. 60 | 61 | In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 62 | 63 | 9. Accepting Warranty or Additional Liability. 64 | 65 | While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 66 | 67 | END OF TERMS AND CONDITIONS 68 | 69 | APPENDIX: 70 | 71 | Copyright [2019] [STMicroelectronics] 72 | 73 | Licensed under the Apache License, Version 2.0 (the "License"); 74 | you may not use this file except in compliance with the License. 75 | You may obtain a copy of the License at 76 | 77 | http://www.apache.org/licenses/LICENSE-2.0 78 | 79 | Unless required by applicable law or agreed to in writing, software 80 | distributed under the License is distributed on an "AS IS" BASIS, 81 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 82 | See the License for the specific language governing permissions and 83 | limitations under the License. -------------------------------------------------------------------------------- /Drivers/CMSIS/Include/cmsis_compiler.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file cmsis_compiler.h 3 | * @brief CMSIS compiler generic header file 4 | * @version V5.0.4 5 | * @date 10. January 2018 6 | ******************************************************************************/ 7 | /* 8 | * Copyright (c) 2009-2018 Arm Limited. All rights reserved. 9 | * 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the License); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | */ 24 | 25 | #ifndef __CMSIS_COMPILER_H 26 | #define __CMSIS_COMPILER_H 27 | 28 | #include 29 | 30 | /* 31 | * Arm Compiler 4/5 32 | */ 33 | #if defined ( __CC_ARM ) 34 | #include "cmsis_armcc.h" 35 | 36 | 37 | /* 38 | * Arm Compiler 6 (armclang) 39 | */ 40 | #elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 41 | #include "cmsis_armclang.h" 42 | 43 | 44 | /* 45 | * GNU Compiler 46 | */ 47 | #elif defined ( __GNUC__ ) 48 | #include "cmsis_gcc.h" 49 | 50 | 51 | /* 52 | * IAR Compiler 53 | */ 54 | #elif defined ( __ICCARM__ ) 55 | #include 56 | 57 | 58 | /* 59 | * TI Arm Compiler 60 | */ 61 | #elif defined ( __TI_ARM__ ) 62 | #include 63 | 64 | #ifndef __ASM 65 | #define __ASM __asm 66 | #endif 67 | #ifndef __INLINE 68 | #define __INLINE inline 69 | #endif 70 | #ifndef __STATIC_INLINE 71 | #define __STATIC_INLINE static inline 72 | #endif 73 | #ifndef __STATIC_FORCEINLINE 74 | #define __STATIC_FORCEINLINE __STATIC_INLINE 75 | #endif 76 | #ifndef __NO_RETURN 77 | #define __NO_RETURN __attribute__((noreturn)) 78 | #endif 79 | #ifndef __USED 80 | #define __USED __attribute__((used)) 81 | #endif 82 | #ifndef __WEAK 83 | #define __WEAK __attribute__((weak)) 84 | #endif 85 | #ifndef __PACKED 86 | #define __PACKED __attribute__((packed)) 87 | #endif 88 | #ifndef __PACKED_STRUCT 89 | #define __PACKED_STRUCT struct __attribute__((packed)) 90 | #endif 91 | #ifndef __PACKED_UNION 92 | #define __PACKED_UNION union __attribute__((packed)) 93 | #endif 94 | #ifndef __UNALIGNED_UINT32 /* deprecated */ 95 | struct __attribute__((packed)) T_UINT32 { uint32_t v; }; 96 | #define __UNALIGNED_UINT32(x) (((struct T_UINT32 *)(x))->v) 97 | #endif 98 | #ifndef __UNALIGNED_UINT16_WRITE 99 | __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; }; 100 | #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void*)(addr))->v) = (val)) 101 | #endif 102 | #ifndef __UNALIGNED_UINT16_READ 103 | __PACKED_STRUCT T_UINT16_READ { uint16_t v; }; 104 | #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v) 105 | #endif 106 | #ifndef __UNALIGNED_UINT32_WRITE 107 | __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; }; 108 | #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val)) 109 | #endif 110 | #ifndef __UNALIGNED_UINT32_READ 111 | __PACKED_STRUCT T_UINT32_READ { uint32_t v; }; 112 | #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v) 113 | #endif 114 | #ifndef __ALIGNED 115 | #define __ALIGNED(x) __attribute__((aligned(x))) 116 | #endif 117 | #ifndef __RESTRICT 118 | #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored. 119 | #define __RESTRICT 120 | #endif 121 | 122 | 123 | /* 124 | * TASKING Compiler 125 | */ 126 | #elif defined ( __TASKING__ ) 127 | /* 128 | * The CMSIS functions have been implemented as intrinsics in the compiler. 129 | * Please use "carm -?i" to get an up to date list of all intrinsics, 130 | * Including the CMSIS ones. 131 | */ 132 | 133 | #ifndef __ASM 134 | #define __ASM __asm 135 | #endif 136 | #ifndef __INLINE 137 | #define __INLINE inline 138 | #endif 139 | #ifndef __STATIC_INLINE 140 | #define __STATIC_INLINE static inline 141 | #endif 142 | #ifndef __STATIC_FORCEINLINE 143 | #define __STATIC_FORCEINLINE __STATIC_INLINE 144 | #endif 145 | #ifndef __NO_RETURN 146 | #define __NO_RETURN __attribute__((noreturn)) 147 | #endif 148 | #ifndef __USED 149 | #define __USED __attribute__((used)) 150 | #endif 151 | #ifndef __WEAK 152 | #define __WEAK __attribute__((weak)) 153 | #endif 154 | #ifndef __PACKED 155 | #define __PACKED __packed__ 156 | #endif 157 | #ifndef __PACKED_STRUCT 158 | #define __PACKED_STRUCT struct __packed__ 159 | #endif 160 | #ifndef __PACKED_UNION 161 | #define __PACKED_UNION union __packed__ 162 | #endif 163 | #ifndef __UNALIGNED_UINT32 /* deprecated */ 164 | struct __packed__ T_UINT32 { uint32_t v; }; 165 | #define __UNALIGNED_UINT32(x) (((struct T_UINT32 *)(x))->v) 166 | #endif 167 | #ifndef __UNALIGNED_UINT16_WRITE 168 | __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; }; 169 | #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val)) 170 | #endif 171 | #ifndef __UNALIGNED_UINT16_READ 172 | __PACKED_STRUCT T_UINT16_READ { uint16_t v; }; 173 | #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v) 174 | #endif 175 | #ifndef __UNALIGNED_UINT32_WRITE 176 | __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; }; 177 | #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val)) 178 | #endif 179 | #ifndef __UNALIGNED_UINT32_READ 180 | __PACKED_STRUCT T_UINT32_READ { uint32_t v; }; 181 | #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v) 182 | #endif 183 | #ifndef __ALIGNED 184 | #define __ALIGNED(x) __align(x) 185 | #endif 186 | #ifndef __RESTRICT 187 | #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored. 188 | #define __RESTRICT 189 | #endif 190 | 191 | 192 | /* 193 | * COSMIC Compiler 194 | */ 195 | #elif defined ( __CSMC__ ) 196 | #include 197 | 198 | #ifndef __ASM 199 | #define __ASM _asm 200 | #endif 201 | #ifndef __INLINE 202 | #define __INLINE inline 203 | #endif 204 | #ifndef __STATIC_INLINE 205 | #define __STATIC_INLINE static inline 206 | #endif 207 | #ifndef __STATIC_FORCEINLINE 208 | #define __STATIC_FORCEINLINE __STATIC_INLINE 209 | #endif 210 | #ifndef __NO_RETURN 211 | // NO RETURN is automatically detected hence no warning here 212 | #define __NO_RETURN 213 | #endif 214 | #ifndef __USED 215 | #warning No compiler specific solution for __USED. __USED is ignored. 216 | #define __USED 217 | #endif 218 | #ifndef __WEAK 219 | #define __WEAK __weak 220 | #endif 221 | #ifndef __PACKED 222 | #define __PACKED @packed 223 | #endif 224 | #ifndef __PACKED_STRUCT 225 | #define __PACKED_STRUCT @packed struct 226 | #endif 227 | #ifndef __PACKED_UNION 228 | #define __PACKED_UNION @packed union 229 | #endif 230 | #ifndef __UNALIGNED_UINT32 /* deprecated */ 231 | @packed struct T_UINT32 { uint32_t v; }; 232 | #define __UNALIGNED_UINT32(x) (((struct T_UINT32 *)(x))->v) 233 | #endif 234 | #ifndef __UNALIGNED_UINT16_WRITE 235 | __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; }; 236 | #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val)) 237 | #endif 238 | #ifndef __UNALIGNED_UINT16_READ 239 | __PACKED_STRUCT T_UINT16_READ { uint16_t v; }; 240 | #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v) 241 | #endif 242 | #ifndef __UNALIGNED_UINT32_WRITE 243 | __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; }; 244 | #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val)) 245 | #endif 246 | #ifndef __UNALIGNED_UINT32_READ 247 | __PACKED_STRUCT T_UINT32_READ { uint32_t v; }; 248 | #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v) 249 | #endif 250 | #ifndef __ALIGNED 251 | #warning No compiler specific solution for __ALIGNED. __ALIGNED is ignored. 252 | #define __ALIGNED(x) 253 | #endif 254 | #ifndef __RESTRICT 255 | #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored. 256 | #define __RESTRICT 257 | #endif 258 | 259 | 260 | #else 261 | #error Unknown compiler. 262 | #endif 263 | 264 | 265 | #endif /* __CMSIS_COMPILER_H */ 266 | 267 | -------------------------------------------------------------------------------- /Drivers/CMSIS/Include/cmsis_version.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file cmsis_version.h 3 | * @brief CMSIS Core(M) Version definitions 4 | * @version V5.0.2 5 | * @date 19. April 2017 6 | ******************************************************************************/ 7 | /* 8 | * Copyright (c) 2009-2017 ARM Limited. All rights reserved. 9 | * 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the License); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | */ 24 | 25 | #if defined ( __ICCARM__ ) 26 | #pragma system_include /* treat file as system include file for MISRA check */ 27 | #elif defined (__clang__) 28 | #pragma clang system_header /* treat file as system include file */ 29 | #endif 30 | 31 | #ifndef __CMSIS_VERSION_H 32 | #define __CMSIS_VERSION_H 33 | 34 | /* CMSIS Version definitions */ 35 | #define __CM_CMSIS_VERSION_MAIN ( 5U) /*!< [31:16] CMSIS Core(M) main version */ 36 | #define __CM_CMSIS_VERSION_SUB ( 1U) /*!< [15:0] CMSIS Core(M) sub version */ 37 | #define __CM_CMSIS_VERSION ((__CM_CMSIS_VERSION_MAIN << 16U) | \ 38 | __CM_CMSIS_VERSION_SUB ) /*!< CMSIS Core(M) version number */ 39 | #endif 40 | -------------------------------------------------------------------------------- /Drivers/CMSIS/Include/tz_context.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * @file tz_context.h 3 | * @brief Context Management for Armv8-M TrustZone 4 | * @version V1.0.1 5 | * @date 10. January 2018 6 | ******************************************************************************/ 7 | /* 8 | * Copyright (c) 2017-2018 Arm Limited. All rights reserved. 9 | * 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the License); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | */ 24 | 25 | #if defined ( __ICCARM__ ) 26 | #pragma system_include /* treat file as system include file for MISRA check */ 27 | #elif defined (__clang__) 28 | #pragma clang system_header /* treat file as system include file */ 29 | #endif 30 | 31 | #ifndef TZ_CONTEXT_H 32 | #define TZ_CONTEXT_H 33 | 34 | #include 35 | 36 | #ifndef TZ_MODULEID_T 37 | #define TZ_MODULEID_T 38 | /// \details Data type that identifies secure software modules called by a process. 39 | typedef uint32_t TZ_ModuleId_t; 40 | #endif 41 | 42 | /// \details TZ Memory ID identifies an allocated memory slot. 43 | typedef uint32_t TZ_MemoryId_t; 44 | 45 | /// Initialize secure context memory system 46 | /// \return execution status (1: success, 0: error) 47 | uint32_t TZ_InitContextSystem_S (void); 48 | 49 | /// Allocate context memory for calling secure software modules in TrustZone 50 | /// \param[in] module identifies software modules called from non-secure mode 51 | /// \return value != 0 id TrustZone memory slot identifier 52 | /// \return value 0 no memory available or internal error 53 | TZ_MemoryId_t TZ_AllocModuleContext_S (TZ_ModuleId_t module); 54 | 55 | /// Free context memory that was previously allocated with \ref TZ_AllocModuleContext_S 56 | /// \param[in] id TrustZone memory slot identifier 57 | /// \return execution status (1: success, 0: error) 58 | uint32_t TZ_FreeModuleContext_S (TZ_MemoryId_t id); 59 | 60 | /// Load secure context (called on RTOS thread context switch) 61 | /// \param[in] id TrustZone memory slot identifier 62 | /// \return execution status (1: success, 0: error) 63 | uint32_t TZ_LoadContext_S (TZ_MemoryId_t id); 64 | 65 | /// Store secure context (called on RTOS thread context switch) 66 | /// \param[in] id TrustZone memory slot identifier 67 | /// \return execution status (1: success, 0: error) 68 | uint32_t TZ_StoreContext_S (TZ_MemoryId_t id); 69 | 70 | #endif // TZ_CONTEXT_H 71 | -------------------------------------------------------------------------------- /Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_def.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f1xx_hal_def.h 4 | * @author MCD Application Team 5 | * @brief This file contains HAL common defines, enumeration, macros and 6 | * structures definitions. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | *

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

12 | * 13 | * This software component is licensed by ST under BSD 3-Clause license, 14 | * the "License"; You may not use this file except in compliance with the 15 | * License. You may obtain a copy of the License at: 16 | * opensource.org/licenses/BSD-3-Clause 17 | * 18 | ****************************************************************************** 19 | */ 20 | 21 | /* Define to prevent recursive inclusion -------------------------------------*/ 22 | #ifndef __STM32F1xx_HAL_DEF 23 | #define __STM32F1xx_HAL_DEF 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | /* Includes ------------------------------------------------------------------*/ 30 | #include "stm32f1xx.h" 31 | #include "Legacy/stm32_hal_legacy.h" 32 | #include 33 | 34 | /* Exported types ------------------------------------------------------------*/ 35 | 36 | /** 37 | * @brief HAL Status structures definition 38 | */ 39 | typedef enum 40 | { 41 | HAL_OK = 0x00U, 42 | HAL_ERROR = 0x01U, 43 | HAL_BUSY = 0x02U, 44 | HAL_TIMEOUT = 0x03U 45 | } HAL_StatusTypeDef; 46 | 47 | /** 48 | * @brief HAL Lock structures definition 49 | */ 50 | typedef enum 51 | { 52 | HAL_UNLOCKED = 0x00U, 53 | HAL_LOCKED = 0x01U 54 | } HAL_LockTypeDef; 55 | 56 | /* Exported macro ------------------------------------------------------------*/ 57 | #define HAL_MAX_DELAY 0xFFFFFFFFU 58 | 59 | #define HAL_IS_BIT_SET(REG, BIT) (((REG) & (BIT)) != 0U) 60 | #define HAL_IS_BIT_CLR(REG, BIT) (((REG) & (BIT)) == 0U) 61 | 62 | #define __HAL_LINKDMA(__HANDLE__, __PPP_DMA_FIELD__, __DMA_HANDLE__) \ 63 | do{ \ 64 | (__HANDLE__)->__PPP_DMA_FIELD__ = &(__DMA_HANDLE__); \ 65 | (__DMA_HANDLE__).Parent = (__HANDLE__); \ 66 | } while(0U) 67 | 68 | #define UNUSED(X) (void)X /* To avoid gcc/g++ warnings */ 69 | 70 | /** @brief Reset the Handle's State field. 71 | * @param __HANDLE__ specifies the Peripheral Handle. 72 | * @note This macro can be used for the following purpose: 73 | * - When the Handle is declared as local variable; before passing it as parameter 74 | * to HAL_PPP_Init() for the first time, it is mandatory to use this macro 75 | * to set to 0 the Handle's "State" field. 76 | * Otherwise, "State" field may have any random value and the first time the function 77 | * HAL_PPP_Init() is called, the low level hardware initialization will be missed 78 | * (i.e. HAL_PPP_MspInit() will not be executed). 79 | * - When there is a need to reconfigure the low level hardware: instead of calling 80 | * HAL_PPP_DeInit() then HAL_PPP_Init(), user can make a call to this macro then HAL_PPP_Init(). 81 | * In this later function, when the Handle's "State" field is set to 0, it will execute the function 82 | * HAL_PPP_MspInit() which will reconfigure the low level hardware. 83 | * @retval None 84 | */ 85 | #define __HAL_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = 0U) 86 | 87 | #if (USE_RTOS == 1U) 88 | /* Reserved for future use */ 89 | #error "USE_RTOS should be 0 in the current HAL release" 90 | #else 91 | #define __HAL_LOCK(__HANDLE__) \ 92 | do{ \ 93 | if((__HANDLE__)->Lock == HAL_LOCKED) \ 94 | { \ 95 | return HAL_BUSY; \ 96 | } \ 97 | else \ 98 | { \ 99 | (__HANDLE__)->Lock = HAL_LOCKED; \ 100 | } \ 101 | }while (0U) 102 | 103 | #define __HAL_UNLOCK(__HANDLE__) \ 104 | do{ \ 105 | (__HANDLE__)->Lock = HAL_UNLOCKED; \ 106 | }while (0U) 107 | #endif /* USE_RTOS */ 108 | 109 | #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) /* ARM Compiler V6 */ 110 | #ifndef __weak 111 | #define __weak __attribute__((weak)) 112 | #endif 113 | #ifndef __packed 114 | #define __packed __attribute__((packed)) 115 | #endif 116 | #elif defined ( __GNUC__ ) && !defined (__CC_ARM) /* GNU Compiler */ 117 | #ifndef __weak 118 | #define __weak __attribute__((weak)) 119 | #endif /* __weak */ 120 | #ifndef __packed 121 | #define __packed __attribute__((__packed__)) 122 | #endif /* __packed */ 123 | #endif /* __GNUC__ */ 124 | 125 | 126 | /* Macro to get variable aligned on 4-bytes, for __ICCARM__ the directive "#pragma data_alignment=4" must be used instead */ 127 | #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) /* ARM Compiler V6 */ 128 | #ifndef __ALIGN_BEGIN 129 | #define __ALIGN_BEGIN 130 | #endif 131 | #ifndef __ALIGN_END 132 | #define __ALIGN_END __attribute__ ((aligned (4))) 133 | #endif 134 | #elif defined ( __GNUC__ ) && !defined (__CC_ARM) /* GNU Compiler */ 135 | #ifndef __ALIGN_END 136 | #define __ALIGN_END __attribute__ ((aligned (4))) 137 | #endif /* __ALIGN_END */ 138 | #ifndef __ALIGN_BEGIN 139 | #define __ALIGN_BEGIN 140 | #endif /* __ALIGN_BEGIN */ 141 | #else 142 | #ifndef __ALIGN_END 143 | #define __ALIGN_END 144 | #endif /* __ALIGN_END */ 145 | #ifndef __ALIGN_BEGIN 146 | #if defined (__CC_ARM) /* ARM Compiler V5*/ 147 | #define __ALIGN_BEGIN __align(4) 148 | #elif defined (__ICCARM__) /* IAR Compiler */ 149 | #define __ALIGN_BEGIN 150 | #endif /* __CC_ARM */ 151 | #endif /* __ALIGN_BEGIN */ 152 | #endif /* __GNUC__ */ 153 | 154 | 155 | /** 156 | * @brief __RAM_FUNC definition 157 | */ 158 | #if defined ( __CC_ARM ) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)) 159 | /* ARM Compiler V4/V5 and V6 160 | -------------------------- 161 | RAM functions are defined using the toolchain options. 162 | Functions that are executed in RAM should reside in a separate source module. 163 | Using the 'Options for File' dialog you can simply change the 'Code / Const' 164 | area of a module to a memory space in physical RAM. 165 | Available memory areas are declared in the 'Target' tab of the 'Options for Target' 166 | dialog. 167 | */ 168 | #define __RAM_FUNC 169 | 170 | #elif defined ( __ICCARM__ ) 171 | /* ICCARM Compiler 172 | --------------- 173 | RAM functions are defined using a specific toolchain keyword "__ramfunc". 174 | */ 175 | #define __RAM_FUNC __ramfunc 176 | 177 | #elif defined ( __GNUC__ ) 178 | /* GNU Compiler 179 | ------------ 180 | RAM functions are defined using a specific toolchain attribute 181 | "__attribute__((section(".RamFunc")))". 182 | */ 183 | #define __RAM_FUNC __attribute__((section(".RamFunc"))) 184 | 185 | #endif 186 | 187 | /** 188 | * @brief __NOINLINE definition 189 | */ 190 | #if defined ( __CC_ARM ) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)) || defined ( __GNUC__ ) 191 | /* ARM V4/V5 and V6 & GNU Compiler 192 | ------------------------------- 193 | */ 194 | #define __NOINLINE __attribute__ ( (noinline) ) 195 | 196 | #elif defined ( __ICCARM__ ) 197 | /* ICCARM Compiler 198 | --------------- 199 | */ 200 | #define __NOINLINE _Pragma("optimize = no_inline") 201 | 202 | #endif 203 | 204 | #ifdef __cplusplus 205 | } 206 | #endif 207 | 208 | #endif /* ___STM32F1xx_HAL_DEF */ 209 | 210 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 211 | -------------------------------------------------------------------------------- /Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_flash.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f1xx_hal_flash.h 4 | * @author MCD Application Team 5 | * @brief Header file of Flash HAL module. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

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

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __STM32F1xx_HAL_FLASH_H 22 | #define __STM32F1xx_HAL_FLASH_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm32f1xx_hal_def.h" 30 | 31 | /** @addtogroup STM32F1xx_HAL_Driver 32 | * @{ 33 | */ 34 | 35 | /** @addtogroup FLASH 36 | * @{ 37 | */ 38 | 39 | /** @addtogroup FLASH_Private_Constants 40 | * @{ 41 | */ 42 | #define FLASH_TIMEOUT_VALUE 50000U /* 50 s */ 43 | /** 44 | * @} 45 | */ 46 | 47 | /** @addtogroup FLASH_Private_Macros 48 | * @{ 49 | */ 50 | 51 | #define IS_FLASH_TYPEPROGRAM(VALUE) (((VALUE) == FLASH_TYPEPROGRAM_HALFWORD) || \ 52 | ((VALUE) == FLASH_TYPEPROGRAM_WORD) || \ 53 | ((VALUE) == FLASH_TYPEPROGRAM_DOUBLEWORD)) 54 | 55 | #if defined(FLASH_ACR_LATENCY) 56 | #define IS_FLASH_LATENCY(__LATENCY__) (((__LATENCY__) == FLASH_LATENCY_0) || \ 57 | ((__LATENCY__) == FLASH_LATENCY_1) || \ 58 | ((__LATENCY__) == FLASH_LATENCY_2)) 59 | 60 | #else 61 | #define IS_FLASH_LATENCY(__LATENCY__) ((__LATENCY__) == FLASH_LATENCY_0) 62 | #endif /* FLASH_ACR_LATENCY */ 63 | /** 64 | * @} 65 | */ 66 | 67 | /* Exported types ------------------------------------------------------------*/ 68 | /** @defgroup FLASH_Exported_Types FLASH Exported Types 69 | * @{ 70 | */ 71 | 72 | /** 73 | * @brief FLASH Procedure structure definition 74 | */ 75 | typedef enum 76 | { 77 | FLASH_PROC_NONE = 0U, 78 | FLASH_PROC_PAGEERASE = 1U, 79 | FLASH_PROC_MASSERASE = 2U, 80 | FLASH_PROC_PROGRAMHALFWORD = 3U, 81 | FLASH_PROC_PROGRAMWORD = 4U, 82 | FLASH_PROC_PROGRAMDOUBLEWORD = 5U 83 | } FLASH_ProcedureTypeDef; 84 | 85 | /** 86 | * @brief FLASH handle Structure definition 87 | */ 88 | typedef struct 89 | { 90 | __IO FLASH_ProcedureTypeDef ProcedureOnGoing; /*!< Internal variable to indicate which procedure is ongoing or not in IT context */ 91 | 92 | __IO uint32_t DataRemaining; /*!< Internal variable to save the remaining pages to erase or half-word to program in IT context */ 93 | 94 | __IO uint32_t Address; /*!< Internal variable to save address selected for program or erase */ 95 | 96 | __IO uint64_t Data; /*!< Internal variable to save data to be programmed */ 97 | 98 | HAL_LockTypeDef Lock; /*!< FLASH locking object */ 99 | 100 | __IO uint32_t ErrorCode; /*!< FLASH error code 101 | This parameter can be a value of @ref FLASH_Error_Codes */ 102 | } FLASH_ProcessTypeDef; 103 | 104 | /** 105 | * @} 106 | */ 107 | 108 | /* Exported constants --------------------------------------------------------*/ 109 | /** @defgroup FLASH_Exported_Constants FLASH Exported Constants 110 | * @{ 111 | */ 112 | 113 | /** @defgroup FLASH_Error_Codes FLASH Error Codes 114 | * @{ 115 | */ 116 | 117 | #define HAL_FLASH_ERROR_NONE 0x00U /*!< No error */ 118 | #define HAL_FLASH_ERROR_PROG 0x01U /*!< Programming error */ 119 | #define HAL_FLASH_ERROR_WRP 0x02U /*!< Write protection error */ 120 | #define HAL_FLASH_ERROR_OPTV 0x04U /*!< Option validity error */ 121 | 122 | /** 123 | * @} 124 | */ 125 | 126 | /** @defgroup FLASH_Type_Program FLASH Type Program 127 | * @{ 128 | */ 129 | #define FLASH_TYPEPROGRAM_HALFWORD 0x01U /*!ACR |= FLASH_ACR_HLFCYA) 183 | 184 | /** 185 | * @brief Disable the FLASH half cycle access. 186 | * @note half cycle access can only be used with a low-frequency clock of less than 187 | 8 MHz that can be obtained with the use of HSI or HSE but not of PLL. 188 | * @retval None 189 | */ 190 | #define __HAL_FLASH_HALF_CYCLE_ACCESS_DISABLE() (FLASH->ACR &= (~FLASH_ACR_HLFCYA)) 191 | 192 | /** 193 | * @} 194 | */ 195 | 196 | #if defined(FLASH_ACR_LATENCY) 197 | /** @defgroup FLASH_EM_Latency FLASH Latency 198 | * @brief macros to handle FLASH Latency 199 | * @{ 200 | */ 201 | 202 | /** 203 | * @brief Set the FLASH Latency. 204 | * @param __LATENCY__ FLASH Latency 205 | * The value of this parameter depend on device used within the same series 206 | * @retval None 207 | */ 208 | #define __HAL_FLASH_SET_LATENCY(__LATENCY__) (FLASH->ACR = (FLASH->ACR&(~FLASH_ACR_LATENCY)) | (__LATENCY__)) 209 | 210 | 211 | /** 212 | * @brief Get the FLASH Latency. 213 | * @retval FLASH Latency 214 | * The value of this parameter depend on device used within the same series 215 | */ 216 | #define __HAL_FLASH_GET_LATENCY() (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY)) 217 | 218 | /** 219 | * @} 220 | */ 221 | 222 | #endif /* FLASH_ACR_LATENCY */ 223 | /** @defgroup FLASH_Prefetch FLASH Prefetch 224 | * @brief macros to handle FLASH Prefetch buffer 225 | * @{ 226 | */ 227 | /** 228 | * @brief Enable the FLASH prefetch buffer. 229 | * @retval None 230 | */ 231 | #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE() (FLASH->ACR |= FLASH_ACR_PRFTBE) 232 | 233 | /** 234 | * @brief Disable the FLASH prefetch buffer. 235 | * @retval None 236 | */ 237 | #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE() (FLASH->ACR &= (~FLASH_ACR_PRFTBE)) 238 | 239 | /** 240 | * @} 241 | */ 242 | 243 | /** 244 | * @} 245 | */ 246 | 247 | /* Include FLASH HAL Extended module */ 248 | #include "stm32f1xx_hal_flash_ex.h" 249 | 250 | /* Exported functions --------------------------------------------------------*/ 251 | /** @addtogroup FLASH_Exported_Functions 252 | * @{ 253 | */ 254 | 255 | /** @addtogroup FLASH_Exported_Functions_Group1 256 | * @{ 257 | */ 258 | /* IO operation functions *****************************************************/ 259 | HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data); 260 | HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data); 261 | 262 | /* FLASH IRQ handler function */ 263 | void HAL_FLASH_IRQHandler(void); 264 | /* Callbacks in non blocking modes */ 265 | void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue); 266 | void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue); 267 | 268 | /** 269 | * @} 270 | */ 271 | 272 | /** @addtogroup FLASH_Exported_Functions_Group2 273 | * @{ 274 | */ 275 | /* Peripheral Control functions ***********************************************/ 276 | HAL_StatusTypeDef HAL_FLASH_Unlock(void); 277 | HAL_StatusTypeDef HAL_FLASH_Lock(void); 278 | HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void); 279 | HAL_StatusTypeDef HAL_FLASH_OB_Lock(void); 280 | void HAL_FLASH_OB_Launch(void); 281 | 282 | /** 283 | * @} 284 | */ 285 | 286 | /** @addtogroup FLASH_Exported_Functions_Group3 287 | * @{ 288 | */ 289 | /* Peripheral State and Error functions ***************************************/ 290 | uint32_t HAL_FLASH_GetError(void); 291 | 292 | /** 293 | * @} 294 | */ 295 | 296 | /** 297 | * @} 298 | */ 299 | 300 | /* Private function -------------------------------------------------*/ 301 | /** @addtogroup FLASH_Private_Functions 302 | * @{ 303 | */ 304 | HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout); 305 | #if defined(FLASH_BANK2_END) 306 | HAL_StatusTypeDef FLASH_WaitForLastOperationBank2(uint32_t Timeout); 307 | #endif /* FLASH_BANK2_END */ 308 | 309 | /** 310 | * @} 311 | */ 312 | 313 | /** 314 | * @} 315 | */ 316 | 317 | /** 318 | * @} 319 | */ 320 | 321 | #ifdef __cplusplus 322 | } 323 | #endif 324 | 325 | #endif /* __STM32F1xx_HAL_FLASH_H */ 326 | 327 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 328 | 329 | -------------------------------------------------------------------------------- /Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_pcd_ex.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f1xx_hal_pcd_ex.h 4 | * @author MCD Application Team 5 | * @brief Header file of PCD HAL Extension module. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

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

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef STM32F1xx_HAL_PCD_EX_H 22 | #define STM32F1xx_HAL_PCD_EX_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm32f1xx_hal_def.h" 30 | 31 | #if defined (USB) || defined (USB_OTG_FS) 32 | /** @addtogroup STM32F1xx_HAL_Driver 33 | * @{ 34 | */ 35 | 36 | /** @addtogroup PCDEx 37 | * @{ 38 | */ 39 | /* Exported types ------------------------------------------------------------*/ 40 | /* Exported constants --------------------------------------------------------*/ 41 | /* Exported macros -----------------------------------------------------------*/ 42 | /* Exported functions --------------------------------------------------------*/ 43 | /** @addtogroup PCDEx_Exported_Functions PCDEx Exported Functions 44 | * @{ 45 | */ 46 | /** @addtogroup PCDEx_Exported_Functions_Group1 Peripheral Control functions 47 | * @{ 48 | */ 49 | 50 | #if defined (USB_OTG_FS) 51 | HAL_StatusTypeDef HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo, uint16_t size); 52 | HAL_StatusTypeDef HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef *hpcd, uint16_t size); 53 | #endif /* defined (USB_OTG_FS) */ 54 | 55 | #if defined (USB) 56 | HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd, uint16_t ep_addr, 57 | uint16_t ep_kind, uint32_t pmaadress); 58 | 59 | void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state); 60 | #endif /* defined (USB) */ 61 | void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg); 62 | void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg); 63 | 64 | /** 65 | * @} 66 | */ 67 | 68 | /** 69 | * @} 70 | */ 71 | 72 | /** 73 | * @} 74 | */ 75 | 76 | /** 77 | * @} 78 | */ 79 | #endif /* defined (USB) || defined (USB_OTG_FS) */ 80 | 81 | #ifdef __cplusplus 82 | } 83 | #endif 84 | 85 | 86 | #endif /* STM32F1xx_HAL_PCD_EX_H */ 87 | 88 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 89 | -------------------------------------------------------------------------------- /Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_tim_ex.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f1xx_hal_tim_ex.h 4 | * @author MCD Application Team 5 | * @brief Header file of TIM HAL Extended module. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

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

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef STM32F1xx_HAL_TIM_EX_H 22 | #define STM32F1xx_HAL_TIM_EX_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm32f1xx_hal_def.h" 30 | 31 | /** @addtogroup STM32F1xx_HAL_Driver 32 | * @{ 33 | */ 34 | 35 | /** @addtogroup TIMEx 36 | * @{ 37 | */ 38 | 39 | /* Exported types ------------------------------------------------------------*/ 40 | /** @defgroup TIMEx_Exported_Types TIM Extended Exported Types 41 | * @{ 42 | */ 43 | 44 | /** 45 | * @brief TIM Hall sensor Configuration Structure definition 46 | */ 47 | 48 | typedef struct 49 | { 50 | uint32_t IC1Polarity; /*!< Specifies the active edge of the input signal. 51 | This parameter can be a value of @ref TIM_Input_Capture_Polarity */ 52 | 53 | uint32_t IC1Prescaler; /*!< Specifies the Input Capture Prescaler. 54 | This parameter can be a value of @ref TIM_Input_Capture_Prescaler */ 55 | 56 | uint32_t IC1Filter; /*!< Specifies the input capture filter. 57 | This parameter can be a number between Min_Data = 0x0 and Max_Data = 0xF */ 58 | 59 | uint32_t Commutation_Delay; /*!< Specifies the pulse value to be loaded into the Capture Compare Register. 60 | This parameter can be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF */ 61 | } TIM_HallSensor_InitTypeDef; 62 | /** 63 | * @} 64 | */ 65 | /* End of exported types -----------------------------------------------------*/ 66 | 67 | /* Exported constants --------------------------------------------------------*/ 68 | /** @defgroup TIMEx_Exported_Constants TIM Extended Exported Constants 69 | * @{ 70 | */ 71 | 72 | /** @defgroup TIMEx_Remap TIM Extended Remapping 73 | * @{ 74 | */ 75 | /** 76 | * @} 77 | */ 78 | 79 | /** 80 | * @} 81 | */ 82 | /* End of exported constants -------------------------------------------------*/ 83 | 84 | /* Exported macro ------------------------------------------------------------*/ 85 | /** @defgroup TIMEx_Exported_Macros TIM Extended Exported Macros 86 | * @{ 87 | */ 88 | 89 | /** 90 | * @} 91 | */ 92 | /* End of exported macro -----------------------------------------------------*/ 93 | 94 | /* Private macro -------------------------------------------------------------*/ 95 | /** @defgroup TIMEx_Private_Macros TIM Extended Private Macros 96 | * @{ 97 | */ 98 | 99 | /** 100 | * @} 101 | */ 102 | /* End of private macro ------------------------------------------------------*/ 103 | 104 | /* Exported functions --------------------------------------------------------*/ 105 | /** @addtogroup TIMEx_Exported_Functions TIM Extended Exported Functions 106 | * @{ 107 | */ 108 | 109 | /** @addtogroup TIMEx_Exported_Functions_Group1 Extended Timer Hall Sensor functions 110 | * @brief Timer Hall Sensor functions 111 | * @{ 112 | */ 113 | /* Timer Hall Sensor functions **********************************************/ 114 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, TIM_HallSensor_InitTypeDef *sConfig); 115 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim); 116 | 117 | void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef *htim); 118 | void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim); 119 | 120 | /* Blocking mode: Polling */ 121 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim); 122 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim); 123 | /* Non-Blocking mode: Interrupt */ 124 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim); 125 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim); 126 | /* Non-Blocking mode: DMA */ 127 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length); 128 | HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim); 129 | /** 130 | * @} 131 | */ 132 | 133 | /** @addtogroup TIMEx_Exported_Functions_Group2 Extended Timer Complementary Output Compare functions 134 | * @brief Timer Complementary Output Compare functions 135 | * @{ 136 | */ 137 | /* Timer Complementary Output Compare functions *****************************/ 138 | /* Blocking mode: Polling */ 139 | HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel); 140 | HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel); 141 | 142 | /* Non-Blocking mode: Interrupt */ 143 | HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel); 144 | HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel); 145 | 146 | /* Non-Blocking mode: DMA */ 147 | HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length); 148 | HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel); 149 | /** 150 | * @} 151 | */ 152 | 153 | /** @addtogroup TIMEx_Exported_Functions_Group3 Extended Timer Complementary PWM functions 154 | * @brief Timer Complementary PWM functions 155 | * @{ 156 | */ 157 | /* Timer Complementary PWM functions ****************************************/ 158 | /* Blocking mode: Polling */ 159 | HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel); 160 | HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel); 161 | 162 | /* Non-Blocking mode: Interrupt */ 163 | HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel); 164 | HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel); 165 | /* Non-Blocking mode: DMA */ 166 | HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length); 167 | HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel); 168 | /** 169 | * @} 170 | */ 171 | 172 | /** @addtogroup TIMEx_Exported_Functions_Group4 Extended Timer Complementary One Pulse functions 173 | * @brief Timer Complementary One Pulse functions 174 | * @{ 175 | */ 176 | /* Timer Complementary One Pulse functions **********************************/ 177 | /* Blocking mode: Polling */ 178 | HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel); 179 | HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel); 180 | 181 | /* Non-Blocking mode: Interrupt */ 182 | HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel); 183 | HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel); 184 | /** 185 | * @} 186 | */ 187 | 188 | /** @addtogroup TIMEx_Exported_Functions_Group5 Extended Peripheral Control functions 189 | * @brief Peripheral Control functions 190 | * @{ 191 | */ 192 | /* Extended Control functions ************************************************/ 193 | HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger, 194 | uint32_t CommutationSource); 195 | HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger, 196 | uint32_t CommutationSource); 197 | HAL_StatusTypeDef HAL_TIMEx_ConfigCommutEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger, 198 | uint32_t CommutationSource); 199 | HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim, 200 | TIM_MasterConfigTypeDef *sMasterConfig); 201 | HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim, 202 | TIM_BreakDeadTimeConfigTypeDef *sBreakDeadTimeConfig); 203 | HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap); 204 | /** 205 | * @} 206 | */ 207 | 208 | /** @addtogroup TIMEx_Exported_Functions_Group6 Extended Callbacks functions 209 | * @brief Extended Callbacks functions 210 | * @{ 211 | */ 212 | /* Extended Callback **********************************************************/ 213 | void HAL_TIMEx_CommutCallback(TIM_HandleTypeDef *htim); 214 | void HAL_TIMEx_CommutHalfCpltCallback(TIM_HandleTypeDef *htim); 215 | void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim); 216 | /** 217 | * @} 218 | */ 219 | 220 | /** @addtogroup TIMEx_Exported_Functions_Group7 Extended Peripheral State functions 221 | * @brief Extended Peripheral State functions 222 | * @{ 223 | */ 224 | /* Extended Peripheral State functions ***************************************/ 225 | HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(TIM_HandleTypeDef *htim); 226 | HAL_TIM_ChannelStateTypeDef HAL_TIMEx_GetChannelNState(TIM_HandleTypeDef *htim, uint32_t ChannelN); 227 | /** 228 | * @} 229 | */ 230 | 231 | /** 232 | * @} 233 | */ 234 | /* End of exported functions -------------------------------------------------*/ 235 | 236 | /* Private functions----------------------------------------------------------*/ 237 | /** @addtogroup TIMEx_Private_Functions TIMEx Private Functions 238 | * @{ 239 | */ 240 | void TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma); 241 | void TIMEx_DMACommutationHalfCplt(DMA_HandleTypeDef *hdma); 242 | /** 243 | * @} 244 | */ 245 | /* End of private functions --------------------------------------------------*/ 246 | 247 | /** 248 | * @} 249 | */ 250 | 251 | /** 252 | * @} 253 | */ 254 | 255 | #ifdef __cplusplus 256 | } 257 | #endif 258 | 259 | 260 | #endif /* STM32F1xx_HAL_TIM_EX_H */ 261 | 262 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 263 | -------------------------------------------------------------------------------- /Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_ll_utils.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f1xx_ll_utils.h 4 | * @author MCD Application Team 5 | * @brief Header file of UTILS LL module. 6 | @verbatim 7 | ============================================================================== 8 | ##### How to use this driver ##### 9 | ============================================================================== 10 | [..] 11 | The LL UTILS driver contains a set of generic APIs that can be 12 | used by user: 13 | (+) Device electronic signature 14 | (+) Timing functions 15 | (+) PLL configuration functions 16 | 17 | @endverbatim 18 | ****************************************************************************** 19 | * @attention 20 | * 21 | *

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

23 | * 24 | * This software component is licensed by ST under BSD 3-Clause license, 25 | * the "License"; You may not use this file except in compliance with the 26 | * License. You may obtain a copy of the License at: 27 | * opensource.org/licenses/BSD-3-Clause 28 | * 29 | ****************************************************************************** 30 | */ 31 | 32 | /* Define to prevent recursive inclusion -------------------------------------*/ 33 | #ifndef __STM32F1xx_LL_UTILS_H 34 | #define __STM32F1xx_LL_UTILS_H 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | /* Includes ------------------------------------------------------------------*/ 41 | #include "stm32f1xx.h" 42 | 43 | /** @addtogroup STM32F1xx_LL_Driver 44 | * @{ 45 | */ 46 | 47 | /** @defgroup UTILS_LL UTILS 48 | * @{ 49 | */ 50 | 51 | /* Private types -------------------------------------------------------------*/ 52 | /* Private variables ---------------------------------------------------------*/ 53 | 54 | /* Private constants ---------------------------------------------------------*/ 55 | /** @defgroup UTILS_LL_Private_Constants UTILS Private Constants 56 | * @{ 57 | */ 58 | 59 | /* Max delay can be used in LL_mDelay */ 60 | #define LL_MAX_DELAY 0xFFFFFFFFU 61 | 62 | /** 63 | * @brief Unique device ID register base address 64 | */ 65 | #define UID_BASE_ADDRESS UID_BASE 66 | 67 | /** 68 | * @brief Flash size data register base address 69 | */ 70 | #define FLASHSIZE_BASE_ADDRESS FLASHSIZE_BASE 71 | 72 | /** 73 | * @} 74 | */ 75 | 76 | /* Private macros ------------------------------------------------------------*/ 77 | /** @defgroup UTILS_LL_Private_Macros UTILS Private Macros 78 | * @{ 79 | */ 80 | /** 81 | * @} 82 | */ 83 | /* Exported types ------------------------------------------------------------*/ 84 | /** @defgroup UTILS_LL_ES_INIT UTILS Exported structures 85 | * @{ 86 | */ 87 | /** 88 | * @brief UTILS PLL structure definition 89 | */ 90 | typedef struct 91 | { 92 | uint32_t PLLMul; /*!< Multiplication factor for PLL VCO input clock. 93 | This parameter can be a value of @ref RCC_LL_EC_PLL_MUL 94 | 95 | This feature can be modified afterwards using unitary function 96 | @ref LL_RCC_PLL_ConfigDomain_SYS(). */ 97 | 98 | uint32_t Prediv; /*!< Division factor for HSE used as PLL clock source. 99 | This parameter can be a value of @ref RCC_LL_EC_PREDIV_DIV 100 | 101 | This feature can be modified afterwards using unitary function 102 | @ref LL_RCC_PLL_ConfigDomain_SYS(). */ 103 | } LL_UTILS_PLLInitTypeDef; 104 | 105 | /** 106 | * @brief UTILS System, AHB and APB buses clock configuration structure definition 107 | */ 108 | typedef struct 109 | { 110 | uint32_t AHBCLKDivider; /*!< The AHB clock (HCLK) divider. This clock is derived from the system clock (SYSCLK). 111 | This parameter can be a value of @ref RCC_LL_EC_SYSCLK_DIV 112 | 113 | This feature can be modified afterwards using unitary function 114 | @ref LL_RCC_SetAHBPrescaler(). */ 115 | 116 | uint32_t APB1CLKDivider; /*!< The APB1 clock (PCLK1) divider. This clock is derived from the AHB clock (HCLK). 117 | This parameter can be a value of @ref RCC_LL_EC_APB1_DIV 118 | 119 | This feature can be modified afterwards using unitary function 120 | @ref LL_RCC_SetAPB1Prescaler(). */ 121 | 122 | uint32_t APB2CLKDivider; /*!< The APB2 clock (PCLK2) divider. This clock is derived from the AHB clock (HCLK). 123 | This parameter can be a value of @ref RCC_LL_EC_APB2_DIV 124 | 125 | This feature can be modified afterwards using unitary function 126 | @ref LL_RCC_SetAPB2Prescaler(). */ 127 | 128 | } LL_UTILS_ClkInitTypeDef; 129 | 130 | /** 131 | * @} 132 | */ 133 | 134 | /* Exported constants --------------------------------------------------------*/ 135 | /** @defgroup UTILS_LL_Exported_Constants UTILS Exported Constants 136 | * @{ 137 | */ 138 | 139 | /** @defgroup UTILS_EC_HSE_BYPASS HSE Bypass activation 140 | * @{ 141 | */ 142 | #define LL_UTILS_HSEBYPASS_OFF 0x00000000U /*!< HSE Bypass is not enabled */ 143 | #define LL_UTILS_HSEBYPASS_ON 0x00000001U /*!< HSE Bypass is enabled */ 144 | /** 145 | * @} 146 | */ 147 | 148 | /** 149 | * @} 150 | */ 151 | 152 | /* Exported macro ------------------------------------------------------------*/ 153 | 154 | /* Exported functions --------------------------------------------------------*/ 155 | /** @defgroup UTILS_LL_Exported_Functions UTILS Exported Functions 156 | * @{ 157 | */ 158 | 159 | /** @defgroup UTILS_EF_DEVICE_ELECTRONIC_SIGNATURE DEVICE ELECTRONIC SIGNATURE 160 | * @{ 161 | */ 162 | 163 | /** 164 | * @brief Get Word0 of the unique device identifier (UID based on 96 bits) 165 | * @retval UID[31:0] 166 | */ 167 | __STATIC_INLINE uint32_t LL_GetUID_Word0(void) 168 | { 169 | return (uint32_t)(READ_REG(*((uint32_t *)UID_BASE_ADDRESS))); 170 | } 171 | 172 | /** 173 | * @brief Get Word1 of the unique device identifier (UID based on 96 bits) 174 | * @retval UID[63:32] 175 | */ 176 | __STATIC_INLINE uint32_t LL_GetUID_Word1(void) 177 | { 178 | return (uint32_t)(READ_REG(*((uint32_t *)(UID_BASE_ADDRESS + 4U)))); 179 | } 180 | 181 | /** 182 | * @brief Get Word2 of the unique device identifier (UID based on 96 bits) 183 | * @retval UID[95:64] 184 | */ 185 | __STATIC_INLINE uint32_t LL_GetUID_Word2(void) 186 | { 187 | return (uint32_t)(READ_REG(*((uint32_t *)(UID_BASE_ADDRESS + 8U)))); 188 | } 189 | 190 | /** 191 | * @brief Get Flash memory size 192 | * @note This bitfield indicates the size of the device Flash memory expressed in 193 | * Kbytes. As an example, 0x040 corresponds to 64 Kbytes. 194 | * @retval FLASH_SIZE[15:0]: Flash memory size 195 | */ 196 | __STATIC_INLINE uint32_t LL_GetFlashSize(void) 197 | { 198 | return (uint16_t)(READ_REG(*((uint32_t *)FLASHSIZE_BASE_ADDRESS))); 199 | } 200 | 201 | 202 | /** 203 | * @} 204 | */ 205 | 206 | /** @defgroup UTILS_LL_EF_DELAY DELAY 207 | * @{ 208 | */ 209 | 210 | /** 211 | * @brief This function configures the Cortex-M SysTick source of the time base. 212 | * @param HCLKFrequency HCLK frequency in Hz (can be calculated thanks to RCC helper macro) 213 | * @note When a RTOS is used, it is recommended to avoid changing the SysTick 214 | * configuration by calling this function, for a delay use rather osDelay RTOS service. 215 | * @param Ticks Number of ticks 216 | * @retval None 217 | */ 218 | __STATIC_INLINE void LL_InitTick(uint32_t HCLKFrequency, uint32_t Ticks) 219 | { 220 | /* Configure the SysTick to have interrupt in 1ms time base */ 221 | SysTick->LOAD = (uint32_t)((HCLKFrequency / Ticks) - 1UL); /* set reload register */ 222 | SysTick->VAL = 0UL; /* Load the SysTick Counter Value */ 223 | SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | 224 | SysTick_CTRL_ENABLE_Msk; /* Enable the Systick Timer */ 225 | } 226 | 227 | void LL_Init1msTick(uint32_t HCLKFrequency); 228 | void LL_mDelay(uint32_t Delay); 229 | 230 | /** 231 | * @} 232 | */ 233 | 234 | /** @defgroup UTILS_EF_SYSTEM SYSTEM 235 | * @{ 236 | */ 237 | 238 | void LL_SetSystemCoreClock(uint32_t HCLKFrequency); 239 | #if defined(FLASH_ACR_LATENCY) 240 | ErrorStatus LL_SetFlashLatency(uint32_t Frequency); 241 | #endif /* FLASH_ACR_LATENCY */ 242 | ErrorStatus LL_PLL_ConfigSystemClock_HSI(LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, 243 | LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct); 244 | ErrorStatus LL_PLL_ConfigSystemClock_HSE(uint32_t HSEFrequency, uint32_t HSEBypass, 245 | LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct); 246 | #if defined(RCC_PLL2_SUPPORT) 247 | ErrorStatus LL_PLL_ConfigSystemClock_PLL2(uint32_t HSEFrequency, uint32_t HSEBypass, LL_UTILS_PLLInitTypeDef *UTILS_PLLInitStruct, 248 | LL_UTILS_PLLInitTypeDef *UTILS_PLL2InitStruct, LL_UTILS_ClkInitTypeDef *UTILS_ClkInitStruct); 249 | #endif /* RCC_PLL2_SUPPORT */ 250 | /** 251 | * @} 252 | */ 253 | 254 | /** 255 | * @} 256 | */ 257 | 258 | /** 259 | * @} 260 | */ 261 | 262 | /** 263 | * @} 264 | */ 265 | 266 | #ifdef __cplusplus 267 | } 268 | #endif 269 | 270 | #endif /* __STM32F1xx_LL_UTILS_H */ 271 | 272 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 273 | -------------------------------------------------------------------------------- /Drivers/STM32F1xx_HAL_Driver/License.md: -------------------------------------------------------------------------------- 1 | Copyright 2016(-2021) STMicroelectronics. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation and/or 12 | other materials provided with the distribution. 13 | 14 | 3. Neither the name of the copyright holder nor the names of its contributors 15 | may be used to endorse or promote products derived from this software without 16 | specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f1xx_hal_gpio_ex.c 4 | * @author MCD Application Team 5 | * @brief GPIO Extension HAL module driver. 6 | * This file provides firmware functions to manage the following 7 | * functionalities of the General Purpose Input/Output (GPIO) extension peripheral. 8 | * + Extended features functions 9 | * 10 | @verbatim 11 | ============================================================================== 12 | ##### GPIO Peripheral extension features ##### 13 | ============================================================================== 14 | [..] GPIO module on STM32F1 family, manage also the AFIO register: 15 | (+) Possibility to use the EVENTOUT Cortex feature 16 | 17 | ##### How to use this driver ##### 18 | ============================================================================== 19 | [..] This driver provides functions to use EVENTOUT Cortex feature 20 | (#) Configure EVENTOUT Cortex feature using the function HAL_GPIOEx_ConfigEventout() 21 | (#) Activate EVENTOUT Cortex feature using the HAL_GPIOEx_EnableEventout() 22 | (#) Deactivate EVENTOUT Cortex feature using the HAL_GPIOEx_DisableEventout() 23 | 24 | @endverbatim 25 | ****************************************************************************** 26 | * @attention 27 | * 28 | *

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

30 | * 31 | * This software component is licensed by ST under BSD 3-Clause license, 32 | * the "License"; You may not use this file except in compliance with the 33 | * License. You may obtain a copy of the License at: 34 | * opensource.org/licenses/BSD-3-Clause 35 | * 36 | ****************************************************************************** 37 | */ 38 | 39 | /* Includes ------------------------------------------------------------------*/ 40 | #include "stm32f1xx_hal.h" 41 | 42 | /** @addtogroup STM32F1xx_HAL_Driver 43 | * @{ 44 | */ 45 | 46 | /** @defgroup GPIOEx GPIOEx 47 | * @brief GPIO HAL module driver 48 | * @{ 49 | */ 50 | 51 | #ifdef HAL_GPIO_MODULE_ENABLED 52 | 53 | /** @defgroup GPIOEx_Exported_Functions GPIOEx Exported Functions 54 | * @{ 55 | */ 56 | 57 | /** @defgroup GPIOEx_Exported_Functions_Group1 Extended features functions 58 | * @brief Extended features functions 59 | * 60 | @verbatim 61 | ============================================================================== 62 | ##### Extended features functions ##### 63 | ============================================================================== 64 | [..] This section provides functions allowing to: 65 | (+) Configure EVENTOUT Cortex feature using the function HAL_GPIOEx_ConfigEventout() 66 | (+) Activate EVENTOUT Cortex feature using the HAL_GPIOEx_EnableEventout() 67 | (+) Deactivate EVENTOUT Cortex feature using the HAL_GPIOEx_DisableEventout() 68 | 69 | @endverbatim 70 | * @{ 71 | */ 72 | 73 | /** 74 | * @brief Configures the port and pin on which the EVENTOUT Cortex signal will be connected. 75 | * @param GPIO_PortSource Select the port used to output the Cortex EVENTOUT signal. 76 | * This parameter can be a value of @ref GPIOEx_EVENTOUT_PORT. 77 | * @param GPIO_PinSource Select the pin used to output the Cortex EVENTOUT signal. 78 | * This parameter can be a value of @ref GPIOEx_EVENTOUT_PIN. 79 | * @retval None 80 | */ 81 | void HAL_GPIOEx_ConfigEventout(uint32_t GPIO_PortSource, uint32_t GPIO_PinSource) 82 | { 83 | /* Verify the parameters */ 84 | assert_param(IS_AFIO_EVENTOUT_PORT(GPIO_PortSource)); 85 | assert_param(IS_AFIO_EVENTOUT_PIN(GPIO_PinSource)); 86 | 87 | /* Apply the new configuration */ 88 | MODIFY_REG(AFIO->EVCR, (AFIO_EVCR_PORT) | (AFIO_EVCR_PIN), (GPIO_PortSource) | (GPIO_PinSource)); 89 | } 90 | 91 | /** 92 | * @brief Enables the Event Output. 93 | * @retval None 94 | */ 95 | void HAL_GPIOEx_EnableEventout(void) 96 | { 97 | SET_BIT(AFIO->EVCR, AFIO_EVCR_EVOE); 98 | } 99 | 100 | /** 101 | * @brief Disables the Event Output. 102 | * @retval None 103 | */ 104 | void HAL_GPIOEx_DisableEventout(void) 105 | { 106 | CLEAR_BIT(AFIO->EVCR, AFIO_EVCR_EVOE); 107 | } 108 | 109 | /** 110 | * @} 111 | */ 112 | 113 | /** 114 | * @} 115 | */ 116 | 117 | #endif /* HAL_GPIO_MODULE_ENABLED */ 118 | 119 | /** 120 | * @} 121 | */ 122 | 123 | /** 124 | * @} 125 | */ 126 | 127 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 128 | -------------------------------------------------------------------------------- /Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pcd_ex.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f1xx_hal_pcd_ex.c 4 | * @author MCD Application Team 5 | * @brief PCD Extended HAL module driver. 6 | * This file provides firmware functions to manage the following 7 | * functionalities of the USB Peripheral Controller: 8 | * + Extended features functions 9 | * 10 | ****************************************************************************** 11 | * @attention 12 | * 13 | *

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

15 | * 16 | * This software component is licensed by ST under BSD 3-Clause license, 17 | * the "License"; You may not use this file except in compliance with the 18 | * License. You may obtain a copy of the License at: 19 | * opensource.org/licenses/BSD-3-Clause 20 | * 21 | ****************************************************************************** 22 | */ 23 | 24 | /* Includes ------------------------------------------------------------------*/ 25 | #include "stm32f1xx_hal.h" 26 | 27 | /** @addtogroup STM32F1xx_HAL_Driver 28 | * @{ 29 | */ 30 | 31 | /** @defgroup PCDEx PCDEx 32 | * @brief PCD Extended HAL module driver 33 | * @{ 34 | */ 35 | 36 | #ifdef HAL_PCD_MODULE_ENABLED 37 | 38 | #if defined (USB) || defined (USB_OTG_FS) 39 | /* Private types -------------------------------------------------------------*/ 40 | /* Private variables ---------------------------------------------------------*/ 41 | /* Private constants ---------------------------------------------------------*/ 42 | /* Private macros ------------------------------------------------------------*/ 43 | /* Private functions ---------------------------------------------------------*/ 44 | /* Exported functions --------------------------------------------------------*/ 45 | 46 | /** @defgroup PCDEx_Exported_Functions PCDEx Exported Functions 47 | * @{ 48 | */ 49 | 50 | /** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions 51 | * @brief PCDEx control functions 52 | * 53 | @verbatim 54 | =============================================================================== 55 | ##### Extended features functions ##### 56 | =============================================================================== 57 | [..] This section provides functions allowing to: 58 | (+) Update FIFO configuration 59 | 60 | @endverbatim 61 | * @{ 62 | */ 63 | #if defined (USB_OTG_FS) 64 | /** 65 | * @brief Set Tx FIFO 66 | * @param hpcd PCD handle 67 | * @param fifo The number of Tx fifo 68 | * @param size Fifo size 69 | * @retval HAL status 70 | */ 71 | HAL_StatusTypeDef HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo, uint16_t size) 72 | { 73 | uint8_t i; 74 | uint32_t Tx_Offset; 75 | 76 | /* TXn min size = 16 words. (n : Transmit FIFO index) 77 | When a TxFIFO is not used, the Configuration should be as follows: 78 | case 1 : n > m and Txn is not used (n,m : Transmit FIFO indexes) 79 | --> Txm can use the space allocated for Txn. 80 | case2 : n < m and Txn is not used (n,m : Transmit FIFO indexes) 81 | --> Txn should be configured with the minimum space of 16 words 82 | The FIFO is used optimally when used TxFIFOs are allocated in the top 83 | of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones. 84 | When DMA is used 3n * FIFO locations should be reserved for internal DMA registers */ 85 | 86 | Tx_Offset = hpcd->Instance->GRXFSIZ; 87 | 88 | if (fifo == 0U) 89 | { 90 | hpcd->Instance->DIEPTXF0_HNPTXFSIZ = ((uint32_t)size << 16) | Tx_Offset; 91 | } 92 | else 93 | { 94 | Tx_Offset += (hpcd->Instance->DIEPTXF0_HNPTXFSIZ) >> 16; 95 | for (i = 0U; i < (fifo - 1U); i++) 96 | { 97 | Tx_Offset += (hpcd->Instance->DIEPTXF[i] >> 16); 98 | } 99 | 100 | /* Multiply Tx_Size by 2 to get higher performance */ 101 | hpcd->Instance->DIEPTXF[fifo - 1U] = ((uint32_t)size << 16) | Tx_Offset; 102 | } 103 | 104 | return HAL_OK; 105 | } 106 | 107 | /** 108 | * @brief Set Rx FIFO 109 | * @param hpcd PCD handle 110 | * @param size Size of Rx fifo 111 | * @retval HAL status 112 | */ 113 | HAL_StatusTypeDef HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef *hpcd, uint16_t size) 114 | { 115 | hpcd->Instance->GRXFSIZ = size; 116 | 117 | return HAL_OK; 118 | } 119 | #endif /* defined (USB_OTG_FS) */ 120 | #if defined (USB) 121 | /** 122 | * @brief Configure PMA for EP 123 | * @param hpcd Device instance 124 | * @param ep_addr endpoint address 125 | * @param ep_kind endpoint Kind 126 | * USB_SNG_BUF: Single Buffer used 127 | * USB_DBL_BUF: Double Buffer used 128 | * @param pmaadress: EP address in The PMA: In case of single buffer endpoint 129 | * this parameter is 16-bit value providing the address 130 | * in PMA allocated to endpoint. 131 | * In case of double buffer endpoint this parameter 132 | * is a 32-bit value providing the endpoint buffer 0 address 133 | * in the LSB part of 32-bit value and endpoint buffer 1 address 134 | * in the MSB part of 32-bit value. 135 | * @retval HAL status 136 | */ 137 | 138 | HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd, uint16_t ep_addr, 139 | uint16_t ep_kind, uint32_t pmaadress) 140 | { 141 | PCD_EPTypeDef *ep; 142 | 143 | /* initialize ep structure*/ 144 | if ((0x80U & ep_addr) == 0x80U) 145 | { 146 | ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK]; 147 | } 148 | else 149 | { 150 | ep = &hpcd->OUT_ep[ep_addr]; 151 | } 152 | 153 | /* Here we check if the endpoint is single or double Buffer*/ 154 | if (ep_kind == PCD_SNG_BUF) 155 | { 156 | /* Single Buffer */ 157 | ep->doublebuffer = 0U; 158 | /* Configure the PMA */ 159 | ep->pmaadress = (uint16_t)pmaadress; 160 | } 161 | else /* USB_DBL_BUF */ 162 | { 163 | /* Double Buffer Endpoint */ 164 | ep->doublebuffer = 1U; 165 | /* Configure the PMA */ 166 | ep->pmaaddr0 = (uint16_t)(pmaadress & 0xFFFFU); 167 | ep->pmaaddr1 = (uint16_t)((pmaadress & 0xFFFF0000U) >> 16); 168 | } 169 | 170 | return HAL_OK; 171 | } 172 | 173 | /** 174 | * @brief Software Device Connection, 175 | * this function is not required by USB OTG FS peripheral, it is used 176 | * only by USB Device FS peripheral. 177 | * @param hpcd PCD handle 178 | * @param state connection state (0 : disconnected / 1: connected) 179 | * @retval None 180 | */ 181 | __weak void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) 182 | { 183 | /* Prevent unused argument(s) compilation warning */ 184 | UNUSED(hpcd); 185 | UNUSED(state); 186 | /* NOTE : This function Should not be modified, when the callback is needed, 187 | the HAL_PCDEx_SetConnectionState could be implemented in the user file 188 | */ 189 | } 190 | #endif /* defined (USB) */ 191 | 192 | /** 193 | * @brief Send LPM message to user layer callback. 194 | * @param hpcd PCD handle 195 | * @param msg LPM message 196 | * @retval HAL status 197 | */ 198 | __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg) 199 | { 200 | /* Prevent unused argument(s) compilation warning */ 201 | UNUSED(hpcd); 202 | UNUSED(msg); 203 | 204 | /* NOTE : This function should not be modified, when the callback is needed, 205 | the HAL_PCDEx_LPM_Callback could be implemented in the user file 206 | */ 207 | } 208 | 209 | /** 210 | * @brief Send BatteryCharging message to user layer callback. 211 | * @param hpcd PCD handle 212 | * @param msg LPM message 213 | * @retval HAL status 214 | */ 215 | __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg) 216 | { 217 | /* Prevent unused argument(s) compilation warning */ 218 | UNUSED(hpcd); 219 | UNUSED(msg); 220 | 221 | /* NOTE : This function should not be modified, when the callback is needed, 222 | the HAL_PCDEx_BCD_Callback could be implemented in the user file 223 | */ 224 | } 225 | 226 | /** 227 | * @} 228 | */ 229 | 230 | /** 231 | * @} 232 | */ 233 | #endif /* defined (USB) || defined (USB_OTG_FS) */ 234 | #endif /* HAL_PCD_MODULE_ENABLED */ 235 | 236 | /** 237 | * @} 238 | */ 239 | 240 | /** 241 | * @} 242 | */ 243 | 244 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 245 | -------------------------------------------------------------------------------- /Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc/usbd_cdc.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_cdc.h 4 | * @author MCD Application Team 5 | * @brief header file for the usbd_cdc.c file. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

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

11 | * 12 | * This software component is licensed by ST under Ultimate Liberty license 13 | * SLA0044, the "License"; You may not use this file except in compliance with 14 | * the License. You may obtain a copy of the License at: 15 | * www.st.com/SLA0044 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __USB_CDC_H 22 | #define __USB_CDC_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "usbd_ioreq.h" 30 | 31 | /** @addtogroup STM32_USB_DEVICE_LIBRARY 32 | * @{ 33 | */ 34 | 35 | /** @defgroup usbd_cdc 36 | * @brief This file is the Header file for usbd_cdc.c 37 | * @{ 38 | */ 39 | 40 | 41 | /** @defgroup usbd_cdc_Exported_Defines 42 | * @{ 43 | */ 44 | #define CDC_IN_EP 0x81U /* EP1 for data IN */ 45 | #define CDC_OUT_EP 0x01U /* EP1 for data OUT */ 46 | #define CDC_CMD_EP 0x82U /* EP2 for CDC commands */ 47 | 48 | #ifndef CDC_HS_BINTERVAL 49 | #define CDC_HS_BINTERVAL 0x10U 50 | #endif /* CDC_HS_BINTERVAL */ 51 | 52 | #ifndef CDC_FS_BINTERVAL 53 | #define CDC_FS_BINTERVAL 0x10U 54 | #endif /* CDC_FS_BINTERVAL */ 55 | 56 | /* CDC Endpoints parameters: you can fine tune these values depending on the needed baudrates and performance. */ 57 | #define CDC_DATA_HS_MAX_PACKET_SIZE 512U /* Endpoint IN & OUT Packet size */ 58 | #define CDC_DATA_FS_MAX_PACKET_SIZE 64U /* Endpoint IN & OUT Packet size */ 59 | #define CDC_CMD_PACKET_SIZE 8U /* Control Endpoint Packet size */ 60 | 61 | #define USB_CDC_CONFIG_DESC_SIZ 67U 62 | #define CDC_DATA_HS_IN_PACKET_SIZE CDC_DATA_HS_MAX_PACKET_SIZE 63 | #define CDC_DATA_HS_OUT_PACKET_SIZE CDC_DATA_HS_MAX_PACKET_SIZE 64 | 65 | #define CDC_DATA_FS_IN_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE 66 | #define CDC_DATA_FS_OUT_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE 67 | 68 | /*---------------------------------------------------------------------*/ 69 | /* CDC definitions */ 70 | /*---------------------------------------------------------------------*/ 71 | #define CDC_SEND_ENCAPSULATED_COMMAND 0x00U 72 | #define CDC_GET_ENCAPSULATED_RESPONSE 0x01U 73 | #define CDC_SET_COMM_FEATURE 0x02U 74 | #define CDC_GET_COMM_FEATURE 0x03U 75 | #define CDC_CLEAR_COMM_FEATURE 0x04U 76 | #define CDC_SET_LINE_CODING 0x20U 77 | #define CDC_GET_LINE_CODING 0x21U 78 | #define CDC_SET_CONTROL_LINE_STATE 0x22U 79 | #define CDC_SEND_BREAK 0x23U 80 | 81 | /** 82 | * @} 83 | */ 84 | 85 | 86 | /** @defgroup USBD_CORE_Exported_TypesDefinitions 87 | * @{ 88 | */ 89 | 90 | /** 91 | * @} 92 | */ 93 | typedef struct 94 | { 95 | uint32_t bitrate; 96 | uint8_t format; 97 | uint8_t paritytype; 98 | uint8_t datatype; 99 | } USBD_CDC_LineCodingTypeDef; 100 | 101 | typedef struct _USBD_CDC_Itf 102 | { 103 | int8_t (* Init)(void); 104 | int8_t (* DeInit)(void); 105 | int8_t (* Control)(uint8_t cmd, uint8_t *pbuf, uint16_t length); 106 | int8_t (* Receive)(uint8_t *Buf, uint32_t *Len); 107 | 108 | } USBD_CDC_ItfTypeDef; 109 | 110 | 111 | typedef struct 112 | { 113 | uint32_t data[CDC_DATA_HS_MAX_PACKET_SIZE / 4U]; /* Force 32bits alignment */ 114 | uint8_t CmdOpCode; 115 | uint8_t CmdLength; 116 | uint8_t *RxBuffer; 117 | uint8_t *TxBuffer; 118 | uint32_t RxLength; 119 | uint32_t TxLength; 120 | 121 | __IO uint32_t TxState; 122 | __IO uint32_t RxState; 123 | } 124 | USBD_CDC_HandleTypeDef; 125 | 126 | 127 | 128 | /** @defgroup USBD_CORE_Exported_Macros 129 | * @{ 130 | */ 131 | 132 | /** 133 | * @} 134 | */ 135 | 136 | /** @defgroup USBD_CORE_Exported_Variables 137 | * @{ 138 | */ 139 | 140 | extern USBD_ClassTypeDef USBD_CDC; 141 | #define USBD_CDC_CLASS &USBD_CDC 142 | /** 143 | * @} 144 | */ 145 | 146 | /** @defgroup USB_CORE_Exported_Functions 147 | * @{ 148 | */ 149 | uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev, 150 | USBD_CDC_ItfTypeDef *fops); 151 | 152 | uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, 153 | uint8_t *pbuff, 154 | uint16_t length); 155 | 156 | uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, 157 | uint8_t *pbuff); 158 | 159 | uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev); 160 | 161 | uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev); 162 | /** 163 | * @} 164 | */ 165 | 166 | #ifdef __cplusplus 167 | } 168 | #endif 169 | 170 | #endif /* __USB_CDC_H */ 171 | /** 172 | * @} 173 | */ 174 | 175 | /** 176 | * @} 177 | */ 178 | 179 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 180 | -------------------------------------------------------------------------------- /Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_core.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_core.h 4 | * @author MCD Application Team 5 | * @brief Header file for usbd_core.c file 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

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

11 | * 12 | * This software component is licensed by ST under Ultimate Liberty license 13 | * SLA0044, the "License"; You may not use this file except in compliance with 14 | * the License. You may obtain a copy of the License at: 15 | * www.st.com/SLA0044 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __USBD_CORE_H 22 | #define __USBD_CORE_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "usbd_conf.h" 30 | #include "usbd_def.h" 31 | #include "usbd_ioreq.h" 32 | #include "usbd_ctlreq.h" 33 | 34 | /** @addtogroup STM32_USB_DEVICE_LIBRARY 35 | * @{ 36 | */ 37 | 38 | /** @defgroup USBD_CORE 39 | * @brief This file is the Header file for usbd_core.c file 40 | * @{ 41 | */ 42 | 43 | 44 | /** @defgroup USBD_CORE_Exported_Defines 45 | * @{ 46 | */ 47 | #ifndef USBD_DEBUG_LEVEL 48 | #define USBD_DEBUG_LEVEL 0U 49 | #endif /* USBD_DEBUG_LEVEL */ 50 | /** 51 | * @} 52 | */ 53 | 54 | 55 | /** @defgroup USBD_CORE_Exported_TypesDefinitions 56 | * @{ 57 | */ 58 | 59 | 60 | /** 61 | * @} 62 | */ 63 | 64 | 65 | 66 | /** @defgroup USBD_CORE_Exported_Macros 67 | * @{ 68 | */ 69 | 70 | /** 71 | * @} 72 | */ 73 | 74 | /** @defgroup USBD_CORE_Exported_Variables 75 | * @{ 76 | */ 77 | #define USBD_SOF USBD_LL_SOF 78 | /** 79 | * @} 80 | */ 81 | 82 | /** @defgroup USBD_CORE_Exported_FunctionsPrototype 83 | * @{ 84 | */ 85 | USBD_StatusTypeDef USBD_Init(USBD_HandleTypeDef *pdev, USBD_DescriptorsTypeDef *pdesc, uint8_t id); 86 | USBD_StatusTypeDef USBD_DeInit(USBD_HandleTypeDef *pdev); 87 | USBD_StatusTypeDef USBD_Start(USBD_HandleTypeDef *pdev); 88 | USBD_StatusTypeDef USBD_Stop(USBD_HandleTypeDef *pdev); 89 | USBD_StatusTypeDef USBD_RegisterClass(USBD_HandleTypeDef *pdev, USBD_ClassTypeDef *pclass); 90 | 91 | USBD_StatusTypeDef USBD_RunTestMode(USBD_HandleTypeDef *pdev); 92 | USBD_StatusTypeDef USBD_SetClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx); 93 | USBD_StatusTypeDef USBD_ClrClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx); 94 | 95 | USBD_StatusTypeDef USBD_LL_SetupStage(USBD_HandleTypeDef *pdev, uint8_t *psetup); 96 | USBD_StatusTypeDef USBD_LL_DataOutStage(USBD_HandleTypeDef *pdev, uint8_t epnum, uint8_t *pdata); 97 | USBD_StatusTypeDef USBD_LL_DataInStage(USBD_HandleTypeDef *pdev, uint8_t epnum, uint8_t *pdata); 98 | 99 | USBD_StatusTypeDef USBD_LL_Reset(USBD_HandleTypeDef *pdev); 100 | USBD_StatusTypeDef USBD_LL_SetSpeed(USBD_HandleTypeDef *pdev, USBD_SpeedTypeDef speed); 101 | USBD_StatusTypeDef USBD_LL_Suspend(USBD_HandleTypeDef *pdev); 102 | USBD_StatusTypeDef USBD_LL_Resume(USBD_HandleTypeDef *pdev); 103 | 104 | USBD_StatusTypeDef USBD_LL_SOF(USBD_HandleTypeDef *pdev); 105 | USBD_StatusTypeDef USBD_LL_IsoINIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum); 106 | USBD_StatusTypeDef USBD_LL_IsoOUTIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum); 107 | 108 | USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef *pdev); 109 | USBD_StatusTypeDef USBD_LL_DevDisconnected(USBD_HandleTypeDef *pdev); 110 | 111 | /* USBD Low Level Driver */ 112 | USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev); 113 | USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev); 114 | USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev); 115 | USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev); 116 | USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, 117 | uint8_t ep_addr, 118 | uint8_t ep_type, 119 | uint16_t ep_mps); 120 | 121 | USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr); 122 | USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr); 123 | USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr); 124 | USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr); 125 | uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr); 126 | USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr); 127 | USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, 128 | uint8_t ep_addr, 129 | uint8_t *pbuf, 130 | uint16_t size); 131 | 132 | USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, 133 | uint8_t ep_addr, 134 | uint8_t *pbuf, 135 | uint16_t size); 136 | 137 | uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr); 138 | void USBD_LL_Delay(uint32_t Delay); 139 | 140 | /** 141 | * @} 142 | */ 143 | 144 | #ifdef __cplusplus 145 | } 146 | #endif 147 | 148 | #endif /* __USBD_CORE_H */ 149 | 150 | /** 151 | * @} 152 | */ 153 | 154 | /** 155 | * @} 156 | */ 157 | 158 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 159 | 160 | 161 | 162 | -------------------------------------------------------------------------------- /Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ctlreq.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_req.h 4 | * @author MCD Application Team 5 | * @brief Header file for the usbd_req.c file 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

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

11 | * 12 | * This software component is licensed by ST under Ultimate Liberty license 13 | * SLA0044, the "License"; You may not use this file except in compliance with 14 | * the License. You may obtain a copy of the License at: 15 | * www.st.com/SLA0044 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __USB_REQUEST_H 22 | #define __USB_REQUEST_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "usbd_def.h" 30 | 31 | 32 | /** @addtogroup STM32_USB_DEVICE_LIBRARY 33 | * @{ 34 | */ 35 | 36 | /** @defgroup USBD_REQ 37 | * @brief header file for the usbd_req.c file 38 | * @{ 39 | */ 40 | 41 | /** @defgroup USBD_REQ_Exported_Defines 42 | * @{ 43 | */ 44 | /** 45 | * @} 46 | */ 47 | 48 | 49 | /** @defgroup USBD_REQ_Exported_Types 50 | * @{ 51 | */ 52 | /** 53 | * @} 54 | */ 55 | 56 | 57 | 58 | /** @defgroup USBD_REQ_Exported_Macros 59 | * @{ 60 | */ 61 | /** 62 | * @} 63 | */ 64 | 65 | /** @defgroup USBD_REQ_Exported_Variables 66 | * @{ 67 | */ 68 | /** 69 | * @} 70 | */ 71 | 72 | /** @defgroup USBD_REQ_Exported_FunctionsPrototype 73 | * @{ 74 | */ 75 | 76 | USBD_StatusTypeDef USBD_StdDevReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); 77 | USBD_StatusTypeDef USBD_StdItfReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); 78 | USBD_StatusTypeDef USBD_StdEPReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); 79 | 80 | 81 | void USBD_CtlError(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); 82 | 83 | void USBD_ParseSetupRequest(USBD_SetupReqTypedef *req, uint8_t *pdata); 84 | 85 | void USBD_GetString(uint8_t *desc, uint8_t *unicode, uint16_t *len); 86 | /** 87 | * @} 88 | */ 89 | 90 | #ifdef __cplusplus 91 | } 92 | #endif 93 | 94 | #endif /* __USB_REQUEST_H */ 95 | 96 | /** 97 | * @} 98 | */ 99 | 100 | /** 101 | * @} 102 | */ 103 | 104 | 105 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 106 | -------------------------------------------------------------------------------- /Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ioreq.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_ioreq.h 4 | * @author MCD Application Team 5 | * @brief Header file for the usbd_ioreq.c file 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

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

11 | * 12 | * This software component is licensed by ST under Ultimate Liberty license 13 | * SLA0044, the "License"; You may not use this file except in compliance with 14 | * the License. You may obtain a copy of the License at: 15 | * www.st.com/SLA0044 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __USBD_IOREQ_H 22 | #define __USBD_IOREQ_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "usbd_def.h" 30 | #include "usbd_core.h" 31 | 32 | /** @addtogroup STM32_USB_DEVICE_LIBRARY 33 | * @{ 34 | */ 35 | 36 | /** @defgroup USBD_IOREQ 37 | * @brief header file for the usbd_ioreq.c file 38 | * @{ 39 | */ 40 | 41 | /** @defgroup USBD_IOREQ_Exported_Defines 42 | * @{ 43 | */ 44 | /** 45 | * @} 46 | */ 47 | 48 | 49 | /** @defgroup USBD_IOREQ_Exported_Types 50 | * @{ 51 | */ 52 | 53 | 54 | /** 55 | * @} 56 | */ 57 | 58 | 59 | 60 | /** @defgroup USBD_IOREQ_Exported_Macros 61 | * @{ 62 | */ 63 | 64 | /** 65 | * @} 66 | */ 67 | 68 | /** @defgroup USBD_IOREQ_Exported_Variables 69 | * @{ 70 | */ 71 | 72 | /** 73 | * @} 74 | */ 75 | 76 | /** @defgroup USBD_IOREQ_Exported_FunctionsPrototype 77 | * @{ 78 | */ 79 | 80 | USBD_StatusTypeDef USBD_CtlSendData(USBD_HandleTypeDef *pdev, 81 | uint8_t *pbuf, 82 | uint16_t len); 83 | 84 | USBD_StatusTypeDef USBD_CtlContinueSendData(USBD_HandleTypeDef *pdev, 85 | uint8_t *pbuf, 86 | uint16_t len); 87 | 88 | USBD_StatusTypeDef USBD_CtlPrepareRx(USBD_HandleTypeDef *pdev, 89 | uint8_t *pbuf, 90 | uint16_t len); 91 | 92 | USBD_StatusTypeDef USBD_CtlContinueRx(USBD_HandleTypeDef *pdev, 93 | uint8_t *pbuf, 94 | uint16_t len); 95 | 96 | USBD_StatusTypeDef USBD_CtlSendStatus(USBD_HandleTypeDef *pdev); 97 | 98 | USBD_StatusTypeDef USBD_CtlReceiveStatus(USBD_HandleTypeDef *pdev); 99 | 100 | uint32_t USBD_GetRxCount(USBD_HandleTypeDef *pdev, uint8_t ep_addr); 101 | 102 | /** 103 | * @} 104 | */ 105 | 106 | #ifdef __cplusplus 107 | } 108 | #endif 109 | 110 | #endif /* __USBD_IOREQ_H */ 111 | 112 | /** 113 | * @} 114 | */ 115 | 116 | /** 117 | * @} 118 | */ 119 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 120 | -------------------------------------------------------------------------------- /Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbd_ioreq.c 4 | * @author MCD Application Team 5 | * @brief This file provides the IO requests APIs for control endpoints. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

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

11 | * 12 | * This software component is licensed by ST under Ultimate Liberty license 13 | * SLA0044, the "License"; You may not use this file except in compliance with 14 | * the License. You may obtain a copy of the License at: 15 | * www.st.com/SLA0044 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Includes ------------------------------------------------------------------*/ 21 | #include "usbd_ioreq.h" 22 | 23 | /** @addtogroup STM32_USB_DEVICE_LIBRARY 24 | * @{ 25 | */ 26 | 27 | 28 | /** @defgroup USBD_IOREQ 29 | * @brief control I/O requests module 30 | * @{ 31 | */ 32 | 33 | /** @defgroup USBD_IOREQ_Private_TypesDefinitions 34 | * @{ 35 | */ 36 | /** 37 | * @} 38 | */ 39 | 40 | 41 | /** @defgroup USBD_IOREQ_Private_Defines 42 | * @{ 43 | */ 44 | 45 | /** 46 | * @} 47 | */ 48 | 49 | 50 | /** @defgroup USBD_IOREQ_Private_Macros 51 | * @{ 52 | */ 53 | /** 54 | * @} 55 | */ 56 | 57 | 58 | /** @defgroup USBD_IOREQ_Private_Variables 59 | * @{ 60 | */ 61 | 62 | /** 63 | * @} 64 | */ 65 | 66 | 67 | /** @defgroup USBD_IOREQ_Private_FunctionPrototypes 68 | * @{ 69 | */ 70 | /** 71 | * @} 72 | */ 73 | 74 | 75 | /** @defgroup USBD_IOREQ_Private_Functions 76 | * @{ 77 | */ 78 | 79 | /** 80 | * @brief USBD_CtlSendData 81 | * send data on the ctl pipe 82 | * @param pdev: device instance 83 | * @param buff: pointer to data buffer 84 | * @param len: length of data to be sent 85 | * @retval status 86 | */ 87 | USBD_StatusTypeDef USBD_CtlSendData(USBD_HandleTypeDef *pdev, 88 | uint8_t *pbuf, uint16_t len) 89 | { 90 | /* Set EP0 State */ 91 | pdev->ep0_state = USBD_EP0_DATA_IN; 92 | pdev->ep_in[0].total_length = len; 93 | pdev->ep_in[0].rem_length = len; 94 | 95 | /* Start the transfer */ 96 | USBD_LL_Transmit(pdev, 0x00U, pbuf, len); 97 | 98 | return USBD_OK; 99 | } 100 | 101 | /** 102 | * @brief USBD_CtlContinueSendData 103 | * continue sending data on the ctl pipe 104 | * @param pdev: device instance 105 | * @param buff: pointer to data buffer 106 | * @param len: length of data to be sent 107 | * @retval status 108 | */ 109 | USBD_StatusTypeDef USBD_CtlContinueSendData(USBD_HandleTypeDef *pdev, 110 | uint8_t *pbuf, uint16_t len) 111 | { 112 | /* Start the next transfer */ 113 | USBD_LL_Transmit(pdev, 0x00U, pbuf, len); 114 | 115 | return USBD_OK; 116 | } 117 | 118 | /** 119 | * @brief USBD_CtlPrepareRx 120 | * receive data on the ctl pipe 121 | * @param pdev: device instance 122 | * @param buff: pointer to data buffer 123 | * @param len: length of data to be received 124 | * @retval status 125 | */ 126 | USBD_StatusTypeDef USBD_CtlPrepareRx(USBD_HandleTypeDef *pdev, 127 | uint8_t *pbuf, uint16_t len) 128 | { 129 | /* Set EP0 State */ 130 | pdev->ep0_state = USBD_EP0_DATA_OUT; 131 | pdev->ep_out[0].total_length = len; 132 | pdev->ep_out[0].rem_length = len; 133 | 134 | /* Start the transfer */ 135 | USBD_LL_PrepareReceive(pdev, 0U, pbuf, len); 136 | 137 | return USBD_OK; 138 | } 139 | 140 | /** 141 | * @brief USBD_CtlContinueRx 142 | * continue receive data on the ctl pipe 143 | * @param pdev: device instance 144 | * @param buff: pointer to data buffer 145 | * @param len: length of data to be received 146 | * @retval status 147 | */ 148 | USBD_StatusTypeDef USBD_CtlContinueRx(USBD_HandleTypeDef *pdev, 149 | uint8_t *pbuf, uint16_t len) 150 | { 151 | USBD_LL_PrepareReceive(pdev, 0U, pbuf, len); 152 | 153 | return USBD_OK; 154 | } 155 | 156 | /** 157 | * @brief USBD_CtlSendStatus 158 | * send zero lzngth packet on the ctl pipe 159 | * @param pdev: device instance 160 | * @retval status 161 | */ 162 | USBD_StatusTypeDef USBD_CtlSendStatus(USBD_HandleTypeDef *pdev) 163 | { 164 | /* Set EP0 State */ 165 | pdev->ep0_state = USBD_EP0_STATUS_IN; 166 | 167 | /* Start the transfer */ 168 | USBD_LL_Transmit(pdev, 0x00U, NULL, 0U); 169 | 170 | return USBD_OK; 171 | } 172 | 173 | /** 174 | * @brief USBD_CtlReceiveStatus 175 | * receive zero lzngth packet on the ctl pipe 176 | * @param pdev: device instance 177 | * @retval status 178 | */ 179 | USBD_StatusTypeDef USBD_CtlReceiveStatus(USBD_HandleTypeDef *pdev) 180 | { 181 | /* Set EP0 State */ 182 | pdev->ep0_state = USBD_EP0_STATUS_OUT; 183 | 184 | /* Start the transfer */ 185 | USBD_LL_PrepareReceive(pdev, 0U, NULL, 0U); 186 | 187 | return USBD_OK; 188 | } 189 | 190 | /** 191 | * @brief USBD_GetRxCount 192 | * returns the received data length 193 | * @param pdev: device instance 194 | * @param ep_addr: endpoint address 195 | * @retval Rx Data blength 196 | */ 197 | uint32_t USBD_GetRxCount(USBD_HandleTypeDef *pdev, uint8_t ep_addr) 198 | { 199 | return USBD_LL_GetRxDataSize(pdev, ep_addr); 200 | } 201 | 202 | /** 203 | * @} 204 | */ 205 | 206 | 207 | /** 208 | * @} 209 | */ 210 | 211 | 212 | /** 213 | * @} 214 | */ 215 | 216 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 217 | -------------------------------------------------------------------------------- /STM32F103C8TX_FLASH.ld: -------------------------------------------------------------------------------- 1 | /* 2 | ****************************************************************************** 3 | ** 4 | ** @file : LinkerScript.ld 5 | ** 6 | ** @author : Auto-generated by STM32CubeIDE 7 | ** 8 | ** @brief : Linker script for STM32F103C8Tx Device from STM32F1 series 9 | ** 64Kbytes FLASH 10 | ** 20Kbytes RAM 11 | ** 12 | ** Set heap size, stack size and stack location according 13 | ** to application requirements. 14 | ** 15 | ** Set memory bank area and size if external memory is used 16 | ** 17 | ** Target : STMicroelectronics STM32 18 | ** 19 | ** Distribution: The file is distributed as is, without any warranty 20 | ** of any kind. 21 | ** 22 | ****************************************************************************** 23 | ** @attention 24 | ** 25 | ** Copyright (c) 2022 STMicroelectronics. 26 | ** All rights reserved. 27 | ** 28 | ** This software is licensed under terms that can be found in the LICENSE file 29 | ** in the root directory of this software component. 30 | ** If no LICENSE file comes with this software, it is provided AS-IS. 31 | ** 32 | ****************************************************************************** 33 | */ 34 | 35 | /* Entry Point */ 36 | ENTRY(Reset_Handler) 37 | 38 | /* Highest address of the user mode stack */ 39 | _estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */ 40 | 41 | _Min_Heap_Size = 0x200 ; /* required amount of heap */ 42 | _Min_Stack_Size = 0x400 ; /* required amount of stack */ 43 | 44 | /* Memories definition */ 45 | MEMORY 46 | { 47 | RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K 48 | FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 64K 49 | } 50 | 51 | /* Sections */ 52 | SECTIONS 53 | { 54 | /* The startup code into "FLASH" Rom type memory */ 55 | .isr_vector : 56 | { 57 | . = ALIGN(4); 58 | KEEP(*(.isr_vector)) /* Startup code */ 59 | . = ALIGN(4); 60 | } >FLASH 61 | 62 | /* The program code and other data into "FLASH" Rom type memory */ 63 | .text : 64 | { 65 | . = ALIGN(4); 66 | *(.text) /* .text sections (code) */ 67 | *(.text*) /* .text* sections (code) */ 68 | *(.glue_7) /* glue arm to thumb code */ 69 | *(.glue_7t) /* glue thumb to arm code */ 70 | *(.eh_frame) 71 | 72 | KEEP (*(.init)) 73 | KEEP (*(.fini)) 74 | 75 | . = ALIGN(4); 76 | _etext = .; /* define a global symbols at end of code */ 77 | } >FLASH 78 | 79 | /* Constant data into "FLASH" Rom type memory */ 80 | .rodata : 81 | { 82 | . = ALIGN(4); 83 | *(.rodata) /* .rodata sections (constants, strings, etc.) */ 84 | *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ 85 | . = ALIGN(4); 86 | } >FLASH 87 | 88 | .ARM.extab : { 89 | . = ALIGN(4); 90 | *(.ARM.extab* .gnu.linkonce.armextab.*) 91 | . = ALIGN(4); 92 | } >FLASH 93 | 94 | .ARM : { 95 | . = ALIGN(4); 96 | __exidx_start = .; 97 | *(.ARM.exidx*) 98 | __exidx_end = .; 99 | . = ALIGN(4); 100 | } >FLASH 101 | 102 | .preinit_array : 103 | { 104 | . = ALIGN(4); 105 | PROVIDE_HIDDEN (__preinit_array_start = .); 106 | KEEP (*(.preinit_array*)) 107 | PROVIDE_HIDDEN (__preinit_array_end = .); 108 | . = ALIGN(4); 109 | } >FLASH 110 | 111 | .init_array : 112 | { 113 | . = ALIGN(4); 114 | PROVIDE_HIDDEN (__init_array_start = .); 115 | KEEP (*(SORT(.init_array.*))) 116 | KEEP (*(.init_array*)) 117 | PROVIDE_HIDDEN (__init_array_end = .); 118 | . = ALIGN(4); 119 | } >FLASH 120 | 121 | .fini_array : 122 | { 123 | . = ALIGN(4); 124 | PROVIDE_HIDDEN (__fini_array_start = .); 125 | KEEP (*(SORT(.fini_array.*))) 126 | KEEP (*(.fini_array*)) 127 | PROVIDE_HIDDEN (__fini_array_end = .); 128 | . = ALIGN(4); 129 | } >FLASH 130 | 131 | /* Used by the startup to initialize data */ 132 | _sidata = LOADADDR(.data); 133 | 134 | /* Initialized data sections into "RAM" Ram type memory */ 135 | .data : 136 | { 137 | . = ALIGN(4); 138 | _sdata = .; /* create a global symbol at data start */ 139 | *(.data) /* .data sections */ 140 | *(.data*) /* .data* sections */ 141 | *(.RamFunc) /* .RamFunc sections */ 142 | *(.RamFunc*) /* .RamFunc* sections */ 143 | 144 | . = ALIGN(4); 145 | _edata = .; /* define a global symbol at data end */ 146 | 147 | } >RAM AT> FLASH 148 | 149 | /* Uninitialized data section into "RAM" Ram type memory */ 150 | . = ALIGN(4); 151 | .bss : 152 | { 153 | /* This is used by the startup in order to initialize the .bss section */ 154 | _sbss = .; /* define a global symbol at bss start */ 155 | __bss_start__ = _sbss; 156 | *(.bss) 157 | *(.bss*) 158 | *(COMMON) 159 | 160 | . = ALIGN(4); 161 | _ebss = .; /* define a global symbol at bss end */ 162 | __bss_end__ = _ebss; 163 | } >RAM 164 | 165 | /* User_heap_stack section, used to check that there is enough "RAM" Ram type memory left */ 166 | ._user_heap_stack : 167 | { 168 | . = ALIGN(8); 169 | PROVIDE ( end = . ); 170 | PROVIDE ( _end = . ); 171 | . = . + _Min_Heap_Size; 172 | . = . + _Min_Stack_Size; 173 | . = ALIGN(8); 174 | } >RAM 175 | 176 | /* Remove information from the compiler libraries */ 177 | /DISCARD/ : 178 | { 179 | libc.a ( * ) 180 | libm.a ( * ) 181 | libgcc.a ( * ) 182 | } 183 | 184 | .ARM.attributes 0 : { *(.ARM.attributes) } 185 | } 186 | -------------------------------------------------------------------------------- /STM32F103_BLDC_Driver (1).launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /STM32F103_BLDC_Driver.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /USB_DEVICE/App/usb_device.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : usb_device.c 5 | * @version : v2.0_Cube 6 | * @brief : This file implements the USB Device 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2022 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | 21 | /* Includes ------------------------------------------------------------------*/ 22 | 23 | #include "usb_device.h" 24 | #include "usbd_core.h" 25 | #include "usbd_desc.h" 26 | #include "usbd_cdc.h" 27 | #include "usbd_cdc_if.h" 28 | 29 | /* USER CODE BEGIN Includes */ 30 | 31 | /* USER CODE END Includes */ 32 | 33 | /* USER CODE BEGIN PV */ 34 | /* Private variables ---------------------------------------------------------*/ 35 | 36 | /* USER CODE END PV */ 37 | 38 | /* USER CODE BEGIN PFP */ 39 | /* Private function prototypes -----------------------------------------------*/ 40 | 41 | /* USER CODE END PFP */ 42 | 43 | /* USB Device Core handle declaration. */ 44 | USBD_HandleTypeDef hUsbDeviceFS; 45 | 46 | /* 47 | * -- Insert your variables declaration here -- 48 | */ 49 | /* USER CODE BEGIN 0 */ 50 | 51 | /* USER CODE END 0 */ 52 | 53 | /* 54 | * -- Insert your external function declaration here -- 55 | */ 56 | /* USER CODE BEGIN 1 */ 57 | 58 | /* USER CODE END 1 */ 59 | 60 | /** 61 | * Init USB device Library, add supported class and start the library 62 | * @retval None 63 | */ 64 | void MX_USB_DEVICE_Init(void) 65 | { 66 | /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */ 67 | 68 | /* USER CODE END USB_DEVICE_Init_PreTreatment */ 69 | 70 | /* Init Device Library, add supported class and start the library. */ 71 | if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK) 72 | { 73 | Error_Handler(); 74 | } 75 | if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC) != USBD_OK) 76 | { 77 | Error_Handler(); 78 | } 79 | if (USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS) != USBD_OK) 80 | { 81 | Error_Handler(); 82 | } 83 | if (USBD_Start(&hUsbDeviceFS) != USBD_OK) 84 | { 85 | Error_Handler(); 86 | } 87 | 88 | /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */ 89 | 90 | /* USER CODE END USB_DEVICE_Init_PostTreatment */ 91 | } 92 | 93 | /** 94 | * @} 95 | */ 96 | 97 | /** 98 | * @} 99 | */ 100 | 101 | -------------------------------------------------------------------------------- /USB_DEVICE/App/usb_device.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : usb_device.h 5 | * @version : v2.0_Cube 6 | * @brief : Header for usb_device.c file. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2022 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | 21 | /* Define to prevent recursive inclusion -------------------------------------*/ 22 | #ifndef __USB_DEVICE__H__ 23 | #define __USB_DEVICE__H__ 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | /* Includes ------------------------------------------------------------------*/ 30 | #include "stm32f1xx.h" 31 | #include "stm32f1xx_hal.h" 32 | #include "usbd_def.h" 33 | 34 | /* USER CODE BEGIN INCLUDE */ 35 | 36 | /* USER CODE END INCLUDE */ 37 | 38 | /** @addtogroup USBD_OTG_DRIVER 39 | * @{ 40 | */ 41 | 42 | /** @defgroup USBD_DEVICE USBD_DEVICE 43 | * @brief Device file for Usb otg low level driver. 44 | * @{ 45 | */ 46 | 47 | /** @defgroup USBD_DEVICE_Exported_Variables USBD_DEVICE_Exported_Variables 48 | * @brief Public variables. 49 | * @{ 50 | */ 51 | 52 | /* Private variables ---------------------------------------------------------*/ 53 | /* USER CODE BEGIN PV */ 54 | 55 | /* USER CODE END PV */ 56 | 57 | /* Private function prototypes -----------------------------------------------*/ 58 | /* USER CODE BEGIN PFP */ 59 | 60 | /* USER CODE END PFP */ 61 | 62 | /* 63 | * -- Insert your variables declaration here -- 64 | */ 65 | /* USER CODE BEGIN VARIABLES */ 66 | 67 | /* USER CODE END VARIABLES */ 68 | /** 69 | * @} 70 | */ 71 | 72 | /** @defgroup USBD_DEVICE_Exported_FunctionsPrototype USBD_DEVICE_Exported_FunctionsPrototype 73 | * @brief Declaration of public functions for Usb device. 74 | * @{ 75 | */ 76 | 77 | /** USB Device initialization function. */ 78 | void MX_USB_DEVICE_Init(void); 79 | 80 | /* 81 | * -- Insert functions declaration here -- 82 | */ 83 | /* USER CODE BEGIN FD */ 84 | 85 | /* USER CODE END FD */ 86 | /** 87 | * @} 88 | */ 89 | 90 | /** 91 | * @} 92 | */ 93 | 94 | /** 95 | * @} 96 | */ 97 | 98 | #ifdef __cplusplus 99 | } 100 | #endif 101 | 102 | #endif /* __USB_DEVICE__H__ */ 103 | -------------------------------------------------------------------------------- /USB_DEVICE/App/usbd_cdc_if.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : usbd_cdc_if.c 5 | * @version : v2.0_Cube 6 | * @brief : Usb device for Virtual Com Port. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2022 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | 21 | /* Includes ------------------------------------------------------------------*/ 22 | #include "usbd_cdc_if.h" 23 | 24 | /* USER CODE BEGIN INCLUDE */ 25 | #include "usb_comm.h" 26 | /* USER CODE END INCLUDE */ 27 | 28 | /* Private typedef -----------------------------------------------------------*/ 29 | /* Private define ------------------------------------------------------------*/ 30 | /* Private macro -------------------------------------------------------------*/ 31 | 32 | /* USER CODE BEGIN PV */ 33 | /* Private variables ---------------------------------------------------------*/ 34 | 35 | /* USER CODE END PV */ 36 | 37 | /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY 38 | * @brief Usb device library. 39 | * @{ 40 | */ 41 | 42 | /** @addtogroup USBD_CDC_IF 43 | * @{ 44 | */ 45 | 46 | /** @defgroup USBD_CDC_IF_Private_TypesDefinitions USBD_CDC_IF_Private_TypesDefinitions 47 | * @brief Private types. 48 | * @{ 49 | */ 50 | 51 | /* USER CODE BEGIN PRIVATE_TYPES */ 52 | 53 | /* USER CODE END PRIVATE_TYPES */ 54 | 55 | /** 56 | * @} 57 | */ 58 | 59 | /** @defgroup USBD_CDC_IF_Private_Defines USBD_CDC_IF_Private_Defines 60 | * @brief Private defines. 61 | * @{ 62 | */ 63 | 64 | /* USER CODE BEGIN PRIVATE_DEFINES */ 65 | /* USER CODE END PRIVATE_DEFINES */ 66 | 67 | /** 68 | * @} 69 | */ 70 | 71 | /** @defgroup USBD_CDC_IF_Private_Macros USBD_CDC_IF_Private_Macros 72 | * @brief Private macros. 73 | * @{ 74 | */ 75 | 76 | /* USER CODE BEGIN PRIVATE_MACRO */ 77 | 78 | /* USER CODE END PRIVATE_MACRO */ 79 | 80 | /** 81 | * @} 82 | */ 83 | 84 | /** @defgroup USBD_CDC_IF_Private_Variables USBD_CDC_IF_Private_Variables 85 | * @brief Private variables. 86 | * @{ 87 | */ 88 | /* Create buffer for reception and transmission */ 89 | /* It's up to user to redefine and/or remove those define */ 90 | /** Received data over USB are stored in this buffer */ 91 | uint8_t UserRxBufferFS[APP_RX_DATA_SIZE]; 92 | 93 | /** Data to send over USB CDC are stored in this buffer */ 94 | uint8_t UserTxBufferFS[APP_TX_DATA_SIZE]; 95 | 96 | /* USER CODE BEGIN PRIVATE_VARIABLES */ 97 | 98 | /* USER CODE END PRIVATE_VARIABLES */ 99 | 100 | /** 101 | * @} 102 | */ 103 | 104 | /** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables 105 | * @brief Public variables. 106 | * @{ 107 | */ 108 | 109 | extern USBD_HandleTypeDef hUsbDeviceFS; 110 | 111 | /* USER CODE BEGIN EXPORTED_VARIABLES */ 112 | 113 | /* USER CODE END EXPORTED_VARIABLES */ 114 | 115 | /** 116 | * @} 117 | */ 118 | 119 | /** @defgroup USBD_CDC_IF_Private_FunctionPrototypes USBD_CDC_IF_Private_FunctionPrototypes 120 | * @brief Private functions declaration. 121 | * @{ 122 | */ 123 | 124 | static int8_t CDC_Init_FS(void); 125 | static int8_t CDC_DeInit_FS(void); 126 | static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length); 127 | static int8_t CDC_Receive_FS(uint8_t* pbuf, uint32_t *Len); 128 | 129 | /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */ 130 | 131 | /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */ 132 | 133 | /** 134 | * @} 135 | */ 136 | 137 | USBD_CDC_ItfTypeDef USBD_Interface_fops_FS = 138 | { 139 | CDC_Init_FS, 140 | CDC_DeInit_FS, 141 | CDC_Control_FS, 142 | CDC_Receive_FS 143 | }; 144 | 145 | /* Private functions ---------------------------------------------------------*/ 146 | /** 147 | * @brief Initializes the CDC media low layer over the FS USB IP 148 | * @retval USBD_OK if all operations are OK else USBD_FAIL 149 | */ 150 | static int8_t CDC_Init_FS(void) 151 | { 152 | /* USER CODE BEGIN 3 */ 153 | /* Set Application Buffers */ 154 | USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0); 155 | USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS); 156 | return (USBD_OK); 157 | /* USER CODE END 3 */ 158 | } 159 | 160 | /** 161 | * @brief DeInitializes the CDC media low layer 162 | * @retval USBD_OK if all operations are OK else USBD_FAIL 163 | */ 164 | static int8_t CDC_DeInit_FS(void) 165 | { 166 | /* USER CODE BEGIN 4 */ 167 | return (USBD_OK); 168 | /* USER CODE END 4 */ 169 | } 170 | 171 | /** 172 | * @brief Manage the CDC class requests 173 | * @param cmd: Command code 174 | * @param pbuf: Buffer containing command data (request parameters) 175 | * @param length: Number of data to be sent (in bytes) 176 | * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL 177 | */ 178 | static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length) 179 | { 180 | /* USER CODE BEGIN 5 */ 181 | switch(cmd) 182 | { 183 | case CDC_SEND_ENCAPSULATED_COMMAND: 184 | 185 | break; 186 | 187 | case CDC_GET_ENCAPSULATED_RESPONSE: 188 | 189 | break; 190 | 191 | case CDC_SET_COMM_FEATURE: 192 | 193 | break; 194 | 195 | case CDC_GET_COMM_FEATURE: 196 | 197 | break; 198 | 199 | case CDC_CLEAR_COMM_FEATURE: 200 | 201 | break; 202 | 203 | /*******************************************************************************/ 204 | /* Line Coding Structure */ 205 | /*-----------------------------------------------------------------------------*/ 206 | /* Offset | Field | Size | Value | Description */ 207 | /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/ 208 | /* 4 | bCharFormat | 1 | Number | Stop bits */ 209 | /* 0 - 1 Stop bit */ 210 | /* 1 - 1.5 Stop bits */ 211 | /* 2 - 2 Stop bits */ 212 | /* 5 | bParityType | 1 | Number | Parity */ 213 | /* 0 - None */ 214 | /* 1 - Odd */ 215 | /* 2 - Even */ 216 | /* 3 - Mark */ 217 | /* 4 - Space */ 218 | /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */ 219 | /*******************************************************************************/ 220 | case CDC_SET_LINE_CODING: 221 | 222 | break; 223 | 224 | case CDC_GET_LINE_CODING: 225 | 226 | break; 227 | 228 | case CDC_SET_CONTROL_LINE_STATE: 229 | 230 | break; 231 | 232 | case CDC_SEND_BREAK: 233 | 234 | break; 235 | 236 | default: 237 | break; 238 | } 239 | 240 | return (USBD_OK); 241 | /* USER CODE END 5 */ 242 | } 243 | 244 | /** 245 | * @brief Data received over USB OUT endpoint are sent over CDC interface 246 | * through this function. 247 | * 248 | * @note 249 | * This function will issue a NAK packet on any OUT packet received on 250 | * USB endpoint until exiting this function. If you exit this function 251 | * before transfer is complete on CDC interface (ie. using DMA controller) 252 | * it will result in receiving more data while previous ones are still 253 | * not sent. 254 | * 255 | * @param Buf: Buffer of data to be received 256 | * @param Len: Number of data received (in bytes) 257 | * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL 258 | */ 259 | static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len) 260 | { 261 | /* USER CODE BEGIN 6 */ 262 | USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]); 263 | USBD_CDC_ReceivePacket(&hUsbDeviceFS); 264 | 265 | extern uint8_t ReceivedData[40]; 266 | 267 | for (uint8_t iter = 0; iter < 40; ++iter) { 268 | ReceivedData[iter] = 0; 269 | } 270 | 271 | strlcpy((char*)ReceivedData, (char*)Buf, (*Len) + 1); 272 | 273 | uint8_t DataToSend[80]; 274 | uint8_t MessageLength = 0; 275 | 276 | if (ReceivedData[0] == '?') { 277 | MessageLength = sprintf((char*)DataToSend, "$%s/\n\r", "BLDCController"); 278 | CDC_Transmit_FS(DataToSend, MessageLength); 279 | } else { 280 | decode_message(ReceivedData, 40); 281 | } 282 | 283 | return (USBD_OK); 284 | /* USER CODE END 6 */ 285 | } 286 | 287 | /** 288 | * @brief CDC_Transmit_FS 289 | * Data to send over USB IN endpoint are sent over CDC interface 290 | * through this function. 291 | * @note 292 | * 293 | * 294 | * @param Buf: Buffer of data to be sent 295 | * @param Len: Number of data to be sent (in bytes) 296 | * @retval USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY 297 | */ 298 | uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) 299 | { 300 | uint8_t result = USBD_OK; 301 | /* USER CODE BEGIN 7 */ 302 | USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData; 303 | if (hcdc->TxState != 0){ 304 | return USBD_BUSY; 305 | } 306 | USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len); 307 | result = USBD_CDC_TransmitPacket(&hUsbDeviceFS); 308 | /* USER CODE END 7 */ 309 | return result; 310 | } 311 | 312 | /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */ 313 | 314 | /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */ 315 | 316 | /** 317 | * @} 318 | */ 319 | 320 | /** 321 | * @} 322 | */ 323 | -------------------------------------------------------------------------------- /USB_DEVICE/App/usbd_cdc_if.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : usbd_cdc_if.h 5 | * @version : v2.0_Cube 6 | * @brief : Header for usbd_cdc_if.c file. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2022 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | 21 | /* Define to prevent recursive inclusion -------------------------------------*/ 22 | #ifndef __USBD_CDC_IF_H__ 23 | #define __USBD_CDC_IF_H__ 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | /* Includes ------------------------------------------------------------------*/ 30 | #include "usbd_cdc.h" 31 | 32 | /* USER CODE BEGIN INCLUDE */ 33 | 34 | /* USER CODE END INCLUDE */ 35 | 36 | /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY 37 | * @brief For Usb device. 38 | * @{ 39 | */ 40 | 41 | /** @defgroup USBD_CDC_IF USBD_CDC_IF 42 | * @brief Usb VCP device module 43 | * @{ 44 | */ 45 | 46 | /** @defgroup USBD_CDC_IF_Exported_Defines USBD_CDC_IF_Exported_Defines 47 | * @brief Defines. 48 | * @{ 49 | */ 50 | /* Define size for the receive and transmit buffer over CDC */ 51 | #define APP_RX_DATA_SIZE 1000 52 | #define APP_TX_DATA_SIZE 1000 53 | /* USER CODE BEGIN EXPORTED_DEFINES */ 54 | 55 | /* USER CODE END EXPORTED_DEFINES */ 56 | 57 | /** 58 | * @} 59 | */ 60 | 61 | /** @defgroup USBD_CDC_IF_Exported_Types USBD_CDC_IF_Exported_Types 62 | * @brief Types. 63 | * @{ 64 | */ 65 | 66 | /* USER CODE BEGIN EXPORTED_TYPES */ 67 | 68 | /* USER CODE END EXPORTED_TYPES */ 69 | 70 | /** 71 | * @} 72 | */ 73 | 74 | /** @defgroup USBD_CDC_IF_Exported_Macros USBD_CDC_IF_Exported_Macros 75 | * @brief Aliases. 76 | * @{ 77 | */ 78 | 79 | /* USER CODE BEGIN EXPORTED_MACRO */ 80 | 81 | /* USER CODE END EXPORTED_MACRO */ 82 | 83 | /** 84 | * @} 85 | */ 86 | 87 | /** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables 88 | * @brief Public variables. 89 | * @{ 90 | */ 91 | 92 | /** CDC Interface callback. */ 93 | extern USBD_CDC_ItfTypeDef USBD_Interface_fops_FS; 94 | 95 | /* USER CODE BEGIN EXPORTED_VARIABLES */ 96 | 97 | /* USER CODE END EXPORTED_VARIABLES */ 98 | 99 | /** 100 | * @} 101 | */ 102 | 103 | /** @defgroup USBD_CDC_IF_Exported_FunctionsPrototype USBD_CDC_IF_Exported_FunctionsPrototype 104 | * @brief Public functions declaration. 105 | * @{ 106 | */ 107 | 108 | uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len); 109 | 110 | /* USER CODE BEGIN EXPORTED_FUNCTIONS */ 111 | 112 | /* USER CODE END EXPORTED_FUNCTIONS */ 113 | 114 | /** 115 | * @} 116 | */ 117 | 118 | /** 119 | * @} 120 | */ 121 | 122 | /** 123 | * @} 124 | */ 125 | 126 | #ifdef __cplusplus 127 | } 128 | #endif 129 | 130 | #endif /* __USBD_CDC_IF_H__ */ 131 | 132 | -------------------------------------------------------------------------------- /USB_DEVICE/App/usbd_desc.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : usbd_desc.c 5 | * @version : v2.0_Cube 6 | * @brief : Header for usbd_conf.c file. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2022 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __USBD_DESC__C__ 22 | #define __USBD_DESC__C__ 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "usbd_def.h" 30 | 31 | /* USER CODE BEGIN INCLUDE */ 32 | 33 | /* USER CODE END INCLUDE */ 34 | 35 | /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY 36 | * @{ 37 | */ 38 | 39 | /** @defgroup USBD_DESC USBD_DESC 40 | * @brief Usb device descriptors module. 41 | * @{ 42 | */ 43 | 44 | /** @defgroup USBD_DESC_Exported_Constants USBD_DESC_Exported_Constants 45 | * @brief Constants. 46 | * @{ 47 | */ 48 | #define DEVICE_ID1 (UID_BASE) 49 | #define DEVICE_ID2 (UID_BASE + 0x4) 50 | #define DEVICE_ID3 (UID_BASE + 0x8) 51 | 52 | #define USB_SIZ_STRING_SERIAL 0x1A 53 | 54 | /* USER CODE BEGIN EXPORTED_CONSTANTS */ 55 | 56 | /* USER CODE END EXPORTED_CONSTANTS */ 57 | 58 | /** 59 | * @} 60 | */ 61 | 62 | /** @defgroup USBD_DESC_Exported_Defines USBD_DESC_Exported_Defines 63 | * @brief Defines. 64 | * @{ 65 | */ 66 | 67 | /* USER CODE BEGIN EXPORTED_DEFINES */ 68 | 69 | /* USER CODE END EXPORTED_DEFINES */ 70 | 71 | /** 72 | * @} 73 | */ 74 | 75 | /** @defgroup USBD_DESC_Exported_TypesDefinitions USBD_DESC_Exported_TypesDefinitions 76 | * @brief Types. 77 | * @{ 78 | */ 79 | 80 | /* USER CODE BEGIN EXPORTED_TYPES */ 81 | 82 | /* USER CODE END EXPORTED_TYPES */ 83 | 84 | /** 85 | * @} 86 | */ 87 | 88 | /** @defgroup USBD_DESC_Exported_Macros USBD_DESC_Exported_Macros 89 | * @brief Aliases. 90 | * @{ 91 | */ 92 | 93 | /* USER CODE BEGIN EXPORTED_MACRO */ 94 | 95 | /* USER CODE END EXPORTED_MACRO */ 96 | 97 | /** 98 | * @} 99 | */ 100 | 101 | /** @defgroup USBD_DESC_Exported_Variables USBD_DESC_Exported_Variables 102 | * @brief Public variables. 103 | * @{ 104 | */ 105 | 106 | /** Descriptor for the Usb device. */ 107 | extern USBD_DescriptorsTypeDef FS_Desc; 108 | 109 | /* USER CODE BEGIN EXPORTED_VARIABLES */ 110 | 111 | /* USER CODE END EXPORTED_VARIABLES */ 112 | 113 | /** 114 | * @} 115 | */ 116 | 117 | /** @defgroup USBD_DESC_Exported_FunctionsPrototype USBD_DESC_Exported_FunctionsPrototype 118 | * @brief Public functions declaration. 119 | * @{ 120 | */ 121 | 122 | /* USER CODE BEGIN EXPORTED_FUNCTIONS */ 123 | 124 | /* USER CODE END EXPORTED_FUNCTIONS */ 125 | 126 | /** 127 | * @} 128 | */ 129 | 130 | /** 131 | * @} 132 | */ 133 | 134 | /** 135 | * @} 136 | */ 137 | 138 | #ifdef __cplusplus 139 | } 140 | #endif 141 | 142 | #endif /* __USBD_DESC__C__ */ 143 | 144 | -------------------------------------------------------------------------------- /USB_DEVICE/Target/usbd_conf.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : usbd_conf.h 5 | * @version : v2.0_Cube 6 | * @brief : Header for usbd_conf.c file. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2022 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the LICENSE file 14 | * in the root directory of this software component. 15 | * If no LICENSE file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | 21 | /* Define to prevent recursive inclusion -------------------------------------*/ 22 | #ifndef __USBD_CONF__H__ 23 | #define __USBD_CONF__H__ 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | /* Includes ------------------------------------------------------------------*/ 30 | #include 31 | #include 32 | #include 33 | #include "main.h" 34 | #include "stm32f1xx.h" 35 | #include "stm32f1xx_hal.h" 36 | 37 | /* USER CODE BEGIN INCLUDE */ 38 | 39 | /* USER CODE END INCLUDE */ 40 | 41 | /** @addtogroup USBD_OTG_DRIVER 42 | * @{ 43 | */ 44 | 45 | /** @defgroup USBD_CONF USBD_CONF 46 | * @brief Configuration file for Usb otg low level driver. 47 | * @{ 48 | */ 49 | 50 | /** @defgroup USBD_CONF_Exported_Variables USBD_CONF_Exported_Variables 51 | * @brief Public variables. 52 | * @{ 53 | */ 54 | 55 | /** 56 | * @} 57 | */ 58 | 59 | /** @defgroup USBD_CONF_Exported_Defines USBD_CONF_Exported_Defines 60 | * @brief Defines for configuration of the Usb device. 61 | * @{ 62 | */ 63 | 64 | /*---------- -----------*/ 65 | #define USBD_MAX_NUM_INTERFACES 1 66 | /*---------- -----------*/ 67 | #define USBD_MAX_NUM_CONFIGURATION 1 68 | /*---------- -----------*/ 69 | #define USBD_MAX_STR_DESC_SIZ 512 70 | /*---------- -----------*/ 71 | #define USBD_DEBUG_LEVEL 0 72 | /*---------- -----------*/ 73 | #define USBD_SELF_POWERED 1 74 | /*---------- -----------*/ 75 | #define MAX_STATIC_ALLOC_SIZE 512 76 | 77 | /****************************************/ 78 | /* #define for FS and HS identification */ 79 | #define DEVICE_FS 0 80 | 81 | /** 82 | * @} 83 | */ 84 | 85 | /** @defgroup USBD_CONF_Exported_Macros USBD_CONF_Exported_Macros 86 | * @brief Aliases. 87 | * @{ 88 | */ 89 | 90 | /* Memory management macros */ 91 | 92 | /** Alias for memory allocation. */ 93 | #define USBD_malloc (uint32_t *)USBD_static_malloc 94 | 95 | /** Alias for memory release. */ 96 | #define USBD_free USBD_static_free 97 | 98 | /** Alias for memory set. */ 99 | #define USBD_memset /* Not used */ 100 | 101 | /** Alias for memory copy. */ 102 | #define USBD_memcpy /* Not used */ 103 | 104 | /** Alias for delay. */ 105 | #define USBD_Delay HAL_Delay 106 | 107 | /* For footprint reasons and since only one allocation is handled in the HID class 108 | driver, the malloc/free is changed into a static allocation method */ 109 | void *USBD_static_malloc(uint32_t size); 110 | void USBD_static_free(void *p); 111 | 112 | /* DEBUG macros */ 113 | 114 | #if (USBD_DEBUG_LEVEL > 0) 115 | #define USBD_UsrLog(...) printf(__VA_ARGS__);\ 116 | printf("\n"); 117 | #else 118 | #define USBD_UsrLog(...) 119 | #endif 120 | 121 | #if (USBD_DEBUG_LEVEL > 1) 122 | 123 | #define USBD_ErrLog(...) printf("ERROR: ") ;\ 124 | printf(__VA_ARGS__);\ 125 | printf("\n"); 126 | #else 127 | #define USBD_ErrLog(...) 128 | #endif 129 | 130 | #if (USBD_DEBUG_LEVEL > 2) 131 | #define USBD_DbgLog(...) printf("DEBUG : ") ;\ 132 | printf(__VA_ARGS__);\ 133 | printf("\n"); 134 | #else 135 | #define USBD_DbgLog(...) 136 | #endif 137 | 138 | /** 139 | * @} 140 | */ 141 | 142 | /** @defgroup USBD_CONF_Exported_Types USBD_CONF_Exported_Types 143 | * @brief Types. 144 | * @{ 145 | */ 146 | 147 | /** 148 | * @} 149 | */ 150 | 151 | /** @defgroup USBD_CONF_Exported_FunctionsPrototype USBD_CONF_Exported_FunctionsPrototype 152 | * @brief Declaration of public functions for Usb device. 153 | * @{ 154 | */ 155 | 156 | /* Exported functions -------------------------------------------------------*/ 157 | 158 | /** 159 | * @} 160 | */ 161 | 162 | /** 163 | * @} 164 | */ 165 | 166 | /** 167 | * @} 168 | */ 169 | 170 | #ifdef __cplusplus 171 | } 172 | #endif 173 | 174 | #endif /* __USBD_CONF__H__ */ 175 | 176 | -------------------------------------------------------------------------------- /projectbldc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KoSik-prog/STM32F103_BLDC_Driver/3d108960543fd65714011477147a4a047ca86acc/projectbldc.png -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ![alt text](projectbldc.png) 2 | 3 | 4 | Schematic: https://a360.co/3iP8vQz 5 | 6 | 3D view: https://a360.co/3f0I6O4 7 | 8 | YouTube: https://youtu.be/0DW3Gm4tZaE 9 | 10 | 11 | Problems or ideas, write me: kosik84@gmail.com 12 | --------------------------------------------------------------------------------