Skip to content

Instantly share code, notes, and snippets.

@asi-msk
Created April 21, 2022 09:59
Show Gist options
  • Save asi-msk/73bf4283d574d860fdcf3dafe5537bb5 to your computer and use it in GitHub Desktop.
Save asi-msk/73bf4283d574d860fdcf3dafe5537bb5 to your computer and use it in GitHub Desktop.
bool reserved_addr(uint8_t addr) {
return (addr & 0x78) == 0 || (addr & 0x78) == 0x78;
}
void scani2c(void) {
printf("\nI2C Bus Scan\n");
printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
for (int addr = 0; addr < (1 << 7); ++addr) {
if (addr % 16 == 0) {
printf("%02x ", addr);
}
// Perform a 1-byte dummy read from the probe address. If a slave
// acknowledges this address, the function returns the number of bytes
// transferred. If the address byte is ignored, the function returns
// -1.
// Skip over any reserved addresses.
int ret;
uint8_t rxdata;
if (reserved_addr(addr))
ret = PICO_ERROR_GENERIC;
else
ret = i2c_read_blocking(i2c_default, addr, &rxdata, 1, false);
printf(ret < 0 ? "." : "@");
printf(addr % 16 == 15 ? "\n" : " ");
}
printf("Done.\n");
}
void write_GP2Y0(void) {
//# 変更後の デバイスアドレス -> SET_DATAに設定すべき値(上位4bitが書き込む値)
//# 0x40 -> 0x08
//# 0x50 -> 0x0A
//# 0x60 -> 0x0C
//# 0x70 -> 0x0E
#define SET_DATA 0x0c
int GP2Y0 = 0x40; //変更前の デバイスアドレス
int GP2Y_VPP = 2; // Vppを接続
const bool LOW = 0;
const bool HIGH = 1;
gpio_init(GP2Y_VPP);
gpio_set_dir(GP2Y_VPP, GPIO_OUT);
gpio_put(GP2Y_VPP, LOW);
//<Stage 1> Clock Select(0xEC:R/W)
const uint8_t s1[] = {0xEC, 0xFF};
i2c_write_blocking(i2c_default, GP2Y0, s1, 2, false);
sleep_ms(10);
gpio_put(GP2Y_VPP, HIGH);
sleep_ms(10);
//<Stage 2> E-Fuse(0xC8:R/W)
const uint8_t s2[] = {0xC8, 0x00};
i2c_write_blocking(i2c_default, GP2Y0, s2, 2, false);
sleep_ms(10);
//<Stage 3> E-Fuse Bit No/ Bank Assign(0xC9:R/W)
const uint8_t s3[] = {0xC9, 0x45};
i2c_write_blocking(i2c_default, GP2Y0, s3, 2, false);
sleep_ms(10);
//<Stage 4> E-Fuse Program Data(0xCD:R/W)
const uint8_t s4[] = {0xCD, SET_DATA};
i2c_write_blocking(i2c_default, GP2Y0, s4, 2, false);
sleep_ms(10);
//<Stage 5> E-Fuse Program Enable Bit(0xCA:R/W)
// 0x00=Disable, 0x01=Enable
const uint8_t s5[] = {0xCA, 0x01};
i2c_write_blocking(i2c_default, GP2Y0, s5, 2, false);
sleep_ms(10);
//<Stage 6> E-Fuse Program Enable Bit(0xCA:R/W)
const uint8_t s6[] = {0xCA, 0x00};
i2c_write_blocking(i2c_default, GP2Y0, s6, 2, false);
sleep_ms(10);
gpio_put(GP2Y_VPP, LOW);
sleep_ms(10);
//<Stage 7.1> Bank Select(0xEF:R/W)
const uint8_t s7_1[] = {0xEF, 0x00};
i2c_write_blocking(i2c_default, GP2Y0, s7_1, 2, false);
sleep_ms(10);
//<Stage 7.2> E-Fuse(0xC8:R/W) bank3のregisterにload
const uint8_t s7_2[] = {0xC8, 0x40};
i2c_write_blocking(i2c_default, GP2Y0, s7_2, 2, false);
sleep_ms(10);
//<Stage 7.3> E-Fuse(0xC8:R/W)
const uint8_t s7_3[] = {0xC8, 0x00};
i2c_write_blocking(i2c_default, GP2Y0, s7_3, 2, false);
sleep_ms(10);
//<Stage 8> Software Reset(0xEE:W) 0x06=software reset
const uint8_t s8[] = {0xEE, 0x06};
i2c_write_blocking(i2c_default, GP2Y0, s8, 2, false);
sleep_ms(10);
/*
//<Stage 9.1> Clock Select(0xEC:R/W)
const uint8_t s9_1[] = {0xEC, 0xFF};
i2c_write_blocking(i2c_default, GP2Y0, s9_1, 2, false);
sleep_ms(10);
//<Stage 9.2> Bank Select(0xEF:R/W)
const uint8_t s9_2[] = {0xEF, 0x03};
i2c_write_blocking(i2c_default, GP2Y0, s9_2, 2, false);
sleep_ms(10);
//<Stage 9.3> 設定したデバイスアドレスの確認
Wire.beginTransmission(GP2Y0);
Wire.write(0x27);
Wire.endTransmission();
Wire.requestFrom(GP2Y0, 1);
int readOutData = 0;
Serial.print("read");
sleep_ms(100);
readOutData = Wire.read();
Serial.println(" done");
sleep_ms(10);
//<Stage 9.4> Bank Select(0xEF:R/W)
Wire.beginTransmission(GP2Y0);
Wire.write(0xEF);
Wire.write(0x00);
Wire.endTransmission();
sleep_ms(10);
//<Stage 9.5> Clock Select(0xEC:R/W)
Wire.beginTransmission(GP2Y0);
Wire.write(0xEC);
Wire.write(0x7F);
Wire.endTransmission();
sleep_ms(10);
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment