1 /**
2   ******************************************************************************
3   * @file    tae32f53xx_ll_ecu.c
4   * @author  MCD Application Team
5   * @brief   ECU LL module driver
6   *
7   ******************************************************************************
8   * @attention
9   *
10   * <h2><center>&copy; Copyright (c) 2020 Tai-Action.
11   * All rights reserved.</center></h2>
12   *
13   * This software is licensed by Tai-Action under BSD 3-Clause license,
14   * the "License"; You may not use this file except in compliance with the
15   * License. You may obtain a copy of the License at:
16   *                        opensource.org/licenses/BSD-3-Clause
17   *
18   ******************************************************************************
19   */
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "tae32f53xx_ll.h"
23 
24 
25 #define DBG_TAG             "ECU LL"
26 #define DBG_LVL             DBG_ERROR
27 #include "dbg/tae32f53xx_dbg.h"
28 
29 
30 /** @addtogroup TAE32F53xx_LL_Driver
31   * @{
32   */
33 
34 /** @defgroup ECU_LL ECU LL
35   * @brief    ECU LL module driver
36   * @{
37   */
38 
39 #ifdef LL_ECU_MODULE_ENABLED
40 
41 /* Private typedef -----------------------------------------------------------*/
42 /* Private define ------------------------------------------------------------*/
43 /* Private macro -------------------------------------------------------------*/
44 /* Private variables ---------------------------------------------------------*/
45 /* Private function prototypes -----------------------------------------------*/
46 /* Exported functions --------------------------------------------------------*/
47 /** @defgroup ECU_LL_Exported_Functions ECU LL Exported Functions
48   * @brief    ECU LL Exported Functions
49   * @{
50   */
51 
52 /** @defgroup ECU_LL_Exported_Functions_Group1 Initialization and de-initialization functions
53  *  @brief    Initialization and Configuration functions
54  *
55 @verbatim
56  ===============================================================================
57               ##### Initialization and de-initialization functions #####
58  ===============================================================================
59   [..]
60     This section provides functions allowing to initialize and de-initialize the ECU
61     to be ready for use.
62 
63 @endverbatim
64   * @{
65   */
66 
67 /**
68   * @brief  Initializes the ECU peripheral according to the specified parameters in the ECU_Init.
69   * @param  ECU_Init: pointer to a ECU_InitTypeDef structure that contains
70   *         the configuration information for the specified ECU peripheral.
71   * @return status of the initialization
72   */
LL_ECU_Init(ECU_TypeDef * Instance,ECU_InitTypeDef * ECU_Init)73 LL_StatusETypeDef LL_ECU_Init(ECU_TypeDef *Instance, ECU_InitTypeDef *ECU_Init)
74 {
75     /*hardware level Initializes*/
76     LL_ECU_MspInit(Instance);
77     /*check paramter*/
78     assert_param(IS_ECU_MODULE(ECU_Init->ModuleEnable));
79     assert_param(IS_ECU_AVERAGE(ECU_Init->AverageSel));
80     assert_param(IS_ECU_ACSFT(ECU_Init->ACLeftShift));
81     assert_param(IS_ECU_APSFT(ECU_Init->APRightShift));
82     assert_param(IS_ECU_DATSEL(ECU_Init->DataSel));
83     assert_param(IS_ECU_CRSSEL(ECU_Init->CrossZeroSel));
84 
85     if (ECU_Init->ModuleEnable == ENABLE) {
86         /*ECU control register, enable mdule */
87         if (ECU_Init->InterruptEn == ENABLE) {
88             SET_BIT(Instance->CON, ECU_CON_ENABLE | ECU_CON_INTEN);
89         } else {
90             SET_BIT(Instance->CON, ECU_CON_ENABLE);
91         }
92 
93         /*Configure averages the data results*/
94         /*Configure The right shift number of power results and
95          *the left shift number of active power results are configured.*/
96         MODIFY_REG(Instance->CON,
97                    ECU_CON_AVGSEL
98                    | ECU_CON_APSFT
99                    | ECU_CON_ACSFT,
100                    ECU_Init->AverageSel
101                    | ECU_Init->APRightShift
102                    | ECU_Init->ACLeftShift
103                   );
104         /*Configure the corresponding event selection bits*/
105         MODIFY_REG(Instance->PRC,
106                    ECU_PRC_CRSSEL
107                    | ECU_PRC_ADRSEL
108                    | ECU_PRC_DATSEL,
109                    ECU_Init->CrossZeroSel
110                    | ECU_Init->AddressSel
111                    | ECU_Init->DataSel
112                   );
113         /*Configure cache voltage and current data storage addresses and offset addresses*/
114         /*Configure voltage start address*/
115         MODIFY_REG(Instance->V_ADDR1, ECU_V_ADDR1_STADDR, ECU_Init->VStartAddr);
116         /*Configure voltage offset address*/
117         MODIFY_REG(Instance->V_ADDR2, ECU_V_ADDR2_OFADDR, ECU_Init->VOffsetAddr);
118         /*Configure current start address*/
119         MODIFY_REG(Instance->I_ADDR1, ECU_I_ADDR1_STADDR, ECU_Init->IStartAddr);
120         /*Configure current offset address*/
121         MODIFY_REG(Instance->I_ADDR2, ECU_I_ADDR2_OFADDR, ECU_Init->IOffsetAddr);
122 
123         return LL_OK;
124     } else {
125         return LL_ERROR;
126     }
127 }
128 
129 /**
130   * @brief  De-initializes the ECU peripheral registers to their default reset values.
131   * @return status of the de-initialization
132   */
LL_ECU_DeInit(ECU_TypeDef * Instance)133 LL_StatusETypeDef LL_ECU_DeInit(ECU_TypeDef *Instance)
134 {
135     /*Get the current working status of the ECU*/
136     if (__LL_ECU_GET_STA(Instance)) {
137         /*Disable the module*/
138         __LL_ECU_MODULE_DISABLE(Instance);
139     }
140 
141     /*Reset configuration register,Sets the value of the register to restore the default value */
142     MODIFY_REG(Instance->CON,
143                ECU_CON_INTEN | ECU_CON_INT | ECU_CON_AVGSEL | ECU_CON_APSFT | ECU_CON_ACSFT,
144                (0x8U << ECU_CON_APSFT_Pos) | (0x8U << ECU_CON_ACSFT_Pos)
145               );
146     /*Reset the event selection register*/
147     CLEAR_BIT(Instance->PRC, ECU_PRC_DATSEL | ECU_PRC_ADRSEL | ECU_PRC_CRSSEL);
148     /*Clears the input square root data*/
149     CLEAR_REG(Instance->SQRT_IN);
150     /*Clear the starting address of the voltage buffer*/
151     CLEAR_BIT(Instance->V_ADDR1, ECU_V_ADDR1_STADDR);
152     /*Clear the offset address of the voltage buffer*/
153     CLEAR_BIT(Instance->V_ADDR2, ECU_V_ADDR2_OFADDR);
154     /*Clear the starting address of the current buffer*/
155     CLEAR_BIT(Instance->I_ADDR1, ECU_I_ADDR1_STADDR);
156     /*Clear the offset address of the current buffer*/
157     CLEAR_BIT(Instance->I_ADDR2, ECU_I_ADDR2_OFADDR);
158 
159     return LL_OK;
160 }
161 
162 /**
163   * @brief  Initializes the ECU MSP.
164   * @param  ECU ECU instance
165   * @return None
166   */
LL_ECU_MspInit(ECU_TypeDef * Instance)167 __WEAK void LL_ECU_MspInit(ECU_TypeDef *Instance)
168 {
169     /* Prevent unused argument(s) compilation warning */
170     LL_UNUSED(Instance);
171     /* NOTE: This function should not be modified, when the callback is needed,
172              the LL_ECU_MspInit could be implemented in the user file
173      */
174 }
175 
176 /**
177   * @brief  DeInitializes the ECU MSP
178   * @param  ECU ECU instance
179   * @return None
180   */
LL_ECU_MspDeInit(ECU_TypeDef * Instance)181 __WEAK void LL_ECU_MspDeInit(ECU_TypeDef *Instance)
182 {
183     /* Prevent unused argument(s) compilation warning */
184     LL_UNUSED(Instance);
185     /* NOTE: This function should not be modified, when the callback is needed,
186              the LL_ECU_MspDeInit could be implemented in the user file
187      */
188 }
189 /**
190   * @}
191   */
192 
193 
194 /** @defgroup ECU_LL_Exported_Functions_Group2 ECU Read calulate result operation functions
195  *  @brief    ECU Read calulate result operation functions
196  *
197 @verbatim
198  ===============================================================================
199                        ##### Read result value functions #####
200  ===============================================================================
201   [..]
202     This subsection provides a set of functions allowing to manage the ECUs:
203     (+) Read the ECU SQRT_OUT value
204     (+) Write the ECU SQRT_IN value
205   @note For the above operation of reading registers, please determine whether the mark of completion of calculation
206         is set before reading, or try to read data in the interrupt
207 @endverbatim
208   * @{
209   */
210 
211 /**
212   * @brief  Set the data input that you want to take the square root of
213   * @param  __VALUE__ input data
214   * @return None
215   */
LL_ECU_WriteSqrtInData(ECU_TypeDef * Instance,uint32_t SqrtValue)216 void LL_ECU_WriteSqrtInData(ECU_TypeDef *Instance, uint32_t SqrtValue)
217 {
218     /*Check whether the last square root calculation was completed.*/
219     if (__LL_ECU_SQRT_DONE_FLAG(Instance)) {
220         /*If not, wait for the flag to clear*/
221         while (__LL_ECU_SQRT_DONE_FLAG(Instance));
222     }
223 
224     /*If complete, write the data you need to take the square root of*/
225     MODIFY_REG(Instance->SQRT_IN, ECU_SQRT_INDATA, SqrtValue);
226     /*Square root enable*/
227     __LL_ECU_SQRT_ENABLE(Instance);
228 }
229 
230 /**
231   * @brief  Get the result of square root data
232   * @param  Instance ECU instance
233   * @return The result of the square root of the input data
234   */
LL_ECU_ReadSqrtOutData(ECU_TypeDef * Instance)235 uint32_t LL_ECU_ReadSqrtOutData(ECU_TypeDef *Instance)
236 {
237     /*Check whether the last square root calculation was completed.*/
238     if (__LL_ECU_SQRT_DONE_FLAG(Instance)) {
239         /*If not, wait for the flag to clear*/
240         while (__LL_ECU_SQRT_DONE_FLAG(Instance));
241     }
242 
243     /*If completed, the result of the operation is read*/
244     return (uint32_t)READ_REG(Instance->SQRT_OUT);
245 }
246 /**
247   * @}
248   */
249 
250 
251 /** @defgroup ECU_LL_Exported_Functions_Interrupt ECU IRQ handler management
252   * @brief    ECU IRQ handler management
253   *
254 @verbatim
255   ==============================================================================
256                         ##### IRQ handler management #####
257   ==============================================================================
258   [..]
259     This section provides ECU IRQ handler function.
260 
261 @endverbatim
262   * @{
263   */
264 
265 /**
266   * @brief  This function handles ECU interrupts requests.
267   * @param  ECU ECU instance
268   * @return None
269   */
LL_ECU_IRQHandler(ECU_TypeDef * Instance)270 void LL_ECU_IRQHandler(ECU_TypeDef *Instance)
271 {
272     if (__LL_ECU_DONE_GET_IT(Instance) && __LL_ECU_DONE_GET_FLAG(Instance)) {
273         __LL_ECU_DONE_CLEAR_FLAG(Instance);
274 
275         /*Handle something*/
276         LL_ECU_CalDoneCallback(Instance);
277     }
278 }
279 
280 /**
281   * @brief  ECU parameters calculate completed detection callback.
282   * @return None
283   */
LL_ECU_CalDoneCallback(ECU_TypeDef * Instance)284 __WEAK void LL_ECU_CalDoneCallback(ECU_TypeDef *Instance)
285 {
286     /* Prevent unused argument(s) compilation warning */
287     LL_UNUSED(Instance);
288     /* NOTE: This function should not be modified, when the callback is needed,
289              the LL_ECU_CalDoneCallback could be implemented in the user file
290      */
291 }
292 
293 /**
294   * @}
295   */
296 
297 /**
298   * @}
299   */
300 
301 
302 /* Private functions ---------------------------------------------------------*/
303 
304 
305 #endif /* LL_ECU_MODULE_ENABLE */
306 
307 
308 /**
309   * @}
310   */
311 
312 /**
313   * @}
314   */
315 
316 
317 /************************* (C) COPYRIGHT Tai-Action *****END OF FILE***********/
318 
319