mac_backoff_timer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /**************************************************************************************************
  2. Filename: mac_backoff_timer.c
  3. Revised: $Date: 2007-09-11 10:58:41 -0700 (Tue, 11 Sep 2007) $
  4. Revision: $Revision: 15371 $
  5. Description: Describe the purpose and contents of the file.
  6. Copyright 2006-2012 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. /* ------------------------------------------------------------------------------------------------
  34. * Includes
  35. * ------------------------------------------------------------------------------------------------
  36. */
  37. /* hal */
  38. #include "hal_types.h"
  39. #include "hal_mcu.h"
  40. /* high-level specific */
  41. #include "mac_spec.h"
  42. /* exported low-level */
  43. #include "mac_low_level.h"
  44. /* low-level specific */
  45. #include "mac_backoff_timer.h"
  46. #include "mac_tx.h"
  47. /* target specific */
  48. #include "mac_radio_defs.h"
  49. /* debug */
  50. #include "mac_assert.h"
  51. /* ------------------------------------------------------------------------------------------------
  52. * Defines
  53. * ------------------------------------------------------------------------------------------------
  54. */
  55. #define COMPARE_STATE_ROLLOVER_BV BV(0)
  56. #define COMPARE_STATE_TRIGGER_BV BV(1)
  57. #define COMPARE_STATE_ARM_BV BV(2)
  58. #define COMPARE_STATE_TRIGGER COMPARE_STATE_TRIGGER_BV
  59. #define COMPARE_STATE_ROLLOVER COMPARE_STATE_ROLLOVER_BV
  60. #define COMPARE_STATE_ROLLOVER_AND_TRIGGER (COMPARE_STATE_ROLLOVER_BV | COMPARE_STATE_TRIGGER_BV)
  61. #define COMPARE_STATE_ROLLOVER_AND_ARM_TRIGGER (COMPARE_STATE_ROLLOVER_BV | COMPARE_STATE_ARM_BV)
  62. /*
  63. * The datasheet mentions a small delay on both receive and transmit side of approximately
  64. * two microseconds. The precise characterization is given below.
  65. * (This data is not given in rev 1.03 datasheet)
  66. */
  67. #define RX_TX_PROP_DELAY_AVG_USEC ((MAC_RADIO_RX_TX_PROP_DELAY_MIN_USEC + MAC_RADIO_RX_TX_PROP_DELAY_MAX_USEC) / 2)
  68. #define RX_TX_PROP_DELAY_AVG_TIMER_TICKS ((uint16)(MAC_RADIO_TIMER_TICKS_PER_USEC() * RX_TX_PROP_DELAY_AVG_USEC + 0.5))
  69. /*
  70. * For slotted receives, the SFD signal is expected to occur on a specifc symbol boundary.
  71. * This does *not* correspond to the backoff boundary. The SFD signal occurs at an
  72. * offset from the backoff boundary. This is done for efficiency of related algorithms.
  73. *
  74. * Once transmit is strobed there is a fixed delay until the SFD signal occurs. The frame
  75. * does not start over-the-air transmit until after an internal radio delay of 12 symbols.
  76. * Once transmitting over-the-air, the preamble is sent (8 symbols) followed by the
  77. * SFD field (2 symbols). After the SFD field completes, the SFD signal occurs. This
  78. * adds up to a total of 22 symbols from strobe to SFD signal.
  79. *
  80. * Since 22 symbols spans more than a backoff (20 symbols) the modulus operation is used
  81. * to find the symbol offset which is 2 symbols.
  82. *
  83. * This math is derived formally via the pre-processor.
  84. */
  85. #define SYMBOLS_FROM_STROBE_TO_PREAMBLE 12 /* from datasheet */
  86. #define SYMBOLS_FROM_PREAMBLE_TO_SFD (MAC_SPEC_PREAMBLE_FIELD_LENGTH + MAC_SPEC_SFD_FIELD_LENGTH)
  87. #define SYMBOLS_FROM_STROBE_TO_SFD (SYMBOLS_FROM_STROBE_TO_PREAMBLE + SYMBOLS_FROM_PREAMBLE_TO_SFD)
  88. #define SYMBOLS_EXPECTED_AT_SFD (SYMBOLS_FROM_STROBE_TO_SFD % MAC_A_UNIT_BACKOFF_PERIOD)
  89. /* after all that formal math, make sure the result is as expected */
  90. #if (SYMBOLS_EXPECTED_AT_SFD != 2)
  91. #error "ERROR! Internal problem with pre-processor math of slotted alignment."
  92. #endif
  93. /*
  94. * The expected SFD signal occurs at the symbol offset *plus* a small internal propagation delay
  95. * internal to the radio. This delay is given as the sum of a receive side delay and a transmit
  96. * side delay. When this delay is subtracted from the internal timer, the internal time base
  97. * actually becomes the actual receive time *minus* the transmit delay. This works out though.
  98. * The transmit logic does *not* take into account this delay. Since the timer is skewed by the
  99. * transmit delay already, the transmits go out precisely on time.
  100. */
  101. #define TIMER_TICKS_EXPECTED_AT_SFD ((SYMBOLS_EXPECTED_AT_SFD * MAC_RADIO_TIMER_TICKS_PER_SYMBOL()) \
  102. + RX_TX_PROP_DELAY_AVG_TIMER_TICKS)
  103. /* ------------------------------------------------------------------------------------------------
  104. * Local Variables
  105. * ------------------------------------------------------------------------------------------------
  106. */
  107. static uint32 backoffTimerRollover;
  108. static uint32 backoffTimerTrigger;
  109. /**************************************************************************************************
  110. * @fn macBackoffTimerInit
  111. *
  112. * @brief Intializes backoff timer.
  113. *
  114. * @param none
  115. *
  116. * @return none
  117. **************************************************************************************************
  118. */
  119. MAC_INTERNAL_API void macBackoffTimerInit(void)
  120. {
  121. MAC_RADIO_BACKOFF_SET_COUNT(0);
  122. macBackoffTimerSetRollover(MAC_BACKOFF_TIMER_DEFAULT_ROLLOVER);
  123. MAC_RADIO_BACKOFF_PERIOD_CLEAR_INTERRUPT();
  124. MAC_RADIO_BACKOFF_PERIOD_ENABLE_INTERRUPT();
  125. MAC_RADIO_BACKOFF_COMPARE_CLEAR_INTERRUPT();
  126. MAC_RADIO_BACKOFF_COMPARE_ENABLE_INTERRUPT();
  127. }
  128. /**************************************************************************************************
  129. * @fn macBackoffTimerReset
  130. *
  131. * @brief Resets backoff timer.
  132. *
  133. * @param none
  134. *
  135. * @return none
  136. **************************************************************************************************
  137. */
  138. MAC_INTERNAL_API void macBackoffTimerReset(void)
  139. {
  140. MAC_RADIO_BACKOFF_COMPARE_DISABLE_INTERRUPT();
  141. MAC_RADIO_BACKOFF_PERIOD_DISABLE_INTERRUPT();
  142. macBackoffTimerInit();
  143. }
  144. /**************************************************************************************************
  145. * @fn macBackoffTimerSetRollover
  146. *
  147. * @brief Set rollover count of backoff timer.
  148. *
  149. * @param rolloverBackoff - backoff count where count is reset to zero
  150. *
  151. * @return none
  152. **************************************************************************************************
  153. */
  154. MAC_INTERNAL_API void macBackoffTimerSetRollover(uint32 rolloverBackoff)
  155. {
  156. halIntState_t s;
  157. MAC_ASSERT(rolloverBackoff > MAC_RADIO_BACKOFF_COUNT()); /* rollover value must be greater than count */
  158. HAL_ENTER_CRITICAL_SECTION(s);
  159. backoffTimerRollover = rolloverBackoff;
  160. MAC_RADIO_BACKOFF_SET_PERIOD(rolloverBackoff);
  161. HAL_EXIT_CRITICAL_SECTION(s);
  162. }
  163. /**************************************************************************************************
  164. * @fn macBackoffTimerSetCount
  165. *
  166. * @brief Sets the count of the backoff timer.
  167. *
  168. * @param backoff - new count
  169. *
  170. * @return none
  171. **************************************************************************************************
  172. */
  173. MAC_INTERNAL_API void macBackoffTimerSetCount(uint32 backoff)
  174. {
  175. halIntState_t s;
  176. MAC_ASSERT(backoff < backoffTimerRollover); /* count must be less than rollover value */
  177. MAC_ASSERT(!(backoff & 0x80000000)); /* count must not represent negative value for int32 */
  178. HAL_ENTER_CRITICAL_SECTION(s);
  179. MAC_RADIO_BACKOFF_SET_COUNT(backoff);
  180. HAL_EXIT_CRITICAL_SECTION(s);
  181. }
  182. /**************************************************************************************************
  183. * @fn macBackoffTimerCount
  184. *
  185. * @brief Returns the current backoff count.
  186. *
  187. * @param none
  188. *
  189. * @return current backoff count
  190. **************************************************************************************************
  191. */
  192. MAC_INTERNAL_API uint32 macBackoffTimerCount(void)
  193. {
  194. halIntState_t s;
  195. uint32 backoffCount;
  196. HAL_ENTER_CRITICAL_SECTION(s);
  197. backoffCount = MAC_RADIO_BACKOFF_COUNT();
  198. HAL_EXIT_CRITICAL_SECTION(s);
  199. #ifdef MAC_RADIO_FEATURE_HARDWARE_OVERFLOW_NO_ROLLOVER
  200. /*
  201. * Extra processing is required if the radio has a special hardware overflow
  202. * count feature. Unfortunately this feature does not provide for setting a
  203. * rollover value. This must be done manually.
  204. *
  205. * This means there is a small window in time when reading the hardware count
  206. * will be inaccurate. It's possible it could be one more than the allowable
  207. * count. This happens if the count has just incremented beyond the maximum
  208. * and is queried before the ISR has a chance to run and reset the backoff
  209. * count back to zero. (Pure software implementation of backoff count does
  210. * not have this problem.)
  211. *
  212. * To solve this, before returning a value for the backoff count, the value
  213. * must be tested to see if it is beyond the maximum value. If so, a rollover
  214. * interrupt that will set backoff count to zero is imminent. In that case,
  215. * the correct backoff count of zero is returned.
  216. */
  217. if (backoffCount >= backoffTimerRollover)
  218. {
  219. return(0);
  220. }
  221. #endif
  222. return(backoffCount);
  223. }
  224. /**************************************************************************************************
  225. * @fn macBackoffTimerCapture
  226. *
  227. * @brief Returns the most recently captured backoff count
  228. *
  229. * @param none
  230. *
  231. * @return last backoff count that was captured
  232. **************************************************************************************************
  233. */
  234. MAC_INTERNAL_API uint32 macBackoffTimerCapture(void)
  235. {
  236. halIntState_t s;
  237. uint32 backoffCapture;
  238. HAL_ENTER_CRITICAL_SECTION(s);
  239. backoffCapture = MAC_RADIO_BACKOFF_CAPTURE();
  240. HAL_EXIT_CRITICAL_SECTION(s);
  241. #ifdef MAC_RADIO_FEATURE_HARDWARE_OVERFLOW_NO_ROLLOVER
  242. /*
  243. * See other instance of this #ifdef for detailed comments.
  244. * Those comments apply to the backoff capture value too.
  245. */
  246. if (backoffCapture >= backoffTimerRollover)
  247. {
  248. return(0);
  249. }
  250. #endif
  251. return(backoffCapture);
  252. }
  253. /**************************************************************************************************
  254. * @fn macBackoffTimerGetTrigger
  255. *
  256. * @brief Returns the trigger set for the backoff timer.
  257. *
  258. * @param none
  259. *
  260. * @return backoff count of trigger
  261. **************************************************************************************************
  262. */
  263. MAC_INTERNAL_API uint32 macBackoffTimerGetTrigger(void)
  264. {
  265. return(backoffTimerTrigger);
  266. }
  267. /**************************************************************************************************
  268. * @fn macBackoffTimerSetTrigger
  269. *
  270. * @brief Sets the trigger count for the backoff counter. A callback is exectuted when
  271. * the backoff count reaches the trigger
  272. *
  273. * @param triggerBackoff - backoff count for new trigger
  274. *
  275. * @return none
  276. **************************************************************************************************
  277. */
  278. MAC_INTERNAL_API void macBackoffTimerSetTrigger(uint32 triggerBackoff)
  279. {
  280. halIntState_t s;
  281. MAC_ASSERT(triggerBackoff < backoffTimerRollover); /* trigger backoff must be less than rollover backoff */
  282. HAL_ENTER_CRITICAL_SECTION(s);
  283. backoffTimerTrigger = triggerBackoff;
  284. MAC_RADIO_BACKOFF_SET_COMPARE(triggerBackoff);
  285. if (triggerBackoff == MAC_RADIO_BACKOFF_COUNT())
  286. {
  287. /* Clear the interrupt and fire it manually */
  288. MAC_RADIO_BACKOFF_COMPARE_CLEAR_INTERRUPT();
  289. HAL_EXIT_CRITICAL_SECTION(s);
  290. macBackoffTimerTriggerCallback();
  291. }
  292. else
  293. {
  294. HAL_EXIT_CRITICAL_SECTION(s);
  295. }
  296. }
  297. /**************************************************************************************************
  298. * @fn macBackoffTimerCancelTrigger
  299. *
  300. * @brief Cancels the trigger for the backoff counter - obselete for CC2530.
  301. *
  302. * @param none
  303. *
  304. * @return none
  305. **************************************************************************************************
  306. */
  307. MAC_INTERNAL_API void macBackoffTimerCancelTrigger(void)
  308. {
  309. /* Stub for high level MAC */
  310. }
  311. /**************************************************************************************************
  312. * @fn macBackoffTimerRealign
  313. *
  314. * @brief
  315. *
  316. * Realignment is accomplished by adjusting the internal time base to align with the expected
  317. * reception time of an incoming frame. The difference between the expected reception time and
  318. * the actual reception time is computed and this difference is used to adjust the hardware
  319. * timer count and backoff count.
  320. *
  321. * The realignment is based on the SFD signal for the incoming frame. The timer is aligned
  322. * by adjusting it with the difference between the expected SFD time and the actual SFD time.
  323. *
  324. * @param none
  325. *
  326. * @return none
  327. **************************************************************************************************
  328. */
  329. MAC_INTERNAL_API int32 macBackoffTimerRealign(macRx_t *pMsg)
  330. {
  331. uint16 timerDelayTicks;
  332. int32 backoffDelta;
  333. int32 backoffCount;
  334. MAC_ASSERT(!MAC_TX_IS_PHYSICALLY_ACTIVE()); /* realignment during actual transmit corrupts timing */
  335. /*-------------------------------------------------------------------------------
  336. * Calculate the delta backoff difference between expected backoff count,
  337. * which is zero, and the backoff count of the received frame.
  338. */
  339. /* since expected receive time is zero, the delta is simply the receive time */
  340. backoffDelta = pMsg->mac.timestamp;
  341. /* if the frame was received more than halfway to the rollover count, use a negative delta value */
  342. if (((uint32) backoffDelta) > (backoffTimerRollover / 2))
  343. {
  344. backoffDelta = backoffDelta - backoffTimerRollover; /* result will be negative */
  345. }
  346. /*-------------------------------------------------------------------------------
  347. * Calculate the number of timer ticks to delay that will align the internal
  348. * time base with the received frame.
  349. */
  350. /* retrieve the timer count when frame was received */
  351. timerDelayTicks = pMsg->mac.timestamp2;
  352. /*
  353. * Subtract the expected SFD time from the actual SFD time to find the needed
  354. * timer adjustment. If subtracting the offset would result in a negative value,
  355. * the tick delay must wrap around.
  356. */
  357. if (timerDelayTicks >= TIMER_TICKS_EXPECTED_AT_SFD)
  358. {
  359. /* since delay count is greater than or equal to offset, subtract it directly */
  360. timerDelayTicks = timerDelayTicks - TIMER_TICKS_EXPECTED_AT_SFD;
  361. }
  362. else
  363. {
  364. /*
  365. * The expected time is greater that actualy time so it cannot be subtracted directly.
  366. * The tick count per backoff is added to wrap around within the backoff.
  367. * Since a wrap around did happen, the backoff delta is adjusted by one.
  368. */
  369. timerDelayTicks = timerDelayTicks - TIMER_TICKS_EXPECTED_AT_SFD + MAC_RADIO_TIMER_TICKS_PER_BACKOFF();
  370. backoffDelta--;
  371. }
  372. /*-------------------------------------------------------------------------------
  373. * Calculate the new backoff count.
  374. */
  375. backoffCount = MAC_RADIO_BACKOFF_COUNT() - backoffDelta;
  376. if (backoffCount >= ((int32) backoffTimerRollover))
  377. {
  378. backoffCount -= backoffTimerRollover;
  379. }
  380. else if (backoffCount < 0)
  381. {
  382. backoffCount += backoffTimerRollover;
  383. }
  384. MAC_RADIO_TIMER_FORCE_DELAY(timerDelayTicks);
  385. MAC_RADIO_BACKOFF_SET_COUNT(backoffCount);
  386. return(backoffDelta);
  387. }
  388. /**************************************************************************************************
  389. * @fn macBackoffTimerCompareIsr
  390. *
  391. * @brief Interrupt service routine that fires when the backoff count is equal
  392. * to the trigger count.
  393. *
  394. * @param none
  395. *
  396. * @return none
  397. **************************************************************************************************
  398. */
  399. MAC_INTERNAL_API void macBackoffTimerCompareIsr(void)
  400. {
  401. macBackoffTimerTriggerCallback();
  402. }
  403. /**************************************************************************************************
  404. * @fn macBackoffTimerPeriodIsr
  405. *
  406. * @brief Interrupt service routine that fires when the backoff count rolls over on
  407. * overflow period.
  408. *
  409. * @param none
  410. *
  411. * @return none
  412. **************************************************************************************************
  413. */
  414. MAC_INTERNAL_API void macBackoffTimerPeriodIsr(void)
  415. {
  416. macMcuAccumulatedOverFlow();
  417. macBackoffTimerRolloverCallback();
  418. }
  419. /**************************************************************************************************
  420. * @fn macGetBackOffTimerRollover
  421. *
  422. * @brief Function to get the timer 2 rollover value
  423. *
  424. * @param none
  425. *
  426. * @return timer 2 rollover value
  427. **************************************************************************************************
  428. */
  429. MAC_INTERNAL_API uint32 macGetBackOffTimerRollover(void)
  430. {
  431. return backoffTimerRollover;
  432. }
  433. /**************************************************************************************************
  434. */