-
-
Save kpykc/69d5e63d55d792655250 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* MT9P031 image sensor simulator | |
*/ | |
#include <Wire.h> | |
#define I2C_SLAVE_ADDRESS 0x5D | |
#define BUFFER_SIZE 8 | |
#define NUM_REGS 256 | |
unsigned short regs[NUM_REGS]; | |
unsigned char i2c_buffer[BUFFER_SIZE]; | |
unsigned char reg; | |
void load_default_reg_vals() | |
{ | |
// only initializing the non-zero 'named' registers | |
regs[0] = 0x1801; | |
regs[1] = 0x0036; | |
regs[2] = 0x0010; | |
regs[3] = 0x0797; | |
regs[4] = 0x0A1F; | |
regs[6] = 0x0019; | |
regs[7] = 0x1F82; | |
regs[9] = 0x0797; | |
regs[16] = 0x0050; | |
regs[17] = 0x6404; | |
regs[30] = 0x4006; | |
regs[32] = 0x0040; | |
regs[43] = 0x0008; | |
regs[44] = 0x0008; | |
regs[45] = 0x0008; | |
regs[46] = 0x0008; | |
regs[53] = 0x0008; | |
regs[255] = 0x1801; | |
} | |
/* | |
This gets called when the master does a write. | |
*/ | |
void i2c_slave_write_handler(int numBytes) | |
{ | |
/* eat all the bytes, just for demo, we never look at any more then three */ | |
for (int i = 0; i < numBytes && i < BUFFER_SIZE; i++) { | |
i2c_buffer[i] = Wire.receive(); | |
} | |
reg = i2c_buffer[0]; | |
if (numBytes > 2) { | |
regs[reg] = (i2c_buffer[1] << 8) | i2c_buffer[2]; | |
} | |
} | |
/* | |
This gets called when the master does a read. | |
We are simulating a one byte write command followed | |
by a two byte response from slave behavior. | |
*/ | |
void i2c_slave_read_handler() | |
{ | |
i2c_buffer[0] = (regs[reg] >> 8) & 0xff; | |
i2c_buffer[1] = regs[reg] & 0xff; | |
Wire.send(i2c_buffer, 2); | |
} | |
void begin_i2c() | |
{ | |
Wire.begin(I2C_SLAVE_ADDRESS); | |
Wire.onReceive(i2c_slave_write_handler); | |
Wire.onRequest(i2c_slave_read_handler); | |
} | |
void setup() | |
{ | |
load_default_reg_vals(); | |
begin_i2c(); | |
} | |
void loop() | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment