ble_idle.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "Includes.h"
  2. #define RST_READY_TIME 0x07 //-
  3. #define OSC32_STABLE_TIME 47// 2ms:47 6ms 180
  4. #define WAIT_BLE_EXIT_LOWPWR_TIME 10
  5. #define PRE_WAKEUP_TIME (RST_READY_TIME + OSC32_STABLE_TIME + WAIT_BLE_EXIT_LOWPWR_TIME)
  6. #define CPR_SYS_TIME ((volatile unsigned *)(0x40002400 + 0x1C))
  7. #define CPR_SLP_CTL ((volatile unsigned *)(0x40002400 + 0x4))
  8. #define CPR_SLPCTL_INT_MASK ((volatile unsigned *)(0x40002400 + 0x8))
  9. #define CPR_SLP_PD_MASK ((volatile unsigned *)(0x40002400 + 0xC))
  10. #define CPR_SLP_SRC_MASK ((volatile unsigned *)(0x40000000 + 0x000))
  11. static uint32_t system_wakeup(uint16_t val)
  12. {
  13. Timer_disable(1);
  14. return 0;
  15. }
  16. #define CPR_AO_BASE 0x40002400
  17. void ble_system_idle_init(void)
  18. {
  19. __write_hw_reg32(CPR_SYS_TIME, ((RST_READY_TIME<<12) | OSC32_STABLE_TIME));
  20. __write_hw_reg32(CPR_SLP_CTL, 0x00);
  21. __write_hw_reg32(CPR_SLPCTL_INT_MASK, 0xFFFFFfBF);
  22. __write_hw_reg32(CPR_SLP_PD_MASK,0x101);
  23. __write_hw_reg32(CPR_SLP_SRC_MASK, 0x60006);
  24. *((volatile unsigned *)(CPR_AO_BASE + 0x40)) &=0x0F; //ROM断电
  25. *((volatile unsigned *)(CPR_AO_BASE + 0x44)) |=0x10; //ROM断电隔离 深睡眠要将这两行注释来保证唤醒正常复位
  26. //*((volatile unsigned *)(CPR_AO_BASE + 0x20)) =0x2D; //auto switch core ldo voltage
  27. Timer_Register_Callback(system_wakeup, 1);
  28. /*gpio_direction_output(20);*/
  29. }
  30. extern uint32_t volatile testlp;
  31. extern uint8_t miss_rev_pkt_flag;
  32. void ble_system_idle(void)
  33. {
  34. uint32_t VAL, VAL1;
  35. VAL = ble_lowpower_remaining_clock(0);
  36. VAL1 = *(((volatile unsigned *)(0x40003000 + 0x0C )));
  37. if((VAL <= PRE_WAKEUP_TIME))// || (miss_rev_pkt_flag ==1))
  38. {
  39. return;
  40. }
  41. __disable_irq();
  42. VAL = ble_lowpower_remaining_clock(1);
  43. if(VAL == 0xFFFFFFFF) {
  44. do {
  45. VAL = timer_current_count(0);
  46. if(VAL != 0xFFFFFFFF) break;
  47. } while(1);
  48. }
  49. if(VAL <= PRE_WAKEUP_TIME) {
  50. __enable_irq();
  51. return;
  52. }
  53. #if 0
  54. {
  55. DMA_buf[0]=VAL&0XFF;
  56. DMA_buf[1]=(VAL&0XFF00)>>8;
  57. DMA_buf[2]=VAL1&0xff;
  58. DMA_buf[3]=0XCC;
  59. DMA_Uart_SendChar(1);
  60. }
  61. #endif
  62. VAL = VAL - PRE_WAKEUP_TIME;
  63. Init_Timer(1, VAL);
  64. ble_enter_lowpower();
  65. __write_hw_reg32(CPR_SLP_PD_MASK,0x101); //加上此句 auto switch core ldo voltage 1.17v to 0.89v 才能一直生效
  66. //xc_rf_ldo_off(); //close rf_ldo
  67. *((volatile unsigned *)(CPR_AO_BASE + 0x50)) &=0xFFFFFFFE; //close rf digital 手动开关RF总开关
  68. *((volatile unsigned *)(CPR_AO_BASE + 0x40)) &=0x1E; //cpr_ao_vdd_switch_en BT_MODEM断电
  69. *((volatile unsigned *)(CPR_AO_BASE + 0x44)) |=0x01; //cpr_ao_vdd_iso_en BT_MODEM断电隔离
  70. //*((volatile unsigned *)(CPR_AO_BASE + 0x20)) =0x2C;
  71. __NOP();
  72. __NOP();
  73. __NOP();
  74. __WFI();
  75. __NOP();
  76. __NOP();
  77. __NOP();
  78. *((volatile unsigned *)(CPR_AO_BASE + 0x40)) |=0x1; //cpr_ao_vdd_switch_en BT_MODEM断电
  79. *((volatile unsigned *)(CPR_AO_BASE + 0x44)) &=0x1E; //cpr_ao_vdd_iso_en BT_MODEM断电隔离
  80. *((volatile unsigned *)(CPR_AO_BASE + 0x50)) |=0x1; //open rf digital 手动开关RF总开关
  81. //xc_rf_ldo_on(); //open rf_ldo
  82. ble_exit_lowpower(VAL);
  83. __enable_irq();
  84. }