xref: /btstack/port/stm32-l451-miromico-sx1280/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_sai_ex.c (revision 2fd737d36a1de5d778cacc671d4b4d8c4f3fed82)
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.
14   * All rights reserved.</center></h2>
15   *
16   * This software component is licensed by ST under BSD 3-Clause license,
17   * the "License"; You may not use this file except in compliance with the
18   * License. You may obtain a copy of the License at:
19   *                        opensource.org/licenses/BSD-3-Clause
20   *
21   ******************************************************************************
22   */
23 
24 /* Includes ------------------------------------------------------------------*/
25 #include "stm32l4xx_hal.h"
26 
27 /** @addtogroup STM32L4xx_HAL_Driver
28   * @{
29   */
30 #ifdef HAL_SAI_MODULE_ENABLED
31 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx) || \
32     defined(STM32L4P5xx) || defined(STM32L4Q5xx)
33 
34 /** @defgroup SAIEx SAIEx
35   * @brief SAI Extended HAL module driver
36   * @{
37   */
38 
39 /* Private types -------------------------------------------------------------*/
40 /* Private variables ---------------------------------------------------------*/
41 /* Private constants ---------------------------------------------------------*/
42 /** @defgroup SAIEx_Private_Defines SAIEx Extended Private Defines
43   * @{
44   */
45 #define SAI_PDM_DELAY_MASK          0x77U
46 #define SAI_PDM_DELAY_OFFSET        8U
47 #define SAI_PDM_RIGHT_DELAY_OFFSET  4U
48 /**
49   * @}
50   */
51 
52 /* Private macros ------------------------------------------------------------*/
53 /* Private functions ---------------------------------------------------------*/
54 /* Exported functions --------------------------------------------------------*/
55 /** @defgroup SAIEx_Exported_Functions SAIEx Extended Exported Functions
56   * @{
57   */
58 
59 /** @defgroup SAIEx_Exported_Functions_Group1 Peripheral Control functions
60   * @brief    SAIEx control functions
61   *
62 @verbatim
63  ===============================================================================
64                  ##### Extended features functions #####
65  ===============================================================================
66     [..]  This section provides functions allowing to:
67       (+) Modify PDM microphone delays
68 
69 @endverbatim
70   * @{
71   */
72 
73 /**
74   * @brief  Configure PDM microphone delays.
75   * @param  hsai SAI handle.
76   * @param  pdmMicDelay Microphone delays configuration.
77   * @retval HAL status
78   */
HAL_SAIEx_ConfigPdmMicDelay(SAI_HandleTypeDef * hsai,SAIEx_PdmMicDelayParamTypeDef * pdmMicDelay)79 HAL_StatusTypeDef HAL_SAIEx_ConfigPdmMicDelay(SAI_HandleTypeDef *hsai, 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 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
136