<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>SPI Slave mode - missing first bit</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/77019/spi-slave-mode---missing-first-bit</link><description>I am using nRF52840 DK and connecting it with SPI to ESP32. The first bit of the transmission is missed for some reason. I could verify that the transmission is ok with logic analyser: 
 
 
 
 But the data received by the nRF is: 
 &amp;lt;info&amp;gt; app: F8 B2 56</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 01 Jul 2021 18:08:54 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/77019/spi-slave-mode---missing-first-bit" /><item><title>RE: SPI Slave mode - missing first bit</title><link>https://devzone.nordicsemi.com/thread/318305?ContentTypeID=1</link><pubDate>Thu, 01 Jul 2021 18:08:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c9f51aff-3a07-4d92-bf56-7feb31ac1f29</guid><dc:creator>Dmitry</dc:creator><description>&lt;p&gt;It&amp;#39;s hard to see on the picture, but&amp;nbsp;CSN to CLK setup time is obviously too low - about half CLK period. For 52840 it should be at least 1000 ns (see &lt;a href="https://infocenter.nordicsemi.com/topic/ps_nrf52840/spis.html?cp=4_0_0_5_25_5#unique_1831034047"&gt;electrical specification for SPIS&lt;/a&gt;).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It seems that software-controlled CS is &lt;a href="https://github.com/espressif/esp-idf/issues/1268"&gt;the only solution&lt;/a&gt;&amp;nbsp;for full-duplex mode.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Edit: oops.. I didn&amp;#39;t look at time markers - they show exactly 140 ns CSN-to-CLK time&lt;/em&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI Slave mode - missing first bit</title><link>https://devzone.nordicsemi.com/thread/318300?ContentTypeID=1</link><pubDate>Thu, 01 Jul 2021 16:54:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d513de7b-5730-4ce9-9436-8bcbd63882b0</guid><dc:creator>vasilje</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Thanks for the answer. I dont think that issue is relevant unfortunately, I am not using Arduino but I am using ESP-IDF. Also the essence of the problem is different.&lt;/p&gt;
