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
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 #include <rtems.h>
0036
0037 #include <stdlib.h>
0038
0039 #include <rtems/clockdrv.h>
0040 #include <rtems/score/sh_io.h>
0041 #include <rtems/score/sh.h>
0042 #include <rtems/score/ispsh7045.h>
0043 #include <rtems/score/iosh7045.h>
0044
0045 static void Clock_exit( void );
0046
0047 extern uint32_t bsp_clicks_per_second;
0048
0049 #define _MTU_COUNTER0_MICROSECOND (Clock_MHZ/16)
0050
0051 #ifndef CLOCKPRIO
0052 #define CLOCKPRIO 10
0053 #endif
0054
0055 #define MTU0_STARTMASK 0xfe
0056 #define MTU0_SYNCMASK 0xfe
0057 #define MTU0_MODEMASK 0xc0
0058 #define MTU0_TCRMASK 0x22
0059 #define MTU0_STAT_MASK 0xc0
0060 #define MTU0_IRQMASK 0xfe
0061 #define MTU0_TIERMASK 0x01
0062 #define IPRC_MTU0_MASK 0xff0f
0063 #define MTU0_TIORVAL 0x08
0064
0065
0066
0067
0068
0069
0070 #define CLOCK_VECTOR MTUA0_ISP_V
0071
0072
0073
0074
0075
0076 volatile uint32_t Clock_driver_ticks;
0077
0078 static rtems_isr Clock_isr( rtems_vector_number vector );
0079 static uint32_t Clock_MHZ ;
0080
0081
0082
0083
0084
0085
0086
0087
0088 uint32_t Clock_isrs;
0089 static uint32_t Clock_isrs_const;
0090
0091
0092
0093
0094 rtems_isr_entry Old_ticker;
0095
0096
0097
0098
0099 static rtems_isr Clock_isr(
0100 rtems_vector_number vector
0101 )
0102 {
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112 uint8_t temp;
0113
0114
0115 temp = read8( MTU_TSR0) & MTU0_STAT_MASK;
0116 write8( temp, MTU_TSR0);
0117
0118 Clock_driver_ticks++ ;
0119
0120 if( Clock_isrs == 1)
0121 {
0122 rtems_clock_tick();
0123 Clock_isrs = Clock_isrs_const;
0124 }
0125 else
0126 {
0127 Clock_isrs-- ;
0128 }
0129 }
0130
0131
0132
0133
0134
0135
0136
0137 static void Install_clock(
0138 rtems_isr_entry clock_isr
0139 )
0140 {
0141 uint8_t temp8 = 0;
0142 uint32_t factor = 1000000;
0143
0144
0145
0146
0147
0148 Clock_driver_ticks = 0;
0149 Clock_isrs_const = rtems_configuration_get_microseconds_per_tick() / 10000;
0150 Clock_isrs = Clock_isrs_const;
0151
0152 factor /= rtems_configuration_get_microseconds_per_tick();
0153 Clock_MHZ = bsp_clicks_per_second / factor ;
0154
0155 rtems_interrupt_catch( Clock_isr, CLOCK_VECTOR, &Old_ticker );
0156
0157
0158
0159
0160
0161
0162 temp8 = read8( MTU_TSTR) & MTU0_STARTMASK;
0163 write8( temp8, MTU_TSTR);
0164
0165
0166 write16( 0, MTU_TCNT0);
0167
0168
0169 temp8 = read8( MTU_TSYR) & MTU0_SYNCMASK;
0170 write8( temp8, MTU_TSYR);
0171
0172
0173 temp8 = read8( MTU_TMDR0) & MTU0_MODEMASK;
0174 write8( temp8, MTU_TMDR0);
0175
0176
0177 write8( MTU0_TCRMASK , MTU_TCR0);
0178
0179
0180 write8( MTU0_TIORVAL, MTU_TIORL0);
0181
0182
0183 temp8 = read8( MTU_TSR0) & MTU0_STAT_MASK;
0184 write8( temp8, MTU_TSR0);
0185
0186
0187 temp8 = read8( MTU_TIER0) | MTU0_TIERMASK;
0188 write8( temp8, MTU_TIER0);
0189
0190
0191 if( sh_set_irq_priority( CLOCK_VECTOR, CLOCKPRIO ) != RTEMS_SUCCESSFUL)
0192 rtems_fatal_error_occurred( RTEMS_NOT_CONFIGURED);
0193
0194
0195 write16( _MTU_COUNTER0_MICROSECOND, MTU_GR0A);
0196
0197
0198 temp8 = read8( MTU_TSTR) |~MTU0_STARTMASK;
0199 write8( temp8, MTU_TSTR);
0200
0201
0202
0203
0204 atexit( Clock_exit );
0205 }
0206
0207
0208
0209
0210 void Clock_exit( void )
0211 {
0212 uint8_t temp8 = 0;
0213
0214
0215
0216 if( sh_set_irq_priority( CLOCK_VECTOR, 0 ) != RTEMS_SUCCESSFUL)
0217 rtems_fatal_error_occurred( RTEMS_UNSATISFIED);
0218
0219
0220
0221
0222
0223
0224
0225 temp8 = read8( MTU_TSTR) & MTU0_STARTMASK;
0226 write8( temp8, MTU_TSTR);
0227
0228
0229 }
0230
0231 void _Clock_Initialize( void )
0232 {
0233 Install_clock( Clock_isr );
0234 }