Merge pull request #1965 from espruino/esp32_improvement

Update SPI (fix #1963)
This commit is contained in:
Gordon Williams 2020-12-07 08:08:05 +00:00 committed by GitHub
commit 6b19a0249b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -145,36 +145,25 @@ void jshSPISetup(
* of the previous send (or -1). If data<0, no data is sent and the function
* waits for data to be returned */
int jshSPISend(
IOEventFlags device, //!< The identity of the SPI device through which data is being sent.
int data //!< The data to be sent or an indication that no data is to be sent.
IOEventFlags device,
int data
) {
int channelPnt = getSPIChannelPnt(device);
uint8_t byte = (uint8_t)data;
//os_printf("> jshSPISend - device=%d, data=%x\n", device, data);
int retData = (int)SPIChannels[channelPnt].g_lastSPIRead;
if (data >=0) {
jshSPIWait(device);
// Send 8 bits of data taken from "data" over the selected spi and store the returned
// data for subsequent retrieval.
//spiTransferBits(_spi[which_spi], (uint32_t)data, &g_lastSPIRead, 8);
esp_err_t ret;
memset(&spi_trans, 0, sizeof(spi_trans)); //Zero out the transaction
spi_trans.length=8; //Command is 8 bits
spi_trans.tx_buffer=&data; //The data is the cmd itself
// https://esp-idf.readthedocs.io/en/latest/api/spi_master.html#type-definitions
// should this be a switch or always read?
spi_Sending = true;
ret=spi_device_queue_trans(SPIChannels[channelPnt].spi, &spi_trans, portMAX_DELAY); //Transmit - blocks until result - need to change this?
if (ret != ESP_OK) {
spi_Sending = false;
jsExceptionHere(JSET_INTERNALERROR, "SPI Send Error %d\n", ret);
return false;
}
SPIChannels[channelPnt].g_lastSPIRead=spi_trans.rx_data[0];
jshSPIWait(device);
esp_err_t ret;
spi_transaction_t t;
memset(&t, 0, sizeof(t));
t.length=8;
t.tx_buffer=&data;
t.flags=SPI_TRANS_USE_RXDATA;
ret=spi_device_transmit(SPIChannels[channelPnt].spi, &t);
assert(ret == ESP_OK);
SPIChannels[channelPnt].g_lastSPIRead=t.rx_data[0];
} else {
SPIChannels[channelPnt].g_lastSPIRead = (uint32_t)-1;
}
int retData = (int)SPIChannels[channelPnt].g_lastSPIRead;
return (int)retData;
}
@ -186,6 +175,7 @@ bool jshSPISendMany(IOEventFlags device, unsigned char *tx, unsigned char *rx, s
if (count==1) {
int r = jshSPISend(device, tx?*tx:-1);
if (rx) *rx = r;
if(callback)callback();
return true;
}
jshSPIWait(device);