├── Public ├── system.c ├── system.h ├── usart.h ├── SysTick.h ├── SysTick.c └── usart.c ├── Pic ├── 1.PNG └── 2.PNG ├── APP ├── led │ ├── led.c │ └── led.h ├── mpu6050 │ ├── M5StickC.h │ ├── PS2_SONY.h │ ├── iompu6050.h │ ├── PS2_SONY.c │ └── iompu6050.c └── pwm │ ├── pwm.h │ └── pwm.c ├── keilkilll.bat ├── User ├── stm32f10x.h ├── stm32f10x_it.h ├── stm32f10x_conf.h └── stm32f10x_it.c ├── USB ├── CONFIG │ ├── usb_endp.c │ ├── usb_prop.c │ ├── hw_config.c │ ├── hw_config.h │ ├── platform_config.h │ ├── usb_pwr.h │ ├── usb_istr.h │ ├── usb_desc.h │ ├── usb_prop.h │ ├── usb_conf.h │ ├── usb_istr.c │ ├── usb_desc.c │ └── usb_pwr.c └── STM32_USB-FS-Device_Driver │ ├── inc │ ├── usb_int.h │ ├── usb_type.h │ ├── usb_mem.h │ ├── usb_sil.h │ ├── usb_lib.h │ ├── usb_init.h │ ├── usb_def.h │ └── usb_core.h │ └── src │ ├── usb_init.c │ ├── usb_mem.c │ ├── usb_sil.c │ └── usb_int.c ├── Libraries ├── STM32F10x_StdPeriph_Driver │ ├── src │ │ ├── stm32f10x_usart.c │ │ ├── misc.c │ │ └── stm32f10x_exti.c │ └── inc │ │ ├── stm32f10x_exti.h │ │ ├── misc.h │ │ └── stm32f10x_usart.h └── CMSIS │ ├── system_stm32f10x.h │ └── startup_stm32f10x_md.s └── README.md /Public/system.c: -------------------------------------------------------------------------------- 1 | #include "system.h" 2 | 3 | -------------------------------------------------------------------------------- /Pic/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piseason/mySTM32Car/HEAD/Pic/1.PNG -------------------------------------------------------------------------------- /Pic/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piseason/mySTM32Car/HEAD/Pic/2.PNG -------------------------------------------------------------------------------- /APP/led/led.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piseason/mySTM32Car/HEAD/APP/led/led.c -------------------------------------------------------------------------------- /keilkilll.bat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piseason/mySTM32Car/HEAD/keilkilll.bat -------------------------------------------------------------------------------- /Public/system.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piseason/mySTM32Car/HEAD/Public/system.h -------------------------------------------------------------------------------- /User/stm32f10x.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piseason/mySTM32Car/HEAD/User/stm32f10x.h -------------------------------------------------------------------------------- /USB/CONFIG/usb_endp.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piseason/mySTM32Car/HEAD/USB/CONFIG/usb_endp.c -------------------------------------------------------------------------------- /USB/CONFIG/usb_prop.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piseason/mySTM32Car/HEAD/USB/CONFIG/usb_prop.c -------------------------------------------------------------------------------- /USB/CONFIG/hw_config.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piseason/mySTM32Car/HEAD/USB/CONFIG/hw_config.c -------------------------------------------------------------------------------- /USB/CONFIG/hw_config.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piseason/mySTM32Car/HEAD/USB/CONFIG/hw_config.h -------------------------------------------------------------------------------- /Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_usart.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piseason/mySTM32Car/HEAD/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_usart.c -------------------------------------------------------------------------------- /Public/usart.h: -------------------------------------------------------------------------------- 1 | #ifndef __usart_H 2 | #define __usart_H 3 | 4 | #include "system.h" 5 | #include "stdio.h" 6 | 7 | void USART1_Init(u32 bound); 8 | 9 | #endif 10 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mySTM32Car 2 | ## 实物图 3 | ![image](https://github.com/piseason/mySTM32Car/blob/master/Pic/1.PNG) 4 | ## 接线图 5 | ![image](https://github.com/piseason/mySTM32Car/blob/master/Pic/2.PNG) 6 | -------------------------------------------------------------------------------- /Public/SysTick.h: -------------------------------------------------------------------------------- 1 | #ifndef _SysTick_H 2 | #define _SysTick_H 3 | 4 | #include "system.h" 5 | 6 | void SysTick_Init(u8 SYSCLK); 7 | void delay_ms(u16 nms); 8 | void delay_us(u32 nus); 9 | 10 | 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /APP/led/led.h: -------------------------------------------------------------------------------- 1 | #ifndef _led_H 2 | #define _led_H 3 | 4 | #include "system.h" 5 | 6 | /* LED时钟端口、引脚定义 */ 7 | #define LED_PORT GPIOC 8 | //#define LED_PIN (GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7) 9 | #define LED_PIN GPIO_Pin_13 10 | #define LED_PORT_RCC RCC_APB2Periph_GPIOC 11 | 12 | 13 | #define led PCout(13) 14 | 15 | 16 | void LED_Init(void); 17 | 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /APP/mpu6050/M5StickC.h: -------------------------------------------------------------------------------- 1 | #ifndef __M5StickC_H 2 | #define __M5StickC_H 3 | 4 | extern float thetaP, thetaI, thetaD, power; 5 | extern float theta0, theta_dot0, theta, theta_dot, Kp, Ki, Kd; 6 | extern float Kvp, Kvi, Kvd, thetaII, thetaOffset,thetaV; 7 | 8 | void ini_theta(void); 9 | void update_theta(void); 10 | void update_theta2(float thetaTar); 11 | void angleFilter(float tmpI); 12 | void angleCorrection(void); 13 | void update_thetaEx(float thetaTar); 14 | #endif 15 | -------------------------------------------------------------------------------- /APP/pwm/pwm.h: -------------------------------------------------------------------------------- 1 | #ifndef _pwm_H 2 | #define _pwm_H 3 | 4 | #include "system.h" 5 | 6 | #define PWM_PIN_TIM1 GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 7 | 8 | #define PWM_PIN_TIM3_A GPIO_Pin_6 | GPIO_Pin_7 9 | #define PWM_PIN_TIM3_B GPIO_Pin_0 | GPIO_Pin_1 10 | 11 | #define msSpeed 500 //100ms 12 | 13 | extern volatile u16 cntL; 14 | extern volatile u16 cntR; 15 | 16 | 17 | void TIM1_PWM_Init(u16 arr, u16 psc); //USART1占了PA9&10 18 | void TIM3_PWM_Init(u16 arr, u16 psc); 19 | void TIM2_CH12_PWM_Init(u16 arr,u16 psc); 20 | void TIM2_CH34_Input_Init(u16 arr,u16 psc); 21 | void TIM4_CH12_PWM_Init(u16 arr,u16 psc); 22 | void startSpeedCount(void); 23 | void TIM4_counter_Init(u16 arr,u16 psc); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /Public/SysTick.c: -------------------------------------------------------------------------------- 1 | #include "SysTick.h" 2 | 3 | static u8 fac_us=0; //us延时倍乘数 4 | static u16 fac_ms=0; //ms延时倍乘数 5 | 6 | 7 | //初始化延迟函数 8 | //SYSTICK的时钟固定为AHB时钟的1/8 9 | //SYSCLK:系统时钟频率 10 | void SysTick_Init(u8 SYSCLK) 11 | { 12 | SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); 13 | fac_us=SYSCLK/8; 14 | fac_ms=(u16)fac_us*1000; 15 | } 16 | 17 | 18 | //延时nus 19 | //nus为要延时的us数. 20 | void delay_us(u32 nus) 21 | { 22 | u32 temp; 23 | SysTick->LOAD=nus*fac_us; //时间加载 24 | SysTick->VAL=0x00; //清空计数器 25 | SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //开始倒数 26 | do 27 | { 28 | temp=SysTick->CTRL; 29 | }while((temp&0x01)&&!(temp&(1<<16))); //等待时间到达 30 | SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器 31 | SysTick->VAL =0X00; //清空计数器 32 | } 33 | 34 | //延时nms 35 | //注意nms的范围 36 | //SysTick->LOAD为24位寄存器,所以,最大延时为: 37 | //nms<=0xffffff*8*1000/SYSCLK 38 | //SYSCLK单位为Hz,nms单位为ms 39 | //对72M条件下,nms<=1864 40 | void delay_ms(u16 nms) 41 | { 42 | u32 temp; 43 | SysTick->LOAD=(u32)nms*fac_ms; //时间加载(SysTick->LOAD为24bit) 44 | SysTick->VAL =0x00; //清空计数器 45 | SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //开始倒数 46 | do 47 | { 48 | temp=SysTick->CTRL; 49 | }while((temp&0x01)&&!(temp&(1<<16))); //等待时间到达 50 | SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器 51 | SysTick->VAL =0X00; //清空计数器 52 | } 53 | 54 | 55 | -------------------------------------------------------------------------------- /APP/mpu6050/PS2_SONY.h: -------------------------------------------------------------------------------- 1 | #ifndef __PS2_SONY_H__ 2 | #define __PS2_SONY_H__ 3 | 4 | #include "system.h" 5 | 6 | #define PS2_DAT PBin(3) 7 | #define PS2_CMD PBout(4) 8 | #define PS2_ATT PBout(5) 9 | #define PS2_CLK PBout(6) 10 | 11 | /***********************************宏定义*******************************************/ 12 | #define START_CMD 0x01 //手柄起始指令 13 | #define ASK_DAT_CMD 0x42 //手柄应答指令 14 | #define PSX_GREEN_MODE 0x41 //手柄绿灯模式对应码 15 | #define PSX_RED_MODE 0x73 //手柄红灯模式对应码 16 | #define PSX_BUF_SIZE 9 //手柄数组大小 17 | #define CMD_RETURN_SIZE 50 //串口打印数组大小 18 | 19 | #define RELEASE_L1 0x01 //设置L1键弹起标志 20 | #define RELEASE_L2 0x02 //设置L2键弹起标志 21 | #define RELEASE_R1 0x03 //设置R1键弹起标志 22 | #define RELEASE_R2 0x04 //设置R2键弹起标志 23 | #define RELEASE_UP 0x05 //设置上键弹起标志 24 | #define RELEASE_DOWN 0x06 //设置下键弹起标志 25 | #define RELEASE_LEFT 0x07 //设置左键弹起标志 26 | #define RELEASE_RIGHT 0x08 //设置右键弹起标志 27 | #define RELEASE_TRI 0x09 //设置三角键弹起标志 28 | #define RELEASE_CROSS 0x10 //设置交叉键弹起标志 29 | #define RELEASE_RECT 0x11 //设置方块键弹起标志 30 | #define RELEASE_CIRCLE 0x12 //设置圆圈键弹起标志 31 | #define RELEASE_SELECT 0x13 //设置选择键弹起标志 32 | #define RELEASE_START 0x14 //设置开始键弹起标志 33 | #define RELEASE_L3 0x15 //设置L3键弹起标志 34 | #define RELEASE_R3 0x16 //设置R3键弹起标志 35 | 36 | extern u8 PS2Count; 37 | extern u8 release_flag; 38 | 39 | void PSX_init(void); //配置初始化 40 | void getStateHold(void); //弹起后才能按别的 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /USB/CONFIG/platform_config.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file platform_config.h 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 21-January-2013 7 | * @brief Evaluation board specific configuration file. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2013 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __PLATFORM_CONFIG_H 31 | #define __PLATFORM_CONFIG_H 32 | 33 | /* Includes ------------------------------------------------------------------*/ 34 | #include "system.h" 35 | 36 | 37 | /* Exported types ------------------------------------------------------------*/ 38 | /* Exported constants --------------------------------------------------------*/ 39 | /* Uncomment the line corresponding to the STMicroelectronics evaluation board 40 | used to run the example */ 41 | 42 | #define USE_STM3210E_EVAL 43 | 44 | 45 | 46 | 47 | 48 | #endif /* __PLATFORM_CONFIG_H */ 49 | 50 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 51 | -------------------------------------------------------------------------------- /USB/STM32_USB-FS-Device_Driver/inc/usb_int.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_int.h 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 28-August-2012 7 | * @brief Endpoint CTR (Low and High) interrupt's service routines prototypes 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __USB_INT_H 31 | #define __USB_INT_H 32 | 33 | /* Includes ------------------------------------------------------------------*/ 34 | /* Exported types ------------------------------------------------------------*/ 35 | /* Exported constants --------------------------------------------------------*/ 36 | /* Exported macro ------------------------------------------------------------*/ 37 | /* Exported functions ------------------------------------------------------- */ 38 | void CTR_LP(void); 39 | void CTR_HP(void); 40 | 41 | /* External variables --------------------------------------------------------*/ 42 | 43 | #endif /* __USB_INT_H */ 44 | 45 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 46 | -------------------------------------------------------------------------------- /USB/STM32_USB-FS-Device_Driver/inc/usb_type.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_type.h 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 28-August-2012 7 | * @brief Type definitions used by the USB Library 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __USB_TYPE_H 31 | #define __USB_TYPE_H 32 | 33 | /* Includes ------------------------------------------------------------------*/ 34 | #include "usb_conf.h" 35 | 36 | /* Exported types ------------------------------------------------------------*/ 37 | /* Exported constants --------------------------------------------------------*/ 38 | #ifndef NULL 39 | #define NULL ((void *)0) 40 | #endif 41 | 42 | typedef enum 43 | { 44 | FALSE = 0, TRUE = !FALSE 45 | } 46 | bool; 47 | 48 | /* Exported macro ------------------------------------------------------------*/ 49 | /* Exported functions ------------------------------------------------------- */ 50 | /* External variables --------------------------------------------------------*/ 51 | 52 | #endif /* __USB_TYPE_H */ 53 | 54 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 55 | -------------------------------------------------------------------------------- /USB/STM32_USB-FS-Device_Driver/inc/usb_mem.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_mem.h 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 28-August-2012 7 | * @brief Utility prototypes functions for memory/PMA transfers 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __USB_MEM_H 31 | #define __USB_MEM_H 32 | 33 | /* Includes ------------------------------------------------------------------*/ 34 | /* Exported types ------------------------------------------------------------*/ 35 | /* Exported constants --------------------------------------------------------*/ 36 | /* Exported macro ------------------------------------------------------------*/ 37 | /* Exported functions ------------------------------------------------------- */ 38 | void UserToPMABufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes); 39 | void PMAToUserBufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes); 40 | 41 | /* External variables --------------------------------------------------------*/ 42 | 43 | #endif /*__USB_MEM_H*/ 44 | 45 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 46 | -------------------------------------------------------------------------------- /USB/STM32_USB-FS-Device_Driver/inc/usb_sil.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_sil.h 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 28-August-2012 7 | * @brief Simplified Interface Layer function prototypes. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __USB_SIL_H 31 | #define __USB_SIL_H 32 | 33 | /* Includes ------------------------------------------------------------------*/ 34 | /* Exported types ------------------------------------------------------------*/ 35 | /* Exported constants --------------------------------------------------------*/ 36 | /* Exported macro ------------------------------------------------------------*/ 37 | /* Exported functions ------------------------------------------------------- */ 38 | 39 | uint32_t USB_SIL_Init(void); 40 | uint32_t USB_SIL_Write(uint8_t bEpAddr, uint8_t* pBufferPointer, uint32_t wBufferSize); 41 | uint32_t USB_SIL_Read(uint8_t bEpAddr, uint8_t* pBufferPointer); 42 | 43 | /* External variables --------------------------------------------------------*/ 44 | 45 | #endif /* __USB_SIL_H */ 46 | 47 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 48 | -------------------------------------------------------------------------------- /USB/STM32_USB-FS-Device_Driver/inc/usb_lib.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_lib.h 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 28-August-2012 7 | * @brief USB library include files 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __USB_LIB_H 31 | #define __USB_LIB_H 32 | 33 | /* Includes ------------------------------------------------------------------*/ 34 | #include "hw_config.h" 35 | #include "usb_type.h" 36 | #include "usb_regs.h" 37 | #include "usb_def.h" 38 | #include "usb_core.h" 39 | #include "usb_init.h" 40 | #include "usb_sil.h" 41 | #include "usb_mem.h" 42 | #include "usb_int.h" 43 | 44 | /* Exported types ------------------------------------------------------------*/ 45 | /* Exported constants --------------------------------------------------------*/ 46 | /* Exported macro ------------------------------------------------------------*/ 47 | /* Exported functions ------------------------------------------------------- */ 48 | /* External variables --------------------------------------------------------*/ 49 | 50 | #endif /* __USB_LIB_H */ 51 | 52 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 53 | -------------------------------------------------------------------------------- /User/stm32f10x_it.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file Project/STM32F10x_StdPeriph_Template/stm32f10x_it.h 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 08-April-2011 7 | * @brief This file contains the headers of the interrupt handlers. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | #ifndef __STM32F10x_IT_H 24 | #define __STM32F10x_IT_H 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | /* Includes ------------------------------------------------------------------*/ 31 | #include "stm32f10x.h" 32 | 33 | /* Exported types ------------------------------------------------------------*/ 34 | /* Exported constants --------------------------------------------------------*/ 35 | /* Exported macro ------------------------------------------------------------*/ 36 | /* Exported functions ------------------------------------------------------- */ 37 | 38 | void NMI_Handler(void); 39 | void HardFault_Handler(void); 40 | void MemManage_Handler(void); 41 | void BusFault_Handler(void); 42 | void UsageFault_Handler(void); 43 | void SVC_Handler(void); 44 | void DebugMon_Handler(void); 45 | void PendSV_Handler(void); 46 | void SysTick_Handler(void); 47 | 48 | #ifdef __cplusplus 49 | } 50 | #endif 51 | 52 | #endif /* __STM32F10x_IT_H */ 53 | 54 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 55 | -------------------------------------------------------------------------------- /Libraries/CMSIS/system_stm32f10x.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file system_stm32f10x.h 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Header File. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /** @addtogroup CMSIS 23 | * @{ 24 | */ 25 | 26 | /** @addtogroup stm32f10x_system 27 | * @{ 28 | */ 29 | 30 | /** 31 | * @brief Define to prevent recursive inclusion 32 | */ 33 | #ifndef __SYSTEM_STM32F10X_H 34 | #define __SYSTEM_STM32F10X_H 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | /** @addtogroup STM32F10x_System_Includes 41 | * @{ 42 | */ 43 | 44 | /** 45 | * @} 46 | */ 47 | 48 | 49 | /** @addtogroup STM32F10x_System_Exported_types 50 | * @{ 51 | */ 52 | 53 | extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */ 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 2011 STMicroelectronics *****END OF FILE****/ 99 | -------------------------------------------------------------------------------- /USB/CONFIG/usb_pwr.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_pwr.h 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 21-January-2013 7 | * @brief Connection/disconnection & power management header 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2013 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __USB_PWR_H 31 | #define __USB_PWR_H 32 | 33 | /* Includes ------------------------------------------------------------------*/ 34 | /* Exported types ------------------------------------------------------------*/ 35 | typedef enum _RESUME_STATE 36 | { 37 | RESUME_EXTERNAL, 38 | RESUME_INTERNAL, 39 | RESUME_LATER, 40 | RESUME_WAIT, 41 | RESUME_START, 42 | RESUME_ON, 43 | RESUME_OFF, 44 | RESUME_ESOF 45 | } RESUME_STATE; 46 | 47 | typedef enum _DEVICE_STATE 48 | { 49 | UNCONNECTED, 50 | ATTACHED, 51 | POWERED, 52 | SUSPENDED, 53 | ADDRESSED, 54 | CONFIGURED 55 | } DEVICE_STATE; 56 | 57 | /* Exported constants --------------------------------------------------------*/ 58 | /* Exported macro ------------------------------------------------------------*/ 59 | /* Exported functions ------------------------------------------------------- */ 60 | void Suspend(void); 61 | void Resume_Init(void); 62 | void Resume(RESUME_STATE eResumeSetVal); 63 | RESULT PowerOn(void); 64 | RESULT PowerOff(void); 65 | 66 | /* External variables --------------------------------------------------------*/ 67 | extern __IO uint32_t bDeviceState; /* USB device status */ 68 | extern __IO bool fSuspendEnabled; /* true when suspend is possible */ 69 | 70 | #endif /*__USB_PWR_H*/ 71 | 72 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 73 | -------------------------------------------------------------------------------- /User/stm32f10x_conf.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file Project/STM32F10x_StdPeriph_Template/stm32f10x_conf.h 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 08-April-2011 7 | * @brief Library configuration file. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | #ifndef __STM32F10x_CONF_H 24 | #define __STM32F10x_CONF_H 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | /* Uncomment/Comment the line below to enable/disable peripheral header file inclusion */ 28 | #include "stm32f10x_exti.h" 29 | #include "stm32f10x_gpio.h" 30 | #include "stm32f10x_rcc.h" 31 | #include "stm32f10x_tim.h" 32 | #include "stm32f10x_usart.h" 33 | #include "misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */ 34 | 35 | /* Exported types ------------------------------------------------------------*/ 36 | /* Exported constants --------------------------------------------------------*/ 37 | /* Uncomment the line below to expanse the "assert_param" macro in the 38 | Standard Peripheral Library drivers code */ 39 | /* #define USE_FULL_ASSERT 1 */ 40 | 41 | /* Exported macro ------------------------------------------------------------*/ 42 | #ifdef USE_FULL_ASSERT 43 | 44 | /** 45 | * @brief The assert_param macro is used for function's parameters check. 46 | * @param expr: If expr is false, it calls assert_failed function which reports 47 | * the name of the source file and the source line number of the call 48 | * that failed. If expr is true, it returns no value. 49 | * @retval None 50 | */ 51 | #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) 52 | /* Exported functions ------------------------------------------------------- */ 53 | void assert_failed(uint8_t* file, uint32_t line); 54 | #else 55 | #define assert_param(expr) ((void)0) 56 | #endif /* USE_FULL_ASSERT */ 57 | 58 | #endif /* __STM32F10x_CONF_H */ 59 | 60 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 61 | -------------------------------------------------------------------------------- /USB/STM32_USB-FS-Device_Driver/inc/usb_init.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_init.h 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 28-August-2012 7 | * @brief Initialization routines & global variables 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __USB_INIT_H 31 | #define __USB_INIT_H 32 | 33 | /* Includes ------------------------------------------------------------------*/ 34 | /* Exported types ------------------------------------------------------------*/ 35 | /* Exported constants --------------------------------------------------------*/ 36 | /* Exported macro ------------------------------------------------------------*/ 37 | /* Exported functions ------------------------------------------------------- */ 38 | void USB_Init(void); 39 | 40 | /* External variables --------------------------------------------------------*/ 41 | /* The number of current endpoint, it will be used to specify an endpoint */ 42 | extern uint8_t EPindex; 43 | /* The number of current device, it is an index to the Device_Table */ 44 | /*extern uint8_t Device_no; */ 45 | /* Points to the DEVICE_INFO structure of current device */ 46 | /* The purpose of this register is to speed up the execution */ 47 | extern DEVICE_INFO* pInformation; 48 | /* Points to the DEVICE_PROP structure of current device */ 49 | /* The purpose of this register is to speed up the execution */ 50 | extern DEVICE_PROP* pProperty; 51 | /* Temporary save the state of Rx & Tx status. */ 52 | /* Whenever the Rx or Tx state is changed, its value is saved */ 53 | /* in this variable first and will be set to the EPRB or EPRA */ 54 | /* at the end of interrupt process */ 55 | extern USER_STANDARD_REQUESTS *pUser_Standard_Requests; 56 | 57 | extern uint16_t SaveState ; 58 | extern uint16_t wInterrupt_Mask; 59 | 60 | #endif /* __USB_INIT_H */ 61 | 62 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 63 | -------------------------------------------------------------------------------- /Public/usart.c: -------------------------------------------------------------------------------- 1 | #include "usart.h" 2 | #include "string.h" 3 | 4 | int fputc(int ch,FILE *p) //函数默认的,在使用printf函数时自动调用 5 | { 6 | USART_SendData(USART1,(u8)ch); 7 | while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET); 8 | return ch; 9 | } 10 | 11 | /******************************************************************************* 12 | * 函 数 名 : USART1_Init 13 | * 函数功能 : USART1初始化函数 14 | * 输 入 : bound:波特率 15 | * 输 出 : 无 16 | *******************************************************************************/ 17 | void USART1_Init(u32 bound) 18 | { 19 | //GPIO端口设置 20 | GPIO_InitTypeDef GPIO_InitStructure; 21 | USART_InitTypeDef USART_InitStructure; 22 | NVIC_InitTypeDef NVIC_InitStructure; 23 | 24 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); 25 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); 26 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //打开时钟 27 | 28 | 29 | /* 配置GPIO的模式和IO口 */ 30 | GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX //串口输出PA9 31 | GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; 32 | GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出 33 | GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化串口输入IO */ 34 | GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX //串口输入PA10 35 | GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //模拟输入 36 | GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化GPIO */ 37 | 38 | 39 | //USART1 初始化设置 40 | USART_InitStructure.USART_BaudRate = bound;//波特率设置 41 | USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 42 | USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 43 | USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 44 | USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 45 | USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 46 | USART_Init(USART1, &USART_InitStructure); //初始化串口1 47 | 48 | USART_Cmd(USART1, ENABLE); //使能串口1 49 | 50 | USART_ClearFlag(USART1, USART_FLAG_TC); 51 | 52 | USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启相关中断 53 | 54 | //Usart1 NVIC 配置 55 | NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断通道 56 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3 57 | NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3 58 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 59 | NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、 60 | } 61 | 62 | 63 | 64 | /******************************************************************************* 65 | * 函 数 名 : USART1_IRQHandler 66 | * 函数功能 : USART1中断函数 67 | * 输 入 : 无 68 | * 输 出 : 无 69 | *******************************************************************************/ 70 | void USART1_IRQHandler(void) //串口1中断服务程序 71 | { 72 | u8 r; 73 | if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断 74 | { 75 | r =USART_ReceiveData(USART1);//(USART1->DR); //读取接收到的数据 76 | USART_SendData(USART1,r); 77 | while(USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET); 78 | } 79 | USART_ClearFlag(USART1,USART_FLAG_TC); 80 | } 81 | -------------------------------------------------------------------------------- /USB/CONFIG/usb_istr.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_istr.h 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 21-January-2013 7 | * @brief This file includes the peripherals header files in the user application. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2013 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __USB_ISTR_H 31 | #define __USB_ISTR_H 32 | 33 | /* Includes ------------------------------------------------------------------*/ 34 | #include "usb_conf.h" 35 | 36 | /* Exported types ------------------------------------------------------------*/ 37 | /* Exported constants --------------------------------------------------------*/ 38 | /* Exported macro ------------------------------------------------------------*/ 39 | /* Exported functions ------------------------------------------------------- */ 40 | 41 | void USB_Istr(void); 42 | 43 | /* function prototypes Automatically built defining related macros */ 44 | 45 | void EP1_IN_Callback(void); 46 | void EP2_IN_Callback(void); 47 | void EP3_IN_Callback(void); 48 | void EP4_IN_Callback(void); 49 | void EP5_IN_Callback(void); 50 | void EP6_IN_Callback(void); 51 | void EP7_IN_Callback(void); 52 | 53 | void EP1_OUT_Callback(void); 54 | void EP2_OUT_Callback(void); 55 | void EP3_OUT_Callback(void); 56 | void EP4_OUT_Callback(void); 57 | void EP5_OUT_Callback(void); 58 | void EP6_OUT_Callback(void); 59 | void EP7_OUT_Callback(void); 60 | 61 | #ifdef CTR_CALLBACK 62 | void CTR_Callback(void); 63 | #endif 64 | 65 | #ifdef DOVR_CALLBACK 66 | void DOVR_Callback(void); 67 | #endif 68 | 69 | #ifdef ERR_CALLBACK 70 | void ERR_Callback(void); 71 | #endif 72 | 73 | #ifdef WKUP_CALLBACK 74 | void WKUP_Callback(void); 75 | #endif 76 | 77 | #ifdef SUSP_CALLBACK 78 | void SUSP_Callback(void); 79 | #endif 80 | 81 | #ifdef RESET_CALLBACK 82 | void RESET_Callback(void); 83 | #endif 84 | 85 | #ifdef SOF_CALLBACK 86 | void SOF_Callback(void); 87 | #endif 88 | 89 | #ifdef ESOF_CALLBACK 90 | void ESOF_Callback(void); 91 | #endif 92 | #endif /*__USB_ISTR_H*/ 93 | 94 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 95 | -------------------------------------------------------------------------------- /USB/CONFIG/usb_desc.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_desc.h 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 21-January-2013 7 | * @brief Descriptor Header for Virtual COM Port Device 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2013 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __USB_DESC_H 31 | #define __USB_DESC_H 32 | 33 | /* Includes ------------------------------------------------------------------*/ 34 | /* Exported types ------------------------------------------------------------*/ 35 | /* Exported constants --------------------------------------------------------*/ 36 | /* Exported macro ------------------------------------------------------------*/ 37 | /* Exported define -----------------------------------------------------------*/ 38 | #define USB_DEVICE_DESCRIPTOR_TYPE 0x01 39 | #define USB_CONFIGURATION_DESCRIPTOR_TYPE 0x02 40 | #define USB_STRING_DESCRIPTOR_TYPE 0x03 41 | #define USB_INTERFACE_DESCRIPTOR_TYPE 0x04 42 | #define USB_ENDPOINT_DESCRIPTOR_TYPE 0x05 43 | 44 | #define VIRTUAL_COM_PORT_DATA_SIZE 64 45 | #define VIRTUAL_COM_PORT_INT_SIZE 8 46 | 47 | #define VIRTUAL_COM_PORT_SIZ_DEVICE_DESC 18 48 | #define VIRTUAL_COM_PORT_SIZ_CONFIG_DESC 67 49 | #define VIRTUAL_COM_PORT_SIZ_STRING_LANGID 4 50 | #define VIRTUAL_COM_PORT_SIZ_STRING_VENDOR 38 51 | #define VIRTUAL_COM_PORT_SIZ_STRING_PRODUCT 50 52 | #define VIRTUAL_COM_PORT_SIZ_STRING_SERIAL 26 53 | 54 | #define STANDARD_ENDPOINT_DESC_SIZE 0x09 55 | 56 | /* Exported functions ------------------------------------------------------- */ 57 | extern const uint8_t Virtual_Com_Port_DeviceDescriptor[VIRTUAL_COM_PORT_SIZ_DEVICE_DESC]; 58 | extern const uint8_t Virtual_Com_Port_ConfigDescriptor[VIRTUAL_COM_PORT_SIZ_CONFIG_DESC]; 59 | 60 | extern const uint8_t Virtual_Com_Port_StringLangID[VIRTUAL_COM_PORT_SIZ_STRING_LANGID]; 61 | extern const uint8_t Virtual_Com_Port_StringVendor[VIRTUAL_COM_PORT_SIZ_STRING_VENDOR]; 62 | extern const uint8_t Virtual_Com_Port_StringProduct[VIRTUAL_COM_PORT_SIZ_STRING_PRODUCT]; 63 | extern uint8_t Virtual_Com_Port_StringSerial[VIRTUAL_COM_PORT_SIZ_STRING_SERIAL]; 64 | 65 | #endif /* __USB_DESC_H */ 66 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 67 | -------------------------------------------------------------------------------- /USB/STM32_USB-FS-Device_Driver/inc/usb_def.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_def.h 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 28-August-2012 7 | * @brief Definitions related to USB Core 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Define to prevent recursive inclusion -------------------------------------*/ 29 | #ifndef __USB_DEF_H 30 | #define __USB_DEF_H 31 | 32 | /* Includes ------------------------------------------------------------------*/ 33 | /* Exported types ------------------------------------------------------------*/ 34 | typedef enum _RECIPIENT_TYPE 35 | { 36 | DEVICE_RECIPIENT, /* Recipient device */ 37 | INTERFACE_RECIPIENT, /* Recipient interface */ 38 | ENDPOINT_RECIPIENT, /* Recipient endpoint */ 39 | OTHER_RECIPIENT 40 | } RECIPIENT_TYPE; 41 | 42 | 43 | typedef enum _STANDARD_REQUESTS 44 | { 45 | GET_STATUS = 0, 46 | CLEAR_FEATURE, 47 | RESERVED1, 48 | SET_FEATURE, 49 | RESERVED2, 50 | SET_ADDRESS, 51 | GET_DESCRIPTOR, 52 | SET_DESCRIPTOR, 53 | GET_CONFIGURATION, 54 | SET_CONFIGURATION, 55 | GET_INTERFACE, 56 | SET_INTERFACE, 57 | TOTAL_sREQUEST, /* Total number of Standard request */ 58 | SYNCH_FRAME = 12 59 | } STANDARD_REQUESTS; 60 | 61 | /* Definition of "USBwValue" */ 62 | typedef enum _DESCRIPTOR_TYPE 63 | { 64 | DEVICE_DESCRIPTOR = 1, 65 | CONFIG_DESCRIPTOR, 66 | STRING_DESCRIPTOR, 67 | INTERFACE_DESCRIPTOR, 68 | ENDPOINT_DESCRIPTOR 69 | } DESCRIPTOR_TYPE; 70 | 71 | /* Feature selector of a SET_FEATURE or CLEAR_FEATURE */ 72 | typedef enum _FEATURE_SELECTOR 73 | { 74 | ENDPOINT_STALL, 75 | DEVICE_REMOTE_WAKEUP 76 | } FEATURE_SELECTOR; 77 | 78 | /* Exported constants --------------------------------------------------------*/ 79 | /* Definition of "USBbmRequestType" */ 80 | #define REQUEST_TYPE 0x60 /* Mask to get request type */ 81 | #define STANDARD_REQUEST 0x00 /* Standard request */ 82 | #define CLASS_REQUEST 0x20 /* Class request */ 83 | #define VENDOR_REQUEST 0x40 /* Vendor request */ 84 | 85 | #define RECIPIENT 0x1F /* Mask to get recipient */ 86 | 87 | /* Exported macro ------------------------------------------------------------*/ 88 | /* Exported functions ------------------------------------------------------- */ 89 | 90 | #endif /* __USB_DEF_H */ 91 | 92 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 93 | -------------------------------------------------------------------------------- /USB/STM32_USB-FS-Device_Driver/src/usb_init.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_init.c 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 28-August-2012 7 | * @brief Initialization routines & global variables 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Includes ------------------------------------------------------------------*/ 30 | #include "usb_lib.h" 31 | 32 | /* Private typedef -----------------------------------------------------------*/ 33 | /* Private define ------------------------------------------------------------*/ 34 | /* Private macro -------------------------------------------------------------*/ 35 | /* Private variables ---------------------------------------------------------*/ 36 | /* The number of current endpoint, it will be used to specify an endpoint */ 37 | uint8_t EPindex; 38 | /* The number of current device, it is an index to the Device_Table */ 39 | /* uint8_t Device_no; */ 40 | /* Points to the DEVICE_INFO structure of current device */ 41 | /* The purpose of this register is to speed up the execution */ 42 | DEVICE_INFO *pInformation; 43 | /* Points to the DEVICE_PROP structure of current device */ 44 | /* The purpose of this register is to speed up the execution */ 45 | DEVICE_PROP *pProperty; 46 | /* Temporary save the state of Rx & Tx status. */ 47 | /* Whenever the Rx or Tx state is changed, its value is saved */ 48 | /* in this variable first and will be set to the EPRB or EPRA */ 49 | /* at the end of interrupt process */ 50 | uint16_t SaveState ; 51 | uint16_t wInterrupt_Mask; 52 | DEVICE_INFO Device_Info; 53 | USER_STANDARD_REQUESTS *pUser_Standard_Requests; 54 | 55 | /* Extern variables ----------------------------------------------------------*/ 56 | /* Private function prototypes -----------------------------------------------*/ 57 | /* Private functions ---------------------------------------------------------*/ 58 | 59 | /******************************************************************************* 60 | * Function Name : USB_Init 61 | * Description : USB system initialization 62 | * Input : None. 63 | * Output : None. 64 | * Return : None. 65 | *******************************************************************************/ 66 | void USB_Init(void) 67 | { 68 | pInformation = &Device_Info; 69 | pInformation->ControlState = 2; 70 | pProperty = &Device_Property; 71 | pUser_Standard_Requests = &User_Standard_Requests; 72 | /* Initialize devices one by one */ 73 | pProperty->Init(); 74 | } 75 | 76 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 77 | -------------------------------------------------------------------------------- /USB/CONFIG/usb_prop.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_prop.h 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 21-January-2013 7 | * @brief All processing related to Virtual COM Port Demo (Endpoint 0) 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2013 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __usb_prop_H 31 | #define __usb_prop_H 32 | 33 | /* Includes ------------------------------------------------------------------*/ 34 | /* Exported types ------------------------------------------------------------*/ 35 | typedef struct 36 | { 37 | uint32_t bitrate; 38 | uint8_t format; 39 | uint8_t paritytype; 40 | uint8_t datatype; 41 | }LINE_CODING; 42 | 43 | /* Exported constants --------------------------------------------------------*/ 44 | /* Exported macro ------------------------------------------------------------*/ 45 | /* Exported define -----------------------------------------------------------*/ 46 | 47 | #define Virtual_Com_Port_GetConfiguration NOP_Process 48 | //#define Virtual_Com_Port_SetConfiguration NOP_Process 49 | #define Virtual_Com_Port_GetInterface NOP_Process 50 | #define Virtual_Com_Port_SetInterface NOP_Process 51 | #define Virtual_Com_Port_GetStatus NOP_Process 52 | #define Virtual_Com_Port_ClearFeature NOP_Process 53 | #define Virtual_Com_Port_SetEndPointFeature NOP_Process 54 | #define Virtual_Com_Port_SetDeviceFeature NOP_Process 55 | //#define Virtual_Com_Port_SetDeviceAddress NOP_Process 56 | 57 | #define SEND_ENCAPSULATED_COMMAND 0x00 58 | #define GET_ENCAPSULATED_RESPONSE 0x01 59 | #define SET_COMM_FEATURE 0x02 60 | #define GET_COMM_FEATURE 0x03 61 | #define CLEAR_COMM_FEATURE 0x04 62 | #define SET_LINE_CODING 0x20 63 | #define GET_LINE_CODING 0x21 64 | #define SET_CONTROL_LINE_STATE 0x22 65 | #define SEND_BREAK 0x23 66 | 67 | /* Exported functions ------------------------------------------------------- */ 68 | void Virtual_Com_Port_init(void); 69 | void Virtual_Com_Port_Reset(void); 70 | void Virtual_Com_Port_SetConfiguration(void); 71 | void Virtual_Com_Port_SetDeviceAddress (void); 72 | void Virtual_Com_Port_Status_In (void); 73 | void Virtual_Com_Port_Status_Out (void); 74 | RESULT Virtual_Com_Port_Data_Setup(uint8_t); 75 | RESULT Virtual_Com_Port_NoData_Setup(uint8_t); 76 | RESULT Virtual_Com_Port_Get_Interface_Setting(uint8_t Interface, uint8_t AlternateSetting); 77 | uint8_t *Virtual_Com_Port_GetDeviceDescriptor(uint16_t ); 78 | uint8_t *Virtual_Com_Port_GetConfigDescriptor(uint16_t); 79 | uint8_t *Virtual_Com_Port_GetStringDescriptor(uint16_t); 80 | 81 | uint8_t *Virtual_Com_Port_GetLineCoding(uint16_t Length); 82 | uint8_t *Virtual_Com_Port_SetLineCoding(uint16_t Length); 83 | 84 | #endif /* __usb_prop_H */ 85 | 86 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 87 | 88 | -------------------------------------------------------------------------------- /USB/STM32_USB-FS-Device_Driver/src/usb_mem.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_mem.c 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 28-August-2012 7 | * @brief Utility functions for memory transfers to/from PMA 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "usb_lib.h" 30 | 31 | /* Private typedef -----------------------------------------------------------*/ 32 | /* Private define ------------------------------------------------------------*/ 33 | /* Private macro -------------------------------------------------------------*/ 34 | /* Private variables ---------------------------------------------------------*/ 35 | /* Extern variables ----------------------------------------------------------*/ 36 | /* Private function prototypes -----------------------------------------------*/ 37 | /* Private functions ---------------------------------------------------------*/ 38 | 39 | /******************************************************************************* 40 | * Function Name : UserToPMABufferCopy 41 | * Description : Copy a buffer from user memory area to packet memory area (PMA) 42 | * Input : - pbUsrBuf: pointer to user memory area. 43 | * - wPMABufAddr: address into PMA. 44 | * - wNBytes: no. of bytes to be copied. 45 | * Output : None. 46 | * Return : None . 47 | *******************************************************************************/ 48 | void UserToPMABufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes) 49 | { 50 | uint32_t n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */ 51 | uint32_t i, temp1, temp2; 52 | uint16_t *pdwVal; 53 | pdwVal = (uint16_t *)(wPMABufAddr * 2 + PMAAddr); 54 | for (i = n; i != 0; i--) 55 | { 56 | temp1 = (uint16_t) * pbUsrBuf; 57 | pbUsrBuf++; 58 | temp2 = temp1 | (uint16_t) * pbUsrBuf << 8; 59 | *pdwVal++ = temp2; 60 | pdwVal++; 61 | pbUsrBuf++; 62 | } 63 | } 64 | 65 | /******************************************************************************* 66 | * Function Name : PMAToUserBufferCopy 67 | * Description : Copy a buffer from user memory area to packet memory area (PMA) 68 | * Input : - pbUsrBuf = pointer to user memory area. 69 | * - wPMABufAddr = address into PMA. 70 | * - wNBytes = no. of bytes to be copied. 71 | * Output : None. 72 | * Return : None. 73 | *******************************************************************************/ 74 | void PMAToUserBufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes) 75 | { 76 | uint32_t n = (wNBytes + 1) >> 1;/* /2*/ 77 | uint32_t i; 78 | uint32_t *pdwVal; 79 | pdwVal = (uint32_t *)(wPMABufAddr * 2 + PMAAddr); 80 | for (i = n; i != 0; i--) 81 | { 82 | *(uint16_t*)pbUsrBuf++ = *pdwVal++; 83 | pbUsrBuf++; 84 | } 85 | } 86 | 87 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 88 | -------------------------------------------------------------------------------- /USB/CONFIG/usb_conf.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_conf.h 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 21-January-2013 7 | * @brief Virtual COM Port Demo configuration header 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2013 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __USB_CONF_H 31 | #define __USB_CONF_H 32 | 33 | /* Includes ------------------------------------------------------------------*/ 34 | /* Exported types ------------------------------------------------------------*/ 35 | /* Exported constants --------------------------------------------------------*/ 36 | /* Exported macro ------------------------------------------------------------*/ 37 | /* Exported functions ------------------------------------------------------- */ 38 | /* External variables --------------------------------------------------------*/ 39 | 40 | /*-------------------------------------------------------------*/ 41 | /* EP_NUM */ 42 | /* defines how many endpoints are used by the device */ 43 | /*-------------------------------------------------------------*/ 44 | 45 | #define EP_NUM (4) 46 | 47 | /*-------------------------------------------------------------*/ 48 | /* -------------- Buffer Description Table -----------------*/ 49 | /*-------------------------------------------------------------*/ 50 | /* buffer table base address */ 51 | /* buffer table base address */ 52 | #define BTABLE_ADDRESS (0x00) 53 | 54 | /* EP0 */ 55 | /* rx/tx buffer base address */ 56 | #define ENDP0_RXADDR (0x40) 57 | #define ENDP0_TXADDR (0x80) 58 | 59 | /* EP1 */ 60 | /* tx buffer base address */ 61 | #define ENDP1_TXADDR (0xC0) 62 | #define ENDP2_TXADDR (0x100) 63 | #define ENDP3_RXADDR (0x110) 64 | 65 | 66 | /*-------------------------------------------------------------*/ 67 | /* ------------------- ISTR events -------------------------*/ 68 | /*-------------------------------------------------------------*/ 69 | /* IMR_MSK */ 70 | /* mask defining which events has to be handled */ 71 | /* by the device application software */ 72 | #define IMR_MSK (CNTR_CTRM | CNTR_WKUPM | CNTR_SUSPM | CNTR_ERRM | CNTR_SOFM \ 73 | | CNTR_ESOFM | CNTR_RESETM ) 74 | 75 | /*#define CTR_CALLBACK*/ 76 | /*#define DOVR_CALLBACK*/ 77 | /*#define ERR_CALLBACK*/ 78 | /*#define WKUP_CALLBACK*/ 79 | /*#define SUSP_CALLBACK*/ 80 | /*#define RESET_CALLBACK*/ 81 | #define SOF_CALLBACK 82 | /*#define ESOF_CALLBACK*/ 83 | /* CTR service routines */ 84 | /* associated to defined endpoints */ 85 | /*#define EP1_IN_Callback NOP_Process*/ 86 | #define EP2_IN_Callback NOP_Process 87 | #define EP3_IN_Callback NOP_Process 88 | #define EP4_IN_Callback NOP_Process 89 | #define EP5_IN_Callback NOP_Process 90 | #define EP6_IN_Callback NOP_Process 91 | #define EP7_IN_Callback NOP_Process 92 | 93 | #define EP1_OUT_Callback NOP_Process 94 | #define EP2_OUT_Callback NOP_Process 95 | /*#define EP3_OUT_Callback NOP_Process*/ 96 | #define EP4_OUT_Callback NOP_Process 97 | #define EP5_OUT_Callback NOP_Process 98 | #define EP6_OUT_Callback NOP_Process 99 | #define EP7_OUT_Callback NOP_Process 100 | 101 | #endif /* __USB_CONF_H */ 102 | 103 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 104 | -------------------------------------------------------------------------------- /USB/STM32_USB-FS-Device_Driver/src/usb_sil.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_sil.c 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 28-August-2012 7 | * @brief Simplified Interface Layer for Global Initialization and Endpoint 8 | * Rea/Write operations. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | *

