| Author |
Message |
|
|
Posted: Apr 09, 2012 - 11:06 AM |
|

Joined: Apr 03, 2004
Posts: 407
|
|
For the first time, I try to make use of libraries in AVRGCC (actually it is AS 5.1)
The library should service the TWI interrupt-driven on a ATMEGA32.
This is my ISR:
Code:
ISR(TWI_vect)
{
switch (TWSR & 0xF8)
{
case TW_START: // Start-Condition aufgetreten
{
TWDR=*TWI_Buffer; //Adresse übertragen
TWCR=(1<<TWINT)| (1<<TWEN) | (1<<TWIE);
break;
}
case TW_REP_START:
{
TWDR=*TWI_Buffer; //Adresse übertragen
TWCR=(1<<TWINT)| (1<<TWEN) | (1<<TWIE);
break;
}
case TW_MT_SLA_ACK: //Adresse wurde fertig übetragen
{
TWI_Buffer++;
TWI_Buffer++;
TWDR=*TWI_Buffer++; //1. Datenbyte senden
TWCR=(1<<TWINT)| (1<<TWEN) | (1<<TWIE);
break;
}
case TW_MT_DATA_ACK:
{
TWI_ByteCounter--;
TWI_Buffer++; //Pointer erhöhen
if (TWI_ByteCounter)
{// Es sind noch Bytes zu senden -> weitermachen
TWDR=*TWI_Buffer;
}
else
{// alle Bytes übertragen -> beenden
TWCR=(1<<TWINT)| (1<<TWEN) | (1<<TWSTO );
//Damit sollte auch die Interruptgesteuerte Übetragung sollte enden !
} //else
break;
} //case
} //switch
} //ISR
compiling this, I get a warning that the ISR return type defaults to int, and another that the ISR-function reaches end of non-void function.
Runnning this code, I get stuck in the BADISR-routine.
Why ?
by the way:The TWI-ISR is located in the Library, while the BADISR has been placed in the main-code. |
_________________ I program like a man:
COPY CON: > firmware.hex
|
| |
|
|
|
|
|
Posted: Apr 09, 2012 - 11:22 AM |
|

Joined: Feb 12, 2005
Posts: 16295
Location: Wormshill, England
|
|
|
Quote:
compiling this, I get a warning that the ISR return type defaults to int, and another that the ISR-function reaches end of non-void function.
This means that you have not #included <avr/interrupt.h>
Regarding the logic of your ISR(). I have no idea what you have put in your TWI_buffer[]. But I doubt if your 'logic' does what you want.
What happens if you receive another 'case' ?
The ISR() will never clear the TWINT bit !!
I always recommend using respected library code.
Use it according to its published documentation, and get your project working perfectly.
Now you can develop your design of a square wheel. You can compare its performance with the round variety. There is a very good chance of you being able to improve on a library. At least you can test against it.
David. |
|
|
| |
|
|
|
|
|
Posted: Apr 09, 2012 - 11:26 AM |
|

Joined: Apr 03, 2004
Posts: 407
|
|
Okay, some parts of my question can be answered myself:
1. The error messages about int-return-type and reaching end of non-void function were stopped by including avr/interrupts.h.
2. Even with no more compilation-warnings I still ended up in BADISR until I moved the TWI_vect ISR from the library source file to the main-project.
Now, the code seems to work basically.
But the target was/is to develop a library which just gets linked to the project to provide interrupt-driven TWI-support.
Is there something more to do than just choosing "static library" as the target type ? |
_________________ I program like a man:
COPY CON: > firmware.hex
|
| |
|
|
|
|
|
Posted: Apr 09, 2012 - 11:40 AM |
|

Joined: Feb 12, 2005
Posts: 16295
Location: Wormshill, England
|
|
Yes. You use a respected library in the first place.
1. Atmel has app notes.
2. Pascal Stang.
3. CodeVision.
4. many published avr-gcc projects
5. ...
David. |
|
|
| |
|
|
|
|
|
Posted: Apr 09, 2012 - 06:15 PM |
|

Joined: Apr 03, 2004
Posts: 407
|
|
Okay, I will try again.
The question was basically if libraries can have ISR-routines at first.
My code is surely far away from being perfect or even working as desired. |
_________________ I program like a man:
COPY CON: > firmware.hex
|
| |
|
|
|
|
|
Posted: Apr 09, 2012 - 06:55 PM |
|


Joined: Jul 18, 2005
Posts: 62281
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Quote:
The question was basically if libraries can have ISR-routines at first.
There was a thread about this very thing in the last week. The answer is yes but you need to include a "tag" to force linker inclusion. Searching the terms "ISR library tag" should get you there. |
_________________
|
| |
|
|
|
|
|
Posted: Apr 09, 2012 - 09:48 PM |
|


Joined: Jan 14, 2008
Posts: 1147
Location: San Diego
|
|
|
drnicolas wrote:
Okay, I will try again.
The question was basically if libraries can have ISR-routines at first.
My code is surely far away from being perfect or even working as desired.
Yes they can. In order for it to be linked though a symbol (function, variable...) that is defined in the file that the ISR is in must be used in the main.c file.
That's why I have the init_i2c() function in the same file as the ISR. Calling init_i2c then forces every thing in that file to be linked. |
_________________ ~~John
TWI C source code
|
| |
|
|
|
|
|