SAM4s I2C (or TWI) master example

Discussion around product based on ARM Cortex M4 core.

Moderators: nferre, ncollot

wdekimpe
Posts: 6
Joined: Fri Mar 15, 2013 9:55 am

SAM4s I2C (or TWI) master example

Fri Mar 15, 2013 10:27 am

Hi

I am trying to get the twi working from the ASF, but find the documentation a bit cryptic.
I have found a slave example, but is there any good example for a master implementation around?

I want to use TWI0, here is what I have come up with (not tested yet):

Code: Select all

	
int main (void)
{
	board_init();
	sysclk_init();
	pmc_enable_periph_clk(ID_TWI0);
	
	uint8_t buffer[2];
	buffer[0] = 10;
	buffer[1] = 20;
	
	
	twi_options_t opt = {
	 .speed = 100000,
	 .chip  = SFM4100_I2C_ADDR_AIR
	};
	twi_packet_t packet = {
		.addr = SFM4100_I2C_ADDR_AIR,
		.addr_length = 1,
		.buffer = buffer,
		.chip = ???,
		.length = 2
	}
	twi_enable_master_mode(TWI0);
	twi_master_init(TWI0,&twi_options);
	twi_master_write(TWI0,&packet);
	
	while(1);
}	
Following questions:
- I do not understand the packet definition: the address, is that the slave address, or is that an internal register address of the slave device?
- Which address length should I use?
- what is the "chip" field?
- is it correct to use TWI0 as the pointer to the twi?
- is it ok to use IDTWI0 in the peripheral clock init function?
- When sending packets to different slave devices on the same bus, do I always have to do a call to the init function?
davewr
Posts: 28
Joined: Sun Mar 24, 2013 9:46 pm

Re: SAM4s I2C (or TWI) master example

Wed Mar 27, 2013 5:15 pm

There is an I2C master example on the Atmel site (Perhaps it was in a pdf doc) -- have a look for it. If I spot it again I will post the link here.
davewr
Posts: 28
Joined: Sun Mar 24, 2013 9:46 pm

Re: SAM4s I2C (or TWI) master example

Thu Mar 28, 2013 3:58 pm

You can also find the software in a zip file on the site:
http://www.atmel.com/Images/doc6327.pdf

hth
paragn
Posts: 3
Joined: Tue Aug 11, 2015 6:33 am

Re: SAM4s I2C (or TWI) master example

Tue Aug 11, 2015 1:32 pm

Hello davewr

The link provided by you does not contain any code. can you please provide the code
jonavarque
Posts: 29
Joined: Fri Jul 10, 2015 6:35 pm

Re: SAM4s I2C (or TWI) master example

Tue Aug 11, 2015 4:44 pm

I do not understand the packet definition: the address, is that the slave address, or is that an internal register address of the slave device?
Which address length should I use?
It is a 3 byte address you would use for memory devices. For registers just use one byte and set the length to 1.
- what is the "chip" field?
Slave address of the device. And I had to shift mine right once. From 0x30 to 0x18.
- is it correct to use TWI0 as the pointer to the twi?
YES
- is it ok to use IDTWI0 in the peripheral clock init function?
Here's what I am using at the moment.

Code: Select all

#define THETWI TWI4
bool SetupTWI0()
{
//	status_code_t result;
	twi_options_t twiOptions  =
	{
		.master_clk = sysclk_get_cpu_hz(),
		.speed = 40000,
		.chip  = UDA1380Addr
	};
	flexcom_enable(BOARD_FLEXCOM_TWI);
	flexcom_set_opmode(BOARD_FLEXCOM_TWI, FLEXCOM_TWI);
	twi_enable_master_mode(THETWI);
	//twi_master_setup(twP, twi_master_options_t *twiOptions) ;
	twi_master_init(THETWI,&twiOptions);
	Reset_UDA1380();

}
- When sending packets to different slave devices on the same bus, do I always have to do a call to the init function?
I would think not. BTW, agreed on the documentation. Woefully pathetic.

Return to “SAM4 Cortex-M4 MCU”

Who is online

Users browsing this forum: No registered users and 1 guest