uint8_t * OV7670_capture_image() { DCMI_InitTypeDef DCMI_InitStructure; DMA_InitTypeDef DMA_InitStructure; /* DMA2 Stream1 Configuration */ DMA_DeInit(DMA2_Stream1); DMA_InitStructure.DMA_Channel = DMA_Channel_1; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&DCMI->DR); DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) image_buffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = (IMG_SIZE/sizeof(uint32_t)); 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_Word; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; /* DMA2 IRQ channel Configuration */ DMA_Init(DMA2_Stream1, &DMA_InitStructure); DMA_Cmd(DMA2_Stream1, ENABLE); DCMI_Cmd(ENABLE); DCMI_CaptureCmd(ENABLE); int temp; // Wait until DCMI DMA Rx transfer is complete while (DMA_GetFlagStatus(DMA2_Stream1, DMA_FLAG_TCIF1) == RESET) { temp = DMA_GetCurrDataCounter(DMA2_Stream1); temp = DCMI->CR; temp = DCMI->RISR; } /* Clear all DMA Streams flags */ DMA_ClearFlag(DMA2_Stream1, DMA_FLAG_HTIF1 | DMA_FLAG_TCIF1); /* Disable the DMA Rx Stream */ DMA_Cmd(DMA2_Stream1, DISABLE); //Do not disable DCMI peripheral untill DMA disable command gets through while(DMA_GetCmdStatus(DMA2_Stream1) == ENABLE) {} DCMI_Cmd(DISABLE); return (uint8_t*) image_buffer; }
void Dcmi::DisableDCMI(){ DCMI_CaptureCmd(DISABLE); DCMI_Cmd(DISABLE); DMA_Cmd(DMA2_Stream1, DISABLE); DCMI_CROPCmd(DISABLE); DCMI_ITConfig(DCMI_IT_FRAME, DISABLE); }
/** * @brief Disable DCMI DMA stream */ void dcmi_dma_disable() { /* Disable DMA2 stream 1 and DCMI interface then stop image capture */ DMA_Cmd(DMA2_Stream1, DISABLE); DCMI_Cmd(DISABLE); DCMI_CaptureCmd(DISABLE); }
/** * @brief Enable DCMI DMA stream */ void dcmi_dma_enable() { /* Enable DMA2 stream 1 and DCMI interface then start image capture */ DMA_Cmd(DMA2_Stream1, ENABLE); DCMI_Cmd(ENABLE); DCMI_CaptureCmd(ENABLE); dma_it_init(); }
void Dcmi::EnableDCMI(){ 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); DMA_Cmd(DMA2_Stream1, ENABLE); DCMI_Cmd(ENABLE); }
/** * @brief Initialize image capture process (start its thread) * @param None * @retval None */ void IPCAM_ImageCaptureInit(void) { /* Initializes the DCMI interface (I2C and GPIO) used to configure the camera */ OV2640_HW_Init(); /* Read the OV9655/OV2640 Manufacturer identifier */ OV9655_ReadID(&OV9655_Camera_ID); OV2640_ReadID(&OV2640_Camera_ID); if(OV9655_Camera_ID.PID == 0x96) { IPCAM_ImageBuffer.Camera = OV9655_CAMERA; } else if(OV2640_Camera_ID.PIDH == 0x26) { IPCAM_ImageBuffer.Camera = OV2640_CAMERA; } else { ETHERNET_UpdateCameraInit(); return; } if (IPCAM_ImageBuffer.ImageFormat == BMP_QQVGA) { /* Initialize the camera structure */ IPCAM_ImageBuffer.ImageHeaderLen = 159; IPCAM_ImageBuffer.ImageHeader = (uint8_t*)QQVGAImageHeader; IPCAM_ImageBuffer.ImageLen = (160*120*2); IPCAM_ImageBuffer.MaxImageLen = (160*120*3); IPCAM_ImageBuffer.ImageData = (uint8_t*)FSMC_SRAM_ADDRESS; IPCAM_ImageBuffer.BufferStatus = BUFFER_EMPTY; } else if (IPCAM_ImageBuffer.ImageFormat == BMP_QVGA) { /* Initialize the camera structure */ IPCAM_ImageBuffer.ImageHeaderLen = 160; IPCAM_ImageBuffer.ImageHeader = (uint8_t*)QVGAImageHeader; IPCAM_ImageBuffer.ImageLen = (320*240*2); IPCAM_ImageBuffer.MaxImageLen = (320*240*3); IPCAM_ImageBuffer.ImageData = (uint8_t*)FSMC_SRAM_ADDRESS; IPCAM_ImageBuffer.BufferStatus = BUFFER_EMPTY; } /* Configure camera module mounted on STM322xG-EVAL board */ eth_Camera_Config(); /* Clears the DCMI's and DMA's pending bits */ DCMI_ClearITPendingBit(DCMI_IT_FRAME | DCMI_IT_OVF); DMA_ClearITPendingBit(DMA2_Stream1, DMA_IT_TCIF1 | DMA_IT_TEIF1); /* Enable DMA2 stream 1 and DCMI interface then start image capture */ DMA_Cmd(DMA2_Stream1, ENABLE); DCMI_Cmd(ENABLE); DCMI_CaptureCmd(ENABLE); }
void DMA_init() { DMA_InitTypeDef DMA_InitStructure; __IO uint32_t Timeout = HSE_STARTUP_TIMEOUT; RCC_AHB1PeriphClockCmd(DMA_Camera_STREAM_CLOCK, ENABLE); DMA_DeInit(DMA_CameraToRAM_Stream); while (DMA_GetCmdStatus(DMA_CameraToRAM_Stream) != DISABLE) { } DMA_InitStructure.DMA_Channel = DMA_Camera_Channel; DMA_InitStructure.DMA_PeripheralBaseAddr = DCMI_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) RAM_Buffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = picture_x * picture_y * 2 / 4; 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_HalfWord; DMA_InitStructure.DMA_Mode = 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_Init(DMA_CameraToRAM_Stream, &DMA_InitStructure); Timeout = HSE_STARTUP_TIMEOUT; while ((DMA_GetCmdStatus(DMA_CameraToRAM_Stream) != ENABLE) && (Timeout-- > 0)) { } // Check if a timeout condition occurred if (Timeout == 0) { // Manage the error: to simplify the code enter an infinite loop while (1) { // Dopísa program } } NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = DCMI_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); DCMI_ITConfig(DCMI_IT_FRAME, ENABLE); DCMI_ITConfig(DCMI_IT_VSYNC, ENABLE); DCMI_ITConfig(DCMI_IT_LINE, ENABLE); // Enable DCMI Capture mode DCMI_Cmd(ENABLE); DCMI_CaptureCmd(ENABLE); // DMA Stream enable DMA_Cmd(DMA_CameraToRAM_Stream, ENABLE); }
/** * Interrupt Handler for DCMI */ void DCMI_IRQHandler() { if (DCMI_GetFlagStatus(DCMI_FLAG_FRAMERI) == SET) { DCMI_ClearFlag(DCMI_FLAG_FRAMERI); #ifdef CAM cam.ProcessData(); #endif } else if (DCMI_GetFlagStatus(DCMI_FLAG_OVFRI) == SET) { DCMI_ClearFlag(DCMI_FLAG_OVFRI); } else if (DCMI_GetFlagStatus(DCMI_FLAG_ERRRI) == SET) { DCMI_ClearFlag(DCMI_FLAG_ERRRI); } DCMI_CaptureCmd(DISABLE); DCMI_Cmd(DISABLE); DMA_Cmd(DMA2_Stream1, DISABLE); DCMI_Cmd(DISABLE); DCMI_ITConfig(DCMI_IT_FRAME, DISABLE); image = true; }
/** * Taking Picture */ void Camera::takePicture() { DCMI_ITConfig(DCMI_IT_FRAME, ENABLE); DCMI_ITConfig(DCMI_IT_OVF, ENABLE); DCMI_ITConfig(DCMI_IT_ERR, ENABLE); //DCMI_ITConfig(DCMI_IT_VSYNC, ENABLE); //DCMI_ITConfig(DCMI_IT_LINE, ENABLE); DMA_Cmd(DMA2_Stream1, ENABLE); DCMI_Cmd(ENABLE); DCMI_CaptureCmd(ENABLE); AT(NOW() + 10*MILLISECONDS); image = false; }
/** * @brief Return the module main menu from Webserver page * @param None * @retval None */ static void return_from_webserver(void) { EthernetSettings.WebserverEnabled = 0; EthernetSettings.InitDone = 0; ETH_Stop(); vTaskPrioritySet(Task_Handle, (configMAX_PRIORITIES - 1)); if(ETH_Task_Handle != NULL) { vTaskDelete(ETH_Task_Handle); ETH_Task_Handle = NULL; } if (Ethernet_xSemaphore != NULL) { vQueueDelete( Ethernet_xSemaphore ); Ethernet_xSemaphore = NULL; } if(TCPIP_Task_Handle != NULL) { vTaskSuspend(TCPIP_Task_Handle); } if(HTTP_Task_Handle != NULL) { vTaskDelete(HTTP_Task_Handle); HTTP_Task_Handle = NULL; } if(DHCP_Task_Handle != NULL) { vTaskDelete(DHCP_Task_Handle); DHCP_Task_Handle = NULL; } DMA_Cmd(DMA2_Stream1, DISABLE); DCMI_Cmd(DISABLE); DCMI_CaptureCmd(DISABLE); netif_remove(&xnetif); ETHERNET_SwitchPage(EthernetWebServerPage, ETHERNET_MAIN_PAGE); EthernetWebServerPage = NULL; }
int main() { SystemInit(); if (SysTick_Config(SystemCoreClock / 3119))//1ms { /* Capture error */ while (1); } STM_EVAL_LEDInit(LED3); STM3240F_LCD_Init(); LCD_Clear(Black); LCD_SetColors(Green,Black); LCD_SetFont(&Font8x8); if(OV9655_Configuration()==0xFF) { LCD_DisplayStringLine(0,0,"Camera Failed"); }else{ LCD_DisplayStringLine(0,0,"Camera OK!"); } /* Start Image capture */ LCD_SetDisplayWindow(0, 0, 319, 239); LCD_WriteRAM_Prepare(); DCMI_CaptureCmd(ENABLE); /* Enable DMA transfer */ DMA_Cmd(DMA2_Stream1, ENABLE); /* Enable DCMI interface */ DCMI_Cmd(ENABLE); /* Start Image capture */ DCMI_CaptureCmd(ENABLE); // Delay(2); // DCMI_SingleRandomWrite(OV9655_DEVICE_WRITE_ADDRESS,0x8d, 0x10);//Camera Color Test Mode Enabled // temp=DCMI_SingleRandomRead(OV9655_DEVICE_WRITE_ADDRESS,OV9655_COM15); // sprintf(data,"Okunan Data=0x%02X",temp); // LCD_DisplayStringLine(0,0,data); while(1) { } }
u8 Cam_Get_Image(void) { DCMI_Cmd(ENABLE); DMA_Cmd(DMA2_Stream1, ENABLE); DCMI_CaptureCmd(ENABLE); jpegCaptureDoneITFLG = 0; jpegCaptureTime = 0; while(jpegCaptureDoneITFLG ==0) { if(jpegCaptureTime > CAMERA_IMGCAPTURE_TIMEOUT) { jpegTotalTime += CAMERA_IMGCAPTURE_TIMEOUT;//Camera image capture failed SerComSendMessageUser("Camera Capture Timed out\r\n"); return 0; } } jpegTotalTime += jpegCaptureTime; //store total capture time return 1; }
u8 Cam_click_images(u16 dly,u32 * nseq,u32 * fsz) { u8 err=0; u8 sb[50]; u8 handle; u32 seq; u32 sz[2],i,k = 0; jpegTotalTime = 0; sz[0] = sz[1] = 0; ENB_CAM1_ACCESS(); CamApi_Init_Regs(); if(Cam_Get_Image()==0)//capture image on DCMI err |= ERROR_HARD_CAM1; ENB_CAM2_ACCESS(); CamApi_Init_Regs(); //move image to buffer and calculate image size if(JpegBuffer[0] == 0xFF && JpegBuffer[1] == 0xD8) { i=0; while(i < JPEG_BUFFER_SZ) { if(i < OFFLOAD_BUFFER_1SZ) { Jpeg_b1[i] = JpegBuffer[i];//store in offload buffer 1 } else { Jpeg_b2[ i - OFFLOAD_BUFFER_1SZ] = JpegBuffer[i];//store in offload buffer 2 } if(i > 2 ) { if(JpegBuffer[i] == 0xD9 && JpegBuffer[i-1] == 0xFF) { sz[0] = i+1;//store size of this image break; } } i++; } } JpegBuffer[0] = 0;//clear jpeg header DCMI_Cmd(DISABLE);//Prepare for next image capture DCMI_CaptureCmd(DISABLE); Cam_DCMI_Config(); Cam_DMA_Init(); //Reset DMA if required if(Cam_Get_Image()==0) //capture image on DCMI err |= ERROR_HARD_CAM2; SLEEP_CAM12(); DMA_Cmd(DMA2_Stream1, DISABLE); DCMI_Cmd(DISABLE); DCMI_CaptureCmd(DISABLE); Cam_DCMI_Config(); Cam_DMA_Init(); //Get size of second image if(JpegBuffer[0] == 0xFF && JpegBuffer[1] == 0xD8) { for(i=0;i<JPEG_BUFFER_SZ;i++) { if(JpegBuffer[i] == 0xFF && JpegBuffer[i+1] == 0xD9) { sz[1] = i+2; break; } } } if(sz[0] > 0 || sz [1] > 0 ) { //Get a File Access handle = FlashCreateNewFile(&seq); i = 0;//This is the variabe to count bytes written //Write debug imformation header , settings poplated on bootup imgdebug.header= 0XABCDEF01; imgdebug.captime = jpegTotalTime; imgdebug.error = err; imgdebug.filesz[0]=sz[1]; imgdebug.filesz[1]=sz[0]; imgdebug.seqno=seq; imgdebug.footer = 0xFFFE; FlashFileWrite(i,handle,(void*)&imgdebug.header,sizeof(imgdebug)); i = 512;//Leaving a 512 byte space to reduce RAM . TODO optimize this if(sz[0] > 0 )//write Second file { i += FlashFileWrite(i,handle,JpegBuffer,sz[1]); #ifdef UART_IMG_DUMP SerComSendArrayUser(JpegBuffer,sz[1]); #endif } if(sz[0] > 0 )//write First file { k = i%512; //Continue writing at 512 byte boundary i += 512-k; //TODO optimize this if( sz[0] < OFFLOAD_BUFFER_1SZ) { i += FlashFileWrite(i,handle,Jpeg_b1,sz[0]); #ifdef UART_IMG_DUMP SerComSendArrayUser(Jpeg_b1,sz[0]); #endif } else { i += FlashFileWrite(i,handle,Jpeg_b1,OFFLOAD_BUFFER_1SZ);//This will automatically end at 512 boundary as it is multiple of 1024 i += FlashFileWrite(i,handle,Jpeg_b2,sz[0] - OFFLOAD_BUFFER_1SZ); #ifdef UART_IMG_DUMP SerComSendArrayUser(Jpeg_b1,OFFLOAD_BUFFER_1SZ); SerComSendArrayUser(Jpeg_b2,sz[0] - OFFLOAD_BUFFER_1SZ); #endif } } FlashFileBytesWritten = i; FlashCloseFile(handle,seq);//Close File Access } //update sequence and filesize variable *nseq = seq; *fsz = i; //Report bytes written and sequence number return err; }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f4xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); LIS302DL_Reset(); /* SET USER Key */ /* Configure EXTI Line0 (connected to PA0 pin) in interrupt mode */ EXTILine0_Config(); /* Initialize the LCD */ STM32f4_Discovery_LCD_Init(); LCD_Clear(LCD_COLOR_WHITE); LCD_SetTextColor(LCD_COLOR_BLUE); DCMI_Control_IO_Init(); LCD_DisplayStringLine(LINE(2), " Camera Init.."); /* OV9655 Camera Module configuration */ if (DCMI_OV9655Config() == 0x00) { LCD_DisplayStringLine(LINE(2), " "); LCD_SetDisplayWindow(0, 0, 320, 240); LCD_WriteRAM_Prepare(); /* Start Image capture and Display on the LCD *****************************/ /* Enable DMA transfer */ DMA_Cmd(DMA2_Stream1, ENABLE); /* Enable DCMI interface */ DCMI_Cmd(ENABLE); /* Start Image capture */ DCMI_CaptureCmd(ENABLE); /*init the picture count*/ init_picture_count(); KeyPressFlg = 0; while (1) { /* Insert 100ms delay */ Delay(100); if (KeyPressFlg) { KeyPressFlg = 0; /* press user KEY take a photo */ if (capture_Flag == ENABLE) { DCMI_CaptureCmd(DISABLE); capture_Flag = DISABLE; Capture_Image_TO_Bmp(); LCD_SetDisplayWindow(0, 0, 320, 240); LCD_WriteRAM_Prepare(); DCMI_CaptureCmd(ENABLE); capture_Flag = ENABLE; } } } } else { LCD_SetTextColor(LCD_COLOR_RED); LCD_DisplayStringLine(LINE(2), "Camera Init.. fails"); LCD_DisplayStringLine(LINE(4), "Check the Camera HW "); LCD_DisplayStringLine(LINE(5), " and try again "); /* Go to infinite loop */ while (1); } }
void DCMI_DMA_init(void) { GPIO_InitTypeDef GPIO_InitStructure; DCMI_InitTypeDef DCMI_InitStructure; DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); // GPIO config // PA4 - HREF (HSYNC), PA6 - PCLK (PIXCLK) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6; //PA4 - HREF (HSYNC) //PA6 - PCLK (PIXCLK) 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); // PB6 - D5, PB7 - VSYNC, PB8 - D6, PB9 - D7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; 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); // PC6 - D0, PC7 - D1, PC8 - D2, PC9 - D3, PC11 - D4 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_11; 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); // GPIO AF config GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_DCMI); // DCMI config DCMI_DeInit(); DCMI_InitStructure.DCMI_CaptureMode = DCMI_CaptureMode_SnapShot; //DCMI_CaptureMode_SnapShot DCMI_InitStructure.DCMI_ExtendedDataMode = DCMI_ExtendedDataMode_8b; DCMI_InitStructure.DCMI_CaptureRate = DCMI_CaptureRate_All_Frame; //DCMI_CaptureRate_All_Frame; DCMI_InitStructure.DCMI_PCKPolarity = DCMI_PCKPolarity_Rising; DCMI_InitStructure.DCMI_HSPolarity = DCMI_HSPolarity_Low; DCMI_InitStructure.DCMI_VSPolarity = DCMI_VSPolarity_High; DCMI_InitStructure.DCMI_SynchroMode = DCMI_SynchroMode_Hardware; DCMI_Init(&DCMI_InitStructure); DCMI_ITConfig(DCMI_IT_FRAME, ENABLE); DCMI_ITConfig(DCMI_IT_OVF, ENABLE); DCMI_ITConfig(DCMI_IT_ERR, ENABLE); // DMA config DMA_DeInit(DMA2_Stream1); DMA_InitStructure.DMA_Channel = DMA_Channel_1; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) (&DCMI->DR); DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) frame_buffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = IMG_ROWS * IMG_COLUMNS / 2; 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_Word; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; 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_Init(DMA2_Stream1, &DMA_InitStructure); DMA_ITConfig(DMA2_Stream1, DMA_IT_TC, ENABLE); DMA_ITConfig(DMA2_Stream1, DMA_IT_TE, ENABLE); /* DMA2 IRQ channel Configuration */ NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = DCMI_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); DMA_Cmd(DMA2_Stream1, ENABLE); DCMI_Cmd(ENABLE); DCMI_CaptureCmd(ENABLE); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { //RCC_Config(); //GPIO_Config(); //RCC_MCO1Config(RCC_MCO1Source_PLLCLK,RCC_MCO1Div_1); uint8_t tmpreg; LIS302DL_InitTypeDef LIS302DL_InitStruct; /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f4xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); //NVIC_SetPriority(SysTick_IRQn, -1); /* MEMS configuration and set int1/int2 pins*/ LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE; LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100; LIS302DL_InitStruct.Axes_Enable = LIS302DL_XYZ_ENABLE; LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3; LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL; LIS302DL_Init(&LIS302DL_InitStruct); //Set INT1/INT2 pins tmpreg = 0x40; LIS302DL_Write(&tmpreg, LIS302DL_CTRL_REG3_ADDR, 1); //Initialize the touchscreen TSC_Init(); /* Initialize LEDs and push-buttons mounted on STM324xG-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); //STM_EVAL_LEDInit(LED3); //STM_EVAL_LEDInit(LED4); /* Initialize the LCD */ STM324xG_LCD_Init(); //STM_EVAL_LEDOn(LED1); LCD_Clear(Black); LCD_SetTextColor(White); LCD_LOG_SetHeader("STM32 Camera Demo"); LCD_LOG_SetFooter (" Copyright (c) STMicroelectronics" ); /* ADC configuration */ ADC_Config(); /* Initializes the DCMI interface (I2C and GPIO) used to configure the camera */ //OV9655_HW_Init(); OV9712_HW_Init(); /* Read the OV9655/OV2640 Manufacturer identifier */ //OV9655_ReadID(&OV9655_Camera_ID); OV9712_ReadID(&OV9712_Camera_ID); //while(1); if(OV9655_Camera_ID.PID == 0x96) { Camera = OV9712_CAMERA; sprintf((char*)buffer, "OV9655 Camera ID 0x%x", OV9655_Camera_ID.PID); ValueMax = 2; } else if(OV9712_Camera_ID.PIDH == 0x97) { Camera = OV9712_CAMERA; sprintf((char*)buffer, "OV9712 Camera ID 0x%x", OV9712_Camera_ID.PIDH); ValueMax = 2; } else { LCD_SetTextColor(LCD_COLOR_RED); sprintf((char*)buffer, "OV Camera ID 0x%02x%02x", OV9655_Camera_ID.Version, OV9712_Camera_ID.PIDH); LCD_DisplayStringLine(LINE(4), buffer); while(1); } LCD_SetTextColor(LCD_COLOR_YELLOW); LCD_DisplayStringLine(LINE(4), (uint8_t*)buffer); LCD_SetTextColor(LCD_COLOR_WHITE); Delay(200); /* Initialize demo */ ImageFormat = (ImageFormat_TypeDef)BMP_QVGA; /* Configure the Camera module mounted on STM324xG-EVAL board */ Demo_LCD_Clear(); LCD_DisplayStringLine(LINE(4), "Camera Init.. "); Camera_Config(); sprintf((char*)buffer, " Image selected: %s", ImageForematArray[ImageFormat]); LCD_DisplayStringLine(LINE(4),(uint8_t*)buffer); LCD_ClearLine(LINE(4)); Demo_LCD_Clear(); if(ImageFormat == BMP_QQVGA) { /* LCD Display window */ LCD_SetDisplayWindow(60, 80, 160, 120); ReverseLCD(); LCD_WriteRAM_Prepare(); } else if(ImageFormat == BMP_QVGA) { /* LCD Display window */ LCD_SetDisplayWindow(0, 0, 320, 240); ReverseLCD(); LCD_WriteRAM_Prepare(); } { int i, j; for (j = 0; j < 16; j++) { LCD_SetDisplayWindow(0, (240 / 16) * j, 320, (240 / 16)); LCD_WriteRAM_Prepare(); for (i = 0; i < 320 * 240 / 16; i++) { LCD_WriteRAM(0x0003 << j); } } } /* Enable DMA2 stream 1 and DCMI interface then start image capture */ DMA_Cmd(DMA2_Stream1, ENABLE); DCMI_Cmd(ENABLE); /* Insert 100ms delay: wait 100ms */ //Delay(200); DCMI_CaptureCmd(ENABLE); while(1) { OV9655_BrightnessConfig(0x7F);// //static int step = 0; int i, block, begin; unsigned short *p; if (!bShot) { begin = (datablock - 11 + 48) % 48; for (block = begin; block < begin + 11; block++) { p = (unsigned short *)(0x20000000 + (320 * 240 * 2 / 16) * ((block) % 12)); LCD_SetDisplayWindow(0, (block % 16) * (240 / 16), 320, (240 / 16)); //LCD_SetCursor(0, (block % 16) * (240 / 16)); LCD_WriteRAM_Prepare(); for (i = 0; i < 320 * 240 / 16; i++) { LCD_WriteRAM(*p++); } } } if (TSC_TouchDet()) { int x, y; TP_GetAdXY(&x, &y); if (x > 300 && x < 2800 && y > 300 && y < 2800) { if (x < 1550 && y < 1550) { uint32_t StartSector = 0, EndSector = 0, Address = 0, SectorCounter = 0; int m; //拍照bShot bShot = 1; while(datablock % 16); DMA_Cmd(DMA2_Stream1, DISABLE); DCMI_Cmd(DISABLE); //STM_EVAL_LEDOn(LED2); DMA_ClearFlag(DMA2_Stream1, 0x2F7D0F7D); DMA_ClearFlag(DMA2_Stream1, 0x0F7D0F7D); DMA_ClearITPendingBit(DMA2_Stream1, DMA_IT_TCIF1); datablock = 0; DMA2_Stream1->M0AR = 0x20000000 + 9600 * (datablock % 12); DMA_SetCurrDataCounter(DMA2_Stream1, (320 * 240 * 2 / 4) / 16); DCMI_Cmd(ENABLE); DMA_Cmd(DMA2_Stream1, ENABLE); { unsigned long *p, *q; while(datablock < 4); //等待准备好前4块数据,就将这4块数据导入到0x10000000+0x50000之后。 q = (unsigned long *)(0x20000000); p = (unsigned long *)(0x10000000 + 0x5000); while (q < (unsigned long *)(0x20000000 + 4 * (320 * 240 * 2 / 16))) { *p = *q; p++; q++; } } while(datablock < 16); //等待全部采集完成。 STM_EVAL_LEDOn(LED2); //LED2亮表示采集完成。 LCD_SetDisplayWindow(0, 0, 320, 240); LCD_WriteRAM_Prepare(); LCD_Clear(Black); //RAM位置 /* 序号 地址 大小 1: 0x10005000+9600*0 9600 2: 0x10005000+9600*1 9600 3: 0x10005000+9600*2 9600 4: 0x10005000+9600*3 9600 5: 0x20000000+9600*5 9600 6: 0x20000000+9600*6 9600 7: 0x20000000+9600*7 9600 8: 0x20000000+9600*8 9600 9: 0x20000000+9600*9 9600 10: 0x20000000+9600*10 9600 11: 0x20000000+9600*11 9600 12: 0x20000000+9600*0 9600 13: 0x20000000+9600*1 9600 14: 0x20000000+9600*2 9600 15: 0x20000000+9600*3 9600 */ for (m = 0; m < 16; m++) //显示保存的图片 { unsigned short *q; if (m < 4) { q = (unsigned short *)(0x10000000 + 0x5000 + m * (320 * 240 * 2 / 16)); } else { q = (unsigned short *)(0x20000000 + (m % 12) * (320 * 240 * 2 / 16)); } LCD_SetDisplayWindow(0, m * (240 / 16), 320, (240 / 16)); LCD_WriteRAM_Prepare(); for (i = 0; i < 320 * 240 / 16; i++) { LCD_WriteRAM(*q++); } } /* Erase the user Flash area ***********/ FLASH_Unlock(); /* Clear pending flags (if any) */ FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); /* Get the number of the start and end sectors */ StartSector = GetSector(FLASH_USER_START_ADDR); EndSector = GetSector(FLASH_USER_END_ADDR); for (SectorCounter = StartSector; SectorCounter < EndSector; SectorCounter += 8) { /* Device voltage range supposed to be [2.7V to 3.6V], the operation will be done by word */ if (FLASH_EraseSector(SectorCounter, VoltageRange_3) != FLASH_COMPLETE) { /* Error occurred while sector erase. User can add here some code to deal with this error */ while (1) { } } } for (m = 0; m < 16; m++) //保存图片到flash { unsigned long *q; Address = FLASH_USER_START_ADDR + m * (320 * 240 * 2 / 16); if (m < 4) { q = (unsigned long *)(0x10000000 + 0x5000 + m * (320 * 240 * 2 / 16)); } else { q = (unsigned long *)(0x20000000 + (m % 12) * (320 * 240 * 2 / 16)); } while (Address < FLASH_USER_START_ADDR + (m + 1) *(320 * 240 * 2 / 16)) { if (FLASH_ProgramWord(Address, *q) == FLASH_COMPLETE) { Address = Address + 4; q++; } else { /* Error occurred while writing data in Flash memory. User can add here some code to deal with this error */ while (1) { } } } } STM_EVAL_LEDOff(LED2); LCD_SetDisplayWindow(0, 0, 320, 240); LCD_WriteRAM_Prepare(); LCD_Clear(Black); for (m = 0; m < 16; m++) //显示flash中的图片 { unsigned short *q; q = (unsigned short *)(FLASH_USER_START_ADDR + m * (320 * 240 * 2 / 16)); LCD_SetDisplayWindow(0, m * (240 / 16), 320, (240 / 16)); LCD_WriteRAM_Prepare(); for (i = 0; i < 320 * 240 / 16; i++) { LCD_WriteRAM(*q++); } } /* Lock the Flash to disable the flash control register access (recommended to protect the FLASH memory against possible unwanted operation) *********/ FLASH_Lock(); } else if (x >= 1550 && y < 1550) { //righttop STM_EVAL_LEDOff(LED1); STM_EVAL_LEDOff(LED2); bShot = 0; datablock = 0; DMA_Cmd(DMA2_Stream1, ENABLE); DCMI_Cmd(ENABLE); } else if (x < 1550 && y >= 1550) { //righttop //DMA_Cmd(DMA2_Stream1, ENABLE); //DCMI_Cmd(ENABLE); } else if (x >= 1550 && y >= 1550) { //righttop //DMA_Cmd(DMA2_Stream1, ENABLE); //DCMI_Cmd(ENABLE); } } } //Delay(10); } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s) before to branch to application main. */ /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); /* Initialize LEDs mounted on EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); STM_EVAL_LEDOn(LED1); /* Initialize the LCD */ LCD_Init(); LCD_Clear(Black); LCD_SetTextColor(White); LCD_LOG_SetHeader((uint8_t*)"STM32 Camera Demo"); LCD_LOG_SetFooter ((uint8_t*)" Copyright (c) STMicroelectronics" ); /* ADC configuration */ ADC_Config(); /* Initializes the DCMI interface (I2C and GPIO) used to configure the camera */ OV2640_HW_Init(); /* Read the OV9655/OV2640 Manufacturer identifier */ OV9655_ReadID(&OV9655_Camera_ID); OV2640_ReadID(&OV2640_Camera_ID); if(OV9655_Camera_ID.PID == 0x96) { Camera = OV9655_CAMERA; sprintf((char*)abuffer, "OV9655 Camera ID 0x%x", OV9655_Camera_ID.PID); ValueMax = 2; } else if(OV2640_Camera_ID.PIDH == 0x26) { Camera = OV2640_CAMERA; sprintf((char*)abuffer, "OV2640 Camera ID 0x%x", OV2640_Camera_ID.PIDH); ValueMax = 2; } else { LCD_SetTextColor(LCD_COLOR_RED); LCD_DisplayStringLine(LINE(4), (uint8_t*)"Check the Camera HW and try again"); while(1); } LCD_SetTextColor(LCD_COLOR_YELLOW); LCD_DisplayStringLine(LINE(4), (uint8_t*)abuffer); LCD_SetTextColor(LCD_COLOR_WHITE); Delay(200); /* Initialize demo */ ImageFormat = (ImageFormat_TypeDef)Demo_Init(); /* Configure the Camera module mounted on STM324xG-EVAL/STM324x7I-EVAL boards */ Demo_LCD_Clear(); LCD_DisplayStringLine(LINE(4), (uint8_t*)"Camera Init.. "); Camera_Config(); sprintf((char*)abuffer, " Image selected: %s", ImageForematArray[ImageFormat]); LCD_DisplayStringLine(LINE(4),(uint8_t*)abuffer); /* Enable DMA2 stream 1 and DCMI interface then start image capture */ DMA_Cmd(DMA2_Stream1, ENABLE); DCMI_Cmd(ENABLE); /* Insert 100ms delay: wait 100ms */ Delay(200); DCMI_CaptureCmd(ENABLE); LCD_ClearLine(LINE(4)); Demo_LCD_Clear(); if(ImageFormat == BMP_QQVGA) { /* LCD Display window */ LCD_SetDisplayWindow(179, 239, 120, 160); LCD_WriteReg(LCD_REG_3, 0x1038); LCD_WriteRAM_Prepare(); } else if(ImageFormat == BMP_QVGA) { /* LCD Display window */ LCD_SetDisplayWindow(239, 319, 240, 320); LCD_WriteReg(LCD_REG_3, 0x1038); LCD_WriteRAM_Prepare(); } while(1) { /* Blink LD1, LED2 and LED4 */ STM_EVAL_LEDToggle(LED1); STM_EVAL_LEDToggle(LED2); STM_EVAL_LEDToggle(LED3); STM_EVAL_LEDToggle(LED4); /* Insert 100ms delay */ Delay(10); /* Get the last ADC3 conversion result data */ uhADCVal = ADC_GetConversionValue(ADC3); /* Change the Brightness of camera using "Brightness Adjustment" register: For OV9655 camera Brightness can be positively (0x01 ~ 0x7F) and negatively (0x80 ~ 0xFF) adjusted For OV2640 camera Brightness can be positively (0x20 ~ 0x40) and negatively (0 ~ 0x20) adjusted */ if(Camera == OV9655_CAMERA) { OV9655_BrightnessConfig(uhADCVal); } if(Camera == OV2640_CAMERA) { OV2640_BrightnessConfig(uhADCVal/2); } } }
int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f4xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* SysTick end of count event each 10ms */ unsigned int line; enum { TYPE_UNKNOWN=0, TYPE_RGB24, TYPE_RGB16, TYPE_YUV, } image_type = TYPE_UNKNOWN; RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); init_USART1(1048576); USART_puts(USART2,"\nLoaded,"); image_type = TYPE_YUV; frame_done=0; // LIS302DL_Reset(); /* SET USER Key */ /* Configure EXTI Line0 (connected to PA0 pin) in interrupt mode */ // EXTILine0_Config(); // /* Initialize the LCD */ // STM32f4_Discovery_LCD_Init(); // LCD_Clear(LCD_COLOR_WHITE); // LCD_SetTextColor(LCD_COLOR_BLUE); // DCMI_Control_IO_Init();//funtion to configure reset and power pins of the camera **dont need for tw9910 // LCD_DisplayStringLine(LINE(2), " Camera Init.."); /* OV9655 Camera Module configuration */ // ****INIT the TW9910 in the following function if (DCMI_OV9655Config() == 0x00)//configures pins of DCMI,I2C and DMA and the camera settings, if it returns a positive response { //Successful // int fr=0; USART_puts(USART2,"In TW9910\n"); // LCD_DisplayStringLine(LINE(2), " "); // LCD_SetDisplayWindow(0, 0, 320, 240); // LCD_WriteRAM_Prepare(); //initialise an array here for the Image and ensure it is set up in the DMA configuration /* Enable DMA transfer */ DMA_Cmd(DMA2_Stream1, ENABLE); /* Enable DCMI interface */ DCMI_Cmd(ENABLE); /* Start Image capture */ DCMI_CaptureCmd(ENABLE); /*init the picture count*/ //useless for tw9910 and our case //init_picture_count(); // a function to initialise a variable from a counter file in the sdcard to name the new bmp file and file-xxx.bmp while (1) { // int i; //Delay(50); // int i; //for(i=0;i<1000;i++); if (frame_done) { // char s[5]; // fr++; // USART_puts(USART2,"\nNewFrame\n"); // if (capture_Flag == ENABLE) { DCMI_CaptureCmd(DISABLE); USART_puts(USART2,"\njpeg2\n"); huffman_start(IMG_HEIGHT & -8, IMG_WIDTH & -8); huffman_resetdc(); // USART_puts(USART2,"\njpeg\n"); // // for(i=0;i<FULLIMAGESIZE;i++){ // sprintf(s,",%x",imagearray[i]); // USART_puts(USART2,s); // //USART_writebyte(USART2,&imagearray[i]); // // } // // USART_puts(USART2,"\nDone"); // capture_Flag = DISABLE; // // Capture_Image_TO_Bmp(); //// LCD_SetDisplayWindow(0, 0, 320, 240); //// LCD_WriteRAM_Prepare(); // capture_Flag = ENABLE; // } // USART_puts(USART2,"\njpeg2\n"); for (line=0; line<NUM_LINES; line++) { uint8_t* line_buffer=(uint8_t *)&(imagearray[line*(IMG_WIDTH*IMG_HEIGHT*2/NUM_LINES)]); // encode the line using appropriate encoder switch (image_type) { // case TYPE_RGB24: encode_line_rgb24(line_buffer, line); break; // case TYPE_RGB16: encode_line_rgb16(line_buffer, line); break; case TYPE_YUV: encode_line_yuv(line_buffer, line); break; case TYPE_UNKNOWN: default:break; // fprintf(stderr, "error: %s, unsupported encoder for input '%s'\n", argv[0], argv[1]); // exit(1); } } // write .jpeg footer (end-of-image marker) huffman_stop(); USART_puts(USART2,"\njpegends\n"); DCMI_CaptureCmd(ENABLE); frame_done = 0; //if(fr==2) //while(1); } } } else { // LCD_SetTextColor(LCD_COLOR_RED); // LCD_DisplayStringLine(LINE(2), "Camera Init.. fails"); // LCD_DisplayStringLine(LINE(4), "Check the Camera HW "); // LCD_DisplayStringLine(LINE(5), " and try again "); /* Go to infinite loop */ while (1); } }
int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f4xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* SysTick end of count event each 10ms */ #ifndef YUVDEBUG unsigned int line; #endif RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); init_USART1(1048576); USART_puts(USART2,"\nLoaded,"); frame_done=0; /*Camera configuration */ // ****INIT the TW9910 in the following function if (DCMI_OV9655Config() == 0x00)//configures pins of DCMI,I2C and DMA and the camera settings, if it returns a positive response { //Successful int fr=0; USART_puts(USART2,"In TW9910\n"); /* Enable DMA transfer */ DMA_Cmd(DMA2_Stream1, ENABLE); /* Enable DCMI interface */ DCMI_Cmd(ENABLE); /* Start Image capture */ DCMI_CaptureCmd(ENABLE); while (1) { // int i; // Delay(50); if (frame_done) { char s[5]; int i; fr++; DCMI_CaptureCmd(DISABLE); #ifndef YUVDEBUG USART_puts(USART2,"\njpeg2\n"); huffman_start(IMG_HEIGHT & -8, IMG_WIDTH & -8); huffman_resetdc(); #endif #ifdef YUVDEBUG USART_puts(USART2,"\nNewFrame\n"); for(i=0;i<FULLIMAGESIZE*2;i++){ sprintf(s,",%x",imagearray[i]); USART_puts(USART2,s); } #endif #ifndef YUVDEBUG for (line=0; line<NUM_LINES; line++) { uint8_t* line_buffer=(uint8_t *)&(imagearray[line*(IMG_WIDTH*8*2)]); // encode the line using encoder encode_line_yuv(line_buffer, line); } // write .jpeg footer (end-of-image marker) huffman_stop(); #endif // /* print the frame counter */ // sprintf(s,",%d",fr); // USART_puts(USART2,s); //USART_puts(USART2,"\njpegends\n"); DCMI_CaptureCmd(ENABLE); frame_done = 0; } } } else {// Cant INIT the TW9910 USART_puts(USART2,"Cant Init TW9910"); /* Go to infinite loop */ while (1); } }
void DCMI_Interface_Init(void) { DCMI_InitTypeDef DCMI_InitStructure; DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Enable DCMI clock */ RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE); /* DCMI configuration */ //DCMI捕捉模式的设置,默认是连续模式,会看到连续的移动图像 //用户在使用的时候可调整为SnapShot(抓拍),也就是单帧 DCMI_InitStructure.DCMI_CaptureMode = DCMI_CaptureMode_Continuous;//DCMI_CaptureMode_SnapShot; 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_All_Frame; DCMI_InitStructure.DCMI_ExtendedDataMode = DCMI_ExtendedDataMode_8b; /* 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 = FSMC_LCD_ADDRESS; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = 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; //DCMI中断配置,在使用帧中断或者垂直同步中断的时候打开 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); NVIC_InitStructure.NVIC_IRQChannel = DCMI_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //使能DCMI中断 DCMI_ITConfig(DCMI_IT_VSYNC, ENABLE); /* DCMI configuration */ DCMI_Init(&DCMI_InitStructure); /* DMA2 IRQ channel Configuration */ DMA_Init(DMA2_Stream1, &DMA_InitStructure); DMA_Cmd(DMA2_Stream1, ENABLE); DCMI_Cmd(ENABLE); }
int main() { SysTick_Config(SystemCoreClock / 1000); InitLeds(); GPIO_SetBits(GPIOD, GREEN_LED); Delay(100); GPIO_ResetBits(GPIOD, GREEN_LED); GPIO_SetBits(GPIOD, ORANGE_LED); Delay(100); GPIO_ResetBits(GPIOD, ORANGE_LED); GPIO_SetBits(GPIOD, RED_LED); Delay(100); GPIO_ResetBits(GPIOD, RED_LED); GPIO_SetBits(GPIOD, BLUE_LED); Delay(100); GPIO_ResetBits(GPIOD, BLUE_LED); /** * USART */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE); GPIO_PinAFConfig(USART2_TX_PORT, USART2_TX_PIN_SOURCE, GPIO_AF_USART2); GPIO_PinAFConfig(USART2_RX_PORT, USART2_RX_PIN_SOURCE, GPIO_AF_USART2); /* Configure USART Tx as alternate function */ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = USART2_TX_PIN; GPIO_Init(USART2_TX_PORT, &GPIO_InitStructure); /* Configure USART Rx as alternate function */ GPIO_InitStructure.GPIO_Pin = USART2_RX_PIN; GPIO_Init(USART2_RX_PORT, &GPIO_InitStructure); USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); /* Enable USART */ USART_Cmd(USART2, ENABLE); Serial_print(USART2, "Hello World! -- Compiled on: "); Serial_print(USART2, __DATE__); Serial_print(USART2, " - "); Serial_println(USART2, __TIME__); /** * Interrupts */ NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = DCMI_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_Init(&NVIC_InitStructure); DCMI_ClearITPendingBit(DCMI_IT_FRAME | DCMI_IT_OVF | DCMI_IT_ERR); DCMI_ITConfig(DCMI_IT_FRAME | DCMI_IT_OVF | DCMI_IT_ERR, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream1_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_Init(&NVIC_InitStructure); DMA_ClearITPendingBit(DMA2_Stream1, DMA_IT_TCIF1 | DMA_IT_TEIF1); DMA_ITConfig(DMA2_Stream1, DMA_IT_TC | DMA_IT_FE | DMA_IT_TE | DMA_IT_DME, ENABLE); Serial_print(USART2, "Interrupts done. \r\n"); /** Camera Reset Pin & Power Down Pin */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Low_Speed; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_9; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA, GPIO_Pin_10); GPIO_ResetBits(GPIOA, GPIO_Pin_9); /** * DCMI */ OV2640_HW_Init(); Serial_print(USART2, "HW_Init done. \r\n"); /* Print camera Id */ OV2640_IDTypeDef camera_id; OV2640_ReadID(&camera_id); Serial_print(USART2, "Camera ID: "); Serial_print(USART2, camera_id.Manufacturer_ID1, 16); Serial_print(USART2, " - "); Serial_print(USART2, camera_id.Manufacturer_ID2, 16); Serial_print(USART2, " - "); Serial_print(USART2, camera_id.PIDH, 16); Serial_print(USART2, " - "); Serial_print(USART2, camera_id.PIDL, 16); Serial_println(USART2, ""); OV2640_QQVGAConfig(); Serial_print(USART2, "QQVGAConfig done. \r\n"); // OV2640_BandWConfig(0x18); // BW OV2640_Init(BMP_QQVGA); Serial_print(USART2, "Init done. \r\n"); // Memset int i; for (i=0; i<160*120; i++) { Targetbuffer[i] = 0xbeef; } DMA_Cmd(DMA2_Stream1, ENABLE); while ( DMA_GetCmdStatus(DMA2_Stream1) != ENABLE ) ; Serial_print(USART2, "DMA Enable done. \r\n"); DCMI_Cmd(ENABLE); Serial_print(USART2, "DCMI Enable done. \r\n"); DCMI_CaptureCmd(ENABLE); Serial_print(USART2, "DCMI Capture start. \r\n"); Serial_print(USART2, "Print: \r\n.\r\n.\r\n"); Serial_print(USART2, "done. \r\n"); Delay(1); // Clear screen Serial_print(USART2, 0x1b); Serial_print(USART2, "[2J"); while (1) { while (DCMI_GetFlagStatus(DCMI_FLAG_FRAMERI) == RESET) ; print_CameraData(); // Move cursor to home position. Serial_print(USART2, 0x1b); Serial_print(USART2, "[H"); } }
void Cam_Init() { GPIO_InitTypeDef GPIO_InitStructure; DCMI_InitTypeDef DCMI_InitStructure; DCMI_CROPInitTypeDef DCMI_CROPInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE);//DCMI RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);//DMA2 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOE, ENABLE);//使能DCMI的GPIO时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE); GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_MCO);//MCO1:PA8 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); RCC_MCO1Config(RCC_MCO1Source_HSE, RCC_MCO1Div_1);//25MHz GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//PE5:PWRDOWN GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_ResetBits(GPIOA, GPIO_Pin_7);//power on GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; // GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_SetBits(GPIOD, GPIO_Pin_7);//reset delay_ms(10); OV7670_RST(0); //复位OV7670 delay_ms(10); OV7670_RST(1); //结束复位 GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_DCMI);//DCMI_HSYNC GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_DCMI);//DCMI_PIXCLK GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_DCMI);//DCMI_D5 GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_DCMI);//DCMI_VSYNC GPIO_PinAFConfig(GPIOE, GPIO_PinSource5, GPIO_AF_DCMI);//DCMI_D6 GPIO_PinAFConfig(GPIOE, GPIO_PinSource6, GPIO_AF_DCMI);//DCMI_D7 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_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); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6; GPIO_Init(GPIOE, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 ; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6; GPIO_Init(GPIOA, &GPIO_InitStructure); DCMI_CROPInitStructure.DCMI_CaptureCount=319; DCMI_CROPInitStructure.DCMI_HorizontalOffsetCount=1; DCMI_CROPInitStructure.DCMI_VerticalLineCount=119; DCMI_CROPInitStructure.DCMI_VerticalStartLine=1; DCMI_CROPConfig(&DCMI_CROPInitStructure); DCMI_CROPCmd(ENABLE); DCMI_InitStructure.DCMI_CaptureMode =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_Low; DCMI_InitStructure.DCMI_CaptureRate = DCMI_CaptureRate_All_Frame; DCMI_InitStructure.DCMI_ExtendedDataMode = DCMI_ExtendedDataMode_8b; DCMI_Init(&DCMI_InitStructure); DCMI_ITConfig(DCMI_IT_FRAME,ENABLE);//开启帧中断 DCMI_Cmd(ENABLE); //DCMI使能 DCMI_DMA_Init((u32)&jpeg_buf,jpeg_buf_size,DMA_MemoryDataSize_Word,DMA_MemoryInc_Enable); NVIC_InitStructure.NVIC_IRQChannel = DCMI_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=NVIC_DCMI_P;//抢占优先级1 NVIC_InitStructure.NVIC_IRQChannelSubPriority =NVIC_DCMI_S; //子优先级2 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、 }