Browse Source

Merge pull request #4137 from haukepetersen/opt_samr21_pwm

cpu/samd21: optimized pin config for PWM driver
cc430
Andreas "Paul" Pauli 7 years ago
parent
commit
bf63e09a65
  1. 34
      boards/samr21-xpro/include/periph_conf.h
  2. 17
      cpu/samd21/include/periph_cpu.h
  3. 13
      cpu/samd21/periph/pwm.c

34
boards/samr21-xpro/include/periph_conf.h

@ -132,43 +132,21 @@ static const uart_conf_t uart_config[] = {
#define PWM_0_CHANNELS PWM_MAX_CHANNELS
#define PWM_1_CHANNELS PWM_MAX_CHANNELS
/**
* @brief PWM channel configuration data structure
*
* TODO: this should be moved into the CPU folder
*/
typedef struct {
PortGroup *port; /**< GPIO port */
uint8_t pin; /**< GPIO pin */
uint8_t fnct; /**< pin function multiplex value */
uint8_t chan; /**< TCC channel to use */
} pwm_conf_chan_t;
/**
* @brief PWM device configuration data structure
*
* TODO: this should be moved into the CPU folder
*/
typedef struct {
Tcc *dev; /*< TCC device to use */
pwm_conf_chan_t chan[2]; /**< channel configuration */
} pwm_conf_t;
/* PWM device configuration */
#if PWM_NUMOF
static const pwm_conf_t pwm_config[] = {
#if PWM_0_EN
{TCC1, {
/* port , pin, AF, chan */
{(PortGroup *)0x41004400, 6, 4, 0},
{(PortGroup *)0x41004400, 7, 4, 1}
/* GPIO pin, MUX value, TCC channel */
{GPIO_PIN(PA, 6), GPIO_MUX_E, 0},
{GPIO_PIN(PA, 7), GPIO_MUX_E, 1}
}},
#endif
#if PWM_1_EN
{TCC0, {
/* port , pin, AF, chan */
{(PortGroup *)0x41004400, 18, 5, 2},
{(PortGroup *)0x41004400, 19, 5, 3}
/* GPIO pin, MUX value, TCC channel */
{GPIO_PIN(PA, 18), GPIO_MUX_F, 2},
{GPIO_PIN(PA, 19), GPIO_MUX_F, 3}
}},
#endif
};

17
cpu/samd21/include/periph_cpu.h

@ -79,6 +79,23 @@ typedef enum {
GPIO_MUX_H = 0x7, /**< select peripheral function H */
} gpio_mux_t;
/**
* @brief PWM channel configuration data structure
*/
typedef struct {
gpio_t pin; /**< GPIO pin */
gpio_mux_t mux; /**< pin function multiplex value */
uint8_t chan; /**< TCC channel to use */
} pwm_conf_chan_t;
/**
* @brief PWM device configuration data structure
*/
typedef struct {
Tcc *dev; /*< TCC device to use */
pwm_conf_chan_t chan[2]; /**< channel configuration */
} pwm_conf_t;
/**
* @brief UART device configuration
*/

13
cpu/samd21/periph/pwm.c

@ -26,8 +26,9 @@
#include "log.h"
#include "cpu.h"
#include "board.h"
#include "periph/gpio.h"
#include "periph/pwm.h"
#include "periph_conf.h"
/* ignore file in case no PWM devices are defined */
#if PWM_NUMOF
@ -109,14 +110,8 @@ int pwm_init(pwm_t dev, pwm_mode_t mode,
/* configure the used pins */
for (int i = 0; i < PWM_MAX_CHANNELS; i++) {
PortGroup *port = pwm_config[dev].chan[i].port;
int pin = pwm_config[dev].chan[i].pin;
int fnct = pwm_config[dev].chan[i].fnct;
/* set pin as output and enable the MUX */
port->DIRSET.reg = (1 << pin);
port->PINCFG[pin].reg = (PORT_PINCFG_PMUXEN);
port->PMUX[pin >> 1].reg &= ~(0xf << (4 * (pin & 0x1)));
port->PMUX[pin >> 1].reg |= (fnct << (4 * (pin & 0x1)));
gpio_init(pwm_config[dev].chan[i].pin, GPIO_DIR_OUT, GPIO_NOPULL);
gpio_init_mux(pwm_config[dev].chan[i].pin, pwm_config[dev].chan[i].mux);
}
/* power on the device */

Loading…
Cancel
Save