I am using the esp-idf for an embedded project using the esp32. I am trying to port some code which uses the Arduino's transfer16 function so I wrote the following code. (I should mention that the code doesn't seem to work):
Code: Select all
// Transmit and receive 16 bit data in a single transaction
// If successful 16 bit data
// -1 returned if error occurred
static int spiTransfer16(uint16_t data, int handle) {
spi_transaction_t transaction;
memset(&transaction, 0, sizeof(transaction));
transaction.flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA
transaction.length = 16; // Length in bits
transaction.tx_data[0] = ((data >> 8) & 0xFF);
transaction.tx_data[1] = data & 0xFF;
esp_err_t result = spi_device_transmit((spi_device_handle_t) handle, &transaction);
if (result == ESP_OK) {
// Operation was successful, return the data
return ((((uint16_t) transaction.rx_data[0]) << 8) + transaction.rx_data[1]);
} else {
printf("spiTransfer16 error: %d\n", result);
return -1;
}
}
1. Is this code the equivalent to Arduino's transfer16 function?
2. Can the spi_device_transmit function send and receive data at the same time?
3. Are there any flags that need to be set to allow this type of operation?
Any guidance would be appreciated