File indexing completed on 2025-05-11 08:24:01
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #include <rtems.h>
0024
0025 #include <stdlib.h>
0026
0027 #include <rtems/clockdrv.h>
0028 #include <rtems/score/sh_io.h>
0029 #include <rtems/score/sh.h>
0030 #include <rtems/score/ispsh7032.h>
0031 #include <rtems/score/iosh7032.h>
0032
0033 extern uint32_t bsp_clicks_per_second;
0034
0035 #ifndef CLOCKPRIO
0036 #define CLOCKPRIO 10
0037 #endif
0038
0039 #define I_CLK_PHI_1 0
0040 #define I_CLK_PHI_2 1
0041 #define I_CLK_PHI_4 2
0042 #define I_CLK_PHI_8 3
0043
0044
0045
0046
0047
0048
0049 #define I_CLK_PHI I_CLK_PHI_4
0050 #define CLOCK_SCALE (1<<I_CLK_PHI)
0051
0052 #define ITU0_STARTMASK 0xfe
0053 #define ITU0_SYNCMASK 0xfe
0054 #define ITU0_MODEMASK 0xfe
0055 #define ITU0_TCRMASK (0x20 | I_CLK_PHI)
0056 #define ITU_STAT_MASK 0xf8
0057 #define ITU0_IRQMASK 0xfe
0058 #define ITU0_TIERMASK 0x01
0059 #define IPRC_ITU0_MASK 0xff0f
0060 #define ITU0_TIORVAL 0x08
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082 static unsigned int sh_clicks_per_tick(
0083 unsigned int clicks_per_sec,
0084 unsigned int usec_per_tick
0085 )
0086 {
0087 #if 1
0088 unsigned int clicks_per_tick = 0 ;
0089
0090 unsigned int b = clicks_per_sec ;
0091 unsigned int c = 1000000 ;
0092 unsigned int d = 1 ;
0093 unsigned int a = ( ( b / c ) * usec_per_tick ) / d;
0094
0095 clicks_per_tick += a ;
0096
0097 while ( ( b %= c ) > 0 )
0098 {
0099 c /= 10 ;
0100 d *= 10 ;
0101 a = ( ( b / c ) * usec_per_tick ) / d ;
0102 clicks_per_tick += a ;
0103 }
0104 return clicks_per_tick ;
0105 #else
0106 double fclicks_per_tick =
0107 ((double) clicks_per_sec * (double) usec_per_tick) / 1000000.0 ;
0108 return (uint32_t) fclicks_per_tick ;
0109 #endif
0110 }
0111
0112
0113
0114
0115
0116
0117 #define CLOCK_VECTOR IMIA0_ISP_V
0118
0119
0120
0121
0122
0123
0124 volatile uint32_t Clock_driver_ticks;
0125
0126 static void Clock_exit( void );
0127 static rtems_isr Clock_isr( rtems_vector_number vector );
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137 uint32_t Clock_isrs;
0138 static uint32_t Clock_isrs_const;
0139
0140
0141
0142
0143 rtems_isr_entry Old_ticker;
0144
0145
0146
0147
0148 static rtems_isr Clock_isr(
0149 rtems_vector_number vector
0150 )
0151 {
0152
0153
0154
0155
0156
0157
0158
0159
0160 uint8_t temp;
0161
0162
0163 temp = read8( ITU_TSR0) & ITU_STAT_MASK;
0164 write8( temp, ITU_TSR0);
0165
0166 Clock_driver_ticks++ ;
0167
0168 if( Clock_isrs == 1)
0169 {
0170 rtems_clock_tick();
0171 Clock_isrs = Clock_isrs_const;
0172 }
0173 else
0174 {
0175 Clock_isrs-- ;
0176 }
0177 }
0178
0179
0180
0181
0182
0183
0184
0185 static void Install_clock(
0186 rtems_isr_entry clock_isr
0187 )
0188 {
0189 uint8_t temp8 = 0;
0190 uint32_t microseconds_per_tick;
0191 uint32_t cclicks_per_tick;
0192 uint16_t Clock_limit;
0193
0194
0195
0196
0197
0198 Clock_driver_ticks = 0;
0199
0200 if ( rtems_configuration_get_microseconds_per_tick() != 0 )
0201 microseconds_per_tick = rtems_configuration_get_microseconds_per_tick() ;
0202 else
0203 microseconds_per_tick = 10000 ;
0204
0205
0206 cclicks_per_tick = sh_clicks_per_tick(
0207 bsp_clicks_per_second / CLOCK_SCALE, microseconds_per_tick );
0208
0209 Clock_isrs_const = cclicks_per_tick >> 16 ;
0210 if ( ( cclicks_per_tick | 0xffff ) > 0 )
0211 Clock_isrs_const++ ;
0212 Clock_limit = cclicks_per_tick / Clock_isrs_const ;
0213 Clock_isrs = Clock_isrs_const;
0214
0215 rtems_interrupt_catch( Clock_isr, CLOCK_VECTOR, &Old_ticker );
0216
0217
0218
0219
0220
0221 temp8 = read8( ITU_TSTR) & ITU0_STARTMASK;
0222 write8( temp8, ITU_TSTR);
0223
0224
0225 write16( 0, ITU_TCNT0);
0226
0227
0228 temp8 = read8( ITU_TSNC) & ITU0_SYNCMASK;
0229 write8( temp8, ITU_TSNC);
0230
0231
0232 temp8 = read8( ITU_TMDR) & ITU0_MODEMASK;
0233 write8( temp8, ITU_TMDR);
0234
0235
0236 write8( ITU0_TCRMASK , ITU_TCR0);
0237
0238
0239 write8( ITU0_TIORVAL, ITU_TIOR0);
0240
0241
0242 temp8 = read8( ITU_TSR0) & ITU_STAT_MASK;
0243 write8( temp8, ITU_TSR0);
0244
0245
0246 temp8 = read8( ITU_TIER0) | ITU0_TIERMASK;
0247 write8( temp8, ITU_TIER0);
0248
0249
0250 if( sh_set_irq_priority( CLOCK_VECTOR, CLOCKPRIO ) != RTEMS_SUCCESSFUL)
0251 rtems_fatal_error_occurred( RTEMS_NOT_CONFIGURED);
0252
0253
0254 write16( Clock_limit, ITU_GRA0);
0255
0256
0257 temp8 = read8( ITU_TSTR) |~ITU0_STARTMASK;
0258 write8( temp8, ITU_TSTR);
0259
0260
0261
0262
0263
0264 atexit( Clock_exit );
0265 }
0266
0267
0268
0269
0270 void Clock_exit( void )
0271 {
0272 uint8_t temp8 = 0;
0273
0274
0275
0276 if( sh_set_irq_priority( CLOCK_VECTOR, 0 ) != RTEMS_SUCCESSFUL)
0277 rtems_fatal_error_occurred( RTEMS_UNSATISFIED);
0278
0279
0280
0281
0282
0283
0284
0285 temp8 = read8( ITU_TSTR) & ITU0_STARTMASK;
0286 write8( temp8, ITU_TSTR);
0287
0288
0289 }
0290
0291 void _Clock_Initialize( void )
0292 {
0293 Install_clock( Clock_isr );
0294 }