You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.6 KiB

  1. /* Copyright 2021 QMK
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include <ch.h>
  18. static __inline__ uint8_t __interrupt_disable__(void) {
  19. chSysLock();
  20. return 1;
  21. }
  22. static __inline__ void __interrupt_enable__(const uint8_t *__s) {
  23. chSysUnlock();
  24. __asm__ volatile("" ::: "memory");
  25. (void)__s;
  26. }
  27. static __inline__ syssts_t __interrupt_lock__(void) {
  28. return chSysGetStatusAndLockX();
  29. }
  30. static __inline__ void __interrupt_unlock__(const syssts_t *__s) {
  31. chSysRestoreStatusX(*__s);
  32. __asm__ volatile("" ::: "memory");
  33. }
  34. #define ATOMIC_BLOCK(type) for (type, __ToDo = 1; __ToDo; __ToDo = 0)
  35. #define ATOMIC_FORCEON uint8_t status_save __attribute__((__cleanup__(__interrupt_enable__))) = __interrupt_disable__()
  36. #define ATOMIC_RESTORESTATE syssts_t status_save __attribute__((__cleanup__(__interrupt_unlock__))) = __interrupt_lock__()
  37. #define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
  38. #define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON)