Working with a Camera and DCMI

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

IgorEE
Posts: 21
Joined: Fri Nov 14, 2014 4:40 pm

Working with a Camera and DCMI

Post by IgorEE »

Hi,

I've been trying to implement a camera system using a STM32F407 (stm32F4 discovery board) and a OV2640 camera. So far, I can talk with the camera via SCCB (Using I2C driver in ChibiOS) meaning I can set and read registers correctly. I have verified this by reading the camera ID registers correctly and manipulating the camera clock register and then observing the camera PCLK frequency changing on the oscilloscope.

Now it is time to try and get (JPEG) data out of the camera. My plan is to capture a JPEG from the camera (preferably using DCMI) and then slowly output it via USB CDC or serial UART to a host computer. \

The problem is that ChibiOS does not have a DCMI driver. I have found the DCMI implementation posted to this forum by EwanC, but it is over two years old and reading through the code and the post it doesn't seem like it ever worked well.

My question is what would be the suggested way to get the data out of this cam?
1) Try to get the DCMI driver by EwanC working?
2) Pull in std-periph-libraries and use their implementation of the DCMI driver (how will this live along side ChibiOS?)
3) Configure DCMI and DMA at register level using CMSIS (this seems to be quite tedious)?
4) Ditch ChibiOS completely and implement the whole program via std_periph_library?
5) Something else?

Basically, I'm not sure how to proceed at this point and I would like to get something working as quick as possible and I feel like if I take the wrong approach now it might take quite a long time to get something out of the camera.

Appreciate any advice,
Thanks,
-Igor
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Working with a Camera and DCMI

Post by Giovanni »

All the above solutions could work, I would try 1 and 2 first.

Giovanni
IgorEE
Posts: 21
Joined: Fri Nov 14, 2014 4:40 pm

Re: Working with a Camera and DCMI

Post by IgorEE »

Thanks Giovanni,

Following your recommendation I am now trying to integrate the DCMI driver by Ewan Coldicott into my project. I have done the following:
1) Downloaded ChibiStudio Newest
2) Downloaded ZIP file of Ewan's DCMI library
3) Copied files
...Downloads\ChibiOS-master\ChibiOS-master\os\hal\include\dcmi.h to C:\ChibiStudio\chibios\os\hal\include
...Downloads\ChibiOS-master\ChibiOS-master\os\hal\platforms\STM32F4xx\dcmi_lld.c to C:\ChibiStudio\chibios\os\hal\platforms\STM32F4xx
...Downloads\ChibiOS-master\ChibiOS-master\os\hal\platforms\STM32F4xx\dcmi_lld.h to C:\ChibiStudio\chibios\os\hal\platforms\STM32F4xx
...Downloads\ChibiOS-master\ChibiOS-master\os\hal\src\dcmi.c to C:\ChibiStudio\chibios\os\hal\src
...Downloads\ChibiOS-master\ChibiOS-master\testhal\STM32F4xx\DCMI\* to C:\ChibiStudio\chibios\testhal\STM32F4xx\DCMI (created folder)
4) Modifed C:\ChibiStudio\chibios\os\hal\hal.mk to add dcmi.c entry
5) Modified C:\ChibiStudio\chibios\os\hal\platforms\STM32F4xx\platform.mk to add dcmi_lld.c entry

Then I added a section to halconf.h as per Ewan's example project to include DCMI subsystem.
Finally, I added the following declaration to my main.c file:

Code: Select all

static const DCMIConfig dcmicfg = {
   frameEndCb,
   dmaTxferEndCb,
   0            //empty cr
};

to see whether the compilation will work. Unfortunately, it fails but I do not know why. The error the compiler produces is:

