ald_lptim.c 21.5 KB
Newer Older
W
wangyq2018 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/**
  *********************************************************************************
  *
  * @file    ald_lptim.c
  * @brief   LPTIM module driver.
  *	     This is the common part of the LPTIM initialization
  *
  * @version V1.0
  * @date    09 Nov 2017
  * @author  AE Team
  * @note
  *
  * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  *
  *********************************************************************************
  */

#include "ald_lptim.h"


/** @addtogroup ES32FXXX_ALD
  * @{
  */

/** @defgroup LPTIM LPTIM
  * @brief LPTIM module driver
  * @{
  */
#ifdef ALD_LPTIM

/** @defgroup LPTIM_Public_Functions LPTIM Public Functions
  * @{
  */

/** @defgroup LPTIM_Public_Functions_Group1 Initialization functions
  * @brief    Initialization and Configuration functions
  *
  * @{
  */
/**
  * @brief  Reset the LPTIM peripheral.
  * @param  hperh: Pointer to a lptim_handle_t.
  * @retval None
  */
void ald_lptim_reset(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
47
	assert_param(IS_LPTIM(hperh->perh));
W
wangyq2018 已提交
48

W
wangyq2018 已提交
49 50 51 52
	hperh->state = LPTIM_STATE_BUSY;
	LPTIM_DISABLE(hperh);
	hperh->state = LPTIM_STATE_RESET;
	__UNLOCK(hperh);
W
wangyq2018 已提交
53

W
wangyq2018 已提交
54
	return;
W
wangyq2018 已提交
55 56 57 58 59 60 61 62 63 64 65
}

/**
  * @brief  Configure the LPTIM trigger mode according to the specified parameters in
  *         the lptim_trigger_init_t.
  * @param  hperh: Pointer to a lptim_handle_t.
  * @param  config: Pointer to a lptim_trigger_init_t.
  * @retval None
  */
void ald_lptim_trigger_config(lptim_handle_t *hperh, lptim_trigger_init_t *config)
{
W
wangyq2018 已提交
66 67 68
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_TRIGEN(config->mode));
	assert_param(IS_LPTIM_TRIGSEL(config->sel));
W
wangyq2018 已提交
69

W
wangyq2018 已提交
70 71
	MODIFY_REG(hperh->perh->CON0, LP16T_CON0_TRIGEN_MSK, (config->mode) << LP16T_CON0_TRIGEN_POSS);
	MODIFY_REG(hperh->perh->CON0, LP16T_CON0_TRIGSEL_MSK, (config->sel) << LP16T_CON0_TRIGSEL_POSS);
W
wangyq2018 已提交
72

W
wangyq2018 已提交
73
	return;
W
wangyq2018 已提交
74 75 76 77 78 79 80 81 82 83 84
}

/**
  * @brief  Configure the LPTIM clock source according to the specified parameters in
  *         the lptim_clock_source_init_t.
  * @param  hperh: Pointer to a lptim_handle_t.
  * @param  config: Pointer to a lptim_clock_source_init_t.
  * @retval None
  */
void ald_lptim_clock_source_config(lptim_handle_t *hperh, lptim_clock_source_init_t *config)
{
W
wangyq2018 已提交
85 86 87
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_CKSEL(config->sel));
	assert_param(IS_LPTIM_CKPOL(config->polarity));
W
wangyq2018 已提交
88

W
wangyq2018 已提交
89 90
	MODIFY_REG(hperh->perh->CON0, LP16T_CON0_CKSEL_MSK, (config->sel) << LP16T_CON0_CKSEL_POS);
	MODIFY_REG(hperh->perh->CON0, LP16T_CON0_CKPOL_MSK, (config->polarity) << LP16T_CON0_CKPOL_POS);
W
wangyq2018 已提交
91

W
wangyq2018 已提交
92
	return;
W
wangyq2018 已提交
93 94 95 96 97 98 99 100 101 102 103
}

/**
  * @brief  Configure the LPTIM trigger filter parameter according to
  *         the specified parameters in the lptim_trgflt_t.
  * @param  hperh: Pointer to a lptim_handle_t.
  * @param  flt: Pointer to a lptim_trgflt_t.
  * @retval None
  */
