This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

SPI not working with 1.9.1 SK but with 1.8.0 (nrf9160)

Hi, 

I upgraded to the newest SDK and now our SPI Code (RFID Reader) is not working anymore. Is there something that changed with parameters or anything else? If I step back to 1.8.0 the same code works.

best regards

daniel

This is some Code from Initialisation

struct spi_buf spiTxBufStruct = {.buf = NULL, .len = 0};
struct spi_buf_set spiTxSetStruct = {.buffers = NULL, .count = 1};

struct spi_buf spiRxBufStruct = {.buf = NULL, .len = 0};
struct spi_buf_set spiRxSetStruct = {.buffers = NULL, .count = 1};

struct spi_cs_control spi_cs = {.gpio_pin = CS_PIN, .gpio_dt_flags = GPIO_ACTIVE_LOW, .delay = 0};
static const struct spi_config spi_cfg = {.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB | SPI_OP_MODE_MASTER, .frequency = 1000000, .slave = 0, .cs = &spi_cs};

this is init

uint8_t rfidInit(void) {

    spi_dev = device_get_binding("SPI_1");

    if (spi_dev == NULL) {
        LOG_ERR("[RFID] Could not get device");
        return -1;
    }
    gpio_rfid_dev = device_get_binding("GPIO_0");
    spi_cs.gpio_dev = device_get_binding("GPIO_0");

    if (!gpio_rfid_dev) {
        LOG_ERR("[RFID] INT PIN Init Error");
        return -1;
    }
    gpio_pin_configure(gpio_rfid_dev, CS_PIN, GPIO_OUTPUT);
    gpio_pin_configure(gpio_rfid_dev, IRQ_IN, GPIO_OUTPUT);
    // Pin 4 LED 3 INT OUT
    // PIN 3 LED 2 CS

    k_work_init_delayable(&timeoutRfidInitWork, timeoutRfidInitFn);
    k_work_init_delayable(&timeoutRfidScanWork, timeoutRfidScanFn);

    // rfidReset(true);
    gpio_pin_set(gpio_rfid_dev, CS_PIN, 1);
    gpio_pin_set(gpio_rfid_dev, IRQ_IN, 1);  // LED 4, INT IN
    k_sleep(K_MSEC(150));
    gpio_pin_set(gpio_rfid_dev, IRQ_IN, 0);
    k_sleep(K_MSEC(20));
    gpio_pin_set(gpio_rfid_dev, IRQ_IN, 1);
    k_sleep(K_MSEC(50));

    triggerRfidInitOk = true;
    return 0;
}

This is send routine

struct rfidData rfidSendCommand(uint8_t *tx_buffer, uint8_t tx_size) {
    struct rfidData rfidDataLocal = {.dataSize = 0, .err = 0, .dataCmd = 0};
    if (!triggerRfidInitOk)
        return rfidDataLocal;
    int err;
    uint8_t rx_buffer_short[5];
    uint8_t rx_buffer[256];
    uint8_t tx_buffer_short[5];

    spiTxBufStruct.buf = tx_buffer;
    spiTxBufStruct.len = tx_size;
    spiTxSetStruct.buffers = &spiTxBufStruct;
    spiRxBufStruct.buf = rx_buffer_short;
    spiRxBufStruct.len = sizeof(rx_buffer_short);
    spiRxSetStruct.buffers = &spiRxBufStruct;

    k_sleep(K_MSEC(4));
    err = spi_transceive(spi_dev, &spi_cfg, &spiTxSetStruct, &spiRxSetStruct);
    if (err) {
        LOG_ERR("[RFID] SPI error: %d\n", err);
    }

