<?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>Read Function with TWI/TWIM legacy and NRFX</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/66997/read-function-with-twi-twim-legacy-and-nrfx</link><description>Hi I am using nRF52 DK, nRF52832 and a sensor. The TWI communication is described as : 
 
 
 and : 
 
 
 I tried to use the Legacy of TWI drivers in order to implement a read function able to read the registers. As well i tried to use nrfx module with</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 12 Oct 2020 13:07:57 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/66997/read-function-with-twi-twim-legacy-and-nrfx" /><item><title>RE: Read Function with TWI/TWIM legacy and NRFX</title><link>https://devzone.nordicsemi.com/thread/274332?ContentTypeID=1</link><pubDate>Mon, 12 Oct 2020 13:07:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dfad5f92-5608-42f1-9c28-109c0c9e9b11</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;The same transfer macros should be available in the legacy driver, please take a look at the driver documentation here:&amp;nbsp;&lt;span&gt;&lt;a title="TWI master" href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v14.2.0/hardware_driver_twi.html?cp=7_5_4_2_16"&gt;TWI master. &lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Also, below I have included an example I made to interface with the Sensirion sht3x. It demonstrate how the transfer macros can be used to set up a complete transaction. Hopefully it may be useful as a reference. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**
 * Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA &amp;quot;AS IS&amp;quot; AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
/** @file
 * @defgroup tw_sensor_example main.c
 * @{
 * @ingroup nrf_twi_example
 * @brief TWI Sensor Example main file.
 *
 * This file contains the source code for a sample application using TWI.
 *
 */

#include &amp;lt;stdio.h&amp;gt;
#include &amp;quot;boards.h&amp;quot;
#include &amp;quot;app_util_platform.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;nrf_drv_twi.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;


#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;

/* TWI instance ID. */
#define TWI_INSTANCE_ID     0

/* Common addresses definition for temperature sensor. */
#define SHT3x_ADDRESS                   (0x45U)
#define SHT3x_MEAS_CMD_H                {0x2c, 0x06}                       /**&amp;lt;Start high repeatability measurement with clock stretching*/

/* TWI instance. */
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);


/**
 * @brief Function for handling data from temperature sensor.
 *
 * @param[in] temp          Temperature in Celsius degrees read from sensor.
 */
__STATIC_INLINE void data_handler(nrf_drv_twi_evt_t const * p_event)
{
    uint16_t      raw_value;
    float         temperature;
    uint16_t      humidity;
    
    /* Raw data conversion. We are ignoring the checksum value here */ 
    raw_value = uint16_big_decode(&amp;amp;p_event-&amp;gt;xfer_desc.p_secondary_buf[0]); /* Swap byte order to little endian */
    
    temperature = 175 * (uint16_t)raw_value / 65535.0f -45.0f;

    raw_value = uint16_big_decode(&amp;amp;p_event-&amp;gt;xfer_desc.p_secondary_buf[3]);
    humidity = 100.0f * raw_value / 65535.0f;
    
    NRF_LOG_INFO(&amp;quot;Temperature: &amp;quot;NRF_LOG_FLOAT_MARKER&amp;quot; Celsius degrees.&amp;quot;, NRF_LOG_FLOAT(temperature));
    NRF_LOG_INFO(&amp;quot;Humidity: %d%%&amp;quot;, humidity);
}

/**
 * @brief TWI events handler.
 */
void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
    switch (p_event-&amp;gt;type)
    {
        case NRF_DRV_TWI_EVT_DONE:
            if (p_event-&amp;gt;xfer_desc.type == NRF_DRV_TWI_XFER_TXRX)
            {
                data_handler(p_event);
            }
            break;
        case NRF_DRV_TWI_EVT_ADDRESS_NACK:
            NRF_LOG_WARNING(&amp;quot;Received NRF_DRV_TWI_EVT_ADDRESS_NACK event&amp;quot;);
            break;
        case NRF_DRV_TWI_EVT_DATA_NACK:
            NRF_LOG_WARNING(&amp;quot;Received NRF_DRV_TWI_EVT_DATA_NACK event&amp;quot;);
            break;
        default:
            break;
    }
}

/**
 * @brief UART initialization.
 */
void twi_init (void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_sht3x_config = {
       .scl                = ARDUINO_SCL_PIN,
       .sda                = ARDUINO_SDA_PIN,
       .frequency          = NRF_DRV_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&amp;amp;m_twi, &amp;amp;twi_sht3x_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&amp;amp;m_twi);
}

/**
 * @brief Function for reading data from temperature sensor.
 */
static void read_sensor_data()
{  
    uint32_t err_code;
    static uint8_t twim_rx_buf[6];
    static uint8_t twim_tx_buf[] = SHT3x_MEAS_CMD_H;

    nrf_drv_twi_xfer_desc_t xfer_desc = NRF_DRV_TWI_XFER_DESC_TXRX(
        SHT3x_ADDRESS, twim_tx_buf, sizeof(twim_tx_buf), twim_rx_buf, sizeof(twim_rx_buf));

    err_code = nrf_drv_twi_xfer(&amp;amp;m_twi, &amp;amp;xfer_desc, NRF_DRV_TWI_FLAG_TX_NO_STOP);
    if (err_code == NRF_ERROR_BUSY)
    {
        NRF_LOG_INFO(&amp;quot;TWIM driver is busy&amp;quot;);
    }
    else
    {
        APP_ERROR_CHECK(err_code);
    }
}

/**
 * @brief Function for main application entry.
 */
int main(void)
{
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO(&amp;quot;\r\nTWI SHT3x example started.&amp;quot;);
    NRF_LOG_FLUSH();
    twi_init();

    while (true)
    {
        nrf_delay_ms(500);
        read_sensor_data();
        NRF_LOG_FLUSH();
    }
}

/** @} */&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Vidar&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>