OV5642/OV7725 only outputting the same data Topic is solved

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
Pethead
Posts: 37
Joined: Mon Jul 11, 2016 1:47 pm
Has thanked: 1 time

OV5642/OV7725 only outputting the same data

Post by Pethead »

Hello,

I am trying to get my OV5642 camera module to work and I've looked a little bit at this thread for configuring the DCMI and DMA interface that writes to my ov5642_ram_buffer. The OV5642 has the option to convert to JPEG itself so I will be using that instead if I actually get it to work instead of the software encoding.

I have been looking at the OV5642 SW Application Manual in order to try to get it to work, I'm using the settings for the QVGA_PREVIEW and it should put out 320x240 in YUV422 format which should end up in my uint16_t ov5642_ram_buffer[]; with 2 bytes per pixel.

However, when I print the data from the ram_buffer it's all the same value 0x0b.. And if I send the sequence for outputting it in JPEG format I only receive 0xa3

I haven't really looked through all the settings in from the QVGA preview but all the registers that I've looked at seems to be correct.

I've also tried my other module OV7725 with the preview settings for that module and it's the same problem there, I only receive the same value, so I assume that there is something that I've missed. However, I have no idea where to start looking..

The camera needs an external clock source between 6 - 27 MHz and my PWM outputs a square wave of roughly 20 MHz.

Code: Select all

#include "ch.h"
#include "hal.h"

#include <stdio.h>
#include <string.h>

#include "ov5642.h"

#define OV5642_MAXX               320
#define OV5642_MAXY               120

#define OV5642_BUFFER_SIZE         OV5642_MAXX*OV5642_MAXY
#define OV5642_I2C_ADDR            0x3C   /* OV5642 I2C slave addresses */

uint16_t ov5642_ram_buffer[OV5642_BUFFER_SIZE];
uint8_t samplingFinished = 0;

/* i2c1 Interface config */
static const I2CConfig i2ccfg1 =
{
      OPMODE_I2C,
      400000,
      FAST_DUTY_CYCLE_2,
};

/* PWM configuration for ov5642 XCLK*/
static PWMConfig pwmcfg =
{
      168000000,
      8,
      NULL,
      {
            {PWM_OUTPUT_ACTIVE_HIGH, NULL},      /* ch.1 */
            {PWM_OUTPUT_DISABLED, NULL},      /* ch.2 */
            {PWM_OUTPUT_DISABLED, NULL},      /* ch.3 */
            {PWM_OUTPUT_DISABLED, NULL}         /* ch.4 */
      },
      0,
      0
};

/* Function declarations */
static void ov5642_TransmitConfig(void);
static void dma_avail(uint32_t);
static void ov5642_InitDMA(void);
static void ov5642_InitDCMI(void);
static void ov5642_InitXCLK(void);

/*
 * Start PWM for ov7725 XCLK on tim1
 */
static void ov5642_InitXCLK(void)
{
   pwmStart(&PWMD1, &pwmcfg);
   pwmEnableChannel(&PWMD1, 0, 4);
}

/*
 * Transmit OV5642 configuration over i2c1
 */
static void ov5642_TransmitConfig(void)
{
   uint32_t i;
   uint8_t txbuf[3];
   //uint8_t rxbuf = 0;

   for (i = 0; i < sizeof(OV5642_QVGA); i++)
   {
      /* We have 16-bit address registers */
      txbuf[0] = OV5642_QVGA[i].reg >> 8;
      txbuf[1] = OV5642_QVGA[i].reg & 0xff;
      txbuf[2] = OV5642_QVGA[i].val;

      i2cAcquireBus(&I2CD1);
      i2cMasterTransmitTimeout(&I2CD1, OV5642_I2C_ADDR, &txbuf[0], 3, NULL, 0, MS2ST(100));
      i2cReleaseBus(&I2CD1);
   }
}


/*
 * ISR callback called when DCMI buffer moved into RAM by DMA implicitly
 */
static void dma_avail(uint32_t flags)
{
   (void)flags;

   palClearPad(GPIOD, 15);

   samplingFinished = 1;
   dmaStreamDisable(STM32_DMA2_STREAM1);
}

/*
 * Initializes DMA
 */
static void ov5642_InitDMA(void)
{
   const stm32_dma_stream_t *stream = STM32_DMA2_STREAM1;

   dmaStreamAllocate(stream, 10, (stm32_dmaisr_t)dma_avail, NULL);
   dmaStreamSetPeripheral(stream, ((uint32_t*)&DCMI->DR));
   dmaStreamSetMemory0(stream, (uint32_t)ov5642_ram_buffer);
   dmaStreamSetTransactionSize(stream, OV5642_BUFFER_SIZE/sizeof(uint16_t));
   dmaStreamSetMode(stream, STM32_DMA_CR_CHSEL(1) | STM32_DMA_CR_DIR_P2M
                                       | STM32_DMA_CR_MINC
                                       | STM32_DMA_CR_PSIZE_WORD
                                       | STM32_DMA_CR_MSIZE_HWORD
                                       | STM32_DMA_CR_MBURST_SINGLE
                                       | STM32_DMA_CR_PBURST_SINGLE
                                       | STM32_DMA_CR_TCIE);
   dmaStreamSetFIFO(stream, STM32_DMA_FCR_FTH_FULL);
   dmaStreamEnable(stream);
}

