Skip to content

Instantly share code, notes, and snippets.

@CreateRemoteThread
Created June 25, 2019 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CreateRemoteThread/c80be132d42a8194b4d3c1bed41f67ee to your computer and use it in GitHub Desktop.
Save CreateRemoteThread/c80be132d42a8194b4d3c1bed41f67ee to your computer and use it in GitHub Desktop.
__sfr __at(0xff) POWEROFF;
__sfr __at(0xfe) DEBUG;
__sfr __at(0xfd) CHAROUT;
__xdata __at(0xff00) unsigned char FLAG[0x100];
__sfr __at(0xfa) RAW_I2C_SCL;
__sfr __at(0xfb) RAW_I2C_SDA;
// I2C-M module/chip control data structure.
__xdata __at(0xfe00) unsigned char I2C_ADDR; // 8-bit version.
__xdata __at(0xfe01) unsigned char I2C_LENGTH; // At most 8 (excluding addr).
__xdata __at(0xfe02) unsigned char I2C_RW_MASK; // 1 R, 0 W.
__xdata __at(0xfe03) unsigned char I2C_ERROR_CODE; // 0 - no errors.
__xdata __at(0xfe08) unsigned char I2C_DATA[8]; // Don't repeat addr.
__sfr __at(0xfc) I2C_STATE; // Read: 0 - idle, 1 - busy; Write: 1 - start
const unsigned char SEEPROM_I2C_ADDR_MEMORY = 0b10100000;
const unsigned char SEEPROM_I2C_ADDR_SECURE = 0b01010000;
void print(const char *str) {
while (*str) {
CHAROUT = *str++;
}
}
void seeprom_wait_until_idle() {
while (I2C_STATE != 0) {}
}
unsigned char seeprom_read_byte(unsigned char addr) {
seeprom_wait_until_idle();
I2C_ADDR = SEEPROM_I2C_ADDR_MEMORY;
I2C_LENGTH = 2;
I2C_ERROR_CODE = 0;
I2C_DATA[0] = addr;
I2C_RW_MASK = 0b10; // Write Byte, then Read Byte
I2C_STATE = 1;
seeprom_wait_until_idle();
if (I2C_ERROR_CODE != 0) {
return 0;
}
return I2C_DATA[1];
}
int nop()
{
int i = 0;
int lol = 0;
for(i = 0;i < 2;i++){lol++;}
return lol;
}
void start(void)
{
RAW_I2C_SDA = 1;
RAW_I2C_SCL = 1;
nop();
RAW_I2C_SDA = 0;
nop();
RAW_I2C_SCL = 0;
nop();
}
void stop(void)
{
RAW_I2C_SDA = 0;
nop();
RAW_I2C_SCL = 1;
nop();
RAW_I2C_SDA = 1;
}
void i2c_write(unsigned char data)
{
unsigned char data_ = data;
int i = 0;
for(i = 0;i < 8;i++)
{
RAW_I2C_SDA = (data_ & 0x80) ? 1:0;
RAW_I2C_SCL = 1; RAW_I2C_SCL = 0;
data_ <<= 1;
}
RAW_I2C_SCL = 1;
nop();
RAW_I2C_SCL = 0;
}
unsigned char i2c_read()
{
unsigned char data = 0;
int i = 0;
RAW_I2C_SDA = 1;
for(i = 0;i < 8;i++)
{
RAW_I2C_SCL = 1;
if(RAW_I2C_SDA == 1)
{
print("1");
}
else if(RAW_I2C_SDA == 0)
{
print("0");
}
else
{
print("?");
}
nop();
data <<= 1;
data = (data | RAW_I2C_SDA);
RAW_I2C_SCL = 0;
nop();
}
// RAW_I2C_SDA = 0; // - ack : we're done reading.
RAW_I2C_SDA = 1; // - nack: keep going.
nop();
RAW_I2C_SCL = 1;
nop();
RAW_I2C_SCL = 0;
nop();
return data;
}
void main(void) {
print("I2C_BASKETCASE_3\n");
start();
i2c_write(0b10100000); // write...
i2c_write(0x5);
i2c_write('b');
stop();
unsigned char t[32];
t[0] = seeprom_read_byte(0x5);
print(t);
print("\n");
print("test bitbang read...\n");
int i = 0;
start();
i2c_write(0b10100000); // read
i2c_write(63); // read?;
start();
print("Locking all pages...\n");
i2c_write(0b01011111); // read?;
// stop();
print("Reading...\n");
// start();
t[31] = '\x00';
for(i = 0;i < 31;i++)
{
start();
i2c_write(0b10100001); // read?;
t[i] = i2c_read();
if(t[i] == 0)
{
t[i] = ' ';
}
}
print(t);
stop();
/*
stop();
start();
i2c_write(0b10100001); // read?;
i2c_write(0x5);
for(i = 0;i < 32;i++)
{
print("+");
t[0] = i2c_read();
print(t);
}
*/
// stop();
stop();
POWEROFF = 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment