stm32g4xx_hal_sai_ex.c 3.8 KB

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