interrupt.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. }
  179. at91_gpio_irq_init();
  180. /* init interrupt nest, and context in thread sp */
  181. rt_interrupt_nest = 0;
  182. rt_interrupt_from_thread = 0;
  183. rt_interrupt_to_thread = 0;
  184. rt_thread_switch_interrupt_flag = 0;
  185. }
  186. static void at91_gpio_irq_mask(int irq)
  187. {
  188. rt_uint32_t pin, pio, bank;
  189. bank = (irq - AIC_IRQS)>>5;
  190. if (bank == 0)
  191. {
  192. pio = AT91_PIOA;
  193. }
  194. else if (bank == 1)
  195. {
  196. pio = AT91_PIOB;
  197. }
  198. else if (bank == 2)
  199. {
  200. pio = AT91_PIOC;
  201. }
  202. else
  203. return;
  204. pin = 1 << ((irq - AIC_IRQS) & 31);
  205. at91_sys_write(pio+PIO_IDR, pin);
  206. }
  207. /**
  208. * This function will mask a interrupt.
  209. * @param vector the interrupt number
  210. */
  211. void rt_hw_interrupt_mask(int irq)
  212. {
  213. if (irq >= AIC_IRQS)
  214. {
  215. at91_gpio_irq_mask(irq);
  216. }
  217. else
  218. {
  219. /* Disable interrupt on AIC */
  220. at91_sys_write(AT91_AIC_IDCR, 1 << irq);
  221. }
  222. }
  223. static void at91_gpio_irq_umask(int irq)
  224. {
  225. rt_uint32_t pin, pio, bank;
  226. bank = (irq - AIC_IRQS)>>5;
  227. if (bank == 0)
  228. {
  229. pio = AT91_PIOA;
  230. }
  231. else if (bank == 1)
  232. {
  233. pio = AT91_PIOB;
  234. }
  235. else if (bank == 2)
  236. {
  237. pio = AT91_PIOC;
  238. }
  239. else
  240. return;
  241. pin = 1 << ((irq - AIC_IRQS) & 31);
  242. at91_sys_write(pio+PIO_IER, pin);
  243. }
  244. /**
  245. * This function will un-mask a interrupt.
  246. * @param vector the interrupt number
  247. */
  248. void rt_hw_interrupt_umask(int irq)
  249. {
  250. if (irq >= AIC_IRQS)
  251. {
  252. at91_gpio_irq_umask(irq);
  253. }
  254. else
  255. {
  256. /* Enable interrupt on AIC */
  257. at91_sys_write(AT91_AIC_IECR, 1 << irq);
  258. }
  259. }
  260. /**
  261. * This function will install a interrupt service routine to a interrupt.
  262. * @param vector the interrupt number
  263. * @param handler the interrupt service routine to be installed
  264. * @param param the interrupt service function parameter
  265. * @param name the interrupt name
  266. * @return old handler
  267. */
  268. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  269. void *param, char *name)
  270. {
  271. rt_isr_handler_t old_handler = RT_NULL;
  272. if(vector < MAX_HANDLERS)
  273. {
  274. old_handler = irq_desc[vector].handler;
  275. if (handler != RT_NULL)
  276. {
  277. rt_snprintf(irq_desc[vector].name, RT_NAME_MAX - 1, "%s", name);
  278. irq_desc[vector].handler = (rt_isr_handler_t)handler;
  279. irq_desc[vector].param = param;
  280. }
  281. }
  282. return old_handler;
  283. }
  284. /*@}*/
  285. static int at91_aic_set_type(unsigned irq, unsigned type)
  286. {
  287. unsigned int smr, srctype;
  288. switch (type) {
  289. case IRQ_TYPE_LEVEL_HIGH:
  290. srctype = AT91_AIC_SRCTYPE_HIGH;
  291. break;
  292. case IRQ_TYPE_EDGE_RISING:
  293. srctype = AT91_AIC_SRCTYPE_RISING;
  294. break;
  295. case IRQ_TYPE_LEVEL_LOW:
  296. if ((irq == AT91_ID_FIQ) || is_extern_irq(irq)) /* only supported on external interrupts */
  297. srctype = AT91_AIC_SRCTYPE_LOW;
  298. else
  299. return -1;
  300. break;
  301. case IRQ_TYPE_EDGE_FALLING:
  302. if ((irq == AT91_ID_FIQ) || is_extern_irq(irq)) /* only supported on external interrupts */
  303. srctype = AT91_AIC_SRCTYPE_FALLING;
  304. else
  305. return -1;
  306. break;
  307. default:
  308. return -1;
  309. }
  310. smr = at91_sys_read(AT91_AIC_SMR(irq)) & ~AT91_AIC_SRCTYPE;
  311. at91_sys_write(AT91_AIC_SMR(irq), smr | srctype);
  312. return 0;
  313. }
  314. #ifdef RT_USING_FINSH
  315. void list_irq(void)
  316. {
  317. int irq;
  318. rt_kprintf("number\tcount\tname\n");
  319. for (irq = 0; irq < MAX_HANDLERS; irq++)
  320. {
  321. if (rt_strncmp(irq_desc[irq].name, "default", sizeof("default")))
  322. {
  323. rt_kprintf("%02ld: %10ld %s\n", irq, irq_desc[irq].name);
  324. }
  325. }
  326. }
  327. #include <finsh.h>
  328. FINSH_FUNCTION_EXPORT(list_irq, list system irq);
  329. #endif