⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 h8s_kexcept1.c

📁 ecos移植到R8H系列的源码。源码包来自http://www.cetoni.de/develop/develop_ecosh8s_en.html
💻 C
字号:
/*=================================================================////        kexcept1.cxx////        Kernel C API Exception test 1////=================================================================//####ECOSGPLCOPYRIGHTBEGIN####// -------------------------------------------// This file is part of eCos, the Embedded Configurable Operating System.// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.//// eCos is free software; you can redistribute it and/or modify it under// the terms of the GNU General Public License as published by the Free// Software Foundation; either version 2 or (at your option) any later version.//// eCos is distributed in the hope that it will be useful, but WITHOUT ANY// WARRANTY; without even the implied warranty of MERCHANTABILITY or// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License// for more details.//// You should have received a copy of the GNU General Public License along// with eCos; if not, write to the Free Software Foundation, Inc.,// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.//// As a special exception, if other files instantiate templates or use macros// or inline functions from this file, or you compile this file and link it// with other works to produce a work based on this file, this file does not// by itself cause the resulting work to be covered by the GNU General Public// License. However the source code for this file must still be made available// in accordance with section (3) of the GNU General Public License.//// This exception does not invalidate any other reasons why a work based on// this file might be covered by the GNU General Public License.//// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.// at http://sources.redhat.com/ecos/ecos-license/// -------------------------------------------//####ECOSGPLCOPYRIGHTEND####//=================================================================//#####DESCRIPTIONBEGIN####//// Author(s):     dsm// Contributors:  dsm, jlarmour, Uwe Kindler// Date:          2004-02-11// Description:   Test basic H8S exception functionality. Checks H8S trap and//                trace exceptions.//####DESCRIPTIONEND####*///===========================================================================//                            INCLUDES//===========================================================================#include <cyg/hal/hal_arch.h>           // CYGNUM_HAL_STACK_SIZE_TYPICAL#include <cyg/kernel/kapi.h>#include <cyg/infra/testcase.h>#ifdef CYGPKG_KERNEL_EXCEPTIONS#ifdef CYGFUN_KERNEL_API_C#include <cyg/hal/hal_intr.h>           // exception ranges#include "testaux.h"//===========================================================================//                                  TYPES//===========================================================================typedef union _exc_status{    cyg_uint8 byte;    struct {               cyg_uint8 unused : 2;               cyg_uint8 err    : 1;               cyg_uint8 trace  : 1;               cyg_uint8 trap3  : 1;               cyg_uint8 trap2  : 1;               cyg_uint8 trap1  : 1;               cyg_uint8 trap0  : 1;           } bit;} exception_status_t;//===========================================================================//                                  DEFINES//===========================================================================#define NTHREADS 1#define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL//===========================================================================//                                  LOCALS//===========================================================================static cyg_handle_t            thread[NTHREADS];static cyg_thread              thread_obj[NTHREADS];static char                    stack[NTHREADS][STACKSIZE];static CYG_ADDRESS             old_vsr_handler[CYGNUM_HAL_EXCEPTION_COUNT];static exception_status_t      status; static cyg_exception_handler_t handler0;static int                     d0;//===========================================================================//                            EXCEPTION HANDLER 0//===========================================================================static void handler0(cyg_addrword_t data, cyg_code_t number, cyg_addrword_t info){    HAL_SavedRegisters *regs = (HAL_SavedRegisters *)info;        switch (number)    {        case CYGNUM_HAL_EXCEPTION_TRAP0:             CYG_TEST_INFO("trap #0 exception handler called");             status.bit.trap0 = 1;             break;                     case CYGNUM_HAL_EXCEPTION_TRAP1:             CYG_TEST_INFO("trap #1 exception handler called");             status.bit.trap1 = 1;             break;                     case CYGNUM_HAL_EXCEPTION_TRAP2:             CYG_TEST_INFO("trap #2 exception handler called");             status.bit.trap2 = 1;             break;                     case CYGNUM_HAL_EXCEPTION_TRAP3:             CYG_TEST_INFO("trap #3 exception handler called");             status.bit.trap3 = 1;             break;                     case CYGNUM_HAL_EXCEPTION_TRACE:             CYG_TEST_INFO("trace exception handler called");             regs->exr &= ~0x80; // clear trace bit if a trace exception happens             status.bit.trace = 1;             break;                     default:             CYG_TEST_INFO("unknown exception caused");             status.bit.err = 1;    }        CYG_TEST_CHECK((CYG_ADDRWORD)123 == data, "handler given wrong data");}//===========================================================================//                            EXCEPTION HANDLER 1//===========================================================================static void handler1(cyg_addrword_t data, cyg_code_t number, cyg_addrword_t info){    CYG_TEST_INFO("handler 1 called");    CYG_TEST_CHECK((cyg_addrword_t)&d0 == data, "handler given wrong data");#ifdef CYGSEM_KERNEL_EXCEPTIONS_DECODE    CYG_TEST_CHECK(number == CYGNUM_HAL_EXCEPTION_MAX, "handler given wrong number");#else    CYG_UNUSED_PARAM(cyg_code_t, number);#endif    CYG_TEST_CHECK((cyg_addrword_t)99 == info, "handler given wrong info");}//===========================================================================//                            CAUSE EXCEPTION//===========================================================================void cause_exception(void){    //    // cause a trace exception    //     asm volatile (                              "stc   exr,r0l \n\t"              "bset  #7, r0l \n\t"                    "ldc   r0l,exr \n\t"               "mov.l er0, er1\n\t"             "mov.l er1, er0\n\t"                                 );    //    // now cause H8S trap exceptions    //    asm volatile ("trapa #0\n");    asm volatile ("trapa #1\n");    asm volatile ("trapa #2\n");    asm volatile ("trapa #3\n");}//===========================================================================//                               THE MAIN THREAD//===========================================================================static void entry0( CYG_ADDRWORD data ){    cyg_code_t n;    cyg_exception_handler_t *old_handler, *old_handler1;    cyg_addrword_t old_data, old_data1;    CYG_UNUSED_PARAM(CYG_ADDRESS, data);    cyg_exception_set_handler(        CYGNUM_HAL_EXCEPTION_MAX,         &handler1,        (cyg_addrword_t)&d0,        &old_handler,        &old_data);    cyg_exception_set_handler(        CYGNUM_HAL_EXCEPTION_MAX,         &handler1,        (cyg_addrword_t)&d0,        &old_handler1,        &old_data1);        CYG_TEST_CHECK(old_handler1 == &handler1,        "register exception: old_handler not the one previously registered");    CYG_TEST_CHECK(old_data1 == (cyg_addrword_t)&d0,        "register exception: old_data not those previously registered");    cyg_exception_call_handler(        cyg_thread_self(),        CYGNUM_HAL_EXCEPTION_MAX,        (cyg_addrword_t)99);    CYG_TEST_INFO("handler 1 returned");    cyg_exception_clear_handler(CYGNUM_HAL_EXCEPTION_MAX);    cyg_exception_clear_handler(CYGNUM_HAL_EXCEPTION_MAX);    for(n = CYGNUM_HAL_EXCEPTION_MIN; n <= CYGNUM_HAL_EXCEPTION_MAX; n++) {        cyg_exception_set_handler(            n,            handler0,            (cyg_addrword_t)123,             &old_handler1,            &old_data1);    }    CYG_TEST_PASS("Attempting to provoke exception");    status.byte = 0;    cause_exception();        //    // Reset the exception handlers - after these calls it is safe to place    // breakpoints because trap handlers are reset to RedBoot trap handlers    //#ifdef CYGNUM_HAL_EXCEPTION_TRACE    HAL_VSR_SET( CYGNUM_HAL_EXCEPTION_TRACE, old_vsr_handler[0], NULL );#endif#ifdef CYGNUM_HAL_EXCEPTION_TRAP0    HAL_VSR_SET( CYGNUM_HAL_EXCEPTION_TRAP0, old_vsr_handler[1], NULL );#endif#ifdef CYGNUM_HAL_EXCEPTION_TRAP1    HAL_VSR_SET( CYGNUM_HAL_EXCEPTION_TRAP1, old_vsr_handler[2], NULL );#endif#ifdef CYGNUM_HAL_EXCEPTION_TRAP2    HAL_VSR_SET( CYGNUM_HAL_EXCEPTION_TRAP2, old_vsr_handler[3], NULL );#endif#ifdef CYGNUM_HAL_EXCEPTION_TRAP3    HAL_VSR_SET( CYGNUM_HAL_EXCEPTION_TRAP3, old_vsr_handler[4], NULL );#endif    CYG_TEST_CHECK(status.bit.trace == 1, "trace exception not triggered");    CYG_TEST_CHECK(status.bit.trap0 == 1, "trap #0 exception not triggered");    CYG_TEST_CHECK(status.bit.trap1 == 1, "trap #1 exception not triggered");    CYG_TEST_CHECK(status.bit.trap2 == 1, "trap #2 exception not triggered");    CYG_TEST_CHECK(status.bit.trap3 == 1, "trap #3 exception not triggered");    CYG_TEST_CHECK(status.bit.err   == 0, "Unknown exception caused");        CYG_TEST_PASS_FINISH("Kernel API C exception handling OK");}//===========================================================================//                                THE MAIN ROUTINE//===========================================================================void except0_main( void ){    // Use CYG_TEST_GDBCMD _before_ CYG_TEST_INIT()    CYG_TEST_GDBCMD("handle SIGBUS nostop");    CYG_TEST_GDBCMD("handle SIGSEGV nostop");    CYG_TEST_GDBCMD("handle SIGFPE nostop");    CYG_TEST_INIT();#ifdef HAL_VSR_SET_TO_ECOS_HANDLER    //    // Reclaim the VSRs off RedBoot possibly. We claim also the trap vectors    // required for GDB breakpoint support so it is not possible to place a    // breakpoint after these calls.    //#ifdef CYGNUM_HAL_EXCEPTION_TRACE    HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_TRACE, &old_vsr_handler[0] );#endif#ifdef CYGNUM_HAL_EXCEPTION_TRAP0    HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_TRAP0, &old_vsr_handler[1] );#endif#ifdef CYGNUM_HAL_EXCEPTION_TRAP1    HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_TRAP1, &old_vsr_handler[2] );#endif#ifdef CYGNUM_HAL_EXCEPTION_TRAP2    HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_TRAP2, &old_vsr_handler[3] );#endif#ifdef CYGNUM_HAL_EXCEPTION_TRAP3    HAL_VSR_SET_TO_ECOS_HANDLER( CYGNUM_HAL_EXCEPTION_TRAP3, &old_vsr_handler[4] );#endif#endif    cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kexcept1",        (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]);    cyg_thread_resume(thread[0]);    cyg_scheduler_start();    CYG_TEST_FAIL_FINISH("Not reached");}//===========================================================================//                       KERNEL APPLICATION TRANSITION//===========================================================================externC void cyg_start( void ){    except0_main();}#else // def CYGFUN_KERNEL_API_C#define N_A_MSG "Kernel C API layer disabled"#endif // def CYGFUN_KERNEL_API_C#else // def CYGPKG_KERNEL_EXCEPTIONS#define N_A_MSG "Exceptions disabled"#endif // def CYGPKG_KERNEL_EXCEPTIONS#ifdef N_A_MSGexternC voidcyg_start( void ){    CYG_TEST_INIT();    CYG_TEST_NA( N_A_MSG);}#endif // N_A_MSG/* EOF kexcept1.c */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -