1 /**
2   ******************************************************************************
3   * @file    stm32u5xx_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) 2021 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 "stm32u5xx_hal.h"
140 
141 #if defined(CORDIC)
142 #ifdef HAL_CORDIC_MODULE_ENABLED
143 
144 /** @addtogroup STM32U5xx_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 
890   /* Check the parameters */
891   assert_param(IS_CORDIC_DMA_DIRECTION(DMADirection));
892 
893   /* Check parameters setting */
894   if (NbCalc == 0U)
895   {
896     /* Update the error code */
897     hcordic->ErrorCode |= HAL_CORDIC_ERROR_PARAM;
898 
899     /* Return error status */
900     return HAL_ERROR;
901   }
902 
903   /* Check if CORDIC DMA direction "Out" is requested */
904   if ((DMADirection == CORDIC_DMA_DIR_OUT) || (DMADirection == CORDIC_DMA_DIR_IN_OUT))
905   {
906     /* Check parameters setting */
907     if (pOutBuff == NULL)
908     {
909       /* Update the error code */
910       hcordic->ErrorCode |= HAL_CORDIC_ERROR_PARAM;
911 
912       /* Return error status */
913       return HAL_ERROR;
914     }
915   }
916 
917   /* Check if CORDIC DMA direction "In" is requested */
918   if ((DMADirection == CORDIC_DMA_DIR_IN) || (DMADirection == CORDIC_DMA_DIR_IN_OUT))
919   {
920     /* Check parameters setting */
921     if (pInBuff == NULL)
922     {
923       /* Update the error code */
924       hcordic->ErrorCode |= HAL_CORDIC_ERROR_PARAM;
925 
926       /* Return error status */
927       return HAL_ERROR;
928     }
929   }
930 
931   if (hcordic->State == HAL_CORDIC_STATE_READY)
932   {
933     /* Reset CORDIC error code */
934     hcordic->ErrorCode = HAL_CORDIC_ERROR_NONE;
935 
936     /* Change the CORDIC state */
937     hcordic->State = HAL_CORDIC_STATE_BUSY;
938 
939     /* Get DMA direction */
940     hcordic->DMADirection = DMADirection;
941 
942     /* Check if CORDIC DMA direction "Out" is requested */
943     if ((DMADirection == CORDIC_DMA_DIR_OUT) || (DMADirection == CORDIC_DMA_DIR_IN_OUT))
944     {
945       /* Set the CORDIC DMA transfer complete callback */
946       hcordic->hdmaOut->XferCpltCallback = CORDIC_DMAOutCplt;
947       /* Set the DMA error callback */
948       hcordic->hdmaOut->XferErrorCallback = CORDIC_DMAError;
949 
950       /* Check number of output data at each calculation,
951          to retrieve the size of output data buffer */
952       if (HAL_IS_BIT_SET(hcordic->Instance->CSR, CORDIC_CSR_NRES))
953       {
954         sizeoutbuff = 2U * NbCalc;
955       }
956       else
957       {
958         sizeoutbuff = NbCalc;
959       }
960 
961       /* Convert the output buffer size into corresponding number of bytes.
962          This is necessary as the DMA handles the data at byte-level. */
963       sizeoutbuff = 4U * sizeoutbuff;
964 
965       /* Enable the DMA stream managing CORDIC output data read */
966       if (HAL_DMA_Start_IT(hcordic->hdmaOut, (uint32_t)&hcordic->Instance->RDATA, (uint32_t) pOutBuff, sizeoutbuff)
967           != HAL_OK)
968       {
969         /* Update the error code */
970         hcordic->ErrorCode |= HAL_CORDIC_ERROR_DMA;
971 
972         /* Return error status */
973         return HAL_ERROR;
974       }
975 
976       /* Enable output data Read DMA requests */
977       SET_BIT(hcordic->Instance->CSR, CORDIC_DMA_REN);
978     }
979 
980     /* Check if CORDIC DMA direction "In" is requested */
981     if ((DMADirection == CORDIC_DMA_DIR_IN) || (DMADirection == CORDIC_DMA_DIR_IN_OUT))
982     {
983       /* Set the CORDIC DMA transfer complete callback */
984       hcordic->hdmaIn->XferCpltCallback = CORDIC_DMAInCplt;
985       /* Set the DMA error callback */
986       hcordic->hdmaIn->XferErrorCallback = CORDIC_DMAError;
987 
988       /* Check number of input data expected for each calculation,
989          to retrieve the size of input data buffer */
990       if (HAL_IS_BIT_SET(hcordic->Instance->CSR, CORDIC_CSR_NARGS))
991       {
992         sizeinbuff = 2U * NbCalc;
993       }
994       else
995       {
996         sizeinbuff = NbCalc;
997       }
998 
999       /* Convert the input buffer size into corresponding number of bytes.
1000          This is necessary as the DMA handles the data at byte-level. */
1001       sizeinbuff = 4U * sizeinbuff;
1002 
1003       /* Enable the DMA stream managing CORDIC input data write */
1004       if (HAL_DMA_Start_IT(hcordic->hdmaIn, (uint32_t) pInBuff, (uint32_t)&hcordic->Instance->WDATA, sizeinbuff)
1005           != HAL_OK)
1006       {
1007         /* Update the error code */
1008         hcordic->ErrorCode |= HAL_CORDIC_ERROR_DMA;
1009 
1010         /* Return error status */
1011         return HAL_ERROR;
1012       }
1013 
1014       /* Enable input data Write DMA request */
1015       SET_BIT(hcordic->Instance->CSR, CORDIC_DMA_WEN);
1016     }
1017 
1018     /* Return function status */
1019     return HAL_OK;
1020   }
1021   else
1022   {
1023     /* Set CORDIC error code */
1024     hcordic->ErrorCode |= HAL_CORDIC_ERROR_NOT_READY;
1025 
1026     /* Return function status */
1027     return HAL_ERROR;
1028   }
1029 }
1030 
1031 /**
1032   * @}
1033   */
1034 
1035 /** @defgroup CORDIC_Exported_Functions_Group3 Callback functions
1036   *  @brief    Callback functions.
1037   *
1038 @verbatim
1039   ==============================================================================
1040                       ##### Callback functions  #####
1041   ==============================================================================
1042     [..]  This section provides Interruption and DMA callback functions:
1043       (+) DMA or Interrupt calculate complete
1044       (+) DMA or Interrupt error
1045 
1046 @endverbatim
1047   * @{
1048   */
1049 
1050 /**
1051   * @brief  CORDIC error callback.
1052   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
1053   *         the configuration information for CORDIC module
1054   * @retval None
1055   */
HAL_CORDIC_ErrorCallback(CORDIC_HandleTypeDef * hcordic)1056 __weak void HAL_CORDIC_ErrorCallback(CORDIC_HandleTypeDef *hcordic)
1057 {
1058   /* Prevent unused argument(s) compilation warning */
1059   UNUSED(hcordic);
1060 
1061   /* NOTE : This function should not be modified; when the callback is needed,
1062             the HAL_CORDIC_ErrorCallback can be implemented in the user file
1063    */
1064 }
1065 
1066 /**
1067   * @brief  CORDIC calculate complete callback.
1068   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
1069   *         the configuration information for CORDIC module
1070   * @retval None
1071   */
HAL_CORDIC_CalculateCpltCallback(CORDIC_HandleTypeDef * hcordic)1072 __weak void HAL_CORDIC_CalculateCpltCallback(CORDIC_HandleTypeDef *hcordic)
1073 {
1074   /* Prevent unused argument(s) compilation warning */
1075   UNUSED(hcordic);
1076 
1077   /* NOTE : This function should not be modified; when the callback is needed,
1078             the HAL_CORDIC_CalculateCpltCallback can be implemented in the user file
1079    */
1080 }
1081 
1082 /**
1083   * @}
1084   */
1085 
1086 /** @defgroup CORDIC_Exported_Functions_Group4 IRQ handler management
1087   *  @brief    IRQ handler.
1088   *
1089 @verbatim
1090   ==============================================================================
1091                 ##### IRQ handler management #####
1092   ==============================================================================
1093 [..]  This section provides IRQ handler function.
1094 
1095 @endverbatim
1096   * @{
1097   */
1098 
1099 /**
1100   * @brief  Handle CORDIC interrupt request.
1101   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
1102   *         the configuration information for CORDIC module
1103   * @retval None
1104   */
HAL_CORDIC_IRQHandler(CORDIC_HandleTypeDef * hcordic)1105 void HAL_CORDIC_IRQHandler(CORDIC_HandleTypeDef *hcordic)
1106 {
1107   /* Check if calculation complete interrupt is enabled and if result ready
1108      flag is raised */
1109   if (__HAL_CORDIC_GET_IT_SOURCE(hcordic, CORDIC_IT_IEN) != 0U)
1110   {
1111     if (__HAL_CORDIC_GET_FLAG(hcordic, CORDIC_FLAG_RRDY) != 0U)
1112     {
1113       /* Decrement number of calculations to get */
1114       hcordic->NbCalcToGet--;
1115 
1116       /* Read output data from Read Data register, and increment output buffer pointer */
1117       CORDIC_ReadOutDataIncrementPtr(hcordic, &(hcordic->pOutBuff));
1118 
1119       /* Check if calculations are still to be ordered */
1120       if (hcordic->NbCalcToOrder > 0U)
1121       {
1122         /* Decrement number of calculations to order */
1123         hcordic->NbCalcToOrder--;
1124 
1125         /* Continue the processing by providing another write of input data
1126            in the Write Data register, and increment input buffer pointer */
1127         CORDIC_WriteInDataIncrementPtr(hcordic, &(hcordic->pInBuff));
1128       }
1129 
1130       /* Check if all calculations results are got */
1131       if (hcordic->NbCalcToGet == 0U)
1132       {
1133         /* Disable Result Ready Interrupt */
1134         __HAL_CORDIC_DISABLE_IT(hcordic, CORDIC_IT_IEN);
1135 
1136         /* Change the CORDIC state */
1137         hcordic->State = HAL_CORDIC_STATE_READY;
1138 
1139         /* Call calculation complete callback */
1140 #if USE_HAL_CORDIC_REGISTER_CALLBACKS == 1
1141         /*Call registered callback*/
1142         hcordic->CalculateCpltCallback(hcordic);
1143 #else
1144         /*Call legacy weak (surcharged) callback*/
1145         HAL_CORDIC_CalculateCpltCallback(hcordic);
1146 #endif /* USE_HAL_CORDIC_REGISTER_CALLBACKS */
1147       }
1148     }
1149   }
1150 }
1151 
1152 /**
1153   * @}
1154   */
1155 
1156 /** @defgroup CORDIC_Exported_Functions_Group5 Peripheral State functions
1157   *  @brief   Peripheral State functions.
1158   *
1159 @verbatim
1160   ==============================================================================
1161                       ##### Peripheral State functions #####
1162   ==============================================================================
1163     [..]
1164     This subsection permits to get in run-time the status of the peripheral.
1165 
1166 @endverbatim
1167   * @{
1168   */
1169 
1170 /**
1171   * @brief  Return the CORDIC handle state.
1172   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
1173   *         the configuration information for CORDIC module
1174   * @retval HAL state
1175   */
HAL_CORDIC_GetState(const CORDIC_HandleTypeDef * hcordic)1176 HAL_CORDIC_StateTypeDef HAL_CORDIC_GetState(const CORDIC_HandleTypeDef *hcordic)
1177 {
1178   /* Return CORDIC handle state */
1179   return hcordic->State;
1180 }
1181 
1182 /**
1183   * @brief  Return the CORDIC peripheral error.
1184   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
1185   *         the configuration information for CORDIC module
1186   * @note   The returned error is a bit-map combination of possible errors
1187   * @retval Error bit-map
1188   */
HAL_CORDIC_GetError(const CORDIC_HandleTypeDef * hcordic)1189 uint32_t HAL_CORDIC_GetError(const CORDIC_HandleTypeDef *hcordic)
1190 {
1191   /* Return CORDIC error code */
1192   return hcordic->ErrorCode;
1193 }
1194 
1195 /**
1196   * @}
1197   */
1198 
1199 /**
1200   * @}
1201   */
1202 
1203 /** @addtogroup CORDIC_Private_Functions
1204   * @{
1205   */
1206 
1207 /**
1208   * @brief  Write input data for CORDIC processing, and increment input buffer pointer.
1209   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
1210   *         the configuration information for CORDIC module.
1211   * @param  ppInBuff Pointer to pointer to input buffer.
1212   * @retval none
1213   */
CORDIC_WriteInDataIncrementPtr(const CORDIC_HandleTypeDef * hcordic,const int32_t ** ppInBuff)1214 static void CORDIC_WriteInDataIncrementPtr(const CORDIC_HandleTypeDef *hcordic, const int32_t **ppInBuff)
1215 {
1216   /* First write of input data in the Write Data register */
1217   WRITE_REG(hcordic->Instance->WDATA, (uint32_t) **ppInBuff);
1218 
1219   /* Increment input data pointer */
1220   (*ppInBuff)++;
1221 
1222   /* Check if second write of input data is expected */
1223   if (HAL_IS_BIT_SET(hcordic->Instance->CSR, CORDIC_CSR_NARGS))
1224   {
1225     /* Second write of input data in the Write Data register */
1226     WRITE_REG(hcordic->Instance->WDATA, (uint32_t) **ppInBuff);
1227 
1228     /* Increment input data pointer */
1229     (*ppInBuff)++;
1230   }
1231 }
1232 
1233 /**
1234   * @brief  Read output data of CORDIC processing, and increment output buffer pointer.
1235   * @param  hcordic pointer to a CORDIC_HandleTypeDef structure that contains
1236   *         the configuration information for CORDIC module.
1237   * @param  ppOutBuff Pointer to pointer to output buffer.
1238   * @retval none
1239   */
CORDIC_ReadOutDataIncrementPtr(const CORDIC_HandleTypeDef * hcordic,int32_t ** ppOutBuff)1240 static void CORDIC_ReadOutDataIncrementPtr(const CORDIC_HandleTypeDef *hcordic, int32_t **ppOutBuff)
1241 {
1242   /* First read of output data from the Read Data register */
1243   **ppOutBuff = (int32_t)READ_REG(hcordic->Instance->RDATA);
1244 
1245   /* Increment output data pointer */
1246   (*ppOutBuff)++;
1247 
1248   /* Check if second read of output data is expected */
1249   if (HAL_IS_BIT_SET(hcordic->Instance->CSR, CORDIC_CSR_NRES))
1250   {
1251     /* Second read of output data from the Read Data register */
1252     **ppOutBuff = (int32_t)READ_REG(hcordic->Instance->RDATA);
1253 
1254     /* Increment output data pointer */
1255     (*ppOutBuff)++;
1256   }
1257 }
1258 
1259 /**
1260   * @brief  DMA CORDIC Input Data process complete callback.
1261   * @param  hdma DMA handle.
1262   * @retval None
1263   */
CORDIC_DMAInCplt(DMA_HandleTypeDef * hdma)1264 static void CORDIC_DMAInCplt(DMA_HandleTypeDef *hdma)
1265 {
1266   CORDIC_HandleTypeDef *hcordic = (CORDIC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1267 
1268   /* Disable the DMA transfer for input request */
1269   CLEAR_BIT(hcordic->Instance->CSR, CORDIC_DMA_WEN);
1270 
1271   /* Check if DMA direction is CORDIC Input only (no DMA for CORDIC Output) */
1272   if (hcordic->DMADirection == CORDIC_DMA_DIR_IN)
1273   {
1274     /* Change the CORDIC DMA direction to none */
1275     hcordic->DMADirection = CORDIC_DMA_DIR_NONE;
1276 
1277     /* Change the CORDIC state to ready */
1278     hcordic->State = HAL_CORDIC_STATE_READY;
1279 
1280     /* Call calculation complete callback */
1281 #if USE_HAL_CORDIC_REGISTER_CALLBACKS == 1
1282     /*Call registered callback*/
1283     hcordic->CalculateCpltCallback(hcordic);
1284 #else
1285     /*Call legacy weak (surcharged) callback*/
1286     HAL_CORDIC_CalculateCpltCallback(hcordic);
1287 #endif /* USE_HAL_CORDIC_REGISTER_CALLBACKS */
1288   }
1289 }
1290 
1291 /**
1292   * @brief  DMA CORDIC Output Data process complete callback.
1293   * @param  hdma DMA handle.
1294   * @retval None
1295   */
CORDIC_DMAOutCplt(DMA_HandleTypeDef * hdma)1296 static void CORDIC_DMAOutCplt(DMA_HandleTypeDef *hdma)
1297 {
1298   CORDIC_HandleTypeDef *hcordic = (CORDIC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1299 
1300   /* Disable the DMA transfer for output request */
1301   CLEAR_BIT(hcordic->Instance->CSR, CORDIC_DMA_REN);
1302 
1303   /* Change the CORDIC DMA direction to none */
1304   hcordic->DMADirection = CORDIC_DMA_DIR_NONE;
1305 
1306   /* Change the CORDIC state to ready */
1307   hcordic->State = HAL_CORDIC_STATE_READY;
1308 
1309   /* Call calculation complete callback */
1310 #if USE_HAL_CORDIC_REGISTER_CALLBACKS == 1
1311   /*Call registered callback*/
1312   hcordic->CalculateCpltCallback(hcordic);
1313 #else
1314   /*Call legacy weak (surcharged) callback*/
1315   HAL_CORDIC_CalculateCpltCallback(hcordic);
1316 #endif /* USE_HAL_CORDIC_REGISTER_CALLBACKS */
1317 }
1318 
1319 /**
1320   * @brief  DMA CORDIC communication error callback.
1321   * @param  hdma DMA handle.
1322   * @retval None
1323   */
CORDIC_DMAError(DMA_HandleTypeDef * hdma)1324 static void CORDIC_DMAError(DMA_HandleTypeDef *hdma)
1325 {
1326   CORDIC_HandleTypeDef *hcordic = (CORDIC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1327 
1328   /* Set CORDIC handle state to error */
1329   hcordic->State = HAL_CORDIC_STATE_READY;
1330 
1331   /* Set CORDIC handle error code to DMA error */
1332   hcordic->ErrorCode |= HAL_CORDIC_ERROR_DMA;
1333 
1334   /* Call user callback */
1335 #if USE_HAL_CORDIC_REGISTER_CALLBACKS == 1
1336   /*Call registered callback*/
1337   hcordic->ErrorCallback(hcordic);
1338 #else
1339   /*Call legacy weak (surcharged) callback*/
1340   HAL_CORDIC_ErrorCallback(hcordic);
1341 #endif /* USE_HAL_CORDIC_REGISTER_CALLBACKS */
1342 }
1343 
1344 /**
1345   * @}
1346   */
1347 
1348 /**
1349   * @}
1350   */
1351 
1352 /**
1353   * @}
1354   */
1355 
1356 #endif /* HAL_CORDIC_MODULE_ENABLED */
1357 #endif /* CORDIC */
1358