]> git.karo-electronics.de Git - oswald.git/blob - metawatch/mw_acc.c
Start support for the accelerometer, change text position for clock setup
[oswald.git] / metawatch / mw_acc.c
1 #include <msp430.h>
2 #include <msp430xgeneric.h>
3 #include <stdint.h>
4
5 #include "mw_main.h"
6
7 #include "mw_acc.h"
8
9 void mw_init_acc_i2c(void)
10 {
11         /* enable reset before configuration */
12         ACCELEROMETER_CTL1 |= UCSWRST;
13
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;
18
19         ACCELEROMETER_BR1 = 0;
20         ACCELEROMETER_I2CSA = KIONIX_DEVICE_ADDRESS;
21
22         /* release reset */
23         ACCELEROMETER_CTL1 &= ~UCSWRST;
24 }
25
26 /*
27  * DMA2 = SPI for LCD
28  */
29 static void mw_acc_i2c_write_byte(uint8_t byte)
30 {
31         ACCELEROMETER_TXBUF = byte;
32         while ((ACCELEROMETER_CTL1 & ACCELEROMETER_IFG) == 0)
33                 nop();
34 }
35
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)
38 {
39         int i;
40
41         if (len == 0) {
42                 return;  
43         }
44   
45         while (UCB1STAT & UCBBUSY)
46                 nop();
47   
48         /* 
49          * setup for write and send the start condition
50          */
51         ACCELEROMETER_IFG = 0;
52         ACCELEROMETER_CTL1 |= UCTR + UCTXSTT;
53         while (!(ACCELEROMETER_IFG & UCTXIFG))
54                 nop();
55   
56         /* 
57          * clear transmit interrupt flag,
58          * send the register address
59          */
60         ACCELEROMETER_IFG = 0;
61
62         mw_acc_i2c_write_byte(addr);
63
64         for (i=0; i<len; i++)
65                 mw_acc_i2c_write_byte(*(uint8_t *)(data+i));
66
67         while (ACCELEROMETER_CTL1 & UCTXSTP)
68                 nop();
69 }
70
71