| 1 | #include <hidef.h> |
| 2 | |
| 3 | #include "derivative.h" |
| 4 | #include "MC9S12XS128.h" |
| 5 | |
| 6 | /* laziness */ |
| 7 | #define null16 0x0000 |
| 8 | #define null8 0x00 |
| 9 | |
| 10 | /* x, y, z enum */ |
| 11 | typedef enum { X, Y, Z } axis; |
| 12 | |
| 13 | /* gets magnetic field gauss val from lsm |
| 14 | * over spi & parses into passed int ptr |
| 15 | */ |
| 16 | void read_axis(axis x, int *hi, int *lo) { |
| 17 | |
| 18 | switch(x) { |
| 19 | |
| 20 | case X: |
| 21 | |
| 22 | SPI0DR = 0x8900; |
| 23 | while(!SPI0SR_SPTEF); |
| 24 | *hi = SPI0DRL; |
| 25 | |
| 26 | SPI0DR = 0x8800; |
| 27 | while(!SPI0SR_SPTEF); |
| 28 | *lo = SPI0DRL; |
| 29 | |
| 30 | break; |
| 31 | |
| 32 | case Y: |
| 33 | |
| 34 | SPI0DR = 0x8B00; |
| 35 | while(!SPI0SR_SPTEF); |
| 36 | *hi = SPI0DRL; |
| 37 | |
| 38 | SPI0DR = 0x8A00; |
| 39 | while(!SPI0SR_SPTEF); |
| 40 | *lo = SPI0DRL; |
| 41 | |
| 42 | break; |
| 43 | |
| 44 | case Z: |
| 45 | |
| 46 | SPI0DR = 0x8D00; |
| 47 | while(!SPI0SR_SPTEF); |
| 48 | *hi = SPI0DRL; |
| 49 | |
| 50 | SPI0DR = 0x8C00; |
| 51 | while(!SPI0SR_SPTEF); |
| 52 | *lo = SPI0DRL; |
| 53 | |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /* parses 8 bit HI/LO pairs into x/y/z words */ |
| 59 | void parse_raw(int in[], long set[]) { |
| 60 | |
| 61 | set[0] = (in[0] << 8) + in[1]; |
| 62 | set[1] = (in[2] << 8) + in[3]; |
| 63 | set[2] = (in[4] << 8) + in[5]; |
| 64 | } |
| 65 | |
| 66 | void configure_lsm() { |
| 67 | |
| 68 | /* wait for SPI ready */ |
| 69 | while(!SPI0SR_SPTEF); |
| 70 | |
| 71 | /* fill transfer reg */ |
| 72 | SPI0DR = 0x2474; |
| 73 | |
| 74 | /* wait for transfer */ |
| 75 | while(!SPI0SR_SPTEF); |
| 76 | } |
| 77 | |
| 78 | void configure_spi() { |
| 79 | |
| 80 | /* reroute spi I/O */ |
| 81 | MODRR_MODRR4 = 1; |
| 82 | |
| 83 | /* 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 */ |
| 89 | |
| 90 | SPI0CR2_XFRW = 1; /* set 16 bit width */ |
| 91 | SPI0CR2_MODFEN = 1; /* enable slave select */ |
| 92 | } |
| 93 | |
| 94 | void main(void) { |
| 95 | |
| 96 | /* SDA :: SPI in :: PM2 :: white |
| 97 | * SDO :: SPI out :: PM4 :: blue |
| 98 | * SCL :: clock :: PM5 :: yellow |
| 99 | * CS :: select :: PM3 :: green |
| 100 | */ |
| 101 | |
| 102 | /* positional vars & init */ |
| 103 | long coords[3] = { null16, null16, null16 }; |
| 104 | int coord8[6] = { null8, null8, null8, null8, null8, null8 }; |
| 105 | |
| 106 | /* configure SPI */ |
| 107 | configure_spi(); |
| 108 | |
| 109 | /* configure LSM */ |
| 110 | configure_lsm(); |
| 111 | |
| 112 | /* latch intrs. */ |
| 113 | EnableInterrupts; |
| 114 | |
| 115 | for(;;) { |
| 116 | |
| 117 | /* read x hi/lo */ |
| 118 | read_axis(X, &coord8[0], &coord8[1]); |
| 119 | |
| 120 | /* read y hi/lo */ |
| 121 | read_axis(Y, &coord8[2], &coord8[3]); |
| 122 | |
| 123 | /* read z hi/lo */ |
| 124 | read_axis(Z, &coord8[4], &coord8[5]);; |
| 125 | |
| 126 | /* parse raw bytes into x/y/z vars */ |
| 127 | parse_raw(coord8, coords); |
| 128 | } |
| 129 | } |