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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [sh/] [arch/] [current/] [src/] [hal_misc.c] - Blame information for rev 817

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

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

powered by: WebSVN 2.1.0

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