&lt;p&gt;Why I thought the problem is from the nRF side is because the signal produced by ESP is good and it was verified via digital analyser (I am not sure why the screenshot resolution is not good, once I click on the picture, I can see everything quite clearly - please let me know how to update in even better resolution if necessary)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Code for SPI initialisation on nRF side:&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void spis_init(volatile comm_status_def* comm_status)
{
    comm_status_ = comm_status;
    comm_status_-&amp;gt;spi = INITIALIZED;
    // Enable the constant latency sub power mode to minimize the time it takes
    // for the SPIS peripheral to become active after the CSN line is asserted
    // (when the CPU is in sleep mode).
    NRF_POWER-&amp;gt;TASKS_CONSTLAT = 1;

    NRF_LOG_INFO(&amp;quot;SPIS example&amp;quot;);

    nrf_drv_spis_config_t spis_config = NRF_DRV_SPIS_DEFAULT_CONFIG;
    spis_config.csn_pin               = APP_SPIS_CS_PIN;
    spis_config.miso_pin              = APP_SPIS_MISO_PIN;
    spis_config.mosi_pin              = APP_SPIS_MOSI_PIN;
    spis_config.sck_pin               = APP_SPIS_SCK_PIN;
    spis_config.mode                  = NRF_SPIS_MODE_0;

    NRF_LOG_INFO(&amp;quot;SPIS example started with pins: SS: %d, MISO: %d, MOSI: %d, SCK: %d&amp;quot;, APP_SPIS_CS_PIN, APP_SPIS_MISO_PIN, APP_SPIS_MOSI_PIN, APP_SPIS_SCK_PIN);

    APP_ERROR_CHECK(nrf_drv_spis_init(&amp;amp;spis, &amp;amp;spis_config, spis_event_handler));
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Full code from ESP32 side:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;freertos/FreeRTOS.h&amp;quot;
#include &amp;quot;freertos/task.h&amp;quot;
#include &amp;quot;esp_system.h&amp;quot;
#include &amp;quot;driver/spi_master.h&amp;quot;
#include &amp;quot;driver/gpio.h&amp;quot;
#include &amp;quot;esp_log.h&amp;quot;

#define PIN_NUM_MISO 19
#define PIN_NUM_MOSI 23
#define PIN_NUM_CLK  18
#define PIN_NUM_CS   16

#define PIN_NUM_DC   21

#define FORWARD         0x04, 0x8C, 0x95, 0xF1, 0x8C, 0x00, 0x00, 0x11, 0x0A, 0x8B, 0x00, 0xDC, 0xB7, 0xFF 
#define ADDRESS         0x2C, 0x59, 0x2B, 0x46, 0xA6  
#define SIZE_BYTES      19       

#define PARALLEL_LINES 16


void lcd_spi_pre_transfer_callback(spi_transaction_t *t)
{
    int dc=(int)t-&amp;gt;user;
    gpio_set_level(PIN_NUM_DC, dc);
}

void app_main(void)
{
    esp_err_t ret;
    spi_device_handle_t spi;
    spi_bus_config_t buscfg={
        .miso_io_num=PIN_NUM_MISO,
        .mosi_io_num=PIN_NUM_MOSI,
        .sclk_io_num=PIN_NUM_CLK,
        .quadwp_io_num=-1,
        .quadhd_io_num=-1,
        .max_transfer_sz=PARALLEL_LINES*320*2+8     //TODO: Update the max size
    };
    spi_device_interface_config_t devcfg={
        .clock_speed_hz=4*1000*1000,           //Clock out at 4 MHz
        .mode=0,                                //SPI mode 0
        .spics_io_num=PIN_NUM_CS,               //CS pin
        .queue_size=7,                          //We want to be able to queue 7 transactions at a time
        .pre_cb=lcd_spi_pre_transfer_callback,  //Specify pre-transfer callback to handle D/C line
    };
    
    //Initialize the SPI bus
    ret=spi_bus_initialize(HSPI_HOST, &amp;amp;buscfg, SPI_DMA_DISABLED);   // SPI_DMA_CH_AUTO for faster communication
    ESP_ERROR_CHECK(ret);
    
    //Attach the to the SPI bus
    ret=spi_bus_add_device(HSPI_HOST, &amp;amp;devcfg, &amp;amp;spi);
    ESP_ERROR_CHECK(ret);


    uint8_t tx_data[] = {ADDRESS,FORWARD};
    uint8_t rx_data[SIZE_BYTES];

    spi_transaction_t t_data = {
        .length = sizeof(rx_data) * 8,
        .rx_buffer = rx_data,
        .tx_buffer = tx_data
    };

    while(true)
    {
        memset(rx_data, 0x00, sizeof(rx_data));
        ret = spi_device_transmit(spi, &amp;amp;t_data);
        if (ret != ESP_OK) {
            ESP_LOGI(&amp;quot;DATA DUMP&amp;quot;, &amp;quot;Problem with spi transmission=%d&amp;quot;,ret);
        } else
        {
            ESP_LOG_BUFFER_HEXDUMP(&amp;quot;DATA DUMP&amp;quot;, rx_data, sizeof(rx_data), ESP_LOG_INFO);
        }
        vTaskDelay(4000/portTICK_PERIOD_MS);
    }
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI Slave mode - missing first bit</title><link>https://devzone.nordicsemi.com/thread/318295?ContentTypeID=1</link><pubDate>Thu, 01 Jul 2021 16:36:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4effc534-354c-4ef5-81c3-55dc267fca06</guid><dc:creator>Dmitry</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;couldn&amp;#39;t it be &lt;a href="https://github.com/esp8266/Arduino/issues/2416"&gt;this&lt;/a&gt; issue?&lt;/p&gt;
&lt;p&gt;It would be nice if you show your SPI configuration code for both sides, LA screenshot with better&amp;nbsp;resolution would also be helpful.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>