nu_pwm.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**************************************************************************//**
  2. * @file pwm.h
  3. * @brief N9H30 series PWM driver header file
  4. *
  5. * @note
  6. * SPDX-License-Identifier: Apache-2.0
  7. * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #ifndef __NU_PWM_H__
  10. #define __NU_PWM_H__
  11. #include "N9H30.h"
  12. #include "nu_sys.h"
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. /** @addtogroup N9H30_Device_Driver N9H30 Device Driver
  18. @{
  19. */
  20. /** @addtogroup N9H30_PWM_Driver PWM Driver
  21. @{
  22. */
  23. /** @addtogroup N9H30_PWM_EXPORTED_CONSTANTS PWM Exported Constants
  24. @{
  25. */
  26. #define PWM_OFFSET 0xc ///< each channel has 3 control registers which occupies 12 bytes
  27. // Timer channel identity information
  28. #define PWM_TIMER_NUM 4 ///< Total PWM channel count
  29. #define PWM_TIMER_MIN 0 ///< Min PWM channel number
  30. #define PWM_TIMER_MAX 3 ///< Max PWM channel number
  31. #define PWM_TIMER0 0 ///< PWM channel 0
  32. #define PWM_TIMER1 1 ///< PWM channel 1
  33. #define PWM_TIMER2 2 ///< PWM channel 2
  34. #define PWM_TIMER3 3 ///< PWM channel 3
  35. //ioctl command
  36. #define START_PWMTIMER 0 ///< Start PWM ioctl command
  37. #define STOP_PWMTIMER 1 ///< Stop PWM ioctl command
  38. #define SET_CSR 2 ///< Set CSR ioctl command
  39. #define SET_CP 3 ///< Set CP ioctl command
  40. #define SET_DZI 4 ///< Set dead zone ioctl command
  41. #define SET_INVERTER 5 ///< Set inverter ioctl command
  42. #define SET_MODE 6 ///< Set OP mode ioctl command
  43. #define ENABLE_DZ_GENERATOR 7 ///< Enable dead zone ioctl command
  44. #define DISABLE_DZ_GENERATOR 8 ///< Disable dead zone ioctl command
  45. #define ENABLE_PWMGPIOOUTPUT 9 ///< Enable PWM output ioctl command
  46. #define PWM_STOP_METHOD1 1 ///< PWM stop method 1
  47. #define PWM_STOP_METHOD2 2 ///< PWM stop method 2
  48. //#define PWM_STOP_METHOD3 3 not recommended
  49. //Timer default value
  50. #define DEFAULT_CSR CSRD16 ///< Default CSR value
  51. #define DEFAULT_CP 255 ///< Default CP value
  52. #define DEFAULT_DZI 50 ///< Default DZI value
  53. #define DEFAULT_CNR 19531 ///< Default CNR value
  54. #define DEFAULT_CMR (19531/4) ///< Default CMR value
  55. #define DEFAULT_MODE PWM_TOGGLE ///< Default OP mode
  56. // for PWM_PPR
  57. #define DZI_MIN 0 ///< Min DZI value
  58. #define DZI_MAX 255 ///< Max DZI value
  59. #define CP_MIN 0 ///< Min CP value
  60. #define CP_MAX 255 ///< Max CP value
  61. // for PWM_CSR
  62. #define CSR_MIN 0 ///< Min CSR value
  63. #define CSR_MAX 4 ///< Mac SCR value
  64. #define CSRD2 0x0 ///< Div by 2
  65. #define CSRD4 0x1 ///< Div by 4
  66. #define CSRD8 0x2 ///< Div by 8
  67. #define CSRD16 0x3 ///< Div by 16
  68. #define CSRD1 0x4 ///< Div by 1
  69. // for PWM_PCR
  70. #define PWMDZG_ENABLE 1 ///< Enable PWM dead zone
  71. #define PWMDZG_DISABLE 0 ///< Disable PWM dead zone
  72. #define PWM_ENABLE 1 ///< Enable PWM channel
  73. #define PWM_DISABLE 0 ///< Disable PWM channel
  74. #define PWM_TOGGLE 1 ///< PWM toggle mode
  75. #define PWM_ONESHOT 0 ///< PWM one-shot mode
  76. #define PWM_INVON 1 ///< Enable PWM inverter
  77. #define PWM_INVOFF 0 ///< Disable PWM inverter
  78. // for PWM_CNR
  79. #define CNR_MIN 0 ///< Min CNR value
  80. #define CNR_MAX 65535 ///< Mac CNR value
  81. // for PWM_CMR
  82. #define CMR_MIN 0 ///< Min CMR value
  83. #define CMR_MAX 65535 ///< Max CMR value
  84. // for pin control
  85. #define PWM0_GPA12 0 ///< PWM0 output on GPA12
  86. #define PWM0_GPB2 1 ///< PWM0 output on GPB2
  87. #define PWM1_GPA13 4 ///< PWM1 output on GPA13
  88. #define PWM1_GPB3 5 ///< PWM1 output on GPB3
  89. #define PWM2_GPA14 7 ///< PWM2 output on GPA14
  90. #define PWM2_GPH2 9 ///< PWM2 output on GPH2
  91. #define PWM3_GPA15 10 ///< PWM3 output on GPA15
  92. #define PWM3_GPH3 12 ///< PWM3 output on GPH3
  93. #define PWM_ERR_ID 0xFFFF1300 ///< PWM library ID
  94. //PWM Error code
  95. #define pwmInvalidTimerChannel (PWM_ERR_ID|1) ///< Invalid channel number
  96. #define pwmInvalidStructLength (PWM_ERR_ID|2) ///< Invalid structure length
  97. #define pwmInvalidIoctlCommand (PWM_ERR_ID|3) ///< Invalid ioctl command
  98. #define pwmInvalidStopMethod (PWM_ERR_ID|4) ///< Invalid stop mode
  99. #define pwmInvalidCPValue (PWM_ERR_ID|5) ///< Invalid CP value
  100. #define pwmInvalidDZIValue (PWM_ERR_ID|6) ///< Invalid DZI value
  101. #define pwmInvalidCSRValue (PWM_ERR_ID|7) ///< Invalid CSR value
  102. #define pwmInvalidDZGStatus (PWM_ERR_ID|8) ///< Invalid DZ status
  103. #define pwmInvalidTimerStatus (PWM_ERR_ID|9) ///< Invalid timer status
  104. #define pwmInvalidInverterValue (PWM_ERR_ID|10) ///< Invalid inverter value
  105. #define pwmInvalidModeStatus (PWM_ERR_ID|11) ///< Invalid OP mode
  106. #define pwmInvalidCNRValue (PWM_ERR_ID|12) ///< Invalid CNR value
  107. #define pwmInvalidCMRValue (PWM_ERR_ID|13) ///< Invalid CMR value
  108. #define pwmTimerNotOpen (PWM_ERR_ID|14) ///< PWM channel not stop
  109. #define pwmTimerBusy (PWM_ERR_ID|15) ///< PWM channel is busy
  110. #define pwmInvalidPin (PWM_ERR_ID|16) ///< Invalid PWM output pin
  111. /*@}*/ /* end of group N9H30_PWM_EXPORTED_CONSTANTS */
  112. /// @cond HIDDEN_SYMBOLS
  113. /** @addtogroup N9H30_PWM_EXPORTED_STRUCTS PWM Exported Structs
  114. @{
  115. */
  116. typedef union
  117. {
  118. UINT value;
  119. struct
  120. {
  121. UINT cp0: 8, cp1: 8, dzi0: 8, dzi1: 8;
  122. } field;
  123. } typePPR;
  124. typedef union
  125. {
  126. UINT value;
  127. struct
  128. {
  129. UINT csr0: 3, _reserved3: 1,
  130. csr1: 3, _reserved7: 1,
  131. csr2: 3, _reserved11: 1,
  132. csr3: 3, _reserved15: 1,
  133. _reserved16_31: 16;
  134. } field;
  135. } typeCSR;
  136. typedef union
  137. {
  138. UINT value;
  139. struct
  140. {
  141. UINT ch0_en: 1, _reserved1: 1, ch0_inverter: 1, ch0_mode: 1,
  142. grpup0_dzen: 1, grpup1_dzen: 1,
  143. _reserved6_7: 2,
  144. ch1_en: 1, _reserved9: 1, ch1_inverter: 1, ch1_mode: 1,
  145. ch2_en: 1, _reserved13: 1, ch2_inverter: 1, ch2_mode: 1,
  146. ch3_en: 1, _reserved17: 1, ch3_inverter: 1, ch3_mode: 1,
  147. _reserved20_31: 12;
  148. } field;
  149. } typePCR;
  150. typedef union
  151. {
  152. UINT value;
  153. struct
  154. {
  155. UINT cnr: 16, _reserved16_31: 16;
  156. } field;
  157. } typeCNR;
  158. typedef union
  159. {
  160. UINT value;
  161. struct
  162. {
  163. UINT cmr: 16, _reserved16_31: 16;
  164. } field;
  165. } typeCMR;
  166. // for write operation
  167. typedef union
  168. {
  169. UINT value;
  170. struct
  171. {
  172. UINT cnr: 16, cmr: 16;
  173. } field;
  174. } typePWMVALUE;
  175. // for read operation
  176. typedef struct
  177. {
  178. UINT volatile PDR;
  179. BOOL volatile InterruptFlag;
  180. BOOL _reversed0;
  181. BOOL _reversed1;
  182. BOOL _reversed2;
  183. } typePWMSTATUS;
  184. /*@}*/ /* end of group N9H30_PWM_EXPORTED_STRUCTS */
  185. /// @endcond /* HIDDEN_SYMBOLS */
  186. /** @addtogroup N9H30_PWM_EXPORTED_FUNCTIONS PWM Exported Functions
  187. @{
  188. */
  189. // function definition
  190. INT pwmInit(void);
  191. INT pwmExit(void);
  192. INT pwmOpen(const INT nTimerIdentity);
  193. INT pwmClose(const INT nTimerIdentity);
  194. INT pwmRead(const INT nTimerIdentity, PUCHAR pucStatusValue, const UINT uLength);
  195. INT pwmWrite(const INT nTimerIdentity, PUCHAR pucCNRCMRValue, const UINT uLength);
  196. INT pwmIoctl(const INT nTimerIdentity, const UINT uCommand, const UINT uIndication, UINT uValue);
  197. /*@}*/ /* end of group N9H30_PWM_EXPORTED_FUNCTIONS */
  198. /*@}*/ /* end of group N9H30_PWM_Driver */
  199. /*@}*/ /* end of group N9H30_Device_Driver */
  200. #ifdef __cplusplus
  201. }
  202. #endif
  203. #endif //__NU_PWM_H__
  204. /*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/