mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge pull request #1318 from haukepetersen/msba2_pwm
cpu: added low-level PWM driver for the lpc2387
This commit is contained in:
commit
749d6a7446
46
boards/avsextrem/include/periph_conf.h
Normal file
46
boards/avsextrem/include/periph_conf.h
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (C) 2014 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 boards_avsextrem
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Peripheral configuration for the avsextrem
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*/
|
||||
|
||||
#ifndef __PERIPH_CONF_H
|
||||
#define __PERIPH_CONF_H
|
||||
|
||||
#include "lpc2387.h"
|
||||
|
||||
/**
|
||||
* @brief PWM device and pinout configuration
|
||||
*/
|
||||
#define PWM_NUMOF (0) /* disable PWM for now as no pins are specified */
|
||||
#define PWM_0_EN (0)
|
||||
|
||||
/* PWM_0 device configuration */
|
||||
#define PWM_0_CH0 (3) /* TODO: adjust pins for avsextrem */
|
||||
#define PWM_0_CH0_MR PWM1MR3
|
||||
#define PWM_0_CH1 (4)
|
||||
#define PWM_0_CH1_MR PWM1MR4
|
||||
#define PWM_0_CH2 (5)
|
||||
#define PWM_0_CH2_MR PWM1MR5
|
||||
/* PWM_0 pin configuration */
|
||||
#define PWM_0_PORT PINSEL4
|
||||
#define PWM_0_CH0_PIN (2)
|
||||
#define PWM_0_CH1_PIN (3)
|
||||
#define PWM_0_CH2_PIN (4)
|
||||
#define PWM_0_FUNC (1)
|
||||
|
||||
|
||||
#endif /* __PERIPH_CONF_H */
|
||||
/** @} */
|
46
boards/msba2/include/periph_conf.h
Normal file
46
boards/msba2/include/periph_conf.h
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (C) 2014 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 boards_msba2
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief MSB-A2 peripheral configuration
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*/
|
||||
|
||||
#ifndef __PERIPH_CONF_H
|
||||
#define __PERIPH_CONF_H
|
||||
|
||||
#include "lpc2387.h"
|
||||
|
||||
/**
|
||||
* @brief PWM device and pinout configuration
|
||||
*/
|
||||
#define PWM_NUMOF (1)
|
||||
#define PWM_0_EN (1)
|
||||
|
||||
/* PWM_0 device configuration */
|
||||
#define PWM_0_CH0 (3)
|
||||
#define PWM_0_CH0_MR PWM1MR3
|
||||
#define PWM_0_CH1 (4)
|
||||
#define PWM_0_CH1_MR PWM1MR4
|
||||
#define PWM_0_CH2 (5)
|
||||
#define PWM_0_CH2_MR PWM1MR5
|
||||
/* PWM_0 pin configuration */
|
||||
#define PWM_0_PORT PINSEL4
|
||||
#define PWM_0_CH0_PIN (2)
|
||||
#define PWM_0_CH1_PIN (3)
|
||||
#define PWM_0_CH2_PIN (4)
|
||||
#define PWM_0_FUNC (1)
|
||||
|
||||
|
||||
#endif /* __PERIPH_CONF_H */
|
||||
/** @} */
|
46
boards/pttu/include/periph_conf.h
Normal file
46
boards/pttu/include/periph_conf.h
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (C) 2014 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 boards_pttu
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Peripheral configuration for the PTTU
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*/
|
||||
|
||||
#ifndef __PERIPH_CONF_H
|
||||
#define __PERIPH_CONF_H
|
||||
|
||||
#include "lpc2387.h"
|
||||
|
||||
/**
|
||||
* @brief PWM device and pinout configuration
|
||||
*/
|
||||
#define PWM_NUMOF (0) /* disable PWM for now as no pins are specified */
|
||||
#define PWM_0_EN (0)
|
||||
|
||||
/* PWM_0 device configuration */
|
||||
#define PWM_0_CH0 (3) /* TODO: adjust pins for the PTTU */
|
||||
#define PWM_0_CH0_MR PWM1MR3
|
||||
#define PWM_0_CH1 (4)
|
||||
#define PWM_0_CH1_MR PWM1MR4
|
||||
#define PWM_0_CH2 (5)
|
||||
#define PWM_0_CH2_MR PWM1MR5
|
||||
/* PWM_0 pin configuration */
|
||||
#define PWM_0_PORT PINSEL4
|
||||
#define PWM_0_CH0_PIN (2)
|
||||
#define PWM_0_CH1_PIN (3)
|
||||
#define PWM_0_CH2_PIN (4)
|
||||
#define PWM_0_FUNC (1)
|
||||
|
||||
|
||||
#endif /* __PERIPH_CONF_H */
|
||||
/** @} */
|
@ -2,7 +2,8 @@ MODULE =cpu
|
||||
|
||||
include $(RIOTCPU)/$(CPU)/Makefile.include
|
||||
|
||||
DIRS = $(RIOTCPU)/arm_common $(RIOTCPU)/lpc_common
|
||||
DIRS = $(RIOTCPU)/arm_common $(RIOTCPU)/lpc_common periph
|
||||
|
||||
ifneq (,$(filter gpioint,$(USEMODULE)))
|
||||
DIRS += gpioint
|
||||
endif
|
||||
|
@ -3,4 +3,4 @@ INCLUDES += -I$(RIOTCPU)/lpc2387/include
|
||||
include $(RIOTCPU)/arm_common/Makefile.include
|
||||
include $(RIOTCPU)/lpc_common/Makefile.include
|
||||
|
||||
export USEMODULE += arm_common lpc_common
|
||||
export USEMODULE += arm_common lpc_common periph
|
||||
|
3
cpu/lpc2387/periph/Makefile
Normal file
3
cpu/lpc2387/periph/Makefile
Normal file
@ -0,0 +1,3 @@
|
||||
MODULE = periph
|
||||
|
||||
include $(RIOTBASE)/Makefile.base
|
149
cpu/lpc2387/periph/pwm.c
Normal file
149
cpu/lpc2387/periph/pwm.c
Normal file
@ -0,0 +1,149 @@
|
||||
/*
|
||||
* Copyright (C) 2014 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 lpc2387
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief CPU specific low-level PWM driver implementation for the LPC2387
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include "bitarithm.h"
|
||||
#include "lpc2387.h"
|
||||
#include "periph/pwm.h"
|
||||
#include "periph_conf.h"
|
||||
|
||||
|
||||
#define PCPWM1 BIT6
|
||||
|
||||
/**
|
||||
* @note The PWM is always initialized with left-aligned mode.
|
||||
*
|
||||
* TODO: add center and right aligned modes
|
||||
*/
|
||||
int pwm_init(pwm_t dev, pwm_mode_t mode, unsigned int frequency, unsigned int resolution)
|
||||
{
|
||||
switch (dev) {
|
||||
#if PWM_0_EN
|
||||
case PWM_0:
|
||||
/* select function PWM[3] for pins */
|
||||
PWM_0_PORT &= ~((3 << PWM_0_CH0_PIN * 2) |
|
||||
(3 << PWM_0_CH1_PIN * 2) |
|
||||
(3 << PWM_0_CH2_PIN * 2));
|
||||
PWM_0_PORT |= (PWM_0_FUNC << PWM_0_CH0_PIN * 2) |
|
||||
(PWM_0_FUNC << PWM_0_CH1_PIN * 2) |
|
||||
(PWM_0_FUNC << PWM_0_CH2_PIN * 2);
|
||||
|
||||
/* power on PWM1 */
|
||||
PCONP |= BIT6;
|
||||
|
||||
/* select PWM1 clock */
|
||||
PCLKSEL0 &= ~(BIT13);
|
||||
PCLKSEL0 |= (BIT12);
|
||||
|
||||
/* reset PWM1s counter */
|
||||
PWM1TCR = BIT1;
|
||||
|
||||
/* set prescaler */
|
||||
PWM1PR = (F_CPU / (frequency * resolution)) - 1;
|
||||
|
||||
/* set match register */
|
||||
PWM1MR0 = resolution;
|
||||
PWM_0_CH0_MR = 0;
|
||||
PWM_0_CH1_MR = 0;
|
||||
PWM_0_CH2_MR = 0;
|
||||
|
||||
/* reset timer counter on MR0 match */
|
||||
PWM1MCR = BIT1;
|
||||
|
||||
/* enable PWM1 channel 3, 4 and 5 */
|
||||
PWM1PCR = (1 << (8 + PWM_0_CH0)) | (1 << (8 + PWM_0_CH1)) | (1 << (8 + PWM_0_CH2));
|
||||
|
||||
/* enable PWM1 timer in PWM mode */
|
||||
PWM1TCR = BIT0 + BIT3;
|
||||
|
||||
/* update match registers */
|
||||
PWM1LER = BIT0 | (1 << PWM_0_CH0) | (1 << PWM_0_CH1) | (1 << PWM_0_CH2);
|
||||
break;
|
||||
#endif
|
||||
case PWM_UNDEFINED:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pwm_set(pwm_t dev, int channel, unsigned int value)
|
||||
{
|
||||
switch (dev) {
|
||||
#if PWM_0_EN
|
||||
case PWM_0:
|
||||
switch (channel) {
|
||||
case 0:
|
||||
PWM_0_CH0_MR = value;
|
||||
PWM1LER |= (1 << PWM_0_CH0);
|
||||
break;
|
||||
case 1:
|
||||
PWM_0_CH1_MR = value;
|
||||
PWM1LER |= (1 << PWM_0_CH1);
|
||||
break;
|
||||
case 2:
|
||||
PWM_0_CH2_MR = value;
|
||||
PWM1LER |= (1 << PWM_0_CH2);
|
||||
break;
|
||||
default:
|
||||
return -2;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
case PWM_UNDEFINED:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pwm_start(pwm_t dev)
|
||||
{
|
||||
switch (dev) {
|
||||
#if PWM_0_EN
|
||||
case PWM_0:
|
||||
PCONP |= PCPWM1; /* enable PWM1 device */
|
||||
break;
|
||||
#endif
|
||||
case PWM_UNDEFINED:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pwm_stop(pwm_t dev)
|
||||
{
|
||||
switch (dev) {
|
||||
#if PWM_0_EN
|
||||
case PWM_0:
|
||||
PCONP &= ~PCPWM1; /* disable PWM1 device */
|
||||
break;
|
||||
#endif
|
||||
case PWM_UNDEFINED:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
*
|
||||
* This file is subject to the terms and conditions of the LGPLv2 License.
|
||||
* See the file LICENSE in the top level directory for more details.
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
@ -10,7 +11,7 @@
|
||||
* @brief Low-level PWM peripheral driver
|
||||
* @{
|
||||
*
|
||||
* @file pwm.h
|
||||
* @file
|
||||
* @brief Low-level PWM peripheral driver interface definitions
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
@ -52,7 +53,6 @@ typedef enum {
|
||||
PWM_CENTER /*< use center aligned PWM */
|
||||
} pwm_mode_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Initialize a PWM device
|
||||
*
|
||||
@ -65,12 +65,14 @@ typedef enum {
|
||||
* frequency if needed. To verify the correct settings compare the returned value which
|
||||
* is the actually set frequency.
|
||||
*
|
||||
* @param dev PWM channel to initialize
|
||||
* @param mode PWM mode, left, right or center aligned
|
||||
* @param frequency the PWM frequency in Hz
|
||||
* @param resolution the PWM resolution
|
||||
* @return 0 on success, -1 if requested mode and/or frequency and resolution
|
||||
* not applicable
|
||||
* @param[in] dev PWM channel to initialize
|
||||
* @param[in] mode PWM mode, left, right or center aligned
|
||||
* @param[in] frequency the PWM frequency in Hz
|
||||
* @param[in] resolution the PWM resolution
|
||||
*
|
||||
* @return 0 on success
|
||||
* @return -1 on invalid device
|
||||
* @return -2 on requested mode and/or frequency and resolution not applicable
|
||||
*/
|
||||
int pwm_init(pwm_t dev, pwm_mode_t mode, unsigned int frequency, unsigned int resolution);
|
||||
|
||||
@ -80,27 +82,35 @@ int pwm_init(pwm_t dev, pwm_mode_t mode, unsigned int frequency, unsigned int re
|
||||
* The duty-cycle is set in relation to the chosen resolution of the given device. If
|
||||
* value > resolution, value is set to resolution.
|
||||
*
|
||||
* @param dev the PWM device to set
|
||||
* @param channel the channel of the given device to set
|
||||
* @param value the desired duty-cycle to set
|
||||
* @return 0 on success, -1 on invalid device or channel
|
||||
* @param[in] dev the PWM device to set
|
||||
* @param[in] channel the channel of the given device to set
|
||||
* @param[in] value the desired duty-cycle to set
|
||||
*
|
||||
* @return 0 on success
|
||||
* @return -1 on invalid device
|
||||
* @return -2 on invalid channel
|
||||
*/
|
||||
int pwm_set(pwm_t dev, int channel, unsigned int value);
|
||||
|
||||
/**
|
||||
* @brief Start PWM generation on the given device
|
||||
*
|
||||
* @param dev device to start
|
||||
* @param[in] dev device to start
|
||||
*
|
||||
* @return 0 on success
|
||||
* @return -1 if invalid device given
|
||||
*/
|
||||
void pwm_start(pwm_t dev);
|
||||
int pwm_start(pwm_t dev);
|
||||
|
||||
/**
|
||||
* @brief Stop PWM generation on the given device
|
||||
*
|
||||
* @param dev device to stop
|
||||
* @param[in] dev device to stop
|
||||
*
|
||||
* @return 0 on success
|
||||
* @return -1 if invalid device given
|
||||
*/
|
||||
void pwm_stop(pwm_t dev);
|
||||
|
||||
int pwm_stop(pwm_t dev);
|
||||
|
||||
#endif /* __PWM_H */
|
||||
/** @} */
|
||||
|
Loading…
Reference in New Issue
Block a user