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   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
14   *
15   * Redistribution and use in source and binary forms, with or without modification,
16   * are permitted provided that the following conditions are met:
17   *   1. Redistributions of source code must retain the above copyright notice,
18   *      this list of conditions and the following disclaimer.
19   *   2. Redistributions in binary form must reproduce the above copyright notice,
20   *      this list of conditions and the following disclaimer in the documentation
21   *      and/or other materials provided with the distribution.
22   *   3. Neither the name of STMicroelectronics nor the names of its contributors
23   *      may be used to endorse or promote products derived from this software
24   *      without specific prior written permission.
25   *
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36   *
37   ******************************************************************************
38   */
39 
40 /* Includes ------------------------------------------------------------------*/
41 #include "stm32l4xx_hal.h"
42 
43 /** @addtogroup STM32L4xx_HAL_Driver
44   * @{
45   */
46 #ifdef HAL_SAI_MODULE_ENABLED
47 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
48 
49 /** @defgroup SAIEx SAIEx
50   * @brief SAI Extended HAL module driver
51   * @{
52   */
53 
54 /* Private types -------------------------------------------------------------*/
55 /* Private variables ---------------------------------------------------------*/
56 /* Private constants ---------------------------------------------------------*/
57 /** @defgroup SAIEx_Private_Defines SAIEx Extended Private Defines
58   * @{
59   */
60 #define SAI_PDM_DELAY_MASK          0x77U
61 #define SAI_PDM_DELAY_OFFSET        8U
62 #define SAI_PDM_RIGHT_DELAY_OFFSET  4U
63 /**
64   * @}
65   */
66 
67 /* Private macros ------------------------------------------------------------*/
68 /* Private functions ---------------------------------------------------------*/
69 /* Exported functions --------------------------------------------------------*/
70 /** @defgroup SAIEx_Exported_Functions SAIEx Extended Exported Functions
71   * @{
72   */
73 
74 /** @defgroup SAIEx_Exported_Functions_Group1 Peripheral Control functions
75   * @brief    SAIEx control functions
76   *
77 @verbatim
78  ===============================================================================
79                  ##### Extended features functions #####
80  ===============================================================================
81     [..]  This section provides functions allowing to:
82       (+) Modify PDM microphone delays
83 
84 @endverbatim
85   * @{
86   */
87 
88 /**
89   * @brief  Configure PDM microphone delays.
90   * @param  hsai SAI handle.
91   * @param  pdmMicDelay Microphone delays configuration.
92   * @retval HAL status
93   */
HAL_SAIEx_ConfigPdmMicDelay(SAI_HandleTypeDef * hsai,SAIEx_PdmMicDelayParamTypeDef * pdmMicDelay)94 HAL_StatusTypeDef HAL_SAIEx_ConfigPdmMicDelay(SAI_HandleTypeDef *hsai, SAIEx_PdmMicDelayParamTypeDef *pdmMicDelay)
95 {
96   HAL_StatusTypeDef status = HAL_OK;
97   uint32_t offset;
98 
99   /* Check that SAI sub-block is SAI1 sub-block A */
100   if (hsai->Instance != SAI1_Block_A)
101   {
102     status = HAL_ERROR;
103   }
104   else
105   {
106     /* Check microphone delay parameters */
107     assert_param(IS_SAI_PDM_MIC_PAIRS_NUMBER(pdmMicDelay->MicPair));
108     assert_param(IS_SAI_PDM_MIC_DELAY(pdmMicDelay->LeftDelay));
109     assert_param(IS_SAI_PDM_MIC_DELAY(pdmMicDelay->RightDelay));
110 
111     /* Compute offset on PDMDLY register according mic pair number */
112     offset = SAI_PDM_DELAY_OFFSET * (pdmMicDelay->MicPair - 1U);
113 
114     /* Check SAI state and offset */
115     if ((hsai->State != HAL_SAI_STATE_RESET) && (offset <= 24U))
116     {
117       /* Reset current delays for specified microphone */
118       SAI1->PDMDLY &= ~(SAI_PDM_DELAY_MASK << offset);
119 
120       /* Apply new microphone delays */
121       SAI1->PDMDLY |= (((pdmMicDelay->RightDelay << SAI_PDM_RIGHT_DELAY_OFFSET) | pdmMicDelay->LeftDelay) << offset);
122     }
123     else
124     {
125       status = HAL_ERROR;
126     }
127   }
128   return status;
129 }
130 
131 /**
132   * @}
133   */
134 
135 /**
136   * @}
137   */
138 
139 /**
140   * @}
141   */
142 
143 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
144 #endif /* HAL_SAI_MODULE_ENABLED */
145 /**
146   * @}
147   */
148 
149 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
150