CSI interface and i2c bus

StereoPi hardware discussion
Post Reply
delgoodie
Posts: 12
Joined: Sat Dec 28, 2019 12:57 am

CSI interface and i2c bus

Post by delgoodie »

It appears that both csi ports on the stereoPi are attached to the i2c bus 0. I have chosen to use two arducam OV9281 cameras but they both have the same address (60) which makes register modifying unattainable. To fix this I was wondering if it is possible to change the bus of one of the csi ports or modify the port by connecting it to the gpios supporting i2c bus 1? My end goal is to write to a series of registers enabling frame synchronization on the cameras (to use for stereo vision and disparity map creation). I tried soldering the oscillator from one camera to the other which broke them so now I am seeking to tackle the problem from the i2c bus instead of the camera hardware. Any suggestions appreciated!

User avatar
Realizator
Site Admin
Posts: 595
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: CSI interface and i2c bus

Post by Realizator »

Hi Delgoodie,
Well, the best option for us if both of your cameras have different I2C addresses. I've look in Arducam's Wiki and googled some Github discussions, and looks like there is no way to change I2C address of the camera. You can ask Arducam for it, may be there are some undeclared features for changing this address for the camera.

Now let's look inside StereoPi schematics, I mean this main part.
Also we'll need Compute Module datasheet (pages 18 and 19).

Here are some key points in this picture:
Image

For the CAM0 13 and 14 contacts of the camera connector are connected with the 5th and 6th legs of the Compute Module. And if you will look inside our device tree you find, that we use them as I2C for the 1st camera (cam0). In the CM datasheet you can find, that these pins can be configured to SDA0, SA5 or PCLK for 0 and SCL0, SA4 and DE for 1. So we can not reconfigure them for another I2C bus.

For the CAM1 13th and 14th camera pins are connected to the 28th (GPIO28) and 30th (GPIO29) legs of RPi Compute Module. According to RPi datasheet, they can be configured as SDA0, SA5, PCM_CLK and FL0 for GPIO28 and SCL0, SA4, PCM_FS and FL1 for GPIO29. That is, we can not reconfigure them for another I2C bus.

So it is impossible to solve this problem in a software way.

We have 3 options here:
1. You can try to use RPi development board, where you can commutate CAM1 13th and 14th contacts to GPIO44 and GPIO45, where another I2C is sitting (ATLT2 functions for these GPIOs.) Also you'll need to modify device tree (dt-blob) to describe to Raspbian what's going on.
2. Ugly hardware way. CAM0 is Ok for us, as it works with I2C0. So wee need to take another I2C and put it to the CAM1. Where this second I2C is sitting? In RPi datasheet we can see, that second I2C (SDA1 and SCL1) can be found:
- either on GPIO1 and GPIO2, which are actually commutated to the GPIO head
- or on GPIO44 and GPIO45, which are actually commutated to the 11th contacts of each camera connectors.
If I have such a task, I'd created a simple adapter board for one of your Arducam module ribbon cable. So you take all lines from the StereoPi to this adaptor board, and also a GPIO1 and GPIO2. And on the exit you put GPIO1 and GPIO2 lines instead of GPIO28 and GPIO29. That is you are feeding second camera with the second I2C bus. Of course, you'll need to modify device tree to implement functions needed.
3. As an idea - in the next revision of the StereoPi we can change schematics, and replace GPIO28 and GPIO29 with the GPIO44 and GPIO45. The feature of 44 and 45 GPIOS that they can be configured for both I2C0 and I2C 1 (ALT1 and ALT2 functions in a RPi Compute datasheet, page 19). But this is not so quick way...
Eugene a.k.a. Realizator

User avatar
Realizator
Site Admin
Posts: 595
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: CSI interface and i2c bus

Post by Realizator »

UPD> I've found this configuration example in Arducam Wiki:
https://arducam.com/downloads/modules/O ... _RAW10.cfg

You can see that you can set I2C address and registers here.
The question is: can you set a new I2C address for your second camera and "save" it as a default? So your sensors can sit on a different addresses. I thisn it is better to ask Arducam guys about it.
I've also asked a question on Arducam forum here.
Eugene a.k.a. Realizator

delgoodie
Posts: 12
Joined: Sat Dec 28, 2019 12:57 am

Re: CSI interface and i2c bus

Post by delgoodie »

Thank you so much for reviewing all the options, I considered the other ideas, however, I had been thinking especially about the second option you mentioned since I already have the stereoPi. I was not sure if it would still work if I just replaced the i2c lines with the gpio pins that support i2c bus 1 but I'm glad to have it confirmed! Do you know of an easy hardware solution to wiring (other than cutting and soldering directly on the csi ribbon wires). I was trying to look for a csi breakout board so that I could break out all the pins and then use another one to connect them all back to csi but swap the i2c wires, however, I couldn't seem to find it anywhere, other than a link on pintrest to an expired ebay page http://www.zeppy.io/product/gb/2/111521924226/. I also found https://www.adafruit.com/product/3671?h ... rt_id=3671 the adafruit csi extender and I noticed some solder points on top of the board but I was detered for three reasons. The first is that there are only 13 solder pins on the board meaning three are not broken out, the second is that their product page mentions that the board "does all the funky rearanging" which I would want more detail of to figure out if I can even use it. The third is that I could not find a schematic for the board to answer any of these questions, although I could buy it and use a multimeter for continuity (I am in a bit of a time crunch). Once again, thank you for the detailed response and thanks for the post you made!

