[205dbb9d] | 1 | /** |
---|
| 2 | * @file |
---|
[be47df9] | 3 | * |
---|
[205dbb9d] | 4 | * @brief RTEMS Create Timer |
---|
| 5 | * @ingroup ClassicTimer |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | /* |
---|
[c55df85] | 9 | * COPYRIGHT (c) 1989-2002. |
---|
[be47df9] | 10 | * On-Line Applications Research Corporation (OAR). |
---|
| 11 | * |
---|
| 12 | * The license and distribution terms for this file may be |
---|
| 13 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 14 | * http://www.rtems.org/license/LICENSE. |
---|
[be47df9] | 15 | */ |
---|
| 16 | |
---|
[1095ec1] | 17 | #if HAVE_CONFIG_H |
---|
| 18 | #include "config.h" |
---|
| 19 | #endif |
---|
| 20 | |
---|
[03b900d] | 21 | #include <rtems/rtems/timerimpl.h> |
---|
| 22 | #include <rtems/rtems/clock.h> |
---|
[be47df9] | 23 | #include <rtems/rtems/status.h> |
---|
| 24 | #include <rtems/rtems/support.h> |
---|
[2903090] | 25 | #include <rtems/score/assert.h> |
---|
[03b900d] | 26 | #include <rtems/score/chainimpl.h> |
---|
[be47df9] | 27 | #include <rtems/score/thread.h> |
---|
[03b900d] | 28 | #include <rtems/score/todimpl.h> |
---|
[4b48ece0] | 29 | #include <rtems/score/watchdogimpl.h> |
---|
[be47df9] | 30 | |
---|
[03b900d] | 31 | RTEMS_STATIC_ASSERT( |
---|
[bf2a53d2] | 32 | PER_CPU_WATCHDOG_REALTIME == TIMER_CLASS_BIT_TIME_OF_DAY, |
---|
[03b900d] | 33 | TIMER_CLASS_BIT_TIME_OF_DAY |
---|
| 34 | ); |
---|
| 35 | |
---|
| 36 | void _Timer_Routine_adaptor( Watchdog_Control *the_watchdog ) |
---|
[2903090] | 37 | { |
---|
[03b900d] | 38 | Timer_Control *the_timer; |
---|
| 39 | Per_CPU_Control *cpu; |
---|
[1ccbd052] | 40 | |
---|
[03b900d] | 41 | the_timer = RTEMS_CONTAINER_OF( the_watchdog, Timer_Control, Ticker ); |
---|
| 42 | cpu = _Watchdog_Get_CPU( &the_timer->Ticker ); |
---|
| 43 | the_timer->stop_time = _Timer_Get_CPU_ticks( cpu ); |
---|
[2903090] | 44 | |
---|
[03b900d] | 45 | ( *the_timer->routine )( the_timer->Object.id, the_timer->user_data ); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | rtems_status_code _Timer_Fire( |
---|
| 49 | rtems_id id, |
---|
| 50 | rtems_interval interval, |
---|
| 51 | rtems_timer_service_routine_entry routine, |
---|
| 52 | void *user_data, |
---|
| 53 | Timer_Classes the_class, |
---|
| 54 | Watchdog_Service_routine_entry adaptor |
---|
| 55 | ) |
---|
| 56 | { |
---|
[77e6eba7] | 57 | Timer_Control *the_timer; |
---|
| 58 | ISR_lock_Context lock_context; |
---|
| 59 | |
---|
| 60 | the_timer = _Timer_Get( id, &lock_context ); |
---|
| 61 | if ( the_timer != NULL ) { |
---|
| 62 | Per_CPU_Control *cpu; |
---|
| 63 | |
---|
| 64 | cpu = _Timer_Acquire_critical( the_timer, &lock_context ); |
---|
| 65 | _Timer_Cancel( cpu, the_timer ); |
---|
| 66 | _Watchdog_Initialize( &the_timer->Ticker, adaptor ); |
---|
| 67 | the_timer->the_class = the_class; |
---|
| 68 | the_timer->routine = routine; |
---|
| 69 | the_timer->user_data = user_data; |
---|
| 70 | the_timer->initial = interval; |
---|
| 71 | the_timer->start_time = _Timer_Get_CPU_ticks( cpu ); |
---|
| 72 | |
---|
| 73 | if ( _Timer_Is_interval_class( the_class ) ) { |
---|
| 74 | _Watchdog_Insert( |
---|
[9480815a] | 75 | &cpu->Watchdog.Header[ PER_CPU_WATCHDOG_TICKS ], |
---|
[77e6eba7] | 76 | &the_timer->Ticker, |
---|
| 77 | cpu->Watchdog.ticks + interval |
---|
| 78 | ); |
---|
| 79 | } else { |
---|
| 80 | _Watchdog_Insert( |
---|
[bf2a53d2] | 81 | &cpu->Watchdog.Header[ PER_CPU_WATCHDOG_REALTIME ], |
---|
[77e6eba7] | 82 | &the_timer->Ticker, |
---|
[3a4e044] | 83 | _Watchdog_Ticks_from_seconds( interval ) |
---|
[77e6eba7] | 84 | ); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | _Timer_Release( cpu, &lock_context ); |
---|
| 88 | return RTEMS_SUCCESSFUL; |
---|
[2903090] | 89 | } |
---|
[1ccbd052] | 90 | |
---|
[03b900d] | 91 | return RTEMS_INVALID_ID; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | rtems_status_code _Timer_Fire_after( |
---|
| 95 | rtems_id id, |
---|
| 96 | rtems_interval ticks, |
---|
| 97 | rtems_timer_service_routine_entry routine, |
---|
| 98 | void *user_data, |
---|
| 99 | Timer_Classes the_class, |
---|
| 100 | Watchdog_Service_routine_entry adaptor |
---|
| 101 | ) |
---|
| 102 | { |
---|
| 103 | if ( ticks == 0 ) |
---|
| 104 | return RTEMS_INVALID_NUMBER; |
---|
| 105 | |
---|
| 106 | if ( !routine ) |
---|
| 107 | return RTEMS_INVALID_ADDRESS; |
---|
| 108 | |
---|
| 109 | return _Timer_Fire( |
---|
| 110 | id, |
---|
| 111 | ticks, |
---|
| 112 | routine, |
---|
| 113 | user_data, |
---|
| 114 | the_class, |
---|
| 115 | adaptor |
---|
| 116 | ); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | rtems_status_code _Timer_Fire_when( |
---|
| 120 | rtems_id id, |
---|
| 121 | const rtems_time_of_day *wall_time, |
---|
| 122 | rtems_timer_service_routine_entry routine, |
---|
| 123 | void *user_data, |
---|
| 124 | Timer_Classes the_class, |
---|
| 125 | Watchdog_Service_routine_entry adaptor |
---|
| 126 | ) |
---|
| 127 | { |
---|
| 128 | rtems_interval seconds; |
---|
| 129 | |
---|
| 130 | if ( !_TOD_Is_set() ) |
---|
| 131 | return RTEMS_NOT_DEFINED; |
---|
| 132 | |
---|
| 133 | if ( !routine ) |
---|
| 134 | return RTEMS_INVALID_ADDRESS; |
---|
| 135 | |
---|
| 136 | if ( !_TOD_Validate( wall_time ) ) |
---|
| 137 | return RTEMS_INVALID_CLOCK; |
---|
| 138 | |
---|
| 139 | seconds = _TOD_To_seconds( wall_time ); |
---|
| 140 | if ( seconds <= _TOD_Seconds_since_epoch() ) |
---|
| 141 | return RTEMS_INVALID_CLOCK; |
---|
| 142 | |
---|
| 143 | return _Timer_Fire( |
---|
| 144 | id, |
---|
| 145 | seconds, |
---|
| 146 | routine, |
---|
| 147 | user_data, |
---|
| 148 | the_class, |
---|
| 149 | adaptor |
---|
| 150 | ); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | void _Timer_Cancel( Per_CPU_Control *cpu, Timer_Control *the_timer ) |
---|
| 154 | { |
---|
| 155 | Timer_Classes the_class; |
---|
| 156 | |
---|
| 157 | the_class = the_timer->the_class; |
---|
| 158 | |
---|
| 159 | if ( _Watchdog_Is_scheduled( &the_timer->Ticker ) ) { |
---|
| 160 | the_timer->stop_time = _Timer_Get_CPU_ticks( cpu ); |
---|
| 161 | _Watchdog_Remove( |
---|
| 162 | &cpu->Watchdog.Header[ _Timer_Watchdog_header_index( the_class ) ], |
---|
| 163 | &the_timer->Ticker |
---|
| 164 | ); |
---|
| 165 | } else if ( _Timer_Is_on_task_class( the_class ) ) { |
---|
| 166 | Timer_server_Control *timer_server; |
---|
| 167 | ISR_lock_Context lock_context; |
---|
| 168 | |
---|
| 169 | timer_server = _Timer_server; |
---|
| 170 | _Assert( timer_server != NULL ); |
---|
| 171 | _Timer_server_Acquire_critical( timer_server, &lock_context ); |
---|
| 172 | |
---|
| 173 | if ( _Watchdog_Get_state( &the_timer->Ticker ) == WATCHDOG_PENDING ) { |
---|
| 174 | _Watchdog_Set_state( &the_timer->Ticker, WATCHDOG_INACTIVE ); |
---|
| 175 | _Chain_Extract_unprotected( &the_timer->Ticker.Node.Chain ); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | _Timer_server_Release_critical( timer_server, &lock_context ); |
---|
| 179 | } |
---|
[2903090] | 180 | } |
---|
| 181 | |
---|
[be47df9] | 182 | rtems_status_code rtems_timer_create( |
---|
[d3b72ca3] | 183 | rtems_name name, |
---|
| 184 | rtems_id *id |
---|
[be47df9] | 185 | ) |
---|
| 186 | { |
---|
| 187 | Timer_Control *the_timer; |
---|
| 188 | |
---|
| 189 | if ( !rtems_is_name_valid( name ) ) |
---|
| 190 | return RTEMS_INVALID_NAME; |
---|
| 191 | |
---|
[e980b219] | 192 | if ( !id ) |
---|
| 193 | return RTEMS_INVALID_ADDRESS; |
---|
| 194 | |
---|
[be47df9] | 195 | the_timer = _Timer_Allocate(); |
---|
| 196 | |
---|
| 197 | if ( !the_timer ) { |
---|
[23fec9f0] | 198 | _Objects_Allocator_unlock(); |
---|
[be47df9] | 199 | return RTEMS_TOO_MANY; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | the_timer->the_class = TIMER_DORMANT; |
---|
[03b900d] | 203 | _Watchdog_Preinitialize( &the_timer->Ticker, _Per_CPU_Get_snapshot() ); |
---|
[be47df9] | 204 | |
---|
[6312db3] | 205 | _Objects_Open( |
---|
| 206 | &_Timer_Information, |
---|
| 207 | &the_timer->Object, |
---|
| 208 | (Objects_Name) name |
---|
| 209 | ); |
---|
[be47df9] | 210 | |
---|
| 211 | *id = the_timer->Object.id; |
---|
[23fec9f0] | 212 | _Objects_Allocator_unlock(); |
---|
[be47df9] | 213 | return RTEMS_SUCCESSFUL; |
---|
| 214 | } |
---|