py32f0xx_hal_led.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. ******************************************************************************
  3. * @file py32f0xx_hal_led.c
  4. * @author MCU Application Team
  5. * @brief LED HAL 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 "py32f0xx_hal.h"
  32. /** @addtogroup PY32F0xx_HAL_Driver
  33. * @{
  34. */
  35. #ifdef HAL_LED_MODULE_ENABLED
  36. /** @defgroup LED LED
  37. * @brief LED HAL module driver
  38. * @{
  39. */
  40. /* Private Functions --------------------------------------------------------*/
  41. /** @defgroup LED_Private_Functions LED Private Functions
  42. * @{
  43. */
  44. static void LED_SetConfig(LED_HandleTypeDef *hled);
  45. /**
  46. * @}
  47. */
  48. /* Private Functions --------------------------------------------------------*/
  49. /** @defgroup LED_Private_Functions LED Private Functions
  50. * @{
  51. */
  52. /**
  53. * @brief LED Register config
  54. * @param hled Pointer to a LED_HandleTypeDef structure that contains
  55. * the configuration information for the specified LED..
  56. * @retval State
  57. */
  58. static void LED_SetConfig(LED_HandleTypeDef *hled)
  59. {
  60. uint32_t tmpreg;
  61. tmpreg=hled->Init.ComDrive | (hled->Init.ComNum << LED_CR_LED_COM_SEL_Pos);
  62. MODIFY_REG(hled->Instance->CR,
  63. (uint32_t)(LED_CR_LED_COM_SEL | LED_CR_EHS),
  64. tmpreg);
  65. WRITE_REG(hled->Instance->PR, hled->Init.Prescaler);
  66. WRITE_REG(hled->Instance->TR, (hled->Init.LightTime | (hled->Init.DeadTime<<LED_TR_T2_Pos)));
  67. }
  68. /**
  69. * @}
  70. */
  71. /* Exported Functions --------------------------------------------------------*/
  72. /** @defgroup LED_Exported_Functions LED Exported Functions
  73. * @{
  74. */
  75. /** @defgroup LED_Exported_Functions_Group1 Initialization/de-initialization functions
  76. * @brief Initialization and Configuration functions
  77. * @{
  78. */
  79. /**
  80. * @brief Initializes the LED according to the specified parameters
  81. * in the LED_InitTypeDef and initialize the associated handle.
  82. * @param hled Pointer to a LED_HandleTypeDef structure that contains
  83. * the configuration information for the specified LED.
  84. * @retval HAL status
  85. */
  86. HAL_StatusTypeDef HAL_LED_Init(LED_HandleTypeDef *hled)
  87. {
  88. /* Check the LED handle allocation */
  89. if (hled == NULL)
  90. {
  91. return HAL_ERROR;
  92. }
  93. /* Check the parameters */
  94. assert_param(IS_LED_ALL_INSTANCE(hled->Instance));
  95. assert_param(IS_LED_COM_DRIVE(hled->Init.ComDrive));
  96. assert_param(IS_LED_PRISCALER(hled->Init.Prescaler));
  97. assert_param(IS_LED_COM_NUM(hled->Init.ComNum));
  98. assert_param(IS_LED_LIGHT_TIME(hled->Init.LightTime));
  99. assert_param(IS_LED_DEAD_TIME(hled->Init.DeadTime));
  100. if (hled->State == HAL_LED_STATE_RESET)
  101. {
  102. /* Allocate lock resource and initialize it */
  103. __HAL_UNLOCK(hled);
  104. #if (USE_HAL_LED_REGISTER_CALLBACKS == 1)
  105. /* Reset Callback pointers */
  106. if (hled->EwiCallback == NULL)
  107. {
  108. hled->EwiCallback = HAL_LED_LightCpltCallback;
  109. }
  110. if (hled->MspInitCallback == NULL)
  111. {
  112. hled->MspInitCallback = HAL_LED_MspInit;
  113. }
  114. /* Init the low level hardware */
  115. hled->MspInitCallback(hwwdg);
  116. #else
  117. HAL_LED_MspInit(hled);
  118. #endif
  119. }
  120. hled->State = HAL_LED_STATE_BUSY;
  121. /* LED Register config */
  122. LED_SetConfig(hled);
  123. /* Enable the peripheral */
  124. __HAL_LED_ENABLE(hled);
  125. /* Initialize the LED state */
  126. hled->State= HAL_LED_STATE_READY;
  127. return HAL_OK;
  128. }
  129. /**
  130. * @brief De-initializes the LED peripheral registers to their default reset values.
  131. * @param hled Pointer to a LED_HandleTypeDef structure that contains
  132. * the configuration information for the specified LED.
  133. * @retval HAL status
  134. */
  135. HAL_StatusTypeDef HAL_LED_DeInit(LED_HandleTypeDef *hled)
  136. {
  137. if (hled == NULL)
  138. {
  139. return HAL_ERROR;
  140. }
  141. /* Check the parameters */
  142. assert_param(IS_LED_ALL_INSTANCE(hled->Instance));
  143. hled->Instance->CR = 0x00;
  144. hled->Instance->PR = 0x00;
  145. hled->Instance->DR0 = 0x00;
  146. hled->Instance->DR1 = 0x00;
  147. hled->Instance->DR2 = 0x00;
  148. hled->Instance->DR3 = 0x00;
  149. hled->Instance->IR = 0x01;
  150. hled->State = HAL_LED_STATE_RESET;
  151. /* Release Lock */
  152. __HAL_UNLOCK(hled);
  153. return HAL_OK;
  154. }
  155. /**
  156. * @}
  157. */
  158. /** @defgroup LED_Exported_Functions_Group2 LED Operation Functions
  159. * @brief LED operation functions
  160. * @{
  161. */
  162. /**
  163. * @brief Sets the specified COM display value
  164. * @param hled Pointer to a LED_HandleTypeDef structure that contains
  165. * the configuration information for the specified LED..
  166. * @param comCh Specify COM channels.
  167. * @arg @ref LED_COM0
  168. * @arg @ref LED_COM1
  169. * @arg @ref LED_COM2
  170. * @arg @ref LED_COM3
  171. * @arg @ref LED_COM_ALL
  172. * @param data Specify display values
  173. * @arg @ref LED_DISP_0 display value 0
  174. * @arg @ref LED_DISP_1 display value 1
  175. * @arg @ref LED_DISP_2 display value 2
  176. * @arg @ref LED_DISP_3 display value 3
  177. * @arg @ref LED_DISP_4 display value 4
  178. * @arg @ref LED_DISP_5 display value 5
  179. * @arg @ref LED_DISP_6 display value 6
  180. * @arg @ref LED_DISP_7 display value 7
  181. * @arg @ref LED_DISP_8 display value 8
  182. * @arg @ref LED_DISP_9 display value 9
  183. * @arg @ref LED_DISP_A display character A
  184. * @arg @ref LED_DISP_B display character B
  185. * @arg @ref LED_DISP_C display character C
  186. * @arg @ref LED_DISP_D display character D
  187. * @arg @ref LED_DISP_E display character E
  188. * @arg @ref LED_DISP_F display character F
  189. * @arg @ref LED_DISP_H display character H
  190. * @arg @ref LED_DISP_P display character P
  191. * @arg @ref LED_DISP_U display character U
  192. * @arg @ref LED_DISP_DOT display .
  193. * @arg @ref LED_DISP_FULL display 8.
  194. * @arg @ref LED_DISP_NONE display nothing
  195. * @retval State
  196. */
  197. HAL_StatusTypeDef HAL_LED_SetComDisplay(LED_HandleTypeDef *hled, uint8_t comCh, uint8_t data)
  198. {
  199. uint32_t position=0x00U;
  200. uint32_t chcurrent = 0x00U;
  201. uint32_t *pTmp;
  202. if (hled->State == HAL_LED_STATE_READY)
  203. {
  204. /* Process Locked */
  205. __HAL_LOCK(hled);
  206. hled->State = HAL_LED_STATE_BUSY;
  207. while ((comCh >> position) != 0)
  208. {
  209. /* Get the COM channel position */
  210. chcurrent = comCh & (1U << position);
  211. if(chcurrent)
  212. {
  213. pTmp = ((uint32_t *)&hled->Instance->DR0) + position;
  214. WRITE_REG(*pTmp, data);
  215. }
  216. position++;
  217. }
  218. /* Process Unlocked */
  219. __HAL_UNLOCK(hled);
  220. hled->State = HAL_LED_STATE_READY;
  221. return HAL_OK;
  222. }
  223. else
  224. {
  225. return HAL_ERROR;
  226. }
  227. }
  228. /**
  229. * @brief Handle LED interrupt request.
  230. * @param hled LED handle.
  231. * @retval None
  232. */
  233. void HAL_LED_IRQHandler(LED_HandleTypeDef *hled)
  234. {
  235. /* LED interrupt occurred -------------------------------------*/
  236. if (((hled->Instance->IR & LED_IR_FLAG) != 0U) && ((hled->Instance->CR & LED_CR_IE) != 0U))
  237. {
  238. __HAL_LED_CLEAR_FLAG(hled, LED_IR_FLAG);
  239. HAL_LED_LightCpltCallback(hled);
  240. }
  241. }
  242. __weak void HAL_LED_MspInit(LED_HandleTypeDef *hled)
  243. {
  244. /* Prevent unused argument(s) compilation warning */
  245. UNUSED(hled);
  246. /* NOTE : This function should not be modified, when the callback is needed,
  247. the HAL_LED_MspInit could be implemented in the user file
  248. */
  249. }
  250. __weak void HAL_LED_LightCpltCallback(LED_HandleTypeDef *hled)
  251. {
  252. /* Prevent unused argument(s) compilation warning */
  253. UNUSED(hled);
  254. /* NOTE : This function should not be modified, when the callback is needed,
  255. the HAL_LED_LightCpltCallback could be implemented in the user file
  256. */
  257. }
  258. /**
  259. * @}
  260. */
  261. /**
  262. * @}
  263. */
  264. /**
  265. * @}
  266. */
  267. #endif /* HAL_LED_MODULE_ENABLED */
  268. /**
  269. * @}
  270. */