hw/i2c/bitbang_i2c: Change state calling bitbang_i2c_set_state() helper

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Acked-by: Corey Minyard <cminyard@mvista.com>
Message-id: 20230111085016.44551-4-philmd@linaro.org
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
Philippe Mathieu-Daudé 2023-01-11 09:50:14 +01:00 committed by Peter Maydell
parent 2b9339d3b4
commit dc575b5e03

View File

@ -26,13 +26,19 @@ do { printf("bitbang_i2c: " fmt , ## __VA_ARGS__); } while (0)
#define DPRINTF(fmt, ...) do {} while(0) #define DPRINTF(fmt, ...) do {} while(0)
#endif #endif
static void bitbang_i2c_set_state(bitbang_i2c_interface *i2c,
bitbang_i2c_state state)
{
i2c->state = state;
}
static void bitbang_i2c_enter_stop(bitbang_i2c_interface *i2c) static void bitbang_i2c_enter_stop(bitbang_i2c_interface *i2c)
{ {
DPRINTF("STOP\n"); DPRINTF("STOP\n");
if (i2c->current_addr >= 0) if (i2c->current_addr >= 0)
i2c_end_transfer(i2c->bus); i2c_end_transfer(i2c->bus);
i2c->current_addr = -1; i2c->current_addr = -1;
i2c->state = STOPPED; bitbang_i2c_set_state(i2c, STOPPED);
} }
/* Set device data pin. */ /* Set device data pin. */
@ -69,7 +75,7 @@ int bitbang_i2c_set(bitbang_i2c_interface *i2c, int line, int level)
if (level == 0) { if (level == 0) {
DPRINTF("START\n"); DPRINTF("START\n");
/* START condition. */ /* START condition. */
i2c->state = SENDING_BIT7; bitbang_i2c_set_state(i2c, SENDING_BIT7);
i2c->current_addr = -1; i2c->current_addr = -1;
} else { } else {
/* STOP condition. */ /* STOP condition. */
@ -96,7 +102,7 @@ int bitbang_i2c_set(bitbang_i2c_interface *i2c, int line, int level)
case SENDING_BIT7 ... SENDING_BIT0: case SENDING_BIT7 ... SENDING_BIT0:
i2c->buffer = (i2c->buffer << 1) | data; i2c->buffer = (i2c->buffer << 1) | data;
/* will end up in WAITING_FOR_ACK */ /* will end up in WAITING_FOR_ACK */
i2c->state++; bitbang_i2c_set_state(i2c, i2c->state + 1);
return bitbang_i2c_ret(i2c, 1); return bitbang_i2c_ret(i2c, 1);
case WAITING_FOR_ACK: case WAITING_FOR_ACK:
@ -117,13 +123,14 @@ int bitbang_i2c_set(bitbang_i2c_interface *i2c, int line, int level)
* device we were sending to decided to NACK us). * device we were sending to decided to NACK us).
*/ */
DPRINTF("Got NACK\n"); DPRINTF("Got NACK\n");
bitbang_i2c_set_state(i2c, SENT_NACK);
bitbang_i2c_enter_stop(i2c); bitbang_i2c_enter_stop(i2c);
return bitbang_i2c_ret(i2c, 1); return bitbang_i2c_ret(i2c, 1);
} }
if (i2c->current_addr & 1) { if (i2c->current_addr & 1) {
i2c->state = RECEIVING_BIT7; bitbang_i2c_set_state(i2c, RECEIVING_BIT7);
} else { } else {
i2c->state = SENDING_BIT7; bitbang_i2c_set_state(i2c, SENDING_BIT7);
} }
return bitbang_i2c_ret(i2c, 0); return bitbang_i2c_ret(i2c, 0);
} }
@ -134,18 +141,18 @@ int bitbang_i2c_set(bitbang_i2c_interface *i2c, int line, int level)
case RECEIVING_BIT6 ... RECEIVING_BIT0: case RECEIVING_BIT6 ... RECEIVING_BIT0:
data = i2c->buffer >> 7; data = i2c->buffer >> 7;
/* will end up in SENDING_ACK */ /* will end up in SENDING_ACK */
i2c->state++; bitbang_i2c_set_state(i2c, i2c->state + 1);
i2c->buffer <<= 1; i2c->buffer <<= 1;
return bitbang_i2c_ret(i2c, data); return bitbang_i2c_ret(i2c, data);
case SENDING_ACK: case SENDING_ACK:
i2c->state = RECEIVING_BIT7;
if (data != 0) { if (data != 0) {
DPRINTF("NACKED\n"); DPRINTF("NACKED\n");
i2c->state = SENT_NACK; bitbang_i2c_set_state(i2c, SENT_NACK);
i2c_nack(i2c->bus); i2c_nack(i2c->bus);
} else { } else {
DPRINTF("ACKED\n"); DPRINTF("ACKED\n");
bitbang_i2c_set_state(i2c, RECEIVING_BIT7);
} }
return bitbang_i2c_ret(i2c, 1); return bitbang_i2c_ret(i2c, 1);
} }