cpuport.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * File : cpuport.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009 - 2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2011-06-15 aozima the first version for lpc214x
  13. */
  14. #include <rtthread.h>
  15. #include "lpc214x.h"
  16. #define MAX_HANDLERS 32
  17. #define SVCMODE 0x13
  18. extern rt_uint32_t rt_interrupt_nest;
  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)
  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()
  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. for (index = 0; index < MAX_HANDLERS; index ++)
  81. {
  82. vect_addr = (rt_uint32_t *)(VIC_BASE_ADDR + 0x100 + (index << 2));
  83. vect_ctl = (rt_uint32_t *)(VIC_BASE_ADDR + 0x200 + (index << 2));
  84. *vect_addr = (rt_uint32_t)rt_hw_interrupt_handler;
  85. *vect_ctl = 0xF;
  86. }
  87. /* init interrupt nest, and context in thread sp */
  88. rt_interrupt_nest = 0;
  89. rt_interrupt_from_thread = 0;
  90. rt_interrupt_to_thread = 0;
  91. rt_thread_switch_interrupt_flag = 0;
  92. }
  93. /**
  94. * This function will mask a interrupt.
  95. * @param vector the interrupt number
  96. */
  97. void rt_hw_interrupt_mask(int vector)
  98. {
  99. VICIntEnClr = (1 << vector);
  100. }
  101. /**
  102. * This function will un-mask a interrupt.
  103. * @param vector the interrupt number
  104. */
  105. void rt_hw_interrupt_umask(int vector)
  106. {
  107. VICIntEnable = (1 << vector);
  108. }
  109. /**
  110. * This function will install a interrupt service routine to a interrupt.
  111. * @param vector the interrupt number
  112. * @param new_handler the interrupt service routine to be installed
  113. * @param old_handler the old interrupt service routine
  114. */
  115. void rt_hw_interrupt_install(int vector, rt_isr_handler_t new_handler, rt_isr_handler_t *old_handler)
  116. {
  117. if(vector >= 0 && vector < MAX_HANDLERS)
  118. {
  119. /* get VIC address */
  120. rt_uint32_t* vect_addr = (rt_uint32_t *)(VIC_BASE_ADDR + 0x100 + (vector << 2));
  121. rt_uint32_t* vect_ctl = (rt_uint32_t *)(VIC_BASE_ADDR + 0x200 + (vector << 2));
  122. /* assign IRQ slot and enable this slot */
  123. *vect_ctl = 0x20 | (vector & 0x1F);
  124. if (old_handler != RT_NULL) *old_handler = (rt_isr_handler_t) *vect_addr;
  125. if (new_handler != RT_NULL) *vect_addr = (rt_uint32_t) new_handler;
  126. }
  127. }
  128. /**
  129. * this function will reset CPU
  130. *
  131. */
  132. void rt_hw_cpu_reset()
  133. {
  134. }
  135. /**
  136. * this function will shutdown CPU
  137. *
  138. */
  139. void rt_hw_cpu_shutdown()
  140. {
  141. rt_kprintf("shutdown...\n");
  142. while (1);
  143. }
  144. void rt_hw_trap_irq()
  145. {
  146. rt_isr_handler_t isr_func;
  147. isr_func = (rt_isr_handler_t) VICVectAddr;
  148. isr_func(0);
  149. /* acknowledge Interrupt */
  150. // VICVectAddr = 0;
  151. }
  152. void rt_hw_trap_fiq()
  153. {
  154. rt_kprintf("fast interrupt request\n");
  155. }
  156. /*@}*/