ATSAM4S TWI read

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a problem with a ATSAM4S16C using the TWI interface.

I run always into a timeout when reading from the slave device. The slave and the addresses are correct (tested with an aduino).

I followed the instructions of the ASF tutorial for ATSAM4S TWI (common api), but maybe the instructions are not complete as &TWIM0 is unknown and I replaced it with TWI0.

What is the problem or what init step did it forget? My code is:

#include <asf.h>

void twi_init(void)
  {
        twi_master_options_t opt = {
                .speed = 100000,
                .chip  = 0x28
    };
    
        twi_master_setup(TWI0, &opt);
  }

int main (void)
{
    /* Insert system clock initialization code here (sysclk_init()). */

    board_init();

     twi_init();
     twi_enable_master_mode(TWI0);
   
      uint8_t data_received[10];
      
         twi_package_t packet_read = {
               .addr         = 0x00,      // TWI slave memory address data
               .addr_length  = sizeof (uint8_t),    // TWI slave memory address data size
               .chip         = 0x28,      // TWI slave bus address
               .buffer       = data_received,        // transfer data destination buffer
             .length       = 10                    // transfer data size (bytes)
         };
        
         // Perform a multi-byte read access then check the result.
         uint32_t status1 = twi_master_read(TWI0, &packet_read) ;
         if(status1 == TWI_SUCCESS){
               //Check read content
               if(data_received[0]==0xA0)
                LED_On( LED0_GPIO);
         }

}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you checked the clock line with an oscilloscope? 

I am having problems to configure the TWI of a SAMC21 with ASF and I can't see nothing on my SCK line. 

 

If you solve the problem would be great if you post here, maybe I can use it too.

 

Thanks

Regards!
FS.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you programmed the PIO controller to specify that whatever pins you are using for TWI should be configured as such?