drv_pwm.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-12-04 Sundm75 the first version
  9. */
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. #include "ls1c.h"
  16. #include "../libraries/ls1c_public.h"
  17. #include "../libraries/ls1c_regs.h"
  18. #include "../libraries/ls1c_clock.h"
  19. #include "../libraries/ls1c_pwm.h"
  20. #include "../libraries/ls1c_pin.h"
  21. #define PWM_CHANNEL_MAX (4) /* 0-3*/
  22. #ifdef RT_USING_PWM
  23. struct rt_ls1c_pwm
  24. {
  25. struct rt_device_pwm parent;
  26. rt_uint32_t period[PWM_CHANNEL_MAX];
  27. rt_uint32_t pulse[PWM_CHANNEL_MAX];
  28. };
  29. static struct rt_ls1c_pwm _ls1c_pwm_device;
  30. static rt_err_t set(struct rt_device_pwm *device, struct rt_pwm_configuration *configuration)
  31. {
  32. rt_err_t result = RT_EOK;
  33. struct rt_ls1c_pwm *ls1c_pwm_device = (struct rt_ls1c_pwm *)device;
  34. if (configuration->channel > (PWM_CHANNEL_MAX - 1))
  35. {
  36. result = -RT_EIO;
  37. goto _exit;
  38. }
  39. rt_kprintf("drv_pwm.c set channel %d: period: %d, pulse: %d\n", configuration->channel, configuration->period, configuration->pulse);
  40. ls1c_pwm_device->period[configuration->channel] = configuration->period;
  41. ls1c_pwm_device->pulse[configuration->channel] = configuration->pulse;
  42. _exit:
  43. return result;
  44. }
  45. static rt_err_t get(struct rt_device_pwm *device, struct rt_pwm_configuration *configuration)
  46. {
  47. rt_err_t result = RT_EOK;
  48. struct rt_ls1c_pwm *ls1c_pwm_device = (struct rt_ls1c_pwm *)device;
  49. if (configuration->channel > (PWM_CHANNEL_MAX - 1))
  50. {
  51. result = -RT_EIO;
  52. goto _exit;
  53. }
  54. configuration->period = ls1c_pwm_device->period[configuration->channel];
  55. configuration->pulse = ls1c_pwm_device->pulse[configuration->channel];
  56. rt_kprintf("drv_pwm.c get channel %d: period: %d, pulse: %d\n", configuration->channel, configuration->period, configuration->pulse);
  57. _exit:
  58. return result;
  59. }
  60. static rt_err_t control(struct rt_device_pwm *device, int cmd, void *arg)
  61. {
  62. rt_err_t result = RT_EOK;
  63. struct rt_pwm_configuration * configuration = (struct rt_pwm_configuration *)arg;
  64. rt_kprintf("drv_pwm.c control cmd: %d. \n", cmd);
  65. if (cmd == PWM_CMD_ENABLE)
  66. {
  67. rt_kprintf("PWM_CMD_ENABLE\n");
  68. pwm_info_t pwm_info;
  69. switch ( configuration->channel)
  70. {
  71. case 0:
  72. pwm_info.gpio = LS1C_PWM0_GPIO06;
  73. //pwm_info.gpio = LS1C_PWM0_GPIO04;
  74. break;
  75. case 1:
  76. pwm_info.gpio = LS1C_PWM1_GPIO92;
  77. //pwm_info.gpio = LS1C_PWM1_GPIO05;
  78. break;
  79. case 2:
  80. pwm_info.gpio = LS1C_PWM2_GPIO52;
  81. //pwm_info.gpio = LS1C_PWM2_GPIO46;
  82. break;
  83. case 3:
  84. pwm_info.gpio = LS1C_PWM3_GPIO47;
  85. //pwm_info.gpio = LS1C_PWM3_GPIO53;
  86. break;
  87. default:
  88. break;
  89. }
  90. pwm_info.mode = PWM_MODE_NORMAL;
  91. pwm_info.duty = ( (float)configuration->pulse ) / ((float)configuration->period );
  92. pwm_info.period_ns = configuration->period;
  93. pwm_init(&pwm_info);
  94. pwm_enable(&pwm_info);
  95. }
  96. else if (cmd == PWM_CMD_DISABLE)
  97. {
  98. rt_kprintf("PWM_CMD_DISABLE\n");
  99. pwm_info_t pwm_info;
  100. switch ( configuration->channel)
  101. {
  102. case 0:
  103. pwm_info.gpio = LS1C_PWM0_GPIO06;
  104. //pwm_info.gpio = LS1C_PWM0_GPIO04;
  105. break;
  106. case 1:
  107. pwm_info.gpio = LS1C_PWM1_GPIO92;
  108. //pwm_info.gpio = LS1C_PWM1_GPIO05;
  109. break;
  110. case 2:
  111. pwm_info.gpio = LS1C_PWM2_GPIO52;
  112. //pwm_info.gpio = LS1C_PWM2_GPIO46;
  113. break;
  114. case 3:
  115. pwm_info.gpio = LS1C_PWM3_GPIO47;
  116. //pwm_info.gpio = LS1C_PWM3_GPIO53;
  117. break;
  118. default:
  119. break;
  120. }
  121. pwm_info.mode = PWM_MODE_NORMAL;
  122. pwm_info.duty = ( (float)configuration->pulse ) / ((float)configuration->period );
  123. pwm_info.period_ns = configuration->period;
  124. pwm_init(&pwm_info);
  125. pwm_disable(&pwm_info);
  126. }
  127. else if (cmd == PWM_CMD_SET)
  128. {
  129. rt_kprintf("PWM_CMD_SET\n");
  130. result = set(device, (struct rt_pwm_configuration *)arg);
  131. }
  132. else if (cmd == PWM_CMD_GET)
  133. {
  134. rt_kprintf("PWM_CMD_GET\n");
  135. result = get(device, (struct rt_pwm_configuration *)arg);
  136. }
  137. return result;
  138. }
  139. static const struct rt_pwm_ops pwm_ops =
  140. {
  141. control,
  142. };
  143. int rt_hw_pwm_init(void)
  144. {
  145. int ret = RT_EOK;
  146. /* add pwm initial. */
  147. ret = rt_device_pwm_register(&_ls1c_pwm_device.parent, "pwm", &pwm_ops, RT_NULL);
  148. return ret;
  149. }
  150. INIT_DEVICE_EXPORT(rt_hw_pwm_init);
  151. #endif /*RT_USING_PWM*/