Equivalent of micropython SPI.read(nbytes, write=0xff)?
Posted: 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:
It only works with the write=0xff part, but I can't seem to translate that into IDF code. My code is:
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:
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.
Code: Select all
spi.write(bytearray([PAGE_FLAG | page]))
spi.write(bytearray([address]))
return spi.read(3, write=0xff)
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);
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
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.