Welcome to mirror list, hosted at ThFree Co, Russian Federation.

lptimer_common_all.c « common « stm32 « lib - github.com/thirdpin/libopencm3.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1dc4e0ab345c026806fdcf37ccdcb33770a662cf (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/** @addtogroup lptimer_file LPTIM peripheral API
 * @ingroup peripheral_apis
 *
 * @author @htmlonly &copy; @endhtmlonly 2019 Guillaume Revaillot <g.revaillot@gmail.com>
 *
 * @date 2 July 2019
 *
 * LGPL License Terms @ref lgpl_license
 *
 * @section lptim_api_ex Basic LPTIMER handling API.
 *
 * Example: LPTIM1 with 2x clock prescaler, from internal clock (LSE), irq on match and reload.
 *
 * @code
 *
 *	rcc_set_peripheral_clk_sel(LPTIM1, RCC_CCIPR_LPTIM1SEL_LSE);
 *
 *	rcc_periph_clock_enable(RCC_LPTIM1);
 *
 *	lptimer_set_internal_clock_source(LPTIM1);
 *	lptimer_enable_trigger(LPTIM1, LPTIM_CFGR_TRIGEN_SW);
 *	lptimer_set_prescaler(LPTIM1, LPTIM_CFGR_PRESC_2);
 *
 *	lptimer_enable(LPTIM1);
 *
 *	lptimer_set_period(LPTIM1, 0xffff);
 *	lptimer_set_compare(LPTIM1, 1234);
 *
 *	lptimer_enable_irq(LPTIM1, LPTIM_IER_ARRMIE | LPTIM_IER_CMPMIE);
 *	nvic_enable_irq(NVIC_LPTIM1_IRQ);
 *
 *	lptimer_start_counter(LPTIM1, LPTIM_CR_CNTSTRT);
 *
 * @endcode
 *
 * Note: LPTIM internal clock source selection is device specific, see clock tree
 * and rcc section of reference manual.
 *
 */
/*
 * This file is part of the libopencm3 project.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

/**@{*/

#include <libopencm3/stm32/lptimer.h>

/** @brief Set lptimer Counter
 *
 * Set the value of a lptimer counter.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 * @param[in] count Counter value.
*/
void lptimer_set_counter(uint32_t lptimer_peripheral, uint16_t count)
{
	LPTIM_CNT(lptimer_peripheral) = count;
}

/** @brief Read lptimer Counter
 *
 * Read back the value of lptimer counter.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 * @returns Counter value.
 */
uint16_t lptimer_get_counter(uint32_t lptimer_peripheral)
{
	return LPTIM_CNT(lptimer_peripheral);
}

/** @brief Clear lptimer Status Flag.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 * @param[in] flag Status Register clear flag (@ref lptim_icr)
 */
void lptimer_clear_flag(uint32_t lptimer_peripheral, uint32_t flag)
{
	LPTIM_ICR(lptimer_peripheral) = flag;
}

/** @brief Read lptimer Status Flag.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 * @param[in] flag Status Register flag (@ref lptim_isr)
 * @returns flag set.
 */
bool lptimer_get_flag(uint32_t lptimer_peripheral, uint32_t flag)
{
	return (LPTIM_ISR(lptimer_peripheral) & flag);
}

/*---------------------------------------------------------------------------*/
/** @brief Enable lptimer interrupts.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 * @param[in] irq Logical or of all interrupt enable bits to be set (@ref lptim_ier)
 */
void lptimer_enable_irq(uint32_t lptimer_peripheral, uint32_t irq)
{
	LPTIM_IER(lptimer_peripheral) |= irq;
}

/*---------------------------------------------------------------------------*/
/** @brief Disable lptimer Interrupts.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 * @param[in] irq Logical or of all interrupt enable bits to be cleared (@ref lptim_ier)
 */
void lptimer_disable_irq(uint32_t lptimer_peripheral, uint32_t irq)
{
	LPTIM_IER(lptimer_peripheral) &= ~irq;
}

/** @brief Enable lptimer.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 */
void lptimer_enable(uint32_t lptimer_peripheral)
{
	LPTIM_CR(lptimer_peripheral) |= LPTIM_CR_ENABLE;
}

/** @brief Disable lptimer.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 */
void lptimer_disable(uint32_t lptimer_peripheral)
{
	LPTIM_CR(lptimer_peripheral) &= ~LPTIM_CR_ENABLE;
}

/** @brief Start lptimer in a given mode.
 *
 * Starts the timer in specified mode - Either Single (@ref LPTIM_CR_SNGSTRT) or
 * Continuous mode (@ref LPTIM_CR_CNTSTRT). In Single mode, the timer will stop at
 * next match on compare or period value.
 * If LPTIM_CR_SNGSTRT is set while timer is started in countious mode, it
 * will stop at next match on compare or period value.
 * If Software trigger is disabled, start will be delayed until programmed
 * triggers is detected.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 * @param[in] mode lptimer start mode (@ref LPTIM_CR_SNGSTRT or @ref LPTIM_CR_CNTSTRT)
 */
void lptimer_start_counter(uint32_t lptimer_peripheral, uint32_t mode)
{
	LPTIM_CR(lptimer_peripheral) |= mode;
}

/** @brief Set lptimer clock prescaler.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 * @param[in] prescaler Clock prescaler (@ref lptim_cfgr_presc)
 */
void lptimer_set_prescaler(uint32_t lptimer_peripheral, uint32_t prescaler)
{
	uint32_t reg32 = LPTIM_CFGR(lptimer_peripheral);
	reg32 &= ~(LPTIM_CFGR_PRESC_MASK << LPTIM_CFGR_PRESC_SHIFT);
	LPTIM_CFGR(lptimer_peripheral) = reg32 | prescaler;
}

/** @brief Enable lptimer External Trigger
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 * @param[in] trigger Trigger selector (@ref lptim_cfgr_trigsel)
 */
void lptimer_enable_trigger(uint32_t lptimer_peripheral, uint32_t trigen)
{
	uint32_t reg32 = LPTIM_CFGR(lptimer_peripheral);
	reg32 &= ~(LPTIM_CFGR_TRIGEN_MASK << LPTIM_CFGR_TRIGEN_SHIFT);
	LPTIM_CFGR(lptimer_peripheral) = reg32 | trigen;
}

/** @brief Select lptimer Trigger Source
 *
 * Select timer external trigger source.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 * @param[in] trigger Trigger selector (@ref lptim_cfgr_trigsel)
 */
void lptimer_select_trigger_source(uint32_t lptimer_peripheral, uint32_t trigger_source)
{
	uint32_t reg32 = LPTIM_CFGR(lptimer_peripheral);
	reg32 &= ~(LPTIM_CFGR_TRIGSEL_MASK << LPTIM_CFGR_TRIGSEL_SHIFT);
	LPTIM_CFGR(lptimer_peripheral) = reg32 | trigger_source;
}

/** @brief Set lptimer counter Compare Value
 *
 * Set the timer compare value. Must only be set with timer enabled.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 * @param[in] compare_value Compare value.
 */
void lptimer_set_compare(uint32_t lptimer_peripheral, uint16_t compare_value)
{
	LPTIM_CMP(lptimer_peripheral) = compare_value;
}

/** @brief Set lptimer period
 *
 * Set the timer period in the auto-reload register. Must only be set with timer
 * enabled.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 * @param[in] period_value Autoreload value. Must be greater that CMP value.
 */
void lptimer_set_period(uint32_t lptimer_peripheral, uint16_t period_value)
{
	LPTIM_ARR(lptimer_peripheral) = period_value;
}

/** @brief Enable lptimer Preload mode.
 *
 * Enable lptimer preload mode, delaying update of period and compare registers
 * to the end of current period.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 */
void lptimer_enable_preload(uint32_t lptimer_peripheral)
{
	LPTIM_CFGR(lptimer_peripheral) |= LPTIM_CFGR_PRELOAD;
}

/** @brief Disable lptimer Preload mode.
 *
 * Disable lptimer preload mode, ensureing updated period and compare registers
 * values are taken in account immediatly.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 */
void lptimer_disable_preload(uint32_t lptimer_peripheral)
{
	LPTIM_CFGR(lptimer_peripheral) &= ~LPTIM_CFGR_PRELOAD;
}


/** @brief Set lptimer Internal Clock source
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 */
void lptimer_set_internal_clock_source(uint32_t lptimer_peripheral)
{
	LPTIM_CFGR(lptimer_peripheral) &= ~LPTIM_CFGR_CKSEL;
}

/** @brief Set lptimer External Clock source
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 */
void lptimer_set_external_clock_source(uint32_t lptimer_peripheral)
{
	LPTIM_CFGR(lptimer_peripheral) |= LPTIM_CFGR_CKSEL;
}

/** @brief Set lptimer Waveform Output Polarity High
 *
 * Set lptimer waveform output to reflect compare result between LPTIN_CNT
 * and LPTIM_CMP.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 */
void lptimer_set_waveform_polarity_high(uint32_t lptimer_peripheral)
{
	LPTIM_CFGR(lptimer_peripheral) |= LPTIM_CFGR_WAVPOL;
}

/** @brief Set lptimer Waveform Output Polarity Low
 *
 * Set lptimer waveform output to reflect the inverse of the compare result
 * between LPTIN_CNT and LPTIM_CMP.
 *
 * @param[in] lptimer_peripheral lptimer base address (@ref lptim_reg_base)
 */
void lptimer_set_waveform_polarity_low(uint32_t lptimer_peripheral)
{
	LPTIM_CFGR(lptimer_peripheral) &= ~LPTIM_CFGR_WAVPOL;
}

/**@}*/