Hi All,
I am new here!
Long story short:
I needed I2C slave support in the libmaple core. I saw some half working and half implemented attempts scattered around, so I gave up and extended libmaple.
So: I have a solid working I2C code implementation.
Features:
*Allows STM to run as a slave AND as a master. Multi-master tested OK (apart from the usual multi-master issues)
*Allows each I2C module to run as a master and a slave
*Each I2C module can accept dual i2C bus addresses
*Supports General call write only addressing (on address 0x00)
*Has configurable callbacks for each i2C module
*Can callback on each incoming/outgoing byte
*supports buffered IO. Single RX callback and TX callback for each request
*Requests use the same dev_msg machanism
I have extended a few chunks of code, but kept to the spirit of the current (excellent) design. notably each feature now has a new flag that is passed to i2c_master_init. Under the circumstances, I don't think i2c_master_init is a true representation of what it now does, but creating a slave_init function just added bloat.
Resources:
Diff file http://pastebin.com/k0vTt5rT
Example:
setup()
{
msgs.data = buf;
i2c_master_enable(I2C1, I2C_FAST_MODE | I2C_SLAVE_DUAL_ADDRESS | I2C_SLAVE_GENERAL_CALL | I2C_SLAVE_USE_RX_BUFFER | I2C_SLAVE_USE_TX_BUFFER);
i2c_slave_attach_recv_handler(I2C1, &msgs, funcrx);
i2c_slave_attach_transmit_handler(I2C1, &msgs, functx);
i2c_slave_set_own_address(I2C1, 0x0010);
i2c_slave_set_own_address2(I2C1, 0x0020);
}
void funcrx(i2c_msg *msg)
{
someval = msg->data[0];
}
void functx(i2c_msg *msg)
{
msg->data[0] = 0x01;
msg->data[1] = 0x02;
msg->data[2] = 0x03;
msg->data[3] = 0x04;
msg->data[4] = 0x05;
msg->length = 5;
}
i2c_msg msgs;
uint8 buf[255];
Questions?
Ginge