stm32f1_hal_lowlevel.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. /* This file combines several STM32F4 HAL Functions into one file. This was done
  2. for space reasons, to avoid having several MB of HAL functions that most people
  3. will not use. In addition this HAL is slightly less demanding (no interrupts),
  4. but less robust as doesn't implement the timeouts.
  5. The original HAL files are COPYRIGHT STMicroelectronics, as shown below:
  6. */
  7. /*
  8. * COPYRIGHT(c) 2017 STMicroelectronics
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. ******************************************************************************
  33. */
  34. #include "stm32f1_hal.h"
  35. #include "stm32f1_hal_lowlevel.h"
  36. #include "stm32f1xx_hal_rcc.h"
  37. #include "stm32f1xx_hal_gpio.h"
  38. #include "stm32f1xx_hal_dma.h"
  39. #include "stm32f1xx_hal_uart.h"
  40. #include "stm32f1xx_hal_flash.h"
  41. #define assert_param(expr) ((void)0U)
  42. uint32_t HAL_GetTick(void)
  43. {
  44. static uint32_t tick;
  45. return tick++;;
  46. }
  47. __weak void HAL_Delay(__IO uint32_t Delay)
  48. {
  49. uint32_t tickstart = 0;
  50. tickstart = HAL_GetTick();
  51. while((HAL_GetTick() - tickstart) < Delay)
  52. {
  53. }
  54. }
  55. #define UART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  56. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
  57. uint32_t SystemCoreClock = 8000000;
  58. uint32_t HAL_RCC_GetSysClockFreq(void)
  59. {
  60. return 7372800U;
  61. }
  62. uint32_t HAL_RCC_GetPCLK1Freq(void)
  63. {
  64. return 7372800U;
  65. }
  66. /**
  67. * @brief Returns the PCLK2 frequency
  68. * @note Each time PCLK2 changes, this function must be called to update the
  69. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  70. * @retval PCLK2 frequency
  71. */
  72. uint32_t HAL_RCC_GetPCLK2Freq(void)
  73. {
  74. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  75. //return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2)>> POSITION_VAL(RCC_CFGR_PPRE2)]);
  76. return 7372800;
  77. }
  78. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  79. {
  80. uint32_t tickstart = 0;
  81. /* Check the parameters */
  82. assert_param(RCC_OscInitStruct != NULL);
  83. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  84. /*------------------------------- HSE Configuration ------------------------*/
  85. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  86. {
  87. /* Check the parameters */
  88. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  89. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  90. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE)
  91. || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE)))
  92. {
  93. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  94. {
  95. return HAL_ERROR;
  96. }
  97. }
  98. else
  99. {
  100. /* Set the new HSE configuration ---------------------------------------*/
  101. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  102. /* Check the HSE State */
  103. if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  104. {
  105. /* Get Start Tick */
  106. tickstart = HAL_GetTick();
  107. /* Wait till HSE is ready */
  108. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  109. {
  110. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  111. {
  112. return HAL_TIMEOUT;
  113. }
  114. }
  115. }
  116. else
  117. {
  118. /* Get Start Tick */
  119. tickstart = HAL_GetTick();
  120. /* Wait till HSE is disabled */
  121. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
  122. {
  123. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  124. {
  125. return HAL_TIMEOUT;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. /*----------------------------- HSI Configuration --------------------------*/
  132. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  133. {
  134. /* Check the parameters */
  135. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  136. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  137. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  138. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI)
  139. || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI_DIV2)))
  140. {
  141. /* When HSI is used as system clock it will not disabled */
  142. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
  143. {
  144. return HAL_ERROR;
  145. }
  146. /* Otherwise, just the calibration is allowed */
  147. else
  148. {
  149. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  150. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  151. }
  152. }
  153. else
  154. {
  155. /* Check the HSI State */
  156. if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  157. {
  158. /* Enable the Internal High Speed oscillator (HSI). */
  159. __HAL_RCC_HSI_ENABLE();
  160. /* Get Start Tick */
  161. tickstart = HAL_GetTick();
  162. /* Wait till HSI is ready */
  163. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  164. {
  165. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  166. {
  167. return HAL_TIMEOUT;
  168. }
  169. }
  170. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  171. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  172. }
  173. else
  174. {
  175. /* Disable the Internal High Speed oscillator (HSI). */
  176. __HAL_RCC_HSI_DISABLE();
  177. /* Get Start Tick */
  178. tickstart = HAL_GetTick();
  179. /* Wait till HSI is disabled */
  180. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
  181. {
  182. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  183. {
  184. return HAL_TIMEOUT;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. /*------------------------------ LSI Configuration -------------------------*/
  191. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  192. {
  193. /* Check the parameters */
  194. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  195. /* Check the LSI State */
  196. if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  197. {
  198. /* Enable the Internal Low Speed oscillator (LSI). */
  199. __HAL_RCC_LSI_ENABLE();
  200. /* Get Start Tick */
  201. tickstart = HAL_GetTick();
  202. /* Wait till LSI is ready */
  203. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
  204. {
  205. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  206. {
  207. return HAL_TIMEOUT;
  208. }
  209. }
  210. /* To have a fully stabilized clock in the specified range, a software delay of 1ms
  211. should be added.*/
  212. HAL_Delay(1);
  213. }
  214. else
  215. {
  216. /* Disable the Internal Low Speed oscillator (LSI). */
  217. __HAL_RCC_LSI_DISABLE();
  218. /* Get Start Tick */
  219. tickstart = HAL_GetTick();
  220. /* Wait till LSI is disabled */
  221. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
  222. {
  223. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  224. {
  225. return HAL_TIMEOUT;
  226. }
  227. }
  228. }
  229. }
  230. /*------------------------------ LSE Configuration -------------------------*/
  231. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  232. {
  233. /* Check the parameters */
  234. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  235. /* Enable Power Clock*/
  236. __HAL_RCC_PWR_CLK_ENABLE();
  237. /* Enable write access to Backup domain */
  238. SET_BIT(PWR->CR, PWR_CR_DBP);
  239. /* Wait for Backup domain Write protection disable */
  240. tickstart = HAL_GetTick();
  241. while((PWR->CR & PWR_CR_DBP) == RESET)
  242. {
  243. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  244. {
  245. return HAL_TIMEOUT;
  246. }
  247. }
  248. /* Set the new LSE configuration -----------------------------------------*/
  249. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  250. /* Check the LSE State */
  251. if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  252. {
  253. /* Get Start Tick */
  254. tickstart = HAL_GetTick();
  255. /* Wait till LSE is ready */
  256. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  257. {
  258. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  259. {
  260. return HAL_TIMEOUT;
  261. }
  262. }
  263. }
  264. else
  265. {
  266. /* Get Start Tick */
  267. tickstart = HAL_GetTick();
  268. /* Wait till LSE is disabled */
  269. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
  270. {
  271. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  272. {
  273. return HAL_TIMEOUT;
  274. }
  275. }
  276. }
  277. }
  278. #if defined(RCC_CR_PLL2ON)
  279. /*-------------------------------- PLL2 Configuration -----------------------*/
  280. /* Check the parameters */
  281. assert_param(IS_RCC_PLL2(RCC_OscInitStruct->PLL2.PLL2State));
  282. if ((RCC_OscInitStruct->PLL2.PLL2State) != RCC_PLL2_NONE)
  283. {
  284. /* This bit can not be cleared if the PLL2 clock is used indirectly as system
  285. clock (i.e. it is used as PLL clock entry that is used as system clock). */
  286. if((__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE) && \
  287. (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && \
  288. ((READ_BIT(RCC->CFGR2,RCC_CFGR2_PREDIV1SRC)) == RCC_CFGR2_PREDIV1SRC_PLL2))
  289. {
  290. return HAL_ERROR;
  291. }
  292. else
  293. {
  294. if((RCC_OscInitStruct->PLL2.PLL2State) == RCC_PLL2_ON)
  295. {
  296. /* Check the parameters */
  297. assert_param(IS_RCC_PLL2_MUL(RCC_OscInitStruct->PLL2.PLL2MUL));
  298. assert_param(IS_RCC_HSE_PREDIV2(RCC_OscInitStruct->PLL2.HSEPrediv2Value));
  299. /* Prediv2 can be written only when the PLLI2S is disabled. */
  300. /* Return an error only if new value is different from the programmed value */
  301. if (HAL_IS_BIT_SET(RCC->CR,RCC_CR_PLL3ON) && \
  302. (__HAL_RCC_HSE_GET_PREDIV2() != RCC_OscInitStruct->PLL2.HSEPrediv2Value))
  303. {
  304. return HAL_ERROR;
  305. }
  306. /* Disable the main PLL2. */
  307. __HAL_RCC_PLL2_DISABLE();
  308. /* Get Start Tick */
  309. tickstart = HAL_GetTick();
  310. /* Wait till PLL2 is disabled */
  311. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) != RESET)
  312. {
  313. if((HAL_GetTick() - tickstart ) > PLL2_TIMEOUT_VALUE)
  314. {
  315. return HAL_TIMEOUT;
  316. }
  317. }
  318. /* Configure the HSE prediv2 factor --------------------------------*/
  319. __HAL_RCC_HSE_PREDIV2_CONFIG(RCC_OscInitStruct->PLL2.HSEPrediv2Value);
  320. /* Configure the main PLL2 multiplication factors. */
  321. __HAL_RCC_PLL2_CONFIG(RCC_OscInitStruct->PLL2.PLL2MUL);
  322. /* Enable the main PLL2. */
  323. __HAL_RCC_PLL2_ENABLE();
  324. /* Get Start Tick */
  325. tickstart = HAL_GetTick();
  326. /* Wait till PLL2 is ready */
  327. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) == RESET)
  328. {
  329. if((HAL_GetTick() - tickstart ) > PLL2_TIMEOUT_VALUE)
  330. {
  331. return HAL_TIMEOUT;
  332. }
  333. }
  334. }
  335. else
  336. {
  337. /* Set PREDIV1 source to HSE */
  338. CLEAR_BIT(RCC->CFGR2, RCC_CFGR2_PREDIV1SRC);
  339. /* Disable the main PLL2. */
  340. __HAL_RCC_PLL2_DISABLE();
  341. /* Get Start Tick */
  342. tickstart = HAL_GetTick();
  343. /* Wait till PLL2 is disabled */
  344. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) != RESET)
  345. {
  346. if((HAL_GetTick() - tickstart ) > PLL2_TIMEOUT_VALUE)
  347. {
  348. return HAL_TIMEOUT;
  349. }
  350. }
  351. }
  352. }
  353. }
  354. #endif /* RCC_CR_PLL2ON */
  355. /*-------------------------------- PLL Configuration -----------------------*/
  356. /* Check the parameters */
  357. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  358. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  359. {
  360. /* Check if the PLL is used as system clock or not */
  361. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  362. {
  363. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  364. {
  365. /* Check the parameters */
  366. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  367. assert_param(IS_RCC_PLL_MUL(RCC_OscInitStruct->PLL.PLLMUL));
  368. /* Disable the main PLL. */
  369. __HAL_RCC_PLL_DISABLE();
  370. /* Get Start Tick */
  371. tickstart = HAL_GetTick();
  372. /* Wait till PLL is disabled */
  373. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  374. {
  375. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  376. {
  377. return HAL_TIMEOUT;
  378. }
  379. }
  380. /* Configure the HSE prediv factor --------------------------------*/
  381. /* It can be written only when the PLL is disabled. Not used in PLL source is different than HSE */
  382. if(RCC_OscInitStruct->PLL.PLLSource == RCC_PLLSOURCE_HSE)
  383. {
  384. /* Check the parameter */
  385. assert_param(IS_RCC_HSE_PREDIV(RCC_OscInitStruct->HSEPredivValue));
  386. #if defined(RCC_CFGR2_PREDIV1SRC)
  387. assert_param(IS_RCC_PREDIV1_SOURCE(RCC_OscInitStruct->Prediv1Source));
  388. /* Set PREDIV1 source */
  389. SET_BIT(RCC->CFGR2, RCC_OscInitStruct->Prediv1Source);
  390. #endif /* RCC_CFGR2_PREDIV1SRC */
  391. /* Set PREDIV1 Value */
  392. __HAL_RCC_HSE_PREDIV_CONFIG(RCC_OscInitStruct->HSEPredivValue);
  393. }
  394. /* Configure the main PLL clock source and multiplication factors. */
  395. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  396. RCC_OscInitStruct->PLL.PLLMUL);
  397. /* Enable the main PLL. */
  398. __HAL_RCC_PLL_ENABLE();
  399. /* Get Start Tick */
  400. tickstart = HAL_GetTick();
  401. /* Wait till PLL is ready */
  402. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  403. {
  404. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  405. {
  406. return HAL_TIMEOUT;
  407. }
  408. }
  409. }
  410. else
  411. {
  412. /* Disable the main PLL. */
  413. __HAL_RCC_PLL_DISABLE();
  414. /* Get Start Tick */
  415. tickstart = HAL_GetTick();
  416. /* Wait till PLL is disabled */
  417. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  418. {
  419. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  420. {
  421. return HAL_TIMEOUT;
  422. }
  423. }
  424. }
  425. }
  426. else
  427. {
  428. return HAL_ERROR;
  429. }
  430. }
  431. return HAL_OK;
  432. }
  433. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  434. {
  435. uint32_t tickstart = 0;
  436. /* Check the parameters */
  437. assert_param(RCC_ClkInitStruct != NULL);
  438. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  439. assert_param(IS_FLASH_LATENCY(FLatency));
  440. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  441. must be correctly programmed according to the frequency of the CPU clock
  442. (HCLK) of the device. */
  443. #if defined(FLASH_ACR_LATENCY)
  444. /* Increasing the number of wait states because of higher CPU frequency */
  445. if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
  446. {
  447. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  448. __HAL_FLASH_SET_LATENCY(FLatency);
  449. /* Check that the new number of wait states is taken into account to access the Flash
  450. memory by reading the FLASH_ACR register */
  451. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  452. {
  453. return HAL_ERROR;
  454. }
  455. }
  456. #endif /* FLASH_ACR_LATENCY */
  457. /*-------------------------- HCLK Configuration --------------------------*/
  458. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  459. {
  460. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  461. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  462. }
  463. /*------------------------- SYSCLK Configuration ---------------------------*/
  464. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  465. {
  466. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  467. /* HSE is selected as System Clock Source */
  468. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  469. {
  470. /* Check the HSE ready flag */
  471. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  472. {
  473. return HAL_ERROR;
  474. }
  475. }
  476. /* PLL is selected as System Clock Source */
  477. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  478. {
  479. /* Check the PLL ready flag */
  480. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  481. {
  482. return HAL_ERROR;
  483. }
  484. }
  485. /* HSI is selected as System Clock Source */
  486. else
  487. {
  488. /* Check the HSI ready flag */
  489. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  490. {
  491. return HAL_ERROR;
  492. }
  493. }
  494. __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource);
  495. /* Get Start Tick */
  496. tickstart = HAL_GetTick();
  497. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  498. {
  499. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)
  500. {
  501. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  502. {
  503. return HAL_TIMEOUT;
  504. }
  505. }
  506. }
  507. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  508. {
  509. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  510. {
  511. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  512. {
  513. return HAL_TIMEOUT;
  514. }
  515. }
  516. }
  517. else
  518. {
  519. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)
  520. {
  521. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  522. {
  523. return HAL_TIMEOUT;
  524. }
  525. }
  526. }
  527. }
  528. #if defined(FLASH_ACR_LATENCY)
  529. /* Decreasing the number of wait states because of lower CPU frequency */
  530. if(FLatency < (FLASH->ACR & FLASH_ACR_LATENCY))
  531. {
  532. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  533. __HAL_FLASH_SET_LATENCY(FLatency);
  534. /* Check that the new number of wait states is taken into account to access the Flash
  535. memory by reading the FLASH_ACR register */
  536. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  537. {
  538. return HAL_ERROR;
  539. }
  540. }
  541. #endif /* FLASH_ACR_LATENCY */
  542. /*-------------------------- PCLK1 Configuration ---------------------------*/
  543. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  544. {
  545. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  546. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  547. }
  548. /*-------------------------- PCLK2 Configuration ---------------------------*/
  549. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  550. {
  551. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  552. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3));
  553. }
  554. /* Update the SystemCoreClock global variable */
  555. //SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> RCC_CFGR_HPRE_BITNUMBER];
  556. /* Configure the source of time base considering new system clocks settings*/
  557. //HAL_InitTick (TICK_INT_PRIORITY);
  558. return HAL_OK;
  559. }
  560. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  561. {
  562. uint32_t tickstart = 0, temp_reg = 0;
  563. #if defined(STM32F105xC) || defined(STM32F107xC)
  564. uint32_t pllactive = 0;
  565. #endif /* STM32F105xC || STM32F107xC */
  566. /* Check the parameters */
  567. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  568. /*------------------------------- RTC/LCD Configuration ------------------------*/
  569. if ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC))
  570. {
  571. /* check for RTC Parameters used to output RTCCLK */
  572. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  573. /* Enable Power Clock*/
  574. __HAL_RCC_PWR_CLK_ENABLE();
  575. /* Enable write access to Backup domain */
  576. SET_BIT(PWR->CR, PWR_CR_DBP);
  577. /* Wait for Backup domain Write protection disable */
  578. tickstart = HAL_GetTick();
  579. while((PWR->CR & PWR_CR_DBP) == RESET)
  580. {
  581. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  582. {
  583. return HAL_TIMEOUT;
  584. }
  585. }
  586. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  587. temp_reg = (RCC->BDCR & RCC_BDCR_RTCSEL);
  588. if((temp_reg != 0x00000000U) && (temp_reg != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  589. {
  590. /* Store the content of BDCR register before the reset of Backup Domain */
  591. temp_reg = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  592. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  593. __HAL_RCC_BACKUPRESET_FORCE();
  594. __HAL_RCC_BACKUPRESET_RELEASE();
  595. /* Restore the Content of BDCR register */
  596. RCC->BDCR = temp_reg;
  597. /* Wait for LSERDY if LSE was enabled */
  598. if (HAL_IS_BIT_SET(temp_reg, RCC_BDCR_LSEON))
  599. {
  600. /* Get timeout */
  601. tickstart = HAL_GetTick();
  602. /* Wait till LSE is ready */
  603. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  604. {
  605. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  606. {
  607. return HAL_TIMEOUT;
  608. }
  609. }
  610. }
  611. }
  612. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  613. }
  614. /*------------------------------ ADC clock Configuration ------------------*/
  615. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC) == RCC_PERIPHCLK_ADC)
  616. {
  617. /* Check the parameters */
  618. assert_param(IS_RCC_ADCPLLCLK_DIV(PeriphClkInit->AdcClockSelection));
  619. /* Configure the ADC clock source */
  620. __HAL_RCC_ADC_CONFIG(PeriphClkInit->AdcClockSelection);
  621. }
  622. #if defined(STM32F105xC) || defined(STM32F107xC)
  623. /*------------------------------ I2S2 Configuration ------------------------*/
  624. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S2) == RCC_PERIPHCLK_I2S2)
  625. {
  626. /* Check the parameters */
  627. assert_param(IS_RCC_I2S2CLKSOURCE(PeriphClkInit->I2s2ClockSelection));
  628. /* Configure the I2S2 clock source */
  629. __HAL_RCC_I2S2_CONFIG(PeriphClkInit->I2s2ClockSelection);
  630. }
  631. /*------------------------------ I2S3 Configuration ------------------------*/
  632. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S3) == RCC_PERIPHCLK_I2S3)
  633. {
  634. /* Check the parameters */
  635. assert_param(IS_RCC_I2S3CLKSOURCE(PeriphClkInit->I2s3ClockSelection));
  636. /* Configure the I2S3 clock source */
  637. __HAL_RCC_I2S3_CONFIG(PeriphClkInit->I2s3ClockSelection);
  638. }
  639. /*------------------------------ PLL I2S Configuration ----------------------*/
  640. /* Check that PLLI2S need to be enabled */
  641. if (HAL_IS_BIT_SET(RCC->CFGR2, RCC_CFGR2_I2S2SRC) || HAL_IS_BIT_SET(RCC->CFGR2, RCC_CFGR2_I2S3SRC))
  642. {
  643. /* Update flag to indicate that PLL I2S should be active */
  644. pllactive = 1;
  645. }
  646. /* Check if PLL I2S need to be enabled */
  647. if (pllactive == 1)
  648. {
  649. /* Enable PLL I2S only if not active */
  650. if (HAL_IS_BIT_CLR(RCC->CR, RCC_CR_PLL3ON))
  651. {
  652. /* Check the parameters */
  653. assert_param(IS_RCC_PLLI2S_MUL(PeriphClkInit->PLLI2S.PLLI2SMUL));
  654. assert_param(IS_RCC_HSE_PREDIV2(PeriphClkInit->PLLI2S.HSEPrediv2Value));
  655. /* Prediv2 can be written only when the PLL2 is disabled. */
  656. /* Return an error only if new value is different from the programmed value */
  657. if (HAL_IS_BIT_SET(RCC->CR,RCC_CR_PLL2ON) && \
  658. (__HAL_RCC_HSE_GET_PREDIV2() != PeriphClkInit->PLLI2S.HSEPrediv2Value))
  659. {
  660. return HAL_ERROR;
  661. }
  662. /* Configure the HSE prediv2 factor --------------------------------*/
  663. __HAL_RCC_HSE_PREDIV2_CONFIG(PeriphClkInit->PLLI2S.HSEPrediv2Value);
  664. /* Configure the main PLLI2S multiplication factors. */
  665. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SMUL);
  666. /* Enable the main PLLI2S. */
  667. __HAL_RCC_PLLI2S_ENABLE();
  668. /* Get Start Tick*/
  669. tickstart = HAL_GetTick();
  670. /* Wait till PLLI2S is ready */
  671. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  672. {
  673. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  674. {
  675. return HAL_TIMEOUT;
  676. }
  677. }
  678. }
  679. else
  680. {
  681. /* Return an error only if user wants to change the PLLI2SMUL whereas PLLI2S is active */
  682. if (READ_BIT(RCC->CFGR2, RCC_CFGR2_PLL3MUL) != PeriphClkInit->PLLI2S.PLLI2SMUL)
  683. {
  684. return HAL_ERROR;
  685. }
  686. }
  687. }
  688. #endif /* STM32F105xC || STM32F107xC */
  689. #if defined(STM32F102x6) || defined(STM32F102xB) || defined(STM32F103x6)\
  690. || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG)\
  691. || defined(STM32F105xC) || defined(STM32F107xC)
  692. /*------------------------------ USB clock Configuration ------------------*/
  693. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USB) == RCC_PERIPHCLK_USB)
  694. {
  695. /* Check the parameters */
  696. assert_param(IS_RCC_USBPLLCLK_DIV(PeriphClkInit->UsbClockSelection));
  697. /* Configure the USB clock source */
  698. __HAL_RCC_USB_CONFIG(PeriphClkInit->UsbClockSelection);
  699. }
  700. #endif /* STM32F102x6 || STM32F102xB || STM32F103x6 || STM32F103xB || STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */
  701. return HAL_OK;
  702. }
  703. #define GPIO_MODE ((uint32_t)0x00000003)
  704. #define EXTI_MODE ((uint32_t)0x10000000)
  705. #define GPIO_MODE_IT ((uint32_t)0x00010000)
  706. #define GPIO_MODE_EVT ((uint32_t)0x00020000)
  707. #define RISING_EDGE ((uint32_t)0x00100000)
  708. #define FALLING_EDGE ((uint32_t)0x00200000)
  709. #define GPIO_OUTPUT_TYPE ((uint32_t)0x00000010)
  710. #define GPIO_NUMBER ((uint32_t)16)
  711. #define GPIO_CR_MODE_INPUT ((uint32_t)0x00000000) /*!< 00: Input mode (reset state) */
  712. #define GPIO_CR_CNF_ANALOG ((uint32_t)0x00000000) /*!< 00: Analog mode */
  713. #define GPIO_CR_CNF_INPUT_FLOATING ((uint32_t)0x00000004) /*!< 01: Floating input (reset state) */
  714. #define GPIO_CR_CNF_INPUT_PU_PD ((uint32_t)0x00000008) /*!< 10: Input with pull-up / pull-down */
  715. #define GPIO_CR_CNF_GP_OUTPUT_PP ((uint32_t)0x00000000) /*!< 00: General purpose output push-pull */
  716. #define GPIO_CR_CNF_GP_OUTPUT_OD ((uint32_t)0x00000004) /*!< 01: General purpose output Open-drain */
  717. #define GPIO_CR_CNF_AF_OUTPUT_PP ((uint32_t)0x00000008) /*!< 10: Alternate function output Push-pull */
  718. #define GPIO_CR_CNF_AF_OUTPUT_OD ((uint32_t)0x0000000C) /*!< 11: Alternate function output Open-drain */
  719. #define __HAL_RCC_GPIOA_CLK_ENABLE() do { \
  720. __IO uint32_t tmpreg; \
  721. SET_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPAEN);\
  722. /* Delay after an RCC peripheral clock enabling */\
  723. tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPAEN);\
  724. UNUSED(tmpreg); \
  725. } while(0)
  726. #define __HAL_RCC_USART1_CLK_ENABLE() do { \
  727. __IO uint32_t tmpreg; \
  728. SET_BIT(RCC->APB2ENR, RCC_APB2ENR_USART1EN);\
  729. /* Delay after an RCC peripheral clock enabling */\
  730. tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_USART1EN);\
  731. UNUSED(tmpreg); \
  732. } while(0)
  733. void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  734. {
  735. uint32_t position;
  736. uint32_t ioposition = 0x00;
  737. uint32_t iocurrent = 0x00;
  738. uint32_t temp = 0x00;
  739. uint32_t config = 0x00;
  740. __IO uint32_t *configregister; /* Store the address of CRL or CRH register based on pin number */
  741. uint32_t registeroffset = 0; /* offset used during computation of CNF and MODE bits placement inside CRL or CRH register */
  742. /* Check the parameters */
  743. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  744. assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
  745. assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
  746. /* Configure the port pins */
  747. for (position = 0; position < GPIO_NUMBER; position++)
  748. {
  749. /* Get the IO position */
  750. ioposition = ((uint32_t)0x01) << position;
  751. /* Get the current IO position */
  752. iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
  753. if (iocurrent == ioposition)
  754. {
  755. /* Check the Alternate function parameters */
  756. assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
  757. /* Based on the required mode, filling config variable with MODEy[1:0] and CNFy[3:2] corresponding bits */
  758. switch (GPIO_Init->Mode)
  759. {
  760. /* If we are configuring the pin in OUTPUT push-pull mode */
  761. case GPIO_MODE_OUTPUT_PP:
  762. /* Check the GPIO speed parameter */
  763. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  764. config = GPIO_Init->Speed + GPIO_CR_CNF_GP_OUTPUT_PP;
  765. break;
  766. /* If we are configuring the pin in OUTPUT open-drain mode */
  767. case GPIO_MODE_OUTPUT_OD:
  768. /* Check the GPIO speed parameter */
  769. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  770. config = GPIO_Init->Speed + GPIO_CR_CNF_GP_OUTPUT_OD;
  771. break;
  772. /* If we are configuring the pin in ALTERNATE FUNCTION push-pull mode */
  773. case GPIO_MODE_AF_PP:
  774. /* Check the GPIO speed parameter */
  775. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  776. config = GPIO_Init->Speed + GPIO_CR_CNF_AF_OUTPUT_PP;
  777. break;
  778. /* If we are configuring the pin in ALTERNATE FUNCTION open-drain mode */
  779. case GPIO_MODE_AF_OD:
  780. /* Check the GPIO speed parameter */
  781. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  782. config = GPIO_Init->Speed + GPIO_CR_CNF_AF_OUTPUT_OD;
  783. break;
  784. /* If we are configuring the pin in INPUT (also applicable to EVENT and IT mode) */
  785. case GPIO_MODE_INPUT:
  786. case GPIO_MODE_IT_RISING:
  787. case GPIO_MODE_IT_FALLING:
  788. case GPIO_MODE_IT_RISING_FALLING:
  789. case GPIO_MODE_EVT_RISING:
  790. case GPIO_MODE_EVT_FALLING:
  791. case GPIO_MODE_EVT_RISING_FALLING:
  792. /* Check the GPIO pull parameter */
  793. assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
  794. if(GPIO_Init->Pull == GPIO_NOPULL)
  795. {
  796. config = GPIO_CR_MODE_INPUT + GPIO_CR_CNF_INPUT_FLOATING;
  797. }
  798. else if(GPIO_Init->Pull == GPIO_PULLUP)
  799. {
  800. config = GPIO_CR_MODE_INPUT + GPIO_CR_CNF_INPUT_PU_PD;
  801. /* Set the corresponding ODR bit */
  802. GPIOx->BSRR = ioposition;
  803. }
  804. else /* GPIO_PULLDOWN */
  805. {
  806. config = GPIO_CR_MODE_INPUT + GPIO_CR_CNF_INPUT_PU_PD;
  807. /* Reset the corresponding ODR bit */
  808. GPIOx->BRR = ioposition;
  809. }
  810. break;
  811. /* If we are configuring the pin in INPUT analog mode */
  812. case GPIO_MODE_ANALOG:
  813. config = GPIO_CR_MODE_INPUT + GPIO_CR_CNF_ANALOG;
  814. break;
  815. /* Parameters are checked with assert_param */
  816. default:
  817. break;
  818. }
  819. /* Check if the current bit belongs to first half or last half of the pin count number
  820. in order to address CRH or CRL register*/
  821. configregister = (iocurrent < GPIO_PIN_8) ? &GPIOx->CRL : &GPIOx->CRH;
  822. registeroffset = (iocurrent < GPIO_PIN_8) ? (position << 2) : ((position - 8) << 2);
  823. /* Apply the new configuration of the pin to the register */
  824. MODIFY_REG((*configregister), ((GPIO_CRL_MODE0 | GPIO_CRL_CNF0) << registeroffset ), (config << registeroffset));
  825. /*--------------------- EXTI Mode Configuration ------------------------*/
  826. /* Configure the External Interrupt or event for the current IO */
  827. if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
  828. {
  829. /* Enable AFIO Clock */
  830. __HAL_RCC_AFIO_CLK_ENABLE();
  831. temp = AFIO->EXTICR[position >> 2];
  832. CLEAR_BIT(temp, ((uint32_t)0x0F) << (4 * (position & 0x03)));
  833. SET_BIT(temp, (GPIO_GET_INDEX(GPIOx)) << (4 * (position & 0x03)));
  834. AFIO->EXTICR[position >> 2] = temp;
  835. /* Configure the interrupt mask */
  836. if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
  837. {
  838. SET_BIT(EXTI->IMR, iocurrent);
  839. }
  840. else
  841. {
  842. CLEAR_BIT(EXTI->IMR, iocurrent);
  843. }
  844. /* Configure the event mask */
  845. if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
  846. {
  847. SET_BIT(EXTI->EMR, iocurrent);
  848. }
  849. else
  850. {
  851. CLEAR_BIT(EXTI->EMR, iocurrent);
  852. }
  853. /* Enable or disable the rising trigger */
  854. if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
  855. {
  856. SET_BIT(EXTI->RTSR, iocurrent);
  857. }
  858. else
  859. {
  860. CLEAR_BIT(EXTI->RTSR, iocurrent);
  861. }
  862. /* Enable or disable the falling trigger */
  863. if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
  864. {
  865. SET_BIT(EXTI->FTSR, iocurrent);
  866. }
  867. else
  868. {
  869. CLEAR_BIT(EXTI->FTSR, iocurrent);
  870. }
  871. }
  872. }
  873. }
  874. }
  875. void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  876. {
  877. /* Check the parameters */
  878. assert_param(IS_GPIO_PIN(GPIO_Pin));
  879. assert_param(IS_GPIO_PIN_ACTION(PinState));
  880. if(PinState != GPIO_PIN_RESET)
  881. {
  882. GPIOx->BSRR = GPIO_Pin;
  883. }
  884. else
  885. {
  886. GPIOx->BSRR = (uint32_t)GPIO_Pin << 16;
  887. }
  888. }
  889. static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  890. {
  891. uint32_t tickstart = 0;
  892. /* Get tick */
  893. tickstart = HAL_GetTick();
  894. /* Wait until flag is set */
  895. if(Status == RESET)
  896. {
  897. while(__HAL_UART_GET_FLAG(huart, Flag) == RESET)
  898. {
  899. /* Check for the Timeout */
  900. if(Timeout != HAL_MAX_DELAY)
  901. {
  902. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  903. {
  904. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  905. __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
  906. __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
  907. __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
  908. __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
  909. huart->State= HAL_UART_STATE_READY;
  910. /* Process Unlocked */
  911. __HAL_UNLOCK(huart);
  912. return HAL_TIMEOUT;
  913. }
  914. }
  915. }
  916. }
  917. else
  918. {
  919. while(__HAL_UART_GET_FLAG(huart, Flag) != RESET)
  920. {
  921. /* Check for the Timeout */
  922. if(Timeout != HAL_MAX_DELAY)
  923. {
  924. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  925. {
  926. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  927. __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
  928. __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
  929. __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
  930. __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
  931. huart->State= HAL_UART_STATE_READY;
  932. /* Process Unlocked */
  933. __HAL_UNLOCK(huart);
  934. return HAL_TIMEOUT;
  935. }
  936. }
  937. }
  938. }
  939. return HAL_OK;
  940. }
  941. static void UART_SetConfig(UART_HandleTypeDef *huart)
  942. {
  943. uint32_t tmpreg = 0x00;
  944. /* Check the parameters */
  945. assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
  946. assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
  947. assert_param(IS_UART_PARITY(huart->Init.Parity));
  948. assert_param(IS_UART_MODE(huart->Init.Mode));
  949. /*------- UART-associated USART registers setting : CR2 Configuration ------*/
  950. /* Configure the UART Stop Bits: Set STOP[13:12] bits according
  951. * to huart->Init.StopBits value */
  952. MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
  953. /*------- UART-associated USART registers setting : CR1 Configuration ------*/
  954. /* Configure the UART Word Length, Parity and mode:
  955. Set the M bits according to huart->Init.WordLength value
  956. Set PCE and PS bits according to huart->Init.Parity value
  957. Set TE and RE bits according to huart->Init.Mode value */
  958. tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode ;
  959. MODIFY_REG(huart->Instance->CR1,
  960. (uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
  961. tmpreg);
  962. /*------- UART-associated USART registers setting : CR3 Configuration ------*/
  963. /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */
  964. MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE), huart->Init.HwFlowCtl);
  965. /*------- UART-associated USART registers setting : BRR Configuration ------*/
  966. if((huart->Instance == USART1))
  967. {
  968. huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
  969. }
  970. else
  971. {
  972. huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
  973. }
  974. }
  975. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  976. {
  977. /* Check the UART handle allocation */
  978. if(huart == NULL)
  979. {
  980. return HAL_ERROR;
  981. }
  982. /* Check the parameters */
  983. if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
  984. {
  985. /* The hardware flow control is available only for USART1, USART2, USART3 */
  986. assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
  987. assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
  988. }
  989. else
  990. {
  991. assert_param(IS_UART_INSTANCE(huart->Instance));
  992. }
  993. assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  994. assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  995. if(huart->State == HAL_UART_STATE_RESET)
  996. {
  997. /* Allocate lock resource and initialize it */
  998. huart->Lock = HAL_UNLOCKED;
  999. /* Init the low level hardware */
  1000. //HAL_UART_MspInit(huart);
  1001. }
  1002. huart->State = HAL_UART_STATE_BUSY;
  1003. /* Disable the peripheral */
  1004. __HAL_UART_DISABLE(huart);
  1005. /* Set the UART Communication parameters */
  1006. UART_SetConfig(huart);
  1007. /* In asynchronous mode, the following bits must be kept cleared:
  1008. - LINEN and CLKEN bits in the USART_CR2 register,
  1009. - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
  1010. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  1011. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  1012. /* Enable the peripheral */
  1013. __HAL_UART_ENABLE(huart);
  1014. /* Initialize the UART state */
  1015. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1016. huart->State= HAL_UART_STATE_READY;
  1017. return HAL_OK;
  1018. }
  1019. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1020. {
  1021. uint16_t* tmp;
  1022. uint32_t tmp_state = 0;
  1023. tmp_state = huart->State;
  1024. if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_RX))
  1025. {
  1026. if((pData == NULL) || (Size == 0))
  1027. {
  1028. return HAL_ERROR;
  1029. }
  1030. /* Process Locked */
  1031. __HAL_LOCK(huart);
  1032. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1033. /* Check if a non-blocking receive process is ongoing or not */
  1034. if(huart->State == HAL_UART_STATE_BUSY_RX)
  1035. {
  1036. huart->State = HAL_UART_STATE_BUSY_TX_RX;
  1037. }
  1038. else
  1039. {
  1040. huart->State = HAL_UART_STATE_BUSY_TX;
  1041. }
  1042. huart->TxXferSize = Size;
  1043. huart->TxXferCount = Size;
  1044. while(huart->TxXferCount > 0)
  1045. {
  1046. huart->TxXferCount--;
  1047. if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  1048. {
  1049. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK)
  1050. {
  1051. return HAL_TIMEOUT;
  1052. }
  1053. tmp = (uint16_t*) pData;
  1054. huart->Instance->DR = (*tmp & (uint16_t)0x01FF);
  1055. if(huart->Init.Parity == UART_PARITY_NONE)
  1056. {
  1057. pData +=2;
  1058. }
  1059. else
  1060. {
  1061. pData +=1;
  1062. }
  1063. }
  1064. else
  1065. {
  1066. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK)
  1067. {
  1068. return HAL_TIMEOUT;
  1069. }
  1070. huart->Instance->DR = (*pData++ & (uint8_t)0xFF);
  1071. }
  1072. }
  1073. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, Timeout) != HAL_OK)
  1074. {
  1075. return HAL_TIMEOUT;
  1076. }
  1077. /* Check if a non-blocking receive process is ongoing or not */
  1078. if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  1079. {
  1080. huart->State = HAL_UART_STATE_BUSY_RX;
  1081. }
  1082. else
  1083. {
  1084. huart->State = HAL_UART_STATE_READY;
  1085. }
  1086. /* Process Unlocked */
  1087. __HAL_UNLOCK(huart);
  1088. return HAL_OK;
  1089. }
  1090. else
  1091. {
  1092. return HAL_BUSY;
  1093. }
  1094. }
  1095. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1096. {
  1097. uint16_t* tmp;
  1098. uint32_t tmp_state = 0;
  1099. tmp_state = huart->State;
  1100. if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_TX))
  1101. {
  1102. if((pData == NULL ) || (Size == 0))
  1103. {
  1104. return HAL_ERROR;
  1105. }
  1106. /* Process Locked */
  1107. __HAL_LOCK(huart);
  1108. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1109. /* Check if a non-blocking transmit process is ongoing or not */
  1110. if(huart->State == HAL_UART_STATE_BUSY_TX)
  1111. {
  1112. huart->State = HAL_UART_STATE_BUSY_TX_RX;
  1113. }
  1114. else
  1115. {
  1116. huart->State = HAL_UART_STATE_BUSY_RX;
  1117. }
  1118. huart->RxXferSize = Size;
  1119. huart->RxXferCount = Size;
  1120. /* Check the remain data to be received */
  1121. while(huart->RxXferCount > 0)
  1122. {
  1123. huart->RxXferCount--;
  1124. if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  1125. {
  1126. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  1127. {
  1128. return HAL_TIMEOUT;
  1129. }
  1130. tmp = (uint16_t*) pData ;
  1131. if(huart->Init.Parity == UART_PARITY_NONE)
  1132. {
  1133. *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
  1134. pData +=2;
  1135. }
  1136. else
  1137. {
  1138. *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
  1139. pData +=1;
  1140. }
  1141. }
  1142. else
  1143. {
  1144. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  1145. {
  1146. return HAL_TIMEOUT;
  1147. }
  1148. if(huart->Init.Parity == UART_PARITY_NONE)
  1149. {
  1150. *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
  1151. }
  1152. else
  1153. {
  1154. *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
  1155. }
  1156. }
  1157. }
  1158. /* Check if a non-blocking transmit process is ongoing or not */
  1159. if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  1160. {
  1161. huart->State = HAL_UART_STATE_BUSY_TX;
  1162. }
  1163. else
  1164. {
  1165. huart->State = HAL_UART_STATE_READY;
  1166. }
  1167. /* Process Unlocked */
  1168. __HAL_UNLOCK(huart);
  1169. return HAL_OK;
  1170. }
  1171. else
  1172. {
  1173. return HAL_BUSY;
  1174. }
  1175. }