SPI master send / receive
Posted: Mon Jan 19, 2015 6:05 pm
Hello,
I try to implement a SPI send / receive function using HSPI interface. Here is my non working code :
What is wrong with it ?
Thanks for your help
Frantz
I try to implement a SPI send / receive function using HSPI interface. Here is my non working code :
Code: Select all
uint8_t ICACHE_FLASH_ATTR spi_master_send_recv(uint8 data_out)
{
uint32_t regvalue;
uint8_t data_in;
while(READ_PERI_REG(SPI_FLASH_CMD(HSPI))&SPI_FLASH_USR);
regvalue = READ_PERI_REG(SPI_FLASH_USER(HSPI));
// We want to send & receive data, so no command, address. DOUT and SPI_DOUTDIN set
regvalue = regvalue | SPI_CS_SETUP | SPI_CS_HOLD | SPI_FLASH_DOUT | SPI_DOUTDIN;
// BIT2 is undocumented bit, example code wants it cleared
regvalue = regvalue & (~(BIT2 | SPI_USR_COMMAND | SPI_FLASH_USR_DIN | SPI_FLASH_USR_ADDR ));
WRITE_PERI_REG(SPI_FLASH_USER(HSPI), regvalue);
// Send / Receive 8 bits
SET_PERI_REG_BITS(SPI_FLASH_USER1(HSPI),SPI_USR_OUT_BITLEN,7,SPI_USR_OUT_BITLEN_S);
WRITE_PERI_REG(SPI_FLASH_C0(HSPI), data_out);
SET_PERI_REG_MASK(SPI_FLASH_CMD(HSPI), SPI_FLASH_USR); //transmission start
while (READ_PERI_REG(SPI_FLASH_CMD(HSPI))&SPI_FLASH_USR); //waiting for spi module available again
//os_printf("SPI_FLASH_C0 0x%08x\r\n", READ_PERI_REG(SPI_FLASH_C0(HSPI)));
data_in = (uint8_t)(READ_PERI_REG(SPI_FLASH_C0(HSPI)));
return data_in;
}
What is wrong with it ?
Thanks for your help
Frantz