© COPYRIGHT 2012 STMicroelectronics

13 | * 14 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 15 | * You may not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at: 17 | * 18 | * http://www.st.com/software_license_agreement_liberty_v2 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, 22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | ****************************************************************************** 27 | */ 28 | 29 | 30 | /* Includes ------------------------------------------------------------------*/ 31 | #include "usb_lib.h" 32 | 33 | /* Private typedef -----------------------------------------------------------*/ 34 | /* Private define ------------------------------------------------------------*/ 35 | /* Private macro -------------------------------------------------------------*/ 36 | /* Private variables ---------------------------------------------------------*/ 37 | /* Extern variables ----------------------------------------------------------*/ 38 | /* Private function prototypes -----------------------------------------------*/ 39 | /* Private functions ---------------------------------------------------------*/ 40 | 41 | /******************************************************************************* 42 | * Function Name : USB_SIL_Init 43 | * Description : Initialize the USB Device IP and the Endpoint 0. 44 | * Input : None. 45 | * Output : None. 46 | * Return : Status. 47 | *******************************************************************************/ 48 | uint32_t USB_SIL_Init(void) 49 | { 50 | /* USB interrupts initialization */ 51 | /* clear pending interrupts */ 52 | _SetISTR(0); 53 | wInterrupt_Mask = IMR_MSK; 54 | /* set interrupts mask */ 55 | _SetCNTR(wInterrupt_Mask); 56 | return 0; 57 | } 58 | 59 | /******************************************************************************* 60 | * Function Name : USB_SIL_Write 61 | * Description : Write a buffer of data to a selected endpoint. 62 | * Input : - bEpAddr: The address of the non control endpoint. 63 | * - pBufferPointer: The pointer to the buffer of data to be written 64 | * to the endpoint. 65 | * - wBufferSize: Number of data to be written (in bytes). 66 | * Output : None. 67 | * Return : Status. 68 | *******************************************************************************/ 69 | uint32_t USB_SIL_Write(uint8_t bEpAddr, uint8_t* pBufferPointer, uint32_t wBufferSize) 70 | { 71 | /* Use the memory interface function to write to the selected endpoint */ 72 | UserToPMABufferCopy(pBufferPointer, GetEPTxAddr(bEpAddr & 0x7F), wBufferSize); 73 | 74 | /* Update the data length in the control register */ 75 | SetEPTxCount((bEpAddr & 0x7F), wBufferSize); 76 | 77 | return 0; 78 | } 79 | 80 | /******************************************************************************* 81 | * Function Name : USB_SIL_Read 82 | * Description : Write a buffer of data to a selected endpoint. 83 | * Input : - bEpAddr: The address of the non control endpoint. 84 | * - pBufferPointer: The pointer to which will be saved the 85 | * received data buffer. 86 | * Output : None. 87 | * Return : Number of received data (in Bytes). 88 | *******************************************************************************/ 89 | uint32_t USB_SIL_Read(uint8_t bEpAddr, uint8_t* pBufferPointer) 90 | { 91 | uint32_t DataLength = 0; 92 | 93 | /* Get the number of received data on the selected Endpoint */ 94 | DataLength = GetEPRxCount(bEpAddr & 0x7F); 95 | 96 | /* Use the memory interface function to write to the selected endpoint */ 97 | PMAToUserBufferCopy(pBufferPointer, GetEPRxAddr(bEpAddr & 0x7F), DataLength); 98 | 99 | /* Return the number of received data */ 100 | return DataLength; 101 | } 102 | 103 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 104 | -------------------------------------------------------------------------------- /User/stm32f10x_it.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 08-April-2011 7 | * @brief Main Interrupt Service Routines. 8 | * This file provides template for all exceptions handler and 9 | * peripherals interrupt service routine. 10 | ****************************************************************************** 11 | * @attention 12 | * 13 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 14 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 15 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 16 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 17 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 18 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 19 | * 20 | *

