X-Git-Url: https://git.karo-electronics.de/?p=oswald.git;a=blobdiff_plain;f=metawatch%2Fmw_acc.c;fp=metawatch%2Fmw_acc.c;h=270f2467304894237492941ebf905ce38960adc7;hp=0000000000000000000000000000000000000000;hb=6029630643335ebe7dbba2da8e7bf2b5f7a434f9;hpb=b254c4abccb1ef8a6de4bd543b8135af071d5f7f diff --git a/metawatch/mw_acc.c b/metawatch/mw_acc.c new file mode 100644 index 0000000..270f246 --- /dev/null +++ b/metawatch/mw_acc.c @@ -0,0 +1,71 @@ +#include +#include +#include + +#include "mw_main.h" + +#include "mw_acc.h" + +void mw_init_acc_i2c(void) +{ + /* enable reset before configuration */ + ACCELEROMETER_CTL1 |= UCSWRST; + + /* configure as master using smclk / 40 = 399.5 kHz */ + ACCELEROMETER_CTL0 = UCMST + UCMODE_3 + UCSYNC; + ACCELEROMETER_CTL1 = UCSSEL__SMCLK + UCSWRST; + ACCELEROMETER_BR0 = 42; + + ACCELEROMETER_BR1 = 0; + ACCELEROMETER_I2CSA = KIONIX_DEVICE_ADDRESS; + + /* release reset */ + ACCELEROMETER_CTL1 &= ~UCSWRST; +} + +/* + * DMA2 = SPI for LCD + */ +static void mw_acc_i2c_write_byte(uint8_t byte) +{ + ACCELEROMETER_TXBUF = byte; + while ((ACCELEROMETER_CTL1 & ACCELEROMETER_IFG) == 0) + nop(); +} + +/* OK this is polling write, but data is small and 400kHz I2C, it should "just work" :) */ +void mw_acc_i2c_write(const uint8_t addr, const void *data, const uint8_t len) +{ + int i; + + if (len == 0) { + return; + } + + while (UCB1STAT & UCBBUSY) + nop(); + + /* + * setup for write and send the start condition + */ + ACCELEROMETER_IFG = 0; + ACCELEROMETER_CTL1 |= UCTR + UCTXSTT; + while (!(ACCELEROMETER_IFG & UCTXIFG)) + nop(); + + /* + * clear transmit interrupt flag, + * send the register address + */ + ACCELEROMETER_IFG = 0; + + mw_acc_i2c_write_byte(addr); + + for (i=0; i