Translating previously working code to NCS v2.2.0

I'm working on a project with the AD5940, and it looks like this case did the job.

 nrf52832+SPI+AD5940 cannot get ad5940id

 

However, I am using NCS v2.2.0 and not nRF5 v17.1 - so code translation is required.

My current code is as follows:

Overlay:

/ {
    gpio_custom {
        compatible = "gpio-keys";
        gpiocu0: gpiocu0 {
            label = "GPIO_D2";
            gpios = <&gpio1 4 0>;/* D2 */
        };
        gpiocu1: gpiocu1 {
            label = "GPIO_D5";
            gpios = <&gpio1 7 0>;/* D5 */
        };
        gpiocu2: gpiocu2 {
            label = "GPIO_D6";
            gpios = <&gpio1 8 0>;/* D6 */
        };
        ad5940reset: ad5940reset {
            label = "AD5940_RESET";
            gpios = <&gpio1 12 0>;/* D10 */
        };
    };
    aliases {
        gpiod2 = &gpiocu0;
        gpiod5 = &gpiocu1;
        gpiod6 = &gpiocu2;
        reset5940 = &ad5940reset;
    };
};


&arduino_spi {
    status = "okay";
    ad5940:ad5940@0{
        compatible = "adi,adxl345";
        // compatible = "adi,adxl372";
        reg = <0>;
        spi-max-frequency = <DT_FREQ_M(4)>;
        friendly-name = "ad5940";
        // int1-gpios = <&gpio1 8 0>;
    };
};

nRF adjustment(nRF_MCU_Port.c):

#include "ad5940.h"
#include "nRF_MCU_Port.h"
#include <zephyr/device.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/logging/log.h>
#include <inttypes.h>
#include <dk_buttons_and_leds.h>

LOG_MODULE_REGISTER(BIA_AD5940);

volatile static uint32_t ucInterrupted = 0; /* Flag to indicate interrupt occurred */

#define AD5940_SPI DT_NODELABEL(ad5940)
static const struct spi_dt_spec dev_spi = SPI_DT_SPEC_GET(AD5940_SPI, SPI_WORD_SET(8) | SPI_MODE_GET(0), 0);

/* GPIO Configuration*/

#define D6 DT_ALIAS(gpiod6)
#if !DT_NODE_HAS_STATUS(D6, okay)
#error "Unsupported board: gpiod6 devicetree alias is not defined"
#endif

/*For the XXRst functions*/
#define R5940 DT_ALIAS(reset5940)
#if !DT_NODE_HAS_STATUS(R5940, okay)
#error "Unsupported board: reset_5940 devicetree alias is not defined"
#endif



uint8_t selector = 0;

static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(D6, gpios, {0});

/*AD5940 Reset Pin*/
static const struct gpio_dt_spec reset_dev = GPIO_DT_SPEC_GET_OR(R5940, gpios, {0});
/*Interrupt Special settings*/
static struct gpio_callback button_cb_data;

bool AD5940_SPI_SETUP(void)
{
    if (!device_is_ready(dev_spi.bus))
    {
        // LOG_ERR("Couldn't find %s\n", dev_spi.bus->name);
        printk("Couldn't find %s\n", dev_spi.bus->name);

        return false;
    }
    else
    {
        // LOG_WRN("SPI Initialized\n\r");
        bool success = (AD5940_ReadReg(REG_AFECON_ADIID) == AD5940_ADIID) ? true : false;
        if(success)
        printk("SPI Initialized\n\r");
        else
        // printk("SPI Initialization failed\n\r");
        return success;
    }
}

void Ext_Int0_Handler(const struct device *dev, struct gpio_callback *cb,
                      uint32_t pins)
{
    //    /* Clear interrupt flag */
    ucInterrupted = 1;
    // LOG_INF("Ext_Int0_Handler\n\r");
    // todo: ADD BLE and EMG interrupt handling here - or not - depends on the application
    /* This example just set the flag and deal with interrupt in AD5940Main function. It's your choice to choose how to process interrupt. */
}

