interrupt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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);
  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. rt_int32_t i;
  177. register rt_uint32_t idx;
  178. rt_uint32_t *priority = at91sam9260_default_irq_priority;
  179. at91_extern_irq = (1 << AT91SAM9260_ID_IRQ0) | (1 << AT91SAM9260_ID_IRQ1)
  180. | (1 << AT91SAM9260_ID_IRQ2);
  181. /* Initialize the AIC interrupt controller */
  182. at91_aic_init(priority);
  183. /* init exceptions table */
  184. for(idx=0; idx < MAX_HANDLERS; idx++)
  185. {
  186. rt_snprintf(irq_desc[idx].name, RT_NAME_MAX - 1, "default");
  187. irq_desc[idx].handler = (rt_isr_handler_t)rt_hw_interrupt_handle;
  188. irq_desc[idx].param = RT_NULL;
  189. irq_desc[idx].counter = 0;
  190. }
  191. at91_gpio_irq_init();
  192. /* init interrupt nest, and context in thread sp */
  193. rt_interrupt_nest = 0;
  194. rt_interrupt_from_thread = 0;
  195. rt_interrupt_to_thread = 0;
  196. rt_thread_switch_interrupt_flag = 0;
  197. }
  198. static void at91_gpio_irq_mask(int irq)
  199. {
  200. rt_uint32_t pin, pio, bank;
  201. bank = (irq - AIC_IRQS)>>5;
  202. if (bank == 0)
  203. {
  204. pio = AT91_PIOA;
  205. }
  206. else if (bank == 1)
  207. {
  208. pio = AT91_PIOB;
  209. }
  210. else if (bank == 2)
  211. {
  212. pio = AT91_PIOC;
  213. }
  214. else
  215. return;
  216. pin = 1 << ((irq - AIC_IRQS) & 31);
  217. at91_sys_write(pio+PIO_IDR, pin);
  218. }
  219. /**
  220. * This function will mask a interrupt.
  221. * @param vector the interrupt number
  222. */
  223. void rt_hw_interrupt_mask(int irq)
  224. {
  225. if (irq >= AIC_IRQS)
  226. {
  227. at91_gpio_irq_mask(irq);
  228. }
  229. else
  230. {
  231. /* Disable interrupt on AIC */
  232. at91_sys_write(AT91_AIC_IDCR, 1 << irq);
  233. }
  234. }
  235. static void at91_gpio_irq_umask(int irq)
  236. {
  237. rt_uint32_t pin, pio, bank;
  238. bank = (irq - AIC_IRQS)>>5;
  239. if (bank == 0)
  240. {
  241. pio = AT91_PIOA;
  242. }
  243. else if (bank == 1)
  244. {
  245. pio = AT91_PIOB;
  246. }
  247. else if (bank == 2)
  248. {
  249. pio = AT91_PIOC;
  250. }
  251. else
  252. return;
  253. pin = 1 << ((irq - AIC_IRQS) & 31);
  254. at91_sys_write(pio+PIO_IER, pin);
  255. }
  256. /**
  257. * This function will un-mask a interrupt.
  258. * @param vector the interrupt number
  259. */
  260. void rt_hw_interrupt_umask(int irq)
  261. {
  262. if (irq >= AIC_IRQS)
  263. {
  264. at91_gpio_irq_umask(irq);
  265. }
  266. else
  267. {
  268. /* Enable interrupt on AIC */
  269. at91_sys_write(AT91_AIC_IECR, 1 << irq);
  270. }
  271. }
  272. /**
  273. * This function will install a interrupt service routine to a interrupt.
  274. * @param vector the interrupt number
  275. * @param handler the interrupt service routine to be installed
  276. * @param param the interrupt service function parameter
  277. * @param name the interrupt name
  278. * @return old handler
  279. */
  280. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  281. void *param, char *name)
  282. {
  283. rt_isr_handler_t old_handler = RT_NULL;
  284. if(vector < MAX_HANDLERS)
  285. {
  286. old_handler = irq_desc[vector].handler;
  287. if (handler != RT_NULL)
  288. {
  289. rt_snprintf(irq_desc[vector].name, RT_NAME_MAX - 1, "%s", name);
  290. irq_desc[vector].handler = (rt_isr_handler_t)handler;
  291. irq_desc[vector].param = param;
  292. irq_desc[vector].counter = 0;
  293. }
  294. }
  295. return old_handler;
  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. if ((irq == AT91_ID_FIQ) || is_extern_irq(irq)) /* only supported on external interrupts */
  310. srctype = AT91_AIC_SRCTYPE_LOW;
  311. else
  312. return -1;
  313. break;
  314. case IRQ_TYPE_EDGE_FALLING:
  315. if ((irq == AT91_ID_FIQ) || is_extern_irq(irq)) /* only supported on external interrupts */
  316. srctype = AT91_AIC_SRCTYPE_FALLING;
  317. else
  318. return -1;
  319. break;
  320. default:
  321. return -1;
  322. }
  323. smr = at91_sys_read(AT91_AIC_SMR(irq)) & ~AT91_AIC_SRCTYPE;
  324. at91_sys_write(AT91_AIC_SMR(irq), smr | srctype);
  325. return 0;
  326. }
  327. rt_uint32_t rt_hw_interrupt_get_active(rt_uint32_t fiq_irq, rt_uint32_t* id)
  328. {
  329. rt_uint32_t irqstat;
  330. if (fiq_irq == INT_FIQ)
  331. {
  332. *id = 0;
  333. }
  334. else //IRQ
  335. {
  336. /* get irq number */
  337. *id = (rt_uint32_t)at91_sys_read(AT91_AIC_IVR);
  338. /* clear pending register */
  339. irqstat = (rt_uint32_t)at91_sys_read(AT91_AIC_ISR);
  340. }
  341. return irqstat;
  342. }
  343. void rt_hw_interrupt_ack(rt_uint32_t fiq_irq)
  344. {
  345. if (fiq_irq == INT_FIQ)
  346. {
  347. /* new FIQ generation */
  348. }
  349. else
  350. {
  351. // EIOCR must be write any value after interrupt,
  352. // or else can't response next interrupt
  353. /* new IRQ generation */
  354. at91_sys_write(AT91_AIC_EOICR, 0x55555555);
  355. }
  356. }
  357. #ifdef RT_USING_FINSH
  358. void list_irq(void)
  359. {
  360. int irq;
  361. rt_kprintf("number\tcount\tname\n");
  362. for (irq = 0; irq < MAX_HANDLERS; irq++)
  363. {
  364. if (rt_strncmp(irq_desc[irq].name, "default", sizeof("default")))
  365. {
  366. rt_kprintf("%02ld: %10ld %s\n", irq, irq_desc[irq].counter, irq_desc[irq].name);
  367. }
  368. }
  369. }
  370. #include <finsh.h>
  371. FINSH_FUNCTION_EXPORT(list_irq, list system irq);
  372. #endif