1 /**
2   ******************************************************************************
3   * @file    stm32h5xx_hal_cordic.c
4   * @author  MCD Application Team
5   * @brief   CORDIC HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the CORDIC peripheral:
8   *           + Initialization and de-initialization functions
9   *           + Peripheral Control functions
10   *           + Callback functions
11   *           + IRQ handler management
12   *           + Peripheral State functions
13   *
14   ******************************************************************************
15   * @attention
16   *
17   * Copyright (c) 2022 STMicroelectronics.
18   * All rights reserved.
19   *
20   * This software is licensed under terms that can be found in the LICENSE file
21   * in the root directory of this software component.
22   * If no LICENSE file comes with this software, it is provided AS-IS.
23   *
24   ******************************************************************************
25   @verbatim
26   ================================================================================
27             ##### How to use this driver #####
28   ================================================================================
29     [..]
30       The CORDIC HAL driver can be used as follows:
31 
32       (#) Initialize the CORDIC low level resources by implementing the HAL_CORDIC_MspInit():
33          (++) Enable the CORDIC interface clock using __HAL_RCC_CORDIC_CLK_ENABLE()
34          (++) In case of using interrupts (e.g. HAL_CORDIC_Calculate_IT())
35              (+++) Configure the CORDIC interrupt priority using HAL_NVIC_SetPriority()
36              (+++) Enable the CORDIC IRQ handler using HAL_NVIC_EnableIRQ()
37              (+++) In CORDIC IRQ handler, call HAL_CORDIC_IRQHandler()
38          (++) In case of using DMA to control data transfer (e.g. HAL_CORDIC_Calculate_DMA())
39              (+++) Enable the DMA2 interface clock using
40                  __HAL_RCC_DMA2_CLK_ENABLE()
41              (+++) Configure and enable two DMA channels one for managing data transfer from
42                  memory to peripheral (input channel) and another channel for managing data
43                  transfer from peripheral to memory (output channel)
44              (+++) Associate the initialized DMA handle to the CORDIC DMA handle
45                  using __HAL_LINKDMA()
46              (+++) Configure the priority and enable the NVIC for the transfer complete
47                  interrupt on the two DMA channels.
48                  Resort to HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
49 
50       (#) Initialize the CORDIC HAL using HAL_CORDIC_Init(). This function
51          (++) resorts to HAL_CORDIC_MspInit() for low-level initialization,
52 
53       (#) Configure CORDIC processing (calculation) using HAL_CORDIC_Configure().
54           This function configures:
55          (++) Processing functions: Cosine, Sine, Phase, Modulus, Arctangent,
56               Hyperbolic cosine, Hyperbolic sine, Hyperbolic arctangent,
57               Natural log, Square root
58          (++) Scaling factor: 1 to 2exp(-7)
59          (++) Width of input data: 32 bits input data size (Q1.31 format) or 16 bits
60               input data size (Q1.15 format)
61          (++) Width of output data: 32 bits output data size (Q1.31 format) or 16 bits
62               output data size (Q1.15 format)
63          (++) Number of 32-bit write expected for one calculation: One 32-bits write
64               or Two 32-bit write
65          (++) Number of 32-bit read expected after one calculation: One 32-bits read
66               or Two 32-bit read
67          (++) Precision: 1 to 15 cycles for calculation (the more cycles, the better precision)
68 
69       (#) Four processing (calculation) functions are available:
70          (++) Polling mode: processing API is blocking function
71               i.e. it processes the data and wait till the processing is finished
72               API is HAL_CORDIC_Calculate
73          (++) Polling Zero-overhead mode: processing API is blocking function
74               i.e. it processes the data and wait till the processing is finished
75               A bit faster than standard polling mode, but blocking also AHB bus
76               API is HAL_CORDIC_CalculateZO
77          (++) Interrupt mode: processing API is not blocking functions
78               i.e. it processes the data under interrupt
79               API is HAL_CORDIC_Calculate_IT
80          (++) DMA mode: processing API is not blocking functions and the CPU is
81               not used for data transfer,
82               i.e. the data transfer is ensured by DMA
83               API is HAL_CORDIC_Calculate_DMA
84 
85       (#) Call HAL_CORDIC_DeInit() to de-initialize the CORDIC peripheral. This function
86          (++) resorts to HAL_CORDIC_MspDeInit() for low-level de-initialization,
87 
88   *** Callback registration ***
89   =============================================
90 
91   The compilation define  USE_HAL_CORDIC_REGISTER_CALLBACKS when set to 1
92   allows the user to configure dynamically the driver callbacks.
93   Use Function HAL_CORDIC_RegisterCallback() to register an interrupt callback.
94 
95   Function HAL_CORDIC_RegisterCallback() allows to register following callbacks:
96     (+) ErrorCallback             : Error Callback.
97     (+) CalculateCpltCallback     : Calculate complete Callback.
98     (+) MspInitCallback           : CORDIC MspInit.
99     (+) MspDeInitCallback         : CORDIC MspDeInit.
100   This function takes as parameters the HAL peripheral handle, the Callback ID
101   and a pointer to the user callback function.
102 
103   Use function HAL_CORDIC_UnRegisterCallback() to reset a callback to the default
104   weak function.
105   HAL_CORDIC_UnRegisterCallback takes as parameters the HAL peripheral handle,
106   and the Callback ID.
107   This function allows to reset following callbacks:
108     (+) ErrorCallback             : Error Callback.
109     (+) CalculateCpltCallback     : Calculate complete Callback.
110     (+) MspInitCallback           : CORDIC MspInit.
111     (+) MspDeInitCallback         : CORDIC MspDeInit.
112 
113   By default, after the HAL_CORDIC_Init() and when the state is HAL_CORDIC_STATE_RESET,
114   all callbacks are set to the corresponding weak functions:
115   examples HAL_CORDIC_ErrorCallback(), HAL_CORDIC_CalculateCpltCallback().
116   Exception done for MspInit and MspDeInit functions that are
117   reset to the legacy weak function in the HAL_CORDIC_Init()/ HAL_CORDIC_DeInit() only when
118   these callbacks are null (not registered beforehand).
119   if not, MspInit or MspDeInit are not null, the HAL_CORDIC_Init()/ HAL_CORDIC_DeInit()
120   keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
121 
122   Callbacks can be registered/unregistered in HAL_CORDIC_STATE_READY state only.
123   Exception done MspInit/MspDeInit that can be registered/unregistered
124   in HAL_CORDIC_STATE_READY or HAL_CORDIC_STATE_RESET state,
125   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
126   In that case first register the MspInit/MspDeInit user callbacks
127   using HAL_CORDIC_RegisterCallback() before calling HAL_CORDIC_DeInit()
128   or HAL_CORDIC_Init() function.
129 
130   When The compilation define USE_HAL_CORDIC_REGISTER_CALLBACKS is set to 0 or
131   not defined, the callback registration feature is not available and all callbacks
132   are set to the corresponding weak functions.
133 
134   @endverbatim
135   ******************************************************************************
136   */
137 
138 /* Includes ------------------------------------------------------------------*/
139 #include "stm32h5xx_hal.h"
140 
141 #if defined(CORDIC)
142 #ifdef HAL_CORDIC_MODULE_ENABLED
143 
144 /** @addtogroup STM32H5xx_HAL_Driver
145   * @{
146   */
147 
148 /** @defgroup CORDIC CORDIC
149   * @brief CORDIC HAL driver modules.
150   * @{
151   */
152 
153 /* Private typedef -----------------------------------------------------------*/
154 /* Private define ------------------------------------------------------------*/
155 /* Private macro -------------------------------------------------------------*/
156 /* Private variables ---------------------------------------------------------*/
157 /* Private function prototypes -----------------------------------------------*/
158 
159 /** @defgroup CORDIC_Private_Functions CORDIC Private Functions
160   * @{
161   */
162 static void CORDIC_WriteInDataIncrementPtr(const CORDIC_HandleTypeDef *hcordic, const int32_t **ppInBuff);
163 static void CORDIC_ReadOutDataIncrementPtr(const CORDIC_HandleTypeDef *hcordic, int32_t **ppOutBuff);
164 static void CORDIC_DMAInCplt(DMA_HandleTypeDef *hdma);
165 static void CORDIC_DMAOutCplt(DMA_HandleTypeDef *hdma);
166 static void CORDIC_DMAError(DMA_HandleTypeDef *hdma);
167 /**
168   * @}
169   */
170 
171 /* Exported functions --------------------------------------------------------*/
172 
173 /** @defgroup CORDIC_Exported_Functions CORDIC Exported Functions
174   * @{
175   */
176 
177 /** @defgroup CORDIC_Exported_Functions_Group1 Initialization and de-initialization functions
178   *  @brief    Initialization and Configuration functions.
179   *
180 @verbatim
181   ==============================================================================
182               ##### Initialization and de-initialization functions #####
183   ==============================================================================
184     [..]  This section provides functions allowing to:
185       (+) Initialize the CORDIC peripheral and the associated handle
186       (+) DeInitialize the CORDIC peripheral
187       (+) Initialize the CORDIC MSP (MCU Specific Package)
188       (+) De-Initialize the CORDIC MSP
189 
190     [..]
191 
192 @endverbatim
193   * @{
194   */
195 
196 /**
197   * @brief  Initialize the CORDIC peripheral and the associated handle.
198   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure.
199   * @retval HAL status
200   */
HAL_CORDIC_Init(CORDIC_HandleTypeDef * hcordic)201 HAL_StatusTypeDef HAL_CORDIC_Init(CORDIC_HandleTypeDef *hcordic)
202 {
203   /* Check the CORDIC handle allocation */
204   if (hcordic == NULL)
205   {
206     /* Return error status */
207     return HAL_ERROR;
208   }
209 
210   /* Check the instance */
211   assert_param(IS_CORDIC_ALL_INSTANCE(hcordic->Instance));
212 
213 #if USE_HAL_CORDIC_REGISTER_CALLBACKS == 1
214   if (hcordic->State == HAL_CORDIC_STATE_RESET)
215   {
216     /* Allocate lock resource and initialize it */
217     hcordic->Lock = HAL_UNLOCKED;
218 
219     /* Reset callbacks to legacy functions */
220     hcordic->ErrorCallback         = HAL_CORDIC_ErrorCallback;         /* Legacy weak ErrorCallback */
221     hcordic->CalculateCpltCallback = HAL_CORDIC_CalculateCpltCallback; /* Legacy weak CalculateCpltCallback */
222 
223     if (hcordic->MspInitCallback == NULL)
224     {
225       hcordic->MspInitCallback = HAL_CORDIC_MspInit;                   /* Legacy weak MspInit */
226     }
227 
228     /* Initialize the low level hardware */
229     hcordic->MspInitCallback(hcordic);
230   }
231 #else
232   if (hcordic->State == HAL_CORDIC_STATE_RESET)
233   {
234     /* Allocate lock resource and initialize it */
235     hcordic->Lock = HAL_UNLOCKED;
236 
237     /* Initialize the low level hardware */
238     HAL_CORDIC_MspInit(hcordic);
239   }
240 #endif /* (USE_HAL_CORDIC_REGISTER_CALLBACKS) */
241 
242   /* Set CORDIC error code to none */
243   hcordic->ErrorCode = HAL_CORDIC_ERROR_NONE;
244 
245   /* Reset pInBuff and pOutBuff */
246   hcordic->pInBuff = NULL;
247   hcordic->pOutBuff = NULL;
248 
249   /* Reset NbCalcToOrder and NbCalcToGet */
250   hcordic->NbCalcToOrder = 0U;
251   hcordic->NbCalcToGet = 0U;
252 
253   /* Reset DMADirection */
254   hcordic->DMADirection = CORDIC_DMA_DIR_NONE;
255 
256   /* Change CORDIC peripheral state */
257   hcordic->State = HAL_CORDIC_STATE_READY;
258 
259   /* Return function status */
260   return HAL_OK;
261 }
262 
263 /**
264   * @brief  DeInitialize the CORDIC peripheral.
265   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure.
266   * @retval HAL status
267   */
HAL_CORDIC_DeInit(CORDIC_HandleTypeDef * hcordic)268 HAL_StatusTypeDef HAL_CORDIC_DeInit(CORDIC_HandleTypeDef *hcordic)
269 {
270   /* Check the CORDIC handle allocation */
271   if (hcordic == NULL)
272   {
273     /* Return error status */
274     return HAL_ERROR;
275   }
276 
277   /* Check the parameters */
278   assert_param(IS_CORDIC_ALL_INSTANCE(hcordic->Instance));
279 
280   /* Change CORDIC peripheral state */
281   hcordic->State = HAL_CORDIC_STATE_BUSY;
282 
283 #if USE_HAL_CORDIC_REGISTER_CALLBACKS == 1
284   if (hcordic->MspDeInitCallback == NULL)
285   {
286     hcordic->MspDeInitCallback = HAL_CORDIC_MspDeInit;
287   }
288 
289   /* De-Initialize the low level hardware */
290   hcordic->MspDeInitCallback(hcordic);
291 #else
292   /* De-Initialize the low level hardware: CLOCK, NVIC, DMA */
293   HAL_CORDIC_MspDeInit(hcordic);
294 #endif /* USE_HAL_CORDIC_REGISTER_CALLBACKS */
295 
296   /* Set CORDIC error code to none */
297   hcordic->ErrorCode = HAL_CORDIC_ERROR_NONE;
298 
299   /* Reset pInBuff and pOutBuff */
300   hcordic->pInBuff = NULL;
301   hcordic->pOutBuff = NULL;
302 
303   /* Reset NbCalcToOrder and NbCalcToGet */
304   hcordic->NbCalcToOrder = 0U;
305   hcordic->NbCalcToGet = 0U;
306 
307   /* Reset DMADirection */
308   hcordic->DMADirection = CORDIC_DMA_DIR_NONE;
309 
310   /* Change CORDIC peripheral state */
311   hcordic->State = HAL_CORDIC_STATE_RESET;
312 
313   /* Reset Lock */
314   hcordic->Lock = HAL_UNLOCKED;
315 
316   /* Return function status */
317   return HAL_OK;
318 }
319 
320 /**
321   * @brief  Initialize the CORDIC MSP.
322   * @param  hcordic CORDIC handle
323   * @retval None
324   */
HAL_CORDIC_MspInit(CORDIC_HandleTypeDef * hcordic)325 __weak void HAL_CORDIC_MspInit(CORDIC_HandleTypeDef *hcordic)
326 {
327   /* Prevent unused argument(s) compilation warning */
328   UNUSED(hcordic);
329 
330   /* NOTE : This function should not be modified, when the callback is needed,
331             the HAL_CORDIC_MspInit can be implemented in the user file
332    */
333 }
334 
335 /**
336   * @brief  DeInitialize the CORDIC MSP.
337   * @param  hcordic CORDIC handle
338   * @retval None
339   */
HAL_CORDIC_MspDeInit(CORDIC_HandleTypeDef * hcordic)340 __weak void HAL_CORDIC_MspDeInit(CORDIC_HandleTypeDef *hcordic)
341 {
342   /* Prevent unused argument(s) compilation warning */
343   UNUSED(hcordic);
344 
345   /* NOTE : This function should not be modified, when the callback is needed,
346             the HAL_CORDIC_MspDeInit can be implemented in the user file
347    */
348 }
349 
350 #if USE_HAL_CORDIC_REGISTER_CALLBACKS == 1
351 /**
352   * @brief  Register a CORDIC CallBack.
353   *         To be used instead of the weak predefined callback.
354   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
355   *         the configuration information for CORDIC module
356   * @param  CallbackID ID of the callback to be registered
357   *         This parameter can be one of the following values:
358   *           @arg @ref HAL_CORDIC_ERROR_CB_ID error Callback ID
359   *           @arg @ref HAL_CORDIC_CALCULATE_CPLT_CB_ID calculate complete Callback ID
360   *           @arg @ref HAL_CORDIC_MSPINIT_CB_ID MspInit callback ID
361   *           @arg @ref HAL_CORDIC_MSPDEINIT_CB_ID MspDeInit callback ID
362   * @param  pCallback pointer to the Callback function
363   * @retval HAL status
364   */
HAL_CORDIC_RegisterCallback(CORDIC_HandleTypeDef * hcordic,HAL_CORDIC_CallbackIDTypeDef CallbackID,void (* pCallback)(CORDIC_HandleTypeDef * _hcordic))365 HAL_StatusTypeDef HAL_CORDIC_RegisterCallback(CORDIC_HandleTypeDef *hcordic, HAL_CORDIC_CallbackIDTypeDef CallbackID,
366                                               void (* pCallback)(CORDIC_HandleTypeDef *_hcordic))
367 {
368   HAL_StatusTypeDef status = HAL_OK;
369 
370   if (pCallback == NULL)
371   {
372     /* Update the error code */
373     hcordic->ErrorCode |= HAL_CORDIC_ERROR_INVALID_CALLBACK;
374 
375     /* Return error status */
376     return HAL_ERROR;
377   }
378 
379   if (hcordic->State == HAL_CORDIC_STATE_READY)
380   {
381     switch (CallbackID)
382     {
383       case HAL_CORDIC_ERROR_CB_ID :
384         hcordic->ErrorCallback = pCallback;
385         break;
386 
387       case HAL_CORDIC_CALCULATE_CPLT_CB_ID :
388         hcordic->CalculateCpltCallback = pCallback;
389         break;
390 
391       case HAL_CORDIC_MSPINIT_CB_ID :
392         hcordic->MspInitCallback = pCallback;
393         break;
394 
395       case HAL_CORDIC_MSPDEINIT_CB_ID :
396         hcordic->MspDeInitCallback = pCallback;
397         break;
398 
399       default :
400         /* Update the error code */
401         hcordic->ErrorCode |= HAL_CORDIC_ERROR_INVALID_CALLBACK;
402 
403         /* Return error status */
404         status = HAL_ERROR;
405         break;
406     }
407   }
408   else if (hcordic->State == HAL_CORDIC_STATE_RESET)
409   {
410     switch (CallbackID)
411     {
412       case HAL_CORDIC_MSPINIT_CB_ID :
413         hcordic->MspInitCallback = pCallback;
414         break;
415 
416       case HAL_CORDIC_MSPDEINIT_CB_ID :
417         hcordic->MspDeInitCallback = pCallback;
418         break;
419 
420       default :
421         /* Update the error code */
422         hcordic->ErrorCode |= HAL_CORDIC_ERROR_INVALID_CALLBACK;
423 
424         /* Return error status */
425         status = HAL_ERROR;
426         break;
427     }
428   }
429   else
430   {
431     /* Update the error code */
432     hcordic->ErrorCode |= HAL_CORDIC_ERROR_INVALID_CALLBACK;
433 
434     /* Return error status */
435     status = HAL_ERROR;
436   }
437 
438   return status;
439 }
440 #endif /* USE_HAL_CORDIC_REGISTER_CALLBACKS */
441 
442 #if USE_HAL_CORDIC_REGISTER_CALLBACKS == 1
443 /**
444   * @brief  Unregister a CORDIC CallBack.
445   *         CORDIC callback is redirected to the weak predefined callback.
446   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
447   *         the configuration information for CORDIC module
448   * @param  CallbackID ID of the callback to be unregistered
449   *         This parameter can be one of the following values:
450   *           @arg @ref HAL_CORDIC_ERROR_CB_ID error Callback ID
451   *           @arg @ref HAL_CORDIC_CALCULATE_CPLT_CB_ID calculate complete Callback ID
452   *           @arg @ref HAL_CORDIC_MSPINIT_CB_ID MspInit callback ID
453   *           @arg @ref HAL_CORDIC_MSPDEINIT_CB_ID MspDeInit callback ID
454   * @retval HAL status
455   */
HAL_CORDIC_UnRegisterCallback(CORDIC_HandleTypeDef * hcordic,HAL_CORDIC_CallbackIDTypeDef CallbackID)456 HAL_StatusTypeDef HAL_CORDIC_UnRegisterCallback(CORDIC_HandleTypeDef *hcordic, HAL_CORDIC_CallbackIDTypeDef CallbackID)
457 {
458   HAL_StatusTypeDef status = HAL_OK;
459 
460   if (hcordic->State == HAL_CORDIC_STATE_READY)
461   {
462     switch (CallbackID)
463     {
464       case HAL_CORDIC_ERROR_CB_ID :
465         hcordic->ErrorCallback = HAL_CORDIC_ErrorCallback;
466         break;
467 
468       case HAL_CORDIC_CALCULATE_CPLT_CB_ID :
469         hcordic->CalculateCpltCallback = HAL_CORDIC_CalculateCpltCallback;
470         break;
471 
472       case HAL_CORDIC_MSPINIT_CB_ID :
473         hcordic->MspInitCallback = HAL_CORDIC_MspInit;
474         break;
475 
476       case HAL_CORDIC_MSPDEINIT_CB_ID :
477         hcordic->MspDeInitCallback = HAL_CORDIC_MspDeInit;
478         break;
479 
480       default :
481         /* Update the error code */
482         hcordic->ErrorCode |= HAL_CORDIC_ERROR_INVALID_CALLBACK;
483 
484         /* Return error status */
485         status = HAL_ERROR;
486         break;
487     }
488   }
489   else if (hcordic->State == HAL_CORDIC_STATE_RESET)
490   {
491     switch (CallbackID)
492     {
493       case HAL_CORDIC_MSPINIT_CB_ID :
494         hcordic->MspInitCallback = HAL_CORDIC_MspInit;
495         break;
496 
497       case HAL_CORDIC_MSPDEINIT_CB_ID :
498         hcordic->MspDeInitCallback = HAL_CORDIC_MspDeInit;
499         break;
500 
501       default :
502         /* Update the error code */
503         hcordic->ErrorCode |= HAL_CORDIC_ERROR_INVALID_CALLBACK;
504 
505         /* Return error status */
506         status = HAL_ERROR;
507         break;
508     }
509   }
510   else
511   {
512     /* Update the error code */
513     hcordic->ErrorCode |= HAL_CORDIC_ERROR_INVALID_CALLBACK;
514 
515     /* Return error status */
516     status = HAL_ERROR;
517   }
518 
519   return status;
520 }
521 #endif /* USE_HAL_CORDIC_REGISTER_CALLBACKS */
522 
523 /**
524   * @}
525   */
526 
527 /** @defgroup CORDIC_Exported_Functions_Group2 Peripheral Control functions
528   *  @brief    Control functions.
529   *
530 @verbatim
531   ==============================================================================
532                       ##### Peripheral Control functions #####
533   ==============================================================================
534     [..]  This section provides functions allowing to:
535       (+) Configure the CORDIC peripheral: function, precision, scaling factor,
536           number of input data and output data, size of input data and output data.
537       (+) Calculate output data of CORDIC processing on input date, using the
538           existing CORDIC configuration
539     [..]  Four processing functions are available for calculation:
540       (+) Polling mode
541       (+) Polling mode, with Zero-Overhead register access
542       (+) Interrupt mode
543       (+) DMA mode
544 
545 @endverbatim
546   * @{
547   */
548 
549 /**
550   * @brief  Configure the CORDIC processing according to the specified
551             parameters in the CORDIC_ConfigTypeDef structure.
552   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
553   *         the configuration information for CORDIC module
554   * @param  sConfig pointer to a CORDIC_ConfigTypeDef structure that
555   *         contains the CORDIC configuration information.
556   * @retval HAL status
557   */
HAL_CORDIC_Configure(CORDIC_HandleTypeDef * hcordic,const CORDIC_ConfigTypeDef * sConfig)558 HAL_StatusTypeDef HAL_CORDIC_Configure(CORDIC_HandleTypeDef *hcordic, const CORDIC_ConfigTypeDef *sConfig)
559 {
560   HAL_StatusTypeDef status = HAL_OK;
561 
562   /* Check the parameters */
563   assert_param(IS_CORDIC_FUNCTION(sConfig->Function));
564   assert_param(IS_CORDIC_PRECISION(sConfig->Precision));
565   assert_param(IS_CORDIC_SCALE(sConfig->Scale));
566   assert_param(IS_CORDIC_NBWRITE(sConfig->NbWrite));
567   assert_param(IS_CORDIC_NBREAD(sConfig->NbRead));
568   assert_param(IS_CORDIC_INSIZE(sConfig->InSize));
569   assert_param(IS_CORDIC_OUTSIZE(sConfig->OutSize));
570 
571   /* Check handle state is ready */
572   if (hcordic->State == HAL_CORDIC_STATE_READY)
573   {
574     /* Apply all configuration parameters in CORDIC control register */
575     MODIFY_REG(hcordic->Instance->CSR,                                                         \
576                (CORDIC_CSR_FUNC | CORDIC_CSR_PRECISION | CORDIC_CSR_SCALE |                    \
577                 CORDIC_CSR_NARGS | CORDIC_CSR_NRES | CORDIC_CSR_ARGSIZE | CORDIC_CSR_RESSIZE), \
578                (sConfig->Function | sConfig->Precision | sConfig->Scale |                      \
579                 sConfig->NbWrite | sConfig->NbRead | sConfig->InSize | sConfig->OutSize));
580   }
581   else
582   {
583     /* Set CORDIC error code */
584     hcordic->ErrorCode |= HAL_CORDIC_ERROR_NOT_READY;
585 
586     /* Return error status */
587     status = HAL_ERROR;
588   }
589 
590   /* Return function status */
591   return status;
592 }
593 
594 /**
595   * @brief  Carry out data of CORDIC processing in polling mode,
596   *         according to the existing CORDIC configuration.
597   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
598   *         the configuration information for CORDIC module.
599   * @param  pInBuff Pointer to buffer containing input data for CORDIC processing.
600   * @param  pOutBuff Pointer to buffer where output data of CORDIC processing will be stored.
601   * @param  NbCalc Number of CORDIC calculation to process.
602   * @param  Timeout Specify Timeout value
603   * @retval HAL status
604   */
HAL_CORDIC_Calculate(CORDIC_HandleTypeDef * hcordic,const int32_t * pInBuff,int32_t * pOutBuff,uint32_t NbCalc,uint32_t Timeout)605 HAL_StatusTypeDef HAL_CORDIC_Calculate(CORDIC_HandleTypeDef *hcordic, const int32_t *pInBuff, int32_t *pOutBuff,
606                                        uint32_t NbCalc, uint32_t Timeout)
607 {
608   uint32_t tickstart;
609   uint32_t index;
610   const int32_t *p_tmp_in_buff = pInBuff;
611   int32_t *p_tmp_out_buff = pOutBuff;
612 
613   /* Check parameters setting */
614   if ((pInBuff == NULL) || (pOutBuff == NULL) || (NbCalc == 0U))
615   {
616     /* Update the error code */
617     hcordic->ErrorCode |= HAL_CORDIC_ERROR_PARAM;
618 
619     /* Return error status */
620     return HAL_ERROR;
621   }
622 
623   /* Check handle state is ready */
624   if (hcordic->State == HAL_CORDIC_STATE_READY)
625   {
626     /* Reset CORDIC error code */
627     hcordic->ErrorCode = HAL_CORDIC_ERROR_NONE;
628 
629     /* Change the CORDIC state */
630     hcordic->State = HAL_CORDIC_STATE_BUSY;
631 
632     /* Get tick */
633     tickstart = HAL_GetTick();
634 
635     /* Write of input data in Write Data register, and increment input buffer pointer */
636     CORDIC_WriteInDataIncrementPtr(hcordic, &p_tmp_in_buff);
637 
638     /* Calculation is started.
639        Provide next set of input data, until number of calculation is achieved */
640     for (index = (NbCalc - 1U); index > 0U; index--)
641     {
642       /* Write of input data in Write Data register, and increment input buffer pointer */
643       CORDIC_WriteInDataIncrementPtr(hcordic, &p_tmp_in_buff);
644 
645       /* Wait for RRDY flag to be raised */
646       do
647       {
648         /* Check for the Timeout */
649         if (Timeout != HAL_MAX_DELAY)
650         {
651           if ((HAL_GetTick() - tickstart) > Timeout)
652           {
653             /* Set CORDIC error code */
654             hcordic->ErrorCode = HAL_CORDIC_ERROR_TIMEOUT;
655 
656             /* Change the CORDIC state */
657             hcordic->State = HAL_CORDIC_STATE_READY;
658 
659             /* Return function status */
660             return HAL_ERROR;
661           }
662         }
663       } while (HAL_IS_BIT_CLR(hcordic->Instance->CSR, CORDIC_CSR_RRDY));
664 
665       /* Read output data from Read Data register, and increment output buffer pointer */
666       CORDIC_ReadOutDataIncrementPtr(hcordic, &p_tmp_out_buff);
667     }
668 
669     /* Read output data from Read Data register, and increment output buffer pointer */
670     CORDIC_ReadOutDataIncrementPtr(hcordic, &p_tmp_out_buff);
671 
672     /* Change the CORDIC state */
673     hcordic->State = HAL_CORDIC_STATE_READY;
674 
675     /* Return function status */
676     return HAL_OK;
677   }
678   else
679   {
680     /* Set CORDIC error code */
681     hcordic->ErrorCode |= HAL_CORDIC_ERROR_NOT_READY;
682 
683     /* Return function status */
684     return HAL_ERROR;
685   }
686 }
687 
688 /**
689   * @brief  Carry out data of CORDIC processing in Zero-Overhead mode (output data being read
690   *         soon as input data are written), according to the existing CORDIC configuration.
691   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
692   *         the configuration information for CORDIC module.
693   * @param  pInBuff Pointer to buffer containing input data for CORDIC processing.
694   * @param  pOutBuff Pointer to buffer where output data of CORDIC processing will be stored.
695   * @param  NbCalc Number of CORDIC calculation to process.
696   * @param  Timeout Specify Timeout value
697   * @retval HAL status
698   */
HAL_CORDIC_CalculateZO(CORDIC_HandleTypeDef * hcordic,const int32_t * pInBuff,int32_t * pOutBuff,uint32_t NbCalc,uint32_t Timeout)699 HAL_StatusTypeDef HAL_CORDIC_CalculateZO(CORDIC_HandleTypeDef *hcordic, const int32_t *pInBuff, int32_t *pOutBuff,
700                                          uint32_t NbCalc, uint32_t Timeout)
701 {
702   uint32_t tickstart;
703   uint32_t index;
704   const int32_t *p_tmp_in_buff = pInBuff;
705   int32_t *p_tmp_out_buff = pOutBuff;
706 
707   /* Check parameters setting */
708   if ((pInBuff == NULL) || (pOutBuff == NULL) || (NbCalc == 0U))
709   {
710     /* Update the error code */
711     hcordic->ErrorCode |= HAL_CORDIC_ERROR_PARAM;
712 
713     /* Return error status */
714     return HAL_ERROR;
715   }
716 
717   /* Check handle state is ready */
718   if (hcordic->State == HAL_CORDIC_STATE_READY)
719   {
720     /* Reset CORDIC error code */
721     hcordic->ErrorCode = HAL_CORDIC_ERROR_NONE;
722 
723     /* Change the CORDIC state */
724     hcordic->State = HAL_CORDIC_STATE_BUSY;
725 
726     /* Get tick */
727     tickstart = HAL_GetTick();
728 
729     /* Write of input data in Write Data register, and increment input buffer pointer */
730     CORDIC_WriteInDataIncrementPtr(hcordic, &p_tmp_in_buff);
731 
732     /* Calculation is started.
733        Provide next set of input data, until number of calculation is achieved */
734     for (index = (NbCalc - 1U); index > 0U; index--)
735     {
736       /* Write of input data in Write Data register, and increment input buffer pointer */
737       CORDIC_WriteInDataIncrementPtr(hcordic, &p_tmp_in_buff);
738 
739       /* Read output data from Read Data register, and increment output buffer pointer
740          The reading is performed in Zero-Overhead mode:
741          reading is ordered immediately without waiting result ready flag */
742       CORDIC_ReadOutDataIncrementPtr(hcordic, &p_tmp_out_buff);
743 
744       /* Check for the Timeout */
745       if (Timeout != HAL_MAX_DELAY)
746       {
747         if ((HAL_GetTick() - tickstart) > Timeout)
748         {
749           /* Set CORDIC error code */
750           hcordic->ErrorCode = HAL_CORDIC_ERROR_TIMEOUT;
751 
752           /* Change the CORDIC state */
753           hcordic->State = HAL_CORDIC_STATE_READY;
754 
755           /* Return function status */
756           return HAL_ERROR;
757         }
758       }
759     }
760 
761     /* Read output data from Read Data register, and increment output buffer pointer
762        The reading is performed in Zero-Overhead mode:
763        reading is ordered immediately without waiting result ready flag */
764     CORDIC_ReadOutDataIncrementPtr(hcordic, &p_tmp_out_buff);
765 
766     /* Change the CORDIC state */
767     hcordic->State = HAL_CORDIC_STATE_READY;
768 
769     /* Return function status */
770     return HAL_OK;
771   }
772   else
773   {
774     /* Set CORDIC error code */
775     hcordic->ErrorCode |= HAL_CORDIC_ERROR_NOT_READY;
776 
777     /* Return function status */
778     return HAL_ERROR;
779   }
780 }
781 
782 /**
783   * @brief  Carry out data of CORDIC processing in interrupt mode,
784   *         according to the existing CORDIC configuration.
785   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
786   *         the configuration information for CORDIC module.
787   * @param  pInBuff Pointer to buffer containing input data for CORDIC processing.
788   * @param  pOutBuff Pointer to buffer where output data of CORDIC processing will be stored.
789   * @param  NbCalc Number of CORDIC calculation to process.
790   * @retval HAL status
791   */
HAL_CORDIC_Calculate_IT(CORDIC_HandleTypeDef * hcordic,const int32_t * pInBuff,int32_t * pOutBuff,uint32_t NbCalc)792 HAL_StatusTypeDef HAL_CORDIC_Calculate_IT(CORDIC_HandleTypeDef *hcordic, const int32_t *pInBuff, int32_t *pOutBuff,
793                                           uint32_t NbCalc)
794 {
795   const int32_t *tmp_pInBuff = pInBuff;
796 
797   /* Check parameters setting */
798   if ((pInBuff == NULL) || (pOutBuff == NULL) || (NbCalc == 0U))
799   {
800     /* Update the error code */
801     hcordic->ErrorCode |= HAL_CORDIC_ERROR_PARAM;
802 
803     /* Return error status */
804     return HAL_ERROR;
805   }
806 
807   /* Check handle state is ready */
808   if (hcordic->State == HAL_CORDIC_STATE_READY)
809   {
810     /* Reset CORDIC error code */
811     hcordic->ErrorCode = HAL_CORDIC_ERROR_NONE;
812 
813     /* Change the CORDIC state */
814     hcordic->State = HAL_CORDIC_STATE_BUSY;
815 
816     /* Store the buffers addresses and number of calculations in handle,
817        provisioning initial write of input data that will be done */
818     if (HAL_IS_BIT_SET(hcordic->Instance->CSR, CORDIC_CSR_NARGS))
819     {
820       /* Two writes of input data are expected */
821       tmp_pInBuff++;
822       tmp_pInBuff++;
823     }
824     else
825     {
826       /* One write of input data is expected */
827       tmp_pInBuff++;
828     }
829     hcordic->pInBuff = tmp_pInBuff;
830     hcordic->pOutBuff = pOutBuff;
831     hcordic->NbCalcToOrder = NbCalc - 1U;
832     hcordic->NbCalcToGet = NbCalc;
833 
834     /* Enable Result Ready Interrupt */
835     __HAL_CORDIC_ENABLE_IT(hcordic, CORDIC_IT_IEN);
836 
837     /* Set back pointer to start of input data buffer */
838     tmp_pInBuff = pInBuff;
839 
840     /* Initiate the processing by providing input data
841        in the Write Data register */
842     WRITE_REG(hcordic->Instance->WDATA, (uint32_t)*tmp_pInBuff);
843 
844     /* Check if second write of input data is expected */
845     if (HAL_IS_BIT_SET(hcordic->Instance->CSR, CORDIC_CSR_NARGS))
846     {
847       /* Increment pointer to input data */
848       tmp_pInBuff++;
849 
850       /* Perform second write of input data */
851       WRITE_REG(hcordic->Instance->WDATA, (uint32_t)*tmp_pInBuff);
852     }
853 
854     /* Return function status */
855     return HAL_OK;
856   }
857   else
858   {
859     /* Set CORDIC error code */
860     hcordic->ErrorCode |= HAL_CORDIC_ERROR_NOT_READY;
861 
862     /* Return function status */
863     return HAL_ERROR;
864   }
865 }
866 
867 /**
868   * @brief  Carry out input and/or output data of CORDIC processing in DMA mode,
869   *         according to the existing CORDIC configuration.
870   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
871   *         the configuration information for CORDIC module.
872   * @param  pInBuff Pointer to buffer containing input data for CORDIC processing.
873   * @param  pOutBuff Pointer to buffer where output data of CORDIC processing will be stored.
874   * @param  NbCalc Number of CORDIC calculation to process.
875   * @param  DMADirection Direction of DMA transfers.
876   *         This parameter can be one of the following values:
877   *            @arg @ref CORDIC_DMA_Direction CORDIC DMA direction
878   * @note   pInBuff or pOutBuff is unused in case of unique DMADirection transfer, and can
879   *         be set to NULL value in this case.
880   * @note   pInBuff and pOutBuff buffers must be 32-bit aligned to ensure a correct
881   *         DMA transfer to and from the Peripheral.
882   * @retval HAL status
883   */
HAL_CORDIC_Calculate_DMA(CORDIC_HandleTypeDef * hcordic,const int32_t * pInBuff,int32_t * pOutBuff,uint32_t NbCalc,uint32_t DMADirection)884 HAL_StatusTypeDef HAL_CORDIC_Calculate_DMA(CORDIC_HandleTypeDef *hcordic, const int32_t *pInBuff, int32_t *pOutBuff,
885                                            uint32_t NbCalc, uint32_t DMADirection)
886 {
887   uint32_t sizeinbuff;
888   uint32_t sizeoutbuff;
889   uint32_t inputaddr;
890   uint32_t outputaddr;
891 
892   /* Check the parameters */
893   assert_param(IS_CORDIC_DMA_DIRECTION(DMADirection));
894 
895   /* Check parameters setting */
896   if (NbCalc == 0U)
897   {
898     /* Update the error code */
899     hcordic->ErrorCode |= HAL_CORDIC_ERROR_PARAM;
900 
901     /* Return error status */
902     return HAL_ERROR;
903   }
904 
905   /* Check if CORDIC DMA direction "Out" is requested */
906   if ((DMADirection == CORDIC_DMA_DIR_OUT) || (DMADirection == CORDIC_DMA_DIR_IN_OUT))
907   {
908     /* Check parameters setting */
909     if (pOutBuff == NULL)
910     {
911       /* Update the error code */
912       hcordic->ErrorCode |= HAL_CORDIC_ERROR_PARAM;
913 
914       /* Return error status */
915       return HAL_ERROR;
916     }
917   }
918 
919   /* Check if CORDIC DMA direction "In" is requested */
920   if ((DMADirection == CORDIC_DMA_DIR_IN) || (DMADirection == CORDIC_DMA_DIR_IN_OUT))
921   {
922     /* Check parameters setting */
923     if (pInBuff == NULL)
924     {
925       /* Update the error code */
926       hcordic->ErrorCode |= HAL_CORDIC_ERROR_PARAM;
927 
928       /* Return error status */
929       return HAL_ERROR;
930     }
931   }
932 
933   if (hcordic->State == HAL_CORDIC_STATE_READY)
934   {
935     /* Reset CORDIC error code */
936     hcordic->ErrorCode = HAL_CORDIC_ERROR_NONE;
937 
938     /* Change the CORDIC state */
939     hcordic->State = HAL_CORDIC_STATE_BUSY;
940 
941     /* Get DMA direction */
942     hcordic->DMADirection = DMADirection;
943 
944     /* Check if CORDIC DMA direction "Out" is requested */
945     if ((DMADirection == CORDIC_DMA_DIR_OUT) || (DMADirection == CORDIC_DMA_DIR_IN_OUT))
946     {
947       /* Set the CORDIC DMA transfer complete callback */
948       hcordic->hdmaOut->XferCpltCallback = CORDIC_DMAOutCplt;
949       /* Set the DMA error callback */
950       hcordic->hdmaOut->XferErrorCallback = CORDIC_DMAError;
951 
952       /* Check number of output data at each calculation,
953          to retrieve the size of output data buffer */
954       if (HAL_IS_BIT_SET(hcordic->Instance->CSR, CORDIC_CSR_NRES))
955       {
956         sizeoutbuff = 2U * NbCalc;
957       }
958       else
959       {
960         sizeoutbuff = NbCalc;
961       }
962 
963       /* Convert the output buffer size into corresponding number of bytes.
964          This is necessary as the DMA handles the data at byte-level. */
965       sizeoutbuff = 4U * sizeoutbuff;
966 
967       outputaddr = (uint32_t)pOutBuff;
968 
969       /* Enable the DMA stream managing CORDIC output data read */
970       if (HAL_DMA_Start_IT(hcordic->hdmaOut, (uint32_t)&hcordic->Instance->RDATA, outputaddr, sizeoutbuff) != HAL_OK)
971       {
972         /* Update the error code */
973         hcordic->ErrorCode |= HAL_CORDIC_ERROR_DMA;
974 
975         /* Return error status */
976         return HAL_ERROR;
977       }
978 
979       /* Enable output data Read DMA requests */
980       SET_BIT(hcordic->Instance->CSR, CORDIC_DMA_REN);
981     }
982 
983     /* Check if CORDIC DMA direction "In" is requested */
984     if ((DMADirection == CORDIC_DMA_DIR_IN) || (DMADirection == CORDIC_DMA_DIR_IN_OUT))
985     {
986       /* Set the CORDIC DMA transfer complete callback */
987       hcordic->hdmaIn->XferCpltCallback = CORDIC_DMAInCplt;
988       /* Set the DMA error callback */
989       hcordic->hdmaIn->XferErrorCallback = CORDIC_DMAError;
990 
991       /* Check number of input data expected for each calculation,
992          to retrieve the size of input data buffer */
993       if (HAL_IS_BIT_SET(hcordic->Instance->CSR, CORDIC_CSR_NARGS))
994       {
995         sizeinbuff = 2U * NbCalc;
996       }
997       else
998       {
999         sizeinbuff = NbCalc;
1000       }
1001 
1002       /* Convert the input buffer size into corresponding number of bytes.
1003          This is necessary as the DMA handles the data at byte-level. */
1004       sizeinbuff = 4U * sizeinbuff;
1005 
1006       inputaddr  = (uint32_t)pInBuff;
1007 
1008       /* Enable the DMA stream managing CORDIC input data write */
1009       if (HAL_DMA_Start_IT(hcordic->hdmaIn, inputaddr, (uint32_t)&hcordic->Instance->WDATA, sizeinbuff) != HAL_OK)
1010       {
1011         /* Update the error code */
1012         hcordic->ErrorCode |= HAL_CORDIC_ERROR_DMA;
1013 
1014         /* Return error status */
1015         return HAL_ERROR;
1016       }
1017 
1018       /* Enable input data Write DMA request */
1019       SET_BIT(hcordic->Instance->CSR, CORDIC_DMA_WEN);
1020     }
1021 
1022     /* Return function status */
1023     return HAL_OK;
1024   }
1025   else
1026   {
1027     /* Set CORDIC error code */
1028     hcordic->ErrorCode |= HAL_CORDIC_ERROR_NOT_READY;
1029 
1030     /* Return function status */
1031     return HAL_ERROR;
1032   }
1033 }
1034 
1035 /**
1036   * @}
1037   */
1038 
1039 /** @defgroup CORDIC_Exported_Functions_Group3 Callback functions
1040   *  @brief    Callback functions.
1041   *
1042 @verbatim
1043   ==============================================================================
1044                       ##### Callback functions  #####
1045   ==============================================================================
1046     [..]  This section provides Interruption and DMA callback functions:
1047       (+) DMA or Interrupt calculate complete
1048       (+) DMA or Interrupt error
1049 
1050 @endverbatim
1051   * @{
1052   */
1053 
1054 /**
1055   * @brief  CORDIC error callback.
1056   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
1057   *         the configuration information for CORDIC module
1058   * @retval None
1059   */
HAL_CORDIC_ErrorCallback(CORDIC_HandleTypeDef * hcordic)1060 __weak void HAL_CORDIC_ErrorCallback(CORDIC_HandleTypeDef *hcordic)
1061 {
1062   /* Prevent unused argument(s) compilation warning */
1063   UNUSED(hcordic);
1064 
1065   /* NOTE : This function should not be modified; when the callback is needed,
1066             the HAL_CORDIC_ErrorCallback can be implemented in the user file
1067    */
1068 }
1069 
1070 /**
1071   * @brief  CORDIC calculate complete callback.
1072   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
1073   *         the configuration information for CORDIC module
1074   * @retval None
1075   */
HAL_CORDIC_CalculateCpltCallback(CORDIC_HandleTypeDef * hcordic)1076 __weak void HAL_CORDIC_CalculateCpltCallback(CORDIC_HandleTypeDef *hcordic)
1077 {
1078   /* Prevent unused argument(s) compilation warning */
1079   UNUSED(hcordic);
1080 
1081   /* NOTE : This function should not be modified; when the callback is needed,
1082             the HAL_CORDIC_CalculateCpltCallback can be implemented in the user file
1083    */
1084 }
1085 
1086 /**
1087   * @}
1088   */
1089 
1090 /** @defgroup CORDIC_Exported_Functions_Group4 IRQ handler management
1091   *  @brief    IRQ handler.
1092   *
1093 @verbatim
1094   ==============================================================================
1095                 ##### IRQ handler management #####
1096   ==============================================================================
1097 [..]  This section provides IRQ handler function.
1098 
1099 @endverbatim
1100   * @{
1101   */
1102 
1103 /**
1104   * @brief  Handle CORDIC interrupt request.
1105   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
1106   *         the configuration information for CORDIC module
1107   * @retval None
1108   */
HAL_CORDIC_IRQHandler(CORDIC_HandleTypeDef * hcordic)1109 void HAL_CORDIC_IRQHandler(CORDIC_HandleTypeDef *hcordic)
1110 {
1111   /* Check if calculation complete interrupt is enabled and if result ready
1112      flag is raised */
1113   if (__HAL_CORDIC_GET_IT_SOURCE(hcordic, CORDIC_IT_IEN) != 0U)
1114   {
1115     if (__HAL_CORDIC_GET_FLAG(hcordic, CORDIC_FLAG_RRDY) != 0U)
1116     {
1117       /* Decrement number of calculations to get */
1118       hcordic->NbCalcToGet--;
1119 
1120       /* Read output data from Read Data register, and increment output buffer pointer */
1121       CORDIC_ReadOutDataIncrementPtr(hcordic, &(hcordic->pOutBuff));
1122 
1123       /* Check if calculations are still to be ordered */
1124       if (hcordic->NbCalcToOrder > 0U)
1125       {
1126         /* Decrement number of calculations to order */
1127         hcordic->NbCalcToOrder--;
1128 
1129         /* Continue the processing by providing another write of input data
1130            in the Write Data register, and increment input buffer pointer */
1131         CORDIC_WriteInDataIncrementPtr(hcordic, &(hcordic->pInBuff));
1132       }
1133 
1134       /* Check if all calculations results are got */
1135       if (hcordic->NbCalcToGet == 0U)
1136       {
1137         /* Disable Result Ready Interrupt */
1138         __HAL_CORDIC_DISABLE_IT(hcordic, CORDIC_IT_IEN);
1139 
1140         /* Change the CORDIC state */
1141         hcordic->State = HAL_CORDIC_STATE_READY;
1142 
1143         /* Call calculation complete callback */
1144 #if USE_HAL_CORDIC_REGISTER_CALLBACKS == 1
1145         /*Call registered callback*/
1146         hcordic->CalculateCpltCallback(hcordic);
1147 #else
1148         /*Call legacy weak (surcharged) callback*/
1149         HAL_CORDIC_CalculateCpltCallback(hcordic);
1150 #endif /* USE_HAL_CORDIC_REGISTER_CALLBACKS */
1151       }
1152     }
1153   }
1154 }
1155 
1156 /**
1157   * @}
1158   */
1159 
1160 /** @defgroup CORDIC_Exported_Functions_Group5 Peripheral State functions
1161   *  @brief   Peripheral State functions.
1162   *
1163 @verbatim
1164   ==============================================================================
1165                       ##### Peripheral State functions #####
1166   ==============================================================================
1167     [..]
1168     This subsection permits to get in run-time the status of the peripheral.
1169 
1170 @endverbatim
1171   * @{
1172   */
1173 
1174 /**
1175   * @brief  Return the CORDIC handle state.
1176   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
1177   *         the configuration information for CORDIC module
1178   * @retval HAL state
1179   */
HAL_CORDIC_GetState(const CORDIC_HandleTypeDef * hcordic)1180 HAL_CORDIC_StateTypeDef HAL_CORDIC_GetState(const CORDIC_HandleTypeDef *hcordic)
1181 {
1182   /* Return CORDIC handle state */
1183   return hcordic->State;
1184 }
1185 
1186 /**
1187   * @brief  Return the CORDIC peripheral error.
1188   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
1189   *         the configuration information for CORDIC module
1190   * @note   The returned error is a bit-map combination of possible errors
1191   * @retval Error bit-map
1192   */
HAL_CORDIC_GetError(const CORDIC_HandleTypeDef * hcordic)1193 uint32_t HAL_CORDIC_GetError(const CORDIC_HandleTypeDef *hcordic)
1194 {
1195   /* Return CORDIC error code */
1196   return hcordic->ErrorCode;
1197 }
1198 
1199 /**
1200   * @}
1201   */
1202 
1203 /**
1204   * @}
1205   */
1206 
1207 /** @addtogroup CORDIC_Private_Functions
1208   * @{
1209   */
1210 
1211 /**
1212   * @brief  Write input data for CORDIC processing, and increment input buffer pointer.
1213   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
1214   *         the configuration information for CORDIC module.
1215   * @param  ppInBuff Pointer to pointer to input buffer.
1216   * @retval none
1217   */
CORDIC_WriteInDataIncrementPtr(const CORDIC_HandleTypeDef * hcordic,const int32_t ** ppInBuff)1218 static void CORDIC_WriteInDataIncrementPtr(const CORDIC_HandleTypeDef *hcordic, const int32_t **ppInBuff)
1219 {
1220   /* First write of input data in the Write Data register */
1221   WRITE_REG(hcordic->Instance->WDATA, (uint32_t) **ppInBuff);
1222 
1223   /* Increment input data pointer */
1224   (*ppInBuff)++;
1225 
1226   /* Check if second write of input data is expected */
1227   if (HAL_IS_BIT_SET(hcordic->Instance->CSR, CORDIC_CSR_NARGS))
1228   {
1229     /* Second write of input data in the Write Data register */
1230     WRITE_REG(hcordic->Instance->WDATA, (uint32_t) **ppInBuff);
1231 
1232     /* Increment input data pointer */
1233     (*ppInBuff)++;
1234   }
1235 }
1236 
1237 /**
1238   * @brief  Read output data of CORDIC processing, and increment output buffer pointer.
1239   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
1240   *         the configuration information for CORDIC module.
1241   * @param  ppOutBuff Pointer to pointer to output buffer.
1242   * @retval none
1243   */
CORDIC_ReadOutDataIncrementPtr(const CORDIC_HandleTypeDef * hcordic,int32_t ** ppOutBuff)1244 static void CORDIC_ReadOutDataIncrementPtr(const CORDIC_HandleTypeDef *hcordic, int32_t **ppOutBuff)
1245 {
1246   /* First read of output data from the Read Data register */
1247   **ppOutBuff = (int32_t)READ_REG(hcordic->Instance->RDATA);
1248 
1249   /* Increment output data pointer */
1250   (*ppOutBuff)++;
1251 
1252   /* Check if second read of output data is expected */
1253   if (HAL_IS_BIT_SET(hcordic->Instance->CSR, CORDIC_CSR_NRES))
1254   {
1255     /* Second read of output data from the Read Data register */
1256     **ppOutBuff = (int32_t)READ_REG(hcordic->Instance->RDATA);
1257 
1258     /* Increment output data pointer */
1259     (*ppOutBuff)++;
1260   }
1261 }
1262 
1263 /**
1264   * @brief  DMA CORDIC Input Data process complete callback.
1265   * @param  hdma DMA handle.
1266   * @retval None
1267   */
CORDIC_DMAInCplt(DMA_HandleTypeDef * hdma)1268 static void CORDIC_DMAInCplt(DMA_HandleTypeDef *hdma)
1269 {
1270   CORDIC_HandleTypeDef *hcordic = (CORDIC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1271 
1272   /* Disable the DMA transfer for input request */
1273   CLEAR_BIT(hcordic->Instance->CSR, CORDIC_DMA_WEN);
1274 
1275   /* Check if DMA direction is CORDIC Input only (no DMA for CORDIC Output) */
1276   if (hcordic->DMADirection == CORDIC_DMA_DIR_IN)
1277   {
1278     /* Change the CORDIC DMA direction to none */
1279     hcordic->DMADirection = CORDIC_DMA_DIR_NONE;
1280 
1281     /* Change the CORDIC state to ready */
1282     hcordic->State = HAL_CORDIC_STATE_READY;
1283 
1284     /* Call calculation complete callback */
1285 #if USE_HAL_CORDIC_REGISTER_CALLBACKS == 1
1286     /*Call registered callback*/
1287     hcordic->CalculateCpltCallback(hcordic);
1288 #else
1289     /*Call legacy weak (surcharged) callback*/
1290     HAL_CORDIC_CalculateCpltCallback(hcordic);
1291 #endif /* USE_HAL_CORDIC_REGISTER_CALLBACKS */
1292   }
1293 }
1294 
1295 /**
1296   * @brief  DMA CORDIC Output Data process complete callback.
1297   * @param  hdma DMA handle.
1298   * @retval None
1299   */
CORDIC_DMAOutCplt(DMA_HandleTypeDef * hdma)1300 static void CORDIC_DMAOutCplt(DMA_HandleTypeDef *hdma)
1301 {
1302   CORDIC_HandleTypeDef *hcordic = (CORDIC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1303 
1304   /* Disable the DMA transfer for output request */
1305   CLEAR_BIT(hcordic->Instance->CSR, CORDIC_DMA_REN);
1306 
1307   /* Change the CORDIC DMA direction to none */
1308   hcordic->DMADirection = CORDIC_DMA_DIR_NONE;
1309 
1310   /* Change the CORDIC state to ready */
1311   hcordic->State = HAL_CORDIC_STATE_READY;
1312 
1313   /* Call calculation complete callback */
1314 #if USE_HAL_CORDIC_REGISTER_CALLBACKS == 1
1315   /*Call registered callback*/
1316   hcordic->CalculateCpltCallback(hcordic);
1317 #else
1318   /*Call legacy weak (surcharged) callback*/
1319   HAL_CORDIC_CalculateCpltCallback(hcordic);
1320 #endif /* USE_HAL_CORDIC_REGISTER_CALLBACKS */
1321 }
1322 
1323 /**
1324   * @brief  DMA CORDIC communication error callback.
1325   * @param  hdma DMA handle.
1326   * @retval None
1327   */
CORDIC_DMAError(DMA_HandleTypeDef * hdma)1328 static void CORDIC_DMAError(DMA_HandleTypeDef *hdma)
1329 {
1330   CORDIC_HandleTypeDef *hcordic = (CORDIC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1331 
1332   /* Set CORDIC handle state to error */
1333   hcordic->State = HAL_CORDIC_STATE_READY;
1334 
1335   /* Set CORDIC handle error code to DMA error */
1336   hcordic->ErrorCode |= HAL_CORDIC_ERROR_DMA;
1337 
1338   /* Call user callback */
1339 #if USE_HAL_CORDIC_REGISTER_CALLBACKS == 1
1340   /*Call registered callback*/
1341   hcordic->ErrorCallback(hcordic);
1342 #else
1343   /*Call legacy weak (surcharged) callback*/
1344   HAL_CORDIC_ErrorCallback(hcordic);
1345 #endif /* USE_HAL_CORDIC_REGISTER_CALLBACKS */
1346 }
1347 
1348 /**
1349   * @}
1350   */
1351 
1352 /**
1353   * @}
1354   */
1355 
1356 /**
1357   * @}
1358   */
1359 
1360 #endif /* HAL_CORDIC_MODULE_ENABLED */
1361 #endif /* CORDIC */
1362