© COPYRIGHT 2011 STMicroelectronics

21 | ****************************************************************************** 22 | */ 23 | 24 | /* Includes ------------------------------------------------------------------*/ 25 | #include "stm32f10x_it.h" 26 | 27 | 28 | /** @addtogroup STM32F10x_StdPeriph_Template 29 | * @{ 30 | */ 31 | 32 | /* Private typedef -----------------------------------------------------------*/ 33 | /* Private define ------------------------------------------------------------*/ 34 | /* Private macro -------------------------------------------------------------*/ 35 | /* Private variables ---------------------------------------------------------*/ 36 | /* Private function prototypes -----------------------------------------------*/ 37 | /* Private functions ---------------------------------------------------------*/ 38 | 39 | /******************************************************************************/ 40 | /* Cortex-M3 Processor Exceptions Handlers */ 41 | /******************************************************************************/ 42 | 43 | /** 44 | * @brief This function handles NMI exception. 45 | * @param None 46 | * @retval None 47 | */ 48 | void NMI_Handler(void) 49 | { 50 | } 51 | 52 | /** 53 | * @brief This function handles Hard Fault exception. 54 | * @param None 55 | * @retval None 56 | */ 57 | //void HardFault_Handler(void) 58 | //{ 59 | // /* Go to infinite loop when Hard Fault exception occurs */ 60 | // while (1) 61 | // { 62 | // } 63 | //} 64 | 65 | /** 66 | * @brief This function handles Memory Manage exception. 67 | * @param None 68 | * @retval None 69 | */ 70 | void MemManage_Handler(void) 71 | { 72 | /* Go to infinite loop when Memory Manage exception occurs */ 73 | while (1) 74 | { 75 | } 76 | } 77 | 78 | /** 79 | * @brief This function handles Bus Fault exception. 80 | * @param None 81 | * @retval None 82 | */ 83 | void BusFault_Handler(void) 84 | { 85 | /* Go to infinite loop when Bus Fault exception occurs */ 86 | while (1) 87 | { 88 | } 89 | } 90 | 91 | /** 92 | * @brief This function handles Usage Fault exception. 93 | * @param None 94 | * @retval None 95 | */ 96 | void UsageFault_Handler(void) 97 | { 98 | /* Go to infinite loop when Usage Fault exception occurs */ 99 | while (1) 100 | { 101 | } 102 | } 103 | 104 | /** 105 | * @brief This function handles SVCall exception. 106 | * @param None 107 | * @retval None 108 | */ 109 | void SVC_Handler(void) 110 | { 111 | } 112 | 113 | /** 114 | * @brief This function handles Debug Monitor exception. 115 | * @param None 116 | * @retval None 117 | */ 118 | void DebugMon_Handler(void) 119 | { 120 | } 121 | 122 | /** 123 | * @brief This function handles PendSVC exception. 124 | * @param None 125 | * @retval None 126 | */ 127 | void PendSV_Handler(void) 128 | { 129 | } 130 | 131 | /** 132 | * @brief This function handles SysTick Handler. 133 | * @param None 134 | * @retval None 135 | */ 136 | void SysTick_Handler(void) 137 | { 138 | } 139 | 140 | 141 | 142 | 143 | /******************************************************************************/ 144 | /* STM32F10x Peripherals Interrupt Handlers */ 145 | /* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */ 146 | /* available peripheral interrupt handler's name please refer to the startup */ 147 | /* file (startup_stm32f10x_xx.s). */ 148 | /******************************************************************************/ 149 | 150 | /** 151 | * @brief This function handles PPP interrupt request. 152 | * @param None 153 | * @retval None 154 | */ 155 | /*void PPP_IRQHandler(void) 156 | { 157 | }*/ 158 | 159 | /** 160 | * @} 161 | */ 162 | 163 | 164 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 165 | -------------------------------------------------------------------------------- /APP/mpu6050/iompu6050.h: -------------------------------------------------------------------------------- 1 | #ifndef __IOMPU6050_H 2 | #define __IOMPU6050_H 3 | 4 | #include "system.h" 5 | 6 | 7 | //使用IIC PB10,PB11 8 | 9 | //#define SDA_IN() {GPIOB->CRH&=0XFFFF0FFF;GPIOB->CRH|=0x00008000;} 10 | //#define SDA_OUT() {GPIOB->CRH&=0XFFFF0FFF;GPIOB->CRH|=0x00007000;} //7为开漏,推挽为3 11 | 12 | //IO操作函数 13 | #define IIC_SCL PAout(2) //SCL 14 | #define IIC_SDA PAout(3) //SDA 15 | //#define READ_SDA PBin(11) //输入SDA 16 | #define READ_SDA ((GPIOA->IDR & GPIO_Pin_3) != 0) /* 读SDA口线状态 */ 17 | 18 | 19 | 20 | 21 | //**************************************** 22 | // 定义MPU6050内部地址 23 | //**************************************** 24 | //#define MPU6050_ACCEL_OFFS_REG 0X06 //accel_offs寄存器,可读取版本号,寄存器手册未提到 25 | //#define MPU6050_PROD_ID_REG 0X0C //prod id寄存器,在寄存器手册未提到 26 | #define MPU6050_SELF_TESTX_REG 0X0D //自检寄存器X 27 | #define MPU6050_SELF_TESTY_REG 0X0E //自检寄存器Y 28 | #define MPU6050_SELF_TESTZ_REG 0X0F //自检寄存器Z 29 | #define MPU6050_SELF_TESTA_REG 0X10 //自检寄存器A 30 | #define MPU6050_SAMPLE_RATE_REG 0X19 //采样频率分频器 31 | #define MPU6050_CFG_REG 0X1A //配置寄存器 32 | #define MPU6050_GYRO_CFG_REG 0X1B //陀螺仪配置寄存器 33 | #define MPU6050_ACCEL_CFG_REG 0X1C //加速度计配置寄存器 34 | #define MPU6050_MOTION_DET_REG 0X1F //运动检测阀值设置寄存器 35 | #define MPU6050_FIFO_EN_REG 0X23 //FIFO使能寄存器 36 | #define MPU6050_I2CMST_CTRL_REG 0X24 //IIC主机控制寄存器 37 | #define MPU6050_I2CSLV0_ADDR_REG 0X25 //IIC从机0器件地址寄存器 38 | #define MPU6050_I2CSLV0_REG 0X26 //IIC从机0数据地址寄存器 39 | #define MPU6050_I2CSLV0_CTRL_REG 0X27 //IIC从机0控制寄存器 40 | #define MPU6050_I2CSLV1_ADDR_REG 0X28 //IIC从机1器件地址寄存器 41 | #define MPU6050_I2CSLV1_REG 0X29 //IIC从机1数据地址寄存器 42 | #define MPU6050_I2CSLV1_CTRL_REG 0X2A //IIC从机1控制寄存器 43 | #define MPU6050_I2CSLV2_ADDR_REG 0X2B //IIC从机2器件地址寄存器 44 | #define MPU6050_I2CSLV2_REG 0X2C //IIC从机2数据地址寄存器 45 | #define MPU6050_I2CSLV2_CTRL_REG 0X2D //IIC从机2控制寄存器 46 | #define MPU6050_I2CSLV3_ADDR_REG 0X2E //IIC从机3器件地址寄存器 47 | #define MPU6050_I2CSLV3_REG 0X2F //IIC从机3数据地址寄存器 48 | #define MPU6050_I2CSLV3_CTRL_REG 0X30 //IIC从机3控制寄存器 49 | #define MPU6050_I2CSLV4_ADDR_REG 0X31 //IIC从机4器件地址寄存器 50 | #define MPU6050_I2CSLV4_REG 0X32 //IIC从机4数据地址寄存器 51 | #define MPU6050_I2CSLV4_DO_REG 0X33 //IIC从机4写数据寄存器 52 | #define MPU6050_I2CSLV4_CTRL_REG 0X34 //IIC从机4控制寄存器 53 | #define MPU6050_I2CSLV4_DI_REG 0X35 //IIC从机4读数据寄存器 54 | 55 | #define MPU6050_I2CMST_STA_REG 0X36 //IIC主机状态寄存器 56 | #define MPU6050_INTBP_CFG_REG 0X37 //中断/旁路设置寄存器 57 | #define MPU6050_INT_EN_REG 0X38 //中断使能寄存器 58 | #define MPU6050_INT_STA_REG 0X3A //中断状态寄存器 59 | 60 | #define MPU6050_ACCEL_XOUTH_REG 0X3B //加速度值,X轴高8位寄存器 61 | #define MPU6050_ACCEL_XOUTL_REG 0X3C //加速度值,X轴低8位寄存器 62 | #define MPU6050_ACCEL_YOUTH_REG 0X3D //加速度值,Y轴高8位寄存器 63 | #define MPU6050_ACCEL_YOUTL_REG 0X3E //加速度值,Y轴低8位寄存器 64 | #define MPU6050_ACCEL_ZOUTH_REG 0X3F //加速度值,Z轴高8位寄存器 65 | #define MPU6050_ACCEL_ZOUTL_REG 0X40 //加速度值,Z轴低8位寄存器 66 | 67 | #define MPU6050_TEMP_OUTH_REG 0X41 //温度值高八位寄存器 68 | #define MPU6050_TEMP_OUTL_REG 0X42 //温度值低8位寄存器 69 | 70 | #define MPU6050_GYRO_XOUTH_REG 0X43 //陀螺仪值,X轴高8位寄存器 71 | #define MPU6050_GYRO_XOUTL_REG 0X44 //陀螺仪值,X轴低8位寄存器 72 | #define MPU6050_GYRO_YOUTH_REG 0X45 //陀螺仪值,Y轴高8位寄存器 73 | #define MPU6050_GYRO_YOUTL_REG 0X46 //陀螺仪值,Y轴低8位寄存器 74 | #define MPU6050_GYRO_ZOUTH_REG 0X47 //陀螺仪值,Z轴高8位寄存器 75 | #define MPU6050_GYRO_ZOUTL_REG 0X48 //陀螺仪值,Z轴低8位寄存器 76 | 77 | #define MPU6050_I2CSLV0_DO_REG 0X63 //IIC从机0数据寄存器 78 | #define MPU6050_I2CSLV1_DO_REG 0X64 //IIC从机1数据寄存器 79 | #define MPU6050_I2CSLV2_DO_REG 0X65 //IIC从机2数据寄存器 80 | #define MPU6050_I2CSLV3_DO_REG 0X66 //IIC从机3数据寄存器 81 | 82 | #define MPU6050_I2CMST_DELAY_REG 0X67 //IIC主机延时管理寄存器 83 | #define MPU6050_SIGPATH_RST_REG 0X68 //信号通道复位寄存器 84 | #define MPU6050_MDETECT_CTRL_REG 0X69 //运动检测控制寄存器 85 | #define MPU6050_USER_CTRL_REG 0X6A //用户控制寄存器 86 | #define MPU6050_PWR_MGMT1_REG 0X6B //电源管理寄存器1 87 | #define MPU6050_PWR_MGMT2_REG 0X6C //电源管理寄存器2 88 | #define MPU6050_FIFO_CNTH_REG 0X72 //FIFO计数寄存器高八位 89 | #define MPU6050_FIFO_CNTL_REG 0X73 //FIFO计数寄存器低八位 90 | #define MPU6050_FIFO_RW_REG 0X74 //FIFO读写寄存器 91 | #define MPU6050_DEVICE_ID_REG 0X75 //器件ID寄存器 92 | 93 | //如果AD0脚(9脚)接地,IIC地址为0X68(不包含最低位). 94 | //如果接V3.3,则IIC地址为0X69(不包含最低位). 95 | //#define MPU6050_ADDR 0X68 96 | #define MPU6050_ADDR 0XD0 97 | 98 | ////因为开发板接GND,所以转为读写地址后,为0XD1和0XD0(如果接V3.3,则为0XD3和0XD2) 99 | //#define MPU6050_READ 0XD1 100 | //#define MPU6050_WRITE 0XD0 101 | 102 | //IIC所有操作函数 103 | void IIC_Init(void); //初始化IIC的IO口 104 | void IIC_Start(void); //发送IIC开始信号 105 | void IIC_Stop(void); //发送IIC停止信号 106 | void IIC_Send_Byte(u8 txd); //IIC发送一个字节 107 | u8 IIC_Read_Byte(unsigned char ack);//IIC读取一个字节 108 | u8 IIC_Wait_Ack(void); //IIC等待ACK信号 109 | void IIC_Ack(void); //IIC发送ACK信号 110 | void IIC_NAck(void); //IIC不发送ACK信号 111 | 112 | u8 I2C_WriteByte(u8 addr, u8 data, u8 device_addr); 113 | u8 I2C_ReadByte(u8 addr, u8 device_addr); 114 | u8 I2C_WriteBytes(u8 addr,u8 len,u8 *buf,u8 device_addr); 115 | u8 I2C_ReadBytes(u8 addr,u8 len,u8 *buf,u8 device_addr); 116 | 117 | u8 IOMPU6050_Init(void); //初始化MPU6050 118 | u8 MPU6050_Write_Len(u8 reg,u8 len,u8 *buf);//IIC连续写 119 | u8 MPU6050_Read_Len(u8 reg,u8 len,u8 *buf); //IIC连续读 120 | u8 MPU6050_Write_Byte(u8 reg,u8 data); //IIC写一个字节 121 | u8 MPU6050_Read_Byte(u8 reg); //IIC读一个字节 122 | 123 | u8 MPU6050_Set_Gyro_Fsr(u8 fsr); 124 | u8 MPU6050_Set_Accel_Fsr(u8 fsr); 125 | u8 MPU6050_Set_LPF(u16 lpf); 126 | u8 MPU6050_Set_Rate(u16 rate); 127 | u8 MPU6050_Set_Fifo(u8 sens); 128 | 129 | short MPU6050_Get_Temperature(void); 130 | u8 MPU6050_Get_Gyroscope(short *gx,short *gy,short *gz); 131 | u8 MPU6050_Get_Accelerometer(short *ax,short *ay,short *az); 132 | 133 | #endif 134 | -------------------------------------------------------------------------------- /USB/STM32_USB-FS-Device_Driver/src/usb_int.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_int.c 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 28-August-2012 7 | * @brief Endpoint CTR (Low and High) interrupt's service routines 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "usb_lib.h" 30 | 31 | /* Private typedef -----------------------------------------------------------*/ 32 | /* Private define ------------------------------------------------------------*/ 33 | /* Private macro -------------------------------------------------------------*/ 34 | /* Private variables ---------------------------------------------------------*/ 35 | __IO uint16_t SaveRState; 36 | __IO uint16_t SaveTState; 37 | 38 | /* Extern variables ----------------------------------------------------------*/ 39 | extern void (*pEpInt_IN[7])(void); /* Handles IN interrupts */ 40 | extern void (*pEpInt_OUT[7])(void); /* Handles OUT interrupts */ 41 | 42 | /* Private function prototypes -----------------------------------------------*/ 43 | /* Private functions ---------------------------------------------------------*/ 44 | 45 | /******************************************************************************* 46 | * Function Name : CTR_LP. 47 | * Description : Low priority Endpoint Correct Transfer interrupt's service 48 | * routine. 49 | * Input : None. 50 | * Output : None. 51 | * Return : None. 52 | *******************************************************************************/ 53 | void CTR_LP(void) 54 | { 55 | __IO uint16_t wEPVal = 0; 56 | /* stay in loop while pending interrupts */ 57 | while (((wIstr = _GetISTR()) & ISTR_CTR) != 0) 58 | { 59 | /* extract highest priority endpoint number */ 60 | EPindex = (uint8_t)(wIstr & ISTR_EP_ID); 61 | if (EPindex == 0) 62 | { 63 | /* Decode and service control endpoint interrupt */ 64 | /* calling related service routine */ 65 | /* (Setup0_Process, In0_Process, Out0_Process) */ 66 | 67 | /* save RX & TX status */ 68 | /* and set both to NAK */ 69 | 70 | SaveRState = _GetENDPOINT(ENDP0); 71 | SaveTState = SaveRState & EPTX_STAT; 72 | SaveRState &= EPRX_STAT; 73 | 74 | _SetEPRxTxStatus(ENDP0,EP_RX_NAK,EP_TX_NAK); 75 | 76 | /* DIR bit = origin of the interrupt */ 77 | 78 | if ((wIstr & ISTR_DIR) == 0) 79 | { 80 | /* DIR = 0 */ 81 | 82 | /* DIR = 0 => IN int */ 83 | /* DIR = 0 implies that (EP_CTR_TX = 1) always */ 84 | 85 | _ClearEP_CTR_TX(ENDP0); 86 | In0_Process(); 87 | 88 | /* before terminate set Tx & Rx status */ 89 | 90 | _SetEPRxTxStatus(ENDP0,SaveRState,SaveTState); 91 | return; 92 | } 93 | else 94 | { 95 | /* DIR = 1 */ 96 | 97 | /* DIR = 1 & CTR_RX => SETUP or OUT int */ 98 | /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */ 99 | 100 | wEPVal = _GetENDPOINT(ENDP0); 101 | 102 | if ((wEPVal &EP_SETUP) != 0) 103 | { 104 | _ClearEP_CTR_RX(ENDP0); /* SETUP bit kept frozen while CTR_RX = 1 */ 105 | Setup0_Process(); 106 | /* before terminate set Tx & Rx status */ 107 | 108 | _SetEPRxTxStatus(ENDP0,SaveRState,SaveTState); 109 | return; 110 | } 111 | 112 | else if ((wEPVal & EP_CTR_RX) != 0) 113 | { 114 | _ClearEP_CTR_RX(ENDP0); 115 | Out0_Process(); 116 | /* before terminate set Tx & Rx status */ 117 | 118 | _SetEPRxTxStatus(ENDP0,SaveRState,SaveTState); 119 | return; 120 | } 121 | } 122 | }/* if(EPindex == 0) */ 123 | else 124 | { 125 | /* Decode and service non control endpoints interrupt */ 126 | 127 | /* process related endpoint register */ 128 | wEPVal = _GetENDPOINT(EPindex); 129 | if ((wEPVal & EP_CTR_RX) != 0) 130 | { 131 | /* clear int flag */ 132 | _ClearEP_CTR_RX(EPindex); 133 | 134 | /* call OUT service function */ 135 | (*pEpInt_OUT[EPindex-1])(); 136 | 137 | } /* if((wEPVal & EP_CTR_RX) */ 138 | 139 | if ((wEPVal & EP_CTR_TX) != 0) 140 | { 141 | /* clear int flag */ 142 | _ClearEP_CTR_TX(EPindex); 143 | 144 | /* call IN service function */ 145 | (*pEpInt_IN[EPindex-1])(); 146 | } /* if((wEPVal & EP_CTR_TX) != 0) */ 147 | 148 | }/* if(EPindex == 0) else */ 149 | 150 | }/* while(...) */ 151 | } 152 | 153 | /******************************************************************************* 154 | * Function Name : CTR_HP. 155 | * Description : High Priority Endpoint Correct Transfer interrupt's service 156 | * routine. 157 | * Input : None. 158 | * Output : None. 159 | * Return : None. 160 | *******************************************************************************/ 161 | void CTR_HP(void) 162 | { 163 | uint32_t wEPVal = 0; 164 | 165 | while (((wIstr = _GetISTR()) & ISTR_CTR) != 0) 166 | { 167 | _SetISTR((uint16_t)CLR_CTR); /* clear CTR flag */ 168 | /* extract highest priority endpoint number */ 169 | EPindex = (uint8_t)(wIstr & ISTR_EP_ID); 170 | /* process related endpoint register */ 171 | wEPVal = _GetENDPOINT(EPindex); 172 | if ((wEPVal & EP_CTR_RX) != 0) 173 | { 174 | /* clear int flag */ 175 | _ClearEP_CTR_RX(EPindex); 176 | 177 | /* call OUT service function */ 178 | (*pEpInt_OUT[EPindex-1])(); 179 | 180 | } /* if((wEPVal & EP_CTR_RX) */ 181 | else if ((wEPVal & EP_CTR_TX) != 0) 182 | { 183 | /* clear int flag */ 184 | _ClearEP_CTR_TX(EPindex); 185 | 186 | /* call IN service function */ 187 | (*pEpInt_IN[EPindex-1])(); 188 | 189 | 190 | } /* if((wEPVal & EP_CTR_TX) != 0) */ 191 | 192 | }/* while(...) */ 193 | } 194 | 195 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 196 | -------------------------------------------------------------------------------- /Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_exti.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_exti.h 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief This file contains all the functions prototypes for the EXTI firmware 8 | * library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 13 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 14 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 15 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 16 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 17 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 18 | * 19 | *

