main.c

来自「M16C瑞萨平台」· C语言 代码 · 共 884 行 · 第 1/2 页

C
884
字号
/*
 * Copyright (c) 2007 EISLAB - Lulea University of Technology.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * The purpose of this program is to toggle LED via external interrupt
 *
 */

#include "sema.h"
#include "hardware.h"
#include "inthandler.h"
#include "rv8564.h"
 
typedef unsigned char	uchar;
typedef unsigned char	uint8;
typedef unsigned short	uint16;
typedef unsigned int	uint32;

#define TRUE 1
#define FALSE 0

sema_t semaphores[3] = {0};
 
extern int Readback_8564ram[16];
extern int ACK_IS_SET;
extern int ACK;



uchar  Msg4Trans;	
 						   
int b,l= 0;
uint8 toggle_led = 0;

/*******************   Function declaration *******************************/
void idle(void);
void transition();
void toggle(int);
void blink(int);

void Write_x1288();
void iic_mr_ini(void);


void i2c_init(void);

void i2c_ini(void);          //asembely language using part
void iic_mw_start(void);     //asembely language using part
void m_start(void);          //asembely language using part

void i2c_enable_rx(void);
void i2c_disable_rx(void);

void i2c_start(void);
void i2c_restart(void);
void i2c_stop(void);

/****************** Use These Functions To Simulate The I2C Protocal ********************************/
void  SendByte(uchar);
void  TxNByte(uchar,uchar,uchar *,uchar);
uchar RcvByte(void);
void  AckI2C_MstAck(int);
void  RV8564ByteWrite(uchar,uchar,uchar);



void I2C_WAIT_UNTIL_FINISHED(void);
void TwinkleLedP26(void);
void TwinkleLedP27(void);

void WriteOneByteRV8564(int,int);
/**************************************************/



