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

I can not communicate with SPI and TWI with timer example

Hi,

I am using nRF_SDK_15.0.0_a53641 and writing a program with keil.

I am receiving data from the sensor with TWI communication and writing it to the sensor by SPI communication.

So I use timer example to periodically write data.

However, as of now, the timer example program is not working well.

It is possible to write data from the sensor to SD without using timer.

Also, it is possible to check the operation with a simple program that periodically toggles the LED with only the timer example.

Does anyone know why SPI and TWI communication can not be done using timer example?

Thank you!

Parents
  • Hi,

    There should be no issues with using timer together with SPI and TWI. You need to describe the issue in more details, and include some code showing what you have tried and what is not working, in order to get any help.

    Best regards,
    Jørgen

  • Thank you for reply!

    I wrote timer_event_handler like the code below, but no data was written to the SD card.

    void timer_twi_event_handler(nrf_timer_event_t event_type, void* p_context)
    {
    
        switch (event_type)
        {
            case NRF_TIMER_EVENT_COMPARE1:
    					
            
    		    TWI_start();//ST
    	        nrf_delay_us(5);
    		    twi_init();
    	        nrf_delay_us(5);
    	      	LM75B_set_mode();//SAD+W,SUB(SUB[7] set to 1)
             	nrf_delay_us(100);
            	TWI_start();//SR
    	    	nrf_delay_us(600);//600-500
    	    	read_sensor_data();//SAD+R
    	    	nrf_delay_us(4);
    	        TWI_stop();//SP
    	    	nrf_delay_us(5);
    	
    	        //uint8 to short(16bit)
            	x = (short)(m_sample[1] << 8 | m_sample[0]);
            	y = (short)(m_sample[3] << 8 | m_sample[2]);
        		z = (short)(m_sample[5] << 8 | m_sample[4]);
    
    
        
                x_g = (((float)x*2*2)/(float)65536);
    	    	y_g = (((float)y*2*2)/(float)65536);
    	    	z_g = (((float)z*2*2)/(float)65536);
    
    
    	        sprintf(buf0,"%lf",x_g);
    	        sprintf(buf1,"%lf",y_g);
    	        sprintf(buf2,"%lf",z_g);
    				
                fatfs_example(buf0,buf1,buf2);
    					
                break;
    
            default:
                //Do nothing.
                break;
        }
    }

    As a result of describing like this, it did not work well, so it did not work well using flags as follows.

    void timer_twi_event_handler(nrf_timer_event_t event_type, void* p_context)
    {
    
        switch (event_type)
        {
            case NRF_TIMER_EVENT_COMPARE1:
    					
            
    				flag = true;
    					
                break;
    
            default:
                //Do nothing.
                break;
        }
    }
    
    int main(){
    //config
    
    
        if (flag == 1){
    
        //my spi&twi code
        
        flag = false;
    
        }
    }

    When I was watching other questioner's posts, I saw some people saying about priority, are there any relationships?

Reply
  • Thank you for reply!

    I wrote timer_event_handler like the code below, but no data was written to the SD card.

    void timer_twi_event_handler(nrf_timer_event_t event_type, void* p_context)
    {
    
        switch (event_type)
        {
            case NRF_TIMER_EVENT_COMPARE1:
    					
            
    		    TWI_start();//ST
    	        nrf_delay_us(5);
    		    twi_init();
    	        nrf_delay_us(5);
    	      	LM75B_set_mode();//SAD+W,SUB(SUB[7] set to 1)
             	nrf_delay_us(100);
            	TWI_start();//SR
    	    	nrf_delay_us(600);//600-500
    	    	read_sensor_data();//SAD+R
    	    	nrf_delay_us(4);
    	        TWI_stop();//SP
    	    	nrf_delay_us(5);
    	
    	        //uint8 to short(16bit)
            	x = (short)(m_sample[1] << 8 | m_sample[0]);
            	y = (short)(m_sample[3] << 8 | m_sample[2]);
        		z = (short)(m_sample[5] << 8 | m_sample[4]);
    
    
        
                x_g = (((float)x*2*2)/(float)65536);
    	    	y_g = (((float)y*2*2)/(float)65536);
    	    	z_g = (((float)z*2*2)/(float)65536);
    
    
    	        sprintf(buf0,"%lf",x_g);
    	        sprintf(buf1,"%lf",y_g);
    	        sprintf(buf2,"%lf",z_g);
    				
                fatfs_example(buf0,buf1,buf2);
    					
                break;
    
            default:
                //Do nothing.
                break;
        }
    }

    As a result of describing like this, it did not work well, so it did not work well using flags as follows.

    void timer_twi_event_handler(nrf_timer_event_t event_type, void* p_context)
    {
    
        switch (event_type)
        {
            case NRF_TIMER_EVENT_COMPARE1:
    					
            
    				flag = true;
    					
                break;
    
            default:
                //Do nothing.
                break;
        }
    }
    
    int main(){
    //config
    
    
        if (flag == 1){
    
        //my spi&twi code
        
        flag = false;
    
        }
    }

    When I was watching other questioner's posts, I saw some people saying about priority, are there any relationships?

Children
Related