usb_bsp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /**
  2. ******************************************************************************
  3. * @file usb_bsp.c
  4. * @author MCD Application Team
  5. * @version V2.1.0
  6. * @date 19-March-2012
  7. * @brief This file implements the board support package for the USB host library
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  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. /* Includes ------------------------------------------------------------------*/
  28. #include "usb_bsp.h"
  29. /** @addtogroup USBH_USER
  30. * @{
  31. */
  32. /** @defgroup USB_BSP
  33. * @brief This file is responsible to offer board support package
  34. * @{
  35. */
  36. /** @defgroup USB_BSP_Private_Defines
  37. * @{
  38. */
  39. #define USE_ACCURATE_TIME
  40. #define TIM_MSEC_DELAY 0x01
  41. #define TIM_USEC_DELAY 0x02
  42. #if 0 /* 安富莱STM32-V5 不支持USB HOST接口的供电控制和过流检测 */
  43. #define HOST_OVRCURR_PORT GPIOE
  44. #define HOST_OVRCURR_LINE GPIO_Pin_1
  45. #define HOST_OVRCURR_PORT_SOURCE GPIO_PortSourceGPIOE
  46. #define HOST_OVRCURR_PIN_SOURCE GPIO_PinSource1
  47. #define HOST_OVRCURR_PORT_RCC RCC_APB2Periph_GPIOE
  48. #define HOST_OVRCURR_EXTI_LINE EXTI_Line1
  49. #define HOST_OVRCURR_IRQn EXTI1_IRQn
  50. #ifdef USE_STM3210C_EVAL
  51. #define HOST_POWERSW_PORT_RCC RCC_APB2Periph_GPIOC
  52. #define HOST_POWERSW_PORT GPIOC
  53. #define HOST_POWERSW_VBUS GPIO_Pin_9
  54. #else
  55. #ifdef USE_USB_OTG_FS
  56. #define HOST_POWERSW_PORT_RCC RCC_AHB1Periph_GPIOH
  57. #define HOST_POWERSW_PORT GPIOH
  58. #define HOST_POWERSW_VBUS GPIO_Pin_5
  59. #endif
  60. #endif
  61. #define HOST_SOF_OUTPUT_RCC RCC_APB2Periph_GPIOA
  62. #define HOST_SOF_PORT GPIOA
  63. #define HOST_SOF_SIGNAL GPIO_Pin_8
  64. #endif
  65. /**
  66. * @}
  67. */
  68. /** @defgroup USB_BSP_Private_TypesDefinitions
  69. * @{
  70. */
  71. /**
  72. * @}
  73. */
  74. /** @defgroup USB_BSP_Private_Macros
  75. * @{
  76. */
  77. /**
  78. * @}
  79. */
  80. /** @defgroup USBH_BSP_Private_Variables
  81. * @{
  82. */
  83. ErrorStatus HSEStartUpStatus;
  84. #ifdef USE_ACCURATE_TIME
  85. __IO uint32_t BSP_delay = 0;
  86. #endif
  87. /**
  88. * @}
  89. */
  90. /** @defgroup USBH_BSP_Private_FunctionPrototypes
  91. * @{
  92. */
  93. #ifdef USE_ACCURATE_TIME
  94. static void BSP_SetTime(uint8_t Unit);
  95. static void BSP_Delay(uint32_t nTime, uint8_t Unit);
  96. static void USB_OTG_BSP_TimeInit(void);
  97. #endif
  98. /**
  99. * @}
  100. */
  101. /** @defgroup USB_BSP_Private_Functions
  102. * @{
  103. */
  104. /**
  105. * @brief USB_OTG_BSP_Init
  106. * Initilizes BSP configurations
  107. * @param None
  108. * @retval None
  109. */
  110. void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev)
  111. {
  112. // EXTI_InitTypeDef EXTI_InitStructure;
  113. #ifdef USE_STM3210C_EVAL
  114. RCC_OTGFSCLKConfig(RCC_OTGFSCLKSource_PLLVCO_Div3);
  115. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, ENABLE);
  116. #else // USE_STM322xG_EVAL
  117. GPIO_InitTypeDef GPIO_InitStructure;
  118. #ifdef USE_USB_OTG_FS
  119. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  120. /* Configure SOF VBUS ID DM DP Pins */
  121. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11 | GPIO_Pin_12;
  122. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  123. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  124. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  125. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  126. GPIO_Init(GPIOA, &GPIO_InitStructure);
  127. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  128. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  129. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  130. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  131. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  132. GPIO_Init(GPIOA, &GPIO_InitStructure);
  133. GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_OTG1_FS);
  134. GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_OTG1_FS);
  135. GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_OTG1_FS);
  136. /* this for ID line debug */
  137. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  138. GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  139. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  140. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  141. GPIO_Init(GPIOA, &GPIO_InitStructure);
  142. GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_OTG1_FS);
  143. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  144. RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE);
  145. #else // USE_USB_OTG_HS
  146. #ifdef USE_ULPI_PHY // ULPI
  147. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB |
  148. RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOH |
  149. RCC_AHB1Periph_GPIOI,
  150. ENABLE);
  151. GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_OTG2_HS); // D0
  152. GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_OTG2_HS); // CLK
  153. GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_OTG2_HS); // D1
  154. GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_OTG2_HS); // D2
  155. GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_OTG2_HS); // D7
  156. GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_OTG2_HS); // D3
  157. GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_OTG2_HS); // D4
  158. GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_OTG2_HS); // D5
  159. GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_OTG2_HS); // D6
  160. GPIO_PinAFConfig(GPIOH, GPIO_PinSource4, GPIO_AF_OTG2_HS); // NXT
  161. GPIO_PinAFConfig(GPIOI, GPIO_PinSource11, GPIO_AF_OTG2_HS); // DIR
  162. GPIO_PinAFConfig(GPIOC, GPIO_PinSource0, GPIO_AF_OTG2_HS); // STP
  163. // CLK
  164. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  165. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  166. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  167. GPIO_Init(GPIOA, &GPIO_InitStructure);
  168. // D0
  169. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  170. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  171. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  172. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  173. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  174. GPIO_Init(GPIOA, &GPIO_InitStructure);
  175. // D1 D2 D3 D4 D5 D6 D7
  176. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 |
  177. GPIO_Pin_5 | GPIO_Pin_10 |
  178. GPIO_Pin_11 | GPIO_Pin_12 |
  179. GPIO_Pin_13;
  180. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  181. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  182. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  183. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  184. GPIO_Init(GPIOB, &GPIO_InitStructure);
  185. // STP
  186. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  187. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  188. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  189. GPIO_Init(GPIOC, &GPIO_InitStructure);
  190. // NXT
  191. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  192. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  193. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  194. GPIO_Init(GPIOH, &GPIO_InitStructure);
  195. // DIR
  196. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  197. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  198. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  199. GPIO_Init(GPIOI, &GPIO_InitStructure);
  200. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_OTG_HS |
  201. RCC_AHB1Periph_OTG_HS_ULPI,
  202. ENABLE);
  203. #else
  204. #if 1
  205. /* 安富莱只使用2根线 PB14/OTG_HS_DM 和 PB15/OTG_HS_DP 接U盘 */
  206. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  207. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15;
  208. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  209. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  210. GPIO_Init(GPIOB, &GPIO_InitStructure);
  211. GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_OTG2_FS);
  212. GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_OTG2_FS);
  213. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_OTG_HS, ENABLE);
  214. #else
  215. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  216. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 |
  217. GPIO_Pin_13 |
  218. GPIO_Pin_14 |
  219. GPIO_Pin_15;
  220. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  221. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  222. GPIO_Init(GPIOB, &GPIO_InitStructure);
  223. GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_OTG2_FS);
  224. GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_OTG2_FS);
  225. GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_OTG2_FS);
  226. GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_OTG2_FS);
  227. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_OTG_HS, ENABLE);
  228. #endif
  229. #endif
  230. #endif // USB_OTG_HS
  231. #endif // USE_STM322xG_EVAL
  232. /* Intialize Timer for delay function */
  233. USB_OTG_BSP_TimeInit();
  234. }
  235. /**
  236. * @brief USB_OTG_BSP_EnableInterrupt
  237. * Configures USB Global interrupt
  238. * @param None
  239. * @retval None
  240. */
  241. void USB_OTG_BSP_EnableInterrupt(USB_OTG_CORE_HANDLE *pdev)
  242. {
  243. NVIC_InitTypeDef NVIC_InitStructure;
  244. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  245. #ifdef USE_USB_OTG_HS
  246. NVIC_InitStructure.NVIC_IRQChannel = OTG_HS_IRQn;
  247. #else
  248. NVIC_InitStructure.NVIC_IRQChannel = OTG_FS_IRQn;
  249. #endif
  250. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  251. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  252. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  253. NVIC_Init(&NVIC_InitStructure);
  254. }
  255. /**
  256. * @brief BSP_Drive_VBUS
  257. * Drives the Vbus signal through IO
  258. * @param state : VBUS states
  259. * @retval None
  260. */
  261. void USB_OTG_BSP_DriveVBUS(USB_OTG_CORE_HANDLE *pdev, uint8_t state)
  262. {
  263. #if 0 /* 安富莱STM32-F4开发板不支持对外供电的控制 */
  264. /*
  265. On-chip 5 V VBUS generation is not supported. For this reason, a charge pump
  266. or, if 5 V are available on the application board, a basic power switch, must
  267. be added externally to drive the 5 V VBUS line. The external charge pump can
  268. be driven by any GPIO output. When the application decides to power on VBUS
  269. using the chosen GPIO, it must also set the port power bit in the host port
  270. control and status register (PPWR bit in OTG_FS_HPRT).
  271. Bit 12 PPWR: Port power
  272. The application uses this field to control power to this port, and the core
  273. clears this bit on an overcurrent condition.
  274. */
  275. #ifndef USE_USB_OTG_HS
  276. if (0 == state)
  277. {
  278. /* DISABLE is needed on output of the Power Switch */
  279. GPIO_SetBits(HOST_POWERSW_PORT, HOST_POWERSW_VBUS);
  280. }
  281. else
  282. {
  283. /*ENABLE the Power Switch by driving the Enable LOW */
  284. GPIO_ResetBits(HOST_POWERSW_PORT, HOST_POWERSW_VBUS);
  285. }
  286. #endif
  287. #endif
  288. }
  289. /**
  290. * @brief USB_OTG_BSP_ConfigVBUS
  291. * Configures the IO for the Vbus and OverCurrent
  292. * @param None
  293. * @retval None
  294. */
  295. void USB_OTG_BSP_ConfigVBUS(USB_OTG_CORE_HANDLE *pdev)
  296. {
  297. #if 0 /* 安富莱STM32-F4 不支持VBUS */
  298. #ifndef USE_USB_OTG_HS
  299. GPIO_InitTypeDef GPIO_InitStructure;
  300. #ifdef USE_STM3210C_EVAL
  301. RCC_APB2PeriphClockCmd(HOST_POWERSW_PORT_RCC, ENABLE);
  302. /* Configure Power Switch Vbus Pin */
  303. GPIO_InitStructure.GPIO_Pin = HOST_POWERSW_VBUS;
  304. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  305. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  306. GPIO_Init(HOST_POWERSW_PORT, &GPIO_InitStructure);
  307. #else
  308. #ifdef USE_USB_OTG_FS
  309. RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOH , ENABLE);
  310. GPIO_InitStructure.GPIO_Pin = HOST_POWERSW_VBUS;
  311. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  312. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  313. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  314. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  315. GPIO_Init(HOST_POWERSW_PORT,&GPIO_InitStructure);
  316. #endif
  317. #endif
  318. /* By Default, DISABLE is needed on output of the Power Switch */
  319. GPIO_SetBits(HOST_POWERSW_PORT, HOST_POWERSW_VBUS);
  320. USB_OTG_BSP_mDelay(200); /* Delay is need for stabilising the Vbus Low
  321. in Reset Condition, when Vbus=1 and Reset-button is pressed by user */
  322. #endif
  323. #endif
  324. }
  325. /**
  326. * @brief USB_OTG_BSP_TimeInit
  327. * Initializes delay unit using Timer2
  328. * @param None
  329. * @retval None
  330. */
  331. static void USB_OTG_BSP_TimeInit(void)
  332. {
  333. #ifdef USE_ACCURATE_TIME
  334. NVIC_InitTypeDef NVIC_InitStructure;
  335. /* Set the Vector Table base address at 0x08000000 */
  336. NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x00);
  337. /* Configure the Priority Group to 2 bits */
  338. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  339. /* Enable the TIM2 gloabal Interrupt */
  340. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  341. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  342. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  343. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  344. NVIC_Init(&NVIC_InitStructure);
  345. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  346. #endif
  347. }
  348. /**
  349. * @brief USB_OTG_BSP_uDelay
  350. * This function provides delay time in micro sec
  351. * @param usec : Value of delay required in micro sec
  352. * @retval None
  353. */
  354. void USB_OTG_BSP_uDelay(const uint32_t usec)
  355. {
  356. #ifdef USE_ACCURATE_TIME
  357. BSP_Delay(usec, TIM_USEC_DELAY);
  358. #else
  359. __IO uint32_t count = 0;
  360. const uint32_t utime = (120 * usec / 7);
  361. do
  362. {
  363. if (++count > utime)
  364. {
  365. return;
  366. }
  367. } while (1);
  368. #endif
  369. }
  370. /**
  371. * @brief USB_OTG_BSP_mDelay
  372. * This function provides delay time in milli sec
  373. * @param msec : Value of delay required in milli sec
  374. * @retval None
  375. */
  376. void USB_OTG_BSP_mDelay(const uint32_t msec)
  377. {
  378. #ifdef USE_ACCURATE_TIME
  379. BSP_Delay(msec, TIM_MSEC_DELAY);
  380. #else
  381. USB_OTG_BSP_uDelay(msec * 1000);
  382. #endif
  383. }
  384. /**
  385. * @brief USB_OTG_BSP_TimerIRQ
  386. * Time base IRQ
  387. * @param None
  388. * @retval None
  389. */
  390. void USB_OTG_BSP_TimerIRQ(void)
  391. {
  392. #ifdef USE_ACCURATE_TIME
  393. if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
  394. {
  395. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  396. if (BSP_delay > 0x00)
  397. {
  398. BSP_delay--;
  399. }
  400. else
  401. {
  402. TIM_Cmd(TIM2, DISABLE);
  403. }
  404. }
  405. #endif
  406. }
  407. #ifdef USE_ACCURATE_TIME
  408. /**
  409. * @brief BSP_Delay
  410. * Delay routine based on TIM2
  411. * @param nTime : Delay Time
  412. * @param unit : Delay Time unit : mili sec / micro sec
  413. * @retval None
  414. */
  415. static void BSP_Delay(uint32_t nTime, uint8_t unit)
  416. {
  417. BSP_delay = nTime;
  418. BSP_SetTime(unit);
  419. while (BSP_delay != 0)
  420. ;
  421. TIM_Cmd(TIM2, DISABLE);
  422. }
  423. /**
  424. * @brief BSP_SetTime
  425. * Configures TIM2 for delay routine based on TIM2
  426. * @param unit : msec /usec
  427. * @retval None
  428. */
  429. static void BSP_SetTime(uint8_t unit)
  430. {
  431. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  432. TIM_Cmd(TIM2, DISABLE);
  433. TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
  434. if (unit == TIM_USEC_DELAY)
  435. {
  436. TIM_TimeBaseStructure.TIM_Period = 11;
  437. }
  438. else if (unit == TIM_MSEC_DELAY)
  439. {
  440. TIM_TimeBaseStructure.TIM_Period = 11999;
  441. }
  442. TIM_TimeBaseStructure.TIM_Prescaler = 5;
  443. TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  444. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  445. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  446. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  447. TIM_ARRPreloadConfig(TIM2, ENABLE);
  448. /* TIM IT enable */
  449. TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  450. /* TIM2 enable counter */
  451. TIM_Cmd(TIM2, ENABLE);
  452. }
  453. #endif
  454. /**
  455. * @}
  456. */
  457. /**
  458. * @}
  459. */
  460. /**
  461. * @}
  462. */
  463. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/