cancel
Showing results for 
Search instead for 
Did you mean: 

SPI read problem

_behn4m
Associate II
Posted on December 23, 2013 at 06:47

Hello

I want to set some parameters on W5100 chip and then read them. the parameters sets right, but when I want to read a register what I receive from SPI is value of past register address I've sent. My micro is STM32F103 and I use KEIL here is my codes, I use these functions too write and read SPI:

void W5100_write(u16 addr, u8 data)
{
//SPI_NSSInternalSoftwareConfig(SPI1, RESET); // enable the W5100 chip
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, W5100_WRITE_OPCODE); // need to write a byte
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (addr & 0xff00) >> 8); // send MSB of addr
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (addr & 0xff)); // send LSB
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, data); // send the data
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
//SPI_NSSInternalSoftwareConfig(SPI1, SET); // disable the W5100 chip // done with the chip
while( SPI1->SR & SPI_I2S_FLAG_BSY );
GPIO_SetBits(GPIOA, GPIO_Pin_4);
}
u8 W5100_read(u16 addr)
{
u8 data;
//SPI_NSSInternalSoftwareConfig(SPI1, RESET); // enable the W5100 chip
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, W5100_READ_OPCODE); // need to write a byte
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (addr & 0xff00) >> 8); // send MSB of addr
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (addr & 0xff)); // send LSB
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (0x00)); // send Dummy byte
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); 
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
data = SPI_I2S_ReceiveData(SPI1); // done with the chip
while( SPI1->SR & SPI_I2S_FLAG_BSY );
GPIO_SetBits(GPIOA, GPIO_Pin_4);
return data;
}

here I write then read and send to pc by usart:

W5100_write(W5100_GAR + 0, pcfg->gtw_addr[0]); // set up the gateway address
W5100_write(W5100_GAR + 1, pcfg->gtw_addr[1]);
W5100_write(W5100_GAR + 2, pcfg->gtw_addr[2]);
W5100_write(W5100_GAR + 3, pcfg->gtw_addr[3]);
delay_ms(1);
while( !(USART1->SR & 0x00000040));
USART_SendData(USART1, W5100_read(W5100_SIPR + 0)); 
while( !(USART1->SR & 0x00000040));
USART_SendData(USART1, W5100_read(W5100_SIPR + 1));
while( !(USART1->SR & 0x00000040));
USART_SendData(USART1, W5100_read(W5100_SIPR + 2));
while( !(USART1->SR & 0x00000040));
USART_SendData(USART1, W5100_read(W5100_SIPR + 3));

I can't understand whats the problem, thank you. #spi #w5100 #stm32f103 #spi
10 REPLIES 10
_behn4m
Associate II
Posted on December 25, 2013 at 20:48

thank you, very good point

I was testing this when I saw your answer. I found the override is happening and it's needed too read DR after each send. so nice, I've used dummy variable in my test, as you :D

my problem SOLVED, Thanks a lot