This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Code gets stuck in m_xfer_done loop while trying to interface with a TWI OLED display

Hello,

I'm trying to interface this cheap OLED display from ebay

and while running and debugging the code I think should work(all based off the TWI_sensor sdk example just edited it to my need) the code gets stuck in the loop: while (m_xfer_done == false);

while the err_code is 0x00

here's the code using the TWI scanner the address of the device was found to be 0x3C:

#include <stdio.h>
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_drv_twi.h"
#include "nrf_delay.h"


#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

/* TWI instance ID. */
#define TWI_INSTANCE_ID     0

/* Common addresses definition for temperature sensor. */
#define OLED_ADDR          (0x78U >> 1)

#define OLED_REG_TEMP      0x00U
#define OLED_REG_CONF      0x81U
#define OLED_REG_THYST     0x02U
#define OLED_REG_TOS       0x03U

/*
#define NORMAL_MODE 0XAF

/* Indicates if operation on TWI has ended. */
static volatile bool m_xfer_done = false;

/* TWI instance. */
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);

/* Buffer for samples read from temperature sensor. */
static uint8_t m_sample;


void OLED_set_mode(void)
{
    ret_code_t err_code;

    
    uint8_t reg[2] = {OLED_REG_CONF, NORMAL_MODE};
    err_code = nrf_drv_twi_tx(&m_twi, OLED_ADDR, reg, sizeof(reg), false);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);

    
}

void OLED_set_contrast(void)
{
    ret_code_t err_code;

    uint8_t reg[3] = {0x00,0x81, 0x80};
    err_code = nrf_drv_twi_tx(&m_twi, OLED_ADDR, reg, sizeof(reg), false);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);

}

void OLED_set_display(void)
{
    ret_code_t err_code;

    /* Writing to LM75B_REG_CONF "0" set temperature sensor in NORMAL mode. */
    uint8_t reg[2] = {0x00,0xA5};
    err_code = nrf_drv_twi_tx(&m_twi, OLED_ADDR, reg, sizeof(reg), false);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);

   
}

/**
 * @brief Function for handling data from temperature sensor.
 *
 * @param[in] temp          Temperature in Celsius degrees read from sensor.
 */
__STATIC_INLINE void data_handler(uint8_t temp)
{
    NRF_LOG_INFO("Temperature: %d Celsius degrees.", temp);
}



void OLED_set_inverse(void)
{
    ret_code_t err_code;

    /* Writing to LM75B_REG_CONF "0" set temperature sensor in NORMAL mode. */
    uint8_t reg[2] = {0x00,0xA7};
    err_code = nrf_drv_twi_tx(&m_twi, OLED_ADDR, reg, sizeof(reg), false);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);

}
/**
 * @brief TWI events handler.
 */
void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
    switch (p_event->type)
    {
        case NRF_DRV_TWI_EVT_DONE:
            if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
            {
                data_handler(m_sample);
            }
            m_xfer_done = true;
            break;
        default:
            break;
    }
}

/**
 * @brief UART initialization.
 */
void twi_init (void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_oled_config = {
       .scl                = ARDUINO_SCL_PIN,
       .sda                = ARDUINO_SDA_PIN,
       .frequency          = NRF_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&m_twi, &twi_oled_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi);
}

/**
 * @brief Function for reading data from temperature sensor.
 */
static void read_oled_data()
{
    m_xfer_done = false;

    /* Read 1 byte from the specified address - skip 3 bits dedicated for fractional part of temperature. */
    ret_code_t err_code = nrf_drv_twi_rx(&m_twi, OLED_ADDR, &m_sample, sizeof(m_sample));
    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("\r\nTWI sensor example");
    NRF_LOG_FLUSH();
    twi_init();
    OLED_set_mode();
    OLED_set_contrast();
    OLED_set_display();
    OLED_set_inverse();


    while (true)
    {
        nrf_delay_ms(500);

        do
        {
            __WFE();
        }while (m_xfer_done == false);

      //  read_sensor_data();
        NRF_LOG_FLUSH();
    }
}

/** @} */

and here is the table from the datasheet: image description

Related