⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 BlackFin与摄像头的接口程序
💻 C
📖 第 1 页 / 共 2 页
字号:

#if defined (__ADSPBF561__)
	// to prevent overheating of BF561	
	adi_pwr_SetVoltageRegulator(ADI_PWR_CMD_SET_VR_VLEV, (void *)ADI_PWR_VLEV_110);
#endif	
	
	adi_pwr_GetFreq(&cclk, &sclk, &vco);
	
	// open the uart	
	T_UART_HANDLE hUART = uart_open(0, sclk, 1024, 1024, 0);
	uart_setMode(hUART, UART_BAUD_RATE, UART_NONE, 8, 1);

	uart_writeString (hUART, "Configuring camera modul on PPI1...");

	T_CAMERA_HANDLE phCamera;
	unsigned short pConfigParams[255];
	
#if defined(OV7660)
	if ((nXsize == 320) && (nYsize == 240)) {
		// set configuration parameters for QVGA, ITU656, YUV, 15fps
		cam_paramSet_15_QVGA_YUV_8 (pConfigParams);
	} else {
		// set configuration parameters for VGA, ITU656, YUV, 15fps
		cam_paramSet_15_VGA_YUV_8 (pConfigParams);
	}
#elif defined (OV2640)
	unsigned short nNumParams = cam_paramSet_OV2641_Init(pConfigParams);
#if defined (YUV)	
	cam_paramSet_OV2641_YUV(&pConfigParams[nNumParams]);
#elif defined (RAW)
	cam_paramSet_OV2641_RAW10(&pConfigParams[nNumParams]);
#elif defined (RGB565)
	cam_paramSet_OV2641_RGB565(&pConfigParams[nNumParams]);
#endif
#elif defined (OV2630)
	unsigned short nNumParams = cam_paramSet_OV2630_Init(pConfigParams);
	cam_paramSet_OV2630_UXGA_15fps_RAW10(&pConfigParams[nNumParams]);
