hwtimer.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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_err_t timeout_set(rt_hwtimer_t *timer, rt_hwtimerval_t *tv)
  27. {
  28. rt_err_t err;
  29. float overflow;
  30. float timeout;
  31. rt_uint32_t counter;
  32. int i, index;
  33. float tv_sec;
  34. float devi_min = 1;
  35. float devi;
  36. if (timer->ops->stop != RT_NULL)
  37. {
  38. timer->ops->stop(timer);
  39. }
  40. else
  41. {
  42. return -RT_ENOSYS;
  43. }
  44. /* 把定时器溢出时间和定时时间换算成秒 */
  45. overflow = timer->info->maxcnt/(float)timer->freq;
  46. tv_sec = tv->sec + tv->usec/(float)1000000;
  47. if (tv_sec < (1/(float)timer->freq))
  48. {
  49. /* 定时时间小于计数周期 */
  50. i = 0;
  51. timeout = 1/(float)timer->freq;
  52. }
  53. else
  54. {
  55. for (i = 1; i > 0; i ++)
  56. {
  57. timeout = tv_sec/i;
  58. if (timeout <= overflow)
  59. {
  60. counter = timeout*timer->freq;
  61. devi = tv_sec - (counter/(float)timer->freq)*i;
  62. /* 计算最小误差 */
  63. if (devi > devi_min)
  64. {
  65. i = index;
  66. timeout = tv_sec/i;
  67. break;
  68. }
  69. else if (devi == 0)
  70. {
  71. break;
  72. }
  73. else if (devi < devi_min)
  74. {
  75. devi_min = devi;
  76. index = i;
  77. }
  78. }
  79. }
  80. }
  81. timer->cycles = i;
  82. timer->reload = i;
  83. timer->period_sec = timeout;
  84. counter = timeout*timer->freq;
  85. if (timer->ops->timeout_set != RT_NULL)
  86. {
  87. err = timer->ops->timeout_set(timer, counter);
  88. }
  89. return err;
  90. }
  91. static rt_err_t rt_hwtimer_init(struct rt_device *dev)
  92. {
  93. rt_err_t result = RT_EOK;
  94. rt_hwtimer_t *timer;
  95. timer = (rt_hwtimer_t *)dev;
  96. /* 尝试将默认计数频率设为1Mhz */
  97. if ((1000000 <= timer->info->maxfreq) && (1000000 >= timer->info->minfreq))
  98. {
  99. timer->freq = 1000000;
  100. }
  101. else
  102. {
  103. timer->freq = timer->info->minfreq;
  104. }
  105. timer->mode = HWTIMER_MODE_ONESHOT;
  106. timer->cycles = 0;
  107. if (timer->ops->init)
  108. {
  109. timer->ops->init(timer);
  110. }
  111. else
  112. {
  113. result = -RT_ENOSYS;
  114. }
  115. return result;
  116. }
  117. static rt_err_t rt_hwtimer_open(struct rt_device *dev, rt_uint16_t oflag)
  118. {
  119. rt_err_t result = RT_EOK;
  120. rt_hwtimer_t *timer;
  121. timer = (rt_hwtimer_t *)dev;
  122. if (timer->ops->control != RT_NULL)
  123. {
  124. timer->ops->control(timer, HWTIMER_CTRL_FREQ_SET, &timer->freq);
  125. }
  126. else
  127. {
  128. result = -RT_ENOSYS;
  129. }
  130. return result;
  131. }
  132. static rt_err_t rt_hwtimer_close(struct rt_device *dev)
  133. {
  134. rt_err_t result = RT_EOK;
  135. rt_hwtimer_t *timer;
  136. timer = (rt_hwtimer_t*)dev;
  137. if (timer->ops->deinit != RT_NULL)
  138. {
  139. timer->ops->deinit(timer);
  140. }
  141. else
  142. {
  143. result = -RT_ENOSYS;
  144. }
  145. dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
  146. dev->rx_indicate = RT_NULL;
  147. return result;
  148. }
  149. static rt_size_t rt_hwtimer_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
  150. {
  151. rt_hwtimer_t *timer;
  152. rt_hwtimerval_t tv;
  153. rt_uint32_t cnt;
  154. float t;
  155. timer = (rt_hwtimer_t *)dev;
  156. if (timer->ops->count_get == RT_NULL)
  157. return 0;
  158. cnt = timer->ops->count_get(timer);
  159. if (timer->info->cntmode == HWTIMER_CNTMODE_DW)
  160. {
  161. cnt = timer->info->maxcnt - cnt;
  162. }
  163. t = timer->overflow * timer->period_sec + cnt/(float)timer->freq;
  164. tv.sec = t;
  165. tv.usec = (t - tv.sec) * 1000000;
  166. size = size > sizeof(tv)? sizeof(tv) : size;
  167. rt_memcpy(buffer, &tv, size);
  168. return size;
  169. }
  170. static rt_size_t rt_hwtimer_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size)
  171. {
  172. rt_size_t len = 0;
  173. rt_hwtimerval_t *t;
  174. rt_err_t err;
  175. if (size == sizeof(rt_hwtimerval_t))
  176. {
  177. t = (rt_hwtimerval_t*)buffer;
  178. err = timeout_set((rt_hwtimer_t*)dev, t);
  179. if (err == RT_EOK)
  180. {
  181. len = size;
  182. }
  183. }
  184. return len;
  185. }
  186. static rt_err_t rt_hwtimer_control(struct rt_device *dev, rt_uint8_t cmd, void *args)
  187. {
  188. rt_err_t result = RT_EOK;
  189. rt_hwtimer_t *timer;
  190. timer = (rt_hwtimer_t *)dev;
  191. switch (cmd)
  192. {
  193. case HWTIMER_CTRL_START:
  194. {
  195. if (timer->ops->start != RT_NULL)
  196. {
  197. rt_hwtimer_mode_t opm;
  198. if ((timer->cycles <= 1) && (timer->mode == HWTIMER_MODE_ONESHOT))
  199. {
  200. opm = HWTIMER_MODE_ONESHOT;
  201. }
  202. else
  203. {
  204. opm = HWTIMER_MODE_PERIOD;
  205. }
  206. timer->overflow = 0;
  207. timer->ops->start(timer, opm);
  208. }
  209. else
  210. {
  211. result = -RT_ENOSYS;
  212. }
  213. }
  214. break;
  215. case HWTIMER_CTRL_STOP:
  216. {
  217. if (timer->ops->stop != RT_NULL)
  218. {
  219. timer->ops->stop(timer);
  220. }
  221. else
  222. {
  223. result = -RT_ENOSYS;
  224. }
  225. }
  226. break;
  227. case HWTIMER_CTRL_TIMEOUT_SET:
  228. {
  229. if (args == RT_NULL)
  230. {
  231. result = -RT_EEMPTY;
  232. break;
  233. }
  234. result = timeout_set(timer, (rt_hwtimerval_t*)args);
  235. }
  236. break;
  237. case HWTIMER_CTRL_FREQ_SET:
  238. {
  239. rt_uint32_t *f;
  240. if (args == RT_NULL)
  241. {
  242. result = -RT_EEMPTY;
  243. break;
  244. }
  245. f = (rt_uint32_t*)args;
  246. if ((*f > timer->info->maxfreq) || (*f < timer->info->minfreq))
  247. {
  248. result = -RT_ERROR;
  249. break;
  250. }
  251. if (timer->ops->control != RT_NULL)
  252. {
  253. result = timer->ops->control(timer, cmd, args);
  254. if (result == RT_EOK)
  255. {
  256. timer->freq = *f;
  257. }
  258. }
  259. else
  260. {
  261. result = -RT_ENOSYS;
  262. }
  263. }
  264. break;
  265. case HWTIMER_CTRL_INFO_GET:
  266. {
  267. if (args == RT_NULL)
  268. {
  269. result = -RT_EEMPTY;
  270. break;
  271. }
  272. *((struct rt_hwtimer_info*)args) = *timer->info;
  273. }
  274. case HWTIMER_CTRL_MODE_SET:
  275. {
  276. rt_hwtimer_mode_t *m;
  277. if (args == RT_NULL)
  278. {
  279. result = -RT_EEMPTY;
  280. break;
  281. }
  282. m = (rt_hwtimer_mode_t*)args;
  283. if ((*m != HWTIMER_MODE_ONESHOT) && (*m != HWTIMER_MODE_PERIOD))
  284. {
  285. result = -RT_ERROR;
  286. break;
  287. }
  288. timer->mode = *m;
  289. }
  290. break;
  291. default:
  292. {
  293. result = -RT_ENOSYS;
  294. }
  295. break;
  296. }
  297. return result;
  298. }
  299. void rt_device_hwtimer_isr(rt_hwtimer_t *timer)
  300. {
  301. RT_ASSERT(timer != RT_NULL);
  302. timer->overflow ++;
  303. if (timer->cycles != 0)
  304. {
  305. timer->cycles --;
  306. }
  307. if (timer->cycles == 0)
  308. {
  309. timer->cycles = timer->reload;
  310. if (timer->mode == HWTIMER_MODE_ONESHOT)
  311. {
  312. if (timer->ops->stop != RT_NULL)
  313. {
  314. timer->ops->stop(timer);
  315. }
  316. }
  317. if (timer->parent.rx_indicate != RT_NULL)
  318. {
  319. timer->parent.rx_indicate(&timer->parent, sizeof(struct rt_hwtimerval));
  320. }
  321. }
  322. }
  323. rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data)
  324. {
  325. struct rt_device *device;
  326. RT_ASSERT(timer != RT_NULL);
  327. RT_ASSERT(timer->ops != RT_NULL);
  328. RT_ASSERT(timer->info != RT_NULL);
  329. device = &(timer->parent);
  330. device->type = RT_Device_Class_HwTimer;
  331. device->rx_indicate = RT_NULL;
  332. device->tx_complete = RT_NULL;
  333. device->init = rt_hwtimer_init;
  334. device->open = rt_hwtimer_open;
  335. device->close = rt_hwtimer_close;
  336. device->read = rt_hwtimer_read;
  337. device->write = rt_hwtimer_write;
  338. device->control = rt_hwtimer_control;
  339. device->user_data = user_data;
  340. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  341. }