Home > Software engineering >  How to use linux i2c driver to flash a binary
How to use linux i2c driver to flash a binary

Time:07-26

I was told to use this driver to flash a binary to some chip via I2C.

I'm not interested in customizing the driver or modifying in any way really, I'm just hoping to learn enough to use the max77958_update_action_mtp function:

max77958_update_action_mtp(struct max77958_dev *max77958,
        const u8 *action_bin, int fw_bin_len)

to flash a binary (action_bin) to some i2c device (max77958).

I am a complete noob to embedded development and c programming, but from what I can understand I need to manage the following:

  1. Include this driver in some other c file and resolve any missing dependencies (done)
  2. Create some max77958_dev and pass the pointer to this function as the first argument.
  3. Open the binary as a const u8 and pass the pointer as the second argument.

Have I misunderstood the steps I need to finish?

Assuming I have understood the steps I need to take correctly:

Step 1

I managed to find all the header files in my system under /usr/src/linux-headers-5.4.0-122-generic/ other than one called <linux/wakelock.h> which from what I can tell is some Android specific header, so I commented it out and will try to remove anything the compiler complains about as I shouldn't need anything Android related.

Step 2

Does it look like there is some default max77958_dev struct that I can use? Following the definition of the struct in VS Code, I can see its definition is located here but I'm too much of a noob in c to understand if something here helps me create a default max77958_dev struct with some default values or if I have to somehow know all of the correct values and create one myself.

I'm also quite oblivious as to how the Ubuntu PC would connect to this I2C device. I assume I can get some USB-2-I2C dongle from FTDI or the like, but how would my C code know to communicate via this dongle to the I2C slave?

Step 3

I think I can figure out how to read in a binary file, but it seems strange that this is a const u8. The binary is surely larger than 8 bits. Guessing this is a lack of understanding of pointers, but can someone clarify how exactly I should introduce a binary file to this function?

CodePudding user response:

I was told to use this driver to flash a binary to some chip via I2C.

From what I see this driver already pushes the firmware. But if I have correctly understood you are going totally different direction. Trying to re-use some parts of the code from device driver in your user-space application is obviously not going to work.

Instead, build this driver as a module and load on your target machine with MAX77958 connected to one of its I2C busses. How you can instantiate device is another question and is architecture dependent - if you are going to use Ubuntu on x86 you can do this manually from user-space.

CodePudding user response:

On step 3: You pass a pointer to as many u8 as fw_bin_len says. Such a pointer can point to a single byte, but also to the first of an array of bytes. C cannot differentiate between such usages.

  • Related