//GPIO Free "interrupt"
void set_interrupt(void)
{
    ucInterrupted = 1;
}
void AD5940_INT_SETUP(void)
{
    int ret;
    if (!device_is_ready(button.port))
    {
        printk("Error: button device %s is not ready\n",
               button.port->name);
        return;
    }

    ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
    if (ret != 0)
    {
        printk("Error %d: failed to configure %s pin %d\n",
               ret, button.port->name, button.pin);
        return;
    }

    ret = gpio_pin_interrupt_configure_dt(&button,
                                          GPIO_INT_EDGE_TO_ACTIVE);
    if (ret != 0)
    {
        printk("Error %d: failed to configure interrupt on %s pin %d\n",
               ret, button.port->name, button.pin);
        return;
    }

    gpio_init_callback(&button_cb_data, Ext_Int0_Handler, BIT(button.pin));
    gpio_add_callback(button.port, &button_cb_data);
    printk("Set up button at %s pin %d\n", button.port->name, button.pin);
    // LOG_INF("Set up button at %s pin %d\n", button.port->name, button.pin);
}

// void AD5940_GUI_SETUP(void)
// {
//     if (!device_is_ready(dev_led0.port->name))
//     {
//         // LOG_ERR("Couldn't find %s\n", dev_led0.port->name);
//         printk("Couldn't find %s\n", dev_led0.port->name);
//         return;
//     }
//     else
//     {
//         // LOG_WRN("GPIO Initialized\n\r");
//         printk("GPIO Initialized\n\r");
//         gpio_pin_configure_dt(&dev_led0, GPIO_OUTPUT_INIT_LOW);
//     }
// }

void AD5940_RESET_SETUP(void)
{
    if (!device_is_ready(reset_dev.port))
    {
        // LOG_ERR("Couldn't find %s\n", reset_dev.port->name);
        printk("Couldn't find %s\n", reset_dev.port->name);
        return;
    }
    else
    {
        // LOG_WRN("GPIO Initialized\n\r");
        printk("GPIO Initialized\n\r");
        gpio_pin_configure_dt(&reset_dev, GPIO_OUTPUT);
    }
}

void AD5940_ReadWriteNBytes(unsigned char *pSendBuffer, unsigned char *pRecvBuff, unsigned long length)
{
    const struct spi_buf tx_buf = {
        .buf = pSendBuffer,
        .len = length};
    const struct spi_buf_set tx = {
        .buffers = &tx_buf,
        .count = 1};
    const struct spi_buf rx_buf = {
        .buf = pRecvBuff,
        .len = length};
    const struct spi_buf_set rx = {
        .buffers = &rx_buf,
        .count = 1};

    if (spi_transceive_dt(&dev_spi, &tx, &rx))
    {

        // LOG_ERR("SPI transceive failed\n");
        printk("SPI transceive failed\n");
        return;
    }
}

void AD5940_CsClr(void)
{
    //   Do nothing - built in inside SPI API
}

void AD5940_CsSet(void)
{
    //   Do nothing - built in inside SPI API
}

void AD5940_RstSet(void)
{
    // Check it - maybe it has special flag
    if (gpio_pin_get_dt(&reset_dev) == 0)
    {
        // LOG_INF("Reset pin set\n");
        printk("nRF_MCU_Port: Reset pin set\n");
        gpio_pin_set_dt(&reset_dev, 1);
    }
}

void AD5940_RstClr(void)
{
    // Check it - maybe it has special flag
    if (gpio_pin_get_dt(&reset_dev) == 1)
    {
        // LOG_INF("nRF_MCU_Port: Reset pin clear\n");
        printk("nRF_MCU_Port: Reset pin clear\n");
        gpio_pin_set_dt(&reset_dev, 0);
    }
}

void AD5940_DATA_GUI(void)
{
    // gpio_pin_toggle_dt(&dev_led0);
}

void AD5940_Delay10us(uint32_t time)
{
    k_usleep(time * 10);
}

uint32_t AD5940_GetMCUIntFlag(void)
{
    return ucInterrupted;
}

uint32_t AD5940_ClrMCUIntFlag(void)
{
    ucInterrupted = 0;
    return 1;
}

