diff options
author | Daniel Friesel <derf@finalrewind.org> | 2018-10-25 15:50:18 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2018-10-25 15:50:18 +0200 |
commit | 6da18337a9e4c8c68829df09befddf4582584135 (patch) | |
tree | 40aea3c41284b6c5f3477aaf9c530842517f2792 /src | |
parent | 61ff5bbb4cae80997562bcd4edd5c35a4f801b44 (diff) |
msp430 i2c: refactor code a bit
Diffstat (limited to 'src')
-rw-r--r-- | src/arch/msp430fr5969lp/driver/i2c.cc | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/src/arch/msp430fr5969lp/driver/i2c.cc b/src/arch/msp430fr5969lp/driver/i2c.cc index cb6fa79..f807d00 100644 --- a/src/arch/msp430fr5969lp/driver/i2c.cc +++ b/src/arch/msp430fr5969lp/driver/i2c.cc @@ -8,6 +8,17 @@ volatile unsigned short old_ifg = 0; +inline void await_i2c_int(unsigned int ie_flags, unsigned int ifg_flags) +{ + UCB0IFG = 0; + old_ifg = 0; + UCB0IE = ie_flags; + while (!(old_ifg & ifg_flags)) { + arch.idle(); + } + UCB0IE = 0; +} + signed char I2C::setup() { #ifdef I2C_PULLUP_FIXED_GPIO @@ -58,13 +69,7 @@ signed char I2C::xmit(unsigned char address, if (tx_len) { UCB0CTL1 |= UCTR | UCTXSTT; for (i = 0; i < tx_len; i++) { - UCB0IFG = 0; - old_ifg = 0; - UCB0IE = UCTXIE0 | UCNACKIE | UCCLTOIE; - while (!(old_ifg & (UCTXIFG0 | UCNACKIFG | UCCLTOIFG))) { - arch.idle(); - } - UCB0IE = 0; + await_i2c_int(UCTXIE0 | UCNACKIE | UCCLTOIE, UCTXIFG0 | UCNACKIFG | UCCLTOIFG); if (old_ifg & (UCNACKIFG | UCCLTOIFG)) { UCB0CTL1 |= UCTXSTP; return -1; @@ -72,11 +77,7 @@ signed char I2C::xmit(unsigned char address, old_ifg = 0; UCB0TXBUF = tx_buf[i]; } - UCB0IE = UCTXIE0 | UCNACKIE | UCCLTOIE; - while (!(old_ifg & (UCTXIFG0 | UCNACKIFG | UCCLTOIFG))) { - arch.idle(); - } - UCB0IE = 0; + await_i2c_int(UCTXIE0 | UCNACKIE | UCCLTOIE, UCTXIFG0 | UCNACKIFG | UCCLTOIFG); //if (UCB0IFG & (UCNACKIFG | UCCLTOIFG)) { // UCB0IFG &= ~UCNACKIFG; // UCB0IFG &= ~UCCLTOIFG; @@ -96,13 +97,7 @@ signed char I2C::xmit(unsigned char address, for (i = 0; i < rx_len; i++) { if (i == rx_len - 1) UCB0CTL1 |= UCTXSTP; - UCB0IFG = 0; - old_ifg = 0; - UCB0IE = UCRXIE | UCNACKIE | UCCLTOIE; - while (!(old_ifg & (UCRXIFG0 | UCNACKIFG | UCCLTOIFG))) { - arch.idle(); - } - UCB0IE = 0; + await_i2c_int(UCRXIE | UCNACKIE | UCCLTOIE, UCRXIFG0 | UCNACKIFG | UCCLTOIFG); rx_buf[i] = UCB0RXBUF; UCB0IFG &= ~UCRXIFG0; } |