STM32 + W5500 SPI DMA: Software CS un-aligned with CLK

0

I'm using STM32F030 with W5500, and having problems with HAL SPI library. First I was using SPI (1 Mbit/s) without DMA, but Cortex M0 doesn't support data un-alignement and that produces hard_Fault. For avoiding this, they recomend us to use SPI with DMA or IT.

Well, W5500 requires Write+Read during CS activation, so I set up CS to software.

When the SPI writes a value, it does it properly. But when a Read function is used, the CS is de-activated before CLK finish.

Here is the main function (original from W5500 libraries):

void     WIZCHIP_READ_BUF (uint32_t AddrSel, uint8_t* pBuf, uint16_t len)
{
   uint8_t spi_data[3];
   uint16_t i;

   WIZCHIP_CRITICAL_ENTER();
   WIZCHIP.CS._select();

   AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_VDM_OP_);

   if(!WIZCHIP.IF.SPI._read_burst || !WIZCHIP.IF.SPI._write_burst)  // byte operation
   {
        WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
        WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >>  8);
        WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >>  0);
        for(i = 0; i < len; i++)
           pBuf[i] = WIZCHIP.IF.SPI._read_byte();
   }
   else                                                             // burst operation
   {
        spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
        spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
        spi_data[2] = (AddrSel & 0x000000FF) >> 0;
        WIZCHIP.IF.SPI._write_burst(spi_data, 3);
        WIZCHIP.IF.SPI._read_burst(pBuf, len);
   }

   WIZCHIP.CS._deselect();
   WIZCHIP_CRITICAL_EXIT();
}

WIZCHIP_CRITICAL_ENTER and EXIT are null functions. CS software selection is done (redirected) by:

void    wizchip_cs_select(void)            {
    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET);
}

void    wizchip_cs_deselect(void)          {
    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);
}

And Write / Read functions are:

uint8_t wizchip_spi_readbyte(void)        {
    uint8_t datain;
    uint8_t dataout=0xAA;
    HAL_SPI_TransmitReceive_DMA(&hspi1, &dataout, &datain, 1);
    return datain;
}

void    wizchip_spi_writebyte(uint8_t wb) {
    HAL_SPI_Transmit_DMA(&hspi1, &wb, 1);
}

void    wizchip_spi_readburst(uint8_t* pBuf, uint16_t len)  {
    uint8_t dataout=0xAA;
    HAL_SPI_TransmitReceive_DMA(&hspi1, &dataout, pBuf, len);
}

void    wizchip_spi_writeburst(uint8_t* pBuf, uint16_t len) {
    HAL_SPI_Transmit_DMA(&hspi1, pBuf, len);
}

This is what I get when Write only function:

write function

And this is when Read function is used:

enter image description here

Why is it happening??

stm32
spi
dma
stm32f0
asked on Stack Overflow Nov 4, 2018 by Fran H

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0