默れ

图像采集初始化—i2c、PPI

0
阅读(2442)
文件实现了BF533的PPI,Time,中断等函数的初始化 
****************************************************************************/ 
#include 
#include 
#include 
#include "../dm_bf5xx.h"
#include "../dm_cpu.h"

#define POLC  0x00004000
#define PORT_CFG_2_3_EXT_FS 0x00000020 
#define XFR_TYPE_NON_ITU656 0x0000000C

/****************************************************************************
* 名称 :Init_Timers
* 功能 :初始化TIMER0 为PWM模式。
* 入口参数 :无
* 出口参数 :无
****************************************************************************/
void Init_Timers(void)
{
	*pTIMER0_CONFIG		= 0x0019;
	*pTIMER0_PERIOD		= 0x00800000;
	*pTIMER0_WIDTH		= 0x00400000;
	*pTIMER_ENABLE		= 0x0001;
}
/****************************************************************************
* 名称 :Init_Interrupts
* 功能 : DMA0 PPI interrupt 允许	
* 入口参数 :无
* 出口参数 :无
****************************************************************************/
void Init_Interrupts(void)
{
	*pSIC_IAR0 = *pSIC_IAR0 & 0xffffffff | 0x00000000;	
	*pSIC_IAR1 = *pSIC_IAR1 & 0xffffffff | 0x00000001;	 
	*pSIC_IAR2 = *pSIC_IAR2 & 0xffffffff | 0x00000000;														
	register_handler(ik_ivg8, DMA0_PPI_ISR);		 
   	*pSIC_IMASK=0x00000100; 	 
}
/****************************************************************************
* 名称 :Init_CPLD
* 功能 :选通CMOS 245 并选择输入PPI 时钟为外部CMOS PCLK 时钟
* 入口参数 :无
* 出口参数 :无
****************************************************************************/
void Init_CPLD(void)
 { 
    
	*pCtrOut_Flag_OE=~CMOS_245_OE; //CMOS_OE 245 允许 ADG_OE=1
    *pCtrOut_Flag_B  =~CMOSCLK_SET; 
}

/****************************************************************************
* 名称 :Init_Platform
* 功能 : 初始化硬件平台	
* 入口参数 :无
* 出口参数 :无
****************************************************************************/
void Init_Platform(void)
{
    Set_PLL(22,5) ;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
    Setup_Flags();
    Init_EBIU();
    Init_SDRAM();
    Init_Flash();
    Init_CPLD();
  	Init_Interrupts();  
}
/****************************************************************************
* 名称 :Video_Frame_Capture
* 功能 : 初始化PPI控制器 初始化DMA0控制器
* 入口参数 :无
* 出口参数 :无
****************************************************************************/
void    Video_Frame_Capture( uint8_t *pAdd )
{
	*pPPI_FRAME = LINES_PER_FRAME;  		
	*pPPI_COUNT = PPICOUNT;	
	*pPPI_DELAY = 0;
	*pPPI_CONTROL =  POLC | PACK_EN | DLEN_8 | PORT_CFG_2_3_EXT_FS | XFR_TYPE_NON_ITU656;	
	*pDMA0_START_ADDR = pAdd;	
	*pDMA0_X_COUNT = PIXEL_PER_LINE;
	*pDMA0_X_MODIFY = 0x2;			
	*pDMA0_Y_COUNT = LINES_PER_FRAME;	
	*pDMA0_Y_MODIFY = 0x2;												
	*pDMA0_PERIPHERAL_MAP = 0x0;			
	*pDMA0_CONFIG =  WNR | WDSIZE_16| DMA2D | RESTART | DI_EN;	
	*pDMA0_CONFIG |= DMAEN;
	ssync();
	*pPPI_CONTROL |= PORT_EN;
	ssync();				
}

/****************************************************************************
* 名称 :EX_INTERRUPT_HANDLER
* 功能 : 中断函数 清 DMA  中断标志,关PPI
* 入口参数 :无
* 出口参数 :无
****************************************************************************/

EX_INTERRUPT_HANDLER(DMA0_PPI_ISR)
{	
        *pDMA0_IRQ_STATUS = 0x1;   
		*pPPI_CONTROL &= 0xfffe;
	    printf( "\nSuccess Capture One Frame:  VGA(320*240)\n" );
	    printf( "                            YUV422 Format\n" );
}


文件实现了i2c总线的底层函数定义
****************************************************************************/ 
#include 
#include 
#include 
#include 
#include "dm_cmos_i2c.h"
#include "../dm_types.h"

// 板子上的晶振大小 12 MHz
#define __CORE_CLK_IN__ 27 * 1000 * 1000

#define SET_PF(pf)   \
	do{\
	   *pFIO_FLAG_S = (pf);\
	   ssync();\
	}while(0)

#define CLR_PF(pf)   \
	do{\
	   *pFIO_FLAG_C = (pf);\
	   ssync();\
	}while(0)

#define SET_PF_OUTPUT(pf)   \
	do{\
	   *pFIO_INEN  &= ~(pf);\
	   *pFIO_DIR	|= (pf);\
	   ssync();\
	}while(0)

#define SET_PF_INPUT(pf)   \
	do{\
	   *pFIO_DIR	&= ~(pf);\
	   *pFIO_INEN  |= (pf);\
	   ssync();\
	}while(0)

