Interrupt vector in library

I started to touch AVR and PlatrformIO and play with them. What I’m doing and why isn’t important, it’s just for fun and studying.

At first, I wrote ISR in my main source file. It worked perfectly, as I expected.
Further, I decided to create a library in directory lib, put there one header and one cpp file.
The last one contains ISR:

ISR(USART_UDRE_vect, ISR_NAKED) {
    asm volatile(
    "\n\t"
    ....
    "reti\n\t"
    :
    :[USART_B]"M"(&UCSR0B)...
}

Then I run pio run and found, that this ISR was not put into firmware

N-Ryzhkov-MBP:ser namezys$ avr-objdump -S  .pio/build/uno/firmware.elf

.pio/build/uno/firmware.elf:     file format elf32-avr

Disassembly of section .text:

00000000 <__vectors>:
   0:	0c 94 34 00 	jmp	0x68	; 0x68 <__ctors_end>
   4:	0c 94 3e 00 	jmp	0x7c	; 0x7c <__bad_interrupt>
... (everything is same)
  64:	0c 94 3e 00 	jmp	0x7c	; 0x7c <__bad_interrupt>

what I’m doing wrong?

Linking with weakly defined ISRs only works with lib_archive = no – many threads here regarding e.g. FreeRTOS + SVC/Timer interrupts are also about this.

If you want PlatformIO to do this automatically for your library, give your library folder within lib/ a library.json and specify libArchive to true.

1 Like