interrupt.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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_interrupt_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. void rt_hw_interrupt_mask(int irq);
  69. void rt_hw_interrupt_umask(int irq);
  70. rt_isr_handler_t rt_hw_interrupt_handle(rt_uint32_t vector)
  71. {
  72. rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
  73. return RT_NULL;
  74. }
  75. rt_isr_handler_t at91_gpio_irq_handle(rt_uint32_t vector)
  76. {
  77. rt_uint32_t isr, pio, irq_n;
  78. if (vector == AT91SAM9260_ID_PIOA)
  79. {
  80. pio = AT91_PIOA;
  81. irq_n = AIC_IRQS;
  82. }
  83. else if (vector == AT91SAM9260_ID_PIOB)
  84. {
  85. pio = AT91_PIOB;
  86. irq_n = AIC_IRQS + 32;
  87. }
  88. else if (vector == AT91SAM9260_ID_PIOC)
  89. {
  90. pio = AT91_PIOC;
  91. irq_n = AIC_IRQS + 32*2;
  92. }
  93. else
  94. return RT_NULL;
  95. isr = at91_sys_read(pio+PIO_ISR) & at91_sys_read(pio+PIO_IMR);
  96. while (isr)
  97. {
  98. if (isr & 1)
  99. {
  100. isr_table[irq_n](irq_n);
  101. }
  102. isr >>= 1;
  103. irq_n++;
  104. }
  105. return RT_NULL;
  106. }
  107. /*
  108. * Initialize the AIC interrupt controller.
  109. */
  110. void at91_aic_init(rt_uint32_t *priority)
  111. {
  112. rt_uint32_t i;
  113. /*
  114. * The IVR is used by macro get_irqnr_and_base to read and verify.
  115. * The irq number is NR_AIC_IRQS when a spurious interrupt has occurred.
  116. */
  117. for (i = 0; i < AIC_IRQS; i++) {
  118. /* Put irq number in Source Vector Register: */
  119. at91_sys_write(AT91_AIC_SVR(i), i);
  120. /* Active Low interrupt, with the specified priority */
  121. at91_sys_write(AT91_AIC_SMR(i), AT91_AIC_SRCTYPE_LOW | priority[i]);
  122. //AT91_AIC_SRCTYPE_FALLING
  123. /* Perform 8 End Of Interrupt Command to make sure AIC will not Lock out nIRQ */
  124. if (i < 8)
  125. at91_sys_write(AT91_AIC_EOICR, 0);
  126. }
  127. /*
  128. * Spurious Interrupt ID in Spurious Vector Register is NR_AIC_IRQS
  129. * When there is no current interrupt, the IRQ Vector Register reads the value stored in AIC_SPU
  130. */
  131. at91_sys_write(AT91_AIC_SPU, AIC_IRQS);
  132. /* No debugging in AIC: Debug (Protect) Control Register */
  133. at91_sys_write(AT91_AIC_DCR, 0);
  134. /* Disable and clear all interrupts initially */
  135. at91_sys_write(AT91_AIC_IDCR, 0xFFFFFFFF);
  136. at91_sys_write(AT91_AIC_ICCR, 0xFFFFFFFF);
  137. }
  138. static void at91_gpio_irq_init()
  139. {
  140. at91_sys_write(AT91_PIOA+PIO_IDR, 0xffffffff);
  141. at91_sys_write(AT91_PIOB+PIO_IDR, 0xffffffff);
  142. at91_sys_write(AT91_PIOC+PIO_IDR, 0xffffffff);
  143. isr_table[AT91SAM9260_ID_PIOA] = (rt_isr_handler_t)at91_gpio_irq_handle;
  144. isr_table[AT91SAM9260_ID_PIOB] = (rt_isr_handler_t)at91_gpio_irq_handle;
  145. isr_table[AT91SAM9260_ID_PIOC] = (rt_isr_handler_t)at91_gpio_irq_handle;
  146. rt_hw_interrupt_umask(AT91SAM9260_ID_PIOA);
  147. rt_hw_interrupt_umask(AT91SAM9260_ID_PIOB);
  148. rt_hw_interrupt_umask(AT91SAM9260_ID_PIOC);
  149. }
  150. /**
  151. * This function will initialize hardware interrupt
  152. */
  153. void rt_hw_interrupt_init(void)
  154. {
  155. rt_int32_t i;
  156. register rt_uint32_t idx;
  157. rt_uint32_t *priority = at91sam9260_default_irq_priority;
  158. at91_extern_irq = (1 << AT91SAM9260_ID_IRQ0) | (1 << AT91SAM9260_ID_IRQ1)
  159. | (1 << AT91SAM9260_ID_IRQ2);
  160. /* Initialize the AIC interrupt controller */
  161. at91_aic_init(priority);
  162. /* init exceptions table */
  163. for(idx=0; idx < MAX_HANDLERS; idx++)
  164. {
  165. isr_table[idx] = (rt_isr_handler_t)rt_hw_interrupt_handle;
  166. }
  167. at91_gpio_irq_init();
  168. /* init interrupt nest, and context in thread sp */
  169. rt_interrupt_nest = 0;
  170. rt_interrupt_from_thread = 0;
  171. rt_interrupt_to_thread = 0;
  172. rt_thread_switch_interrupt_flag = 0;
  173. }
  174. static void at91_gpio_irq_mask(int irq)
  175. {
  176. rt_uint32_t pin, pio, bank;
  177. bank = (irq - AIC_IRQS)>>5;
  178. if (bank == 0)
  179. {
  180. pio = AT91_PIOA;
  181. }
  182. else if (bank == 1)
  183. {
  184. pio = AT91_PIOB;
  185. }
  186. else if (bank == 2)
  187. {
  188. pio = AT91_PIOC;
  189. }
  190. else
  191. return;
  192. pin = 1 << ((irq - AIC_IRQS) & 31);
  193. at91_sys_write(pio+PIO_IDR, pin);
  194. }
  195. /**
  196. * This function will mask a interrupt.
  197. * @param vector the interrupt number
  198. */
  199. void rt_hw_interrupt_mask(int irq)
  200. {
  201. if (irq >= AIC_IRQS)
  202. {
  203. at91_gpio_irq_mask(irq);
  204. }
  205. else
  206. {
  207. /* Disable interrupt on AIC */
  208. at91_sys_write(AT91_AIC_IDCR, 1 << irq);
  209. }
  210. }
  211. static void at91_gpio_irq_umask(int irq)
  212. {
  213. rt_uint32_t pin, pio, bank;
  214. bank = (irq - AIC_IRQS)>>5;
  215. if (bank == 0)
  216. {
  217. pio = AT91_PIOA;
  218. }
  219. else if (bank == 1)
  220. {
  221. pio = AT91_PIOB;
  222. }
  223. else if (bank == 2)
  224. {
  225. pio = AT91_PIOC;
  226. }
  227. else
  228. return;
  229. pin = 1 << ((irq - AIC_IRQS) & 31);
  230. at91_sys_write(pio+PIO_IER, pin);
  231. }
  232. /**
  233. * This function will un-mask a interrupt.
  234. * @param vector the interrupt number
  235. */
  236. void rt_hw_interrupt_umask(int irq)
  237. {
  238. if (irq >= AIC_IRQS)
  239. {
  240. at91_gpio_irq_umask(irq);
  241. }
  242. else
  243. {
  244. /* Enable interrupt on AIC */
  245. at91_sys_write(AT91_AIC_IECR, 1 << irq);
  246. }
  247. }
  248. /**
  249. * This function will install a interrupt service routine to a interrupt.
  250. * @param vector the interrupt number
  251. * @param new_handler the interrupt service routine to be installed
  252. * @param old_handler the old interrupt service routine
  253. */
  254. void rt_hw_interrupt_install(int vector, rt_isr_handler_t new_handler, rt_isr_handler_t *old_handler)
  255. {
  256. if(vector < MAX_HANDLERS)
  257. {
  258. if (old_handler != RT_NULL) *old_handler = isr_table[vector];
  259. if (new_handler != RT_NULL) isr_table[vector] = new_handler;
  260. }
  261. }
  262. /*@}*/
  263. static int at91_aic_set_type(unsigned irq, unsigned type)
  264. {
  265. unsigned int smr, srctype;
  266. switch (type) {
  267. case IRQ_TYPE_LEVEL_HIGH:
  268. srctype = AT91_AIC_SRCTYPE_HIGH;
  269. break;
  270. case IRQ_TYPE_EDGE_RISING:
  271. srctype = AT91_AIC_SRCTYPE_RISING;
  272. break;
  273. case IRQ_TYPE_LEVEL_LOW:
  274. if ((irq == AT91_ID_FIQ) || is_extern_irq(irq)) /* only supported on external interrupts */
  275. srctype = AT91_AIC_SRCTYPE_LOW;
  276. else
  277. return -1;
  278. break;
  279. case IRQ_TYPE_EDGE_FALLING:
  280. if ((irq == AT91_ID_FIQ) || is_extern_irq(irq)) /* only supported on external interrupts */
  281. srctype = AT91_AIC_SRCTYPE_FALLING;
  282. else
  283. return -1;
  284. break;
  285. default:
  286. return -1;
  287. }
  288. smr = at91_sys_read(AT91_AIC_SMR(irq)) & ~AT91_AIC_SRCTYPE;
  289. at91_sys_write(AT91_AIC_SMR(irq), smr | srctype);
  290. return 0;
  291. }