/*
* Initializes DCMI
*/
static void ov5642_InitDCMI(void)
{
   RCC->AHB2ENR |= (1 << 0);   /* DCMI clock enable */

   /* EDM[1:0]: Extended data mode
    * 00: Interface captures 8-bit data on every pixel clock
    * 01: Interface captures 10-bit data on every pixel clock
    * 10: Interface captures 12-bit data on every pixel clock
    * 11: Interface captures 14-bit data on every pixel clock
    */
   DCMI->CR &= ~DCMI_CR_EDM_1;
   DCMI->CR &= ~DCMI_CR_EDM_0;

   /* FCRC[1:0]: Frame capture rate control
    * 00: All frames are captured
    * 01: Every alternate frame captured (50% bandwidth reduction)
    * 10: One frame in 4 frames is captured (75% bandwidth reduction)
    * 11: Reserved
    */
   DCMI->CR &= ~DCMI_CR_FCRC_1;
   DCMI->CR &= ~DCMI_CR_FCRC_0;

   /* VSPOL: Vertical Synchronization polarity
    * 0: VSYNC active low
    * 1: VSYNC active high
    */
   DCMI->CR |= DCMI_CR_VSPOL;

   /* HSPOL: Horizontal Synchronization polarity
    * 0: HSYNC active low
    * 1: HSYNC active high
    */
   DCMI->CR |= DCMI_CR_HSPOL;

   /* PCKPOL: Pixel clock polarity
    * 0: Falling edge active
    * 1: Rising edge active
    */
   DCMI->CR |= DCMI_CR_PCKPOL;

   /* ESS: Embedded synchronization select
    * 0: Hardware sync, data capture is synced with HSYNC/VSYNC signals
    * 1: Embedded sync, data capture is synced with codes embedded in the data flow
    */
   DCMI->CR &= ~DCMI_CR_ESS;

   /* JPEG: JPEG format
    * 0: Uncompressed video format
    * 1: Used for JPEG data transfers
    */
   DCMI->CR &= ~DCMI_CR_JPEG;

   /* CROP: Crop feature
    * 0: Full image is captured
    * 1: Only data inside the window specified by the crop register will be captured
    */
   DCMI->CR &= ~DCMI_CR_CROP;

   /* CM: Capture Mode
    * 0: Continuous grab mode
    * 1: Snapshot mode
    */
   DCMI->CR &= ~DCMI_CR_CM;

   DCMI->CR |= (uint32_t)DCMI_CR_ENABLE;   /* DCMI enable */
   DCMI->CR |= (uint32_t)DCMI_CR_CAPTURE;   /* Capture enable */
}

void ov5642_Init(void)
{
   /* Configure GPIO pins for I2C_1 */
   palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN);   /* I2C_1 SCL */
   palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN); /* I2C_1 SCK */

   /* Configure GPIO pins for DCMI interface  */
   palSetPadMode(GPIOA, 6, PAL_MODE_ALTERNATE(13));   /* DCMI_PIXCK */
   palSetPadMode(GPIOC, 8, PAL_MODE_ALTERNATE(13));   /* DCMI_D2 */
   palSetPadMode(GPIOE, 1, PAL_MODE_ALTERNATE(13));   /* DCMI_D3 */
   palSetPadMode(GPIOC, 11, PAL_MODE_ALTERNATE(13));   /* DCMI_D4 */
   palSetPadMode(GPIOB, 6, PAL_MODE_ALTERNATE(13));   /* DCMI_D5 */
   palSetPadMode(GPIOE, 5, PAL_MODE_ALTERNATE(13));   /* DCMI_D6 */
   palSetPadMode(GPIOE, 6, PAL_MODE_ALTERNATE(13));   /* DCMI_D7 */
   palSetPadMode(GPIOC, 10, PAL_MODE_ALTERNATE(13));   /* DCMI_D8 */
   palSetPadMode(GPIOC, 12, PAL_MODE_ALTERNATE(13));   /* DCMI_D9 */
   palSetPadMode(GPIOB, 7, PAL_MODE_ALTERNATE(13));   /* DCMI_VSYNC */
   palSetPadMode(GPIOA, 4, PAL_MODE_ALTERNATE(13));   /* DCMI_HSYNC */

   /* Configure GPIO pin for XCLK timer1 ch 1 on PE9 (needed for ov5642)*/
   palSetPadMode(GPIOE, 9, PAL_MODE_ALTERNATE(1));      /* XCLK */

   palSetPadMode(GPIOD, 15, PAL_MODE_OUTPUT_PUSHPULL);
   palSetPad(GPIOD, 15); /* Cleared on 1st DMA interrupt */

   ov5642_InitXCLK();            /* Start XCLK */
   i2cStart(&I2CD1, &i2ccfg1);   /* Initialize i2c1 */
   ov5642_TransmitConfig();      /* Transmit config over i2c1 */
   ov5642_InitDMA();             /* Initialize DCMI DMA */   
   ov5642_InitDCMI();            /* Initialize DCMI */

   return;
}


