Fix-Point commented on code in PR #17316:
URL: https://github.com/apache/nuttx/pull/17316#discussion_r2525476046


##########
include/nuttx/timers/clkcnt.h:
##########
@@ -0,0 +1,440 @@
+/****************************************************************************
+ * include/nuttx/timers/clkcnt.h
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_TIMERS_CLKCNT_H
+#define __INCLUDE_NUTTX_TIMERS_CLKCNT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <stdint.h>
+
+#include <nuttx/clock.h>
+#include <nuttx/compiler.h>
+#include <nuttx/lib/math32.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CLKCNT_MAX UINT64_MAX
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+typedef uint64_t clkcnt_t;
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Name: clkcnt_best_multshift
+ *
+ * Description:
+ *   Calculate the parameters for converting `x * scale / freq`
+ *   to `x * mult >> shift`.
+ *
+ * Input Parameters:
+ *   scale - Time scale per second.
+ *   freq  - Clock frequency.
+ *   mult  - Pointer to multiply parameter.
+ *   shift - Pointer to shift parameter.
+ *
+ * Returned Value:
+ *   None.
+ ****************************************************************************/
+
+static inline_function
+void clkcnt_best_multshift(uint32_t freq, uint32_t scale,
+                           FAR uint32_t *mult, FAR uint32_t *shift)
+{
+  uint32_t logfreq = log2floor(freq);
+
+  /* Be careful of the round-nearest behavior here.
+   * It may lead to the converted result by the mult and shift
+   * is larger than the integer division.
+   * In rare cases, this can cause the converted tick to be greater
+   * than the actual tick, causing the timer to fire prematurely
+   * or tasks be scheduled incorrectly.

Review Comment:
   > Can you update the comment to explain the cases where the tick will be 
incorrect? What scenarios does it fail in?
   > 
   > Maybe you can also add a compile-time check here to throw an error if the 
system tick is in the nanosecond range? I doubt anyone will ever have 
nanosecond system ticks but if it ever happens this will make the limitation 
clear.
   
   More detailed comments have been added.
   



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to