cpuport.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * 2011-06-15 aozima the first version for lpc214x
  9. * 2013-03-29 aozima Modify the interrupt interface implementations.
  10. */
  11. #include <rtthread.h>
  12. #include <rthw.h>
  13. #include "lpc214x.h"
  14. #define MAX_HANDLERS 32
  15. #define SVCMODE 0x13
  16. extern rt_uint32_t rt_interrupt_nest;
  17. /* exception and interrupt handler table */
  18. struct rt_irq_desc irq_desc[MAX_HANDLERS];
  19. /**
  20. * @addtogroup LPC214x
  21. */
  22. /*@{*/
  23. /**
  24. * This function will initialize thread stack
  25. *
  26. * @param tentry the entry of thread
  27. * @param parameter the parameter of entry
  28. * @param stack_addr the beginning stack address
  29. * @param texit the function will be called when thread exit
  30. *
  31. * @return stack address
  32. */
  33. rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
  34. rt_uint8_t *stack_addr, void *texit)
  35. {
  36. unsigned long *stk;
  37. stk = (unsigned long *)stack_addr;
  38. *(stk) = (unsigned long)tentry; /* entry point */
  39. *(--stk) = (unsigned long)texit; /* lr */
  40. *(--stk) = 0; /* r12 */
  41. *(--stk) = 0; /* r11 */
  42. *(--stk) = 0; /* r10 */
  43. *(--stk) = 0; /* r9 */
  44. *(--stk) = 0; /* r8 */
  45. *(--stk) = 0; /* r7 */
  46. *(--stk) = 0; /* r6 */
  47. *(--stk) = 0; /* r5 */
  48. *(--stk) = 0; /* r4 */
  49. *(--stk) = 0; /* r3 */
  50. *(--stk) = 0; /* r2 */
  51. *(--stk) = 0; /* r1 */
  52. *(--stk) = (unsigned long)parameter; /* r0 : argument */
  53. /* cpsr */
  54. if ((rt_uint32_t)tentry & 0x01)
  55. *(--stk) = SVCMODE | 0x20; /* thumb mode */
  56. else
  57. *(--stk) = SVCMODE; /* arm mode */
  58. /* return task's current stack address */
  59. return (rt_uint8_t *)stk;
  60. }
  61. /* exception and interrupt handler table */
  62. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  63. rt_uint32_t rt_thread_switch_interrupt_flag;
  64. void rt_hw_interrupt_handler(int vector, void *param)
  65. {
  66. rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
  67. }
  68. /**
  69. * This function will initialize hardware interrupt
  70. */
  71. void rt_hw_interrupt_init(void)
  72. {
  73. rt_base_t index;
  74. rt_uint32_t *vect_addr, *vect_ctl;
  75. /* initialize VIC*/
  76. VICIntEnClr = 0xffffffff;
  77. VICVectAddr = 0;
  78. /* set all to IRQ */
  79. VICIntSelect = 0;
  80. rt_memset(irq_desc, 0x00, sizeof(irq_desc));
  81. for (index = 0; index < MAX_HANDLERS; index ++)
  82. {
  83. irq_desc[index].handler = rt_hw_interrupt_handler;
  84. vect_addr = (rt_uint32_t *)(VIC_BASE_ADDR + 0x100 + (index << 2));
  85. vect_ctl = (rt_uint32_t *)(VIC_BASE_ADDR + 0x200 + (index << 2));
  86. *vect_addr = (rt_uint32_t)&irq_desc[index];
  87. *vect_ctl = 0xF;
  88. }
  89. /* init interrupt nest, and context in thread sp */
  90. rt_interrupt_nest = 0;
  91. rt_interrupt_from_thread = 0;
  92. rt_interrupt_to_thread = 0;
  93. rt_thread_switch_interrupt_flag = 0;
  94. }
  95. /**
  96. * This function will mask a interrupt.
  97. * @param vector the interrupt number
  98. */
  99. void rt_hw_interrupt_mask(int vector)
  100. {
  101. VICIntEnClr = (1 << vector);
  102. }
  103. /**
  104. * This function will un-mask a interrupt.
  105. * @param vector the interrupt number
  106. */
  107. void rt_hw_interrupt_umask(int vector)
  108. {
  109. VICIntEnable = (1 << vector);
  110. }
  111. /**
  112. * This function will install a interrupt service routine to a interrupt.
  113. * @param vector the interrupt number
  114. * @param handler the interrupt service routine to be installed
  115. * @param param the interrupt service function parameter
  116. * @param name the interrupt name
  117. * @return old handler
  118. */
  119. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  120. void *param, const char *name)
  121. {
  122. rt_isr_handler_t old_handler = RT_NULL;
  123. if(vector >= 0 && vector < MAX_HANDLERS)
  124. {
  125. rt_uint32_t* vect_ctl = (rt_uint32_t *)(VIC_BASE_ADDR + 0x200 + (vector << 2));
  126. /* assign IRQ slot and enable this slot */
  127. *vect_ctl = 0x20 | (vector & 0x1F);
  128. old_handler = irq_desc[vector].handler;
  129. if (handler != RT_NULL)
  130. {
  131. irq_desc[vector].handler = handler;
  132. irq_desc[vector].param = param;
  133. }
  134. }
  135. return old_handler;
  136. }
  137. void rt_hw_trap_irq(void)
  138. {
  139. int irqno;
  140. struct rt_irq_desc* irq;
  141. extern struct rt_irq_desc irq_desc[];
  142. irq = (struct rt_irq_desc*) VICVectAddr;
  143. irqno = ((rt_uint32_t) irq - (rt_uint32_t) &irq_desc[0])/sizeof(struct rt_irq_desc);
  144. /* invoke isr */
  145. irq->handler(irqno, irq->param);
  146. /* acknowledge Interrupt */
  147. // VICVectAddr = 0;
  148. }
  149. void rt_hw_trap_fiq(void)
  150. {
  151. rt_kprintf("fast interrupt request\n");
  152. }
  153. /*@}*/