<?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>Internal temperature measurement with Zephyr</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/95829/internal-temperature-measurement-with-zephyr</link><description>Hi, 
 using the example from STM32 Temperature Sensor I was able to create a working program that displayed the MCU die temperature in degrees Celsius every second. 
 
 After measuring the duration of the sensor_sample_fetch() function, it turned out</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 20 Jan 2023 13:56:32 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/95829/internal-temperature-measurement-with-zephyr" /><item><title>RE: Internal temperature measurement with Zephyr</title><link>https://devzone.nordicsemi.com/thread/405850?ContentTypeID=1</link><pubDate>Fri, 20 Jan 2023 13:56:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c24eaf07-9692-4e23-b80b-ccc4c638038f</guid><dc:creator>Peter_S</dc:creator><description>&lt;p&gt;Hi, J&amp;oslash;rgen here is code that uses &lt;strong&gt;sensor_sample_fetch_chan()&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr/zephyr.h&amp;gt;
#include &amp;lt;zephyr/device.h&amp;gt;
#include &amp;lt;zephyr/drivers/sensor.h&amp;gt;
#include &amp;lt;zephyr/logging/log.h&amp;gt;

LOG_MODULE_REGISTER(Info, LOG_LEVEL_INF);

static const struct device *temp_dev = DEVICE_DT_GET_ANY(nordic_nrf_temp);

/*** Time measure functions ***/
void Duration_Timer_Init()
{
	NRF_TIMER4-&amp;gt;TASKS_START = 1;
	NRF_TIMER4-&amp;gt;PRESCALER = 1;
}

void Duration_Timer_Start()
{
	NRF_TIMER4-&amp;gt;TASKS_CLEAR = 1;
	NRF_TIMER4-&amp;gt;TASKS_CAPTURE[0] = 1;
}

void Duration_Timer_Stop()
{
	NRF_TIMER4-&amp;gt;TASKS_CAPTURE[1] = 1;
	uint32_t start = NRF_TIMER4-&amp;gt;CC[0];
	uint32_t stop = NRF_TIMER4-&amp;gt;CC[1];
	LOG_INF(&amp;quot;Function duration [us]: %d&amp;quot;, (stop - start) &amp;gt;&amp;gt; 3);	
}

/*** Performs a temperature measurement of the MCU and returns its value in degrees C ***/
static int16_t Temperature_Sensor_Get_Data()
{
	struct sensor_value temp_value;
	int err;

	Duration_Timer_Start();
    err = sensor_sample_fetch_chan(temp_dev, SENSOR_CHAN_DIE_TEMP);
	Duration_Timer_Stop();

    if(err) 
        LOG_ERR(&amp;quot;sensor_sample_fetch failed with error: %d&amp;quot;, err);

    err = sensor_channel_get(temp_dev, SENSOR_CHAN_DIE_TEMP, &amp;amp;temp_value);
    if(err) 
        LOG_ERR(&amp;quot;sensor_channel_get failed with error: %d&amp;quot;, err);

    return temp_value.val1;
}

/*** Main ***/
void main(void)
{
	Duration_Timer_Init();

	while(1)
	{
		k_msleep(1000);
		LOG_INF(&amp;quot;MCU temperature [C]: %d&amp;quot;, Temperature_Sensor_Get_Data());
	}
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;and here is the log:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;[00:00:02.001,159] &amp;lt;inf&amp;gt; Info: Function duration [us]: 480
[00:00:02.001,190] &amp;lt;inf&amp;gt; Info: MCU temperature [C]: 23
[00:00:03.001,739] &amp;lt;inf&amp;gt; Info: Function duration [us]: 481
[00:00:03.001,770] &amp;lt;inf&amp;gt; Info: MCU temperature [C]: 23
[00:00:04.002,319] &amp;lt;inf&amp;gt; Info: Function duration [us]: 480
[00:00:04.002,319] &amp;lt;inf&amp;gt; Info: MCU temperature [C]: 23&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;prj.conf:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;CONFIG_USE_SEGGER_RTT=y
CONFIG_LOG=y
CONFIG_LOG_BACKEND_SHOW_COLOR=y

# Enable multithreading
CONFIG_MULTITHREADING=y

# TIMER 4
CONFIG_NRFX_TIMER4=y

# Sensor
CONFIG_SENSOR=y

# Enable internal oscillator
CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=y

# Enable MPU
CONFIG_ARM_MPU=y

# Enable hardware stack protection
CONFIG_HW_STACK_PROTECTION=y

# Configure GPIO as pin RESET
CONFIG_GPIO_AS_PINRESET=y&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t know why, but it seems that the addition of the &lt;strong&gt;CONFIG_BT=y&lt;/strong&gt; in the &lt;strong&gt;prj.conf&lt;/strong&gt; reduces the duration of this function from 480 us to 38 us. The &lt;strong&gt;mpsl_temperature_get()&lt;/strong&gt; also takes 38 us. The problem seems to be solved, thank you for your help.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Internal temperature measurement with Zephyr</title><link>https://devzone.nordicsemi.com/thread/405736?ContentTypeID=1</link><pubDate>Thu, 19 Jan 2023 17:08:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f135fc2b-5c28-47e9-b852-eab35925e6d9</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;If you are planning to add BLE functionality to the project, can you check if &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.2.0/nrfxlib/mpsl/doc/api.html#c.mpsl_temperature_get"&gt;mpsl_temperature_get&lt;/a&gt;&lt;span&gt;() gives faster run time?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;400us sound very long for getting a temperature reading. Can you post the sample you used for testing this?&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;br /&gt;Jørgen&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>