pwm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * This file is part of FH8620 BSP for RT-Thread distribution.
  3. *
  4. * Copyright (c) 2016 Shanghai Fullhan Microelectronics Co., Ltd.
  5. * All rights reserved
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Visit http://www.fullhan.com to get contact with Fullhan.
  22. *
  23. * Change Logs:
  24. * Date Author Notes
  25. */
  26. #include "fh_def.h"
  27. #include "pwm.h"
  28. #include "interrupt.h"
  29. #include "board_info.h"
  30. #include "inc/fh_driverlib.h"
  31. #include <rtdevice.h>
  32. #ifdef FH_PWM_DEBUG
  33. #define PRINT_PWM_DBG(fmt, args...) \
  34. do \
  35. { \
  36. rt_kprintf("FH_PWM_DEBUG: "); \
  37. rt_kprintf(fmt, ## args); \
  38. } \
  39. while(0)
  40. #else
  41. #define PRINT_PWM_DBG(fmt, args...) do { } while (0)
  42. #endif
  43. static struct pwm_driver pwm_drv =
  44. {
  45. };
  46. static int pwm_get_duty_cycle_ns(struct pwm_device* pwm)
  47. {
  48. struct fh_pwm_obj *pwm_obj = (struct fh_pwm_obj *)pwm_drv.priv;
  49. rt_uint32_t reg, period, duty;
  50. rt_uint32_t clk_rate = 1000000/*todo: clk_get_rate(fh_pwm_ctrl.clk)*/;
  51. reg = PWM_GetPwmCmd(pwm_obj, pwm->id);
  52. period = reg & 0x0fff;
  53. duty = (reg >> 16) & 0xfff;
  54. duty = period - duty; //reverse duty cycle
  55. if(period == 0)
  56. {
  57. period = duty;
  58. }
  59. pwm->counter_ns = duty * 1000000000 / clk_rate;
  60. pwm->period_ns = period * 1000000000 / clk_rate;
  61. PRINT_PWM_DBG("get duty: %d, period: %d, reg: 0x%x\n", duty, period, reg);
  62. return 0;
  63. }
  64. static int pwm_set_duty_cycle_ns(struct pwm_device* pwm)
  65. {
  66. struct fh_pwm_obj *pwm_obj = (struct fh_pwm_obj *)pwm_drv.priv;
  67. rt_uint32_t period, duty, reg, clk_rate, duty_revert;
  68. clk_rate = 1000000/*todo: clk_get_rate(fh_pwm_ctrl.clk)*/;
  69. if(!clk_rate)
  70. {
  71. rt_kprintf("PWM: clock rate is 0\n");
  72. return -RT_EIO;
  73. }
  74. period = pwm->period_ns / (1000000000 / clk_rate);
  75. if(period < 8)
  76. {
  77. rt_kprintf("PWM: min period is 8\n");
  78. return -RT_EIO;
  79. }
  80. duty = pwm->counter_ns / (1000000000 / clk_rate);
  81. if(period < duty)
  82. {
  83. rt_kprintf("PWM: period < duty\n");
  84. return -RT_EIO;
  85. }
  86. duty_revert = period - duty;
  87. if(duty == period)
  88. {
  89. reg = (duty & 0xfff) << 16 | (0 & 0xfff);
  90. }
  91. else
  92. {
  93. reg = (duty_revert & 0xfff) << 16 | (period & 0xfff);
  94. }
  95. PRINT_PWM_DBG("set duty_revert: %d, period: %d, reg: 0x%x\n", duty_revert, period, reg);
  96. PWM_SetPwmCmd(pwm_obj, pwm->id, reg);
  97. return 0;
  98. }
  99. static rt_err_t fh_pwm_open(rt_device_t dev, rt_uint16_t oflag)
  100. {
  101. struct fh_pwm_obj *pwm_obj = (struct fh_pwm_obj *)pwm_drv.priv;
  102. PWM_Enable(pwm_obj, RT_TRUE);
  103. return 0;
  104. }
  105. static rt_err_t fh_pwm_close(rt_device_t dev)
  106. {
  107. struct fh_pwm_obj *pwm_obj = (struct fh_pwm_obj *)pwm_drv.priv;
  108. PWM_Enable(pwm_obj, RT_FALSE);
  109. return 0;
  110. }
  111. static rt_err_t fh_pwm_ioctl(rt_device_t dev, int cmd, void *arg)
  112. {
  113. int ret = 0;
  114. struct pwm_device *pwm;
  115. struct fh_pwm_obj *pwm_obj = (struct fh_pwm_obj *)pwm_drv.priv;
  116. switch(cmd)
  117. {
  118. case ENABLE_PWM:
  119. PWM_Enable(pwm_obj, RT_FALSE);
  120. break;
  121. case DISABLE_PWM:
  122. PWM_Enable(pwm_obj, RT_TRUE);
  123. break;
  124. case SET_PWM_DUTY_CYCLE:
  125. pwm = (struct pwm_device *)arg;
  126. PRINT_PWM_DBG("ioctl: pwm addr: %p, pwm->period: %d ns\n", pwm, pwm->period_ns);
  127. pwm_set_duty_cycle_ns(pwm);
  128. break;
  129. case GET_PWM_DUTY_CYCLE:
  130. pwm = (struct pwm_device *)arg;
  131. PRINT_PWM_DBG("ioctl: pwm->id: %d, pwm->counter: %d, pwm->period: %d\n", pwm->id, pwm->counter_ns, pwm->period_ns);
  132. pwm_get_duty_cycle_ns(pwm);
  133. break;
  134. }
  135. return ret;
  136. }
  137. int fh_pwm_probe(void *priv_data)
  138. {
  139. rt_device_t pwm_dev ;
  140. struct fh_pwm_obj *pwm_obj = (struct fh_pwm_obj *)priv_data;
  141. rt_memset(&pwm_drv, 0, sizeof(struct pwm_driver));
  142. pwm_drv.pwm[0].id = 0;
  143. pwm_drv.pwm[1].id = 1;
  144. pwm_drv.pwm[2].id = 2;
  145. pwm_drv.pwm[0].working = 0;
  146. pwm_drv.pwm[1].working = 0;
  147. pwm_drv.pwm[2].working = 0;
  148. pwm_drv.priv = pwm_obj;
  149. //todo: clk
  150. PWM_Enable(pwm_obj, RT_FALSE);
  151. pwm_dev = rt_malloc(sizeof(struct rt_device));
  152. rt_memset(pwm_dev, 0, sizeof(struct rt_device));
  153. if (pwm_dev == RT_NULL)
  154. {
  155. rt_kprintf("ERROR: %s rt_device malloc failed\n", __func__);
  156. }
  157. pwm_dev->user_data = &pwm_drv;
  158. pwm_dev->open =fh_pwm_open;
  159. pwm_dev->close = fh_pwm_close;
  160. pwm_dev->control = fh_pwm_ioctl;
  161. pwm_dev->type = RT_Device_Class_Miscellaneous;
  162. rt_device_register(pwm_dev, "pwm", RT_DEVICE_FLAG_RDWR);
  163. return 0;
  164. }
  165. int fh_pwm_exit(void *priv_data)
  166. {
  167. return 0;
  168. }
  169. struct fh_board_ops pwm_driver_ops =
  170. {
  171. .probe = fh_pwm_probe,
  172. .exit = fh_pwm_exit,
  173. };
  174. void rt_hw_pwm_init(void)
  175. {
  176. PRINT_PWM_DBG("%s start\n", __func__);
  177. fh_board_driver_register("pwm", &pwm_driver_ops);
  178. PRINT_PWM_DBG("%s end\n", __func__);
  179. }