I am then sending the ov5642_ram_buffer data in a thread when the samplingFinished flag is set

Code: Select all

send(newsocket_fd, ov5642_ram_buffer, 38400, 0);


And I've made a python script to receive it

Code: Select all

import socket
import sys
 
host = '192.168.1.7'
port = 8001

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))

img = open('test.jpg', 'wb')

sock.send("msg".encode())
data = sock.recv(100000)

print(data)
img.write(data)

img.close()
sock.close()
I know that it's not a jpg at this point if I am not using the JPEG encoding settings. So another file format should be used for YUV422.


Structure with settings for the camera

Code: Select all

struct sensor_reg
{
   uint16_t reg;
   uint8_t val;
};

struct sensor_reg OV5642_QVGA[] =
{
   {0x3103 ,0x93},
   {0x3008 ,0x82},
   {0x3017 ,0x7f},
   {0x3018 ,0xfc},
   {0x3810 ,0xc2},
   {0x3615 ,0xf0},
   {0x3000 ,0x00},
   {0x3001 ,0x00},
   {0x3002 ,0x5c},
   {0x3003 ,0x00},
   {0x3004 ,0xff},
   {0x3005 ,0xff},
   {0x3006 ,0x43},
   {0x3007 ,0x37},
   {0x3011 ,0x08},
   {0x3010 ,0x10},
   {0x460c ,0x22},
   {0x3815 ,0x04},
   {0x370c ,0xa0},
   {0x3602 ,0xfc},
   {0x3612 ,0xff},
   {0x3634 ,0xc0},
   {0x3613 ,0x00},
   {0x3605 ,0x7c},
   {0x3621 ,0x09},
   {0x3622 ,0x60},
   {0x3604 ,0x40},
   {0x3603 ,0xa7},
   {0x3603 ,0x27},
   {0x4000 ,0x21},
   {0x401d ,0x22},
   {0x3600 ,0x54},
   {0x3605 ,0x04},
   {0x3606 ,0x3f},
   {0x3c01 ,0x80},
   {0x5000 ,0x4f},
   {0x5020 ,0x04},
   {0x5181 ,0x79},
   {0x5182 ,0x00},
   {0x5185 ,0x22},
   {0x5197 ,0x01},
   {0x5001 ,0xff},
   {0x5500 ,0x0a},
   {0x5504 ,0x00},
   {0x5505 ,0x7f},
   {0x5080 ,0x08},
   {0x300e ,0x18},
   {0x4610 ,0x00},
   {0x471d ,0x05},
   {0x4708 ,0x06},
   {0x3808 ,0x02},
   {0x3809 ,0x80},
   {0x380a ,0x01},
   {0x380b ,0xe0},
   {0x380e ,0x07},
   {0x380f ,0xd0},
   {0x501f ,0x00},
   {0x5000 ,0x4f},
   {0x4300 ,0x30},
   {0x3503 ,0x07},
   {0x3501 ,0x73},
   {0x3502 ,0x80},
   {0x350b ,0x00},
   {0x3503 ,0x07},
   {0x3824 ,0x11},
   {0x3501 ,0x1e},
   {0x3502 ,0x80},
   {0x350b ,0x7f},
   {0x380c ,0x0c},
   {0x380d ,0x80},
   {0x380e ,0x03},
   {0x380f ,0xe8},
   {0x3a0d ,0x04},
   {0x3a0e ,0x03},
   {0x3818 ,0xc1},
   {0x3705 ,0xdb},
   {0x370a ,0x81},
   {0x3801 ,0x80},
   {0x3621 ,0x87},
   {0x3801 ,0x50},
   {0x3803 ,0x08},
   {0x3827 ,0x08},
   {0x3810 ,0x40},
   {0x3804 ,0x05},
   {0x3805 ,0x00},
   {0x5682 ,0x05},
   {0x5683 ,0x00},
   {0x3806 ,0x03},
   {0x3807 ,0xc0},
   {0x5686 ,0x03},
   {0x5687 ,0xbc},
   {0x3a00 ,0x78},
   {0x3a1a ,0x05},
   {0x3a13 ,0x30},
   {0x3a18 ,0x00},
   {0x3a19 ,0x7c},
   {0x3a08 ,0x12},
   {0x3a09 ,0xc0},
   {0x3a0a ,0x0f},
   {0x3a0b ,0xa0},
   {0x350c ,0x07},
   {0x350d ,0xd0},
   {0x3500 ,0x00},
   {0x3501 ,0x00},
   {0x3502 ,0x00},
   {0x350a ,0x00},
   {0x350b ,0x00},
   {0x3503 ,0x00},
   {0x528a ,0x02},
   {0x528b ,0x04},
   {0x528c ,0x08},
   {0x528d ,0x08},
   {0x528e ,0x08},
   {0x528f ,0x10},
   {0x5290 ,0x10},
   {0x5292 ,0x00},
   {0x5293 ,0x02},
   {0x5294 ,0x00},
   {0x5295 ,0x02},
   {0x5296 ,0x00},
   {0x5297 ,0x02},
   {0x5298 ,0x00},
   {0x5299 ,0x02},
   {0x529a ,0x00},
   {0x529b ,0x02},
   {0x529c ,0x00},
   {0x529d ,0x02},
   {0x529e ,0x00},
   {0x529f ,0x02},
   {0x3030 ,0x0b},
   {0x3a02 ,0x00},
   {0x3a03 ,0x7d},
   {0x3a04 ,0x00},
   {0x3a14 ,0x00},
   {0x3a15 ,0x7d},
   {0x3a16 ,0x00},
   {0x3a00 ,0x78},
   {0x3a08 ,0x09},
   {0x3a09 ,0x60},
   {0x3a0a ,0x07},
   {0x3a0b ,0xd0},
   {0x3a0d ,0x08},
   {0x3a0e ,0x06},
   {0x5193 ,0x70},
   {0x589b ,0x04},
   {0x589a ,0xc5},
   {0x401e ,0x20},
   {0x4001 ,0x42},
   {0x401c ,0x04},
   {0x528a ,0x01},
   {0x528b ,0x04},
   {0x528c ,0x08},
   {0x528d ,0x10},
   {0x528e ,0x20},
   {0x528f ,0x28},
   {0x5290 ,0x30},
   {0x5292 ,0x00},
   {0x5293 ,0x01},
   {0x5294 ,0x00},
   {0x5295 ,0x04},
   {0x5296 ,0x00},
   {0x5297 ,0x08},
   {0x5298 ,0x00},
   {0x5299 ,0x10},
   {0x529a ,0x00},
   {0x529b ,0x20},
   {0x529c ,0x00},
   {0x529d ,0x28},
   {0x529e ,0x00},
   {0x529f ,0x30},
   {0x5282 ,0x00},
   {0x5300 ,0x00},
   {0x5301 ,0x20},
   {0x5302 ,0x00},
   {0x5303 ,0x7c},
   {0x530c ,0x00},
   {0x530d ,0x0c},
   {0x530e ,0x20},
   {0x530f ,0x80},
   {0x5310 ,0x20},
   {0x5311 ,0x80},
   {0x5308 ,0x20},
   {0x5309 ,0x40},
   {0x5304 ,0x00},
   {0x5305 ,0x30},
   {0x5306 ,0x00},
   {0x5307 ,0x80},
   {0x5314 ,0x08},
   {0x5315 ,0x20},
   {0x5319 ,0x30},
   {0x5316 ,0x10},
   {0x5317 ,0x00},
   {0x5318 ,0x02},
   {0x5402 ,0x3f},
   {0x5403 ,0x00},
   {0x3406 ,0x00},
   {0x5180 ,0xff},
   {0x5181 ,0x52},
   {0x5182 ,0x11},
   {0x5183 ,0x14},
   {0x5184 ,0x25},
   {0x5185 ,0x24},
   {0x5186 ,0x06},
   {0x5187 ,0x08},
   {0x5188 ,0x08},
   {0x5189 ,0x7c},
   {0x518a ,0x60},
   {0x518b ,0xb2},
   {0x518c ,0xb2},
   {0x518d ,0x44},
   {0x518e ,0x3d},
   {0x518f ,0x58},
   {0x5190 ,0x46},
   {0x5191 ,0xf8},
   {0x5192 ,0x04},
   {0x5193 ,0x70},
   {0x5194 ,0xf0},
   {0x5195 ,0xf0},
   {0x5196 ,0x03},
   {0x5197 ,0x01},
   {0x5198 ,0x04},
   {0x5199 ,0x12},
   {0x519a ,0x04},
   {0x519b ,0x00},
   {0x519c ,0x06},
   {0x519d ,0x82},
   {0x519e ,0x00},
   {0x5025 ,0x80},
   {0x5583 ,0x40},
   {0x5584 ,0x40},
   {0x5580 ,0x02},
   {0x5000 ,0xcf},
   {0x3710 ,0x10},
   {0x3632 ,0x51},
   {0x3702 ,0x10},
   {0x3703 ,0xb2},
   {0x3704 ,0x18},
   {0x370b ,0x40},
   {0x370d ,0x03},
   {0x3631 ,0x01},
   {0x3632 ,0x52},
   {0x3606 ,0x24},
   {0x3620 ,0x96},
   {0x5785 ,0x07},
   {0x3a13 ,0x30},
   {0x3600 ,0x52},
   {0x3604 ,0x48},
   {0x3606 ,0x1b},
   {0x370d ,0x0b},
   {0x370f ,0xc0},
   {0x3709 ,0x01},
   {0x3823 ,0x00},
   {0x5007 ,0x00},
   {0x5009 ,0x00},
   {0x5011 ,0x00},
   {0x5013 ,0x00},
   {0x519e ,0x00},
   {0x5086 ,0x00},
   {0x5087 ,0x00},
   {0x5088 ,0x00},
   {0x5089 ,0x00},
   {0x302b ,0x00},
   {0x3808 ,0x01},
   {0x3809 ,0x40},
   {0x380a ,0x00},
   {0x380b ,0xf0},
   {0x3a00 ,0x78},
   {0x5001 ,0xFF},
   {0x5583 ,0x50},
   {0x5584 ,0x50},
   {0x5580 ,0x02},
   {0x3c01 ,0x80},
   {0x3c00 ,0x04},

