Skip to content

Commit

Permalink
fixup! delete center mode and adapt pins
Browse files Browse the repository at this point in the history
  • Loading branch information
Semjon Kerner committed Aug 17, 2018
1 parent 9ea2b93 commit 7cf60d8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
4 changes: 3 additions & 1 deletion boards/common/nrf52xxxdk/include/periph_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,15 @@ static const i2c_conf_t i2c_config[] = {
#define I2C_NUMOF (sizeof(i2c_config) / sizeof(i2c_config[0]))
/** @} */

#if defined(BOARD_NRF52DK) || defined(BOARD_NRF52840DK)
/**
* @name PWM configuration
* @{
*/
static const pwm_conf_t pwm_config[] = {
{ NRF_PWM0, { 23, 24, 25, 26 } }
{ NRF_PWM0, { 28, 29, 30, 31 } }
};
#endif

#define PWM_NUMOF (sizeof(pwm_config) / sizeof(pwm_config[0]))
/** @} */
Expand Down
4 changes: 2 additions & 2 deletions cpu/nrf52/include/periph_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ typedef struct {
typedef enum {
PWM_LEFT = PWM_MODE(0, 1), /**< left aligned PWM */
PWM_RIGHT = PWM_MODE(0, 0), /**< right aligned PWM */
PWM_CENTER = PWM_MODE(1, 1), /**< center aligned PWM */
PWM_CENTER_INV = PWM_MODE(1, 0) /**< center aligned with negative polarity */
PWM_CENTER = PWM_MODE(1, 1), /**< not supported */
PWM_CENTER_INV = PWM_MODE(1, 0) /**< not supported */
} pwm_mode_t;
/** @} */

Expand Down
18 changes: 12 additions & 6 deletions cpu/nrf52/periph/pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#define POL_MASK (0x8000)
#define MAX_RES (0x7fff)
#define PIN_CNF_SET (GPIO_PIN_CNF_DIR_Output | GPIO_PIN_CNF_INPUT_Msk)
#define CNTR_MODE_MASK (0x0001)
#define MAX_PRESCALER (7U)

/** @} */
Expand All @@ -53,21 +52,28 @@ static inline NRF_PWM_Type *dev(pwm_t pwm)
return pwm_config[pwm].dev;
}

/**
* @note Center mode is not supported. Use left or right aligned PWM modes.
*/
uint32_t pwm_init(pwm_t pwm, pwm_mode_t mode, uint32_t freq, uint16_t res)
{
uint32_t real_clk;

/* check if given device is valid */
if ((pwm >= PWM_NUMOF) || (res > MAX_RES)) {
return 0;
}

/* check if pwm mode is supported */
if ((mode != PWM_RIGHT) && (mode != PWM_LEFT)) {
return 0;
}

/* make sure the device is stopped */
dev(pwm)->TASKS_STOP = 1;
dev(pwm)->ENABLE = 0;

/* calculate the needed frequency, for center modes we need double */
uint32_t clk = ((freq * res) << (mode & PWM_CENTER));
uint32_t real_clk;
uint32_t clk = (freq * res);
/* match to best fitting prescaler */
for (uint8_t i = 0; i < (MAX_PRESCALER + 1); i++) {
real_clk = (PERIPH_CLOCK >> i);
Expand Down Expand Up @@ -102,8 +108,8 @@ uint32_t pwm_init(pwm_t pwm, pwm_mode_t mode, uint32_t freq, uint16_t res)
dev(pwm)->COUNTERTOP = (res - 1);

/* select PWM mode */
dev(pwm)->MODE = (mode & CNTR_MODE_MASK);
dev(pwm)->LOOP = 0;
dev(pwm)->MODE = PWM_MODE_UPDOWN_Up;
dev(pwm)->LOOP = PWM_LOOP_CNT_Disabled;
dev(pwm)->DECODER = PWM_DECODER_LOAD_Individual;

DEBUG("MODE: 0x%08x\n", (int)dev(pwm)->MODE);
Expand Down

0 comments on commit 7cf60d8

Please sign in to comment.