interrupt.c 8.4 KB

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