User avatar
Realizator
Site Admin
Posts: 595
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: CSI interface and i2c bus

Post by Realizator »

Delgoodie, I've googled breakout boards too, and looks like it is rare thing. :-)
IMHO Adafruit csi-csi board is the best option. As for 13 soldering points - you see, CSI ribbon actually has a lot of ground contacts, that's why they breaks out not all 15 lines. You can also buy just a CSI connector itself, but soldering it without PCB is a challenge.
Eugene a.k.a. Realizator

delgoodie
Posts: 12
Joined: Sat Dec 28, 2019 12:57 am

Re: CSI interface and i2c bus

Post by delgoodie »

I know I'm shocked at how rare this seemingly common breakout board is! Luckily, my teacher can make a custom pcb for me to break out only the i2c lines of the csi cable and replace them with the gpio pins that support i2c bus 1. For now I have tested it by scrating and soldering onto a spare ribbon cable(and it looks as bad as it sounds). Thanks so much for helping me through this roadblock because I thought there was no way around the problem besides buying new cameras!

User avatar
Realizator
Site Admin
Posts: 595
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: CSI interface and i2c bus

Post by Realizator »

Delgoodie, I also noticed that CSI breakout boards are rare. Looks like that they are not so popular, as most or RPi users are not engineers. But if they are engineers, they can do this breakout PCB by themselves. We even has an idea to release our own breakout boards set, but I'm not sure it will be popular.
Eugene a.k.a. Realizator

delgoodie
Posts: 12
Joined: Sat Dec 28, 2019 12:57 am

Re: CSI interface and i2c bus

Post by delgoodie »

Yes, I understand that the pi is a hobbyist tool and not too much more. I think the breakout boards might be popular considering that most people who use the StereoPi will be using the same camera and odds are it is a rather inexpensive camera with no way to change the i2c address. On the flip side, I am not sure how many people even venture away from the standard pi cam v2 that comes with stereoPi because of all the code and hardware needed to get cameras like the arducam working with stereoPi. Also, like you said, hobbyist usually don't need much more than an 8MP rolling shutter camera for simple opencv coding and disparity maps.

delgoodie
Posts: 12
Joined: Sat Dec 28, 2019 12:57 am

Re: CSI interface and i2c bus

Post by delgoodie »

Update:
I have soldered new i2c lines to one of the cameras and kept all the other pins untouched. Now, when I enable the cameras using the arducam function I still can't write to the registers of the cameras and when I use i2cdetect 0 and i2cdetect 1 it only shows the addresses on bus 1, even though the csi port of the camera is on bus 0. Furthermore, when I remove the soldered-in camera and just use the csi port and one camera, i2cdetect gives me all the addresses on bus 0. It seems that when I enable the first camera I have access to it on the bus but as soon as I enable a second camera the prior bus goes blank? I am not sure where the problem is? They are on separate busses

User avatar
Realizator
Site Admin
Posts: 595
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: CSI interface and i2c bus

Post by Realizator »

Delgoodie, you're trying to work with the two cameras, not supported by kernel drivers. It means you need to load your drivers to enable them both (and you're in the process now). So you don't need any settings, related to original kernel cameras support. For example, our "dt-blob.bin" is not needed here, so rename or remove it. After that you'll have a default RPi settings, and Arducam drivers can do their job easier.
Next step is to enable both I2C via config.txt Have you enabled second I2C? You can look at similar discussion here on RPi forum.
After enabling second I2C try to test it with the single camera. And after success try to run two cams at a time.
Eugene a.k.a. Realizator

delgoodie
Posts: 12
Joined: Sat Dec 28, 2019 12:57 am

Re: CSI interface and i2c bus

Post by delgoodie »

I have dt_param=i2c_arm=on and dt_param=i2c_vc=on in my config which means both busses are enabled. In my /boot folder I have all the default dtb files that came with raspbian and in my overlays I don’t see a StereoPi dtbo. I never downloaded a stereopi os, just regular raspbian. When I run with one camera everything works, when I run with two, i get an image from each camera but I can only write i2c commands to one and only one is shown on the i2c bus.

User avatar
Realizator
Site Admin
Posts: 595
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: CSI interface and i2c bus

Post by Realizator »

Delgoodie, could you please test the second camera only with the second I2C you get from GPIO?
Ideally, it is better to check if the second I2S works itself, without your Arducam camera.

Only after that tests add a second camera.
Eugene a.k.a. Realizator

delgoodie
Posts: 12
Joined: Sat Dec 28, 2019 12:57 am

Re: CSI interface and i2c bus

Post by delgoodie »

Yes, when I remove the camera in the default csi port and only use the camera connected to i2c bus 1 through gpio wires, everything works perfectly, I get images and can write & registers.

User avatar
Realizator
Site Admin
Posts: 595
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: CSI interface and i2c bus

