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

help for TWI

HI

i'm trying to use TSL2561 sensor, but i can't geting to work. I found the code that works on the psoc that, I change it so it fits nrf52840 but it still does not work, but works on the psoc

can anyone see what i have done wrong

#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 "TSL2561.h"
#include "log.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

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

/* 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;
uint8_t command_bits=COMMAND_BIT;
uint8_t control_powerOn=CONTROL_POWERON;
uint8_t com_timing=(COMMAND_BIT|REGISTER_TIMING);
uint8_t gain_low_int=0x00;

/**
 * @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_lm75b_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_lm75b_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi);
}



/**
 * @brief Function for main application entry.
 */
 uint8_t init_TSL2561(void)
{ 
    uint32_t status;
    uint8_t I2C_buffer[4];                            /* buffer for I2C communication with TSL2561 */
uint8_t ADC0_buffer[2];
uint8_t ADC1_buffer;
    /* Write User Register */
   uint8_t COM_WORD_REGISTER_ADC1_LO=0xAE;
    uint8_t COM_WORD_REGISTER_ADC0_LO=(COMMAND_BIT|WORD_BIT|REGISTER_ADC0_LO);
    
    status =  nrf_drv_twi_tx(&m_twi, TSL_ADDR , &m_sample, sizeof(m_sample),false); // write direction
    if (status !=  NRF_SUCCESS ) {goto exit;}
       __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "write mode\n")
    nrf_delay_ms(1000);

    status = nrf_drv_twi_tx(&m_twi,TSL_ADDR, &control_powerOn,sizeof(control_powerOn),false);
    if (status !=  NRF_SUCCESS ) {__LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"TSL2561 is %u\r\n",status); goto exit;}
    nrf_delay_ms(1000);
    //now check to confirm the device is powered up
    status = nrf_drv_twi_rx(&m_twi, TSL_ADDR , &m_sample, sizeof(m_sample)); // read direction
    if (status !=  NRF_SUCCESS ){__LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"TSL2561 is %u\r\n",status);  goto exit;}
  __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "read mode\n");
   
 

   // status = nrf_drv_twi_rx(&m_twi, TSL_ADDR , &m_sample, sizeof(m_sample));
    memset(I2C_buffer, 0, 4);
    memcpy(I2C_buffer, &status, sizeof(status));
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"TSL2561 is %u\r\n",status);
    if (I2C_buffer[0] == 0 || I2C_buffer[0] == 51) { 
         __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"TSL2561 is powered\r\n");
        nrf_delay_ms(1000);
        /* Switch back to Write User Register */
        status =nrf_drv_twi_tx(&m_twi,TSL_ADDR, &m_sample,sizeof(m_sample),false); // write direction
        if (status != NRF_SUCCESS) goto exit;
         __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"write mode\r\n");
nrf_delay_ms(1000);
        status = nrf_drv_twi_tx(&m_twi,TSL_ADDR, &com_timing,sizeof(com_timing),false); // write direction
        
        if (status != NRF_SUCCESS) goto exit;
         __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"register timing\r\n");
nrf_delay_ms(1000);
        status = nrf_drv_twi_tx(&m_twi,TSL_ADDR, &gain_low_int,sizeof(gain_low_int),false); // write direction
         if (status != NRF_SUCCESS) goto exit;
         __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"set gain low\r\n");
        
       
        return 1;
    }


    
    exit:
      __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "fejl fejl fejl\n")
        return 0;
}

uint8_t getTSL2561_Lumos(void)
{
    uint8_t errchk;
    uint32_t status;
    //uint8 I2C_buffer[4];                            /* buffer for I2C communication with TSL2561 */
    uint8_t ADC0_buffer[2];                            /* ADC0 buffer for I2C communication with TSL2561 */
    uint8_t ADC1_buffer[2];                            /* ADC0 buffer for I2C communication with TSL2561 */
    uint16_t lightval;
    uint16_t IRval;
    char str[50];
uint8_t COM_WORD_REGISTER_ADC0_LO=0xAC;
uint8_t COM_WORD_REGISTER_ADC1_LO=0xAE;
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"Reading values from TSL2561\r\n");
    memset(ADC0_buffer, 0, 2);
    memset(ADC1_buffer, 0, 2);
    nrf_delay_ms(1000);
    //Channel 0
    status = nrf_drv_twi_tx(&m_twi,TSL_ADDR,&m_sample,sizeof(m_sample),false); // write direction
    if (status != NRF_SUCCESS) goto exit;
     __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"write mode CH0\r\n");
nrf_delay_ms(1000);
    status =  nrf_drv_twi_tx(&m_twi,TSL_ADDR,&COM_WORD_REGISTER_ADC0_LO,sizeof(COM_WORD_REGISTER_ADC0_LO),false);
      if (status != NRF_SUCCESS) goto exit;

 nrf_delay_ms(1000);  
    status= nrf_drv_twi_rx(&m_twi,TSL_ADDR, &ADC0_buffer,sizeof(ADC0_buffer));
      
    if (status != NRF_SUCCESS) goto exit;
     __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"checking ADC0 %u %u\r\n",ADC0_buffer[0], ADC0_buffer[1]);
     nrf_delay_ms(1000);
   
    
    //Channel 1
    status =  nrf_drv_twi_tx(&m_twi,TSL_ADDR,&m_sample,sizeof(m_sample),false); // write direction
    if (status != NRF_SUCCESS) goto exit;
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"write mode CH1\r\n");
nrf_delay_ms(1000);
    status = nrf_drv_twi_tx(&m_twi,TSL_ADDR,&COM_WORD_REGISTER_ADC1_LO,sizeof(COM_WORD_REGISTER_ADC1_LO),false);
          if (status != NRF_SUCCESS) goto exit;

nrf_delay_ms(1000);
           status= nrf_drv_twi_rx(&m_twi,TSL_ADDR, &ADC1_buffer,2);
   // if (status != NRF_SUCCESS) goto exit;
   __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"checking ADC0 %u %u\r\n",ADC1_buffer[0], ADC1_buffer[1]);
 
 __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"checking ADC1\r\n");
  
    int i=0;
    if(i==0)
     {__LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Values = %X %X %X %X\r\n", ADC0_buffer[0],ADC0_buffer[1],ADC1_buffer[0],ADC1_buffer[1]);
     
    lightval = (ADC0_buffer[1] << 8) + ADC0_buffer[0];
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Light Value = %hu\r\n", lightval);
      
    IRval = (ADC1_buffer[1] << 8) +  ADC1_buffer[0];
   __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "IR Value = %hu\r\n", IRval);
 
        }
   
    return 1;

    exit:
        __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"There was a problem reading TSL2561\r\n");
       
        return 0;

}

int main(void)
{ __LOG_INIT(LOG_SRC_APP | LOG_SRC_ACCESS, LOG_LEVEL_INFO, LOG_CALLBACK_DEFAULT);
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO("\r\nTWI sensor example");
    NRF_LOG_FLUSH();
    twi_init();
    //LM75B_set_mode();

    while (true)
    {
        nrf_delay_ms(500);

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

       // read_sensor_data();
       for (int xx = 0; xx < 5; xx++) {
        if(init_TSL2561()) {
     __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "skal læse værdi\n")
            // short delay to get sensor readings
            
           if (getTSL2561_Lumos())
            { __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "læst værdi\n")}
        }
        }
      
        NRF_LOG_FLUSH();
    }
}

i got it to work yesterday, but when i test to day it did not work and i have not change the code from yesterday.

Related