   {0x5800 ,0x48},
   {0x5801 ,0x31},
   {0x5802 ,0x21},
   {0x5803 ,0x1b},
   {0x5804 ,0x1a},
   {0x5805 ,0x1e},
   {0x5806 ,0x29},
   {0x5807 ,0x38},
   {0x5808 ,0x26},
   {0x5809 ,0x17},
   {0x580a ,0x11},
   {0x580b ,0xe },
   {0x580c ,0xd },
   {0x580d ,0xe },
   {0x580e ,0x13},
   {0x580f ,0x1a},
   {0x5810 ,0x15},
   {0x5811 ,0xd },
   {0x5812 ,0x8 },
   {0x5813 ,0x5 },
   {0x5814 ,0x4 },
   {0x5815 ,0x5 },
   {0x5816 ,0x9 },
   {0x5817 ,0xd },
   {0x5818 ,0x11},
   {0x5819 ,0xa },
   {0x581a ,0x4 },
   {0x581b ,0x0 },
   {0x581c ,0x0 },
   {0x581d ,0x1 },
   {0x581e ,0x6 },
   {0x581f ,0x9 },
   {0x5820 ,0x12},
   {0x5821 ,0xb },
   {0x5822 ,0x4 },
   {0x5823 ,0x0 },
   {0x5824 ,0x0 },
   {0x5825 ,0x1 },
   {0x5826 ,0x6 },
   {0x5827 ,0xa },
   {0x5828 ,0x17},
   {0x5829 ,0xf },
   {0x582a ,0x9 },
   {0x582b ,0x6 },
   {0x582c ,0x5 },
   {0x582d ,0x6 },
   {0x582e ,0xa },
   {0x582f ,0xe },
   {0x5830 ,0x28},
   {0x5831 ,0x1a},
   {0x5832 ,0x11},
   {0x5833 ,0xe },
   {0x5834 ,0xe },
   {0x5835 ,0xf },
   {0x5836 ,0x15},
   {0x5837 ,0x1d},
   {0x5838 ,0x6e},
   {0x5839 ,0x39},
   {0x583a ,0x27},
   {0x583b ,0x1f},
   {0x583c ,0x1e},
   {0x583d ,0x23},
   {0x583e ,0x2f},
   {0x583f ,0x41},
   {0x5840 ,0xe },
   {0x5841 ,0xc },
   {0x5842 ,0xd },
   {0x5843 ,0xc },
   {0x5844 ,0xc },
   {0x5845 ,0xc },
   {0x5846 ,0xc },
   {0x5847 ,0xc },
   {0x5848 ,0xd },
   {0x5849 ,0xe },
   {0x584a ,0xe },
   {0x584b ,0xa },
   {0x584c ,0xe },
   {0x584d ,0xe },
   {0x584e ,0x10},
   {0x584f ,0x10},
   {0x5850 ,0x11},
   {0x5851 ,0xa },
   {0x5852 ,0xf },
   {0x5853 ,0xe },
   {0x5854 ,0x10},
   {0x5855 ,0x10},
   {0x5856 ,0x10},
   {0x5857 ,0xa },
   {0x5858 ,0xe },
   {0x5859 ,0xe },
   {0x585a ,0xf },
   {0x585b ,0xf },
   {0x585c ,0xf },
   {0x585d ,0xa },
   {0x585e ,0x9 },
   {0x585f ,0xd },
   {0x5860 ,0xc },
   {0x5861 ,0xb },
   {0x5862 ,0xd },
   {0x5863 ,0x7 },
   {0x5864 ,0x17},
   {0x5865 ,0x14},
   {0x5866 ,0x18},
   {0x5867 ,0x18},
   {0x5868 ,0x16},
   {0x5869 ,0x12},
   {0x586a ,0x1b},
   {0x586b ,0x1a},
   {0x586c ,0x16},
   {0x586d ,0x16},
   {0x586e ,0x18},
   {0x586f ,0x1f},
   {0x5870 ,0x1c},
   {0x5871 ,0x16},
   {0x5872 ,0x10},
   {0x5873 ,0xf },
   {0x5874 ,0x13},
   {0x5875 ,0x1c},
   {0x5876 ,0x1e},
   {0x5877 ,0x17},
   {0x5878 ,0x11},
   {0x5879 ,0x11},
   {0x587a ,0x14},
   {0x587b ,0x1e},
   {0x587c ,0x1c},
   {0x587d ,0x1c},
   {0x587e ,0x1a},
   {0x587f ,0x1a},
   {0x5880 ,0x1b},
   {0x5881 ,0x1f},
   {0x5882 ,0x14},
   {0x5883 ,0x1a},
   {0x5884 ,0x1d},
   {0x5885 ,0x1e},
   {0x5886 ,0x1a},
   {0x5887 ,0x1a},

