1 /**
2   ******************************************************************************
3   * @file    stm32h7xx_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) 2017 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 "stm32h7xx_hal.h"
25 
26 /** @addtogroup STM32H7xx_HAL_Driver
27   * @{
28   */
29 #ifdef HAL_SAI_MODULE_ENABLED
30 
31 /** @defgroup SAIEx SAIEx
32   * @brief SAI Extended HAL module driver
33   * @{
34   */
35 
36 /* Private types -------------------------------------------------------------*/
37 /* Private variables ---------------------------------------------------------*/
38 /* Private constants ---------------------------------------------------------*/
39 #define SAI_PDM_DELAY_MASK          0x77U
40 #define SAI_PDM_DELAY_OFFSET        8U
41 #define SAI_PDM_RIGHT_DELAY_OFFSET  4U
42 
43 /* Private macros ------------------------------------------------------------*/
44 /* Private functions ---------------------------------------------------------*/
45 /* Exported functions --------------------------------------------------------*/
46 /** @defgroup SAIEx_Exported_Functions SAIEx Extended Exported Functions
47   * @{
48   */
49 
50 /** @defgroup SAIEx_Exported_Functions_Group1 Peripheral Control functions
51   * @brief    SAIEx control functions
52   *
53 @verbatim
54  ===============================================================================
55                  ##### Extended features functions #####
56  ===============================================================================
57     [..]  This section provides functions allowing to:
58       (+) Modify PDM microphone delays
59 
60 @endverbatim
61   * @{
62   */
63 
64 /**
65   * @brief  Configure PDM microphone delays.
66   * @param  hsai SAI handle.
67   * @param  pdmMicDelay Microphone delays configuration.
68   * @retval HAL status
69   */
HAL_SAIEx_ConfigPdmMicDelay(const SAI_HandleTypeDef * hsai,const SAIEx_PdmMicDelayParamTypeDef * pdmMicDelay)70 HAL_StatusTypeDef HAL_SAIEx_ConfigPdmMicDelay(const SAI_HandleTypeDef *hsai,
71                                               const SAIEx_PdmMicDelayParamTypeDef *pdmMicDelay)
72 {
73   HAL_StatusTypeDef status = HAL_OK;
74   uint32_t offset;
75   SAI_TypeDef *SaiBaseAddress;
76 
77   /* Get the SAI base address according to the SAI handle */
78 #if defined(SAI4)
79   SaiBaseAddress = ((hsai->Instance == SAI1_Block_A) ? SAI1 : \
80                     (hsai->Instance == SAI4_Block_A) ? SAI4 : \
81                      NULL);
82 #else
83   SaiBaseAddress = ((hsai->Instance == SAI1_Block_A) ? SAI1 : NULL);
84 #endif /* SAI4 */
85 
86   /* Check that SAI sub-block is SAI sub-block A */
87   if (SaiBaseAddress == NULL)
88   {
89     status = HAL_ERROR;
90   }
91   else
92   {
93     /* Check microphone delay parameters */
94     assert_param(IS_SAI_PDM_MIC_PAIRS_NUMBER(pdmMicDelay->MicPair));
95     assert_param(IS_SAI_PDM_MIC_DELAY(pdmMicDelay->LeftDelay));
96     assert_param(IS_SAI_PDM_MIC_DELAY(pdmMicDelay->RightDelay));
97 
98     /* Compute offset on PDMDLY register according mic pair number */
99     offset = SAI_PDM_DELAY_OFFSET * (pdmMicDelay->MicPair - 1U);
100 
101     /* Check SAI state and offset */
102     if ((hsai->State != HAL_SAI_STATE_RESET) && (offset <= 24U))
103     {
104       /* Reset current delays for specified microphone */
105       SaiBaseAddress->PDMDLY &= ~(SAI_PDM_DELAY_MASK << offset);
106 
107       /* Apply new microphone delays */
108       SaiBaseAddress->PDMDLY |= (((pdmMicDelay->RightDelay << SAI_PDM_RIGHT_DELAY_OFFSET) | pdmMicDelay->LeftDelay) << offset);
109     }
110     else
111     {
112       status = HAL_ERROR;
113     }
114   }
115   return status;
116 }
117 
118 /**
119   * @}
120   */
121 
122 /**
123   * @}
124   */
125 
126 /**
127   * @}
128   */
129 
130 #endif /* HAL_SAI_MODULE_ENABLED */
131 /**
132   * @}
133   */
134 
135