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