I have make a test for use the SPI (not the HSPI)
this SPI is used for flash but I have succeeded to configure the SPI without make a reset on the board ref viewtopic.php?p=3624. (I configure, make me comunication, an restor the previous configuration)
On me oscilloscope the comunication work. but on the code the read data are not correct.
with this code.
uint8 val=0xCF;
with spi_mast_byte_write(SPI,&val);
on the ossiocope I have 16clk and on the MISO I have
first 8clk = 0XCF(= send data corect for a 4 to 3 wire SPI)
last 8clk = 0XBC (its correct reply from the chip HTS221).
but in the esp8266 val=0.
If i change WRITE_PERI_REG(SPI_W0(spi_no), 0X12);
val=0x12.
i I conclued no data are writed in SPI_W0.
If you cann help me thanks
void spi_master_init(uint8 spi_no, uint16 prescaler)
{
uint32 regvalue;
if(spi_no>1) return; //handle invalid input number
if(spi_no==SPI){
WRITE_PERI_REG(PERIPHS_IO_MUX, 0x005);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, 1);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, 1);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA0_U, 1);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA1_U, 1);//configure io to spi mode
}
else if(spi_no==HSPI){
WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);//configure io to spi mode
}
SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_CS_SETUP|SPI_CS_HOLD|/*SPI_DOUTDIN|*/SPI_USR_MOSI);
CLEAR_PERI_REG_MASK(SPI_USER(HSPI), SPI_FLASH_MODE|SPI_WR_BYTE_ORDER|SPI_USR_MISO|
SPI_RD_BYTE_ORDER|SPI_USR_ADDR|SPI_USR_COMMAND|SPI_USR_DUMMY);
//clear Daul or Quad lines transmission mode
CLEAR_PERI_REG_MASK(SPI_CTRL(spi_no), SPI_QIO_MODE|SPI_DIO_MODE|SPI_DOUT_MODE|SPI_QOUT_MODE);
// SPI clock=CPU clock/8
WRITE_PERI_REG(SPI_CLOCK(spi_no),
((prescaler&SPI_CLKDIV_PRE)<<SPI_CLKDIV_PRE_S)|
((3&SPI_CLKCNT_N)<<SPI_CLKCNT_N_S)|
((1&SPI_CLKCNT_H)<<SPI_CLKCNT_H_S)|
((3&SPI_CLKCNT_L)<<SPI_CLKCNT_L_S)); //clear bit 31,set SPI clock div
//set 8bit output buffer length, the buffer is the low 8bit of register"SPI_FLASH_C0"
WRITE_PERI_REG(SPI_USER1(spi_no),
((7&SPI_USR_MOSI_BITLEN)<<SPI_USR_MOSI_BITLEN_S)|
((7&SPI_USR_MISO_BITLEN)<<SPI_USR_MISO_BITLEN_S));
}
void spi_mast_byte_write(uint8 spi_no, uint8 *data)
{
if(spi_no>1) return; //handle invalid input number
while(READ_PERI_REG(SPI_CMD(spi_no))&SPI_USR);
WRITE_PERI_REG(SPI_W0(spi_no), 0);
WRITE_PERI_REG(SPI_USER2(spi_no),
((0&SPI_USR_COMMAND_BITLEN)<<SPI_USR_COMMAND_BITLEN_S)|((uint32)*data));
if(spi_no==SPI)
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, 0);//HACK for not enable FLASH_CS
SET_PERI_REG_MASK(SPI_CMD(spi_no), SPI_USR);
while(READ_PERI_REG(SPI_CMD(spi_no))&SPI_USR);
if(spi_no==SPI)
PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, 1);//cs
*data = (uint8)(READ_PERI_REG(SPI_W0(spi_no))&0xff);
}