summaryrefslogtreecommitdiffstats
path: root/Sensor Watch Starter Project/hpl/tcc/hpl_tcc.c
blob: 52fc6f3c751c16cd8a62d3819eda45eacd69d0b4 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
 * \file
 *
 * \brief SAM TCC
 *
 * Copyright (c) 2014-2019 Microchip Technology Inc. and its subsidiaries.
 *
 * \asf_license_start
 *
 * \page License
 *
 * Subject to your compliance with these terms, you may use Microchip
 * software and any derivatives exclusively with Microchip products.
 * It is your responsibility to comply with third party license terms applicable
 * to your use of third party software (including open source software) that
 * may accompany Microchip software.
 *
 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
 *
 * \asf_license_stop
 *
 */

#include <compiler.h>
#include <hpl_pwm.h>
#include <hpl_tcc.h>
#include <hpl_tcc_config.h>
#include <hpl_timer.h>
#include <utils.h>
#include <utils_assert.h>

/**
 * \brief TCC configuration type
 */
struct tcc_cfg {
	void *                 hw; /*!< instance of TCC */
	IRQn_Type              irq;
	hri_tcc_ctrla_reg_t    ctrl_a;
	hri_tcc_ctrlbset_reg_t ctrl_b;
	hri_tcc_dbgctrl_reg_t  dbg_ctrl;
	hri_tcc_evctrl_reg_t   event_ctrl;
	hri_tcc_cc_reg_t       cc0;
	hri_tcc_cc_reg_t       cc1;
	hri_tcc_cc_reg_t       cc2;
	hri_tcc_cc_reg_t       cc3;
	hri_tcc_per_reg_t      per;
};
/**
 * \brief pwm configuration type
 */
struct tcc_pwm_cfg {
	void *    hw; /*!< instance of TCC */
	IRQn_Type irq;
	uint8_t   sel_ch;
	uint32_t  period;
	uint32_t  duty_cycle;
	uint32_t  wave;
};

/**
 * \internal Retrieve configuration
 *
 * \param[in] hw The pointer of TCC base address
 *
 * \return The configuration
 */
static struct tcc_cfg *_get_tcc_cfg(void *hw);

/**
 * \brief Array of TCC configurations
 */
static struct tcc_cfg _cfgs[1] = {
    {(void *)TCC0,
     TCC0_IRQn,
     CONF_TCC0_CTRLA,
     CONF_TCC0_CTRLB,
     CONF_TCC0_DBGCTRL,
     CONF_TCC0_EVCTRL,
     CONF_TCC0_CC0,
     CONF_TCC0_CC1,
     CONF_TCC0_CC2,
     CONF_TCC0_CC3,
     CONF_TCC0_PER},
};

/**
 * \internal Retrieve configuration
 *
 * \param[in] hw The pointer of TCC base address
 *
 * \return The configuration
 */
static struct tcc_pwm_cfg *_get_tcc_pwm_cfg(void *hw);

/**
 * \brief Array of PWM configurations
 */
static struct tcc_pwm_cfg _cfgs_pwm[1] = {
    {(void *)TCC0,
     TCC0_IRQn,
     CONF_TCC0_SEL_CH,
     CONF_TCC0_PER_REG,
     CONF_TCC0_CCX_REG,
     (CONF_TCC0_WAVEGEN << TCC_WAVE_WAVEGEN_Pos)},
};
/* Renamed access REG name PERB -> PERBUF */
#define hri_tcc_write_PERB_reg hri_tcc_write_PERBUF_reg
#define hri_tcc_read_PERB_reg hri_tcc_read_PERBUF_reg

/** Renamed access REG name CCB -> CCBUF */
#define hri_tcc_write_CCB_reg hri_tcc_write_CCBUF_reg
#define hri_tcc_read_CCB_reg hri_tcc_read_CCBUF_reg

static struct _pwm_device *_tcc0_dev = NULL;

/**
 * \brief Set of pointer to hal_pwm helper functions
 */
static struct _pwm_hpl_interface _tcc_pwm_functions = {
    _tcc_pwm_init,
    _tcc_pwm_deinit,
    _tcc_start_pwm,
    _tcc_stop_pwm,
    _tcc_set_pwm_param,
    _tcc_is_pwm_enabled,
    _tcc_pwm_get_period,
    _tcc_pwm_get_duty,
    _tcc_pwm_set_irq_state,
};

