cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Communication with ATMega32

Rohith
Associate II

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.

1 ACCEPTED SOLUTION

Accepted Solutions

I've tried that but it didn't solve the issue then due to lack of time I changed the communication to USART.

View solution in original post

6 REPLIES 6
MasterT
Senior III

What voltage AtMega32 is running?

5V

MasterT
Senior III

Than you need logic level converter 5V to 3.3V

But SPI1 works perfectly and SPI2 Also works fine for most of times (Issue occurs sometimes in data send from ATmega only). And I was working on that. Now I got data in SPI3 also. But sometimes it is not working. So I would like to confirm that my configurations for SPI are correct. If you don't mind please go through the code and let me know is there any changes needed. Thank you.

Are the pins assigned for SPI3 5V tolerant (STM inputs)? If not, then do not connect the TAmega outputs-STM inputs directly, but through some smaller resistor (200-500ohm).

I've tried that but it didn't solve the issue then due to lack of time I changed the communication to USART.