interrupt.c 9.5 KB

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