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