fw_pca.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ___FW_PCA_H___
  15. #define ___FW_PCA_H___
  16. #include "fw_conf.h"
  17. #include "fw_types.h"
  18. typedef enum
  19. {
  20. PCA_ClockSource_SysClkDiv12 = 0x00,
  21. PCA_ClockSource_SysClkDiv2 = 0x01,
  22. PCA_ClockSource_Timer0Overflow = 0x02,
  23. PCA_ClockSource_ExtClock = 0x03,
  24. PCA_ClockSource_SysClk = 0x04,
  25. PCA_ClockSource_SysClkDiv4 = 0x05,
  26. PCA_ClockSource_SysClkDiv6 = 0x06,
  27. PCA_ClockSource_SysClkDiv8 = 0x07,
  28. } PCA_ClockSource_t;
  29. typedef enum
  30. {
  31. PCA_WorkMode_None = 0x00, // n/a
  32. PCA_WorkMode_PWM_NonInterrupt = 0x42, // 6/7/8/10-bit PWM, no interrupt
  33. PCA_WorkMode_PWM_RiseInterrupt = 0x63, // 6/7/8/10-bit PWM, rising interrupt
  34. PCA_WorkMode_PWM_FallInterrupt = 0x53, // 6/7/8/10-bit PWM, falling interrupt
  35. PCA_WorkMode_PWM_EdgeInterrupt = 0x73, // 6/7/8/10-bit PWM, both edge interrupt
  36. PCA_WorkMode_CAP_16bitRising = 0x20, // 16-bit Capture Mode, capture triggered by rising edge on CCPn/PCAn
  37. PCA_WorkMode_CAP_16bitFalling = 0x10, // 16-bit Capture Mode, capture triggered by falling edge on CCPn/PCAn
  38. PCA_WorkMode_CAP_16bitEdge = 0x30, // 16-bit Capture Mode, capture triggered by both edge on CCPn/PCAn
  39. PCA_WorkMode_CAP_16bitTimer = 0x48, // 16-bit software timer
  40. PCA_WorkMode_CAP_16bitPulseOut = 0x4C, // 16-bit high-speed pulse output
  41. } PCA_WorkMode_t;
  42. typedef enum
  43. {
  44. PCA_PWM_BitWidth_8 = 0x00, // {EPCnH, CCAPnH[7:0]} {EPCnL, CCAPnL[7:0]}
  45. PCA_PWM_BitWidth_7 = 0x01, // {EPCnH, CCAPnH[6:0]} {EPCnL, CCAPnL[6:0]}
  46. PCA_PWM_BitWidth_6 = 0x02, // {EPCnH, CCAPnH[5:0]} {EPCnL, CCAPnL[5:0]}
  47. PCA_PWM_BitWidth_10 = 0x03, // {EPCnH, XCCAPnH[1:0], CCAPnH[7:0]} {EPCnL, XCCAPnL[1:0], CCAPnL[7:0]}
  48. } PCA_PWM_Bitwidth_t;
  49. typedef enum
  50. {
  51. // ECI CCP0 CCP1 CCP2
  52. PCA_AlterPort_P12_P11_P10_P37 = 0x00,
  53. PCA_AlterPort_P34_P35_P36_P37 = 0x01,
  54. PCA_AlterPort_P24_P25_P26_P27 = 0x10,
  55. } PCA_AlterPort_t;
  56. typedef enum
  57. {
  58. // ECI CCP0 CCP1 CCP2
  59. PCA_AlterPort_G1K08A_P55_P32_P33_P54 = 0x00,
  60. PCA_AlterPort_G1K08A_P55_P31_P33_P54 = 0x01,
  61. PCA_AlterPort_G1K08A_P31_P32_P33_P55 = 0x10,
  62. } PCA_AlterPort_G1K08A_t;
  63. typedef enum
  64. {
  65. // ECI CCP0 CCP1 CCP2
  66. PCA_AlterPort_G1K08T_P13_P11_P10_P37 = 0x00,
  67. PCA_AlterPort_G1K08T_P34_P35_P36_P37 = 0x01,
  68. PCA_AlterPort_G1K08T_P54_P13_P14_P15 = 0x10,
  69. } PCA_AlterPort_G1K08T_t;
  70. #define PCA_SetCounterState(__STATE__) SBIT_ASSIGN(CR, __STATE__)
  71. #define PCA_ClearCounterOverflowInterrupt() SBIT_RESET(CF)
  72. #define PCA_PCA0_ClearInterrupt() SBIT_RESET(CCF0)
  73. #define PCA_PCA1_ClearInterrupt() SBIT_RESET(CCF1)
  74. #define PCA_PCA2_ClearInterrupt() SBIT_RESET(CCF2)
  75. #define PCA_SetStopCounterInIdle(__STATE__) SFR_ASSIGN(CMOD, 7, __STATE__)
  76. #define PCA_SetClockSource(__SOURCE___) (CMOD = CMOD & ~(0x07 << 1) | ((__SOURCE___) << 1))
  77. #define PCA_EnableCounterOverflowInterrupt(__STATE__) SFR_ASSIGN(CMOD, 0, __STATE__)
  78. #define PCA_PCA0_SetWorkMode(__MODE__) (CCAPM0 = (__MODE__))
  79. #define PCA_PCA1_SetWorkMode(__MODE__) (CCAPM1 = (__MODE__))
  80. #define PCA_PCA2_SetWorkMode(__MODE__) (CCAPM2 = (__MODE__))
  81. #define PCA_PWM0_SetBitWidth(__BIT_WIDTH__) (PCA_PWM0 = PCA_PWM0 & ~(0x03 << 6) | ((__BIT_WIDTH__) << 6))
  82. #define PCA_PWM1_SetBitWidth(__BIT_WIDTH__) (PCA_PWM1 = PCA_PWM1 & ~(0x03 << 6) | ((__BIT_WIDTH__) << 6))
  83. #define PCA_PWM2_SetBitWidth(__BIT_WIDTH__) (PCA_PWM2 = PCA_PWM2 & ~(0x03 << 6) | ((__BIT_WIDTH__) << 6))
  84. /**
  85. * Set initiale comparison value
  86. */
  87. #define PCA_PCA0_SetCompareValue(__VALUE__) do{CCAP0H = (__VALUE__); CCAP0L = (__VALUE__);}while(0)
  88. #define PCA_PCA1_SetCompareValue(__VALUE__) do{CCAP1H = (__VALUE__); CCAP1L = (__VALUE__);}while(0)
  89. #define PCA_PCA2_SetCompareValue(__VALUE__) do{CCAP2H = (__VALUE__); CCAP2L = (__VALUE__);}while(0)
  90. /**
  91. * Change comparison value when running
  92. */
  93. #define PCA_PCA0_ChangeCompareValue(__VALUE__) (CCAP0H = (__VALUE__))
  94. #define PCA_PCA1_ChangeCompareValue(__VALUE__) (CCAP1H = (__VALUE__))
  95. #define PCA_PCA2_ChangeCompareValue(__VALUE__) (CCAP2H = (__VALUE__))
  96. /**
  97. * Set initiale comparison value
  98. */
  99. #define PCA_PCA0_SetCompareValue10bit(__VALUE__) do{ \
  100. PCA_PWM0 = PCA_PWM0 & ~(0x0F << 2) | (((__VALUE__) >> 4) & 0x30) | (((__VALUE__) >> 6) & 0x0C); \
  101. CCAP0H = ((__VALUE__) & 0xFF); CCAP0L = ((__VALUE__) & 0xFF); \
  102. }while(0)
  103. #define PCA_PCA1_SetCompareValue10bit(__VALUE__) do{ \
  104. PCA_PWM1 = PCA_PWM1 & ~(0x0F << 2) | (((__VALUE__) >> 4) & 0x30) | (((__VALUE__) >> 6) & 0x0C); \
  105. CCAP1H = ((__VALUE__) & 0xFF); CCAP1L = ((__VALUE__) & 0xFF); \
  106. }while(0)
  107. #define PCA_PCA2_SetCompareValue10bit(__VALUE__) do{ \
  108. PCA_PWM2 = PCA_PWM2 & ~(0x0F << 2) | (((__VALUE__) >> 4) & 0x30) | (((__VALUE__) >> 6) & 0x0C); \
  109. CCAP2H = ((__VALUE__) & 0xFF); CCAP2L = ((__VALUE__) & 0xFF); \
  110. }while(0)
  111. /**
  112. * Change comparison value in running mode
  113. * Note: set high 2bit first, then low 8bit, the hardware will make sure new 10bit be loaded as a whole number.
  114. * If you write low bits first, it will introduce glitchnoise.
  115. */
  116. #define PCA_PCA0_ChangeCompareValue10bit(__VALUE__) do{ \
  117. PCA_PWM0 = PCA_PWM0 & ~(0x03 << 4) | (((__VALUE__) >> 4) & 0x30); \
  118. CCAP0H = ((__VALUE__) & 0xFF); \
  119. }while(0)
  120. #define PCA_PCA1_ChangeCompareValue10bit(__VALUE__) do{ \
  121. PCA_PWM1 = PCA_PWM1 & ~(0x03 << 4) | (((__VALUE__) >> 4) & 0x30); \
  122. CCAP1H = ((__VALUE__) & 0xFF); \
  123. }while(0)
  124. #define PCA_PCA2_ChangeCompareValue10bit(__VALUE__) do{ \
  125. PCA_PWM2 = PCA_PWM2 & ~(0x03 << 4) | (((__VALUE__) >> 4) & 0x30); \
  126. CCAP2H = ((__VALUE__) & 0xFF); \
  127. }while(0)
  128. /**
  129. * Alternative port selection
  130. */
  131. #define PCA_SetPort(__ALTER_PORT__) (P_SW1 = P_SW1 & ~(0x03 << 4) | ((__ALTER_PORT__) << 4))
  132. #endif