   {0x5180 ,0xff},
   {0x5181 ,0x52},
   {0x5182 ,0x11},
   {0x5183 ,0x14},
   {0x5184 ,0x25},
   {0x5185 ,0x24},
   {0x5186 ,0x14},
   {0x5187 ,0x14},
   {0x5188 ,0x14},
   {0x5189 ,0x69},
   {0x518a ,0x60},
   {0x518b ,0xa2},
   {0x518c ,0x9c},
   {0x518d ,0x36},
   {0x518e ,0x34},
   {0x518f ,0x54},
   {0x5190 ,0x4c},
   {0x5191 ,0xf8},
   {0x5192 ,0x04},
   {0x5193 ,0x70},
   {0x5194 ,0xf0},
   {0x5195 ,0xf0},
   {0x5196 ,0x03},
   {0x5197 ,0x01},
   {0x5198 ,0x05},
   {0x5199 ,0x2f},
   {0x519a ,0x04},
   {0x519b ,0x00},
   {0x519c ,0x06},
   {0x519d ,0xa0},
   {0x519e ,0xa0},

   {0x528a ,0x00},
   {0x528b ,0x01},
   {0x528c ,0x04},
   {0x528d ,0x08},
   {0x528e ,0x10},
   {0x528f ,0x20},
   {0x5290 ,0x30},
   {0x5292 ,0x00},
   {0x5293 ,0x00},
   {0x5294 ,0x00},
   {0x5295 ,0x01},
   {0x5296 ,0x00},
   {0x5297 ,0x04},
   {0x5298 ,0x00},
   {0x5299 ,0x08},
   {0x529a ,0x00},
   {0x529b ,0x10},
   {0x529c ,0x00},
   {0x529d ,0x20},
   {0x529e ,0x00},
   {0x529f ,0x30},
   {0x5282 ,0x00},
   {0x5300 ,0x00},
   {0x5301 ,0x20},
   {0x5302 ,0x00},
   {0x5303 ,0x7c},
   {0x530c ,0x00},
   {0x530d ,0x10},
   {0x530e ,0x20},
   {0x530f ,0x80},
   {0x5310 ,0x20},
   {0x5311 ,0x80},
   {0x5308 ,0x20},
   {0x5309 ,0x40},
   {0x5304 ,0x00},
   {0x5305 ,0x30},
   {0x5306 ,0x00},
   {0x5307 ,0x80},
   {0x5314 ,0x08},
   {0x5315 ,0x20},
   {0x5319 ,0x30},
   {0x5316 ,0x10},
   {0x5317 ,0x00},
   {0x5318 ,0x02},

