hwtimer.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * File : hwtimer.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2015-08-31 heyuanjie87 first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv)
  27. {
  28. float overflow;
  29. float timeout;
  30. rt_uint32_t counter;
  31. int i, index;
  32. float tv_sec;
  33. float devi_min = 1;
  34. float devi;
  35. /* changed to second */
  36. overflow = timer->info->maxcnt/(float)timer->freq;
  37. tv_sec = tv->sec + tv->usec/(float)1000000;
  38. if (tv_sec < (1/(float)timer->freq))
  39. {
  40. /* little timeout */
  41. i = 0;
  42. timeout = 1/(float)timer->freq;
  43. }
  44. else
  45. {
  46. for (i = 1; i > 0; i ++)
  47. {
  48. timeout = tv_sec/i;
  49. if (timeout <= overflow)
  50. {
  51. counter = timeout*timer->freq;
  52. devi = tv_sec - (counter/(float)timer->freq)*i;
  53. /* Minimum calculation error */
  54. if (devi > devi_min)
  55. {
  56. i = index;
  57. timeout = tv_sec/i;
  58. break;
  59. }
  60. else if (devi == 0)
  61. {
  62. break;
  63. }
  64. else if (devi < devi_min)
  65. {
  66. devi_min = devi;
  67. index = i;
  68. }
  69. }
  70. }
  71. }
  72. timer->cycles = i;
  73. timer->reload = i;
  74. timer->period_sec = timeout;
  75. counter = timeout*timer->freq;
  76. return counter;
  77. }
  78. static rt_err_t rt_hwtimer_init(struct rt_device *dev)
  79. {
  80. rt_err_t result = RT_EOK;
  81. rt_hwtimer_t *timer;
  82. timer = (rt_hwtimer_t *)dev;
  83. /* try to change to 1MHz */
  84. if ((1000000 <= timer->info->maxfreq) && (1000000 >= timer->info->minfreq))
  85. {
  86. timer->freq = 1000000;
  87. }
  88. else
  89. {
  90. timer->freq = timer->info->minfreq;
  91. }
  92. timer->mode = HWTIMER_MODE_ONESHOT;
  93. timer->cycles = 0;
  94. timer->overflow = 0;
  95. if (timer->ops->init)
  96. {
  97. timer->ops->init(timer, 1);
  98. }
  99. else
  100. {
  101. result = -RT_ENOSYS;
  102. }
  103. return result;
  104. }
  105. static rt_err_t rt_hwtimer_open(struct rt_device *dev, rt_uint16_t oflag)
  106. {
  107. rt_err_t result = RT_EOK;
  108. rt_hwtimer_t *timer;
  109. timer = (rt_hwtimer_t *)dev;
  110. if (timer->ops->control != RT_NULL)
  111. {
  112. timer->ops->control(timer, HWTIMER_CTRL_FREQ_SET, &timer->freq);
  113. }
  114. else
  115. {
  116. result = -RT_ENOSYS;
  117. }
  118. return result;
  119. }
  120. static rt_err_t rt_hwtimer_close(struct rt_device *dev)
  121. {
  122. rt_err_t result = RT_EOK;
  123. rt_hwtimer_t *timer;
  124. timer = (rt_hwtimer_t*)dev;
  125. if (timer->ops->init != RT_NULL)
  126. {
  127. timer->ops->init(timer, 0);
  128. }
  129. else
  130. {
  131. result = -RT_ENOSYS;
  132. }
  133. dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
  134. dev->rx_indicate = RT_NULL;
  135. return result;
  136. }
  137. static rt_size_t rt_hwtimer_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
  138. {
  139. rt_hwtimer_t *timer;
  140. rt_hwtimerval_t tv;
  141. rt_uint32_t cnt;
  142. float t;
  143. timer = (rt_hwtimer_t *)dev;
  144. if (timer->ops->count_get == RT_NULL)
  145. return 0;
  146. cnt = timer->ops->count_get(timer);
  147. if (timer->info->cntmode == HWTIMER_CNTMODE_DW)
  148. {
  149. cnt = timer->info->maxcnt - cnt;
  150. }
  151. t = timer->overflow * timer->period_sec + cnt/(float)timer->freq;
  152. tv.sec = t;
  153. tv.usec = (t - tv.sec) * 1000000;
  154. size = size > sizeof(tv)? sizeof(tv) : size;
  155. rt_memcpy(buffer, &tv, size);
  156. return size;
  157. }
  158. static rt_size_t rt_hwtimer_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size)
  159. {
  160. rt_uint32_t t;
  161. rt_hwtimer_mode_t opm = HWTIMER_MODE_PERIOD;
  162. rt_hwtimer_t *timer;
  163. timer = (rt_hwtimer_t *)dev;
  164. if ((timer->ops->start == RT_NULL) || (timer->ops->stop == RT_NULL))
  165. return 0;
  166. if (size != sizeof(rt_hwtimerval_t))
  167. return 0;
  168. timer->ops->stop(timer);
  169. timer->overflow = 0;
  170. t = timeout_calc(timer, (rt_hwtimerval_t*)buffer);
  171. if ((timer->cycles <= 1) && (timer->mode == HWTIMER_MODE_ONESHOT))
  172. {
  173. opm = HWTIMER_MODE_ONESHOT;
  174. }
  175. if (timer->ops->start(timer, t, opm) != RT_EOK)
  176. size = 0;
  177. return size;
  178. }
  179. static rt_err_t rt_hwtimer_control(struct rt_device *dev, int cmd, void *args)
  180. {
  181. rt_err_t result = RT_EOK;
  182. rt_hwtimer_t *timer;
  183. timer = (rt_hwtimer_t *)dev;
  184. switch (cmd)
  185. {
  186. case HWTIMER_CTRL_STOP:
  187. {
  188. if (timer->ops->stop != RT_NULL)
  189. {
  190. timer->ops->stop(timer);
  191. }
  192. else
  193. {
  194. result = -RT_ENOSYS;
  195. }
  196. }
  197. break;
  198. case HWTIMER_CTRL_FREQ_SET:
  199. {
  200. rt_uint32_t *f;
  201. if (args == RT_NULL)
  202. {
  203. result = -RT_EEMPTY;
  204. break;
  205. }
  206. f = (rt_uint32_t*)args;
  207. if ((*f > timer->info->maxfreq) || (*f < timer->info->minfreq))
  208. {
  209. result = -RT_ERROR;
  210. break;
  211. }
  212. if (timer->ops->control != RT_NULL)
  213. {
  214. result = timer->ops->control(timer, cmd, args);
  215. if (result == RT_EOK)
  216. {
  217. timer->freq = *f;
  218. }
  219. }
  220. else
  221. {
  222. result = -RT_ENOSYS;
  223. }
  224. }
  225. break;
  226. case HWTIMER_CTRL_INFO_GET:
  227. {
  228. if (args == RT_NULL)
  229. {
  230. result = -RT_EEMPTY;
  231. break;
  232. }
  233. *((struct rt_hwtimer_info*)args) = *timer->info;
  234. }
  235. case HWTIMER_CTRL_MODE_SET:
  236. {
  237. rt_hwtimer_mode_t *m;
  238. if (args == RT_NULL)
  239. {
  240. result = -RT_EEMPTY;
  241. break;
  242. }
  243. m = (rt_hwtimer_mode_t*)args;
  244. if ((*m != HWTIMER_MODE_ONESHOT) && (*m != HWTIMER_MODE_PERIOD))
  245. {
  246. result = -RT_ERROR;
  247. break;
  248. }
  249. timer->mode = *m;
  250. }
  251. break;
  252. default:
  253. {
  254. result = -RT_ENOSYS;
  255. }
  256. break;
  257. }
  258. return result;
  259. }
  260. void rt_device_hwtimer_isr(rt_hwtimer_t *timer)
  261. {
  262. RT_ASSERT(timer != RT_NULL);
  263. timer->overflow ++;
  264. if (timer->cycles != 0)
  265. {
  266. timer->cycles --;
  267. }
  268. if (timer->cycles == 0)
  269. {
  270. timer->cycles = timer->reload;
  271. if (timer->mode == HWTIMER_MODE_ONESHOT)
  272. {
  273. if (timer->ops->stop != RT_NULL)
  274. {
  275. timer->ops->stop(timer);
  276. }
  277. }
  278. if (timer->parent.rx_indicate != RT_NULL)
  279. {
  280. timer->parent.rx_indicate(&timer->parent, sizeof(struct rt_hwtimerval));
  281. }
  282. }
  283. }
  284. #ifdef RT_USING_DEVICE_OPS
  285. const static struct rt_device_ops hwtimer_ops =
  286. {
  287. rt_hwtimer_init,
  288. rt_hwtimer_open,
  289. rt_hwtimer_close,
  290. rt_hwtimer_read,
  291. rt_hwtimer_write,
  292. rt_hwtimer_control
  293. };
  294. #endif
  295. rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data)
  296. {
  297. struct rt_device *device;
  298. RT_ASSERT(timer != RT_NULL);
  299. RT_ASSERT(timer->ops != RT_NULL);
  300. RT_ASSERT(timer->info != RT_NULL);
  301. device = &(timer->parent);
  302. device->type = RT_Device_Class_Timer;
  303. device->rx_indicate = RT_NULL;
  304. device->tx_complete = RT_NULL;
  305. #ifdef RT_USING_DEVICE_OPS
  306. device->ops = &hwtimer_ops;
  307. #else
  308. device->init = rt_hwtimer_init;
  309. device->open = rt_hwtimer_open;
  310. device->close = rt_hwtimer_close;
  311. device->read = rt_hwtimer_read;
  312. device->write = rt_hwtimer_write;
  313. device->control = rt_hwtimer_control;
  314. #endif
  315. device->user_data = user_data;
  316. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  317. }