<?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>Error When Compiling I2C Code</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/107081/error-when-compiling-i2c-code</link><description>Hello, 
 I wrote a sample code to read 8bits from an I2C device as below. 
 
 But I get the following error when I try to compile. 
 
 I&amp;#39;m quite new to this platform and I appreciate any help with this. Thank you.</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 09 Jan 2024 17:04:03 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/107081/error-when-compiling-i2c-code" /><item><title>RE: Error When Compiling I2C Code</title><link>https://devzone.nordicsemi.com/thread/463417?ContentTypeID=1</link><pubDate>Tue, 09 Jan 2024 17:04:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:425c46b5-6cd6-402f-a903-79da2bdbc239</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;If you only read 255 (b11111111) it does point to some communication or sensor issue, as this probably means the MISO line is constantly high, rather than receiving actual data.&amp;nbsp;&lt;/p&gt;
[quote user="Asanka"]Only a handful of available GPIOs respond when I try to control them. Maybe you can suggest a reason why this is happening?[/quote]
&lt;p&gt;The nRF5340 chip on the nRF7002DK uses a lot of GPIO&amp;#39;s for connecting the nRF7002, external flash memory, buttons, LED&amp;#39;s and other things. Possibly the GPIO&amp;#39;s you are unable to control are already assigned to some of these other functions?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can get a visual representation of your board file in VSCode which shows you at a glance what the various IO pins are assigned to. Not sure you have tried this out?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It should look something like this:&lt;br /&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1704819834561v2.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Error When Compiling I2C Code</title><link>https://devzone.nordicsemi.com/thread/463041?ContentTypeID=1</link><pubDate>Mon, 08 Jan 2024 00:23:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8c00eab5-f103-4f58-9e9e-33cec14f3227</guid><dc:creator>Asanka</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I think I figured it out. I managed to get SPI working after setting up the code as below.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;Read successful. Data: 255
Read successful. Data: 255
Read successful. Data: 255
Read successful. Data: 255&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define SPI0_NODE DT_NODELABEL(device)

const struct spi_config spi_cfg = {
    .frequency = DT_PROP(DT_NODELABEL(spi3), clock_frequency),
    .operation = SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8),
    .cs = {
        .gpio = {
            .port = DEVICE_DT_GET(DT_SPI_DEV_CS_GPIOS_CTLR(DT_NODELABEL(device))),
            .pin = DT_SPI_DEV_CS_GPIOS_PIN(DT_NODELABEL(device)),
            .dt_flags = DT_SPI_DEV_CS_GPIOS_FLAGS(DT_NODELABEL(device)),
        },
        .delay = 400,
    },
};