   {0x5380 ,0x01},
   {0x5381 ,0x00},
   {0x5382 ,0x00},
   {0x5383 ,0x1f},
   {0x5384 ,0x00},
   {0x5385 ,0x06},
   {0x5386 ,0x00},
   {0x5387 ,0x00},
   {0x5388 ,0x00},
   {0x5389 ,0xE1},
   {0x538A ,0x00},
   {0x538B ,0x2B},
   {0x538C ,0x00},
   {0x538D ,0x00},
   {0x538E ,0x00},
   {0x538F ,0x10},
   {0x5390 ,0x00},
   {0x5391 ,0xB3},
   {0x5392 ,0x00},
   {0x5393 ,0xA6},
   {0x5394 ,0x08},

   {0x5480 ,0x0c},
   {0x5481 ,0x18},
   {0x5482 ,0x2f},
   {0x5483 ,0x55},
   {0x5484 ,0x64},
   {0x5485 ,0x71},
   {0x5486 ,0x7d},
   {0x5487 ,0x87},
   {0x5488 ,0x91},
   {0x5489 ,0x9a},
   {0x548A ,0xaa},
   {0x548B ,0xb8},
   {0x548C ,0xcd},
   {0x548D ,0xdd},
   {0x548E ,0xea},
   {0x548F ,0x1d},
   {0x5490 ,0x05},
   {0x5491 ,0x00},
   {0x5492 ,0x04},
   {0x5493 ,0x20},
   {0x5494 ,0x03},
   {0x5495 ,0x60},
   {0x5496 ,0x02},
   {0x5497 ,0xB8},
   {0x5498 ,0x02},
   {0x5499 ,0x86},
   {0x549A ,0x02},
   {0x549B ,0x5B},
   {0x549C ,0x02},
   {0x549D ,0x3B},
   {0x549E ,0x02},
   {0x549F ,0x1C},
   {0x54A0 ,0x02},
   {0x54A1 ,0x04},
   {0x54A2 ,0x01},
   {0x54A3 ,0xED},
   {0x54A4 ,0x01},
   {0x54A5 ,0xC5},
   {0x54A6 ,0x01},
   {0x54A7 ,0xA5},
   {0x54A8 ,0x01},
   {0x54A9 ,0x6C},
   {0x54AA ,0x01},
   {0x54AB ,0x41},
   {0x54AC ,0x01},
   {0x54AD ,0x20},
   {0x54AE ,0x00},
   {0x54AF ,0x16},
   {0x54B0 ,0x01},
   {0x54B1 ,0x20},
   {0x54B2 ,0x00},
   {0x54B3 ,0x10},
   {0x54B4 ,0x00},
   {0x54B5 ,0xf0},
   {0x54B6 ,0x00},
   {0x54B7 ,0xDF},
   {0x5402 ,0x3f},
   {0x5403 ,0x00},

