[C] 纯文本查看 复制代码
I2S_HandleTypeDef hi2s1;
I2S_HandleTypeDef hi2s2;
uint16_t I2S1_Buffer_Tx[BufferSize] = {0x0102, 0x0304, 0x0506, 0x0708, 0x090A, 0x0B0C,
0x0D0E, 0x0F10, 0x1112, 0x1314, 0x1516, 0x1718,
0x191A, 0x1B1C, 0x1D1E, 0x1F20, 0x2122, 0x2324,
0x2526, 0x2728, 0x292A, 0x2B2C, 0x2D2E, 0x2F30,
0x3132, 0x3334, 0x3536, 0x3738, 0x393A, 0x3B3C,
0x3D3E, 0x3F40};
uint16_t SPI1_Buffer_Tx[BufferSize] = {0x5152, 0x5354, 0x5556, 0x5758, 0x595A, 0x5B5C,
0x5D5E, 0x5F60, 0x6162, 0x6364, 0x6566, 0x6768,
0x696A, 0x6B6C, 0x6D6E, 0x6F70, 0x7172, 0x7374,
0x7576, 0x7778, 0x797A, 0x7B7C, 0x7D7E, 0x7F80,
0x8182, 0x8384, 0x8586, 0x8788, 0x898A, 0x8B8C,
0x8D8E, 0x8F90};
__IO uint16_t I2S2_Buffer_Rx[BufferSize];
__IO uint16_t SPI2_Buffer_Rx[BufferSize];
__IO uint8_t TxIdx = 0, RxIdx = 0;
volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
volatile TestStatus TransferStatus3 = FAILED;
/* Private function prototypes -----------------------------------------------*/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SPI1;
PeriphClkInitStruct.Spi123ClockSelection = RCC_SPI123CLKSOURCE_CLKP;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
while(1);
}
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SPI2;
PeriphClkInitStruct.Spi123ClockSelection = RCC_SPI123CLKSOURCE_CLKP;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
while(1);
}
__HAL_RCC_SPI1_CLK_ENABLE();
__HAL_RCC_SPI2_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/* PA4-I2S1-WS PA5-I2S1-CK PA7-I2S1-SDO
PB12-I2S2-WS PB13-I2S2-CK PB15-I2S2-SDO
*/
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_4; //PC4-I2S1-MCK
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_6; //PC6-I2S2-MCK
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
void I2S1_Master_TO_I2S2_Slave_Config(void)
{
__HAL_RCC_SPI1_CLK_ENABLE();
__HAL_RCC_SPI2_CLK_ENABLE();
if (HAL_I2S_DeInit(&hi2s1) != HAL_OK)
{
while(1);
}
if (HAL_I2S_DeInit(&hi2s2) != HAL_OK)
{
while(1);
}
hi2s1.Instance = SPI1;
hi2s1.Init.Mode = I2S_MODE_MASTER_TX;
hi2s1.Init.Standard = I2S_STANDARD_PHILIPS;
hi2s1.Init.DataFormat = I2S_DATAFORMAT_16B;
hi2s1.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
hi2s1.Init.AudioFreq = I2S_AUDIOFREQ_48K;
hi2s1.Init.CPOL = I2S_CPOL_LOW;
hi2s1.Init.FirstBit = I2S_FIRSTBIT_MSB;
hi2s1.Init.WSInversion = I2S_WS_INVERSION_DISABLE;
hi2s1.Init.Data24BitAlignment = I2S_DATA_24BIT_ALIGNMENT_RIGHT;
hi2s1.Init.MasterKeepIOState = I2S_MASTER_KEEP_IO_STATE_DISABLE;
if (HAL_I2S_Init(&hi2s1) != HAL_OK)
{
while(1);
}
hi2s2.Instance = SPI2;
hi2s2.Init.Mode = I2S_MODE_SLAVE_RX;
hi2s2.Init.Standard = I2S_STANDARD_PHILIPS;
hi2s2.Init.DataFormat = I2S_DATAFORMAT_16B;
hi2s2.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
hi2s2.Init.AudioFreq = I2S_AUDIOFREQ_48K;
hi2s2.Init.CPOL = I2S_CPOL_LOW;
hi2s2.Init.FirstBit = I2S_FIRSTBIT_MSB;
hi2s2.Init.WSInversion = I2S_WS_INVERSION_DISABLE;
hi2s2.Init.Data24BitAlignment = I2S_DATA_24BIT_ALIGNMENT_RIGHT;
hi2s2.Init.MasterKeepIOState = I2S_MASTER_KEEP_IO_STATE_DISABLE;
if (HAL_I2S_Init(&hi2s2) != HAL_OK)
{
while(1);
}
__HAL_I2S_ENABLE(&hi2s1);
__HAL_I2S_ENABLE(&hi2s2);
}
uint8_t TEST_I2S1M_TO_I2S2S(void)
{
uint16_t i;
GPIO_Configuration();
I2S1_Master_TO_I2S2_Slave_Config();
printf("待发送的数据为 \n");
for ( i=0; i<BufferSize; i++ ) //填充缓冲
{
printf("0x%04X ", I2S1_Buffer_Tx[i]);
if(i%8 == 7)
printf("\n\r");
}
//开始发送数据
while (RxIdx < BufferSize)
{
while (__HAL_I2S_GET_FLAG(&hi2s1,I2S_FLAG_TXE) == RESET) //等待 发送缓冲区为空
if (HAL_I2S_Transmit(&hi2s1, (uint16_t *)I2S1_Buffer_Tx[TxIdx++], BufferSize, 5000)!= HAL_OK)
{
while(1);
}
printf("123 \n");
while (__HAL_I2S_GET_FLAG(&hi2s2,I2S_FLAG_RXNE) == RESET) //等待 接收缓冲区非空
if (HAL_I2S_Receive(&hi2s2, (uint16_t *)I2S2_Buffer_Rx[RxIdx++], BufferSize, 5000)!= HAL_OK)
{
while(1);
}
}
printf("接收到的数据为 \n");
for (i=0; i<BufferSize; i++) //将I2c_Buf_Read中的数据通过串口打印
{
if(I2S1_Buffer_Tx[i] != I2S2_Buffer_Rx[i])
{
printf("0x%04X ", I2S2_Buffer_Rx[i]);
printf("错误:I2S 写入与读出的数据不一致");
return 0;
}
printf("0x%04X ", I2S2_Buffer_Rx[i]);
if(i%8 == 7)
printf("\n\r");
}
printf(" I2S1 和 I2S2 主发从收通讯测试成功 \n");
return 1;
}