void ald_lptim_trigger_filter_config(lptim_handle_t *hperh, lptim_trgflt_t flt)
{
W
wangyq2018 已提交
104 105
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_TRGFLT(flt));
W
wangyq2018 已提交
106

W
wangyq2018 已提交
107
	MODIFY_REG(hperh->perh->CON0, LP16T_CON0_TRGFLT_MSK, flt << LP16T_CON0_TRGFLT_POSS);
W
wangyq2018 已提交
108

W
wangyq2018 已提交
109
	return;
W
wangyq2018 已提交
110 111 112 113 114 115 116 117 118 119 120
}

/**
  * @brief  Configure the LPTIM clock filter parameter according to
  *         the specified parameters in the lptim_ckflt_t.
  * @param  hperh: Pointer to a lptim_handle_t.
  * @param  flt: Pointer to a lptim_ckflt_t.
  * @retval None
  */
void ald_lptim_clock_filter_config(lptim_handle_t *hperh, lptim_ckflt_t flt)
{
W
wangyq2018 已提交
121 122
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_CKFLT(flt));
W
wangyq2018 已提交
123

W
wangyq2018 已提交
124
	MODIFY_REG(hperh->perh->CON0, LP16T_CON0_CKFLT_MSK, flt << LP16T_CON0_CKFLT_POSS);
W
wangyq2018 已提交
125

W
wangyq2018 已提交
126
	return;
W
wangyq2018 已提交
127 128 129 130 131
}
/**
  * @}
  */

W
wangyq2018 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
/** @defgroup LPTIM_Public_Functions_Group2 LPTIM base functions
  *  @brief   LPTIM base functions
  *
  * @verbatim
  ==============================================================================
                  ##### Low Pow Time Base functions #####
  ==============================================================================
  [..]
    This section provides functions allowing to:
    (+) Initialize the LPTIM .
    (+) Start the LPTIM.
    (+) Stop the LPTIM.
    (+) Start the LPTIM and enable interrupt.
    (+) Stop the LPTIM and disable interrupt.

    @endverbatim
  * @{
  */
/**
  * @brief  Initializes the TIM according to the specified
  *		parameters in the tim_handle_t.
  * @param  hperh: LPTIM handle
  * @retval Status, see @ref ald_status_t.
  */
ald_status_t ald_lptim_base_init(lptim_handle_t *hperh)
{
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_PRESC(hperh->init.psc));

	__LOCK(hperh);
	hperh->state = LPTIM_STATE_BUSY;

	ald_cmu_lptim0_clock_select(hperh->init.clock);

	WRITE_REG(hperh->perh->UPDATE, 1);
	MODIFY_REG(hperh->perh->CON0, LP16T_CON0_WAVE_MSK, LPTIM_WAVE_NONE << LP16T_CON0_WAVE_POSS);
	MODIFY_REG(hperh->perh->CON0, LP16T_CON0_PRESC_MSK, (hperh->init.psc) << LP16T_CON0_PRESC_POSS);
	WRITE_REG(hperh->perh->ARR, hperh->init.arr);
	WRITE_REG(hperh->perh->CMP, hperh->init.cmp);
	WRITE_REG(hperh->perh->UPDATE, 0);

	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_ARRWBSY_MSK));
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CMPWBSY_MSK));

	hperh->state = LPTIM_STATE_READY;
	__UNLOCK(hperh);
	return OK;
}

/**
  * @brief  Starts the LPTIM.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_base_start(lptim_handle_t *hperh)
{
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_MODE(hperh->init.mode));

	LPTIM_ENABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));

	if (hperh->init.mode == LPTIM_MODE_CONTINUOUS)
		LPTIM_CNTSTART(hperh);
	else
		LPTIM_SNGSTART(hperh);

	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
	return;
}

/**
  * @brief  Stops the LPTIM.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_base_stop(lptim_handle_t *hperh)
{
	assert_param(IS_LPTIM(hperh->perh));

	LPTIM_DISABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
	return;
}

/**
  * @brief  Starts the LPTIM in interrupt mode.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_base_start_by_it(lptim_handle_t *hperh)
{
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_MODE(hperh->init.mode));

	ald_lptim_interrupt_config(hperh, LPTIM_IT_ARRMAT, ENABLE);
	LPTIM_ENABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));

	if (hperh->init.mode == LPTIM_MODE_CONTINUOUS)
		LPTIM_CNTSTART(hperh);
	else
		LPTIM_SNGSTART(hperh);

	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
	return;
}

/**
  * @brief  Stops the LPTIM Output toggle in interrupt mode.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_base_stop_by_it(lptim_handle_t *hperh)
{
	assert_param(IS_LPTIM(hperh->perh));

	ald_lptim_interrupt_config(hperh, LPTIM_IT_ARRMAT, DISABLE);
	LPTIM_DISABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
	return;
}
/**
  * @}
  */

