Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By Sifourquier
#17158 Hi

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

Code: Select allvoid 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));
}

Code: Select allvoid 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);
Last edited by Sifourquier on Tue May 12, 2015 12:15 pm, edited 1 time in total.
User avatar
By metalphreak
#17197 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);

You have MISO disabled in the config, so it's not reading in any data...


https://github.com/MetalPhreak/ESP8266_SPI_Driver

You can add your bit of code that enables/disabled the FLASH_CS to my SPI driver and use that. The commands to send/receive data easily are then provided which handle setting the SPI_USER config bits.
User avatar
By Sifourquier
#17222 EDIT resolved watch next post

Thanks for your lib

I have only change spi_clock(spi_no, 20, SPI_CLK_CNTDIV);
(I don't have add me code for disabled FLASH_CS)


and test with
val=spi_transaction(SPI,8,READ|INC|WHO_AM_I,0,0x00,0,0x00,8,0);
or
val=spi_transaction(SPI,0,0x00,0,0x00,8,READ|INC|WHO_AM_I,8,0);

But i have only 10clk on the SPI the cmd(or mosi) as send well but after I have only 2clk
Last edited by Sifourquier on Tue May 12, 2015 12:16 pm, edited 1 time in total.
User avatar
By Sifourquier
#17256 I have found why I have 10CLK
The spi is in Quad lines transmission mode

If I add

Code: Select allCLEAR_PERI_REG_MASK(SPI_CTRL(spi_no), SPI_QIO_MODE|SPI_DIO_MODE|SPI_DOUT_MODE|SPI_QOUT_MODE);


I have 16clk

but the read data are every time 0X6F (test with real data=0xBC and 0x00)