interrupt.c 7.6 KB
Newer Older
L
luohui2320@gmail.com 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * File      : interrupt.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://openlab.rt-thread.com/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2011-01-13     weety      first version
 */

#include <rtthread.h>
#include "at91sam926x.h"

L
luohui2320@gmail.com 已提交
18
#define MAX_HANDLERS	(AIC_IRQS + PIN_IRQS)
L
luohui2320@gmail.com 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

extern rt_uint32_t rt_interrupt_nest;

/* exception and interrupt handler table */
rt_isr_handler_t isr_table[MAX_HANDLERS];
rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
rt_uint32_t rt_thread_switch_interrput_flag;


/* --------------------------------------------------------------------
 *  Interrupt initialization
 * -------------------------------------------------------------------- */

rt_uint32_t at91_extern_irq;

#define is_extern_irq(irq) ((1 << (irq)) & at91_extern_irq)

/*
 * The default interrupt priority levels (0 = lowest, 7 = highest).
 */
static rt_uint32_t at91sam9260_default_irq_priority[MAX_HANDLERS] = {
	7,	/* Advanced Interrupt Controller */
	7,	/* System Peripherals */
	1,	/* Parallel IO Controller A */
	1,	/* Parallel IO Controller B */
	1,	/* Parallel IO Controller C */
	0,	/* Analog-to-Digital Converter */
	5,	/* USART 0 */
	5,	/* USART 1 */
	5,	/* USART 2 */
	0,	/* Multimedia Card Interface */
	2,	/* USB Device Port */
	6,	/* Two-Wire Interface */
	5,	/* Serial Peripheral Interface 0 */
	5,	/* Serial Peripheral Interface 1 */
	5,	/* Serial Synchronous Controller */
	0,
	0,
	0,	/* Timer Counter 0 */
	0,	/* Timer Counter 1 */
	0,	/* Timer Counter 2 */
	2,	/* USB Host port */
	3,	/* Ethernet */
	0,	/* Image Sensor Interface */
	5,	/* USART 3 */
	5,	/* USART 4 */
	5,	/* USART 5 */
	0,	/* Timer Counter 3 */
	0,	/* Timer Counter 4 */
	0,	/* Timer Counter 5 */
	0,	/* Advanced Interrupt Controller */
	0,	/* Advanced Interrupt Controller */
	0,	/* Advanced Interrupt Controller */
};

/**
 * @addtogroup AT91SAM926X
 */
/*@{*/

L
luohui2320@gmail.com 已提交
79 80 81
void rt_hw_interrupt_mask(int irq);
void rt_hw_interrupt_umask(int irq);

L
luohui2320@gmail.com 已提交
82 83 84 85 86 87
rt_isr_handler_t rt_hw_interrupt_handle(rt_uint32_t vector)
{
	rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
	return RT_NULL;
}

L
luohui2320@gmail.com 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
rt_isr_handler_t at91_gpio_irq_handle(rt_uint32_t vector)
{
	rt_uint32_t isr, pio, irq_n;

	if (vector == AT91SAM9260_ID_PIOA) 
	{
		pio = AT91_PIOA;
		irq_n = AIC_IRQS;
	}
	else if (vector == AT91SAM9260_ID_PIOB) 
	{
		pio = AT91_PIOB;
		irq_n = AIC_IRQS + 32;
	}
	else if (vector == AT91SAM9260_ID_PIOC) 
	{
		pio = AT91_PIOC;
		irq_n = AIC_IRQS + 32*2;
	}
	else
L
luohui2320@gmail.com 已提交
108
		return RT_NULL;
L
luohui2320@gmail.com 已提交
109 110 111 112 113 114 115 116 117 118
	isr = at91_sys_read(pio+PIO_ISR) & at91_sys_read(pio+PIO_IMR);
	while (isr) 
	{
		if (isr & 1) 
		{
			isr_table[irq_n](irq_n);
		}
		isr >>= 1;
		irq_n++;
	}
L
luohui2320@gmail.com 已提交
119 120

	return RT_NULL;
L
luohui2320@gmail.com 已提交
121 122
}

