stm32f3_hal_lowlevel.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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 "stm32f3_hal.h"
  35. #include "stm32f3_hal_lowlevel.h"
  36. #include "stm32f3xx_hal_rcc.h"
  37. #include "stm32f3xx_hal_gpio.h"
  38. #include "stm32f3xx_hal_dma.h"
  39. #include "stm32f3xx_hal_uart.h"
  40. #include "stm32f3xx_hal_flash.h"
  41. #include "stm32f3xx_hal_cortex.h"
  42. #define assert_param(expr) ((void)0U)
  43. uint32_t hal_sys_tick = 0;
  44. uint32_t uwTick = 0;
  45. uint32_t SystemCoreClock = 8000000U;
  46. void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
  47. {
  48. uint32_t prioritygroup = 0x00U;
  49. /* Check the parameters */
  50. assert_param(IS_NVIC_SUB_PRIORITY(SubPriority));
  51. assert_param(IS_NVIC_PREEMPTION_PRIORITY(PreemptPriority));
  52. prioritygroup = NVIC_GetPriorityGrouping();
  53. NVIC_SetPriority(IRQn, NVIC_EncodePriority(prioritygroup, PreemptPriority, SubPriority));
  54. }
  55. #ifndef ENABLE_TICK_TIMING
  56. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  57. {
  58. hal_sys_tick = 0;
  59. return HAL_OK;
  60. }
  61. uint32_t HAL_GetTick(void)
  62. {
  63. return hal_sys_tick++;
  64. }
  65. void HAL_IncTick(void)
  66. {
  67. }
  68. #else
  69. __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  70. {
  71. /*Configure the SysTick to have interrupt in 1ms time basis*/
  72. HAL_SYSTICK_Config(SystemCoreClock / 1000U);
  73. /*Configure the SysTick IRQ priority */
  74. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0U);
  75. /* Return function status */
  76. return HAL_OK;
  77. }
  78. __weak uint32_t HAL_GetTick(void)
  79. {
  80. return uwTick;
  81. }
  82. __weak void HAL_IncTick(void)
  83. {
  84. uwTick++;
  85. }
  86. #endif
  87. #define RCC_CFGR_HPRE_BITNUMBER POSITION_VAL(RCC_CFGR_HPRE)
  88. #define UART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  89. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
  90. uint32_t HAL_RCC_GetSysClockFreq(void)
  91. {
  92. return F_CPU;
  93. }
  94. uint32_t HAL_RCC_GetPCLK1Freq(void)
  95. {
  96. return F_CPU;
  97. }
  98. /**
  99. * @brief Initializes the RCC Oscillators according to the specified parameters in the
  100. * RCC_OscInitTypeDef.
  101. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  102. * contains the configuration information for the RCC Oscillators.
  103. * @note The PLL is not disabled when used as system clock.
  104. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  105. * supported by this macro. User should request a transition to LSE Off
  106. * first and then LSE On or LSE Bypass.
  107. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  108. * supported by this macro. User should request a transition to HSE Off
  109. * first and then HSE On or HSE Bypass.
  110. * @retval HAL status
  111. */
  112. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  113. {
  114. uint32_t tickstart = 0U;
  115. /* Check the parameters */
  116. assert_param(RCC_OscInitStruct != NULL);
  117. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  118. /*------------------------------- HSE Configuration ------------------------*/
  119. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  120. {
  121. /* Check the parameters */
  122. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  123. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  124. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE)
  125. || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE)))
  126. {
  127. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  128. {
  129. return HAL_ERROR;
  130. }
  131. }
  132. else
  133. {
  134. /* Set the new HSE configuration ---------------------------------------*/
  135. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  136. #if defined(RCC_CFGR_PLLSRC_HSI_DIV2)
  137. /* Configure the HSE predivision factor --------------------------------*/
  138. __HAL_RCC_HSE_PREDIV_CONFIG(RCC_OscInitStruct->HSEPredivValue);
  139. #endif /* RCC_CFGR_PLLSRC_HSI_DIV2 */
  140. /* Check the HSE State */
  141. if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  142. {
  143. /* Get Start Tick */
  144. tickstart = HAL_GetTick();
  145. /* Wait till HSE is ready */
  146. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  147. {
  148. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  149. {
  150. return HAL_TIMEOUT;
  151. }
  152. }
  153. }
  154. else
  155. {
  156. /* Get Start Tick */
  157. tickstart = HAL_GetTick();
  158. /* Wait till HSE is disabled */
  159. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
  160. {
  161. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  162. {
  163. return HAL_TIMEOUT;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. /*----------------------------- HSI Configuration --------------------------*/
  170. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  171. {
  172. /* Check the parameters */
  173. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  174. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  175. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  176. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI)
  177. || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI)))
  178. {
  179. /* When HSI is used as system clock it will not disabled */
  180. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
  181. {
  182. return HAL_ERROR;
  183. }
  184. /* Otherwise, just the calibration is allowed */
  185. else
  186. {
  187. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  188. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  189. }
  190. }
  191. else
  192. {
  193. /* Check the HSI State */
  194. if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  195. {
  196. /* Enable the Internal High Speed oscillator (HSI). */
  197. __HAL_RCC_HSI_ENABLE();
  198. /* Get Start Tick */
  199. tickstart = HAL_GetTick();
  200. /* Wait till HSI is ready */
  201. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  202. {
  203. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  204. {
  205. return HAL_TIMEOUT;
  206. }
  207. }
  208. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  209. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  210. }
  211. else
  212. {
  213. /* Disable the Internal High Speed oscillator (HSI). */
  214. __HAL_RCC_HSI_DISABLE();
  215. /* Get Start Tick */
  216. tickstart = HAL_GetTick();
  217. /* Wait till HSI is disabled */
  218. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
  219. {
  220. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  221. {
  222. return HAL_TIMEOUT;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. /*------------------------------ LSI Configuration -------------------------*/
  229. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  230. {
  231. /* Check the parameters */
  232. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  233. /* Check the LSI State */
  234. if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  235. {
  236. /* Enable the Internal Low Speed oscillator (LSI). */
  237. __HAL_RCC_LSI_ENABLE();
  238. /* Get Start Tick */
  239. tickstart = HAL_GetTick();
  240. /* Wait till LSI is ready */
  241. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
  242. {
  243. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  244. {
  245. return HAL_TIMEOUT;
  246. }
  247. }
  248. }
  249. else
  250. {
  251. /* Disable the Internal Low Speed oscillator (LSI). */
  252. __HAL_RCC_LSI_DISABLE();
  253. /* Get Start Tick */
  254. tickstart = HAL_GetTick();
  255. /* Wait till LSI is disabled */
  256. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
  257. {
  258. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  259. {
  260. return HAL_TIMEOUT;
  261. }
  262. }
  263. }
  264. }
  265. /*------------------------------ LSE Configuration -------------------------*/
  266. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  267. {
  268. FlagStatus pwrclkchanged = RESET;
  269. /* Check the parameters */
  270. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  271. /* Update LSE configuration in Backup Domain control register */
  272. /* Requires to enable write access to Backup Domain of necessary */
  273. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  274. {
  275. __HAL_RCC_PWR_CLK_ENABLE();
  276. pwrclkchanged = SET;
  277. }
  278. if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  279. {
  280. /* Enable write access to Backup domain */
  281. SET_BIT(PWR->CR, PWR_CR_DBP);
  282. /* Wait for Backup domain Write protection disable */
  283. tickstart = HAL_GetTick();
  284. while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  285. {
  286. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  287. {
  288. return HAL_TIMEOUT;
  289. }
  290. }
  291. }
  292. /* Set the new LSE configuration -----------------------------------------*/
  293. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  294. /* Check the LSE State */
  295. if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  296. {
  297. /* Get Start Tick */
  298. tickstart = HAL_GetTick();
  299. /* Wait till LSE is ready */
  300. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  301. {
  302. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  303. {
  304. return HAL_TIMEOUT;
  305. }
  306. }
  307. }
  308. else
  309. {
  310. /* Get Start Tick */
  311. tickstart = HAL_GetTick();
  312. /* Wait till LSE is disabled */
  313. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
  314. {
  315. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  316. {
  317. return HAL_TIMEOUT;
  318. }
  319. }
  320. }
  321. /* Require to disable power clock if necessary */
  322. if(pwrclkchanged == SET)
  323. {
  324. __HAL_RCC_PWR_CLK_DISABLE();
  325. }
  326. }
  327. /*-------------------------------- PLL Configuration -----------------------*/
  328. /* Check the parameters */
  329. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  330. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  331. {
  332. /* Check if the PLL is used as system clock or not */
  333. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  334. {
  335. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  336. {
  337. /* Check the parameters */
  338. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  339. assert_param(IS_RCC_PLL_MUL(RCC_OscInitStruct->PLL.PLLMUL));
  340. #if defined(RCC_CFGR_PLLSRC_HSI_PREDIV)
  341. assert_param(IS_RCC_PREDIV(RCC_OscInitStruct->PLL.PREDIV));
  342. #endif
  343. /* Disable the main PLL. */
  344. __HAL_RCC_PLL_DISABLE();
  345. /* Get Start Tick */
  346. tickstart = HAL_GetTick();
  347. /* Wait till PLL is disabled */
  348. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  349. {
  350. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  351. {
  352. return HAL_TIMEOUT;
  353. }
  354. }
  355. #if defined(RCC_CFGR_PLLSRC_HSI_PREDIV)
  356. /* Configure the main PLL clock source, predivider and multiplication factor. */
  357. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  358. RCC_OscInitStruct->PLL.PREDIV,
  359. RCC_OscInitStruct->PLL.PLLMUL);
  360. #else
  361. /* Configure the main PLL clock source and multiplication factor. */
  362. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  363. RCC_OscInitStruct->PLL.PLLMUL);
  364. #endif /* RCC_CFGR_PLLSRC_HSI_PREDIV */
  365. /* Enable the main PLL. */
  366. __HAL_RCC_PLL_ENABLE();
  367. /* Get Start Tick */
  368. tickstart = HAL_GetTick();
  369. /* Wait till PLL is ready */
  370. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  371. {
  372. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  373. {
  374. return HAL_TIMEOUT;
  375. }
  376. }
  377. }
  378. else
  379. {
  380. /* Disable the main PLL. */
  381. __HAL_RCC_PLL_DISABLE();
  382. /* Get Start Tick */
  383. tickstart = HAL_GetTick();
  384. /* Wait till PLL is disabled */
  385. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  386. {
  387. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  388. {
  389. return HAL_TIMEOUT;
  390. }
  391. }
  392. }
  393. }
  394. else
  395. {
  396. return HAL_ERROR;
  397. }
  398. }
  399. return HAL_OK;
  400. }
  401. /**
  402. * @brief Initializes the CPU, AHB and APB buses clocks according to the specified
  403. * parameters in the RCC_ClkInitStruct.
  404. * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that
  405. * contains the configuration information for the RCC peripheral.
  406. * @param FLatency FLASH Latency
  407. * The value of this parameter depend on device used within the same series
  408. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  409. * and updated by @ref HAL_RCC_GetHCLKFreq() function called within this function
  410. *
  411. * @note The HSI is used (enabled by hardware) as system clock source after
  412. * start-up from Reset, wake-up from STOP and STANDBY mode, or in case
  413. * of failure of the HSE used directly or indirectly as system clock
  414. * (if the Clock Security System CSS is enabled).
  415. *
  416. * @note A switch from one clock source to another occurs only if the target
  417. * clock source is ready (clock stable after start-up delay or PLL locked).
  418. * If a clock source which is not yet ready is selected, the switch will
  419. * occur when the clock source will be ready.
  420. * You can use @ref HAL_RCC_GetClockConfig() function to know which clock is
  421. * currently used as system clock source.
  422. * @retval HAL status
  423. */
  424. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  425. {
  426. uint32_t tickstart = 0U;
  427. /* Check the parameters */
  428. assert_param(RCC_ClkInitStruct != NULL);
  429. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  430. assert_param(IS_FLASH_LATENCY(FLatency));
  431. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  432. must be correctly programmed according to the frequency of the CPU clock
  433. (HCLK) of the device. */
  434. /* Increasing the number of wait states because of higher CPU frequency */
  435. if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
  436. {
  437. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  438. __HAL_FLASH_SET_LATENCY(FLatency);
  439. /* Check that the new number of wait states is taken into account to access the Flash
  440. memory by reading the FLASH_ACR register */
  441. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  442. {
  443. return HAL_ERROR;
  444. }
  445. }
  446. /*-------------------------- HCLK Configuration --------------------------*/
  447. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  448. {
  449. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  450. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  451. }
  452. /*------------------------- SYSCLK Configuration ---------------------------*/
  453. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  454. {
  455. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  456. /* HSE is selected as System Clock Source */
  457. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  458. {
  459. /* Check the HSE ready flag */
  460. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  461. {
  462. return HAL_ERROR;
  463. }
  464. }
  465. /* PLL is selected as System Clock Source */
  466. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  467. {
  468. /* Check the PLL ready flag */
  469. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  470. {
  471. return HAL_ERROR;
  472. }
  473. }
  474. /* HSI is selected as System Clock Source */
  475. else
  476. {
  477. /* Check the HSI ready flag */
  478. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  479. {
  480. return HAL_ERROR;
  481. }
  482. }
  483. __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource);
  484. /* Get Start Tick */
  485. tickstart = HAL_GetTick();
  486. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  487. {
  488. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)
  489. {
  490. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  491. {
  492. return HAL_TIMEOUT;
  493. }
  494. }
  495. }
  496. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  497. {
  498. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  499. {
  500. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  501. {
  502. return HAL_TIMEOUT;
  503. }
  504. }
  505. }
  506. else
  507. {
  508. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)
  509. {
  510. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  511. {
  512. return HAL_TIMEOUT;
  513. }
  514. }
  515. }
  516. }
  517. /* Decreasing the number of wait states because of lower CPU frequency */
  518. if(FLatency < (FLASH->ACR & FLASH_ACR_LATENCY))
  519. {
  520. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  521. __HAL_FLASH_SET_LATENCY(FLatency);
  522. /* Check that the new number of wait states is taken into account to access the Flash
  523. memory by reading the FLASH_ACR register */
  524. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  525. {
  526. return HAL_ERROR;
  527. }
  528. }
  529. /*-------------------------- PCLK1 Configuration ---------------------------*/
  530. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  531. {
  532. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  533. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  534. }
  535. /*-------------------------- PCLK2 Configuration ---------------------------*/
  536. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  537. {
  538. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  539. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
  540. }
  541. /* Update the SystemCoreClock global variable */
  542. //SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> RCC_CFGR_HPRE_BITNUMBER];
  543. /* Configure the source of time base considering new system clocks settings*/
  544. //HAL_InitTick (TICK_INT_PRIORITY);
  545. return HAL_OK;
  546. }
  547. /**
  548. * @brief Returns the PCLK2 frequency
  549. * @note Each time PCLK2 changes, this function must be called to update the
  550. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  551. * @retval PCLK2 frequency
  552. */
  553. uint32_t HAL_RCC_GetPCLK2Freq(void)
  554. {
  555. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  556. //return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2)>> POSITION_VAL(RCC_CFGR_PPRE2)]);
  557. return F_CPU;
  558. }
  559. #define GPIO_MODE (0x00000003U)
  560. #define EXTI_MODE (0x10000000U)
  561. #define GPIO_MODE_IT (0x00010000U)
  562. #define GPIO_MODE_EVT (0x00020000U)
  563. #define RISING_EDGE (0x00100000U)
  564. #define FALLING_EDGE (0x00200000U)
  565. #define GPIO_OUTPUT_TYPE (0x00000010U)
  566. #define GPIO_NUMBER (16U)
  567. #define __HAL_RCC_GPIOA_CLK_ENABLE() do { \
  568. __IO uint32_t tmpreg; \
  569. SET_BIT(RCC->AHBENR, RCC_AHBENR_GPIOAEN);\
  570. /* Delay after an RCC peripheral clock enabling */ \
  571. tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_GPIOAEN);\
  572. UNUSED(tmpreg); \
  573. } while(0U)
  574. /**
  575. * @brief Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
  576. * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F3 family devices
  577. * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
  578. * the configuration information for the specified GPIO peripheral.
  579. * @retval None
  580. */
  581. void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  582. {
  583. uint32_t position = 0x00U;
  584. uint32_t iocurrent = 0x00U;
  585. uint32_t temp = 0x00U;
  586. /* Check the parameters */
  587. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  588. assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
  589. assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
  590. assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
  591. /* Configure the port pins */
  592. while (((GPIO_Init->Pin) >> position) != RESET)
  593. {
  594. /* Get current io position */
  595. iocurrent = (GPIO_Init->Pin) & (1U << position);
  596. if(iocurrent)
  597. {
  598. /*--------------------- GPIO Mode Configuration ------------------------*/
  599. /* In case of Alternate function mode selection */
  600. if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
  601. {
  602. /* Check the Alternate function parameters */
  603. assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
  604. assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
  605. /* Configure Alternate function mapped with the current IO */
  606. temp = GPIOx->AFR[position >> 3];
  607. temp &= ~(0xFU << ((uint32_t)(position & 0x07U) * 4U)) ;
  608. temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & 0x07U) * 4U));
  609. GPIOx->AFR[position >> 3] = temp;
  610. }
  611. /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
  612. temp = GPIOx->MODER;
  613. temp &= ~(GPIO_MODER_MODER0 << (position * 2U));
  614. temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
  615. GPIOx->MODER = temp;
  616. /* In case of Output or Alternate function mode selection */
  617. if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
  618. (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
  619. {
  620. /* Check the Speed parameter */
  621. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  622. /* Configure the IO Speed */
  623. temp = GPIOx->OSPEEDR;
  624. temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2U));
  625. temp |= (GPIO_Init->Speed << (position * 2U));
  626. GPIOx->OSPEEDR = temp;
  627. /* Configure the IO Output Type */
  628. temp = GPIOx->OTYPER;
  629. temp &= ~(GPIO_OTYPER_OT_0 << position) ;
  630. temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4U) << position);
  631. GPIOx->OTYPER = temp;
  632. }
  633. /* Activate the Pull-up or Pull down resistor for the current IO */
  634. temp = GPIOx->PUPDR;
  635. temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U));
  636. temp |= ((GPIO_Init->Pull) << (position * 2U));
  637. GPIOx->PUPDR = temp;
  638. /*--------------------- EXTI Mode Configuration ------------------------*/
  639. /* Configure the External Interrupt or event for the current IO */
  640. if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
  641. {
  642. /* Enable SYSCFG Clock */
  643. __HAL_RCC_SYSCFG_CLK_ENABLE();
  644. temp = SYSCFG->EXTICR[position >> 2];
  645. temp &= ~((0x0FU) << (4U * (position & 0x03U)));
  646. temp |= (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U)));
  647. SYSCFG->EXTICR[position >> 2] = temp;
  648. /* Clear EXTI line configuration */
  649. temp = EXTI->IMR;
  650. temp &= ~((uint32_t)iocurrent);
  651. if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
  652. {
  653. temp |= iocurrent;
  654. }
  655. EXTI->IMR = temp;
  656. temp = EXTI->EMR;
  657. temp &= ~((uint32_t)iocurrent);
  658. if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
  659. {
  660. temp |= iocurrent;
  661. }
  662. EXTI->EMR = temp;
  663. /* Clear Rising Falling edge configuration */
  664. temp = EXTI->RTSR;
  665. temp &= ~((uint32_t)iocurrent);
  666. if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
  667. {
  668. temp |= iocurrent;
  669. }
  670. EXTI->RTSR = temp;
  671. temp = EXTI->FTSR;
  672. temp &= ~((uint32_t)iocurrent);
  673. if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
  674. {
  675. temp |= iocurrent;
  676. }
  677. EXTI->FTSR = temp;
  678. }
  679. }
  680. position++;
  681. }
  682. }
  683. /**
  684. * @brief Set or clear the selected data port bit.
  685. *
  686. * @note This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
  687. * accesses. In this way, there is no risk of an IRQ occurring between
  688. * the read and the modify access.
  689. *
  690. * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F3 family
  691. * @param GPIO_Pin: specifies the port bit to be written.
  692. * This parameter can be one of GPIO_PIN_x where x can be (0..15).
  693. * @param PinState: specifies the value to be written to the selected bit.
  694. * This parameter can be one of the GPIO_PinState enum values:
  695. * @arg GPIO_PIN_RESET: to clear the port pin
  696. * @arg GPIO_PIN_SET: to set the port pin
  697. * @retval None
  698. */
  699. void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  700. {
  701. /* Check the parameters */
  702. assert_param(IS_GPIO_PIN(GPIO_Pin));
  703. assert_param(IS_GPIO_PIN_ACTION(PinState));
  704. if(PinState != GPIO_PIN_RESET)
  705. {
  706. GPIOx->BSRR = (uint32_t)GPIO_Pin;
  707. }
  708. else
  709. {
  710. GPIOx->BRR = (uint32_t)GPIO_Pin;
  711. }
  712. }
  713. /**
  714. * @brief Configure the UART peripheral.
  715. * @param huart: UART handle.
  716. * @retval HAL status
  717. */
  718. HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
  719. {
  720. uint32_t tmpreg = 0x00000000U;
  721. UART_ClockSourceTypeDef clocksource = UART_CLOCKSOURCE_UNDEFINED;
  722. uint16_t brrtemp = 0x0000U;
  723. uint16_t usartdiv = 0x0000U;
  724. HAL_StatusTypeDef ret = HAL_OK;
  725. /* Check the parameters */
  726. assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
  727. assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  728. assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
  729. assert_param(IS_UART_PARITY(huart->Init.Parity));
  730. assert_param(IS_UART_MODE(huart->Init.Mode));
  731. assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
  732. assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
  733. assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  734. /*-------------------------- USART CR1 Configuration -----------------------*/
  735. /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
  736. * the UART Word Length, Parity, Mode and oversampling:
  737. * set the M bits according to huart->Init.WordLength value
  738. * set PCE and PS bits according to huart->Init.Parity value
  739. * set TE and RE bits according to huart->Init.Mode value
  740. * set OVER8 bit according to huart->Init.OverSampling value */
  741. tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
  742. MODIFY_REG(huart->Instance->CR1, UART_CR1_FIELDS, tmpreg);
  743. /*-------------------------- USART CR2 Configuration -----------------------*/
  744. /* Configure the UART Stop Bits: Set STOP[13:12] bits according
  745. * to huart->Init.StopBits value */
  746. MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
  747. /*-------------------------- USART CR3 Configuration -----------------------*/
  748. /* Configure
  749. * - UART HardWare Flow Control: set CTSE and RTSE bits according
  750. * to huart->Init.HwFlowCtl value
  751. * - one-bit sampling method versus three samples' majority rule according
  752. * to huart->Init.OneBitSampling */
  753. tmpreg = (uint32_t)huart->Init.HwFlowCtl | huart->Init.OneBitSampling ;
  754. MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT), tmpreg);
  755. /*-------------------------- USART BRR Configuration -----------------------*/
  756. UART_GETCLOCKSOURCE(huart, clocksource);
  757. /* Check UART Over Sampling to set Baud Rate Register */
  758. if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
  759. {
  760. switch (clocksource)
  761. {
  762. case UART_CLOCKSOURCE_PCLK1:
  763. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
  764. break;
  765. case UART_CLOCKSOURCE_PCLK2:
  766. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate));
  767. break;
  768. case UART_CLOCKSOURCE_HSI:
  769. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate));
  770. break;
  771. case UART_CLOCKSOURCE_SYSCLK:
  772. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
  773. break;
  774. case UART_CLOCKSOURCE_LSE:
  775. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(LSE_VALUE, huart->Init.BaudRate));
  776. break;
  777. case UART_CLOCKSOURCE_UNDEFINED:
  778. default:
  779. ret = HAL_ERROR;
  780. break;
  781. }
  782. brrtemp = usartdiv & 0xFFF0U;
  783. brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
  784. huart->Instance->BRR = brrtemp;
  785. }
  786. else
  787. {
  788. switch (clocksource)
  789. {
  790. case UART_CLOCKSOURCE_PCLK1:
  791. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
  792. break;
  793. case UART_CLOCKSOURCE_PCLK2:
  794. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate));
  795. break;
  796. case UART_CLOCKSOURCE_HSI:
  797. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate));
  798. break;
  799. case UART_CLOCKSOURCE_SYSCLK:
  800. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
  801. break;
  802. case UART_CLOCKSOURCE_LSE:
  803. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(LSE_VALUE, huart->Init.BaudRate));
  804. break;
  805. case UART_CLOCKSOURCE_UNDEFINED:
  806. default:
  807. ret = HAL_ERROR;
  808. break;
  809. }
  810. }
  811. return ret;
  812. }
  813. /**
  814. * @brief Check the UART Idle State.
  815. * @param huart UART handle.
  816. * @retval HAL status
  817. */
  818. HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
  819. {
  820. uint32_t tickstart = 0U;
  821. /* Initialize the UART ErrorCode */
  822. huart->ErrorCode = HAL_UART_ERROR_NONE;
  823. /* Init tickstart for timeout managment*/
  824. tickstart = HAL_GetTick();
  825. /* Check if the Transmitter is enabled */
  826. if((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  827. {
  828. /* Wait until TEACK flag is set */
  829. if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  830. {
  831. /* Timeout Occured */
  832. return HAL_TIMEOUT;
  833. }
  834. }
  835. /* Check if the Receiver is enabled */
  836. if((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  837. {
  838. /* Wait until REACK flag is set */
  839. if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  840. {
  841. /* Timeout Occured */
  842. return HAL_TIMEOUT;
  843. }
  844. }
  845. /* Initialize the UART State */
  846. huart->gState = HAL_UART_STATE_READY;
  847. huart->RxState = HAL_UART_STATE_READY;
  848. /* Process Unlocked */
  849. __HAL_UNLOCK(huart);
  850. return HAL_OK;
  851. }
  852. /**
  853. * @brief Initialize the UART mode according to the specified
  854. * parameters in the UART_InitTypeDef and initialize the associated handle.
  855. * @param huart: UART handle.
  856. * @retval HAL status
  857. */
  858. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  859. {
  860. /* Check the UART handle allocation */
  861. if(huart == NULL)
  862. {
  863. return HAL_ERROR;
  864. }
  865. if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
  866. {
  867. /* Check the parameters */
  868. assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
  869. }
  870. else
  871. {
  872. /* Check the parameters */
  873. assert_param(IS_UART_INSTANCE(huart->Instance));
  874. }
  875. if(huart->gState == HAL_UART_STATE_RESET)
  876. {
  877. /* Allocate lock resource and initialize it */
  878. huart->Lock = HAL_UNLOCKED;
  879. /* Init the low level hardware : GPIO, CLOCK */
  880. //HAL_UART_MspInit(huart);
  881. }
  882. huart->gState = HAL_UART_STATE_BUSY;
  883. /* Disable the Peripheral */
  884. __HAL_UART_DISABLE(huart);
  885. /* Set the UART Communication parameters */
  886. if (UART_SetConfig(huart) == HAL_ERROR)
  887. {
  888. return HAL_ERROR;
  889. }
  890. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  891. {
  892. //UART_AdvFeatureConfig(huart);
  893. }
  894. /* In asynchronous mode, the following bits must be kept cleared:
  895. - LINEN and CLKEN bits in the USART_CR2 register,
  896. - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
  897. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  898. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  899. /* Enable the Peripheral */
  900. __HAL_UART_ENABLE(huart);
  901. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  902. return UART_CheckIdleState(huart);
  903. }
  904. /**
  905. * @brief Handle UART Communication Timeout.
  906. * @param huart UART handle.
  907. * @param Flag Specifies the UART flag to check
  908. * @param Status Flag status (SET or RESET)
  909. * @param Tickstart Tick start value
  910. * @param Timeout Timeout duration
  911. * @retval HAL status
  912. */
  913. HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  914. {
  915. /* Wait until flag is set */
  916. while((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
  917. {
  918. /* Check for the Timeout */
  919. if(Timeout != HAL_MAX_DELAY)
  920. {
  921. if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout))
  922. {
  923. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  924. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  925. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  926. huart->gState = HAL_UART_STATE_READY;
  927. huart->RxState = HAL_UART_STATE_READY;
  928. /* Process Unlocked */
  929. __HAL_UNLOCK(huart);
  930. return HAL_TIMEOUT;
  931. }
  932. }
  933. }
  934. return HAL_OK;
  935. }
  936. /**
  937. * @brief Send an amount of data in blocking mode.
  938. * @param huart: UART handle.
  939. * @param pData: Pointer to data buffer.
  940. * @param Size: Amount of data to be sent.
  941. * @param Timeout: Timeout duration.
  942. * @retval HAL status
  943. */
  944. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  945. {
  946. uint16_t* tmp;
  947. uint32_t tickstart = 0U;
  948. /* Check that a Tx process is not already ongoing */
  949. if(huart->gState == HAL_UART_STATE_READY)
  950. {
  951. if((pData == NULL ) || (Size == 0U))
  952. {
  953. return HAL_ERROR;
  954. }
  955. /* Process Locked */
  956. __HAL_LOCK(huart);
  957. huart->ErrorCode = HAL_UART_ERROR_NONE;
  958. huart->gState = HAL_UART_STATE_BUSY_TX;
  959. /* Init tickstart for timeout managment*/
  960. tickstart = HAL_GetTick();
  961. huart->TxXferSize = Size;
  962. huart->TxXferCount = Size;
  963. while(huart->TxXferCount > 0U)
  964. {
  965. huart->TxXferCount--;
  966. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  967. {
  968. return HAL_TIMEOUT;
  969. }
  970. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  971. {
  972. tmp = (uint16_t*) pData;
  973. huart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
  974. pData += 2U;
  975. }
  976. else
  977. {
  978. huart->Instance->TDR = (*pData++ & (uint8_t)0xFFU);
  979. }
  980. }
  981. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  982. {
  983. return HAL_TIMEOUT;
  984. }
  985. /* At end of Tx process, restore huart->gState to Ready */
  986. huart->gState = HAL_UART_STATE_READY;
  987. /* Process Unlocked */
  988. __HAL_UNLOCK(huart);
  989. return HAL_OK;
  990. }
  991. else
  992. {
  993. return HAL_BUSY;
  994. }
  995. }
  996. /**
  997. * @brief Receive an amount of data in blocking mode.
  998. * @param huart: UART handle.
  999. * @param pData: pointer to data buffer.
  1000. * @param Size: amount of data to be received.
  1001. * @param Timeout: Timeout duration.
  1002. * @retval HAL status
  1003. */
  1004. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1005. {
  1006. uint16_t* tmp;
  1007. uint16_t uhMask;
  1008. uint32_t tickstart = 0U;
  1009. /* Check that a Rx process is not already ongoing */
  1010. if(huart->RxState == HAL_UART_STATE_READY)
  1011. {
  1012. if((pData == NULL ) || (Size == 0U))
  1013. {
  1014. return HAL_ERROR;
  1015. }
  1016. /* Process Locked */
  1017. __HAL_LOCK(huart);
  1018. huart->Instance->ICR = 0xFFFFFFFF;
  1019. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1020. huart->RxState = HAL_UART_STATE_BUSY_RX;
  1021. /* Init tickstart for timeout managment*/
  1022. tickstart = HAL_GetTick();
  1023. huart->RxXferSize = Size;
  1024. huart->RxXferCount = Size;
  1025. /* Computation of UART mask to apply to RDR register */
  1026. UART_MASK_COMPUTATION(huart);
  1027. uhMask = huart->Mask;
  1028. /* as long as data have to be received */
  1029. while(huart->RxXferCount > 0U)
  1030. {
  1031. huart->RxXferCount--;
  1032. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  1033. {
  1034. return HAL_TIMEOUT;
  1035. }
  1036. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1037. {
  1038. tmp = (uint16_t*) pData ;
  1039. *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
  1040. pData +=2U;
  1041. }
  1042. else
  1043. {
  1044. *pData++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  1045. }
  1046. }
  1047. /* At end of Rx process, restore huart->RxState to Ready */
  1048. huart->RxState = HAL_UART_STATE_READY;
  1049. /* Process Unlocked */
  1050. __HAL_UNLOCK(huart);
  1051. return HAL_OK;
  1052. }
  1053. else
  1054. {
  1055. return HAL_BUSY;
  1056. }
  1057. }