int main (void)
{
 
 hw_initialise();
 PD4.BIT.PD4_7 = OUTPUT;		    /* setting port P47 as OUTPUT port*/
 P4.BIT.P4_7 = HIGH;                 /* set Low RTC clock output disable, high enable CLK output*/
 
 
  PD7.BIT.PD7_0 = OUTPUT;	// SDA2	     
  PD7.BIT.PD7_1 = OUTPUT;
 

  

  INT0IC.BYTE = 0x00; //Enable, diasble RTC clk output interrupt INT0
  INT1IC.BYTE = 0x04;			    /* enable interrupt1, level 3, rising edge */
  INT2IC.BYTE = 0x04;               //Enable RTC interrupt INT2 for RV8564 Timer.....
  // IFSR.BIT.IFSR2=1;
  
  INT4IC.BYTE = 0x04;			    /* enable interrupt4, level 3, rising edge */ 
  IFSR.BIT.IFSR6=1;                 //set 1 for Int4 selected, or iso int, int. source switch
  
  asm("fset i");			    /* enable interrupts if any */
  
  
//  i2c_ini();          //asembely language using part
//  iic_mw_start();     //asembely language using part

    ACK_IS_SET=0;
    
//   	i2c_init();	
//    	i2c_start();
//	    i2c_enable_rx();


	
/********** Backup code running before debugging pause ... ****************************************/	
	
	RV8564ByteWrite(RV8564_WR_ADDR,RV8564_CTR1,0x00);
    RV8564ByteWrite(RV8564_WR_ADDR,RV8564_CTR2,0x13);
		
    RV8564ByteWrite(RV8564_WR_ADDR,RV8564_CLKF,0x82);// Enable the CLKOUT here, then enable INT0 as follows
	                                                 //0x82-32HZ
	
	RV8564ByteWrite(RV8564_WR_ADDR,RV8564_TC,0x81);   // Enable the timer here,0x81-64HZ
	RV8564ByteWrite(RV8564_WR_ADDR,RV8564_TIMER,0x80);// 0x81 - 2s
	
	TwinkleLedP27();
	
	INT0IC.BYTE = 0x04; //Enable, diasble RTC clk output interrupt INT0
	
	while(1){	
	  if(ACK_IS_SET){
	  ACK_IS_SET=0;
	  TwinkleLedP27(); 
	   } 		
	};	//Debugging pause...
	
/********** Backup code running before debugging pause ... ****************************************/
  
	
while(1);	//Debugging pause...
    
	RV8564ByteWrite(RV8564_WR_ADDR,RV8564_TC,0x81);
	RV8564ByteWrite(RV8564_WR_ADDR,RV8564_TIMER,0x80);// Enable the timer here 
    RV8564ByteWrite(RV8564_WR_ADDR,RV8564_CTR2,0x13);
	
	
	
    RV8564ByteWrite(RV8564_WR_ADDR,RV8564_CTR1,0x00);
    RV8564ByteWrite(RV8564_WR_ADDR,RV8564_CTR2,0x13);
		
    RV8564ByteWrite(RV8564_WR_ADDR,RV8564_CLKF,0x82);// Enable the CLKOUT here, then enable INT0 as follows
	                                                 //0x82-32HZ
	
	RV8564ByteWrite(RV8564_WR_ADDR,RV8564_TC,0x81);
	RV8564ByteWrite(RV8564_WR_ADDR,RV8564_TIMER,0x80);// Enable the timer here 
	
	
	
	
	/********** Debugging Code for I2C reading ... ****************************************/
	
    TwinkleLedP27();
	
    i2c_start();
	
	SendByte(RV8564_WR_ADDR);// Slave address of device WRrite
	SendByte(RV8564_CTR2);   // Word address of device
	
	i2c_start();
	
	SendByte(RV8564_RD_ADDR); // Slave address of device Read
	
	TwinkleLedP26();
		
	Msg4Trans=RcvByte();
    AckI2C_MstAck(FALSE);// Mater acknowledge 1 before SP condition
 
	i2c_stop();
	

	
	if(Msg4Trans==0x13){
		while(1){
		TwinkleLedP26();
		}
	
	}//end if
	
	
	/********** Debugging Code for I2C reading ... ****************************************/
	
	
	
	
	

    i2c_start(); 

    int i;
	for(i=0;i<18;i++)
	{
    SendByte(RV_8564ram[i]);
	idle();
	}
	
  	i2c_stop();
	
			
	
// 	U2SMR3.BYTE = 0x02;		// CKPH = 1, all others 0 for now 
//	while(S2RIC);
	
	
	
                // transmit data (enable bit)
//	

	
//  	Msg4Trans = RV_8564ram[0];  
//    U2TB.WORD = ( 0x0100 | Msg4Trans); // transmit data 
 
  
//      int i;
	for(i=0;i<18;i++)
	{
	U2C1.BIT.TE_U2C1 = 1;// TE set 1 to start
	
	while(!U2C1.BIT.TI_U2C1);   // wait for tx buffer to be empty
	
	S2RIC.BIT.IR = 0;          // reset reception interrupt bit
 	S2TIC.BIT.IR = 0;          // reset transmission interrupt bit
	
	while(!U2C0.BIT.TXEPT);	
	while(!U2C1.BIT.TI_U2C1);
		
    U2TB.BYTE.U2TBL= RV_8564ram[i];
	
	if(!U2C0.BIT.TXEPT) TwinkleLedP27();
	//while((!S2RIC.BIT.IR) && (!S2RIC.BIT.IR));	// wait until ack or nack
	
	//U2TB.WORD= ( 0x0100 | RV_8564ram[i]);
    //  U2TB.BYTE.U2TBL= RV_8564ram[i];
  while(!U2C0.BIT.TXEPT);//Transmit end, U2C0.BIT.TXEPT=1, !U2C0.BIT.TXEPT=0
	                       //if transmission not end, U2C0.BIT.TXEPT=0, !U2C0.BIT.TXEPT=1,loop to wait
  while(!U2C1.BIT.TI_U2C1);   // wait for tx buffer to be empty	
	
	U2C1.BIT.TE_U2C1 = 0;// TE set 1 to start
	
	}	//end transmission
	
	

	   
	   
	U2SMR2.BIT.CSC = 1; //Enable Clock synchronous mode.
  	idle();
	U2SMR2.BIT.SWC2= 1; //Set SCL output to low
 	idle();
 	U2SMR2.BIT.SWC2= 0; //SWC2 :1; /* SCL wait output bit 2   
	
	idle();
	
//     i2c_restart();
	
//	Msg4Trans=RV_8564ram[1];
// 	U2TB.WORD = ( 0x0100 | RV_8564ram[1]);
//    I2C_WAIT_UNTIL_FINISHED();	
	
	
  
	










	
//	for(NumofMsg=0;NumofMsg<18;NumofMsg++)
//	{	
//    U2TB.WORD= ( 0x0100 | RV_8564ram[NumofMsg]);
//	idle();
//    //  U2TB.BYTE.U2TBL= RV_8564ram[i];
//    I2C_WAIT_UNTIL_FINISHED();
//	}	//end transmission
	

 
    i2c_stop();
	
	Msg4Trans=0x00;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
    i2c_stop();


	
	Msg4Trans=0x00;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x13;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x01;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x01;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x01;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x01;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x01;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x01;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x07;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x80;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x80;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x80;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x80;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x83;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x81;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
	Msg4Trans=0x80;
 	U2TB.WORD = ( 0x0100 | Msg4Trans);
    I2C_WAIT_UNTIL_FINISHED();
	
//	Msg4Trans=0xa2;
//	U2TB.WORD = ( 0x0100 | Msg4Trans);
//  I2C_WAIT_UNTIL_FINISHED();
	
/* ****************  read the value stored in the specified address   ********************************************/ 			
	i2c_start();
	
	
	  
	U2TB.WORD= ( 0x0100 | 0xa2);	
    I2C_WAIT_UNTIL_FINISHED();
	
	U2TB.WORD= ( 0x0100 | RV8564_CLKF);	
    I2C_WAIT_UNTIL_FINISHED();
	
	i2c_start();
	
	U2TB.WORD= ( 0x0100 | 0xa3);	
    I2C_WAIT_UNTIL_FINISHED();
	
	Readback_8564ram[0] = U2RB.BYTE.U2RBL;
	
	i2c_stop();
//***** at this point, the master becomes a Receiver, the slave becomes the transmitter****************************/ 	

while(!U2C1.BIT.RI_U2C1);//If no data received and stored in U2RB register, infinite loop
if(Readback_8564ram[0]==0x0F){	
    while(1){
//	TwinkleLedP26();
	};	
}	
else {
	  while(1){TwinkleLedP27();}
     }

//	U2TB.WORD= ( 0x0000 | 0xa2);  // LSB '1' of high byte will release the bus for slave
//  U2RB.BYTE.U2RBL = 0x00;						                    	// Arbitration lost flag clear
//	U2TB.BYTE.U2TBL= 0x02;
	
	
/* Maybe the failure is because the chip can not be written beyound the span out of 1sec */
	i2c_start();
//	int i=0;
	for(i=0;i<18;i++)
	{	
    U2TB.WORD= ( 0x0100 | RV_8564ram[i]);
    //  U2TB.BYTE.U2TBL= RV_8564ram[i];
    I2C_WAIT_UNTIL_FINISHED();
	}	//end transmission
    i2c_stop();
/******** The above is a segment for continuously writing the RAM of RV8564 **************/

	
/***********This segmetn is used for writing the RAM byte by byte ***************************************/	
	WriteOneByteRV8564(0x00,0x00);//See: void WriteOneByteRV8564(int SpecAddress,int DataWrite)
	WriteOneByteRV8564(0x01,0x13);//See: void WriteOneByteRV8564(int SpecAddress,int DataWrite)		
	WriteOneByteRV8564(0x0E,0x81);//See: void WriteOneByteRV8564(int SpecAddress,int DataWrite)	

⌨️ 快捷键说明

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