py32f002b_ll_utils.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_ll_utils.c
  4. * @author MCU Application Team
  5. * @brief UTILS LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by Puya under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. /* Includes ------------------------------------------------------------------*/
  31. #include "py32f002b_ll_utils.h"
  32. #include "py32f002b_ll_rcc.h"
  33. #include "py32f002b_ll_system.h"
  34. #ifdef USE_FULL_ASSERT
  35. #include "py32_assert.h"
  36. #else
  37. #define assert_param(expr) ((void)0U)
  38. #endif /* USE_FULL_ASSERT */
  39. /** @addtogroup PY32F002B_LL_Driver
  40. * @{
  41. */
  42. /** @addtogroup UTILS_LL
  43. * @{
  44. */
  45. /* Private types -------------------------------------------------------------*/
  46. /* Private variables ---------------------------------------------------------*/
  47. /* Private constants ---------------------------------------------------------*/
  48. /** @addtogroup UTILS_LL_Private_Constants
  49. * @{
  50. */
  51. /* Defines used for FLASH latency according to HCLK Frequency */
  52. #define UTILS_SCALE1_LATENCY1_FREQ 24000000U /*!< HCLK frequency to set FLASH latency 1 in power scale 1 */
  53. #define UTILS_SCALE1_LATENCY2_FREQ 48000000U /*!< HCLK frequency to set FLASH latency 2 in power scale 1 */
  54. /**
  55. * @}
  56. */
  57. /* Private macros ------------------------------------------------------------*/
  58. /** @addtogroup UTILS_LL_Private_Macros
  59. * @{
  60. */
  61. #define IS_LL_UTILS_SYSCLK_DIV(__VALUE__) (((__VALUE__) == LL_RCC_SYSCLK_DIV_1) \
  62. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_2) \
  63. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_4) \
  64. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_8) \
  65. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_16) \
  66. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_64) \
  67. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_128) \
  68. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_256) \
  69. || ((__VALUE__) == LL_RCC_SYSCLK_DIV_512))
  70. #define IS_LL_UTILS_APB1_DIV(__VALUE__) (((__VALUE__) == LL_RCC_APB1_DIV_1) \
  71. || ((__VALUE__) == LL_RCC_APB1_DIV_2) \
  72. || ((__VALUE__) == LL_RCC_APB1_DIV_4) \
  73. || ((__VALUE__) == LL_RCC_APB1_DIV_8) \
  74. || ((__VALUE__) == LL_RCC_APB1_DIV_16))
  75. #define IS_LL_UTILS_HSE_BYPASS(__STATE__) (((__STATE__) == LL_UTILS_HSEBYPASS_ON) \
  76. || ((__STATE__) == LL_UTILS_HSEBYPASS_OFF))
  77. /**
  78. * @}
  79. */
  80. /* Exported functions --------------------------------------------------------*/
  81. /** @addtogroup UTILS_LL_Exported_Functions
  82. * @{
  83. */
  84. /** @addtogroup UTILS_LL_EF_DELAY
  85. * @{
  86. */
  87. /**
  88. * @brief This function configures the Cortex-M SysTick source to have 1ms time base.
  89. * @note When a RTOS is used, it is recommended to avoid changing the Systick
  90. * configuration by calling this function, for a delay use rather osDelay RTOS service.
  91. * @param HCLKFrequency HCLK frequency in Hz
  92. * @note HCLK frequency can be calculated thanks to RCC helper macro or function @ref LL_RCC_GetSystemClocksFreq
  93. * @retval None
  94. */
  95. void LL_Init1msTick(uint32_t HCLKFrequency)
  96. {
  97. /* Use frequency provided in argument */
  98. LL_InitTick(HCLKFrequency, 1000U);
  99. }
  100. /**
  101. * @brief This function provides accurate delay (in milliseconds) based
  102. * on SysTick counter flag
  103. * @note When a RTOS is used, it is recommended to avoid using blocking delay
  104. * and use rather osDelay service.
  105. * @note To respect 1ms timebase, user should call @ref LL_Init1msTick function which
  106. * will configure Systick to 1ms
  107. * @param Delay specifies the delay time length, in milliseconds.
  108. * @retval None
  109. */
  110. void LL_mDelay(uint32_t Delay)
  111. {
  112. __IO uint32_t tmp = SysTick->CTRL; /* Clear the COUNTFLAG first */
  113. uint32_t tmpDelay; /* MISRAC2012-Rule-17.8 */
  114. /* Add this code to indicate that local variable is not used */
  115. ((void)tmp);
  116. tmpDelay = Delay;
  117. /* Add a period to guaranty minimum wait */
  118. if (tmpDelay < LL_MAX_DELAY)
  119. {
  120. tmpDelay ++;
  121. }
  122. while (tmpDelay != 0U)
  123. {
  124. if ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)
  125. {
  126. tmpDelay --;
  127. }
  128. }
  129. }
  130. /**
  131. * @}
  132. */
  133. /** @defgroup UTILS_EF_SYSTEM SYSTEM
  134. * @{
  135. */
  136. /**
  137. * @brief This function sets directly SystemCoreClock CMSIS variable.
  138. * @note Variable can be calculated also through SystemCoreClockUpdate function.
  139. * @param HCLKFrequency HCLK frequency in Hz (can be calculated thanks to RCC helper macro)
  140. * @retval None
  141. */
  142. void LL_SetSystemCoreClock(uint32_t HCLKFrequency)
  143. {
  144. /* HCLK clock frequency */
  145. SystemCoreClock = HCLKFrequency;
  146. }
  147. /**
  148. * @brief Update number of Flash wait states in line with new frequency and current
  149. * voltage range.
  150. * @param HCLKFrequency HCLK frequency
  151. * @retval An ErrorStatus enumeration value:
  152. * - SUCCESS: Latency has been modified
  153. * - ERROR: Latency cannot be modified
  154. */
  155. ErrorStatus LL_SetFlashLatency(uint32_t HCLKFrequency)
  156. {
  157. uint32_t timeout;
  158. uint32_t getlatency;
  159. uint32_t latency;
  160. ErrorStatus status;
  161. /* Frequency cannot be equal to 0 or greater than max clock */
  162. if ((HCLKFrequency == 0U) || (HCLKFrequency > UTILS_SCALE1_LATENCY2_FREQ))
  163. {
  164. status = ERROR;
  165. return status;
  166. }
  167. else
  168. {
  169. if (HCLKFrequency > UTILS_SCALE1_LATENCY1_FREQ)
  170. {
  171. /* 24 < HCLK <= 48 => 1WS (2 CPU cycles) */
  172. latency = LL_FLASH_LATENCY_1;
  173. }
  174. else
  175. {
  176. /* else HCLKFrequency < 24MHz default LL_FLASH_LATENCY_0 0WS */
  177. latency = LL_FLASH_LATENCY_0;
  178. }
  179. }
  180. LL_FLASH_SetLatency(latency);
  181. /* Check that the new number of wait states is taken into account to access the Flash
  182. memory by reading the FLASH_ACR register */
  183. timeout = 2u;
  184. do
  185. {
  186. /* Wait for Flash latency to be updated */
  187. getlatency = LL_FLASH_GetLatency();
  188. timeout--;
  189. }
  190. while ((getlatency != latency) && (timeout > 0u));
  191. if (getlatency != latency)
  192. {
  193. status = ERROR;
  194. }
  195. else
  196. {
  197. status = SUCCESS;
  198. }
  199. return status;
  200. }
  201. /**
  202. * @}
  203. */
  204. /**
  205. * @}
  206. */
  207. /**
  208. * @}
  209. */
  210. /**
  211. * @}
  212. */
  213. /************************ (C) COPYRIGHT Puya *****END OF FILE****/