interrupt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. #include "interrupt.h"
  27. #define MAX_HANDLERS (AIC_IRQS + PIN_IRQS)
  28. extern rt_uint32_t rt_interrupt_nest;
  29. /* exception and interrupt handler table */
  30. struct rt_irq_desc irq_desc[MAX_HANDLERS];
  31. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  32. rt_uint32_t rt_thread_switch_interrupt_flag;
  33. /* --------------------------------------------------------------------
  34. * Interrupt initialization
  35. * -------------------------------------------------------------------- */
  36. rt_uint32_t at91_extern_irq;
  37. #define is_extern_irq(irq) ((1 << (irq)) & at91_extern_irq)
  38. /*
  39. * The default interrupt priority levels (0 = lowest, 7 = highest).
  40. */
  41. static rt_uint32_t at91sam9260_default_irq_priority[MAX_HANDLERS] = {
  42. 7, /* Advanced Interrupt Controller */
  43. 7, /* System Peripherals */
  44. 1, /* Parallel IO Controller A */
  45. 1, /* Parallel IO Controller B */
  46. 1, /* Parallel IO Controller C */
  47. 0, /* Analog-to-Digital Converter */
  48. 5, /* USART 0 */
  49. 5, /* USART 1 */
  50. 5, /* USART 2 */
  51. 0, /* Multimedia Card Interface */
  52. 2, /* USB Device Port */
  53. 6, /* Two-Wire Interface */
  54. 5, /* Serial Peripheral Interface 0 */
  55. 5, /* Serial Peripheral Interface 1 */
  56. 5, /* Serial Synchronous Controller */
  57. 0,
  58. 0,
  59. 0, /* Timer Counter 0 */
  60. 0, /* Timer Counter 1 */
  61. 0, /* Timer Counter 2 */
  62. 2, /* USB Host port */
  63. 3, /* Ethernet */
  64. 0, /* Image Sensor Interface */
  65. 5, /* USART 3 */
  66. 5, /* USART 4 */
  67. 5, /* USART 5 */
  68. 0, /* Timer Counter 3 */
  69. 0, /* Timer Counter 4 */
  70. 0, /* Timer Counter 5 */
  71. 0, /* Advanced Interrupt Controller */
  72. 0, /* Advanced Interrupt Controller */
  73. 0, /* Advanced Interrupt Controller */
  74. };
  75. /**
  76. * @addtogroup AT91SAM926X
  77. */
  78. /*@{*/
  79. void rt_hw_interrupt_mask(int irq);
  80. void rt_hw_interrupt_umask(int irq);
  81. rt_isr_handler_t rt_hw_interrupt_handle(rt_uint32_t vector, void *param)
  82. {
  83. rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
  84. return RT_NULL;
  85. }
  86. rt_isr_handler_t at91_gpio_irq_handle(rt_uint32_t vector, void *param)
  87. {
  88. rt_uint32_t isr, pio, irq_n;
  89. void *parameter;
  90. if (vector == AT91SAM9260_ID_PIOA)
  91. {
  92. pio = AT91_PIOA;
  93. irq_n = AIC_IRQS;
  94. }
  95. else if (vector == AT91SAM9260_ID_PIOB)
  96. {
  97. pio = AT91_PIOB;
  98. irq_n = AIC_IRQS + 32;
  99. }
  100. else if (vector == AT91SAM9260_ID_PIOC)
  101. {
  102. pio = AT91_PIOC;
  103. irq_n = AIC_IRQS + 32*2;
  104. }
  105. else
  106. return RT_NULL;
  107. isr = at91_sys_read(pio+PIO_ISR) & at91_sys_read(pio+PIO_IMR);
  108. while (isr)
  109. {
  110. if (isr & 1)
  111. {
  112. parameter = irq_desc[irq_n].param;
  113. irq_desc[irq_n].handler(irq_n, parameter);
  114. }
  115. isr >>= 1;
  116. irq_n++;
  117. }
  118. return RT_NULL;
  119. }
  120. /*
  121. * Initialize the AIC interrupt controller.
  122. */
  123. void at91_aic_init(rt_uint32_t *priority)
  124. {
  125. rt_uint32_t i;
  126. /*
  127. * The IVR is used by macro get_irqnr_and_base to read and verify.
  128. * The irq number is NR_AIC_IRQS when a spurious interrupt has occurred.
  129. */
  130. for (i = 0; i < AIC_IRQS; i++) {
  131. /* Put irq number in Source Vector Register: */
  132. at91_sys_write(AT91_AIC_SVR(i), i); // no-used
  133. /* Active Low interrupt, with the specified priority */
  134. at91_sys_write(AT91_AIC_SMR(i), AT91_AIC_SRCTYPE_LOW | priority[i]);
  135. //AT91_AIC_SRCTYPE_FALLING
  136. /* Perform 8 End Of Interrupt Command to make sure AIC will not Lock out nIRQ */
  137. if (i < 8)
  138. at91_sys_write(AT91_AIC_EOICR, 0);
  139. }
  140. /*
  141. * Spurious Interrupt ID in Spurious Vector Register is NR_AIC_IRQS
  142. * When there is no current interrupt, the IRQ Vector Register reads the value stored in AIC_SPU
  143. */
  144. at91_sys_write(AT91_AIC_SPU, AIC_IRQS);
  145. /* No debugging in AIC: Debug (Protect) Control Register */
  146. at91_sys_write(AT91_AIC_DCR, 0);
  147. /* Disable and clear all interrupts initially */
  148. at91_sys_write(AT91_AIC_IDCR, 0xFFFFFFFF);
  149. at91_sys_write(AT91_AIC_ICCR, 0xFFFFFFFF);
  150. }
  151. static void at91_gpio_irq_init()
  152. {
  153. int i, idx;
  154. char *name[] = {"PIOA", "PIOB", "PIOC"};
  155. at91_sys_write(AT91_PIOA+PIO_IDR, 0xffffffff);
  156. at91_sys_write(AT91_PIOB+PIO_IDR, 0xffffffff);
  157. at91_sys_write(AT91_PIOC+PIO_IDR, 0xffffffff);
  158. idx = AT91SAM9260_ID_PIOA;
  159. for (i = 0; i < 3; i++)
  160. {
  161. rt_snprintf(irq_desc[idx].name, RT_NAME_MAX - 1, name[i]);
  162. irq_desc[idx].handler = (rt_isr_handler_t)at91_gpio_irq_handle;
  163. irq_desc[idx].param = RT_NULL;
  164. irq_desc[idx].counter = 0;
  165. idx++;
  166. }
  167. rt_hw_interrupt_umask(AT91SAM9260_ID_PIOA);
  168. rt_hw_interrupt_umask(AT91SAM9260_ID_PIOB);
  169. rt_hw_interrupt_umask(AT91SAM9260_ID_PIOC);
  170. }
  171. /**
  172. * This function will initialize hardware interrupt
  173. */
  174. void rt_hw_interrupt_init(void)
  175. {
  176. register rt_uint32_t idx;
  177. rt_uint32_t *priority = at91sam9260_default_irq_priority;
  178. at91_extern_irq = (1UL << AT91SAM9260_ID_IRQ0) | (1UL << AT91SAM9260_ID_IRQ1)
  179. | (1UL << 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. /*
  298. static int at91_aic_set_type(unsigned irq, unsigned type)
  299. {
  300. unsigned int smr, srctype;
  301. switch (type) {
  302. case IRQ_TYPE_LEVEL_HIGH:
  303. srctype = AT91_AIC_SRCTYPE_HIGH;
  304. break;
  305. case IRQ_TYPE_EDGE_RISING:
  306. srctype = AT91_AIC_SRCTYPE_RISING;
  307. break;
  308. case IRQ_TYPE_LEVEL_LOW:
  309. // only supported on external interrupts
  310. if ((irq == AT91_ID_FIQ) || is_extern_irq(irq))
  311. srctype = AT91_AIC_SRCTYPE_LOW;
  312. else
  313. return -1;
  314. break;
  315. case IRQ_TYPE_EDGE_FALLING:
  316. // only supported on external interrupts
  317. if ((irq == AT91_ID_FIQ) || is_extern_irq(irq))
  318. srctype = AT91_AIC_SRCTYPE_FALLING;
  319. else
  320. return -1;
  321. break;
  322. default:
  323. return -1;
  324. }
  325. smr = at91_sys_read(AT91_AIC_SMR(irq)) & ~AT91_AIC_SRCTYPE;
  326. at91_sys_write(AT91_AIC_SMR(irq), smr | srctype);
  327. return 0;
  328. }
  329. */
  330. rt_uint32_t rt_hw_interrupt_get_active(rt_uint32_t fiq_irq)
  331. {
  332. //volatile rt_uint32_t irqstat;
  333. rt_uint32_t id;
  334. if (fiq_irq == INT_FIQ)
  335. return 0;
  336. //IRQ
  337. /* AIC need this dummy read */
  338. at91_sys_read(AT91_AIC_IVR);
  339. /* clear pending register */
  340. id = at91_sys_read(AT91_AIC_ISR);
  341. return id;
  342. }
  343. void rt_hw_interrupt_ack(rt_uint32_t fiq_irq, rt_uint32_t id)
  344. {
  345. /* new FIQ generation */
  346. if (fiq_irq == INT_FIQ)
  347. return;
  348. /* new IRQ generation */
  349. // EIOCR must be write any value after interrupt,
  350. // or else can't response next interrupt
  351. at91_sys_write(AT91_AIC_EOICR, 0x0);
  352. }
  353. #ifdef RT_USING_FINSH
  354. #ifdef RT_USING_INTERRUPT_INFO
  355. void list_irq(void)
  356. {
  357. int irq;
  358. rt_kprintf("number\tcount\tname\n");
  359. for (irq = 0; irq < MAX_HANDLERS; irq++)
  360. {
  361. if (rt_strncmp(irq_desc[irq].name, "default", sizeof("default")))
  362. {
  363. rt_kprintf("%02ld: %10ld %s\n", irq, irq_desc[irq].counter, irq_desc[irq].name);
  364. }
  365. }
  366. }
  367. #include <finsh.h>
  368. FINSH_FUNCTION_EXPORT(list_irq, list system irq);
  369. #ifdef FINSH_USING_MSH
  370. int cmd_list_irq(int argc, char** argv)
  371. {
  372. list_irq();
  373. return 0;
  374. }
  375. FINSH_FUNCTION_EXPORT_ALIAS(cmd_list_irq, __cmd_list_irq, list system irq.);
  376. #endif
  377. #endif
  378. #endif