#endif
}
-static int twi_slave_event(struct twi_slave_t* p, uint8_t address, enum twi_event event)
-{
- switch (event) {
- case TWI_START:
- break;
- case TWI_STOP:
- break;
- case TWI_PROBE:
- break;
- }
- return 0;
-}
// handle a data write, after a (re)start
static int twi_slave_write(struct twi_slave_t* p, uint8_t v)
return 0;
}
-static twi_slave_t slave_driver = {
- .event = twi_slave_event,
- .write = twi_slave_write,
- .read = twi_slave_read
-};
static int avr_twi_ioctl(struct avr_io_t * port, uint32_t ctl, void * io_param)
{
p->io = _io;
avr_register_io(avr, &p->io);
avr_register_vector(avr, &p->twi);
- p->slave = slave_driver; // get default callbacks
+// p->slave = slave_driver; // get default callbacks
twi_slave_init(&p->slave, 0, p);
twi_bus_init(&p->bus);
#include <stdlib.h>
#include <string.h>
+#include <stdio.h>
#include "sim_twi.h"
static void twi_bus_master_irq_notify(struct avr_irq_t * irq, uint32_t value, void * param)
{
+ twi_bus_t * bus = (twi_bus_t *)param;
switch (irq->irq) {
case TWI_MASTER_STOP:
bus->peer = NULL;
bus->ack = 0;
break;
case TWI_MASTER_ACK:
- if (!peer) {
+ if (!bus->peer) {
}
break;
}
}
}
-static void twi_slave_master_irq_notify(struct avr_irq_t * irq, uint32_t value, void * param)
+static void twi_slave_irq_notify(struct avr_irq_t * irq, uint32_t value, void * param)
{
twi_slave_t * slave = (twi_slave_t*)param;
switch (irq->irq) {
slave->match = 0;
break;
case TWI_MASTER_START:
- if ((value & 0xfe) == slave->address & 0xfe) {
+ if ((value & 0xfe) == (slave->address & 0xfe)) {
if (slave->match) {
// restart
}
void twi_bus_init(twi_bus_t * bus)
{
memset(bus, 0, sizeof(twi_bus_t));
- avr_init_irq(bus->irq, 0, TWI_STATE_COUNT);
+ avr_init_irq(bus->irq, 0, TWI_MASTER_STATE_COUNT);
for (int i = 0; i < TWI_MASTER_STATE_COUNT; i++)
avr_irq_register_notify(bus->irq + i, twi_bus_master_irq_notify, bus);
}
avr_raise_irq(bus->irq + TWI_MASTER_STOP, 0);
}
-int twi_bus_write(twi_bus_t * bus, uint8_t data)
-{
- if (!bus->peer || !bus->peer->write)
- return 0;
-
- int res = bus->peer->write(bus->peer, data);
- if (bus->peer)
- bus->peer->byte_index++;
- return res;
-}
-
-uint8_t twi_bus_read(twi_bus_t * bus)
-{
- if (!bus->peer || !bus->peer->read)
- return 0;
-
- uint8_t res = bus->peer->read(bus->peer);
- if (bus->peer)
- bus->peer->byte_index++;
- return res;
-}
void twi_slave_init(twi_slave_t * slave, uint8_t address, void * param)
{
memset(slave, 0, sizeof(twi_slave_t));
slave->address = address;
- slave->param = param;
-
+// slave->param = param;
}
void twi_slave_detach(twi_slave_t * slave)
}
}
-int twi_slave_match(twi_slave_t * slave, uint8_t address)
-{
- if (slave->address)
- return (address & ~1) == (slave->address & ~1);
- return slave->event(slave, address, TWI_PROBE);
-}
-