stm32f0_hal_lowlevel.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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 "stm32f0_hal.h"
  35. #include "stm32f0_hal_lowlevel.h"
  36. #include "stm32f0xx_hal_rcc.h"
  37. #include "stm32f0xx_hal_gpio.h"
  38. #include "stm32f0xx_hal_dma.h"
  39. #include "stm32f0xx_hal_uart.h"
  40. #include "stm32f0xx_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. #define UART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  48. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
  49. uint32_t SystemCoreClock = 8000000;
  50. uint32_t HAL_RCC_GetSysClockFreq(void)
  51. {
  52. return 7372800U;
  53. }
  54. uint32_t HAL_RCC_GetPCLK1Freq(void)
  55. {
  56. return 7372800U;
  57. }
  58. /**
  59. * @brief Returns the PCLK2 frequency
  60. * @note Each time PCLK2 changes, this function must be called to update the
  61. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  62. * @retval PCLK2 frequency
  63. */
  64. uint32_t HAL_RCC_GetPCLK2Freq(void)
  65. {
  66. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  67. //return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2)>> POSITION_VAL(RCC_CFGR_PPRE2)]);
  68. return 7372800;
  69. }
  70. /**
  71. * @brief Initializes the RCC Oscillators according to the specified parameters in the
  72. * RCC_OscInitTypeDef.
  73. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  74. * contains the configuration information for the RCC Oscillators.
  75. * @note The PLL is not disabled when used as system clock.
  76. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  77. * supported by this macro. User should request a transition to LSE Off
  78. * first and then LSE On or LSE Bypass.
  79. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  80. * supported by this macro. User should request a transition to HSE Off
  81. * first and then HSE On or HSE Bypass.
  82. * @retval HAL status
  83. */
  84. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  85. {
  86. uint32_t tickstart = 0U;
  87. /* Check the parameters */
  88. assert_param(RCC_OscInitStruct != NULL);
  89. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  90. /*------------------------------- HSE Configuration ------------------------*/
  91. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  92. {
  93. /* Check the parameters */
  94. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  95. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  96. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE)
  97. || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE)))
  98. {
  99. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  100. {
  101. return HAL_ERROR;
  102. }
  103. }
  104. else
  105. {
  106. /* Set the new HSE configuration ---------------------------------------*/
  107. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  108. /* Check the HSE State */
  109. if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  110. {
  111. /* Get Start Tick */
  112. tickstart = HAL_GetTick();
  113. /* Wait till HSE is ready */
  114. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  115. {
  116. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  117. {
  118. return HAL_TIMEOUT;
  119. }
  120. }
  121. }
  122. else
  123. {
  124. /* Get Start Tick */
  125. tickstart = HAL_GetTick();
  126. /* Wait till HSE is disabled */
  127. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
  128. {
  129. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  130. {
  131. return HAL_TIMEOUT;
  132. }
  133. }
  134. }
  135. }
  136. }
  137. /*----------------------------- HSI Configuration --------------------------*/
  138. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  139. {
  140. /* Check the parameters */
  141. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  142. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  143. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  144. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI)
  145. || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI)))
  146. {
  147. /* When HSI is used as system clock it will not disabled */
  148. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
  149. {
  150. return HAL_ERROR;
  151. }
  152. /* Otherwise, just the calibration is allowed */
  153. else
  154. {
  155. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  156. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  157. }
  158. }
  159. else
  160. {
  161. /* Check the HSI State */
  162. if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  163. {
  164. /* Enable the Internal High Speed oscillator (HSI). */
  165. __HAL_RCC_HSI_ENABLE();
  166. /* Get Start Tick */
  167. tickstart = HAL_GetTick();
  168. /* Wait till HSI is ready */
  169. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  170. {
  171. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  172. {
  173. return HAL_TIMEOUT;
  174. }
  175. }
  176. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  177. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  178. }
  179. else
  180. {
  181. /* Disable the Internal High Speed oscillator (HSI). */
  182. __HAL_RCC_HSI_DISABLE();
  183. /* Get Start Tick */
  184. tickstart = HAL_GetTick();
  185. /* Wait till HSI is disabled */
  186. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
  187. {
  188. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  189. {
  190. return HAL_TIMEOUT;
  191. }
  192. }
  193. }
  194. }
  195. }
  196. /*------------------------------ LSI Configuration -------------------------*/
  197. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  198. {
  199. /* Check the parameters */
  200. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  201. /* Check the LSI State */
  202. if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  203. {
  204. /* Enable the Internal Low Speed oscillator (LSI). */
  205. __HAL_RCC_LSI_ENABLE();
  206. /* Get Start Tick */
  207. tickstart = HAL_GetTick();
  208. /* Wait till LSI is ready */
  209. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
  210. {
  211. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  212. {
  213. return HAL_TIMEOUT;
  214. }
  215. }
  216. }
  217. else
  218. {
  219. /* Disable the Internal Low Speed oscillator (LSI). */
  220. __HAL_RCC_LSI_DISABLE();
  221. /* Get Start Tick */
  222. tickstart = HAL_GetTick();
  223. /* Wait till LSI is disabled */
  224. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
  225. {
  226. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  227. {
  228. return HAL_TIMEOUT;
  229. }
  230. }
  231. }
  232. }
  233. /*------------------------------ LSE Configuration -------------------------*/
  234. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  235. {
  236. FlagStatus pwrclkchanged = RESET;
  237. /* Check the parameters */
  238. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  239. /* Update LSE configuration in Backup Domain control register */
  240. /* Requires to enable write access to Backup Domain of necessary */
  241. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  242. {
  243. __HAL_RCC_PWR_CLK_ENABLE();
  244. pwrclkchanged = SET;
  245. }
  246. if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  247. {
  248. /* Enable write access to Backup domain */
  249. SET_BIT(PWR->CR, PWR_CR_DBP);
  250. /* Wait for Backup domain Write protection disable */
  251. tickstart = HAL_GetTick();
  252. while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  253. {
  254. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  255. {
  256. return HAL_TIMEOUT;
  257. }
  258. }
  259. }
  260. /* Set the new LSE configuration -----------------------------------------*/
  261. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  262. /* Check the LSE State */
  263. if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  264. {
  265. /* Get Start Tick */
  266. tickstart = HAL_GetTick();
  267. /* Wait till LSE is ready */
  268. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  269. {
  270. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  271. {
  272. return HAL_TIMEOUT;
  273. }
  274. }
  275. }
  276. else
  277. {
  278. /* Get Start Tick */
  279. tickstart = HAL_GetTick();
  280. /* Wait till LSE is disabled */
  281. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
  282. {
  283. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  284. {
  285. return HAL_TIMEOUT;
  286. }
  287. }
  288. }
  289. /* Require to disable power clock if necessary */
  290. if(pwrclkchanged == SET)
  291. {
  292. __HAL_RCC_PWR_CLK_DISABLE();
  293. }
  294. }
  295. /*----------------------------- HSI14 Configuration --------------------------*/
  296. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI14) == RCC_OSCILLATORTYPE_HSI14)
  297. {
  298. /* Check the parameters */
  299. assert_param(IS_RCC_HSI14(RCC_OscInitStruct->HSI14State));
  300. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSI14CalibrationValue));
  301. /* Check the HSI14 State */
  302. if(RCC_OscInitStruct->HSI14State == RCC_HSI14_ON)
  303. {
  304. /* Disable ADC control of the Internal High Speed oscillator HSI14 */
  305. __HAL_RCC_HSI14ADC_DISABLE();
  306. /* Enable the Internal High Speed oscillator (HSI). */
  307. __HAL_RCC_HSI14_ENABLE();
  308. /* Get Start Tick */
  309. tickstart = HAL_GetTick();
  310. /* Wait till HSI is ready */
  311. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI14RDY) == RESET)
  312. {
  313. if((HAL_GetTick() - tickstart) > HSI14_TIMEOUT_VALUE)
  314. {
  315. return HAL_TIMEOUT;
  316. }
  317. }
  318. /* Adjusts the Internal High Speed oscillator 14Mhz (HSI14) calibration value. */
  319. __HAL_RCC_HSI14_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSI14CalibrationValue);
  320. }
  321. else if(RCC_OscInitStruct->HSI14State == RCC_HSI14_ADC_CONTROL)
  322. {
  323. /* Enable ADC control of the Internal High Speed oscillator HSI14 */
  324. __HAL_RCC_HSI14ADC_ENABLE();
  325. /* Adjusts the Internal High Speed oscillator 14Mhz (HSI14) calibration value. */
  326. __HAL_RCC_HSI14_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSI14CalibrationValue);
  327. }
  328. else
  329. {
  330. /* Disable ADC control of the Internal High Speed oscillator HSI14 */
  331. __HAL_RCC_HSI14ADC_DISABLE();
  332. /* Disable the Internal High Speed oscillator (HSI). */
  333. __HAL_RCC_HSI14_DISABLE();
  334. /* Get Start Tick */
  335. tickstart = HAL_GetTick();
  336. /* Wait till HSI is ready */
  337. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI14RDY) != RESET)
  338. {
  339. if((HAL_GetTick() - tickstart) > HSI14_TIMEOUT_VALUE)
  340. {
  341. return HAL_TIMEOUT;
  342. }
  343. }
  344. }
  345. }
  346. #if defined(RCC_HSI48_SUPPORT)
  347. /*----------------------------- HSI48 Configuration --------------------------*/
  348. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
  349. {
  350. /* Check the parameters */
  351. assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
  352. /* When the HSI48 is used as system clock it is not allowed to be disabled */
  353. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI48) ||
  354. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI48)))
  355. {
  356. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) != RESET) && (RCC_OscInitStruct->HSI48State != RCC_HSI48_ON))
  357. {
  358. return HAL_ERROR;
  359. }
  360. }
  361. else
  362. {
  363. /* Check the HSI48 State */
  364. if(RCC_OscInitStruct->HSI48State != RCC_HSI48_OFF)
  365. {
  366. /* Enable the Internal High Speed oscillator (HSI48). */
  367. __HAL_RCC_HSI48_ENABLE();
  368. /* Get Start Tick */
  369. tickstart = HAL_GetTick();
  370. /* Wait till HSI48 is ready */
  371. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) == RESET)
  372. {
  373. if((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  374. {
  375. return HAL_TIMEOUT;
  376. }
  377. }
  378. }
  379. else
  380. {
  381. /* Disable the Internal High Speed oscillator (HSI48). */
  382. __HAL_RCC_HSI48_DISABLE();
  383. /* Get Start Tick */
  384. tickstart = HAL_GetTick();
  385. /* Wait till HSI48 is ready */
  386. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) != RESET)
  387. {
  388. if((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  389. {
  390. return HAL_TIMEOUT;
  391. }
  392. }
  393. }
  394. }
  395. }
  396. #endif /* RCC_HSI48_SUPPORT */
  397. /*-------------------------------- PLL Configuration -----------------------*/
  398. /* Check the parameters */
  399. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  400. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  401. {
  402. /* Check if the PLL is used as system clock or not */
  403. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  404. {
  405. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  406. {
  407. /* Check the parameters */
  408. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  409. assert_param(IS_RCC_PLL_MUL(RCC_OscInitStruct->PLL.PLLMUL));
  410. assert_param(IS_RCC_PREDIV(RCC_OscInitStruct->PLL.PREDIV));
  411. /* Disable the main PLL. */
  412. __HAL_RCC_PLL_DISABLE();
  413. /* Get Start Tick */
  414. tickstart = HAL_GetTick();
  415. /* Wait till PLL is disabled */
  416. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  417. {
  418. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  419. {
  420. return HAL_TIMEOUT;
  421. }
  422. }
  423. /* Configure the main PLL clock source, predivider and multiplication factor. */
  424. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  425. RCC_OscInitStruct->PLL.PREDIV,
  426. RCC_OscInitStruct->PLL.PLLMUL);
  427. /* Enable the main PLL. */
  428. __HAL_RCC_PLL_ENABLE();
  429. /* Get Start Tick */
  430. tickstart = HAL_GetTick();
  431. /* Wait till PLL is ready */
  432. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  433. {
  434. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  435. {
  436. return HAL_TIMEOUT;
  437. }
  438. }
  439. }
  440. else
  441. {
  442. /* Disable the main PLL. */
  443. __HAL_RCC_PLL_DISABLE();
  444. /* Get Start Tick */
  445. tickstart = HAL_GetTick();
  446. /* Wait till PLL is disabled */
  447. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  448. {
  449. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  450. {
  451. return HAL_TIMEOUT;
  452. }
  453. }
  454. }
  455. }
  456. else
  457. {
  458. return HAL_ERROR;
  459. }
  460. }
  461. return HAL_OK;
  462. }
  463. /**
  464. * @brief Initializes the CPU, AHB and APB buses clocks according to the specified
  465. * parameters in the RCC_ClkInitStruct.
  466. * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that
  467. * contains the configuration information for the RCC peripheral.
  468. * @param FLatency FLASH Latency
  469. * The value of this parameter depend on device used within the same series
  470. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  471. * and updated by @ref HAL_RCC_GetHCLKFreq() function called within this function
  472. *
  473. * @note The HSI is used (enabled by hardware) as system clock source after
  474. * start-up from Reset, wake-up from STOP and STANDBY mode, or in case
  475. * of failure of the HSE used directly or indirectly as system clock
  476. * (if the Clock Security System CSS is enabled).
  477. *
  478. * @note A switch from one clock source to another occurs only if the target
  479. * clock source is ready (clock stable after start-up delay or PLL locked).
  480. * If a clock source which is not yet ready is selected, the switch will
  481. * occur when the clock source will be ready.
  482. * You can use @ref HAL_RCC_GetClockConfig() function to know which clock is
  483. * currently used as system clock source.
  484. * @retval HAL status
  485. */
  486. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  487. {
  488. uint32_t tickstart = 0U;
  489. /* Check the parameters */
  490. assert_param(RCC_ClkInitStruct != NULL);
  491. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  492. assert_param(IS_FLASH_LATENCY(FLatency));
  493. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  494. must be correctly programmed according to the frequency of the CPU clock
  495. (HCLK) of the device. */
  496. /* Increasing the number of wait states because of higher CPU frequency */
  497. if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
  498. {
  499. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  500. __HAL_FLASH_SET_LATENCY(FLatency);
  501. /* Check that the new number of wait states is taken into account to access the Flash
  502. memory by reading the FLASH_ACR register */
  503. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  504. {
  505. return HAL_ERROR;
  506. }
  507. }
  508. /*-------------------------- HCLK Configuration --------------------------*/
  509. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  510. {
  511. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  512. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  513. }
  514. /*------------------------- SYSCLK Configuration ---------------------------*/
  515. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  516. {
  517. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  518. /* HSE is selected as System Clock Source */
  519. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  520. {
  521. /* Check the HSE ready flag */
  522. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  523. {
  524. return HAL_ERROR;
  525. }
  526. }
  527. /* PLL is selected as System Clock Source */
  528. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  529. {
  530. /* Check the PLL ready flag */
  531. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  532. {
  533. return HAL_ERROR;
  534. }
  535. }
  536. #if defined(RCC_CFGR_SWS_HSI48)
  537. /* HSI48 is selected as System Clock Source */
  538. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSI48)
  539. {
  540. /* Check the HSI48 ready flag */
  541. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) == RESET)
  542. {
  543. return HAL_ERROR;
  544. }
  545. }
  546. #endif /* RCC_CFGR_SWS_HSI48 */
  547. /* HSI is selected as System Clock Source */
  548. else
  549. {
  550. /* Check the HSI ready flag */
  551. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  552. {
  553. return HAL_ERROR;
  554. }
  555. }
  556. __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource);
  557. /* Get Start Tick */
  558. tickstart = HAL_GetTick();
  559. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  560. {
  561. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)
  562. {
  563. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  564. {
  565. return HAL_TIMEOUT;
  566. }
  567. }
  568. }
  569. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  570. {
  571. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  572. {
  573. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  574. {
  575. return HAL_TIMEOUT;
  576. }
  577. }
  578. }
  579. #if defined(RCC_CFGR_SWS_HSI48)
  580. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSI48)
  581. {
  582. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI48)
  583. {
  584. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  585. {
  586. return HAL_TIMEOUT;
  587. }
  588. }
  589. }
  590. #endif /* RCC_CFGR_SWS_HSI48 */
  591. else
  592. {
  593. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)
  594. {
  595. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  596. {
  597. return HAL_TIMEOUT;
  598. }
  599. }
  600. }
  601. }
  602. /* Decreasing the number of wait states because of lower CPU frequency */
  603. if(FLatency < (FLASH->ACR & FLASH_ACR_LATENCY))
  604. {
  605. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  606. __HAL_FLASH_SET_LATENCY(FLatency);
  607. /* Check that the new number of wait states is taken into account to access the Flash
  608. memory by reading the FLASH_ACR register */
  609. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  610. {
  611. return HAL_ERROR;
  612. }
  613. }
  614. /*-------------------------- PCLK1 Configuration ---------------------------*/
  615. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  616. {
  617. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  618. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE, RCC_ClkInitStruct->APB1CLKDivider);
  619. }
  620. /* Update the SystemCoreClock global variable */
  621. //SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> RCC_CFGR_HPRE_BITNUMBER];
  622. SystemCoreClock = 7372800;
  623. /* Configure the source of time base considering new system clocks settings*/
  624. //HAL_InitTick (TICK_INT_PRIORITY);
  625. return HAL_OK;
  626. }
  627. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  628. {
  629. uint32_t tickstart = 0U;
  630. uint32_t temp_reg = 0U;
  631. /* Check the parameters */
  632. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  633. /*---------------------------- RTC configuration -------------------------------*/
  634. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  635. {
  636. /* check for RTC Parameters used to output RTCCLK */
  637. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  638. FlagStatus pwrclkchanged = RESET;
  639. /* As soon as function is called to change RTC clock source, activation of the
  640. power domain is done. */
  641. /* Requires to enable write access to Backup Domain of necessary */
  642. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  643. {
  644. __HAL_RCC_PWR_CLK_ENABLE();
  645. pwrclkchanged = SET;
  646. }
  647. if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  648. {
  649. /* Enable write access to Backup domain */
  650. SET_BIT(PWR->CR, PWR_CR_DBP);
  651. /* Wait for Backup domain Write protection disable */
  652. tickstart = HAL_GetTick();
  653. while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  654. {
  655. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  656. {
  657. return HAL_TIMEOUT;
  658. }
  659. }
  660. }
  661. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  662. temp_reg = (RCC->BDCR & RCC_BDCR_RTCSEL);
  663. if((temp_reg != 0x00000000U) && (temp_reg != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  664. {
  665. /* Store the content of BDCR register before the reset of Backup Domain */
  666. temp_reg = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  667. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  668. __HAL_RCC_BACKUPRESET_FORCE();
  669. __HAL_RCC_BACKUPRESET_RELEASE();
  670. /* Restore the Content of BDCR register */
  671. RCC->BDCR = temp_reg;
  672. /* Wait for LSERDY if LSE was enabled */
  673. if (HAL_IS_BIT_SET(temp_reg, RCC_BDCR_LSEON))
  674. {
  675. /* Get Start Tick */
  676. tickstart = HAL_GetTick();
  677. /* Wait till LSE is ready */
  678. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  679. {
  680. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  681. {
  682. return HAL_TIMEOUT;
  683. }
  684. }
  685. }
  686. }
  687. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  688. /* Require to disable power clock if necessary */
  689. if(pwrclkchanged == SET)
  690. {
  691. __HAL_RCC_PWR_CLK_DISABLE();
  692. }
  693. }
  694. /*------------------------------- USART1 Configuration ------------------------*/
  695. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART1) == RCC_PERIPHCLK_USART1)
  696. {
  697. /* Check the parameters */
  698. assert_param(IS_RCC_USART1CLKSOURCE(PeriphClkInit->Usart1ClockSelection));
  699. /* Configure the USART1 clock source */
  700. __HAL_RCC_USART1_CONFIG(PeriphClkInit->Usart1ClockSelection);
  701. }
  702. #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx)\
  703. || defined(STM32F091xC) || defined(STM32F098xx)
  704. /*----------------------------- USART2 Configuration --------------------------*/
  705. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART2) == RCC_PERIPHCLK_USART2)
  706. {
  707. /* Check the parameters */
  708. assert_param(IS_RCC_USART2CLKSOURCE(PeriphClkInit->Usart2ClockSelection));
  709. /* Configure the USART2 clock source */
  710. __HAL_RCC_USART2_CONFIG(PeriphClkInit->Usart2ClockSelection);
  711. }
  712. #endif /* STM32F071xB || STM32F072xB || STM32F078xx || */
  713. /* STM32F091xC || STM32F098xx */
  714. #if defined(STM32F091xC) || defined(STM32F098xx)
  715. /*----------------------------- USART3 Configuration --------------------------*/
  716. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART3) == RCC_PERIPHCLK_USART3)
  717. {
  718. /* Check the parameters */
  719. assert_param(IS_RCC_USART3CLKSOURCE(PeriphClkInit->Usart3ClockSelection));
  720. /* Configure the USART3 clock source */
  721. __HAL_RCC_USART3_CONFIG(PeriphClkInit->Usart3ClockSelection);
  722. }
  723. #endif /* STM32F091xC || STM32F098xx */
  724. /*------------------------------ I2C1 Configuration ------------------------*/
  725. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C1) == RCC_PERIPHCLK_I2C1)
  726. {
  727. /* Check the parameters */
  728. assert_param(IS_RCC_I2C1CLKSOURCE(PeriphClkInit->I2c1ClockSelection));
  729. /* Configure the I2C1 clock source */
  730. __HAL_RCC_I2C1_CONFIG(PeriphClkInit->I2c1ClockSelection);
  731. }
  732. #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || defined(STM32F070x6)
  733. /*------------------------------ USB Configuration ------------------------*/
  734. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USB) == RCC_PERIPHCLK_USB)
  735. {
  736. /* Check the parameters */
  737. assert_param(IS_RCC_USBCLKSOURCE(PeriphClkInit->UsbClockSelection));
  738. /* Configure the USB clock source */
  739. __HAL_RCC_USB_CONFIG(PeriphClkInit->UsbClockSelection);
  740. }
  741. #endif /* STM32F042x6 || STM32F048xx || STM32F072xB || STM32F078xx || STM32F070xB || STM32F070x6 */
  742. #if defined(STM32F042x6) || defined(STM32F048xx)\
  743. || defined(STM32F051x8) || defined(STM32F058xx)\
  744. || defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx)\
  745. || defined(STM32F091xC) || defined(STM32F098xx)
  746. /*------------------------------ CEC clock Configuration -------------------*/
  747. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CEC) == RCC_PERIPHCLK_CEC)
  748. {
  749. /* Check the parameters */
  750. assert_param(IS_RCC_CECCLKSOURCE(PeriphClkInit->CecClockSelection));
  751. /* Configure the CEC clock source */
  752. __HAL_RCC_CEC_CONFIG(PeriphClkInit->CecClockSelection);
  753. }
  754. #endif /* STM32F042x6 || STM32F048xx || */
  755. /* STM32F051x8 || STM32F058xx || */
  756. /* STM32F071xB || STM32F072xB || STM32F078xx || */
  757. /* STM32F091xC || STM32F098xx */
  758. return HAL_OK;
  759. }
  760. #define GPIO_MODE (0x00000003U)
  761. #define EXTI_MODE (0x10000000U)
  762. #define GPIO_MODE_IT (0x00010000U)
  763. #define GPIO_MODE_EVT (0x00020000U)
  764. #define RISING_EDGE (0x00100000U)
  765. #define FALLING_EDGE (0x00200000U)
  766. #define GPIO_OUTPUT_TYPE (0x00000010U)
  767. #define GPIO_NUMBER (16U)
  768. #define __HAL_RCC_GPIOA_CLK_ENABLE() do { \
  769. __IO uint32_t tmpreg; \
  770. SET_BIT(RCC->AHBENR, RCC_AHBENR_GPIOAEN);\
  771. /* Delay after an RCC peripheral clock enabling */\
  772. tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_GPIOAEN);\
  773. UNUSED(tmpreg); \
  774. } while(0)
  775. #define __HAL_RCC_USART1_CLK_ENABLE() do { \
  776. __IO uint32_t tmpreg; \
  777. SET_BIT(RCC->APB2ENR, RCC_APB2ENR_USART1EN);\
  778. /* Delay after an RCC peripheral clock enabling */\
  779. tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_USART1EN);\
  780. UNUSED(tmpreg); \
  781. } while(0)
  782. /**
  783. * @brief Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
  784. * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F0 family
  785. * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
  786. * the configuration information for the specified GPIO peripheral.
  787. * @retval None
  788. */
  789. void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  790. {
  791. uint32_t position = 0x00U;
  792. uint32_t iocurrent = 0x00U;
  793. uint32_t temp = 0x00U;
  794. /* Check the parameters */
  795. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  796. assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
  797. assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
  798. assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
  799. /* Configure the port pins */
  800. while (((GPIO_Init->Pin) >> position) != RESET)
  801. {
  802. /* Get current io position */
  803. iocurrent = (GPIO_Init->Pin) & (1U << position);
  804. if(iocurrent)
  805. {
  806. /*--------------------- GPIO Mode Configuration ------------------------*/
  807. /* In case of Alternate function mode selection */
  808. if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
  809. {
  810. /* Check the Alternate function parameters */
  811. assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
  812. assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
  813. /* Configure Alternate function mapped with the current IO */
  814. temp = GPIOx->AFR[position >> 3];
  815. CLEAR_BIT(temp, 0xFU << ((uint32_t)(position & 0x07U) * 4U)) ;
  816. SET_BIT(temp, (uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & 0x07U) * 4U));
  817. GPIOx->AFR[position >> 3U] = temp;
  818. }
  819. /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
  820. temp = GPIOx->MODER;
  821. CLEAR_BIT(temp, GPIO_MODER_MODER0 << (position * 2U));
  822. SET_BIT(temp, (GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
  823. GPIOx->MODER = temp;
  824. /* In case of Output or Alternate function mode selection */
  825. if ((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
  826. (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
  827. {
  828. /* Check the Speed parameter */
  829. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  830. /* Configure the IO Speed */
  831. temp = GPIOx->OSPEEDR;
  832. CLEAR_BIT(temp, GPIO_OSPEEDER_OSPEEDR0 << (position * 2U));
  833. SET_BIT(temp, GPIO_Init->Speed << (position * 2U));
  834. GPIOx->OSPEEDR = temp;
  835. /* Configure the IO Output Type */
  836. temp = GPIOx->OTYPER;
  837. CLEAR_BIT(temp, GPIO_OTYPER_OT_0 << position) ;
  838. SET_BIT(temp, ((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4U) << position);
  839. GPIOx->OTYPER = temp;
  840. }
  841. /* Activate the Pull-up or Pull down resistor for the current IO */
  842. temp = GPIOx->PUPDR;
  843. CLEAR_BIT(temp, GPIO_PUPDR_PUPDR0 << (position * 2U));
  844. SET_BIT(temp, (GPIO_Init->Pull) << (position * 2U));
  845. GPIOx->PUPDR = temp;
  846. /*--------------------- EXTI Mode Configuration ------------------------*/
  847. /* Configure the External Interrupt or event for the current IO */
  848. if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
  849. {
  850. /* Enable SYSCFG Clock */
  851. __HAL_RCC_SYSCFG_CLK_ENABLE();
  852. temp = SYSCFG->EXTICR[position >> 2];
  853. CLEAR_BIT(temp, (0x0FU) << (4U * (position & 0x03U)));
  854. SET_BIT(temp, (GPIO_GET_INDEX(GPIOx)) << (4U * (position & 0x03U)));
  855. SYSCFG->EXTICR[position >> 2] = temp;
  856. /* Clear EXTI line configuration */
  857. temp = EXTI->IMR;
  858. CLEAR_BIT(temp, (uint32_t)iocurrent);
  859. if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
  860. {
  861. SET_BIT(temp, iocurrent);
  862. }
  863. EXTI->IMR = temp;
  864. temp = EXTI->EMR;
  865. CLEAR_BIT(temp, (uint32_t)iocurrent);
  866. if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
  867. {
  868. SET_BIT(temp, iocurrent);
  869. }
  870. EXTI->EMR = temp;
  871. /* Clear Rising Falling edge configuration */
  872. temp = EXTI->RTSR;
  873. CLEAR_BIT(temp, (uint32_t)iocurrent);
  874. if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
  875. {
  876. SET_BIT(temp, iocurrent);
  877. }
  878. EXTI->RTSR = temp;
  879. temp = EXTI->FTSR;
  880. CLEAR_BIT(temp, (uint32_t)iocurrent);
  881. if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
  882. {
  883. SET_BIT(temp, iocurrent);
  884. }
  885. EXTI->FTSR = temp;
  886. }
  887. }
  888. position++;
  889. }
  890. }
  891. /**
  892. * @brief Set or clear the selected data port bit.
  893. * @note This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
  894. * accesses. In this way, there is no risk of an IRQ occurring between
  895. * the read and the modify access.
  896. *
  897. * @param GPIOx: where x can be (A..H) to select the GPIO peripheral for STM32F0 family
  898. * @param GPIO_Pin: specifies the port bit to be written.
  899. * This parameter can be one of GPIO_PIN_x where x can be (0..15).
  900. * @param PinState: specifies the value to be written to the selected bit.
  901. * This parameter can be one of the GPIO_PinState enum values:
  902. * @arg GPIO_PIN_RESET: to clear the port pin
  903. * @arg GPIO_PIN_SET: to set the port pin
  904. * @retval None
  905. */
  906. void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  907. {
  908. /* Check the parameters */
  909. assert_param(IS_GPIO_PIN(GPIO_Pin));
  910. assert_param(IS_GPIO_PIN_ACTION(PinState));
  911. if (PinState != GPIO_PIN_RESET)
  912. {
  913. GPIOx->BSRR = (uint32_t)GPIO_Pin;
  914. }
  915. else
  916. {
  917. GPIOx->BRR = (uint32_t)GPIO_Pin;
  918. }
  919. }
  920. /**
  921. * @brief Handle UART Communication Timeout.
  922. * @param huart UART handle.
  923. * @param Flag Specifies the UART flag to check
  924. * @param Status Flag status (SET or RESET)
  925. * @param Tickstart Tick start value
  926. * @param Timeout Timeout duration
  927. * @retval HAL status
  928. */
  929. HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  930. {
  931. /* Wait until flag is set */
  932. while((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
  933. {
  934. /* Check for the Timeout */
  935. if(Timeout != HAL_MAX_DELAY)
  936. {
  937. if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout))
  938. {
  939. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  940. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  941. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  942. huart->gState = HAL_UART_STATE_READY;
  943. huart->RxState = HAL_UART_STATE_READY;
  944. /* Process Unlocked */
  945. __HAL_UNLOCK(huart);
  946. return HAL_TIMEOUT;
  947. }
  948. }
  949. }
  950. return HAL_OK;
  951. }
  952. /**
  953. * @brief Check the UART Idle State.
  954. * @param huart UART handle.
  955. * @retval HAL status
  956. */
  957. HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
  958. {
  959. #if !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070xB)&& !defined(STM32F070x6)&& !defined(STM32F030xC)
  960. uint32_t tickstart = 0U;
  961. #endif /* !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070xB)&& !defined(STM32F070x6)&& !defined(STM32F030xC) */
  962. /* Initialize the UART ErrorCode */
  963. huart->ErrorCode = HAL_UART_ERROR_NONE;
  964. #if !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070xB)&& !defined(STM32F070x6)&& !defined(STM32F030xC)
  965. /* Init tickstart for timeout managment*/
  966. tickstart = HAL_GetTick();
  967. /* TEACK and REACK bits in ISR are checked only when available (not available on all F0 devices).
  968. Bits are defined for some specific devices, and are available only for UART instances supporting WakeUp from Stop Mode feature.
  969. */
  970. if (IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance))
  971. {
  972. /* Check if the Transmitter is enabled */
  973. if((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  974. {
  975. /* Wait until TEACK flag is set */
  976. if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  977. {
  978. /* Timeout occurred */
  979. return HAL_TIMEOUT;
  980. }
  981. }
  982. /* Check if the Receiver is enabled */
  983. if((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  984. {
  985. /* Wait until REACK flag is set */
  986. if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  987. {
  988. /* Timeout occurred */
  989. return HAL_TIMEOUT;
  990. }
  991. }
  992. }
  993. #endif /* !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070xB)&& !defined(STM32F070x6)&& !defined(STM32F030xC) */
  994. /* Initialize the UART State */
  995. huart->gState = HAL_UART_STATE_READY;
  996. huart->RxState = HAL_UART_STATE_READY;
  997. /* Process Unlocked */
  998. __HAL_UNLOCK(huart);
  999. return HAL_OK;
  1000. }
  1001. HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
  1002. {
  1003. uint32_t tmpreg = 0x00000000U;
  1004. UART_ClockSourceTypeDef clocksource = UART_CLOCKSOURCE_UNDEFINED;
  1005. uint16_t brrtemp = 0x0000U;
  1006. uint16_t usartdiv = 0x0000U;
  1007. HAL_StatusTypeDef ret = HAL_OK;
  1008. /* Check the parameters */
  1009. assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
  1010. assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  1011. assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
  1012. assert_param(IS_UART_PARITY(huart->Init.Parity));
  1013. assert_param(IS_UART_MODE(huart->Init.Mode));
  1014. assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
  1015. assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
  1016. assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  1017. /*-------------------------- USART CR1 Configuration -----------------------*/
  1018. /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
  1019. * the UART Word Length, Parity, Mode and oversampling:
  1020. * set the M bits according to huart->Init.WordLength value
  1021. * set PCE and PS bits according to huart->Init.Parity value
  1022. * set TE and RE bits according to huart->Init.Mode value
  1023. * set OVER8 bit according to huart->Init.OverSampling value */
  1024. tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
  1025. MODIFY_REG(huart->Instance->CR1, UART_CR1_FIELDS, tmpreg);
  1026. /*-------------------------- USART CR2 Configuration -----------------------*/
  1027. /* Configure the UART Stop Bits: Set STOP[13:12] bits according
  1028. * to huart->Init.StopBits value */
  1029. MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
  1030. /*-------------------------- USART CR3 Configuration -----------------------*/
  1031. /* Configure
  1032. * - UART HardWare Flow Control: set CTSE and RTSE bits according
  1033. * to huart->Init.HwFlowCtl value
  1034. * - one-bit sampling method versus three samples' majority rule according
  1035. * to huart->Init.OneBitSampling */
  1036. tmpreg = (uint32_t)huart->Init.HwFlowCtl | huart->Init.OneBitSampling ;
  1037. MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT), tmpreg);
  1038. /*-------------------------- USART BRR Configuration -----------------------*/
  1039. UART_GETCLOCKSOURCE(huart, clocksource);
  1040. /* Check UART Over Sampling to set Baud Rate Register */
  1041. if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
  1042. {
  1043. switch (clocksource)
  1044. {
  1045. case UART_CLOCKSOURCE_PCLK1:
  1046. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
  1047. break;
  1048. case UART_CLOCKSOURCE_HSI:
  1049. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate));
  1050. break;
  1051. case UART_CLOCKSOURCE_SYSCLK:
  1052. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
  1053. break;
  1054. case UART_CLOCKSOURCE_LSE:
  1055. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(LSE_VALUE, huart->Init.BaudRate));
  1056. break;
  1057. case UART_CLOCKSOURCE_UNDEFINED:
  1058. default:
  1059. ret = HAL_ERROR;
  1060. break;
  1061. }
  1062. brrtemp = usartdiv & 0xFFF0U;
  1063. brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
  1064. huart->Instance->BRR = brrtemp;
  1065. }
  1066. else
  1067. {
  1068. switch (clocksource)
  1069. {
  1070. case UART_CLOCKSOURCE_PCLK1:
  1071. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
  1072. break;
  1073. case UART_CLOCKSOURCE_HSI:
  1074. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate));
  1075. break;
  1076. case UART_CLOCKSOURCE_SYSCLK:
  1077. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
  1078. break;
  1079. case UART_CLOCKSOURCE_LSE:
  1080. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(LSE_VALUE, huart->Init.BaudRate));
  1081. break;
  1082. case UART_CLOCKSOURCE_UNDEFINED:
  1083. default:
  1084. ret = HAL_ERROR;
  1085. break;
  1086. }
  1087. }
  1088. return ret;
  1089. }
  1090. /**
  1091. * @brief Initialize the UART mode according to the specified
  1092. * parameters in the UART_InitTypeDef and initialize the associated handle.
  1093. * @param huart: UART handle.
  1094. * @retval HAL status
  1095. */
  1096. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  1097. {
  1098. /* Check the UART handle allocation */
  1099. if(huart == NULL)
  1100. {
  1101. return HAL_ERROR;
  1102. }
  1103. if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
  1104. {
  1105. /* Check the parameters */
  1106. assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
  1107. }
  1108. else
  1109. {
  1110. /* Check the parameters */
  1111. assert_param(IS_UART_INSTANCE(huart->Instance));
  1112. }
  1113. if(huart->gState == HAL_UART_STATE_RESET)
  1114. {
  1115. /* Allocate lock resource and initialize it */
  1116. huart->Lock = HAL_UNLOCKED;
  1117. /* Init the low level hardware : GPIO, CLOCK */
  1118. //HAL_UART_MspInit(huart);
  1119. }
  1120. huart->gState = HAL_UART_STATE_BUSY;
  1121. /* Disable the Peripheral */
  1122. __HAL_UART_DISABLE(huart);
  1123. /* Set the UART Communication parameters */
  1124. if (UART_SetConfig(huart) == HAL_ERROR)
  1125. {
  1126. return HAL_ERROR;
  1127. }
  1128. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  1129. {
  1130. //UART_AdvFeatureConfig(huart);
  1131. }
  1132. /* In asynchronous mode, the following bits must be kept cleared:
  1133. - LINEN (if LIN is supported) and CLKEN bits in the USART_CR2 register,
  1134. - SCEN (if Smartcard is supported), HDSEL and IREN (if IrDA is supported) bits in the USART_CR3 register. */
  1135. #if defined (USART_CR2_LINEN)
  1136. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  1137. #else
  1138. CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
  1139. #endif
  1140. #if defined (USART_CR3_SCEN)
  1141. #if defined (USART_CR3_IREN)
  1142. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  1143. #else
  1144. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL));
  1145. #endif
  1146. #else
  1147. #if defined (USART_CR3_IREN)
  1148. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN));
  1149. #else
  1150. CLEAR_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
  1151. #endif
  1152. #endif
  1153. /* Enable the Peripheral */
  1154. __HAL_UART_ENABLE(huart);
  1155. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  1156. return (UART_CheckIdleState(huart));
  1157. }
  1158. /**
  1159. * @brief Receive an amount of data in blocking mode.
  1160. * @param huart: UART handle.
  1161. * @param pData: pointer to data buffer.
  1162. * @param Size: amount of data to be received.
  1163. * @param Timeout: Timeout duration.
  1164. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1165. * address of user data buffer for storing data to be received, should be aligned on a half word frontier (16 bits)
  1166. * (as received data will be handled using u16 pointer cast). Depending on compilation chain,
  1167. * use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  1168. * @retval HAL status
  1169. */
  1170. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1171. {
  1172. uint16_t* tmp;
  1173. uint16_t uhMask;
  1174. uint32_t tickstart = 0;
  1175. /* Check that a Rx process is not already ongoing */
  1176. if(huart->RxState == HAL_UART_STATE_READY)
  1177. {
  1178. if((pData == NULL ) || (Size == 0U))
  1179. {
  1180. return HAL_ERROR;
  1181. }
  1182. /* In case of 9bits/No Parity transfer, pData buffer provided as input paramter
  1183. should be aligned on a u16 frontier, as data to be received from RDR will be
  1184. handled through a u16 cast. */
  1185. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1186. {
  1187. if((((uint32_t)pData)&1U) != 0U)
  1188. {
  1189. return HAL_ERROR;
  1190. }
  1191. }
  1192. /* Process Locked */
  1193. __HAL_LOCK(huart);
  1194. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1195. huart->RxState = HAL_UART_STATE_BUSY_RX;
  1196. /* Init tickstart for timeout managment*/
  1197. tickstart = HAL_GetTick();
  1198. huart->RxXferSize = Size;
  1199. huart->RxXferCount = Size;
  1200. /* Computation of UART mask to apply to RDR register */
  1201. UART_MASK_COMPUTATION(huart);
  1202. uhMask = huart->Mask;
  1203. /* as long as data have to be received */
  1204. while(huart->RxXferCount > 0U)
  1205. {
  1206. huart->RxXferCount--;
  1207. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  1208. {
  1209. return HAL_TIMEOUT;
  1210. }
  1211. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1212. {
  1213. tmp = (uint16_t*) pData ;
  1214. *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
  1215. pData +=2U;
  1216. }
  1217. else
  1218. {
  1219. *pData++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  1220. }
  1221. }
  1222. /* At end of Rx process, restore huart->RxState to Ready */
  1223. huart->RxState = HAL_UART_STATE_READY;
  1224. /* Process Unlocked */
  1225. __HAL_UNLOCK(huart);
  1226. return HAL_OK;
  1227. }
  1228. else
  1229. {
  1230. return HAL_BUSY;
  1231. }
  1232. }
  1233. /**
  1234. * @brief Send an amount of data in blocking mode.
  1235. * @param huart: UART handle.
  1236. * @param pData: Pointer to data buffer.
  1237. * @param Size: Amount of data to be sent.
  1238. * @param Timeout: Timeout duration.
  1239. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1240. * address of user data buffer containing data to be sent, should be aligned on a half word frontier (16 bits)
  1241. * (as sent data will be handled using u16 pointer cast). Depending on compilation chain,
  1242. * use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  1243. * @retval HAL status
  1244. */
  1245. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1246. {
  1247. uint16_t* tmp;
  1248. uint32_t tickstart = 0U;
  1249. /* Check that a Tx process is not already ongoing */
  1250. if(huart->gState == HAL_UART_STATE_READY)
  1251. {
  1252. if((pData == NULL ) || (Size == 0U))
  1253. {
  1254. return HAL_ERROR;
  1255. }
  1256. /* In case of 9bits/No Parity transfer, pData buffer provided as input paramter
  1257. should be aligned on a u16 frontier, as data to be filled into TDR will be
  1258. handled through a u16 cast. */
  1259. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1260. {
  1261. if((((uint32_t)pData)&1U) != 0U)
  1262. {
  1263. return HAL_ERROR;
  1264. }
  1265. }
  1266. /* Process Locked */
  1267. __HAL_LOCK(huart);
  1268. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1269. huart->gState = HAL_UART_STATE_BUSY_TX;
  1270. /* Init tickstart for timeout managment*/
  1271. tickstart = HAL_GetTick();
  1272. huart->TxXferSize = Size;
  1273. huart->TxXferCount = Size;
  1274. while(huart->TxXferCount > 0)
  1275. {
  1276. huart->TxXferCount--;
  1277. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  1278. {
  1279. return HAL_TIMEOUT;
  1280. }
  1281. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1282. {
  1283. tmp = (uint16_t*) pData;
  1284. huart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
  1285. pData += 2;
  1286. }
  1287. else
  1288. {
  1289. huart->Instance->TDR = (*pData++ & (uint8_t)0xFFU);
  1290. }
  1291. }
  1292. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  1293. {
  1294. return HAL_TIMEOUT;
  1295. }
  1296. /* At end of Tx process, restore huart->gState to Ready */
  1297. huart->gState = HAL_UART_STATE_READY;
  1298. /* Process Unlocked */
  1299. __HAL_UNLOCK(huart);
  1300. return HAL_OK;
  1301. }
  1302. else
  1303. {
  1304. return HAL_BUSY;
  1305. }
  1306. }