TWI interrupt driven code

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

Hello,
I am writing code for a 128. I want to use TWI using interrupt driven hardware functions. Does the standard lib for WinAVR support this? I looked at the documentation, and I could not tell if it was interrupt driven or not. I looked in the academy and elsewhere on this site and found a couple of different sources of TWI functions. But while the write-up may suggest it is interrupt driven, the headers sometimes disclaim that.

So, what is the best source for interrupt driven hardware TWI functions for the 128. I hope to avoid having to write my own (buggy) stuff.

-Tony

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

Look at aplication notes:

AVR311: Using the TWI module as I2C slave (12 pages, revision D, updated 10/04) This application note describes a TWI slave implementation, in form of a fullfeatured driver and an example of usage for this driver.
AVR315: Using the TWI module as I2C master (11 pages, revision B, updated 09/04) This Application Note describes a TWI master implementation, in form of a fullfeatured driver and an example of usage for this driver.
http://www.atmel.com/dyn/products/app_notes.asp?family_id=607
Keep in mind that multimaster mode has hardware problems and be very cautious if you are planning to use it!

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

Thanks, I will check these app notes! I do not plan on multiple masters. Right now ONLY an RTC, but other stuff like eeprom might come.

-Tony

brberie wrote:
Look at aplication notes:

AVR311: Using the TWI module as I2C slave (12 pages, revision D, updated 10/04) This application note describes a TWI slave implementation, in form of a fullfeatured driver and an example of usage for this driver.
AVR315: Using the TWI module as I2C master (11 pages, revision B, updated 09/04) This Application Note describes a TWI master implementation, in form of a fullfeatured driver and an example of usage for this driver.
http://www.atmel.com/dyn/products/app_notes.asp?family_id=607
Keep in mind that multimaster mode has hardware problems and be very cautious if you are planning to use it!

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

OK, thanks! I found the apnoes. I also got the Atmel code. Is this the same or better than what the WinAvr library uses? I can convert the IAR specific code to GCC but is it necessary or will the WinARV work just fine as is?

-Tony

brberie wrote:
Look at aplication notes:

AVR311: Using the TWI module as I2C slave (12 pages, revision D, updated 10/04) This application note describes a TWI slave implementation, in form of a fullfeatured driver and an example of usage for this driver.
AVR315: Using the TWI module as I2C master (11 pages, revision B, updated 09/04) This Application Note describes a TWI master implementation, in form of a fullfeatured driver and an example of usage for this driver.
http://www.atmel.com/dyn/products/app_notes.asp?family_id=607
Keep in mind that multimaster mode has hardware problems and be very cautious if you are planning to use it!

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

you have to convert

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

I have the same question. Please tell me how to convert the example-code discribed in the app-note for TW-Master into GCC compatible code. What has to be done? Thanks for your help

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

> Is this the same or better than what the WinAvr library uses?

With very few exceptions (most notably EEPROM handling), avr-libc (the
library that ships with WinAVR) doesn't do anything on your hardware,
so it does not contain any TWI routines itself.

You might perhaps confuse that with the twidemo example that ships as
part of avr-libc, but as the name suggest, that's an example only, and
it's using simple polled mode.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

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

OP: I think you should look at the Procyon library http://hubbard.engr.scu.edu/embedded/avr/avrlib/

(it is to be used in addition to all the standard libraries, not in the place of)

It has a great TWI library, both interupt and non-interupt driven. It seems to be quite stable.

It also has soooooo many great other libraries too. It is GPL'd I think, so read the license carefully to see it meets your needs, especially if making a commercial app

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

Bazoon wrote:
I have the same question. Please tell me how to convert the example-code discribed in the app-note for TW-Master into GCC compatible code. What has to be done? Thanks for your help

Well, I ahve not gotten to the stage of actually implementing the interrupt diven code. But it looks relatively (!) simple. Actually, a variant of my interrupt-driven EEPROM code ought to do it.

Take a look at my interrupt-driven EEPROM code to see the strategy.

In essence, you need to do the following (off the top of my head)

1) prepare the registers of the TWI hardware to give the clock speed you need. (100k, 400k are 2 "standard" speeds ) (The hardware is capable of about 409K max)

2) create a buffer for the data

3) call a subroutine with a pointer to the data

4) the subroutine moves the data to the buffer, and places the first byte into the TWI hardware and initiates the transfer with the TWI interrupt enabled. Then returns to the calling program.

5) The interrupt service routine, when the interrupt occurs, will check the buffer and place the next byte in the TWI hardware and initiate another transfer with the interrupt enabled. If there is no futher data, then (maybe reset the TWI hardware) exit the interrupt service routine.

