PF's I2C library with the XMEGA

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

In an attempt to get Peter Fluery's bit-bang I2C library working with an ATXmega128A1 I made the following changes to the source (i2cmaster.s):

Library: http://homepage.hispeed.ch/peter...

ORIGINAL:

;***** Adapt these SCA and SCL port and pin definition to your target !!
;
#define SDA     	4		// SDA Port D, Pin 4   
#define SCL		5		// SCL Port D, Pin 5
#define SDA_PORT        PORTD           // SDA Port D
#define SCL_PORT        PORTD           // SCL Port D         

;******

;-- map the IO register back into the IO address space
#define SDA_DDR		(_SFR_IO_ADDR(SDA_PORT) - 1)
#define SCL_DDR		(_SFR_IO_ADDR(SCL_PORT) - 1)
#define SDA_OUT		_SFR_IO_ADDR(SDA_PORT)
#define SCL_OUT		_SFR_IO_ADDR(SCL_PORT)
#define SDA_IN		(_SFR_IO_ADDR(SDA_PORT) - 2)
#define SCL_IN		(_SFR_IO_ADDR(SCL_PORT) - 2)

MODIFIED (using VP0 in I/O address space):

;***** Adapt these SCA and SCL port and pin definition to your target !!
;

#define SDA     				4		
#define SCL						5		
#define SDA_PORT      			VPORT0_OUT
#define SCL_PORT				VPORT0_OUT

;******

;-- map the IO register back into the IO address space
#define SDA_DDR		(_SFR_IO_ADDR(SDA_PORT)-1)
#define SCL_DDR		(_SFR_IO_ADDR(SCL_PORT)-1)
#define SDA_OUT		(_SFR_IO_ADDR(SDA_PORT))
#define SCL_OUT		(_SFR_IO_ADDR(SCL_PORT))
#define SDA_IN		(_SFR_IO_ADDR(SDA_PORT)+1)
#define SCL_IN		(_SFR_IO_ADDR(SCL_PORT)+1)

And then: PORTCFG.VPCTRLA=PORTCFG_VP0MAP_PORTD_gc; in main.c before using the library.

With the modification, both code snippets should now be the same - but I can't get any activity on my XMEGA data and clock pins (using test code provided with the library)...

Can anyone see any issues with the modifications?

Thanks in advance! :)

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

I have always used the regular port addressing rather than a Virtual port.

Your changes look about right to me. The obvious thing is to step through the code in the Simulator.

My obvious question is "Can you afford to buy external pull-up resistors?"

The code will not work without them.
Incidentally, the Xmega TWI lives on PORTD.0 and PORTD.1 and not PORTD.4, PORTD.5

David.

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

david.prentice wrote:
I have always used the regular port addressing rather than a Virtual port.

Your changes look about right to me. The obvious thing is to step through the code in the Simulator.

My obvious question is "Can you afford to buy external pull-up resistors?"

The code will not work without them.
Incidentally, the Xmega TWI lives on PORTD.0 and PORTD.1 and not PORTD.4, PORTD.5

David.

Thanks for the reply David.

I wrongfully assumed that the device I was connecting the MCU to already had pullups - turns out I was wrong! :oops: Thanks for the tip!

So far in testing, the code I originally posted works perfectly. I'll post again if there are any issues ;)

Oh and in my design no TWIs are available, so bit-banging is my only choice...

Cheers

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

What. Not one of the four TWIs is available!

So the porting was really simple.

David.

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

Is there any way you can post the code you have to get i2c working for the xmega?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
Last Edited: Sun. Mar 15, 2015 - 04:33 AM