2 #include <msp430xgeneric.h>
9 void mw_init_acc_i2c(void)
11 /* enable reset before configuration */
12 ACCELEROMETER_CTL1 |= UCSWRST;
14 /* configure as master using smclk / 40 = 399.5 kHz */
15 ACCELEROMETER_CTL0 = UCMST + UCMODE_3 + UCSYNC;
16 ACCELEROMETER_CTL1 = UCSSEL__SMCLK + UCSWRST;
17 ACCELEROMETER_BR0 = 42;
19 ACCELEROMETER_BR1 = 0;
20 ACCELEROMETER_I2CSA = KIONIX_DEVICE_ADDRESS;
23 ACCELEROMETER_CTL1 &= ~UCSWRST;
29 static void mw_acc_i2c_write_byte(uint8_t byte)
31 ACCELEROMETER_TXBUF = byte;
32 while ((ACCELEROMETER_CTL1 & ACCELEROMETER_IFG) == 0)
36 /* OK this is polling write, but data is small and 400kHz I2C, it should "just work" :) */
37 void mw_acc_i2c_write(const uint8_t addr, const void *data, const uint8_t len)
45 while (UCB1STAT & UCBBUSY)
49 * setup for write and send the start condition
51 ACCELEROMETER_IFG = 0;
52 ACCELEROMETER_CTL1 |= UCTR + UCTXSTT;
53 while (!(ACCELEROMETER_IFG & UCTXIFG))
57 * clear transmit interrupt flag,
58 * send the register address
60 ACCELEROMETER_IFG = 0;
62 mw_acc_i2c_write_byte(addr);
65 mw_acc_i2c_write_byte(*(uint8_t *)(data+i));
67 while (ACCELEROMETER_CTL1 & UCTXSTP)