Code to get all the data through serial port
Dependents: payload_sensor payload_sensor
FXAS21002.cpp@2:1d1cd08b674d, 2021-07-30 (annotated)
- Committer:
- sbk
- Date:
- Fri Jul 30 11:11:34 2021 +0000
- Revision:
- 2:1d1cd08b674d
- Parent:
- 1:8461f7fe0a7f
d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:2741e9e34e8a | 1 | #include "mbed.h" |
Rhyme | 0:2741e9e34e8a | 2 | #include "FXAS21002.h" |
Rhyme | 0:2741e9e34e8a | 3 | |
Rhyme | 0:2741e9e34e8a | 4 | /* Register Address definitions */ |
Rhyme | 0:2741e9e34e8a | 5 | #define REG_STATUS 0x00 |
Rhyme | 0:2741e9e34e8a | 6 | #define REG_OUT_X_MSB 0x01 |
Rhyme | 0:2741e9e34e8a | 7 | #define REG_OUT_X_LSB 0x02 |
Rhyme | 0:2741e9e34e8a | 8 | #define REG_OUT_Y_MSB 0x03 |
Rhyme | 0:2741e9e34e8a | 9 | #define REG_OUT_Y_LSB 0x04 |
Rhyme | 0:2741e9e34e8a | 10 | #define REG_OUT_Z_MSB 0x05 |
Rhyme | 0:2741e9e34e8a | 11 | #define REG_OUT_Z_LSB 0x06 |
Rhyme | 0:2741e9e34e8a | 12 | #define REG_DR_STATUS 0x07 |
Rhyme | 0:2741e9e34e8a | 13 | #define REG_F_STATUS 0x08 |
Rhyme | 0:2741e9e34e8a | 14 | #define REG_F_SETUP 0x09 |
Rhyme | 0:2741e9e34e8a | 15 | #define REG_F_EVENT 0x0A |
Rhyme | 0:2741e9e34e8a | 16 | #define REG_INT_SRC_FLAG 0x0B |
Rhyme | 0:2741e9e34e8a | 17 | #define REG_WHO_AM_I 0x0C |
Rhyme | 0:2741e9e34e8a | 18 | #define REG_CTRL_REG0 0x0D |
Rhyme | 0:2741e9e34e8a | 19 | #define REG_RT_CFG 0x0E |
Rhyme | 0:2741e9e34e8a | 20 | #define REG_RT_SRC 0x0F |
Rhyme | 0:2741e9e34e8a | 21 | #define REG_RT_THS 0x10 |
Rhyme | 0:2741e9e34e8a | 22 | #define REG_RT_COUNT 0x11 |
Rhyme | 0:2741e9e34e8a | 23 | #define REG_TEMP 0x12 |
Rhyme | 0:2741e9e34e8a | 24 | #define REG_CTRL_REG1 0x13 |
Rhyme | 0:2741e9e34e8a | 25 | #define REG_CTRL_REG2 0x14 |
Rhyme | 0:2741e9e34e8a | 26 | #define REG_CTRL_REG3 0x15 |
Rhyme | 0:2741e9e34e8a | 27 | |
Rhyme | 0:2741e9e34e8a | 28 | |
Rhyme | 0:2741e9e34e8a | 29 | FXAS21002::FXAS21002(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) { |
Rhyme | 0:2741e9e34e8a | 30 | // activate the peripheral |
sbk | 2:1d1cd08b674d | 31 | FXAS21002::activate(1); |
sbk | 2:1d1cd08b674d | 32 | FXAS21002::ready(1); |
Rhyme | 0:2741e9e34e8a | 33 | } |
Rhyme | 0:2741e9e34e8a | 34 | |
Rhyme | 0:2741e9e34e8a | 35 | FXAS21002::~FXAS21002() { } |
Rhyme | 0:2741e9e34e8a | 36 | |
Rhyme | 0:2741e9e34e8a | 37 | uint8_t FXAS21002::getStatus(void) |
Rhyme | 0:2741e9e34e8a | 38 | { |
Rhyme | 0:2741e9e34e8a | 39 | uint8_t status ; |
Rhyme | 0:2741e9e34e8a | 40 | readRegs(REG_STATUS, &status, 1) ; |
Rhyme | 0:2741e9e34e8a | 41 | return( status ) ; |
Rhyme | 0:2741e9e34e8a | 42 | } |
Rhyme | 0:2741e9e34e8a | 43 | |
Rhyme | 0:2741e9e34e8a | 44 | int16_t FXAS21002::getX(void) |
Rhyme | 0:2741e9e34e8a | 45 | { |
Rhyme | 0:2741e9e34e8a | 46 | uint8_t data[2] ; |
Rhyme | 0:2741e9e34e8a | 47 | int16_t value ; |
Rhyme | 0:2741e9e34e8a | 48 | readRegs(REG_OUT_X_MSB, &data[0], 1) ; |
Rhyme | 0:2741e9e34e8a | 49 | readRegs(REG_OUT_X_LSB, &data[1], 1) ; |
Rhyme | 0:2741e9e34e8a | 50 | value = (data[0] << 8) | data[1] ; |
sbk | 2:1d1cd08b674d | 51 | // printf("X: %d,%d, %d\t", data[0], data[1], value); |
Rhyme | 0:2741e9e34e8a | 52 | return(value) ; |
Rhyme | 0:2741e9e34e8a | 53 | } |
Rhyme | 0:2741e9e34e8a | 54 | |
Rhyme | 0:2741e9e34e8a | 55 | int16_t FXAS21002::getY(void) |
Rhyme | 0:2741e9e34e8a | 56 | { |
Rhyme | 0:2741e9e34e8a | 57 | uint8_t data[2] ; |
Rhyme | 0:2741e9e34e8a | 58 | int16_t value ; |
Rhyme | 0:2741e9e34e8a | 59 | readRegs(REG_OUT_Y_MSB, &data[0], 1) ; |
Rhyme | 0:2741e9e34e8a | 60 | readRegs(REG_OUT_Y_LSB, &data[1], 1) ; |
Rhyme | 0:2741e9e34e8a | 61 | value = (data[0] << 8) | data[1] ; |
sbk | 2:1d1cd08b674d | 62 | // printf("Y: %d,%d, %d\t", data[0], data[1], value); |
Rhyme | 0:2741e9e34e8a | 63 | return(value) ; |
Rhyme | 0:2741e9e34e8a | 64 | } |
Rhyme | 0:2741e9e34e8a | 65 | |
Rhyme | 0:2741e9e34e8a | 66 | int16_t FXAS21002::getZ(void) |
Rhyme | 0:2741e9e34e8a | 67 | { |
Rhyme | 0:2741e9e34e8a | 68 | uint8_t data[2] ; |
Rhyme | 0:2741e9e34e8a | 69 | int16_t value ; |
Rhyme | 0:2741e9e34e8a | 70 | readRegs(REG_OUT_Z_MSB, &data[0], 1) ; |
Rhyme | 0:2741e9e34e8a | 71 | readRegs(REG_OUT_Z_LSB, &data[1], 1) ; |
Rhyme | 0:2741e9e34e8a | 72 | value = (data[0] << 8) | data[1] ; |
sbk | 2:1d1cd08b674d | 73 | // printf("Z: %d,%d, %d\t", data[0], data[1], value); |
Rhyme | 0:2741e9e34e8a | 74 | return(value) ; |
Rhyme | 0:2741e9e34e8a | 75 | } |
Rhyme | 0:2741e9e34e8a | 76 | |
Rhyme | 1:8461f7fe0a7f | 77 | void FXAS21002::selftest(bool mode) |
Rhyme | 0:2741e9e34e8a | 78 | { |
Rhyme | 0:2741e9e34e8a | 79 | uint8_t value ; |
Rhyme | 0:2741e9e34e8a | 80 | value = getCTRL1() ; |
Rhyme | 0:2741e9e34e8a | 81 | if (mode) { |
Rhyme | 0:2741e9e34e8a | 82 | value |= 0x10 ; |
Rhyme | 0:2741e9e34e8a | 83 | } else { |
Rhyme | 0:2741e9e34e8a | 84 | value ^= 0x10 ; |
Rhyme | 0:2741e9e34e8a | 85 | } |
Rhyme | 0:2741e9e34e8a | 86 | setCTRL1(value) ; |
Rhyme | 0:2741e9e34e8a | 87 | } |
Rhyme | 0:2741e9e34e8a | 88 | |
Rhyme | 0:2741e9e34e8a | 89 | void FXAS21002::activate(bool mode) |
Rhyme | 0:2741e9e34e8a | 90 | { |
Rhyme | 0:2741e9e34e8a | 91 | uint8_t value ; |
Rhyme | 0:2741e9e34e8a | 92 | value = getCTRL1() ; |
Rhyme | 0:2741e9e34e8a | 93 | if (mode) { |
Rhyme | 0:2741e9e34e8a | 94 | value |= 0x02 ; |
Rhyme | 0:2741e9e34e8a | 95 | } else { |
Rhyme | 0:2741e9e34e8a | 96 | value ^= 0x02 ; |
Rhyme | 0:2741e9e34e8a | 97 | } |
Rhyme | 0:2741e9e34e8a | 98 | setCTRL1(value) ; |
Rhyme | 0:2741e9e34e8a | 99 | } |
Rhyme | 0:2741e9e34e8a | 100 | |
Rhyme | 0:2741e9e34e8a | 101 | void FXAS21002::ready(bool mode) |
Rhyme | 0:2741e9e34e8a | 102 | { |
Rhyme | 0:2741e9e34e8a | 103 | uint8_t value ; |
Rhyme | 0:2741e9e34e8a | 104 | value = getCTRL1() ; |
Rhyme | 0:2741e9e34e8a | 105 | if (mode) { |
Rhyme | 0:2741e9e34e8a | 106 | value |= 0x01 ; |
Rhyme | 0:2741e9e34e8a | 107 | } else { |
Rhyme | 0:2741e9e34e8a | 108 | value ^= 0x01 ; |
Rhyme | 0:2741e9e34e8a | 109 | } |
Rhyme | 0:2741e9e34e8a | 110 | setCTRL1(value) ; |
Rhyme | 0:2741e9e34e8a | 111 | } |
Rhyme | 0:2741e9e34e8a | 112 | |
Rhyme | 0:2741e9e34e8a | 113 | uint8_t FXAS21002::getCTRL1(void) |
Rhyme | 0:2741e9e34e8a | 114 | { |
Rhyme | 0:2741e9e34e8a | 115 | uint8_t value ; |
Rhyme | 0:2741e9e34e8a | 116 | readRegs(REG_CTRL_REG1, &value, 1) ; |
Rhyme | 0:2741e9e34e8a | 117 | return(value) ; |
Rhyme | 0:2741e9e34e8a | 118 | } |
Rhyme | 0:2741e9e34e8a | 119 | |
Rhyme | 0:2741e9e34e8a | 120 | void FXAS21002::setCTRL1(uint8_t value) |
Rhyme | 0:2741e9e34e8a | 121 | { |
Rhyme | 0:2741e9e34e8a | 122 | uint8_t data[2] ; |
Rhyme | 0:2741e9e34e8a | 123 | data[0] = REG_CTRL_REG1 ; |
Rhyme | 0:2741e9e34e8a | 124 | data[1] = value ; |
Rhyme | 0:2741e9e34e8a | 125 | writeRegs(data, 2) ; |
Rhyme | 0:2741e9e34e8a | 126 | } |
Rhyme | 0:2741e9e34e8a | 127 | |
Rhyme | 0:2741e9e34e8a | 128 | uint8_t FXAS21002::getCTRL2(void) |
Rhyme | 0:2741e9e34e8a | 129 | { |
Rhyme | 0:2741e9e34e8a | 130 | uint8_t value ; |
Rhyme | 0:2741e9e34e8a | 131 | readRegs(REG_CTRL_REG2, &value, 1) ; |
Rhyme | 0:2741e9e34e8a | 132 | return(value) ; |
Rhyme | 0:2741e9e34e8a | 133 | } |
Rhyme | 0:2741e9e34e8a | 134 | |
Rhyme | 0:2741e9e34e8a | 135 | void FXAS21002::setCTRL2(uint8_t value) |
Rhyme | 0:2741e9e34e8a | 136 | { |
Rhyme | 0:2741e9e34e8a | 137 | uint8_t data[2] ; |
Rhyme | 0:2741e9e34e8a | 138 | data[0] = REG_CTRL_REG2 ; |
Rhyme | 0:2741e9e34e8a | 139 | data[1] = value ; |
Rhyme | 0:2741e9e34e8a | 140 | writeRegs(data, 2) ; |
Rhyme | 0:2741e9e34e8a | 141 | } |
Rhyme | 0:2741e9e34e8a | 142 | |
Rhyme | 0:2741e9e34e8a | 143 | uint8_t FXAS21002::getCTRL3(void) |
Rhyme | 0:2741e9e34e8a | 144 | { |
Rhyme | 0:2741e9e34e8a | 145 | uint8_t value ; |
Rhyme | 0:2741e9e34e8a | 146 | readRegs(REG_CTRL_REG3, &value, 1) ; |
Rhyme | 0:2741e9e34e8a | 147 | return(value) ; |
Rhyme | 0:2741e9e34e8a | 148 | } |
Rhyme | 0:2741e9e34e8a | 149 | |
Rhyme | 0:2741e9e34e8a | 150 | void FXAS21002::setCTRL3(uint8_t value) |
Rhyme | 0:2741e9e34e8a | 151 | { |
Rhyme | 0:2741e9e34e8a | 152 | uint8_t data[2] ; |
Rhyme | 0:2741e9e34e8a | 153 | data[0] = REG_CTRL_REG3 ; |
Rhyme | 0:2741e9e34e8a | 154 | data[1] = value ; |
Rhyme | 0:2741e9e34e8a | 155 | writeRegs(data, 2) ; |
Rhyme | 0:2741e9e34e8a | 156 | } |
Rhyme | 0:2741e9e34e8a | 157 | |
Rhyme | 0:2741e9e34e8a | 158 | |
Rhyme | 0:2741e9e34e8a | 159 | void FXAS21002::readRegs(int addr, uint8_t * data, int len) { |
Rhyme | 0:2741e9e34e8a | 160 | char t[1] = {addr}; |
Rhyme | 0:2741e9e34e8a | 161 | m_i2c.write(m_addr, t, 1, true); |
Rhyme | 0:2741e9e34e8a | 162 | m_i2c.read(m_addr, (char *)data, len); |
Rhyme | 0:2741e9e34e8a | 163 | } |
Rhyme | 0:2741e9e34e8a | 164 | |
Rhyme | 0:2741e9e34e8a | 165 | void FXAS21002::writeRegs(uint8_t * data, int len) { |
Rhyme | 0:2741e9e34e8a | 166 | m_i2c.write(m_addr, (char *)data, len); |
Rhyme | 0:2741e9e34e8a | 167 | } |