i2c_first 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /***************************************************************************
  2. Author : ADI - Apps www.analog.com/MicroConverter
  3. Date : May. 2007
  4. File : I2C_Slave.c
  5. Hardware : Applicable to ADuC702x rev H or I silicon
  6. Currently targetting ADuC7028.
  7. Description : I2C Slave to demonstrate with I2C_Master.
  8. Operates in two modes, read & write (called recieve and
  9. transmit here). At the begining of an I2C transmission, the
  10. Master sends an address. The LSB of this address determines
  11. if the Master is to read (1) or write (0).
  12. ***************************************************************************/
  13. #include<ADuC7028.h>
  14. void My_IRQ_Handler(void);
  15. typedef unsigned char BYTE; //8-bit
  16. typedef unsigned short int HALFWORD; //16-bit
  17. typedef unsigned int WORD; //32-bit
  18. #define TRUE 1
  19. #define FALSE 0
  20. #define HIGH 1
  21. #define LOW 0
  22. short i = 0, dat[256], buf = 0;
  23. int r = 0;
  24. int w = 0;
  25. BYTE i2c_first = TRUE;
  26. int main()
  27. {
  28. dat[0] = 0;
  29. dat[1] = 10;
  30. dat[2] = 20;
  31. dat[3] = 30;
  32. dat[4] = 40;
  33. dat[5] = 50;
  34. dat[6] = 60;
  35. dat[7] = 70;
  36. i=1;
  37. // configures GPIO to flash LED P4.2
  38. GP4DAT = 0x04000000; // P4.2 configured as an output. LED is turned on
  39. //GP0CON = 0x00000000; // setting IO
  40. //GP0DAT = 0x00000000; // group P0.x as input
  41. // I2C on P1.0 and P1.1
  42. GP1CON = 0x22;
  43. IRQ=My_IRQ_Handler;
  44. IRQEN = 0x200; // I2C0 Slave Interupt
  45. I2C0CFG = 0x4001;//0x01; // Slave Enable
  46. I2C0ID0 = 0xA0; // Slave ID
  47. I2C0STX = 0x77;
  48. while (1)
  49. {
  50. };
  51. // return 0;
  52. }
  53. /*short V = -1;
  54. DAC1CON = 0x12; // AGND-ARef range 0x12 2.5V
  55. DAC1DAT = DATtoADC(V); // output voltage on DAC0
  56. //GP4DAT ^= 0x00040000; // Complement P4.2
  57. while (1){
  58. V = -V;
  59. if(V>4096)
  60. V = 0;
  61. DAC1DAT = DATtoADC(V); // output voltage on DAC0
  62. if (Read_Digital(0)==1)
  63. GP4DAT ^= 0x00040000; // Complement P4.2
  64. */
  65. /*************************************************/
  66. /*************************************************/
  67. /************ IRQ Service Routine *************/
  68. /*************************************************/
  69. /*************************************************/
  70. // 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.
  71. unsigned int status;
  72. unsigned int x;
  73. unsigned int addr;
  74. unsigned int first=0;
  75. unsigned step = 0;
  76. void My_IRQ_Handler()
  77. {
  78. status = I2C0SSTA;
  79. if (((status & 0x0400)==0x0400)) // Stsop test
  80. {
  81. step = 0;
  82. }
  83. // Slave Recieve
  84. if ((status & 0x08)==0x08) // Slave Recieve IRQ
  85. {
  86. //GP4DAT ^= 0x00040000;
  87. switch (step){
  88. case 0:
  89. addr = I2C0SRX;
  90. I2C0FSTA = I2C0FSTA|0x100;
  91. I2C0STX = dat[addr];
  92. break;
  93. case 1:
  94. dat[addr] = I2C0SRX;
  95. break;
  96. case 2:
  97. dat[addr] += I2C0SRX<<8;
  98. break;
  99. }
  100. step++;
  101. }
  102. // Slave Transmit
  103. if ((status & 0x04)==0x04) // Slave Transmit IRQ
  104. {
  105. I2C0STX = dat[addr]>>8;
  106. step = 0;
  107. }
  108. }