ppisa commented on code in PR #12081:
URL: https://github.com/apache/nuttx/pull/12081#discussion_r1564993689


##########
arch/arm/src/samv7/sam_qencoder.c:
##########
@@ -270,11 +316,139 @@ static int sam_reset(struct qe_lowerhalf_s *lower)
 static int sam_ioctl(struct qe_lowerhalf_s *lower, int cmd,
                      unsigned long arg)
 {
-  /* No ioctl commands supported */
-
+#ifdef CONFIG_SAMV7_QENCODER_ENABLE_GETINDEX
+  switch (cmd)
+    {
+      case QEIOC_GETINDEX:
+        {
+          /* Call the qeindex function */
+
+          sam_qeindex(lower, (struct qe_index_s *) arg);
+          return OK;
+        }
+
+      default:
+        {
+          return -ENOTTY;
+        }
+    }
+#else
   return -ENOTTY;
+#endif
 }
 
+#ifdef CONFIG_SAMV7_QENCODER_ENABLE_GETINDEX
+/****************************************************************************
+ * Name: sam_qe_pos_16to32b
+ *
+ * Description:
+ *   An inline function performing the extension of current position.
+ *   Last reading is saved to priv->last_pos.
+ *
+ ****************************************************************************/
+
+static inline int32_t sam_qe_pos_16to32b(struct qe_lowerhalf_s *lower,
+                                         uint32_t current_pos)
+{
+  struct sam_lowerhalf_s *priv = (struct sam_lowerhalf_s *) lower;
+
+  uint32_t new_pos = *(volatile uint32_t *) &priv->last_pos;
+  new_pos += (int16_t) (current_pos - new_pos);
+  *(volatile uint32_t *) &priv->last_pos = new_pos;

Review Comment:
   Please, consult following reply on StackOverflow
   
https://stackoverflow.com/questions/18195715/why-is-unsigned-integer-overflow-defined-behavior-but-signed-integer-overflow-is
   or go to the C language standard. There is important even definition for 
conversion into unsigned type (6.3.1.3) which ensures correct behavior of 
proposed code even if the sign magnitude or one's complement encoding is used 
for signed types. As for the conversion of result of subtraction to int16_t, 
the result of subtraction should fit, if it does not (position read is not 
called often enough) then conversion result is not defined by C rules but 
position is lost anyway.



-- 
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