Disables then re-enables interrupts based on variable scope. Ensures you don't forget to re-enable interrupts. Use Wisely!
intdisabler.h@0:cd6400023513, 2014-04-23 (annotated)
- Committer:
- Tomo2k
- Date:
- Wed Apr 23 10:16:56 2014 +0000
- Revision:
- 0:cd6400023513
IntDisabler class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Tomo2k | 0:cd6400023513 | 1 | /* (C) 2014 Richard Thompson (Tomo2k) |
Tomo2k | 0:cd6400023513 | 2 | This software is Apache 2.0 licenced for this to be reused. |
Tomo2k | 0:cd6400023513 | 3 | |
Tomo2k | 0:cd6400023513 | 4 | That said, it's trivial and rather obvious! |
Tomo2k | 0:cd6400023513 | 5 | */ |
Tomo2k | 0:cd6400023513 | 6 | #pragma once |
Tomo2k | 0:cd6400023513 | 7 | |
Tomo2k | 0:cd6400023513 | 8 | #include "mbed.h" |
Tomo2k | 0:cd6400023513 | 9 | |
Tomo2k | 0:cd6400023513 | 10 | //! Disable then Enable interrupts using variable scope. |
Tomo2k | 0:cd6400023513 | 11 | //! Ensures always re-enabled regardless of method exit |
Tomo2k | 0:cd6400023513 | 12 | class IntDisabler |
Tomo2k | 0:cd6400023513 | 13 | { |
Tomo2k | 0:cd6400023513 | 14 | public: |
Tomo2k | 0:cd6400023513 | 15 | //! Disable Interrupts |
Tomo2k | 0:cd6400023513 | 16 | IntDisabler() { |
Tomo2k | 0:cd6400023513 | 17 | __disable_irq(); |
Tomo2k | 0:cd6400023513 | 18 | } |
Tomo2k | 0:cd6400023513 | 19 | //! Enables Interrupts on destruction |
Tomo2k | 0:cd6400023513 | 20 | ~IntDisabler() { |
Tomo2k | 0:cd6400023513 | 21 | __enable_irq(); |
Tomo2k | 0:cd6400023513 | 22 | } |
Tomo2k | 0:cd6400023513 | 23 | private: |
Tomo2k | 0:cd6400023513 | 24 | // Disable copy |
Tomo2k | 0:cd6400023513 | 25 | IntDisabler & operator=(const IntDisabler&); |
Tomo2k | 0:cd6400023513 | 26 | IntDisabler(const IntDisabler &); |
Tomo2k | 0:cd6400023513 | 27 | }; |