   {0x5500 ,0x10},
   {0x5502 ,0x00},
   {0x5503 ,0x06},
   {0x5504 ,0x00},
   {0x5505 ,0x7f},

   {0x5025 ,0x80},
   {0x3a0f ,0x30},
   {0x3a10 ,0x28},
   {0x3a1b ,0x30},
   {0x3a1e ,0x28},
   {0x3a11 ,0x61},
   {0x3a1f ,0x10},
   {0x5688 ,0xfd},
   {0x5689 ,0xdf},
   {0x568a ,0xfe},
   {0x568b ,0xef},
   {0x568c ,0xfe},
   {0x568d ,0xef},
   {0x568e ,0xaa},
   {0x568f ,0xaa},

   {0xffff,0xff},

   /* YUV to JPEG settings */
/*   {0x460c, 0x02},
   {0x460b, 0x35},
   {0x471d, 0x00},
   {0x3002, 0x0c},
   {0x3002, 0x00},
   {0x4713, 0x03},
   {0x471c, 0x50},
   {0x3815, 0x44},
   {0x3818, 0x0f},
   {0x3006, 0xff},
   {0x3007, 0x3f},
*/
};
Pethead
Posts: 37
Joined: Mon Jul 11, 2016 1:47 pm
Has thanked: 1 time

Re: OV5642/OV7725 only outputting the same data

Post by Pethead »

Should be

Code: Select all

for (i = 0; i < sizeof(OV5642_QVGA)/ sizeof(sensor_reg); i++)
in ov5642_TransferConfig, accidently pasted wrong code for that for loop.. Problem still remains!
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: OV5642/OV7725 only outputting the same data

Post by Giovanni »

Never used that, I hope somebody else can help here.

I remember there was a silicon errata related to DCMI and DMA on F4, you should verify this.

Giovanni
Pethead
Posts: 37
Joined: Mon Jul 11, 2016 1:47 pm
Has thanked: 1 time

Re: OV5642/OV7725 only outputting the same data

Post by Pethead »

In the datasheet for the module it says that the HSYNC and VSYNC pins are both active high. However, when I looked at the oscilloscope on both HSYNC and VSYNC pins they were never high at the same time. I changed the HREF be active low in the DCMI configuration and configured the camera to output a test pattern and you can clearly tell that it is the test pattern is received in a very distorted way, the received picture is very green with a lot of noise.

The received image is just a 1/4 of the test pattern image since I'm just capturing 320x120 pixels right now.

Any ideas of why the picture is so green?

I've written this python script to receive the image and save it to a file:

Code: Select all

