drv_lptim.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2006-2021, 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. #define LED5_PIN GET_PIN(D, 9)
  19. LPTIM_HandleTypeDef hlptim1;
  20. extern int lptim_stop(void);
  21. void LPTIM1_IRQHandler(void)
  22. {
  23. /* enter interrupt */
  24. rt_interrupt_enter();
  25. HAL_LPTIM_IRQHandler(&hlptim1);
  26. /* leave interrupt */
  27. rt_interrupt_leave();
  28. }
  29. void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
  30. {
  31. if(hlptim->Instance == LPTIM1)
  32. {
  33. HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_9);
  34. }
  35. #if defined(BSP_USING_PWR)
  36. /* All level of ITs can interrupt */
  37. __set_BASEPRI(0U);
  38. lptim_stop();
  39. rt_kprintf("system returns to normal!\n");
  40. #endif
  41. }
  42. static int lptim_control(uint8_t pre_value)
  43. {
  44. if(pre_value > 7)
  45. {
  46. pre_value = 7;
  47. }
  48. hlptim1.Instance->CFGR &= ~(7 << 9); /* clear PRESC[2:0] */
  49. hlptim1.Instance->CFGR |= pre_value << 9; /* set PRESC[2:0] */
  50. rt_kprintf("set lptim pre value [0x%x] success!\n", pre_value);
  51. return RT_EOK;
  52. }
  53. int lptim_start(void)
  54. {
  55. /* ### Start counting in interrupt mode ############################# */
  56. if (HAL_LPTIM_Counter_Start_IT(&hlptim1, 32767) != HAL_OK)
  57. {
  58. LOG_D("lptim1 start counting failed!\n");
  59. return -RT_ERROR;
  60. }
  61. LOG_D("lptim1 start counting success!\n");
  62. return RT_EOK;
  63. }
  64. int lptim_stop(void)
  65. {
  66. if (HAL_LPTIM_Counter_Stop_IT(&hlptim1) != HAL_OK)
  67. {
  68. LOG_D("lptim1 stop failed!\n");
  69. return -RT_ERROR;
  70. }
  71. LOG_D("lptim1 stop counting success!\n");
  72. return RT_EOK;
  73. }
  74. int lptim_init(void)
  75. {
  76. rt_pin_mode(LED5_PIN, PIN_MODE_OUTPUT);
  77. hlptim1.Instance = LPTIM1;
  78. hlptim1.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
  79. hlptim1.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV8;
  80. hlptim1.Init.UltraLowPowerClock.Polarity = LPTIM_CLOCKPOLARITY_RISING;
  81. hlptim1.Init.UltraLowPowerClock.SampleTime = LPTIM_CLOCKSAMPLETIME_DIRECTTRANSITION;
  82. hlptim1.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;
  83. hlptim1.Init.OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH;
  84. hlptim1.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;
  85. hlptim1.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;
  86. hlptim1.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO;
  87. hlptim1.Init.Input2Source = LPTIM_INPUT2SOURCE_GPIO;
  88. if (HAL_LPTIM_Init(&hlptim1) != HAL_OK)
  89. {
  90. LOG_D("lptim init failed!\n");
  91. return -RT_ERROR;
  92. }
  93. LOG_D("lptim init success!\n");
  94. return RT_EOK;
  95. }
  96. INIT_DEVICE_EXPORT(lptim_init);
  97. static int lptim_sample(int argc, char *argv[])
  98. {
  99. if (argc > 1)
  100. {
  101. if (!strcmp(argv[1], "start"))
  102. {
  103. lptim_start();
  104. return RT_EOK;
  105. }
  106. else if (!strcmp(argv[1], "stop"))
  107. {
  108. lptim_stop();
  109. return RT_EOK;
  110. }
  111. else if (!strcmp(argv[1], "set"))
  112. {
  113. if (argc > 2)
  114. {
  115. lptim_control(atoi(argv[2]));
  116. return RT_EOK;
  117. }
  118. else
  119. {
  120. goto _exit;
  121. }
  122. }
  123. else
  124. {
  125. goto _exit;
  126. }
  127. }
  128. _exit:
  129. {
  130. rt_kprintf("Usage:\n");
  131. rt_kprintf("lptim_sample start - start lptim, the LED5 will start blink\n");
  132. rt_kprintf("lptim_sample stop - stop lptim, the LED5 will stop blink\n");
  133. rt_kprintf("lptim_sample set - set the lptim prescaler to change LED5 blink frquency, lptim_sample set [0 - 7]\n");
  134. }
  135. return -RT_ERROR;
  136. }
  137. MSH_CMD_EXPORT(lptim_sample, low power timer sample);
  138. #endif