OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [hal/] [sh/] [arch/] [v2_0/] [src/] [hal_misc.c] - Blame information for rev 810

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      hal_misc.c
4
//
5
//      HAL miscellaneous functions
6
//
7
//==========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
//
36
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//==========================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):    jskov
44
// Contributors: jskov, jlarmour, nickg
45
// Date:         1999-04-03
46
// Purpose:      HAL miscellaneous functions
47
// Description:  This file contains miscellaneous functions provided by the
48
//               HAL.
49
//
50
//####DESCRIPTIONEND####
51
//
52
//===========================================================================
53
 
54
#include <pkgconf/hal.h>
55
 
56
#include <cyg/infra/cyg_type.h>
57
#include <cyg/infra/cyg_trac.h>         // tracing macros
58
#include <cyg/infra/cyg_ass.h>          // assertion macros
59
#include <cyg/infra/diag.h>             // diag_printf
60
 
61
#include <cyg/hal/hal_arch.h>           // HAL header
62
#include <cyg/hal/hal_cache.h>          // HAL cache
63
#include <cyg/hal/hal_intr.h>           // HAL interrupts/exceptions
64
 
65
#include <cyg/hal/sh_regs.h>            // timer registers
66
 
67
//---------------------------------------------------------------------------
68
// Functions used during initialization.
69
 
70
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
71
cyg_bool cyg_hal_stop_constructors;
72
#endif
73
 
74
typedef void (*pfunc) (void);
75
extern pfunc __CTOR_LIST__[];
76
extern pfunc __CTOR_END__[];
77
 
78
void
79
cyg_hal_invoke_constructors (void)
80
{
81
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
82
    static pfunc *p = &__CTOR_END__[-1];
83
 
84
    cyg_hal_stop_constructors = 0;
85
    for (; p >= __CTOR_LIST__; p--) {
86
        (*p) ();
87
        if (cyg_hal_stop_constructors) {
88
            p--;
89
            break;
90
        }
91
    }
92
#else
93
    pfunc *p;
94
 
95
    for (p = &__CTOR_END__[-1]; p >= __CTOR_LIST__; p--)
96
        (*p) ();
97
#endif
98
}
99
 
100
//---------------------------------------------------------------------------
101
// First level C exception handler.
102
 
103
externC void __handle_exception (void);
104
 
105
externC HAL_SavedRegisters *_hal_registers;
106
#ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
107
externC void* volatile __mem_fault_handler;
108
#endif
109
 
110
void
111
cyg_hal_exception_handler(HAL_SavedRegisters *regs)
112
{
113
#ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
114
    if (__mem_fault_handler &&
115
        ((regs->event >= _CYGNUM_HAL_VECTOR_FIRST_MEM_FAULT) &&
116
         (regs->event <= _CYGNUM_HAL_VECTOR_LAST_MEM_FAULT))) {
117
        regs->pc = (unsigned long)__mem_fault_handler;
118
        return; // Caught an exception inside stubs        
119
    }
120
 
121
    // Set the pointer to the registers of the current exception
122
    // context. At entry the GDB stub will expand the
123
    // HAL_SavedRegisters structure into a (bigger) register array.
124
    _hal_registers = regs;
125
 
126
    __handle_exception();
127
 
128
#elif defined(CYGFUN_HAL_COMMON_KERNEL_SUPPORT) && \
129
      defined(CYGPKG_HAL_EXCEPTIONS)
130
 
131
    cyg_hal_deliver_exception( regs->event, (CYG_ADDRWORD)regs );
132
 
133
#else
134
 
135
    CYG_FAIL("Exception!!!");
136
 
137
#endif    
138
 
139
    return;
140
}
141
 
142
//---------------------------------------------------------------------------
143
// Default ISR
144
externC cyg_uint32
145
hal_arch_default_isr(CYG_ADDRWORD vector, CYG_ADDRWORD data)
146
{
147
    return 0;
148
}
149
 
