ESP32 IDF, rewrite SPI custom function to meet the specific of the device

Viewed 20

I'm trying to use the esp-idf standard SPI management functions instead of custom ones.

The custom functions are:

// the initialization of the gpio:

void _spi_init(pn532_t *obj, uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t ss)
{
    obj->_clk = clk;
    obj->_miso = miso;
    obj->_mosi = mosi;
    obj->_ss = ss;

    gpio_pad_select_gpio(obj->_clk);
    gpio_pad_select_gpio(obj->_miso);
    gpio_pad_select_gpio(obj->_mosi);
    gpio_pad_select_gpio(obj->_ss);

    gpio_set_direction(obj->_ss, GPIO_MODE_OUTPUT);
    gpio_set_level(obj->_ss, 1);
    gpio_set_direction(obj->_clk, GPIO_MODE_OUTPUT);
    gpio_set_direction(obj->_mosi, GPIO_MODE_OUTPUT);
    gpio_set_direction(obj->_miso, GPIO_MODE_INPUT);
}

// read and write to SPI

uint8_t _spi_read(pn532_t *obj)
{
    int8_t i, x;
    x = 0;

    gpio_set_level(obj->_clk, 1);

    for (i = 0; i < 8; i++)
    {
        if (gpio_get_level(obj->_miso))
        {
            x |= _BV(i);
        }
        gpio_set_level(obj->_clk, 0);
        gpio_set_level(obj->_clk, 1);
    }

    return x;
}

void _spi_write(pn532_t *obj, uint8_t c)
{
    int8_t i;
    gpio_set_level(obj->_clk, 1);

    for (i = 0; i < 8; i++)
    {
        gpio_set_level(obj->_clk, 0);
        if (c & _BV(i))
        {
            gpio_set_level(obj->_mosi, 1);
        }
        else
        {
            gpio_set_level(obj->_mosi, 0);
        }
        gpio_set_level(obj->_clk, 1);
    }
}


My refactoring currently is only for these 3 functions:


void _spi_init(pn532_t *obj, spi_host_device_t spiHostDevice, uint8_t ss) {

    // the spi_bus_initialize is already initialized in the main function.

    gpio_set_direction((gpio_num_t) ss, GPIO_MODE_OUTPUT);
    gpio_set_level((gpio_num_t) ss, 1);

    //Config Frequency and SS GPIO
    spi_device_interface_config_t deviceInterfaceConfig = {
            .mode = 0,  //SPI mode 0
            .clock_speed_hz= 1200000,  // Usually 1.2 Mhz
            .input_delay_ns = 0,
            .spics_io_num = ss,
            .flags = SPI_DEVICE_BIT_LSBFIRST, // the device use LSB
            .queue_size = 1
    };

    spi_device_handle_t spiDeviceHandle;

    //Attach the pn532 to the SPI bus
    ESP_ERROR_CHECK(spi_bus_add_device(spiHostDevice, &deviceInterfaceConfig, &spiDeviceHandle));

    obj->spi = spiDeviceHandle;

}

void _spi_write(pn532_t *obj, uint8_t data) {
    spi_transaction_t t;
    memset(&t, 0, sizeof(t));       //Zero out the transaction
    t.length = 8;
    t.tx_buffer = &data;

    ESP_LOGI(TAG, "send data: 0b%s", toBinary(data,8));
    ESP_ERROR_CHECK(spi_device_polling_transmit(obj->spi, &t));

}


uint8_t _spi_read(pn532_t *obj) {

    uint8_t data = 1;
    spi_transaction_t t;
    memset(&t, 0, sizeof(t));       //Zero out the transaction
    t.length = 8;
    t.rxlength = 8;
    t.tx_buffer = NULL;
    t.rx_buffer = &data;

    ESP_ERROR_CHECK(spi_device_polling_transmit(obj->spi, &t));

    ESP_LOGI(TAG, "read data: 0b%s", toBinary(data,8));

    return data;

}

Currently, this rewrite is not working. I already did a lot of tries with any combination of config that makes sense, without success.

Practically, when I ask the device to give me the status, I'm expecting one byte in response, in form of 0x00 or 0x01. But I'm receiving 0x08. So still one bit up, but in the wrong position.

So I think I'm still missing some configuration on my rewrite.

Apprising any hit or test that I can do in order to found what is missing!

Thanks!

0 Answers
Related