/**
 * \brief Init irq param with the given tcc hardware instance
 */
static void _tcc_init_irq_param(const void *const hw, void *dev)
{
	if (hw == TCC0) {
		_tcc0_dev = (struct _pwm_device *)dev;
	}
}
/**
 * \brief Initialize TCC for PWM mode
 */
int32_t _tcc_pwm_init(struct _pwm_device *const device, void *const hw)
{
	struct tcc_cfg *cfg = _get_tcc_cfg(hw);
	if (cfg == NULL) {
		return ERR_NOT_FOUND;
	}
	struct tcc_pwm_cfg *cfg_pwm = _get_tcc_pwm_cfg(hw);
	if (cfg_pwm == NULL) {
		return ERR_NOT_FOUND;
	}

	device->hw = hw;

	if (!hri_tcc_is_syncing(hw, TCC_SYNCBUSY_SWRST)) {
		if (hri_tcc_get_CTRLA_reg(hw, TCC_CTRLA_ENABLE)) {
			hri_tcc_clear_CTRLA_ENABLE_bit(hw);
			hri_tcc_wait_for_sync(hw, TCC_SYNCBUSY_ENABLE);
		}
		hri_tcc_write_CTRLA_reg(hw, TCC_CTRLA_SWRST);
	}
	hri_tcc_wait_for_sync(hw, TCC_SYNCBUSY_SWRST);

	hri_tcc_write_CTRLA_reg(hw, cfg->ctrl_a);
	hri_tcc_set_CTRLB_reg(hw, cfg->ctrl_b);
	hri_tcc_write_DBGCTRL_reg(hw, cfg->dbg_ctrl);
	hri_tcc_write_EVCTRL_reg(hw, cfg->event_ctrl);

	hri_tcc_write_WAVE_reg(hw, cfg_pwm->wave);
	hri_tcc_write_PER_reg(hw, cfg_pwm->period);
	cfg->per = cfg_pwm->period;
	switch (cfg_pwm->sel_ch) {
	case 0:
		cfg->cc0 = cfg_pwm->duty_cycle;
		hri_tcc_write_CC_reg(hw, 0, cfg->cc0);
		break;
	case 1:
		cfg->cc1 = cfg_pwm->duty_cycle;
		hri_tcc_write_CC_reg(hw, 1, cfg->cc1);
		break;
	case 2:
		cfg->cc2 = cfg_pwm->duty_cycle;
		hri_tcc_write_CC_reg(hw, 2, cfg->cc2);
		break;
	case 3:
		cfg->cc3 = cfg_pwm->duty_cycle;
		hri_tcc_write_CC_reg(hw, 3, cfg->cc3);
		break;
	default:
		return ERR_NO_RESOURCE;
		break;
	}
	hri_tcc_clear_CTRLB_LUPD_bit(hw);

	_tcc_init_irq_param(hw, (void *)device);
	NVIC_DisableIRQ((IRQn_Type)cfg_pwm->irq);
	NVIC_ClearPendingIRQ((IRQn_Type)cfg_pwm->irq);
	NVIC_EnableIRQ((IRQn_Type)cfg_pwm->irq);

	return ERR_NONE;
}
/**
 * \brief De-initialize TCC for PWM mode
 */
void _tcc_pwm_deinit(struct _pwm_device *const device)
{
	void *const         hw      = device->hw;
	struct tcc_pwm_cfg *cfg_pwm = _get_tcc_pwm_cfg(hw);
	if (cfg_pwm != NULL) {
		NVIC_DisableIRQ((IRQn_Type)cfg_pwm->irq);
		hri_tcc_clear_CTRLA_ENABLE_bit(hw);
		hri_tcc_set_CTRLA_SWRST_bit(hw);
	}
}
/**
 * \brief Start PWM
 */
void _tcc_start_pwm(struct _pwm_device *const device)
{
	hri_tcc_set_CTRLA_ENABLE_bit(device->hw);
}
/**
 * \brief Stop PWM
 */
