1 /*********************************************************************************************************//**
2  * @file    ht32f5xxxx_ledc.c
3  * @version $Rev:: 6374         $
4  * @date    $Date:: 2022-10-25 #$
5  * @brief   This file provides all the LEDC firmware functions.
6  *************************************************************************************************************
7  * @attention
8  *
9  * Firmware Disclaimer Information
10  *
11  * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
12  *    code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
13  *    proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
14  *    other intellectual property laws.
15  *
16  * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
17  *    code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
18  *    other than HOLTEK and the customer.
19  *
20  * 3. The program technical documentation, including the code, is provided "as is" and for customer reference
21  *    only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
22  *    the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
23  *    the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
24  *
25  * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
26  ************************************************************************************************************/
27 
28 /* Includes ------------------------------------------------------------------------------------------------*/
29 #include "ht32f5xxxx_ledc.h"
30 
31 /** @addtogroup HT32F5xxxx_Peripheral_Driver HT32F5xxxx Peripheral Driver
32   * @{
33   */
34 
35 /** @defgroup LEDC LEDC
36   * @brief LEDC driver modules
37   * @{
38   */
39 
40 
41 /* Private constants ---------------------------------------------------------------------------------------*/
42 /** @defgroup LEDC_Private_Define LEDC private definitions
43   * @{
44   */
45 #define RPRE_MASK     0xF000FFFF
46 
47 /**
48   * @}
49   */
50 
51 /* Global functions ----------------------------------------------------------------------------------------*/
52 /** @defgroup LEDC_Exported_Functions LEDC exported functions
53   * @{
54   */
55 /*********************************************************************************************************//**
56  * @brief Deinitialize the LEDC peripheral registers to their default reset values.
57  * @retval None
58  ************************************************************************************************************/
LEDC_DeInit(void)59 void LEDC_DeInit(void)
60 {
61   RSTCU_PeripReset_TypeDef RSTCUReset = {{0}};
62   RSTCUReset.Bit.LEDC = 1;
63   RSTCU_PeripReset(RSTCUReset, ENABLE);
64 }
65 
66 /*********************************************************************************************************//**
67  * @brief Initialize the LEDC peripheral according to the specified parameters in the LEDC_InitStruct.
68  * @param LEDC_InitStruct: Pointer to a LEDC_InitTypeDef structure. Please note the following.
69           1. When LEDC is started, the LEDC_ClockSource, LEDC_ClockPrescaler and LEDC_DeadTime can't
70                      be changed, so LEDC_Init() will turn off the LED first.You need to restart LEDC by
71                      LEDC_Cmd(ENABEL).
72           2. The LEDC_DeadTime number must be less than the LEDC_Prescaler.
73              Example:
74              If LEDC_DutyClockNumber selects LEDC_DTYNUM_8, the valid LEDC_DeadTime ranges from 0 to 7.
75              If LEDC_DutyClockNumber selects LEDC_DTYNUM_16, the valid LEDC_DeadTime ranges from 0 to 15.
76              If LEDC_DutyClockNumber selects LEDC_DTYNUM_32, the valid LEDC_DeadTime ranges from 0 to 31.
77              If LEDC_DutyClockNumber selects LEDC_DTYNUM_64, the valid LEDC_DeadTime ranges from 0 to 63.
78  * @retval None
79  ************************************************************************************************************/
LEDC_Init(LEDC_InitTypeDef * LEDC_InitStruct)80 void LEDC_Init(LEDC_InitTypeDef* LEDC_InitStruct)
81 {
82   /* Check the parameters                                                                                   */
83   Assert_Param(IS_LEDC_SRC(LEDC_InitStruct->LEDC_ClockSource));
84   Assert_Param(IS_LEDC_DTYNUM(LEDC_InitStruct->LEDC_DutyClockNumber));
85   Assert_Param(IS_LEDC_PSC(LEDC_InitStruct->LEDC_ClockPrescaler));
86   Assert_Param(IS_LEDC_COMEN(LEDC_InitStruct->LEDC_COMxEN));
87   Assert_Param(IS_LEDC_DTCR(LEDC_InitStruct->LEDC_DeadTime));
88 
89   /* Disable LEDC */
90   HT_LEDC->CR = 0;
91 
92   /* LEDC Control Register Configuration */
93   HT_LEDC->CR = LEDC_InitStruct->LEDC_ClockSource     << 8 |\
94                 LEDC_InitStruct->LEDC_DutyClockNumber << 12 |\
95                 LEDC_InitStruct->LEDC_ClockPrescaler  << 16;
96 
97   /* LEDC COM Enable Register Configuration */
98   HT_LEDC->CER = LEDC_InitStruct->LEDC_COMxEN;
99 
100   /* LEDC  Dead Time Control Register Configuration */
101   HT_LEDC->DTCR = LEDC_InitStruct->LEDC_DeadTime;
102 }
103 
104 /*********************************************************************************************************//**
105  * @brief Select the LEDC timer clock source.
106  * @param Source: specify the clock source of LEDC.
107  *   @arg LEDC_SRC_PCLK
108  *   @arg LEDC_SRC_LSI  : Low speed internal clock.
109  *   @arg LEDC_SRC_LSE  : Low speed external clock.
110  * @retval None
111  ************************************************************************************************************/
LEDC_ClockSourceConfig(LEDC_SRC_Enum Source)112 void LEDC_ClockSourceConfig(LEDC_SRC_Enum Source)
113 {
114   Assert_Param(IS_LEDC_SRC(Source));
115 
116   HT_LEDC->CR = (HT_LEDC->CR & ~(3UL << 8)) | ((u32)Source << 8);
117 }
118 
119 /*********************************************************************************************************//**
120  * @brief Configure the LEDC prescaler.
121  * @param Psc: Value of LEDC prescaler: 0~4095
122  *   This parameter can be one of following values:
123  * @retval None
124  ************************************************************************************************************/
LEDC_SetPrescaler(u32 Psc)125 void LEDC_SetPrescaler(u32 Psc)
126 {
127   Assert_Param(IS_LEDC_PSC(Psc));
128 
129   HT_LEDC->CR = (HT_LEDC->CR & RPRE_MASK) | (Psc << 16);
130 }
131 
132 /*********************************************************************************************************//**
133  * @brief Enable or Disable the LEDC.
134  * @param NewState: This parameter can be ENABLE or DISABLE.
135  * @retval None
136  ************************************************************************************************************/
LEDC_Cmd(ControlStatus NewState)137 void LEDC_Cmd(ControlStatus NewState)
138 {
139   if (NewState != DISABLE)
140   {
141     HT_LEDC->CR |= (1UL);
142   }
143   else
144   {
145     HT_LEDC->CR &= ~(1UL);
146   }
147 }
148 
149 /*********************************************************************************************************//**
150  * @brief Enable or Disable Frame interrupt.
151  * @param NewState: This parameter can be ENABLE or DISABLE.
152  * @retval None
153  ************************************************************************************************************/
LEDC_IntConfig(ControlStatus NewState)154 void LEDC_IntConfig(ControlStatus NewState)
155 {
156   if (NewState != DISABLE)
157   {
158     HT_LEDC->IER |= LEDC_INT_FRAME;
159   }
160   else
161   {
162     HT_LEDC->IER &= ~LEDC_INT_FRAME;
163   }
164 }
165 
166 /*********************************************************************************************************//**
167  * @brief Get the LEDC flag.
168  * @retval SET or RESET
169  ************************************************************************************************************/
LEDC_GetFlagStatus(void)170 FlagStatus LEDC_GetFlagStatus(void)
171 {
172   if (HT_LEDC->SR & LEDC_FLAG_FRAME)
173   {
174     return SET;
175   }
176   else
177   {
178     return RESET;
179   }
180 }
181 
182 /*********************************************************************************************************//**
183  * @brief Clear the LEDC graflag.
184  * @retval None
185  ************************************************************************************************************/
LEDC_ClearFlagStatus(void)186 void LEDC_ClearFlagStatus(void)
187 {
188   HT_LEDC->SR |= LEDC_FLAG_FRAME;
189 }
190 
191 /*********************************************************************************************************//**
192  * @brief Configure COMx's state of LEDC with specified pins.
193  * @param LEDC_COMxEN: This parameter can be any combination of the following values:
194  *        @arg LEDC_COM0EN : Set LEDC COM0
195  *        @arg LEDC_COM1EN : Set LEDC COM1
196  *        @arg LEDC_COM2EN : Set LEDC COM2
197  *        @arg LEDC_COM3EN : Set LEDC COM3
198  *        @arg LEDC_COM4EN : Set LEDC COM4
199  *        @arg LEDC_COM5EN : Set LEDC COM5
200  *        @arg LEDC_COM6EN : Set LEDC COM6
201  *        @arg LEDC_COM7EN : Set LEDC COM7
202  *        @arg LEDC_COM8EN : Set LEDC COM8(Only support 54253 )
203  *        @arg LEDC_COM9EN : Set LEDC COM9(Only support 54253 )
204  *        @arg LEDC_COM10EN : Set LEDC COM10(Only support 54253 )
205  *        @arg LEDC_COM11EN : Set LEDC COM11(Only support 54253 )
206  * @param Cmd: This parameter can be ENABLE or DISABLE.
207  * @retval None
208  ************************************************************************************************************/
LEDC_COMxConfig(u32 LEDC_COMxEN,ControlStatus Cmd)209 void LEDC_COMxConfig(u32 LEDC_COMxEN, ControlStatus Cmd)
210 {
211     /* Check the parameters                                                                                   */
212   Assert_Param(IS_LEDC_COMEN(LEDC_COMxEN));
213 
214   if (Cmd != DISABLE)
215     HT_LEDC->CER |= LEDC_COMxEN;
216   else
217     HT_LEDC->CER &= ~LEDC_COMxEN;
218 }
219 
220 /*********************************************************************************************************//**
221  * @brief Configure the dead time duty. The LED brightness can be adjusted by adjusting the dead duty.
222  * @param LEDC_DeadTimeDuty: Deadtime Clock Numbers. The LEDC_DeadTimeDuty number must be less than the
223                              LEDC_DutyClockNumber(DTYNUM).
224              Example:
225              If LEDC_DutyClockNumber selects LEDC_DTYNUM_8, the valid LEDC_DeadTime ranges from 0 to 7.
226              If LEDC_DutyClockNumber selects LEDC_DTYNUM_16, the valid LEDC_DeadTime ranges from 0 to 15.
227              If LEDC_DutyClockNumber selects LEDC_DTYNUM_32, the valid LEDC_DeadTime ranges from 0 to 31.
228              If LEDC_DutyClockNumber selects LEDC_DTYNUM_64, the valid LEDC_DeadTime ranges from 0 to 63.
229  * @retval None
230  ************************************************************************************************************/
LEDC_SetDeadTimeDuty(u32 LEDC_DeadTimeDuty)231 void LEDC_SetDeadTimeDuty(u32 LEDC_DeadTimeDuty)
232 {
233     /* Check the parameters                                                                                 */
234   Assert_Param(IS_LEDC_DTCR(LEDC_DeadTimeDuty));
235 
236   HT_LEDC->DTCR = LEDC_DeadTimeDuty;
237 }
238 
239 /*********************************************************************************************************//**
240  * @brief Set the output polarity of COM and SEG.
241  * @param LEDC_COMxPOL: This parameter can be any combination of the following values:
242  *        @arg LEDC_COM0POL : Set COM0 polarity
243  *        @arg LEDC_COM1POL : Set COM1 polarity
244  *        @arg LEDC_COM2POL : Set COM2 polarity
245  *        @arg LEDC_COM3POL : Set COM3 polarity
246  *        @arg LEDC_COM4POL : Set COM4 polarity
247  *        @arg LEDC_COM5POL : Set COM5 polarity
248  *        @arg LEDC_COM6POL : Set COM6 polarity
249  *        @arg LEDC_COM7POL : Set COM7 polarity
250  *        @arg LEDC_COM8POL : Set COM8 polarity(Only support 54253 )
251  *        @arg LEDC_COM9POL : Set COM9 polarity(Only support 54253 )
252  *        @arg LEDC_COM10POL : Set COM10 polarity(Only support 54253 )
253  *        @arg LEDC_COM11POL : Set COM11 polarity(Only support 54253 )
254  * @param LEDC_SEGxPOL: This parameter can be any combination of the following values:
255  *        @arg LEDC_SEG0POL : Set SEG0 polarity
256  *        @arg LEDC_SEG1POL : Set SEG1 polarity
257  *        @arg LEDC_SEG2POL : Set SEG2 polarity
258  *        @arg LEDC_SEG3POL : Set SEG3 polarity
259  *        @arg LEDC_SEG4POL : Set SEG4 polarity
260  *        @arg LEDC_SEG5POL : Set SEG5 polarity
261  *        @arg LEDC_SEG6POL : Set SEG6 polarity
262  *        @arg LEDC_SEG7POL : Set SEG7 polarity
263  * @param mode: LED layout mode.
264  *                                       SEG polarity   COM polarity
265  *             -------------------------------------------------------
266  *        @arg COMMON_CATHODE          : non-inverted   non-inverted
267  *        @arg COMMON_CATHODE_WITH_NPN : non-inverted   inverted
268  *        @arg COMMON_ANODE_WITH_PNP   : inverted       non-inverted
269  *        @arg COMMON_ANODE_WITH_NPN   : inverted       inverted
270  *        @arg @retval None
271  ************************************************************************************************************/
LEDC_SetPolarityMode(u32 LEDC_COMxPOL,u32 LEDC_SEGxPOL,LEDC_Mode mode)272 void LEDC_SetPolarityMode(u32 LEDC_COMxPOL, u32 LEDC_SEGxPOL , LEDC_Mode mode)
273 {
274     /* Check the parameters                                                                                 */
275   Assert_Param(IS_LEDC_DTCR(mode));
276   Assert_Param(IS_LEDC_COMPOL(LEDC_COMxPOL));
277   Assert_Param(IS_LEDC_SEGPOL(LEDC_SEGxPOL));
278 
279   switch(mode)
280   {
281     case COMMON_CATHODE:
282       HT_LEDC->PCR &= ~(LEDC_COMxPOL|LEDC_SEGxPOL);
283     break;
284     case COMMON_CATHODE_WITH_NPN:
285       HT_LEDC->PCR |= LEDC_COMxPOL;
286       HT_LEDC->PCR &= ~(LEDC_SEGxPOL);
287     break;
288     case COMMON_ANODE_WITH_PNP:
289       HT_LEDC->PCR &= ~(LEDC_COMxPOL);
290       HT_LEDC->PCR |= LEDC_SEGxPOL;
291     break;
292     case COMMON_ANODE_WITH_NPN:
293       HT_LEDC->PCR |= LEDC_COMxPOL|LEDC_SEGxPOL;
294     break;
295   }
296 }
297 
298 /**
299   * @}
300   */
301 
302 
303 /**
304   * @}
305   */
306 
307 /**
308   * @}
309   */
310