Enable global interrupts in library?

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

I'm writing a library for the AVR (more specifically, the ATMEGA328p) that requires global interrupts to be enabled.

Is it common practice for a library to enable global interrupts in its initialization routine, or is it more common to expect the application to enable them?

On the one hand, some developers might want tight control over when the global interrupts are enabled or disabled, which means my library's documentation would have to say that they need to be enabled.

On the other hand, I want to make my library relatively easy to use right away without the end-user having to Read The Fine Manual before my library will work as advertised.

What do you guys think? Does it depend on my target audience?

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

Quote:
Is it common practice for a library to enable global interrupts in its initialization routine, or is it more common to expect the application to enable them?

I think the user would expect to call all the various init calls (ADC_init(), UART_init(), etc.) and THEN sei(). Otherwise you may have turned it on already when they then call Timer_init() and when the timer is only half initialized the first interrupt fires.

So I'd just document that the user must take the responsibility. Of course if you have a subsequent DEVICE_putchar() or DEVICE_getchar() then they could check the state of I and enable it if it were found that it still wasn't enabled.

If you think the user won't read the manual then rename DEVICE_init() as DEVICE_init_but_dont_forget_to_enable_interrupts() ;-)

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

Thanks. I was leaning toward having my library enable interrupts, but you made a good case against that.

I will include a short demo program that calls sei() after the init routine with a comment that clearly says that global interrupts must be enabled. I'll also include that demo program and mention global interrupts in the API reference in my documentation. Hopefully that will be enough.