interrupt.c 12.0 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9 10 11 12 13 14
 *
 * Change Logs:
 * Date           Author       Notes
 * 2011-01-13     weety      first version
 */

#include <rthw.h>
#include "at91sam9g45.h"
#include "interrupt.h"

15
#define AIC_IRQS    32
16 17 18 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 79 80 81 82 83 84 85 86 87 88 89
#define MAX_HANDLERS    (AIC_IRQS + PIN_IRQS)

extern rt_uint32_t rt_interrupt_nest;

/* exception and interrupt handler table */
struct rt_irq_desc irq_desc[MAX_HANDLERS];

rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
rt_uint32_t rt_thread_switch_interrupt_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 at91sam9g45_default_irq_priority[MAX_HANDLERS] = {
    7,  /* Advanced Interrupt Controller - FIQ    */
    7,  /* System Controller Interrupt            */
    1,  /* Parallel I/O Controller A,             */
    1,  /* Parallel I/O Controller B              */
    1,  /* Parallel I/O Controller C              */
    0,  /* Parallel I/O Controller D/E            */
    5,  /* True Random Number Generator           */
    5,  /* USART 0                                */
    5,  /* USART 1                                */
    0,  /* USART 2                                */
    2,  /* USART 3                                */
    6,  /* High Speed Multimedia Card Interface 0 */
    5,  /* Two-Wire Interface 0                   */
    5,  /* Two-Wire Interface 1                   */
    5,  /* Serial Peripheral Interface            */
    0,  /* Serial Peripheral Interface            */
    0,  /* Synchronous Serial Controller 0        */
    0,  /* Synchronous Serial Controller 1        */
    0,  /* Timer Counter 0,1,2,3,4,5              */
    0,  /* Pulse Width Modulation Controller      */
    2,  /* Touch Screen ADC Controller            */
    3,  /* DMA Controller                         */
    0,  /* USB Host High Speed                    */
    5,  /* LCD Controller                         */
    5,  /* AC97 Controller                        */
    5,  /* Ethernet MAC                           */
    0,  /* Image Sensor Interface                 */
    0,  /* USB Device High Speed                  */
    0,  /* N/A                                    */
    0,  /* High Speed Multimedia Card Interface 1 */
    0,  /* Reserved                               */
    0,  /* Advanced Interrupt Controller - IRQ    */
};

/**
 * @addtogroup AT91SAM9G45
 */
/*@{*/

void rt_hw_interrupt_mask(int irq);
void rt_hw_interrupt_umask(int irq);

rt_isr_handler_t rt_hw_interrupt_handle(rt_uint32_t vector, void *param)
{
    rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
    return RT_NULL;
}

rt_isr_handler_t at91_gpio_irq_handle(rt_uint32_t bank, void *param)
{
    rt_uint32_t isr, irq_n;
90
    AT91PS_PIO pio;
91 92 93 94
    void *parameter;

    switch (bank)
    {
95 96 97 98 99 100
    case 0: pio = AT91C_BASE_PIOA; break;
    case 1: pio = AT91C_BASE_PIOB; break;
    case 2: pio = AT91C_BASE_PIOC; break;
    case 3: pio = AT91C_BASE_PIOD; break;
    case 4: pio = AT91C_BASE_PIOE; break;
    default: return RT_NULL;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    }
    irq_n = AIC_IRQS + 32*bank;
    isr = readl(pio->PIO_ISR);
    isr &= readl(pio->PIO_IMR);
    while (isr)
    {
        if (isr & 1)
        {
            parameter = irq_desc[irq_n].param;
            irq_desc[irq_n].handler(irq_n, parameter);
        }
        isr >>= 1;
        irq_n++;
    }

    return RT_NULL;
}

unsigned int SpuriousCount = 0;
static void DefaultSpuriousHandler( void )
{
122 123 124
    SpuriousCount++;
    rt_kprintf("Spurious interrupt %d occured!!!\n", SpuriousCount);
    return ;
125 126 127 128
}

static void DefaultFiqHandler(void)
{
129 130
    rt_kprintf("Unhandled FIQ occured!!!\n");
    while (1);
131 132 133 134
}

static void DefaultIrqHandler(void)
{
135 136
    rt_kprintf("Unhandled IRQ %d occured!!!\n", AT91C_BASE_AIC->AIC_ISR);
    while (1);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
}

