fw_sys.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_SYS_H___
  15. #define ___FW_SYS_H___
  16. #include "fw_conf.h"
  17. #include "fw_types.h"
  18. typedef enum
  19. {
  20. SYS_ExternalOSC_Clock = 0x00,
  21. SYS_ExternalOSC_Crystal = 0x01,
  22. } SYS_ExternalOSC_t;
  23. typedef enum
  24. {
  25. SYS_SysClkOutputPort_P54 = 0x00,
  26. SYS_SysClkOutputPort_P16 = 0x01,
  27. } SYS_SysClkOutputPort_t;
  28. /**
  29. * STC8H Clock:
  30. * MCKSEL ||===> MCLKODIV ==> MCLKO_S => P1.6/P5.4
  31. * 00 Internal IRC | ||
  32. * 01 External OSC |==> CLKDIV ==> SYSCLK
  33. * 10 External 32KHz |
  34. * 11 Internal 32KHz |
  35. */
  36. /**
  37. * Precompute system clock based on __CONF_FOSC and __CONF_CLKDIV
  38. */
  39. #define __SYSCLOCK (__CONF_FOSC / ((__CONF_CLKDIV == 0) ? 1 : __CONF_CLKDIV))
  40. #define SYS_SetFOSC(__IRCBAND__, __VRTRIM__, __IRTRIM__, __LIRTRIM__) do { \
  41. IRCBAND = ((__IRCBAND__) & 0x03); \
  42. VRTRIM = (__VRTRIM__); \
  43. IRTRIM = (__IRTRIM__); \
  44. LIRTRIM = ((__LIRTRIM__) & 0x03); \
  45. } while(0)
  46. /**
  47. * Enable high speed internal oscillator
  48. */
  49. #define SYS_EnableOscillatorHSI() do { \
  50. SFRX_ON(); \
  51. (HIRCCR) = (HIRCCR) | (0x01 << 7); \
  52. while (!(HIRCCR & 0x01)); \
  53. SFRX_OFF(); \
  54. } while(0)
  55. /**
  56. * Disable high speed internal oscillator
  57. */
  58. #define SYS_DisableOscillatorHSI() do { \
  59. SFRX_ON(); \
  60. (HIRCCR) = (HIRCCR) & ~(0x01 << 7); \
  61. SFRX_OFF(); \
  62. } while(0)
  63. /**
  64. * Enable low speed internal oscillator
  65. */
  66. #define SYS_EnableOscillatorLSI() do { \
  67. SFRX_ON(); \
  68. (IRC32KCR) = (IRC32KCR) | (0x01 << 7); \
  69. while (!(IRC32KCR & 0x01)); \
  70. SFRX_OFF(); \
  71. } while(0)
  72. /**
  73. * Disable low speed internal oscillator
  74. */
  75. #define SYS_DisableOscillatorLSI() do { \
  76. SFRX_ON(); \
  77. (IRC32KCR) = (IRC32KCR) & ~(0x01 << 7); \
  78. SFRX_OFF(); \
  79. } while(0)
  80. /**
  81. * Enable high speed external oscillator
  82. */
  83. #define SYS_EnableOscillatorHSE() do { \
  84. SFRX_ON(); \
  85. (XOSCCR) = (XOSCCR) | (0x01 << 7); \
  86. while (!(HIRCCR & 0x01)); \
  87. SFRX_OFF(); \
  88. } while(0)
  89. /**
  90. * Disable high speed external oscillator
  91. */
  92. #define SYS_DisableOscillatorHSE() do { \
  93. SFRX_ON(); \
  94. (XOSCCR) = (XOSCCR) & ~(0x01 << 7); \
  95. SFRX_OFF(); \
  96. } while(0)
  97. /**
  98. * Set high speed external oscillator type
  99. */
  100. #define SYS_SetExternalOscType(__TYPE__) SFRX_ASSIGN(XOSCCR, 6, (__TYPE__))
  101. /**
  102. * Enable low speed external oscillator
  103. */
  104. #define SYS_EnableOscillatorLSE() do { \
  105. SFRX_ON(); \
  106. (X32KCR) = (X32KCR) | (0x01 << 7); \
  107. while (!(X32KCR & 0x01)); \
  108. SFRX_OFF(); \
  109. } while(0)
  110. /**
  111. * Disable low speed external oscillator
  112. */
  113. #define SYS_DisableOscillatorLSE() do { \
  114. SFRX_ON(); \
  115. (X32KCR) = (X32KCR) & ~(0x01 << 7); \
  116. SFRX_OFF(); \
  117. } while(0)
  118. /**
  119. * Set low speed external oscillator gain
  120. */
  121. #define SYS_SetExternal32kHighGain(__STATE__) SFRX_ASSIGN(X32KCR, 6, (__STATE__))
  122. /**
  123. * System clock output
  124. * 0:No output, Foutput = SYSCLK / __DIV__
  125. */
  126. #define SYS_SetSysClockOutputDivider(__DIV__) do { \
  127. SFRX_ON(); \
  128. (MCLKOCR) = (MCLKOCR) & ~(0x7F) | (__DIV__); \
  129. SFRX_OFF(); \
  130. } while(0)
  131. /**
  132. * System clock output pin
  133. * 0:P5.4, 1:P1.6
  134. */
  135. #define SYS_SetClockOutputPin(__PORT__) SFRX_ASSIGN(MCLKOCR, 7, (__STATE__))
  136. /**
  137. * Enable 48MHz USB OSC
  138. */
  139. #define SYS_EnableOscillator48M() do { \
  140. SFRX_ON(); \
  141. RSTFLAG = 0x07; \
  142. (IRC48MCR) = (IRC48MCR) | (0x01 << 7); \
  143. while (!(IRC48MCR & 0x01)); \
  144. SFRX_OFF(); \
  145. } while(0)
  146. /**
  147. * Disable 48MHz USB OSC
  148. */
  149. #define SYS_DisableOscillator48M() do { \
  150. SFRX_ON(); \
  151. (IRC48MCR) = (IRC48MCR) & ~(0x01 << 7); \
  152. SFRX_OFF(); \
  153. } while(0)
  154. void SYS_SetClock(void);
  155. void SYS_TrimClock(uint8_t vrtrim, uint8_t irtrim);
  156. void SYS_Delay(uint16_t t);
  157. void SYS_DelayUs(uint16_t t);
  158. #endif