L
luohui2320@gmail.com 已提交
123 124 125 126 127 128 129 130 131 132 133
/*
 * Initialize the AIC interrupt controller.
 */
void at91_aic_init(rt_uint32_t *priority)
{
	rt_uint32_t i;

	/*
	 * The IVR is used by macro get_irqnr_and_base to read and verify.
	 * The irq number is NR_AIC_IRQS when a spurious interrupt has occurred.
	 */
L
luohui2320@gmail.com 已提交
134
	for (i = 0; i < AIC_IRQS; i++) {
L
luohui2320@gmail.com 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
		/* Put irq number in Source Vector Register: */
		at91_sys_write(AT91_AIC_SVR(i), i);
		/* Active Low interrupt, with the specified priority */
		at91_sys_write(AT91_AIC_SMR(i), AT91_AIC_SRCTYPE_LOW | priority[i]);
		//AT91_AIC_SRCTYPE_FALLING

		/* Perform 8 End Of Interrupt Command to make sure AIC will not Lock out nIRQ */
		if (i < 8)
			at91_sys_write(AT91_AIC_EOICR, 0);
	}

	/*
	 * Spurious Interrupt ID in Spurious Vector Register is NR_AIC_IRQS
	 * When there is no current interrupt, the IRQ Vector Register reads the value stored in AIC_SPU
	 */
L
luohui2320@gmail.com 已提交
150
	at91_sys_write(AT91_AIC_SPU, AIC_IRQS);
L
luohui2320@gmail.com 已提交
151 152 153 154 155 156 157 158 159 160

	/* No debugging in AIC: Debug (Protect) Control Register */
	at91_sys_write(AT91_AIC_DCR, 0);

	/* Disable and clear all interrupts initially */
	at91_sys_write(AT91_AIC_IDCR, 0xFFFFFFFF);
	at91_sys_write(AT91_AIC_ICCR, 0xFFFFFFFF);
}


L
luohui2320@gmail.com 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
static void at91_gpio_irq_init()
{
	at91_sys_write(AT91_PIOA+PIO_IDR, 0xffffffff);
	at91_sys_write(AT91_PIOB+PIO_IDR, 0xffffffff);
	at91_sys_write(AT91_PIOC+PIO_IDR, 0xffffffff);

	isr_table[AT91SAM9260_ID_PIOA] = (rt_isr_handler_t)at91_gpio_irq_handle;
	isr_table[AT91SAM9260_ID_PIOB] = (rt_isr_handler_t)at91_gpio_irq_handle;
	isr_table[AT91SAM9260_ID_PIOC] = (rt_isr_handler_t)at91_gpio_irq_handle;

	rt_hw_interrupt_umask(AT91SAM9260_ID_PIOA);
	rt_hw_interrupt_umask(AT91SAM9260_ID_PIOB);
	rt_hw_interrupt_umask(AT91SAM9260_ID_PIOC);
}


L
luohui2320@gmail.com 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
/**
 * This function will initialize hardware interrupt
 */
void rt_hw_interrupt_init(void)
{
	rt_int32_t i;
	register rt_uint32_t idx;
	rt_uint32_t *priority = at91sam9260_default_irq_priority;
	
	at91_extern_irq = (1 << AT91SAM9260_ID_IRQ0) | (1 << AT91SAM9260_ID_IRQ1)
			| (1 << AT91SAM9260_ID_IRQ2);

	/* Initialize the AIC interrupt controller */
	at91_aic_init(priority);

	/* init exceptions table */
	for(idx=0; idx < MAX_HANDLERS; idx++)
	{
		isr_table[idx] = (rt_isr_handler_t)rt_hw_interrupt_handle;
	}

L
luohui2320@gmail.com 已提交
198 199
	at91_gpio_irq_init();

L
luohui2320@gmail.com 已提交
200 201 202 203 204 205 206
	/* init interrupt nest, and context in thread sp */
	rt_interrupt_nest = 0;
	rt_interrupt_from_thread = 0;
	rt_interrupt_to_thread = 0;
	rt_thread_switch_interrput_flag = 0;
}

