Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of FXLS8471Q by
FXLS8471Q.cpp@2:6aae25fe9ab4, 2014-06-02 (annotated)
- Committer:
- screamer
- Date:
- Mon Jun 02 17:14:15 2014 +0000
- Revision:
- 2:6aae25fe9ab4
- Parent:
- 1:c80be04510fe
- Child:
- 4:b6e0c4ad3aee
Updated documentation (now visible)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JimCarver | 1:c80be04510fe | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
JimCarver | 1:c80be04510fe | 2 | * |
JimCarver | 1:c80be04510fe | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
JimCarver | 1:c80be04510fe | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
JimCarver | 1:c80be04510fe | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
JimCarver | 1:c80be04510fe | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
JimCarver | 1:c80be04510fe | 7 | * Software is furnished to do so, subject to the following conditions: |
JimCarver | 1:c80be04510fe | 8 | * |
JimCarver | 1:c80be04510fe | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
JimCarver | 1:c80be04510fe | 10 | * substantial portions of the Software. |
JimCarver | 1:c80be04510fe | 11 | * |
JimCarver | 1:c80be04510fe | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
JimCarver | 1:c80be04510fe | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
JimCarver | 1:c80be04510fe | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
JimCarver | 1:c80be04510fe | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
JimCarver | 1:c80be04510fe | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
JimCarver | 1:c80be04510fe | 17 | */ |
JimCarver | 1:c80be04510fe | 18 | |
JimCarver | 1:c80be04510fe | 19 | |
screamer | 2:6aae25fe9ab4 | 20 | #include "FXLS8471Q.h" |
screamer | 2:6aae25fe9ab4 | 21 | #define UINT14_MAX 16383 |
screamer | 2:6aae25fe9ab4 | 22 | |
screamer | 2:6aae25fe9ab4 | 23 | FXLS8471Q::FXLS8471Q(PinName mosi, PinName miso, PinName sck, PinName cs) : _spi(mosi, miso, sck), _spi_cs(cs) |
screamer | 2:6aae25fe9ab4 | 24 | { |
JimCarver | 0:f89f2dc4b003 | 25 | _spi_cs = 1; |
JimCarver | 0:f89f2dc4b003 | 26 | wait(0.1); |
JimCarver | 0:f89f2dc4b003 | 27 | begin(); |
JimCarver | 0:f89f2dc4b003 | 28 | } |
JimCarver | 0:f89f2dc4b003 | 29 | |
JimCarver | 0:f89f2dc4b003 | 30 | void FXLS8471Q::RegWrite( int reg, int *d, int len) |
JimCarver | 0:f89f2dc4b003 | 31 | { |
JimCarver | 0:f89f2dc4b003 | 32 | int ob[2]; |
JimCarver | 0:f89f2dc4b003 | 33 | int c = 0; |
JimCarver | 0:f89f2dc4b003 | 34 | ob[1] = 0; |
JimCarver | 0:f89f2dc4b003 | 35 | ob[0] = reg + 128; |
JimCarver | 0:f89f2dc4b003 | 36 | _spi_cs = 0; |
JimCarver | 0:f89f2dc4b003 | 37 | _spi.write(ob[0]); |
JimCarver | 0:f89f2dc4b003 | 38 | _spi.write(ob[1]); |
JimCarver | 0:f89f2dc4b003 | 39 | while(c < len) _spi.write(d[c++]); |
JimCarver | 0:f89f2dc4b003 | 40 | _spi_cs = 1; |
JimCarver | 0:f89f2dc4b003 | 41 | } |
screamer | 2:6aae25fe9ab4 | 42 | |
JimCarver | 0:f89f2dc4b003 | 43 | void FXLS8471Q::RegRead( int reg, int *d, int len) |
JimCarver | 0:f89f2dc4b003 | 44 | { |
JimCarver | 0:f89f2dc4b003 | 45 | int ob[2]; |
JimCarver | 0:f89f2dc4b003 | 46 | int c = 0; |
JimCarver | 0:f89f2dc4b003 | 47 | ob[0] = reg; |
JimCarver | 0:f89f2dc4b003 | 48 | ob[1] = 0; |
JimCarver | 0:f89f2dc4b003 | 49 | _spi_cs = 0; |
JimCarver | 0:f89f2dc4b003 | 50 | _spi.write(ob[0]); |
JimCarver | 0:f89f2dc4b003 | 51 | _spi.write(ob[1]); |
JimCarver | 0:f89f2dc4b003 | 52 | while(c < len) d[c++] = _spi.write(0xff); |
JimCarver | 0:f89f2dc4b003 | 53 | _spi_cs = 1; |
JimCarver | 0:f89f2dc4b003 | 54 | } |
JimCarver | 0:f89f2dc4b003 | 55 | |
JimCarver | 1:c80be04510fe | 56 | char FXLS8471Q::getWhoAmI(void) |
JimCarver | 1:c80be04510fe | 57 | { |
JimCarver | 1:c80be04510fe | 58 | int d; |
JimCarver | 1:c80be04510fe | 59 | RegRead( FXLS8471Q_WHOAMI, &d, 1); |
JimCarver | 1:c80be04510fe | 60 | return((char) d); |
JimCarver | 1:c80be04510fe | 61 | } |
JimCarver | 1:c80be04510fe | 62 | |
JimCarver | 1:c80be04510fe | 63 | |
JimCarver | 0:f89f2dc4b003 | 64 | void FXLS8471Q::begin(void) |
JimCarver | 0:f89f2dc4b003 | 65 | { |
JimCarver | 0:f89f2dc4b003 | 66 | int databyte; |
JimCarver | 0:f89f2dc4b003 | 67 | |
JimCarver | 0:f89f2dc4b003 | 68 | // write 0000 0000 = 0x00 to accelerometer control register 1 to place FXLS8471Q into |
JimCarver | 0:f89f2dc4b003 | 69 | // standby |
JimCarver | 0:f89f2dc4b003 | 70 | // [7-1] = 0000 000 |
JimCarver | 0:f89f2dc4b003 | 71 | // [0]: active=0 |
JimCarver | 0:f89f2dc4b003 | 72 | databyte = 0x00; |
JimCarver | 0:f89f2dc4b003 | 73 | RegWrite(FXLS8471Q_CTRL_REG1, &databyte, 1); |
JimCarver | 0:f89f2dc4b003 | 74 | |
JimCarver | 0:f89f2dc4b003 | 75 | // write 0000 0001= 0x01 to XYZ_DATA_CFG register |
JimCarver | 0:f89f2dc4b003 | 76 | // [7]: reserved |
JimCarver | 0:f89f2dc4b003 | 77 | // [6]: reserved |
JimCarver | 0:f89f2dc4b003 | 78 | // [5]: reserved |
JimCarver | 0:f89f2dc4b003 | 79 | // [4]: hpf_out=0 |
JimCarver | 0:f89f2dc4b003 | 80 | // [3]: reserved |
JimCarver | 0:f89f2dc4b003 | 81 | // [2]: reserved |
JimCarver | 0:f89f2dc4b003 | 82 | // [1-0]: fs=00 for accelerometer range of +/-2g with 0.244mg/LSB |
JimCarver | 0:f89f2dc4b003 | 83 | databyte = 0x00; |
JimCarver | 0:f89f2dc4b003 | 84 | RegWrite(FXLS8471Q_XYZ_DATA_CFG, &databyte, 1); |
JimCarver | 0:f89f2dc4b003 | 85 | |
JimCarver | 0:f89f2dc4b003 | 86 | // write 0001 0101b = 0x15 to accelerometer control register 1 |
JimCarver | 0:f89f2dc4b003 | 87 | // [7-6]: aslp_rate=00 |
JimCarver | 0:f89f2dc4b003 | 88 | // [5-3]: dr=010 for 200Hz data rate |
JimCarver | 0:f89f2dc4b003 | 89 | // [2]: lnoise=1 for low noise mode |
JimCarver | 0:f89f2dc4b003 | 90 | // [1]: f_read=0 for normal 16 bit reads |
JimCarver | 0:f89f2dc4b003 | 91 | // [0]: active=1 to take the part out of standby and enable sampling |
JimCarver | 0:f89f2dc4b003 | 92 | databyte = 0x15; |
screamer | 2:6aae25fe9ab4 | 93 | RegWrite(FXLS8471Q_CTRL_REG1, &databyte, 1); |
JimCarver | 0:f89f2dc4b003 | 94 | } |
JimCarver | 0:f89f2dc4b003 | 95 | |
JimCarver | 0:f89f2dc4b003 | 96 | void FXLS8471Q::ReadXYZ(float * fdata) |
JimCarver | 0:f89f2dc4b003 | 97 | { |
screamer | 2:6aae25fe9ab4 | 98 | int raw[7]; |
screamer | 2:6aae25fe9ab4 | 99 | int16_t ix, iy, iz; |
screamer | 2:6aae25fe9ab4 | 100 | RegRead(FXLS8471Q_STATUS, raw, 7); |
JimCarver | 0:f89f2dc4b003 | 101 | |
screamer | 2:6aae25fe9ab4 | 102 | ix = ((raw[1] * 256) + raw[2]);// / 4; |
screamer | 2:6aae25fe9ab4 | 103 | iy = ((raw[3] * 256) + raw[4]);// / 4; |
screamer | 2:6aae25fe9ab4 | 104 | iz = ((raw[5] * 256) + raw[6]);// / 4; |
screamer | 2:6aae25fe9ab4 | 105 | fdata[0] = ((float) ix) / 16384.0; |
screamer | 2:6aae25fe9ab4 | 106 | fdata[1] = ((float) iy) / 16384.0; |
screamer | 2:6aae25fe9ab4 | 107 | fdata[2] = ((float) iz) / 16384.0; |
JimCarver | 0:f89f2dc4b003 | 108 | } |
JimCarver | 1:c80be04510fe | 109 | |
JimCarver | 1:c80be04510fe | 110 | void FXLS8471Q::ReadXYZraw(int16_t * d) |
JimCarver | 1:c80be04510fe | 111 | { |
screamer | 2:6aae25fe9ab4 | 112 | int res[6]; |
screamer | 2:6aae25fe9ab4 | 113 | int16_t acc; |
JimCarver | 1:c80be04510fe | 114 | RegRead(FXLS8471Q_OUT_X_MSB, res, 6); |
JimCarver | 1:c80be04510fe | 115 | |
JimCarver | 1:c80be04510fe | 116 | acc = (res[0] << 6) | (res[1] >> 2); |
JimCarver | 1:c80be04510fe | 117 | if (acc > UINT14_MAX/2) |
JimCarver | 1:c80be04510fe | 118 | acc -= UINT14_MAX; |
JimCarver | 1:c80be04510fe | 119 | d[0] = acc; |
JimCarver | 1:c80be04510fe | 120 | acc = (res[2] << 6) | (res[3] >> 2); |
JimCarver | 1:c80be04510fe | 121 | if (acc > UINT14_MAX/2) |
JimCarver | 1:c80be04510fe | 122 | acc -= UINT14_MAX; |
JimCarver | 1:c80be04510fe | 123 | d[1] = acc; |
JimCarver | 1:c80be04510fe | 124 | acc = (res[4] << 6) | (res[5] >> 2); |
JimCarver | 1:c80be04510fe | 125 | if (acc > UINT14_MAX/2) |
JimCarver | 1:c80be04510fe | 126 | acc -= UINT14_MAX; |
JimCarver | 1:c80be04510fe | 127 | d[2] = acc; |
JimCarver | 1:c80be04510fe | 128 | } |
screamer | 2:6aae25fe9ab4 | 129 |