nu_pwm.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**************************************************************************//**
  2. * @file pwm.h
  3. * @version V1.00
  4. * $Revision: 3 $
  5. * $Date: 15/05/19 10:16a $
  6. * @brief NUC980 series PWM driver header file
  7. *
  8. * @note
  9. * SPDX-License-Identifier: Apache-2.0
  10. * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  11. *****************************************************************************/
  12. #ifndef __NU_PWM_H__
  13. #define __NU_PWM_H__
  14. #include "nuc980.h"
  15. #include "nu_sys.h"
  16. #ifdef __cplusplus
  17. extern "C"
  18. {
  19. #endif
  20. /** @addtogroup Standard_Driver Standard Driver
  21. @{
  22. */
  23. /** @addtogroup PWM_Driver PWM Driver
  24. @{
  25. */
  26. /** @addtogroup PWM_EXPORTED_CONSTANTS PWM Exported Constants
  27. @{
  28. */
  29. #define PWM_OFFSET 0xc ///< each channel has 3 control registers which occupies 12 bytes
  30. // Timer channel identity information
  31. #define PWM_TIMER_NUM 8 ///< Total PWM channel count
  32. #define PWM_TIMER_MIN 0 ///< Min PWM channel number
  33. #define PWM_TIMER_MAX 7 ///< Max PWM channel number
  34. #define PWM0_TIMER0 0 ///< PWM0 channel 0
  35. #define PWM0_TIMER1 1 ///< PWM0 channel 1
  36. #define PWM0_TIMER2 2 ///< PWM0 channel 2
  37. #define PWM0_TIMER3 3 ///< PWM0 channel 3
  38. #define PWM1_TIMER0 4 ///< PWM1 channel 0
  39. #define PWM1_TIMER1 5 ///< PWM1 channel 1
  40. #define PWM1_TIMER2 6 ///< PWM1 channel 2
  41. #define PWM1_TIMER3 7 ///< PWM1 channel 3
  42. //ioctl command
  43. #define START_PWMTIMER 0 ///< Start PWM ioctl command
  44. #define STOP_PWMTIMER 1 ///< Stop PWM ioctl command
  45. #define SET_CSR 2 ///< Set CSR ioctl command
  46. #define SET_CP 3 ///< Set CP ioctl command
  47. #define SET_DZI 4 ///< Set dead zone ioctl command
  48. #define SET_INVERTER 5 ///< Set inverter ioctl command
  49. #define SET_MODE 6 ///< Set OP mode ioctl command
  50. #define ENABLE_DZ_GENERATOR 7 ///< Enable dead zone ioctl command
  51. #define DISABLE_DZ_GENERATOR 8 ///< Disable dead zone ioctl command
  52. #define ENABLE_PWMGPIOOUTPUT 9 ///< Enable PWM output ioctl command
  53. #define PWM_STOP_METHOD1 1 ///< PWM stop method 1
  54. #define PWM_STOP_METHOD2 2 ///< PWM stop method 2
  55. //#define PWM_STOP_METHOD3 3 not recommended
  56. //Timer default value
  57. #define DEFAULT_CSR CSRD16 ///< Default CSR value
  58. #define DEFAULT_CP 255 ///< Default CP value
  59. #define DEFAULT_DZI 50 ///< Default DZI value
  60. #define DEFAULT_CNR 19531 ///< Default CNR value
  61. #define DEFAULT_CMR (19531/4) ///< Default CMR value
  62. #define DEFAULT_MODE PWM_TOGGLE ///< Default OP mode
  63. // for PWM_PPR
  64. #define DZI_MIN 0 ///< Min DZI value
  65. #define DZI_MAX 255 ///< Max DZI value
  66. #define CP_MIN 0 ///< Min CP value
  67. #define CP_MAX 255 ///< Max CP value
  68. // for PWM_CSR
  69. #define CSR_MIN 0 ///< Min CSR value
  70. #define CSR_MAX 4 ///< Mac SCR value
  71. #define CSRD2 0x0 ///< Div by 2
  72. #define CSRD4 0x1 ///< Div by 4
  73. #define CSRD8 0x2 ///< Div by 8
  74. #define CSRD16 0x3 ///< Div by 16
  75. #define CSRD1 0x4 ///< Div by 1
  76. // for PWM_PCR
  77. #define PWMDZG_ENABLE 1 ///< Enable PWM dead zone
  78. #define PWMDZG_DISABLE 0 ///< Disable PWM dead zone
  79. #define PWM_ENABLE 1 ///< Enable PWM channel
  80. #define PWM_DISABLE 0 ///< Disable PWM channel
  81. #define PWM_TOGGLE 1 ///< PWM toggle mode
  82. #define PWM_ONESHOT 0 ///< PWM one-shot mode
  83. #define PWM_INVON 1 ///< Enable PWM inverter
  84. #define PWM_INVOFF 0 ///< Disable PWM inverter
  85. // for PWM_CNR
  86. #define CNR_MIN 0 ///< Min CNR value
  87. #define CNR_MAX 65535 ///< Mac CNR value
  88. // for PWM_CMR
  89. #define CMR_MIN 0 ///< Min CMR value
  90. #define CMR_MAX 65535 ///< Max CMR value
  91. // for pin control
  92. #define PWM00_GPF5 0 ///< PWM00 output on GPF5
  93. #define PWM01_GPF6 1 ///< PWM01 output on GPF6
  94. #define PWM02_GPF7 2 ///< PWM02 output on GPF7
  95. #define PWM03_GPF8 3 ///< PWM03 output on GPF8
  96. #define PWM00_GPG0 4 ///< PWM00 output on GPG0
  97. #define PWM01_GPG1 5 ///< PWM01 output on GPG1
  98. #define PWM02_GPG2 6 ///< PWM02 output on GPG2
  99. #define PWM03_GPG3 7 ///< PWM03 output on GPG3
  100. #define PWM00_GPD12 8 ///< PWM00 output on GPD12
  101. #define PWM01_GPD13 9 ///< PWM01 output on GPD13
  102. #define PWM02_GPD14 10 ///< PWM02 output on GPD14
  103. #define PWM03_GPD15 11 ///< PWM03 output on GPD15
  104. #define PWM00_GPG10 12 ///< PWM00 output on GPG10
  105. #define PWM01_GPA15 13 ///< PWM01 output on GPA15
  106. #define PWM02_GPA14 14 ///< PWM02 output on GPA14
  107. #define PWM03_GPA13 15 ///< PWM03 output on GPA13
  108. #define PWM02_GPB13 16 ///< PWM02 output on GPB13
  109. #define PWM10_GPB12 17 ///< PWM10 output on GPB12
  110. #define PWM11_GPB11 18 ///< PWM11 output on GPB11
  111. #define PWM12_GPB10 19 ///< PWM12 output on GPB10
  112. #define PWM13_GPB9 20 ///< PWM13 output on GPB9
  113. #define PWM10_GPG6 21 ///< PWM10 output on GPG6
  114. #define PWM11_GPG7 22 ///< PWM11 output on GPG7
  115. #define PWM12_GPG8 23 ///< PWM12 output on GPG8
  116. #define PWM13_GPG9 24 ///< PWM13 output on GPG9
  117. #define PWM10_GPG11 25 ///< PWM10 output on GPG11
  118. #define PWM11_GPG12 26 ///< PWM11 output on GPG12
  119. #define PWM12_GPG13 27 ///< PWM12 output on GPG13
  120. #define PWM13_GPG14 28 ///< PWM13 output on GPG14
  121. #define PWM10_GPF9 29 ///< PWM10 output on GPF9
  122. #define PWM11_GPF10 30 ///< PWM11 output on GPF10
  123. #define PWM12_GPE10 31 ///< PWM12 output on GPE10
  124. #define PWM13_GPE12 32 ///< PWM13 output on GPE12
  125. #define PWM_ERR_ID 0xFFFF1300 ///< PWM library ID
  126. //PWM Error code
  127. #define pwmInvalidTimerChannel (PWM_ERR_ID|1) ///< Invalid channel number
  128. #define pwmInvalidStructLength (PWM_ERR_ID|2) ///< Invalid structure length
  129. #define pwmInvalidIoctlCommand (PWM_ERR_ID|3) ///< Invalid ioctl command
  130. #define pwmInvalidStopMethod (PWM_ERR_ID|4) ///< Invalid stop mode
  131. #define pwmInvalidCPValue (PWM_ERR_ID|5) ///< Invalid CP value
  132. #define pwmInvalidDZIValue (PWM_ERR_ID|6) ///< Invalid DZI value
  133. #define pwmInvalidCSRValue (PWM_ERR_ID|7) ///< Invalid CSR value
  134. #define pwmInvalidDZGStatus (PWM_ERR_ID|8) ///< Invalid DZ status
  135. #define pwmInvalidTimerStatus (PWM_ERR_ID|9) ///< Invalid timer status
  136. #define pwmInvalidInverterValue (PWM_ERR_ID|10) ///< Invalid inverter value
  137. #define pwmInvalidModeStatus (PWM_ERR_ID|11) ///< Invalid OP mode
  138. #define pwmInvalidCNRValue (PWM_ERR_ID|12) ///< Invalid CNR value
  139. #define pwmInvalidCMRValue (PWM_ERR_ID|13) ///< Invalid CMR value
  140. #define pwmTimerNotOpen (PWM_ERR_ID|14) ///< PWM channel not stop
  141. #define pwmTimerBusy (PWM_ERR_ID|15) ///< PWM channel is busy
  142. #define pwmInvalidPin (PWM_ERR_ID|16) ///< Invalid PWM output pin
  143. /*@}*/ /* end of group PWM_EXPORTED_CONSTANTS */
  144. /// @cond HIDDEN_SYMBOLS
  145. /** @addtogroup PWM_EXPORTED_STRUCTS PWM Exported Structs
  146. @{
  147. */
  148. typedef union
  149. {
  150. UINT value;
  151. struct
  152. {
  153. UINT cp0: 8, cp1: 8, dzi0: 8, dzi1: 8;
  154. } field;
  155. } typePPR;
  156. typedef union
  157. {
  158. UINT value;
  159. struct
  160. {
  161. UINT csr0: 3, _reserved3: 1,
  162. csr1: 3, _reserved7: 1,
  163. csr2: 3, _reserved11: 1,
  164. csr3: 3, _reserved15: 1,
  165. _reserved16_31: 16;
  166. } field;
  167. } typeCSR;
  168. typedef union
  169. {
  170. UINT value;
  171. struct
  172. {
  173. UINT ch0_en: 1, _reserved1: 1, ch0_inverter: 1, ch0_mode: 1,
  174. grpup0_dzen: 1, grpup1_dzen: 1,
  175. _reserved6_7: 2,
  176. ch1_en: 1, _reserved9: 1, ch1_inverter: 1, ch1_mode: 1,
  177. ch2_en: 1, _reserved13: 1, ch2_inverter: 1, ch2_mode: 1,
  178. ch3_en: 1, _reserved17: 1, ch3_inverter: 1, ch3_mode: 1,
  179. _reserved20_31: 12;
  180. } field;
  181. } typePCR;
  182. typedef union
  183. {
  184. UINT value;
  185. struct
  186. {
  187. UINT cnr: 16, _reserved16_31: 16;
  188. } field;
  189. } typeCNR;
  190. typedef union
  191. {
  192. UINT value;
  193. struct
  194. {
  195. UINT cmr: 16, _reserved16_31: 16;
  196. } field;
  197. } typeCMR;
  198. // for write operation
  199. typedef union
  200. {
  201. UINT value;
  202. struct
  203. {
  204. UINT cnr: 16, cmr: 16;
  205. } field;
  206. } typePWMVALUE;
  207. // for read operation
  208. typedef struct
  209. {
  210. UINT volatile PDR;
  211. BOOL volatile InterruptFlag;
  212. BOOL _reversed0;
  213. BOOL _reversed1;
  214. BOOL _reversed2;
  215. } typePWMSTATUS;
  216. /*@}*/ /* end of group PWM_EXPORTED_STRUCTS */
  217. /// @endcond /* HIDDEN_SYMBOLS */
  218. /** @addtogroup PWM_EXPORTED_FUNCTIONS PWM Exported Functions
  219. @{
  220. */
  221. // function definition
  222. INT pwmInit(const INT nTimerIdentity);
  223. INT pwmExit(void);
  224. INT pwmOpen(const INT nTimerIdentity);
  225. INT pwmClose(const INT nTimerIdentity);
  226. INT pwmRead(const INT nTimerIdentity, PUCHAR pucStatusValue, const UINT uLength);
  227. INT pwmWrite(const INT nTimerIdentity, PUCHAR pucCNRCMRValue, const UINT uLength);
  228. INT pwmIoctl(const INT nTimerIdentity, const UINT uCommand, const UINT uIndication, UINT uValue);
  229. /*@}*/ /* end of group PWM_EXPORTED_FUNCTIONS */
  230. /*@}*/ /* end of group PWM_Driver */
  231. /*@}*/ /* end of group Standard_Driver */
  232. #ifdef __cplusplus
  233. }
  234. #endif
  235. #endif //__NU_PWM_H__
  236. /*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/