L
luohui2320@gmail.com 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
static void at91_gpio_irq_mask(int irq)
{
	rt_uint32_t pin, pio, bank;

	bank = (irq - AIC_IRQS)>>5;

	if (bank == 0) 
	{
		pio = AT91_PIOA;
	}
	else if (bank == 1) 
	{
		pio = AT91_PIOB;
	}
	else if (bank == 2) 
	{
		pio = AT91_PIOC;
	}
	else
		return;
	pin = 1 << ((irq - AIC_IRQS) & 31);
	at91_sys_write(pio+PIO_IDR, pin);
}

L
luohui2320@gmail.com 已提交
231 232 233 234 235 236
/**
 * This function will mask a interrupt.
 * @param vector the interrupt number
 */
void rt_hw_interrupt_mask(int irq)
{
L
luohui2320@gmail.com 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	if (irq >= AIC_IRQS) 
	{
		at91_gpio_irq_mask(irq);
	}
	else
	{
		/* Disable interrupt on AIC */
		at91_sys_write(AT91_AIC_IDCR, 1 << irq);
	}
}

static void at91_gpio_irq_umask(int irq)
{
	rt_uint32_t pin, pio, bank;

	bank = (irq - AIC_IRQS)>>5;

	if (bank == 0) 
	{
		pio = AT91_PIOA;
	}
	else if (bank == 1) 
	{
		pio = AT91_PIOB;
	}
	else if (bank == 2) 
	{
		pio = AT91_PIOC;
	}
	else
		return;
	pin = 1 << ((irq - AIC_IRQS) & 31);
	at91_sys_write(pio+PIO_IER, pin);
L
luohui2320@gmail.com 已提交
270 271 272 273 274 275 276 277
}

/**
 * This function will un-mask a interrupt.
 * @param vector the interrupt number
 */
void rt_hw_interrupt_umask(int irq)
{
L
luohui2320@gmail.com 已提交
278 279 280 281 282 283 284 285 286
	if (irq >= AIC_IRQS) 
	{
		at91_gpio_irq_umask(irq);
	}
	else
	{
		/* Enable interrupt on AIC */
		at91_sys_write(AT91_AIC_IECR, 1 << irq);
	}
L
luohui2320@gmail.com 已提交
287 288 289 290 291 292 293 294 295 296 297 298
}

/**
 * This function will install a interrupt service routine to a interrupt.
 * @param vector the interrupt number
 * @param new_handler the interrupt service routine to be installed
 * @param old_handler the old interrupt service routine
 */
void rt_hw_interrupt_install(int vector, rt_isr_handler_t new_handler, rt_isr_handler_t *old_handler)
{
	if(vector < MAX_HANDLERS)
	{
299
		if (old_handler != RT_NULL) *old_handler = isr_table[vector];
L
luohui2320@gmail.com 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
		if (new_handler != RT_NULL) isr_table[vector] = new_handler;
	}
}

/*@}*/


static int at91_aic_set_type(unsigned irq, unsigned type)
{
	unsigned int smr, srctype;

	switch (type) {
	case IRQ_TYPE_LEVEL_HIGH:
		srctype = AT91_AIC_SRCTYPE_HIGH;
		break;
	case IRQ_TYPE_EDGE_RISING:
		srctype = AT91_AIC_SRCTYPE_RISING;
		break;
	case IRQ_TYPE_LEVEL_LOW:
		if ((irq == AT91_ID_FIQ) || is_extern_irq(irq))		/* only supported on external interrupts */
			srctype = AT91_AIC_SRCTYPE_LOW;
		else
			return -1;
		break;
	case IRQ_TYPE_EDGE_FALLING:
		if ((irq == AT91_ID_FIQ) || is_extern_irq(irq))		/* only supported on external interrupts */
			srctype = AT91_AIC_SRCTYPE_FALLING;
		else
			return -1;
		break;
	default:
		return -1;
	}

	smr = at91_sys_read(AT91_AIC_SMR(irq)) & ~AT91_AIC_SRCTYPE;
	at91_sys_write(AT91_AIC_SMR(irq), smr | srctype);
	return 0;
}