stm32f4中用SD卡存儲(chǔ)DCMI的圖像
因?yàn)樽约罕究谱龅膭?chuàng)新性實(shí)驗(yàn)和飛思卡爾小車(chē)都是攝像頭的,研究生也做的視頻處理方向。后來(lái),想做一個(gè)小視頻監(jiān)制,閑麻煩,沒(méi)有用那TI的DM6446,就用的手頭stm32f4開(kāi)發(fā)板,由于沒(méi)有LCD顯示屏,我只能直接把DCMI圖像保存在內(nèi)部RAM中,再保存到SD里,在上位機(jī)讀取SD卡轉(zhuǎn)換成圖片,我就用VC+OPENCV。
現(xiàn)在說(shuō)說(shuō)做的流程吧。攝像頭是買(mǎi)的OV9665 。直接接的是DCMI接口。而SD卡不能接SDIO了,因?yàn)槲疫@開(kāi)發(fā)板是100引腳封裝的,SDIO和DCMI復(fù)用引腳沖突。之后SD卡選用的是SPI接口。
1.關(guān)于SPI接口的SD卡讀寫(xiě)操作,我在前幾篇博客中寫(xiě)過(guò),也附帶了寫(xiě)好的FATFS文件系統(tǒng)程序,大家可以參考,我這里就不多寫(xiě)了。
2.重要的是關(guān)于DCMI的攝像頭接口,主要是在DCMI的配置上和DMA的配置,下面著重進(jìn)行講解。
void OV9655_HW_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStruct;
/*** Configures the DCMI GPIOs to interface with the OV9655 camera module ***/
/* Enable DCMI GPIOs clocks */
/* Enable DCMI GPIOs clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC |
RCC_AHB1Periph_GPIOE, ENABLE);
/* Enable DCMI clock */
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE);
/* Connect DCMI pins to AF13 ************************************************/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_DCMI);//HSYNC
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_DCMI);//PIXCLK
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_DCMI);//DCMI_D5
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_DCMI);//VSYNC
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_DCMI);//DCMI_D0
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_DCMI);//DCMI_D1
GPIO_PinAFConfig(GPIOE, GPIO_PinSource0, GPIO_AF_DCMI);//DCMI_D2
GPIO_PinAFConfig(GPIOE, GPIO_PinSource1, GPIO_AF_DCMI);//DCMI_D3
GPIO_PinAFConfig(GPIOE, GPIO_PinSource4, GPIO_AF_DCMI);//DCMI_D4
GPIO_PinAFConfig(GPIOE, GPIO_PinSource5, GPIO_AF_DCMI);//DCMI_D6
GPIO_PinAFConfig(GPIOE, GPIO_PinSource6, GPIO_AF_DCMI);//DCMI_D7
/* DCMI GPIO configuration **************************************************/
/* HSYNC(PA4), PIXCLK(PA6) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* DCMI_D5(PB6), VSYNC(PB7) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* DCMI_D0(PC6), DCMI_D1(PC7) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* DCMI_D2(PE0), DCMI_D3(PE1), DCMI_D4(PE4), DCMI_D6(PE5), DCMI_D7(PE6),*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/****** Configures the I2C1 used for OV9655 camera module configuration *****/
/* I2C1 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
/* GPIOB clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* Connect I2C1 pins to AF4 */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
/* Configure I2C1 GPIOs */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure I2C1 */
/* I2C DeInit */
I2C_DeInit(I2C1);
/* Enable the I2C peripheral */
I2C_Cmd(I2C1, ENABLE);
/* Set the I2C structure parameters */
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStruct.I2C_OwnAddress1 = 0xFE;
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStruct.I2C_ClockSpeed = 30000;
/* Initialize the I2C peripheral w/ selected parameters */
I2C_Init(I2C1, &I2C_InitStruct);
}
關(guān)于DCMI和DMA的初始化程序如下
void OV9655_Init(ImageFormat_TypeDef ImageFormat)
{
DCMI_InitTypeDef DCMI_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
/*** Configures the DCMI to interface with the OV9655 camera module ***/
/* Enable DCMI clock */
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE);
/* DCMI configuration */
DCMI_InitStructure.DCMI_CaptureMode = DCMI_CaptureMode_SnapShot;//DCMI_CaptureMode_Continuous;
DCMI_InitStructure.DCMI_SynchroMode = DCMI_SynchroMode_Hardware;
DCMI_InitStructure.DCMI_PCKPolarity = DCMI_PCKPolarity_Falling;
DCMI_InitStructure.DCMI_VSPolarity = DCMI_VSPolarity_High;
DCMI_InitStructure.DCMI_HSPolarity = DCMI_HSPolarity_High;
DCMI_InitStructure.DCMI_CaptureRate = DCMI_CaptureRate_1of4_Frame;//DCMI_CaptureRate_All_Frame;
DCMI_InitStructure.DCMI_ExtendedDataMode = DCMI_ExtendedDataMode_8b;
//----- mask interrupt for DCMI -----
DCMI_ITConfig(DCMI_IT_VSYNC, ENABLE);
DCMI_ITConfig(DCMI_IT_LINE, ENABLE);
DCMI_ITConfig(DCMI_IT_FRAME, ENABLE);
DCMI_ITConfig(DCMI_IT_OVF, ENABLE);
DCMI_ITConfig(DCMI_IT_ERR, ENABLE);
/* Configures the DMA2 to transfer Data from DCMI */
/* Enable DMA2 clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
/* DMA2 Stream1 Configuration */
DMA_DeInit(DMA2_Stream1);
DMA_InitStructure.DMA_Channel = DMA_Channel_1;
DMA_InitStructure.DMA_PeripheralBaseAddr = DCMI_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)theMap;//FSMC_LCD_ADDRESS;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 9600;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
//------------------------中斷
DMA_ITConfig(DMA2_Stream1, DMA_IT_TC, ENABLE);
DMA_ITConfig(DMA2_Stream1, DMA_IT_HT, ENABLE);
DMA_ITConfig(DMA2_Stream1, DMA_IT_TE, ENABLE);
DMA_ITConfig(DMA2_Stream1, DMA_IT_FE, ENABLE);
switch(ImageFormat)
{
case BMP_QQVGA:
{
/* DCMI configuration */
DCMI_Init(&DCMI_InitStructure);
/* DMA2 IRQ channel Configuration */
DMA_Init(DMA2_Stream1, &DMA_InitStructure);
break;
}
case BMP_QVGA:
{
/* DCMI configuration */
DCMI_Init(&DCMI_InitStructure);
/* DMA2 IRQ channel Configuration */
DMA_Init(DMA2_Stream1, &DMA_InitStructure);
break;
}
default:
{
/* DCMI configuration */
DCMI_Init(&DCMI_InitStructure);
/* DMA2 IRQ channel Configuration */
DMA_Init(DMA2_Stream1, &DMA_InitStructure);
break;
}
}
}
其中DCMI_InitStructure.DCMI_CaptureMode這里選用的是DCMI_CaptureMode_SnapShot,沒(méi)有選用DCMI_CaptureMode_Continuous,因?yàn)槌绦虼鎸D像存SD里,速度有限,只能采一張,存一張。
所以,只要DCMI_CaptureCmd(ENABLE);DCMI就開(kāi)始拍照一張,拍完一張后,使能自動(dòng)關(guān)閉。下次要再拍照的時(shí)候,重新DCMI_CaptureCmd(ENABLE); 即可。
然后關(guān)于DMA,這
個(gè)配置讓我頭疼了些時(shí)間,主要是對(duì)DMA不熟。
DMA_InitStructure.DMA_Mode 用的是 DMA_Mode_Circular模式,因?yàn)镈CMI用的是單張拍照的,所以這里用DMA_Mode_Circular模式?jīng)]有問(wèn)題。
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;//外設(shè)的數(shù)據(jù)字長(zhǎng),DCMI的寄存器是32位的,所以這里選的word
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//內(nèi)存的數(shù)據(jù)字長(zhǎng),我存圖像用的數(shù)組是unsigned char類(lèi)型,所以這里用BYTE。
DMA_InitStructure.DMA_BufferSize = 9600;//關(guān)鍵是這個(gè),因?yàn)閿z像頭設(shè)置的圖像大小為120*160的,是RGB565格式,一個(gè)像素點(diǎn)占兩個(gè)字節(jié),所以存一幅圖像需要38400字節(jié)。而這個(gè)buffersize是相對(duì)于DMA數(shù)據(jù)源的字長(zhǎng)來(lái)說(shuō)的,這里就是對(duì)于DCMI的數(shù)據(jù)寄存器word類(lèi)型,38400/4=9600;
還有關(guān)于上位機(jī)軟件讀取這個(gè)文件時(shí)的方法,我用的是VC+OPENCV,38400字節(jié)中,是每?jī)蓚€(gè)字節(jié)表示一個(gè)像素點(diǎn)的RGB顏色,這兩個(gè)字節(jié)是低字節(jié)在前,高字節(jié)在后,示意方法如下
for(int i=0; i
{
rgb565 = p[2*i+0] + 256*p[2*i+1];
b = (rgb565>>0) & 0x001f;
g = (rgb565>>5) & 0x003f;
r = (rgb565>>11) & 0x001f;
b = b<<3;
g = g<<2;
r = r<<3;
vec.push_back(b);
vec.push_back(g);
vec.push_back(r);
}
在OPENCV中存儲(chǔ)圖像的程序示意如下:
void vecToImage(vector& vec, IplImage* pImg8u3)
{
int cnt=0;
for(int y=0; yheight; y++)
{
unsigned char* ptr = (unsigned char*)(pImg8u3->imageData + y * pImg8u3->widthStep);
for(int x=0; xwidth; x++)
{
*(ptr + 3*x+0) = vec.at(cnt++);
*(ptr + 3*x+1) = vec.at(cnt++);
*(ptr + 3*x+2) = vec.at(cnt++);
}
}
}