linguini1 commented on code in PR #18397:
URL: https://github.com/apache/nuttx/pull/18397#discussion_r2810016528


##########
boards/arm/stm32h7/nucleo-h753zi/src/stm32_i2c.c:
##########
@@ -0,0 +1,646 @@
+/****************************************************************************
+ * boards/arm/stm32h7/nucleo-h753zi/src/stm32_i2c.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <stddef.h>
+#include <stdbool.h>
+#include <string.h>
+#include <errno.h>
+#include <syslog.h>
+#include <stdio.h>
+
+#include "stm32_i2c.h"
+#include <nuttx/i2c/i2c_master.h>
+#include <arch/board/board.h>
+
+#include "nucleo-h753zi.h"
+
+#ifdef CONFIG_STM32H7_I2C
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MAX_I2C_DEVICES_PER_BUS 16
+#define INVALID_I2C_ADDR 0xff
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* I2C device registration structure */
+
+struct i2c_device_s
+{
+  uint8_t addr;           /* I2C slave address (7-bit) */
+  uint32_t frequency;     /* Bus frequency for this device */
+  char name[16];          /* Device instance name (for logging) */
+  bool in_use;            /* true = slot occupied */
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* I2C master interfaces (cached after initialization) */
+
+#ifdef CONFIG_STM32H7_I2C1
+static struct i2c_master_s *g_i2c1_master = NULL;
+static struct i2c_device_s g_i2c1_devices[MAX_I2C_DEVICES_PER_BUS];
+#endif
+
+#ifdef CONFIG_STM32H7_I2C2
+static struct i2c_master_s *g_i2c2_master = NULL;
+static struct i2c_device_s g_i2c2_devices[MAX_I2C_DEVICES_PER_BUS];
+#endif
+
+#ifdef CONFIG_STM32H7_I2C3
+static struct i2c_master_s *g_i2c3_master = NULL;
+static struct i2c_device_s g_i2c3_devices[MAX_I2C_DEVICES_PER_BUS];
+#endif
+
+#ifdef CONFIG_STM32H7_I2C4
+static struct i2c_master_s *g_i2c4_master = NULL;
+static struct i2c_device_s g_i2c4_devices[MAX_I2C_DEVICES_PER_BUS];
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: get_i2c_master_ptr
+ *
+ * Description:
+ *   Get pointer to I2C master interface storage for a specific bus.
+ *
+ * Input Parameters:
+ *   i2c_bus - I2C bus number (1-4)
+ *
+ * Returned Value:
+ *   Pointer to I2C master pointer storage, NULL if invalid bus
+ *
+ ****************************************************************************/
+
+static struct i2c_master_s **get_i2c_master_ptr(int i2c_bus)
+{
+  switch (i2c_bus)
+    {
+#ifdef CONFIG_STM32H7_I2C1
+      case 1:
+        return &g_i2c1_master;
+#endif
+#ifdef CONFIG_STM32H7_I2C2
+      case 2:
+        return &g_i2c2_master;
+#endif
+#ifdef CONFIG_STM32H7_I2C3
+      case 3:
+        return &g_i2c3_master;
+#endif
+#ifdef CONFIG_STM32H7_I2C4
+      case 4:
+        return &g_i2c4_master;
+#endif
+      default:
+        return NULL;
+    }
+}
+
+/****************************************************************************
+ * Name: get_i2c_devices_array
+ *
+ * Description:
+ *   Get I2C devices array for a specific bus.
+ *
+ * Input Parameters:
+ *   i2c_bus - I2C bus number (1-4)
+ *
+ * Returned Value:
+ *   Pointer to I2C devices array, NULL if invalid bus
+ *
+ ****************************************************************************/
+
+static struct i2c_device_s *get_i2c_devices_array(int i2c_bus)
+{
+  switch (i2c_bus)
+    {
+#ifdef CONFIG_STM32H7_I2C1
+      case 1:
+        return g_i2c1_devices;
+#endif
+#ifdef CONFIG_STM32H7_I2C2
+      case 2:
+        return g_i2c2_devices;
+#endif
+#ifdef CONFIG_STM32H7_I2C3
+      case 3:
+        return g_i2c3_devices;
+#endif
+#ifdef CONFIG_STM32H7_I2C4
+      case 4:
+        return g_i2c4_devices;
+#endif
+      default:
+        return NULL;
+    }
+}
+
+/****************************************************************************
+ * Name: validate_i2c_addr
+ *
+ * Description:
+ *   Validate I2C 7-bit address.
+ *   Reserved addresses: 0x00-0x07, 0x78-0x7F
+ *
+ * Input Parameters:
+ *   addr - I2C address to validate
+ *
+ * Returned Value:
+ *   true if valid, false if reserved or out of range
+ *
+ ****************************************************************************/
+
+static bool validate_i2c_addr(uint8_t addr)
+{
+  /* I2C 7-bit addressing:
+   * 0x00-0x07: Reserved addresses
+   * 0x08-0x77: Valid addresses
+   * 0x78-0x7F: Reserved addresses
+   */
+
+  if (addr < 0x08 || addr > 0x77)
+    {
+      return false;
+    }
+
+  return true;
+}
+
+/****************************************************************************
+ * Name: find_device_slot
+ *
+ * Description:
+ *   Find a free slot in the I2C devices array.
+ *
+ * Input Parameters:
+ *   devices - Pointer to I2C devices array
+ *
+ * Returned Value:
+ *   Index of free slot, -ENOMEM if no slots available
+ *
+ ****************************************************************************/
+
+static int find_device_slot(struct i2c_device_s *devices)
+{
+  int i;
+
+  for (i = 0; i < MAX_I2C_DEVICES_PER_BUS; i++)
+    {
+      if (!devices[i].in_use)
+        {
+          return i;
+        }
+    }
+
+  return -ENOMEM;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: stm32_i2c_initialize
+ *
+ * Description:
+ *   Initialize I2C buses based on Kconfig configuration.
+ *   Pins are selected via board.h pinset configuration.
+ *
+ * Returned Value:
+ *   OK on success, negative errno on error
+ *
+ ****************************************************************************/
+
+int stm32_i2c_initialize(void)
+{
+  syslog(LOG_INFO, "Initializing I2C interfaces\n");
+
+#ifdef CONFIG_NUCLEO_H753ZI_I2C1_ENABLE
+  g_i2c1_master = stm32_i2cbus_initialize(1);
+  if (!g_i2c1_master)
+    {
+      syslog(LOG_ERR, "ERROR: Failed to initialize I2C1\n");
+      return -ENODEV;
+    }
+
+  memset(g_i2c1_devices, 0, sizeof(g_i2c1_devices));
+  syslog(LOG_INFO, "I2C1 initialized successfully\n");
+#endif
+
+#ifdef CONFIG_NUCLEO_H753ZI_I2C2_ENABLE
+  g_i2c2_master = stm32_i2cbus_initialize(2);
+  if (!g_i2c2_master)
+    {
+      syslog(LOG_ERR, "ERROR: Failed to initialize I2C2\n");
+      return -ENODEV;
+    }
+
+  memset(g_i2c2_devices, 0, sizeof(g_i2c2_devices));
+  syslog(LOG_INFO, "I2C2 initialized successfully\n");
+#endif
+
+#ifdef CONFIG_NUCLEO_H753ZI_I2C3_ENABLE
+  g_i2c3_master = stm32_i2cbus_initialize(3);
+  if (!g_i2c3_master)
+    {
+      syslog(LOG_ERR, "ERROR: Failed to initialize I2C3\n");
+      return -ENODEV;
+    }
+
+  memset(g_i2c3_devices, 0, sizeof(g_i2c3_devices));
+  syslog(LOG_INFO, "I2C3 initialized successfully\n");
+#endif
+
+#ifdef CONFIG_NUCLEO_H753ZI_I2C4_ENABLE
+  g_i2c4_master = stm32_i2cbus_initialize(4);
+  if (!g_i2c4_master)
+    {
+      syslog(LOG_ERR, "ERROR: Failed to initialize I2C4\n");
+      return -ENODEV;
+    }
+
+  memset(g_i2c4_devices, 0, sizeof(g_i2c4_devices));
+  syslog(LOG_INFO, "I2C4 initialized successfully\n");
+#endif
+
+  syslog(LOG_INFO, "I2C initialization completed\n");
+  return OK;
+}
+
+/****************************************************************************
+ * Name: stm32_i2c_register_device
+ *
+ * Description:
+ *   Register an I2C device with specific address, frequency, and name.
+ *   This function tracks device configurations for debugging and
+ *   provides validation.
+ *
+ * Input Parameters:
+ *   i2c_bus   - I2C bus number (1-4)
+ *   addr      - I2C slave address (7-bit, 0x08-0x77)
+ *   frequency - Bus frequency for this device (Hz)
+ *   name      - Descriptive name for logging (can be NULL)
+ *
+ * Returned Value:
+ *   OK on success, negative errno on error
+ *
+ ****************************************************************************/
+
+int stm32_i2c_register_device(int i2c_bus, uint8_t addr,
+                               uint32_t frequency, const char *name)
+{
+  struct i2c_device_s *devices;
+  struct i2c_device_s *device;
+  int slot;
+  int i;
+
+  /* Validate bus */
+
+  devices = get_i2c_devices_array(i2c_bus);
+  if (!devices)
+    {
+      syslog(LOG_ERR, "ERROR: Invalid I2C bus %d\n", i2c_bus);
+      return -EINVAL;
+    }
+
+  /* Validate address */
+
+  if (!validate_i2c_addr(addr))
+    {
+      syslog(LOG_ERR,
+             "ERROR: Invalid I2C address 0x%02x "

Review Comment:
   > Please, could you provide a documentation about it? Thank you!
   
   Good idea. From your perspective as a contributor, do you think this would 
be best put under this page? 
https://nuttx.apache.org/docs/latest/contributing/coding_style.html



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

To unsubscribe, e-mail: [email protected]

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

Reply via email to