int_t get_core_clk(void)
{
	uint_t tempPLLCTL;
	uint_t _DF;
	uint_t VCO;
	uint_t MSEL;
	
	tempPLLCTL = *pPLL_CTL;
	
	//get MSEL, DF
	MSEL = ((tempPLLCTL & 0x7E00) >> 9);
	_DF   =  tempPLLCTL & 0x0001;
	
	VCO  = MSEL * __CORE_CLK_IN__;
	if(_DF == 1)
		VCO /= 2;
	
	return  VCO;
}
void delay_ns(uint_t core_clock, uint64_t count)
{
	count *= core_clock;
	count /= 1000000000;
	while(count--);
//	wait_cycles(count);
}

int_t _get_sdata(i2c_device * dev)
{
	return ((*pFIO_FLAG_D & dev->sdata) ? 1 : 0);
}

void i2c_init(i2c_device * dev)
{
   dev->core_clock = get_core_clk();
   dev->delay_ns = delay_ns;
	*pFIO_DIR |=  dev->sclk | dev->sdata;
	ssync();
}

void i2c_deinit(i2c_device * dev)
{
	dev->sclk = 0;
	dev->sdata = 0;
	
	*pFIO_DIR &=  ~(dev->sclk | dev->sdata);
	ssync();
}

void i2c_start(i2c_device * dev)
{
	SET_PF_OUTPUT(dev->sdata);
	SET_PF_OUTPUT(dev->sclk);
	
	SET_PF(dev->sdata);
	SET_PF(dev->sclk);
	delay_ns(dev->core_clock, dev->high_ns);
	
	CLR_PF(dev->sdata);
	delay_ns(dev->core_clock, dev->low_ns);
	
	CLR_PF(dev->sclk);
	delay_ns(dev->core_clock, dev->low_ns);
}

void i2c_stop(i2c_device * dev)
{
	CLR_PF(dev->sclk);
	delay_ns(dev->core_clock, dev->low_ns);
	
	SET_PF_OUTPUT(dev->sdata);
	CLR_PF(dev->sdata);
	delay_ns(dev->core_clock, dev->low_ns);
	
	SET_PF_INPUT(dev->sclk);
	delay_ns(dev->core_clock, dev->high_ns);
	
	SET_PF_INPUT(dev->sdata);
	delay_ns(dev->core_clock, dev->high_ns);
}

int_t i2c_read_ack(i2c_device * dev)
{
	int_t ret = 0;
	
	SET_PF_INPUT(dev->sdata);
	delay_ns(dev->core_clock, dev->high_ns/3);
	
	SET_PF(dev->sclk);
	delay_ns(dev->core_clock, dev->high_ns/3);
	
	ret = _get_sdata(dev);
	
	delay_ns(dev->core_clock, dev->high_ns/3);
	CLR_PF(dev->sclk);
	
	delay_ns(dev->core_clock, dev->low_ns);
	
	SET_PF_OUTPUT(dev->sdata);
	return ret;
}

int_t i2c_wait_slave(i2c_device * dev, uint_t time_out)
{
	int_t ret;
	int_t count = time_out * 2 / dev->high_ns;
	
	SET_PF_INPUT(dev->sclk);
	delay_ns(dev->core_clock, dev->high_ns/2);
	
	do{
		ret = *pFIO_FLAG_D & dev->sclk;
		if(ret)
		   break;
		delay_ns(dev->core_clock, dev->high_ns/2);
	}while(count--);
	
	SET_PF_OUTPUT(dev->sclk);
	return !ret;
}

void i2c_write_ack(i2c_device * dev)
{
	SET_PF_OUTPUT(dev->sdata);
	CLR_PF(dev->sdata);
	delay_ns(dev->core_clock, dev->high_ns/2);
	SET_PF(dev->sclk);
	delay_ns(dev->core_clock, dev->high_ns);
	
	CLR_PF(dev->sclk);
	delay_ns(dev->core_clock, dev->low_ns);
}

int_t i2c_write(i2c_device * dev, uint8_t value, int_t need_ack)
{
	int_t ret = -1;
	uint8_t index;
	
	SET_PF_OUTPUT(dev->sdata);
	
	//send 8 bits to slave
	for(index = 0; index < 8; index++){
		//send one bit to the i2c bus
		if((value<sdata);
		} else {
			CLR_PF(dev->sdata);
		}
		
		delay_ns(dev->core_clock, dev->low_ns/2);
		
		SET_PF(dev->sclk);
		delay_ns(dev->core_clock, dev->high_ns);
		
		CLR_PF(dev->sclk);
		delay_ns(dev->core_clock, dev->low_ns/2);
	}
	
	if(need_ack){
		ret = i2c_read_ack(dev);
	}
	return ret;
}

int_t i2c_read(i2c_device * dev, uint8_t * value, int_t send_ack)
{
	uint8_t index;
	*value = 0x00;
	
	SET_PF_INPUT(dev->sdata);
	delay_ns(dev->core_clock, dev->high_ns/2);
	
	//get 8 bits from the device
	for(index = 0; index < 8; index++){
		SET_PF(dev->sclk);
		delay_ns(dev->core_clock, dev->high_ns/2);
		
		*value <<= 1;
		*value |= _get_sdata(dev);
		
		delay_ns(dev->core_clock, dev->high_ns/2);
		
		CLR_PF(dev->sclk);
		delay_ns(dev->core_clock, dev->low_ns);
	}
	
	// send ack to slave
	if(send_ack){
		i2c_write_ack(dev);
	}
	return *value;
}