150
//--------------------------------------------------------------------------
151
// Determine the index of the ls bit of the supplied mask.
152
 
153
cyg_uint32
154
hal_lsbit_index(cyg_uint32 mask)
155
{
156
    cyg_uint32 n = mask;
157
 
158
    static const signed char tab[64] =
159
    { -1, 0, 1, 12, 2, 6, 0, 13, 3, 0, 7, 0, 0, 0, 0, 14, 10,
160
      4, 0, 0, 8, 0, 0, 25, 0, 0, 0, 0, 0, 21, 27 , 15, 31, 11,
161
      5, 0, 0, 0, 0, 0, 9, 0, 0, 24, 0, 0 , 20, 26, 30, 0, 0, 0,
162
      0, 23, 0, 19, 29, 0, 22, 18, 28, 17, 16, 0
163
    };
164
 
165
    n &= ~(n-1UL);
166
    n = (n<<16)-n;
167
    n = (n<<6)+n;
168
    n = (n<<4)+n;
169
 
170
    return tab[n>>26];
171
}
172
 
173
//--------------------------------------------------------------------------
174
// Determine the index of the ms bit of the supplied mask.
175
 
176
cyg_uint32
177
hal_msbit_index(cyg_uint32 mask)
178
{
179
    cyg_uint32 x = mask;
180
    cyg_uint32 w;
181
 
182
    // Phase 1: make word with all ones from that one to the right
183
    x |= x >> 16;
184
    x |= x >> 8;
185
    x |= x >> 4;
186
    x |= x >> 2;
187
    x |= x >> 1;
188
 
189
    // Phase 2: calculate number of "1" bits in the word
190
    w = (x & 0x55555555) + ((x >> 1) & 0x55555555);
191
    w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
192
    w = w + (w >> 4);
193
    w = (w & 0x000F000F) + ((w >> 8) & 0x000F000F);
194
    return (cyg_uint32)((w + (w >> 16)) & 0xFF) - 1;
195
 
196
}
197
 
198
//---------------------------------------------------------------------------
199
// Idle thread action
200
 
201
void
202
hal_idle_thread_action( cyg_uint32 count )
203
{
204
}
205
 
206
#ifdef CYGHWR_SH_RTC_TIMER_IS_TMU
207
//---------------------------------------------------------------------------
208
// Low-level delay (in microseconds)
209
 
210
void
211
hal_delay_us(int usecs)
212
{
213
    unsigned char _tstr;  // Current clock control
214
    volatile unsigned char *tstr = (volatile unsigned char *)CYGARC_REG_TSTR;
215
    volatile unsigned long *tcnt = (volatile unsigned long *)CYGARC_REG_TCNT1;
216
    volatile unsigned long *tcor = (volatile unsigned long *)CYGARC_REG_TCOR1;
217
    unsigned long clocks_per_us = (CYGHWR_HAL_SH_ONCHIP_PERIPHERAL_SPEED+(CYGHWR_HAL_SH_TMU_PRESCALE_0-1))/CYGHWR_HAL_SH_TMU_PRESCALE_0;  // Rounded up
218
    int diff, diff2;
219
    cyg_uint32 val1, val2;
220
 
221
    _tstr = *tstr;
222
    *tstr |= CYGARC_REG_TSTR_STR1;  // Enable channel 1
223
    while (usecs-- > 0) {
224
        diff = 0;
225
        while (diff < clocks_per_us) {
226
            val1 = *tcnt;
227
            while ((val2 = *tcnt) == val1) ;
228
            diff2 = val2 - val1;
229
            if (diff2 < 0) diff2 += *tcor;
230
            diff += diff2;
231
        }
232
    }
233
    *tstr = _tstr;                  // Restore timer to previous state
234
}
235
#endif
236
 
237
//---------------------------------------------------------------------------
238
// End of hal_misc.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.