    tx_buffer_short[0] = 0x03;
    spiTxBufStruct.buf = tx_buffer_short;
    spiTxBufStruct.len = sizeof(tx_buffer_short);
    spiTxSetStruct.buffers = &spiTxBufStruct;
    spiRxBufStruct.buf = rx_buffer_short;
    spiRxBufStruct.len = sizeof(rx_buffer_short);
    spiRxSetStruct.buffers = &spiRxBufStruct;
    for (int i = 0; i <= 10; i++) {
        err = spi_transceive(spi_dev, &spi_cfg, &spiTxSetStruct, &spiRxSetStruct);
        if (err) {
            LOG_ERR("[RFID] SPI error: %d\n", err);
        } else if (rx_buffer_short[0] & (1 << 3)) {
            LOG_INF("RFID Data Ready (Buffer: %x Count: %d)", rx_buffer_short[0], i);
            break;
        }

        k_sleep(K_MSEC(4));
    }
    LOG_INF("Send Check %d %d %d %d", rx_buffer_short[0], rx_buffer_short[1], rx_buffer_short[2], rx_buffer_short[3]);

    tx_buffer_short[0] = 0x02;
    spiTxBufStruct.buf = tx_buffer_short;
    spiTxBufStruct.len = sizeof(tx_buffer_short);
    spiTxSetStruct.buffers = &spiTxBufStruct;
    spiRxBufStruct.buf = rx_buffer;
    spiRxBufStruct.len = sizeof(rx_buffer);
    spiRxSetStruct.buffers = &spiRxBufStruct;

    for (int i = 0; i <= 0; i++) {
        err = spi_transceive(spi_dev, &spi_cfg, &spiTxSetStruct, &spiRxSetStruct);
        if (err) {
            LOG_ERR("[RFID] SPI error: %d", err);
        } else {
            // 0x87 = no response/ timeout for receive
            if (rx_buffer[1] != 0x80 && rx_buffer[1] != 0x90) {
                if (rx_buffer[1] != 0x87 && rx_buffer[1] != 0x00 && rx_buffer[1] <= 0x90) {
                    LOG_WRN("[RFID] Read Error: %x", rx_buffer[1]);
                    rfidReaderReset();
                }
                if (rx_buffer[1] > 0x90) {
                    // LOG_ERR("[RFID] Read Error for Reset: B0:%x B1:%x Size:%x", rx_buffer[0], rx_buffer[1], rx_buffer[2]);
                    rfidReaderReset();
                }
                rfidDataLocal.err = RFID_NO_RESULT;
                return rfidDataLocal;
            }
            rfidDataLocal.dataSize = rx_buffer[2];
            rfidDataLocal.dataCmd = rx_buffer[1];
            rfidDataLocal.txCmd = tx_buffer[1];

            char str[128];
            sprintf(str, "%s ", "Data:");
            for (int i = 3; i < rfidDataLocal.dataSize + 3; i++) {
                rfidDataLocal.rawData[i - 3] = (uint8_t)rx_buffer[i];
                sprintf(str + strlen(str), "%d ", (uint8_t)rx_buffer[i]);
            }
            LOG_INF("[RFID] RX CMD: %d %s Size: %d", rx_buffer[1], str, rfidDataLocal.dataSize);
        }
    }
    LOG_DBG("[RFID] TX(C: %d S: %d) RX(C: %d S: %d)", tx_buffer[1], tx_buffer[2], rx_buffer[1], rx_buffer[2]);
    return rfidDataLocal;
}

bool rfidSendHaltToTag(void) {
    uint8_t tx_data[10] = {0x00, 0x04, 0x03, 0x50, 0x00, 0x28};
    rfidSendCommand(tx_data, sizeof(tx_data));
    return true;
}

void rfidSetType5(void) {
    LOG_INF("[RFID] Set Type 5");
    if (!triggerRfidInitOk)
        return;
    // RFID Init Protocol Select (Controlbyte, Command (0x02), Length(0x02), Data)
    uint8_t tx_data[10] = {0x00, 0x02, 0x02, 0x01, 0x01};
    rfidSendCommand(tx_data, sizeof(tx_data));
}

