<p>
各位安:</p><p>
使用AM335X芯片做ecap捕获,用到了全部三个ecap通道,ecap0用于时钟频率捕获,其中ecap1通道和ecap2通道用来捕获45HZ~55HZ方波,ecap1配置为APWM模式时,ecap2可以正常捕获到正确的周期,反之 ecap2设置为APWM模式时,ecap1也可以正常捕获到正确的周期。同时配置为ECAP_CAPTURE_MODE就都不能正常工作,求赐教。</p>
<br><p>
代码如下:</p><pre style="max-width: 100%;"><code class="cpp hljs" codemark="1"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">freq_init</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span>
</span>{
<span class="hljs-keyword">void</span> __iomem *p_REGS;
<span class="hljs-comment">/**************************配置时钟和设备地址映射****************************/</span>
p_REGS = ioremap(SOC_PWMSS1_REGS, SZ_8K);
<span class="hljs-keyword">if</span>(!p_REGS)
{
printk(<span class="hljs-string">"%s:ioremap(%x) error "</span>, __func__, SOC_PWMSS1_REGS);
<span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
}
g_SOC_PWMSS1_REGS = (<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span>)p_REGS;
g_SOC_ECAP_1_REGS = g_SOC_PWMSS1_REGS + SOC_ECAP_REGS;
g_aREGS[<span class="hljs-number">0</span>] = g_SOC_ECAP_1_REGS;
p_REGS = ioremap(SOC_PWMSS2_REGS, SZ_8K);
<span class="hljs-keyword">if</span>(!p_REGS)
{
printk(<span class="hljs-string">"%s:ioremap(%x) error "</span>, __func__, SOC_PWMSS2_REGS);
iounmap((<span class="hljs-keyword">void</span> __iomem *)g_SOC_CONTROL_REGS);
iounmap((<span class="hljs-keyword">void</span> __iomem *)g_SOC_PWMSS1_REGS);
<span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
}
g_SOC_PWMSS2_REGS = (<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span>)p_REGS;
g_SOC_ECAP_2_REGS = g_SOC_PWMSS2_REGS + SOC_ECAP_REGS;
g_aREGS[<span class="hljs-number">1</span>] = g_SOC_ECAP_2_REGS;
<span class="hljs-comment">/* Enable Clock */</span>
ECAPClockEnable(g_SOC_PWMSS1_REGS);
<span class="hljs-keyword">while</span>(ECAPClockEnableStatusGet(g_SOC_PWMSS1_REGS) == <span class="hljs-number">0</span>);
ECAPClockEnable(g_SOC_PWMSS2_REGS);
<span class="hljs-keyword">while</span>(ECAPClockEnableStatusGet(g_SOC_PWMSS2_REGS) == <span class="hljs-number">0</span>);
<span class="hljs-comment">/***************************配置两个ecap的硬件管脚*****************************/</span>
p_REGS = ioremap(SOC_CONTROL_REGS, SZ_8K);
<span class="hljs-keyword">if</span>(!p_REGS)
{
printk(<span class="hljs-string">"%s:ioremap(%x) error "</span>, __func__, SOC_CONTROL_REGS);
<span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
}
g_SOC_CONTROL_REGS = (<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span>)p_REGS;
<span class="hljs-comment">/* eCAP1输入管脚 AM335X_ECAP1_IN I2C0_SCL C16 */</span>
HWREG(g_SOC_CONTROL_REGS + CONTROL_CONF_I2C0_SCL) =
(CONTROL_CONF_PULLUPSEL |CONTROL_CONF_RXACTIVE
| CONTROL_CONF_MUXMODE(<span class="hljs-number">3</span>));
<span class="hljs-comment">/* eCAP2输入管脚 eCAP2_in_PWM2_out模式 MCASP0_AHCLKR C12 */</span>
HWREG(g_SOC_CONTROL_REGS + CONTROL_CONF_MCASP0_AHCLKR) =
(CONTROL_CONF_PULLUPSEL |CONTROL_CONF_RXACTIVE
| CONTROL_CONF_MUXMODE(<span class="hljs-number">4</span>));
<span class="hljs-comment">/****************************配置两个ecap的寄存器******************************/</span>
<span class="hljs-comment">/* 捕捉模式
** ECCTL2[9] CAP_APWM CAP/APWM operating mode select
** 0h = ECAP module operates in capture mode. This mode forces the
** following configuration. (a) Inhibits TSCTR resets via PRDEQ event.
** (b) Inhibits shadow loads on CAP1 and 2 registers. (c) Permits user
** to enable CAP1-4 register load. (d) ECAPn/APWMn pin operates as
** a capture input.
** 1h = ECAP module operates in APWM mode. This mode forces the
** following configuration. (a) Resets TSCTR on PRDEQ event (period
** boundary). (b) Permits shadow loading on CAP1 and 2 registers. (c)
** Disables loading of time-stamps into CAP1-4 registers. (d)
** ECAPn/APWMn pin operates as a APWM output.
*/</span>
ECAPOperatingModeSelect(g_SOC_ECAP_1_REGS, ECAP_CAPTURE_MODE);
ECAPOperatingModeSelect(g_SOC_ECAP_2_REGS, ECAP_APWM_MODE); <span class="hljs-comment">/* 问题就在此处 ecap2设备APWM模式时 ecap1才能正常捕获,反之亦然 */</span>
<span class="hljs-comment">/* 连续模式
** ECCTL2[0] CONT_ONESHT 0h = Operate in continuous mode
*/</span>
ECAPContinousModeConfig(g_SOC_ECAP_1_REGS);
ECAPContinousModeConfig(g_SOC_ECAP_2_REGS);
<span class="hljs-comment">/* 清除中断
** ECCLR[0:7] EC**&0xFF Writing a 1 clears the Int flag condition
*/</span>
ECAPIntStatusClear(g_SOC_ECAP_1_REGS, <span class="hljs-number">0xFF</span>);
ECAPIntStatusClear(g_SOC_ECAP_2_REGS, <span class="hljs-number">0xFF</span>);
<span class="hljs-comment">/* 这里的分频是指输入信号的分频,不是ECAP的时钟分频
** ECCTL1[9:13] PRESCALE 0h = Divide by 1 (i.e,. no prescale, by-pass the prescaler)
*/</span>
ECAPPrescaleConfig(g_SOC_ECAP_1_REGS, <span class="hljs-number">0</span>);
ECAPPrescaleConfig(g_SOC_ECAP_2_REGS, <span class="hljs-number">0</span>);
<span class="hljs-comment">/* enables capture loading
** ECCTL1[8] CAPLDEN 1h = Enable CAP1-4 register loads at capture event time.
*/</span>
ECAPCaptureLoadingEnable(g_SOC_ECAP_1_REGS);
ECAPCaptureLoadingEnable(g_SOC_ECAP_2_REGS);
<span class="hljs-comment">/* configures Capture Event polarity.上升沿捕捉 */</span>
ECAPCapeEvtPolarityConfig(g_SOC_ECAP_1_REGS, EC_RISING_EDGE, EC_RISING_EDGE, EC_RISING_EDGE, EC_RISING_EDGE);
ECAPCapeEvtPolarityConfig(g_SOC_ECAP_2_REGS, EC_RISING_EDGE, EC_RISING_EDGE, EC_RISING_EDGE, EC_RISING_EDGE);
<span class="hljs-comment">/* enables reset of the counters upon Capture Events. */</span>
ECAPCaptureEvtCntrRstConfig(g_SOC_ECAP_1_REGS, EC_DELTA_MODE, EC_DELTA_MODE, EC_DELTA_MODE, EC_DELTA_MODE);
ECAPCaptureEvtCntrRstConfig(g_SOC_ECAP_2_REGS, EC_DELTA_MODE, EC_DELTA_MODE, EC_DELTA_MODE, EC_DELTA_MODE);
<span class="hljs-comment">/* 0h = Stop after Capture Event 1 in one-shot mode. Wrap after
Capture Event 1 in continuous mode
0->0->0 CAP1 */</span>
<span class="hljs-comment">//ECAPStopWrapConfig(g_SOC_ECAP_1_REGS, ECAP_CAPTURE_EVENT4_STOP);</span>
<span class="hljs-comment">//ECAPStopWrapConfig(g_SOC_ECAP_2_REGS, ECAP_CAPTURE_EVENT1_STOP);</span>
<span class="hljs-comment">/* 使能中断 */</span>
ECAPIntEnable(g_SOC_ECAP_1_REGS, ECAP_CEVT1_INT|ECAP_CEVT2_INT|ECAP_CEVT3_INT|ECAP_CEVT4_INT);
ECAPIntEnable(g_SOC_ECAP_2_REGS, ECAP_CEVT1_INT|ECAP_CEVT2_INT|ECAP_CEVT3_INT|ECAP_CEVT4_INT);
<span class="hljs-comment">/* Disables the sync out ,sync in. TI定义的宏ECAP_SYNC_OUT_DISABLE有问题,改用EC_SYNC_OUT_DISABLE
** ECCTL2[5] SYNCI_EN 0h = Disable sync-in option
** ECCTL2[6:7] SYNCO_SEL 2h = Disable sync out signal
*/</span>
ECAPSyncInOutSelect(g_SOC_ECAP_1_REGS, ECAP_SYNC_IN_DISABLE, EC_SYNC_OUT_DISABLE);
ECAPSyncInOutSelect(g_SOC_ECAP_2_REGS, ECAP_SYNC_IN_DISABLE, EC_SYNC_OUT_DISABLE);
<span class="hljs-comment">/* Set Counter
** RSCTR[0:31] 0 Active 32 bit counter register that is used as the capture time-base
*/</span>
ECAPCounterConfig(g_SOC_ECAP_1_REGS, <span class="hljs-number">0</span>);
ECAPCounterConfig(g_SOC_ECAP_2_REGS, <span class="hljs-number">0</span>);
<span class="hljs-comment">/* 自由运行 ECCTL2[4] 1 TSCTRSTOP->TSCTR free-running 计数器停止位控制 计数器开始计数 */</span>
ECAPCounterControl(g_SOC_ECAP_1_REGS, ECAP_COUNTER_FREE_RUNNING);
ECAPCounterControl(g_SOC_ECAP_2_REGS, ECAP_COUNTER_FREE_RUNNING);
rt_request_irq(CFG_ECAP1INT, CFG_INT_LEVEL_ECAP, freq_isr0, <span class="hljs-string">"ecap1"</span>, <span class="hljs-number">0</span>);
rt_request_irq(CFG_ECAP2INT, CFG_INT_LEVEL_ECAP, freq_isr1, <span class="hljs-string">"ecap2"</span>, <span class="hljs-number">0</span>);
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">freq_isr</span><span class="hljs-params">(<span class="hljs-keyword">int</span> index)</span>
</span>{
<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> status, count = <span class="hljs-number">0</span>;
<span class="hljs-keyword">float</span> freq;
<span class="hljs-comment">/* 读取中断状态值 */</span>
status = ECAPIntStatus(g_aREGS[index], <span class="hljs-number">0x3F</span>);
<span class="hljs-keyword">if</span>(status & ECAP_CEVT1_INT)
{
count = ECAPTimeStampRead(g_aREGS[index], ECAP_CAPTURE_EVENT_1);
}
<span class="hljs-keyword">if</span>(status & ECAP_CEVT2_INT)
{
count = ECAPTimeStampRead(g_aREGS[index], ECAP_CAPTURE_EVENT_2);
}
<span class="hljs-keyword">if</span>(status & ECAP_CEVT3_INT)
{
count = ECAPTimeStampRead(g_aREGS[index], ECAP_CAPTURE_EVENT_3);
}
<span class="hljs-keyword">if</span>(status & ECAP_CEVT4_INT)
{
count = ECAPTimeStampRead(g_aREGS[index], ECAP_CAPTURE_EVENT_4);
}
<span class="hljs-comment">/* 清中断标志位 */</span>
ECAPIntStatusClear(g_aREGS[index], <span class="hljs-number">0x3F</span>);
<span class="hljs-comment">/* 换算成频率.不能除0 */</span>
<span class="hljs-keyword">if</span>(count)
{
freq = (<span class="hljs-keyword">float</span>)ECAP_CLK_FREQ/count;
}
<span class="hljs-keyword">else</span>
{
freq = <span class="hljs-number">0</span>;
}
<span class="hljs-comment">//将频率范围限定在45hz到55hz。</span>
<span class="hljs-keyword">if</span>(freq>=<span class="hljs-number">44.0</span> && freq <= <span class="hljs-number">56.0</span>)
{
g_freqency[index] = freq;
}
}
</code></pre><p><br></p>
一周热门 更多>