cancel
Showing results for 
Search instead for 
Did you mean: 

OV7670 with stm32f4-discovery?

ctc.ctc
Associate II
Posted on January 11, 2013 at 18:51

I have a project with OV7670 with stm32f4-discovery. My OV7670 has 16 pins and without fifo. And I don't have LCD. I hope I can use OV7670 to get view into stm32f4 and transmit data to smart phone and get the real time video.

I download a sample code and modify value and buffer position. But I still have no idea how to get a complete picture from ov7670. Now I set ov7670 with QCIF format(176*144) and RGB565. I set stm32f4 dcmi to snapshot that I can get one picture at one time. Then I dump the buffer memory to android phone and use bitmap to transform a picture from the raw data. But I can get only black with black view, and other color become dark purple-red just like the attach picture.

http://postimage.org/image/tah877urb/

 So I  use black paper to do some test. The first image of picture is that I cover the upper part of ov7670 with black paper. Second is cover upper-right part. Third, is cover upper-left part. I can only find that the second black add third black almost equal first black. But I have no idea why there are purple-red color. Here is my code:

https://github.com/ctc8631/ov7670

 

I'm learning the embedded system. Though I'm try many ways as I can figure out, but it didn't work at all. Can anyone give me some direction how can I do? Thanks a lot.

#stm32f4-ov7670
5 REPLIES 5
frankmeyer9
Associate II
Posted on January 16, 2013 at 12:57

Maybe this thread can provide you some useful information:

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/DCMI%20camera%20interface%20program%20with%20DMA%20with%20STM32f4-discovery%20board&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/AllItems.aspx&currentviews=1394]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fcortex_mx_stm32%2fDCMI%20camera%20interface%20program%20with%20DMA%20with%20STM32f4-discovery%20board&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&TopicsView=https%3A%2F%2Fmy.st.com%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2FAllItems.aspx&currentviews=1394

armindavatgaran
Associate III
Posted on April 22, 2015 at 16:41

Hello

I can write to registers but can't read them, what is the reason?

I supply xclk pin with 42MHz sysclk coming from MCO2.

Thanks. 
Posted on April 22, 2015 at 17:01

Spamming dozens of old threads here with the same question, and insufficient circuit or software context, will win you no friends.

Very few people participate here.

You'll need to break out your scope or logic analyzer, observe all the interactions, and read the manuals thoroughly.

Problems with I2C, make sure you drive the pins open-drain, with external pull-ups, if the I2C peripheral is causing issues, try bit banging the bus to confirm expected functionality.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
armindavatgaran
Associate III
Posted on June 20, 2015 at 13:04

Hello

I use pb6 and pb9 pins as I2C1 that are pulled up in stm32f4discovery, supply xclk pin with 6MHz from mco2 and use stm32cubef4 functions as source code,here is I2C functions:


HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)

{

/* Check the parameters */

assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));


if
(hi2c->State == HAL_I2C_STATE_READY)

{

if
((pData == NULL) || (Size == 0))

{

return
HAL_ERROR;

}


if
(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)

{

return
HAL_BUSY;

}


/* Process Locked */

__HAL_LOCK(hi2c);


hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;

hi2c->ErrorCode = HAL_I2C_ERROR_NONE;


/* Send Slave Address and Memory Address */

if
(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)

{

if
(hi2c->ErrorCode == HAL_I2C_ERROR_AF)

{

/* Process Unlocked */

__HAL_UNLOCK(hi2c);

return
HAL_ERROR;

}

else

{

/* Process Unlocked */

__HAL_UNLOCK(hi2c);

return
HAL_TIMEOUT;

}

}


while
(Size > 0)

{

/* Wait until TXE flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


/* Write data to DR */

hi2c->Instance->DR = (*pData++);

Size--;


if
((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (Size != 0))

{

/* Write data to DR */

hi2c->Instance->DR = (*pData++);

Size--;

}

}


/* Wait until TXE flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


/* Generate Stop */

hi2c->Instance->CR1 |= I2C_CR1_STOP;


/* Wait until BUSY flag is reset */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


hi2c->State = HAL_I2C_STATE_READY;


/* Process Unlocked */

__HAL_UNLOCK(hi2c);


return
HAL_OK;

}

else

{

return
HAL_BUSY;

}

}


static
HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)

{

/* Generate Start */

hi2c->Instance->CR1 |= I2C_CR1_START;


/* Wait until SB flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


/* Send slave address */

hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);


/* Wait until ADDR flag is set */

if
(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)

{

if
(hi2c->ErrorCode == HAL_I2C_ERROR_AF)

{

return
HAL_ERROR;

}

else

{

return
HAL_TIMEOUT;

}

}


/* Clear ADDR flag */

__HAL_I2C_CLEAR_ADDRFLAG(hi2c);


/* Wait until TXE flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


/* If Memory address size is 8Bit */

if
(MemAddSize == I2C_MEMADD_SIZE_8BIT)

{

/* Send Memory Address */

hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);

}

/* If Memory address size is 16Bit */

else

{

/* Send MSB of Memory Address */

hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);


/* Wait until TXE flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


/* Send LSB of Memory Address */

hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);

}


return
HAL_OK;

}


HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)

