drv_lptim.c 3.8 KB

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