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

TWIM error

Hi to everyone!

I am using nRF52832 driven by nRF52 DK and a sensor from Maxim Integrated. As debug output i am using the RTT viewer and as IDE Keil.

The sensor comunicate with the nRF through i2c, and i have defined read byte functions in this way: 

void twim_handler(nrfx_twim_evt_t const * p_event, void * p_context)
{

    size_t primaryBufferSize = p_event->xfer_desc.primary_length;
    uint8_t *bytesTransferred = p_event->xfer_desc.p_primary_buf;
    size_t secondaryBufferSize = p_event->xfer_desc.secondary_length;
    uint8_t *bytesRead = p_event->xfer_desc.p_secondary_buf;

    switch (p_event->type)
    {
        case NRFX_TWIM_EVT_DONE:
    
            NRF_LOG_INFO("STATE OF BUFFERS IN EVENT HANDLER:");
            NRF_LOG_INFO("\t Size of primary buffer: %lu", primaryBufferSize);
            NRF_LOG_INFO("\t Size of secondary buffer: %lu", secondaryBufferSize);
          
					if (p_event->xfer_desc.type == NRFX_TWIM_XFER_TX)
            {
                twim_tx_done = true;
            }
          
						if (p_event->xfer_desc.type == NRFX_TWIM_XFER_RX)
            {
                twim_rx_done = true;
            }
            break;
        
		case NRFX_TWIM_EVT_ADDRESS_NACK:
						if (p_event->xfer_desc.type == NRFX_TWIM_XFER_TX)
            {
                twim_tx_failed = true;
            }
          
						if (p_event->xfer_desc.type == NRFX_TWIM_XFER_RX)
            {
                 twim_rx_failed = true;
            }
            NRF_LOG_INFO("Received NACK after sending address!");
            break;
        
		case NRFX_TWIM_EVT_DATA_NACK:
					if (p_event->xfer_desc.type == NRFX_TWIM_XFER_TX)
            {
                twim_tx_failed = true;
            }
          
						if (p_event->xfer_desc.type == NRFX_TWIM_XFER_RX)
            {
                 twim_rx_failed = true;
            }
            NRF_LOG_INFO("Received NACK after sending data.");
            break;
        default:
            break;
    }

	}



uint8_t readByte( uint8_t familyByte, uint8_t indexByte)
{
    
		uint8_t flag_retry=1;
	
		nrfx_err_t err_code;

		uint8_t StatusByte;    
		uint8_t ReturnByte;
   
        uint8_t rx_buffer[2];
		size_t rx_lenght =2;

		uint8_t tx_buffer[] = {familyByte, indexByte};    
		size_t tx_lenght = sizeof(familyByte) + sizeof(indexByte);


		while(flag_retry==1){
		    uint32_t timeout = OXI_TWI_TIMEOUT;
			
		    nrfx_twim_xfer_desc_t tx_xfer = NRFX_TWIM_XFER_DESC_TX(OXI_ADDRESS, tx_buffer, tx_lenght);
		    err_code = nrfx_twim_xfer(&twim_instance, &tx_xfer, 0);
	
		    while((!twim_tx_done) && --timeout && 	twim_tx_failed == false);
		    if(!timeout) err_code=NRF_ERROR_TIMEOUT;
		    APP_ERROR_CHECK(err_code);
		    if(twim_tx_done) {
			    flag_retry=0;
			    twim_tx_done=false;
		    }
		    if(twim_tx_failed) {
			flag_retry=1;
			twim_tx_failed=false;
		    }
		}
		
		nrf_delay_ms(6);
		flag_retry=1;
		
		while(flag_retry==1){
		    uint32_t timeout = OXI_TWI_TIMEOUT;
			
		    nrfx_twim_xfer_desc_t rx_xfer = NRFX_TWIM_XFER_DESC_RX(OXI_ADDRESS, rx_buffer, rx_lenght);
		    err_code = nrfx_twim_xfer(&twim_instance, &rx_xfer, 0);
		
		    while((!twim_rx_done) && --timeout && 	twim_rx_failed == false);
		    if(!timeout) err_code=NRF_ERROR_TIMEOUT;
		    APP_ERROR_CHECK(err_code);
		    if(twim_rx_done) {
			    flag_retry=0;
			    twim_rx_done=false;
		    }
		    if(twim_rx_failed) {
			    flag_retry=1;
			    twim_rx_failed=false;
	    	}
		}
		
		nrf_delay_ms(6);
		
		
		StatusByte = rx_buffer[0];
        NRF_LOG_INFO("Status Byte: %lu \n",StatusByte);
    
		ReturnByte = rx_buffer[1];  
		return ReturnByte;
}

When i debug my code this function works properly till inside the main loop. It is strange because i cannot see anything wrong from my TWIM0 window. I don't think it is a problem of transaction because i am stack at the declaration of the type of xfer at the pointer in the following screenshot:

I tried to check the info about the error but i can't understand where this come from. 

In my RTT window it seems that the code continue to run the application properly.

Thanks, 

polimarte

  • Hi,

    I just noticed that you have created an earlier thread where you asked this question but didn't get any response. I'm sorry for that.

    Back to your question:

    You're stuck at that step? Where does the program go if try to single step through the code? 

    Could you include more of your code?

    I see that you have a lot other cases with the TWI. I'm just wondering, is there any specific reason for why you don't use the more high level nrf_drv_twi() driver? 

    regards

    Jared 

  • Hi, i had a lot of problems with the i2c configuration you are right. I have resolved all the issues of my read/write byte functions thanks to the help of this blog. Anyway I found the error, it was an hardware problem, when the voltages are not stable the behavior of the application is very strange and it gives me errors in different part of the code.

    thanks for the help,

    polimarte

Related