/** @defgroup LPTIM_Public_Functions_Group3 LPTIM output toggle functions
  *  @brief   LPTIM output toggle functions
W
wangyq2018 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  *
  * @verbatim
  ==============================================================================
                  ##### Time Output Compare functions #####
  ==============================================================================
  [..]
    This section provides functions allowing to:
    (+) Initialize the LPTIM Output Toggle.
    (+) Start the LPTIM Output Toggle.
    (+) Stop the LPTIM Output Toggle.
    (+) Start the LPTIM Output Toggle and enable interrupt.
    (+) Stop the LPTIM Output Toggle and disable interrupt.

    @endverbatim
  * @{
  */
/**
  * @brief  Initializes the TIM Output toggle according to the specified
  *		parameters in the tim_handle_t.
  * @param  hperh: LPTIM handle
  * @retval Status, see @ref ald_status_t.
  */
ald_status_t ald_lptim_toggle_init(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
284 285
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_PRESC(hperh->init.psc));
W
wangyq2018 已提交
286

W
wangyq2018 已提交
287 288
	__LOCK(hperh);
	hperh->state = LPTIM_STATE_BUSY;
W
wangyq2018 已提交
289

W
wangyq2018 已提交
290
	ald_cmu_lptim0_clock_select(hperh->init.clock);
W
wangyq2018 已提交
291

W
wangyq2018 已提交
292 293 294 295 296 297
	WRITE_REG(hperh->perh->UPDATE, 1);
	MODIFY_REG(hperh->perh->CON0, LP16T_CON0_WAVE_MSK, LPTIM_WAVE_TOGGLE << LP16T_CON0_WAVE_POSS);
	MODIFY_REG(hperh->perh->CON0, LP16T_CON0_PRESC_MSK, (hperh->init.psc) << LP16T_CON0_PRESC_POSS);
	WRITE_REG(hperh->perh->ARR, hperh->init.arr);
	WRITE_REG(hperh->perh->CMP, hperh->init.cmp);
	WRITE_REG(hperh->perh->UPDATE, 0);
W
wangyq2018 已提交
298

W
wangyq2018 已提交
299 300
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_ARRWBSY_MSK));
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CMPWBSY_MSK));
W
wangyq2018 已提交
301

W
wangyq2018 已提交
302 303 304
	hperh->state = LPTIM_STATE_READY;
	__UNLOCK(hperh);
	return OK;
W
wangyq2018 已提交
305 306 307 308 309 310 311 312 313
}

/**
  * @brief  Starts the LPTIM Output toggle.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_toggle_start(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
314 315
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_MODE(hperh->init.mode));
W
wangyq2018 已提交
316

W
wangyq2018 已提交
317 318
	LPTIM_ENABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
W
wangyq2018 已提交
319

W
wangyq2018 已提交
320 321 322 323
	if (hperh->init.mode == LPTIM_MODE_CONTINUOUS)
		LPTIM_CNTSTART(hperh);
	else
		LPTIM_SNGSTART(hperh);
W
wangyq2018 已提交
324

W
wangyq2018 已提交
325 326
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
	return;
W
wangyq2018 已提交
327 328 329 330 331 332 333 334 335
}

/**
  * @brief  Stops the LPTIM Output toggle.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_toggle_stop(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
336
	assert_param(IS_LPTIM(hperh->perh));
W
wangyq2018 已提交
337

W
wangyq2018 已提交
338 339 340
	LPTIM_DISABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
	return;
W
wangyq2018 已提交
341 342 343 344 345 346 347 348 349
}

/**
  * @brief  Starts the LPTIM Output toggle in interrupt mode.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_toggle_start_by_it(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
350 351
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_MODE(hperh->init.mode));
W
wangyq2018 已提交
352

W
wangyq2018 已提交
353 354 355
	ald_lptim_interrupt_config(hperh, LPTIM_IT_ARRMAT, ENABLE);
	LPTIM_ENABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
W
wangyq2018 已提交
356

W
wangyq2018 已提交
357 358 359 360
	if (hperh->init.mode == LPTIM_MODE_CONTINUOUS)
		LPTIM_CNTSTART(hperh);
	else
		LPTIM_SNGSTART(hperh);
W
wangyq2018 已提交
361

W
wangyq2018 已提交
362 363
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
	return;
W
wangyq2018 已提交
364 365 366 367 368 369 370 371 372
}

/**
  * @brief  Stops the LPTIM Output toggle in interrupt mode.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_toggle_stop_by_it(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
373
	assert_param(IS_LPTIM(hperh->perh));
W
wangyq2018 已提交
374

W
wangyq2018 已提交
375 376 377 378
	ald_lptim_interrupt_config(hperh, LPTIM_IT_ARRMAT, DISABLE);
	LPTIM_DISABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
	return;
W
wangyq2018 已提交
379 380 381 382 383
}
/**
  * @}
  */