Code: Select all

 
...
Compiling usb.c
Compiling dcmi.c
Compiling stm32_dma.c
../../os/hal/src/dcmi.c: In function 'dcmiInit':
../../os/hal/src/dcmi.c:63:3: warning: implicit declaration of function 'dcmi_lld_init' [-Wimplicit-function-declaration]
../../os/hal/src/dcmi.c: At top level:
../../os/hal/src/dcmi.c:73:21: error: unknown type name 'DCMIDriver'
../../os/hal/src/dcmi.c:93:16: error: unknown type name 'DCMIDriver'
../../os/hal/src/dcmi.c:93:16: error: unknown type name 'DCMIConfig'
../../os/hal/src/dcmi.c:113:15: error: unknown type name 'DCMIDriver'
../../os/hal/src/dcmi.c:142:23: error: unknown type name 'DCMIDriver'
../../os/hal/src/dcmi.c:169:30: error: unknown type name 'DCMIDriver'
make: *** [build/obj/dcmi.o] Error 1
make: *** Waiting for unfinished jobs....
Compiling hal_lld.c
Compiling dcmi_lld.c
../../os/hal/platforms/STM32F4xx/dcmi_lld.c:66:45: error: unknown type name 'DCMIDriver'
../../os/hal/platforms/STM32F4xx/dcmi_lld.c: In function 'Vector178':
../../os/hal/platforms/STM32F4xx/dcmi_lld.c:96:3: error: 'GPIOA_LED2' undeclared (first use in this function)
../../os/hal/platforms/STM32F4xx/dcmi_lld.c:96:3: note: each undeclared identifier is reported only once for each function it appears in
../../os/hal/platforms/STM32F4xx/dcmi_lld.c: At top level:
../../os/hal/platforms/STM32F4xx/dcmi_lld.c:143:21: error: unknown type name 'DCMIDriver'
../../os/hal/platforms/STM32F4xx/dcmi_lld.c:180:20: error: unknown type name 'DCMIDriver'
../../os/hal/platforms/STM32F4xx/dcmi_lld.c:212:23: error: unknown type name 'DCMIDriver'
make: *** [build/obj/dcmi_lld.o] Error 1


So I need to let the build system know about these files somewhere else as well, but I can't find where. Any ideas where else should I look to add the dcmi sources in order to get the project to compile?
Thanks,
-Igor
IgorEE
Posts: 21
Joined: Fri Nov 14, 2014 4:40 pm

Re: Working with a Camera and DCMI

Post by IgorEE »

After more digging around and following the include chain I added:

Code: Select all

 
#include "dcmi.h"

to os\hal\include\hal.h and this fixed all my problems. I am left with two more warnings that need to be addressed. These are shown below:

Code: Select all

 
../../os/hal/platforms/STM32F4xx/dcmi_lld.c: In function 'dcmi_lld_start':
../../os/hal/platforms/STM32F4xx/dcmi_lld.c:145:3: warning: implicit declaration of function 'rccEnableDCMI' [-Wimplicit-function-declaration]
../../os/hal/platforms/STM32F4xx/dcmi_lld.c: In function 'dcmi_lld_stop':
../../os/hal/platforms/STM32F4xx/dcmi_lld.c:189:7: warning: implicit declaration of function 'rccDisableDCMI' [-Wimplicit-function-declaration]


As it can be seen, these are in dcmi_lld.c from Ewan. However, I can not find these functions (rccEnableDCMI and rccDisableDCMI) anywhere for the life of me! Are these from an older release of ChibiOS? If so, are there replacements?
Thanks,
-Igor
IgorEE
Posts: 21
Joined: Fri Nov 14, 2014 4:40 pm

Re: Working with a Camera and DCMI

Post by IgorEE »

It seems right after I post here I manage to resolve my problem :) So in that spirit, I finally found the required functions hidden in os\hal\platforms\STM32F4xx\stm32_rcc.h. I copied the following code from that file:

Code: Select all

 
/**
 * @name    DCMI peripheral specific RCC operations
 * @{
 */
/**
 * @brief   Enables the DCMI peripheral clock.
 *
 * @param[in] lp     low power enable flag
 *
 * @api
 */
#define rccEnableDCMI(lp) rccEnableAHB2(RCC_AHB2ENR_DCMIEN, lp)

