// Reads the 3 magnetometer channels and stores them in vector m void readMag(LSM303 *compass) { alt_u16 xhm, xlm, yhm, ylm, zhm, zlm; alt_u8 szBuf[5]; //need 6 values, magnetometer only refreshes data once these 6 values have been read int Num, i; if (I2C_MultipleRead(COMPASS_I2C_SCL_BASE, COMPASS_I2C_SDA_BASE, MAG_ADDRESS, LSM303DLHC_OUT_X_H_M , szBuf, sizeof(szBuf))) { Num = sizeof(szBuf)/sizeof(szBuf[0]); for(i=0;i<Num;i++){ printf("Addr[%d] = %02xh\r\n", i, szBuf[i]); } xhm = szBuf[0]; xlm = szBuf[1]; zhm = szBuf[2]; zlm = szBuf[3]; yhm = szBuf[4]; ylm = szBuf[5]; } else{ printf("Failed to read magnetometer\r\n"); } // combine high and low bytes compass->m.x = (alt_u16)(xhm << 8 | xlm); compass->m.y = (alt_u16)(yhm << 8 | ylm); compass->m.z = (alt_u16)(zhm << 8 | zlm); }
void DEMO_EEPROM(void) { alt_u8 szBuf[16]; int i,Num; const alt_u8 DeviceAddr = 0xA0; const alt_u8 ControlAddr = 00; // set clock as output //IOWR_ALTERA_AVALON_PIO_DIRECTION(I2C_SCL_BASE, ALTERA_AVALON_PIO_DIRECTION_OUTPUT); IOWR(SELECT_I2C_CLK_BASE, 0, 0x01); if (I2C_MultipleRead(I2C_SCL_BASE, I2C_SDA_BASE, DeviceAddr, ControlAddr, szBuf, sizeof(szBuf))) { Num = sizeof(szBuf)/sizeof(szBuf[0]); for(i=0; i<Num; i++) { printf("Addr[%02d] = %02xh\r\n", i, szBuf[i]); } } else { printf("Failed to access EEPROM\r\n"); } }
bool DDR2_I2C_Read(void){ const alt_u8 DeviceAddr = 0xA0; // 1010-000x bool bSuccess; int i; #if 1 alt_u8 szData[256]; bSuccess = I2C_MultipleRead(DDR2_I2C_SCL_BASE, DDR2_I2C_SDA_BASE, DeviceAddr, szData, sizeof(szData)); if (bSuccess){ for(i=0;i<256 && bSuccess;i++){ printf("EEPROM[%03d]=%02Xh ", i, szData[i]); // if (i == 0) printf("(Number of SPD Bytes Used)\n"); else if (i == 1) printf("(Total Number of Bytes in SPD Device, Log2(N))\n"); else if (i == 2) printf("(Basic Memory Type[08h:DDR2])\n"); else if (i == 3) printf("(Number of Row Addresses on Assembly)\n"); else if (i == 4) printf("(Number of Column Addresses on Assembly)\n"); else if (i == 5) printf("(DIMM Height and Module Rank Number[b2b1b0+1])\n"); else if (i == 6) printf("(Module Data Width)\n"); else if (i == 7) printf("(Module Data Width, Continued)\n"); else if (i == 16) printf("(Burst Lengths Supported[bitmap: x x x x 8 4 x x])\n"); else if (i == 13) printf("(Primary SDRAM width)\n"); else if (i == 14) printf("(ECC SDRAM width)\n"); else if (i == 17) printf("(Banks per SDRAM device)\n"); else if (i == 18) printf("(CAS lantencies supported[bitmap: x x 5 4 3 2 x x])\n"); else if (i == 20) printf("(DIMM Type: x x Mini-UDIMM Mini-RDIMM Micro-DIMM SO-DIMM UDIMMM RDIMM)\n"); else if (i == 22) printf("(Memory Chip feature bitmap)\n"); else if (i == 27) printf("(Minimun row precharge time[tRP;nsx4])\n"); else if (i == 28) printf("(Minimun row active-row activce delay[tRRD;nsx4])\n"); else if (i == 29) printf("(Minimun RAS to CAS delay[tRCD;nsx4])\n"); else if (i == 30) printf("(Minimun acive to precharge time[tRAS;ns])\n"); else if (i == 31) printf("(Size of each rank[bitmap:512MB,256MB,128MB,16GB,8GB,4GB,2GB,1GB)\n"); else if (i == 36) printf("(Minimun write receovery time[tWR;nsx4])\n"); else if (i == 37) printf("(Internal write to read command delay[tWTR;nsx4])\n"); else if (i == 38) printf("(Internal read to precharge command delay[tRTP;nsx4])\n"); else if (i == 41) printf("(Minimun activce to active/refresh time[tRC;ns])\n"); else if (i == 42) printf("(Minimun refresh to active/refresh time[tRFC;ns])\n"); else if (i == 62) printf("(SPD Revision)\n"); else if (i == 63) printf("(Checksum)\n"); else if (i == 64) printf("(64~71: Manufacturer JEDEC ID)\n"); else if (i == 72) printf("(Module manufacturing location[Vendor-specific code])\n"); else if (i == 73) printf("(73~90: Moduloe part number)\n"); else if (i == 91) printf("(91~92: Moduloe revision code)\n"); else if (i == 93) printf("(Manufacture Years since 2000[0-255])\n"); else if (i == 94) printf("(Manufacture Weeks[1-52])\n"); else if (i == 95) printf("(95~98[4-bytes]: Module serial number)\n"); else if (i == 99) printf("(99~128: Manufacturer-specific data)\n"); else printf("\n"); } }else{ printf("Failed to read EEPROM\n"); } #else alt_u8 ControlAddr, Value; printf("DDR2 EEPROM Dump\n"); bSuccess = TRUE; usleep(20*1000); for(i=0;i<256 && bSuccess;i++){ ControlAddr = i; bSuccess = I2C_Read(DDR2_I2C_SCL_BASE, DDR2_I2C_SDA_BASE, DeviceAddr, ControlAddr, &Value); if (bSuccess){ printf("EEPROM[%03d]=%02Xh\n", ControlAddr, Value); }else{ printf("Failed to read EEPROM\n"); } } #endif // test write if (bSuccess){ alt_u8 WriteData = 0x12, TestAddr = 128; alt_u8 ReadData; usleep(20*1000); bSuccess = I2C_Write(DDR2_I2C_SCL_BASE, DDR2_I2C_SDA_BASE, DeviceAddr, TestAddr, WriteData); if (!bSuccess){ printf("Failed to write EEPROM\n"); }else{ bSuccess = I2C_Read(DDR2_I2C_SCL_BASE, DDR2_I2C_SDA_BASE, DeviceAddr, TestAddr, &ReadData); if (!bSuccess){ printf("Failed to read EEPROM for verify\n"); }else{ if (ReadData != WriteData){ bSuccess = FALSE; printf("Verify EEPROM write fail, ReadData=%02Xh, WriteData=%02Xh\n", ReadData, WriteData); } } } if (bSuccess) printf("Success to write EEPROM\n"); else printf("Failed to write EEPROM\n"); } return bSuccess; }