© COPYRIGHT 2011 STMicroelectronics

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

© COPYRIGHT 2013 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Includes ------------------------------------------------------------------*/ 30 | #include "usb_lib.h" 31 | #include "usb_prop.h" 32 | #include "usb_pwr.h" 33 | #include "usb_istr.h" 34 | 35 | /* Private typedef -----------------------------------------------------------*/ 36 | /* Private define ------------------------------------------------------------*/ 37 | /* Private macro -------------------------------------------------------------*/ 38 | /* Private variables ---------------------------------------------------------*/ 39 | __IO uint16_t wIstr; /* ISTR register last read value */ 40 | __IO uint8_t bIntPackSOF = 0; /* SOFs received between 2 consecutive packets */ 41 | __IO uint32_t esof_counter =0; /* expected SOF counter */ 42 | __IO uint32_t wCNTR=0; 43 | 44 | /* Extern variables ----------------------------------------------------------*/ 45 | /* Private function prototypes -----------------------------------------------*/ 46 | /* Private functions ---------------------------------------------------------*/ 47 | /* function pointers to non-control endpoints service routines */ 48 | void (*pEpInt_IN[7])(void) = 49 | { 50 | EP1_IN_Callback, 51 | EP2_IN_Callback, 52 | EP3_IN_Callback, 53 | EP4_IN_Callback, 54 | EP5_IN_Callback, 55 | EP6_IN_Callback, 56 | EP7_IN_Callback, 57 | }; 58 | 59 | void (*pEpInt_OUT[7])(void) = 60 | { 61 | EP1_OUT_Callback, 62 | EP2_OUT_Callback, 63 | EP3_OUT_Callback, 64 | EP4_OUT_Callback, 65 | EP5_OUT_Callback, 66 | EP6_OUT_Callback, 67 | EP7_OUT_Callback, 68 | }; 69 | 70 | /******************************************************************************* 71 | * Function Name : USB_Istr 72 | * Description : ISTR events interrupt service routine 73 | * Input : None. 74 | * Output : None. 75 | * Return : None. 76 | *******************************************************************************/ 77 | void USB_Istr(void) 78 | { 79 | uint32_t i=0; 80 | __IO uint32_t EP[8]; 81 | 82 | wIstr = _GetISTR(); 83 | 84 | #if (IMR_MSK & ISTR_SOF) 85 | if (wIstr & ISTR_SOF & wInterrupt_Mask) 86 | { 87 | _SetISTR((uint16_t)CLR_SOF); 88 | bIntPackSOF++; 89 | 90 | #ifdef SOF_CALLBACK 91 | SOF_Callback(); 92 | #endif 93 | } 94 | #endif 95 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 96 | 97 | #if (IMR_MSK & ISTR_CTR) 98 | if (wIstr & ISTR_CTR & wInterrupt_Mask) 99 | { 100 | /* servicing of the endpoint correct transfer interrupt */ 101 | /* clear of the CTR flag into the sub */ 102 | CTR_LP(); 103 | #ifdef CTR_CALLBACK 104 | CTR_Callback(); 105 | #endif 106 | } 107 | #endif 108 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 109 | #if (IMR_MSK & ISTR_RESET) 110 | if (wIstr & ISTR_RESET & wInterrupt_Mask) 111 | { 112 | _SetISTR((uint16_t)CLR_RESET); 113 | Device_Property.Reset(); 114 | #ifdef RESET_CALLBACK 115 | RESET_Callback(); 116 | #endif 117 | } 118 | #endif 119 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 120 | #if (IMR_MSK & ISTR_DOVR) 121 | if (wIstr & ISTR_DOVR & wInterrupt_Mask) 122 | { 123 | _SetISTR((uint16_t)CLR_DOVR); 124 | #ifdef DOVR_CALLBACK 125 | DOVR_Callback(); 126 | #endif 127 | } 128 | #endif 129 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 130 | #if (IMR_MSK & ISTR_ERR) 131 | if (wIstr & ISTR_ERR & wInterrupt_Mask) 132 | { 133 | _SetISTR((uint16_t)CLR_ERR); 134 | #ifdef ERR_CALLBACK 135 | ERR_Callback(); 136 | #endif 137 | } 138 | #endif 139 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 140 | #if (IMR_MSK & ISTR_WKUP) 141 | if (wIstr & ISTR_WKUP & wInterrupt_Mask) 142 | { 143 | _SetISTR((uint16_t)CLR_WKUP); 144 | Resume(RESUME_EXTERNAL); 145 | #ifdef WKUP_CALLBACK 146 | WKUP_Callback(); 147 | #endif 148 | } 149 | #endif 150 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 151 | #if (IMR_MSK & ISTR_SUSP) 152 | if (wIstr & ISTR_SUSP & wInterrupt_Mask) 153 | { 154 | 155 | /* check if SUSPEND is possible */ 156 | if (fSuspendEnabled) 157 | { 158 | Suspend(); 159 | } 160 | else 161 | { 162 | /* if not possible then resume after xx ms */ 163 | Resume(RESUME_LATER); 164 | } 165 | /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */ 166 | _SetISTR((uint16_t)CLR_SUSP); 167 | #ifdef SUSP_CALLBACK 168 | SUSP_Callback(); 169 | #endif 170 | } 171 | #endif 172 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 173 | 174 | #if (IMR_MSK & ISTR_ESOF) 175 | if (wIstr & ISTR_ESOF & wInterrupt_Mask) 176 | { 177 | /* clear ESOF flag in ISTR */ 178 | _SetISTR((uint16_t)CLR_ESOF); 179 | 180 | if ((_GetFNR()&FNR_RXDP)!=0) 181 | { 182 | /* increment ESOF counter */ 183 | esof_counter ++; 184 | 185 | /* test if we enter in ESOF more than 3 times with FSUSP =0 and RXDP =1=>> possible missing SUSP flag*/ 186 | if ((esof_counter >3)&&((_GetCNTR()&CNTR_FSUSP)==0)) 187 | { 188 | /* this a sequence to apply a force RESET*/ 189 | 190 | /*Store CNTR value */ 191 | wCNTR = _GetCNTR(); 192 | 193 | /*Store endpoints registers status */ 194 | for (i=0;i<8;i++) EP[i] = _GetENDPOINT(i); 195 | 196 | /*apply FRES */ 197 | wCNTR|=CNTR_FRES; 198 | _SetCNTR(wCNTR); 199 | 200 | /*clear FRES*/ 201 | wCNTR&=~CNTR_FRES; 202 | _SetCNTR(wCNTR); 203 | 204 | /*poll for RESET flag in ISTR*/ 205 | while((_GetISTR()&ISTR_RESET) == 0); 206 | 207 | /* clear RESET flag in ISTR */ 208 | _SetISTR((uint16_t)CLR_RESET); 209 | 210 | /*restore Enpoints*/ 211 | for (i=0;i<8;i++) 212 | _SetENDPOINT(i, EP[i]); 213 | 214 | esof_counter = 0; 215 | } 216 | } 217 | else 218 | { 219 | esof_counter = 0; 220 | } 221 | 222 | /* resume handling timing is made with ESOFs */ 223 | Resume(RESUME_ESOF); /* request without change of the machine state */ 224 | 225 | #ifdef ESOF_CALLBACK 226 | ESOF_Callback(); 227 | #endif 228 | } 229 | #endif 230 | } /* USB_Istr */ 231 | 232 | 233 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 234 | -------------------------------------------------------------------------------- /Libraries/STM32F10x_StdPeriph_Driver/src/misc.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file misc.c 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief This file provides all the miscellaneous firmware functions (add-on 8 | * to CMSIS functions). 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 13 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 14 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 15 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 16 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 17 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 18 | * 19 | *

© COPYRIGHT 2011 STMicroelectronics

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

© COPYRIGHT 2013 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Includes ------------------------------------------------------------------*/ 30 | #include "usb_lib.h" 31 | #include "usb_desc.h" 32 | 33 | /* USB Standard Device Descriptor */ 34 | const uint8_t Virtual_Com_Port_DeviceDescriptor[] = 35 | { 36 | 0x12, /* bLength */ 37 | USB_DEVICE_DESCRIPTOR_TYPE, /* bDescriptorType */ 38 | 0x00, 39 | 0x02, /* bcdUSB = 2.00 */ 40 | 0x02, /* bDeviceClass: CDC */ 41 | 0x00, /* bDeviceSubClass */ 42 | 0x00, /* bDeviceProtocol */ 43 | 0x40, /* bMaxPacketSize0 */ 44 | 0x83, 45 | 0x04, /* idVendor = 0x0483 */ 46 | 0x40, 47 | 0x57, /* idProduct = 0x7540 */ 48 | 0x00, 49 | 0x02, /* bcdDevice = 2.00 */ 50 | 1, /* Index of string descriptor describing manufacturer */ 51 | 2, /* Index of string descriptor describing product */ 52 | 3, /* Index of string descriptor describing the device's serial number */ 53 | 0x01 /* bNumConfigurations */ 54 | }; 55 | 56 | const uint8_t Virtual_Com_Port_ConfigDescriptor[] = 57 | { 58 | /*Configuration Descriptor*/ 59 | 0x09, /* bLength: Configuration Descriptor size */ 60 | USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */ 61 | VIRTUAL_COM_PORT_SIZ_CONFIG_DESC, /* wTotalLength:no of returned bytes */ 62 | 0x00, 63 | 0x02, /* bNumInterfaces: 2 interface */ 64 | 0x01, /* bConfigurationValue: Configuration value */ 65 | 0x00, /* iConfiguration: Index of string descriptor describing the configuration */ 66 | 0xC0, /* bmAttributes: self powered */ 67 | 0x32, /* MaxPower 0 mA */ 68 | /*Interface Descriptor*/ 69 | 0x09, /* bLength: Interface Descriptor size */ 70 | USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType: Interface */ 71 | /* Interface descriptor type */ 72 | 0x00, /* bInterfaceNumber: Number of Interface */ 73 | 0x00, /* bAlternateSetting: Alternate setting */ 74 | 0x01, /* bNumEndpoints: One endpoints used */ 75 | 0x02, /* bInterfaceClass: Communication Interface Class */ 76 | 0x02, /* bInterfaceSubClass: Abstract Control Model */ 77 | 0x01, /* bInterfaceProtocol: Common AT commands */ 78 | 0x00, /* iInterface: */ 79 | /*Header Functional Descriptor*/ 80 | 0x05, /* bLength: Endpoint Descriptor size */ 81 | 0x24, /* bDescriptorType: CS_INTERFACE */ 82 | 0x00, /* bDescriptorSubtype: Header Func Desc */ 83 | 0x10, /* bcdCDC: spec release number */ 84 | 0x01, 85 | /*Call Management Functional Descriptor*/ 86 | 0x05, /* bFunctionLength */ 87 | 0x24, /* bDescriptorType: CS_INTERFACE */ 88 | 0x01, /* bDescriptorSubtype: Call Management Func Desc */ 89 | 0x00, /* bmCapabilities: D0+D1 */ 90 | 0x01, /* bDataInterface: 1 */ 91 | /*ACM Functional Descriptor*/ 92 | 0x04, /* bFunctionLength */ 93 | 0x24, /* bDescriptorType: CS_INTERFACE */ 94 | 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ 95 | 0x02, /* bmCapabilities */ 96 | /*Union Functional Descriptor*/ 97 | 0x05, /* bFunctionLength */ 98 | 0x24, /* bDescriptorType: CS_INTERFACE */ 99 | 0x06, /* bDescriptorSubtype: Union func desc */ 100 | 0x00, /* bMasterInterface: Communication class interface */ 101 | 0x01, /* bSlaveInterface0: Data Class Interface */ 102 | /*Endpoint 2 Descriptor*/ 103 | 0x07, /* bLength: Endpoint Descriptor size */ 104 | USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */ 105 | 0x82, /* bEndpointAddress: (IN2) */ 106 | 0x03, /* bmAttributes: Interrupt */ 107 | VIRTUAL_COM_PORT_INT_SIZE, /* wMaxPacketSize: */ 108 | 0x00, 109 | 0xFF, /* bInterval: */ 110 | /*Data class interface descriptor*/ 111 | 0x09, /* bLength: Endpoint Descriptor size */ 112 | USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType: */ 113 | 0x01, /* bInterfaceNumber: Number of Interface */ 114 | 0x00, /* bAlternateSetting: Alternate setting */ 115 | 0x02, /* bNumEndpoints: Two endpoints used */ 116 | 0x0A, /* bInterfaceClass: CDC */ 117 | 0x00, /* bInterfaceSubClass: */ 118 | 0x00, /* bInterfaceProtocol: */ 119 | 0x00, /* iInterface: */ 120 | /*Endpoint 3 Descriptor*/ 121 | 0x07, /* bLength: Endpoint Descriptor size */ 122 | USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */ 123 | 0x03, /* bEndpointAddress: (OUT3) */ 124 | 0x02, /* bmAttributes: Bulk */ 125 | VIRTUAL_COM_PORT_DATA_SIZE, /* wMaxPacketSize: */ 126 | 0x00, 127 | 0x00, /* bInterval: ignore for Bulk transfer */ 128 | /*Endpoint 1 Descriptor*/ 129 | 0x07, /* bLength: Endpoint Descriptor size */ 130 | USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */ 131 | 0x81, /* bEndpointAddress: (IN1) */ 132 | 0x02, /* bmAttributes: Bulk */ 133 | VIRTUAL_COM_PORT_DATA_SIZE, /* wMaxPacketSize: */ 134 | 0x00, 135 | 0x00 /* bInterval */ 136 | }; 137 | 138 | /* USB String Descriptors */ 139 | const uint8_t Virtual_Com_Port_StringLangID[VIRTUAL_COM_PORT_SIZ_STRING_LANGID] = 140 | { 141 | VIRTUAL_COM_PORT_SIZ_STRING_LANGID, 142 | USB_STRING_DESCRIPTOR_TYPE, 143 | 0x09, 144 | 0x04 /* LangID = 0x0409: U.S. English */ 145 | }; 146 | 147 | const uint8_t Virtual_Com_Port_StringVendor[VIRTUAL_COM_PORT_SIZ_STRING_VENDOR] = 148 | { 149 | VIRTUAL_COM_PORT_SIZ_STRING_VENDOR, /* Size of Vendor string */ 150 | USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType*/ 151 | /* Manufacturer: "STMicroelectronics" */ 152 | 'S', 0, 'T', 0, 'M', 0, 'i', 0, 'c', 0, 'r', 0, 'o', 0, 'e', 0, 153 | 'l', 0, 'e', 0, 'c', 0, 't', 0, 'r', 0, 'o', 0, 'n', 0, 'i', 0, 154 | 'c', 0, 's', 0 155 | }; 156 | 157 | const uint8_t Virtual_Com_Port_StringProduct[VIRTUAL_COM_PORT_SIZ_STRING_PRODUCT] = 158 | { 159 | VIRTUAL_COM_PORT_SIZ_STRING_PRODUCT, /* bLength */ 160 | USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */ 161 | /* Product name: "STM32 Virtual COM Port" */ 162 | 'S', 0, 'T', 0, 'M', 0, '3', 0, '2', 0, ' ', 0, 'V', 0, 'i', 0, 163 | 'r', 0, 't', 0, 'u', 0, 'a', 0, 'l', 0, ' ', 0, 'C', 0, 'O', 0, 164 | 'M', 0, ' ', 0, 'P', 0, 'o', 0, 'r', 0, 't', 0, ' ', 0, ' ', 0 165 | }; 166 | 167 | uint8_t Virtual_Com_Port_StringSerial[VIRTUAL_COM_PORT_SIZ_STRING_SERIAL] = 168 | { 169 | VIRTUAL_COM_PORT_SIZ_STRING_SERIAL, /* bLength */ 170 | USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */ 171 | 'S', 0, 'T', 0, 'M', 0, '3', 0, '2', 0 172 | }; 173 | 174 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 175 | -------------------------------------------------------------------------------- /Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_exti.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_exti.c 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief This file provides all the EXTI firmware functions. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

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