/**
 * @brief   Disables the DCMI peripheral clock.
 *
 * @param[in] lp        low power enable flag
 *
 * @api
 */
#define rccDisableDCMI(lp) rccDisableAHB2(RCC_AHB2ENR_DCMIEN, lp)

/**
 * @brief    Resets the DCMI periphera.
 *
 * @api
 */
#define rccResetDCMI() rccResetAHB2(RCC_AHB2RSTR_DCMIRST)
/** @} */

and added it to the corresponding file in the new chibiOS release I am using. I am getting the project to successfully compile now. Next step will be to try and configure and use the DCMI peripheral according to Ewan's examples. If Giovanni does not mind I will keep updating this thread with my steps for future reference.

Regards,
-Igor
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Working with a Camera and DCMI

Post by Giovanni »

Of course it is OK, this reference material for other developers.

Giovanni
IgorEE
Posts: 21
Joined: Fri Nov 14, 2014 4:40 pm

Re: Working with a Camera and DCMI

Post by IgorEE »

Hi,

for anyone following along, after getting the project to compile I was unable to get anything out of the DCMI peripheral. After a few hours of debugging, I figured out an additional change is needed to the hal.c file.
I added the following code:

Code: Select all

#if HAL_USE_DCMI || defined(__DOXYGEN__)
  dcmiInit();
#endif


