spi in dev-esp32-idf4 seems broken
pjsg opened this issue · comments
Expected behavior
device:transfer({address=1, rxlen=1, txdata=string.char(0)})
ought to work
Actual behavior
It actually says
spi_master: check_trans_valid(687): rx length > tx length in full duplex mode
Root cause
It appears that the code in spi_master.c
is broken:
in lspi_device_transfer
the following code is missing the line that I've marked
trans.cmd = opt_checkint( L, "cmd", 0 );
trans.addr = opt_checkint( L, "addr", 0 );
//
rx_len = opt_checkint( L, "rxlen", 0 );
//
trans.flags |= opt_checkbool( L, "addr_mode", false ) ? SPI_TRANS_MODE_DIOQIO_ADDR : 0;
//
lua_getfield( L, stack, "mode" );
trans.flags |= options_flags[ luaL_checkoption( L, -1, options[0], options ) ];
lua_pop(L, 1); // MISSING LINE ---------------------------------------------------
//
data_len = 0;
data = opt_checklstring( L, "txdata", "", &data_len );
Without this line, the txdata doesn't get fetched from the right place (as the lua_getfield pushes an element onto the stack).
This leads to a more general question -- does anybody have working spi code? I'm trying to talk to a device but it doesn't appear to work. I may have to break out the logic analyzer....
Also, if you want to use the cs
config item when you create the device, then you need to configure that as a gpio
output pin and write the inactive state to it. I wonder if this should just be builtin to our device creation function. I don't think that it would do any harm -- and might save people a bunch of time. [I had to get my logic analyzer out to see what was going on!]