drv_standby.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2019 Winner Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-03-13 tyx first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "drv_standby.h"
  14. #ifdef BSP_USING_STANDBY
  15. #include "wm_type_def.h"
  16. #include "wm_cpu.h"
  17. #include "wm_pmu.h"
  18. #include "wm_irq.h"
  19. #include "wm_regs.h"
  20. typedef volatile unsigned long vu32;
  21. #define M32(adr) (*((vu32*) (adr)))
  22. typedef void (*rom_standby_func)(void);
  23. static const rom_standby_func pm_standby = (rom_standby_func)0x499;
  24. #ifdef __ICCARM__
  25. extern void standby_idr(void);
  26. #endif
  27. #if (1 == GCC_COMPILE)
  28. void wm_pm_standby(void)
  29. {
  30. __asm volatile (
  31. " cpsid i \n" /* disable irq*/
  32. " dsb \n"
  33. " ldr r0, =0X499 \n"
  34. " bx r0 \n"
  35. " movs r0, #0x00 \n"
  36. " isb \n"
  37. );
  38. }
  39. #endif
  40. /**
  41. * This function will put w60x into run/shutdown mode.
  42. *
  43. * @param timeout How many OS Ticks that MCU can sleep
  44. */
  45. void sys_start_standby(int ms)
  46. {
  47. rt_uint32_t val;
  48. int timeout = ms;
  49. RT_ASSERT(timeout > 0);
  50. tls_pmu_clk_select(0);
  51. if (timeout <= 65535)
  52. {
  53. /* Enter PM_TIMER_MODE */
  54. tls_irq_enable(PMU_TIMER1_INT);
  55. tls_pmu_timer1_stop();
  56. tls_pmu_timer1_start(timeout);
  57. }
  58. else if(timeout <= 65535000)
  59. {
  60. timeout /= 1000;
  61. tls_irq_enable(PMU_TIMER0_INT);
  62. tls_pmu_timer0_stop();
  63. tls_pmu_timer0_start(timeout);
  64. }
  65. else
  66. {
  67. return;
  68. }
  69. tls_irq_enable(PMU_GPIO_WAKEUP_INT); //Open interrupt by default to clear the interrupt flag for IO wake-up
  70. val = tls_reg_read32(HR_PMU_PS_CR);
  71. val |= 0x01;
  72. tls_reg_write32(HR_PMU_PS_CR, val);
  73. }
  74. #ifdef RT_USING_FINSH
  75. #include <finsh.h>
  76. #include <stdlib.h>
  77. static void standby(uint8_t argc, char **argv)
  78. {
  79. if (argc != 2)
  80. {
  81. rt_kprintf("Usage: standby timeout(ms)\n");
  82. rt_kprintf("eg : standby 5000\n");
  83. }
  84. else
  85. {
  86. sys_start_standby(atoi(argv[1]));
  87. }
  88. }
  89. FINSH_FUNCTION_EXPORT_ALIAS(standby, __cmd_standby, sleep System);
  90. #endif /* RT_USING_FINSH */
  91. #endif /* BSP_USING_STANDBY */