Equivalent of micropython SPI.read(nbytes, write=0xff)?

willemmerson
Posts: 40
Joined: Mon Mar 18, 2019 12:34 pm

Equivalent of micropython SPI.read(nbytes, write=0xff)?

Postby willemmerson » Wed Oct 23, 2019 4:42 pm

I am successfully communicating with a CS5480 metering chip using micropython but I can't seem to do it using the normal IDF sdk on the ESP32. My micropython code is:

Code: Select all

spi.write(bytearray([PAGE_FLAG | page]))
spi.write(bytearray([address]))
return spi.read(3, write=0xff)
It only works with the write=0xff part, but I can't seem to translate that into IDF code. My code is:

Code: Select all

spi_transaction_t page_transaction = {
            .tx_data={PAGE_FLAG | page},
            .length=1 * 8,
            .flags = SPI_TRANS_USE_TXDATA,
    };
    spi_transaction_t address_transaction = {
            .tx_data={address},
            .length=1 * 8,
            .flags = SPI_TRANS_USE_TXDATA,
    };
    spi_transaction_t read_transaction = {
            .tx_data={0xff, 0xff, 0xff},
            .length=3 * 8,
            .rx_buffer=rx_buffer,
            .rxlength=3 * 8,
            .flags = SPI_TRANS_USE_TXDATA,
    };

    pin_low(CS_METER34);
    ESP_ERROR_CHECK(spi_device_transmit(powermeter_device_handle, &page_transaction));
    ESP_ERROR_CHECK(spi_device_transmit(powermeter_device_handle, &address_transaction));
    ESP_ERROR_CHECK(spi_device_transmit(powermeter_device_handle, &read_transaction));
    pin_high(CS_METER34);
This doesn't work at all, but if I take away the transmit part of the read_transaction then I at least get something back, but it's wrong.
Below I try and read an address 4 times, the address should be 82 8F 5C:

Code: Select all

82 02 00 
5c 00 02 
82 02 00 
5c 00 02 
This is pretty much the result I get in micropython if I remove the write=0xff part.

I can communicate with another, different spi device on the same bus so I think my IDF C code is at least vaguely sensible but I am more a python programmer than a C programmer.
I have tried various other things which didn't work:

- Doing page, address and read all in one transaction
- Reading 3 seperate bytes back, with and without 0xff
- Putting various delays in
- Using .cmd and .addr parts of transaction

Unfortunately I can't find the micropython source for machine.SPI.read(), I don't know why.

willemmerson
Posts: 40
Joined: Mon Mar 18, 2019 12:34 pm

Re: Equivalent of micropython SPI.read(nbytes, write=0xff)?

Postby willemmerson » Wed Oct 23, 2019 5:19 pm

Actually I changed it from half-duplex to full-duplex and it works now, strange.

Who is online

Users browsing this forum: No registered users and 109 guests