interrupt.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * File : interrupt.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, 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://openlab.rt-thread.com/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2011-01-13 weety first version
  13. */
  14. #include <rtthread.h>
  15. #include "at91sam926x.h"
  16. #define MAX_HANDLERS (AIC_IRQS + PIN_IRQS)
  17. extern rt_uint32_t rt_interrupt_nest;
  18. /* exception and interrupt handler table */
  19. rt_isr_handler_t isr_table[MAX_HANDLERS];
  20. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  21. rt_uint32_t rt_thread_switch_interrput_flag;
  22. /* --------------------------------------------------------------------
  23. * Interrupt initialization
  24. * -------------------------------------------------------------------- */
  25. rt_uint32_t at91_extern_irq;
  26. #define is_extern_irq(irq) ((1 << (irq)) & at91_extern_irq)
  27. /*
  28. * The default interrupt priority levels (0 = lowest, 7 = highest).
  29. */
  30. static rt_uint32_t at91sam9260_default_irq_priority[MAX_HANDLERS] = {
  31. 7, /* Advanced Interrupt Controller */
  32. 7, /* System Peripherals */
  33. 1, /* Parallel IO Controller A */
  34. 1, /* Parallel IO Controller B */
  35. 1, /* Parallel IO Controller C */
  36. 0, /* Analog-to-Digital Converter */
  37. 5, /* USART 0 */
  38. 5, /* USART 1 */
  39. 5, /* USART 2 */
  40. 0, /* Multimedia Card Interface */
  41. 2, /* USB Device Port */
  42. 6, /* Two-Wire Interface */
  43. 5, /* Serial Peripheral Interface 0 */
  44. 5, /* Serial Peripheral Interface 1 */
  45. 5, /* Serial Synchronous Controller */
  46. 0,
  47. 0,
  48. 0, /* Timer Counter 0 */
  49. 0, /* Timer Counter 1 */
  50. 0, /* Timer Counter 2 */
  51. 2, /* USB Host port */
  52. 3, /* Ethernet */
  53. 0, /* Image Sensor Interface */
  54. 5, /* USART 3 */
  55. 5, /* USART 4 */
  56. 5, /* USART 5 */
  57. 0, /* Timer Counter 3 */
  58. 0, /* Timer Counter 4 */
  59. 0, /* Timer Counter 5 */
  60. 0, /* Advanced Interrupt Controller */
  61. 0, /* Advanced Interrupt Controller */
  62. 0, /* Advanced Interrupt Controller */
  63. };
  64. /**
  65. * @addtogroup AT91SAM926X
  66. */
  67. /*@{*/
  68. rt_isr_handler_t rt_hw_interrupt_handle(rt_uint32_t vector)
  69. {
  70. rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
  71. return RT_NULL;
  72. }
  73. rt_isr_handler_t at91_gpio_irq_handle(rt_uint32_t vector)
  74. {
  75. rt_uint32_t isr, pio, irq_n;
  76. if (vector == AT91SAM9260_ID_PIOA)
  77. {
  78. pio = AT91_PIOA;
  79. irq_n = AIC_IRQS;
  80. }
  81. else if (vector == AT91SAM9260_ID_PIOB)
  82. {
  83. pio = AT91_PIOB;
  84. irq_n = AIC_IRQS + 32;
  85. }
  86. else if (vector == AT91SAM9260_ID_PIOC)
  87. {
  88. pio = AT91_PIOC;
  89. irq_n = AIC_IRQS + 32*2;
  90. }
  91. else
  92. return;
  93. isr = at91_sys_read(pio+PIO_ISR) & at91_sys_read(pio+PIO_IMR);
  94. while (isr)
  95. {
  96. if (isr & 1)
  97. {
  98. isr_table[irq_n](irq_n);
  99. }
  100. isr >>= 1;
  101. irq_n++;
  102. }
  103. }
  104. /*
  105. * Initialize the AIC interrupt controller.
  106. */
  107. void at91_aic_init(rt_uint32_t *priority)
  108. {
  109. rt_uint32_t i;
  110. /*
  111. * The IVR is used by macro get_irqnr_and_base to read and verify.
  112. * The irq number is NR_AIC_IRQS when a spurious interrupt has occurred.
  113. */
  114. for (i = 0; i < AIC_IRQS; i++) {
  115. /* Put irq number in Source Vector Register: */
  116. at91_sys_write(AT91_AIC_SVR(i), i);
  117. /* Active Low interrupt, with the specified priority */
  118. at91_sys_write(AT91_AIC_SMR(i), AT91_AIC_SRCTYPE_LOW | priority[i]);
  119. //AT91_AIC_SRCTYPE_FALLING
  120. /* Perform 8 End Of Interrupt Command to make sure AIC will not Lock out nIRQ */
  121. if (i < 8)
  122. at91_sys_write(AT91_AIC_EOICR, 0);
  123. }
  124. /*
  125. * Spurious Interrupt ID in Spurious Vector Register is NR_AIC_IRQS
  126. * When there is no current interrupt, the IRQ Vector Register reads the value stored in AIC_SPU
  127. */
  128. at91_sys_write(AT91_AIC_SPU, AIC_IRQS);
  129. /* No debugging in AIC: Debug (Protect) Control Register */
  130. at91_sys_write(AT91_AIC_DCR, 0);
  131. /* Disable and clear all interrupts initially */
  132. at91_sys_write(AT91_AIC_IDCR, 0xFFFFFFFF);
  133. at91_sys_write(AT91_AIC_ICCR, 0xFFFFFFFF);
  134. }
  135. static void at91_gpio_irq_init()
  136. {
  137. at91_sys_write(AT91_PIOA+PIO_IDR, 0xffffffff);
  138. at91_sys_write(AT91_PIOB+PIO_IDR, 0xffffffff);
  139. at91_sys_write(AT91_PIOC+PIO_IDR, 0xffffffff);
  140. isr_table[AT91SAM9260_ID_PIOA] = (rt_isr_handler_t)at91_gpio_irq_handle;
  141. isr_table[AT91SAM9260_ID_PIOB] = (rt_isr_handler_t)at91_gpio_irq_handle;
  142. isr_table[AT91SAM9260_ID_PIOC] = (rt_isr_handler_t)at91_gpio_irq_handle;
  143. rt_hw_interrupt_umask(AT91SAM9260_ID_PIOA);
  144. rt_hw_interrupt_umask(AT91SAM9260_ID_PIOB);
  145. rt_hw_interrupt_umask(AT91SAM9260_ID_PIOC);
  146. }
  147. /**
  148. * This function will initialize hardware interrupt
  149. */
  150. void rt_hw_interrupt_init(void)
  151. {
  152. rt_int32_t i;
  153. register rt_uint32_t idx;
  154. rt_uint32_t *priority = at91sam9260_default_irq_priority;
  155. at91_extern_irq = (1 << AT91SAM9260_ID_IRQ0) | (1 << AT91SAM9260_ID_IRQ1)
  156. | (1 << AT91SAM9260_ID_IRQ2);
  157. /* Initialize the AIC interrupt controller */
  158. at91_aic_init(priority);
  159. /* init exceptions table */
  160. for(idx=0; idx < MAX_HANDLERS; idx++)
  161. {
  162. isr_table[idx] = (rt_isr_handler_t)rt_hw_interrupt_handle;
  163. }
  164. at91_gpio_irq_init();
  165. /* init interrupt nest, and context in thread sp */
  166. rt_interrupt_nest = 0;
  167. rt_interrupt_from_thread = 0;
  168. rt_interrupt_to_thread = 0;
  169. rt_thread_switch_interrput_flag = 0;
  170. }
  171. static void at91_gpio_irq_mask(int irq)
  172. {
  173. rt_uint32_t pin, pio, bank;
  174. bank = (irq - AIC_IRQS)>>5;
  175. if (bank == 0)
  176. {
  177. pio = AT91_PIOA;
  178. }
  179. else if (bank == 1)
  180. {
  181. pio = AT91_PIOB;
  182. }
  183. else if (bank == 2)
  184. {
  185. pio = AT91_PIOC;
  186. }
  187. else
  188. return;
  189. pin = 1 << ((irq - AIC_IRQS) & 31);
  190. at91_sys_write(pio+PIO_IDR, pin);
  191. }
  192. /**
  193. * This function will mask a interrupt.
  194. * @param vector the interrupt number
  195. */
  196. void rt_hw_interrupt_mask(int irq)
  197. {
  198. if (irq >= AIC_IRQS)
  199. {
  200. at91_gpio_irq_mask(irq);
  201. }
  202. else
  203. {
  204. /* Disable interrupt on AIC */
  205. at91_sys_write(AT91_AIC_IDCR, 1 << irq);
  206. }
  207. }
  208. static void at91_gpio_irq_umask(int irq)
  209. {
  210. rt_uint32_t pin, pio, bank;
  211. bank = (irq - AIC_IRQS)>>5;
  212. if (bank == 0)
  213. {
  214. pio = AT91_PIOA;
  215. }
  216. else if (bank == 1)
  217. {
  218. pio = AT91_PIOB;
  219. }
  220. else if (bank == 2)
  221. {
  222. pio = AT91_PIOC;
  223. }
  224. else
  225. return;
  226. pin = 1 << ((irq - AIC_IRQS) & 31);
  227. at91_sys_write(pio+PIO_IER, pin);
  228. }
  229. /**
  230. * This function will un-mask a interrupt.
  231. * @param vector the interrupt number
  232. */
  233. void rt_hw_interrupt_umask(int irq)
  234. {
  235. if (irq >= AIC_IRQS)
  236. {
  237. at91_gpio_irq_umask(irq);
  238. }
  239. else
  240. {
  241. /* Enable interrupt on AIC */
  242. at91_sys_write(AT91_AIC_IECR, 1 << irq);
  243. }
  244. }
  245. /**
  246. * This function will install a interrupt service routine to a interrupt.
  247. * @param vector the interrupt number
  248. * @param new_handler the interrupt service routine to be installed
  249. * @param old_handler the old interrupt service routine
  250. */
  251. void rt_hw_interrupt_install(int vector, rt_isr_handler_t new_handler, rt_isr_handler_t *old_handler)
  252. {
  253. if(vector < MAX_HANDLERS)
  254. {
  255. if (old_handler != RT_NULL) *old_handler = isr_table[vector];
  256. if (new_handler != RT_NULL) isr_table[vector] = new_handler;
  257. }
  258. }
  259. /*@}*/
  260. static int at91_aic_set_type(unsigned irq, unsigned type)
  261. {
  262. unsigned int smr, srctype;
  263. switch (type) {
  264. case IRQ_TYPE_LEVEL_HIGH:
  265. srctype = AT91_AIC_SRCTYPE_HIGH;
  266. break;
  267. case IRQ_TYPE_EDGE_RISING:
  268. srctype = AT91_AIC_SRCTYPE_RISING;
  269. break;
  270. case IRQ_TYPE_LEVEL_LOW:
  271. if ((irq == AT91_ID_FIQ) || is_extern_irq(irq)) /* only supported on external interrupts */
  272. srctype = AT91_AIC_SRCTYPE_LOW;
  273. else
  274. return -1;
  275. break;
  276. case IRQ_TYPE_EDGE_FALLING:
  277. if ((irq == AT91_ID_FIQ) || is_extern_irq(irq)) /* only supported on external interrupts */
  278. srctype = AT91_AIC_SRCTYPE_FALLING;
  279. else
  280. return -1;
  281. break;
  282. default:
  283. return -1;
  284. }
  285. smr = at91_sys_read(AT91_AIC_SMR(irq)) & ~AT91_AIC_SRCTYPE;
  286. at91_sys_write(AT91_AIC_SMR(irq), smr | srctype);
  287. return 0;
  288. }