interrupt.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2015/9/15 Bernard Update to new interrupt framework.
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <bsp.h>
  13. extern void rt_hw_idt_init(void);
  14. /* exception and interrupt handler table */
  15. struct rt_irq_desc irq_desc[MAX_HANDLERS];
  16. rt_uint16_t irq_mask_8259A = 0xFFFF;
  17. void rt_hw_interrupt_handle(int vector, void* param);
  18. /**
  19. * @addtogroup I386
  20. */
  21. /*@{*/
  22. /**
  23. * This function initializes 8259 interrupt controller
  24. */
  25. void rt_hw_pic_init()
  26. {
  27. outb(IO_PIC1, 0x11);
  28. outb(IO_PIC1+1, IRQ_OFFSET);
  29. outb(IO_PIC1+1, 1<<IRQ_SLAVE);
  30. outb(IO_PIC1+1, 0x3);
  31. outb(IO_PIC1+1, 0xff);
  32. outb(IO_PIC1, 0x68);
  33. outb(IO_PIC1, 0x0a);
  34. outb(IO_PIC2, 0x11);
  35. outb(IO_PIC2+1, IRQ_OFFSET + 8);
  36. outb(IO_PIC2+1, IRQ_SLAVE);
  37. outb(IO_PIC2+1, 0x3);
  38. outb(IO_PIC2+1, 0xff);
  39. outb(IO_PIC2, 0x68);
  40. outb(IO_PIC2, 0x0a);
  41. if (irq_mask_8259A != 0xFFFF)
  42. {
  43. outb(IO_PIC1+1, (char)irq_mask_8259A);
  44. outb(IO_PIC2+1, (char)(irq_mask_8259A >> 8));
  45. }
  46. }
  47. void rt_hw_interrupt_handle(int vector, void* param)
  48. {
  49. rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
  50. }
  51. void rt_hw_isr(int vector)
  52. {
  53. if (vector < MAX_HANDLERS)
  54. {
  55. irq_desc[vector].handler(vector, irq_desc[vector].param);
  56. }
  57. }
  58. /**
  59. * This function initializes interrupt descript table and 8259 interrupt controller
  60. *
  61. */
  62. void rt_hw_interrupt_init(void)
  63. {
  64. int idx;
  65. rt_hw_idt_init();
  66. rt_hw_pic_init();
  67. /* init exceptions table */
  68. for(idx=0; idx < MAX_HANDLERS; idx++)
  69. {
  70. irq_desc[idx].handler = (rt_isr_handler_t)rt_hw_interrupt_handle;
  71. irq_desc[idx].param = RT_NULL;
  72. #ifdef RT_USING_INTERRUPT_INFO
  73. rt_snprintf(irq_desc[idx].name, RT_NAME_MAX - 1, "default");
  74. irq_desc[idx].counter = 0;
  75. #endif
  76. }
  77. }
  78. void rt_hw_interrupt_umask(int vector)
  79. {
  80. irq_mask_8259A = irq_mask_8259A&~(1<<vector);
  81. outb(IO_PIC1+1, (char)irq_mask_8259A);
  82. outb(IO_PIC2+1, (char)(irq_mask_8259A >> 8));
  83. }
  84. void rt_hw_interrupt_mask(int vector)
  85. {
  86. irq_mask_8259A = irq_mask_8259A | (1<<vector);
  87. outb(IO_PIC1+1, (char)irq_mask_8259A);
  88. outb(IO_PIC2+1, (char)(irq_mask_8259A >> 8));
  89. }
  90. rt_isr_handler_t rt_hw_interrupt_install(int vector,
  91. rt_isr_handler_t handler,
  92. void *param,
  93. const char *name)
  94. {
  95. rt_isr_handler_t old_handler = RT_NULL;
  96. if(vector < MAX_HANDLERS)
  97. {
  98. old_handler = irq_desc[vector].handler;
  99. if (handler != RT_NULL)
  100. {
  101. irq_desc[vector].handler = (rt_isr_handler_t)handler;
  102. irq_desc[vector].param = param;
  103. #ifdef RT_USING_INTERRUPT_INFO
  104. rt_snprintf(irq_desc[vector].name, RT_NAME_MAX - 1, "%s", name);
  105. irq_desc[vector].counter = 0;
  106. #endif
  107. }
  108. }
  109. return old_handler;
  110. }
  111. /*@}*/