uint32_t AD5940_MCUResourceInit(void)
{
    // LOG_WRN("AD5940_MCUResourceInit\n\r");
    printk("nRF_MCU_Port: AD5940_MCUResourceInit\n\r");
    AD5940_INT_SETUP();
    // AD5940_GUI_SETUP();
    AD5940_RESET_SETUP();
    AD5940_RstSet();
    if(!AD5940_SPI_SETUP())
    {
        // LOG_ERR("SPI Initialization failed\n\r");
        printk("SPI Initialization failed\n\r");
        while(1);
    }
    return 0;
}

nRF_MCU_Port.h

#ifndef NRF_MCU_PORT_H_
#define NRF_MCU_PORT_H_
uint32_t AD5940_MCUResourceInit(void);
void AD5940_DATA_GUI(void);
void set_interrupt(void);
#endif /* NRF_MCU_PORT_H_ */

Currently, it looks like the reset and interrupts are initializing well. The spi instance is "initialized," but I can't use the spi port as a sensor.

The evaluation board for this component is based on the Arduino R3 pinout.

The component is functioning ( I tested with the native MCU)

This is urgent for our project schedule; please help!

Thanks to all the helpers!

Parents Reply Children
  • When providing information about the issue, please include concrete details, such as logs, images and specifications.

    For example:

    Meh said:
    see any visible connection

    Where do you see this from?
    Logs?
    What would you expect to see?

    Meh said:
    I saw some activity, with some responses from the MISO

    Please include an image of the logic analyzer trace, so I can the response myself as well.

    Meh said:
    it didn't initialize the device's setup correctly.

    How can you tell that it is not initialized correctly?
    What would you expect to see if it worked?

    Also, have you done debugging on your code?
    Do you see any error codes returned?
    Or does the code halt?
    Have you debugged your code and seen that it goes through the expected steps?

    Regards,
    Sigurd Hellesvik

  • Where do you see this from?
    Logs?
    What would you expect to see?

    My SPI configuration function is as follows - and the log was the Initialized from here:

    #define AD5940_SPI DT_NODELABEL(ad5940)
    static const struct spi_dt_spec dev_spi = SPI_DT_SPEC_GET(AD5940_SPI, SPI_WORD_SET(8) | SPI_MODE_GET(0), 0);
    
    void AD5940_SPI_SETUP(void)
    {
        if (!device_is_ready(dev_spi.bus))
        {
            LOG_ERR("Couldn't find %s\n", dev_spi.bus->name);
    
            return;
        }
        else
        {
            LOG_WRN("SPI Initialized\n\r");
    
            return;
        }
    }

    How can you tell that it is not initialized correctly?

    Because I got a timeout error, it can be only if I try to read specific registers without success. When I tried another register, it gave me the same answer failed.

    Regarding the debugging - I didn't get much informative data from there. I just added breaks for rationalizing the logger.

    SPI logs:

    Setup_AD5940.zip

    (You can open it with PulseView)

    Some key moments in addition:

    Post interrupt(When it needs to send data back):

    When I tried to monitor the original hardware host, it looked like this:

    And while sending data:

  • Thank you for the SPI traces.
    It looks like  something happens from the nRF, but the sensor does not respond.

    I have some more questions:

    Meh said:
    When I tried to monitor the original hardware host, it looked like this:

    What is the "original hardware host"?

    From your trace, we can see that the Master tries to send something:

    Can you find which call your application does when this happens?

    Also, how have you connected the sensor to your nRF?

    Regards,
    Sigurd Hellesvik

  • Yes, I did - the sensor is formatted as an Arduino shield, so my logic analyzer is hooked up to the GPIOs of the sensor, which is hooked to the nRF exposed pins.

    Regarding the response from the MOSI, I'll try to find it.

  • Hello,
    I am facing the same issue while porting it to NRF52833DK.
    I stacked the AD5940 Shield (since it is in Arduino R3 Form Factor) onto NRF52833DK.
    My AD5940 seems unresponsive, could you share your solution if you have found it ?

Related