You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2021/07/12 19:57:17 UTC

[GitHub] [incubator-nuttx] michallenc opened a new pull request #4138: i.MX RT: added support for Tickless OS

michallenc opened a new pull request #4138:
URL: https://github.com/apache/incubator-nuttx/pull/4138


   ## Summary
   This PR adds support for TIckless OS mode (only alarm is currently supported). GPT is used as a timer that is ran by 16.6 MHz peripheral clock and divided by prescaler 1 to 4096. This value can be represented by various CONFIG_USEC_PER_TICK (10 or 100 for example) and thus we should not have problems with the commutative time error caused when using 32.768 kHz clock (as this value or its divisions cannot be represented by CONFIG_USEC_PER_TICK).
   
   ## Impact
   i.MX RT MCU and boards only.
   
   ## Testing
   Tested with Teensy 4.1: real-time DC motor control application designed using pysimCoder
   sleep command
   ```
   nsh> time "sleep 10"
   
   10.0001 sec
   nsh>
   ```
   takes about 10 usec longer but this should be the property of the sleep command [as described here](https://nuttx.apache.org/docs/latest/applications/nsh/commands.html#time-execution-of-another-command-time)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] acassis merged pull request #4138: i.MX RT: added support for Tickless OS

Posted by GitBox <gi...@apache.org>.
acassis merged pull request #4138:
URL: https://github.com/apache/incubator-nuttx/pull/4138


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #4138: i.MX RT: added support for Tickless OS

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #4138:
URL: https://github.com/apache/incubator-nuttx/pull/4138#discussion_r668160012



##########
File path: arch/arm/src/imxrt/imxrt_tickless.c
##########
@@ -0,0 +1,603 @@
+/****************************************************************************
+ * arch/arm/src/imxrt/imxrt_tickless.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Tickless OS Support.
+ *
+ * When CONFIG_SCHED_TICKLESS is enabled, all support for timer interrupts
+ * is suppressed and the platform specific code is expected to provide the
+ * following custom functions.
+ *
+ *   void up_timer_initialize(void): Initializes the timer facilities.
+ *     Called early in the initialization sequence (by up_initialize()).
+ *   int up_timer_gettime(FAR struct timespec *ts):  Returns the current
+ *     time from the platform specific time source.
+ *   int up_timer_cancel(void):  Cancels the interval timer.
+ *   int up_timer_start(FAR const struct timespec *ts): Start (or re-starts)
+ *     the interval timer.
+ *
+ * The RTOS will provide the following interfaces for use by the platform-
+ * specific interval timer implementation:
+ *
+ *   void nxsched_timer_expiration(void):  Called by the platform-specific
+ *     logic when the interval timer expires.
+ *
+ * NOTE
+ * Only alarm option selected by CONFIG_SCHED_TICKLESS_ALARM is currently
+ * suported for iMXRT.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * iMXRT Timer Usage
+ *
+ * This implementation uses one timer:  A free running timer to provide
+ * the current time and a capture/compare channel for timed-events.
+ *
+ * This timer can be either General Purpose Timer (GPT) 1 or 2, which can
+ * be set by CONFIG_IMXRT_TICKLESS_TIMER. CONFIG_IMXRT_TICKLESS_CHANNEL
+ * selects which channel generates the interrupt for compare value.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <assert.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/irq.h>
+#include <debug.h>
+
+#include "arm_arch.h"
+#include "imxrt_periphclks.h"
+#include "hardware/imxrt_gpt.h"
+#include "imxrt_irq.h"
+
+#ifdef CONFIG_SCHED_TICKLESS
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Only alarm option is currently supported */
+
+#ifndef CONFIG_SCHED_TICKLESS_ALARM
+#  error Interval timer support is not supported yet, please select alarm
+#endif
+
+/* The Peripheral Clock (ipg_clk) is selected as the GPT clock source.
+ *
+ * REVISIT: Here we assume that the Peripheral Clock is 16.6 MHz. That is:
+ *
+ * PRECLK_CLOCK_ROOT = IPG_CLOCK_ROOT / IMXRT_PERCLK_PODF_DIVIDER
+ * where IPG_CLOCK_ROOT = 150 MHz and IMXRT_PERCLK_PODF_DIVIDER = 9
+ *
+ * Those clocks are set in imxrt_clockconfig.c, but makros are defined in
+ * board level section (file board.h) so clock settings may actually vary
+ * when using different boards.
+ *
+ * So, Peripheral Clock Frequency = 16.6 MHz
+ */
+
+#define GPT_CLOCK 16600000
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct imxrt_tickless_s
+{
+  uint8_t timer;                   /* The timer/counter in use */
+  uint8_t out_compare;             /* Number of output compare channel */
+  uint32_t frequency;              /* Frequency of the timer */
+  uint32_t overflow;               /* Timer counter overflow */
+  uint32_t irq;                    /* Interrupt number */
+  volatile bool pending;           /* True: pending task */
+  uint32_t base;                   /* Base address of the timer */
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct imxrt_tickless_s g_tickless;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: imxrt_get_counter
+ *
+ * Description:
+ *   Get counter value and add it to overflow value
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   Counter value
+ *
+ ****************************************************************************/
+
+static uint64_t imxrt_get_counter(void)
+{
+  return getreg32(g_tickless.base + IMXRT_GPT_CNT_OFFSET) | \
+                     ((uint64_t)g_tickless.overflow << 32);
+}
+
+/****************************************************************************
+ * Name: imxrt_interval_handler
+ *
+ * Description:
+ *   Called when the timer counter matches the compare register
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void imxrt_interval_handler(void)
+{
+  struct timespec tv;
+  uint32_t regval;
+
+  /* Disable the compare interrupt for now */
+
+  regval = getreg32(g_tickless.base + IMXRT_GPT_IR_OFFSET);
+  regval &= ~(1 << (g_tickless.out_compare - 1));
+  putreg32(regval, g_tickless.base + IMXRT_GPT_IR_OFFSET);
+
+  /* And clear it */
+
+  putreg32((1 << (g_tickless.out_compare - 1)),
+           g_tickless.base + IMXRT_GPT_SR_OFFSET);
+
+  g_tickless.pending = false;
+
+  up_timer_gettime(&tv);
+  nxsched_alarm_expiration(&tv);
+}
+
+/****************************************************************************
+ * Name: imxrt_timing_handler
+ *
+ * Description:
+ *   Timer interrupt callback.  When the freerun timer counter overflows,
+ *   this interrupt will occur.  We will just increment an overflow count.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void imxrt_timing_handler(void)
+{
+  g_tickless.overflow++;
+  uint32_t regval;
+
+  /* Clear interrupt bit */
+
+  putreg32(GPT_SR_ROV, g_tickless.base + IMXRT_GPT_SR_OFFSET);
+}
+
+/****************************************************************************
+ * Name: imxrt_tickless_handler
+ *
+ * Description:
+ *   Generic interrupt handler for this timer.  It checks the source of the
+ *   interrupt and fires the appropriate handler.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *

Review comment:
       Please fix doc!




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] michallenc commented on a change in pull request #4138: i.MX RT: added support for Tickless OS

Posted by GitBox <gi...@apache.org>.
michallenc commented on a change in pull request #4138:
URL: https://github.com/apache/incubator-nuttx/pull/4138#discussion_r668538136



##########
File path: arch/arm/src/imxrt/imxrt_tickless.c
##########
@@ -0,0 +1,603 @@
+/****************************************************************************
+ * arch/arm/src/imxrt/imxrt_tickless.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Tickless OS Support.
+ *
+ * When CONFIG_SCHED_TICKLESS is enabled, all support for timer interrupts
+ * is suppressed and the platform specific code is expected to provide the
+ * following custom functions.
+ *
+ *   void up_timer_initialize(void): Initializes the timer facilities.
+ *     Called early in the initialization sequence (by up_initialize()).
+ *   int up_timer_gettime(FAR struct timespec *ts):  Returns the current
+ *     time from the platform specific time source.
+ *   int up_timer_cancel(void):  Cancels the interval timer.
+ *   int up_timer_start(FAR const struct timespec *ts): Start (or re-starts)
+ *     the interval timer.
+ *
+ * The RTOS will provide the following interfaces for use by the platform-
+ * specific interval timer implementation:
+ *
+ *   void nxsched_timer_expiration(void):  Called by the platform-specific
+ *     logic when the interval timer expires.
+ *
+ * NOTE
+ * Only alarm option selected by CONFIG_SCHED_TICKLESS_ALARM is currently
+ * suported for iMXRT.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * iMXRT Timer Usage
+ *
+ * This implementation uses one timer:  A free running timer to provide
+ * the current time and a capture/compare channel for timed-events.
+ *
+ * This timer can be either General Purpose Timer (GPT) 1 or 2, which can
+ * be set by CONFIG_IMXRT_TICKLESS_TIMER. CONFIG_IMXRT_TICKLESS_CHANNEL
+ * selects which channel generates the interrupt for compare value.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <assert.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/irq.h>
+#include <debug.h>
+
+#include "arm_arch.h"
+#include "imxrt_periphclks.h"
+#include "hardware/imxrt_gpt.h"
+#include "imxrt_irq.h"
+
+#ifdef CONFIG_SCHED_TICKLESS
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Only alarm option is currently supported */
+
+#ifndef CONFIG_SCHED_TICKLESS_ALARM
+#  error Interval timer support is not supported yet, please select alarm
+#endif
+
+/* The Peripheral Clock (ipg_clk) is selected as the GPT clock source.
+ *
+ * REVISIT: Here we assume that the Peripheral Clock is 16.6 MHz. That is:
+ *
+ * PRECLK_CLOCK_ROOT = IPG_CLOCK_ROOT / IMXRT_PERCLK_PODF_DIVIDER
+ * where IPG_CLOCK_ROOT = 150 MHz and IMXRT_PERCLK_PODF_DIVIDER = 9
+ *
+ * Those clocks are set in imxrt_clockconfig.c, but makros are defined in
+ * board level section (file board.h) so clock settings may actually vary
+ * when using different boards.
+ *
+ * So, Peripheral Clock Frequency = 16.6 MHz
+ */
+
+#define GPT_CLOCK 16600000
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct imxrt_tickless_s
+{
+  uint8_t timer;                   /* The timer/counter in use */
+  uint8_t out_compare;             /* Number of output compare channel */
+  uint32_t frequency;              /* Frequency of the timer */
+  uint32_t overflow;               /* Timer counter overflow */
+  uint32_t irq;                    /* Interrupt number */
+  volatile bool pending;           /* True: pending task */
+  uint32_t base;                   /* Base address of the timer */
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct imxrt_tickless_s g_tickless;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: imxrt_get_counter
+ *
+ * Description:
+ *   Get counter value and add it to overflow value
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   Counter value
+ *
+ ****************************************************************************/
+
+static uint64_t imxrt_get_counter(void)
+{
+  return getreg32(g_tickless.base + IMXRT_GPT_CNT_OFFSET) | \
+                     ((uint64_t)g_tickless.overflow << 32);
+}
+
+/****************************************************************************
+ * Name: imxrt_interval_handler
+ *
+ * Description:
+ *   Called when the timer counter matches the compare register
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void imxrt_interval_handler(void)
+{
+  struct timespec tv;
+  uint32_t regval;
+
+  /* Disable the compare interrupt for now */
+
+  regval = getreg32(g_tickless.base + IMXRT_GPT_IR_OFFSET);
+  regval &= ~(1 << (g_tickless.out_compare - 1));
+  putreg32(regval, g_tickless.base + IMXRT_GPT_IR_OFFSET);
+
+  /* And clear it */
+
+  putreg32((1 << (g_tickless.out_compare - 1)),
+           g_tickless.base + IMXRT_GPT_SR_OFFSET);
+
+  g_tickless.pending = false;
+
+  up_timer_gettime(&tv);
+  nxsched_alarm_expiration(&tv);
+}
+
+/****************************************************************************
+ * Name: imxrt_timing_handler
+ *
+ * Description:
+ *   Timer interrupt callback.  When the freerun timer counter overflows,
+ *   this interrupt will occur.  We will just increment an overflow count.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void imxrt_timing_handler(void)
+{
+  g_tickless.overflow++;
+  uint32_t regval;
+
+  /* Clear interrupt bit */
+
+  putreg32(GPT_SR_ROV, g_tickless.base + IMXRT_GPT_SR_OFFSET);
+}
+
+/****************************************************************************
+ * Name: imxrt_tickless_handler
+ *
+ * Description:
+ *   Generic interrupt handler for this timer.  It checks the source of the
+ *   interrupt and fires the appropriate handler.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *

Review comment:
       @acassis done, thanks.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org