qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Corey Minyard <minyard@acm.org>
To: Paz Offer <poffer@nvidia.com>
Cc: "qemu-devel@nongnu.org" <qemu-devel@nongnu.org>
Subject: Re: Trying to write data to i2c bus
Date: Mon, 19 Feb 2024 10:32:17 -0600	[thread overview]
Message-ID: <ZdOCkeotwf3EF11u@mail.minyard.net> (raw)
In-Reply-To: <MW4PR12MB6997A0A111FB6999ACF87F26A0512@MW4PR12MB6997.namprd12.prod.outlook.com>

On Mon, Feb 19, 2024 at 01:49:44PM +0000, Paz Offer wrote:
> Hi,
> 
> I am new to QEMU development, so please excuse if I my direction here is wrong:
> 
> I am trying to implement an i2c slave device.
> My device should be able to read/write data from its i2c bus.
> 
> I defined my device-state object like so:
> 
>     typedef struct {
>           I2CSlave i2c;
>           void     *my_data;
> 
>     }   MyI2CSlave;
> 
> 
> In my implementation occasionally I may have to send data on the bus, due to an internal event on my side.
> For this I implemented the following code:
> 
>     //  Get bus pointer:
>     BusState *parentBus = qdev_get_parent_bus(DEVICE(&obj->i2c));
>     I2CBus   *i2cBus    = I2C_BUS(parentBus);
>       
>     //  Try to send data on bus:  
>     if (i2c_start_send(i2cBus, address)) {
>         //  error?
>         return;
>     }
>     for (int i = 0; i < size; i++) {
>         i2c_send(i2cBus, data[i]);
>     }
>     i2c_end_transfer(i2cBus);
> 
>       
> The problem is that 'i2c_start_send()' always fails here:
> 
>     if (QLIST_EMPTY(&bus->current_devs)) {
>         return 1;
>     }
> 
> The member 'i2cBus->current_devs.lh_first' is always null.
> 
> I will add that in my QEMU execution I specify the bus 'aspeed.i2c.bus.0' to be used with my device.
> In my 'realize' method I can see that a bus is connected to my device, as 'qdev_get_parent_bus()' does return a valid pointer.
> 
> My question:
> 1. Am I missing some initialization for the bus?
> 2. Is there other way to send data on the i2c bus, assuming it can happen anytime due to an internal event on my device side?

You are missing a lot of stuff, but let's start with basics...

First of all, this is not how i2c generally works.  Generally you have a
bus master, the host, that reads and writes to slave devices on the bus.
The devices on the bus don't asynchronously send data to the host.

That said, you can have multiple bus masters on the bus.  If that's what
you are doing, what are you sending your data to?  You have to have
something that will receive it.

If you have some slave device that has data it's holding for the host,
the host has to fetch it.  You could have an interrupt that comes from
the slave device saying it has data, and there's something called SMBus
alert that can consolidate interrupts (though it's not implemented in
QEMU at the moment).

Are you simulating some real device here?

-corey

> 
> Thanks for any tip,
> Pazo  


  reply	other threads:[~2024-02-19 16:33 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-19 13:49 Trying to write data to i2c bus Paz Offer
2024-02-19 16:32 ` Corey Minyard [this message]
2024-02-19 16:53   ` Paz Offer
2024-02-19 17:14     ` Corey Minyard
2024-02-19 18:26       ` Cédric Le Goater
2024-02-25  8:29       ` Paz Offer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZdOCkeotwf3EF11u@mail.minyard.net \
    --to=minyard@acm.org \
    --cc=poffer@nvidia.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).