3 #include "derivative.h"
4 #include "MC9S12XS128.h"
11 typedef enum { X
, Y
, Z
} axis
;
13 /* gets magnetic field gauss val from lsm
14 * over spi & parses into passed int ptr
16 void read_axis(axis x
, int *hi
, int *lo
) {
58 /* parses 8 bit HI/LO pairs into x/y/z words */
59 void parse_raw(int in
[], long set
[]) {
61 set
[0] = (in
[0] << 8) + in
[1];
62 set
[1] = (in
[2] << 8) + in
[3];
63 set
[2] = (in
[4] << 8) + in
[5];
66 void configure_lsm() {
68 /* wait for SPI ready */
71 /* fill transfer reg */
74 /* wait for transfer */
78 void configure_spi() {
84 SPI0CR1_SSOE
= 1; /* enable slave select */
85 SPI0CR1_MSTR
= 1; /* enable master */
86 SPI0CR1_CPOL
= 0; /* clock polarity */
87 SPI0CR1_SPE
= 1; /* enable system */
88 SPI0CR1_LSBFE
= 1; /* set endian-ness */
90 SPI0CR2_XFRW
= 1; /* set 16 bit width */
91 SPI0CR2_MODFEN
= 1; /* enable slave select */
96 /* SDA :: SPI in :: PM2 :: white
97 * SDO :: SPI out :: PM4 :: blue
98 * SCL :: clock :: PM5 :: yellow
99 * CS :: select :: PM3 :: green
102 /* positional vars & init */
103 long coords
[3] = { null16
, null16
, null16
};
104 int coord8
[6] = { null8
, null8
, null8
, null8
, null8
, null8
};
118 read_axis(X
, &coord8
[0], &coord8
[1]);
121 read_axis(Y
, &coord8
[2], &coord8
[3]);
124 read_axis(Z
, &coord8
[4], &coord8
[5]);;
126 /* parse raw bytes into x/y/z vars */
127 parse_raw(coord8
, coords
);