/*
 * 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.
     */
    AT91C_BASE_AIC->AIC_SVR[0] = (rt_uint32_t)DefaultFiqHandler;
    for (i = 1; i < AIC_IRQS; i++) {
        /* Put irq number in Source Vector Register: */
        AT91C_BASE_AIC->AIC_SVR[i] = (rt_uint32_t)DefaultIrqHandler; // no-used
        /* Active Low interrupt, with the specified priority */
        AT91C_BASE_AIC->AIC_SMR[i] = AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE | priority[i];
        //AT91C_AIC_SRCTYPE_FALLING
    }

    /*
     * 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
     */
    AT91C_BASE_AIC->AIC_SPU = (rt_uint32_t)DefaultSpuriousHandler;

    /* Perform 8 End Of Interrupt Command to make sure AIC will not Lock out nIRQ */
    for (i = 0; i < 8; i++)
        AT91C_BASE_AIC->AIC_EOICR = 0;

    /* No debugging in AIC: Debug (Protect) Control Register */
    AT91C_BASE_AIC->AIC_DCR = 0;

    /* Disable and clear all interrupts initially */
    AT91C_BASE_AIC->AIC_IDCR = 0xFFFFFFFF;
    AT91C_BASE_AIC->AIC_ICCR = 0xFFFFFFFF;
}


static void at91_gpio_irq_init()
{
    int i, idx;
    char *name[] = {"PIOA", "PIOB", "PIOC", "PIODE"};
    rt_uint32_t aic_pids[] = { AT91C_ID_PIOA, AT91C_ID_PIOB, AT91C_ID_PIOC, AT91C_ID_PIOD_E };

    AT91C_BASE_PIOA->PIO_IDR = 0xffffffff;
    AT91C_BASE_PIOB->PIO_IDR = 0xffffffff;
    AT91C_BASE_PIOC->PIO_IDR = 0xffffffff;
    AT91C_BASE_PIOD->PIO_IDR = 0xffffffff;
    AT91C_BASE_PIOE->PIO_IDR = 0xffffffff;

    for (i = 0; i < 4; i++)
    {
        idx = aic_pids[i];
        irq_desc[idx].handler = (rt_isr_handler_t)at91_gpio_irq_handle;
        irq_desc[idx].param = RT_NULL;
#ifdef RT_USING_INTERRUPT_INFO
        rt_snprintf(irq_desc[idx].name, RT_NAME_MAX - 1, name[i]);
        irq_desc[idx].counter = 0;
#endif

        rt_hw_interrupt_umask(idx);
    }
}


/**
 * This function will initialize hardware interrupt
 */
void rt_hw_interrupt_init(void)
{
    register rt_uint32_t idx;
    rt_uint32_t *priority = at91sam9g45_default_irq_priority;

    at91_extern_irq = (1UL << AT91C_ID_IRQ0);

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

    /* init exceptions table */
    for(idx=0; idx < MAX_HANDLERS; idx++)
    {
        irq_desc[idx].handler = (rt_isr_handler_t)rt_hw_interrupt_handle;
        irq_desc[idx].param = RT_NULL;
#ifdef RT_USING_INTERRUPT_INFO
        rt_snprintf(irq_desc[idx].name, RT_NAME_MAX - 1, "default");
        irq_desc[idx].counter = 0;
#endif
    }

    at91_gpio_irq_init();

    /* 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_interrupt_flag = 0;
}

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

    bank = (irq - AIC_IRQS)>>5;

    switch (bank)
    {
    case 0: pio = AT91C_BASE_PIOA; break;
    case 1: pio = AT91C_BASE_PIOB; break;
    case 2: pio = AT91C_BASE_PIOC; break;
    case 3: pio = AT91C_BASE_PIOD; break;
    case 4: pio = AT91C_BASE_PIOE; break;
    default: return;
    }
    pin = 1 << ((irq - AIC_IRQS) & 31);
    pio->PIO_IDR = pin;
}

/**
 * This function will mask a interrupt.
 * @param irq   the interrupt number
 */
void rt_hw_interrupt_mask(int irq)
{
    if (irq >= AIC_IRQS)
    {
        at91_gpio_irq_mask(irq);
    }
    else
    {
        /* Disable interrupt on AIC */
        AT91C_BASE_AIC->AIC_IDCR = 1 << irq;
    }
}

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

    bank = (irq - AIC_IRQS)>>5;

    switch (bank)
    {
    case 0: pio = AT91C_BASE_PIOA; break;
    case 1: pio = AT91C_BASE_PIOB; break;
    case 2: pio = AT91C_BASE_PIOC; break;
    case 3: pio = AT91C_BASE_PIOD; break;
    case 4: pio = AT91C_BASE_PIOE; break;
    default: return;
    }
    pin = 1 << ((irq - AIC_IRQS) & 31);
    pio->PIO_IER = pin;
}

/**
 * This function will un-mask a interrupt.
 * @param vector the interrupt number
 */