W
wangyq2018 已提交
384 385
/** @defgroup LPTIM_Public_Functions_Group4 LPTIM output pulse functions
  *  @brief   LPTIM output pulse functions
W
wangyq2018 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
  *
  * @verbatim
  ==============================================================================
                  ##### Time Output Compare functions #####
  ==============================================================================
  [..]
    This section provides functions allowing to:
    (+) Initialize the LPTIM Output pulse.
    (+) Start the LPTIM Output pulse.
    (+) Stop the LPTIM Output pulse.
    (+) Start the LPTIM Output pulse and enable interrupt.
    (+) Stop the LPTIM Output pulse and disable interrupt.

    @endverbatim
  * @{
  */
/**
  * @brief  Initializes the TIM Output pulse according to the specified
  *		parameters in the tim_handle_t.
  * @param  hperh: LPTIM handle
  * @retval Status, see @ref ald_status_t.
  */
ald_status_t ald_lptim_pulse_init(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_PRESC(hperh->init.psc));

	__LOCK(hperh);
	hperh->state = LPTIM_STATE_BUSY;
	ald_cmu_lptim0_clock_select(hperh->init.clock);

	WRITE_REG(hperh->perh->UPDATE, 1);
	MODIFY_REG(hperh->perh->CON0, LP16T_CON0_WAVE_MSK, LPTIM_WAVE_PULSE << LP16T_CON0_WAVE_POSS);
	MODIFY_REG(hperh->perh->CON0, LP16T_CON0_PRESC_MSK, (hperh->init.psc) << LP16T_CON0_PRESC_POSS);
	WRITE_REG(hperh->perh->ARR, hperh->init.arr);
	WRITE_REG(hperh->perh->CMP, hperh->init.cmp);
	WRITE_REG(hperh->perh->UPDATE, 0);

	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_ARRWBSY_MSK));
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CMPWBSY_MSK));

	hperh->state = LPTIM_STATE_READY;
	__UNLOCK(hperh);
	return OK;
W
wangyq2018 已提交
430 431 432 433 434 435 436 437 438
}

