| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2004-2010, 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: bastmlib_clock_reference.c $ |
|---|
| 11 | * $brcm_Revision: Hydra_Software_Devel/5 $ |
|---|
| 12 | * $brcm_Date: 7/14/10 7:44p $ |
|---|
| 13 | * |
|---|
| 14 | * Revision History: |
|---|
| 15 | * |
|---|
| 16 | * $brcm_Log: /magnum/syslib/astmlib/noarch/bastmlib_clock_reference.c $ |
|---|
| 17 | * |
|---|
| 18 | * Hydra_Software_Devel/5 7/14/10 7:44p bandrews |
|---|
| 19 | * SW3548-1161: expose default configs as public |
|---|
| 20 | * |
|---|
| 21 | * Hydra_Software_Devel/4 7/17/09 6:56p bandrews |
|---|
| 22 | * PR49215: playback support |
|---|
| 23 | * |
|---|
| 24 | * Hydra_Software_Devel/3 11/20/08 6:16p bandrews |
|---|
| 25 | * PR49489: Add debug messages; ensure no events are recorded if stopped; |
|---|
| 26 | * clear event queue on start; only schedule settling timers if started |
|---|
| 27 | * |
|---|
| 28 | * Hydra_Software_Devel/2 4/9/08 2:55p bandrews |
|---|
| 29 | * PR41524: Fixed to use struct BASTMlib_ClockReference_Impl |
|---|
| 30 | * |
|---|
| 31 | * Hydra_Software_Devel/1 3/24/08 3:08p bandrews |
|---|
| 32 | * PR40865: Fixed |
|---|
| 33 | * |
|---|
| 34 | * Hydra_Software_Devel/2 2/15/08 10:00p bandrews |
|---|
| 35 | * PR36148: Updated ASTM based on reviews |
|---|
| 36 | * |
|---|
| 37 | * Hydra_Software_Devel/1 1/25/08 9:21p bandrews |
|---|
| 38 | * PR36148: Updated based on simulation |
|---|
| 39 | ***************************************************************************/ |
|---|
| 40 | |
|---|
| 41 | #include "bstd.h" |
|---|
| 42 | #include "bastmlib_priv.h" |
|---|
| 43 | #include "bastmlib_clock_reference.h" |
|---|
| 44 | #include "bastmlib_clock_reference_priv.h" |
|---|
| 45 | |
|---|
| 46 | BDBG_MODULE(astmlib); |
|---|
| 47 | |
|---|
| 48 | void BASTMlib_ClockReference_Create(BASTMlib_ClockReference_Handle * phReference) |
|---|
| 49 | { |
|---|
| 50 | BASTMlib_ClockReference_Handle hReference; |
|---|
| 51 | |
|---|
| 52 | BDBG_ENTER(BASTMlib_ClockReference_Create); |
|---|
| 53 | |
|---|
| 54 | hReference = (BASTMlib_ClockReference_Handle)BKNI_Malloc(sizeof(struct BASTMlib_ClockReference_Impl)); |
|---|
| 55 | |
|---|
| 56 | BDBG_ASSERT(hReference); |
|---|
| 57 | BKNI_Memset(hReference, 0, sizeof(struct BASTMlib_ClockReference_Impl)); |
|---|
| 58 | BASTMlib_ClockReference_GetDefaultConfig(&hReference->sConfig); |
|---|
| 59 | |
|---|
| 60 | BDBG_MSG(("clock reference initial configuration:")); |
|---|
| 61 | BDBG_MSG((" min time between events: %u ms", hReference->sConfig.uiMinimumTimeBetweenEvents)); |
|---|
| 62 | BDBG_MSG((" clock reference domain: %u Hz", hReference->sConfig.eClockReferenceDomain)); |
|---|
| 63 | BDBG_MSG((" deviation threshold: %u ticks", hReference->sConfig.uiDeviationThreshold)); |
|---|
| 64 | BDBG_MSG((" deviant count threshold: %u events", hReference->sConfig.uiDeviantCountThreshold)); |
|---|
| 65 | BDBG_MSG((" ideal count threshold: %u events", hReference->sConfig.uiIdealCountThreshold)); |
|---|
| 66 | |
|---|
| 67 | *phReference = hReference; |
|---|
| 68 | |
|---|
| 69 | BDBG_LEAVE(BASTMlib_ClockReference_Create); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void BASTMlib_ClockReference_Destroy(BASTMlib_ClockReference_Handle hReference) |
|---|
| 73 | { |
|---|
| 74 | BDBG_ENTER(BASTMlib_ClockReference_Destroy); |
|---|
| 75 | BDBG_ASSERT(hReference); |
|---|
| 76 | |
|---|
| 77 | if (hReference->sEventQueue.asEvents) |
|---|
| 78 | { |
|---|
| 79 | BKNI_Free(hReference->sEventQueue.asEvents); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | BKNI_Free(hReference); |
|---|
| 83 | BDBG_LEAVE(BASTMlib_ClockReference_Destroy); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void BASTMlib_ClockReference_GetDefaultConfig(BASTMlib_ClockReference_Config * psConfig) |
|---|
| 87 | { |
|---|
| 88 | BDBG_ENTER(BASTMlib_ClockReference_P_GetDefaultConfig); |
|---|
| 89 | |
|---|
| 90 | BDBG_ASSERT(psConfig); |
|---|
| 91 | |
|---|
| 92 | psConfig->eClockReferenceDomain = BASTMlib_ClockRate_e45Khz; |
|---|
| 93 | psConfig->uiMinimumTimeBetweenEvents = BASTMLIB_CLOCK_REFERENCE_P_DEFAULT_MIN_TIME_BETWEEN_EVENTS; |
|---|
| 94 | psConfig->uiDeviationThreshold = BASTMLIB_CLOCK_REFERENCE_P_DEFAULT_DEVIATION_THRESHOLD * BASTMlib_ClockRate_e45Khz / 1000; |
|---|
| 95 | psConfig->uiDeviantCountThreshold = BASTMLIB_CLOCK_REFERENCE_P_DEFAULT_DEVIANT_COUNT_THRESHOLD; |
|---|
| 96 | psConfig->uiIdealCountThreshold = BASTMLIB_CLOCK_REFERENCE_P_DEFAULT_IDEAL_COUNT_THRESHOLD; |
|---|
| 97 | |
|---|
| 98 | BDBG_LEAVE(BASTMlib_ClockReference_P_GetDefaultConfig); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | void BASTMlib_ClockReference_Reset_isr( |
|---|
| 102 | BASTMlib_ClockReference_Handle hReference |
|---|
| 103 | ) |
|---|
| 104 | { |
|---|
| 105 | BDBG_ENTER(BASTMlib_ClockReference_Reset_isr); |
|---|
| 106 | |
|---|
| 107 | BDBG_ASSERT(hReference); |
|---|
| 108 | |
|---|
| 109 | hReference->lAverageDeviation = 0; |
|---|
| 110 | hReference->uiDeviantCount = 0; |
|---|
| 111 | hReference->uiIdealCount = 0; |
|---|
| 112 | |
|---|
| 113 | hReference->sEventQueue.uiWrite = hReference->sEventQueue.uiCapacity / 2; |
|---|
| 114 | hReference->sEventQueue.uiRead = hReference->sEventQueue.uiWrite - 1; |
|---|
| 115 | hReference->sEventQueue.uiSize = 0; |
|---|
| 116 | |
|---|
| 117 | BDBG_LEAVE(BASTMlib_ClockReference_Reset_isr); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | void BASTMlib_ClockReference_GetConfig( |
|---|
| 121 | const BASTMlib_ClockReference_Handle hReference, |
|---|
| 122 | BASTMlib_ClockReference_Config * psConfig |
|---|
| 123 | ) |
|---|
| 124 | { |
|---|
| 125 | BDBG_ENTER(BASTMlib_ClockReference_GetConfig); |
|---|
| 126 | |
|---|
| 127 | BDBG_ASSERT(hReference); |
|---|
| 128 | BDBG_ASSERT(psConfig); |
|---|
| 129 | |
|---|
| 130 | *psConfig = hReference->sConfig; |
|---|
| 131 | |
|---|
| 132 | BDBG_LEAVE(BASTMlib_ClockReference_GetConfig); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | void BASTMlib_ClockReference_SetConfig( |
|---|
| 136 | BASTMlib_ClockReference_Handle hReference, |
|---|
| 137 | const BASTMlib_ClockReference_Config * psConfig |
|---|
| 138 | ) |
|---|
| 139 | { |
|---|
| 140 | BASTMlib_Handle hAstm; |
|---|
| 141 | |
|---|
| 142 | BDBG_ENTER(BASTMlib_ClockReference_SetConfig); |
|---|
| 143 | |
|---|
| 144 | BDBG_ASSERT(hReference); |
|---|
| 145 | BDBG_ASSERT(psConfig); |
|---|
| 146 | |
|---|
| 147 | hReference->sConfig = *psConfig; |
|---|
| 148 | |
|---|
| 149 | BDBG_MSG(("clock reference reconfigured:")); |
|---|
| 150 | BDBG_MSG((" min time between events: %u ms", hReference->sConfig.uiMinimumTimeBetweenEvents)); |
|---|
| 151 | BDBG_MSG((" clock reference domain: %u Hz", hReference->sConfig.eClockReferenceDomain)); |
|---|
| 152 | BDBG_MSG((" deviation threshold: %u ticks", hReference->sConfig.uiDeviationThreshold)); |
|---|
| 153 | BDBG_MSG((" deviant count threshold: %u events", hReference->sConfig.uiDeviantCountThreshold)); |
|---|
| 154 | BDBG_MSG((" ideal count threshold: %u events", hReference->sConfig.uiIdealCountThreshold)); |
|---|
| 155 | |
|---|
| 156 | hAstm = hReference->hAstm; |
|---|
| 157 | |
|---|
| 158 | /* don't resize if already started, wait till next restart */ |
|---|
| 159 | if (!hAstm->bStarted) |
|---|
| 160 | { |
|---|
| 161 | BASTMlib_ClockReference_ResizeEventQueue(hReference); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | BDBG_LEAVE(BASTMlib_ClockReference_SetConfig); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | void BASTMlib_ClockReference_EventHandler_isr( |
|---|
| 168 | BASTMlib_ClockReference_Handle hReference, |
|---|
| 169 | const BASTMlib_ClockReference_Event * psEvent |
|---|
| 170 | |
|---|
| 171 | ) |
|---|
| 172 | { |
|---|
| 173 | BASTMlib_Handle hAstm; |
|---|
| 174 | |
|---|
| 175 | BDBG_ENTER(BASTMlib_ClockReference_EventHandler_isr); |
|---|
| 176 | |
|---|
| 177 | BDBG_ASSERT(hReference); |
|---|
| 178 | BDBG_ASSERT(psEvent); |
|---|
| 179 | |
|---|
| 180 | hAstm = hReference->hAstm; |
|---|
| 181 | BDBG_ASSERT(hAstm); |
|---|
| 182 | |
|---|
| 183 | if (hAstm->bStarted) |
|---|
| 184 | { |
|---|
| 185 | if (hAstm->sClockCoupling.bAcquire) |
|---|
| 186 | { |
|---|
| 187 | if (hReference->sEventQueue.uiSize < hReference->sEventQueue.uiCapacity) |
|---|
| 188 | { |
|---|
| 189 | #if BASTMLIB_DEBUG_QUEUE |
|---|
| 190 | BDBG_MSG(("Adding event to clock reference queue at %u", hReference->sEventQueue.uiWrite)); |
|---|
| 191 | #endif |
|---|
| 192 | hReference->sEventQueue.asEvents[hReference->sEventQueue.uiWrite] = *psEvent; |
|---|
| 193 | hReference->sEventQueue.uiWrite++; |
|---|
| 194 | hReference->sEventQueue.uiWrite %= hReference->sEventQueue.uiCapacity; |
|---|
| 195 | hReference->sEventQueue.uiSize++; |
|---|
| 196 | } |
|---|
| 197 | else |
|---|
| 198 | { |
|---|
| 199 | BDBG_MSG(("Clock reference event queue full. Discarding events.")); |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | else |
|---|
| 203 | { |
|---|
| 204 | BDBG_MSG(("Clock reference event received while not acquiring. Ignored")); |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | else |
|---|
| 208 | { |
|---|
| 209 | BDBG_MSG(("Clock reference event received while stopped. Ignored")); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | BDBG_LEAVE(BASTMlib_ClockReference_EventHandler_isr); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | void BASTMlib_ClockReference_ResizeEventQueue( |
|---|
| 216 | BASTMlib_ClockReference_Handle hReference |
|---|
| 217 | ) |
|---|
| 218 | { |
|---|
| 219 | unsigned int uiMinimumTimeBetweenEvents; |
|---|
| 220 | unsigned int uiMaximumAcquisitionTime; |
|---|
| 221 | unsigned int uiCapacity; |
|---|
| 222 | |
|---|
| 223 | BDBG_ASSERT(hReference); |
|---|
| 224 | |
|---|
| 225 | BDBG_ENTER(BASTMlib_ClockReference_ResizeEventQueue); |
|---|
| 226 | |
|---|
| 227 | uiMinimumTimeBetweenEvents = hReference->sConfig.uiMinimumTimeBetweenEvents; |
|---|
| 228 | uiMaximumAcquisitionTime = hReference->uiMaximumAcquisitionTime; |
|---|
| 229 | |
|---|
| 230 | if (uiMinimumTimeBetweenEvents && uiMaximumAcquisitionTime) |
|---|
| 231 | { |
|---|
| 232 | uiCapacity = uiMaximumAcquisitionTime / uiMinimumTimeBetweenEvents + 1; |
|---|
| 233 | uiCapacity += uiCapacity / 10; /* 10% bigger than required */ |
|---|
| 234 | } |
|---|
| 235 | else |
|---|
| 236 | { |
|---|
| 237 | uiCapacity = BASTMLIB_CLOCK_REFERENCE_P_DEFAULT_EVENT_QUEUE_CAPACITY; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | if (hReference->sEventQueue.uiCapacity != uiCapacity) |
|---|
| 241 | { |
|---|
| 242 | if (hReference->sEventQueue.asEvents) |
|---|
| 243 | { |
|---|
| 244 | BKNI_Free(hReference->sEventQueue.asEvents); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | hReference->sEventQueue.asEvents = (BASTMlib_ClockReference_Event *)BKNI_Malloc(sizeof(BASTMlib_ClockReference_Event) * uiCapacity); |
|---|
| 248 | BDBG_ASSERT(hReference->sEventQueue.asEvents); |
|---|
| 249 | BKNI_Memset(hReference->sEventQueue.asEvents, 0, sizeof(BASTMlib_ClockReference_Event) * uiCapacity); |
|---|
| 250 | |
|---|
| 251 | hReference->sEventQueue.uiCapacity = uiCapacity; |
|---|
| 252 | |
|---|
| 253 | BKNI_EnterCriticalSection(); |
|---|
| 254 | BASTMlib_ClockReference_Reset_isr(hReference); |
|---|
| 255 | BKNI_LeaveCriticalSection(); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | BDBG_LEAVE(BASTMlib_ClockReference_ResizeEventQueue); |
|---|
| 259 | } |
|---|
| 260 | |
|---|