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 2022/01/29 23:52:42 UTC

[GitHub] [incubator-nuttx] acassis opened a new pull request #5376: i2c: Add TCA9548A I2C Multiplexer

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


   ## Summary
   Add TCA9548A I2C Multiplexer
   ## Impact
   NuttX users will be able to use up to 8 I2C devices even when they have same I2C address.
   ## Testing
   ESP32-Devkit + TCA9548A HW-617 module
   


-- 
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 #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: include/nuttx/i2c/tca9548a.h
##########
@@ -0,0 +1,126 @@
+/****************************************************************************
+ * include/nuttx/i2c/tca9548a.h
+ *
+ * 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_I2C_TCA9548A_H
+#define __INCLUDE_NUTTX_I2C_TCA9548A_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/ioctl.h>
+#include <nuttx/i2c/i2c_master.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* It is possible to config. TCA9548A to communicate from 0x70-0x77 */
+
+#define TCA9548A_BASEADDR0    0x70
+#define TCA9548A_BASEADDR1    0x71
+#define TCA9548A_BASEADDR2    0x72
+#define TCA9548A_BASEADDR3    0x73
+#define TCA9548A_BASEADDR4    0x74
+#define TCA9548A_BASEADDR5    0x75
+#define TCA9548A_BASEADDR6    0x76
+#define TCA9548A_BASEADDR7    0x77
+
+/* The TCA9548A can multiplex up to 8 channels */
+
+#define TCA9548A_SEL_CH0      0x0
+#define TCA9548A_SEL_CH1      0x1
+#define TCA9548A_SEL_CH2      0x2
+#define TCA9548A_SEL_CH3      0x3
+#define TCA9548A_SEL_CH4      0x4
+#define TCA9548A_SEL_CH5      0x5
+#define TCA9548A_SEL_CH6      0x6
+#define TCA9548A_SEL_CH7      0x7
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct tca9548a_dev_s
+{
+  FAR struct i2c_master_s *i2c;      /* I2C interface */
+  uint16_t addr;

Review comment:
       You are right, it can be uint8_t, just modified it.




-- 
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 #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,145 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =
+                                                {
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL
+                                                };
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_tca9548a_initialize
+ *
+ * Description:
+ *   Initialize and register the TCA9548A Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_tca9548a_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  uint8_t addr;
+
+  i2cinfo("Initializing TCA9548A!\n");
+
+  /* TCA9548A valid addresses are 0:0x70 - 7:0x77 */
+
+  if (devno < TCA9548A_SEL_CH0 && devno > TCA9548A_SEL_CH8)

Review comment:
       Thank you!




-- 
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 #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: include/nuttx/i2c/tca9548a.h
##########
@@ -0,0 +1,126 @@
+/****************************************************************************
+ * include/nuttx/i2c/tca9548a.h
+ *
+ * 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_I2C_TCA9548A_H
+#define __INCLUDE_NUTTX_I2C_TCA9548A_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/ioctl.h>
+#include <nuttx/i2c/i2c_master.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* It is possible to config. TCA9548A to communicate from 0x70-0x77 */
+
+#define TCA9548A_BASEADDR0    0x70
+#define TCA9548A_BASEADDR1    0x71
+#define TCA9548A_BASEADDR2    0x72
+#define TCA9548A_BASEADDR3    0x73
+#define TCA9548A_BASEADDR4    0x74
+#define TCA9548A_BASEADDR5    0x75
+#define TCA9548A_BASEADDR6    0x76
+#define TCA9548A_BASEADDR7    0x77
+
+/* The TCA9548A can multiplex up to 8 channels */
+
+#define TCA9548A_SEL_CH0      0x0
+#define TCA9548A_SEL_CH2      0x1

Review comment:
       Yes, I jumper over CH1, thank for fixing this issue




-- 
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 #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,145 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =
+                                                {
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL
+                                                };
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_tca9548a_initialize
+ *
+ * Description:
+ *   Initialize and register the TCA9548A Multiplexer.
+ *
+ * Input Parameters:
+ *   devno - The device number associated to I2C address, 0=0x70 ... 7=0x77
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_tca9548a_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  uint8_t addr;
+
+  i2cinfo("Initializing TCA9548A!\n");
+
+  /* TCA9548A valid addresses are 0:0x70 - 7:0x77 */
+
+  if (devno < TCA9548A_SEL_CH0 && devno > TCA9548A_SEL_CH7)
+    {
+      i2cerr("Invalid devno: %d! Valid range: 0 to 7!\n", devno);
+      return -EINVAL;
+    }
+
+  addr = TCA9548A_BASEADDR0 + devno;

Review comment:
       You are right, comparing it to TCA9548A_SEL_CH7 at "board_tca9548a_initialize" is generating confusion:2
   ```
   if (devno < TCA9548A_SEL_CH0 && devno > TCA9548A_SEL_CH7)
   ```
   
   I could do other way too:
   ```
     addr = TCA9548A_BASEADDR0 + devno;
   
     if (addr < TCA9548A_BASEADDR0 || addr > TCA9548A_BASEADDR7)
       {
         i2cerr("Invalid devno: %d! Valid range: 0 to 7!\n", devno);
         return -EINVAL;
       }
   ```




-- 
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] a-lunev commented on a change in pull request #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: drivers/i2c/tca9548a.c
##########
@@ -0,0 +1,404 @@
+/****************************************************************************
+ * drivers/i2c/tca9548a.c
+ * Driver for the TCA9448A i2c multiplexer
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#ifdef CONFIG_I2CMULTIPLEXER_TCA9548A
+
+#ifndef CONFIG_TCA9548A_I2C_FREQUENCY
+#  define CONFIG_TCA9548A_I2C_FREQUENCY    400000
+#endif
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* I2C Helpers */
+
+static int tca9548a_write_config(FAR struct tca9548a_dev_s *priv,
+                                 FAR uint8_t regvalue);
+static int tca9548a_read_config(FAR struct tca9548a_dev_s *priv,
+                                FAR uint8_t *regvalue);
+
+/* Other helpers */
+
+static int tca9548a_select_channel(FAR struct tca9548a_dev_s *priv,
+                                   uint8_t val);
+
+/* I2C multiplexer vtable */
+
+static int tca9548a_transfer_on_channel (FAR struct i2c_master_s *dev,
+                                         FAR struct i2c_msg_s *msgs,
+                                         int count);
+#ifdef CONFIG_I2C_RESET
+static int tca9548a_reset_on_channel (FAR struct i2c_master_s *dev);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct i2c_ops_s g_i2cmux_ops =
+{
+  tca9548a_transfer_on_channel
+#ifdef CONFIG_I2C_RESET
+  , tca9548a_reset_on_channel
+#endif
+};
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct i2c_channel_dev_s
+{
+  FAR struct i2c_master_s vi2c;      /* Nested structure to allow casting as
+                                      * public i2c master */
+  uint8_t channel;                   /* Associated channel on the mux */
+  FAR struct tca9548a_dev_s *dev;    /* Associated device */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tca9548a_write_config
+ *
+ * Description:
+ *   Write to the mux register of TCA9548A.
+ *
+ * Input Parameters:
+ *   priv     - A pointer to the TCA9548A device structure.
+ *   regvalue - The value that will be written.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_write_config(FAR struct tca9548a_dev_s *priv,
+                                 FAR uint8_t regvalue)

Review comment:
       Is the FAR required for regvalue?




-- 
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 #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,145 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =
+                                                {
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL
+                                                };

Review comment:
       hmm, if I move it to global it will be allocated on bss and will be zeroed by default. Anyway I think it is save to initialize as NULL to avoid erratic behavior of compiler. I removed the spaces as you suggested.




-- 
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 #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,145 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =
+                                                {
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL
+                                                };

Review comment:
       hmm, if a move it to global it will be allocated on bss and will be zeroed by default. Anyway I think it is save to initialize as NULL to avoid erratic behavior of compiler.




-- 
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] a-lunev commented on a change in pull request #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: drivers/i2c/tca9548a.c
##########
@@ -0,0 +1,404 @@
+/****************************************************************************
+ * drivers/i2c/tca9548a.c
+ * Driver for the TCA9448A i2c multiplexer
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#ifdef CONFIG_I2CMULTIPLEXER_TCA9548A
+
+#ifndef CONFIG_TCA9548A_I2C_FREQUENCY
+#  define CONFIG_TCA9548A_I2C_FREQUENCY    400000
+#endif
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* I2C Helpers */
+
+static int tca9548a_write_config(FAR struct tca9548a_dev_s *priv,
+                                 FAR uint8_t regvalue);
+static int tca9548a_read_config(FAR struct tca9548a_dev_s *priv,
+                                FAR uint8_t *regvalue);
+
+/* Other helpers */
+
+static int tca9548a_select_channel(FAR struct tca9548a_dev_s *priv,
+                                   uint8_t val);
+
+/* I2C multiplexer vtable */
+
+static int tca9548a_transfer_on_channel (FAR struct i2c_master_s *dev,
+                                         FAR struct i2c_msg_s *msgs,
+                                         int count);
+#ifdef CONFIG_I2C_RESET
+static int tca9548a_reset_on_channel (FAR struct i2c_master_s *dev);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct i2c_ops_s g_i2cmux_ops =
+{
+  tca9548a_transfer_on_channel
+#ifdef CONFIG_I2C_RESET
+  , tca9548a_reset_on_channel
+#endif
+};
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct i2c_channel_dev_s
+{
+  FAR struct i2c_master_s vi2c;      /* Nested structure to allow casting as
+                                      * public i2c master */
+  uint8_t channel;                   /* Associated channel on the mux */
+  FAR struct tca9548a_dev_s *dev;    /* Associated device */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tca9548a_write_config
+ *
+ * Description:
+ *   Write to the mux register of TCA9548A.
+ *
+ * Input Parameters:
+ *   priv     - A pointer to the TCA9548A device structure.
+ *   regvalue - The value that will be written.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_write_config(FAR struct tca9548a_dev_s *priv,
+                                 FAR uint8_t regvalue)
+{
+  struct i2c_config_s iconf;
+  int ret;
+
+  iconf.frequency = CONFIG_TCA9548A_I2C_FREQUENCY;
+  iconf.address   = priv->addr;
+  iconf.addrlen   = 7;
+
+  ret = i2c_write(priv->i2c, &iconf, &regvalue, 1);
+
+  i2cinfo("Write to address 0x%02X; register value: 0x%02x ret: %d\n",
+          priv->addr, regvalue, ret);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tca9548a_read_config
+ *
+ * Description:
+ *   Read the mux register from TCA9548A.
+ *
+ * Input Parameters:
+ *   priv     - A pointer to the TCA9548A device structure.
+ *   regvalue - A pointer to a buffer into which data will be received.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_read_config(FAR struct tca9548a_dev_s *priv,
+                                FAR uint8_t *regvalue)
+{
+  struct i2c_config_s iconf;
+  int ret;
+
+  iconf.frequency = CONFIG_TCA9548A_I2C_FREQUENCY;
+  iconf.address   = priv->addr;
+  iconf.addrlen   = 7;
+
+  ret = i2c_read(priv->i2c, &iconf, regvalue, 1);
+
+  i2cinfo("Read from address: 0x%02X; register value: 0x%02x ret: %d\n",
+          priv->addr, *regvalue, ret);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tca9548a_select_channel
+ *
+ * Description:
+ *   Helper function to select a channel in the TCA9548A. The TCA9548A allows
+ *   to select more than 1 channel at same time, but here we are forcing it
+ *   to only enable a single channel by time. It will avoid collision in case
+ *   where two devices with same address are connected to two channels.
+ *
+ * Input Parameters:
+ *   dev  - Pointer to the (virtual) i2c_master_s.
+ *   val  - The channel to be selected.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_select_channel(FAR struct tca9548a_dev_s *priv,
+                                   uint8_t val)
+{
+  if (val > TCA9548A_SEL_CH7)
+    {
+      /* channel not existent/supported */
+
+      return -EINVAL;
+    }
+
+  if ((priv->state & (1 << val)) != 0)
+    {
+      /* channel already selected */
+
+      return OK;
+    }
+
+  /* Modify state and write it to the mux. Selecting a channel always enables
+   * the device
+   */
+
+  priv->state = (1 << val);

Review comment:
       priv->state should be overwritten only if tca9548a_write_config(priv, priv->state) is successful.




-- 
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] pkarashchenko merged pull request #5376: i2c: Add TCA9548A I2C Multiplexer

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


   


-- 
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 #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,145 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =
+                                                {
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL
+                                                };
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_tca9548a_initialize
+ *
+ * Description:
+ *   Initialize and register the TCA9548A Multiplexer.
+ *
+ * Input Parameters:
+ *   devno - The device number associated to I2C address, 0=0x70 ... 7=0x77
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_tca9548a_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  uint8_t addr;
+
+  i2cinfo("Initializing TCA9548A!\n");
+
+  /* TCA9548A valid addresses are 0:0x70 - 7:0x77 */
+
+  if (devno < TCA9548A_SEL_CH0 && devno > TCA9548A_SEL_CH7)
+    {
+      i2cerr("Invalid devno: %d! Valid range: 0 to 7!\n", devno);
+      return -EINVAL;
+    }
+
+  addr = TCA9548A_BASEADDR0 + devno;
+
+  /* Initialize TCA9548A */
+
+  i2c = esp32_i2cbus_initialize(busno);
+  if (i2c != NULL)
+    {
+      /* Then try to initialize the TCA9548A */
+
+      g_tca9448a_devs[devno] = tca9548a_initialize(i2c, addr);

Review comment:
       Yes, I think it is a good idea! Thank you!




-- 
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] pkarashchenko commented on a change in pull request #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,156 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =

Review comment:
       ```suggestion
   struct tca9548a_dev_s *g_tca9448a_devs[8] =
   ```
   




-- 
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 #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: include/nuttx/i2c/tca9548a.h
##########
@@ -0,0 +1,126 @@
+/****************************************************************************
+ * include/nuttx/i2c/tca9548a.h
+ *
+ * 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_I2C_TCA9548A_H
+#define __INCLUDE_NUTTX_I2C_TCA9548A_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/ioctl.h>
+#include <nuttx/i2c/i2c_master.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* It is possible to config. TCA9548A to communicate from 0x70-0x77 */
+
+#define TCA9548A_BASEADDR0    0x70
+#define TCA9548A_BASEADDR1    0x71
+#define TCA9548A_BASEADDR2    0x72
+#define TCA9548A_BASEADDR3    0x73
+#define TCA9548A_BASEADDR4    0x74
+#define TCA9548A_BASEADDR5    0x75
+#define TCA9548A_BASEADDR6    0x76
+#define TCA9548A_BASEADDR7    0x77
+
+/* The TCA9548A can multiplex up to 8 channels */
+
+#define TCA9548A_SEL_CH0      0x0
+#define TCA9548A_SEL_CH1      0x1
+#define TCA9548A_SEL_CH2      0x2
+#define TCA9548A_SEL_CH3      0x3
+#define TCA9548A_SEL_CH4      0x4
+#define TCA9548A_SEL_CH5      0x5
+#define TCA9548A_SEL_CH6      0x6
+#define TCA9548A_SEL_CH7      0x7
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct tca9548a_dev_s
+{
+  FAR struct i2c_master_s *i2c;      /* I2C interface */
+  uint16_t addr;

Review comment:
       Addr is always 16-bit, please see "struct i2c_msg_s" definition.




-- 
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 #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: drivers/i2c/tca9548a.c
##########
@@ -0,0 +1,404 @@
+/****************************************************************************
+ * drivers/i2c/tca9548a.c
+ * Driver for the TCA9448A i2c multiplexer
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#ifdef CONFIG_I2CMULTIPLEXER_TCA9548A
+
+#ifndef CONFIG_TCA9548A_I2C_FREQUENCY
+#  define CONFIG_TCA9548A_I2C_FREQUENCY    400000
+#endif
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* I2C Helpers */
+
+static int tca9548a_write_config(FAR struct tca9548a_dev_s *priv,
+                                 FAR uint8_t regvalue);
+static int tca9548a_read_config(FAR struct tca9548a_dev_s *priv,
+                                FAR uint8_t *regvalue);
+
+/* Other helpers */
+
+static int tca9548a_select_channel(FAR struct tca9548a_dev_s *priv,
+                                   uint8_t val);
+
+/* I2C multiplexer vtable */
+
+static int tca9548a_transfer_on_channel (FAR struct i2c_master_s *dev,
+                                         FAR struct i2c_msg_s *msgs,
+                                         int count);
+#ifdef CONFIG_I2C_RESET
+static int tca9548a_reset_on_channel (FAR struct i2c_master_s *dev);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct i2c_ops_s g_i2cmux_ops =
+{
+  tca9548a_transfer_on_channel
+#ifdef CONFIG_I2C_RESET
+  , tca9548a_reset_on_channel
+#endif
+};
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct i2c_channel_dev_s
+{
+  FAR struct i2c_master_s vi2c;      /* Nested structure to allow casting as
+                                      * public i2c master */
+  uint8_t channel;                   /* Associated channel on the mux */
+  FAR struct tca9548a_dev_s *dev;    /* Associated device */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tca9548a_write_config
+ *
+ * Description:
+ *   Write to the mux register of TCA9548A.
+ *
+ * Input Parameters:
+ *   priv     - A pointer to the TCA9548A device structure.
+ *   regvalue - The value that will be written.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_write_config(FAR struct tca9548a_dev_s *priv,
+                                 FAR uint8_t regvalue)

Review comment:
       Good point, it is not!




-- 
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 #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,156 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =

Review comment:
       Right, it is board specific, we don't need FAR :-)




-- 
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 #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,145 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =
+                                                {
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL
+                                                };

Review comment:
       Not defining it to NULL will create a dangling pointer, please do a test if you want to confirm it:
   ```
   #include <stdio.h>
   
   struct dev_s
   {
     int a;
     int b;
     char c;
   };
   
   int main(void)
   {
     struct dev_s *devs[8];
     struct dev_s *devx[8]=
     {
   	  NULL,
   	  NULL,
   	  NULL,
   	  NULL,
   	  NULL,
   	  NULL,
   	  NULL,
   	  NULL,
     };
   
     if (devs[0] == NULL)
     {
       printf("devs[0] is NULL\n");
     }
     else
     {
       printf("devs[0] is NOT NULL\n");
     }
   
     if (devx[0] == NULL)
     {
       printf("devx[0] is NULL\n");
     }
     else
     {
       printf("devx[0] is NOT NULL\n");
     }
   
     return 0;
   }
   ```




-- 
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] a-lunev commented on a change in pull request #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: include/nuttx/i2c/tca9548a.h
##########
@@ -0,0 +1,126 @@
+/****************************************************************************
+ * include/nuttx/i2c/tca9548a.h
+ *
+ * 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_I2C_TCA9548A_H
+#define __INCLUDE_NUTTX_I2C_TCA9548A_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/ioctl.h>
+#include <nuttx/i2c/i2c_master.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* It is possible to config. TCA9548A to communicate from 0x70-0x77 */
+
+#define TCA9548A_BASEADDR0    0x70
+#define TCA9548A_BASEADDR1    0x71
+#define TCA9548A_BASEADDR2    0x72
+#define TCA9548A_BASEADDR3    0x73
+#define TCA9548A_BASEADDR4    0x74
+#define TCA9548A_BASEADDR5    0x75
+#define TCA9548A_BASEADDR6    0x76
+#define TCA9548A_BASEADDR7    0x77
+
+/* The TCA9548A can multiplex up to 8 channels */
+
+#define TCA9548A_SEL_CH0      0x0
+#define TCA9548A_SEL_CH1      0x1
+#define TCA9548A_SEL_CH2      0x2
+#define TCA9548A_SEL_CH3      0x3
+#define TCA9548A_SEL_CH4      0x4
+#define TCA9548A_SEL_CH5      0x5
+#define TCA9548A_SEL_CH6      0x6
+#define TCA9548A_SEL_CH7      0x7
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct tca9548a_dev_s
+{
+  FAR struct i2c_master_s *i2c;      /* I2C interface */
+  uint16_t addr;

Review comment:
       I have analyzed the source code of NuttX concerning I2C. The addr field of tca9548a_dev_s is not part of any driver layer API. It is used solely in the scope of the upper half TCA9548A driver.
   Thus I think tca9548a_dev_s.addr can be uint8_t to save memory.




-- 
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] a-lunev commented on a change in pull request #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,145 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =
+                                                {
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL
+                                                };
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_tca9548a_initialize
+ *
+ * Description:
+ *   Initialize and register the TCA9548A Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_tca9548a_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  uint8_t addr;
+
+  i2cinfo("Initializing TCA9548A!\n");
+
+  /* TCA9548A valid addresses are 0:0x70 - 7:0x77 */
+
+  if (devno < TCA9548A_SEL_CH0 && devno > TCA9548A_SEL_CH8)

Review comment:
       Should be TCA9548A_SEL_CH7

##########
File path: include/nuttx/i2c/tca9548a.h
##########
@@ -0,0 +1,126 @@
+/****************************************************************************
+ * include/nuttx/i2c/tca9548a.h
+ *
+ * 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_I2C_TCA9548A_H
+#define __INCLUDE_NUTTX_I2C_TCA9548A_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/ioctl.h>
+#include <nuttx/i2c/i2c_master.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* It is possible to config. TCA9548A to communicate from 0x70-0x77 */
+
+#define TCA9548A_BASEADDR0    0x70
+#define TCA9548A_BASEADDR1    0x71
+#define TCA9548A_BASEADDR2    0x72
+#define TCA9548A_BASEADDR3    0x73
+#define TCA9548A_BASEADDR4    0x74
+#define TCA9548A_BASEADDR5    0x75
+#define TCA9548A_BASEADDR6    0x76
+#define TCA9548A_BASEADDR7    0x77
+
+/* The TCA9548A can multiplex up to 8 channels */
+
+#define TCA9548A_SEL_CH0      0x0
+#define TCA9548A_SEL_CH2      0x1

Review comment:
       Should be:
   #define TCA9548A_SEL_CH1      0x1
   #define TCA9548A_SEL_CH2      0x2
   #define TCA9548A_SEL_CH3      0x3
   #define TCA9548A_SEL_CH4      0x4
   #define TCA9548A_SEL_CH5      0x5
   #define TCA9548A_SEL_CH6      0x6
   #define TCA9548A_SEL_CH7      0x7

##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,145 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =
+                                                {
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL
+                                                };
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_tca9548a_initialize
+ *
+ * Description:
+ *   Initialize and register the TCA9548A Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_tca9548a_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  uint8_t addr;
+
+  i2cinfo("Initializing TCA9548A!\n");
+
+  /* TCA9548A valid addresses are 0:0x70 - 7:0x77 */
+
+  if (devno < TCA9548A_SEL_CH0 && devno > TCA9548A_SEL_CH8)
+    {
+      i2cerr("Invalid devno: %d! Valid range: 0 to 7!\n", devno);
+      return -EINVAL;
+    }
+
+  addr = TCA9548A_BASEADDR0 + devno;
+
+  /* Initialize TCA9548A */
+
+  i2c = esp32_i2cbus_initialize(busno);
+  if (i2c != NULL)
+    {
+      /* Then try to initialize the TCA9548A */
+
+      g_tca9448a_devs[devno] = tca9548a_initialize(i2c, addr);
+
+      if (g_tca9448a_devs[devno] == NULL)
+        {
+          i2cerr("ERROR: Error registering TCA9548A in I2C%d\n", busno);
+        }
+    }
+  else
+    {
+      return -ENODEV;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: esp32_i2cmux_getmaster
+ *
+ * Description:
+ *   Returns an I2C Master for TCA9548A multiplexer channel.
+ *
+ *   NOTE: esp32_i2cmux_getmaster() is generic name, it can be used to return
+ *   an I2C Master for others I2C Master, not only TCA9548A.
+ *
+ * Input Parameters:
+ *   devno   - The device number, it is the TCA9548A I2C_Addr minus 0x70.
+ *   channel - The TCA9548A's channel where the device will be added.
+ *
+ * Returned Value:
+ *   Common i2c multiplexer device instance; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct i2c_master_s *esp32_i2cmux_getmaster(int devno, uint8_t channel)
+{
+  /* Check if we have a valid device number */
+
+  if (devno < TCA9548A_SEL_CH0 && devno > TCA9548A_SEL_CH8)
+    {
+      i2cerr("Invalid devno: %d! Valid range: 0 to 7!\n", devno);
+      return NULL;
+    }
+
+  /* Check if we have a valid channel */
+
+  if (channel > TCA9548A_SEL_CH8)

Review comment:
       Should be TCA9548A_SEL_CH7

##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,145 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =
+                                                {
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL
+                                                };
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_tca9548a_initialize
+ *
+ * Description:
+ *   Initialize and register the TCA9548A Pressure Sensor driver.
+ *
+ * Input Parameters:
+ *   devno - The device number, used to build the device path as /dev/pressN
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_tca9548a_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  uint8_t addr;
+
+  i2cinfo("Initializing TCA9548A!\n");
+
+  /* TCA9548A valid addresses are 0:0x70 - 7:0x77 */
+
+  if (devno < TCA9548A_SEL_CH0 && devno > TCA9548A_SEL_CH8)
+    {
+      i2cerr("Invalid devno: %d! Valid range: 0 to 7!\n", devno);
+      return -EINVAL;
+    }
+
+  addr = TCA9548A_BASEADDR0 + devno;
+
+  /* Initialize TCA9548A */
+
+  i2c = esp32_i2cbus_initialize(busno);
+  if (i2c != NULL)
+    {
+      /* Then try to initialize the TCA9548A */
+
+      g_tca9448a_devs[devno] = tca9548a_initialize(i2c, addr);
+
+      if (g_tca9448a_devs[devno] == NULL)
+        {
+          i2cerr("ERROR: Error registering TCA9548A in I2C%d\n", busno);
+        }
+    }
+  else
+    {
+      return -ENODEV;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: esp32_i2cmux_getmaster
+ *
+ * Description:
+ *   Returns an I2C Master for TCA9548A multiplexer channel.
+ *
+ *   NOTE: esp32_i2cmux_getmaster() is generic name, it can be used to return
+ *   an I2C Master for others I2C Master, not only TCA9548A.
+ *
+ * Input Parameters:
+ *   devno   - The device number, it is the TCA9548A I2C_Addr minus 0x70.
+ *   channel - The TCA9548A's channel where the device will be added.
+ *
+ * Returned Value:
+ *   Common i2c multiplexer device instance; NULL on failure.
+ *
+ ****************************************************************************/
+
+FAR struct i2c_master_s *esp32_i2cmux_getmaster(int devno, uint8_t channel)
+{
+  /* Check if we have a valid device number */
+
+  if (devno < TCA9548A_SEL_CH0 && devno > TCA9548A_SEL_CH8)

Review comment:
       Should be TCA9548A_SEL_CH7




-- 
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] pkarashchenko commented on a change in pull request #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,145 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =
+                                                {
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL
+                                                };

Review comment:
       ```suggestion
   FAR struct tca9548a_dev_s *g_tca9448a_devs[8];
   ```
   Is zero initialized by a standard. Only relevant in case if `NULL` is not defined to zero.
   Or remove extra spaces and make like:
   ```
   {
     NULL,
     NULL,
   ...
   };
   ```

##########
File path: drivers/i2c/tca9548a.c
##########
@@ -0,0 +1,404 @@
+/****************************************************************************
+ * drivers/i2c/tca9548a.c
+ * Driver for the TCA9448A i2c multiplexer
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#ifdef CONFIG_I2CMULTIPLEXER_TCA9548A
+
+#ifndef CONFIG_TCA9548A_I2C_FREQUENCY
+#  define CONFIG_TCA9548A_I2C_FREQUENCY    400000
+#endif
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* I2C Helpers */
+
+static int tca9548a_write_config(FAR struct tca9548a_dev_s *priv,
+                                 FAR uint8_t regvalue);
+static int tca9548a_read_config(FAR struct tca9548a_dev_s *priv,
+                                FAR uint8_t *regvalue);
+
+/* Other helpers */
+
+static int tca9548a_select_channel(FAR struct tca9548a_dev_s *priv,
+                                   uint8_t val);
+
+/* I2C multiplexer vtable */
+
+static int tca9548a_transfer_on_channel (FAR struct i2c_master_s *dev,
+                                         FAR struct i2c_msg_s *msgs,
+                                         int count);
+#ifdef CONFIG_I2C_RESET
+static int tca9548a_reset_on_channel (FAR struct i2c_master_s *dev);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct i2c_ops_s g_i2cmux_ops =
+{
+  tca9548a_transfer_on_channel
+#ifdef CONFIG_I2C_RESET
+  , tca9548a_reset_on_channel
+#endif
+};
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct i2c_channel_dev_s
+{
+  FAR struct i2c_master_s vi2c;      /* Nested structure to allow casting as
+                                      * public i2c master */
+  uint8_t channel;                   /* Associated channel on the mux */
+  FAR struct tca9548a_dev_s *dev;    /* Associated device */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tca9548a_write_config
+ *
+ * Description:
+ *   Write to the mux register of TCA9548A.
+ *
+ * Input Parameters:
+ *   priv     - A pointer to the TCA9548A device structure.
+ *   regvalue - The value that will be written.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_write_config(FAR struct tca9548a_dev_s *priv,
+                                 FAR uint8_t regvalue)
+{
+  struct i2c_config_s iconf;
+  int ret;
+
+  iconf.frequency = CONFIG_TCA9548A_I2C_FREQUENCY;
+  iconf.address   = priv->addr;
+  iconf.addrlen   = 7;
+
+  ret = i2c_write(priv->i2c, &iconf, &regvalue, 1);
+
+  i2cinfo("Write to address 0x%02X; register value: 0x%02x ret: %d\n",
+          priv->addr, regvalue, ret);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tca9548a_read_config
+ *
+ * Description:
+ *   Read the mux register from TCA9548A.
+ *
+ * Input Parameters:
+ *   priv     - A pointer to the TCA9548A device structure.
+ *   regvalue - A pointer to a buffer into which data will be received.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_read_config(FAR struct tca9548a_dev_s *priv,
+                                FAR uint8_t *regvalue)
+{
+  struct i2c_config_s iconf;
+  int ret;
+
+  iconf.frequency = CONFIG_TCA9548A_I2C_FREQUENCY;
+  iconf.address   = priv->addr;
+  iconf.addrlen   = 7;
+
+  ret = i2c_read(priv->i2c, &iconf, regvalue, 1);
+
+  i2cinfo("Read from address: 0x%02X; register value: 0x%02x ret: %d\n",
+          priv->addr, *regvalue, ret);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tca9548a_select_channel
+ *
+ * Description:
+ *   Helper function to select a channel in the TCA9548A. The TCA9548A allows
+ *   to select more than 1 channel at same time, but here we are forcing it
+ *   to only enable a single channel by time. It will avoid collision in case
+ *   where two devices with same address are connected to two channels.
+ *
+ * Input Parameters:
+ *   dev  - Pointer to the (virtual) i2c_master_s.
+ *   val  - The channel to be selected.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_select_channel(FAR struct tca9548a_dev_s *priv,
+                                   uint8_t val)
+{
+  if (val > TCA9548A_SEL_CH7)
+    {
+      /* channel not existent/supported */
+
+      return -EINVAL;
+    }
+
+  if ((priv->state & (1 << val)) != 0)
+    {
+      /* channel already selected */
+
+      return OK;
+    }
+
+  /* Modify state and write it to the mux. Selecting a channel always enables
+   * the device
+   */
+
+  priv->state = (1 << val);

Review comment:
       ```suggestion
     priv->state = 1 << val;
   ```

##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,145 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =
+                                                {
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL
+                                                };
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_tca9548a_initialize
+ *
+ * Description:
+ *   Initialize and register the TCA9548A Multiplexer.
+ *
+ * Input Parameters:
+ *   devno - The device number associated to I2C address, 0=0x70 ... 7=0x77
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_tca9548a_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  uint8_t addr;
+
+  i2cinfo("Initializing TCA9548A!\n");
+
+  /* TCA9548A valid addresses are 0:0x70 - 7:0x77 */
+
+  if (devno < TCA9548A_SEL_CH0 && devno > TCA9548A_SEL_CH7)
+    {
+      i2cerr("Invalid devno: %d! Valid range: 0 to 7!\n", devno);
+      return -EINVAL;
+    }
+
+  addr = TCA9548A_BASEADDR0 + devno;
+
+  /* Initialize TCA9548A */
+
+  i2c = esp32_i2cbus_initialize(busno);
+  if (i2c != NULL)
+    {
+      /* Then try to initialize the TCA9548A */
+
+      g_tca9448a_devs[devno] = tca9548a_initialize(i2c, addr);

Review comment:
       should we check if `g_tca9448a_devs[devno]` is not `NULL` before calling init to avoid double init or not?

##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,145 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =
+                                                {
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL
+                                                };
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_tca9548a_initialize
+ *
+ * Description:
+ *   Initialize and register the TCA9548A Multiplexer.
+ *
+ * Input Parameters:
+ *   devno - The device number associated to I2C address, 0=0x70 ... 7=0x77
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_tca9548a_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  uint8_t addr;
+
+  i2cinfo("Initializing TCA9548A!\n");
+
+  /* TCA9548A valid addresses are 0:0x70 - 7:0x77 */
+
+  if (devno < TCA9548A_SEL_CH0 && devno > TCA9548A_SEL_CH7)
+    {
+      i2cerr("Invalid devno: %d! Valid range: 0 to 7!\n", devno);
+      return -EINVAL;
+    }
+
+  addr = TCA9548A_BASEADDR0 + devno;

Review comment:
       This part and part in `esp32_i2cmux_getmaster` are really confusing to me. All the time compare with `TCA9548A_SEL_CH`.
   I would suggest to rework this into:
   ```suggestion
     if (devno < 0 || devno > (TCA9548A_BASEADDR7 - TCA9548A_BASEADDR0))
       {
         i2cerr("Invalid devno: %d! Valid range: 0 to 7!\n", devno);
         return -EINVAL;
       }
   
     addr = TCA9548A_BASEADDR0 + devno;
   ```
   And the same in `esp32_i2cmux_getmaster`
   
   or
   ```
   #define TCA9548A_DEV_CNT (TCA9548A_BASEADDR7 - TCA9548A_BASEADDR0 + 1)
   ```
   and use it in `FAR struct tca9548a_dev_s *g_tca9448a_devs[TCA9548A_DEV_CNT]` and here in check condition `if (devno < 0 || devno >= TCA9548A_DEV_CNT)`.
   
   Anyway even with current logic need a change from `&&` to `||`




-- 
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] a-lunev commented on a change in pull request #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: include/nuttx/i2c/tca9548a.h
##########
@@ -0,0 +1,126 @@
+/****************************************************************************
+ * include/nuttx/i2c/tca9548a.h
+ *
+ * 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_I2C_TCA9548A_H
+#define __INCLUDE_NUTTX_I2C_TCA9548A_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/ioctl.h>
+#include <nuttx/i2c/i2c_master.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* It is possible to config. TCA9548A to communicate from 0x70-0x77 */
+
+#define TCA9548A_BASEADDR0    0x70
+#define TCA9548A_BASEADDR1    0x71
+#define TCA9548A_BASEADDR2    0x72
+#define TCA9548A_BASEADDR3    0x73
+#define TCA9548A_BASEADDR4    0x74
+#define TCA9548A_BASEADDR5    0x75
+#define TCA9548A_BASEADDR6    0x76
+#define TCA9548A_BASEADDR7    0x77
+
+/* The TCA9548A can multiplex up to 8 channels */
+
+#define TCA9548A_SEL_CH0      0x0
+#define TCA9548A_SEL_CH1      0x1
+#define TCA9548A_SEL_CH2      0x2
+#define TCA9548A_SEL_CH3      0x3
+#define TCA9548A_SEL_CH4      0x4
+#define TCA9548A_SEL_CH5      0x5
+#define TCA9548A_SEL_CH6      0x6
+#define TCA9548A_SEL_CH7      0x7
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct tca9548a_dev_s
+{
+  FAR struct i2c_master_s *i2c;      /* I2C interface */
+  uint16_t addr;

Review comment:
       I am not sure is uint16_t required here? Is uint16_t unified for all I2C address in NuttX drivers? At least TCA9548A chip has only 7-bit address.




-- 
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 #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,145 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =
+                                                {
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL
+                                                };
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_tca9548a_initialize
+ *
+ * Description:
+ *   Initialize and register the TCA9548A Multiplexer.
+ *
+ * Input Parameters:
+ *   devno - The device number associated to I2C address, 0=0x70 ... 7=0x77
+ *   busno - The I2C bus number
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int board_tca9548a_initialize(int devno, int busno)
+{
+  struct i2c_master_s *i2c;
+  uint8_t addr;
+
+  i2cinfo("Initializing TCA9548A!\n");
+
+  /* TCA9548A valid addresses are 0:0x70 - 7:0x77 */
+
+  if (devno < TCA9548A_SEL_CH0 && devno > TCA9548A_SEL_CH7)
+    {
+      i2cerr("Invalid devno: %d! Valid range: 0 to 7!\n", devno);
+      return -EINVAL;
+    }
+
+  addr = TCA9548A_BASEADDR0 + devno;

Review comment:
       Done! Thank you!




-- 
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 #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: boards/xtensa/esp32/common/src/esp32_tca9548a.c
##########
@@ -0,0 +1,145 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_tca9548a.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#include "esp32_board_i2c.h"
+#include "esp32_i2c.h"
+#include "esp32_tca9548a.h"
+
+FAR struct tca9548a_dev_s *g_tca9448a_devs[8] =
+                                                {
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL,
+                                                  NULL
+                                                };

Review comment:
       hmm, if I move it to global it will be allocated on bss and will be zeroed by default. Anyway I think it is save to initialize as NULL to avoid erratic behavior of compiler.




-- 
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] a-lunev commented on a change in pull request #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: drivers/i2c/tca9548a.c
##########
@@ -0,0 +1,404 @@
+/****************************************************************************
+ * drivers/i2c/tca9548a.c
+ * Driver for the TCA9448A i2c multiplexer
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#ifdef CONFIG_I2CMULTIPLEXER_TCA9548A
+
+#ifndef CONFIG_TCA9548A_I2C_FREQUENCY
+#  define CONFIG_TCA9548A_I2C_FREQUENCY    400000
+#endif
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* I2C Helpers */
+
+static int tca9548a_write_config(FAR struct tca9548a_dev_s *priv,
+                                 FAR uint8_t regvalue);
+static int tca9548a_read_config(FAR struct tca9548a_dev_s *priv,
+                                FAR uint8_t *regvalue);
+
+/* Other helpers */
+
+static int tca9548a_select_channel(FAR struct tca9548a_dev_s *priv,
+                                   uint8_t val);
+
+/* I2C multiplexer vtable */
+
+static int tca9548a_transfer_on_channel (FAR struct i2c_master_s *dev,
+                                         FAR struct i2c_msg_s *msgs,
+                                         int count);
+#ifdef CONFIG_I2C_RESET
+static int tca9548a_reset_on_channel (FAR struct i2c_master_s *dev);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct i2c_ops_s g_i2cmux_ops =
+{
+  tca9548a_transfer_on_channel
+#ifdef CONFIG_I2C_RESET
+  , tca9548a_reset_on_channel
+#endif
+};
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct i2c_channel_dev_s
+{
+  FAR struct i2c_master_s vi2c;      /* Nested structure to allow casting as
+                                      * public i2c master */
+  uint8_t channel;                   /* Associated channel on the mux */
+  FAR struct tca9548a_dev_s *dev;    /* Associated device */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tca9548a_write_config
+ *
+ * Description:
+ *   Write to the mux register of TCA9548A.
+ *
+ * Input Parameters:
+ *   priv     - A pointer to the TCA9548A device structure.
+ *   regvalue - The value that will be written.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_write_config(FAR struct tca9548a_dev_s *priv,
+                                 FAR uint8_t regvalue)

Review comment:
       Is the FAR required?

##########
File path: include/nuttx/i2c/tca9548a.h
##########
@@ -0,0 +1,126 @@
+/****************************************************************************
+ * include/nuttx/i2c/tca9548a.h
+ *
+ * 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_I2C_TCA9548A_H
+#define __INCLUDE_NUTTX_I2C_TCA9548A_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/ioctl.h>
+#include <nuttx/i2c/i2c_master.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* It is possible to config. TCA9548A to communicate from 0x70-0x77 */
+
+#define TCA9548A_BASEADDR0    0x70
+#define TCA9548A_BASEADDR1    0x71
+#define TCA9548A_BASEADDR2    0x72
+#define TCA9548A_BASEADDR3    0x73
+#define TCA9548A_BASEADDR4    0x74
+#define TCA9548A_BASEADDR5    0x75
+#define TCA9548A_BASEADDR6    0x76
+#define TCA9548A_BASEADDR7    0x77
+
+/* The TCA9548A can multiplex up to 8 channels */
+
+#define TCA9548A_SEL_CH0      0x0
+#define TCA9548A_SEL_CH1      0x1
+#define TCA9548A_SEL_CH2      0x2
+#define TCA9548A_SEL_CH3      0x3
+#define TCA9548A_SEL_CH4      0x4
+#define TCA9548A_SEL_CH5      0x5
+#define TCA9548A_SEL_CH6      0x6
+#define TCA9548A_SEL_CH7      0x7
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+struct tca9548a_dev_s
+{
+  FAR struct i2c_master_s *i2c;      /* I2C interface */
+  uint16_t addr;

Review comment:
       I am not sure is uint16_t required here? Is uint16_t unified for all I2C address in drivers in NuttX? At least TCA9548A chip has only 7-bit address.

##########
File path: drivers/i2c/tca9548a.c
##########
@@ -0,0 +1,404 @@
+/****************************************************************************
+ * drivers/i2c/tca9548a.c
+ * Driver for the TCA9448A i2c multiplexer
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#ifdef CONFIG_I2CMULTIPLEXER_TCA9548A
+
+#ifndef CONFIG_TCA9548A_I2C_FREQUENCY
+#  define CONFIG_TCA9548A_I2C_FREQUENCY    400000
+#endif
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* I2C Helpers */
+
+static int tca9548a_write_config(FAR struct tca9548a_dev_s *priv,
+                                 FAR uint8_t regvalue);
+static int tca9548a_read_config(FAR struct tca9548a_dev_s *priv,
+                                FAR uint8_t *regvalue);
+
+/* Other helpers */
+
+static int tca9548a_select_channel(FAR struct tca9548a_dev_s *priv,
+                                   uint8_t val);
+
+/* I2C multiplexer vtable */
+
+static int tca9548a_transfer_on_channel (FAR struct i2c_master_s *dev,
+                                         FAR struct i2c_msg_s *msgs,
+                                         int count);
+#ifdef CONFIG_I2C_RESET
+static int tca9548a_reset_on_channel (FAR struct i2c_master_s *dev);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct i2c_ops_s g_i2cmux_ops =
+{
+  tca9548a_transfer_on_channel
+#ifdef CONFIG_I2C_RESET
+  , tca9548a_reset_on_channel
+#endif
+};
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct i2c_channel_dev_s
+{
+  FAR struct i2c_master_s vi2c;      /* Nested structure to allow casting as
+                                      * public i2c master */
+  uint8_t channel;                   /* Associated channel on the mux */
+  FAR struct tca9548a_dev_s *dev;    /* Associated device */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tca9548a_write_config
+ *
+ * Description:
+ *   Write to the mux register of TCA9548A.
+ *
+ * Input Parameters:
+ *   priv     - A pointer to the TCA9548A device structure.
+ *   regvalue - The value that will be written.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_write_config(FAR struct tca9548a_dev_s *priv,
+                                 FAR uint8_t regvalue)
+{
+  struct i2c_config_s iconf;
+  int ret;
+
+  iconf.frequency = CONFIG_TCA9548A_I2C_FREQUENCY;
+  iconf.address   = priv->addr;
+  iconf.addrlen   = 7;
+
+  ret = i2c_write(priv->i2c, &iconf, &regvalue, 1);
+
+  i2cinfo("Write to address 0x%02X; register value: 0x%02x ret: %d\n",
+          priv->addr, regvalue, ret);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tca9548a_read_config
+ *
+ * Description:
+ *   Read the mux register from TCA9548A.
+ *
+ * Input Parameters:
+ *   priv     - A pointer to the TCA9548A device structure.
+ *   regvalue - A pointer to a buffer into which data will be received.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_read_config(FAR struct tca9548a_dev_s *priv,
+                                FAR uint8_t *regvalue)
+{
+  struct i2c_config_s iconf;
+  int ret;
+
+  iconf.frequency = CONFIG_TCA9548A_I2C_FREQUENCY;
+  iconf.address   = priv->addr;
+  iconf.addrlen   = 7;
+
+  ret = i2c_read(priv->i2c, &iconf, regvalue, 1);
+
+  i2cinfo("Read from address: 0x%02X; register value: 0x%02x ret: %d\n",
+          priv->addr, *regvalue, ret);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tca9548a_select_channel
+ *
+ * Description:
+ *   Helper function to select a channel in the TCA9548A. The TCA9548A allows
+ *   to select more than 1 channel at same time, but here we are forcing it
+ *   to only enable a single channel by time. It will avoid collision in case
+ *   where two devices with same address are connected to two channels.
+ *
+ * Input Parameters:
+ *   dev  - Pointer to the (virtual) i2c_master_s.
+ *   val  - The channel to be selected.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_select_channel(FAR struct tca9548a_dev_s *priv,
+                                   uint8_t val)
+{
+  if (val > TCA9548A_SEL_CH7)
+    {
+      /* channel not existent/supported */
+
+      return -EINVAL;
+    }
+
+  if ((priv->state & (1 << val)) != 0)
+    {
+      /* channel already selected */
+
+      return OK;
+    }
+
+  /* Modify state and write it to the mux. Selecting a channel always enables
+   * the device
+   */
+
+  priv->state = (1 << val);

Review comment:
       It should be overwritten only if tca9548a_write_config(priv, priv->state) is successful.




-- 
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 #5376: i2c: Add TCA9548A I2C Multiplexer

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



##########
File path: drivers/i2c/tca9548a.c
##########
@@ -0,0 +1,404 @@
+/****************************************************************************
+ * drivers/i2c/tca9548a.c
+ * Driver for the TCA9448A i2c multiplexer
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdlib.h>
+#include <fixedmath.h>
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/tca9548a.h>
+
+#ifdef CONFIG_I2CMULTIPLEXER_TCA9548A
+
+#ifndef CONFIG_TCA9548A_I2C_FREQUENCY
+#  define CONFIG_TCA9548A_I2C_FREQUENCY    400000
+#endif
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* I2C Helpers */
+
+static int tca9548a_write_config(FAR struct tca9548a_dev_s *priv,
+                                 FAR uint8_t regvalue);
+static int tca9548a_read_config(FAR struct tca9548a_dev_s *priv,
+                                FAR uint8_t *regvalue);
+
+/* Other helpers */
+
+static int tca9548a_select_channel(FAR struct tca9548a_dev_s *priv,
+                                   uint8_t val);
+
+/* I2C multiplexer vtable */
+
+static int tca9548a_transfer_on_channel (FAR struct i2c_master_s *dev,
+                                         FAR struct i2c_msg_s *msgs,
+                                         int count);
+#ifdef CONFIG_I2C_RESET
+static int tca9548a_reset_on_channel (FAR struct i2c_master_s *dev);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct i2c_ops_s g_i2cmux_ops =
+{
+  tca9548a_transfer_on_channel
+#ifdef CONFIG_I2C_RESET
+  , tca9548a_reset_on_channel
+#endif
+};
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct i2c_channel_dev_s
+{
+  FAR struct i2c_master_s vi2c;      /* Nested structure to allow casting as
+                                      * public i2c master */
+  uint8_t channel;                   /* Associated channel on the mux */
+  FAR struct tca9548a_dev_s *dev;    /* Associated device */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tca9548a_write_config
+ *
+ * Description:
+ *   Write to the mux register of TCA9548A.
+ *
+ * Input Parameters:
+ *   priv     - A pointer to the TCA9548A device structure.
+ *   regvalue - The value that will be written.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_write_config(FAR struct tca9548a_dev_s *priv,
+                                 FAR uint8_t regvalue)
+{
+  struct i2c_config_s iconf;
+  int ret;
+
+  iconf.frequency = CONFIG_TCA9548A_I2C_FREQUENCY;
+  iconf.address   = priv->addr;
+  iconf.addrlen   = 7;
+
+  ret = i2c_write(priv->i2c, &iconf, &regvalue, 1);
+
+  i2cinfo("Write to address 0x%02X; register value: 0x%02x ret: %d\n",
+          priv->addr, regvalue, ret);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tca9548a_read_config
+ *
+ * Description:
+ *   Read the mux register from TCA9548A.
+ *
+ * Input Parameters:
+ *   priv     - A pointer to the TCA9548A device structure.
+ *   regvalue - A pointer to a buffer into which data will be received.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_read_config(FAR struct tca9548a_dev_s *priv,
+                                FAR uint8_t *regvalue)
+{
+  struct i2c_config_s iconf;
+  int ret;
+
+  iconf.frequency = CONFIG_TCA9548A_I2C_FREQUENCY;
+  iconf.address   = priv->addr;
+  iconf.addrlen   = 7;
+
+  ret = i2c_read(priv->i2c, &iconf, regvalue, 1);
+
+  i2cinfo("Read from address: 0x%02X; register value: 0x%02x ret: %d\n",
+          priv->addr, *regvalue, ret);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: tca9548a_select_channel
+ *
+ * Description:
+ *   Helper function to select a channel in the TCA9548A. The TCA9548A allows
+ *   to select more than 1 channel at same time, but here we are forcing it
+ *   to only enable a single channel by time. It will avoid collision in case
+ *   where two devices with same address are connected to two channels.
+ *
+ * Input Parameters:
+ *   dev  - Pointer to the (virtual) i2c_master_s.
+ *   val  - The channel to be selected.
+ *
+ * Returned Value:
+ *   OK on success or a negative error.
+ *
+ ****************************************************************************/
+
+static int tca9548a_select_channel(FAR struct tca9548a_dev_s *priv,
+                                   uint8_t val)
+{
+  if (val > TCA9548A_SEL_CH7)
+    {
+      /* channel not existent/supported */
+
+      return -EINVAL;
+    }
+
+  if ((priv->state & (1 << val)) != 0)
+    {
+      /* channel already selected */
+
+      return OK;
+    }
+
+  /* Modify state and write it to the mux. Selecting a channel always enables
+   * the device
+   */
+
+  priv->state = (1 << val);

Review comment:
       True! 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