stm32f4_hal_lowlevel.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  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 "stm32f4_hal.h"
  35. #include "stm32f4_hal_lowlevel.h"
  36. #include "stm32f4xx_hal_rcc.h"
  37. #include "stm32f4xx_hal_gpio.h"
  38. #include "stm32f4xx_hal_dma.h"
  39. #include "stm32f4xx_hal_uart.h"
  40. #include "stm32f4xx_hal_flash.h"
  41. #include "stm32f4xx_hal_cryp.h"
  42. #define assert_param(expr) ((void)0U)
  43. #define CLOCKSWITCH_TIMEOUT_VALUE 5000U /* 5 s */
  44. uint32_t HAL_GetTick(void)
  45. {
  46. static uint32_t tick;
  47. return tick++;;
  48. }
  49. /**
  50. * @brief Enables or disables the AHB2 peripheral clock.
  51. * @note After reset, the peripheral clock (used for registers read/write access)
  52. * is disabled and the application software has to enable this clock before
  53. * using it.
  54. * @param RCC_AHBPeriph: specifies the AHB2 peripheral to gates its clock.
  55. * This parameter can be any combination of the following values:
  56. * @arg RCC_AHB2Periph_DCMI: DCMI clock
  57. * @arg RCC_AHB2Periph_CRYP: CRYP clock
  58. * @arg RCC_AHB2Periph_HASH: HASH clock
  59. * @arg RCC_AHB2Periph_RNG: RNG clock
  60. * @arg RCC_AHB2Periph_OTG_FS: USB OTG FS clock
  61. * @param NewState: new state of the specified peripheral clock.
  62. * This parameter can be: ENABLE or DISABLE.
  63. * @retval None
  64. */
  65. void RCC_AHB2PeriphClockCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState)
  66. {
  67. /* Check the parameters */
  68. assert_param(IS_RCC_AHB2_PERIPH(RCC_AHB2Periph));
  69. assert_param(IS_FUNCTIONAL_STATE(NewState));
  70. if (NewState != DISABLE)
  71. {
  72. RCC->AHB2ENR |= RCC_AHB2Periph;
  73. }
  74. else
  75. {
  76. RCC->AHB2ENR &= ~RCC_AHB2Periph;
  77. }
  78. }
  79. /**
  80. * @brief Initializes the RCC Oscillators according to the specified parameters in the
  81. * RCC_OscInitTypeDef.
  82. * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
  83. * contains the configuration information for the RCC Oscillators.
  84. * @note The PLL is not disabled when used as system clock.
  85. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  86. * supported by this API. User should request a transition to LSE Off
  87. * first and then LSE On or LSE Bypass.
  88. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  89. * supported by this API. User should request a transition to HSE Off
  90. * first and then HSE On or HSE Bypass.
  91. * @retval HAL status
  92. */
  93. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  94. {
  95. uint32_t tickstart = 0U;
  96. /* Check the parameters */
  97. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  98. /*------------------------------- HSE Configuration ------------------------*/
  99. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  100. {
  101. /* Check the parameters */
  102. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  103. /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
  104. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) ||\
  105. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)))
  106. {
  107. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  108. {
  109. return HAL_ERROR;
  110. }
  111. }
  112. else
  113. {
  114. /* Set the new HSE configuration ---------------------------------------*/
  115. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  116. /* Check the HSE State */
  117. if((RCC_OscInitStruct->HSEState) != RCC_HSE_OFF)
  118. {
  119. /* Get Start Tick*/
  120. tickstart = HAL_GetTick();
  121. /* Wait till HSE is ready */
  122. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  123. {
  124. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  125. {
  126. return HAL_TIMEOUT;
  127. }
  128. }
  129. }
  130. else
  131. {
  132. /* Get Start Tick*/
  133. tickstart = HAL_GetTick();
  134. /* Wait till HSE is bypassed or disabled */
  135. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
  136. {
  137. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  138. {
  139. return HAL_TIMEOUT;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. /*----------------------------- HSI Configuration --------------------------*/
  146. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  147. {
  148. /* Check the parameters */
  149. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  150. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  151. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  152. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) ||\
  153. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)))
  154. {
  155. /* When HSI is used as system clock it will not disabled */
  156. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
  157. {
  158. return HAL_ERROR;
  159. }
  160. /* Otherwise, just the calibration is allowed */
  161. else
  162. {
  163. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  164. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  165. }
  166. }
  167. else
  168. {
  169. /* Check the HSI State */
  170. if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
  171. {
  172. /* Enable the Internal High Speed oscillator (HSI). */
  173. __HAL_RCC_HSI_ENABLE();
  174. /* Get Start Tick*/
  175. tickstart = HAL_GetTick();
  176. /* Wait till HSI is ready */
  177. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  178. {
  179. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  180. {
  181. return HAL_TIMEOUT;
  182. }
  183. }
  184. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  185. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  186. }
  187. else
  188. {
  189. /* Disable the Internal High Speed oscillator (HSI). */
  190. __HAL_RCC_HSI_DISABLE();
  191. /* Get Start Tick*/
  192. tickstart = HAL_GetTick();
  193. /* Wait till HSI is ready */
  194. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
  195. {
  196. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  197. {
  198. return HAL_TIMEOUT;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. /*------------------------------ LSI Configuration -------------------------*/
  205. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  206. {
  207. /* Check the parameters */
  208. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  209. /* Check the LSI State */
  210. if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
  211. {
  212. /* Enable the Internal Low Speed oscillator (LSI). */
  213. __HAL_RCC_LSI_ENABLE();
  214. /* Get Start Tick*/
  215. tickstart = HAL_GetTick();
  216. /* Wait till LSI is ready */
  217. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
  218. {
  219. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  220. {
  221. return HAL_TIMEOUT;
  222. }
  223. }
  224. }
  225. else
  226. {
  227. /* Disable the Internal Low Speed oscillator (LSI). */
  228. __HAL_RCC_LSI_DISABLE();
  229. /* Get Start Tick*/
  230. tickstart = HAL_GetTick();
  231. /* Wait till LSI is ready */
  232. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
  233. {
  234. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  235. {
  236. return HAL_TIMEOUT;
  237. }
  238. }
  239. }
  240. }
  241. /*------------------------------ LSE Configuration -------------------------*/
  242. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  243. {
  244. /* Check the parameters */
  245. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  246. /* Enable Power Clock*/
  247. __HAL_RCC_PWR_CLK_ENABLE();
  248. /* Enable write access to Backup domain */
  249. PWR->CR |= PWR_CR_DBP;
  250. /* Wait for Backup domain Write protection enable */
  251. tickstart = HAL_GetTick();
  252. while((PWR->CR & PWR_CR_DBP) == RESET)
  253. {
  254. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  255. {
  256. return HAL_TIMEOUT;
  257. }
  258. }
  259. /* Set the new LSE configuration -----------------------------------------*/
  260. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  261. /* Check the LSE State */
  262. if((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF)
  263. {
  264. /* Get Start Tick*/
  265. tickstart = HAL_GetTick();
  266. /* Wait till LSE is ready */
  267. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  268. {
  269. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  270. {
  271. return HAL_TIMEOUT;
  272. }
  273. }
  274. }
  275. else
  276. {
  277. /* Get Start Tick*/
  278. tickstart = HAL_GetTick();
  279. /* Wait till LSE is ready */
  280. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
  281. {
  282. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  283. {
  284. return HAL_TIMEOUT;
  285. }
  286. }
  287. }
  288. }
  289. /*-------------------------------- PLL Configuration -----------------------*/
  290. /* Check the parameters */
  291. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  292. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  293. {
  294. /* Check if the PLL is used as system clock or not */
  295. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  296. {
  297. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  298. {
  299. /* Check the parameters */
  300. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  301. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  302. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  303. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  304. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  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. /* Configure the main PLL clock source, multiplication and division factors. */
  318. WRITE_REG(RCC->PLLCFGR, (RCC_OscInitStruct->PLL.PLLSource | \
  319. RCC_OscInitStruct->PLL.PLLM | \
  320. (RCC_OscInitStruct->PLL.PLLN << POSITION_VAL(RCC_PLLCFGR_PLLN)) | \
  321. (((RCC_OscInitStruct->PLL.PLLP >> 1U) - 1U) << POSITION_VAL(RCC_PLLCFGR_PLLP)) | \
  322. (RCC_OscInitStruct->PLL.PLLQ << POSITION_VAL(RCC_PLLCFGR_PLLQ))));
  323. /* Enable the main PLL. */
  324. __HAL_RCC_PLL_ENABLE();
  325. /* Get Start Tick*/
  326. tickstart = HAL_GetTick();
  327. /* Wait till PLL is ready */
  328. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  329. {
  330. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  331. {
  332. return HAL_TIMEOUT;
  333. }
  334. }
  335. }
  336. else
  337. {
  338. /* Disable the main PLL. */
  339. __HAL_RCC_PLL_DISABLE();
  340. /* Get Start Tick*/
  341. tickstart = HAL_GetTick();
  342. /* Wait till PLL is ready */
  343. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  344. {
  345. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  346. {
  347. return HAL_TIMEOUT;
  348. }
  349. }
  350. }
  351. }
  352. else
  353. {
  354. return HAL_ERROR;
  355. }
  356. }
  357. return HAL_OK;
  358. }
  359. /**
  360. * @brief Initializes the CPU, AHB and APB busses clocks according to the specified
  361. * parameters in the RCC_ClkInitStruct.
  362. * @param RCC_ClkInitStruct: pointer to an RCC_OscInitTypeDef structure that
  363. * contains the configuration information for the RCC peripheral.
  364. * @param FLatency: FLASH Latency, this parameter depend on device selected
  365. *
  366. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  367. * and updated by HAL_RCC_GetHCLKFreq() function called within this function
  368. *
  369. * @note The HSI is used (enabled by hardware) as system clock source after
  370. * startup from Reset, wake-up from STOP and STANDBY mode, or in case
  371. * of failure of the HSE used directly or indirectly as system clock
  372. * (if the Clock Security System CSS is enabled).
  373. *
  374. * @note A switch from one clock source to another occurs only if the target
  375. * clock source is ready (clock stable after startup delay or PLL locked).
  376. * If a clock source which is not yet ready is selected, the switch will
  377. * occur when the clock source will be ready.
  378. *
  379. * @note Depending on the device voltage range, the software has to set correctly
  380. * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
  381. * (for more details refer to section above "Initialization/de-initialization functions")
  382. * @retval None
  383. */
  384. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  385. {
  386. uint32_t tickstart = 0U;
  387. /* Check the parameters */
  388. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  389. assert_param(IS_FLASH_LATENCY(FLatency));
  390. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  391. must be correctly programmed according to the frequency of the CPU clock
  392. (HCLK) and the supply voltage of the device. */
  393. /* Increasing the number of wait states because of higher CPU frequency */
  394. if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
  395. {
  396. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  397. __HAL_FLASH_SET_LATENCY(FLatency);
  398. /* Check that the new number of wait states is taken into account to access the Flash
  399. memory by reading the FLASH_ACR register */
  400. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  401. {
  402. return HAL_ERROR;
  403. }
  404. }
  405. /*-------------------------- HCLK Configuration --------------------------*/
  406. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  407. {
  408. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  409. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  410. }
  411. /*------------------------- SYSCLK Configuration ---------------------------*/
  412. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  413. {
  414. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  415. /* HSE is selected as System Clock Source */
  416. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  417. {
  418. /* Check the HSE ready flag */
  419. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  420. {
  421. return HAL_ERROR;
  422. }
  423. }
  424. /* PLL is selected as System Clock Source */
  425. else if((RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK) ||
  426. (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLRCLK))
  427. {
  428. /* Check the PLL ready flag */
  429. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  430. {
  431. return HAL_ERROR;
  432. }
  433. }
  434. /* HSI is selected as System Clock Source */
  435. else
  436. {
  437. /* Check the HSI ready flag */
  438. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  439. {
  440. return HAL_ERROR;
  441. }
  442. }
  443. __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource);
  444. /* Get Start Tick*/
  445. tickstart = HAL_GetTick();
  446. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  447. {
  448. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)
  449. {
  450. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  451. {
  452. return HAL_TIMEOUT;
  453. }
  454. }
  455. }
  456. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  457. {
  458. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  459. {
  460. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  461. {
  462. return HAL_TIMEOUT;
  463. }
  464. }
  465. }
  466. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLRCLK)
  467. {
  468. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLRCLK)
  469. {
  470. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  471. {
  472. return HAL_TIMEOUT;
  473. }
  474. }
  475. }
  476. else
  477. {
  478. while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)
  479. {
  480. if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  481. {
  482. return HAL_TIMEOUT;
  483. }
  484. }
  485. }
  486. }
  487. /* Decreasing the number of wait states because of lower CPU frequency */
  488. if(FLatency < (FLASH->ACR & FLASH_ACR_LATENCY))
  489. {
  490. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  491. __HAL_FLASH_SET_LATENCY(FLatency);
  492. /* Check that the new number of wait states is taken into account to access the Flash
  493. memory by reading the FLASH_ACR register */
  494. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  495. {
  496. return HAL_ERROR;
  497. }
  498. }
  499. /*-------------------------- PCLK1 Configuration ---------------------------*/
  500. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  501. {
  502. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  503. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  504. }
  505. /*-------------------------- PCLK2 Configuration ---------------------------*/
  506. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  507. {
  508. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  509. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
  510. }
  511. /* Update the SystemCoreClock global variable */
  512. //SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> POSITION_VAL(RCC_CFGR_HPRE)];
  513. /* Configure the source of time base considering new system clocks settings*/
  514. //HAL_InitTick (TICK_INT_PRIORITY);
  515. return HAL_OK;
  516. }
  517. /**
  518. * @brief Returns the PCLK1 frequency
  519. * @note Each time PCLK1 changes, this function must be called to update the
  520. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  521. * @retval PCLK1 frequency
  522. */
  523. uint32_t HAL_RCC_GetPCLK1Freq(void)
  524. {
  525. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  526. //return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1)>> POSITION_VAL(RCC_CFGR_PPRE1)]);
  527. return 7.37E6;
  528. }
  529. /**
  530. * @brief Returns the PCLK2 frequency
  531. * @note Each time PCLK2 changes, this function must be called to update the
  532. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  533. * @retval PCLK2 frequency
  534. */
  535. uint32_t HAL_RCC_GetPCLK2Freq(void)
  536. {
  537. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  538. //return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2)>> POSITION_VAL(RCC_CFGR_PPRE2)]);
  539. return 7.37E6;
  540. }
  541. #define GPIO_MODE 0x00000003U
  542. #define EXTI_MODE 0x10000000U
  543. #define GPIO_MODE_IT 0x00010000U
  544. #define GPIO_MODE_EVT 0x00020000U
  545. #define RISING_EDGE 0x00100000U
  546. #define FALLING_EDGE 0x00200000U
  547. #define GPIO_OUTPUT_TYPE 0x00000010U
  548. #define GPIO_NUMBER 16U
  549. /**
  550. * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
  551. * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
  552. * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
  553. * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
  554. * the configuration information for the specified GPIO peripheral.
  555. * @retval None
  556. */
  557. void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  558. {
  559. uint32_t position;
  560. uint32_t ioposition = 0x00U;
  561. uint32_t iocurrent = 0x00U;
  562. uint32_t temp = 0x00U;
  563. /* Check the parameters */
  564. assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  565. assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
  566. assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
  567. assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
  568. /* Configure the port pins */
  569. for(position = 0U; position < GPIO_NUMBER; position++)
  570. {
  571. /* Get the IO position */
  572. ioposition = 0x01U << position;
  573. /* Get the current IO position */
  574. iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
  575. if(iocurrent == ioposition)
  576. {
  577. /*--------------------- GPIO Mode Configuration ------------------------*/
  578. /* In case of Alternate function mode selection */
  579. if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
  580. {
  581. /* Check the Alternate function parameter */
  582. assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
  583. /* Configure Alternate function mapped with the current IO */
  584. temp = GPIOx->AFR[position >> 3U];
  585. temp &= ~(0xFU << ((uint32_t)(position & 0x07U) * 4U)) ;
  586. temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & 0x07U) * 4U));
  587. GPIOx->AFR[position >> 3U] = temp;
  588. }
  589. /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
  590. temp = GPIOx->MODER;
  591. temp &= ~(GPIO_MODER_MODER0 << (position * 2U));
  592. temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
  593. GPIOx->MODER = temp;
  594. /* In case of Output or Alternate function mode selection */
  595. if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
  596. (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
  597. {
  598. /* Check the Speed parameter */
  599. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  600. /* Configure the IO Speed */
  601. temp = GPIOx->OSPEEDR;
  602. temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2U));
  603. temp |= (GPIO_Init->Speed << (position * 2U));
  604. GPIOx->OSPEEDR = temp;
  605. /* Configure the IO Output Type */
  606. temp = GPIOx->OTYPER;
  607. temp &= ~(GPIO_OTYPER_OT_0 << position) ;
  608. temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4U) << position);
  609. GPIOx->OTYPER = temp;
  610. }
  611. /* Activate the Pull-up or Pull down resistor for the current IO */
  612. temp = GPIOx->PUPDR;
  613. temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U));
  614. temp |= ((GPIO_Init->Pull) << (position * 2U));
  615. GPIOx->PUPDR = temp;
  616. /*--------------------- EXTI Mode Configuration ------------------------*/
  617. /* Configure the External Interrupt or event for the current IO */
  618. if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
  619. {
  620. /* Enable SYSCFG Clock */
  621. __HAL_RCC_SYSCFG_CLK_ENABLE();
  622. temp = SYSCFG->EXTICR[position >> 2U];
  623. temp &= ~(0x0FU << (4U * (position & 0x03U)));
  624. temp |= ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4U * (position & 0x03U)));
  625. SYSCFG->EXTICR[position >> 2U] = temp;
  626. /* Clear EXTI line configuration */
  627. temp = EXTI->IMR;
  628. temp &= ~((uint32_t)iocurrent);
  629. if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
  630. {
  631. temp |= iocurrent;
  632. }
  633. EXTI->IMR = temp;
  634. temp = EXTI->EMR;
  635. temp &= ~((uint32_t)iocurrent);
  636. if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
  637. {
  638. temp |= iocurrent;
  639. }
  640. EXTI->EMR = temp;
  641. /* Clear Rising Falling edge configuration */
  642. temp = EXTI->RTSR;
  643. temp &= ~((uint32_t)iocurrent);
  644. if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
  645. {
  646. temp |= iocurrent;
  647. }
  648. EXTI->RTSR = temp;
  649. temp = EXTI->FTSR;
  650. temp &= ~((uint32_t)iocurrent);
  651. if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
  652. {
  653. temp |= iocurrent;
  654. }
  655. EXTI->FTSR = temp;
  656. }
  657. }
  658. }
  659. }
  660. /**
  661. * @brief Sets or clears the selected data port bit.
  662. *
  663. * @note This function uses GPIOx_BSRR register to allow atomic read/modify
  664. * accesses. In this way, there is no risk of an IRQ occurring between
  665. * the read and the modify access.
  666. *
  667. * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
  668. * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
  669. * @param GPIO_Pin: specifies the port bit to be written.
  670. * This parameter can be one of GPIO_PIN_x where x can be (0..15).
  671. * @param PinState: specifies the value to be written to the selected bit.
  672. * This parameter can be one of the GPIO_PinState enum values:
  673. * @arg GPIO_PIN_RESET: to clear the port pin
  674. * @arg GPIO_PIN_SET: to set the port pin
  675. * @retval None
  676. */
  677. void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  678. {
  679. /* Check the parameters */
  680. assert_param(IS_GPIO_PIN(GPIO_Pin));
  681. assert_param(IS_GPIO_PIN_ACTION(PinState));
  682. if(PinState != GPIO_PIN_RESET)
  683. {
  684. GPIOx->BSRR = GPIO_Pin;
  685. }
  686. else
  687. {
  688. GPIOx->BSRR = (uint32_t)GPIO_Pin << 16U;
  689. }
  690. }
  691. /**
  692. * @brief Configures the UART peripheral.
  693. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  694. * the configuration information for the specified UART module.
  695. * @retval None
  696. */
  697. static void UART_SetConfig(UART_HandleTypeDef *huart)
  698. {
  699. uint32_t tmpreg = 0x00U;
  700. /* Check the parameters */
  701. assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
  702. assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
  703. assert_param(IS_UART_PARITY(huart->Init.Parity));
  704. assert_param(IS_UART_MODE(huart->Init.Mode));
  705. /*-------------------------- USART CR2 Configuration -----------------------*/
  706. tmpreg = huart->Instance->CR2;
  707. /* Clear STOP[13:12] bits */
  708. tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
  709. /* Configure the UART Stop Bits: Set STOP[13:12] bits according to huart->Init.StopBits value */
  710. tmpreg |= (uint32_t)huart->Init.StopBits;
  711. /* Write to USART CR2 */
  712. WRITE_REG(huart->Instance->CR2, (uint32_t)tmpreg);
  713. /*-------------------------- USART CR1 Configuration -----------------------*/
  714. tmpreg = huart->Instance->CR1;
  715. /* Clear M, PCE, PS, TE and RE bits */
  716. tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
  717. USART_CR1_RE | USART_CR1_OVER8));
  718. /* Configure the UART Word Length, Parity and mode:
  719. Set the M bits according to huart->Init.WordLength value
  720. Set PCE and PS bits according to huart->Init.Parity value
  721. Set TE and RE bits according to huart->Init.Mode value
  722. Set OVER8 bit according to huart->Init.OverSampling value */
  723. tmpreg |= (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling;
  724. /* Write to USART CR1 */
  725. WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg);
  726. /*-------------------------- USART CR3 Configuration -----------------------*/
  727. tmpreg = huart->Instance->CR3;
  728. /* Clear CTSE and RTSE bits */
  729. tmpreg &= (uint32_t)~((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE));
  730. /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */
  731. tmpreg |= huart->Init.HwFlowCtl;
  732. /* Write to USART CR3 */
  733. WRITE_REG(huart->Instance->CR3, (uint32_t)tmpreg);
  734. /* Check the Over Sampling */
  735. if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
  736. {
  737. /*-------------------------- USART BRR Configuration ---------------------*/
  738. #if defined(USART6)
  739. if((huart->Instance == USART1) || (huart->Instance == USART6))
  740. {
  741. huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
  742. }
  743. #else
  744. if(huart->Instance == USART1)
  745. {
  746. huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
  747. }
  748. #endif /* USART6 */
  749. else
  750. {
  751. huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
  752. }
  753. }
  754. else
  755. {
  756. /*-------------------------- USART BRR Configuration ---------------------*/
  757. #if defined(USART6)
  758. if((huart->Instance == USART1) || (huart->Instance == USART6))
  759. {
  760. huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
  761. }
  762. #else
  763. if(huart->Instance == USART1)
  764. {
  765. huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
  766. }
  767. #endif /* USART6 */
  768. else
  769. {
  770. huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
  771. }
  772. }
  773. }
  774. /**
  775. * @brief Initializes the UART mode according to the specified parameters in
  776. * the UART_InitTypeDef and create the associated handle.
  777. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  778. * the configuration information for the specified UART module.
  779. * @retval HAL status
  780. */
  781. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  782. {
  783. /* Check the UART handle allocation */
  784. if(huart == NULL)
  785. {
  786. return HAL_ERROR;
  787. }
  788. /* Check the parameters */
  789. if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
  790. {
  791. /* The hardware flow control is available only for USART1, USART2, USART3 and USART6 */
  792. assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
  793. assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
  794. }
  795. else
  796. {
  797. assert_param(IS_UART_INSTANCE(huart->Instance));
  798. }
  799. assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  800. assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  801. if(huart->gState == HAL_UART_STATE_RESET)
  802. {
  803. /* Allocate lock resource and initialize it */
  804. huart->Lock = HAL_UNLOCKED;
  805. /* Init the low level hardware */
  806. //HAL_UART_MspInit(huart);
  807. }
  808. huart->gState = HAL_UART_STATE_BUSY;
  809. /* Disable the peripheral */
  810. __HAL_UART_DISABLE(huart);
  811. /* Set the UART Communication parameters */
  812. UART_SetConfig(huart);
  813. /* In asynchronous mode, the following bits must be kept cleared:
  814. - LINEN and CLKEN bits in the USART_CR2 register,
  815. - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
  816. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  817. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  818. /* Enable the peripheral */
  819. __HAL_UART_ENABLE(huart);
  820. /* Initialize the UART state */
  821. huart->ErrorCode = HAL_UART_ERROR_NONE;
  822. huart->gState= HAL_UART_STATE_READY;
  823. huart->RxState= HAL_UART_STATE_READY;
  824. return HAL_OK;
  825. }
  826. static HAL_StatusTypeDef UART_WaitOnFlagForever(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status)
  827. {
  828. /* Wait until flag is set */
  829. while((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status);
  830. return HAL_OK;
  831. }
  832. /**
  833. * @brief Sends an amount of data in blocking mode.
  834. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  835. * the configuration information for the specified UART module.
  836. * @param pData: Pointer to data buffer
  837. * @param Size: Amount of data to be sent
  838. * @param Timeout: Timeout duration
  839. * @retval HAL status
  840. */
  841. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  842. {
  843. uint16_t* tmp;
  844. //uint32_t tickstart = 0U;
  845. /* Check that a Tx process is not already ongoing */
  846. if(huart->gState == HAL_UART_STATE_READY)
  847. {
  848. if((pData == NULL ) || (Size == 0))
  849. {
  850. return HAL_ERROR;
  851. }
  852. /* Process Locked */
  853. __HAL_LOCK(huart);
  854. huart->ErrorCode = HAL_UART_ERROR_NONE;
  855. huart->gState = HAL_UART_STATE_BUSY_TX;
  856. /* Init tickstart for timeout managment */
  857. //tickstart = HAL_GetTick();
  858. huart->TxXferSize = Size;
  859. huart->TxXferCount = Size;
  860. while(huart->TxXferCount > 0U)
  861. {
  862. huart->TxXferCount--;
  863. if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  864. {
  865. if(UART_WaitOnFlagForever(huart, UART_FLAG_TXE, RESET) != HAL_OK)
  866. {
  867. return HAL_TIMEOUT;
  868. }
  869. tmp = (uint16_t*) pData;
  870. huart->Instance->DR = (*tmp & (uint16_t)0x01FF);
  871. if(huart->Init.Parity == UART_PARITY_NONE)
  872. {
  873. pData +=2U;
  874. }
  875. else
  876. {
  877. pData +=1U;
  878. }
  879. }
  880. else
  881. {
  882. if(UART_WaitOnFlagForever(huart, UART_FLAG_TXE, RESET) != HAL_OK)
  883. {
  884. return HAL_TIMEOUT;
  885. }
  886. huart->Instance->DR = (*pData++ & (uint8_t)0xFF);
  887. }
  888. }
  889. if(UART_WaitOnFlagForever(huart, UART_FLAG_TC, RESET) != HAL_OK)
  890. {
  891. return HAL_TIMEOUT;
  892. }
  893. /* At end of Tx process, restore huart->gState to Ready */
  894. huart->gState = HAL_UART_STATE_READY;
  895. /* Process Unlocked */
  896. __HAL_UNLOCK(huart);
  897. return HAL_OK;
  898. }
  899. else
  900. {
  901. return HAL_BUSY;
  902. }
  903. }
  904. /**
  905. * @brief Receives an amount of data in blocking mode.
  906. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  907. * the configuration information for the specified UART module.
  908. * @param pData: Pointer to data buffer
  909. * @param Size: Amount of data to be received
  910. * @param Timeout: Timeout duration
  911. * @retval HAL status
  912. */
  913. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  914. {
  915. uint16_t* tmp;
  916. //uint32_t tickstart = 0U;
  917. /* Check that a Rx process is not already ongoing */
  918. if(huart->RxState == HAL_UART_STATE_READY)
  919. {
  920. if((pData == NULL ) || (Size == 0))
  921. {
  922. return HAL_ERROR;
  923. }
  924. /* Process Locked */
  925. __HAL_LOCK(huart);
  926. huart->ErrorCode = HAL_UART_ERROR_NONE;
  927. huart->RxState = HAL_UART_STATE_BUSY_RX;
  928. /* Init tickstart for timeout managment */
  929. //tickstart = HAL_GetTick();
  930. huart->RxXferSize = Size;
  931. huart->RxXferCount = Size;
  932. /* Check the remain data to be received */
  933. while(huart->RxXferCount > 0U)
  934. {
  935. huart->RxXferCount--;
  936. if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  937. {
  938. if(UART_WaitOnFlagForever(huart, UART_FLAG_RXNE, RESET) != HAL_OK)
  939. {
  940. return HAL_TIMEOUT;
  941. }
  942. tmp = (uint16_t*) pData;
  943. if(huart->Init.Parity == UART_PARITY_NONE)
  944. {
  945. *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
  946. pData +=2U;
  947. }
  948. else
  949. {
  950. *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
  951. pData +=1U;
  952. }
  953. }
  954. else
  955. {
  956. if(UART_WaitOnFlagForever(huart, UART_FLAG_RXNE, RESET) != HAL_OK)
  957. {
  958. return HAL_TIMEOUT;
  959. }
  960. if(huart->Init.Parity == UART_PARITY_NONE)
  961. {
  962. *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
  963. }
  964. else
  965. {
  966. *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
  967. }
  968. }
  969. }
  970. /* At end of Rx process, restore huart->RxState to Ready */
  971. huart->RxState = HAL_UART_STATE_READY;
  972. /* Process Unlocked */
  973. __HAL_UNLOCK(huart);
  974. return HAL_OK;
  975. }
  976. else
  977. {
  978. return HAL_BUSY;
  979. }
  980. }
  981. ///// ----- From stm32f4xx_cryp.c ------------------------------------------------
  982. //#define FLAG_MASK ((uint8_t)0x20)
  983. ///**
  984. //* @brief Fills each CRYP_KeyInitStruct member with its default value.
  985. //* @param CRYP_KeyInitStruct: pointer to a CRYP_KeyInitTypeDef structure
  986. //* which will be initialized.
  987. //* @retval None
  988. //*/
  989. //void CRYP_KeyStructInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct)
  990. //{
  991. //CRYP_KeyInitStruct->CRYP_Key0Left = 0;
  992. //CRYP_KeyInitStruct->CRYP_Key0Right = 0;
  993. //CRYP_KeyInitStruct->CRYP_Key1Left = 0;
  994. //CRYP_KeyInitStruct->CRYP_Key1Right = 0;
  995. //CRYP_KeyInitStruct->CRYP_Key2Left = 0;
  996. //CRYP_KeyInitStruct->CRYP_Key2Right = 0;
  997. //CRYP_KeyInitStruct->CRYP_Key3Left = 0;
  998. //CRYP_KeyInitStruct->CRYP_Key3Right = 0;
  999. //}
  1000. ///**
  1001. //* @brief Flushes the IN and OUT FIFOs (that is read and write pointers of the
  1002. //* FIFOs are reset)
  1003. //* @note The FIFOs must be flushed only when BUSY flag is reset.
  1004. //* @param None
  1005. //* @retval None
  1006. //*/
  1007. //void CRYP_FIFOFlush(void)
  1008. //{
  1009. ///* Reset the read and write pointers of the FIFOs */
  1010. //CRYP->CR |= CRYP_CR_FFLUSH;
  1011. //}
  1012. ///**
  1013. //* @brief Initializes the CRYP peripheral according to the specified parameters
  1014. //* in the CRYP_InitStruct.
  1015. //* @param CRYP_InitStruct: pointer to a CRYP_InitTypeDef structure that contains
  1016. //* the configuration information for the CRYP peripheral.
  1017. //* @retval None
  1018. //*/
  1019. //void CRYP_Init(CRYP_InitTypeDef* CRYP_InitStruct)
  1020. //{
  1021. ///* Check the parameters */
  1022. //assert_param(IS_CRYP_ALGOMODE(CRYP_InitStruct->CRYP_AlgoMode));
  1023. //assert_param(IS_CRYP_DATATYPE(CRYP_InitStruct->CRYP_DataType));
  1024. //assert_param(IS_CRYP_ALGODIR(CRYP_InitStruct->CRYP_AlgoDir));
  1025. ///* Select Algorithm mode*/
  1026. //CRYP->CR &= ~CRYP_CR_ALGOMODE;
  1027. //CRYP->CR |= CRYP_InitStruct->CRYP_AlgoMode;
  1028. ///* Select dataType */
  1029. //CRYP->CR &= ~CRYP_CR_DATATYPE;
  1030. //CRYP->CR |= CRYP_InitStruct->CRYP_DataType;
  1031. ///* select Key size (used only with AES algorithm) */
  1032. //if ((CRYP_InitStruct->CRYP_AlgoMode != CRYP_AlgoMode_TDES_ECB) &&
  1033. //(CRYP_InitStruct->CRYP_AlgoMode != CRYP_AlgoMode_TDES_CBC) &&
  1034. //(CRYP_InitStruct->CRYP_AlgoMode != CRYP_AlgoMode_DES_ECB) &&
  1035. //(CRYP_InitStruct->CRYP_AlgoMode != CRYP_AlgoMode_DES_CBC))
  1036. //{
  1037. //assert_param(IS_CRYP_KEYSIZE(CRYP_InitStruct->CRYP_KeySize));
  1038. //CRYP->CR &= ~CRYP_CR_KEYSIZE;
  1039. //CRYP->CR |= CRYP_InitStruct->CRYP_KeySize; /* Key size and value must be
  1040. //configured once the key has
  1041. //been prepared */
  1042. //}
  1043. ///* Select data Direction */
  1044. //CRYP->CR &= ~CRYP_CR_ALGODIR;
  1045. //CRYP->CR |= CRYP_InitStruct->CRYP_AlgoDir;
  1046. //}
  1047. ///**
  1048. //* @brief Initializes the CRYP Keys according to the specified parameters in
  1049. //* the CRYP_KeyInitStruct.
  1050. //* @param CRYP_KeyInitStruct: pointer to a CRYP_KeyInitTypeDef structure that
  1051. //* contains the configuration information for the CRYP Keys.
  1052. //* @retval None
  1053. //*/
  1054. //void CRYP_KeyInit(CRYP_KeyInitTypeDef* CRYP_KeyInitStruct)
  1055. //{
  1056. ///* Key Initialisation */
  1057. //CRYP->K0LR = CRYP_KeyInitStruct->CRYP_Key0Left;
  1058. //CRYP->K0RR = CRYP_KeyInitStruct->CRYP_Key0Right;
  1059. //CRYP->K1LR = CRYP_KeyInitStruct->CRYP_Key1Left;
  1060. //CRYP->K1RR = CRYP_KeyInitStruct->CRYP_Key1Right;
  1061. //CRYP->K2LR = CRYP_KeyInitStruct->CRYP_Key2Left;
  1062. //CRYP->K2RR = CRYP_KeyInitStruct->CRYP_Key2Right;
  1063. //CRYP->K3LR = CRYP_KeyInitStruct->CRYP_Key3Left;
  1064. //CRYP->K3RR = CRYP_KeyInitStruct->CRYP_Key3Right;
  1065. //}
  1066. ///**
  1067. //* @brief Enables or disables the CRYP peripheral.
  1068. //* @param NewState: new state of the CRYP peripheral.
  1069. //* This parameter can be: ENABLE or DISABLE.
  1070. //* @retval None
  1071. //*/
  1072. //void CRYP_Cmd(FunctionalState NewState)
  1073. //{
  1074. ///* Check the parameters */
  1075. //assert_param(IS_FUNCTIONAL_STATE(NewState));
  1076. //if (NewState != DISABLE)
  1077. //{
  1078. ///* Enable the Cryptographic processor */
  1079. //CRYP->CR |= CRYP_CR_CRYPEN;
  1080. //}
  1081. //else
  1082. //{
  1083. ///* Disable the Cryptographic processor */
  1084. //CRYP->CR &= ~CRYP_CR_CRYPEN;
  1085. //}
  1086. //}
  1087. ///**
  1088. //* @brief Returns whether CRYP peripheral is enabled or disabled.
  1089. //* @param none.
  1090. //* @retval Current state of the CRYP peripheral (ENABLE or DISABLE).
  1091. //*/
  1092. //FunctionalState CRYP_GetCmdStatus(void)
  1093. //{
  1094. //FunctionalState state = DISABLE;
  1095. //if ((CRYP->CR & CRYP_CR_CRYPEN) != 0)
  1096. //{
  1097. ///* CRYPEN bit is set */
  1098. //state = ENABLE;
  1099. //}
  1100. //else
  1101. //{
  1102. ///* CRYPEN bit is reset */
  1103. //state = DISABLE;
  1104. //}
  1105. //return state;
  1106. //}
  1107. ///**
  1108. //* @brief Writes data in the Data Input register (DIN).
  1109. //* @note After the DIN register has been read once or several times,
  1110. //* the FIFO must be flushed (using CRYP_FIFOFlush() function).
  1111. //* @param Data: data to write in Data Input register
  1112. //* @retval None
  1113. //*/
  1114. //void CRYP_DataIn(uint32_t Data)
  1115. //{
  1116. //CRYP->DR = Data;
  1117. //}
  1118. ///**
  1119. //* @brief Returns the last data entered into the output FIFO.
  1120. //* @param None
  1121. //* @retval Last data entered into the output FIFO.
  1122. //*/
  1123. //uint32_t CRYP_DataOut(void)
  1124. //{
  1125. //return CRYP->DOUT;
  1126. //}
  1127. ///**
  1128. //* @brief Checks whether the specified CRYP flag is set or not.
  1129. //* @param CRYP_FLAG: specifies the CRYP flag to check.
  1130. //* This parameter can be one of the following values:
  1131. //* @arg CRYP_FLAG_IFEM: Input FIFO Empty flag.
  1132. //* @arg CRYP_FLAG_IFNF: Input FIFO Not Full flag.
  1133. //* @arg CRYP_FLAG_OFNE: Output FIFO Not Empty flag.
  1134. //* @arg CRYP_FLAG_OFFU: Output FIFO Full flag.
  1135. //* @arg CRYP_FLAG_BUSY: Busy flag.
  1136. //* @arg CRYP_FLAG_OUTRIS: Output FIFO raw interrupt flag.
  1137. //* @arg CRYP_FLAG_INRIS: Input FIFO raw interrupt flag.
  1138. //* @retval The new state of CRYP_FLAG (SET or RESET).
  1139. //*/
  1140. //FlagStatus CRYP_GetFlagStatus(uint8_t CRYP_FLAG)
  1141. //{
  1142. //FlagStatus bitstatus = RESET;
  1143. //uint32_t tempreg = 0;
  1144. ///* Check the parameters */
  1145. //assert_param(IS_CRYP_GET_FLAG(CRYP_FLAG));
  1146. ///* check if the FLAG is in RISR register */
  1147. //if ((CRYP_FLAG & FLAG_MASK) != 0x00)
  1148. //{
  1149. //tempreg = CRYP->RISR;
  1150. //}
  1151. //else /* The FLAG is in SR register */
  1152. //{
  1153. //tempreg = CRYP->SR;
  1154. //}
  1155. ///* Check the status of the specified CRYP flag */
  1156. //if ((tempreg & CRYP_FLAG ) != (uint8_t)RESET)
  1157. //{
  1158. ///* CRYP_FLAG is set */
  1159. //bitstatus = SET;
  1160. //}
  1161. //else
  1162. //{
  1163. ///* CRYP_FLAG is reset */
  1164. //bitstatus = RESET;
  1165. //}
  1166. ///* Return the CRYP_FLAG status */
  1167. //return bitstatus;
  1168. //}
  1169. ///// ----- From stm32f4xx_cryp_aes.c --------------------------------------------
  1170. //#define AESBUSY_TIMEOUT ((uint32_t) 0x00010000)
  1171. ///**
  1172. //* @brief Encrypt and decrypt using AES in ECB Mode
  1173. //* @param Mode: encryption or decryption Mode.
  1174. //* This parameter can be one of the following values:
  1175. //* @arg MODE_ENCRYPT: Encryption
  1176. //* @arg MODE_DECRYPT: Decryption
  1177. //* @param Key: Key used for AES algorithm.
  1178. //* @param Keysize: length of the Key, must be a 128, 192 or 256.
  1179. //* @param Input: pointer to the Input buffer.
  1180. //* @param Ilength: length of the Input buffer, must be a multiple of 16.
  1181. //* @param Output: pointer to the returned buffer.
  1182. //* @retval An ErrorStatus enumeration value:
  1183. //* - SUCCESS: Operation done
  1184. //* - ERROR: Operation failed
  1185. //*/
  1186. //ErrorStatus CRYP_AES_ECB(uint8_t Mode, uint8_t* Key, uint16_t Keysize,
  1187. //uint8_t* Input, uint32_t Ilength, uint8_t* Output)
  1188. //{
  1189. //CRYP_InitTypeDef AES_CRYP_InitStructure;
  1190. //CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
  1191. //__IO uint32_t counter = 0;
  1192. //uint32_t busystatus = 0;
  1193. //ErrorStatus status = SUCCESS;
  1194. //uint32_t keyaddr = (uint32_t)Key;
  1195. //uint32_t inputaddr = (uint32_t)Input;
  1196. //uint32_t outputaddr = (uint32_t)Output;
  1197. //uint32_t i = 0;
  1198. ///* Crypto structures initialisation*/
  1199. //CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
  1200. //switch(Keysize)
  1201. //{
  1202. //case 128:
  1203. //AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
  1204. //AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  1205. //keyaddr+=4;
  1206. //AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  1207. //keyaddr+=4;
  1208. //AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  1209. //keyaddr+=4;
  1210. //AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  1211. //break;
  1212. //case 192:
  1213. //AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
  1214. //AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  1215. //keyaddr+=4;
  1216. //AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  1217. //keyaddr+=4;
  1218. //AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  1219. //keyaddr+=4;
  1220. //AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  1221. //keyaddr+=4;
  1222. //AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  1223. //keyaddr+=4;
  1224. //AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  1225. //break;
  1226. //case 256:
  1227. //AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
  1228. //AES_CRYP_KeyInitStructure.CRYP_Key0Left = __REV(*(uint32_t*)(keyaddr));
  1229. //keyaddr+=4;
  1230. //AES_CRYP_KeyInitStructure.CRYP_Key0Right= __REV(*(uint32_t*)(keyaddr));
  1231. //keyaddr+=4;
  1232. //AES_CRYP_KeyInitStructure.CRYP_Key1Left = __REV(*(uint32_t*)(keyaddr));
  1233. //keyaddr+=4;
  1234. //AES_CRYP_KeyInitStructure.CRYP_Key1Right= __REV(*(uint32_t*)(keyaddr));
  1235. //keyaddr+=4;
  1236. //AES_CRYP_KeyInitStructure.CRYP_Key2Left = __REV(*(uint32_t*)(keyaddr));
  1237. //keyaddr+=4;
  1238. //AES_CRYP_KeyInitStructure.CRYP_Key2Right= __REV(*(uint32_t*)(keyaddr));
  1239. //keyaddr+=4;
  1240. //AES_CRYP_KeyInitStructure.CRYP_Key3Left = __REV(*(uint32_t*)(keyaddr));
  1241. //keyaddr+=4;
  1242. //AES_CRYP_KeyInitStructure.CRYP_Key3Right= __REV(*(uint32_t*)(keyaddr));
  1243. //break;
  1244. //default:
  1245. //break;
  1246. //}
  1247. ///*------------------ AES Decryption ------------------*/
  1248. //if(Mode == MODE_DECRYPT) /* AES decryption */
  1249. //{
  1250. ///* Flush IN/OUT FIFOs */
  1251. //CRYP_FIFOFlush();
  1252. ///* Crypto Init for Key preparation for decryption process */
  1253. //AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  1254. //AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_Key;
  1255. //AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_32b;
  1256. //CRYP_Init(&AES_CRYP_InitStructure);
  1257. ///* Key Initialisation */
  1258. //CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
  1259. ///* Enable Crypto processor */
  1260. //CRYP_Cmd(ENABLE);
  1261. ///* wait until the Busy flag is RESET */
  1262. //do
  1263. //{
  1264. //busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  1265. //counter++;
  1266. //}while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
  1267. //if (busystatus != RESET)
  1268. //{
  1269. //status = ERROR;
  1270. //}
  1271. //else
  1272. //{
  1273. ///* Crypto Init for decryption process */
  1274. //AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
  1275. //}
  1276. //}
  1277. ///*------------------ AES Encryption ------------------*/
  1278. //else /* AES encryption */
  1279. //{
  1280. //CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
  1281. ///* Crypto Init for Encryption process */
  1282. //AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
  1283. //}
  1284. //AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_ECB;
  1285. //AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
  1286. //CRYP_Init(&AES_CRYP_InitStructure);
  1287. ///* Flush IN/OUT FIFOs */
  1288. //CRYP_FIFOFlush();
  1289. ///* Enable Crypto processor */
  1290. //CRYP_Cmd(ENABLE);
  1291. //if(CRYP_GetCmdStatus() == DISABLE)
  1292. //{
  1293. ///* The CRYP peripheral clock is not enabled or the device doesn't embed
  1294. //the CRYP peripheral (please check the device sales type. */
  1295. //return(ERROR);
  1296. //}
  1297. //for(i=0; ((i<Ilength) && (status != ERROR)); i+=16)
  1298. //{
  1299. ///* Write the Input block in the IN FIFO */
  1300. //CRYP_DataIn(*(uint32_t*)(inputaddr));
  1301. //inputaddr+=4;
  1302. //CRYP_DataIn(*(uint32_t*)(inputaddr));
  1303. //inputaddr+=4;
  1304. //CRYP_DataIn(*(uint32_t*)(inputaddr));
  1305. //inputaddr+=4;
  1306. //CRYP_DataIn(*(uint32_t*)(inputaddr));
  1307. //inputaddr+=4;
  1308. ///* Wait until the complete message has been processed */
  1309. //counter = 0;
  1310. //do
  1311. //{
  1312. //busystatus = CRYP_GetFlagStatus(CRYP_FLAG_BUSY);
  1313. //counter++;
  1314. //}while ((counter != AESBUSY_TIMEOUT) && (busystatus != RESET));
  1315. //if (busystatus != RESET)
  1316. //{
  1317. //status = ERROR;
  1318. //}
  1319. //else
  1320. //{
  1321. ///* Read the Output block from the Output FIFO */
  1322. //*(uint32_t*)(outputaddr) = CRYP_DataOut();
  1323. //outputaddr+=4;
  1324. //*(uint32_t*)(outputaddr) = CRYP_DataOut();
  1325. //outputaddr+=4;
  1326. //*(uint32_t*)(outputaddr) = CRYP_DataOut();
  1327. //outputaddr+=4;
  1328. //*(uint32_t*)(outputaddr) = CRYP_DataOut();
  1329. //outputaddr+=4;
  1330. //}
  1331. //}
  1332. ///* Disable Crypto */
  1333. //CRYP_Cmd(DISABLE);
  1334. //return status;
  1335. //}
  1336. /**
  1337. * @brief Writes the Key in Key registers.
  1338. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1339. * the configuration information for CRYP module
  1340. * @param Key Pointer to Key buffer
  1341. * @param KeySize Size of Key
  1342. * @retval None
  1343. */
  1344. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize)
  1345. {
  1346. uint32_t keyaddr = (uint32_t)Key;
  1347. switch(KeySize)
  1348. {
  1349. case CRYP_KEYSIZE_256B:
  1350. /* Key Initialisation */
  1351. hcryp->Instance->K0LR = __REV(*(uint32_t*)(keyaddr));
  1352. keyaddr+=4U;
  1353. hcryp->Instance->K0RR = __REV(*(uint32_t*)(keyaddr));
  1354. keyaddr+=4U;
  1355. hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr));
  1356. keyaddr+=4U;
  1357. hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr));
  1358. keyaddr+=4U;
  1359. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  1360. keyaddr+=4U;
  1361. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  1362. keyaddr+=4U;
  1363. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  1364. keyaddr+=4U;
  1365. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  1366. break;
  1367. case CRYP_KEYSIZE_192B:
  1368. hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr));
  1369. keyaddr+=4U;
  1370. hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr));
  1371. keyaddr+=4U;
  1372. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  1373. keyaddr+=4U;
  1374. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  1375. keyaddr+=4U;
  1376. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  1377. keyaddr+=4U;
  1378. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  1379. break;
  1380. case CRYP_KEYSIZE_128B:
  1381. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  1382. keyaddr+=4U;
  1383. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  1384. keyaddr+=4U;
  1385. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  1386. keyaddr+=4U;
  1387. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  1388. break;
  1389. default:
  1390. break;
  1391. }
  1392. }
  1393. static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
  1394. /**
  1395. * @brief Process Data: Writes Input data in polling mode and read the output data
  1396. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1397. * the configuration information for CRYP module
  1398. * @param Input Pointer to the Input buffer
  1399. * @param Ilength Length of the Input buffer, must be a multiple of 16.
  1400. * @param Output Pointer to the returned buffer
  1401. * @param Timeout Timeout value
  1402. * @retval None
  1403. */
  1404. static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
  1405. {
  1406. uint32_t tickstart = 0U;
  1407. uint32_t i = 0U;
  1408. uint32_t inputaddr = (uint32_t)Input;
  1409. uint32_t outputaddr = (uint32_t)Output;
  1410. for(i=0U; (i < Ilength); i+=16U)
  1411. {
  1412. /* Write the Input block in the IN FIFO */
  1413. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1414. inputaddr+=4U;
  1415. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1416. inputaddr+=4U;
  1417. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1418. inputaddr+=4U;
  1419. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1420. inputaddr+=4U;
  1421. /* Get tick */
  1422. tickstart = HAL_GetTick();
  1423. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  1424. {
  1425. /* Check for the Timeout */
  1426. if(Timeout != HAL_MAX_DELAY)
  1427. {
  1428. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1429. {
  1430. /* Change state */
  1431. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1432. /* Process Unlocked */
  1433. __HAL_UNLOCK(hcryp);
  1434. return HAL_TIMEOUT;
  1435. }
  1436. }
  1437. }
  1438. /* Read the Output block from the Output FIFO */
  1439. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1440. outputaddr+=4U;
  1441. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1442. outputaddr+=4U;
  1443. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1444. outputaddr+=4U;
  1445. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1446. outputaddr+=4U;
  1447. }
  1448. /* Return function status */
  1449. return HAL_OK;
  1450. }
  1451. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
  1452. {
  1453. /* Check the CRYP handle allocation */
  1454. if(hcryp == NULL)
  1455. {
  1456. return HAL_ERROR;
  1457. }
  1458. /* Check the parameters */
  1459. assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
  1460. assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
  1461. if(hcryp->State == HAL_CRYP_STATE_RESET)
  1462. {
  1463. /* Allocate lock resource and initialize it */
  1464. hcryp->Lock = HAL_UNLOCKED;
  1465. /* Init the low level hardware */
  1466. //HAL_CRYP_MspInit(hcryp);
  1467. }
  1468. /* Change the CRYP state */
  1469. hcryp->State = HAL_CRYP_STATE_BUSY;
  1470. /* Set the key size and data type*/
  1471. CRYP->CR = (uint32_t) (hcryp->Init.KeySize | hcryp->Init.DataType);
  1472. /* Reset CrypInCount and CrypOutCount */
  1473. hcryp->CrypInCount = 0U;
  1474. hcryp->CrypOutCount = 0U;
  1475. /* Change the CRYP state */
  1476. hcryp->State = HAL_CRYP_STATE_READY;
  1477. /* Set the default CRYP phase */
  1478. hcryp->Phase = HAL_CRYP_PHASE_READY;
  1479. /* Return function status */
  1480. return HAL_OK;
  1481. }
  1482. /**
  1483. * @brief DeInitializes the CRYP peripheral.
  1484. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1485. * the configuration information for CRYP module
  1486. * @retval HAL status
  1487. */
  1488. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
  1489. {
  1490. /* Check the CRYP handle allocation */
  1491. if(hcryp == NULL)
  1492. {
  1493. return HAL_ERROR;
  1494. }
  1495. /* Change the CRYP state */
  1496. hcryp->State = HAL_CRYP_STATE_BUSY;
  1497. /* Set the default CRYP phase */
  1498. hcryp->Phase = HAL_CRYP_PHASE_READY;
  1499. /* Reset CrypInCount and CrypOutCount */
  1500. hcryp->CrypInCount = 0U;
  1501. hcryp->CrypOutCount = 0U;
  1502. /* Disable the CRYP Peripheral Clock */
  1503. __HAL_CRYP_DISABLE(hcryp);
  1504. /* DeInit the low level hardware: CLOCK, NVIC.*/
  1505. //HAL_CRYP_MspDeInit(hcryp);
  1506. /* Change the CRYP state */
  1507. hcryp->State = HAL_CRYP_STATE_RESET;
  1508. /* Release Lock */
  1509. __HAL_UNLOCK(hcryp);
  1510. /* Return function status */
  1511. return HAL_OK;
  1512. }
  1513. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  1514. {
  1515. /* Process Locked */
  1516. __HAL_LOCK(hcryp);
  1517. /* Change the CRYP state */
  1518. hcryp->State = HAL_CRYP_STATE_BUSY;
  1519. /* Check if initialization phase has already been performed */
  1520. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1521. {
  1522. /* Set the key */
  1523. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1524. /* Set the CRYP peripheral in AES ECB mode */
  1525. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB);
  1526. /* Flush FIFO */
  1527. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1528. /* Enable CRYP */
  1529. __HAL_CRYP_ENABLE(hcryp);
  1530. /* Set the phase */
  1531. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1532. }
  1533. /* Write Plain Data and Get Cypher Data */
  1534. if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  1535. {
  1536. return HAL_TIMEOUT;
  1537. }
  1538. /* Change the CRYP state */
  1539. hcryp->State = HAL_CRYP_STATE_READY;
  1540. /* Process Unlocked */
  1541. __HAL_UNLOCK(hcryp);
  1542. /* Return function status */
  1543. return HAL_OK;
  1544. }
  1545. /**
  1546. * @brief Initializes the CRYP peripheral in AES ECB decryption mode
  1547. * then decrypted pCypherData. The cypher data are available in pPlainData
  1548. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1549. * the configuration information for CRYP module
  1550. * @param pCypherData Pointer to the cyphertext buffer
  1551. * @param Size Length of the plaintext buffer, must be a multiple of 16.
  1552. * @param pPlainData Pointer to the plaintext buffer
  1553. * @param Timeout Specify Timeout value
  1554. * @retval HAL status
  1555. */
  1556. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  1557. {
  1558. uint32_t tickstart = 0U;
  1559. /* Process Locked */
  1560. __HAL_LOCK(hcryp);
  1561. /* Change the CRYP state */
  1562. hcryp->State = HAL_CRYP_STATE_BUSY;
  1563. /* Check if initialization phase has already been performed */
  1564. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1565. {
  1566. /* Set the key */
  1567. CRYP_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1568. /* Set the CRYP peripheral in AES Key mode */
  1569. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_KEY | CRYP_CR_ALGODIR);
  1570. /* Enable CRYP */
  1571. __HAL_CRYP_ENABLE(hcryp);
  1572. /* Get tick */
  1573. tickstart = HAL_GetTick();
  1574. while(HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  1575. {
  1576. /* Check for the Timeout */
  1577. if(Timeout != HAL_MAX_DELAY)
  1578. {
  1579. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1580. {
  1581. /* Change state */
  1582. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1583. /* Process Unlocked */
  1584. __HAL_UNLOCK(hcryp);
  1585. return HAL_TIMEOUT;
  1586. }
  1587. }
  1588. }
  1589. /* Disable CRYP */
  1590. __HAL_CRYP_DISABLE(hcryp);
  1591. /* Reset the ALGOMODE bits*/
  1592. CRYP->CR &= (uint32_t)(~CRYP_CR_ALGOMODE);
  1593. /* Set the CRYP peripheral in AES ECB decryption mode */
  1594. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_ECB | CRYP_CR_ALGODIR);
  1595. /* Flush FIFO */
  1596. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1597. /* Enable CRYP */
  1598. __HAL_CRYP_ENABLE(hcryp);
  1599. /* Set the phase */
  1600. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1601. }
  1602. /* Write Plain Data and Get Cypher Data */
  1603. if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  1604. {
  1605. return HAL_TIMEOUT;
  1606. }
  1607. /* Change the CRYP state */
  1608. hcryp->State = HAL_CRYP_STATE_READY;
  1609. /* Process Unlocked */
  1610. __HAL_UNLOCK(hcryp);
  1611. /* Return function status */
  1612. return HAL_OK;
  1613. }