import socket
import sys
import binascii
 
host = '10.1.41.46'
port = 8001

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))

img = open('test.yuv', 'wb')

sock.send("d".encode())   # server sends when it receives a msg

data = bytearray()      # create empty byte array

n = 320*120            # Number of bytes to receive

while len(data) < n:
   packet = sock.recv(n-1)

   if not packet:
      break
   data += packet      # append packet to byte array
   
print(binascii.hexlify(data))
print("Received bytes:", sys.getsizeof(data))
img.write(binascii.hexlify(data))

img.close()
sock.close()

And this script converts the YUV422 format to RGB and saves it in a .bmp file

Code: Select all

import Image
import sys
from struct import *
import array
 
if len(sys.argv) != 4:
        print("***** Usage syntax Error!!!! *****\n")
        print("Usage:")
        print("python <script> <.yuv file uyvy422> <width&gt> <height>")
        sys.exit(1) # exit
else:
        pass
 
image_name = sys.argv[1]
width = int(sys.argv[2])
height = int(sys.argv[3])
 
y = array.array('B')
u = array.array('B')
v = array.array('B')
 
f_uyvy = open(image_name, "rb")
f_uv = open(image_name, "rb")
f_uv.seek(width*height, 1)
 
image_out = Image.new("RGB", (width, height))
pix = image_out.load()
 
print("width=", width, "height=", height)
 
for i in range(0,height):
    for j in range(0, width/2):
        u  = ord(f_uyvy.read(1));
        y1 = ord(f_uyvy.read(1));
        v  = ord(f_uyvy.read(1));
        y2 = ord(f_uyvy.read(1));
 
        B = 1.164 * (y1-16) + 2.018 * (u - 128)
        G = 1.164 * (y1-16) - 0.813 * (v - 128) - 0.391 * (u - 128)
        R = 1.164 * (y1-16) + 1.596*(v - 128)
        pix[j*2, i] = int(R), int(G), int(B)
 
        B = 1.164 * (y2-16) + 2.018 * (u - 128)
        G = 1.164 * (y2-16) - 0.813 * (v - 128) - 0.391 * (u - 128)
        R = 1.164 * (y2-16) + 1.596*(v - 128)
        pix[j*2+1, i] = int(R), int(G), int(B)
 
######################################################
# B = 1.164(Y - 16)                   + 2.018(U - 128)
# G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
# R = 1.164(Y - 16) + 1.596(V - 128)
######################################################
 
image_out.save("out.bmp")
Attachments
out.png
out.png (2.39 KiB) Viewed 7277 times
testPattern.png
Pethead
Posts: 37
Joined: Mon Jul 11, 2016 1:47 pm
Has thanked: 1 time

Re: OV5642/OV7725 only outputting the same data

Post by Pethead »

I configured the camera to output RGB565 data instead in the format R[4:0], G[5:3], G[2:0], B[4:0] and made a simple application that connects to the device and receives the data and converts it to RGB888.

Code: Select all

    for (int i = 0;i < 320;i++) 
    {
        for (int j = 0;j < 120;j++)
        {
            int pos = i * 2 + j * 320 * 2;

            if (mData.size() > (pos + 1))
            {

                quint16 rgb565 = ((quint8)mData.at(pos + 0) << 8) | (quint8)mData.at(pos + 1);
                quint8 r = (rgb565 >> 8) & 0xF8;
                quint8 g = (rgb565 >> 3) & 0xFC;
                quint8 b = (rgb565 << 3) & 0xF8;

                if (j == 0)
                {
                    qDebug() << r << g << b << rgb565 << (quint8)mData.at(pos + 0) << (quint8)mData.at(pos + 1);
                }

                QColor pixel(r, g, b);
                img.setPixel(i, j, pixel.rgb());
            }
        }
    }

However, the color seems to be off by quite a bit and I can't figure out what is wrong, I've checked the DCMI data lines and I've had each one of them set to Vcc separately in order to see that I only receive the bit pattern for 1, 2, 4, 8, 16, 32, 64, 128 etc.

When I should receive black (0,0,0) I instead receive (128,0,128) and for white it is (128,252,128) when it should be (255,255,255) or close to atleast...

I'm pretty sure that my conversion is correct or have i done some stupid misstake?

Any ideas?
Attachments
testPicture.png
testPicture.png (11.54 KiB) Viewed 7253 times
color_bar.png
color_bar.png (6.79 KiB) Viewed 7253 times
Pethead
Posts: 37
Joined: Mon Jul 11, 2016 1:47 pm
Has thanked: 1 time

Re: OV5642/OV7725 only outputting the same data

Post by Pethead »

I changed to the ov7725 camera instead and it works like a charm. However, the LWIP has huge packet loss when I'm sending the pictures since so when I try to display the images it's super slow, not even 1 fps.
Post Reply