<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>LPC MicrocontrollersのトピックLPC Open Chip_TIMER_Reset() strange behaviour and implementation</title>
    <link>https://community.nxp.com/t5/LPC-Microcontrollers/LPC-Open-Chip-TIMER-Reset-strange-behaviour-and-implementation/m-p/1976164#M57093</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am using&amp;nbsp;lpcopen_3_02_lpcxpresso_xpresso4337.&lt;/P&gt;&lt;P&gt;When I call&amp;nbsp;Chip_TIMER_Reset(LPC_TIMER2); with no optimisation it takes 480nS but when I call it with -01 optimisation it takes 1.76uS.&lt;/P&gt;&lt;P&gt;This isobviously unexpected. The optimised assembler looks good.&amp;nbsp;&lt;/P&gt;&lt;P&gt;What it interesting is that most of the time is the while loop in&amp;nbsp;Chip_TIMER_Reset();&amp;nbsp;&lt;/P&gt;&lt;P&gt;I cannot see how this is possible.&lt;/P&gt;&lt;P&gt;I suspect the implementation of&amp;nbsp;Chip_TIMER_Reset() is incorrect. It seems overly complicated compared to the CMSIS libraries. Why is TC set to a non zero value in the code below? When it is set to zero the function is much faster.&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;LI-CODE lang="markup"&gt;/* Resets the timer terminal and prescale counts to 0 */
