| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2005-2011, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: btmr.h $ |
|---|
| 11 | * $brcm_Revision: Hydra_Software_Devel/11 $ |
|---|
| 12 | * $brcm_Date: 8/25/11 12:37p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * This is a magnum PI module used to control the hardware timers of a BCM7xxx part. |
|---|
| 16 | * This provides a means to create/destroy and start/stop timers when required. |
|---|
| 17 | * |
|---|
| 18 | * Revision History: |
|---|
| 19 | * |
|---|
| 20 | * $brcm_Log: /magnum/basemodules/tmr/7401/btmr.h $ |
|---|
| 21 | * |
|---|
| 22 | * Hydra_Software_Devel/11 8/25/11 12:37p rjlewis |
|---|
| 23 | * SW7425-1187: make destroy a tagged so users can line up creates and |
|---|
| 24 | * destroys when missing a destroy. |
|---|
| 25 | * |
|---|
| 26 | * Hydra_Software_Devel/10 8/8/11 2:53p rjlewis |
|---|
| 27 | * SW3548-2526: Merged in virtual timer branch. PR22129: SW3548-2526: |
|---|
| 28 | * incorrectly using unsigned long and uint32_t where unsigned correct. |
|---|
| 29 | * SWDTV-5441: added support for secondary timer block. Fixed return |
|---|
| 30 | * variables from uint32 to uslong. SW3548-2526: General clean-up SW3548- |
|---|
| 31 | * 2526: General clean-up. SW35230-935: update branch so I can continue |
|---|
| 32 | * virtual timer changes before merging back into main. SW3548-2526: |
|---|
| 33 | * rewote virtual timers to use separate hardware timers to control free- |
|---|
| 34 | * run versus count-down virtual timers. PR22129: added support for |
|---|
| 35 | * virtal timers. |
|---|
| 36 | * |
|---|
| 37 | * Hydra_Software_Devel/PR22129/7 7/29/11 2:00p rjlewis |
|---|
| 38 | * SW3548-2526: incorrectly using unsigned long and uint32_t where |
|---|
| 39 | * unsigned correct. |
|---|
| 40 | * |
|---|
| 41 | * Hydra_Software_Devel/PR22129/6 5/25/11 3:45p rjlewis |
|---|
| 42 | * SWDTV-5441: added support for secondary timer block. Fixed return |
|---|
| 43 | * variables from uint32 to uslong. |
|---|
| 44 | * |
|---|
| 45 | * Hydra_Software_Devel/PR22129/5 3/18/11 6:05p rjlewis |
|---|
| 46 | * SW3548-2526: General clean-up |
|---|
| 47 | * |
|---|
| 48 | * Hydra_Software_Devel/PR22129/4 3/16/11 11:16a rjlewis |
|---|
| 49 | * SW3548-2526: General clean-up. |
|---|
| 50 | * |
|---|
| 51 | * Hydra_Software_Devel/PR22129/3 2/9/11 3:53p rjlewis |
|---|
| 52 | * SW35230-935: update branch so I can continue virtual timer changes |
|---|
| 53 | * before merging back into main. |
|---|
| 54 | * |
|---|
| 55 | * Hydra_Software_Devel/9 8/17/10 5:42p rjlewis |
|---|
| 56 | * SW35230-935: added support for tagged version of create (so we can find |
|---|
| 57 | * where timers are being created). |
|---|
| 58 | * |
|---|
| 59 | * Hydra_Software_Devel/8 8/6/10 5:49p rjlewis |
|---|
| 60 | * SW3548-2526: merge virtual timers into main |
|---|
| 61 | * |
|---|
| 62 | * Hydra_Software_Devel/PR22129/2 11/20/09 4:52p rjlewis |
|---|
| 63 | * SW3548-2526: rewote virtual timers to use separate hardware timers to |
|---|
| 64 | * control free-run versus count-down virtual timers. |
|---|
| 65 | * |
|---|
| 66 | * Hydra_Software_Devel/PR22129/1 1/30/07 11:57a rjlewis |
|---|
| 67 | * PR22129: added support for virtal timers. |
|---|
| 68 | * |
|---|
| 69 | * Hydra_Software_Devel/7 11/20/09 4:24p rjlewis |
|---|
| 70 | * SW7420-391: Added David's tagged to find where timers being used. |
|---|
| 71 | * |
|---|
| 72 | * Hydra_Software_Devel/6 10/27/06 11:04a rjlewis |
|---|
| 73 | * PR25238: functions with no parameters need void as per ANSI. |
|---|
| 74 | * |
|---|
| 75 | * Hydra_Software_Devel/1 11/21/05 2:04p rjlewis |
|---|
| 76 | * PR18214: Initial version. |
|---|
| 77 | * |
|---|
| 78 | ***************************************************************************/ |
|---|
| 79 | |
|---|
| 80 | /*= Module Overview ********************************************************* |
|---|
| 81 | <verbatim> |
|---|
| 82 | |
|---|
| 83 | Overview |
|---|
| 84 | Timer module represents a generalize representatiion of a hardware timer. The |
|---|
| 85 | basic function of a timer is to count. Some timers count up, while others count |
|---|
| 86 | down. If the timer is given a specific time period, a callback routine (ISR level) |
|---|
| 87 | must be provided. This callback will be used for notification that the timer expired. |
|---|
| 88 | |
|---|
| 89 | This interface requires a set of timers to be opened. Once opened, timers from |
|---|
| 90 | the set can be created, started & stopped, and then destroyed as needed. When |
|---|
| 91 | the timers are no longer required they can be closed. Unless an exclusive timer |
|---|
| 92 | is requested, all created timers are virtual. |
|---|
| 93 | |
|---|
| 94 | Design |
|---|
| 95 | The design for BTMR PI API is broken into three parts. |
|---|
| 96 | |
|---|
| 97 | o Part 1 (open/close): |
|---|
| 98 | |
|---|
| 99 | These APIs are used for opening and closing a general BTMR device. |
|---|
| 100 | This defines a set of timers that can be created/destroyed dynamically. |
|---|
| 101 | |
|---|
| 102 | o Part 2 (create/destroy): |
|---|
| 103 | |
|---|
| 104 | These APIs are used for dynamically allocating an available timer. |
|---|
| 105 | Once created, a timer can be started/stopped/read as desired. |
|---|
| 106 | When the timer is no longer needed it can be destroyed. |
|---|
| 107 | |
|---|
| 108 | o Part 3 (start/stop/read): |
|---|
| 109 | |
|---|
| 110 | These APIs are used for starting and stopping a timer. The timer can |
|---|
| 111 | be read to get the current time value. |
|---|
| 112 | |
|---|
| 113 | Usage |
|---|
| 114 | The usage of BTMR involves the following: |
|---|
| 115 | |
|---|
| 116 | * Opening a set of timers |
|---|
| 117 | * Allocating a timer from the set (create) |
|---|
| 118 | * Start timer |
|---|
| 119 | * Stop/Read timer |
|---|
| 120 | * Destroying the timer |
|---|
| 121 | * Closing the set of timers |
|---|
| 122 | |
|---|
| 123 | =================== |
|---|
| 124 | Types of timers and timer types: |
|---|
| 125 | There are three types of timers (exclusive, shared, and virtual) and three timer types (stop-watch/free-run, count-down, |
|---|
| 126 | and periodic). If a timer is needed, first choose the type of timer based on the description and usage notes below. |
|---|
| 127 | Then choose the timer type based on the timer requirements and restrictions (see "enum BTMR_Type" below). |
|---|
| 128 | |
|---|
| 129 | Types of timers: |
|---|
| 130 | exclusive timer: Not virtual and not shared. Can be manipulated with or without this API. |
|---|
| 131 | shared timer: Not virutal and shared by other shared timer requestors. Cannot be started/stopped. |
|---|
| 132 | virtual timer: Cannot request its registers for external manipulation. |
|---|
| 133 | |
|---|
| 134 | Timer types: |
|---|
| 135 | countdown: Counts down from initial value to zero. Stops and calls back when timer reaches zero. |
|---|
| 136 | periodic: Counts down from initial value to zero. Calls back when timer reaches zero and restarts on return from callback. |
|---|
| 137 | stopwatch/free-run: Counts up and wraps at maximum timer value. |
|---|
| 138 | |
|---|
| 139 | =================== |
|---|
| 140 | Exclusive timers: |
|---|
| 141 | Exclusive access can be requested when creating a timer. This will prevent the timer from being virtualized |
|---|
| 142 | or shared with other requestors. You can request the registers of an exclusive timer and control it directly |
|---|
| 143 | by manipulating the registers or by using the supplied API functions. Care should be taken to NOT mix the |
|---|
| 144 | control (i.e. using this API and direct register control) as state information is saved when using the PI. |
|---|
| 145 | If a callback function is registered at the create, this PI will call the supplied callback function when |
|---|
| 146 | the countdown/periodic timer expires even if the timer is controlled externally. |
|---|
| 147 | |
|---|
| 148 | Usage notes: There are a limited set of exclusive timers and the create will fail if no timer is available. |
|---|
| 149 | This type of timer should be used when the timer control is to be done using firmware by a sub-processor |
|---|
| 150 | or DSP that has access to the hardware register space and has need to start and stop the timer at different times. |
|---|
| 151 | The software would create the timer, request the location of the allocated timer's registers, and pass the location |
|---|
| 152 | to the underlying hardware/firmware. If the hardware/firmware has need to read, but not stop/start the timer, then |
|---|
| 153 | a shared timer should be used instead. |
|---|
| 154 | |
|---|
| 155 | =================== |
|---|
| 156 | Shared timers: |
|---|
| 157 | A shared timer (BTMR_Type_eSharedFreeRun) is a stop watch timer that can only be read. That is, it is a free running |
|---|
| 158 | timer that cannot be started or stopped (its always running, counting up). A shared timer will create a (physical) |
|---|
| 159 | timer on the first request for a shared timer and then provide the same timer handle to subsequent shared timer create |
|---|
| 160 | requests. This timer will be started on first create and stopped on the last destroy. Because this timer is shared |
|---|
| 161 | with other requestors it cannot be started or stopped using this PI (only read). Because a shared timer is a stop |
|---|
| 162 | watch timer it cannot be supplied with a callback function. |
|---|
| 163 | |
|---|
| 164 | Usage notes: A shared timer, like a exculsive timer, should be used used when access to the timer's registers is needed |
|---|
| 165 | but when the external hardware/firmware does not need to start and stop the timer. |
|---|
| 166 | |
|---|
| 167 | Warning: The exclusive flag is ignored when using shared timers. This API lets you get the register locations of the |
|---|
| 168 | (one) shared timer. Care should be taken to NOT WRITE those registers (i.e. start/stop the timer). |
|---|
| 169 | Doing so would disrupt the usage of the shared timer and all its users. |
|---|
| 170 | |
|---|
| 171 | =================== |
|---|
| 172 | Virtual timers: |
|---|
| 173 | Unless an exclusive or shared timer is requested, all other timers are considered virtual. |
|---|
| 174 | That is, one physical timer is (allocated) used to create an unlimited set of virtual timers. The only difference |
|---|
| 175 | in functionality is that a virtual timer cannot control the timer outside of this API (i.e. you CANNOT request the |
|---|
| 176 | pointer to it's registers) and additional overhead is needed to process the virtual timer. |
|---|
| 177 | Attempting to get the registers (BTMR_GetTimerRegisters) of a virtual timer will fail. |
|---|
| 178 | |
|---|
| 179 | Usage notes: A virtual timer should be used whenever access to the timer's registers is not needed (i.e. controlled |
|---|
| 180 | outside of this PI). This should be the default. |
|---|
| 181 | |
|---|
| 182 | =================== |
|---|
| 183 | Interrupt Requirements: |
|---|
| 184 | Callback functions are supplied on some timer types at creation time. These callbacks are called in interrupt |
|---|
| 185 | context and MUST not call functions that aren't designated as ISR safe functions. It is okay to restart a |
|---|
| 186 | countdown timer from the callback and to stop a periodic timer from the callback but the timer should NEVER be |
|---|
| 187 | destroyed from the callback. |
|---|
| 188 | |
|---|
| 189 | =================== |
|---|
| 190 | Timer resolutions: |
|---|
| 191 | Timeouts and initial values are supplied in microsecond increments. Reading the timer value using the PI returns a |
|---|
| 192 | value in microsecond increments. Reading an exclusive or shared timer by using the timer registers will be in |
|---|
| 193 | timer tick values (based on 27Mhz clock). A conversion function is supplied in this PI (BTMR_TimerToMicroSeconds). |
|---|
| 194 | |
|---|
| 195 | A timeout or initial value must not exceed: 39,768,215 microseconds (39,768 milli-seconds or 39.7 seconds). |
|---|
| 196 | The function "BTMR_ReadTimerMax" can be used to get this value. |
|---|
| 197 | |
|---|
| 198 | =================== |
|---|
| 199 | Sample Code |
|---|
| 200 | |
|---|
| 201 | volatile bool interrupt = false; |
|---|
| 202 | static void myIntHandler(context *myParam1, int myParam2) { interrupt = true; } |
|---|
| 203 | |
|---|
| 204 | void main( void ) |
|---|
| 205 | { |
|---|
| 206 | BTMR_Handle hTimerDevice; |
|---|
| 207 | BTMR_DeviceSettings deviceSettings; |
|---|
| 208 | BREG_Handle hReg; |
|---|
| 209 | BCHP_Handle hChip; |
|---|
| 210 | BINT_Handle hInt; |
|---|
| 211 | BTMR_TimerHandle hTimer; |
|---|
| 212 | |
|---|
| 213 | // Do other initialization, i.e. for BREG, BCHP, BINT, etc. |
|---|
| 214 | |
|---|
| 215 | // Now, ready to setup the BTMR |
|---|
| 216 | |
|---|
| 217 | // Get defaults and make any changes required |
|---|
| 218 | BTMR_GetDefaultDeviceSettings(&deviceSettings); |
|---|
| 219 | deviceSettings.timerMask = 0; // defines the timers to reserve for external use (none) |
|---|
| 220 | |
|---|
| 221 | BTMR_Open(&hTimerDevice, hChip, hReg, hInt, &deviceSettings); |
|---|
| 222 | |
|---|
| 223 | // Timers are now ready for use. |
|---|
| 224 | |
|---|
| 225 | // Create and start a timer. |
|---|
| 226 | { |
|---|
| 227 | BTMR_TimerSettings timerSettings = { BTMR_Type_eCountDown, (BTMR_CallbackFunc)myIntHandler, NULL, 0, false }; |
|---|
| 228 | BTMR_CreateTimer(hTimerDevice, &hTimer, &timerSettings); |
|---|
| 229 | BTMR_StartTimer(hTimer, 1000); // interrupt in 1000 usec or 1 millisecond |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | while (!interrupt) ; // wait for timer to go off! |
|---|
| 233 | |
|---|
| 234 | BTMR_StopTimer(hTimer); |
|---|
| 235 | BTMR_DestroyTimer(hTimer); |
|---|
| 236 | |
|---|
| 237 | BTMR_Close(hTimerDevice); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | </verbatim> |
|---|
| 241 | ***************************************************************************/ |
|---|
| 242 | |
|---|
| 243 | #ifndef BTMR_H__ |
|---|
| 244 | #define BTMR_H__ |
|---|
| 245 | |
|---|
| 246 | #include "bchp.h" |
|---|
| 247 | #include "bint.h" |
|---|
| 248 | #include "berr_ids.h" |
|---|
| 249 | |
|---|
| 250 | /*************************************************************************** |
|---|
| 251 | Summary: |
|---|
| 252 | Error Codes specific to BTMR. |
|---|
| 253 | |
|---|
| 254 | Description: |
|---|
| 255 | These define the set of error codes unique to the TMR functions. |
|---|
| 256 | This list does not limit the returned error codes (also uses general errors defined elsewhere). |
|---|
| 257 | |
|---|
| 258 | ****************************************************************************/ |
|---|
| 259 | #define BTMR_ERR_TODO BERR_MAKE_CODE(BERR_TMR_ID, 0) |
|---|
| 260 | #define BTMR_ERR_NO_TIMERS_AVAILABLE BERR_MAKE_CODE(BERR_TMR_ID, 1) /* request for a timer when none available */ |
|---|
| 261 | #define BTMR_ERR_EXCLUSIVE_OPERATION BERR_MAKE_CODE(BERR_TMR_ID, 2) /* exclusive operation selected for non-exclusive device */ |
|---|
| 262 | #define BTMR_ERR_EXCLUSIVE_TIMER BERR_MAKE_CODE(BERR_TMR_ID, 3) /* non-exclusive operation selected for exclusive device */ |
|---|
| 263 | #define BTMR_ERR_ALREADY_STARTED BERR_MAKE_CODE(BERR_TMR_ID, 4) /* request to start a running timer */ |
|---|
| 264 | #define BTMR_ERR_ALREADY_STOPPED BERR_MAKE_CODE(BERR_TMR_ID, 5) /* request to stop a stopped timer (if enabled) */ |
|---|
| 265 | #define BTMR_ERR_FREE_RUN_TIMER BERR_MAKE_CODE(BERR_TMR_ID, 6) /* start/stop operation selected on free-run device */ |
|---|
| 266 | #define BTMR_ERR_NO_TIMEOUT_GIVEN BERR_MAKE_CODE(BERR_TMR_ID, 7) /* attempt to start count-down timer with no time value */ |
|---|
| 267 | #define BTMR_ERR_TIMEOUT_TOO_SMALL BERR_MAKE_CODE(BERR_TMR_ID, 8) /* attempt to create a periodic timer with too small timeout */ |
|---|
| 268 | #define BTMR_ERR_TIMEOUT_TOO_LARGE BERR_MAKE_CODE(BERR_TMR_ID, 9) /* attempt to create a count-down or periodic timer with too large a timeout */ |
|---|
| 269 | #define BTMR_ERR_DELETE_FROM_ISR BERR_MAKE_CODE(BERR_TMR_ID, 10)/* attempt to delete a count-down or periodic timer from callback routine */ |
|---|
| 270 | |
|---|
| 271 | /*************************************************************************** |
|---|
| 272 | Summary: |
|---|
| 273 | The handle for Timer device. |
|---|
| 274 | |
|---|
| 275 | Description: |
|---|
| 276 | An opaque handle for BTMR device. |
|---|
| 277 | This is returned on a successful Open call and is passed into the |
|---|
| 278 | Create and Close calls as input parameter. |
|---|
| 279 | |
|---|
| 280 | See Also: |
|---|
| 281 | BTMR_Open(), BTMR_Close(), BTMR_CreateTimer. |
|---|
| 282 | |
|---|
| 283 | ****************************************************************************/ |
|---|
| 284 | typedef struct BTMR_P_Context *BTMR_Handle; |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | /*************************************************************************** |
|---|
| 288 | Summary: |
|---|
| 289 | The handle for Timer module. |
|---|
| 290 | |
|---|
| 291 | Description: |
|---|
| 292 | An opaque handle for BTMR timer (and allocated timer). |
|---|
| 293 | This is returned on a successful Create call and is passed in to the |
|---|
| 294 | Start, Stop, Read, and Destroy requests as input parameter. |
|---|
| 295 | |
|---|
| 296 | See Also: |
|---|
| 297 | BTMR_CreateTimer(), BTMR_DestroyTimer(), BTMR_StartTimer(), BTMR_StopTimer(), |
|---|
| 298 | BTMR_ReadTimer(), BTMR_GetDefaultTimerSettings(). |
|---|
| 299 | |
|---|
| 300 | ****************************************************************************/ |
|---|
| 301 | typedef struct BTMR_P_TimerContext *BTMR_TimerHandle; |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | /*************************************************************************** |
|---|
| 305 | Summary: |
|---|
| 306 | The format for the callback. |
|---|
| 307 | |
|---|
| 308 | Description: |
|---|
| 309 | When a timer is registered with a callback, that callback will be called |
|---|
| 310 | when the timer interrupts. The callback will occur in interrupt context. |
|---|
| 311 | This is passed in as past of the Timer Settings at creation time. |
|---|
| 312 | Context that includes the timer handle should be passed (pParm1) to the |
|---|
| 313 | callback if stopping the timer in the callback is desired. |
|---|
| 314 | |
|---|
| 315 | See Also: |
|---|
| 316 | BTMR_CreateTimer(), BTMR_StopTimer_isr(). |
|---|
| 317 | |
|---|
| 318 | ****************************************************************************/ |
|---|
| 319 | typedef void (*BTMR_CallbackFunc)( |
|---|
| 320 | void *pParm1, /* [in] First parameter used to define context */ |
|---|
| 321 | int parm2 /* [in] Second parameter used to define context */ |
|---|
| 322 | ); |
|---|
| 323 | |
|---|
| 324 | /*************************************************************************** |
|---|
| 325 | Summary: |
|---|
| 326 | The mask that designates which timers are not to be used. |
|---|
| 327 | |
|---|
| 328 | Description: |
|---|
| 329 | This defines the timer set (set of timers) information. |
|---|
| 330 | Legacy code exists that uses a particular timer outside of this interface. |
|---|
| 331 | The 'timerMask' mask defines which timers should not be included in the list of timers |
|---|
| 332 | controlled by this API. |
|---|
| 333 | Virtual timers require the use of two physical timers. Setting preallocUtilTimers |
|---|
| 334 | to true forces these timers to be allocated statically at the Open call. Setting it to |
|---|
| 335 | false (default) will allocate these timers dynamically on first request for a virtual timer. |
|---|
| 336 | The base register value defines the alternate set of timers to be used by the returned handle. |
|---|
| 337 | The interrupt number defines the level two interrupt that is registered for the alternate set |
|---|
| 338 | of timers. This flags the driver into handling the individual timers as level 3 interrupts. |
|---|
| 339 | |
|---|
| 340 | Usage: |
|---|
| 341 | When using an alternate timer block the following settings should be used to define the |
|---|
| 342 | alternate timer block. It is assumed that if the interrupt number parameter is defined that |
|---|
| 343 | the specified interrupt will be used as a level two interrupt. |
|---|
| 344 | <verbatim> |
|---|
| 345 | BTMR_GetDefaultDeviceSettings(&deviceSettings); |
|---|
| 346 | deviceSettings.baseRegister = BCHP_DVP_HR_TMR_TIMER_IS; |
|---|
| 347 | deviceSettings.interruptNumber = BCHP_INT_ID_TMR_IRQ_0; |
|---|
| 348 | BTMR_Open(&hTimerDevice, hChip, hReg, hInt, &deviceSettings); |
|---|
| 349 | </verbatim> |
|---|
| 350 | |
|---|
| 351 | See Also: |
|---|
| 352 | BTMR_Open(). |
|---|
| 353 | |
|---|
| 354 | ****************************************************************************/ |
|---|
| 355 | typedef struct BTMR_DeviceSettings { |
|---|
| 356 | unsigned timerMask; /* Bitmask which specifies which timers are already in use by outside code. */ |
|---|
| 357 | /* This allows OS or customer specific code to use the hardware timers outside of this module. |
|---|
| 358 | To prevent the TMR module from using a timer set the corresponding bit to 1. |
|---|
| 359 | For example, to exclude timer number 3 set the timerMask value to 0x8. */ |
|---|
| 360 | bool preallocUtilTimers; /* Option to pre-allocate hardware timers required for use with virtual timers (default is false). */ |
|---|
| 361 | |
|---|
| 362 | /* The following parameters are used to support an alternate TMR block. */ |
|---|
| 363 | uint32_t baseRegister; /* This is the address of the base register for this timer block (default is BCHP_TIMER_REG_START). */ |
|---|
| 364 | unsigned interruptNumber; /* This is the level 2 interrupt number that should be used to register for a callback interrupt. */ |
|---|
| 365 | |
|---|
| 366 | } BTMR_DeviceSettings; |
|---|
| 367 | |
|---|
| 368 | #define BTMR_DefaultSettings BTMR_DeviceSettings /* depricated name -- delete this later */ |
|---|
| 369 | #define BTMR_GetDefaultSettings BTMR_GetDefaultDeviceSettings /* depricated name -- delete this later */ |
|---|
| 370 | |
|---|
| 371 | /*************************************************************************** |
|---|
| 372 | Summary: |
|---|
| 373 | Enumeration for timer mode. |
|---|
| 374 | |
|---|
| 375 | Description: |
|---|
| 376 | This enumeration defines the timer mode. |
|---|
| 377 | |
|---|
| 378 | See Also: |
|---|
| 379 | BTMR_CreateTimer(). |
|---|
| 380 | |
|---|
| 381 | ****************************************************************************/ |
|---|
| 382 | typedef enum BTMR_Type { |
|---|
| 383 | BTMR_Type_eCountDown, /* Timer starts at a specified value and counts down to zero. |
|---|
| 384 | Must be restarted once triggered (if desired). |
|---|
| 385 | Restrictions: |
|---|
| 386 | The initial value when starting the timer must not be zero! |
|---|
| 387 | This timer type requires a call-back function (called when timer expires). |
|---|
| 388 | */ |
|---|
| 389 | BTMR_Type_ePeriodic, /* Like countdown but does not need to be restarted once triggered. |
|---|
| 390 | Will continue to fire (callback) until stopped at interval defined by initial value. |
|---|
| 391 | This timer type requires a call-back function (called each time timer expires). |
|---|
| 392 | Restrictions: |
|---|
| 393 | This timer must be supplied a timeout greater than one millisecond (a smaller |
|---|
| 394 | values could hang the system). |
|---|
| 395 | */ |
|---|
| 396 | BTMR_Type_eStopWatch, /* Free running stopwatch. |
|---|
| 397 | Once started it continues to count up (and wraps) until stopped. |
|---|
| 398 | The initial value defines the timers starting value. |
|---|
| 399 | Restrictions: |
|---|
| 400 | No callback function is allowed! |
|---|
| 401 | */ |
|---|
| 402 | BTMR_Type_eSharedFreeRun /* This is special case of the free running stopwatch timer. |
|---|
| 403 | Multiple creates of this timer type use a single shared timer. |
|---|
| 404 | Restrictions: |
|---|
| 405 | Once created it cannot be stopped or started (only read and destroyed). |
|---|
| 406 | Destroying this timer does NOT affect the other shared requestors. |
|---|
| 407 | No callback function is allowed! |
|---|
| 408 | */ |
|---|
| 409 | } BTMR_Type; |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | /*************************************************************************** |
|---|
| 413 | Summary: |
|---|
| 414 | Structure for Timer Information. |
|---|
| 415 | |
|---|
| 416 | Description: |
|---|
| 417 | This defines the timer information. |
|---|
| 418 | Callback function cannot be supplied when timer type is StopWatch (free-run)! |
|---|
| 419 | The two parameters (pParm1 & pParm2) will be passed to the callback function when the timer triggers. |
|---|
| 420 | |
|---|
| 421 | In exclusive mode, the timer is not to be virtualized (each create uses hardware timer). |
|---|
| 422 | |
|---|
| 423 | See Also: |
|---|
| 424 | BTMR_CreateTimer(), BTMR_GetTimerSettings(), BTMR_GetTimerRegisters(). |
|---|
| 425 | |
|---|
| 426 | ****************************************************************************/ |
|---|
| 427 | typedef struct BTMR_TimerSettings { |
|---|
| 428 | BTMR_Type type; /* Timer type. */ |
|---|
| 429 | BTMR_CallbackFunc cb_isr; /* Callback used for count-down type timers (can only call '_isr' functions from callback). */ |
|---|
| 430 | void *pParm1; /* Context parameter returned when callback is executed. */ |
|---|
| 431 | int parm2; /* Context parameter returned when callback is executed. */ |
|---|
| 432 | bool exclusive; /* Requestor wants exclusive access (not virtual and not shared). */ |
|---|
| 433 | } BTMR_TimerSettings; |
|---|
| 434 | |
|---|
| 435 | #define BTMR_Settings BTMR_TimerSettings /* depricated name -- delete this later */ |
|---|
| 436 | |
|---|
| 437 | /*************************************************************************** |
|---|
| 438 | Summary: |
|---|
| 439 | Structure for Timer Register Information. |
|---|
| 440 | |
|---|
| 441 | Description: |
|---|
| 442 | This defines the offsets returned on a register request when using exclusive mode. |
|---|
| 443 | These can then be used to manually control the timer. |
|---|
| 444 | |
|---|
| 445 | See Also: |
|---|
| 446 | BTMR_GetTimerRegisters(). |
|---|
| 447 | |
|---|
| 448 | ****************************************************************************/ |
|---|
| 449 | typedef struct BTMR_TimerRegisters { |
|---|
| 450 | uint32_t control; /* chip offset to register for timer control */ |
|---|
| 451 | uint32_t status; /* chip offset to register for timer status */ |
|---|
| 452 | } BTMR_TimerRegisters; |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | /*************************************************************************** |
|---|
| 456 | Summary: |
|---|
| 457 | Definition for minimum periodic timeout value. |
|---|
| 458 | |
|---|
| 459 | Description: |
|---|
| 460 | If a periodic timer is created with too small a timeout value it could easily cause a system hang! |
|---|
| 461 | The timer would expire and before the callback function had finished the timer expires again (calling the callback ...) |
|---|
| 462 | |
|---|
| 463 | See Also: |
|---|
| 464 | BTMR_CreateTimer(), BTMR_ERR_TIMEOUT_TOO_SMALL |
|---|
| 465 | |
|---|
| 466 | ****************************************************************************/ |
|---|
| 467 | #define BTMR_MINIMUM_TIMEOUT 100 /* (microseconds) minimum timeout value for periodic timer */ |
|---|
| 468 | |
|---|
| 469 | #ifdef __cplusplus |
|---|
| 470 | extern "C" { |
|---|
| 471 | #endif |
|---|
| 472 | |
|---|
| 473 | /*************************************************************************** |
|---|
| 474 | Summary: |
|---|
| 475 | This function opens and creates the base timer handle. |
|---|
| 476 | |
|---|
| 477 | Description: |
|---|
| 478 | This function is responsible for creating a set of timers for general purpose useage. |
|---|
| 479 | This function will register for the timer interrupts. |
|---|
| 480 | This is typically created at the system level and the resulting handle is passed to specific modules. |
|---|
| 481 | |
|---|
| 482 | Returns: |
|---|
| 483 | A valid handle that can be used to create/close on success. |
|---|
| 484 | Status of the request. |
|---|
| 485 | |
|---|
| 486 | See Also: |
|---|
| 487 | BTMR_Close(), BTMR_CreateTimer(). |
|---|
| 488 | |
|---|
| 489 | ****************************************************************************/ |
|---|
| 490 | BERR_Code BTMR_Open( |
|---|
| 491 | BTMR_Handle *phDevice, /* [out] Return device handle on successful open */ |
|---|
| 492 | BCHP_Handle hChip, /* [in] Chip handle */ |
|---|
| 493 | BREG_Handle hRegister, /* [in] Register handle */ |
|---|
| 494 | BINT_Handle hInterrupt, /* [in] Interrupt handle */ |
|---|
| 495 | const BTMR_DeviceSettings *pOpenSettings /* [in] Settings */ |
|---|
| 496 | ); |
|---|
| 497 | |
|---|
| 498 | /*************************************************************************** |
|---|
| 499 | Summary: |
|---|
| 500 | This function closes a previously opened timer handle. |
|---|
| 501 | |
|---|
| 502 | Description: |
|---|
| 503 | This function is responsible for releasing a previously opened set of timers. |
|---|
| 504 | This function will deregister for the interrupts for these timers. |
|---|
| 505 | This is used th undo the actions of the open. |
|---|
| 506 | Any valid timers (created but not yet destroyed) will be destroyed. |
|---|
| 507 | |
|---|
| 508 | Returns: |
|---|
| 509 | The status of the operation. |
|---|
| 510 | |
|---|
| 511 | See Also: |
|---|
| 512 | BTMR_Open(). |
|---|
| 513 | |
|---|
| 514 | ****************************************************************************/ |
|---|
| 515 | BERR_Code BTMR_Close( |
|---|
| 516 | BTMR_Handle hDevice /* [in] Device handle */ |
|---|
| 517 | ); |
|---|
| 518 | |
|---|
| 519 | /*************************************************************************** |
|---|
| 520 | Summary: |
|---|
| 521 | This function returns the default device settings. |
|---|
| 522 | |
|---|
| 523 | Description: |
|---|
| 524 | This function is responsible for providing the default values for the device settings. |
|---|
| 525 | |
|---|
| 526 | Returns: |
|---|
| 527 | A copy of the default settings and the status of the request. |
|---|
| 528 | |
|---|
| 529 | See Also: |
|---|
| 530 | BTMR_Open(). |
|---|
| 531 | |
|---|
| 532 | ****************************************************************************/ |
|---|
| 533 | BERR_Code BTMR_GetDefaultDeviceSettings( |
|---|
| 534 | BTMR_DeviceSettings *pSettings /* [out] Default settings */ |
|---|
| 535 | ); |
|---|
| 536 | |
|---|
| 537 | /*************************************************************************** |
|---|
| 538 | Summary: |
|---|
| 539 | This function creates a timer instance. |
|---|
| 540 | |
|---|
| 541 | Description: |
|---|
| 542 | This function is responsible for creating a timer for general or exclusive |
|---|
| 543 | use. The timer is defined by the settings passed in. Once created, the |
|---|
| 544 | timer can be started, stopped, and read until no longer needed (destroyed). |
|---|
| 545 | This function will call the supplied callback function when the timer expires. |
|---|
| 546 | |
|---|
| 547 | Returns: |
|---|
| 548 | A timer handle that can be used to control the allocated timer on successful creation. |
|---|
| 549 | The status of request. |
|---|
| 550 | |
|---|
| 551 | See Also: |
|---|
| 552 | BTMR_Open(), BTMR_DestroyTimer(). |
|---|
| 553 | |
|---|
| 554 | ****************************************************************************/ |
|---|
| 555 | BERR_Code BTMR_CreateTimer_tagged( |
|---|
| 556 | BTMR_Handle hDevice, /* [in] Device handle */ |
|---|
| 557 | BTMR_TimerHandle *phTimer, /* [out] Returns timer handle */ |
|---|
| 558 | const BTMR_TimerSettings *pSettings, /* [in] Timer settings */ |
|---|
| 559 | const char *file, int line |
|---|
| 560 | ); |
|---|
| 561 | #define BTMR_CreateTimer(d,t,s) BTMR_CreateTimer_tagged(d,t,s,__FILE__,__LINE__) |
|---|
| 562 | |
|---|
| 563 | /*************************************************************************** |
|---|
| 564 | Summary: |
|---|
| 565 | This function destroys a timer instance. |
|---|
| 566 | |
|---|
| 567 | Description: |
|---|
| 568 | This function is responsible for undoing the effect of creating a new timer. |
|---|
| 569 | If the indicated timer is still running, it will be stopped before it is destroyed. |
|---|
| 570 | |
|---|
| 571 | Returns: |
|---|
| 572 | The status of request. |
|---|
| 573 | |
|---|
| 574 | See Also: |
|---|
| 575 | BTMR_CreateTimer(). |
|---|
| 576 | |
|---|
| 577 | ****************************************************************************/ |
|---|
| 578 | BERR_Code BTMR_DestroyTimer_tagged( |
|---|
| 579 | BTMR_TimerHandle hTimer, /* [in] Timer handle */ |
|---|
| 580 | const char *file, int line |
|---|
| 581 | ); |
|---|
| 582 | #define BTMR_DestroyTimer(d) BTMR_DestroyTimer_tagged(d,__FILE__,__LINE__) |
|---|
| 583 | |
|---|
| 584 | /*************************************************************************** |
|---|
| 585 | Summary: |
|---|
| 586 | This function returns a set of defaults for the timer settings. |
|---|
| 587 | |
|---|
| 588 | Description: |
|---|
| 589 | This is used to initialize the settings parameter used to create a timer. |
|---|
| 590 | After calling this, the settings should be modified to reflect the required |
|---|
| 591 | functional aspects for the new timer. |
|---|
| 592 | |
|---|
| 593 | Returns: |
|---|
| 594 | A set of default settings for a timer. |
|---|
| 595 | The status of request. |
|---|
| 596 | |
|---|
| 597 | See Also: |
|---|
| 598 | BTMR_CreateTimer(). |
|---|
| 599 | |
|---|
| 600 | ****************************************************************************/ |
|---|
| 601 | BERR_Code BTMR_GetDefaultTimerSettings( |
|---|
| 602 | BTMR_TimerSettings *pSettings /* [out] A set of defaults for the settings parameter to create */ |
|---|
| 603 | ); |
|---|
| 604 | |
|---|
| 605 | /*************************************************************************** |
|---|
| 606 | Summary: |
|---|
| 607 | Returns a structure that supplies the registers for this timer. |
|---|
| 608 | |
|---|
| 609 | Description: |
|---|
| 610 | When a timer is created in exclusive mode, it will not be controlled by timer start/stop/read functions. |
|---|
| 611 | Instead, it will be controlled externally. If a callback function is supplied, then, when an interrupt |
|---|
| 612 | for that timer is generated, the call back function will be called. |
|---|
| 613 | |
|---|
| 614 | Note: |
|---|
| 615 | This function can also be used on a shared timer (BTMR_Type_eSharedFreeRun). Because this timer is |
|---|
| 616 | shared by other users, care should be taken to NOT start or stop this timer. If you get the registers |
|---|
| 617 | and stop the timer other user's code will break. This was done because of the limited number of timers |
|---|
| 618 | available. |
|---|
| 619 | |
|---|
| 620 | Returns: |
|---|
| 621 | A structure that contains the offsets for the registers for this timer. |
|---|
| 622 | The status of request. |
|---|
| 623 | |
|---|
| 624 | See Also: |
|---|
| 625 | BTMR_CreateTimer(), BTMR_DestroyTimer(). |
|---|
| 626 | |
|---|
| 627 | ****************************************************************************/ |
|---|
| 628 | BERR_Code BTMR_GetTimerRegisters( |
|---|
| 629 | BTMR_TimerHandle hTimer, /* [in] Timer handle */ |
|---|
| 630 | BTMR_TimerRegisters *pRegisters /* [out] Pointer to a set of registers to control this timer. */ |
|---|
| 631 | ); |
|---|
| 632 | |
|---|
| 633 | /*************************************************************************** |
|---|
| 634 | Summary: |
|---|
| 635 | This function returns the current timer settings. |
|---|
| 636 | |
|---|
| 637 | Description: |
|---|
| 638 | This function is responsible for providing a copy of the settings used to create the timer. |
|---|
| 639 | |
|---|
| 640 | Returns: |
|---|
| 641 | The current values being used by the timer. |
|---|
| 642 | The status of request. |
|---|
| 643 | |
|---|
| 644 | See Also: |
|---|
| 645 | BTMR_CreateTimer(). |
|---|
| 646 | |
|---|
| 647 | ****************************************************************************/ |
|---|
| 648 | BERR_Code BTMR_GetTimerSettings( |
|---|
| 649 | BTMR_TimerHandle hTimer, /* [in] Timer handle */ |
|---|
| 650 | BTMR_TimerSettings *pSettings /* [out] Current timer settings */ |
|---|
| 651 | ); |
|---|
| 652 | |
|---|
| 653 | /*************************************************************************** |
|---|
| 654 | Summary: |
|---|
| 655 | This function starts a timer with an initial value. |
|---|
| 656 | |
|---|
| 657 | Description: |
|---|
| 658 | This function is responsible for starting a previously created timer. |
|---|
| 659 | This function cannot be called on timers that were created exclusive or shared. |
|---|
| 660 | |
|---|
| 661 | If the timer is a countdown or perodic timer, the counter will start at the initial value and count-down to zero. |
|---|
| 662 | If the timer is a stopwatch or free run it will start at the initial value and count in the positive direction and wrap at the zero point. |
|---|
| 663 | This function cannot be called on timers that were created shared. |
|---|
| 664 | The initial value is in microseconds and cannot be zero for count-down timer types. |
|---|
| 665 | |
|---|
| 666 | Returns: |
|---|
| 667 | The status of request. |
|---|
| 668 | |
|---|
| 669 | See Also: |
|---|
| 670 | BTMR_CreateTimer(), BTMR_StopTimer(), BTMR_StartTimer_isr(). |
|---|
| 671 | |
|---|
| 672 | ****************************************************************************/ |
|---|
| 673 | BERR_Code BTMR_StartTimer( |
|---|
| 674 | BTMR_TimerHandle hTimer, /* [in] Timer handle */ |
|---|
| 675 | unsigned initialValue /* [in] Starting time value in microseconds */ |
|---|
| 676 | ); |
|---|
| 677 | |
|---|
| 678 | /*************************************************************************** |
|---|
| 679 | Summary: |
|---|
| 680 | This function starts a timer with an initial value from interrupt context. |
|---|
| 681 | |
|---|
| 682 | Description: |
|---|
| 683 | This is identical to the BTMR_StartTimer function but is called from interrupt context. |
|---|
| 684 | This includes being called from timer callback functions (allowed). |
|---|
| 685 | This function cannot be called on timers that were created shared. |
|---|
| 686 | The same calling rules apply as BTMR_StartTimer. |
|---|
| 687 | |
|---|
| 688 | Returns: |
|---|
| 689 | The status of request. |
|---|
| 690 | |
|---|
| 691 | See Also: |
|---|
| 692 | BTMR_CreateTimer(), BTMR_StartTimer(). |
|---|
| 693 | |
|---|
| 694 | ****************************************************************************/ |
|---|
| 695 | BERR_Code BTMR_StartTimer_isr( |
|---|
| 696 | BTMR_TimerHandle hTimer, /* [in] Timer handle */ |
|---|
| 697 | unsigned initialValue /* [in] Starting time value in microseconds */ |
|---|
| 698 | ); |
|---|
| 699 | |
|---|
| 700 | /*************************************************************************** |
|---|
| 701 | Summary: |
|---|
| 702 | This function stops a timer. |
|---|
| 703 | |
|---|
| 704 | Description: |
|---|
| 705 | This function is responsible for stopping a previously started timer. |
|---|
| 706 | This function cannot be called on timers that were created shared. |
|---|
| 707 | |
|---|
| 708 | Returns: |
|---|
| 709 | The status of request. |
|---|
| 710 | |
|---|
| 711 | See Also: |
|---|
| 712 | BTMR_CreateTimer(), BTMR_StartTimer(), BTMR_StopTimer_isr(). |
|---|
| 713 | |
|---|
| 714 | ****************************************************************************/ |
|---|
| 715 | BERR_Code BTMR_StopTimer( |
|---|
| 716 | BTMR_TimerHandle hTimer /* [in] Timer handle */ |
|---|
| 717 | ); |
|---|
| 718 | |
|---|
| 719 | |
|---|
| 720 | /*************************************************************************** |
|---|
| 721 | Summary: |
|---|
| 722 | This function stops a timer from interrupt context. |
|---|
| 723 | |
|---|
| 724 | Description: |
|---|
| 725 | This is identical to the BTMR_StopTimer function but is called from interrupt context. |
|---|
| 726 | This includes being called from timer callback functions (allowed). |
|---|
| 727 | This function cannot be called on timers that were created shared. |
|---|
| 728 | The same calling rules apply as BTMR_StopTimer. |
|---|
| 729 | |
|---|
| 730 | Returns: |
|---|
| 731 | The status of request. |
|---|
| 732 | |
|---|
| 733 | See Also: |
|---|
| 734 | BTMR_CreateTimer(), BTMR_StopTimer(). |
|---|
| 735 | |
|---|
| 736 | ****************************************************************************/ |
|---|
| 737 | BERR_Code BTMR_StopTimer_isr( |
|---|
| 738 | BTMR_TimerHandle hTimer /* [in] Timer handle */ |
|---|
| 739 | ); |
|---|
| 740 | |
|---|
| 741 | |
|---|
| 742 | /*************************************************************************** |
|---|
| 743 | Summary: |
|---|
| 744 | This function reads the current timer value. |
|---|
| 745 | |
|---|
| 746 | Description: |
|---|
| 747 | This function is responsible for returning the current value of the timer. |
|---|
| 748 | If reading a stopped timer then the last value of the timer before the |
|---|
| 749 | stop will be returned. An exception to that is a CountDown timer that |
|---|
| 750 | has expired -- it will return 0. |
|---|
| 751 | The timer must be started before valid values are returned. |
|---|
| 752 | |
|---|
| 753 | Returns: |
|---|
| 754 | The timer value. |
|---|
| 755 | The status of request. |
|---|
| 756 | |
|---|
| 757 | See Also: |
|---|
| 758 | BTMR_CreateTimer(). |
|---|
| 759 | |
|---|
| 760 | ****************************************************************************/ |
|---|
| 761 | BERR_Code BTMR_ReadTimer( |
|---|
| 762 | BTMR_TimerHandle hTimer, /* [in] Timer handle */ |
|---|
| 763 | unsigned *pValue /* [out] Current timer value in microseconds */ |
|---|
| 764 | ); |
|---|
| 765 | |
|---|
| 766 | /*************************************************************************** |
|---|
| 767 | Summary: |
|---|
| 768 | This function reads the current timer value from interrupt context. |
|---|
| 769 | |
|---|
| 770 | Description: |
|---|
| 771 | This is identical to the BTMR_ReadTimer function but is called from interrupt context. |
|---|
| 772 | This includes being called from timer callback functions (allowed). |
|---|
| 773 | The same calling rules apply as BTMR_ReadTimer. |
|---|
| 774 | |
|---|
| 775 | Returns: |
|---|
| 776 | The timer value. |
|---|
| 777 | The status of request. |
|---|
| 778 | |
|---|
| 779 | See Also: |
|---|
| 780 | BTMR_CreateTimer(). |
|---|
| 781 | |
|---|
| 782 | ****************************************************************************/ |
|---|
| 783 | BERR_Code BTMR_ReadTimer_isr( |
|---|
| 784 | BTMR_TimerHandle hTimer, /* [in] Timer handle */ |
|---|
| 785 | unsigned *pValue /* [out] Current timer value in microseconds */ |
|---|
| 786 | ); |
|---|
| 787 | |
|---|
| 788 | /*************************************************************************** |
|---|
| 789 | Summary: |
|---|
| 790 | This function returns the maximum timer value. |
|---|
| 791 | |
|---|
| 792 | Description: |
|---|
| 793 | This function is responsible for returning the maximum value that the |
|---|
| 794 | timer will reach before wrapping back to zero. The value returned is |
|---|
| 795 | in microseconds. This should be used to perform timer wrap calculations. |
|---|
| 796 | |
|---|
| 797 | Returns: |
|---|
| 798 | The maximum timer value. |
|---|
| 799 | |
|---|
| 800 | See Also: |
|---|
| 801 | BTMR_CreateTimer(). |
|---|
| 802 | |
|---|
| 803 | ****************************************************************************/ |
|---|
| 804 | unsigned BTMR_ReadTimerMax(void); |
|---|
| 805 | |
|---|
| 806 | #ifdef __cplusplus |
|---|
| 807 | } |
|---|
| 808 | #endif |
|---|
| 809 | |
|---|
| 810 | #endif /* BTMR_H__ */ |
|---|
| 811 | |
|---|