trns1997 commented on code in PR #10450: URL: https://github.com/apache/nuttx/pull/10450#discussion_r1310582401
########## arch/arm/src/mx8mp/mx8mp_i2c.c: ########## @@ -0,0 +1,819 @@ +/**************************************************************************** + * arch/arm/src/mx8mp/mx8mp_i2c.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/i2c/i2c_master.h> +#include <nuttx/signal.h> + +#include "mx8mp_i2c.h" +#include "mx8mp_ccm.h" +#include "hardware/mx8mp_memorymap.h" + +#include <debug.h> +#include "arm_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define I2C_STALL_TIMEOUT MSEC2TICK(10) /* 10 ms */ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +struct mx8mp_i2c_s; + +static bool is_busy(struct mx8mp_i2c_s *priv); +static void mx8mp_i2c_enable(struct mx8mp_i2c_s *priv); +static void mx8mp_i2c_disable(struct mx8mp_i2c_s *priv); +static int mx8mp_i2c_interrupt(int irq, void *context, void *arg); +static void mx8mp_i2c_reset_bus(struct mx8mp_i2c_s *dev); + +/* I2C lower half driver methods */ + +static int mx8mp_i2c_transfer(struct i2c_master_s *dev, + struct i2c_msg_s *msgs, int count); +static int mx8mp_i2c_reset(struct i2c_master_s *dev); + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* I2C Device Private Data */ + +struct mx8mp_i2c_s +{ + /* Generic I2C device */ + + struct i2c_master_s dev; + + /* Port configuration */ + + uint32_t base; /* I2C base address */ + int clock; /* Peripheral clock as described in hardware/mx8mp_ccm.h */ + uint16_t irq; /* IRQ number */ + uint32_t frequency; /* Current I2C frequency */ + int refs; /* Reference count */ + mutex_t lock; /* Only one thread can access at a time */ + sem_t wait; /* IRQ wait sync */ +}; + +/* I2C lower half driver operations */ + +static const struct i2c_ops_s mx8mp_i2c_ops = +{ + .transfer = mx8mp_i2c_transfer +#ifdef CONFIG_I2C_RESET + , .reset = mx8mp_i2c_reset +#endif +}; + +/* I2C device structures */ + +#ifdef CONFIG_MX8MP_I2C1 +static struct mx8mp_i2c_s g_i2c1_dev = +{ + .dev.ops = &mx8mp_i2c_ops, + .base = MX8M_I2C1, + .clock = I2C1_CLK_ROOT, + .irq = MX8MP_IRQ_I2C1, + .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .wait = SEM_INITIALIZER(0), +}; +#endif + +#ifdef CONFIG_MX8MP_I2C2 +static struct mx8mp_i2c_s g_i2c2_dev = +{ + .dev.ops = &mx8mp_i2c_ops, + .base = MX8M_I2C2, + .clock = I2C2_CLK_ROOT, + .irq = MX8MP_IRQ_I2C2, + .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .wait = SEM_INITIALIZER(0), +}; +#endif + +#ifdef CONFIG_MX8MP_I2C3 +static struct mx8mp_i2c_s g_i2c3_dev = +{ + .dev.ops = &mx8mp_i2c_ops, + .base = MX8M_I2C3, + .clock = I2C3_CLK_ROOT, + .irq = MX8MP_IRQ_I2C3, + .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .wait = SEM_INITIALIZER(0), +}; +#endif + +#ifdef CONFIG_MX8MP_I2C4 +static struct mx8mp_i2c_s g_i2c4_dev = +{ + .dev.ops = &mx8mp_i2c_ops, + .base = MX8M_I2C4, + .clock = I2C4_CLK_ROOT, + .irq = MX8MP_IRQ_I2C4, + .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .wait = SEM_INITIALIZER(0), +}; +#endif + +#ifdef CONFIG_MX8MP_I2C5 +static struct mx8mp_i2c_s g_i2c5_dev = +{ + .dev.ops = &mx8mp_i2c_ops, + .base = MX8M_I2C5, + .clock = I2C5_CLK_ROOT, + .irq = MX8MP_IRQ_I2C5, + .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .wait = SEM_INITIALIZER(0), +}; +#endif + +#ifdef CONFIG_MX8MP_I2C6 +static struct mx8mp_i2c_s g_i2c6_dev = +{ + .dev.ops = &mx8mp_i2c_ops, + .base = MX8M_I2C6, + .clock = I2C6_CLK_ROOT, + .irq = MX8MP_IRQ_I2C6, + .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .wait = SEM_INITIALIZER(0), +}; +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const uint16_t DIVIDERS_MAP[] = +{ + 30, 32, 36, 42, 48, 52, 60, 72, + 80, 88, 104, 128, 144, 160, 192, 240, + 288, 320, 384, 480, 576, 640, 768, 960, + 1152, 1280, 1536, 1920, 2304, 2560, 3072, 3840, + 22, 24, 26, 28, 32, 36, 40, 44, + 48, 56, 64, 72, 80, 96, 112, 128, + 160, 192, 224, 256, 320, 384, 448, 512, + 640, 768, 896, 1024, 1280, 1536, 1792, 2048 +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: kinetis_i2c_init + * + * Description: + * Setup the I2C hardware, ready for operation with defaults + * + ****************************************************************************/ + +static int mx8mp_i2c_init(struct mx8mp_i2c_s *priv) +{ + /* enable peripheral */ + + mx8mp_i2c_enable(priv); + + /* Attach Interrupt Handler */ + + irq_attach(priv->irq, mx8mp_i2c_interrupt, priv); + + /* Enable Interrupt Handler */ + + up_enable_irq(priv->irq); + + /* Force a frequency update */ + + priv->frequency = 0; + + return 0; +} + +/**************************************************************************** + * Name: kinetis_i2c_deinit Review Comment: typo: replace kinetis with mx8mp -- 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]