void rfidSetType2(void) {
    LOG_INF("[RFID] Set Type 2");
    if (!triggerRfidInitOk)
        return;
    // RFID Init Protocol Select (Controlbyte, Command (0x02), Length(0x02), Data)
    uint8_t tx_data[10] = {0x00, 0x02, 0x02, 0x02, 0x00};
    rfidSendCommand(tx_data, sizeof(tx_data));
    uint8_t tx_data2[10] = {0x00, 0x09, 0x04, 0x3A, 0x00, 0x58, 0x04};
    rfidSendCommand(tx_data2, sizeof(tx_data2));
    uint8_t tx_data3[10] = {0x00, 0x09, 0x04, 0x68, 0x01, 0x01, 0xD1};  // D1 Default
    rfidSendCommand(tx_data3, sizeof(tx_data3));
}

this is overlay file

//RFID SPI Bus
&spi1 {
        compatible = "nordic,nrf-spim";
	status = "okay";
	sck-pin = <4>;
	mosi-pin = <3>;
	miso-pin = <2>;
	//cs-gpios = <&gpio0 1 GPIO_ACTIVE_LOW>;
};

Parents
  • Does the project build? Or is this a runtime issue? I noticed a problem compiling when the spi cs-gpoios line is used in the dts file. 

  • its a runtime issue. 

    //cs-gpios = <&gpio0 1 GPIO_ACTIVE_LOW>; this line is also uncommented in my code. it was just copy-paste.

    it compiles with 1.8.0 with no problem and works and exactly same code with 1.9.1 works but the communication returns no data. I could check with probe if the bus still works etc. but the problem should be more simple because like I said 1.8.0 works just fine and also the data response works

  • Hi, 

    The only related change I could think of would be this. All CS lines are now configured when the driver is initialized, not in configure() routine when spi_transcieve() is called as it was earlier. 

    Can you try to add cs-gpios to the SPI node in the overlay?

    regards

    Jared 

  • Hi,

    I added this line to overlay file:

    cs-gpios = <&gpio0 1 GPIO_ACTIVE_LOW>;

    so now its:

    &spi1 {
        compatible = "nordic,nrf-spim";
    	status = "okay";
    	sck-pin = <4>;
    	mosi-pin = <3>;
    	miso-pin = <2>;
    	cs-gpios = <&gpio0 1 GPIO_ACTIVE_LOW>;
    };

    but it did not change. still working with 1.8.0 but not in 1.9.1

Reply
  • Hi,

    I added this line to overlay file:

    cs-gpios = <&gpio0 1 GPIO_ACTIVE_LOW>;

    so now its:

    &spi1 {
        compatible = "nordic,nrf-spim";
    	status = "okay";
    	sck-pin = <4>;
    	mosi-pin = <3>;
    	miso-pin = <2>;
    	cs-gpios = <&gpio0 1 GPIO_ACTIVE_LOW>;
    };

    but it did not change. still working with 1.8.0 but not in 1.9.1

Children
  • Ok, could you probe the SPI lines and see if CS is correctly asserted, and the clock is sent on SCLK? 

  • At the moment I cant use the lab equipment, I am locked at home for the next 5 days :D

    With multimeter I can see different behaviour in the CS Line. Most of the time its low now and also on transmit finished it stays low. in 1.8.0 its most of the time high (makes sense to me) and after transmit its also high

    Edit: So as a conclusion I can say the CS line gets initialized but it behaves different in 1.9.1 to 1.8.0. Because I can clearly see in both FW that it reacts to the code

  • Okay after removing all lines with:

    gpio_pin_set(gpio_rfid_dev, CS_PIN, 1);
    and the line
    gpio_pin_configure(gpio_rfid_dev, CS_PIN, GPIO_OUTPUT);
    it works now. so it seems this "double initialization" was fine in the old SDK but in 1.9.1 its an issue.
Related