asuol / RTEMS_rpi_testing

Test cases and drivers for the Raspberry Pi BSP on RTEMS
3 stars 2 forks source link

I2C device driver #2

Closed giorgiobasile closed 3 years ago

giorgiobasile commented 3 years ago

Hi, first of all, thank you for setting up this repository and your blog posts, they are really helpful. I'm trying to work with your I2C device driver, although I'm actually working with an MCP3425.

I read in your README that "The device driver is to be put in the libchip/I2C RTEMS directory, and must also be referred in the libchip Makefile.".

I'm working with RTEMS v5.1 and in my RTEMS host environment, I installed the raspberrypi2 BSP. Therefore, I have a directory arm-rtems5/raspberrypi2/lib/include/libchip, but inside I can find neither an i2c/ directory nor a Makefile.

Probably things have changed a bit since you wrote your README, but maybe you have some suggestion on this? And, in general, why is it necessary to do these operations? Could you elaborate a little bit on that or send me any useful reference?

Thanks in advance for your help!

asuol commented 3 years ago

Hello Giorgio,

Stuff has changed places a bit in the RTEMS source tree in the last 5 years. The drivers in this repository were originally used with RTEMS 4.11, so the README is a bit out-of-date.

RTEMS stayed in version 4.11 for a few years and only somewhat recently moved to version 5, and with this version bump the tree structure has been updated.

The device drivers in this repository were developed so I could test the rpi BSP I2C and SPI drivers while connected with real hardware through the buses. The SPI and I2C drivers were integrated on the RTEMS repository, but the device drivers were kept out-of-tree (in this repository) as only I had these specific hardware (mcp23008 and 23k256) and it was not relevant for the community to have and maintain these drivers in the RTEMS tree.

Therefore to use these device drivers with RTEMS, you need to copy the sources into the RTEMS source tree so RTEMS can be compiled with them and you can use them from your RTEMS application.

Looking at the current RTEMS source tree, the current procedure should be:

To use the driver from your application just include it with

#include <libchip/mcp23008.h>

along with the <bsp/i2c.h> to include the raspberry I2C driver as shown in the test case (I2C_MCP23008_TEST) provided.

The test may not work directly as the test framework and libraries have likely changed since then, but the driver operation is still the same so it should be enough for you to see how to operate the driver.

I have not tested the proposed changes above on RTEMS 5, but this should work, or at least nudge you in the right direction I hope :)

Let me know how it goes

giorgiobasile commented 3 years ago

I see, so it has to be included in the source code and then built with RSB. It is probably a silly question, but I just wonder whether it is feasible to just include the driver in my RTEMS test application to make it work, or if doing that will result in a failure (especially when the device driver references to the i2c driver i.e. I currently have an error when calling i2c_dev_alloc_and_init)

I'm just interested in understanding whether compiling the driver with RTEMS is the only feasible approach or just including it in my app is also a viable solution and I should expect it to be working.

Again, thanks a lot for your help!

asuol commented 3 years ago

Having the driver in the RTEMS source tree is not mandatory, it was just cleaner that way when using the device driver as part of a RTEMS testsuite test.

Since all applications are linked with RTEMS you have access to the RTEMS libraries, so you can compile the driver alongside your application.

Regarding the i2c_dev_alloc_and_init() function problem, I realized I pointed you to the wrong I2C library: it should still be <dev/i2c/i2c.h> (located at cpukit/include/dev/i2c/i2c.h in the RTEMS tree for reference) as stated on mcp23008.h.

The <libchip/i2c.h> header is for the legacy I2C library. Sorry for the confusion.

You should be able to just take the device driver as is from this repo and compile it together with your application. The <bsp/i2c.h> driver will also be available to your application since you are compiling RTEMS for the RPI BSP (all headers under "bsp" come from the compiled BSP include/bsp directory).

giorgiobasile commented 3 years ago

Great, compiling the device driver with the application seems to be working now, the problem with i2c_dev_alloc_and_init() was related to a wrong bus_path. Sorry for bothering you again, but I have one more question: we have made some adaptations to your driver to create a simple application that reads two bytes from a register of an MCP3425. Here is a simplified version of the code we are implementing:

rpi_setup_i2c_bus();
i2c_dev_register_mcp3425("/dev/i2c-1", "/dev/i2c-1.mcp3425", MCP3425_ADDR);
int fd = open("/dev/i2c-1.mcp3425", O_RDWR);

ioctl(fd, MCP3425_READ);

where MCP3425_READ is used by our i2c_mcp3425_linux_ioctl to trigger a i2c_mcp3425_read function very similar to your i2c_mcp23008_read_register:

static int i2c_mcp3425_linux_ioctl(i2c_dev *dev, ioctl_command_t command, void *arg) {

    uint8_t reg_content [2] = {0};  // *edit*
    int rv = 0;
    switch ( command ) {
        case MCP3425_READ:
             rv = i2c_mcp3425_read(dev, 0x00, reg_content);   // *edit*
             break;
        default:
             rv = -1;
    }
    return rv;
}
static int i2c_mcp3425_read(i2c_dev *dev,  uint8_t reg,  uint8_t* reg_content) {
  i2c_msg msg = {
    {
    .addr = dev->address,
    .flags = 0,
    .len = 1,
    .buf = &reg
    },
    {
    .addr = dev->address,
    .flags = I2C_M_RD,
    .len = 2,    // *edit*
    .buf = reg_content
    }
  };

  return i2c_bus_transfer(dev->bus, &msg, 2);
}

Unfortunately, the application seems to get stuck on i2c_bus_transfer, so we are not getting any error code back, nor any fatal error reported by RTEMS, so it is quite difficult to understand what's happening. Do you have any idea or suggestion on which situations this may happen, other than in case of hardware issues?

Thanks again for your time and useful info!

asuol commented 3 years ago

In polling mode (the default) the driver can be stuck in an endless loop (I2C polling function) if the expected data never arrives. An alternative to this is to use the driver in interrupt mode (set I2C_IO_MODE to 1, using the RSB option --with-rtems-bspopts="I2C_IO_MODE=1"), where i2c_bus_transfer should terminate with a timeout after a while.

Reasons for the data not arriving from the device may include:

giorgiobasile commented 3 years ago

The length of the cable was indeed to blame, the RPi receives the data now. Thanks again for your support!