AnsweredAssumed Answered

SPI read problem

Question asked by Beh on Dec 23, 2013
Latest reply on Dec 25, 2013 by Beh
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.

Outcomes