© COPYRIGHT 2013 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Includes ------------------------------------------------------------------*/ 30 | #include "usb_lib.h" 31 | #include "usb_conf.h" 32 | #include "usb_pwr.h" 33 | #include "hw_config.h" 34 | 35 | /* Private typedef -----------------------------------------------------------*/ 36 | /* Private define ------------------------------------------------------------*/ 37 | /* Private macro -------------------------------------------------------------*/ 38 | /* Private variables ---------------------------------------------------------*/ 39 | __IO uint32_t bDeviceState = UNCONNECTED; /* USB device status */ 40 | __IO bool fSuspendEnabled = TRUE; /* true when suspend is possible */ 41 | __IO uint32_t EP[8]; 42 | 43 | struct 44 | { 45 | __IO RESUME_STATE eState; 46 | __IO uint8_t bESOFcnt; 47 | } 48 | ResumeS; 49 | 50 | __IO uint32_t remotewakeupon=0; 51 | 52 | /* Extern variables ----------------------------------------------------------*/ 53 | /* Private function prototypes -----------------------------------------------*/ 54 | /* Extern function prototypes ------------------------------------------------*/ 55 | /* Private functions ---------------------------------------------------------*/ 56 | 57 | /******************************************************************************* 58 | * Function Name : PowerOn 59 | * Description : 60 | * Input : None. 61 | * Output : None. 62 | * Return : USB_SUCCESS. 63 | *******************************************************************************/ 64 | RESULT PowerOn(void) 65 | { 66 | uint16_t wRegVal; 67 | 68 | /*** cable plugged-in ? ***/ 69 | USB_Cable_Config(ENABLE); 70 | 71 | /*** CNTR_PWDN = 0 ***/ 72 | wRegVal = CNTR_FRES; 73 | _SetCNTR(wRegVal); 74 | 75 | /*** CNTR_FRES = 0 ***/ 76 | wInterrupt_Mask = 0; 77 | _SetCNTR(wInterrupt_Mask); 78 | /*** Clear pending interrupts ***/ 79 | _SetISTR(0); 80 | /*** Set interrupt mask ***/ 81 | wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; 82 | _SetCNTR(wInterrupt_Mask); 83 | 84 | return USB_SUCCESS; 85 | } 86 | 87 | /******************************************************************************* 88 | * Function Name : PowerOff 89 | * Description : handles switch-off conditions 90 | * Input : None. 91 | * Output : None. 92 | * Return : USB_SUCCESS. 93 | *******************************************************************************/ 94 | RESULT PowerOff() 95 | { 96 | /* disable all interrupts and force USB reset */ 97 | _SetCNTR(CNTR_FRES); 98 | /* clear interrupt status register */ 99 | _SetISTR(0); 100 | /* Disable the Pull-Up*/ 101 | USB_Cable_Config(DISABLE); 102 | /* switch-off device */ 103 | _SetCNTR(CNTR_FRES + CNTR_PDWN); 104 | /* sw variables reset */ 105 | /* ... */ 106 | 107 | return USB_SUCCESS; 108 | } 109 | 110 | /******************************************************************************* 111 | * Function Name : Suspend 112 | * Description : sets suspend mode operating conditions 113 | * Input : None. 114 | * Output : None. 115 | * Return : USB_SUCCESS. 116 | *******************************************************************************/ 117 | void Suspend(void) 118 | { 119 | uint32_t i =0; 120 | uint16_t wCNTR; 121 | __IO uint32_t savePWR_CR=0; 122 | /* suspend preparation */ 123 | /* ... */ 124 | 125 | /*Store CNTR value */ 126 | wCNTR = _GetCNTR(); 127 | 128 | /* This a sequence to apply a force RESET to handle a robustness case */ 129 | 130 | /*Store endpoints registers status */ 131 | for (i=0;i<8;i++) EP[i] = _GetENDPOINT(i); 132 | 133 | /* unmask RESET flag */ 134 | wCNTR|=CNTR_RESETM; 135 | _SetCNTR(wCNTR); 136 | 137 | /*apply FRES */ 138 | wCNTR|=CNTR_FRES; 139 | _SetCNTR(wCNTR); 140 | 141 | /*clear FRES*/ 142 | wCNTR&=~CNTR_FRES; 143 | _SetCNTR(wCNTR); 144 | 145 | /*poll for RESET flag in ISTR*/ 146 | while((_GetISTR()&ISTR_RESET) == 0); 147 | 148 | /* clear RESET flag in ISTR */ 149 | _SetISTR((uint16_t)CLR_RESET); 150 | 151 | /*restore Enpoints*/ 152 | for (i=0;i<8;i++) 153 | _SetENDPOINT(i, EP[i]); 154 | 155 | /* Now it is safe to enter macrocell in suspend mode */ 156 | wCNTR |= CNTR_FSUSP; 157 | _SetCNTR(wCNTR); 158 | 159 | /* force low-power mode in the macrocell */ 160 | wCNTR = _GetCNTR(); 161 | wCNTR |= CNTR_LPMODE; 162 | _SetCNTR(wCNTR); 163 | 164 | Enter_LowPowerMode(); 165 | } 166 | 167 | /******************************************************************************* 168 | * Function Name : Resume_Init 169 | * Description : Handles wake-up restoring normal operations 170 | * Input : None. 171 | * Output : None. 172 | * Return : USB_SUCCESS. 173 | *******************************************************************************/ 174 | void Resume_Init(void) 175 | { 176 | uint16_t wCNTR; 177 | 178 | /* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */ 179 | /* restart the clocks */ 180 | /* ... */ 181 | 182 | /* CNTR_LPMODE = 0 */ 183 | wCNTR = _GetCNTR(); 184 | wCNTR &= (~CNTR_LPMODE); 185 | _SetCNTR(wCNTR); 186 | 187 | /* restore full power */ 188 | /* ... on connected devices */ 189 | Leave_LowPowerMode(); 190 | 191 | /* reset FSUSP bit */ 192 | _SetCNTR(IMR_MSK); 193 | 194 | /* reverse suspend preparation */ 195 | /* ... */ 196 | 197 | } 198 | 199 | /******************************************************************************* 200 | * Function Name : Resume 201 | * Description : This is the state machine handling resume operations and 202 | * timing sequence. The control is based on the Resume structure 203 | * variables and on the ESOF interrupt calling this subroutine 204 | * without changing machine state. 205 | * Input : a state machine value (RESUME_STATE) 206 | * RESUME_ESOF doesn't change ResumeS.eState allowing 207 | * decrementing of the ESOF counter in different states. 208 | * Output : None. 209 | * Return : None. 210 | *******************************************************************************/ 211 | void Resume(RESUME_STATE eResumeSetVal) 212 | { 213 | uint16_t wCNTR; 214 | 215 | if (eResumeSetVal != RESUME_ESOF) 216 | ResumeS.eState = eResumeSetVal; 217 | switch (ResumeS.eState) 218 | { 219 | case RESUME_EXTERNAL: 220 | if (remotewakeupon ==0) 221 | { 222 | Resume_Init(); 223 | ResumeS.eState = RESUME_OFF; 224 | } 225 | else /* RESUME detected during the RemoteWAkeup signalling => keep RemoteWakeup handling*/ 226 | { 227 | ResumeS.eState = RESUME_ON; 228 | } 229 | break; 230 | case RESUME_INTERNAL: 231 | Resume_Init(); 232 | ResumeS.eState = RESUME_START; 233 | remotewakeupon = 1; 234 | break; 235 | case RESUME_LATER: 236 | ResumeS.bESOFcnt = 2; 237 | ResumeS.eState = RESUME_WAIT; 238 | break; 239 | case RESUME_WAIT: 240 | ResumeS.bESOFcnt--; 241 | if (ResumeS.bESOFcnt == 0) 242 | ResumeS.eState = RESUME_START; 243 | break; 244 | case RESUME_START: 245 | wCNTR = _GetCNTR(); 246 | wCNTR |= CNTR_RESUME; 247 | _SetCNTR(wCNTR); 248 | ResumeS.eState = RESUME_ON; 249 | ResumeS.bESOFcnt = 10; 250 | break; 251 | case RESUME_ON: 252 | ResumeS.bESOFcnt--; 253 | if (ResumeS.bESOFcnt == 0) 254 | { 255 | wCNTR = _GetCNTR(); 256 | wCNTR &= (~CNTR_RESUME); 257 | _SetCNTR(wCNTR); 258 | ResumeS.eState = RESUME_OFF; 259 | remotewakeupon = 0; 260 | } 261 | break; 262 | case RESUME_OFF: 263 | case RESUME_ESOF: 264 | default: 265 | ResumeS.eState = RESUME_OFF; 266 | break; 267 | } 268 | } 269 | 270 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 271 | -------------------------------------------------------------------------------- /APP/mpu6050/PS2_SONY.c: -------------------------------------------------------------------------------- 1 | 2 | #include "SysTick.h" 3 | #include "PS2_SONY.h" 4 | 5 | u8 psx_buf[PSX_BUF_SIZE]; //按键数据 6 | u8 deal_flag = 0; //处理标志 7 | u8 release_flag = 0; //弹起标志 8 | u8 PS2Count = 0; 9 | 10 | /* 11 | PS2_DAT PB3 IN DI 12 | PS2_CMD PB4 OUT DO 13 | PS2_ATT PB5 OUT CS 14 | PS2_CLK PB6 OUT 15 | */ 16 | void psx_io_config(void) { 17 | GPIO_InitTypeDef GPIO_InitStructure; 18 | 19 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //使能 PB 端口时钟 20 | GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); //使能禁止JTAG,否则PB3&4不能置0 21 | 22 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6; 23 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 24 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO 翻转 50MHz 25 | GPIO_Init(GPIOB, &GPIO_InitStructure); 26 | 27 | 28 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; 29 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; 30 | GPIO_Init(GPIOB, &GPIO_InitStructure); 31 | } 32 | 33 | /*=========================================手柄码解析函数======================================= 34 | 函数名:psx_transfer 35 | 功能介绍:解析手柄传输的数据 36 | 函数参数:dat(手柄源码) 37 | 返回值:rd_data(接) 38 | ===============================================================================================*/ 39 | u8 psx_transfer(u8 dat) { 40 | u8 rd_data; 41 | volatile u16 i=0x01; 42 | rd_data = 0; 43 | for(i=0x01;i<0x0100;i<<=1){ 44 | if(i&dat) PS2_CMD = 1; 45 | else PS2_CMD = 0; 46 | PS2_CLK = 1; 47 | delay_us(5); 48 | PS2_CLK = 0; 49 | delay_us(5); 50 | PS2_CLK = 1; 51 | if(PS2_DAT) { 52 | rd_data |= i; 53 | } 54 | } 55 | delay_us(16); 56 | return rd_data; 57 | } 58 | 59 | //short poll 60 | void PS2_ShortPoll(void) 61 | { 62 | PS2_ATT = 0; 63 | delay_us(16); 64 | psx_transfer(0x01); 65 | psx_transfer(0x42); 66 | psx_transfer(0X00); 67 | psx_transfer(0x00); 68 | psx_transfer(0x00); 69 | PS2_ATT = 1; 70 | delay_us(16); 71 | } 72 | //进入配置 73 | void PS2_EnterConfing(void) 74 | { 75 | PS2_ATT = 0; 76 | delay_us(16); 77 | psx_transfer(0x01); 78 | psx_transfer(0x43); 79 | psx_transfer(0X00); 80 | psx_transfer(0x01); 81 | psx_transfer(0x00); 82 | psx_transfer(0X00); 83 | psx_transfer(0X00); 84 | psx_transfer(0X00); 85 | psx_transfer(0X00); 86 | PS2_ATT = 1; 87 | delay_us(16); 88 | } 89 | //发送模式设置 90 | void PS2_TurnOnAnalogMode(void) 91 | { 92 | PS2_ATT = 0; 93 | psx_transfer(0x01); 94 | psx_transfer(0x44); 95 | psx_transfer(0X00); 96 | psx_transfer(0x01); //analog=0x01;digital=0x00 软件设置发送模式 97 | psx_transfer(0x03); //Ox03锁存设置,即不可通过按键“MODE”设置模式。 98 | //0xEE不锁存软件设置,可通过按键“MODE”设置模式。 99 | psx_transfer(0X00); 100 | psx_transfer(0X00); 101 | psx_transfer(0X00); 102 | psx_transfer(0X00); 103 | PS2_ATT = 1; 104 | delay_us(16); 105 | } 106 | //振动设置 107 | void PS2_VibrationMode(void) 108 | { 109 | PS2_ATT = 0; 110 | delay_us(16); 111 | psx_transfer(0x01); 112 | psx_transfer(0x4D); 113 | psx_transfer(0X00); 114 | psx_transfer(0x00); 115 | psx_transfer(0X01); 116 | PS2_ATT = 1; 117 | delay_us(16); 118 | } 119 | //完成并保存配置 120 | void PS2_ExitConfing(void) 121 | { 122 | PS2_ATT = 0; 123 | delay_us(16); 124 | psx_transfer(0x01); 125 | psx_transfer(0x43); 126 | psx_transfer(0X00); 127 | psx_transfer(0x00); 128 | psx_transfer(0x5A); 129 | psx_transfer(0x5A); 130 | psx_transfer(0x5A); 131 | psx_transfer(0x5A); 132 | psx_transfer(0x5A); 133 | PS2_ATT = 1; 134 | delay_us(16); 135 | } 136 | /*=========================================手柄初始化============================================ 137 | 函数名:psx_init 138 | 功能介绍:PS2引脚拉高 139 | 函数参数:无 140 | 返回值:无 141 | ===============================================================================================*/ 142 | void PSX_init(void) { 143 | psx_io_config(); 144 | // PS2_ATT = 1; 145 | // PS2_CMD = 1; 146 | // PS2_CLK = 1; 147 | //PS2_DAT = 0; 148 | //PS2_ACK = 1; 149 | PS2_ShortPoll(); 150 | //PS2_ShortPoll(); 151 | //PS2_ShortPoll(); 152 | //PS2_EnterConfing(); //进入配置模式 153 | //PS2_TurnOnAnalogMode(); //“红绿灯”配置模式,并选择是否保存 154 | //PS2_VibrationMode(); //开启震动模式 155 | //PS2_ExitConfing(); //完成并保存配置 156 | delay_ms(1000); 157 | } 158 | 159 | /*=========================================手柄原码获取函数======================================= 160 | 函数名:psx_write_read 161 | 功能介绍:解析并保存手柄传输的数据 162 | 函数参数:*get_buf 获取数据 163 | 返回值:无 164 | ===============================================================================================*/ 165 | void psx_write_read(u8 *get_buf) { 166 | 167 | // volatile u8 byte=0; 168 | // volatile u16 ref=0x01; 169 | // PS2_ATT = 0; 170 | // get_buf[0] = psx_transfer(START_CMD); //开始命令 171 | // get_buf[1] = psx_transfer(ASK_DAT_CMD); //请求数据 172 | // for(byte=2;byte<9;byte++) //开始接受数据 173 | // { 174 | // for(ref=0x01;ref<0x100;ref<<=1) 175 | // { 176 | // PS2_CLK = 1; 177 | // delay_us(5); 178 | // PS2_CLK = 0; 179 | // delay_us(5); 180 | // PS2_CLK = 1; 181 | // if(PS2_DAT) get_buf[byte] |= ref; 182 | // } 183 | // delay_us(16); 184 | // } 185 | // PS2_ATT = 1; 186 | 187 | 188 | PS2_ATT = 0; 189 | get_buf[0] = psx_transfer(START_CMD); 190 | get_buf[1] = psx_transfer(ASK_DAT_CMD); 191 | get_buf[2] = psx_transfer(get_buf[0]); 192 | get_buf[3] = psx_transfer(get_buf[0]); 193 | get_buf[4] = psx_transfer(get_buf[0]); 194 | get_buf[5] = psx_transfer(get_buf[0]); 195 | get_buf[6] = psx_transfer(get_buf[0]); 196 | get_buf[7] = psx_transfer(get_buf[0]); 197 | get_buf[8] = psx_transfer(get_buf[0]); 198 | PS2_ATT = 1; 199 | 200 | return; 201 | } 202 | 203 | //弹起后才能按别的 204 | void getStateHold(void) { 205 | static u8 temp_alv, temp_alh, temp_arv, temp_arh; 206 | psx_write_read(psx_buf);//读取手柄 207 | if((psx_buf[1] == PSX_RED_MODE)||(psx_buf[1] == PSX_GREEN_MODE)) { //共用 208 | switch(psx_buf[3]) { 209 | case 0xef: {if(deal_flag < 1) {release_flag = RELEASE_UP;deal_flag++;}break;} //Up 210 | case 0xbf: {if(deal_flag < 1) {release_flag = RELEASE_DOWN;deal_flag++;}break;} //Down 211 | case 0x7f: {if(deal_flag < 1) {release_flag = RELEASE_LEFT;deal_flag++;}break;} //Left 212 | case 0xdf: {if(deal_flag < 1) {release_flag = RELEASE_RIGHT;deal_flag++;}break;} //Right 213 | case 0xfe: {if(deal_flag < 1) {release_flag = RELEASE_SELECT;deal_flag++;}break;} //Select 214 | //红灯模式特有 215 | case 0xf7: {if(deal_flag < 1) {release_flag = RELEASE_START;deal_flag++;}break;} //Start 216 | case 0xfd: {if(deal_flag < 1) {release_flag = RELEASE_L3;deal_flag++;}break;} //L3 217 | case 0xfb: {if(deal_flag < 1) {release_flag = RELEASE_R3;deal_flag++;}break;} //R3 218 | default: { 219 | deal_flag = 0;release_flag = 0;break; //弹起 220 | } 221 | } 222 | if(deal_flag) return; 223 | switch(psx_buf[4]) { 224 | case 0xfb: {if(deal_flag < 1) {release_flag = RELEASE_L1;deal_flag++;}break;} //L1 225 | case 0xfe: {if(deal_flag < 1) {release_flag = RELEASE_L2;deal_flag++;}break;} //L2 226 | case 0xf7: {if(deal_flag < 1) {release_flag = RELEASE_R1;deal_flag++;}break;} //R1 227 | case 0xfd: {if(deal_flag < 1) {release_flag = RELEASE_R2;deal_flag++;}break;} //R2 228 | case 0xef: {if(deal_flag < 1) {release_flag = RELEASE_TRI;deal_flag++;}break;} //Tri 229 | case 0xbf: {if(deal_flag < 1) {release_flag = RELEASE_CROSS;deal_flag++;}break;} //Cross 230 | case 0x7f: {if(deal_flag < 1) {release_flag = RELEASE_RECT;deal_flag++;}break;} //Rect 231 | case 0xdf: {if(deal_flag < 1) {release_flag = RELEASE_CIRCLE;deal_flag++;}break;} //Circle 232 | default:{ 233 | deal_flag = 0;release_flag = 0;break; //弹起 234 | } 235 | } 236 | } 237 | if(psx_buf[1] == PSX_RED_MODE) { //判断是否为红灯模式 238 | temp_alv = psx_buf[8]; //读取左摇杆垂直数据 239 | temp_arv = psx_buf[6]; //读取右摇杆垂直数据 240 | temp_alh = psx_buf[7]; //读取左摇杆水平数据 241 | temp_arh = psx_buf[5]; //读取右摇杆水平数据 242 | if (temp_alv<64) {if(deal_flag < 2) {release_flag = RELEASE_UP;deal_flag++;}} 243 | if (temp_alv>192) {if(deal_flag < 2) {release_flag = RELEASE_DOWN;deal_flag++;}} 244 | if (temp_alh<64) {if(deal_flag < 2) {release_flag = RELEASE_LEFT;deal_flag++;}} 245 | if (temp_alh>192) {if(deal_flag < 2) {release_flag = RELEASE_RIGHT;deal_flag++;}} 246 | if (temp_arv<64) {if(deal_flag < 2) {release_flag = RELEASE_TRI;deal_flag++;}} 247 | if (temp_arv>192) {if(deal_flag < 2) {release_flag = RELEASE_CROSS;deal_flag++;}} 248 | if (temp_arh<64) {if(deal_flag < 2) {release_flag = RELEASE_RECT;deal_flag++;}} 249 | if (temp_arh>192) {if(deal_flag < 2) {release_flag = RELEASE_CIRCLE;deal_flag++;}} 250 | } 251 | //return release_flag; 252 | } 253 | -------------------------------------------------------------------------------- /Libraries/STM32F10x_StdPeriph_Driver/inc/misc.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file misc.h 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief This file contains all the functions prototypes for the miscellaneous 8 | * firmware library functions (add-on to CMSIS functions). 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 13 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 14 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 15 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 16 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 17 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 18 | * 19 | *

© COPYRIGHT 2011 STMicroelectronics

20 | ****************************************************************************** 21 | */ 22 | 23 | /* Define to prevent recursive inclusion -------------------------------------*/ 24 | #ifndef __MISC_H 25 | #define __MISC_H 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | /* Includes ------------------------------------------------------------------*/ 32 | #include "stm32f10x.h" 33 | 34 | /** @addtogroup STM32F10x_StdPeriph_Driver 35 | * @{ 36 | */ 37 | 38 | /** @addtogroup MISC 39 | * @{ 40 | */ 41 | 42 | /** @defgroup MISC_Exported_Types 43 | * @{ 44 | */ 45 | 46 | /** 47 | * @brief NVIC Init Structure definition 48 | */ 49 | 50 | typedef struct 51 | { 52 | uint8_t NVIC_IRQChannel; /*!< Specifies the IRQ channel to be enabled or disabled. 53 | This parameter can be a value of @ref IRQn_Type 54 | (For the complete STM32 Devices IRQ Channels list, please 55 | refer to stm32f10x.h file) */ 56 | 57 | uint8_t NVIC_IRQChannelPreemptionPriority; /*!< Specifies the pre-emption priority for the IRQ channel 58 | specified in NVIC_IRQChannel. This parameter can be a value 59 | between 0 and 15 as described in the table @ref NVIC_Priority_Table */ 60 | 61 | uint8_t NVIC_IRQChannelSubPriority; /*!< Specifies the subpriority level for the IRQ channel specified 62 | in NVIC_IRQChannel. This parameter can be a value 63 | between 0 and 15 as described in the table @ref NVIC_Priority_Table */ 64 | 65 | FunctionalState NVIC_IRQChannelCmd; /*!< Specifies whether the IRQ channel defined in NVIC_IRQChannel 66 | will be enabled or disabled. 67 | This parameter can be set either to ENABLE or DISABLE */ 68 | } NVIC_InitTypeDef; 69 | 70 | /** 71 | * @} 72 | */ 73 | 74 | /** @defgroup NVIC_Priority_Table 75 | * @{ 76 | */ 77 | 78 | /** 79 | @code 80 | The table below gives the allowed values of the pre-emption priority and subpriority according 81 | to the Priority Grouping configuration performed by NVIC_PriorityGroupConfig function 82 | ============================================================================================================================ 83 | NVIC_PriorityGroup | NVIC_IRQChannelPreemptionPriority | NVIC_IRQChannelSubPriority | Description 84 | ============================================================================================================================ 85 | NVIC_PriorityGroup_0 | 0 | 0-15 | 0 bits for pre-emption priority 86 | | | | 4 bits for subpriority 87 | ---------------------------------------------------------------------------------------------------------------------------- 88 | NVIC_PriorityGroup_1 | 0-1 | 0-7 | 1 bits for pre-emption priority 89 | | | | 3 bits for subpriority 90 | ---------------------------------------------------------------------------------------------------------------------------- 91 | NVIC_PriorityGroup_2 | 0-3 | 0-3 | 2 bits for pre-emption priority 92 | | | | 2 bits for subpriority 93 | ---------------------------------------------------------------------------------------------------------------------------- 94 | NVIC_PriorityGroup_3 | 0-7 | 0-1 | 3 bits for pre-emption priority 95 | | | | 1 bits for subpriority 96 | ---------------------------------------------------------------------------------------------------------------------------- 97 | NVIC_PriorityGroup_4 | 0-15 | 0 | 4 bits for pre-emption priority 98 | | | | 0 bits for subpriority 99 | ============================================================================================================================ 100 | @endcode 101 | */ 102 | 103 | /** 104 | * @} 105 | */ 106 | 107 | /** @defgroup MISC_Exported_Constants 108 | * @{ 109 | */ 110 | 111 | /** @defgroup Vector_Table_Base 112 | * @{ 113 | */ 114 | 115 | #define NVIC_VectTab_RAM ((uint32_t)0x20000000) 116 | #define NVIC_VectTab_FLASH ((uint32_t)0x08000000) 117 | #define IS_NVIC_VECTTAB(VECTTAB) (((VECTTAB) == NVIC_VectTab_RAM) || \ 118 | ((VECTTAB) == NVIC_VectTab_FLASH)) 119 | /** 120 | * @} 121 | */ 122 | 123 | /** @defgroup System_Low_Power 124 | * @{ 125 | */ 126 | 127 | #define NVIC_LP_SEVONPEND ((uint8_t)0x10) 128 | #define NVIC_LP_SLEEPDEEP ((uint8_t)0x04) 129 | #define NVIC_LP_SLEEPONEXIT ((uint8_t)0x02) 130 | #define IS_NVIC_LP(LP) (((LP) == NVIC_LP_SEVONPEND) || \ 131 | ((LP) == NVIC_LP_SLEEPDEEP) || \ 132 | ((LP) == NVIC_LP_SLEEPONEXIT)) 133 | /** 134 | * @} 135 | */ 136 | 137 | /** @defgroup Preemption_Priority_Group 138 | * @{ 139 | */ 140 | 141 | #define NVIC_PriorityGroup_0 ((uint32_t)0x700) /*!< 0 bits for pre-emption priority 142 | 4 bits for subpriority */ 143 | #define NVIC_PriorityGroup_1 ((uint32_t)0x600) /*!< 1 bits for pre-emption priority 144 | 3 bits for subpriority */ 145 | #define NVIC_PriorityGroup_2 ((uint32_t)0x500) /*!< 2 bits for pre-emption priority 146 | 2 bits for subpriority */ 147 | #define NVIC_PriorityGroup_3 ((uint32_t)0x400) /*!< 3 bits for pre-emption priority 148 | 1 bits for subpriority */ 149 | #define NVIC_PriorityGroup_4 ((uint32_t)0x300) /*!< 4 bits for pre-emption priority 150 | 0 bits for subpriority */ 151 | 152 | #define IS_NVIC_PRIORITY_GROUP(GROUP) (((GROUP) == NVIC_PriorityGroup_0) || \ 153 | ((GROUP) == NVIC_PriorityGroup_1) || \ 154 | ((GROUP) == NVIC_PriorityGroup_2) || \ 155 | ((GROUP) == NVIC_PriorityGroup_3) || \ 156 | ((GROUP) == NVIC_PriorityGroup_4)) 157 | 158 | #define IS_NVIC_PREEMPTION_PRIORITY(PRIORITY) ((PRIORITY) < 0x10) 159 | 160 | #define IS_NVIC_SUB_PRIORITY(PRIORITY) ((PRIORITY) < 0x10) 161 | 162 | #define IS_NVIC_OFFSET(OFFSET) ((OFFSET) < 0x000FFFFF) 163 | 164 | /** 165 | * @} 166 | */ 167 | 168 | /** @defgroup SysTick_clock_source 169 | * @{ 170 | */ 171 | 172 | #define SysTick_CLKSource_HCLK_Div8 ((uint32_t)0xFFFFFFFB) 173 | #define SysTick_CLKSource_HCLK ((uint32_t)0x00000004) 174 | #define IS_SYSTICK_CLK_SOURCE(SOURCE) (((SOURCE) == SysTick_CLKSource_HCLK) || \ 175 | ((SOURCE) == SysTick_CLKSource_HCLK_Div8)) 176 | /** 177 | * @} 178 | */ 179 | 180 | /** 181 | * @} 182 | */ 183 | 184 | /** @defgroup MISC_Exported_Macros 185 | * @{ 186 | */ 187 | 188 | /** 189 | * @} 190 | */ 191 | 192 | /** @defgroup MISC_Exported_Functions 193 | * @{ 194 | */ 195 | 196 | void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup); 197 | void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct); 198 | void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset); 199 | void NVIC_SystemLPConfig(uint8_t LowPowerMode, FunctionalState NewState); 200 | void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource); 201 | 202 | #ifdef __cplusplus 203 | } 204 | #endif 205 | 206 | #endif /* __MISC_H */ 207 | 208 | /** 209 | * @} 210 | */ 211 | 212 | /** 213 | * @} 214 | */ 215 | 216 | /** 217 | * @} 218 | */ 219 | 220 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 221 | -------------------------------------------------------------------------------- /USB/STM32_USB-FS-Device_Driver/inc/usb_core.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usb_core.h 4 | * @author MCD Application Team 5 | * @version V4.0.0 6 | * @date 28-August-2012 7 | * @brief Standard protocol processing functions prototypes 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | *

© COPYRIGHT 2012 STMicroelectronics

12 | * 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 14 | * You may not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at: 16 | * 17 | * http://www.st.com/software_license_agreement_liberty_v2 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | ****************************************************************************** 26 | */ 27 | 28 | 29 | /* Define to prevent recursive inclusion -------------------------------------*/ 30 | #ifndef __USB_CORE_H 31 | #define __USB_CORE_H 32 | 33 | /* Includes ------------------------------------------------------------------*/ 34 | /* Exported types ------------------------------------------------------------*/ 35 | typedef enum _CONTROL_STATE 36 | { 37 | WAIT_SETUP, /* 0 */ 38 | SETTING_UP, /* 1 */ 39 | IN_DATA, /* 2 */ 40 | OUT_DATA, /* 3 */ 41 | LAST_IN_DATA, /* 4 */ 42 | LAST_OUT_DATA, /* 5 */ 43 | WAIT_STATUS_IN, /* 7 */ 44 | WAIT_STATUS_OUT, /* 8 */ 45 | STALLED, /* 9 */ 46 | PAUSE /* 10 */ 47 | } CONTROL_STATE; /* The state machine states of a control pipe */ 48 | 49 | typedef struct OneDescriptor 50 | { 51 | uint8_t *Descriptor; 52 | uint16_t Descriptor_Size; 53 | } 54 | ONE_DESCRIPTOR, *PONE_DESCRIPTOR; 55 | /* All the request process routines return a value of this type 56 | If the return value is not SUCCESS or NOT_READY, 57 | the software will STALL the correspond endpoint */ 58 | typedef enum _RESULT 59 | { 60 | USB_SUCCESS = 0, /* Process successfully */ 61 | USB_ERROR, 62 | USB_UNSUPPORT, 63 | USB_NOT_READY /* The process has not been finished, endpoint will be 64 | NAK to further request */ 65 | } RESULT; 66 | 67 | 68 | /*-*-*-*-*-*-*-*-*-*-* Definitions for endpoint level -*-*-*-*-*-*-*-*-*-*-*-*/ 69 | typedef struct _ENDPOINT_INFO 70 | { 71 | /* When send data out of the device, 72 | CopyData() is used to get data buffer 'Length' bytes data 73 | if Length is 0, 74 | CopyData() returns the total length of the data 75 | if the request is not supported, returns 0 76 | (NEW Feature ) 77 | if CopyData() returns -1, the calling routine should not proceed 78 | further and will resume the SETUP process by the class device 79 | if Length is not 0, 80 | CopyData() returns a pointer to indicate the data location 81 | Usb_wLength is the data remain to be sent, 82 | Usb_wOffset is the Offset of original data 83 | When receive data from the host, 84 | CopyData() is used to get user data buffer which is capable 85 | of Length bytes data to copy data from the endpoint buffer. 86 | if Length is 0, 87 | CopyData() returns the available data length, 88 | if Length is not 0, 89 | CopyData() returns user buffer address 90 | Usb_rLength is the data remain to be received, 91 | Usb_rPointer is the Offset of data buffer 92 | */ 93 | uint16_t Usb_wLength; 94 | uint16_t Usb_wOffset; 95 | uint16_t PacketSize; 96 | uint8_t *(*CopyData)(uint16_t Length); 97 | }ENDPOINT_INFO; 98 | 99 | /*-*-*-*-*-*-*-*-*-*-*-* Definitions for device level -*-*-*-*-*-*-*-*-*-*-*-*/ 100 | 101 | typedef struct _DEVICE 102 | { 103 | uint8_t Total_Endpoint; /* Number of endpoints that are used */ 104 | uint8_t Total_Configuration;/* Number of configuration available */ 105 | } 106 | DEVICE; 107 | 108 | typedef union 109 | { 110 | uint16_t w; 111 | struct BW 112 | { 113 | uint8_t bb1; 114 | uint8_t bb0; 115 | } 116 | bw; 117 | } uint16_t_uint8_t; 118 | 119 | typedef struct _DEVICE_INFO 120 | { 121 | uint8_t USBbmRequestType; /* bmRequestType */ 122 | uint8_t USBbRequest; /* bRequest */ 123 | uint16_t_uint8_t USBwValues; /* wValue */ 124 | uint16_t_uint8_t USBwIndexs; /* wIndex */ 125 | uint16_t_uint8_t USBwLengths; /* wLength */ 126 | 127 | uint8_t ControlState; /* of type CONTROL_STATE */ 128 | uint8_t Current_Feature; 129 | uint8_t Current_Configuration; /* Selected configuration */ 130 | uint8_t Current_Interface; /* Selected interface of current configuration */ 131 | uint8_t Current_AlternateSetting;/* Selected Alternate Setting of current 132 | interface*/ 133 | 134 | ENDPOINT_INFO Ctrl_Info; 135 | }DEVICE_INFO; 136 | 137 | typedef struct _DEVICE_PROP 138 | { 139 | void (*Init)(void); /* Initialize the device */ 140 | void (*Reset)(void); /* Reset routine of this device */ 141 | 142 | /* Device dependent process after the status stage */ 143 | void (*Process_Status_IN)(void); 144 | void (*Process_Status_OUT)(void); 145 | 146 | /* Procedure of process on setup stage of a class specified request with data stage */ 147 | /* All class specified requests with data stage are processed in Class_Data_Setup 148 | Class_Data_Setup() 149 | responses to check all special requests and fills ENDPOINT_INFO 150 | according to the request 151 | If IN tokens are expected, then wLength & wOffset will be filled 152 | with the total transferring bytes and the starting position 153 | If OUT tokens are expected, then rLength & rOffset will be filled 154 | with the total expected bytes and the starting position in the buffer 155 | 156 | If the request is valid, Class_Data_Setup returns SUCCESS, else UNSUPPORT 157 | 158 | CAUTION: 159 | Since GET_CONFIGURATION & GET_INTERFACE are highly related to 160 | the individual classes, they will be checked and processed here. 161 | */ 162 | RESULT (*Class_Data_Setup)(uint8_t RequestNo); 163 | 164 | /* Procedure of process on setup stage of a class specified request without data stage */ 165 | /* All class specified requests without data stage are processed in Class_NoData_Setup 166 | Class_NoData_Setup 167 | responses to check all special requests and perform the request 168 | 169 | CAUTION: 170 | Since SET_CONFIGURATION & SET_INTERFACE are highly related to 171 | the individual classes, they will be checked and processed here. 172 | */ 173 | RESULT (*Class_NoData_Setup)(uint8_t RequestNo); 174 | 175 | /*Class_Get_Interface_Setting 176 | This function is used by the file usb_core.c to test if the selected Interface 177 | and Alternate Setting (uint8_t Interface, uint8_t AlternateSetting) are supported by 178 | the application. 179 | This function is writing by user. It should return "SUCCESS" if the Interface 180 | and Alternate Setting are supported by the application or "UNSUPPORT" if they 181 | are not supported. */ 182 | 183 | RESULT (*Class_Get_Interface_Setting)(uint8_t Interface, uint8_t AlternateSetting); 184 | 185 | uint8_t* (*GetDeviceDescriptor)(uint16_t Length); 186 | uint8_t* (*GetConfigDescriptor)(uint16_t Length); 187 | uint8_t* (*GetStringDescriptor)(uint16_t Length); 188 | 189 | /* This field is not used in current library version. It is kept only for 190 | compatibility with previous versions */ 191 | void* RxEP_buffer; 192 | 193 | uint8_t MaxPacketSize; 194 | 195 | }DEVICE_PROP; 196 | 197 | typedef struct _USER_STANDARD_REQUESTS 198 | { 199 | void (*User_GetConfiguration)(void); /* Get Configuration */ 200 | void (*User_SetConfiguration)(void); /* Set Configuration */ 201 | void (*User_GetInterface)(void); /* Get Interface */ 202 | void (*User_SetInterface)(void); /* Set Interface */ 203 | void (*User_GetStatus)(void); /* Get Status */ 204 | void (*User_ClearFeature)(void); /* Clear Feature */ 205 | void (*User_SetEndPointFeature)(void); /* Set Endpoint Feature */ 206 | void (*User_SetDeviceFeature)(void); /* Set Device Feature */ 207 | void (*User_SetDeviceAddress)(void); /* Set Device Address */ 208 | } 209 | USER_STANDARD_REQUESTS; 210 | 211 | /* Exported constants --------------------------------------------------------*/ 212 | #define Type_Recipient (pInformation->USBbmRequestType & (REQUEST_TYPE | RECIPIENT)) 213 | 214 | #define Usb_rLength Usb_wLength 215 | #define Usb_rOffset Usb_wOffset 216 | 217 | #define USBwValue USBwValues.w 218 | #define USBwValue0 USBwValues.bw.bb0 219 | #define USBwValue1 USBwValues.bw.bb1 220 | #define USBwIndex USBwIndexs.w 221 | #define USBwIndex0 USBwIndexs.bw.bb0 222 | #define USBwIndex1 USBwIndexs.bw.bb1 223 | #define USBwLength USBwLengths.w 224 | #define USBwLength0 USBwLengths.bw.bb0 225 | #define USBwLength1 USBwLengths.bw.bb1 226 | 227 | /* Exported macro ------------------------------------------------------------*/ 228 | /* Exported functions ------------------------------------------------------- */ 229 | uint8_t Setup0_Process(void); 230 | uint8_t Post0_Process(void); 231 | uint8_t Out0_Process(void); 232 | uint8_t In0_Process(void); 233 | 234 | RESULT Standard_SetEndPointFeature(void); 235 | RESULT Standard_SetDeviceFeature(void); 236 | 237 | uint8_t *Standard_GetConfiguration(uint16_t Length); 238 | RESULT Standard_SetConfiguration(void); 239 | uint8_t *Standard_GetInterface(uint16_t Length); 240 | RESULT Standard_SetInterface(void); 241 | uint8_t *Standard_GetDescriptorData(uint16_t Length, PONE_DESCRIPTOR pDesc); 242 | 243 | uint8_t *Standard_GetStatus(uint16_t Length); 244 | RESULT Standard_ClearFeature(void); 245 | void SetDeviceAddress(uint8_t); 246 | void NOP_Process(void); 247 | 248 | extern DEVICE_PROP Device_Property; 249 | extern USER_STANDARD_REQUESTS User_Standard_Requests; 250 | extern DEVICE Device_Table; 251 | extern DEVICE_INFO Device_Info; 252 | 253 | /* cells saving status during interrupt servicing */ 254 | extern __IO uint16_t SaveRState; 255 | extern __IO uint16_t SaveTState; 256 | 257 | #endif /* __USB_CORE_H */ 258 | 259 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 260 | -------------------------------------------------------------------------------- /APP/mpu6050/iompu6050.c: -------------------------------------------------------------------------------- 1 | 2 | #include "system.h" 3 | #include "SysTick.h" 4 | #include "iompu6050.h" 5 | 6 | 7 | /* 8 | ********************************************************************************************************* 9 | * 函 数 名: i2c_Delay 10 | * 功能说明: I2C总线位延迟,最快400KHz 11 | * 形 参:无 12 | * 返 回 值: 无 13 | ********************************************************************************************************* 14 | */ 15 | static void i2c_Delay(void) 16 | { 17 | uint8_t i; 18 | 19 | /*  20 | 下面的时间是通过安富莱AX-Pro逻辑分析仪测试得到的。 21 | CPU主频72MHz时,在内部Flash运行, MDK工程不优化 22 | 循环次数为10时,SCL频率 = 205KHz 23 | 循环次数为7时,SCL频率 = 347KHz, SCL高电平时间1.5us,SCL低电平时间2.87us 24 | 循环次数为5时,SCL频率 = 421KHz, SCL高电平时间1.25us,SCL低电平时间2.375us 25 | 26 | IAR工程编译效率高,不能设置为7 27 | */ 28 | for (i = 0; i < 10; i++); 29 | } 30 | //产生IIC起始信号 31 | void IIC_Start(void) 32 | { 33 | IIC_SDA=1; 34 | IIC_SCL=1; 35 | i2c_Delay(); 36 | IIC_SDA=0;//START:when CLK is high,DATA change form high to low 37 | i2c_Delay(); 38 | IIC_SCL=0;//钳住I2C总线,准备发送或接收数据 39 | i2c_Delay(); 40 | } 41 | //产生IIC停止信号 42 | void IIC_Stop(void) 43 | { 44 | IIC_SDA=0;//STOP:when CLK is high DATA change form low to high 45 | IIC_SCL=1; 46 | i2c_Delay(); 47 | IIC_SDA=1;//发送I2C总线结束信号 48 | } 49 | //初始化IIC 50 | void IIC_Init(void) 51 | { 52 | GPIO_InitTypeDef GPIO_InitStructure; 53 | RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE ); 54 | 55 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3; 56 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD ; //推挽输出为0xD1,读数不变 57 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 58 | GPIO_Init(GPIOA, &GPIO_InitStructure); 59 | 60 | IIC_Stop(); 61 | } 62 | //等待应答信号到来 63 | //返回值:0,接收应答失败 64 | // 1,接收应答成功 65 | u8 IIC_Wait_Ack(void) 66 | { 67 | u8 ucErrTime=0; 68 | 69 | IIC_SDA=1;i2c_Delay(); 70 | IIC_SCL=1;i2c_Delay(); 71 | if(READ_SDA) 72 | { 73 | ucErrTime=0; 74 | } 75 | else{ 76 | ucErrTime=1; 77 | } 78 | IIC_SCL=0;//时钟输出0 79 | i2c_Delay(); 80 | return ucErrTime; 81 | } 82 | //产生ACK应答 83 | void IIC_Ack(void) 84 | { 85 | IIC_SDA=0; 86 | i2c_Delay(); 87 | IIC_SCL=1; 88 | i2c_Delay(); 89 | IIC_SCL=0; 90 | i2c_Delay(); 91 | IIC_SDA=1; 92 | } 93 | //不产生ACK应答 94 | void IIC_NAck(void) 95 | { 96 | IIC_SDA=1; 97 | i2c_Delay(); 98 | IIC_SCL=1; 99 | i2c_Delay(); 100 | IIC_SCL=0; 101 | i2c_Delay(); 102 | } 103 | /*********************************通用**************************************/ 104 | //IIC发送一个字节 105 | //返回从机有无应答 106 | //1,有应答 107 | //0,无应答 108 | void IIC_Send_Byte(u8 txd) 109 | { 110 | u8 t; 111 | for(t=0;t<8;t++) 112 | { 113 | if (txd&0x80) 114 | { 115 | IIC_SDA=1; 116 | } 117 | else{ 118 | IIC_SDA=0; 119 | } 120 | i2c_Delay(); 121 | IIC_SCL=1; 122 | i2c_Delay(); 123 | IIC_SCL=0; 124 | delay_us(2); 125 | if (t==7) 126 | { 127 | IIC_SDA=1; 128 | } 129 | txd<<=1; 130 | i2c_Delay(); 131 | } 132 | } 133 | //读1个字节,ack=1时,发送ACK,ack=0,发送nACK 134 | u8 IIC_Read_Byte(unsigned char ack) 135 | { 136 | unsigned char i,receive=0; 137 | 138 | for(i=0;i<8;i++ ) 139 | { 140 | receive<<=1; 141 | IIC_SCL=1; 142 | i2c_Delay(); 143 | if(READ_SDA)receive++; 144 | IIC_SCL=0; 145 | i2c_Delay(); 146 | } 147 | if (!ack) 148 | IIC_NAck();//发送nACK 149 | else 150 | IIC_Ack(); //发送ACK 151 | return receive; 152 | } 153 | /***********************************从机*************************************/ 154 | //IIC发送给从机一个字节 155 | //addr:寄存器地址 156 | //data:数据 157 | //返回值:1,成功 158 | // 0,失败 159 | u8 I2C_WriteByte(u8 addr, u8 data, u8 device_addr) 160 | { 161 | IIC_Start(); 162 | 163 | IIC_Send_Byte(device_addr); //发器件地址 164 | if(!IIC_Wait_Ack()) //等待应答 165 | { 166 | IIC_Stop(); 167 | return 0; 168 | } 169 | IIC_Send_Byte(addr); //发送低地址 170 | IIC_Wait_Ack(); 171 | IIC_Send_Byte(data); //发送字节 172 | if(!IIC_Wait_Ack()) //等待应答 173 | { 174 | IIC_Stop(); 175 | return 0; 176 | } 177 | IIC_Stop();//产生一个停止条件 178 | return 1; 179 | } 180 | 181 | //IIC接收来自从机的一个字节 182 | //addr:寄存器地址 183 | //返回值:1,成功 184 | // 0,失败 185 | u8 I2C_ReadByte(u8 addr, u8 device_addr) //读寄存器或读数据 186 | { 187 | u8 data=0; 188 | IIC_Start(); 189 | IIC_Send_Byte(device_addr); 190 | if(!IIC_Wait_Ack()) //等待应答 191 | { 192 | IIC_Stop(); 193 | return 0; 194 | } 195 | IIC_Send_Byte(addr); //发送低地址 196 | IIC_Wait_Ack(); 197 | 198 | IIC_Start(); 199 | IIC_Send_Byte(device_addr+1); //发器件地址 200 | if(!IIC_Wait_Ack()) //等待应答 201 | { 202 | IIC_Stop(); 203 | return 0; 204 | } 205 | 206 | data=IIC_Read_Byte(0); 207 | IIC_Stop();//产生一个停止条件 208 | return data; 209 | } 210 | //IIC发送给从机多个字节 211 | //addr:寄存器地址 212 | //len:字节数 213 | //data:数据 214 | //返回值:1,成功 215 | // 0,失败 216 | u8 I2C_WriteBytes(u8 addr,u8 len,u8 *buf,u8 device_addr) 217 | { 218 | u8 i; 219 | IIC_Start(); 220 | IIC_Send_Byte(device_addr);//发送器件地址 221 | if(!IIC_Wait_Ack()) //等待应答 222 | { 223 | IIC_Stop(); 224 | return 0; 225 | } 226 | IIC_Send_Byte(addr); //写寄存器地址 227 | IIC_Wait_Ack(); //等待应答 228 | for(i=0;i>1) )//器件ID正确 300 | { 301 | //MPU6050_Write_Byte(MPU6050_PWR_MGMT1_REG,0X01); //设置CLKSEL,PLL X轴为参考 302 | //MPU6050_Write_Byte(MPU6050_PWR_MGMT2_REG,0X00); //加速度与陀螺仪都工作 303 | //MPU6050_Set_Rate(50); //设置采样率为50Hz 304 | }else return 0; 305 | return 1; 306 | } 307 | //设置MPU6050陀螺仪传感器满量程范围 308 | //fsr:0,±250dps;1,±500dps;2,±1000dps;3,±2000dps 309 | //返回值:1,设置成功 310 | // 0,设置失败 311 | u8 MPU6050_Set_Gyro_Fsr(u8 fsr) 312 | { 313 | return MPU6050_Write_Byte(MPU6050_GYRO_CFG_REG,fsr<<3);//设置陀螺仪满量程范围 314 | } 315 | //设置MPU6050加速度传感器满量程范围 316 | //fsr:0,±2g;1,±4g;2,±8g;3,±16g, 16位ADC 317 | //返回值:1,设置成功 318 | // 0,设置失败 319 | u8 MPU6050_Set_Accel_Fsr(u8 fsr) 320 | { 321 | return MPU6050_Write_Byte(MPU6050_ACCEL_CFG_REG,fsr<<3);//设置加速度传感器满量程范围 322 | } 323 | //设置MPU6050的数字低通滤波器 324 | //lpf:数字低通滤波频率(Hz) 325 | //返回值:1,设置成功 326 | // 0,设置失败 327 | u8 MPU6050_Set_LPF(u16 lpf) 328 | { 329 | u8 data=0; 330 | if(lpf>=188)data=1; 331 | else if(lpf>=98)data=2; 332 | else if(lpf>=42)data=3; 333 | else if(lpf>=20)data=4; 334 | else if(lpf>=10)data=5; 335 | else data=6; 336 | return MPU6050_Write_Byte(MPU6050_CFG_REG,data);//设置数字低通滤波器 337 | } 338 | //设置MPU6050的采样率(假定Fs=1KHz) 339 | //rate:4~1000(Hz) 340 | //返回值:1,设置成功 341 | // 0,设置失败 342 | u8 MPU6050_Set_Rate(u16 rate) 343 | { 344 | u8 data; 345 | if(rate>1000)rate=1000; 346 | if(rate<4)rate=4; 347 | data=1000/rate-1; 348 | data=MPU6050_Write_Byte(MPU6050_SAMPLE_RATE_REG,data); //设置数字低通滤波器 349 | return MPU6050_Set_LPF(rate/2); //自动设置LPF为采样率的一半 350 | } 351 | 352 | //得到温度值 353 | //返回值:温度值(扩大了100倍) 354 | short MPU6050_Get_Temperature(void) 355 | { 356 | u8 buf[2]; 357 | short raw; 358 | float temp; 359 | MPU6050_Read_Len(MPU6050_TEMP_OUTH_REG,2,buf); 360 | raw=((u16)buf[0]<<8)|buf[1]; 361 | temp=36.53+((double)raw)/340; 362 | return temp*100;; 363 | } 364 | //得到陀螺仪值(原始值) 365 | //gx,gy,gz:陀螺仪x,y,z轴的原始读数(带符号) 366 | //返回值:1,成功 367 | // 0,失败 368 | u8 MPU6050_Get_Gyroscope(short *gx,short *gy,short *gz) 369 | { 370 | u8 buf[6],res; 371 | res=MPU6050_Read_Len(MPU6050_GYRO_XOUTH_REG,6,&buf[0]); 372 | if(res==1) 373 | { 374 | *gx=((u16)buf[0]<<8)|buf[1]; 375 | *gy=((u16)buf[2]<<8)|buf[3]; 376 | *gz=((u16)buf[4]<<8)|buf[5]; 377 | } 378 | return res; 379 | } 380 | //得到加速度值(原始值) 381 | //gx,gy,gz:陀螺仪x,y,z轴的原始读数(带符号) 382 | //返回值:1,成功 383 | // 0,失败 384 | u8 MPU6050_Get_Accelerometer(short *ax,short *ay,short *az) 385 | { 386 | u8 buf[6],res; 387 | res=MPU6050_Read_Len(MPU6050_ACCEL_XOUTH_REG,6,&buf[0]); 388 | if(res==1) 389 | { 390 | *ax=((u16)buf[0]<<8)|buf[1]; 391 | *ay=((u16)buf[2]<<8)|buf[3]; 392 | *az=((u16)buf[4]<<8)|buf[5]; 393 | } 394 | return res; 395 | } 396 | //IIC连续写 397 | //addr:器件地址 398 | //reg:寄存器地址 399 | //len:写入长度 400 | //buf:数据区 401 | //返回值:1,成功 402 | // 0,失败 403 | u8 MPU6050_Write_Len(u8 reg,u8 len,u8 *buf) 404 | { 405 | u8 res; 406 | res=I2C_WriteBytes(reg, len, buf, MPU6050_ADDR); 407 | return res; 408 | } 409 | //IIC连续读 410 | //addr:器件地址 411 | //reg:要读取的寄存器地址 412 | //len:要读取的长度 413 | //buf:读取到的数据存储区 414 | //返回值:1,成功 415 | // 0,失败 416 | u8 MPU6050_Read_Len(u8 reg,u8 len,u8 *buf) 417 | { 418 | 419 | u8 res; 420 | res=I2C_ReadBytes(reg, len, buf, MPU6050_ADDR); 421 | return res; 422 | } 423 | //IIC写一个字节 424 | //reg:寄存器地址 425 | //data:数据 426 | //返回值:1,成功 427 | // 0,失败 428 | u8 MPU6050_Write_Byte(u8 reg,u8 data) 429 | { 430 | u8 res; 431 | res=I2C_WriteByte(reg, data, MPU6050_ADDR); 432 | return res; 433 | } 434 | //IIC读一个字节 435 | //reg:寄存器地址 436 | //返回值:读到的数据 437 | u8 MPU6050_Read_Byte(u8 reg) 438 | { 439 | u8 res; 440 | res=I2C_ReadByte(reg, MPU6050_ADDR); 441 | return res; 442 | } 443 | -------------------------------------------------------------------------------- /Libraries/CMSIS/startup_stm32f10x_md.s: -------------------------------------------------------------------------------- 1 | ;******************** (C) COPYRIGHT 2011 STMicroelectronics ******************** 2 | ;* File Name : startup_stm32f10x_md.s 3 | ;* Author : MCD Application Team 4 | ;* Version : V3.5.0 5 | ;* Date : 11-March-2011 6 | ;* Description : STM32F10x Medium Density Devices vector table for MDK-ARM 7 | ;* toolchain. 8 | ;* This module performs: 9 | ;* - Set the initial SP 10 | ;* - Set the initial PC == Reset_Handler 11 | ;* - Set the vector table entries with the exceptions ISR address 12 | ;* - Configure the clock system 13 | ;* - Branches to __main in the C library (which eventually 14 | ;* calls main()). 15 | ;* After Reset the CortexM3 processor is in Thread mode, 16 | ;* priority is Privileged, and the Stack is set to Main. 17 | ;* <<< Use Configuration Wizard in Context Menu >>> 18 | ;******************************************************************************* 19 | ; THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 20 | ; WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 21 | ; AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 22 | ; INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 23 | ; CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 24 | ; INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 25 | ;******************************************************************************* 26 | 27 | ; Amount of memory (in bytes) allocated for Stack 28 | ; Tailor this value to your application needs 29 | ; Stack Configuration 30 | ; Stack Size (in Bytes) <0x0-0xFFFFFFFF:8> 31 | ; 32 | 33 | Stack_Size EQU 0x00000400 34 | 35 | AREA STACK, NOINIT, READWRITE, ALIGN=3 36 | Stack_Mem SPACE Stack_Size 37 | __initial_sp 38 | 39 | 40 | ; Heap Configuration 41 | ; Heap Size (in Bytes) <0x0-0xFFFFFFFF:8> 42 | ; 43 | 44 | Heap_Size EQU 0x00000200 45 | 46 | AREA HEAP, NOINIT, READWRITE, ALIGN=3 47 | __heap_base 48 | Heap_Mem SPACE Heap_Size 49 | __heap_limit 50 | 51 | PRESERVE8 52 | THUMB 53 | 54 | 55 | ; Vector Table Mapped to Address 0 at Reset 56 | AREA RESET, DATA, READONLY 57 | EXPORT __Vectors 58 | EXPORT __Vectors_End 59 | EXPORT __Vectors_Size 60 | 61 | __Vectors DCD __initial_sp ; Top of Stack 62 | DCD Reset_Handler ; Reset Handler 63 | DCD NMI_Handler ; NMI Handler 64 | DCD HardFault_Handler ; Hard Fault Handler 65 | DCD MemManage_Handler ; MPU Fault Handler 66 | DCD BusFault_Handler ; Bus Fault Handler 67 | DCD UsageFault_Handler ; Usage Fault Handler 68 | DCD 0 ; Reserved 69 | DCD 0 ; Reserved 70 | DCD 0 ; Reserved 71 | DCD 0 ; Reserved 72 | DCD SVC_Handler ; SVCall Handler 73 | DCD DebugMon_Handler ; Debug Monitor Handler 74 | DCD 0 ; Reserved 75 | DCD PendSV_Handler ; PendSV Handler 76 | DCD SysTick_Handler ; SysTick Handler 77 | 78 | ; External Interrupts 79 | DCD WWDG_IRQHandler ; Window Watchdog 80 | DCD PVD_IRQHandler ; PVD through EXTI Line detect 81 | DCD TAMPER_IRQHandler ; Tamper 82 | DCD RTC_IRQHandler ; RTC 83 | DCD FLASH_IRQHandler ; Flash 84 | DCD RCC_IRQHandler ; RCC 85 | DCD EXTI0_IRQHandler ; EXTI Line 0 86 | DCD EXTI1_IRQHandler ; EXTI Line 1 87 | DCD EXTI2_IRQHandler ; EXTI Line 2 88 | DCD EXTI3_IRQHandler ; EXTI Line 3 89 | DCD EXTI4_IRQHandler ; EXTI Line 4 90 | DCD DMA1_Channel1_IRQHandler ; DMA1 Channel 1 91 | DCD DMA1_Channel2_IRQHandler ; DMA1 Channel 2 92 | DCD DMA1_Channel3_IRQHandler ; DMA1 Channel 3 93 | DCD DMA1_Channel4_IRQHandler ; DMA1 Channel 4 94 | DCD DMA1_Channel5_IRQHandler ; DMA1 Channel 5 95 | DCD DMA1_Channel6_IRQHandler ; DMA1 Channel 6 96 | DCD DMA1_Channel7_IRQHandler ; DMA1 Channel 7 97 | DCD ADC1_2_IRQHandler ; ADC1_2 98 | DCD USB_HP_CAN1_TX_IRQHandler ; USB High Priority or CAN1 TX 99 | DCD USB_LP_CAN1_RX0_IRQHandler ; USB Low Priority or CAN1 RX0 100 | DCD CAN1_RX1_IRQHandler ; CAN1 RX1 101 | DCD CAN1_SCE_IRQHandler ; CAN1 SCE 102 | DCD EXTI9_5_IRQHandler ; EXTI Line 9..5 103 | DCD TIM1_BRK_IRQHandler ; TIM1 Break 104 | DCD TIM1_UP_IRQHandler ; TIM1 Update 105 | DCD TIM1_TRG_COM_IRQHandler ; TIM1 Trigger and Commutation 106 | DCD TIM1_CC_IRQHandler ; TIM1 Capture Compare 107 | DCD TIM2_IRQHandler ; TIM2 108 | DCD TIM3_IRQHandler ; TIM3 109 | DCD TIM4_IRQHandler ; TIM4 110 | DCD I2C1_EV_IRQHandler ; I2C1 Event 111 | DCD I2C1_ER_IRQHandler ; I2C1 Error 112 | DCD I2C2_EV_IRQHandler ; I2C2 Event 113 | DCD I2C2_ER_IRQHandler ; I2C2 Error 114 | DCD SPI1_IRQHandler ; SPI1 115 | DCD SPI2_IRQHandler ; SPI2 116 | DCD USART1_IRQHandler ; USART1 117 | DCD USART2_IRQHandler ; USART2 118 | DCD USART3_IRQHandler ; USART3 119 | DCD EXTI15_10_IRQHandler ; EXTI Line 15..10 120 | DCD RTCAlarm_IRQHandler ; RTC Alarm through EXTI Line 121 | DCD USBWakeUp_IRQHandler ; USB Wakeup from suspend 122 | __Vectors_End 123 | 124 | __Vectors_Size EQU __Vectors_End - __Vectors 125 | 126 | AREA |.text|, CODE, READONLY 127 | 128 | ; Reset handler 129 | Reset_Handler PROC 130 | EXPORT Reset_Handler [WEAK] 131 | IMPORT __main 132 | IMPORT SystemInit 133 | LDR R0, =SystemInit 134 | BLX R0 135 | LDR R0, =__main 136 | BX R0 137 | ENDP 138 | 139 | ; Dummy Exception Handlers (infinite loops which can be modified) 140 | 141 | NMI_Handler PROC 142 | EXPORT NMI_Handler [WEAK] 143 | B . 144 | ENDP 145 | HardFault_Handler\ 146 | PROC 147 | EXPORT HardFault_Handler [WEAK] 148 | B . 149 | ENDP 150 | MemManage_Handler\ 151 | PROC 152 | EXPORT MemManage_Handler [WEAK] 153 | B . 154 | ENDP 155 | BusFault_Handler\ 156 | PROC 157 | EXPORT BusFault_Handler [WEAK] 158 | B . 159 | ENDP 160 | UsageFault_Handler\ 161 | PROC 162 | EXPORT UsageFault_Handler [WEAK] 163 | B . 164 | ENDP 165 | SVC_Handler PROC 166 | EXPORT SVC_Handler [WEAK] 167 | B . 168 | ENDP 169 | DebugMon_Handler\ 170 | PROC 171 | EXPORT DebugMon_Handler [WEAK] 172 | B . 173 | ENDP 174 | PendSV_Handler PROC 175 | EXPORT PendSV_Handler [WEAK] 176 | B . 177 | ENDP 178 | SysTick_Handler PROC 179 | EXPORT SysTick_Handler [WEAK] 180 | B . 181 | ENDP 182 | 183 | Default_Handler PROC 184 | 185 | EXPORT WWDG_IRQHandler [WEAK] 186 | EXPORT PVD_IRQHandler [WEAK] 187 | EXPORT TAMPER_IRQHandler [WEAK] 188 | EXPORT RTC_IRQHandler [WEAK] 189 | EXPORT FLASH_IRQHandler [WEAK] 190 | EXPORT RCC_IRQHandler [WEAK] 191 | EXPORT EXTI0_IRQHandler [WEAK] 192 | EXPORT EXTI1_IRQHandler [WEAK] 193 | EXPORT EXTI2_IRQHandler [WEAK] 194 | EXPORT EXTI3_IRQHandler [WEAK] 195 | EXPORT EXTI4_IRQHandler [WEAK] 196 | EXPORT DMA1_Channel1_IRQHandler [WEAK] 197 | EXPORT DMA1_Channel2_IRQHandler [WEAK] 198 | EXPORT DMA1_Channel3_IRQHandler [WEAK] 199 | EXPORT DMA1_Channel4_IRQHandler [WEAK] 200 | EXPORT DMA1_Channel5_IRQHandler [WEAK] 201 | EXPORT DMA1_Channel6_IRQHandler [WEAK] 202 | EXPORT DMA1_Channel7_IRQHandler [WEAK] 203 | EXPORT ADC1_2_IRQHandler [WEAK] 204 | EXPORT USB_HP_CAN1_TX_IRQHandler [WEAK] 205 | EXPORT USB_LP_CAN1_RX0_IRQHandler [WEAK] 206 | EXPORT CAN1_RX1_IRQHandler [WEAK] 207 | EXPORT CAN1_SCE_IRQHandler [WEAK] 208 | EXPORT EXTI9_5_IRQHandler [WEAK] 209 | EXPORT TIM1_BRK_IRQHandler [WEAK] 210 | EXPORT TIM1_UP_IRQHandler [WEAK] 211 | EXPORT TIM1_TRG_COM_IRQHandler [WEAK] 212 | EXPORT TIM1_CC_IRQHandler [WEAK] 213 | EXPORT TIM2_IRQHandler [WEAK] 214 | EXPORT TIM3_IRQHandler [WEAK] 215 | EXPORT TIM4_IRQHandler [WEAK] 216 | EXPORT I2C1_EV_IRQHandler [WEAK] 217 | EXPORT I2C1_ER_IRQHandler [WEAK] 218 | EXPORT I2C2_EV_IRQHandler [WEAK] 219 | EXPORT I2C2_ER_IRQHandler [WEAK] 220 | EXPORT SPI1_IRQHandler [WEAK] 221 | EXPORT SPI2_IRQHandler [WEAK] 222 | EXPORT USART1_IRQHandler [WEAK] 223 | EXPORT USART2_IRQHandler [WEAK] 224 | EXPORT USART3_IRQHandler [WEAK] 225 | EXPORT EXTI15_10_IRQHandler [WEAK] 226 | EXPORT RTCAlarm_IRQHandler [WEAK] 227 | EXPORT USBWakeUp_IRQHandler [WEAK] 228 | 229 | WWDG_IRQHandler 230 | PVD_IRQHandler 231 | TAMPER_IRQHandler 232 | RTC_IRQHandler 233 | FLASH_IRQHandler 234 | RCC_IRQHandler 235 | EXTI0_IRQHandler 236 | EXTI1_IRQHandler 237 | EXTI2_IRQHandler 238 | EXTI3_IRQHandler 239 | EXTI4_IRQHandler 240 | DMA1_Channel1_IRQHandler 241 | DMA1_Channel2_IRQHandler 242 | DMA1_Channel3_IRQHandler 243 | DMA1_Channel4_IRQHandler 244 | DMA1_Channel5_IRQHandler 245 | DMA1_Channel6_IRQHandler 246 | DMA1_Channel7_IRQHandler 247 | ADC1_2_IRQHandler 248 | USB_HP_CAN1_TX_IRQHandler 249 | USB_LP_CAN1_RX0_IRQHandler 250 | CAN1_RX1_IRQHandler 251 | CAN1_SCE_IRQHandler 252 | EXTI9_5_IRQHandler 253 | TIM1_BRK_IRQHandler 254 | TIM1_UP_IRQHandler 255 | TIM1_TRG_COM_IRQHandler 256 | TIM1_CC_IRQHandler 257 | TIM2_IRQHandler 258 | TIM3_IRQHandler 259 | TIM4_IRQHandler 260 | I2C1_EV_IRQHandler 261 | I2C1_ER_IRQHandler 262 | I2C2_EV_IRQHandler 263 | I2C2_ER_IRQHandler 264 | SPI1_IRQHandler 265 | SPI2_IRQHandler 266 | USART1_IRQHandler 267 | USART2_IRQHandler 268 | USART3_IRQHandler 269 | EXTI15_10_IRQHandler 270 | RTCAlarm_IRQHandler 271 | USBWakeUp_IRQHandler 272 | 273 | B . 274 | 275 | ENDP 276 | 277 | ALIGN 278 | 279 | ;******************************************************************************* 280 | ; User Stack and Heap initialization 281 | ;******************************************************************************* 282 | IF :DEF:__MICROLIB 283 | 284 | EXPORT __initial_sp 285 | EXPORT __heap_base 286 | EXPORT __heap_limit 287 | 288 | ELSE 289 | 290 | IMPORT __use_two_region_memory 291 | EXPORT __user_initial_stackheap 292 | 293 | __user_initial_stackheap 294 | 295 | LDR R0, = Heap_Mem 296 | LDR R1, =(Stack_Mem + Stack_Size) 297 | LDR R2, = (Heap_Mem + Heap_Size) 298 | LDR R3, = Stack_Mem 299 | BX LR 300 | 301 | ALIGN 302 | 303 | ENDIF 304 | 305 | END 306 | 307 | ;******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE***** 308 | -------------------------------------------------------------------------------- /APP/pwm/pwm.c: -------------------------------------------------------------------------------- 1 | #include "pwm.h" 2 | 3 | volatile u16 cntL = 0; 4 | volatile u16 cntR = 0; 5 | 6 | /******************************************************************************* 7 | * 函 数 名 : TIM1_CH1~4_PWM_Init 8 | * 函数功能 : TIM1通道1~4 PWM初始化函数 9 | * 输 入 : arr:重装载值 10 | psc:分频系数 11 | * 输 出 : 无 12 | *******************************************************************************/ 13 | void TIM1_PWM_Init(u16 arr, u16 psc) 14 | { 15 | GPIO_InitTypeDef GPIO_InitStructure; 16 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 17 | TIM_OCInitTypeDef TIM_OCInitStructure; 18 | 19 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能定时器1时钟 20 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIO外设时钟 21 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); 22 | 23 | //设置该引脚为复用输出功能,输出TIM1 CH1234的PWM脉冲波形 GPIOA 8 9 10 11 24 | GPIO_InitStructure.GPIO_Pin = PWM_PIN_TIM1; 25 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 26 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 27 | GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIO 28 | GPIO_SetBits(GPIOA, PWM_PIN_TIM1); 29 | 30 | //GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE);//改变指定管脚的映射 31 | 32 | TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 33 | TIM_TimeBaseStructure.TIM_Prescaler = psc; //设置用来作为TIMx时钟频率除数的预分频值 34 | TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim 35 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 36 | TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 37 | 38 | //初始化TIM1 Channel1~4 PWM模式 39 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2 40 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 41 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//TIM_OCPolarity_Low //输出极性:TIM输出比较极性高 42 | TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC1 43 | TIM_OC2Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC2 44 | TIM_OC3Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC3 45 | TIM_OC4Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC4 46 | 47 | TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM1在CCR1上的预装载寄存器 48 | TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM1在CCR2上的预装载寄存器 49 | TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM1在CCR3上的预装载寄存器 50 | TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM1在CCR4上的预装载寄存器 51 | 52 | TIM_Cmd(TIM1, ENABLE); //使能TIM1 53 | TIM_CtrlPWMOutputs(TIM1, ENABLE); 54 | } 55 | 56 | void TIM3_PWM_Init(u16 arr, u16 psc) 57 | { 58 | GPIO_InitTypeDef GPIO_InitStructure; 59 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 60 | TIM_OCInitTypeDef TIM_OCInitStructure; 61 | 62 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能定时器1时钟 63 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIO外设时钟 64 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); 65 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); 66 | 67 | //设置该引脚为复用输出功能,输出TIM3 CH1234的PWM脉冲波形 GPIOA 6 7 GPIOB 0 1 68 | GPIO_InitStructure.GPIO_Pin = PWM_PIN_TIM3_A; 69 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 70 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 71 | GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA 72 | //GPIO_SetBits(GPIOA, PWM_PIN_TIM3_A); 73 | 74 | GPIO_InitStructure.GPIO_Pin = PWM_PIN_TIM3_B; 75 | GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIOB 76 | //GPIO_SetBits(GPIOB, PWM_PIN_TIM3_B); 77 | 78 | //GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE);//改变指定管脚的映射 79 | 80 | TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 81 | TIM_TimeBaseStructure.TIM_Prescaler = psc; //设置用来作为TIMx时钟频率除数的预分频值 82 | TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim 83 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 84 | TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 85 | 86 | //初始化TIM1 Channel1~4 PWM模式 87 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2 88 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 89 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//TIM_OCPolarity_Low //输出极性:TIM输出比较极性高 90 | TIM_OC1Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC1 91 | TIM_OC2Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2 92 | TIM_OC3Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC3 93 | TIM_OC4Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC4 94 | 95 | TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIM3在CCR1上的预装载寄存器 96 | TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器 97 | TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIM3在CCR3上的预装载寄存器 98 | TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIM3在CCR4上的预装载寄存器 99 | TIM_ARRPreloadConfig(TIM3,ENABLE);//使能预装载寄存器 100 | 101 | TIM_Cmd(TIM3, ENABLE); //使能TIM3 102 | } 103 | 104 | void TIM2_CH12_PWM_Init(u16 arr,u16 psc){ 105 | GPIO_InitTypeDef GPIO_InitStructure; 106 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 107 | TIM_OCInitTypeDef TIM_OCInitStructure; 108 | 109 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能定时器2时钟 110 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIO外设时钟 111 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); 112 | 113 | //设置该引脚为复用输出功能,输出TIM2 CH12的PWM脉冲波形 GPIOA 0 1 114 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; 115 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 116 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 117 | GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA 118 | 119 | 120 | //GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE);//改变指定管脚的映射 121 | 122 | TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 123 | TIM_TimeBaseStructure.TIM_Prescaler = psc; //设置用来作为TIMx时钟频率除数的预分频值 124 | TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim 125 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 126 | TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 127 | 128 | //初始化TIM2 Channel1~2 PWM模式 129 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2 130 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 131 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//TIM_OCPolarity_Low //输出极性:TIM输出比较极性高 132 | TIM_OC1Init(TIM2, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM2 OC1 133 | TIM_OC2Init(TIM2, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM2 OC2 134 | 135 | TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM3在CCR1上的预装载寄存器 136 | TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器 137 | TIM_ARRPreloadConfig(TIM2,ENABLE);//使能预装载寄存器 138 | 139 | TIM_Cmd(TIM2, ENABLE); //使能TIM2 140 | } 141 | 142 | void TIM2_CH34_Input_Init(u16 arr,u16 psc) 143 | { 144 | TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; 145 | TIM_ICInitTypeDef TIM_ICInitStructure; 146 | NVIC_InitTypeDef NVIC_InitStructure; 147 | GPIO_InitTypeDef GPIO_InitStructure; 148 | 149 | // 开启TIMx_CLK,x[1,8] 150 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); 151 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); 152 | 153 | GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3;//管脚设置 154 | GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD; //设置下拉输入模式 155 | GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化GPIO */ 156 | 157 | //TIM_TimeBaseInitStructure.TIM_Period = 0xFFFF-1; 158 | // 定时器时钟源TIMxCLK = HCLK=72MHz 159 | // 设定定时器频率为=TIMxCLK/(TIM_Prescaler+1)=10KHz 160 | //TIM_TimeBaseInitStructure.TIM_Prescaler = 7200-1; 161 | // 计数方式 162 | TIM_TimeBaseInitStructure.TIM_Period=arr; //自动装载值 163 | TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //分频系数 164 | TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; 165 | TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 166 | // 初始化定时器TIMx, x[1,8] 167 | TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure); 168 | 169 | /* IC1捕获:上升沿触发 TI1FP1 */ 170 | TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; 171 | TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; 172 | TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; 173 | TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; 174 | TIM_ICInitStructure.TIM_ICFilter = 0xF; //重要 否则数据非常飘 175 | TIM_ICInit(TIM2,&TIM_ICInitStructure); 176 | TIM_ITConfig(TIM2,TIM_IT_CC3,ENABLE); 177 | 178 | /* IC2捕获:下降沿触发 TI1FP2 */ 179 | TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; 180 | TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; 181 | TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; 182 | TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; 183 | TIM_ICInitStructure.TIM_ICFilter = 0xF; //重要 否则数据非常飘 184 | TIM_ICInit(TIM2,&TIM_ICInitStructure); 185 | TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC3|TIM_IT_CC4,ENABLE); 186 | //TIM_ARRPreloadConfig(TIM2,ENABLE);//使能预装载寄存器 187 | 188 | NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;//中断通道 189 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级 190 | NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级 191 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 192 | NVIC_Init(&NVIC_InitStructure); 193 | 194 | /* 使能定时器 */ 195 | //TIM_Cmd(TIM2, ENABLE); 196 | } 197 | 198 | void TIM4_CH12_PWM_Init(u16 arr,u16 psc){ 199 | GPIO_InitTypeDef GPIO_InitStructure; 200 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 201 | TIM_OCInitTypeDef TIM_OCInitStructure; 202 | 203 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能定时器2时钟 204 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能GPIO外设时钟 205 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); 206 | 207 | //设置该引脚为复用输出功能,输出TIM2 CH12的PWM脉冲波形 GPIOB 6 7 208 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; 209 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 210 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 211 | GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIOA 212 | 213 | 214 | //GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE);//改变指定管脚的映射 215 | 216 | TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 217 | TIM_TimeBaseStructure.TIM_Prescaler = psc; //设置用来作为TIMx时钟频率除数的预分频值 218 | TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim 219 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 220 | TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 221 | 222 | //初始化TIM2 Channel1~2 PWM模式 223 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2 224 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 225 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//TIM_OCPolarity_Low //输出极性:TIM输出比较极性高 226 | TIM_OC1Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM2 OC1 227 | TIM_OC2Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM2 OC2 228 | 229 | TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM3在CCR1上的预装载寄存器 230 | TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器 231 | TIM_ARRPreloadConfig(TIM4,ENABLE);//使能预装载寄存器 232 | 233 | TIM_Cmd(TIM4, ENABLE); //使能TIM2 234 | } 235 | 236 | void startSpeedCount(void){ 237 | cntL = 0; 238 | cntR = 0; 239 | TIM_SetCounter(TIM2,0); 240 | TIM_Cmd(TIM2, ENABLE); 241 | } 242 | 243 | /******************************************************************************* 244 | * 函 数 名 : TIM5_IRQHandler 245 | * 函数功能 : TIM5中断函数 246 | * 输 入 : 无 247 | * 输 出 : 无 248 | *******************************************************************************/ 249 | void TIM2_IRQHandler(void) 250 | { 251 | /* 获取输入捕获值 */ 252 | //IC3Value = TIM_GetCapture3(TIM2); 253 | //IC4Value = TIM_GetCapture4(TIM2); 254 | 255 | if(TIM_GetITStatus(TIM2,TIM_IT_CC3)) //发生捕获中断 256 | { 257 | /* 清除定时器捕获中断 */ 258 | TIM_ClearITPendingBit(TIM2, TIM_IT_CC3); 259 | cntL++; 260 | } 261 | if(TIM_GetITStatus(TIM2,TIM_IT_CC4)) //发生捕获中断 262 | { 263 | /* 清除定时器捕获中断 */ 264 | TIM_ClearITPendingBit(TIM2, TIM_IT_CC4); 265 | cntR++; 266 | } 267 | if(TIM_GetITStatus(TIM2,TIM_IT_Update)) //发生溢出中断 268 | { 269 | /* 清除定时器溢出中断 */ 270 | TIM_ClearITPendingBit(TIM2,TIM_IT_Update); 271 | TIM_Cmd(TIM2, DISABLE); 272 | } 273 | 274 | // /* 频率计算 */ 275 | // if (IC3Value>IC3Value0) 276 | // { 277 | // FreqL = 2000/(float)(IC3Value-IC3Value0); 278 | // IC3Value0 = IC3Value; 279 | // } 280 | // else if (IC3Value
© COPYRIGHT 2011 STMicroelectronics
20 | ****************************************************************************** 21 | */ 22 | 23 | /* Define to prevent recursive inclusion -------------------------------------*/ 24 | #ifndef __STM32F10x_USART_H 25 | #define __STM32F10x_USART_H 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | /* Includes ------------------------------------------------------------------*/ 32 | #include "stm32f10x.h" 33 | 34 | /** @addtogroup STM32F10x_StdPeriph_Driver 35 | * @{ 36 | */ 37 | 38 | /** @addtogroup USART 39 | * @{ 40 | */ 41 | 42 | /** @defgroup USART_Exported_Types 43 | * @{ 44 | */ 45 | 46 | /** 47 | * @brief USART Init Structure definition 48 | */ 49 | 50 | typedef struct 51 | { 52 | uint32_t USART_BaudRate; /*!< This member configures the USART communication baud rate. 53 | The baud rate is computed using the following formula: 54 | - IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->USART_BaudRate))) 55 | - FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */ 56 | 57 | uint16_t USART_WordLength; /*!< Specifies the number of data bits transmitted or received in a frame. 58 | This parameter can be a value of @ref USART_Word_Length */ 59 | 60 | uint16_t USART_StopBits; /*!< Specifies the number of stop bits transmitted. 61 | This parameter can be a value of @ref USART_Stop_Bits */ 62 | 63 | uint16_t USART_Parity; /*!< Specifies the parity mode. 64 | This parameter can be a value of @ref USART_Parity 65 | @note When parity is enabled, the computed parity is inserted 66 | at the MSB position of the transmitted data (9th bit when 67 | the word length is set to 9 data bits; 8th bit when the 68 | word length is set to 8 data bits). */ 69 | 70 | uint16_t USART_Mode; /*!< Specifies wether the Receive or Transmit mode is enabled or disabled. 71 | This parameter can be a value of @ref USART_Mode */ 72 | 73 | uint16_t USART_HardwareFlowControl; /*!< Specifies wether the hardware flow control mode is enabled 74 | or disabled. 75 | This parameter can be a value of @ref USART_Hardware_Flow_Control */ 76 | } USART_InitTypeDef; 77 | 78 | /** 79 | * @brief USART Clock Init Structure definition 80 | */ 81 | 82 | typedef struct 83 | { 84 | 85 | uint16_t USART_Clock; /*!< Specifies whether the USART clock is enabled or disabled. 86 | This parameter can be a value of @ref USART_Clock */ 87 | 88 | uint16_t USART_CPOL; /*!< Specifies the steady state value of the serial clock. 89 | This parameter can be a value of @ref USART_Clock_Polarity */ 90 | 91 | uint16_t USART_CPHA; /*!< Specifies the clock transition on which the bit capture is made. 92 | This parameter can be a value of @ref USART_Clock_Phase */ 93 | 94 | uint16_t USART_LastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted 95 | data bit (MSB) has to be output on the SCLK pin in synchronous mode. 96 | This parameter can be a value of @ref USART_Last_Bit */ 97 | } USART_ClockInitTypeDef; 98 | 99 | /** 100 | * @} 101 | */ 102 | 103 | /** @defgroup USART_Exported_Constants 104 | * @{ 105 | */ 106 | 107 | #define IS_USART_ALL_PERIPH(PERIPH) (((PERIPH) == USART1) || \ 108 | ((PERIPH) == USART2) || \ 109 | ((PERIPH) == USART3) || \ 110 | ((PERIPH) == UART4) || \ 111 | ((PERIPH) == UART5)) 112 | 113 | #define IS_USART_123_PERIPH(PERIPH) (((PERIPH) == USART1) || \ 114 | ((PERIPH) == USART2) || \ 115 | ((PERIPH) == USART3)) 116 | 117 | #define IS_USART_1234_PERIPH(PERIPH) (((PERIPH) == USART1) || \ 118 | ((PERIPH) == USART2) || \ 119 | ((PERIPH) == USART3) || \ 120 | ((PERIPH) == UART4)) 121 | /** @defgroup USART_Word_Length 122 | * @{ 123 | */ 124 | 125 | #define USART_WordLength_8b ((uint16_t)0x0000) 126 | #define USART_WordLength_9b ((uint16_t)0x1000) 127 | 128 | #define IS_USART_WORD_LENGTH(LENGTH) (((LENGTH) == USART_WordLength_8b) || \ 129 | ((LENGTH) == USART_WordLength_9b)) 130 | /** 131 | * @} 132 | */ 133 | 134 | /** @defgroup USART_Stop_Bits 135 | * @{ 136 | */ 137 | 138 | #define USART_StopBits_1 ((uint16_t)0x0000) 139 | #define USART_StopBits_0_5 ((uint16_t)0x1000) 140 | #define USART_StopBits_2 ((uint16_t)0x2000) 141 | #define USART_StopBits_1_5 ((uint16_t)0x3000) 142 | #define IS_USART_STOPBITS(STOPBITS) (((STOPBITS) == USART_StopBits_1) || \ 143 | ((STOPBITS) == USART_StopBits_0_5) || \ 144 | ((STOPBITS) == USART_StopBits_2) || \ 145 | ((STOPBITS) == USART_StopBits_1_5)) 146 | /** 147 | * @} 148 | */ 149 | 150 | /** @defgroup USART_Parity 151 | * @{ 152 | */ 153 | 154 | #define USART_Parity_No ((uint16_t)0x0000) 155 | #define USART_Parity_Even ((uint16_t)0x0400) 156 | #define USART_Parity_Odd ((uint16_t)0x0600) 157 | #define IS_USART_PARITY(PARITY) (((PARITY) == USART_Parity_No) || \ 158 | ((PARITY) == USART_Parity_Even) || \ 159 | ((PARITY) == USART_Parity_Odd)) 160 | /** 161 | * @} 162 | */ 163 | 164 | /** @defgroup USART_Mode 165 | * @{ 166 | */ 167 | 168 | #define USART_Mode_Rx ((uint16_t)0x0004) 169 | #define USART_Mode_Tx ((uint16_t)0x0008) 170 | #define IS_USART_MODE(MODE) ((((MODE) & (uint16_t)0xFFF3) == 0x00) && ((MODE) != (uint16_t)0x00)) 171 | /** 172 | * @} 173 | */ 174 | 175 | /** @defgroup USART_Hardware_Flow_Control 176 | * @{ 177 | */ 178 | #define USART_HardwareFlowControl_None ((uint16_t)0x0000) 179 | #define USART_HardwareFlowControl_RTS ((uint16_t)0x0100) 180 | #define USART_HardwareFlowControl_CTS ((uint16_t)0x0200) 181 | #define USART_HardwareFlowControl_RTS_CTS ((uint16_t)0x0300) 182 | #define IS_USART_HARDWARE_FLOW_CONTROL(CONTROL)\ 183 | (((CONTROL) == USART_HardwareFlowControl_None) || \ 184 | ((CONTROL) == USART_HardwareFlowControl_RTS) || \ 185 | ((CONTROL) == USART_HardwareFlowControl_CTS) || \ 186 | ((CONTROL) == USART_HardwareFlowControl_RTS_CTS)) 187 | /** 188 | * @} 189 | */ 190 | 191 | /** @defgroup USART_Clock 192 | * @{ 193 | */ 194 | #define USART_Clock_Disable ((uint16_t)0x0000) 195 | #define USART_Clock_Enable ((uint16_t)0x0800) 196 | #define IS_USART_CLOCK(CLOCK) (((CLOCK) == USART_Clock_Disable) || \ 197 | ((CLOCK) == USART_Clock_Enable)) 198 | /** 199 | * @} 200 | */ 201 | 202 | /** @defgroup USART_Clock_Polarity 203 | * @{ 204 | */ 205 | 206 | #define USART_CPOL_Low ((uint16_t)0x0000) 207 | #define USART_CPOL_High ((uint16_t)0x0400) 208 | #define IS_USART_CPOL(CPOL) (((CPOL) == USART_CPOL_Low) || ((CPOL) == USART_CPOL_High)) 209 | 210 | /** 211 | * @} 212 | */ 213 | 214 | /** @defgroup USART_Clock_Phase 215 | * @{ 216 | */ 217 | 218 | #define USART_CPHA_1Edge ((uint16_t)0x0000) 219 | #define USART_CPHA_2Edge ((uint16_t)0x0200) 220 | #define IS_USART_CPHA(CPHA) (((CPHA) == USART_CPHA_1Edge) || ((CPHA) == USART_CPHA_2Edge)) 221 | 222 | /** 223 | * @} 224 | */ 225 | 226 | /** @defgroup USART_Last_Bit 227 | * @{ 228 | */ 229 | 230 | #define USART_LastBit_Disable ((uint16_t)0x0000) 231 | #define USART_LastBit_Enable ((uint16_t)0x0100) 232 | #define IS_USART_LASTBIT(LASTBIT) (((LASTBIT) == USART_LastBit_Disable) || \ 233 | ((LASTBIT) == USART_LastBit_Enable)) 234 | /** 235 | * @} 236 | */ 237 | 238 | /** @defgroup USART_Interrupt_definition 239 | * @{ 240 | */ 241 | 242 | #define USART_IT_PE ((uint16_t)0x0028) 243 | #define USART_IT_TXE ((uint16_t)0x0727) 244 | #define USART_IT_TC ((uint16_t)0x0626) 245 | #define USART_IT_RXNE ((uint16_t)0x0525) 246 | #define USART_IT_IDLE ((uint16_t)0x0424) 247 | #define USART_IT_LBD ((uint16_t)0x0846) 248 | #define USART_IT_CTS ((uint16_t)0x096A) 249 | #define USART_IT_ERR ((uint16_t)0x0060) 250 | #define USART_IT_ORE ((uint16_t)0x0360) 251 | #define USART_IT_NE ((uint16_t)0x0260) 252 | #define USART_IT_FE ((uint16_t)0x0160) 253 | #define IS_USART_CONFIG_IT(IT) (((IT) == USART_IT_PE) || ((IT) == USART_IT_TXE) || \ 254 | ((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \ 255 | ((IT) == USART_IT_IDLE) || ((IT) == USART_IT_LBD) || \ 256 | ((IT) == USART_IT_CTS) || ((IT) == USART_IT_ERR)) 257 | #define IS_USART_GET_IT(IT) (((IT) == USART_IT_PE) || ((IT) == USART_IT_TXE) || \ 258 | ((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \ 259 | ((IT) == USART_IT_IDLE) || ((IT) == USART_IT_LBD) || \ 260 | ((IT) == USART_IT_CTS) || ((IT) == USART_IT_ORE) || \ 261 | ((IT) == USART_IT_NE) || ((IT) == USART_IT_FE)) 262 | #define IS_USART_CLEAR_IT(IT) (((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \ 263 | ((IT) == USART_IT_LBD) || ((IT) == USART_IT_CTS)) 264 | /** 265 | * @} 266 | */ 267 | 268 | /** @defgroup USART_DMA_Requests 269 | * @{ 270 | */ 271 | 272 | #define USART_DMAReq_Tx ((uint16_t)0x0080) 273 | #define USART_DMAReq_Rx ((uint16_t)0x0040) 274 | #define IS_USART_DMAREQ(DMAREQ) ((((DMAREQ) & (uint16_t)0xFF3F) == 0x00) && ((DMAREQ) != (uint16_t)0x00)) 275 | 276 | /** 277 | * @} 278 | */ 279 | 280 | /** @defgroup USART_WakeUp_methods 281 | * @{ 282 | */ 283 | 284 | #define USART_WakeUp_IdleLine ((uint16_t)0x0000) 285 | #define USART_WakeUp_AddressMark ((uint16_t)0x0800) 286 | #define IS_USART_WAKEUP(WAKEUP) (((WAKEUP) == USART_WakeUp_IdleLine) || \ 287 | ((WAKEUP) == USART_WakeUp_AddressMark)) 288 | /** 289 | * @} 290 | */ 291 | 292 | /** @defgroup USART_LIN_Break_Detection_Length 293 | * @{ 294 | */ 295 | 296 | #define USART_LINBreakDetectLength_10b ((uint16_t)0x0000) 297 | #define USART_LINBreakDetectLength_11b ((uint16_t)0x0020) 298 | #define IS_USART_LIN_BREAK_DETECT_LENGTH(LENGTH) \ 299 | (((LENGTH) == USART_LINBreakDetectLength_10b) || \ 300 | ((LENGTH) == USART_LINBreakDetectLength_11b)) 301 | /** 302 | * @} 303 | */ 304 | 305 | /** @defgroup USART_IrDA_Low_Power 306 | * @{ 307 | */ 308 | 309 | #define USART_IrDAMode_LowPower ((uint16_t)0x0004) 310 | #define USART_IrDAMode_Normal ((uint16_t)0x0000) 311 | #define IS_USART_IRDA_MODE(MODE) (((MODE) == USART_IrDAMode_LowPower) || \ 312 | ((MODE) == USART_IrDAMode_Normal)) 313 | /** 314 | * @} 315 | */ 316 | 317 | /** @defgroup USART_Flags 318 | * @{ 319 | */ 320 | 321 | #define USART_FLAG_CTS ((uint16_t)0x0200) 322 | #define USART_FLAG_LBD ((uint16_t)0x0100) 323 | #define USART_FLAG_TXE ((uint16_t)0x0080) 324 | #define USART_FLAG_TC ((uint16_t)0x0040) 325 | #define USART_FLAG_RXNE ((uint16_t)0x0020) 326 | #define USART_FLAG_IDLE ((uint16_t)0x0010) 327 | #define USART_FLAG_ORE ((uint16_t)0x0008) 328 | #define USART_FLAG_NE ((uint16_t)0x0004) 329 | #define USART_FLAG_FE ((uint16_t)0x0002) 330 | #define USART_FLAG_PE ((uint16_t)0x0001) 331 | #define IS_USART_FLAG(FLAG) (((FLAG) == USART_FLAG_PE) || ((FLAG) == USART_FLAG_TXE) || \ 332 | ((FLAG) == USART_FLAG_TC) || ((FLAG) == USART_FLAG_RXNE) || \ 333 | ((FLAG) == USART_FLAG_IDLE) || ((FLAG) == USART_FLAG_LBD) || \ 334 | ((FLAG) == USART_FLAG_CTS) || ((FLAG) == USART_FLAG_ORE) || \ 335 | ((FLAG) == USART_FLAG_NE) || ((FLAG) == USART_FLAG_FE)) 336 | 337 | #define IS_USART_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0xFC9F) == 0x00) && ((FLAG) != (uint16_t)0x00)) 338 | #define IS_USART_PERIPH_FLAG(PERIPH, USART_FLAG) ((((*(uint32_t*)&(PERIPH)) != UART4_BASE) &&\ 339 | ((*(uint32_t*)&(PERIPH)) != UART5_BASE)) \ 340 | || ((USART_FLAG) != USART_FLAG_CTS)) 341 | #define IS_USART_BAUDRATE(BAUDRATE) (((BAUDRATE) > 0) && ((BAUDRATE) < 0x0044AA21)) 342 | #define IS_USART_ADDRESS(ADDRESS) ((ADDRESS) <= 0xF) 343 | #define IS_USART_DATA(DATA) ((DATA) <= 0x1FF) 344 | 345 | /** 346 | * @} 347 | */ 348 | 349 | /** 350 | * @} 351 | */ 352 | 353 | /** @defgroup USART_Exported_Macros 354 | * @{ 355 | */ 356 | 357 | /** 358 | * @} 359 | */ 360 | 361 | /** @defgroup USART_Exported_Functions 362 | * @{ 363 | */ 364 | 365 | void USART_DeInit(USART_TypeDef* USARTx); 366 | void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct); 367 | void USART_StructInit(USART_InitTypeDef* USART_InitStruct); 368 | void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct); 369 | void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct); 370 | void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState); 371 | void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState); 372 | void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState); 373 | void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address); 374 | void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp); 375 | void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState); 376 | void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength); 377 | void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState); 378 | void USART_SendData(USART_TypeDef* USARTx, uint16_t Data); 379 | uint16_t USART_ReceiveData(USART_TypeDef* USARTx); 380 | void USART_SendBreak(USART_TypeDef* USARTx); 381 | void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime); 382 | void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler); 383 | void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState); 384 | void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState); 385 | void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState); 386 | void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState); 387 | void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState); 388 | void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode); 389 | void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState); 390 | FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG); 391 | void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG); 392 | ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT); 393 | void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT); 394 | 395 | #ifdef __cplusplus 396 | } 397 | #endif 398 | 399 | #endif /* __STM32F10x_USART_H */ 400 | /** 401 | * @} 402 | */ 403 | 404 | /** 405 | * @} 406 | */ 407 | 408 | /** 409 | * @} 410 | */ 411 | 412 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 413 | --------------------------------------------------------------------------------