osi_tick_unit.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _OSI_TICK_UNIT_H_
  13. #define _OSI_TICK_UNIT_H_
  14. #include "hal_chip.h" // HAL_TIMER_FREQ
  15. /**
  16. * \brief tick unit conversion with 64bits calculation
  17. *
  18. * It is implemented as a macro to be able to be used in const initializer.
  19. * Common divider \p cdiv is helpful to avoid intermediate overflow.
  20. *
  21. * \param t source tick
  22. * \param sfreq source tick frequency
  23. * \param dfres destination tick frequency
  24. * \param cdiv common divider of source and destination frequency
  25. * \return destination tick
  26. */
  27. #define OSI_TICK_CONVERT(t, sfreq, dfreq, cdiv) ((int64_t)__OSI_TICK_CONVERT((int64_t)(t), (unsigned)(sfreq), (unsigned)(dfreq), (unsigned)(cdiv)))
  28. /**
  29. * \brief tick unit conversion with 32bits calculation
  30. *
  31. * It is implemented as a macro to be able to be used in const initializer.
  32. * Common divider \p cdiv is helpful to avoid intermediate overflow. It will
  33. * only be used when performance is really cared.
  34. *
  35. * \param t source tick
  36. * \param sfreq source tick frequency
  37. * \param dfres destination tick frequency
  38. * \param cdiv common divider of source and destination frequency
  39. * \return destination tick
  40. */
  41. #define OSI_TICK_CONVERT_U32(t, sfreq, dfreq, cdiv) ((uint32_t)__OSI_TICK_CONVERT((uint32_t)(t), (unsigned)(sfreq), (unsigned)(dfreq), (unsigned)(cdiv)))
  42. /**
  43. * \brief various tick unit conversion with 64bits calculation
  44. */
  45. #define OSI_TICK16K_TO_MS(t) OSI_TICK_CONVERT(t, 16384, 1000, 8)
  46. #define OSI_MS_TO_TICK16K(t) OSI_TICK_CONVERT(t, 1000, 16384, 8)
  47. #define OSI_TICK16K_TO_US(t) OSI_TICK_CONVERT(t, 16384, 1000000, 64)
  48. #define OSI_US_TO_TICK16K(t) OSI_TICK_CONVERT(t, 1000000, 16384, 64)
  49. #define OSI_TICK32K_TO_MS(t) OSI_TICK_CONVERT(t, 32768, 1000, 8)
  50. #define OSI_MS_TO_TICK32K(t) OSI_TICK_CONVERT(t, 1000, 32768, 8)
  51. #define OSI_HWTICK_TO_SECOND(t) OSI_TICK_CONVERT(t, HAL_TIMER_FREQ, 1, 1)
  52. #define OSI_SECOND_TO_HWTICK(t) OSI_TICK_CONVERT(t, 1, HAL_TIMER_FREQ, 1)
  53. #define OSI_HWTICK_TO_MS(t) OSI_TICK_CONVERT(t, HAL_TIMER_FREQ, 1000, __OSI_HWTICK_CMN_DIV_MS)
  54. #define OSI_MS_TO_HWTICK(t) OSI_TICK_CONVERT(t, 1000, HAL_TIMER_FREQ, __OSI_HWTICK_CMN_DIV_MS)
  55. #define OSI_HWTICK_TO_US(t) OSI_TICK_CONVERT(t, HAL_TIMER_FREQ, 1000000, __OSI_HWTICK_CMN_DIV_US)
  56. #define OSI_US_TO_HWTICK(t) OSI_TICK_CONVERT(t, 1000000, HAL_TIMER_FREQ, __OSI_HWTICK_CMN_DIV_US)
  57. #define OSI_HWTICK_TO_16K(t) OSI_TICK_CONVERT(t, HAL_TIMER_FREQ, 16384, __OSI_HWTICK_CMN_DIV_16K)
  58. #define OSI_16K_TO_HWTICK(t) OSI_TICK_CONVERT(t, 16384, HAL_TIMER_FREQ, __OSI_HWTICK_CMN_DIV_16K)
  59. #define OSI_HWTICK_TO_32K(t) OSI_TICK_CONVERT(t, HAL_TIMER_FREQ, 32768, __OSI_HWTICK_CMN_DIV_32K)
  60. #define OSI_32K_TO_HWTICK(t) OSI_TICK_CONVERT(t, 32768, HAL_TIMER_FREQ, __OSI_HWTICK_CMN_DIV_32K)
  61. /**
  62. * \brief various tick unit conversion with 32bits calculation
  63. */
  64. #define OSI_TICK16K_TO_MS_U32(t) OSI_TICK_CONVERT_U32(t, 16384, 1000, 8)
  65. #define OSI_MS_TO_TICK16K_U32(t) OSI_TICK_CONVERT_U32(t, 1000, 16384, 8)
  66. #define OSI_TICK16K_TO_US_U32(t) OSI_TICK_CONVERT_U32(t, 16384, 1000000, 64)
  67. #define OSI_US_TO_TICK16K_U32(t) OSI_TICK_CONVERT_U32(t, 1000000, 16384, 64)
  68. #define OSI_TICK32K_TO_MS_U32(t) OSI_TICK_CONVERT_U32(t, 32768, 1000, 8)
  69. #define OSI_MS_TO_TICK32K_U32(t) OSI_TICK_CONVERT_U32(t, 1000, 32768, 8)
  70. #define OSI_HWTICK_TO_MS_U32(t) OSI_TICK_CONVERT_U32(t, HAL_TIMER_FREQ, 1000, __OSI_HWTICK_CMN_DIV_MS)
  71. #define OSI_MS_TO_HWTICK_U32(t) OSI_TICK_CONVERT_U32(t, 1000, HAL_TIMER_FREQ, __OSI_HWTICK_CMN_DIV_MS)
  72. #define OSI_HWTICK_TO_US_U32(t) OSI_TICK_CONVERT_U32(t, HAL_TIMER_FREQ, 1000000, __OSI_HWTICK_CMN_DIV_US)
  73. #define OSI_US_TO_HWTICK_U32(t) OSI_TICK_CONVERT_U32(t, 1000000, HAL_TIMER_FREQ, __OSI_HWTICK_CMN_DIV_US)
  74. #define OSI_HWTICK_TO_16K_U32(t) OSI_TICK_CONVERT_U32(t, HAL_TIMER_FREQ, 16384, __OSI_HWTICK_CMN_DIV_16K)
  75. #define OSI_16K_TO_HWTICK_U32(t) OSI_TICK_CONVERT_U32(t, 16384, HAL_TIMER_FREQ, __OSI_HWTICK_CMN_DIV_16K)
  76. // ============================================================================
  77. // Implementation
  78. // ============================================================================
  79. #define __OSI_TICK_CONVERT(t, sfreq, dfreq, cdiv) (((t) * ((dfreq) / (cdiv))) / ((sfreq) / (cdiv)))
  80. #if (HAL_TIMER_FREQ == 16384)
  81. #define __OSI_HWTICK_CMN_DIV_MS (8)
  82. #define __OSI_HWTICK_CMN_DIV_US (64)
  83. #define __OSI_HWTICK_CMN_DIV_16K (16384)
  84. #define __OSI_HWTICK_CMN_DIV_32K (16384)
  85. #endif
  86. #if (HAL_TIMER_FREQ == 2000000)
  87. #define __OSI_HWTICK_CMN_DIV_MS (1000)
  88. #define __OSI_HWTICK_CMN_DIV_US (1000000)
  89. #define __OSI_HWTICK_CMN_DIV_16K (128)
  90. #define __OSI_HWTICK_CMN_DIV_32K (128)
  91. #endif
  92. #if (HAL_TIMER_FREQ == 1000000)
  93. #define __OSI_HWTICK_CMN_DIV_MS (1000)
  94. #define __OSI_HWTICK_CMN_DIV_US (1000000)
  95. #define __OSI_HWTICK_CMN_DIV_16K (64)
  96. #define __OSI_HWTICK_CMN_DIV_32K (64)
  97. #endif
  98. #endif