2014-02-20 15:53:05 +01:00
|
|
|
/*
|
2016-12-13 16:44:36 +01:00
|
|
|
* Copyright (C) 2014-2016 Freie Universität Berlin
|
2014-02-20 15:53:05 +01:00
|
|
|
*
|
2016-12-13 16:44:36 +01:00
|
|
|
* 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.
|
2014-02-20 15:53:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-07-16 16:25:45 +02:00
|
|
|
* @ingroup cpu_lpc23xx
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup drivers_periph_pwm
|
2014-02-20 15:53:05 +01:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
2020-07-16 16:25:45 +02:00
|
|
|
* @brief CPU specific low-level PWM driver implementation for the LPC23XX family
|
2014-02-20 15:53:05 +01:00
|
|
|
*
|
2016-12-13 16:44:36 +01:00
|
|
|
* The current implementation is somewhat limited: it supports only a single
|
|
|
|
* hardware PWM device (fixed to PWM1) and it is fixed on supporting 3 channels.
|
|
|
|
*
|
2014-02-20 15:53:05 +01:00
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2016-12-13 16:44:36 +01:00
|
|
|
#include "cpu.h"
|
|
|
|
#include "assert.h"
|
2014-02-20 15:53:05 +01:00
|
|
|
#include "bitarithm.h"
|
2016-12-13 16:44:36 +01:00
|
|
|
#include "periph/pwm.h"
|
2014-02-20 15:53:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @note The PWM is always initialized with left-aligned mode.
|
|
|
|
*
|
|
|
|
* TODO: add center and right aligned modes
|
|
|
|
*/
|
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)
|
2014-02-20 15:53:05 +01:00
|
|
|
{
|
2016-12-13 16:44:36 +01:00
|
|
|
if ((dev != PWM_DEV(0)) && (mode != PWM_LEFT)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-12 07:56:22 +02:00
|
|
|
|
2016-12-13 16:44:36 +01:00
|
|
|
/* select function PWM[3] for pins */
|
|
|
|
PWM_PORT &= ~((3 << PWM_CH0_PIN * 2) |
|
|
|
|
(3 << PWM_CH1_PIN * 2) |
|
|
|
|
(3 << PWM_CH2_PIN * 2));
|
|
|
|
PWM_PORT |= (PWM_FUNC << PWM_CH0_PIN * 2) |
|
|
|
|
(PWM_FUNC << PWM_CH1_PIN * 2) |
|
|
|
|
(PWM_FUNC << PWM_CH2_PIN * 2);
|
2014-02-20 15:53:05 +01:00
|
|
|
|
2016-12-13 16:44:36 +01:00
|
|
|
/* power on PWM1 */
|
2017-02-07 15:05:43 +01:00
|
|
|
PCONP |= PCPWM1;
|
2014-02-20 15:53:05 +01:00
|
|
|
|
2016-12-13 16:44:36 +01:00
|
|
|
/* select PWM1 clock */
|
|
|
|
PCLKSEL0 &= ~(BIT13);
|
|
|
|
PCLKSEL0 |= (BIT12);
|
2014-02-20 15:53:05 +01:00
|
|
|
|
2016-12-13 16:44:36 +01:00
|
|
|
/* reset PWM1s counter */
|
|
|
|
PWM1TCR = BIT1;
|
2014-02-20 15:53:05 +01:00
|
|
|
|
2016-12-13 16:44:36 +01:00
|
|
|
/* set prescaler */
|
|
|
|
PWM1PR = (CLOCK_CORECLOCK / (freq * res)) - 1;
|
2014-02-20 15:53:05 +01:00
|
|
|
|
2016-12-13 16:44:36 +01:00
|
|
|
/* set match register */
|
|
|
|
PWM1MR0 = res;
|
|
|
|
PWM_CH0_MR = 0;
|
|
|
|
PWM_CH1_MR = 0;
|
|
|
|
PWM_CH2_MR = 0;
|
2014-02-20 15:53:05 +01:00
|
|
|
|
2016-12-13 16:44:36 +01:00
|
|
|
/* reset timer counter on MR0 match */
|
|
|
|
PWM1MCR = BIT1;
|
2014-02-20 15:53:05 +01:00
|
|
|
|
2016-12-13 16:44:36 +01:00
|
|
|
/* enable PWM1 channel 3, 4 and 5 */
|
|
|
|
PWM1PCR = (1 << (8 + PWM_CH0)) | (1 << (8 + PWM_CH1)) | (1 << (8 + PWM_CH2));
|
2014-02-20 15:53:05 +01:00
|
|
|
|
2016-12-13 16:44:36 +01:00
|
|
|
/* enable PWM1 timer in PWM mode */
|
|
|
|
PWM1TCR = BIT0 + BIT3;
|
2014-02-20 15:53:05 +01:00
|
|
|
|
2016-12-13 16:44:36 +01:00
|
|
|
/* update match registers */
|
|
|
|
PWM1LER = BIT0 | (1 << PWM_CH0) | (1 << PWM_CH1) | (1 << PWM_CH2);
|
2014-02-20 15:53:05 +01:00
|
|
|
|
2015-10-21 12:42:52 +02:00
|
|
|
return freq;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t pwm_channels(pwm_t dev)
|
|
|
|
{
|
build: fix unused parameter errors
cpu, sam0_common: fix unused parameter in periph/spi
cpu, kinetis_common: fix unused parameter in periph/spi
cpu, cc2538: fix unused param in periph/i2c
cpu, cc2538: fix unused param in periph/spi
cpu, sam3: fix unused param in periph/spi
cpu, stm32_common: fix unused param in periph/pm
cpu, stm32f3: fix unused params in periph/i2c
cpu, nrf5x_common: fix unused param in periph/gpio
cpu, nrf5x_common: fix unused param in periph/spi
cpu, lpc2387: fix unused params in periph/spi
cpu, cc2538: fix unused params in radio/netdev
cpu, cc2650: fix unused params in periph/uart
cpu, lm4f120: fix unused param in periph/spi
cpu, lm4f120: fix unused params in periph/timer
cpu, lm4f120: fix unused params in periph/uart
cpu, stm32_common: fix unused params in periph/dac
cpu, stm32l0: fix unused params in periph/i2c
cpu, msp430fxyz: fix unused params in periph/uart
cpu, mips: fix unused params
cpu, cc430: fix unused-params in periph/timer
cpu, msp430fxyz: fix unused params in periph/spi
drivers, cc2420: fix unused param
cpu, mips32r2_common: fix unused params in periph/timer
cpu, cc2538: fix unused-param in periph/i2c
cpu, mips32r2_common: fix unused-param in periph/timer
cpu, msp430fxyz: fix unused params in periph/timer
cpu, atmega_common: fix unused params in periph/spi
driver, nrfmin: fix unused params
cpu, cc2538_rf: fix unused params
driver, netdev_ieee802514: fix unused param
cpu, mip_pic32m: fix unused params
cpu, lpc2387: fix unused params in periph/pwm
tests/driver_sdcard_spi: fix unused params
cpu, sam3: fix unused param in periph/pwm
tests/driver_dynamixel: fix unused params, and style issues
cpu, cc430: fix unused param in periph/rtc
cpu, atmega_common: fix unused params in periph/i2c
2017-10-31 12:09:11 +01:00
|
|
|
(void)dev;
|
2016-12-13 16:44:36 +01:00
|
|
|
assert(dev == PWM_DEV(0));
|
|
|
|
return PWM_CHANNELS;
|
2014-02-20 15:53:05 +01:00
|
|
|
}
|
|
|
|
|
2015-10-21 12:42:52 +02:00
|
|
|
void pwm_set(pwm_t dev, uint8_t channel, uint16_t value)
|
2014-02-20 15:53:05 +01:00
|
|
|
{
|
build: fix unused parameter errors
cpu, sam0_common: fix unused parameter in periph/spi
cpu, kinetis_common: fix unused parameter in periph/spi
cpu, cc2538: fix unused param in periph/i2c
cpu, cc2538: fix unused param in periph/spi
cpu, sam3: fix unused param in periph/spi
cpu, stm32_common: fix unused param in periph/pm
cpu, stm32f3: fix unused params in periph/i2c
cpu, nrf5x_common: fix unused param in periph/gpio
cpu, nrf5x_common: fix unused param in periph/spi
cpu, lpc2387: fix unused params in periph/spi
cpu, cc2538: fix unused params in radio/netdev
cpu, cc2650: fix unused params in periph/uart
cpu, lm4f120: fix unused param in periph/spi
cpu, lm4f120: fix unused params in periph/timer
cpu, lm4f120: fix unused params in periph/uart
cpu, stm32_common: fix unused params in periph/dac
cpu, stm32l0: fix unused params in periph/i2c
cpu, msp430fxyz: fix unused params in periph/uart
cpu, mips: fix unused params
cpu, cc430: fix unused-params in periph/timer
cpu, msp430fxyz: fix unused params in periph/spi
drivers, cc2420: fix unused param
cpu, mips32r2_common: fix unused params in periph/timer
cpu, cc2538: fix unused-param in periph/i2c
cpu, mips32r2_common: fix unused-param in periph/timer
cpu, msp430fxyz: fix unused params in periph/timer
cpu, atmega_common: fix unused params in periph/spi
driver, nrfmin: fix unused params
cpu, cc2538_rf: fix unused params
driver, netdev_ieee802514: fix unused param
cpu, mip_pic32m: fix unused params
cpu, lpc2387: fix unused params in periph/pwm
tests/driver_sdcard_spi: fix unused params
cpu, sam3: fix unused param in periph/pwm
tests/driver_dynamixel: fix unused params, and style issues
cpu, cc430: fix unused param in periph/rtc
cpu, atmega_common: fix unused params in periph/i2c
2017-10-31 12:09:11 +01:00
|
|
|
(void)dev;
|
2016-12-13 16:44:36 +01:00
|
|
|
assert((dev == PWM_DEV(0)) && (channel < 3));
|
|
|
|
|
|
|
|
switch (channel) {
|
|
|
|
case 0:
|
|
|
|
PWM_CH0_MR = value;
|
|
|
|
PWM1LER |= (1 << PWM_CH0);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
PWM_CH1_MR = value;
|
|
|
|
PWM1LER |= (1 << PWM_CH1);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
PWM_CH2_MR = value;
|
|
|
|
PWM1LER |= (1 << PWM_CH2);
|
2014-02-20 15:53:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 16:08:03 +02:00
|
|
|
void pwm_poweron(pwm_t dev)
|
2014-02-20 15:53:05 +01:00
|
|
|
{
|
build: fix unused parameter errors
cpu, sam0_common: fix unused parameter in periph/spi
cpu, kinetis_common: fix unused parameter in periph/spi
cpu, cc2538: fix unused param in periph/i2c
cpu, cc2538: fix unused param in periph/spi
cpu, sam3: fix unused param in periph/spi
cpu, stm32_common: fix unused param in periph/pm
cpu, stm32f3: fix unused params in periph/i2c
cpu, nrf5x_common: fix unused param in periph/gpio
cpu, nrf5x_common: fix unused param in periph/spi
cpu, lpc2387: fix unused params in periph/spi
cpu, cc2538: fix unused params in radio/netdev
cpu, cc2650: fix unused params in periph/uart
cpu, lm4f120: fix unused param in periph/spi
cpu, lm4f120: fix unused params in periph/timer
cpu, lm4f120: fix unused params in periph/uart
cpu, stm32_common: fix unused params in periph/dac
cpu, stm32l0: fix unused params in periph/i2c
cpu, msp430fxyz: fix unused params in periph/uart
cpu, mips: fix unused params
cpu, cc430: fix unused-params in periph/timer
cpu, msp430fxyz: fix unused params in periph/spi
drivers, cc2420: fix unused param
cpu, mips32r2_common: fix unused params in periph/timer
cpu, cc2538: fix unused-param in periph/i2c
cpu, mips32r2_common: fix unused-param in periph/timer
cpu, msp430fxyz: fix unused params in periph/timer
cpu, atmega_common: fix unused params in periph/spi
driver, nrfmin: fix unused params
cpu, cc2538_rf: fix unused params
driver, netdev_ieee802514: fix unused param
cpu, mip_pic32m: fix unused params
cpu, lpc2387: fix unused params in periph/pwm
tests/driver_sdcard_spi: fix unused params
cpu, sam3: fix unused param in periph/pwm
tests/driver_dynamixel: fix unused params, and style issues
cpu, cc430: fix unused param in periph/rtc
cpu, atmega_common: fix unused params in periph/i2c
2017-10-31 12:09:11 +01:00
|
|
|
(void)dev;
|
2016-12-13 16:44:36 +01:00
|
|
|
assert(dev == PWM_DEV(0));
|
|
|
|
PCONP |= PCPWM1;
|
2017-02-07 15:05:43 +01:00
|
|
|
PWM1TCR |= BIT0;
|
2014-07-24 16:08:03 +02:00
|
|
|
}
|
2014-02-20 15:53:05 +01:00
|
|
|
|
2014-07-24 16:08:03 +02:00
|
|
|
void pwm_poweroff(pwm_t dev)
|
|
|
|
{
|
build: fix unused parameter errors
cpu, sam0_common: fix unused parameter in periph/spi
cpu, kinetis_common: fix unused parameter in periph/spi
cpu, cc2538: fix unused param in periph/i2c
cpu, cc2538: fix unused param in periph/spi
cpu, sam3: fix unused param in periph/spi
cpu, stm32_common: fix unused param in periph/pm
cpu, stm32f3: fix unused params in periph/i2c
cpu, nrf5x_common: fix unused param in periph/gpio
cpu, nrf5x_common: fix unused param in periph/spi
cpu, lpc2387: fix unused params in periph/spi
cpu, cc2538: fix unused params in radio/netdev
cpu, cc2650: fix unused params in periph/uart
cpu, lm4f120: fix unused param in periph/spi
cpu, lm4f120: fix unused params in periph/timer
cpu, lm4f120: fix unused params in periph/uart
cpu, stm32_common: fix unused params in periph/dac
cpu, stm32l0: fix unused params in periph/i2c
cpu, msp430fxyz: fix unused params in periph/uart
cpu, mips: fix unused params
cpu, cc430: fix unused-params in periph/timer
cpu, msp430fxyz: fix unused params in periph/spi
drivers, cc2420: fix unused param
cpu, mips32r2_common: fix unused params in periph/timer
cpu, cc2538: fix unused-param in periph/i2c
cpu, mips32r2_common: fix unused-param in periph/timer
cpu, msp430fxyz: fix unused params in periph/timer
cpu, atmega_common: fix unused params in periph/spi
driver, nrfmin: fix unused params
cpu, cc2538_rf: fix unused params
driver, netdev_ieee802514: fix unused param
cpu, mip_pic32m: fix unused params
cpu, lpc2387: fix unused params in periph/pwm
tests/driver_sdcard_spi: fix unused params
cpu, sam3: fix unused param in periph/pwm
tests/driver_dynamixel: fix unused params, and style issues
cpu, cc430: fix unused param in periph/rtc
cpu, atmega_common: fix unused params in periph/i2c
2017-10-31 12:09:11 +01:00
|
|
|
(void)dev;
|
2016-12-13 16:44:36 +01:00
|
|
|
assert(dev == PWM_DEV(0));
|
2017-02-07 15:05:43 +01:00
|
|
|
PWM1TCR &= ~(BIT0);
|
2016-12-13 16:44:36 +01:00
|
|
|
PCONP &= ~(PCPWM1);
|
2014-02-20 15:53:05 +01:00
|
|
|
}
|