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