SPI Communication with ATMega32
Hello,
I have to use SPI1, SPI2 and SPI3 to communicate with 3 ATmega32 boards(11MHz / 16MHz). I'm testing with STM32F407G-DISC1. My configuration and functions are as given below.
void SPI3_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI3, &SPI_InitStruct);
SPI_Cmd(SPI3, ENABLE);
}
void SPI_Send(uint16_t Data)
{
while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET)
{
}
SPI_I2S_SendData(SPI3, Data);
while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET)
{
}
data = SPI_I2S_ReceiveData(SPI3);
}
uint16_t SPI_Receive()
{
while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET)
{
}
SPI_I2S_SendData(SPI3, 0xFF); //a dummy data to get data from ATmega32 as reply
while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET)
{
}
return SPI_I2S_ReceiveData(SPI3);
}
Here variable 'data' is 'uint16_t ' type. Data which is sent and received are 8bit. ATmega32 board is always in Slave mode. I'm using same configuration for all the three SPIs. SPI1 works fine. For SPI2 sometimes issue arises while receiving data. But SPI3 doesn't work. Please help me to resolve the issue. I'm using Coocox IDE. Thank you.
