drv_lptim.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * 2019-05-06 Zero-Free first version
  9. */
  10. #include <board.h>
  11. #include <drv_lptim.h>
  12. static LPTIM_HandleTypeDef LptimHandle;
  13. void HAL_LPTIM_MspInit(LPTIM_HandleTypeDef *hlptim)
  14. {
  15. if (hlptim->Instance == LPTIM1)
  16. {
  17. /* Peripheral clock enable */
  18. __HAL_RCC_LPTIM1_CLK_ENABLE();
  19. }
  20. }
  21. void LPTIM1_IRQHandler(void)
  22. {
  23. HAL_LPTIM_IRQHandler(&LptimHandle);
  24. }
  25. void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
  26. {
  27. /* enter interrupt */
  28. rt_interrupt_enter();
  29. /* leave interrupt */
  30. rt_interrupt_leave();
  31. }
  32. /**
  33. * This function get current count value of LPTIM
  34. *
  35. * @return the count vlaue
  36. */
  37. rt_uint32_t stm32l4_lptim_get_current_tick(void)
  38. {
  39. return HAL_LPTIM_ReadCounter(&LptimHandle);
  40. }
  41. /**
  42. * This function get the max value that LPTIM can count
  43. *
  44. * @return the max count
  45. */
  46. rt_uint32_t stm32l4_lptim_get_tick_max(void)
  47. {
  48. return (0xFFFF);
  49. }
  50. /**
  51. * This function start LPTIM with reload value
  52. *
  53. * @param reload The value that LPTIM count down from
  54. *
  55. * @return RT_EOK
  56. */
  57. rt_err_t stm32l4_lptim_start(rt_uint32_t reload)
  58. {
  59. HAL_LPTIM_TimeOut_Start_IT(&LptimHandle, 0xFFFF, reload);
  60. return (RT_EOK);
  61. }
  62. /**
  63. * This function stop LPTIM
  64. */
  65. void stm32l4_lptim_stop(void)
  66. {
  67. rt_uint32_t _ier;
  68. _ier = LptimHandle.Instance->IER;
  69. LptimHandle.Instance->ICR = LptimHandle.Instance->ISR & _ier;
  70. }
  71. /**
  72. * This function get the count clock of LPTIM
  73. *
  74. * @return the count clock frequency in Hz
  75. */
  76. rt_uint32_t stm32l4_lptim_get_countfreq(void)
  77. {
  78. return 32000 / 32;
  79. }
  80. /**
  81. * This function initialize the lptim
  82. */
  83. int stm32l4_hw_lptim_init(void)
  84. {
  85. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  86. RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct = {0};
  87. /* Enable LSI clock */
  88. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
  89. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  90. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  91. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  92. /* Select the LSI clock as LPTIM peripheral clock */
  93. RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPTIM1;
  94. RCC_PeriphCLKInitStruct.Lptim1ClockSelection = RCC_LPTIM1CLKSOURCE_LSI;
  95. HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);
  96. LptimHandle.Instance = LPTIM1;
  97. LptimHandle.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
  98. LptimHandle.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV32;
  99. LptimHandle.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;
  100. LptimHandle.Init.OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH;
  101. LptimHandle.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;
  102. LptimHandle.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;
  103. if (HAL_LPTIM_Init(&LptimHandle) != HAL_OK)
  104. {
  105. return -1;
  106. }
  107. NVIC_ClearPendingIRQ(LPTIM1_IRQn);
  108. NVIC_SetPriority(LPTIM1_IRQn, 0);
  109. NVIC_EnableIRQ(LPTIM1_IRQn);
  110. return 0;
  111. }
  112. INIT_DEVICE_EXPORT(stm32l4_hw_lptim_init);