hosal_pwm.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2016-2022 Bouffalolab.
  3. *
  4. * This file is part of
  5. * *** Bouffalolab Software Dev Kit ***
  6. * (see www.bouffalolab.com).
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of Bouffalo Lab nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __HOSAL_PWM_H__
  31. #define __HOSAL_PWM_H__
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /** @addtogroup hosal_pwm PWM
  36. * HOSAL PWM API
  37. *
  38. * @{
  39. */
  40. #include <stdint.h>
  41. /**
  42. * @brief pwm config struct
  43. *
  44. */
  45. typedef struct {
  46. uint8_t pin; /**< pwm pin */
  47. uint32_t duty_cycle; /**< the pwm duty_cycle 0 ~ 10000(0 ~ 100%)*/
  48. uint32_t freq; /**< the pwm freq,range is between 0 and 40M */
  49. } hosal_pwm_config_t;
  50. /**
  51. * @brief pwm dev struct
  52. *
  53. */
  54. typedef struct {
  55. uint8_t port; /**< pwm port */
  56. hosal_pwm_config_t config; /**< pwm config */
  57. void *priv; /**< priv data */
  58. } hosal_pwm_dev_t;
  59. /**
  60. * @brief Initialises a PWM pin
  61. *
  62. * @param[in] pwm the PWM device
  63. *
  64. * @return
  65. * - 0 : success
  66. * - other: fail
  67. */
  68. int hosal_pwm_init(hosal_pwm_dev_t *pwm);
  69. /**
  70. * @brief Starts Pulse-Width Modulation signal output on a PWM pin
  71. *
  72. * @param[in] pwm the PWM device
  73. *
  74. * @return
  75. * - 0 : success
  76. * - other : fail
  77. */
  78. int hosal_pwm_start(hosal_pwm_dev_t *pwm);
  79. /**
  80. * @brief Stops output on a PWM pin
  81. *
  82. * @param[in] pwm the PWM device
  83. *
  84. * @return
  85. * - 0 : success
  86. * - other: fail
  87. */
  88. int hosal_pwm_stop(hosal_pwm_dev_t *pwm);
  89. /**
  90. * @change the para of pwm
  91. *
  92. * @param[in] pwm the PWM device
  93. * @param[in] para the para of pwm
  94. *
  95. * @return
  96. * - 0 : success
  97. * - other: fail
  98. */
  99. int hosal_pwm_para_chg(hosal_pwm_dev_t *pwm, hosal_pwm_config_t para);
  100. /**
  101. * @brief update PWM frequency
  102. *
  103. * @param[in] pwm the PWM device
  104. * @param[in] freq the PWM frequency (0~40M under limited duty)
  105. *
  106. * @return
  107. * - 0 : success
  108. * - other: fail
  109. */
  110. int hosal_pwm_freq_set(hosal_pwm_dev_t *pwm, uint32_t freq);
  111. /**
  112. * @brief get PWM frequency
  113. *
  114. * @param[in] pwm the PWM device
  115. * @param[out] p_freq the pointer to memory frequency
  116. *
  117. * @return
  118. * - 0 : success
  119. * - other: fail
  120. */
  121. int hosal_pwm_freq_get(hosal_pwm_dev_t *pwm, uint32_t *p_freq);
  122. /**
  123. * @brief set PWM duty
  124. *
  125. * @param[in] pwm the PWM device
  126. * @param[in] duty the PWM duty (original duty * 100)
  127. *
  128. * @return
  129. * - 0 : success
  130. * - other: fail
  131. */
  132. int hosal_pwm_duty_set(hosal_pwm_dev_t *pwm, uint32_t duty);
  133. /**
  134. * @brief get PWM duty
  135. *
  136. * @param[in] pwm the PWM device
  137. * @param[out] p_duty the pointer to memory duty(original duty * 100)
  138. *
  139. * @return
  140. * - 0 : success
  141. * - other: fail
  142. */
  143. int hosal_pwm_duty_get(hosal_pwm_dev_t *pwm, uint32_t *p_duty);
  144. /**
  145. * @brief De-initialises an PWM interface, Turns off an PWM hardware interface
  146. *
  147. * @param[in] pwm the interface which should be de-initialised
  148. *
  149. * @return
  150. * - 0 : success
  151. * - other: fail
  152. */
  153. int hosal_pwm_finalize(hosal_pwm_dev_t *pwm);
  154. /** @} */
  155. #ifdef __cplusplus
  156. }
  157. #endif
  158. #endif /* HAL_PWM_H */