Step 1: 在之前使能LCD时钟时,加入下面这句:
uint32_t *const clk_modules_explicit_en[] = {
&cmper->lcdclkctrl,
&cmper->lcdcclkstctrl,
&cmper->epwmss0clkctrl,
0,
};
Step 2: 定义pinmux:
static struct module_pin_mux pwm0_pin_mux[] = {
{OFFSET(ecap0_in_pwm0_out), MODE(0)},
{-1},
};
Step 3: 编写enable_pwm()函数:
static int enable_pwm(void)
{
struct pwmss_regs *pwmss = (struct pwmss_regs *)PWMSS0_BASE;
struct pwmss_ecap_regs *ecap = (struct pwmss_ecap_regs *)AM33XX_ECAP0_BASE;
setbits_le32(&pwmss->clkconfig, ECAP_CLK_EN);
writel(1000 - 1, &ecap->cap3);
setbits_le16(&ecap->ecctl2, ECTRL2_MDSL_ECAP | ECTRL2_SYNCOSEL_MASK);
writel(300, &ecap->cap2);
setbits_le16(&ecap->ecctl2, ECTRL2_CTRSTP_FREERUN);
return 0;
}
=====================================
上面的代码是借鉴board/siemens/pxm2板的,当执行完它之后,ecap0_in_pwm0_out就会输出周期为10us占空比为30%的脉冲波啦。
但是为了搞清楚PWM是怎么工作的,还是得理解AM335x的技术手册:
Chapter 15: Pulse-Width Modulation Subsystem (PWMSS)
15.1 Pulse-Width Modulation Subsystem (PWMSS)
15.1.1 Introduction
15.1.1.1 Features
15.1.1.2 Unsupported Features
15.1.2 Integration
15.1.2.1 PWMSS Connectivity Attributes
15.1.2.2 PWMSS Clock and Reset Management
15.1.2.3 PWMSS Pin list
15.1.3 PWMSS Registers
(上面的代码中,并没有使用AM335x的PWMSS功能,而是使用了eCAP功能实现PWM。)
15.2 Enhanced PWM (ePWM) Module
15.3 Enhanced Capture (eCAP) Module
15.3.1 Introduction
15.3.1.1 Purpose of the Peripheral
15.3.1.2 Features
* When not used in capture mode, the ECAP moduls can be configured as a single channel PWM output.
(因为使用eCAP实现PWM功能非常简单,因此使用它而不是ePWM来实现。)
15.3.2 Functional Descrition
15.3.2.1 Capture and APWM Operating Mode
You can use the eCAP module resources to implement a single-channel PWM generator (with 32 bit capabilities) when it is not being used for input captures. The counter operates in count-up mode, providing a time-base for asymmetrical pulse width modulation (PWM) waveforms. The CAP1 and CAP2 registers become the active period and compare registers, while CAP3 and CAP4 registers become the period and capture shadow registers, respectively.
15.3.2.2 Capture Mode Description
15.3.2.2.1 Event Prescaler
15.3.2.2.2 Edge Polarity Select and Qualifier
15.3.2.2.3 Continuous/One-Shot Control
15.3.2.2.4 32-Bit Counter and Phase Control
15.3.2.2.5 CAP1-CAP4 Registers
15.3.2.2.6 Interrupt Control
15.3.2.2.7 Shadow Load and Lockout Control
15.3.2.2.8 APWM Mode Operation
Main operating highlights of the APWM section:
* The time-stamp counter bus is make available for comparison via 2 digital(32-bit comparators).
* When CAP1/2 registers are not used in capture mode, their contents can be used as Period and Compare values in APWM mode.
* Double buffering is achieved via shadow registers APRD and ACMP (CAP3/4). The shadow register contents are transferred over to CAP1/2 registers either immediately upon a write, or on a PRDEQ trigger.
* During initialization, you must write to the active registers for both period and compare. This automatically copies the initial values into the shadow values. For subsequent compare updates, during run-time, you only need to use the shadow registers.
(这就是为什么使用一路PWM需要CAP1/2/3/4,因为CAP3/4是影子寄存器,它用做缓存,当对它进行操作之后,值会自动被装载进CAP1/2中。)
15.3.3 Use Cases
15.3.3.1 Absolute Time-Stamp Operation Rising Edge Trigger Example
15.3.3.2 Absulute Time-Stamp Operation Rising and Falling Edge Trigger Example
15.3.3.3 Time Difference (Delta) Operating Rising Edge Trigger Example
15.3.3.4 Time Difference (Delta) Operating Rising and Falling Edge Trigger Example
15.3.3.5 Application of the APWM Mode
15.3.3.5.1 Simple PWM Generation (Independent Channel/s) Example
The PWM polarity is active high, which means that the compare value (CAP2 reg is now a compare register) represents the on-time (high level) of the period. Alternatively, if the APWMPOL bit is configured for active low, then the compare value represents the off-time.
15.3.3.5.2 Multichannel PWM Generation with Synchronization Example
15.3.3.5.3 Multichannel PWM Generation with Phase Control Example
15.3.4 Registers
(pxm2的代码中还有对ecap->tsctr的操作,从15.3.3.5.3可知它用在三相控制中,因此simple PWM功能不需要它。)
文章评论(0条评论)
登录后参与讨论