1 /**
2   ******************************************************************************
3   * @file    stm32u5xx_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) 2021 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 "stm32u5xx_hal.h"
25 
26 /** @addtogroup STM32U5xx_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(const SAI_HandleTypeDef * hsai,const SAIEx_PdmMicDelayParamTypeDef * pdmMicDelay)76 HAL_StatusTypeDef HAL_SAIEx_ConfigPdmMicDelay(const SAI_HandleTypeDef *hsai,
77                                               const SAIEx_PdmMicDelayParamTypeDef *pdmMicDelay)
78 {
79   HAL_StatusTypeDef status = HAL_OK;
80   uint32_t offset;
81 
82   /* Check that SAI sub-block is SAI1 sub-block A */
83   if (hsai->Instance != SAI1_Block_A)
84   {
85     status = HAL_ERROR;
86   }
87   else
88   {
89     /* Check microphone delay parameters */
90     assert_param(IS_SAI_PDM_MIC_PAIRS_NUMBER(pdmMicDelay->MicPair));
91     assert_param(IS_SAI_PDM_MIC_DELAY(pdmMicDelay->LeftDelay));
92     assert_param(IS_SAI_PDM_MIC_DELAY(pdmMicDelay->RightDelay));
93 
94     /* Compute offset on PDMDLY register according mic pair number */
95     offset = SAI_PDM_DELAY_OFFSET * (pdmMicDelay->MicPair - 1U);
96 
97     /* Check SAI state and offset */
98     if ((hsai->State != HAL_SAI_STATE_RESET) && (offset <= 24U))
99     {
100       /* Reset current delays for specified microphone */
101       SAI1->PDMDLY &= ~(SAI_PDM_DELAY_MASK << offset);
102 
103       /* Apply new microphone delays */
104       SAI1->PDMDLY |= (((pdmMicDelay->RightDelay << SAI_PDM_RIGHT_DELAY_OFFSET) | pdmMicDelay->LeftDelay) << offset);
105     }
106     else
107     {
108       status = HAL_ERROR;
109     }
110   }
111   return status;
112 }
113 
114 /**
115   * @}
116   */
117 
118 /**
119   * @}
120   */
121 
122 /**
123   * @}
124   */
125 
126 #endif /* HAL_SAI_MODULE_ENABLED */
127 /**
128   * @}
129   */
130