/**
  * @brief  Starts the LPTIM Output pulse.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_pulse_start(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
439 440
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_MODE(hperh->init.mode));
W
wangyq2018 已提交
441

W
wangyq2018 已提交
442 443
	LPTIM_ENABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
W
wangyq2018 已提交
444

W
wangyq2018 已提交
445 446 447 448
	if (hperh->init.mode == LPTIM_MODE_CONTINUOUS)
		LPTIM_CNTSTART(hperh);
	else
		LPTIM_SNGSTART(hperh);
W
wangyq2018 已提交
449

W
wangyq2018 已提交
450 451
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
	return;
W
wangyq2018 已提交
452 453 454 455 456 457 458 459 460
}

/**
  * @brief  Stops the LPTIM Output pulse.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_pulse_stop(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
461
	assert_param(IS_LPTIM(hperh->perh));
W
wangyq2018 已提交
462

W
wangyq2018 已提交
463 464
	LPTIM_DISABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
W
wangyq2018 已提交
465

W
wangyq2018 已提交
466
	return;
W
wangyq2018 已提交
467 468 469 470 471 472 473 474 475
}

/**
  * @brief  Starts the LPTIM Output pulse in interrupt mode.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_pulse_start_by_it(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
476 477
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_MODE(hperh->init.mode));
W
wangyq2018 已提交
478

W
wangyq2018 已提交
479 480 481
	ald_lptim_interrupt_config(hperh, LPTIM_IT_ARRMAT, ENABLE);
	LPTIM_ENABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
W
wangyq2018 已提交
482

W
wangyq2018 已提交
483 484 485 486
	if (hperh->init.mode == LPTIM_MODE_CONTINUOUS)
		LPTIM_CNTSTART(hperh);
	else
		LPTIM_SNGSTART(hperh);
W
wangyq2018 已提交
487

W
wangyq2018 已提交
488 489
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
	return;
W
wangyq2018 已提交
490 491 492 493 494 495 496 497 498
}

/**
  * @brief  Stops the LPTIM Output pulse in interrupt mode.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_pulse_stop_by_it(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
499
	assert_param(IS_LPTIM(hperh->perh));
W
wangyq2018 已提交
500

W
wangyq2018 已提交
501 502 503
	ald_lptim_interrupt_config(hperh, LPTIM_IT_ARRMAT, DISABLE);
	LPTIM_DISABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
W
wangyq2018 已提交
504

W
wangyq2018 已提交
505
	return;
W
wangyq2018 已提交
506 507 508 509 510
}
/**
  * @}
  */

W
wangyq2018 已提交
511 512
/** @defgroup LPTIM_Public_Functions_Group5 LPTIM output pwm functions
  *  @brief   LPTIM output pwm functions
W
wangyq2018 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
  *
  * @verbatim
  ==============================================================================
                  ##### Time Output Compare functions #####
  ==============================================================================
  [..]
    This section provides functions allowing to:
    (+) Initialize the LPTIM Output pwm.
    (+) Start the LPTIM Output pwm.
    (+) Stop the LPTIM Output pwm.
    (+) Start the LPTIM Output pwm and enable interrupt.
    (+) Stop the LPTIM Output pwm and disable interrupt.

    @endverbatim
  * @{
  */
/**
  * @brief  Initializes the TIM Output pwm according to the specified
  *		parameters in the tim_handle_t.
  * @param  hperh: LPTIM handle
  * @retval Status, see @ref ald_status_t.
  */
ald_status_t ald_lptim_pwm_init(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
537 538
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_PRESC(hperh->init.psc));
W
wangyq2018 已提交
539

W
wangyq2018 已提交
540 541
	__LOCK(hperh);
	hperh->state = LPTIM_STATE_BUSY;
W
wangyq2018 已提交
542

W
wangyq2018 已提交
543 544 545 546 547 548
	WRITE_REG(hperh->perh->UPDATE, 1);
	MODIFY_REG(hperh->perh->CON0, LP16T_CON0_WAVE_MSK, LPTIM_WAVE_PWM << LP16T_CON0_WAVE_POSS);
	MODIFY_REG(hperh->perh->CON0, LP16T_CON0_PRESC_MSK, (hperh->init.psc) << LP16T_CON0_PRESC_POSS);
	WRITE_REG(hperh->perh->ARR, hperh->init.arr);
	WRITE_REG(hperh->perh->CMP, hperh->init.cmp);
	WRITE_REG(hperh->perh->UPDATE, 0);
W
wangyq2018 已提交
549

W
wangyq2018 已提交
550 551
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_ARRWBSY_MSK));
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CMPWBSY_MSK));
W
wangyq2018 已提交
552

W
wangyq2018 已提交
553 554 555
	hperh->state = LPTIM_STATE_READY;
	__UNLOCK(hperh);
	return OK;
W
wangyq2018 已提交
556 557 558 559 560 561 562 563 564
}

