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

using TWI while using GCC for 'ble_app_hrs' on NRF51 pca10028 S110

Hello,

I use GCC as IDE because I must(though I debug perfectly via Keil v5). I migrated to eclipse GCC by the tutorial published here on 'Devzone'. I added some C codes and drivers for NRF 51. 'spi_drv_master' works fine but not 'twi_drv_twi'.

I'm trying to use the TWI(I2C) service, but I cannot continue through the 'nrf_drv_twi_tx' function to end it through 'twi_transfer' because: p_cb->transfer_in_progress = true thus, I always return NRF_ERROR_BUSY and fail. here is the snippet from 'twi_transfer':

static ret_code_t twi_transfer(nrf_drv_twi_t const * const p_instance,
                               uint8_t                     address,
                               uint8_t const             * p_data,
                               uint32_t                    length,
                               bool                        xfer_pending,
                               bool                        is_tx)
{
    ASSERT(m_cb[p_instance->instance_id].state == NRF_DRV_STATE_POWERED_ON);
    ASSERT(length > 0);

    control_block_t * p_cb = &m_cb[p_instance->instance_id];

    bool is_busy = false;
    CRITICAL_REGION_ENTER();
    if (p_cb->transfer_in_progress)
    {
        is_busy = true;
    }
    else
    {
        p_cb->transfer_in_progress = true;
    }
    CRITICAL_REGION_EXIT();
    if (is_busy)
    {
        return NRF_ERROR_BUSY;
    }
***
}

'ASSERT' function does not seem to work either since: p_cb->state = NRF_DRV_STATE_UNINTIALIZED and that occurs when I hit the control_block_t * p_cb = &m_cb[p_instance->instance_id]; breakpoint.

when I hit the bool is_busy = false breakpoint, only then when I get: p_cb->state = NRF_DRV_STATE_POWERED_ON

as mentioned above, is_busy = true; and I return and fail.

here is how I initalized the TWI:

void twi_i2c_init (void)
{
    ret_code_t err_code;
  
//	  nrf_gpio_cfg_input(MAX_GPIO_INT_PIN,NRF_GPIO_PIN_PULLUP);
 	
    const nrf_drv_twi_config_t twi_i2c_MAX30101_config = {
       .scl                = I2C_SCL_PIN,
       .sda                = I2C_SDA_PIN,
       .frequency          = NRF_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH
    };
    
    err_code = nrf_drv_twi_init(&twi_i2c_MAX30101, &twi_i2c_MAX30101_config, (nrf_drv_twi_evt_handler_t)twi_i2c_handler, NULL);
    APP_ERROR_CHECK(err_code);
    
    nrf_drv_twi_enable(&twi_i2c_MAX30101);
}

and here is how I read from TWI:

void twi_i2c_MAX30101_read(uint8_t reg_addr, uint8_t* rxBuff, int size)
{
    ret_code_t err_code;
		m_tx_done = true;

		twi_i2c_init();

		if((0 < size) && (size < I2C_RXBUFFER_SIZE))
		{
			/* Writing to MAX30101 */
			i2cTransfer[0] = reg_addr;
			rxBuffer.size = size;
//			if(MAX30101_FIFO_read_flag)
//			{
//				while(nrf_gpio_pin_read(MAX_GPIO_INT_PIN) == 1);
//				MAX30101_FIFO_read_flag = 0;
//			}
			err_code = nrf_drv_twi_tx(&twi_i2c_MAX30101, MAX30101_ADDRESS>>1, i2cTransfer, 1, true);
		}
    APP_ERROR_CHECK(err_code);
    
		do{
				// Make sure any pending events are cleared
				__SEV();
				__WFE();
				// Enter System ON sleep mode
				__WFE();
      }while(m_rx_done == false);
		
			m_rx_done = false;
		
//		i2cTransfer = {0};
		
		for(int i=0; i < size; i++)
		{
			rxBuff[i] = rxBuffer.rx_Buffer[i];
		}
		
//		rxBuff_t rxBuffer = {0};
}

I tried earlier to impose: m_cb[p_instance->instance_id].transfer_in_progress = false; but that did not help me to transfer data. I transfer nonesence.

What should I do? Via Keil it works fine.

Idan

Related