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

TWI stop working properly after playing with Optimization levels

Hi guys,

On my application, I ported the code from TWI_scanner example (SDK14) and it is connected to a temperature sensor the which is read every 45 seconds, It is working properly for longs periods. I tried changing the optimization Level is 3 (-O3) to reduce the application size, but If I change the optimization level all goes wrong with TWI

I was playing changing the optimization level from 2-1 and rebuilding my project, but the functions nrf_drv_twi_tx returns error 3 NRF_ERROR_INVALID_ADDR and nrf_drv_twi_rx also return error 3  NRF_ERROR_DRV_TWI_ERR_OVERRUN. 

why is happening this behavior?

//function for write
 nrf_drv_twi_tx(&m_twi, address, (uint8_t *)&sample_data, 1, true))

//function for read
 nrf_drv_twi_rx(&m_twi, address, (uint8_t *)&sample_data2, sizeof(sample_data))

I am using the TWI in blocking mode:

	const nrf_drv_twi_config_t twi_config =
	{
		.scl = scl_pin,
		.sda = 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_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);
    
	nrf_drv_twi_enable(&m_twi);
	

Module nRF52 DK (I am using the softdevice)

Regards,

Meliodas

Parents
  • Hi,

    A common source of errors when using interrupts and optimization level 3 is volatile variables. But it seems like you aren't using TWI interrupts (handlers) in your application?

    Are you using any other interrupts? And could it be that you are manipulating variables in those interrupts that should be declared 'volatile'?

    Can you share the rest of your code? We can make the case private if you prefer confidentiality. 

  • Hello MartinBL, I am using two timers every 45secs to schedule two TWI write/reads using app_sched_event_put, it works fine only when the optimization level is disabled.

    After enabled any optimization level I received corrupted data from TWI peripheral after that TWI returns error 3, but it only happens when the two timers expire one each other and each timer interruption puts the TWI inside the queue (that I want to do) from there my program does not work how it is supposed to do.

    I tried isolating the issue adding only a timer and queue only a single TWI write/read each 45secs, and it works fine with any optimization level.

    So why my program has an undesired behavior when I queue two TWI write/reads one each other after two timers expire almost at the same time? it is supported to do one task after other.

    Note: I am using gcc to build my program

Reply
  • Hello MartinBL, I am using two timers every 45secs to schedule two TWI write/reads using app_sched_event_put, it works fine only when the optimization level is disabled.

    After enabled any optimization level I received corrupted data from TWI peripheral after that TWI returns error 3, but it only happens when the two timers expire one each other and each timer interruption puts the TWI inside the queue (that I want to do) from there my program does not work how it is supposed to do.

    I tried isolating the issue adding only a timer and queue only a single TWI write/read each 45secs, and it works fine with any optimization level.

    So why my program has an undesired behavior when I queue two TWI write/reads one each other after two timers expire almost at the same time? it is supported to do one task after other.

    Note: I am using gcc to build my program

Children
Related