Essentially it is the same process as the non-interrupt technique, except that instead of polling the hardware, the interrupt will tell you when to do the next byte.

When you get it working let me know, and I will try it out. And you should post it to the academy.

-Tony

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

Bazoon wrote:
I have the same question. Please tell me how to convert the example-code discribed in the app-note for TW-Master into GCC compatible code. What has to be done? Thanks for your help

Well, I ahve not gotten to the stage of actually implementing the interrupt diven code. But it looks relatively (!) simple. Actually, a variant of my interrupt-driven EEPROM code ought to do it.

Take a look at my interrupt-driven EEPROM code to see the strategy.

In essence, you need to do the following (off the top of my head)

1) prepare the registers of the TWI hardware to give the clock speed you need. (100k, 400k are 2 "standard" speeds ) (The hardware is capable of about 409K max)

2) create a buffer for the data

3) call a subroutine with a pointer to the data

4) the subroutine moves the data to the buffer, and places the first byte into the TWI hardware and initiates the transfer with the TWI interrupt enabled. Then returns to the calling program.

5) The interrupt service routine, when the interrupt occurs, will check the buffer and place the next byte in the TWI hardware and initiate another transfer with the interrupt enabled. If there is no futher data, then (maybe reset the TWI hardware) exit the interrupt service routine.

Essentially it is the same process as the non-interrupt technique, except that instead of polling the hardware, the interrupt will tell you when to do the next byte.

When you get it working let me know, and I will try it out. And you should post it to the academy.

-Tony

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

I used Atmel's interrupt driven TWI code, and it works fine. However some stuff need to be changed to compile with WinAVR, I can only recall the syntax for defining interrupt routines (the app. note uses #pragma, but WinAVR uses SIGNAL()).

I even had to merge the master and slave routines, because in my application the microcontroller can be both, depending on situation!

/Jakob Selbing

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

@jacksel

Could you post the code here ??

/Bingo

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

jaksel wrote:
I used Atmel's interrupt driven TWI code, and it works fine.

Which code is that? I don't think I saw it. I just did a detailed search of the academy, and found one interrupt controlled module. I did not initially find it because they did not use the term I2C, instead, they used the full written words.

Can you show us what you ended up with?

-Tony

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

HI everybody,

I need to use the library(TWI_Master.h) that comes with the TWI aplication note "AVR315", maybe some little functions from "TWI_Master.c". This is the TWI master example and because of what I've read from APPnote"AVR315" everthing looks fine for my needs, But, I use codevisionAVR V2.05.0 Standard, and those files are for IAR.

¿What precautions I must take to migrate that library and "C-code" to codevisionAVR?

I've copied the code to my codevision and also I've included the library. when compiling I get a error in a pragma instruction about the interrupt TWI, then I've supressed the pragma and now everything compiles fine...

I've just compiled, I haven't proved anything in the hardware!!, I've only been checking the software in the codevision....

Suggestions...¿¿???

regards

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

jedusa wrote:
HI everybody,

I need to use the library(TWI_Master.h) that comes with the TWI aplication note "AVR315", maybe some little functions from "TWI_Master.c". This is the TWI master example and because of what I've read from APPnote"AVR315" everthing looks fine for my needs, But, I use codevisionAVR V2.05.0 Standard, and those files are for IAR.

¿What precautions I must take to migrate that library and "C-code" to codevisionAVR?

I've copied the code to my codevision and also I've included the library. when compiling I get a error in a pragma instruction about the interrupt TWI, then I've supressed the pragma and now everything compiles fine...

I've just compiled, I haven't proved anything in the hardware!!, I've only been checking the software in the codevision....

Suggestions...¿¿???

regards

This is the wrong forum to post in since you are using CodeVision. This forum is for the avr-gcc compiler. Also you should start a new thread instead of replying to a thread that is more then five years old. CodeVision already has I2C routines so you shouldn't even need AVR315.

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

I'm developing an interrupt driven i2c driver for avr that can be used with async io. It also supports firing off asynchronous callbacks from the main thread. If you want a more detailed explanation for how the framework works, you can email me.

 

For example on how to do I2C using interrupts you can check out the I2C driver that I have just uploaded to github: https://github.com/mkschreder/fo...

 

And for an example of how to use the i2c driver in another driver (l3g4200d gyroscope in this case), you can check out the code for the gyro driver here: https://github.com/mkschreder/fo...

LibK - device driver support for flash based microcontrollers: GitHub project

http://oskit.se - join me on my quest to become a better programmer