#endif
	
	// configure the camera
	T_ERROR_CODE erResult = cam_setup(	&phCamera, 
										CAM_SIO_C_PIN, 
										CAM_SIO_D_PIN,
										CAM_PWDN_PIN,
										CAM_DEVICE_ADDRESS,
										pConfigParams,
										cclk,
										0);
	if (erResult == ERR_NONE) {
		// camera found and initialised
		T_CAMERA_SPEC *hCamera = (T_CAMERA_SPEC *)phCamera;
		
		sprintf (sMessage, "done. Found OV%X connected on PPI1.\xd\xa", hCamera->nCameraID);
		uart_writeString (hUART, (unsigned char *)sMessage);
		
		// give camera time to stabilize the counter for AEC, AWB ...		
		uart_writeString (hUART, "Give camera time to stabilize...\xd\xa");
		Sleep (2500);

		// allocating memory for the image buffer
		char *YUVbuffer = (char *)malloc (nXsize * nYsize * 2);		// 2 byte per pixel
		
		sprintf (sMessage, "Capturing image, resolution: %u X %u...", nXsize, nYsize);
		uart_writeString (hUART, (unsigned char *)sMessage);
#if defined (OV7660)		
		// configure ppi and dma in itu656 mode
		ppi_setup_itu656 (
				0, 
				PPI_SINGLE_SHOT, 
				PPI_MEMORY_WRITE, 
				(unsigned long)YUVbuffer, 
				PPI_FRAMEWISE_INT,
				(T_PPI_CALLBACK)PPIHandler,
				1,
				2,
				PPI_DMA_16_BIT,
				PPI_RECEIVE_ACTIVE_ONLY,
				PPI_16BIT_PACKING,
				PPI_FIELD_BOTH,
				PPI_SKIP_NONE,
				nXsize,
				nYsize,
				0);
#elif defined (OV2640) || defined (OV2630)
#if defined (RAW)
// raw
		ppi_setup_gp ( 	0,
					   	(unsigned long)YUVbuffer,
#if defined (__ADSPBF533__)					   	
						0x882c,
#else						
						0x082c,
#endif						
						nYsize,
						nXsize - 1,
#if defined (__ADSPBF533__)					   	
						348,
#else
						0,
#endif											
						0x00b6,
						nXsize,
						2,
						nYsize,
						2,
						0,
						(T_PPI_CALLBACK)PPIHandler);
#elif defined (YUV)	|| defined (RGB565)					
// YUV4:2:2	or RGB565					
		ppi_setup_gp ( 	0,
					   	(unsigned long)YUVbuffer,
						0x002c,
						nYsize,
						(nXsize * 2) - 1,
						0,
						0x00b2,
						nXsize * 2,
						1,
						nYsize,
						1,
						0,
						(T_PPI_CALLBACK)PPIHandler);
#endif	
#if defined(__ADSPBF537__)
	#include <cdefbf537.h>
	unsigned short nValue = *pPORTG_FER;
	*pPORTG_FER |= 0x03ff;	// enabling PPI data signals (D0 - D9).
	*pPORTG_FER |= 0x03ff;	// enabling PPI data signals (D0 - D9).
	nValue = *pPORT_MUX;
	*pPORT_MUX &= ~0x0200;
	*pPORT_MUX &= ~0x0200;

	// enable the ppi control signals (PPI_FS1, PPI_FS2, PPI_FS3 and PPIClk	
	nValue = *pPORTF_FER;
	*pPORTF_FER |= 0x8380;
	*pPORTF_FER |= 0x8380;
	nValue = *pPORT_MUX;
	*pPORT_MUX |= 0x0100;
	*pPORT_MUX |= 0x0100;
#endif		
#endif
		unsigned long nTimeout = CAPTURE_TIMEOUT;
		// clear flag that signals end of image transfer
		bCapturingFinished = false;
		// enable the ppi dma transfer
		ppi_enable(0);
		// wait for flag that signals end of image transfer
		while (!bCapturingFinished && nTimeout) {
			Sleep (1);						// wait for 1 ms
			nTimeout--;
		}
		// disable the ppi dma transfer
		ppi_disable(0);

		if (!nTimeout) {
			// a timeout occurs
			uart_writeString (hUART, "failed. Timeout error.\xd\xa");
			erCode = 2;
			// free the yuv image buffer
			free (YUVbuffer);
		} else {
			// everthing is fine, process the image
			uart_writeString (hUART, "successfully done.\xd\xa");
		
#if defined (RAW)
			imageConvertRaw10BitOrder (YUVbuffer, nXsize, nYsize);
#if defined (__ADSPBF533__)			
			imageConvertRaw10ToYUV422 (YUVbuffer + 2 * nXsize, YUVbuffer, nXsize, nYsize);
#else			
			imageConvertRaw10ToYUV422 (YUVbuffer, YUVbuffer, nXsize, nYsize);
#endif			
			nXsize = nXsize / 2;	// caused by the demosaicing process
			nYsize = nYsize / 2;	// caused by the demosaicing process
#elif defined (RGB565)
			imageConvertRGB565ToYUV422 (YUVbuffer, YUVbuffer, nXsize, nYsize);
#endif			

	unsigned long nFileSize = 0;
	
#ifdef BMP			
			// convert yuv image to 24bit rgb
			uart_writeString (hUART, "Converting image to RGB 24 bit...");
			char *RGBbuffer = (char *) malloc (nXsize * nYsize * 4);		// 4 byte per pixel
			ConvertYUVToRGB ((unsigned long)YUVbuffer, (unsigned long)RGBbuffer, nXsize, nYsize);
			free (YUVbuffer);
			uart_writeString (hUART, "done.\xd\xa");

			// getting memory for image file buffer
			char *ImageFileBuffer = (char *) malloc (nXsize * nYsize * 4);		// 4 byte per pixel

			// convert to windows bmp format			
			uart_writeString (hUART, "Converting image to windows bmp format...");
			nFileSize = ConvertRGBToWindowsBmp(0, 0, nXsize, nYsize, (unsigned long)RGBbuffer, (unsigned long)ImageFileBuffer);
			uart_writeString (hUART, "done.\xd\xa");
			free (RGBbuffer);
			
#elif defined (JPEG)
			char *ImageFileBuffer = (char *) malloc (nXsize * nYsize * 4);		// 4 byte per pixel
			out_extrn[0] = (unsigned long)ImageFileBuffer;
			jpeg_conversion_setup ();
			int quality	= JPEG_QUALITY;
			// limit quality to maximum	
			if (quality > 101) {
				quality = 101;
			}
			
			// do conversion
			uart_writeString (hUART, "Converting to JPEG...");
			nFileSize = convert_to_jpeg((int)YUVbuffer, nXsize, nYsize, quality);
			sprintf (sMessage, "done. Filesize: %u\xd\xa", nFileSize);
			uart_writeString (hUART, (unsigned char *)sMessage);
			
			free (YUVbuffer);

#else
#error "please select a image file format"
#endif
			
			uart_writeString (hUART, "Start file receiving on your terminal now. Protocol \"xmodem\".\xd\xa");
#ifdef BMP			
			uart_writeString (hUART, "Save file on host pc as \"*.bmp\"...");
#elif defined (JPEG)
			uart_writeString (hUART, "Save file on host pc as \"*.jpg\"...");
#endif			
			// transfering image over the uart with xmodem protocol
			long nBytesTransmittet = XM_TransmitFile (hUART, (unsigned char *)ImageFileBuffer, nFileSize);
			if (nBytesTransmittet > 0) {
				sprintf (sMessage, "successfully done. %u bytes transmittet.\xd\xa", nBytesTransmittet);
				uart_writeString (hUART, (unsigned char *)sMessage);
				uart_writeString (hUART, "You can open the file with an image viewer.\xd\xa");
			} else {
				uart_writeString (hUART, "failed.\xd\xa");
			}
			
			// free the image file buffer
			free (ImageFileBuffer);
		}
	} else {
		// no camera found
		uart_writeString (hUART, "failed. No camera found on PPI1.\xd\xa");
		erCode = 1;
	}
	
	// close the uart
	uart_close (hUART);
	// close the adi interrupt manager
	adi_int_Terminate ();
	
	// entering endless loop
	while (1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -