tmr.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* *****************************************************************************
  2. * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  18. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Except as contained in this notice, the name of Maxim Integrated
  23. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  24. * Products, Inc. Branding Policy.
  25. *
  26. * The mere transfer of this software does not imply any licenses
  27. * of trade secrets, proprietary technology, copyrights, patents,
  28. * trademarks, maskwork rights, or any other form of intellectual
  29. * property whatsoever. Maxim Integrated Products, Inc. retains all
  30. * ownership rights.
  31. *
  32. * $Date: 2019-09-11 14:32:22 -0500 (Wed, 11 Sep 2019) $
  33. * $Revision: 46047 $
  34. *
  35. **************************************************************************** */
  36. /* **** Includes **** */
  37. #include "mxc_config.h"
  38. #include "mxc_assert.h"
  39. #include "mxc_sys.h"
  40. #include "tmr.h"
  41. /* **** Definitions **** */
  42. /* **** Globals **** */
  43. /* **** Functions **** */
  44. /* ************************************************************************** */
  45. int TMR_Init(mxc_tmr_regs_t *tmr, tmr_pres_t pres, const sys_cfg_tmr_t* sys_cfg)
  46. {
  47. MXC_ASSERT(tmr);
  48. int err;
  49. // System settigns
  50. if((err=SYS_TMR_Init(tmr, sys_cfg)) !=E_NO_ERROR)
  51. {
  52. return err;
  53. }
  54. // Disable timer and clear settings
  55. tmr->cn = 0;
  56. // Clear interrupt flag
  57. tmr->intr = MXC_F_TMR_INTR_IRQ_CLR;
  58. // Set the prescaler
  59. tmr->cn = pres;
  60. return err;
  61. }
  62. int TMR_Shutdown(mxc_tmr_regs_t *tmr)
  63. {
  64. MXC_ASSERT(tmr);
  65. int err;
  66. // System settigns
  67. if((err=SYS_TMR_Shutdown(tmr)) !=E_NO_ERROR)
  68. {
  69. return err;
  70. }
  71. // Disable timer and clear settings
  72. tmr->cn = 0;
  73. return err;
  74. }
  75. /* ************************************************************************** */
  76. void TMR_Enable(mxc_tmr_regs_t* tmr)
  77. {
  78. MXC_ASSERT(tmr);
  79. tmr->cn |= MXC_F_TMR_CN_TEN;
  80. }
  81. /* ************************************************************************** */
  82. void TMR_Disable(mxc_tmr_regs_t* tmr)
  83. {
  84. MXC_ASSERT(tmr);
  85. tmr->cn &= ~(MXC_F_TMR_CN_TEN);
  86. }
  87. /* ************************************************************************** */
  88. int TMR_Config(mxc_tmr_regs_t *tmr, const tmr_cfg_t *cfg)
  89. {
  90. MXC_ASSERT(tmr);
  91. // Configure the timer
  92. tmr->cn = (tmr->cn & ~(MXC_F_TMR_CN_TMODE | MXC_F_TMR_CN_TPOL)) |
  93. ((cfg->mode << MXC_F_TMR_CN_TMODE_POS) & MXC_F_TMR_CN_TMODE) |
  94. ((cfg->pol << MXC_F_TMR_CN_TPOL_POS) & MXC_F_TMR_CN_TPOL);
  95. tmr->cnt = 0x1;
  96. tmr->cmp = cfg->cmp_cnt;
  97. return E_NO_ERROR;
  98. }
  99. /* ************************************************************************** */
  100. int TMR_PWMConfig(mxc_tmr_regs_t *tmr, const tmr_pwm_cfg_t *cfg)
  101. {
  102. if (cfg->duty_cnt > cfg->per_cnt) {
  103. return E_BAD_PARAM;
  104. }
  105. // Configure the timer
  106. tmr->cn = (tmr->cn & ~(MXC_F_TMR_CN_TMODE | MXC_F_TMR_CN_TPOL)) |
  107. MXC_S_TMR_CN_TMODE_PWM | ((cfg->pol << MXC_F_TMR_CN_TPOL_POS) & MXC_F_TMR_CN_TPOL);
  108. tmr->cnt = 0x1;
  109. tmr->cmp = cfg->per_cnt;
  110. tmr->pwm = cfg->duty_cnt;
  111. return E_NO_ERROR;
  112. }
  113. /* ************************************************************************** */
  114. int TMR_PWMSetDuty(mxc_tmr_regs_t *tmr, uint32_t duty)
  115. {
  116. uint32_t cnt;
  117. // Make sure the new Duty count is less than the period count
  118. if (duty > tmr->cmp) {
  119. return E_BAD_PARAM;
  120. }
  121. cnt = tmr->cnt; // make sure order of volatile access is known.
  122. // Avoid glitching the output
  123. if (duty >= tmr->pwm) {
  124. // Wait for the count to be in the range of 1 to tmr->pwm
  125. while (cnt > tmr->pwm) {
  126. cnt = tmr->cnt; // update the volatile access variable
  127. }
  128. } else {
  129. // Wait for the count to pass tmr->pwm
  130. while (cnt < tmr->pwm) {
  131. cnt = tmr->cnt; // update the volatile access variable
  132. }
  133. }
  134. tmr->pwm = duty;
  135. return E_NO_ERROR;
  136. }
  137. /* ************************************************************************** */
  138. int TMR_PWMSetPeriod(mxc_tmr_regs_t *tmr, uint32_t per)
  139. {
  140. // Make sure the new Duty count is less than the period count
  141. if (tmr->pwm > per) {
  142. return E_BAD_PARAM;
  143. }
  144. // Wait for the count to be less than the new dut_cnt
  145. while (tmr->cnt >= per) {}
  146. tmr->cmp = per;
  147. return E_NO_ERROR;
  148. }
  149. /* ************************************************************************** */
  150. uint32_t TMR_GetCompare(mxc_tmr_regs_t* tmr)
  151. {
  152. return tmr->cmp;
  153. }
  154. /* ************************************************************************** */
  155. uint32_t TMR_GetCapture(mxc_tmr_regs_t* tmr)
  156. {
  157. return tmr->pwm;
  158. }
  159. /* ************************************************************************* */
  160. uint32_t TMR_GetCount(mxc_tmr_regs_t* tmr)
  161. {
  162. return tmr->cnt;
  163. }
  164. /* ************************************************************************* */
  165. void TMR_IntClear(mxc_tmr_regs_t* tmr)
  166. {
  167. tmr->intr = MXC_F_TMR_INTR_IRQ_CLR;
  168. }
  169. /* ************************************************************************* */
  170. uint32_t TMR_IntStatus(mxc_tmr_regs_t* tmr)
  171. {
  172. return tmr->intr;
  173. }
  174. /* ************************************************************************* */
  175. void TMR_SetCompare(mxc_tmr_regs_t *tmr, uint32_t cmp_cnt)
  176. {
  177. tmr->cmp = cmp_cnt;
  178. }
  179. /* ************************************************************************* */
  180. void TMR_SetCount(mxc_tmr_regs_t *tmr, uint32_t cnt)
  181. {
  182. tmr->cnt = cnt;
  183. }
  184. /* ************************************************************************* */
  185. int TMR_GetTicks(mxc_tmr_regs_t *tmr, uint32_t time, tmr_unit_t units, uint32_t *ticks)
  186. {
  187. uint32_t unit_div0, unit_div1;
  188. uint32_t timerClock;
  189. uint32_t prescale;
  190. uint64_t temp_ticks;
  191. timerClock = SYS_TMR_GetFreq(tmr);
  192. prescale = ((tmr->cn & MXC_F_TMR_CN_PRES) >> MXC_F_TMR_CN_PRES_POS)
  193. | (((tmr->cn & MXC_F_TMR_CN_PRES3) >> (MXC_F_TMR_CN_PRES3_POS))<<3);
  194. switch (units) {
  195. case TMR_UNIT_NANOSEC:
  196. unit_div0 = 1000000;
  197. unit_div1 = 1000;
  198. break;
  199. case TMR_UNIT_MICROSEC:
  200. unit_div0 = 1000;
  201. unit_div1 = 1000;
  202. break;
  203. case TMR_UNIT_MILLISEC:
  204. unit_div0 = 1;
  205. unit_div1 = 1000;
  206. break;
  207. case TMR_UNIT_SEC:
  208. unit_div0 = 1;
  209. unit_div1 = 1;
  210. break;
  211. default:
  212. return E_BAD_PARAM;
  213. }
  214. temp_ticks = (uint64_t)time * (timerClock / unit_div0) / (unit_div1 * (1 << (prescale & 0xF)));
  215. //make sure ticks is within a 32 bit value
  216. if (!(temp_ticks & 0xffffffff00000000) && (temp_ticks & 0xffffffff)) {
  217. *ticks = temp_ticks;
  218. return E_NO_ERROR;
  219. }
  220. return E_INVALID;
  221. }
  222. /* ************************************************************************* */
  223. int TMR_GetTime(mxc_tmr_regs_t *tmr, uint32_t ticks, uint32_t *time, tmr_unit_t *units)
  224. {
  225. uint64_t temp_time = 0;
  226. uint32_t timerClock = SYS_TMR_GetFreq(tmr);
  227. uint32_t prescale = ((tmr->cn & MXC_F_TMR_CN_PRES) >> MXC_F_TMR_CN_PRES_POS)
  228. | (((tmr->cn & MXC_F_TMR_CN_PRES3) >> (MXC_F_TMR_CN_PRES3_POS))<<3);
  229. temp_time = (uint64_t)ticks * 1000 * (1 << (prescale & 0xF)) / (timerClock / 1000000);
  230. if (!(temp_time & 0xffffffff00000000)) {
  231. *time = temp_time;
  232. *units = TMR_UNIT_NANOSEC;
  233. return E_NO_ERROR;
  234. }
  235. temp_time = (uint64_t)ticks * 1000 * (1 << (prescale & 0xF)) / (timerClock / 1000);
  236. if (!(temp_time & 0xffffffff00000000)) {
  237. *time = temp_time;
  238. *units = TMR_UNIT_MICROSEC;
  239. return E_NO_ERROR;
  240. }
  241. temp_time = (uint64_t)ticks * 1000 * (1 << (prescale & 0xF)) / timerClock;
  242. if (!(temp_time & 0xffffffff00000000)) {
  243. *time = temp_time;
  244. *units = TMR_UNIT_MILLISEC;
  245. return E_NO_ERROR;
  246. }
  247. temp_time = (uint64_t)ticks * (1 << (prescale & 0xF)) / timerClock;
  248. if (!(temp_time & 0xffffffff00000000)) {
  249. *time = temp_time;
  250. *units = TMR_UNIT_SEC;
  251. return E_NO_ERROR;
  252. }
  253. return E_INVALID;
  254. }