interrupt.c 4.7 KB
Newer Older
B
bernard 已提交
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
B
bernard 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
B
bernard 已提交
5 6 7 8
 *
 * Change Logs:
 * Date           Author       Notes
 * 2013-07-06     Bernard      first version
山河's avatar
山河 已提交
9
 * 2015-11-06     zchong       support iar compiler
B
bernard 已提交
10 11 12 13 14 15 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
 */

#include <rthw.h>
#include <rtthread.h>

#include "am33xx.h"
#include "interrupt.h"

#define AINTC_BASE  AM33XX_AINTC_REGS

#define MAX_HANDLERS	128

extern volatile rt_uint8_t rt_interrupt_nest;

/* exception and interrupt handler table */
struct rt_irq_desc isr_table[MAX_HANDLERS];
rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
rt_uint32_t rt_thread_switch_interrupt_flag;

/**
 * @addtogroup AM33xx
 */
/*@{*/

void rt_dump_aintc(void)
{
    int k;
    rt_kprintf("active irq %d", INTC_SIR_IRQ(AINTC_BASE));
    rt_kprintf("\n--- hw mask ---\n");
    for (k = 0; k < 4; k++)
    {
        rt_kprintf("0x%08x, ", INTC_MIR(AINTC_BASE, k));
    }
    rt_kprintf("\n--- hw itr ---\n");
    for (k = 0; k < 4; k++)
    {
        rt_kprintf("0x%08x, ", INTC_ITR(AINTC_BASE, k));
    }
    rt_kprintf("\n");
}

const unsigned int AM335X_VECTOR_BASE = 0x4030FC00;
extern void rt_cpu_vector_set_base(unsigned int addr);
山河's avatar
山河 已提交
53 54 55
#ifdef __ICCARM__
extern int __vector;
#else
B
bernard 已提交
56
extern int system_vectors;
山河's avatar
山河 已提交
57
#endif
B
bernard 已提交
58 59 60 61

static void rt_hw_vector_init(void)
{
    unsigned int *dest = (unsigned int *)AM335X_VECTOR_BASE;
山河's avatar
山河 已提交
62 63 64 65
    
#ifdef __ICCARM__
    unsigned int *src =  (unsigned int *)&__vector;
#else
B
bernard 已提交
66
    unsigned int *src =  (unsigned int *)&system_vectors;
山河's avatar
山河 已提交
67 68
#endif
    
B
bernard 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    rt_memcpy(dest, src, 16 * 4);
    rt_cpu_vector_set_base(AM335X_VECTOR_BASE);
}

/**
 * This function will initialize hardware interrupt
 */
void rt_hw_interrupt_init(void)
{
    /* initialize vector table */
    rt_hw_vector_init();

    /* init exceptions table */
    rt_memset(isr_table, 0x00, sizeof(isr_table));

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

/**
 * This function will mask a interrupt.
 * @param vector the interrupt number
 */
void rt_hw_interrupt_mask(int vector)
{
    INTC_MIR_SET(AINTC_BASE, vector >> 0x05) = 0x1 << (vector & 0x1f);
}

/**
 * This function will un-mask a interrupt.
 * @param vector the interrupt number
 */
void rt_hw_interrupt_umask(int vector)
{
    INTC_MIR_CLEAR(AINTC_BASE, vector >> 0x05) = 0x1 << (vector & 0x1f);
}

/**
 * This function will control the interrupt attribute.
 * @param vector the interrupt number
 */
void rt_hw_interrupt_control(int vector, int priority, int route)
{
    int fiq;

    if (route == 0)
        fiq = 0;
    else
        fiq = 1;

    INTC_ILR(AINTC_BASE, vector) = ((priority << 0x02) & 0x1FC) | fiq ;
}

int rt_hw_interrupt_get_active(int fiq_irq)
{
    int ir;
    if (fiq_irq == INT_FIQ)
    {
        ir = INTC_SIR_FIQ(AINTC_BASE) & 0x7f;
    }
    else
    {
        ir = INTC_SIR_IRQ(AINTC_BASE) & 0x7f;
    }

    return ir;
}

void rt_hw_interrupt_ack(int fiq_irq)
{
    if (fiq_irq == INT_FIQ)
    {
        /* new FIQ generation */
        INTC_CONTROL(AINTC_BASE) |= 0x02;
    }
    else
    {
        /* new IRQ generation */
        INTC_CONTROL(AINTC_BASE) |= 0x01;
    }
}

/**
 * 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
 */
rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
armink_ztl's avatar
armink_ztl 已提交
161
        void *param, const char *name)
B
bernard 已提交
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
{
    rt_isr_handler_t old_handler = RT_NULL;

    if(vector < MAX_HANDLERS)
    {
        old_handler = isr_table[vector].handler;

        if (handler != RT_NULL)
        {
#ifdef RT_USING_INTERRUPT_INFO
            rt_strncpy(isr_table[vector].name, name, RT_NAME_MAX);
#endif /* RT_USING_INTERRUPT_INFO */
            isr_table[vector].handler = handler;
            isr_table[vector].param = param;
        }
    }

    return old_handler;
}

/**
 * This function will trigger an interrupt.
 * @param vector the interrupt number
 */
void rt_hw_interrupt_trigger(int vector)
{
    INTC_ISR_SET(AINTC_BASE, vector>>5) = 1 << (vector & 0x1f);
}

void rt_hw_interrupt_clear(int vector)
{
    INTC_ISR_CLEAR(AINTC_BASE, vector>>5) = 1 << (vector & 0x1f);
}

void rt_dump_isr_table(void)
{
    int idx;
    for(idx = 0; idx < MAX_HANDLERS; idx++)
    {
#ifdef RT_USING_INTERRUPT_INFO
        rt_kprintf("nr:%4d, name: %*.s, handler: 0x%p, param: 0x%08x\r\n",
                idx, RT_NAME_MAX, isr_table[idx].name,
                isr_table[idx].handler, isr_table[idx].param);
#else
        rt_kprintf("nr:%4d, handler: 0x%p, param: 0x%08x\r\n",
                idx, isr_table[idx].handler, isr_table[idx].param);
#endif
    }
}
/*@}*/
山河's avatar
山河 已提交
212 213