int main(void)
{
    const struct device *dev_spi;
    dev_spi = DEVICE_DT_GET(DT_NODELABEL(spi3));

    if (!device_is_ready(dev_spi)) {
        printk(&amp;quot;Device not ready....&amp;quot;);
    } else {
        printk(&amp;quot;Device ready....&amp;quot;);
    }

    printk(&amp;quot;\n&amp;quot;);

    while (true) {
        uint8_t x = 0;

        struct spi_buf rx_bufs = { &amp;amp;x, 8 };
        struct spi_buf_set rx = { &amp;amp;rx_bufs, 1 };

        //gpio_pin_set_dt(&amp;amp;spi_cfg.cs.gpio, 0);

        uint8_t status = spi_read(dev_spi, &amp;amp;spi_cfg, &amp;amp;rx);

        //gpio_pin_set_dt(&amp;amp;spi_cfg.cs.gpio, 1);

        if (status == 0) {
            printk(&amp;quot;Read successful. Data: %d&amp;quot;, x);
        } else {
            printk(&amp;quot;Read failed. Status: %x&amp;quot;, status);
        }

        printk(&amp;quot;\n&amp;quot;);

        k_sleep(K_MSEC(1000));
    }
    
    return 0;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;DT:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="json"&gt;&amp;amp;spi3 {
    compatible = &amp;quot;nordic,nrf-spim&amp;quot;;
    status = &amp;quot;okay&amp;quot;;
    cs-gpios = &amp;lt; &amp;amp;gpio1 1 (GPIO_ACTIVE_HIGH || GPIO_OPEN_DRAIN) &amp;gt;;
    clock-frequency = &amp;lt; 10000000 &amp;gt;;

    device: device@0 {
        compatible = &amp;quot;spi-device&amp;quot;;
        reg = &amp;lt; 0 &amp;gt;;
        spi-max-frequency = &amp;lt; 20000000 &amp;gt;;
    };
};&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The sensor seems to be not working because there&amp;#39;s a damaged terminal. However, the ADC that I&amp;#39;ve used seem to be communicating correctly.&lt;/p&gt;
&lt;p&gt;In the case of GPIO, strangely enough, I figured out that only several GPIOs can be used for IO purposes. I am not sure what the reason for this is and there doesn&amp;#39;t seem to be a pattern. This is the code for it.&lt;/p&gt;
&lt;p&gt;Code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/drivers/gpio.h&amp;gt;

#define INDICATOR_LED_NODE DT_NODELABEL(indicator_led)

#define INITIALIZE_TIMEOUT 1

static const struct gpio_dt_spec pin_led = GPIO_DT_SPEC_GET(INDICATOR_LED_NODE, gpios);

int main(void)
{
        printk(&amp;quot;Initializing hardware...\n&amp;quot;);
        initialize();

        gpio_pin_set_dt(&amp;amp;pin_engage, 1);

        printk(&amp;quot;Enabled.\nRunning.&amp;quot;);

        while (true)
        {
                printk(&amp;quot;.&amp;quot;);
                gpio_pin_toggle_dt(&amp;amp;pin_led);
                k_sleep(K_MSEC(1000));
                gpio_pin_toggle_dt(&amp;amp;pin_led);
                k_sleep(K_SECONDS(1));
        }
        

        return 0;
}

void initialize() {
        int error_flag = 0;

        do {
                error_flag += initialize_device(pin_led, GPIO_OUTPUT);

                if (error_flag != 0) {
                        printk(&amp;quot;One or more devices could not be initialized. Will retry in %d seconds...\n&amp;quot;, INITIALIZE_TIMEOUT);
                        k_sleep(K_MSEC(INITIALIZE_TIMEOUT * 1000));
                        error_flag = 0;
                } else {
                        printk(&amp;quot;Hardware initialized.\n&amp;quot;);
                }
        } while (error_flag != 0);
}

int initialize_device(const struct gpio_dt_spec device, gpio_flags_t flags) {
	if(!device_is_ready(device.port)) {
                printk(&amp;quot;ERROR: %s could not be initialized. Device is not ready.&amp;quot;, device.port-&amp;gt;name);

		return -1;
	}

        int ret = gpio_pin_configure_dt(&amp;amp;device, flags);
	if (ret) {
                printk(&amp;quot;ERROR: %s could not be initialized. Pin configuration failed.&amp;quot;, device.port-&amp;gt;name);

		return -2;
	}

        return 0;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;DT:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/{
	board_interface {
		compatible = &amp;quot;gpio-leds&amp;quot;;

		indicator_led: indicator_led {
			gpios = &amp;lt;&amp;amp;gpio0 26 (GPIO_OPEN_DRAIN)&amp;gt;;
			label = &amp;quot;Control indicator LED&amp;quot;;
		};
	};
};&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The traces are pulled up on the PCB itself so it should be an open drain or push-pull from the microcontroller. Only a handful of available GPIOs respond when I try to control them. Maybe you can suggest a reason why this is happening? Thanks for the support! Couldn&amp;#39;t have done it without your code and suggestions.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Error When Compiling I2C Code</title><link>https://devzone.nordicsemi.com/thread/463005?ContentTypeID=1</link><pubDate>Sat, 06 Jan 2024 05:01:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d37555b5-4a69-4119-b7bc-1ace1ceb1ef6</guid><dc:creator>Asanka</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I think you&amp;#39;re right. It seems to be an SPI device. Since it had only CLOCK and DATA, I thought it was I2C. I wrote the code for an SPI device but it doesn&amp;#39;t initialize. Can you please let me know what I&amp;#39;m doing wrong?&lt;/p&gt;
&lt;p&gt;The device boots and then prints &amp;#39;Device not ready...&amp;#39; and reboots.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;Device not ready...*** Booting nRF Connect SDK v2.5.0 ***
Device not ready...*** Booting nRF Connect SDK v2.5.0 ***
Device not ready...*** Booting nRF Connect SDK v2.5.0 ***
Device not ready...*** Booting nRF Connect SDK v2.5.0 ***
Device not ready...*** Booting nRF Connect SDK v2.5.0 ***
Device not ready...*** Booting nRF Connect SDK v2.5.0 ***
Device not ready...*** Booting nRF Connect SDK v2.5.0 ***
Device not ready...*** Booting nRF Connect SDK v2.5.0 ***
Device not ready...*** Booting nRF Connect SDK v2.5.0 ***
Device not ready...*** Booting nRF Connect SDK v2.5.0 ***
Device not ready...*** Booting nRF Connect SDK v2.5.0 ***
Device not ready...*** Booting nRF Connect SDK v2.5.0 ***
Device not ready...&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/drivers/spi.h&amp;gt;

#define SPI0_NODE DT_NODELABEL(device)

struct spi_cs_control spi_device =
        SPI_CS_CONTROL_INIT(SPI0_NODE, 2);

static const struct spi_dt_spec dev_spi = SPI_CS_GPIOS_DT_SPEC_GET(spi_device);

int main(void)
{
        while (true) {
                if (!spi_is_ready_dt(&amp;amp;dev_spi)) {
                        printk(&amp;quot;Device not ready....&amp;quot;);
                } else {
                        printk(&amp;quot;Device ready....&amp;quot;);
                }

                uint8_t buf;

                spi_read_dt(&amp;amp;dev_spi, &amp;amp;buf);

                printk(&amp;quot; Data %x\n&amp;quot;, buf);
        }
        
        return 0;
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;DT:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="json"&gt;&amp;amp;spi3 {
    compatible = &amp;quot;nordic,nrf-spim&amp;quot;;
    status = &amp;quot;okay&amp;quot;;
    cs-gpios = &amp;lt;&amp;amp;gpio0 1 GPIO_ACTIVE_HIGH&amp;gt;;

    device: device@0 {
        compatible = &amp;quot;spi-device&amp;quot;;
        reg = &amp;lt; 0 &amp;gt;;
        spi-max-frequency = &amp;lt; 40000 &amp;gt;;
    };
};&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Error When Compiling I2C Code</title><link>https://devzone.nordicsemi.com/thread/462953?ContentTypeID=1</link><pubDate>Fri, 05 Jan 2024 15:31:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8b51b06a-9ae3-49c9-8ea0-278cbdd1af44</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;That looks like an SPI device to me, not an I2C device. If this is the case the I2C interface will struggle to communicate with it, since it will not receive any acknowledge.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Error When Compiling I2C Code</title><link>https://devzone.nordicsemi.com/thread/462930?ContentTypeID=1</link><pubDate>Fri, 05 Jan 2024 14:31:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:989b9736-fadf-48fe-8d27-9637f50b1cbd</guid><dc:creator>Asanka</dc:creator><description>&lt;p&gt;Thank you. This is the device that I&amp;#39;m trying to control. It&amp;#39;s an ADC.&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1704464981904v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;I will take a look at the overlays. I appreciate the support.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Error When Compiling I2C Code</title><link>https://devzone.nordicsemi.com/thread/462925?ContentTypeID=1</link><pubDate>Fri, 05 Jan 2024 14:26:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cef981a4-3a99-4fa6-ae4b-bc933ed2fcfe</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Asanka&lt;/p&gt;
&lt;p&gt;Why would an I2C sensor have a chip select line?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Chip select is used by SPI devices, I haven&amp;#39;t seen any I2C devices use it.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regardless I don&amp;#39;t think your overlay is quite correct.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I did something similar in a hobby project of mine, where I needed two output pins that could be manually controlled.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can see the overlay &lt;a href="https://github.com/ovrebekk/ncs-nrf5-cam-tl-controller/blob/master/boards/nrf52dk_nrf52832.overlay"&gt;here&lt;/a&gt;, and the relevant source &lt;a href="https://github.com/ovrebekk/ncs-nrf5-cam-tl-controller/blob/master/src/cam_tl_control.c#L8"&gt;here&lt;/a&gt; and &lt;a href="https://github.com/ovrebekk/ncs-nrf5-cam-tl-controller/blob/master/src/cam_tl_control.c#L26-L37"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Error When Compiling I2C Code</title><link>https://devzone.nordicsemi.com/thread/462770?ContentTypeID=1</link><pubDate>Thu, 04 Jan 2024 17:49:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:334d89bc-b5a0-414d-9f75-aff410aa0cca</guid><dc:creator>Asanka</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Thank you so much. I went through the exercise and implemented I2C and it worked! At least I don&amp;#39;t get any errors for now. However, to complete the communication, the other device must receive the chip select pin as LOW. I went ahead and tried to use the GPIO 0.00 to use as a chip select. But the code doesn&amp;#39;t continue after:&lt;/p&gt;
&lt;div style="background-color:#000c18;color:#6688cc;font-family:&amp;#39;Droid Sans Mono&amp;#39;, &amp;#39;monospace&amp;#39;, monospace;font-size:14px;font-weight:normal;line-height:19px;white-space:pre;"&gt;
&lt;div&gt;&lt;span style="color:#ddbb88;"&gt;gpio_is_ready_dt&lt;/span&gt;&lt;span style="color:#6688cc;"&gt;(&lt;/span&gt;&lt;span style="color:#225588;"&gt;&amp;amp;&lt;/span&gt;&lt;span style="color:#6688cc;"&gt;cs&lt;/span&gt;&lt;span style="color:#6688cc;"&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;I am working on the Make It Matter project and I&amp;#39;m trying to complete my firmware. This is why I didn&amp;#39;t yet complete this course as there&amp;#39;s only a limited number of days left.&lt;/p&gt;
&lt;p&gt;My code is as follows:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/drivers/i2c.h&amp;gt;
#include &amp;lt;zephyr/drivers/gpio.h&amp;gt;

#define I2C0_NODE DT_NODELABEL(mysensor)
#define CS0_NODE DT_NODELABEL(cs0)

static const struct i2c_dt_spec dev_i2c = I2C_DT_SPEC_GET(I2C0_NODE);
static const struct gpio_dt_spec cs =
	GPIO_DT_SPEC_GET_OR(DT_NODELABEL(cs0), gpios, {0});


int main(void)
{
        if (!device_is_ready(dev_i2c.bus)) {
	        printk(&amp;quot;I2C bus %s is not ready!\n\r&amp;quot;,dev_i2c.bus-&amp;gt;name);
	        return;
        }

	if (!gpio_is_ready_dt(&amp;amp;cs)) {
		printf(&amp;quot;The CS GPIO port is not ready.\n\r&amp;quot;);
		return 0;
	}

        gpio_pin_configure_dt(&amp;amp;cs, GPIO_OUTPUT);

        while(true) {
                uint8_t data;
                int ret = i2c_read_dt(&amp;amp;dev_i2c, &amp;amp;data, sizeof(data));

                gpio_pin_set_dt(&amp;amp;cs, 0);

                if(ret != 0){
	                printk(&amp;quot;Failed to read from I2C device address %x.\n\r&amp;quot;, dev_i2c.addr);
                } else {
                        printk(&amp;quot;Data %x&amp;quot;, data);
                }

                gpio_pin_set_dt(&amp;amp;cs, 1);
        }

        return 0;
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The overlay file is below:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="json"&gt;&amp;amp;i2c1 {
    compatible = &amp;quot;nordic,nrf-twim&amp;quot;;
    status = &amp;quot;okay&amp;quot;;

    mysensor: mysensor@4a{
        compatible = &amp;quot;i2c-device&amp;quot;;
        reg = &amp;lt; 0x4a &amp;gt;;
        label = &amp;quot;MYSENSOR&amp;quot;;
    };
};

&amp;amp;gpio0 {
    compatible = &amp;quot;nordic,nrf-gpio&amp;quot;;
    status = &amp;quot;okay&amp;quot;;

    cs0 {
        gpio-hog;
        gpios = &amp;lt;0 GPIO_ACTIVE_LOW&amp;gt;;
        output-high;
    };
};&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Any suggestion is highly appreciated.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Error When Compiling I2C Code</title><link>https://devzone.nordicsemi.com/thread/462650?ContentTypeID=1</link><pubDate>Thu, 04 Jan 2024 10:34:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1a27e316-75f6-44aa-92a3-816383f07e4a</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you are new to Nordic I would strongly check out the &lt;a href="https://academy.nordicsemi.com/"&gt;Nordic Developer Academy&lt;/a&gt;. The introductory course even includes a &lt;a href="https://academy.nordicsemi.com/courses/nrf-connect-sdk-fundamentals/lessons/lesson-6-serial-com-i2c/"&gt;chapter dedicated to I2C&lt;/a&gt; which should help you get started.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I expect the problem with your sample is that you haven&amp;#39;t created an I2C sensor in your overlay. The I2C_DT_SPEC_GET() macro will not work on the I2C top node itself, you have to use it with one of the sensors (or buses) on the I2C interface. The devacademy chapter will explain this in more detail.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>