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