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