1 /*********************************************************************************************************//**
2  * @file    ht32f66xxx_pid.c
3  * @version $Rev:: 8260         $
4  * @date    $Date:: 2024-11-05 #$
5  * @brief   This file provides all the PID 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 "ht32f66xxx_pid.h"
30 
31 /** @addtogroup HT32F5xxxx_Peripheral_Driver HT32F5xxxx Peripheral Driver
32   * @{
33   */
34 
35 /** @defgroup PID PID
36   * @brief PID driver modules
37   * @{
38   */
39 
40 /* Global functions ----------------------------------------------------------------------------------------*/
41 /** @addtogroup PID_Exported_Functions PID exported functions
42   * @{
43   */
44 /*********************************************************************************************************//**
45  * @brief Deinitialize the HT_PIDn peripheral registers to their default reset values.
46  * @param HT_PIDn: where HT_PIDn is the selected PID from the PID peripherals.
47  * @retval None
48  ************************************************************************************************************/
PID_DeInit(HT_PID_TypeDef * HT_PIDn)49 void PID_DeInit(HT_PID_TypeDef* HT_PIDn)
50 {
51    RSTCU_PeripReset_TypeDef RSTCUReset = {{0}};
52 
53   /* Check the parameters                                                                                   */
54   Assert_Param(IS_PID(HT_PIDn));
55 
56   if (HT_PIDn == HT_PID0)
57   {
58     RSTCUReset.Bit.PID0 = 1;
59   }
60 
61   RSTCU_PeripReset(RSTCUReset, ENABLE);
62 }
63 
64 /*********************************************************************************************************//**
65  * @brief Initialize the HT_PIDn PID_Mode peripheral according to the specified parameters in the PID_InitTypeDef.
66  * @param HT_PIDn: where HT_PIDn is the selected PID from the PID peripherals.
67  * @param PID_Mode: where PID_Mode is the selected PID mode.
68  *   This parameter can be one of the following values:
69  *     @arg PID_SPD_MODE
70  *     @arg PID_IQ_MODE
71  *     @arg PID_ID_MODE
72  *     @arg PID_FWNK_MODE
73  *     @arg PID_PLL_MODE
74  *     @arg PID_USR_MODE
75  * @param PID_Para: where PID_Para is PID paramater structure.
76  * @retval None
77  ************************************************************************************************************/
PID_Init(HT_PID_TypeDef * HT_PIDn,PID_Mode_Enum PID_Mode,PID_InitTypeDef * PID_Para)78 void PID_Init(HT_PID_TypeDef* HT_PIDn, PID_Mode_Enum PID_Mode, PID_InitTypeDef* PID_Para)
79 {
80   HT_PIDPARA_TypeDef * gPID_ModePara = &(HT_PIDn->SPD);
81 
82   /* Check the parameters                                                                                   */
83   Assert_Param(IS_PID(HT_PIDn));
84   Assert_Param(IS_PID_MODE(PID_Mode));
85 
86   (gPID_ModePara + PID_Mode)->KPIR     = PID_Para->KP;
87   (gPID_ModePara + PID_Mode)->KIIR     = PID_Para->KI;
88   (gPID_ModePara + PID_Mode)->KDIR     = PID_Para->KD;
89   (gPID_ModePara + PID_Mode)->IFVMAXLR = PID_Para->UI_MAX;
90   (gPID_ModePara + PID_Mode)->IFVMINLR = PID_Para->UI_MIN;
91   (gPID_ModePara + PID_Mode)->PIDORLR  = (u32)((PID_Para->OUT_MAX << 16) | ((u16)PID_Para->OUT_MIN));
92   (gPID_ModePara + PID_Mode)->LEIR     = PID_Para->ERRn_1;
93   (gPID_ModePara + PID_Mode)->LIFVR    = PID_Para->UIn_1;
94 }
95 
96 /*********************************************************************************************************//**
97  * @brief Enable or Disable the specified PID interrupts.
98  * @param HT_PIDn: where HT_PIDn is the selected PID from the PID peripherals.
99  * @param PID_INT_x: Specify the PID interrupt sources that is to be enabled or disabled.
100  *   This parameter can be any combination of the following values:
101  *     @arg PID_INT_CMP :
102  *     @arg PID_INT_OVF :
103  * @param NewState: This parameter can be ENABLE or DISABLE.
104  * @retval None
105  ************************************************************************************************************/
PID_IntConfig(HT_PID_TypeDef * HT_PIDn,u32 PID_INT_x,ControlStatus NewState)106 void PID_IntConfig(HT_PID_TypeDef* HT_PIDn, u32 PID_INT_x, ControlStatus NewState)
107 {
108   /* Check the parameters                                                                                   */
109   Assert_Param(IS_PID(HT_PIDn));
110   Assert_Param(IS_PID_INT(PID_INT_x));
111   Assert_Param(IS_CONTROL_STATUS(NewState));
112 
113   if (NewState != DISABLE)
114   {
115     HT_PIDn->CR |= PID_INT_x;
116   }
117   else
118   {
119     HT_PIDn->CR &= ~PID_INT_x;
120   }
121 }
122 
123 /*********************************************************************************************************//**
124  * @brief Check whether the specified PID interrupt has occurred.
125  * @param HT_PIDn: where HT_PIDn is the selected PID from the PID peripherals.
126  * @param PID_INT_x: Specify the PID interrupt sources that is to be enabled or disabled.
127  *   This parameter can be any combination of the following values:
128  *     @arg PID_INT_CMP :
129  *     @arg PID_INT_OVF :
130  * @retval SET or RESET
131  ************************************************************************************************************/
PID_GetIntStatus(HT_PID_TypeDef * HT_PIDn,u32 PID_INT_x)132 FlagStatus PID_GetIntStatus(HT_PID_TypeDef* HT_PIDn, u32 PID_INT_x)
133 {
134   FlagStatus Status;
135 
136   /* Check the parameters                                                                                   */
137   Assert_Param(IS_PID(HT_PIDn));
138   Assert_Param(IS_PID_INT(PID_INT_x));
139 
140   if ((HT_PIDn->CR & (PID_INT_x << PID_INT_Status_Pos)) != RESET)
141   {
142     Status = SET;
143   }
144   else
145   {
146     Status = RESET;
147   }
148 
149   return Status;
150 }
151 
152 /*********************************************************************************************************//**
153  * @brief Clear the PID interrupt pending bits.
154  * @param HT_PIDn: where HT_PIDn is the selected PID from the PID peripherals.
155  * @param PID_INT_x: Specify the PID interrupt sources that is to be enabled or disabled.
156  *   This parameter can be any combination of the following values:
157  *     @arg PID_INT_CMP :
158  *     @arg PID_INT_OVF :
159  * @retval None
160  ************************************************************************************************************/
PID_ClearIntPendingBit(HT_PID_TypeDef * HT_PIDn,u32 PID_INT_x)161 void PID_ClearIntPendingBit(HT_PID_TypeDef* HT_PIDn, u32 PID_INT_x)
162 {
163   /* Check the parameters                                                                                   */
164   Assert_Param(IS_PID(HT_PIDn));
165   Assert_Param(IS_PID_INT(PID_INT_x));
166 
167   HT_PIDn->CR |= (PID_INT_x << PID_INT_Clear_Pos);
168 }
169 
170 /*********************************************************************************************************//**
171  * @brief Set the PID ERR(n) value of the common parameters.
172  * @param HT_PIDn: where HT_PIDn is the selected PID from the PID peripherals.
173  * @param ERRn: where ERRn is PID Controller Error Input Value.
174  * @retval None
175  ************************************************************************************************************/
PID_SetComPara_ERRn(HT_PID_TypeDef * HT_PIDn,s32 ERRn)176 void PID_SetComPara_ERRn(HT_PID_TypeDef* HT_PIDn, s32 ERRn)
177 {
178   /* Check the parameters                                                                                   */
179   Assert_Param(IS_PID(HT_PIDn));
180 
181   HT_PIDn->EIVR  = ERRn;
182 }
183 
184 /*********************************************************************************************************//**
185  * @brief Set the PID UI_input(n) value of the common parameters.
186  * @param HT_PIDn: where HT_PIDn is the selected PID from the PID peripherals.
187  * @param UI_Input: where UI_Input is PID Controller integrator accumulation selection.
188  * @retval None
189  ************************************************************************************************************/
PID_SetComPara_UI_Input(HT_PID_TypeDef * HT_PIDn,s32 UI_Input)190 void PID_SetComPara_UI_Input(HT_PID_TypeDef* HT_PIDn, s32 UI_Input)
191 {
192   /* Check the parameters                                                                                   */
193   Assert_Param(IS_PID(HT_PIDn));
194 
195   HT_PIDn->IFIVR = UI_Input;
196 }
197 
198 /*********************************************************************************************************//**
199  * @brief Enable compute the PID calculation.
200  * @param HT_PIDn: where HT_PIDn is the selected PID from the PID peripherals.
201  * @param PID_Mode: where PID_Mode is the selected PID mode.
202  *   This parameter can be one of the following values:
203  *     @arg PID_SPD_MODE
204  *     @arg PID_IQ_MODE
205  *     @arg PID_ID_MODE
206  *     @arg PID_FWNK_MODE
207  *     @arg PID_PLL_MODE
208  *     @arg PID_USR_MODE
209  * @retval None
210  ************************************************************************************************************/
PID_Compute(HT_PID_TypeDef * HT_PIDn,PID_Mode_Enum PID_Mode)211 void PID_Compute(HT_PID_TypeDef* HT_PIDn, PID_Mode_Enum PID_Mode)
212 {
213   /* Check the parameters                                                                                   */
214   Assert_Param(IS_PID(HT_PIDn));
215   Assert_Param(IS_PID_MODE(PID_Mode));
216 
217   HT_PIDn->CR &= ~(u32)PID_CR_MODSEL_Msk;
218   HT_PIDn->CR |= (PID_Mode << PID_CR_MODSEL_Pos) | (PID_CR_PIDEN);
219 }
220 
221 /*********************************************************************************************************//**
222  * @brief Return the PID ouput result value.
223  * @param HT_PIDn: where HT_PIDn is the selected PID from the PID peripherals.
224  * @retval The Value of PID result.
225  ************************************************************************************************************/
PID_GetOutResult(HT_PID_TypeDef * HT_PIDn)226 s16  PID_GetOutResult(HT_PID_TypeDef* HT_PIDn)
227 {
228   /* Check the parameters                                                                                   */
229   Assert_Param(IS_PID(HT_PIDn));
230 
231   return HT_PIDn->ORR;
232 }
233 
234 /*********************************************************************************************************//**
235  * @brief Check whether the specified PID flag has been set.
236  * @param HT_PIDn: where HT_PIDn is the selected PID from the PID peripherals.
237  * @param PID_FLAG_x: Specify the flag to check.
238  *   This parameter can be any combination of the following values:
239  *     @arg PID_FLAG_CMP :
240  *     @arg PID_FLAG_OVF :
241  * @retval SET or RESET
242  ************************************************************************************************************/
PID_GetFlagStatus(HT_PID_TypeDef * HT_PIDn,u32 PID_FLAG_x)243 FlagStatus PID_GetFlagStatus(HT_PID_TypeDef* HT_PIDn, u32 PID_FLAG_x)
244 {
245   FlagStatus Status;
246 
247   /* Check the parameters                                                                                   */
248   Assert_Param(IS_PID(HT_PIDn));
249   Assert_Param(IS_PID_FLAG(PID_FLAG_x));
250 
251   if ((HT_PIDn->CR & PID_FLAG_x) != RESET)
252   {
253     Status = SET;
254   }
255   else
256   {
257     Status = RESET;
258   }
259 
260   return Status;
261 }
262 
263 /*********************************************************************************************************//**
264  * @brief Enable or Disable the integrator accumulation value set by UI_input(n).
265  * @param HT_PIDn: where HT_PIDn is the selected PID from the PID peripherals.
266  * @param NewState: This parameter can be ENABLE or DISABLE.
267  * @retval None
268  ************************************************************************************************************/
PID_UI_InputCmd(HT_PID_TypeDef * HT_PIDn,ControlStatus NewState)269 void PID_UI_InputCmd(HT_PID_TypeDef* HT_PIDn, ControlStatus NewState)
270 {
271   /* Check the parameters                                                                                   */
272   Assert_Param(IS_PID(HT_PIDn));
273   Assert_Param(IS_CONTROL_STATUS(NewState));
274 
275   if (NewState != DISABLE)
276   {
277     HT_PIDn->CR |= PID_CR_UIF;
278   }
279   else
280   {
281     HT_PIDn->CR &= ~(u32)PID_CR_UIF;
282   }
283 }
284 
285 /*********************************************************************************************************//**
286  * @brief Return the PID last error input value (ERRn_1) for the selected PID mode.
287  * @param HT_PIDn: where HT_PIDn is the selected PID from the PID peripherals.
288  * @param PID_Mode: where PID_Mode is the selected PID mode.
289  *   This parameter can be one of the following values:
290  *     @arg PID_SPD_MODE
291  *     @arg PID_IQ_MODE
292  *     @arg PID_ID_MODE
293  *     @arg PID_FWNK_MODE
294  *     @arg PID_PLL_MODE
295  *     @arg PID_USR_MODE
296  * @retval The Value of ERRn_1 for the selected PID mode.
297  ************************************************************************************************************/
PID_GetERRn_1(HT_PID_TypeDef * HT_PIDn,PID_Mode_Enum PID_Mode)298 s32  PID_GetERRn_1(HT_PID_TypeDef* HT_PIDn, PID_Mode_Enum PID_Mode)
299 {
300   HT_PIDPARA_TypeDef * gPID_ModePara = &(HT_PIDn->SPD);
301 
302   /* Check the parameters                                                                                   */
303   Assert_Param(IS_PID(HT_PIDn));
304   Assert_Param(IS_PID_MODE(PID_Mode));
305 
306   return (gPID_ModePara + PID_Mode)->LEIR;
307 }
308 
309 /*********************************************************************************************************//**
310  * @brief Return the PID last integral function value (UIn_1) for the selected PID mode.
311  * @param HT_PIDn: where HT_PIDn is the selected PID from the PID peripherals.
312  * @param PID_Mode: where PID_Mode is the selected PID mode.
313  *   This parameter can be one of the following values:
314  *     @arg PID_SPD_MODE
315  *     @arg PID_IQ_MODE
316  *     @arg PID_ID_MODE
317  *     @arg PID_FWNK_MODE
318  *     @arg PID_PLL_MODE
319  *     @arg PID_USR_MODE
320  * @retval The Value of UIn_1 for the selected PID mode.
321  ************************************************************************************************************/
PID_GetUIn_1(HT_PID_TypeDef * HT_PIDn,PID_Mode_Enum PID_Mode)322 s32  PID_GetUIn_1(HT_PID_TypeDef* HT_PIDn, PID_Mode_Enum PID_Mode)
323 {
324   HT_PIDPARA_TypeDef * gPID_ModePara = &(HT_PIDn->SPD);
325 
326   /* Check the parameters                                                                                   */
327   Assert_Param(IS_PID(HT_PIDn));
328   Assert_Param(IS_PID_MODE(PID_Mode));
329   return   (gPID_ModePara + PID_Mode)->LIFVR;
330 }
331 
332 /**
333   * @}
334   */
335 
336 /**
337   * @}
338   */
339 
340 /**
341   * @}
342   */
343