Need the equivalent of Arduino's SPI.transfer16

calhjh@gmail.com
Posts: 3
Joined: Tue Oct 02, 2018 11:53 pm

Need the equivalent of Arduino's SPI.transfer16

Postby calhjh@gmail.com » Fri Oct 05, 2018 3:27 pm

Hello

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;
    }
}
My questions are as follows:
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

saden123
Posts: 12
Joined: Tue Jul 24, 2018 12:18 am

Re: Need the equivalent of Arduino's SPI.transfer16

Postby saden123 » Sun Oct 07, 2018 1:27 am

Hi,
I don't think you have set the transaction buffers up properly.
transaction.rx_buffer
transaction.tx_buffer
Need to point at some pre-allocated memory of the required size.
At the moment transaction.tx_data etc will be pointing at memory address 0
Si
[EDIT] I'm wrong on this
Last edited by saden123 on Sun Oct 07, 2018 2:52 am, edited 1 time in total.

ESP_Sprite
Posts: 9708
Joined: Thu Nov 26, 2015 4:08 am

Re: Need the equivalent of Arduino's SPI.transfer16

Postby ESP_Sprite » Sun Oct 07, 2018 2:12 am

That's not true; the SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA flags allow you to dump a limited amount of data directly into the trans struct, without having to allocate external buffers.

saden123
Posts: 12
Joined: Tue Jul 24, 2018 12:18 am

Re: Need the equivalent of Arduino's SPI.transfer16

Postby saden123 » Sun Oct 07, 2018 2:50 am

Thanks, I stand corrected, it's been a while since I last worked with SPI.
Ok. I just looked up the Arduino function and it looks basically the same. Maybe a small delay between bytes. Sorry, not much help.
Cheers,
Si.

calhjh@gmail.com
Posts: 3
Joined: Tue Oct 02, 2018 11:53 pm

Re: Need the equivalent of Arduino's SPI.transfer16

Postby calhjh@gmail.com » Mon Oct 08, 2018 7:34 pm

OK I found my problem. The chip I am talking to (XPT2046 touch screen controller) requires 3 SPI 8 bit transfers for each touch position read. If I let the esp-idf spimaster code control the chip select to the controller it toggles the chip select after each 8 bit transfer messing up the data received from the slave/controller. The fix was to assign a bogus chip select when I define my SPI master device and control the real chip select myself. Leaving the chip select low for the three transfers does the trick.

User avatar
cssvb94
Posts: 1
Joined: Tue Dec 22, 2020 10:22 am

Re: Need the equivalent of Arduino's SPI.transfer16

Postby cssvb94 » Tue Dec 22, 2020 10:36 am

calhjh@gmail.com wrote:
Mon Oct 08, 2018 7:34 pm
OK I found my problem. The chip I am talking to (XPT2046 touch screen controller) requires 3 SPI 8 bit transfers for each touch position read. If I let the esp-idf spimaster code control the chip select to the controller it toggles the chip select after each 8 bit transfer messing up the data received from the slave/controller. The fix was to assign a bogus chip select when I define my SPI master device and control the real chip select myself. Leaving the chip select low for the three transfers does the trick.
Hi,
I'm fighting with the same touch controller.
Would you mind to show your code snippets about SPI configuration?
More specific: spi_device_interface_config_t and how you read from it.
All I got from mine is nonsense or just 0s.

Here is mine config:
[Codebox]
spi_device_interface_config_t touch_devcfg = {
.clock_speed_hz = SPI_MASTER_FREQ_20M,
// .spics_io_num = GPIO_TSCS,
.spics_io_num = -1, // replaced it as per your finding
.queue_size = 7,
.pre_cb = NULL,
.post_cb = NULL,
.command_bits = 8,
.address_bits = 0,
.dummy_bits = 0,
.flags = SPI_DEVICE_NO_DUMMY};
[/Codebox]

transfer16 functions is same as yours
I pull CS down, do the transactions and pull it HIGH at the end.
I transfer/read 0x91 as 1st dummy read, then 0xd1 for Y and 0x91 for X, then 0xd0 for Y and power down and 0 at the end.
All I got is -512 for X and 0 for Y

Thank you in advance.
*

Who is online

Users browsing this forum: Bing [Bot], MicroController, olli100 and 85 guests