Compilation Errors with Interrupts

Hi,

I’ve been trying to compile some code I’ve written for my Baby Orangutan and X2 using interrupts; however, I keep getting the following error:

/home/david/lib-orangutan/devices/atmega1284p_x2/../../src/OrangutanSerial/../../src/OrangutanSerial/OrangutanSerial.cpp:62: multiple definition of `__vector_28'

From this error, it seems there are multiple #defines for the interrupt vectors, but I do not see multiple in either the 1284p’s or the 328p’s IO include file. I’m including interrupt.h and have called the ISR’s with (e.g.):

ISR(USART1_RX_vect)
{ /* Code */ }

Any suggestions? Thanks!

Ryan

Hello, Ryan.

OrangutanSerial.cpp defines an ISR(USART1_RX_vect), which you would probably see if you go to the line that is mentioned in the error message you quoted. This means that you should either avoid defining your own ISR(USART1_RX_vect) or you should avoid calling any functions in OrangutanSerial. There can only be one ISR for a given interrupt.

–David

David,

Then I guess my question is, how do I use my serial comms with interrupts? I have some code I’d like to execute when I send/receive data between my X2 and baby. I’ve seen examples of how it’s done with polling but not interrupts.

Thanks,

Ryan

If you just want to execute some code when serial data is received, then I recommend using the functions in the OrangutanSerial library to poll for the data and then acting on it when the data is received. You don’t have to touch interrupts or UART registers if you just use the functions in our library. Is there some reason you don’t want to do it that way?

–David

David,

We have sensors distributed across the X2 and the baby, and what I am wanting to do is have the baby update the X2 with its new sensor readings whenever one changes. I’ve got a global array set up on the X2 that I need filled with the new sensor data from the baby whenever it is available.

Polling is not ideal, but still doable.

Ryan

If your X2 program has a main loop that is executed frequently, then it would be pretty easy to add a function that is called from the main loop that checks to see if there is serial data and updates the array if necessary. If your main loop doesn’t execute frequently, you could still write that function and just call it before any block of code that relies on the sensors values, but make sure you call it frequently enough so that the serial receive buffer in our library doesn’t fill up.

If you really want to use interrupts to update the array, you can do it, but it will be more difficult. You could simply stop using our serial library on the Orangutan X2, and make your own serial code. You would have to learn about all the serial registers and serial interrupts. Alternatively, you could patch our library so that it calls a callback function defined in your code at the end of ISR(USART1_RX_vect). You could probably use our library functions in that callback function, but I’m not sure because they weren’t explicitly designed to be used from interrupts, so you might need to make further modifications to the library.

–David

David,

With your code, what happens when the receive buffer fills? Do you overwrite the data, or do you block?

Thanks,

Ryan

It depends on which method of receiving you use. If you use serial_receive() and the buffer is full, then any additional bytes received on the UART will be lost. that byte will just be lost. If you use serial_receive_ring(), then when the buffer fills up the library will go to the beginning of the buffer and start writing there.

I recommend using serial_receive_ring() because then you can always be ready to receive bytes from the UART. If you make your buffer big enough, and you poll it often enough, and keep track of what point in the buffer your code as read up to, then you can avoid losing data.

Let’s say your baud rate is 115200 and you make a 200 byte receive buffer. It will take at least 200*10/115200 = .017 seconds to fill up that buffer. That’s assuming that the Baby Orangutan is sending a constant stream of serial data.

–David