/**
  * @brief  Starts the LPTIM Output pwm.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_pwm_start(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
565 566
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_MODE(hperh->init.mode));
W
wangyq2018 已提交
567

W
wangyq2018 已提交
568 569
	LPTIM_ENABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
W
wangyq2018 已提交
570

W
wangyq2018 已提交
571 572 573 574
	if (hperh->init.mode == LPTIM_MODE_CONTINUOUS)
		LPTIM_CNTSTART(hperh);
	else
		LPTIM_SNGSTART(hperh);
W
wangyq2018 已提交
575

W
wangyq2018 已提交
576 577
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
	return;
W
wangyq2018 已提交
578 579 580 581 582 583 584 585 586
}

/**
  * @brief  Stops the LPTIM Output pwm.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_pwm_stop(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
587
	assert_param(IS_LPTIM(hperh->perh));
W
wangyq2018 已提交
588

W
wangyq2018 已提交
589 590
	LPTIM_DISABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
W
wangyq2018 已提交
591

W
wangyq2018 已提交
592
	return;
W
wangyq2018 已提交
593 594 595 596 597 598 599 600 601
}

/**
  * @brief  Starts the LPTIM Output pwm in interrupt mode.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_pwm_start_by_it(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
602 603
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_MODE(hperh->init.mode));
W
wangyq2018 已提交
604

W
wangyq2018 已提交
605 606 607
	ald_lptim_interrupt_config(hperh, LPTIM_IT_CMPMAT, ENABLE);
	LPTIM_ENABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
W
wangyq2018 已提交
608

W
wangyq2018 已提交
609 610 611 612
	if (hperh->init.mode == LPTIM_MODE_CONTINUOUS)
		LPTIM_CNTSTART(hperh);
	else
		LPTIM_SNGSTART(hperh);
W
wangyq2018 已提交
613

W
wangyq2018 已提交
614 615
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
	return;
W
wangyq2018 已提交
616 617 618 619 620 621 622 623 624
}

/**
  * @brief  Stops the LPTIM Output pwm in interrupt mode.
  * @param  hperh: LPTIM handle
  * @retval None
  */
void ald_lptim_pwm_stop_by_it(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
625
	assert_param(IS_LPTIM(hperh->perh));
W
wangyq2018 已提交
626

W
wangyq2018 已提交
627 628 629
	ald_lptim_interrupt_config(hperh, LPTIM_IT_CMPMAT, DISABLE);
	LPTIM_DISABLE(hperh);
	while (READ_BIT(hperh->perh->SYNCSTAT, LP16T_SYNCSTAT_CON1WBSY_MSK));
W
wangyq2018 已提交
630

W
wangyq2018 已提交
631
	return;
W
wangyq2018 已提交
632 633 634 635 636 637
}
/**
  * @}
  */


W
wangyq2018 已提交
638
/** @defgroup LPTIM_Public_Functions_Group6 Control functions
W
wangyq2018 已提交
639 640 641 642 643 644 645 646 647 648 649
  * @brief LPTIM Control functions
  *
  * @{
  */
/**
  * @brief  This function handles LPTIM interrupts requests.
  * @param  hperh: LPTIM  handle
  * @retval None
  */
void ald_lptim_irq_handler(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
	assert_param(IS_LPTIM(hperh->perh));

	/* Output compare event */
	if (((ald_lptim_get_it_status(hperh, LPTIM_IT_CMPMAT)) != RESET) &&
                           ((ald_lptim_get_flag_status(hperh, LPTIM_FLAG_CMPMAT)) != RESET)) {
		ald_lptim_clear_flag_status(hperh, LPTIM_FLAG_CMPMAT);

		if (hperh->cmp_cbk)
			hperh->cmp_cbk(hperh);
	}

	/* Output update event */
	if (((ald_lptim_get_it_status(hperh, LPTIM_IT_ARRMAT)) != RESET) &&
                           ((ald_lptim_get_flag_status(hperh, LPTIM_FLAG_ARRMAT)) != RESET)) {
		ald_lptim_clear_flag_status(hperh, LPTIM_FLAG_ARRMAT);

		if (hperh->update_cbk)
			hperh->update_cbk(hperh);
	}

	/* Trigger event */
	if (((ald_lptim_get_it_status(hperh, LPTIM_IT_EXTTRIG)) != RESET) &&
                           ((ald_lptim_get_flag_status(hperh, LPTIM_FLAG_EXTTRIG)) != RESET)) {
		ald_lptim_clear_flag_status(hperh, LPTIM_FLAG_EXTTRIG);

		if (hperh->trig_cbk)
			hperh->trig_cbk(hperh);
	}

	return;
W
wangyq2018 已提交
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
}

