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