clock.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. #include "ap_cp.h"
  29. #include "hal_defs.h"
  30. #include "hal_mcu.h"
  31. #include "clock.h"
  32. #include "types.h"
  33. #include "gpio.h"
  34. #include "global_config.h"
  35. extern uint32_t hclk,pclk;
  36. extern uint32_t osal_sys_tick;
  37. void clk_gate_enable(MODULE_e module)
  38. {
  39. if(module <3)
  40. return;
  41. AP_PCR->CLKG |= BIT(module);
  42. }
  43. void clk_gate_disable(MODULE_e module)
  44. {
  45. if(module <3)
  46. return;
  47. AP_PCR->CLKG &= ~(BIT(module));
  48. }
  49. void clk_reset(MODULE_e module)
  50. {
  51. AP_PCR->RESET &= ~(BIT(module));
  52. AP_PCR->RESET |= BIT(module);
  53. }
  54. uint32_t clk_hclk(void)
  55. {
  56. return hclk;
  57. }
  58. uint32_t clk_pclk(void)
  59. {
  60. return pclk;
  61. }
  62. /*
  63. cp pclk domain:kscan,cp timer,bb.
  64. ap pclk domain:other modules except pwm.
  65. */
  66. uint32_t clk_ap_pclk(void)
  67. {
  68. return (hclk/((AP_PCR->APB_CLK & 0x0F) + 1));
  69. }
  70. uint32_t clk_cp_pclk(void)
  71. {
  72. return (hclk/(((AP_PCR->APB_CLK & 0xF0) >> 4) + 1));
  73. }
  74. bool clk_div_ap_pclk(uint32_t div)
  75. {
  76. uint8_t divider;
  77. if((div == 0) || (div > 16))
  78. {
  79. return FALSE;
  80. }
  81. divider = div -1;
  82. if((AP_PCR->APB_CLK & 0x0F) != divider)
  83. {
  84. subWriteReg((uint32_t)(&(AP_PCR->APB_CLK)),3,0,divider);
  85. AP_PCR->APB_CLK_U = 3;
  86. }
  87. return TRUE;
  88. }
  89. bool clk_div_cp_pclk(uint32_t div)
  90. {
  91. uint8_t divider;
  92. if((div == 0) || (div > 16))
  93. {
  94. return FALSE;
  95. }
  96. divider = div -1;
  97. if(((AP_PCR->APB_CLK & 0xF0)>>4) != divider)
  98. {
  99. subWriteReg((uint32_t)(&(AP_PCR->APB_CLK)),7,4,divider);
  100. AP_PCR->APB_CLK_U = 3;
  101. pclk = (hclk/(divider + 1));
  102. }
  103. return TRUE;
  104. }
  105. /**************************************************************************************
  106. * @fn hal_rtc_clock_config
  107. *
  108. * @brief This function process for 32768Hz Clock configuation
  109. *
  110. * input parameters
  111. *
  112. * @param CLK32K_e clk32Mode: CLK_32K_RC_LOWPOWER --> use 32K RC osc for extra low power
  113. : CLK_32K_RC_STABLE --> use 32K RC osc for stable conectivity
  114. : CLK_32K_XTAL --> use 32K Xtal
  115. *
  116. * output parameters
  117. *
  118. * @param None.
  119. *
  120. * @return None
  121. **************************************************************************************/
  122. void hal_rtc_clock_config(uint8_t clk32Mode)
  123. {
  124. if(clk32Mode == CLK_32K_RCOSC)
  125. {
  126. subWriteReg(0x4000f014,31,27,0x05);
  127. subWriteReg(0x4000f01c,16,7,0x3fb); //software control 32k_clk
  128. subWriteReg(0x4000f01c,6,6 ,0x01); //enable software control
  129. pGlobal_config[LL_SWITCH]|=RC32_TRACKINK_ALLOW|LL_RC32K_SEL;
  130. // //disable smart windwo for stable conectivity not higher power consumption
  131. // pGlobal_config[LL_SMART_WINDOW_COEF_ALPHA]=0;
  132. }
  133. else if(clk32Mode == CLK_32K_XTAL)
  134. {
  135. // P16 P17 for 32K XTAL input
  136. hal_gpio_pull_set(P16,FLOATING);
  137. hal_gpio_pull_set(P17,FLOATING);
  138. subWriteReg(0x4000f01c,9,8,0x03); //software control 32k_clk
  139. subWriteReg(0x4000f01c,6,6,0x00); //disable software control
  140. subWriteReg(0x4000f014,31,27,0x16);
  141. pGlobal_config[LL_SWITCH]&=0xffffffee;
  142. }
  143. }
  144. uint32_t hal_systick(void)
  145. {
  146. return osal_sys_tick;
  147. }
  148. uint32_t hal_ms_intv(uint32_t tick)
  149. {
  150. uint32_t diff = 0;
  151. if(osal_sys_tick < tick){
  152. diff = 0xffffffff- tick;
  153. diff = osal_sys_tick + diff;
  154. }
  155. else
  156. {
  157. diff = osal_sys_tick - tick;
  158. }
  159. return diff*625/1000;
  160. }