123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- /***************************************************************************
- Author : ADI - Apps www.analog.com/MicroConverter
- Date : May. 2007
- File : I2C_Slave.c
- Hardware : Applicable to ADuC702x rev H or I silicon
- Currently targetting ADuC7028.
- Description : I2C Slave to demonstrate with I2C_Master.
-
- Operates in two modes, read & write (called recieve and
- transmit here). At the begining of an I2C transmission, the
- Master sends an address. The LSB of this address determines
- if the Master is to read (1) or write (0).
-
- ***************************************************************************/
- #include<ADuC7028.h>
- void My_IRQ_Handler(void);
- typedef unsigned char BYTE; //8-bit
- typedef unsigned short int HALFWORD; //16-bit
- typedef unsigned int WORD; //32-bit
- #define TRUE 1
- #define FALSE 0
- #define HIGH 1
- #define LOW 0
- short i = 0, dat[256], buf = 0;
- int r = 0;
- int w = 0;
- BYTE i2c_first = TRUE;
- int main()
- {
- dat[0] = 0;
- dat[1] = 10;
- dat[2] = 20;
- dat[3] = 30;
- dat[4] = 40;
- dat[5] = 50;
- dat[6] = 60;
- dat[7] = 70;
- i=1;
- // configures GPIO to flash LED P4.2
- GP4DAT = 0x04000000; // P4.2 configured as an output. LED is turned on
- //GP0CON = 0x00000000; // setting IO
- //GP0DAT = 0x00000000; // group P0.x as input
-
- // I2C on P1.0 and P1.1
- GP1CON = 0x22;
- IRQ=My_IRQ_Handler;
- IRQEN = 0x200; // I2C0 Slave Interupt
-
- I2C0CFG = 0x4001;//0x01; // Slave Enable
- I2C0ID0 = 0xA0; // Slave ID
- I2C0STX = 0x77;
-
-
- while (1)
- {
- };
- // return 0;
- }
- /*short V = -1;
-
- DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
-
- DAC1DAT = DATtoADC(V); // output voltage on DAC0
- //GP4DAT ^= 0x00040000; // Complement P4.2
- while (1){
- V = -V;
- if(V>4096)
- V = 0;
- DAC1DAT = DATtoADC(V); // output voltage on DAC0
-
- if (Read_Digital(0)==1)
- GP4DAT ^= 0x00040000; // Complement P4.2
- */
- /*************************************************/
- /*************************************************/
- /************ IRQ Service Routine *************/
- /*************************************************/
- /*************************************************/
- // If you are using SMB library, try changing the N to a Y in the file /sys/module/i2c_bcm2708/parameters/combined. This worked for me.
- unsigned int status;
- unsigned int x;
- unsigned int addr;
- unsigned int first=0;
- unsigned step = 0;
- void My_IRQ_Handler()
- {
-
- status = I2C0SSTA;
-
- if (((status & 0x0400)==0x0400)) // Stsop test
- {
- step = 0;
- }
- // Slave Recieve
- if ((status & 0x08)==0x08) // Slave Recieve IRQ
- {
- //GP4DAT ^= 0x00040000;
- switch (step){
- case 0:
- addr = I2C0SRX;
- I2C0FSTA = I2C0FSTA|0x100;
- I2C0STX = dat[addr];
- break;
- case 1:
- dat[addr] = I2C0SRX;
- break;
- case 2:
- dat[addr] += I2C0SRX<<8;
- break;
- }
- step++;
-
- }
-
- // Slave Transmit
- if ((status & 0x04)==0x04) // Slave Transmit IRQ
- {
- I2C0STX = dat[addr]>>8;
- step = 0;
- }
-
- }
|