2015-05-31 00:45:57 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Hamburg University of Applied Sciences
|
|
|
|
* 2015 Freie Universität Berlin
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
|
|
|
* Public License v2.1. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup cpu_samd21
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup drivers_periph_pwm
|
2015-05-31 00:45:57 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Low-level PWM driver implementation
|
|
|
|
*
|
|
|
|
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2020-05-03 01:11:44 +02:00
|
|
|
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
|
2015-05-31 00:45:57 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "board.h"
|
2015-10-22 13:08:39 +02:00
|
|
|
#include "periph/gpio.h"
|
2015-05-31 00:45:57 +02:00
|
|
|
#include "periph/pwm.h"
|
2015-10-22 13:08:39 +02:00
|
|
|
|
2015-05-31 00:45:57 +02:00
|
|
|
static inline Tcc *_tcc(pwm_t dev)
|
|
|
|
{
|
2020-05-03 01:11:44 +02:00
|
|
|
return pwm_config[dev].tim.dev;
|
2015-05-31 00:45:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint8_t _chan(pwm_t dev, int chan)
|
|
|
|
{
|
|
|
|
return pwm_config[dev].chan[chan].chan;
|
|
|
|
}
|
|
|
|
|
2020-05-03 01:11:44 +02:00
|
|
|
static uint8_t _get_prescaler(unsigned int target, int *scale)
|
2015-05-31 00:45:57 +02:00
|
|
|
{
|
|
|
|
if (target == 0) {
|
|
|
|
return 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target >= 512) {
|
|
|
|
*scale = 1024;
|
|
|
|
return TCC_CTRLA_PRESCALER_DIV1024_Val;
|
|
|
|
}
|
|
|
|
if (target >= 128) {
|
|
|
|
*scale = 256;
|
|
|
|
return TCC_CTRLA_PRESCALER_DIV256_Val;
|
|
|
|
}
|
|
|
|
if (target >= 32) {
|
|
|
|
*scale = 64;
|
|
|
|
return TCC_CTRLA_PRESCALER_DIV64_Val;
|
|
|
|
}
|
|
|
|
if (target >= 12) {
|
|
|
|
*scale = 16;
|
|
|
|
return TCC_CTRLA_PRESCALER_DIV16_Val;
|
|
|
|
}
|
|
|
|
if (target >= 6) {
|
|
|
|
*scale = 8;
|
|
|
|
return TCC_CTRLA_PRESCALER_DIV8_Val;
|
|
|
|
}
|
|
|
|
if (target >= 3) {
|
|
|
|
*scale = 4;
|
|
|
|
return TCC_CTRLA_PRESCALER_DIV4_Val;
|
|
|
|
}
|
|
|
|
*scale = target;
|
|
|
|
return target - 1;
|
|
|
|
}
|
|
|
|
|
2020-05-03 01:11:44 +02:00
|
|
|
static uint8_t _get_cc_numof(Tcc *tcc)
|
|
|
|
{
|
|
|
|
switch ((uintptr_t) tcc) {
|
|
|
|
#ifdef TCC0_CC_NUM
|
|
|
|
case (uintptr_t)TCC0:
|
|
|
|
return TCC0_CC_NUM;
|
|
|
|
#endif
|
|
|
|
#ifdef TCC1_CC_NUM
|
|
|
|
case (uintptr_t)TCC1:
|
|
|
|
return TCC1_CC_NUM;
|
|
|
|
#endif
|
|
|
|
#ifdef TCC2_CC_NUM
|
|
|
|
case (uintptr_t)TCC2:
|
|
|
|
return TCC2_CC_NUM;
|
|
|
|
#endif
|
|
|
|
#ifdef TCC3_CC_NUM
|
|
|
|
case (uintptr_t)TCC3:
|
|
|
|
return TCC3_CC_NUM;
|
|
|
|
#endif
|
|
|
|
#ifdef TCC4_CC_NUM
|
|
|
|
case (uintptr_t)TCC4:
|
|
|
|
return TCC4_CC_NUM;
|
|
|
|
#endif
|
|
|
|
#ifdef TCC5_CC_NUM
|
|
|
|
case (uintptr_t)TCC5:
|
|
|
|
return TCC5_CC_NUM;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-07 15:05:43 +01:00
|
|
|
static void poweron(pwm_t dev)
|
|
|
|
{
|
2020-05-03 01:11:44 +02:00
|
|
|
const pwm_conf_t *cfg = &pwm_config[dev];
|
|
|
|
|
|
|
|
sam0_gclk_enable(cfg->gclk_src);
|
|
|
|
#ifdef MCLK
|
|
|
|
GCLK->PCHCTRL[cfg->tim.gclk_id].reg = GCLK_PCHCTRL_GEN(cfg->gclk_src)
|
|
|
|
| GCLK_PCHCTRL_CHEN;
|
|
|
|
*cfg->tim.mclk |= cfg->tim.mclk_mask;
|
|
|
|
#else
|
|
|
|
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN
|
|
|
|
| GCLK_CLKCTRL_GEN(cfg->gclk_src)
|
|
|
|
| GCLK_CLKCTRL_ID(cfg->tim.gclk_id);
|
|
|
|
PM->APBCMASK.reg |= cfg->tim.pm_mask;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void poweroff(pwm_t dev)
|
|
|
|
{
|
|
|
|
const pwm_conf_t *cfg = &pwm_config[dev];
|
|
|
|
|
|
|
|
#ifdef MCLK
|
|
|
|
GCLK->PCHCTRL[cfg->tim.gclk_id].reg = 0;
|
|
|
|
*cfg->tim.mclk &= ~cfg->tim.mclk_mask;
|
|
|
|
#else
|
|
|
|
PM->APBCMASK.reg &= ~cfg->tim.pm_mask;
|
|
|
|
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_GEN_GCLK7
|
|
|
|
| GCLK_CLKCTRL_ID(cfg->tim.gclk_id);
|
|
|
|
#endif
|
2017-02-07 15:05:43 +01:00
|
|
|
}
|
|
|
|
|
2015-10-21 12:42:52 +02:00
|
|
|
uint32_t pwm_init(pwm_t dev, pwm_mode_t mode, uint32_t freq, uint16_t res)
|
2015-05-31 00:45:57 +02:00
|
|
|
{
|
|
|
|
uint8_t prescaler;
|
|
|
|
int scale = 1;
|
2015-10-21 12:42:52 +02:00
|
|
|
uint32_t f_real;
|
2015-05-31 00:45:57 +02:00
|
|
|
|
2016-01-15 19:54:43 +01:00
|
|
|
if ((unsigned int)dev >= PWM_NUMOF) {
|
2015-10-21 12:42:52 +02:00
|
|
|
return 0;
|
2015-05-31 00:45:57 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 01:11:44 +02:00
|
|
|
const uint32_t f_src = sam0_gclk_freq(pwm_config[dev].gclk_src);
|
|
|
|
|
2015-05-31 00:45:57 +02:00
|
|
|
/* calculate the closest possible clock presacler */
|
2020-05-03 01:11:44 +02:00
|
|
|
prescaler = _get_prescaler(f_src / (freq * res), &scale);
|
2015-05-31 00:45:57 +02:00
|
|
|
if (prescaler == 0xff) {
|
2015-10-21 12:42:52 +02:00
|
|
|
return 0;
|
2015-05-31 00:45:57 +02:00
|
|
|
}
|
2020-05-03 01:11:44 +02:00
|
|
|
f_real = f_src / (scale * res);
|
2015-05-31 00:45:57 +02:00
|
|
|
|
|
|
|
/* configure the used pins */
|
2020-04-26 22:26:01 +02:00
|
|
|
for (unsigned i = 0; i < pwm_config[dev].chan_numof; i++) {
|
2016-02-19 16:33:57 +01:00
|
|
|
if (pwm_config[dev].chan[i].pin != GPIO_UNDEF) {
|
2016-02-20 15:41:04 +01:00
|
|
|
gpio_init(pwm_config[dev].chan[i].pin, GPIO_OUT);
|
2016-02-19 16:33:57 +01:00
|
|
|
gpio_init_mux(pwm_config[dev].chan[i].pin, pwm_config[dev].chan[i].mux);
|
|
|
|
}
|
2015-05-31 00:45:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* power on the device */
|
2017-02-07 15:05:43 +01:00
|
|
|
poweron(dev);
|
2015-09-17 15:57:51 +02:00
|
|
|
|
2015-05-31 00:45:57 +02:00
|
|
|
/* reset TCC module */
|
|
|
|
_tcc(dev)->CTRLA.reg = TCC_CTRLA_SWRST;
|
2016-02-11 14:25:02 +01:00
|
|
|
while (_tcc(dev)->SYNCBUSY.reg & TCC_SYNCBUSY_SWRST) {}
|
2020-05-03 01:11:44 +02:00
|
|
|
|
2015-05-31 00:45:57 +02:00
|
|
|
/* set PWM mode */
|
|
|
|
switch (mode) {
|
|
|
|
case PWM_LEFT:
|
|
|
|
_tcc(dev)->CTRLBCLR.reg = TCC_CTRLBCLR_DIR; /* count up */
|
|
|
|
break;
|
|
|
|
case PWM_RIGHT:
|
|
|
|
_tcc(dev)->CTRLBSET.reg = TCC_CTRLBSET_DIR; /* count down */
|
|
|
|
break;
|
|
|
|
case PWM_CENTER: /* currently not supported */
|
|
|
|
default:
|
2015-10-21 12:42:52 +02:00
|
|
|
return 0;
|
2015-05-31 00:45:57 +02:00
|
|
|
}
|
2016-02-11 14:25:02 +01:00
|
|
|
while (_tcc(dev)->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {}
|
2015-05-31 00:45:57 +02:00
|
|
|
|
|
|
|
/* configure the TCC device */
|
2020-05-03 01:11:44 +02:00
|
|
|
_tcc(dev)->CTRLA.reg = TCC_CTRLA_PRESCSYNC_GCLK_Val
|
|
|
|
| TCC_CTRLA_PRESCALER(prescaler);
|
|
|
|
|
2015-05-31 00:45:57 +02:00
|
|
|
/* select the waveform generation mode -> normal PWM */
|
|
|
|
_tcc(dev)->WAVE.reg = (TCC_WAVE_WAVEGEN_NPWM);
|
2016-02-11 14:25:02 +01:00
|
|
|
while (_tcc(dev)->SYNCBUSY.reg & TCC_SYNCBUSY_WAVE) {}
|
2020-05-03 01:11:44 +02:00
|
|
|
|
2015-05-31 00:45:57 +02:00
|
|
|
/* set the selected period */
|
2015-10-21 12:42:52 +02:00
|
|
|
_tcc(dev)->PER.reg = (res - 1);
|
2016-02-11 14:25:02 +01:00
|
|
|
while (_tcc(dev)->SYNCBUSY.reg & TCC_SYNCBUSY_PER) {}
|
2020-05-03 01:11:44 +02:00
|
|
|
|
2015-05-31 00:45:57 +02:00
|
|
|
/* start PWM operation */
|
2020-05-03 01:11:44 +02:00
|
|
|
_tcc(dev)->CTRLA.reg |= TCC_CTRLA_ENABLE;
|
|
|
|
|
2015-05-31 00:45:57 +02:00
|
|
|
/* return the actual frequency the PWM is running at */
|
|
|
|
return f_real;
|
|
|
|
}
|
|
|
|
|
2015-10-21 12:42:52 +02:00
|
|
|
uint8_t pwm_channels(pwm_t dev)
|
|
|
|
{
|
2020-04-26 22:26:01 +02:00
|
|
|
return pwm_config[dev].chan_numof;
|
2015-10-21 12:42:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void pwm_set(pwm_t dev, uint8_t channel, uint16_t value)
|
2015-05-31 00:45:57 +02:00
|
|
|
{
|
2020-04-26 22:26:01 +02:00
|
|
|
if ((channel >= pwm_config[dev].chan_numof) ||
|
2016-02-19 16:33:57 +01:00
|
|
|
(pwm_config[dev].chan[channel].pin == GPIO_UNDEF)) {
|
2015-10-21 12:42:52 +02:00
|
|
|
return;
|
2015-05-31 00:45:57 +02:00
|
|
|
}
|
2020-04-02 23:48:42 +02:00
|
|
|
|
|
|
|
uint8_t chan = _chan(dev, channel);
|
2020-05-03 01:11:44 +02:00
|
|
|
|
|
|
|
/* TODO: use OTMX for pin remapping */
|
|
|
|
chan %= _get_cc_numof(_tcc(dev));
|
|
|
|
|
|
|
|
_tcc(dev)->CC[chan].reg = value;
|
|
|
|
while (_tcc(dev)->SYNCBUSY.reg & (TCC_SYNCBUSY_CC0 << chan)) {}
|
2015-05-31 00:45:57 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 15:05:43 +01:00
|
|
|
void pwm_poweron(pwm_t dev)
|
2015-05-31 00:45:57 +02:00
|
|
|
{
|
2017-02-07 15:05:43 +01:00
|
|
|
poweron(dev);
|
2015-05-31 00:45:57 +02:00
|
|
|
_tcc(dev)->CTRLA.reg |= (TCC_CTRLA_ENABLE);
|
|
|
|
}
|
|
|
|
|
2017-02-07 15:05:43 +01:00
|
|
|
void pwm_poweroff(pwm_t dev)
|
2015-05-31 00:45:57 +02:00
|
|
|
{
|
|
|
|
_tcc(dev)->CTRLA.reg &= ~(TCC_CTRLA_ENABLE);
|
2020-05-03 01:11:44 +02:00
|
|
|
poweroff(dev);
|
2015-05-31 00:45:57 +02:00
|
|
|
}
|