2020-06-23 10:43:18 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2018, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2020-06-19 thread-liu first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <board.h>
|
|
|
|
|
|
|
|
#ifdef BSP_USING_LPTIM
|
|
|
|
#include "drv_config.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
//#define DRV_DEBUG
|
|
|
|
#define LOG_TAG "drv.lptimer"
|
|
|
|
#include <drv_log.h>
|
|
|
|
|
2020-09-04 10:23:08 +08:00
|
|
|
#define LED7_PIN GET_PIN(H, 7)
|
2020-06-23 10:43:18 +08:00
|
|
|
LPTIM_HandleTypeDef hlptim1;
|
|
|
|
|
2020-09-23 13:42:09 +08:00
|
|
|
extern int lptim_stop(void);
|
|
|
|
|
2020-06-23 10:43:18 +08:00
|
|
|
void LPTIM1_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
HAL_LPTIM_IRQHandler(&hlptim1);
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
|
|
|
|
{
|
|
|
|
if(hlptim->Instance == LPTIM1)
|
|
|
|
{
|
2020-09-04 10:23:08 +08:00
|
|
|
HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_7);
|
2020-06-23 10:43:18 +08:00
|
|
|
}
|
2020-09-23 13:42:09 +08:00
|
|
|
|
|
|
|
/* All level of ITs can interrupt */
|
|
|
|
__set_BASEPRI(0U);
|
|
|
|
|
|
|
|
lptim_stop();
|
|
|
|
rt_kprintf("system returns to normal!\n");
|
2020-06-23 10:43:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int lptim_control(uint8_t pre_value)
|
|
|
|
{
|
|
|
|
if(pre_value > 7)
|
|
|
|
{
|
|
|
|
pre_value = 7;
|
|
|
|
}
|
|
|
|
hlptim1.Instance->CFGR &= ~(7 << 9); /* clear PRESC[2:0] */
|
|
|
|
hlptim1.Instance->CFGR |= pre_value << 9; /* set PRESC[2:0] */
|
2020-09-23 13:42:09 +08:00
|
|
|
rt_kprintf("set lptim pre value [0x%x] success!\n", pre_value);
|
|
|
|
|
2020-06-23 10:43:18 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2020-09-23 13:42:09 +08:00
|
|
|
int lptim_start(void)
|
2020-09-04 10:23:08 +08:00
|
|
|
{
|
|
|
|
/* ### Start counting in interrupt mode ############################# */
|
|
|
|
if (HAL_LPTIM_Counter_Start_IT(&hlptim1, 32767) != HAL_OK)
|
|
|
|
{
|
2020-09-23 13:42:09 +08:00
|
|
|
LOG_D("lptim1 start counting failed!\n");
|
2020-09-04 10:23:08 +08:00
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
2020-09-23 13:42:09 +08:00
|
|
|
LOG_D("lptim1 start counting success!\n");
|
|
|
|
|
2020-09-04 10:23:08 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2020-09-23 13:42:09 +08:00
|
|
|
int lptim_stop(void)
|
2020-06-23 10:43:18 +08:00
|
|
|
{
|
2020-09-04 10:23:08 +08:00
|
|
|
if (HAL_LPTIM_Counter_Stop_IT(&hlptim1) != HAL_OK)
|
|
|
|
{
|
2020-09-23 13:42:09 +08:00
|
|
|
LOG_D("lptim1 stop failed!\n");
|
2020-09-04 10:23:08 +08:00
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2020-09-23 13:42:09 +08:00
|
|
|
|
|
|
|
LOG_D("lptim1 stop counting success!\n");
|
|
|
|
|
|
|
|
return RT_EOK;
|
2020-09-04 10:23:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int lptim_init(void)
|
|
|
|
{
|
|
|
|
rt_pin_mode(LED7_PIN, PIN_MODE_OUTPUT);
|
|
|
|
|
2020-06-23 10:43:18 +08:00
|
|
|
hlptim1.Instance = LPTIM1;
|
|
|
|
hlptim1.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
|
2020-09-04 10:23:08 +08:00
|
|
|
hlptim1.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV8;
|
2020-06-23 10:43:18 +08:00
|
|
|
hlptim1.Init.UltraLowPowerClock.Polarity = LPTIM_CLOCKPOLARITY_RISING;
|
|
|
|
hlptim1.Init.UltraLowPowerClock.SampleTime = LPTIM_CLOCKSAMPLETIME_DIRECTTRANSITION;
|
|
|
|
hlptim1.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;
|
|
|
|
hlptim1.Init.OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH;
|
|
|
|
hlptim1.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;
|
|
|
|
hlptim1.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;
|
|
|
|
hlptim1.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO;
|
|
|
|
hlptim1.Init.Input2Source = LPTIM_INPUT2SOURCE_GPIO;
|
|
|
|
if (HAL_LPTIM_Init(&hlptim1) != HAL_OK)
|
|
|
|
{
|
2020-09-23 13:42:09 +08:00
|
|
|
LOG_D("lptim init failed!\n");
|
2020-06-23 10:43:18 +08:00
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2020-09-23 13:42:09 +08:00
|
|
|
LOG_D("lptim init success!\n");
|
2020-06-23 10:43:18 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
2020-09-04 10:23:08 +08:00
|
|
|
INIT_DEVICE_EXPORT(lptim_init);
|
2020-06-23 10:43:18 +08:00
|
|
|
|
|
|
|
static int lptim_sample(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
2020-09-04 10:23:08 +08:00
|
|
|
if (!strcmp(argv[1], "start"))
|
2020-06-23 10:43:18 +08:00
|
|
|
{
|
2020-09-04 10:23:08 +08:00
|
|
|
lptim_start();
|
|
|
|
return RT_EOK;
|
2020-06-23 10:43:18 +08:00
|
|
|
}
|
|
|
|
else if (!strcmp(argv[1], "stop"))
|
|
|
|
{
|
2020-09-04 10:23:08 +08:00
|
|
|
lptim_stop();
|
|
|
|
return RT_EOK;
|
2020-06-23 10:43:18 +08:00
|
|
|
}
|
|
|
|
else if (!strcmp(argv[1], "set"))
|
|
|
|
{
|
|
|
|
if (argc > 2)
|
|
|
|
{
|
|
|
|
lptim_control(atoi(argv[2]));
|
2020-09-04 10:23:08 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto _exit;
|
2020-06-23 10:43:18 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-04 10:23:08 +08:00
|
|
|
_exit:
|
2020-06-23 10:43:18 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("Usage:\n");
|
2020-09-04 10:23:08 +08:00
|
|
|
rt_kprintf("lptim_sample start - start lptim, the LED7 will start blink\n");
|
|
|
|
rt_kprintf("lptim_sample stop - stop lptim, the LED7 will stop blink\n");
|
|
|
|
rt_kprintf("lptim_sample set - set the lptim prescaler to change LED7 blink frquency, lptim_sample set [0 - 7]\n");
|
2020-06-23 10:43:18 +08:00
|
|
|
}
|
|
|
|
|
2020-09-04 10:23:08 +08:00
|
|
|
return -RT_ERROR;
|
2020-06-23 10:43:18 +08:00
|
|
|
}
|
|
|
|
MSH_CMD_EXPORT(lptim_sample, low power timer sample);
|
|
|
|
|
|
|
|
#endif
|