void _tcc_stop_pwm(struct _pwm_device *const device)
{
	hri_tcc_clear_CTRLA_ENABLE_bit(device->hw);
}
/**
 * \brief Set PWM parameter
 */
void _tcc_set_pwm_param(struct _pwm_device *const device, const pwm_period_t period, const pwm_period_t duty_cycle)
{
	void *const         hw      = device->hw;
	struct tcc_pwm_cfg *cfg_pwm = _get_tcc_pwm_cfg(hw);
	if (cfg_pwm != NULL) {
		hri_tcc_write_PERB_reg(hw, period);
		hri_tcc_write_CCB_reg(hw, cfg_pwm->sel_ch, duty_cycle);
		;
	}
}
/**
 * \brief Get pwm waveform period value
 */
pwm_period_t _tcc_pwm_get_period(const struct _pwm_device *const device)
{
	return (pwm_period_t)(hri_tcc_read_PERB_reg(device->hw));
}
/**
 * \brief Get pwm waveform duty cycle
 */
uint32_t _tcc_pwm_get_duty(const struct _pwm_device *const device)
{
	void *const         hw      = device->hw;
	struct tcc_pwm_cfg *cfg_pwm = _get_tcc_pwm_cfg(hw);
	if (cfg_pwm == NULL) {
		return ERR_NOT_FOUND;
	}
	uint32_t per        = hri_tcc_read_PERB_reg(hw);
	uint32_t duty_cycle = hri_tcc_read_CCB_reg(hw, cfg_pwm->sel_ch);

	return ((duty_cycle * 1000) / per);
}
/**
 * \brief Check if PWM is running
 */
bool _tcc_is_pwm_enabled(const struct _pwm_device *const device)
{
	return hri_tcc_get_CTRLA_ENABLE_bit(device->hw);
}
/**
 * \brief Enable/disable PWM interrupt
 */
void _tcc_pwm_set_irq_state(struct _pwm_device *const device, const enum _pwm_callback_type type, const bool disable)
{
	ASSERT(device);

	if (PWM_DEVICE_PERIOD_CB == type) {
		hri_tcc_write_INTEN_OVF_bit(device->hw, disable);
	} else if (PWM_DEVICE_ERROR_CB == type) {
		hri_tcc_write_INTEN_ERR_bit(device->hw, disable);
	}
}

/**
 * \brief Retrieve timer helper functions
 */
struct _timer_hpl_interface *_tcc_get_timer(void)
{
	return NULL;
}

/**
 * \brief Retrieve pwm helper functions
 */
struct _pwm_hpl_interface *_tcc_get_pwm(void)
{
	return &_tcc_pwm_functions;
}
/**
 * \internal TC interrupt handler for PWM
 *
 * \param[in] instance TC instance number
 */
static void tcc_pwm_interrupt_handler(struct _pwm_device *device)
{
	void *const hw = device->hw;

	if (hri_tcc_get_interrupt_OVF_bit(hw)) {
		hri_tcc_clear_interrupt_OVF_bit(hw);
		if (NULL != device->callback.pwm_period_cb) {
			device->callback.pwm_period_cb(device);
		}
	}
	if (hri_tcc_get_INTEN_ERR_bit(hw)) {
		hri_tcc_clear_interrupt_ERR_bit(hw);
		if (NULL != device->callback.pwm_error_cb) {
			device->callback.pwm_error_cb(device);
		}
	}
}

/**
 * \brief TCC interrupt handler
 */
void TCC0_Handler(void)
{
	tcc_pwm_interrupt_handler(_tcc0_dev);
}

static struct tcc_cfg *_get_tcc_cfg(void *hw)
{
	uint8_t i;

	for (i = 0; i < ARRAY_SIZE(_cfgs); i++) {
		if (_cfgs[i].hw == hw) {
			return &(_cfgs[i]);
		}
	}
	return NULL;
}

static struct tcc_pwm_cfg *_get_tcc_pwm_cfg(void *hw)
{
	uint8_t i;

	for (i = 0; i < ARRAY_SIZE(_cfgs_pwm); i++) {
		if (_cfgs_pwm[i].hw == hw) {
			return &(_cfgs_pwm[i]);
		}
	}
	return NULL;
}