void Chip_TIMER_Reset(LPC_TIMER_T *pTMR)
{
	uint32_t reg;

	/* Disable timer, set terminal count to non-0 */
	reg = pTMR-&amp;gt;TCR;
	pTMR-&amp;gt;TCR = 0;
	pTMR-&amp;gt;TC = 1;

	/* Reset timer counter */
	pTMR-&amp;gt;TCR = TIMER_RESET;

	/* Wait for terminal count to clear */
	while (pTMR-&amp;gt;TC != 0) {}

	/* Restore timer state */
	pTMR-&amp;gt;TCR = reg;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But the CMSIS library code is as follows and seems to do the same thing:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;void TIM_ResetCounter(LPC_TIMERn_Type *TIMx)
{
	CHECK_PARAM(PARAM_TIMx(TIMx));
	TIMx-&amp;gt;TCR |= TIM_RESET;
	TIMx-&amp;gt;TCR &amp;amp;= ~TIM_RESET;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone explain&amp;gt;&lt;/P&gt;</description>
    <pubDate>Thu, 17 Oct 2024 14:25:53 GMT</pubDate>
    <dc:creator>RikkiB</dc:creator>
    <dc:date>2024-10-17T14:25:53Z</dc:date>
    <item>
      <title>LPC Open Chip_TIMER_Reset() strange behaviour and implementation</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/LPC-Open-Chip-TIMER-Reset-strange-behaviour-and-implementation/m-p/1976164#M57093</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am using&amp;nbsp;lpcopen_3_02_lpcxpresso_xpresso4337.&lt;/P&gt;&lt;P&gt;When I call&amp;nbsp;Chip_TIMER_Reset(LPC_TIMER2); with no optimisation it takes 480nS but when I call it with -01 optimisation it takes 1.76uS.&lt;/P&gt;&lt;P&gt;This isobviously unexpected. The optimised assembler looks good.&amp;nbsp;&lt;/P&gt;&lt;P&gt;What it interesting is that most of the time is the while loop in&amp;nbsp;Chip_TIMER_Reset();&amp;nbsp;&lt;/P&gt;&lt;P&gt;I cannot see how this is possible.&lt;/P&gt;&lt;P&gt;I suspect the implementation of&amp;nbsp;Chip_TIMER_Reset() is incorrect. It seems overly complicated compared to the CMSIS libraries. Why is TC set to a non zero value in the code below? When it is set to zero the function is much faster.&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;LI-CODE lang="markup"&gt;/* Resets the timer terminal and prescale counts to 0 */
void Chip_TIMER_Reset(LPC_TIMER_T *pTMR)
{
	uint32_t reg;

	/* Disable timer, set terminal count to non-0 */
	reg = pTMR-&amp;gt;TCR;
	pTMR-&amp;gt;TCR = 0;
	pTMR-&amp;gt;TC = 1;

	/* Reset timer counter */
	pTMR-&amp;gt;TCR = TIMER_RESET;

	/* Wait for terminal count to clear */
	while (pTMR-&amp;gt;TC != 0) {}

	/* Restore timer state */
	pTMR-&amp;gt;TCR = reg;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But the CMSIS library code is as follows and seems to do the same thing:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;void TIM_ResetCounter(LPC_TIMERn_Type *TIMx)
{
	CHECK_PARAM(PARAM_TIMx(TIMx));
	TIMx-&amp;gt;TCR |= TIM_RESET;
	TIMx-&amp;gt;TCR &amp;amp;= ~TIM_RESET;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone explain&amp;gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2024 14:25:53 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/LPC-Open-Chip-TIMER-Reset-strange-behaviour-and-implementation/m-p/1976164#M57093</guid>
      <dc:creator>RikkiB</dc:creator>
      <dc:date>2024-10-17T14:25:53Z</dc:date>
    </item>
    <item>
      <title>Re: LPC Open Chip_TIMER_Reset() strange behaviour and implementation</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/LPC-Open-Chip-TIMER-Reset-strange-behaviour-and-implementation/m-p/1976677#M57100</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Pls refer to section 32.6.2 Timer control register, you have to set the CRST bit, then wait for the bit cleared. so it takes a long time to wait for the bit cleared.&lt;/P&gt;
&lt;P&gt;The code is correct.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Chip_TIMER_Reset()&lt;/LI-CODE&gt;
&lt;P&gt;Hope it can help you&lt;/P&gt;
&lt;P&gt;BR&lt;/P&gt;
&lt;P&gt;XiangJun Rong&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="xiangjun_rong_0-1729223614941.png" style="width: 400px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/305386i29C892CDC85174F1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="xiangjun_rong_0-1729223614941.png" alt="xiangjun_rong_0-1729223614941.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2024 05:59:22 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/LPC-Open-Chip-TIMER-Reset-strange-behaviour-and-implementation/m-p/1976677#M57100</guid>
      <dc:creator>xiangjun_rong</dc:creator>
      <dc:date>2024-10-18T05:59:22Z</dc:date>
    </item>
    <item>
      <title>Re: LPC Open Chip_TIMER_Reset() strange behaviour and implementation</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/LPC-Open-Chip-TIMER-Reset-strange-behaviour-and-implementation/m-p/1976890#M57101</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have read&amp;nbsp;&lt;SPAN&gt;section 32.6.2.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;It states:&lt;/P&gt;&lt;P&gt;"When one, the Timer Counter and the Prescale Counter&lt;BR /&gt;are synchronously reset on the next positive edge of&lt;BR /&gt;PCLK. The counters remain reset until TCR[1] is&lt;BR /&gt;returned to zero."&lt;/P&gt;&lt;P&gt;So it should be reset on the next positive edge of PCLK. Seeing as PCLK is running at 122.88MHz on my board, this should take just over 8nS. Not anything like the values I am seeing:&lt;/P&gt;&lt;P&gt;"&lt;SPAN&gt;with no optimisation it takes 480nS but when I call it with -01 optimisation it takes 1.76uS"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Please can you explain why this is so?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I would also like an explanation of&amp;nbsp;Chip_TIMER_Reset(), in particular why it sets the TC to a not zero value before the reset? There is no explanation for this in the manual?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2024 08:41:28 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/LPC-Open-Chip-TIMER-Reset-strange-behaviour-and-implementation/m-p/1976890#M57101</guid>
      <dc:creator>RikkiB</dc:creator>
      <dc:date>2024-10-18T08:41:28Z</dc:date>
    </item>
    <item>
      <title>Re: LPC Open Chip_TIMER_Reset() strange behaviour and implementation</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/LPC-Open-Chip-TIMER-Reset-strange-behaviour-and-implementation/m-p/1977921#M57119</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I suppose that it is okay to use the following function to reset the CTimer.&lt;/P&gt;
&lt;PRE class="lia-code-sample  language-markup"&gt;&lt;CODE&gt;void TIM_ResetCounter(LPC_TIMERn_Type *TIMx)
{
	CHECK_PARAM(PARAM_TIMx(TIMx));
	TIMx-&amp;gt;TCR |= TIM_RESET;
	TIMx-&amp;gt;TCR &amp;amp;= ~TIM_RESET;
}&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have checked the CTimer driver in the recent SDK CTimer driver for the LPC55xx.&lt;/P&gt;
&lt;DIV style="background-color: #ffffff; padding: 0px 0px 0px 2px;"&gt;
&lt;DIV style="color: #000000; background-color: #ffffff; font-family: 'Consolas'; font-size: 10pt; white-space: pre;"&gt;
&lt;P style="margin: 0;"&gt;&lt;SPAN&gt;static&lt;/SPAN&gt; &lt;SPAN&gt;inline&lt;/SPAN&gt; &lt;SPAN&gt;void&lt;/SPAN&gt; &lt;SPAN&gt;CTIMER_Reset&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;CTIMER_Type&lt;/SPAN&gt;&lt;SPAN&gt; *base)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0;"&gt;&lt;SPAN&gt;{&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0;"&gt;&lt;SPAN&gt; base-&amp;gt;&lt;/SPAN&gt;&lt;SPAN&gt;TCR&lt;/SPAN&gt;&lt;SPAN&gt; |= CTIMER_TCR_CRST_MASK;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0;"&gt;&lt;SPAN&gt; base-&amp;gt;&lt;/SPAN&gt;&lt;SPAN&gt;TCR&lt;/SPAN&gt;&lt;SPAN&gt; &amp;amp;= ~CTIMER_TCR_CRST_MASK;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0;"&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0;"&gt;&lt;SPAN&gt;Regarding your issue that the time of calling Chip_TIMER_Reset(LPC_TIMER2); is dependent on the&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0;"&gt;&lt;SPAN&gt;the optimization level, pls check the debug of the function with/without optimization, and check what the problem is.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0;"&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="lia-code-sample  language-markup"&gt;&lt;CODE&gt;void Chip_TIMER_Reset(LPC_TIMER_T *pTMR)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P style="margin: 0;"&gt;&lt;SPAN&gt;Hope it can help you&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0;"&gt;&lt;SPAN&gt;BR&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0;"&gt;&lt;SPAN&gt;XiangJun Rong&lt;/SPAN&gt;&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2024 07:51:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/LPC-Open-Chip-TIMER-Reset-strange-behaviour-and-implementation/m-p/1977921#M57119</guid>
      <dc:creator>xiangjun_rong</dc:creator>
      <dc:date>2024-10-21T07:51:33Z</dc:date>
    </item>
  </channel>
</rss>