Post by Realizator »

Delgoodie, so you can get camera working on the 1st CSI and I2C0 and second CSI with the I2C1. It's great, and it meant all your hardware (including I2C1 soldering) is Ok.
Now let's dig into software part.
Could you please tell me, how you're capturing images from each camera separately, and from both cameras together? I mean software you are using.

As for capturing images from 2 cams - are you using preview_dualcam.c from Arducam Github repo, or another way?

By the way, I've found this comment in this code:
// Important: Using init_camera will invalidate the read/write I2C of another camera_instance,
// ie the control command is invalid and the resolution cannot be switched(autoexposure is also unusable).
// This problem will be fixed in the future.
Eugene a.k.a. Realizator

delgoodie
Posts: 12
Joined: Sat Dec 28, 2019 12:57 am

Re: CSI interface and i2c bus

Post by delgoodie »

Wow I can’t believe I’ve never seen that, I basically used the arducam function and re wrote the capture2opencv cpp to initiate two cameras with a camera structure containing the valid StereoPi pins(but for i2c I did bus 1 and chose the gpio pins for sda and scl) then I used the arducam capture function to store raw jpg images in a buffer, and accessed the images from the buffer.

User avatar
Realizator
Site Admin
Posts: 595
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: CSI interface and i2c bus

Post by Realizator »

Delgoodie, I think you're one step away from the result. You need to look inside Arducam driver.
I have no your Arducam cameras, so can not make this part by myself. But I can suggest, that there are two ways:
1. Modify Arducam driver and functions to work with the second camera, and use it together with unmodified driver for the first camera. You need to check, that both drivers are not in conflict. This is similar to running two raspivid instances for original Pi cams, like this:

Code: Select all

raspivid -cs 0 -w 1280 -h 720 -o 1.h264 &
raspivid -cs 1 -w 1280 -h 720 -o 2.h264

where each of them works with it's own camera.

2. Modify "preview_dualcam" code for work with the two cameras, but also with the two I2Cs. This approach is similar to using one raspivid function with '-3d' option, when one binary works with the 2 cams at once.

I don't know which way is more easy. As for me, I'd try 1st approach, as it looks like a bit easy. But, IMHO, with the 2nd approach you will have a better frames sync (if you didn't soldered both cameras oscillators for syncing).

In any way, it is better to download all sources of Arducam drivers, compile them from scratch, and check if it works (to use it as a reference). And after that try to modify them according to your needs.
Eugene a.k.a. Realizator

delgoodie
Posts: 12
Joined: Sat Dec 28, 2019 12:57 am

Re: CSI interface and i2c bus

Post by delgoodie »

I’m not exactly sure where the current drivers are but I must have them in my folder to get any camera to work at all right? If so, I think the driver code is insidehttps://github.com/ArduCAM/MIPI_Camera/ ... icamera.so which is a binary compiled c code file. I can’t look into this file to see changes so I don’t know how I could modify it. I might be unclear as to what a driver is? I assume it is a bunch of i2c writing to get the camera set up and capture from it?
As for option 2, I tried modifying it by initializing the both cameras with arducam‘s camera struct where I set one camera to i2c bus 0 and the respective pins and the other to i2c bus 1. The results were that I could still get two images but couldn’t read or write from whichever instance I initialized first. AKA, initializing the second camera “re-wrote” the first initialization.

User avatar
Realizator
Site Admin
Posts: 595
Joined: Tue Apr 16, 2019 9:23 am
Contact:

Re: CSI interface and i2c bus

Post by Realizator »

Hmmm, I was unable to find sources of libarducam_mipicamera.so I just found a notice here, that they are based on https://github.com/6by9/raspiraw/ (native RPi camera application). But I don't think it is a good idea for you to write your own driver from scratch.

According to mentioned comment in preview_dualcam.c, you're unable to access first camera's I2C after initializing another camera_instance.
I did not received any answer yet on Arducam forum, so I think we need to find a solution by our own.

How do you think, if this approach can work for you?
- initialize first instance of camera_instance
- write all settings you need by I2C
- initialize another instance
- write another settings for the second camera
Eugene a.k.a. Realizator

mohand
Posts: 1
Joined: Thu Jul 02, 2020 5:47 am

Re: CSI interface and i2c bus

Post by mohand »

I have similar problem but I am using the v1 pi camera (ov5647). i am trying to send i2c command to enable frex mode. when i try that , i can control only a single sensor. So i was wondering if there is a solution for that sensor. Another question, if they are sharing the same i2c bus, then how can the GPU connect to both of them without conflicts (Can you please refer to any PI document in this regard).

delgoodie
Posts: 12
Joined: Sat Dec 28, 2019 12:57 am

Re: CSI interface and i2c bus

Post by delgoodie »

I unfortunately never found a solution to controlling my two arducam cameras because of the i2c issue, however, I think my issue was product specific because if it wasn't it would defeat the purpose of i2c (being able to control multiple devices through the use of addresses). I ended up purchasing the arducam dual camera board which is a setback because I cannot position the cameras farther apart (to get a more accurate distance measurment), but I don't have to deal with the headache of syncing frames!

Post Reply