英文:
Why does my hardware timer not increment the count using HAL libraries?
问题
我正在为SAML21G18B编写一个程序,使用AtmelStart和ASF4 HAL库。
我的计时器的目的是简单地计数直到溢出,此时处理程序会增加计数OVF
并重新启动计时器。我的理解是HAL库,即timer_start(timerInstance)
(请参阅API文档此处),应该启动计时器,我需要重写TC0_Handler
函数以增加我的计数OVF
。然而,timer_start
函数从不返回,而且似乎处理程序反复调用,尽管计时器计数寄存器未从零更改。似乎每次通过hri_tc_clear_interrupt_OVF_bit
清除OVF位时,都会在调试模式下的下一步中重置它。
我已经在计时器的配置中做了一些更改,具体如下:
- 我将其从AtmelStart生成的32位更改为16位,即
#define CONF_TC0_MODE TC_CTRLA_MODE_COUNT16_Val
- 计时器以16MHz(CPU速度)运行,因此计时器滴答周期小于1us,而AtmelStart配置似乎只允许整数,因此我已经覆盖为浮点数。
- 我不想使用API提供的计时器任务,因为我试图让我的代码尽可能接近传统代码。
是否有人能够指导我正确的方向?是否有一些计时器配置我遗漏了?我能找到的所有示例都使用了任务。我对发布SO问题不太熟悉,所以如果您需要更多信息,我可以提供。
编辑:涉及的代码:
int main(void)
{
/* 初始化MCU、驱动程序和中间件 */
atmel_start_init();
ext_irq_register(MCU_SWI, TurnOff_Handler);
//TODO 创建一个单独的驱动程序初始化函数。
timerInit(&Timer0, &TIMER_0, COUNT_UP, 0);
timerStart(&Timer0);
}
英文:
I'm writing a program for the SAML21G18B using AtmelStart and ASF4 HAL libraries.
My timer is intended to simply count up until it overflows, at which point a handler increments a count OVF
and restarts the timer. My understanding is that the HAL libraries i.e. timer_start(timerInstance)
(see API documentation here) should start the timer, and I need to override the TC0_Handler
function so that it increments my count OVF
. However, the timer_start
function never returns, and it appears that the handler is repeatedly called despite the timer count register not changing from zero. It appears that every time the OVF bit is cleared via hri_tc_clear_interrupt_OVF_bit
, it gets reset on the next step when stepping through in debug mode.
I have changed a couple of things in the configuration of the timer, namely:
- I have changed it to be 16 bit rather than the 32 bit that the AtmelStart generates, i.e.
#define CONF_TC0_MODE TC_CTRLA_MODE_COUNT16_Val
- The timer is running at 16MHz (CPU speed) and thus the timer tick period is <1us, where the AtmelStart configuration only seems to allow integers, so I have overridden that to be a float.
- I am not wanting to use the timer tasks provided by the API as I am trying to keep my code as close to legacy as possible.
Is someone able to point me in the right direction here? Is there some timer configuration I'm missing? All examples I can find use the tasks. I'm not too familiar with posting SO questions so if you need more information I am happy to provide.
Edit: the code in question:
int main(void)
{
/* Initializes MCU, drivers and middleware */
atmel_start_init();
ext_irq_register(MCU_SWI, TurnOff_Handler);
//TODO make this separate driver init func.
timerInit(&Timer0, &TIMER_0, COUNT_UP, 0);
timerStart(&Timer0);
}
答案1
得分: 0
我发现问题是因为计数需要读取同步。请查看SAMD21的解决方案,它与SAML21具有相同的解决方案。
英文:
I found the solution is due to the count requiring a read sync. See here for solution on the SAMD21, which has identical solution for the SAML21.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论