{

/* Check the parameters */

assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));


if
(hi2c->State == HAL_I2C_STATE_READY)

{

if
((pData == NULL) || (Size == 0))

{

return
HAL_ERROR;

}


if
(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)

{

return
HAL_BUSY;

}


/* Process Locked */

__HAL_LOCK(hi2c);


hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;

hi2c->ErrorCode = HAL_I2C_ERROR_NONE;


/* Send Slave Address and Memory Address */

if
(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout) != HAL_OK)

{

if
(hi2c->ErrorCode == HAL_I2C_ERROR_AF)

{

/* Process Unlocked */

__HAL_UNLOCK(hi2c);

return
HAL_ERROR;

}

else

{

/* Process Unlocked */

__HAL_UNLOCK(hi2c);

return
HAL_TIMEOUT;

}

}


if
(Size == 1)

{

/* Disable Acknowledge */

hi2c->Instance->CR1 &= ~I2C_CR1_ACK;


/* Clear ADDR flag */

__HAL_I2C_CLEAR_ADDRFLAG(hi2c);


/* Generate Stop */

hi2c->Instance->CR1 |= I2C_CR1_STOP;

}

else
if
(Size == 2)

{

/* Disable Acknowledge */

hi2c->Instance->CR1 &= ~I2C_CR1_ACK;


/* Enable Pos */

hi2c->Instance->CR1 |= I2C_CR1_POS;


/* Clear ADDR flag */

__HAL_I2C_CLEAR_ADDRFLAG(hi2c);

}

else

{

/* Clear ADDR flag */

__HAL_I2C_CLEAR_ADDRFLAG(hi2c);

}


while
(Size > 0)

{

if
(Size <= 3)

{

/* One byte */

if
(Size== 1)

{

/* Wait until RXNE flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


/* Read data from DR */

(*pData++) = hi2c->Instance->DR;

Size--;

}

/* Two bytes */

else
if
(Size == 2)

{

/* Wait until BTF flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


/* Generate Stop */

hi2c->Instance->CR1 |= I2C_CR1_STOP;


/* Read data from DR */

(*pData++) = hi2c->Instance->DR;

Size--;


/* Read data from DR */

(*pData++) = hi2c->Instance->DR;

Size--;

}

/* 3 Last bytes */

else

{

/* Wait until BTF flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


/* Disable Acknowledge */

hi2c->Instance->CR1 &= ~I2C_CR1_ACK;


/* Read data from DR */

(*pData++) = hi2c->Instance->DR;

Size--;


/* Wait until BTF flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


/* Generate Stop */

hi2c->Instance->CR1 |= I2C_CR1_STOP;


/* Read data from DR */

(*pData++) = hi2c->Instance->DR;

Size--;


/* Read data from DR */

(*pData++) = hi2c->Instance->DR;

Size--;

}

}

else

{

/* Wait until RXNE flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


/* Read data from DR */

(*pData++) = hi2c->Instance->DR;

Size--;


if
(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)

{

/* Read data from DR */

(*pData++) = hi2c->Instance->DR;

Size--;

}

}

}


/* Disable Pos */

hi2c->Instance->CR1 &= ~I2C_CR1_POS;


/* Wait until BUSY flag is reset */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


hi2c->State = HAL_I2C_STATE_READY;


/* Process Unlocked */

__HAL_UNLOCK(hi2c);


return
HAL_OK;

}

else

{

return
HAL_BUSY;

}

}


static
HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout)

{

/* Enable Acknowledge */

hi2c->Instance->CR1 |= I2C_CR1_ACK;


/* Generate Start */

hi2c->Instance->CR1 |= I2C_CR1_START;


/* Wait until SB flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


/* Send slave address */

hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);


/* Wait until ADDR flag is set */

if
(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)

{

if
(hi2c->ErrorCode == HAL_I2C_ERROR_AF)

{

return
HAL_ERROR;

}

else

{

return
HAL_TIMEOUT;

}

}


/* Clear ADDR flag */

__HAL_I2C_CLEAR_ADDRFLAG(hi2c);


/* Wait until TXE flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


/* If Memory address size is 8Bit */

if
(MemAddSize == I2C_MEMADD_SIZE_8BIT)

{

/* Send Memory Address */

hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);

}

/* If Memory address size is 16Bit */

else

{

/* Send MSB of Memory Address */

hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);


/* Wait until TXE flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


/* Send LSB of Memory Address */

hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);

}


/* Wait until TXE flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;

}


/* Generate Restart */

hi2c->Instance->CR1 |= I2C_CR1_START;


/* Wait until SB flag is set */

if
(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)

{

return
HAL_TIMEOUT;
4
}
4
4
/* Send slave address */
4
hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
4
4
/* Wait until ADDR flag is set */
4
if
(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout) != HAL_OK)
4
{
4
if
(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
4
{
4
return
HAL_ERROR;
4
}
4
else
4
{
4
return
HAL_TIMEOUT;
4
}
4
}
4
4
return
HAL_OK;
4
}

armindavatgaran
Associate III
Posted on June 28, 2015 at 16:27

Hello

My problem was solved,  it was related to the incompatibility of SCCB protocol andHAL_I2C_Mem_Read function of the stm32cubef4 software package.