1 /** 2 ****************************************************************************** 3 * File Name : I2S.c 4 * Description : This file provides code for the configuration 5 * of the I2S instances. 6 ****************************************************************************** 7 * @attention 8 * 9 * <h2><center>© Copyright (c) 2019 STMicroelectronics. 10 * All rights reserved.</center></h2> 11 * 12 * This software component is licensed by ST under BSD 3-Clause license, 13 * the "License"; You may not use this file except in compliance with the 14 * License. You may obtain a copy of the License at: 15 * opensource.org/licenses/BSD-3-Clause 16 * 17 ****************************************************************************** 18 */ 19 20 /* Includes ------------------------------------------------------------------*/ 21 #include "i2s.h" 22 23 /* USER CODE BEGIN 0 */ 24 25 /* USER CODE END 0 */ 26 27 I2S_HandleTypeDef hi2s2; 28 29 /* I2S2 init function */ MX_I2S2_Init(void)30void MX_I2S2_Init(void) 31 { 32 33 hi2s2.Instance = SPI2; 34 hi2s2.Init.Mode = I2S_MODE_MASTER_TX; 35 hi2s2.Init.Standard = I2S_STANDARD_PHILIPS; 36 hi2s2.Init.DataFormat = I2S_DATAFORMAT_16B; 37 hi2s2.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE; 38 hi2s2.Init.AudioFreq = I2S_AUDIOFREQ_8K; 39 hi2s2.Init.CPOL = I2S_CPOL_LOW; 40 hi2s2.Init.ClockSource = I2S_CLOCK_PLL; 41 hi2s2.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_ENABLE; 42 if (HAL_I2S_Init(&hi2s2) != HAL_OK) 43 { 44 Error_Handler(); 45 } 46 47 } 48 HAL_I2S_MspInit(I2S_HandleTypeDef * i2sHandle)49void HAL_I2S_MspInit(I2S_HandleTypeDef* i2sHandle) 50 { 51 52 GPIO_InitTypeDef GPIO_InitStruct = {0}; 53 if(i2sHandle->Instance==SPI2) 54 { 55 /* USER CODE BEGIN SPI2_MspInit 0 */ 56 57 /* USER CODE END SPI2_MspInit 0 */ 58 /* I2S2 clock enable */ 59 __HAL_RCC_SPI2_CLK_ENABLE(); 60 61 __HAL_RCC_GPIOC_CLK_ENABLE(); 62 __HAL_RCC_GPIOB_CLK_ENABLE(); 63 /**I2S2 GPIO Configuration 64 PC2 ------> I2S2_ext_SD 65 PC3 ------> I2S2_SD 66 PB10 ------> I2S2_CK 67 PB12 ------> I2S2_WS 68 */ 69 GPIO_InitStruct.Pin = GPIO_PIN_2; 70 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 71 GPIO_InitStruct.Pull = GPIO_NOPULL; 72 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 73 GPIO_InitStruct.Alternate = GPIO_AF6_I2S2ext; 74 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); 75 76 GPIO_InitStruct.Pin = PDM_OUT_Pin; 77 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 78 GPIO_InitStruct.Pull = GPIO_NOPULL; 79 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 80 GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; 81 HAL_GPIO_Init(PDM_OUT_GPIO_Port, &GPIO_InitStruct); 82 83 GPIO_InitStruct.Pin = CLK_IN_Pin|GPIO_PIN_12; 84 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 85 GPIO_InitStruct.Pull = GPIO_NOPULL; 86 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 87 GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; 88 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 89 90 /* USER CODE BEGIN SPI2_MspInit 1 */ 91 92 /* USER CODE END SPI2_MspInit 1 */ 93 } 94 } 95 HAL_I2S_MspDeInit(I2S_HandleTypeDef * i2sHandle)96void HAL_I2S_MspDeInit(I2S_HandleTypeDef* i2sHandle) 97 { 98 99 if(i2sHandle->Instance==SPI2) 100 { 101 /* USER CODE BEGIN SPI2_MspDeInit 0 */ 102 103 /* USER CODE END SPI2_MspDeInit 0 */ 104 /* Peripheral clock disable */ 105 __HAL_RCC_SPI2_CLK_DISABLE(); 106 107 /**I2S2 GPIO Configuration 108 PC2 ------> I2S2_ext_SD 109 PC3 ------> I2S2_SD 110 PB10 ------> I2S2_CK 111 PB12 ------> I2S2_WS 112 */ 113 HAL_GPIO_DeInit(GPIOC, GPIO_PIN_2|PDM_OUT_Pin); 114 115 HAL_GPIO_DeInit(GPIOB, CLK_IN_Pin|GPIO_PIN_12); 116 117 /* USER CODE BEGIN SPI2_MspDeInit 1 */ 118 119 /* USER CODE END SPI2_MspDeInit 1 */ 120 } 121 } 122 123 /* USER CODE BEGIN 1 */ 124 125 /* USER CODE END 1 */ 126 127 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 128