Exemplo n.º 1
0
//-----------------------------------------------------------------------------
// Function:	Initialize the general-purpose I/O
//
// Parameters:  none
//
// Returns:     none
//
//-----------------------------------------------------------------------------
int main(void)
{
	float yc_new, yc_old;   		// Cosine Samples
	float ys_new, ys_old;   		// Sine Sample
	float cosw, sinw;				// Sin/Cos Fractions

   EVMOMAPL138_init();				// Initialize the board
   EVMOMAPL138_initRAM();			// Set up the RAM
   EVMOMAPL138_enableDsp();			// Wake up the DSP

	// init the i2c for all to use.
	USTIMER_init();					// General use timers
	I2C_init(I2C0, I2C_CLK_400K);	// I2C initialization

	// set gpio output
	SetGpio();						// Configure the General Purpose I/O
	McASP_Init();					// Initialize McASP
	AIC3106_Init();					// Initialize AIC3106
	McASP_Start();					// Start McASP

	// Initialize oscillators
	ys_new = AMPLITUDE;
	yc_new = 0.0f;
	cosw = cos(THETA_INC);
	sinw = sin(THETA_INC);

	// Infinite loop:  	Each loop reads/writes one sample to the left and right channels.
	while (1){

		// Save old samples
		yc_old = yc_new;
		ys_old = ys_new;

		// Generate new samples
		yc_new = cosw*yc_old - sinw*ys_old;
		ys_new = cosw*ys_old + sinw*yc_old;

        // wait for xmit ready and send a sample to the left channel.
        while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
        MCASP->XBUF11 = (int16_t)yc_old;

        // wait for xmit ready and send a sample to the right channel.
        while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
        MCASP->XBUF11 = (int16_t)ys_old;
            	
     }   
}
Exemplo n.º 2
0
void sendAudioBlock(int16_t *inRight, int16_t *inLeft, unsigned int length){
	unsigned int sample;
	printf("Sending audio to Line out!\r\n");
	AudioInit();
    for (sample = 0; sample < length; sample++)
    {
       // wait for xmit ready and send a sample to the left channel.
       while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
       MCASP->XBUF11 = inLeft[sample]<<15 | 0x00000000;
//       MCASP->XBUF11 = (sinetable[sample%48] << 15) | 0x00000000;

       // wait for xmit ready and send a sample to the left channel.
       while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
       MCASP->XBUF11 = inRight[sample]<<15 | 0x00000000;
//       MCASP->XBUF11 = (sinetable[sample%48] << 15) | 0x00000000;
    }
}
Exemplo n.º 3
0
void getAudioBlock(int16_t *outRight, int16_t *outLeft, unsigned int length){
	unsigned int sample;
	printf("Receiving audio from Line in!\r\n");
	AudioInit();
	for (sample = 0; sample < length; sample++)
	{
		// wait for recv ready and send a sample to the left channel.
		while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
		outLeft[sample] = (int16_t)(MCASP->XBUF12<<1);// >> 16;//TODO: Only shift down by 15 instead of 16?
		MCASP->XBUF11 = 0; //For some reason this is necessary

		// wait for recv ready and send a sample to the right channel.
		while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
		outRight[sample] = (int16_t)(MCASP->XBUF12<<1);// >> 16;
		MCASP->XBUF11 = 0;
	}
}
Exemplo n.º 4
0
void mitt_test(void){
	int32_t dat;
	unsigned int sample;
	AudioInit();
	// loop audio
	while(1)
	{
		for (sample = 0; sample < 48; sample++){
			// wait for xmit ready and send a sample to the left channel.
			while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
			MCASP->XBUF11 = (sinetable[sample] << 15) | 0x00000000;

			// wait for xmit ready and send a sample to the left channel.
			while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
			MCASP->XBUF11 = (sinetable[sample] << 15) | 0x00000000;
		}
	}
}
Exemplo n.º 5
0
void print_bitboard(BitBoard b)
{
    int i, j;
    printf("---------------\n");
    for (i = 64; i > 0; i -= 8) {
        for (j = i - 8; j < i; ++j)
            printf("%s%s",  CHKBIT(b, j) ? "*" : ".", !((j + 1) % 8) ? "\n" : " ");
    }
    printf("---------------\n");
}
Exemplo n.º 6
0
Arquivo: sd_cmd.c Projeto: regop/sdcmd
int mmc_sleep_awake(char *dev_path, uint32_t args)
{
    int ret = -1;
    int dev_fd = -1;
    struct mmc_ioc_cmd cmd_desc;
    
    dev_fd = open(dev_path, O_RDONLY);
    if(dev_fd == -1)
    {
        perror("open device: ");
        return -1;
    }

    memset(&cmd_desc, 0, sizeof(cmd_desc));    
    cmd_desc.opcode = MMC_SLEEP_AWAKE;
    cmd_desc.arg = args;
    cmd_desc.flags = MMC_RSP_R1B;
    ret = ioctl(dev_fd, MMC_IOC_CMD, &cmd_desc);
    
    if (ret)
    {
        perror("ioctl");
    }
    else
    {
        if(CHKBIT(args,BIT(15)))
        {
            printf("Sleep\n");
        }
        else
        {
            printf("Awake\n");
        }
    }
        
#ifdef DEBUG
    printf("response 0 is 0x%08X\n", cmd_desc.response[0]);
    printf("response 1 is 0x%08X\n", cmd_desc.response[1]); 
    printf("response 2 is 0x%08X\n", cmd_desc.response[2]); 
    printf("response 3 is 0x%08X\n", cmd_desc.response[3]); 
#endif

    if(close(dev_fd))
    {
        perror("close device: ");
    }
    
    return ret;
    
     
}
Exemplo n.º 7
0
static void
i2c_start (void)
{
    //-------------------------------------------------------------------------
    // Tx START
    //-------------------------------------------------------------------------
    //  Write to TWCR. Write includes setting bit TWINT (clears INT flag).
    TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);

    //  Wait for INT flag to get set by hardware. Indicates START done.
    while (!(CHKBIT(TWCR,TWINT)))
    {
        ;// Do nothing until TWINT flag is set.
    }
    //  Check TWSR for successful START.
    if ((TWSR & 0xF8) != START)
    {
        ;// TO DO: Write an error routine.
    }    
}
Exemplo n.º 8
0
//-----------------------------------------------------------------------------
// Function:	Initialize the general-purpose I/O
//
// Parameters:  none
//
// Returns:     none
//
//-----------------------------------------------------------------------------
int main(void)
{
	int16_t sample = 0;				// Sample index into the buffer array
	int16_t dataInput;       		// Sine Sample
	int16_t oldDataInput;       	// Sine Sample delayed
	float theta = 0;				// Argument of the sine wave
	int16_t buffer[BUFF_SIZE] = {0};// Buffer to hold the sine samples
	float amplitude = AMPLITUDE;	// Amplitude of the generated sin wave

   EVMOMAPL138_init();				// Initialize the board
   EVMOMAPL138_initRAM();			// Set up the RAM
   EVMOMAPL138_enableDsp();			// Wake up the DSP


	// init the i2c for all to use.
	USTIMER_init();					// General use timers
	I2C_init(I2C0, I2C_CLK_400K);	// I2C initialization

	// set gpio output
	SetGpio();						// Configure the General Purpose I/O
	
	McASP_Init();					// Initialize McASP

	AIC3106_Init();					// Initialize AIC3106

	McASP_Start();					// Start McASP

	// Infinite loop:  	Each loop reads/writes one sample to the left and right channels.
	while (1){

/* The following code is here to allow a test signal to be generated. THIS IS
NOT HOW LAB 1 IS TO BE DONE!  Remove the code up to the indicated spot and insert
your own code.
*/

		// Store the last sample because of the one sample delay between channels.
		oldDataInput = dataInput;

		// Calculate the next sine wave sample...
        dataInput = ((int16_t) amplitude*sin(theta));
			
		// Increment the argument...
		theta += THETA_INC;
        if (theta > 2*PI) theta -= 2*PI;  	// Wrap around 2pi
			
		// Store the new sample in the buffer, for viewing...
        buffer[sample] = dataInput;
        sample = (sample+1)%BUFF_SIZE;      // Update the sample indx
			
/* Before writing the 16-bit samples, you must insert your own processing here. */

        // wait for xmit ready and send a sample to the left channel.
        while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
        MCASP->XBUF11 = oldDataInput;
//        oldDataInput = MCASP->XBUF12;	// Read the left channel input samples.

        // wait for xmit ready and send a sample to the right channel.
        while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
        MCASP->XBUF11 = dataInput;
//        dataInput = MCASP->XBUF12;	// Read the right channel input samples.
            	
     }   
}
Exemplo n.º 9
0
//-----------------------------------------------------------------------------
// Function:	Initialize the general-purpose I/O
//
// Parameters:  none
//
// Returns:     none
//
//-----------------------------------------------------------------------------
int main(void)
{
	int i, k;
	float out;
	int circ_idx = FILTER_LEN-1;
	float coeffs[FILTER_LEN] = { FIR_FILT_32 };
	float z[FILTER_LEN];

	int16_t dataInput;       		// Sine Sample
	int16_t oldDataInput;       	// Sine Sample delayed

   EVMOMAPL138_init();				// Initialize the board
   EVMOMAPL138_initRAM();			// Set up the RAM
   EVMOMAPL138_enableDsp();			// Wake up the DSP


	// init the i2c for all to use.
	USTIMER_init();					// General use timers
	I2C_init(I2C0, I2C_CLK_400K);	// I2C initialization

	// set gpio output
	SetGpio();						// Configure the General Purpose I/O
	
	McASP_Init();					// Initialize McASP

	AIC3106_Init();					// Initialize AIC3106

	McASP_Start();					// Start McASP

	// Infinite loop:  	Each loop reads/writes one sample to the left and right channels.
	while (1){

/* The following code is here to allow a test signal to be generated. THIS IS
NOT HOW LAB 1 IS TO BE DONE!  Remove the code up to the indicated spot and insert
your own code.
*/

		// Store the last sample because of the one sample delay between channels.
		oldDataInput = dataInput;

		//
		// Update the circular buffer index and insert the latest sample
		//
		circ_idx++;
		z[circ_idx & 0x1F] = (float)dataInput;

		//
		// Compute next filter output
		//
		out = 0.0f;                     // Zero the accumulator
		for( i=0; i<FILTER_LEN; i++ )
		{
			k = (circ_idx - i) & 0x1F;  // Index into circular buffer
			out += coeffs[i]*z[k];		// Multiply by coefficient
		}

		dataInput = (int16_t) out;

        // wait for xmit ready and send a sample to the left channel.
        while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
        MCASP->XBUF11 = oldDataInput;
		oldDataInput = MCASP->XBUF12;	// Read the left channel input samples.

        // wait for xmit ready and send a sample to the right channel.
        while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
        MCASP->XBUF11 = dataInput;
		dataInput = MCASP->XBUF12;	// Read the right channel input samples.
            	
     }   
}
Exemplo n.º 10
0
uint32_t AudioInit(void){
	uint32_t rtn = ERR_NO_ERROR;

	//------------------------------------
	// initialize the required bsl modules
	rtn = MCASP_init();
	if (rtn != ERR_NO_ERROR)
	{
		printf("error initializing mcasp!\r\n");
		return (rtn);
	}

	rtn = AIC3106_init();
	if (rtn != ERR_NO_ERROR)
	{
		printf("error initializing aic3106!\r\n");
		return (rtn);
	}

	// enable the audio clocks, verifying each bit is properly set.
	SETBIT(MCASP->XGBLCTL, XHCLKRST);
	while (!CHKBIT(MCASP->XGBLCTL, XHCLKRST)) {}
	SETBIT(MCASP->RGBLCTL, RHCLKRST);
	while (!CHKBIT(MCASP->RGBLCTL, RHCLKRST)) {}

	SETBIT(MCASP->XGBLCTL, XCLKRST);
	while (!CHKBIT(MCASP->XGBLCTL, XCLKRST)) {}
	SETBIT(MCASP->RGBLCTL, RCLKRST);
	while (!CHKBIT(MCASP->RGBLCTL, RCLKRST)) {}

	SETBIT(MCASP->XGBLCTL, XSRCLR);
	while (!CHKBIT(MCASP->XGBLCTL, XSRCLR)) {}
	SETBIT(MCASP->RGBLCTL, RSRCLR);
	while (!CHKBIT(MCASP->RGBLCTL, RSRCLR)) {}

	/* Write a 0, so that no underrun occurs after releasing the state machine */
	MCASP->XBUF11 = 0;
	//MCASP->RBUF12 = 0;

	SETBIT(MCASP->XGBLCTL, XSMRST);
	while (!CHKBIT(MCASP->XGBLCTL, XSMRST)) {}
	SETBIT(MCASP->RGBLCTL, RSMRST);
	while (!CHKBIT(MCASP->RGBLCTL, RSMRST)) {}

	SETBIT(MCASP->XGBLCTL, XFRST);
	while (!CHKBIT(MCASP->XGBLCTL, XFRST)) {}
	SETBIT(MCASP->RGBLCTL, RFRST);
	while (!CHKBIT(MCASP->RGBLCTL, RFRST)) {}

	// wait for transmit ready and send a dummy byte.
	while(!CHKBIT(MCASP->SRCTL11, XRDY)) {}
	MCASP->XBUF11 = 0;

	return rtn;
}
Exemplo n.º 11
0
//-----------------------------------------------------------------------------
// Function:	Initialize the general-purpose I/O
//
// Parameters:  none
//
// Returns:     none
//
//-----------------------------------------------------------------------------
int main(void)
{
	int i;
	float fir_coeffs[FILTER_LEN] = { FIR_FILT_32 };
	float lat_coeffs[FILTER_LEN] = {0.0f};
	float f_prev=0.0f, g_new=0.0f, g_prev=0.0f;
	float g[FILTER_LEN+1]={0.0f};

	int16_t dataInput;       		// Sine Sample
	int16_t oldDataInput;       	// Sine Sample delayed

	//
	// Compute reflection coefficients for lattice filter
	//
	MakeLatticeCoeffs(fir_coeffs, lat_coeffs);

   EVMOMAPL138_init();				// Initialize the board
   EVMOMAPL138_initRAM();			// Set up the RAM
   EVMOMAPL138_enableDsp();			// Wake up the DSP


	// init the i2c for all to use.
	USTIMER_init();					// General use timers
	I2C_init(I2C0, I2C_CLK_400K);	// I2C initialization

	// set gpio output
	SetGpio();						// Configure the General Purpose I/O
	
	McASP_Init();					// Initialize McASP

	AIC3106_Init();					// Initialize AIC3106

	McASP_Start();					// Start McASP

	// Infinite loop:  	Each loop reads/writes one sample to the left and right channels.
	while (1){

/* The following code is here to allow a test signal to be generated. THIS IS
NOT HOW LAB 1 IS TO BE DONE!  Remove the code up to the indicated spot and insert
your own code.
*/

		// Store the last sample because of the one sample delay between channels.
		oldDataInput = dataInput;

		//
		// Update the first stage of the lattice filter
		//
		f_prev = dataInput;			// Set f_0 equal to the input sample
		g_prev = g[0];			// Save the value stored in g[0]
		g[0] = dataInput;				// Update g[0]

		//
		// Iteratively compute next filter output for remaining stages
		//
		for( i=1; i<=FILTER_LEN; i++ )
		{
			// Save the value currently stored in the i-th delay element
			g_new = g[i];
			// Compute g, and store it in the i-th delay element
			g[i] = g_prev + f_prev*lat_coeffs[i-1];
			// Compute f
			f_prev += g_prev*lat_coeffs[i-1];
			// Pass the saved value from the delay element to the next stage
			g_prev = g_new;
		}

		//
		// Subtract the current input sample (Due to FIR to Lattice conversion)
		//
		dataInput = (int16_t) f_prev - dataInput;


        // wait for xmit ready and send a sample to the left channel.
        while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
        MCASP->XBUF11 = oldDataInput;
		oldDataInput = MCASP->XBUF12;	// Read the left channel input samples.

        // wait for xmit ready and send a sample to the right channel.
        while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
        MCASP->XBUF11 = dataInput;
		dataInput = MCASP->XBUF12;	// Read the right channel input samples.
            	
     }   
}
Exemplo n.º 12
0
uint32_t testAudioLineOut(void)
{
   uint32_t rtn = ERR_NO_ERROR;
   int16_t msec, sec;
   int16_t sample;

   // enable the audio clocks, verifying each bit is properly set.
   SETBIT(MCASP->XGBLCTL, XHCLKRST);
   while (!CHKBIT(MCASP->XGBLCTL, XHCLKRST)) {}
   SETBIT(MCASP->RGBLCTL, RHCLKRST);
   while (!CHKBIT(MCASP->RGBLCTL, RHCLKRST)) {}

   SETBIT(MCASP->XGBLCTL, XCLKRST);
   while (!CHKBIT(MCASP->XGBLCTL, XCLKRST)) {}
   SETBIT(MCASP->RGBLCTL, RCLKRST);
   while (!CHKBIT(MCASP->RGBLCTL, RCLKRST)) {}

   SETBIT(MCASP->XGBLCTL, XSRCLR);
   while (!CHKBIT(MCASP->XGBLCTL, XSRCLR)) {}
   SETBIT(MCASP->RGBLCTL, RSRCLR);
   while (!CHKBIT(MCASP->RGBLCTL, RSRCLR)) {}

   /* Write a 0, so that no underrun occurs after releasing the state machine */
   MCASP->XBUF11 = 0;
   //MCASP->RBUF12 = 0;

   SETBIT(MCASP->XGBLCTL, XSMRST);
   while (!CHKBIT(MCASP->XGBLCTL, XSMRST)) {}
   SETBIT(MCASP->RGBLCTL, RSMRST);
   while (!CHKBIT(MCASP->RGBLCTL, RSMRST)) {}

   SETBIT(MCASP->XGBLCTL, XFRST);
   while (!CHKBIT(MCASP->XGBLCTL, XFRST)) {}
   SETBIT(MCASP->RGBLCTL, RFRST);
   while (!CHKBIT(MCASP->RGBLCTL, RFRST)) {}

   // wait for transmit ready and send a dummy byte.
   while(!CHKBIT(MCASP->SRCTL11, XRDY)) {}
   MCASP->XBUF11 = 0;

   // transmit beep.
   for (sec = 0; sec < 5; sec++)
   {
      for (msec = 0; msec < 1000; msec++)
      {
         for (sample = 0; sample < 48; sample++)
         {
            // wait for xmit ready and send a sample to the left channel.
            while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
            MCASP->XBUF11 = (sinetable[sample] << 15) | 0x00000000;

            // wait for xmit ready and send a sample to the left channel.
            while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
            MCASP->XBUF11 = (sinetable[sample] << 15) | 0x00000000;
         }
      }
   }
   return (rtn);
}
Exemplo n.º 13
0
// =============================================================================
// 功能:IIC接收与发送中断服务函数。该函数实现的功能如下:
//       1.每发送与接收一个或若干字节发生一次中断;
//       2.若有多个中断使用同一个中断号,则需根据具体情况区分使用的是哪个中断;
//       3.清中断标志,并判断ACK信号,每读写字节,计数器都需相应修改;
//       4.接收达到倒数第一个字节时,需配置不发送ACK信号;
//       5.接收或发送完成时,需post信号量IntParam->pDrvPostSemp;
//       6.接收或发送完成时,需产生停止时序。
// 参数:i2c_int_line,中断号,本函数没用到
// 返回:无意义
// =============================================================================
static u32 __IIC_ISR(ufast_t i2c_int_line)
{

    static struct tagIIC_IntParamSet *IntParam;
    static struct tagIIC_CB *ICB;
    tagI2CReg *reg;
    u8 ch;
    u32 IicErrorNo;
    u32 irptl_temp=*rTWIIRPTL;//read IRPTL

    reg = (tagI2CReg*)CN_IIC_REGISTER_BADDR0;
    ICB=&s_IIC0_CB;
    IntParam=&IntParamset0;

        //MASTER TX\RX COMPLETE
        if( (irptl_temp & TWITXINT) != 0 )      //发送中断
        {
            if(!(CHKBIT(reg->rTWIMSTAT, TWIANAK)|CHKBIT(reg->rTWIMSTAT, TWIDNAK)))
            {
                //从泛设备读一个字节的数据,并发送
                if(IIC_PortRead(ICB,&ch,1) > 0)
                {
                    *rTXTWI8 = ch;
                    IntParam->TransCount++;
                }
                else if(IntParam->TransCount == IntParam->TransTotalLen)
                {
                    //in Master TX Mode , we need to STOP TWI by ourself
                    Lock_SempPost(IntParam->pDrvPostSemp);
                     __IIC_GenerateStop(reg);
                }
                else
                {
                 IicErrorNo = CN_IIC_NO_ACK_ERR;//调用错处处理API函数
                 IIC_ErrPop(ICB,IicErrorNo);
                }
            }
            else    //TX no ACK
            {
                IicErrorNo = CN_IIC_NO_ACK_ERR;//调用错处处理API函数
                IIC_ErrPop(ICB,IicErrorNo);
                return 1;
            }
            //clear IIC interrupt
            irptl_temp = TWITXINT;
            *rTWIIRPTL = irptl_temp;
        }
        else if( (irptl_temp & TWIRXINT) != 0 )      //接收中断
        {
            if(!(CHKBIT(reg->rTWIMSTAT, TWIANAK)|CHKBIT(reg->rTWIMSTAT, TWIDNAK)))
            {
                ch = *rRXTWI8;
                IIC_PortWrite(ICB,&ch,1);
                IntParam->TransCount ++;
                if(IntParam->TransCount == IntParam->TransTotalLen)
                {
                    __IIC_GenerateStop(reg);
                    Lock_SempPost(IntParam->pDrvPostSemp);//释放总线信号量
                }
            }

            else    //RX no ACK
            {

            }
            //clear IIC interrupt
            irptl_temp = TWIRXINT;
            *rTWIIRPTL = irptl_temp;
        }
        else    //TWIMERR
        {
        }

    irptl_temp==*rTWIIRPTL; //update TWI_IRPTL

        //MASTER TRANS COMPLETE
        if( (irptl_temp & TWIMCOM) != 0 )
        {
            _IIC_GenerateDisable(reg);

            Lock_SempPost(ICB->iic_bus_semp);//释放总线信号量

            //clear STOP
            CLRBIT(reg->rTWIMCTL, TWISTOP);
            //clear IIC interrupt
            irptl_temp |= TWIMCOM;
            *rTWIIRPTL = irptl_temp;
        }

    return 0;
}
Exemplo n.º 14
0
uint16_t
get_temperature (uint8_t i2c_addr)
{
    uint8_t i2c_w_addr, i2c_r_addr;
    i2c_w_addr = i2c_addr << 1;
    i2c_r_addr = i2c_w_addr + 1;
    // Read two bytes over I2C.
    // The TMP102 points to its Temperature Register on power-up.
    // No need to write the pointer register.
    // Just do an I2C read of length two.

    uint16_t degC_12bit = 0;

    //-------------------------------------------------------------------------
    // Tx START
    //-------------------------------------------------------------------------
    //  Write to TWCR. Write includes setting bit TWINT (clears INT flag).
    TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);

    //  Wait for INT flag to get set by hardware. Indicates START done.
    while (!(CHKBIT(TWCR,TWINT)))
    {
        ;// Do nothing until TWINT flag is set.
    }
    //  Check TWSR for successful START.
    if ((TWSR & 0xF8) != START)
    {
        ;// TO DO: Write an error routine.
    }    
    //-------------------------------------------------------------------------

    //-------------------------------------------------------------------------
    // Tx SLA+R
    //-------------------------------------------------------------------------
    //  Load TWDR with SLA+R.
    TWDR = i2c_r_addr; // TO DO: replace with variable that holds slave address.

    //  Write to TWCR. Write includes setting bit TWINT.
    TWCR = (1<<TWINT)|(1<<TWEN);
    
    //  Wait for INT flag to get set by hardware.
    //  Indicates slave adress was transmitted and ACK/NACK received.
    while (!(CHKBIT(TWCR,TWINT)))
    {
        ;// Do nothing until TWINT flag is set.
    }
    //  Check TWSR for successful packet and ACK.
    if ((TWSR & 0xF8) != MR_SLA_ACK)
    {
        ;// TO DO: Write an error routine.
    }

    //-------------------------------------------------------------------------
    // Rx DATA Byte 1
    //-------------------------------------------------------------------------
    //  Write to TWCR. Write includes setting bit TWINT.
    //  Set TWEA to send an ACK.
    TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
    
    //  Wait for INT flag to get set by hardware.
    //  Indicates data was received and ACK returned.
    while (!(CHKBIT(TWCR,TWINT)))
    {
        ;// Do nothing until TWINT flag is set.
    }

    //  Check TWSR for successful packet and ACK.
    if ((TWSR & 0xF8) != MR_DATA_ACK)
    {
        ;// TO DO: Write an error routine.
    }
    // Status == MR_DATA_ACK, so read the data.
    else
    {
        // Grab the received byte.
        uint8_t byte_in;
        byte_in = TWDR;
        // Store the byte in the MSB position of a 16-bit word.
        degC_12bit = byte_in;
        degC_12bit = degC_12bit << 8;
    }
    //-------------------------------------------------------------------------

    //-------------------------------------------------------------------------
    // Rx DATA Byte 2
    //-------------------------------------------------------------------------
    //  Write to TWCR. Write includes setting bit TWINT.
    //  Clear TWEA to send a NACK.
    TWCR = (1<<TWINT)|(1<<TWEN)|(0<<TWEA);
    
    //  Wait for INT flag to get set by hardware.
    //  Indicates data was received and ACK returned.
    while (!(CHKBIT(TWCR,TWINT)))
    {
        ;// Do nothing until TWINT flag is set.
    }

    //  Check TWSR for successful packet and NACK.
    if ((TWSR & 0xF8) != MR_DATA_NACK)
    {
        ;// TO DO: Write an error routine.
    }
    // Status == MR_DATA_NACK, so read the data.
    else
    {
        // Grab the received byte.
        uint8_t byte_in;
        byte_in = TWDR;
        // Store the byte in the LSB position.
        degC_12bit |= byte_in;
    }
    //-------------------------------------------------------------------------

    //-------------------------------------------------------------------------
    // Tx STOP
    //-------------------------------------------------------------------------
    //  Write to TWCR. Write includes setting bit TWINT (clears INT flag).
    TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
    
    //  The INT flag does not get set by hardware. Nothing to check.
    //-------------------------------------------------------------------------

    return degC_12bit;
}
Exemplo n.º 15
0
static void
i2c_tx (uint8_t i2c_addr, uint8_t data) // TO DO: need to write at least two bytes
                    // first is the pointer register, second is the value.
{
    uint8_t i2c_w_addr;
    i2c_w_addr = i2c_addr << 1;

    //-------------------------------------------------------------------------
    // Tx START
    //-------------------------------------------------------------------------
    //  Write to TWCR. Write includes setting bit TWINT (clears INT flag).
    TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);

    //  Wait for INT flag to get set by hardware. Indicates START done.
    while (!(CHKBIT(TWCR,TWINT)))
    {
        ;// Do nothing until TWINT flag is set.
    }
    //  Check TWSR for successful START.
    if ((TWSR & 0xF8) != START)
    {
        ;// TO DO: Write an error routine.
    }    
    //-------------------------------------------------------------------------

    //-------------------------------------------------------------------------
    // Tx SLA+W
    //-------------------------------------------------------------------------
    //  Load TWDR with SLA+W.
    TWDR = i2c_w_addr; // TO DO: replace with variable that holds slave address.

    //  Write to TWCR. Write includes setting bit TWINT.
    TWCR = (1<<TWINT)|(1<<TWEN);
    
    //  Wait for INT flag to get set by hardware.
    //  Indicates slave adress was transmitted and ACK/NACK received.
    while (!(CHKBIT(TWCR,TWINT)))
    {
        ;// Do nothing until TWINT flag is set.
    }
    //  Check TWSR for successful packet and ACK.
    if ((TWSR & 0xF8) != MT_SLA_ACK)
    {
        ;// TO DO: Write an error routine.
    }    

    //-------------------------------------------------------------------------
    // Tx DATA
    //-------------------------------------------------------------------------
    //  Load TWDR with DATA.
    TWDR = data;

    //  Write to TWCR. Write includes setting bit TWINT.
    TWCR = (1<<TWINT)|(1<<TWEN);    
    
    //  Wait for INT flag to get set by hardware.
    //  Indicates data was transmitted and ACK/NACK received.
    while (!(CHKBIT(TWCR,TWINT)))
    {
        ;// Do nothing until TWINT flag is set.
    }

    //  Check TWSR for successful packet and ACK.
    if ((TWSR & 0xF8) != MT_DATA_ACK)
    {
        ;// TO DO: Write an error routine.
    }    
    //-------------------------------------------------------------------------

    //-------------------------------------------------------------------------
    // Tx STOP
    //-------------------------------------------------------------------------
    //  Write to TWCR. Write includes setting bit TWINT (clears INT flag).
    TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
    
    //  The INT flag does not get set by hardware. Nothing to check.
    //-------------------------------------------------------------------------
   
    // Transmit an ACK
    // whenever a data byte is received:
    SETBIT(TWCR,TWEA);
    
    //  p.243: TWSR: Use a bit-rate prescaler of 1.
    CLRBIT(TWSR,TWPS0);
    CLRBIT(TWSR,TWPS1);

}
Exemplo n.º 16
0
//-----------------------------------------------------------------------------
// \brief   transmit data out the spi, at the same time receive data as well.
//
// \param   spi_regs_t *spi - pointer to reg struct for the desired spi port.
//
// \param   uint8_t *src_buffer - pointer to the data to transmit.
//
// \param   uint8_t *dest_buffer - pointer to memory to copy the data being received.
//
// \param   uint32_t in_length - number of bytes to transmit.
//
// \param   spi_cs_hold_e in_cs_hold
//             SPI_HOLD_NONE - do nothing with CSHOLD of SPIDAT1.
//             SPI_HOLD_ACTIVE - keep cs active after completing transfer.
//             SPI_HOLD_CLR - hold cs until the last byte, then clear.
//
// \return  uint32_t
//    ERR_NO_ERROR - input in bounds, byte transmitted.
//    ERR_INVALID_PARAMETER - null pointers.
//-----------------------------------------------------------------------------
uint32_t SPI_xfer(spi_regs_t *spi, uint8_t *src_buffer, uint8_t *dest_buffer, uint32_t in_length, spi_cs_hold_e in_cs_hold)
{
   uint32_t rtn = ERR_INVALID_PARAMETER;

   if (spi != NULL)
   {
      uint32_t i;
      uint32_t spi_data1 = spi->SPIDAT1;
      
      // empty the receive buffer.
      while (!CHKBIT(spi->SPIBUF, RXEMPTY))
      {
         i = spi->SPIBUF;
      }
      
      // set cs hold if desired.
      if ((SPI_HOLD_ACTIVE == in_cs_hold) ||
            (SPI_HOLD_CLR == in_cs_hold))
      {
         SETBIT(spi_data1, CSHOLD);
      }

      // transmit data one byte at a time, copy receive data into input buffer.
      for (i = 0; i < in_length; i++)
      {
         // wait for tx buffer to be empty.
         while (CHKBIT(spi->SPIBUF, TXFULL)) {}

         // clear cs hold if we were told to set it and this is the last byte.
         if ((SPI_HOLD_CLR == in_cs_hold) &&
               (i == (in_length - 1)))
         {
            CLRBIT(spi_data1, CSHOLD);
         }
         
         // clear the tx data buffer in the tmp data reg, then write the data.
         CLRBIT(spi_data1, MASK_TXDATA);
         if (src_buffer != NULL)
         {
            SETBIT(spi_data1, *src_buffer);
            // increment src pointer, if the caller is not using the same
            // memory for src and dest.
            if (src_buffer != dest_buffer)
            {
               src_buffer++;
            }
         }
         
         // copy the tmp reg to the real thing.
         spi->SPIDAT1 = spi_data1;
         
         // wait for data to arrive.
         while (CHKBIT(spi->SPIBUF, RXEMPTY)) {}
         
         // copy the received data.
         if (dest_buffer != NULL)
         {
            *dest_buffer = (uint8_t)spi->SPIBUF;
            // increment src pointer, if the caller is using the same
            // memory as src and dest.
            dest_buffer++;
            if (src_buffer == dest_buffer)
            {
               src_buffer++;
            }
         }
      }
      
      rtn = ERR_NO_ERROR;
   }

   return (rtn);
}
void init_ColorVision(void) {
	
	// initialize ov6620 cmos camera
	i2cdata[0] = 0x01;
	i2cdata[1] = 0x8F;  // Blue Gain control (default 0x80)
	i2cdata[2] = 0x8F;  // Red Gain Control
	i2cdata[3] = 0x80;  // Saturation
	i2cdata[4] = 0x00;  // Reserved
	i2cdata[5] = 0x4f;  // Contrast
	i2cdata[6] = 0x9f; // Brightness
	i2cdata[7] = 0xCF; // Sharpness  (default 0xC6) 			
	i2cdata[8] = 0x00; // Reserved			
	i2cdata[9] = 0x00; // Reserved   		
	i2cdata[10] = 0x00; // Reserved   		
	i2cdata[11] = 0x00; // Reserved   	
	i2cdata[12] = 0x20; // AWB - Blue   		
	i2cdata[13] = 0x20; // AWB - Red   		
	i2cdata[14] = 0x0D; // COMR   		
	i2cdata[15] = 0x05; // COMS   		
	i2cdata[16] = 0x9A; // AEC
	i2cdata[17] = 0x01; // CLKRC   		
	i2cdata[18] = 0x28; // COMA  
	i2cdata[19] = 0x01; // 0x01; // COMB  
	I2C_write(I2C0, 0x60, i2cdata, 20, SET_STOP_BIT_AFTER_WRITE);
	
	i2cdata[0] = 0x20;
	i2cdata[1] = 0x01; // COME
	I2C_write(I2C0, 0x60, i2cdata, 2, SET_STOP_BIT_AFTER_WRITE);
	
	i2cdata[0] = 0x28;
	i2cdata[1] = 0x81; // COMH
	I2C_write(I2C0, 0x60, i2cdata, 2, SET_STOP_BIT_AFTER_WRITE);
	
	i2cdata[0] = 0x39;
	// changed to PCLK always on.  
	i2cdata[1] = 0x00; //0x40; // COML
	I2C_write(I2C0, 0x60, i2cdata, 2, SET_STOP_BIT_AFTER_WRITE);
	
		
    VPIF_initReceive(VIDEO_CONN_CAMERA);

	// PRU setup
	EVMOMAPL138_lpscTransition(PSC0, DOMAIN0, LPSC_DMAX, PSC_ENABLE);
	PRU_stop(PRU0);
	PRU_reset(PRU0);
	PRU_load(PRU0_PROG, PRUCode, sizeof(PRUCode)/sizeof(uint32_t));
	PRU_reset(PRU0);
	PRU_run(PRU0);

	// VPIF (95) interrupt serviced by INT4
	ICR = 0x010; // clear pending interrupts
	IER |= 0x010; // enable interrupt on line    

	// PRU (6) interrupt serviced by INT6
	ICR = 0x040; // clear pending interrupts
	IER |= 0x040; // enable interrupt on line    
	
	pic_data = (int *)ADDR_VIDEO_DATA_BASE;

    Image_data = (bgr *)(IMAGE_DATA_MEM);
    
	Thres_Image = (unsigned char *)(THRES_IMAGE_MEM); 
		
	Linux_Image = (bgr *)(ADDR_VIDEO_DATA_BASE+LINUX_IMAGE_OFFSET);
	
	LCD_Image = (bgr *)(ADDR_VIDEO_DATA_BASE+LCD_IMAGE_OFFSET);
	
	ptrshrdmem = (sharedmemstruct *)SHARED_MEM;	


	while (CHKBIT(VPIF->INTSTAT,INT_FRAME_CH1) == 0) {}
	SETBIT(VPIF->INTSTATCLR, INT_FRAME_CH1);
	
}
Exemplo n.º 18
0
uint32_t testAudioLineIn(void)
{
   uint32_t rtn = ERR_NO_ERROR;
   int16_t msec, sec, sample;
   int32_t dat;

   // enable the audio clocks, verifying each bit is properly set.
   SETBIT(MCASP->XGBLCTL, XHCLKRST);
   while (!CHKBIT(MCASP->XGBLCTL, XHCLKRST)) {}
   SETBIT(MCASP->RGBLCTL, RHCLKRST);
   while (!CHKBIT(MCASP->RGBLCTL, RHCLKRST)) {}

   SETBIT(MCASP->XGBLCTL, XCLKRST);
   while (!CHKBIT(MCASP->XGBLCTL, XCLKRST)) {}
   SETBIT(MCASP->RGBLCTL, RCLKRST);
   while (!CHKBIT(MCASP->RGBLCTL, RCLKRST)) {}

   SETBIT(MCASP->XGBLCTL, XSRCLR);
   while (!CHKBIT(MCASP->XGBLCTL, XSRCLR)) {}
   SETBIT(MCASP->RGBLCTL, RSRCLR);
   while (!CHKBIT(MCASP->RGBLCTL, RSRCLR)) {}

   /* Write a 0, so that no underrun occurs after releasing the state machine */
   MCASP->XBUF11 = 0;

   SETBIT(MCASP->XGBLCTL, XSMRST);
   while (!CHKBIT(MCASP->XGBLCTL, XSMRST)) {}
   SETBIT(MCASP->RGBLCTL, RSMRST);
   while (!CHKBIT(MCASP->RGBLCTL, RSMRST)) {}

   SETBIT(MCASP->XGBLCTL, XFRST);
   while (!CHKBIT(MCASP->XGBLCTL, XFRST)) {}
   SETBIT(MCASP->RGBLCTL, RFRST);
   while (!CHKBIT(MCASP->RGBLCTL, RFRST)) {}

   // wait for transmit ready and send a dummy byte.
   while(!CHKBIT(MCASP->SRCTL11, XRDY)) {}
   MCASP->XBUF11 = 0;

   // loop audio
   for (sec = 0; sec < 15; sec++)
   {
      for (msec = 0; msec < 1000; msec++)
      {
         for (sample = 0; sample < 48; sample++)
         {
            // wait for recv ready and send a sample to the left channel.
            while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
   			MCASP->XBUF11 = dat;
            dat = MCASP->XBUF12;

            // wait for recv ready and send a sample to the right channel.
            while (!CHKBIT(MCASP->SRCTL11, XRDY)) {}
   			MCASP->XBUF11 = dat;
            dat = MCASP->XBUF12;
            //printf("you received %d\n", dat);
         }
      }
   }
   return (rtn);
}