1 /**
2   ******************************************************************************
3   * @file    stm32wbaxx_hal_sai_ex.c
4   * @author  MCD Application Team
5   * @brief   SAI Extended HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionality of the SAI Peripheral Controller:
8   *           + Modify PDM microphone delays.
9   *
10   ******************************************************************************
11   * @attention
12   *
13   * Copyright (c) 2022 STMicroelectronics.
14   * All rights reserved.
15   *
16   * This software is licensed under terms that can be found in the LICENSE file
17   * in the root directory of this software component.
18   * If no LICENSE file comes with this software, it is provided AS-IS.
19   *
20   ******************************************************************************
21   */
22 
23 /* Includes ------------------------------------------------------------------*/
24 #include "stm32wbaxx_hal.h"
25 
26 /** @addtogroup STM32WBAxx_HAL_Driver
27   * @{
28   */
29 #if defined (SAI1)
30 #ifdef HAL_SAI_MODULE_ENABLED
31 
32 /** @defgroup SAIEx SAIEx
33   * @brief SAI Extended HAL module driver
34   * @{
35   */
36 
37 /* Private types -------------------------------------------------------------*/
38 /* Private variables ---------------------------------------------------------*/
39 /* Private constants ---------------------------------------------------------*/
40 /** @defgroup SAIEx_Private_Defines SAIEx Extended Private Defines
41   * @{
42   */
43 #define SAI_PDM_DELAY_MASK          0x77U
44 #define SAI_PDM_DELAY_OFFSET        8U
45 #define SAI_PDM_RIGHT_DELAY_OFFSET  4U
46 /**
47   * @}
48   */
49 
50 /* Private macros ------------------------------------------------------------*/
51 /* Private functions ---------------------------------------------------------*/
52 /* Exported functions --------------------------------------------------------*/
53 /** @defgroup SAIEx_Exported_Functions SAIEx Extended Exported Functions
54   * @{
55   */
56 
57 /** @defgroup SAIEx_Exported_Functions_Group1 Peripheral Control functions
58   * @brief    SAIEx control functions
59   *
60 @verbatim
61  ===============================================================================
62                  ##### Extended features functions #####
63  ===============================================================================
64     [..]  This section provides functions allowing to:
65       (+) Modify PDM microphone delays
66 
67 @endverbatim
68   * @{
69   */
70 
71 /**
72   * @brief  Configure PDM microphone delays.
73   * @param  hsai SAI handle.
74   * @param  pdmMicDelay Microphone delays configuration.
75   * @retval HAL status
76   */
HAL_SAIEx_ConfigPdmMicDelay(const SAI_HandleTypeDef * hsai,const SAIEx_PdmMicDelayParamTypeDef * pdmMicDelay)77 HAL_StatusTypeDef HAL_SAIEx_ConfigPdmMicDelay(const SAI_HandleTypeDef *hsai,
78                                               const SAIEx_PdmMicDelayParamTypeDef *pdmMicDelay)
79 {
80   HAL_StatusTypeDef status = HAL_OK;
81   uint32_t offset;
82 
83   /* Check that SAI sub-block is SAI1 sub-block A */
84   if (hsai->Instance != SAI1_Block_A)
85   {
86     status = HAL_ERROR;
87   }
88   else
89   {
90     /* Check microphone delay parameters */
91     assert_param(IS_SAI_PDM_MIC_PAIRS_NUMBER(pdmMicDelay->MicPair));
92     assert_param(IS_SAI_PDM_MIC_DELAY(pdmMicDelay->LeftDelay));
93     assert_param(IS_SAI_PDM_MIC_DELAY(pdmMicDelay->RightDelay));
94 
95     /* Compute offset on PDMDLY register according mic pair number */
96     offset = SAI_PDM_DELAY_OFFSET * (pdmMicDelay->MicPair - 1U);
97 
98     /* Check SAI state and offset */
99     if ((hsai->State != HAL_SAI_STATE_RESET) && (offset <= 24U))
100     {
101       /* Reset current delays for specified microphone */
102       SAI1->PDMDLY &= ~(SAI_PDM_DELAY_MASK << offset);
103 
104       /* Apply new microphone delays */
105       SAI1->PDMDLY |= (((pdmMicDelay->RightDelay << SAI_PDM_RIGHT_DELAY_OFFSET) | pdmMicDelay->LeftDelay) << offset);
106     }
107     else
108     {
109       status = HAL_ERROR;
110     }
111   }
112   return status;
113 }
114 
115 /**
116   * @}
117   */
118 
119 /**
120   * @}
121   */
122 
123 /**
124   * @}
125   */
126 
127 #endif /* HAL_SAI_MODULE_ENABLED */
128 #endif /* SAI1 */
129 /**
130   * @}
131   */
132 
133