After this change, I finally managed to get some data out of the DCMI peripheral (well, at least I am getting data in the DCMI->DR register, which I was not getting before. Unfortunately, it is still not working, and I think there are some problems with the DMA settings. I will investigate further tomorrow and post back.

Cheers,
-Igor
IgorEE
Posts: 21
Joined: Fri Nov 14, 2014 4:40 pm

Re: Working with a Camera and DCMI

Post by IgorEE »

Hi,

So I'm pretty stuck this time, so if anyone has any suggestions they are all welcome.

Basically, I have Ewan's dcmi driver integrated into my project, a camera connected and configured via SCCB and I think everything is configured correctly. When I attempt to capture something from the camera via "dcmiStartReceiveOneShot(...)" function, nothing seems to happen. Digging around and debugging, I figured out that the dcmi frameEndCallback is called, but the dmaTxEndCallback is never called. Also, the dcmi_lld_serve_dma_rx function which is supposed to be the DMA rx interrupt never gets called either.

I am thinking there is something not right with the DMA config because I can directly read the DCMI->DR register and it is constantly changing (as though DCMI is receiving data), but the DMA transfer never "completes".

My main questions is which register or ChibiOS structure would be able to tell me if there are any DMA errors or what is going on? Basically, what places can I check for errors to help me figure out what is going on?

I can post my code somewhere (it is in multiple files) if required.

Thanks for any feedback,
-Igor
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Working with a Camera and DCMI

Post by Giovanni »

Verify the DMA transfer counter register before and after the operation, verify if numbers are correct.

Giovanni
IgorEE
Posts: 21
Joined: Fri Nov 14, 2014 4:40 pm

Re: Working with a Camera and DCMI

Post by IgorEE »

Hi Giovanni,

Thanks for the suggestions.

I've been looking at the DMA transfer count register and playing around with DCMI configuration register (through the DCMIConfig structure) and tweaked the clocking settings and which edge of the HSYNC and VSYNC and PCLK the DCMI captures at and managed to obtain a few good images in JPEG format! This is indeed great, but a few problems still remain.

My program is a shell, and when it receives the cam_capture command via serial, it triggers the capture routine. The capture command is below:

Code: Select all

 
static void cmd_cam_capture(BaseSequentialStream *chp, int argc, char *argv[]) {
  /* Starts the DCMI and DMA streams in order to capture a single JPEG frame
   * from the camera via DVP interface and store it in internal MCU memory.
   */
  (void)argc;
  (void)argv;

  busy = 1;
  chprintf(chp, "Initiating DCMI->DMA Image Acquisition\r\n");
  chprintf(chp, "DCMI CR is: %x\r\n", DCMID1.dcmi->CR);
  chprintf(chp, "DCMI DR is: %x\r\n", DCMID1.dcmi->DR);
  chprintf(chp, "DMA Counter Before: %d\r\n", dmaStreamGetTransactionSize(DCMID1.dmarx));
  // BUFFER_SIZE is 50 000
  dcmiReceiveOneShot(&DCMID1, BUFFER_SIZE / 2, ImageBuffer0, ImageBuffer1);
  chprintf(chp, "DMA Counter After: %d\r\n", dmaStreamGetTransactionSize(DCMID1.dmarx));
  busy = 0;
  captured = 1;
  chprintf(chp, "Image Acquisition Complete\r\n");
}


On reset, I can issue the cam_capture command and I can obtain a valid JPEG image. However, if I issue the cam_capture command again, it does not work. The function returns as per normal, but he image buffer still contains the old image. Also, the dma callback function never gets called (not even the first time cam_capture is called). So I think I still have a DMA problem but I don't know where to investigate further. Here is a shell interaction with the program (only after the first cam_capture command is a valid image captured from the camera - all other cam_capture calls seem to do nothing (at least the image buffer is unchanged)).

Code: Select all

 
ChibiOS/RT Shell
ch> cam_on
Setting CAM ON
ch> cam_reset
Resetting CAM
CAM Reset Successful
ch> cam_init
CAM Init Completed.
ch> cam_capture
Initiating DCMI->DMA Image Acquisition
DCMI CR is: 28
DCMI DR is: 0
DMA Counter Before: 0
DMA Counter After: 6250
Image Acquisition Complete
ch> cam_capture
Initiating DCMI->DMA Image Acquisition
DCMI CR is: 402A
DCMI DR is: 0
DMA Counter Before: 1770
DMA Counter After: 6250
Image Acquisition Complete
ch> cam_capture
Initiating DCMI->DMA Image Acquisition
DCMI CR is: 402A
DCMI DR is: E0FFD80B
DMA Counter Before: 6250
DMA Counter After: 6250
Image Acquisition Complete
ch> cam_capture
Initiating DCMI->DMA Image Acquisition
DCMI CR is: 402A
DCMI DR is: E0FFD80B
DMA Counter Before: 6250
DMA Counter After: 6250
Image Acquisition Complete
ch> cam_capture
Initiating DCMI->DMA Image Acquisition
DCMI CR is: 402A
DCMI DR is: E0FFD80B
DMA Counter Before: 6250
DMA Counter After: 6250
Image Acquisition Complete
ch>


Notice the strange behaviour of the DMA counter? In dcmi_lld.c the BUFFER_SIZE that gets assigned is divided by 4, so 50 000 / 2 (in function call) = 25 000 / 4 (in dcmi_lld.c dcmi_lld_receive function) = 6250 which should be the DMA transaction size. But it does not get decremented.
Finally, here is my DCMIConfig structure and call back functions.

Code: Select all

 
void frameEndCb(DCMIDriver* dcmip) {
   (void) dcmip;
   palSetPad(GPIOD, 12) ; // Green
}

void dmaTxferEndCb(DCMIDriver* dcmip) {
   (void) dcmip;
   palSetPad(GPIOD, 15); // Blue
}

static const DCMIConfig dcmicfg = {
   frameEndCb,
   dmaTxferEndCb,
   //0b00000000000000000000000011101010
   //DCMI_CR_CM | DCMI_CR_JPEG | DCMI_CR_PCKPOL | DCMI_CR_HSPOL | DCMI_CR_VSPOL           // CR settings
   DCMI_CR_JPEG | DCMI_CR_PCKPOL
};


So if anyone can spot a problem, or has suggestions on how to proceed debugging this please let me know. If you need me to post some other sections of the code or prepare a minimal example that demonstrates the issue I can do that as well.
Thanks!
-Igor
Post Reply