drv_lptim.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-06-19 thread-liu first version
  9. */
  10. #include <board.h>
  11. #ifdef BSP_USING_LPTIM
  12. #include "drv_config.h"
  13. #include <string.h>
  14. #include <stdlib.h>
  15. //#define DRV_DEBUG
  16. #define LOG_TAG "drv.lptimer"
  17. #include <drv_log.h>
  18. LPTIM_HandleTypeDef hlptim1;
  19. void LPTIM1_IRQHandler(void)
  20. {
  21. /* enter interrupt */
  22. rt_interrupt_enter();
  23. HAL_LPTIM_IRQHandler(&hlptim1);
  24. /* leave interrupt */
  25. rt_interrupt_leave();
  26. }
  27. void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
  28. {
  29. if(hlptim->Instance == LPTIM1)
  30. {
  31. rt_kprintf("hello rt-thread!\n");
  32. }
  33. }
  34. static int lptim_control(uint8_t pre_value)
  35. {
  36. if(pre_value > 7)
  37. {
  38. pre_value = 7;
  39. }
  40. hlptim1.Instance->CFGR &= ~(7 << 9); /* clear PRESC[2:0] */
  41. hlptim1.Instance->CFGR |= pre_value << 9; /* set PRESC[2:0] */
  42. return RT_EOK;
  43. }
  44. /**
  45. * This function initialize the lptim
  46. */
  47. static int lptim_init(void)
  48. {
  49. hlptim1.Instance = LPTIM1;
  50. hlptim1.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
  51. hlptim1.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV1;
  52. hlptim1.Init.UltraLowPowerClock.Polarity = LPTIM_CLOCKPOLARITY_RISING;
  53. hlptim1.Init.UltraLowPowerClock.SampleTime = LPTIM_CLOCKSAMPLETIME_DIRECTTRANSITION;
  54. hlptim1.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;
  55. hlptim1.Init.OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH;
  56. hlptim1.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;
  57. hlptim1.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;
  58. hlptim1.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO;
  59. hlptim1.Init.Input2Source = LPTIM_INPUT2SOURCE_GPIO;
  60. if (HAL_LPTIM_Init(&hlptim1) != HAL_OK)
  61. {
  62. LOG_D("LPTIM Init Error!\n");
  63. return -RT_ERROR;
  64. }
  65. /* ### Start counting in interrupt mode ############################# */
  66. if (HAL_LPTIM_Counter_Start_IT(&hlptim1, 5000) != HAL_OK)
  67. {
  68. LOG_D("LPTIM Start Counting Error!\n");
  69. return -RT_ERROR;
  70. }
  71. return RT_EOK;
  72. }
  73. static int lptim_deinit()
  74. {
  75. if (HAL_LPTIM_DeInit(&hlptim1) != HAL_OK)
  76. {
  77. LOG_D("LPTIM Deinit Error!\n");
  78. return -RT_ERROR;
  79. }
  80. return RT_EOK;
  81. }
  82. static int lptim_sample(int argc, char *argv[])
  83. {
  84. if (argc > 1)
  85. {
  86. if (!strcmp(argv[1], "run"))
  87. {
  88. lptim_init();
  89. }
  90. else if (!strcmp(argv[1], "stop"))
  91. {
  92. lptim_deinit();
  93. }
  94. else if (!strcmp(argv[1], "set"))
  95. {
  96. if (argc > 2)
  97. {
  98. lptim_control(atoi(argv[2]));
  99. }
  100. }
  101. }
  102. else
  103. {
  104. rt_kprintf("Usage:\n");
  105. rt_kprintf("lptim_sample run - open lptim, shell will printf 'hello rt-thread'\n");
  106. rt_kprintf("lptim_sample set - set the lptim prescaler, lptim_sample set [0 - 7]\n");
  107. }
  108. return RT_EOK;
  109. }
  110. MSH_CMD_EXPORT(lptim_sample, low power timer sample);
  111. #endif