machine_interface_internal.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef MACHINE_INTERFACE_INTERNAL_H_
  2. #define MACHINE_INTERFACE_INTERNAL_H_
  3. #include "util.h"
  4. #include "machine_interface.h"
  5. #define INTERRUPT_QUEUE_MAX_LEN 16
  6. /** interrupt_queue_freecount - Returns count of free slots in TX queue.
  7. * Count can change at any time, if IRQs are enabled.
  8. */
  9. uint8_t interrupt_queue_freecount(void);
  10. void send_interrupt_count(const struct control_interrupt *irq,
  11. uint8_t size, uint8_t count);
  12. /** send_interrupt - Send an interrupt to the host.
  13. * This is the API for sending an interrupt.
  14. */
  15. static inline
  16. void send_interrupt(const struct control_interrupt *irq,
  17. uint8_t size)
  18. {
  19. send_interrupt_count(irq, size, 1);
  20. }
  21. /** send_interrupt_discard_old - Send an interrupt to the host.
  22. * Also discard already queued IRQs of the same type.
  23. * It's not guaranteed that all old IRQs are discarded, though.
  24. */
  25. void send_interrupt_discard_old(const struct control_interrupt *irq,
  26. uint8_t size);
  27. /** get_active_devflags - Get device flags atomically.
  28. */
  29. uint16_t get_active_devflags(void);
  30. static inline uint8_t get_active_devflags_low(void)
  31. {
  32. extern uint16_t active_devflags;
  33. mb();
  34. return lo8(active_devflags); /* atomic on AVR */
  35. }
  36. static inline uint8_t get_active_devflags_high(void)
  37. {
  38. extern uint16_t active_devflags;
  39. mb();
  40. return hi8(active_devflags); /* atomic on AVR */
  41. }
  42. /** devflag_is_set - Flag test optimized for constant mask.
  43. * Inefficient for non-const mask! */
  44. static inline bool devflag_is_set(const uint16_t mask)
  45. {
  46. bool res;
  47. if (mask == 0)
  48. res = 0;
  49. else if (hi8(mask) == 0)
  50. res = !!(get_active_devflags_low() & lo8(mask));
  51. else if (lo8(mask) == 0)
  52. res = !!(get_active_devflags_high() & hi8(mask));
  53. else
  54. res = !!(get_active_devflags() & mask);
  55. return res;
  56. }
  57. /** modify_devflags - Modify device flags atomically
  58. * and send notification interrupt to the host.
  59. */
  60. void modify_devflags(uint16_t mask, uint16_t set);
  61. /** reset_devflags - Reset device flags to defaults */
  62. void reset_devflags(void);
  63. #endif /* MACHINE_INTERFACE_INTERNAL_H_ */