interrupt.c 10 KB

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