/**
  * @brief  Enables or disables the specified LPTIM interrupts.
  * @param  hperh: Pointer to a lptim_handle_t structure that contains
  *         the configuration information for the specified LPTIM module.
  * @param  it: Specifies the SPI interrupt sources to be enabled or disabled.
  *         This parameter can be one of the @ref lptim_it_t.
  * @param  state: New status
  *           - ENABLE
  *           - DISABLE
  * @retval None
  */
void ald_lptim_interrupt_config(lptim_handle_t *hperh, lptim_it_t it, type_func_t state)
{
W
wangyq2018 已提交
695 696 697 698 699 700 701 702 703
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_IT(it));
	assert_param(IS_FUNC_STATE(state));

	if (state == ENABLE)
		SET_BIT(hperh->perh->IER, (uint32_t)it);
	else
		CLEAR_BIT(hperh->perh->IER, (uint32_t)it);
	return;
W
wangyq2018 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717
}

/**
  * @brief  Checks whether the specified LPTIM interrupt has occurred or not.
  * @param  hperh: Pointer to a lptim_handle_t structure that contains
  *         the configuration information for the specified LPTIM module.
  * @param  it: Specifies the LPTIM interrupt source to check.
  *         This parameter can be one of the @ref lptim_it_t.
  * @retval Status
  *           - SET
  *           - RESET
  */
it_status_t ald_lptim_get_it_status(lptim_handle_t *hperh, lptim_it_t it)
{
W
wangyq2018 已提交
718 719
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_IT(it));
W
wangyq2018 已提交
720

W
wangyq2018 已提交
721 722
	if (READ_BIT(hperh->perh->IER, it))
		return SET;
W
wangyq2018 已提交
723

W
wangyq2018 已提交
724
	return RESET;
W
wangyq2018 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737
}

/** @brief  Check whether the specified LPTIM flag is set or not.
  * @param  hperh: Pointer to a lptim_handle_t structure that contains
  *         the configuration information for the specified LPTIM module.
  * @param  flag: specifies the flag to check.
  *         This parameter can be one of the @ref lptim_flag_t.
  * @retval Status
  *           - SET
  *           - RESET
  */
flag_status_t ald_lptim_get_flag_status(lptim_handle_t *hperh, lptim_flag_t flag)
{
W
wangyq2018 已提交
738 739
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_FLAG(flag));
W
wangyq2018 已提交
740

W
wangyq2018 已提交
741 742
	if (READ_BIT(hperh->perh->ISR, flag))
		return SET;
W
wangyq2018 已提交
743

W
wangyq2018 已提交
744
	return RESET;
W
wangyq2018 已提交
745 746 747 748 749 750 751 752 753 754 755
}

/** @brief  Clear the specified LPTIM pending flags.
  * @param  hperh: Pointer to a lptim_handle_t structure that contains
  *         the configuration information for the specified LPTIM module.
  * @param  flag: specifies the flag to check.
  *         This parameter can be one of the @ref lptim_flag_t.
  * @retval None
  */
void ald_lptim_clear_flag_status(lptim_handle_t *hperh, lptim_flag_t flag)
{
W
wangyq2018 已提交
756 757
	assert_param(IS_LPTIM(hperh->perh));
	assert_param(IS_LPTIM_FLAG(flag));
W
wangyq2018 已提交
758

W
wangyq2018 已提交
759 760
	WRITE_REG(hperh->perh->IFC, (uint32_t)flag);
	return;
W
wangyq2018 已提交
761 762 763 764 765
}
/**
  * @}
  */

W
wangyq2018 已提交
766 767
/** @defgroup LPTIM_Public_Functions_Group7 Peripheral State functions
 *  @brief    Peripheral State functions
W
wangyq2018 已提交
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
 *
 * @verbatim
  ==============================================================================
                        ##### Peripheral State functions #####
  ==============================================================================
    [..]
    This subsection permit to get in run-time the status of the peripheral.

    @endverbatim
  * @{
  */

/**
  * @brief  Return the LPTIM state
  * @param  hperh: LPTIM handle
  * @retval LPTIM peripheral state
  */
lptim_state_t ald_lptim_get_state(lptim_handle_t *hperh)
{
W
wangyq2018 已提交
787
	return hperh->state;
W
wangyq2018 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801
}
/**
  * @}
  */
/**
  * @}
  */
#endif /* ALD_LPTIM */
/**
  * @}
  */
/**
  * @}
  */