void rt_hw_interrupt_umask(int irq)
{
    if (irq >= AIC_IRQS)
    {
        at91_gpio_irq_umask(irq);
    }
    else
    {
        /* Enable interrupt on AIC */
        AT91C_BASE_AIC->AIC_IECR = 1 << irq;
    }
}

/**
 * This function will install a interrupt service routine to a interrupt.
 * @param vector the interrupt number
 * @param handler the interrupt service routine to be installed
 * @param param the interrupt service function parameter
 * @param name the interrupt name
 * @return old handler
 */
rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
321
                                    void *param, const char *name)
322 323 324 325 326 327 328 329 330 331 332 333
{
    rt_isr_handler_t old_handler = RT_NULL;

    if (vector < MAX_HANDLERS)
    {
        old_handler = irq_desc[vector].handler;
        if (handler != RT_NULL)
        {
            irq_desc[vector].handler = (rt_isr_handler_t)handler;
            irq_desc[vector].param = param;
#ifdef RT_USING_INTERRUPT_INFO
            rt_snprintf(irq_desc[vector].name, RT_NAME_MAX - 1, "%s", name);
334
            irq_desc[vector].counter = 0;
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
#endif
        }
    }

    return old_handler;
}

/*@}*/

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

    switch (type) {
    case AT91C_AIC_SRCTYPE_EXT_HIGH_LEVEL:
        srctype = AT91C_AIC_SRCTYPE_HIGH;
        break;
    case AT91C_AIC_SRCTYPE_EXT_POSITIVE_EDGE:
        srctype = AT91C_AIC_SRCTYPE_RISING;
        break;
    case AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE:
        // only supported on external interrupts
        if ((irq == AT91C_ID_FIQ) || is_extern_irq(irq))
            srctype = AT91C_AIC_SRCTYPE_LOW;
        else
            return -1;
        break;
    case AT91C_AIC_SRCTYPE_INT_EDGE_TRIGGERED:
        // only supported on external interrupts
        if ((irq == AT91C_ID_FIQ) || is_extern_irq(irq))
            srctype = AT91C_AIC_SRCTYPE_FALLING;
        else
            return -1;
        break;
    default:
        return -1;
    }

    smr = readl(AT91C_AIC_SMR(irq)) & ~AT91C_AIC_SRCTYPE;
    AT91C_BASE_AIC->AIC_SMR[irq] = smr | srctype;
    return 0;
}
*/
rt_uint32_t rt_hw_interrupt_get_active(rt_uint32_t fiq_irq)
{

    //volatile rt_uint32_t irqstat;
    rt_uint32_t id;
    if (fiq_irq == INT_FIQ)
        return 0;

    //IRQ
    /* AIC need this dummy read */
    readl(AT91C_AIC_IVR);
    /* clear pending register */
    id = readl(AT91C_AIC_ISR);

    return id;
}

void rt_hw_interrupt_ack(rt_uint32_t fiq_irq, rt_uint32_t id)
{
    /* new FIQ generation */
    if (fiq_irq == INT_FIQ)
        return;

    /* new IRQ generation */
    // EIOCR must be write any value after interrupt,
    // or else can't response next interrupt
    AT91C_BASE_AIC->AIC_EOICR = 0x0;
}

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
void rt_interrupt_dispatch(rt_uint32_t fiq_irq)
{
    rt_isr_handler_t isr_func;
    rt_uint32_t irq;
    void *param;

    /* get irq number */
    irq = rt_hw_interrupt_get_active(fiq_irq);

    /* get interrupt service routine */
    isr_func = irq_desc[irq].handler;
    param = irq_desc[irq].param;

    /* turn to interrupt service routine */
    isr_func(irq, param);

    rt_hw_interrupt_ack(fiq_irq, irq);
#ifdef RT_USING_INTERRUPT_INFO
    irq_desc[irq].counter ++;
#endif
}


431 432 433 434
#ifdef RT_USING_FINSH
#ifdef RT_USING_INTERRUPT_INFO
void list_irq(void)
{
435 436 437 438 439 440 441 442 443 444
    int irq;

    rt_kprintf("number\tcount\tname\n");
    for (irq = 0; irq < MAX_HANDLERS; irq++)
    {
        if (rt_strncmp(irq_desc[irq].name, "default", sizeof("default")))
        {
            rt_kprintf("%02ld: %10ld  %s\n", irq, irq_desc[irq].counter, irq_desc[irq].name);
        }
    }
445 446 447 448 449 450 451 452 453 454
}

#include <finsh.h>

#ifdef FINSH_USING_MSH
int cmd_list_irq(int argc, char** argv)
{
    list_irq();
    return 0;
}
455
MSH_CMD_EXPORT_ALIAS(cmd_list_irq, list_irq, list system irq);
456 457 458 459 460
#endif
#endif
#endif