stm32f2_hal_lowlevel.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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 "stm32f2_hal.h"
  35. #include "stm32f2_hal_lowlevel.h"
  36. #include "stm32f2xx_hal_rcc.h"
  37. #include "stm32f2xx_hal_gpio.h"
  38. #include "stm32f2xx_hal_dma.h"
  39. #include "stm32f2xx_hal_uart.h"
  40. #include "stm32f2xx_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 RCC_CFGR_HPRE_BITNUMBER POSITION_VAL(RCC_CFGR_HPRE)
  48. #define CLOCKSWITCH_TIMEOUT_VALUE ((uint32_t)5000U) /* 5 s */
  49. const uint8_t APBAHBPrescTable[16] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
  50. #define UART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  51. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
  52. uint32_t HAL_RCC_GetSysClockFreq(void)
  53. {
  54. return 7372800U;
  55. }
  56. uint32_t HAL_RCC_GetPCLK1Freq(void)
  57. {
  58. return 7372800U;
  59. }
  60. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  61. {
  62. uint32_t tickstart = 0U;
  63. /* Check the parameters */
  64. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  65. /*------------------------------- HSE Configuration ------------------------*/
  66. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  67. {
  68. /* Check the parameters */
  69. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  70. /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
  71. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) ||\
  72. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)))
  73. {
  74. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  75. {
  76. return HAL_ERROR;
  77. }
  78. }
  79. else
  80. {
  81. /* Set the new HSE configuration ---------------------------------------*/
  82. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  83. /* Check the HSE State */
  84. if((RCC_OscInitStruct->HSEState) != RCC_HSE_OFF)
  85. {
  86. /* Get Start Tick*/
  87. tickstart = HAL_GetTick();
  88. /* Wait till HSE is ready */
  89. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  90. {
  91. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  92. {
  93. return HAL_TIMEOUT;
  94. }
  95. }
  96. }
  97. else
  98. {
  99. /* Get Start Tick*/
  100. tickstart = HAL_GetTick();
  101. /* Wait till HSE is bypassed or disabled */
  102. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
  103. {
  104. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  105. {
  106. return HAL_TIMEOUT;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. /*----------------------------- HSI Configuration --------------------------*/
  113. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  114. {
  115. /* Check the parameters */
  116. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  117. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  118. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  119. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) ||\
  120. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)))
  121. {
  122. /* When HSI is used as system clock it will not disabled */
  123. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
  124. {
  125. return HAL_ERROR;
  126. }
  127. /* Otherwise, just the calibration is allowed */
  128. else
  129. {
  130. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  131. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  132. }
  133. }
  134. else
  135. {
  136. /* Check the HSI State */
  137. if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
  138. {
  139. /* Enable the Internal High Speed oscillator (HSI). */
  140. __HAL_RCC_HSI_ENABLE();
  141. /* Get Start Tick*/
  142. tickstart = HAL_GetTick();
  143. /* Wait till HSI is ready */
  144. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  145. {
  146. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  147. {
  148. return HAL_TIMEOUT;
  149. }
  150. }
  151. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  152. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  153. }
  154. else
  155. {
  156. /* Disable the Internal High Speed oscillator (HSI). */
  157. __HAL_RCC_HSI_DISABLE();
  158. /* Get Start Tick*/
  159. tickstart = HAL_GetTick();
  160. /* Wait till HSI is ready */
  161. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
  162. {
  163. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  164. {
  165. return HAL_TIMEOUT;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. /*------------------------------ LSI Configuration -------------------------*/
  172. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  173. {
  174. /* Check the parameters */
  175. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  176. /* Check the LSI State */
  177. if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
  178. {
  179. /* Enable the Internal Low Speed oscillator (LSI). */
  180. __HAL_RCC_LSI_ENABLE();
  181. /* Get Start Tick*/
  182. tickstart = HAL_GetTick();
  183. /* Wait till LSI is ready */
  184. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
  185. {
  186. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  187. {
  188. return HAL_TIMEOUT;
  189. }
  190. }
  191. }
  192. else
  193. {
  194. /* Disable the Internal Low Speed oscillator (LSI). */
  195. __HAL_RCC_LSI_DISABLE();
  196. /* Get Start Tick*/
  197. tickstart = HAL_GetTick();
  198. /* Wait till LSI is ready */
  199. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
  200. {
  201. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  202. {
  203. return HAL_TIMEOUT;
  204. }
  205. }
  206. }
  207. }
  208. /*------------------------------ LSE Configuration -------------------------*/
  209. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  210. {
  211. /* Check the parameters */
  212. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  213. /* Enable Power Clock*/
  214. __HAL_RCC_PWR_CLK_ENABLE();
  215. /* Enable write access to Backup domain */
  216. PWR->CR |= PWR_CR_DBP;
  217. /* Wait for Backup domain Write protection enable */
  218. tickstart = HAL_GetTick();
  219. while((PWR->CR & PWR_CR_DBP) == RESET)
  220. {
  221. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  222. {
  223. return HAL_TIMEOUT;
  224. }
  225. }
  226. /* Set the new LSE configuration -----------------------------------------*/
  227. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  228. /* Check the LSE State */
  229. if((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF)
  230. {
  231. /* Get Start Tick*/
  232. tickstart = HAL_GetTick();
  233. /* Wait till LSE is ready */
  234. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  235. {
  236. if((HAL_GetTick() - tickstart ) > LSE_TIMEOUT_VALUE)
  237. {
  238. return HAL_TIMEOUT;
  239. }
  240. }
  241. }
  242. else
  243. {
  244. /* Get Start Tick*/
  245. tickstart = HAL_GetTick();
  246. /* Wait till LSE is ready */
  247. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
  248. {
  249. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  250. {
  251. return HAL_TIMEOUT;
  252. }
  253. }
  254. }
  255. }
  256. /*-------------------------------- PLL Configuration -----------------------*/
  257. /* Check the parameters */
  258. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  259. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  260. {
  261. /* Check if the PLL is used as system clock or not */
  262. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  263. {
  264. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  265. {
  266. /* Check the parameters */
  267. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  268. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  269. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  270. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  271. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  272. /* Disable the main PLL. */
  273. __HAL_RCC_PLL_DISABLE();
  274. /* Get Start Tick*/
  275. tickstart = HAL_GetTick();
  276. /* Wait till PLL is ready */
  277. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  278. {
  279. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  280. {
  281. return HAL_TIMEOUT;
  282. }
  283. }
  284. /* Configure the main PLL clock source, multiplication and division factors. */
  285. WRITE_REG(RCC->PLLCFGR, (RCC_OscInitStruct->PLL.PLLSource | \
  286. RCC_OscInitStruct->PLL.PLLM | \
  287. (RCC_OscInitStruct->PLL.PLLN << POSITION_VAL(RCC_PLLCFGR_PLLN)) | \
  288. (((RCC_OscInitStruct->PLL.PLLP >> 1U) - 1U) << POSITION_VAL(RCC_PLLCFGR_PLLP)) | \
  289. (RCC_OscInitStruct->PLL.PLLQ << POSITION_VAL(RCC_PLLCFGR_PLLQ))));
  290. /* Enable the main PLL. */
  291. __HAL_RCC_PLL_ENABLE();
  292. /* Get Start Tick*/
  293. tickstart = HAL_GetTick();
  294. /* Wait till PLL is ready */
  295. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  296. {
  297. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  298. {
  299. return HAL_TIMEOUT;
  300. }
  301. }
  302. }
  303. else
  304. {
  305. /* Disable the main PLL. */
  306. __HAL_RCC_PLL_DISABLE();
  307. /* Get Start Tick*/
  308. tickstart = HAL_GetTick();
  309. /* Wait till PLL is ready */
  310. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  311. {
  312. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  313. {
  314. return HAL_TIMEOUT;
  315. }
  316. }
  317. }
  318. }
  319. else
  320. {
  321. return HAL_ERROR;
  322. }
  323. }
  324. return HAL_OK;
  325. }
  326. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  327. {
  328. uint32_t tickstart = 0U;
  329. /* Check the parameters */
  330. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  331. assert_param(IS_FLASH_LATENCY(FLatency));
  332. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  333. must be correctly programmed according to the frequency of the CPU clock
  334. (HCLK) and the supply voltage of the device. */
  335. /* Increasing the number of wait states because of higher CPU frequency */
  336. if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
  337. {
  338. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  339. __HAL_FLASH_SET_LATENCY(FLatency);
  340. /* Check that the new number of wait states is taken into account to access the Flash
  341. memory by reading the FLASH_ACR register */
  342. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  343. {
  344. return HAL_ERROR;
  345. }
  346. }
  347. /*-------------------------- HCLK Configuration --------------------------*/
  348. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  349. {
  350. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  351. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  352. }
  353. /*------------------------- SYSCLK Configuration ---------------------------*/
  354. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  355. {
  356. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  357. /* HSE is selected as System Clock Source */
  358. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  359. {
  360. /* Check the HSE ready flag */
  361. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  362. {
  363. return HAL_ERROR;
  364. }
  365. }
  366. /* PLL is selected as System Clock Source */
  367. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  368. {
  369. /* Check the PLL ready flag */
  370. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  371. {
  372. return HAL_ERROR;
  373. }
  374. }
  375. /* HSI is selected as System Clock Source */
  376. else
  377. {
  378. /* Check the HSI ready flag */
  379. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  380. {
  381. return HAL_ERROR;
  382. }
  383. }
  384. __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource);
  385. /* Get Start Tick*/
  386. tickstart = HAL_GetTick();
  387. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  388. {
  389. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)
  390. {
  391. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  392. {
  393. return HAL_TIMEOUT;
  394. }
  395. }
  396. }
  397. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  398. {
  399. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  400. {
  401. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  402. {
  403. return HAL_TIMEOUT;
  404. }
  405. }
  406. }
  407. else
  408. {
  409. while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)
  410. {
  411. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  412. {
  413. return HAL_TIMEOUT;
  414. }
  415. }
  416. }
  417. }
  418. /* Decreasing the number of wait states because of lower CPU frequency */
  419. if(FLatency < (FLASH->ACR & FLASH_ACR_LATENCY))
  420. {
  421. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  422. __HAL_FLASH_SET_LATENCY(FLatency);
  423. /* Check that the new number of wait states is taken into account to access the Flash
  424. memory by reading the FLASH_ACR register */
  425. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  426. {
  427. return HAL_ERROR;
  428. }
  429. }
  430. /*-------------------------- PCLK1 Configuration ---------------------------*/
  431. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  432. {
  433. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  434. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  435. }
  436. /*-------------------------- PCLK2 Configuration ---------------------------*/
  437. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  438. {
  439. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  440. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
  441. }
  442. /* Update the SystemCoreClock global variable */
  443. //SystemCoreClock = HAL_RCC_GetSysClockFreq() >> APBAHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> POSITION_VAL(RCC_CFGR_HPRE)];
  444. /* Configure the source of time base considering new system clocks settings*/
  445. //HAL_InitTick (TICK_INT_PRIORITY);
  446. return HAL_OK;
  447. }
  448. /**
  449. * @brief Returns the PCLK2 frequency
  450. * @note Each time PCLK2 changes, this function must be called to update the
  451. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  452. * @retval PCLK2 frequency
  453. */
  454. uint32_t HAL_RCC_GetPCLK2Freq(void)
  455. {
  456. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  457. //return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2)>> POSITION_VAL(RCC_CFGR_PPRE2)]);
  458. return 7372800;
  459. }
  460. #define GPIO_MODE ((uint32_t)0x00000003U)
  461. #define EXTI_MODE ((uint32_t)0x10000000U)
  462. #define GPIO_MODE_IT ((uint32_t)0x00010000U)
  463. #define GPIO_MODE_EVT ((uint32_t)0x00020000U)
  464. #define RISING_EDGE ((uint32_t)0x00100000U)
  465. #define FALLING_EDGE ((uint32_t)0x00200000U)
  466. #define GPIO_OUTPUT_TYPE ((uint32_t)0x00000010U)
  467. #define GPIO_NUMBER ((uint32_t)16U)
  468. #define __HAL_RCC_GPIOA_CLK_ENABLE() do { \
  469. __IO uint32_t tmpreg = 0x00U; \
  470. SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOAEN);\
  471. /* Delay after an RCC peripheral clock enabling */ \
  472. tmpreg = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOAEN);\
  473. UNUSED(tmpreg); \
  474. } while(0)
  475. void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  476. {
  477. uint32_t position;
  478. uint32_t ioposition = 0x00U;
  479. uint32_t iocurrent = 0x00U;
  480. uint32_t temp = 0x00U;
  481. /* Check the parameters */
  482. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  483. assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
  484. assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
  485. assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
  486. /* Configure the port pins */
  487. for(position = 0U; position < GPIO_NUMBER; position++)
  488. {
  489. /* Get the IO position */
  490. ioposition = ((uint32_t)0x01U) << position;
  491. /* Get the current IO position */
  492. iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
  493. if(iocurrent == ioposition)
  494. {
  495. /*--------------------- GPIO Mode Configuration ------------------------*/
  496. /* In case of Alternate function mode selection */
  497. if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
  498. {
  499. /* Check the Alternate function parameter */
  500. assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
  501. /* Configure Alternate function mapped with the current IO */
  502. temp = GPIOx->AFR[position >> 3U];
  503. temp &= ~((uint32_t)0xFU << ((uint32_t)(position & (uint32_t)0x07U) * 4U)) ;
  504. temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07U) * 4U));
  505. GPIOx->AFR[position >> 3U] = temp;
  506. }
  507. /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
  508. temp = GPIOx->MODER;
  509. temp &= ~(GPIO_MODER_MODER0 << (position * 2U));
  510. temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
  511. GPIOx->MODER = temp;
  512. /* In case of Output or Alternate function mode selection */
  513. if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
  514. (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
  515. {
  516. /* Check the Speed parameter */
  517. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  518. /* Configure the IO Speed */
  519. temp = GPIOx->OSPEEDR;
  520. temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2U));
  521. temp |= (GPIO_Init->Speed << (position * 2U));
  522. GPIOx->OSPEEDR = temp;
  523. /* Configure the IO Output Type */
  524. temp = GPIOx->OTYPER;
  525. temp &= ~(GPIO_OTYPER_OT_0 << position) ;
  526. temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4U) << position);
  527. GPIOx->OTYPER = temp;
  528. }
  529. /* Activate the Pull-up or Pull down resistor for the current IO */
  530. temp = GPIOx->PUPDR;
  531. temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U));
  532. temp |= ((GPIO_Init->Pull) << (position * 2U));
  533. GPIOx->PUPDR = temp;
  534. /*--------------------- EXTI Mode Configuration ------------------------*/
  535. /* Configure the External Interrupt or event for the current IO */
  536. if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
  537. {
  538. /* Enable SYSCFG Clock */
  539. __HAL_RCC_SYSCFG_CLK_ENABLE();
  540. temp = SYSCFG->EXTICR[position >> 2U];
  541. temp &= ~(((uint32_t)0x0FU) << (4U * (position & 0x03)));
  542. temp |= ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4U * (position & 0x03U)));
  543. SYSCFG->EXTICR[position >> 2U] = temp;
  544. /* Clear EXTI line configuration */
  545. temp = EXTI->IMR;
  546. temp &= ~((uint32_t)iocurrent);
  547. if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
  548. {
  549. temp |= iocurrent;
  550. }
  551. EXTI->IMR = temp;
  552. temp = EXTI->EMR;
  553. temp &= ~((uint32_t)iocurrent);
  554. if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
  555. {
  556. temp |= iocurrent;
  557. }
  558. EXTI->EMR = temp;
  559. /* Clear Rising Falling edge configuration */
  560. temp = EXTI->RTSR;
  561. temp &= ~((uint32_t)iocurrent);
  562. if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
  563. {
  564. temp |= iocurrent;
  565. }
  566. EXTI->RTSR = temp;
  567. temp = EXTI->FTSR;
  568. temp &= ~((uint32_t)iocurrent);
  569. if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
  570. {
  571. temp |= iocurrent;
  572. }
  573. EXTI->FTSR = temp;
  574. }
  575. }
  576. }
  577. }
  578. void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  579. {
  580. /* Check the parameters */
  581. assert_param(IS_GPIO_PIN(GPIO_Pin));
  582. assert_param(IS_GPIO_PIN_ACTION(PinState));
  583. if(PinState != GPIO_PIN_RESET)
  584. {
  585. GPIOx->BSRR = GPIO_Pin;
  586. }
  587. else
  588. {
  589. GPIOx->BSRR = (uint32_t)GPIO_Pin << 16U;
  590. }
  591. }
  592. static void UART_SetConfig(UART_HandleTypeDef *huart)
  593. {
  594. uint32_t tmpreg = 0x00U;
  595. /* Check the parameters */
  596. assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
  597. assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
  598. assert_param(IS_UART_PARITY(huart->Init.Parity));
  599. assert_param(IS_UART_MODE(huart->Init.Mode));
  600. /*-------------------------- USART CR2 Configuration -----------------------*/
  601. tmpreg = huart->Instance->CR2;
  602. /* Clear STOP[13:12] bits */
  603. tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
  604. /* Configure the UART Stop Bits: Set STOP[13:12] bits according to huart->Init.StopBits value */
  605. tmpreg |= (uint32_t)huart->Init.StopBits;
  606. /* Write to USART CR2 */
  607. WRITE_REG(huart->Instance->CR2, (uint32_t)tmpreg);
  608. /*-------------------------- USART CR1 Configuration -----------------------*/
  609. tmpreg = huart->Instance->CR1;
  610. /* Clear M, PCE, PS, TE and RE bits */
  611. tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
  612. USART_CR1_RE | USART_CR1_OVER8));
  613. /* Configure the UART Word Length, Parity and mode:
  614. Set the M bits according to huart->Init.WordLength value
  615. Set PCE and PS bits according to huart->Init.Parity value
  616. Set TE and RE bits according to huart->Init.Mode value
  617. Set OVER8 bit according to huart->Init.OverSampling value */
  618. tmpreg |= (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling;
  619. /* Write to USART CR1 */
  620. WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg);
  621. /*-------------------------- USART CR3 Configuration -----------------------*/
  622. tmpreg = huart->Instance->CR3;
  623. /* Clear CTSE and RTSE bits */
  624. tmpreg &= (uint32_t)~((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE));
  625. /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */
  626. tmpreg |= huart->Init.HwFlowCtl;
  627. /* Write to USART CR3 */
  628. WRITE_REG(huart->Instance->CR3, (uint32_t)tmpreg);
  629. /* Check the Over Sampling */
  630. if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
  631. {
  632. /*-------------------------- USART BRR Configuration ---------------------*/
  633. if((huart->Instance == USART1) || (huart->Instance == USART6))
  634. {
  635. huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
  636. }
  637. else
  638. {
  639. huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
  640. }
  641. }
  642. else
  643. {
  644. /*-------------------------- USART BRR Configuration ---------------------*/
  645. if((huart->Instance == USART1) || (huart->Instance == USART6))
  646. {
  647. huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
  648. }
  649. else
  650. {
  651. huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
  652. }
  653. }
  654. }
  655. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  656. {
  657. /* Check the UART handle allocation */
  658. if(huart == NULL)
  659. {
  660. return HAL_ERROR;
  661. }
  662. /* Check the parameters */
  663. if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
  664. {
  665. /* The hardware flow control is available only for USART1, USART2, USART3 and USART6 */
  666. assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
  667. assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
  668. }
  669. else
  670. {
  671. assert_param(IS_UART_INSTANCE(huart->Instance));
  672. }
  673. assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  674. assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  675. if(huart->gState == HAL_UART_STATE_RESET)
  676. {
  677. /* Allocate lock resource and initialize it */
  678. huart->Lock = HAL_UNLOCKED;
  679. /* Init the low level hardware */
  680. //HAL_UART_MspInit(huart);
  681. }
  682. huart->gState = HAL_UART_STATE_BUSY;
  683. /* Disable the peripheral */
  684. __HAL_UART_DISABLE(huart);
  685. /* Set the UART Communication parameters */
  686. UART_SetConfig(huart);
  687. /* In asynchronous mode, the following bits must be kept cleared:
  688. - LINEN and CLKEN bits in the USART_CR2 register,
  689. - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
  690. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  691. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  692. /* Enable the peripheral */
  693. __HAL_UART_ENABLE(huart);
  694. /* Initialize the UART state */
  695. huart->ErrorCode = HAL_UART_ERROR_NONE;
  696. huart->gState= HAL_UART_STATE_READY;
  697. huart->RxState= HAL_UART_STATE_READY;
  698. return HAL_OK;
  699. }
  700. static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  701. {
  702. /* Wait until flag is set */
  703. while((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
  704. {
  705. /* Check for the Timeout */
  706. if(Timeout != HAL_MAX_DELAY)
  707. {
  708. if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
  709. {
  710. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  711. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  712. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  713. huart->gState = HAL_UART_STATE_READY;
  714. huart->RxState = HAL_UART_STATE_READY;
  715. /* Process Unlocked */
  716. __HAL_UNLOCK(huart);
  717. return HAL_TIMEOUT;
  718. }
  719. }
  720. }
  721. return HAL_OK;
  722. }
  723. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  724. {
  725. uint16_t* tmp;
  726. uint32_t tickstart = 0U;
  727. /* Check that a Tx process is not already ongoing */
  728. if(huart->gState == HAL_UART_STATE_READY)
  729. {
  730. if((pData == NULL ) || (Size == 0U))
  731. {
  732. return HAL_ERROR;
  733. }
  734. /* Process Locked */
  735. __HAL_LOCK(huart);
  736. huart->ErrorCode = HAL_UART_ERROR_NONE;
  737. huart->gState = HAL_UART_STATE_BUSY_TX;
  738. /* Init tickstart for timeout managment */
  739. tickstart = HAL_GetTick();
  740. huart->TxXferSize = Size;
  741. huart->TxXferCount = Size;
  742. while(huart->TxXferCount > 0U)
  743. {
  744. huart->TxXferCount--;
  745. if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  746. {
  747. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  748. {
  749. return HAL_TIMEOUT;
  750. }
  751. tmp = (uint16_t*) pData;
  752. huart->Instance->DR = (*tmp & (uint16_t)0x01FFU);
  753. if(huart->Init.Parity == UART_PARITY_NONE)
  754. {
  755. pData +=2;
  756. }
  757. else
  758. {
  759. pData +=1;
  760. }
  761. }
  762. else
  763. {
  764. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  765. {
  766. return HAL_TIMEOUT;
  767. }
  768. huart->Instance->DR = (*pData++ & (uint8_t)0xFFU);
  769. }
  770. }
  771. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  772. {
  773. return HAL_TIMEOUT;
  774. }
  775. /* At end of Tx process, restore huart->gState to Ready */
  776. huart->gState = HAL_UART_STATE_READY;
  777. /* Process Unlocked */
  778. __HAL_UNLOCK(huart);
  779. return HAL_OK;
  780. }
  781. else
  782. {
  783. return HAL_BUSY;
  784. }
  785. }
  786. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  787. {
  788. uint16_t* tmp;
  789. uint32_t tickstart = 0U;
  790. /* Check that a Rx process is not already ongoing */
  791. if(huart->RxState == HAL_UART_STATE_READY)
  792. {
  793. if((pData == NULL ) || (Size == 0U))
  794. {
  795. return HAL_ERROR;
  796. }
  797. /* Process Locked */
  798. __HAL_LOCK(huart);
  799. huart->ErrorCode = HAL_UART_ERROR_NONE;
  800. huart->RxState = HAL_UART_STATE_BUSY_RX;
  801. /* Init tickstart for timeout managment */
  802. tickstart = HAL_GetTick();
  803. huart->RxXferSize = Size;
  804. huart->RxXferCount = Size;
  805. /* Check the remain data to be received */
  806. while(huart->RxXferCount > 0U)
  807. {
  808. huart->RxXferCount--;
  809. if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  810. {
  811. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  812. {
  813. return HAL_TIMEOUT;
  814. }
  815. tmp = (uint16_t*) pData ;
  816. if(huart->Init.Parity == UART_PARITY_NONE)
  817. {
  818. *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FFU);
  819. pData +=2;
  820. }
  821. else
  822. {
  823. *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FFU);
  824. pData +=1;
  825. }
  826. }
  827. else
  828. {
  829. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  830. {
  831. return HAL_TIMEOUT;
  832. }
  833. if(huart->Init.Parity == UART_PARITY_NONE)
  834. {
  835. *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FFU);
  836. }
  837. else
  838. {
  839. *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007FU);
  840. }
  841. }
  842. }
  843. /* At end of Rx process, restore huart->RxState to Ready */
  844. huart->RxState = HAL_UART_STATE_READY;
  845. /* Process Unlocked */
  846. __HAL_UNLOCK(huart);
  847. return HAL_OK;
  848. }
  849. else
  850. {
  851. return HAL_BUSY;
  852. }
  853. }