#include #include "cpu.h" #include "mcf5xxx.h" #include "m52xxevb.h" #include "i2c.h" #include "debug.h" //#define I2C_DEBUG static unsigned int I2C_DIV_TABLE[]={ 20, 22, 24, 26, 28, 28, 30, 32, 34, 36, 40, 40, 44, 48, 48, 56, 56, 64, 68, 72, 80, 80, 88, 96, 104, 112, 128, 128, 144, 160, 160, 192, 192, 224, 240, 256, 288, 320, 320, 384, 384, 448, 480, 512, 576, 640, 640, 768, 768, 896, 960,1024,1152,1280,1280,1536,1536,1792,1920,2046, 2304,2560,3072,3840 }; static uint8 I2C_IC_TABLE[]={ 32,33,34,35,36, 0, 1,37, 2,38, 3,39, 4,40, 5, 6,41,42, 7,43, 8,44, 9,45,10, 46,11,47,12,13,48,49,14,50,15,51,16,17,52,18,53,54,19,55,20,56,21,57,22,58, 23,59,24,60,25,61,26,62,27,63,28,29,30,31 }; //#define SYSTEM_CLOCK 75 #define I2C_CLOCK 100 //Clock of i2c bus in kHz #define I2C_BusMaxWait 1000 //How long are we willing to wait for I2C bus to become free int I2C_isInit=0; /** * Selects the closest DIV to match frequence given by freqKhz. * Refer to I2C chapter "I2C Frequency Divider Register (I2FDR)" **/ uint8 I2C_divSelect(int freqKhz){ int approxDiv=(SYSTEM_CLOCK*1000000)/(freqKhz*1000); int i; for(i=0; i<64 && I2C_DIV_TABLE[i]1){ MCF_I2C_I2CR &= ~MCF_I2C_I2CR_TXAK; } //First byte is all 0 and is garbage i=(int) MCF_I2C_I2DR; for(i=0; i < len; i++){ if(!I2C_wait4Slave()){ DBG_PRINTL("I2C_read: timeout\n"); I2C_stop(); return 0; } if(i==len-2){ //Disable ack on second to last send MCF_I2C_I2CR |= MCF_I2C_I2CR_TXAK; }else if(i==len-1){ //Stop signal on last byte I2C_stop(); } //Save the byte *buf++ = MCF_I2C_I2DR; //DBG_PRINTF("I2C_read: %02x\n", *buf); //buf++; } return len; }