amiro-lld / drivers / L3G4200D / v1 / alld_L3G4200D.c @ ed9a1bf5
History | View | Annotate | Download (11.154 KB)
| 1 | d6728c5b | Thomas Schöpping | /*
 | 
      
|---|---|---|---|
| 2 | AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
 | 
      ||
| 3 | f69ec051 | Thomas Schöpping | Copyright (C) 2016..2020  Thomas Schöpping et al.
 | 
      
| 4 | d6728c5b | Thomas Schöpping | |
| 5 | This program is free software: you can redistribute it and/or modify
 | 
      ||
| 6 | f0ca400f | Thomas Schöpping | it under the terms of the GNU Lesser General Public License as published by
 | 
      
| 7 | d6728c5b | Thomas Schöpping | the Free Software Foundation, either version 3 of the License, or
 | 
      
| 8 | (at your option) any later version.
 | 
      ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful,
 | 
      ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
      ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
      ||
| 13 | f0ca400f | Thomas Schöpping | GNU Lesser General Public License for more details.
 | 
      
| 14 | d6728c5b | Thomas Schöpping | |
| 15 | f0ca400f | Thomas Schöpping | You should have received a copy of the GNU Lesser General Public License
 | 
      
| 16 | d6728c5b | Thomas Schöpping | along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
      
| 17 | */
 | 
      ||
| 18 | |||
| 19 | 5e2f673b | Marc Rothmann | /**
 | 
      
| 20 | 9466e34d | Thomas Schöpping |  * @file    alld_L3G4200D.c
 | 
      
| 21 | 5e2f673b | Marc Rothmann |  * @brief   Gyroscope function implementations.
 | 
      
| 22 |  *
 | 
      ||
| 23 |  * @addtogroup lld_gyroscope
 | 
      ||
| 24 |  * @{
 | 
      ||
| 25 |  */
 | 
      ||
| 26 | |||
| 27 | 1d5bcc82 | Thomas Schöpping | #include<alld_L3G4200D.h>  | 
      
| 28 | d6728c5b | Thomas Schöpping | #include <string.h>  | 
      
| 29 | |||
| 30 | ef078306 | Thomas Schöpping | /******************************************************************************/
 | 
      
| 31 | /* LOCAL DEFINITIONS                                                          */
 | 
      ||
| 32 | /******************************************************************************/
 | 
      ||
| 33 | |||
| 34 | /******************************************************************************/
 | 
      ||
| 35 | /* EXPORTED VARIABLES                                                         */
 | 
      ||
| 36 | /******************************************************************************/
 | 
      ||
| 37 | |||
| 38 | /******************************************************************************/
 | 
      ||
| 39 | /* LOCAL TYPES                                                                */
 | 
      ||
| 40 | /******************************************************************************/
 | 
      ||
| 41 | |||
| 42 | /******************************************************************************/
 | 
      ||
| 43 | /* LOCAL VARIABLES                                                            */
 | 
      ||
| 44 | /******************************************************************************/
 | 
      ||
| 45 | |||
| 46 | /******************************************************************************/
 | 
      ||
| 47 | /* LOCAL FUNCTIONS                                                            */
 | 
      ||
| 48 | /******************************************************************************/
 | 
      ||
| 49 | |||
| 50 | /******************************************************************************/
 | 
      ||
| 51 | /* EXPORTED FUNCTIONS                                                         */
 | 
      ||
| 52 | /******************************************************************************/
 | 
      ||
| 53 | |||
| 54 | d6728c5b | Thomas Schöpping | /**
 | 
      
| 55 |  * @brief Read the content of one or more registers.
 | 
      ||
| 56 |  * @param[in]   l3gd      The L3G4200D driver to use.
 | 
      ||
| 57 |  * @param[in]   regaddr   The address of the first register.
 | 
      ||
| 58 |  * @param[out]  data      The data read from the registers.
 | 
      ||
| 59 |  * @param[in]   length    Number of registers to read.
 | 
      ||
| 60 |  *
 | 
      ||
| 61 |  * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
 | 
      ||
| 62 |  */
 | 
      ||
| 63 | 21076167 | Thomas Schöpping | apalExitStatus_t l3g4200d_lld_read_register(const L3G4200DDriver* const l3gd, const l3g4200d_lld_register_t regaddr, uint8_t* const data, const uint8_t length)  | 
      
| 64 | d6728c5b | Thomas Schöpping | {
 | 
      
| 65 | apalDbgAssert(l3gd != NULL && l3gd->spid != NULL);  | 
      ||
| 66 |   apalDbgAssert(data != NULL);
 | 
      ||
| 67 | |||
| 68 |   uint8_t buffer[length+1];
 | 
      ||
| 69 | buffer[0] = regaddr | L3G4200D_LLD_SPI_READ | ((length > 1) ? L3G4200D_LLD_SPI_MULT : 0);  | 
      ||
| 70 |   apalExitStatus_t status = apalSPIExchange(l3gd->spid, buffer, buffer, length+1);
 | 
      ||
| 71 |   memcpy(data, buffer+1, length);
 | 
      ||
| 72 |   return status;
 | 
      ||
| 73 | }  | 
      ||
| 74 | |||
| 75 | /**
 | 
      ||
| 76 |  * @brief Write to one or more registers.
 | 
      ||
| 77 |  * @param[in]   l3gd      The L3G4200D driver to use.
 | 
      ||
| 78 |  * @param[in]   regaddr   The address of the first register.
 | 
      ||
| 79 |  * @param[in]   data      The data to be written to the registers.
 | 
      ||
| 80 |  * @param[in]   length    Number of registers to write.
 | 
      ||
| 81 |  *
 | 
      ||
| 82 |  * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
 | 
      ||
| 83 |  */
 | 
      ||
| 84 | 21076167 | Thomas Schöpping | apalExitStatus_t l3g4200d_lld_write_register(const L3G4200DDriver* const l3gd, const l3g4200d_lld_register_t regaddr, const uint8_t* const data, const uint8_t length)  | 
      
| 85 | d6728c5b | Thomas Schöpping | {
 | 
      
| 86 | apalDbgAssert(l3gd != NULL && l3gd->spid != NULL);  | 
      ||
| 87 |   apalDbgAssert(data != NULL);
 | 
      ||
| 88 | |||
| 89 |   uint8_t buffer[length+1];
 | 
      ||
| 90 | buffer[0] = regaddr | L3G4200D_LLD_SPI_WRITE | ((length > 1) ? L3G4200D_LLD_SPI_MULT : 0);  | 
      ||
| 91 |   memcpy(buffer+1, data, length);
 | 
      ||
| 92 | return apalSPITransmit(l3gd->spid, buffer, length+1);  | 
      ||
| 93 | }  | 
      ||
| 94 | |||
| 95 | /**
 | 
      ||
| 96 |  * @brief Read the sensor data of all 3 axes.
 | 
      ||
| 97 |  * @param[in]   l3gd      The L3G4200D driver to use.
 | 
      ||
| 98 |  * @param[out]  data      The sensor data.
 | 
      ||
| 99 |  * @param[in]   cfg       The current configuration. Needed to find out if the data is saved as little or big endian.
 | 
      ||
| 100 |  *
 | 
      ||
| 101 |  * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
 | 
      ||
| 102 |  */
 | 
      ||
| 103 | 21076167 | Thomas Schöpping | apalExitStatus_t l3g4200d_lld_read_all_data(const L3G4200DDriver* const l3gd, int16_t* const data, const l3g4200d_lld_cfg_t* const cfg)  | 
      
| 104 | d6728c5b | Thomas Schöpping | {
 | 
      
| 105 |   apalDbgAssert(data != NULL);
 | 
      ||
| 106 |   apalDbgAssert(cfg != NULL);
 | 
      ||
| 107 | |||
| 108 |   uint8_t buffer[6];
 | 
      ||
| 109 |   apalExitStatus_t status = l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_OUT_X_L, buffer, 6);
 | 
      ||
| 110 |   if (!(cfg->registers.ctrl_reg4 & L3G4200D_LLD_BLE_MSB)) {
 | 
      ||
| 111 | data[0] = (int16_t) (buffer[0] | (buffer[1] << 8));  | 
      ||
| 112 | data[1] = (int16_t) (buffer[2] | (buffer[3] << 8));  | 
      ||
| 113 | data[2] = (int16_t) (buffer[4] | (buffer[5] << 8));  | 
      ||
| 114 |   } else {
 | 
      ||
| 115 | data[0] = (int16_t) (buffer[1] | (buffer[0] << 8));  | 
      ||
| 116 | data[1] = (int16_t) (buffer[3] | (buffer[2] << 8));  | 
      ||
| 117 | data[2] = (int16_t) (buffer[5] | (buffer[4] << 8));  | 
      ||
| 118 | }  | 
      ||
| 119 |   return status;
 | 
      ||
| 120 | }  | 
      ||
| 121 | |||
| 122 | /**
 | 
      ||
| 123 |  * @brief Read the sensor data of one axis.
 | 
      ||
| 124 |  * @param[in]   l3gd      The L3G4200D driver to use.
 | 
      ||
| 125 |  * @param[out]  data      The sensor data.
 | 
      ||
| 126 |  * @param[in]   axis      The axis for which the data should be read.
 | 
      ||
| 127 |  * @param[in]   cfg       The current configuration. Needed to find out if the data is saved as little or big endian.
 | 
      ||
| 128 |  *
 | 
      ||
| 129 |  * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
 | 
      ||
| 130 |  */
 | 
      ||
| 131 | 21076167 | Thomas Schöpping | apalExitStatus_t l3g4200d_lld_read_data(const L3G4200DDriver* const l3gd, int16_t* const data, const l3g4200d_lld_axis_t axis, const l3g4200d_lld_cfg_t* const cfg)  | 
      
| 132 | d6728c5b | Thomas Schöpping | {
 | 
      
| 133 |   apalDbgAssert(data != NULL);
 | 
      ||
| 134 |   apalDbgAssert(cfg != NULL);
 | 
      ||
| 135 | |||
| 136 | apalExitStatus_t status = APAL_STATUS_SUCCESS;  | 
      ||
| 137 |   uint8_t buffer[2];
 | 
      ||
| 138 |   switch (axis) {
 | 
      ||
| 139 |     case L3G4200D_LLD_X_AXIS:
 | 
      ||
| 140 |       status = l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_OUT_X_L, buffer, 2);
 | 
      ||
| 141 |       break;
 | 
      ||
| 142 |     case L3G4200D_LLD_Y_AXIS:
 | 
      ||
| 143 |       status = l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_OUT_Y_L, buffer, 2);
 | 
      ||
| 144 |       break;
 | 
      ||
| 145 |     case L3G4200D_LLD_Z_AXIS:
 | 
      ||
| 146 |       status = l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_OUT_Z_L, buffer, 2);
 | 
      ||
| 147 |       break;
 | 
      ||
| 148 |     default:
 | 
      ||
| 149 |       return APAL_STATUS_INVALIDARGUMENTS;
 | 
      ||
| 150 | }  | 
      ||
| 151 |   // entweder jedes mal endian abfragen oder config mit übergeben
 | 
      ||
| 152 |   if (!(cfg->registers.ctrl_reg4 & L3G4200D_LLD_BLE_MSB)) {
 | 
      ||
| 153 | *data = (int16_t) (buffer[0] | (buffer[1] << 8));  | 
      ||
| 154 |   } else {
 | 
      ||
| 155 | *data = (int16_t) (buffer[1] | (buffer[0] << 8));  | 
      ||
| 156 | }  | 
      ||
| 157 |   return status;
 | 
      ||
| 158 | }  | 
      ||
| 159 | |||
| 160 | /**
 | 
      ||
| 161 |  * @brief Read the current configuration
 | 
      ||
| 162 |  * @param[in]   l3gd      The L3G4200D driver to use.
 | 
      ||
| 163 |  * @param[out]  cfg       The current configuration.
 | 
      ||
| 164 |  *
 | 
      ||
| 165 |  * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
 | 
      ||
| 166 |  */
 | 
      ||
| 167 | 21076167 | Thomas Schöpping | apalExitStatus_t l3g4200d_lld_read_config(const L3G4200DDriver* const l3gd, l3g4200d_lld_cfg_t* const cfg)  | 
      
| 168 | d6728c5b | Thomas Schöpping | {
 | 
      
| 169 |   apalDbgAssert(cfg != NULL);
 | 
      ||
| 170 | |||
| 171 | return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, cfg->data, 5);  | 
      ||
| 172 | }  | 
      ||
| 173 | |||
| 174 | /**
 | 
      ||
| 175 |  * @brief Write the a configuration
 | 
      ||
| 176 |  * @param[in]   l3gd      The L3G4200D driver to use.
 | 
      ||
| 177 |  * @param[in]   cfg       The new configuration.
 | 
      ||
| 178 |  *
 | 
      ||
| 179 |  * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
 | 
      ||
| 180 |  */
 | 
      ||
| 181 | 21076167 | Thomas Schöpping | apalExitStatus_t l3g4200d_lld_write_config(const L3G4200DDriver* const l3gd, const l3g4200d_lld_cfg_t cfg)  | 
      
| 182 | d6728c5b | Thomas Schöpping | {
 | 
      
| 183 | return l3g4200d_lld_write_register(l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, cfg.data, 5);  | 
      ||
| 184 | }  | 
      ||
| 185 | |||
| 186 | /**
 | 
      ||
| 187 |  * @brief Read the interrupt configuration of one of the interrupts.
 | 
      ||
| 188 |  * @param[in]   spid        The SPI driver to use.
 | 
      ||
| 189 |  * @param[out]  cfg         The current interrupt configuration.
 | 
      ||
| 190 |  *
 | 
      ||
| 191 |  * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
 | 
      ||
| 192 |  */
 | 
      ||
| 193 | 21076167 | Thomas Schöpping | apalExitStatus_t l3g4200d_lld_read_int_config(const L3G4200DDriver* const l3gd, l3g4200d_lld_int_cfg_t* const cfg)  | 
      
| 194 | d6728c5b | Thomas Schöpping | {
 | 
      
| 195 |   apalDbgAssert(cfg != NULL);
 | 
      ||
| 196 | |||
| 197 | return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_INT1_CFG, cfg->data, 9);  | 
      ||
| 198 | }  | 
      ||
| 199 | |||
| 200 | /**
 | 
      ||
| 201 |  * @brief Write the interrupt configuration.
 | 
      ||
| 202 |  * @param[in]   spid        The SPI driver to use.
 | 
      ||
| 203 |  * @param[in]   cfg         The current new configuration.
 | 
      ||
| 204 |  *
 | 
      ||
| 205 |  * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
 | 
      ||
| 206 |  */
 | 
      ||
| 207 | 21076167 | Thomas Schöpping | apalExitStatus_t l3g4200d_lld_write_int_config(const L3G4200DDriver* const l3gd, const l3g4200d_lld_int_cfg_t cfg)  | 
      
| 208 | d6728c5b | Thomas Schöpping | {
 | 
      
| 209 |   apalExitStatus_t status = l3g4200d_lld_write_register(l3gd, L3G4200D_LLD_REGISTER_INT1_CFG, cfg.data, 1);
 | 
      ||
| 210 |   if (status != APAL_STATUS_OK) {
 | 
      ||
| 211 |     return status;
 | 
      ||
| 212 | }  | 
      ||
| 213 | return l3g4200d_lld_write_register(l3gd, L3G4200D_LLD_REGISTER_INT1_TSH_XH, &cfg.data[2], 7);  | 
      ||
| 214 | }  | 
      ||
| 215 | |||
| 216 | /**
 | 
      ||
| 217 |  * @brief Read the status register.
 | 
      ||
| 218 |  * @param[in]   spid        The SPI driver to use.
 | 
      ||
| 219 |  * @param[out]  status      The current status register.
 | 
      ||
| 220 |  *
 | 
      ||
| 221 |  * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
 | 
      ||
| 222 |  */
 | 
      ||
| 223 | 21076167 | Thomas Schöpping | apalExitStatus_t l3g4200d_lld_read_status_register(const L3G4200DDriver* const l3gd, uint8_t* const status)  | 
      
| 224 | d6728c5b | Thomas Schöpping | {
 | 
      
| 225 |   apalDbgAssert(status != NULL);
 | 
      ||
| 226 | |||
| 227 | return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_STATUS_REG, status, 1);  | 
      ||
| 228 | }  | 
      ||
| 229 | |||
| 230 | /**
 | 
      ||
| 231 |  * @brief Read the fifo ctrl register.
 | 
      ||
| 232 |  * @param[in]   spid        The SPI driver to use.
 | 
      ||
| 233 |  * @param[out]  fifo        The current fifo ctrl register.
 | 
      ||
| 234 |  *
 | 
      ||
| 235 |  * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
 | 
      ||
| 236 |  */
 | 
      ||
| 237 | inline apalExitStatus_t
 | 
      ||
| 238 | l3g4200d_lld_read_fifo_ctrl_register(const L3G4200DDriver* const l3gd, uint8_t* const fifo)  | 
      ||
| 239 | {
 | 
      ||
| 240 |   apalDbgAssert(fifo != NULL);
 | 
      ||
| 241 | |||
| 242 | return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_FIFO_CTRL_REG, fifo, 1);  | 
      ||
| 243 | }  | 
      ||
| 244 | |||
| 245 | /**
 | 
      ||
| 246 |  * @brief Write the fifo ctrl register.
 | 
      ||
| 247 |  * @param[in]   spid        The SPI driver to use.
 | 
      ||
| 248 |  * @param[in]   fifo        The new fifo ctrl register.
 | 
      ||
| 249 |  *
 | 
      ||
| 250 |  * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
 | 
      ||
| 251 |  */
 | 
      ||
| 252 | 21076167 | Thomas Schöpping | apalExitStatus_t l3g4200d_lld_write_fifo_ctrl_register(const L3G4200DDriver* const l3gd, const uint8_t fifo)  | 
      
| 253 | d6728c5b | Thomas Schöpping | {
 | 
      
| 254 | return l3g4200d_lld_write_register(l3gd, L3G4200D_LLD_REGISTER_FIFO_CTRL_REG, &fifo, 1);  | 
      ||
| 255 | }  | 
      ||
| 256 | |||
| 257 | /**
 | 
      ||
| 258 |  * @brief Read the fifo src register.
 | 
      ||
| 259 |  * @param[in]   spid        The SPI driver to use.
 | 
      ||
| 260 |  * @param[out]  fifo        The current fifo src register.
 | 
      ||
| 261 |  *
 | 
      ||
| 262 |  * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
 | 
      ||
| 263 |  */
 | 
      ||
| 264 | 21076167 | Thomas Schöpping | apalExitStatus_t l3g4200d_lld_read_fifo_src_register(const L3G4200DDriver* const l3gd, uint8_t* const fifo)  | 
      
| 265 | d6728c5b | Thomas Schöpping | {
 | 
      
| 266 |   apalDbgAssert(fifo != NULL);
 | 
      ||
| 267 | |||
| 268 | return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_FIFO_SRC_REG, fifo, 1);  | 
      ||
| 269 | }  | 
      ||
| 270 | |||
| 271 | /**
 | 
      ||
| 272 |  * @brief Read the interrupt src register.
 | 
      ||
| 273 |  * @param[in]   spid        The SPI driver to use.
 | 
      ||
| 274 |  * @param[out]  cfg         The current fifo src register.
 | 
      ||
| 275 |  *
 | 
      ||
| 276 |  * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
 | 
      ||
| 277 |  */
 | 
      ||
| 278 | 21076167 | Thomas Schöpping | apalExitStatus_t l3g4200d_lld_read_int_src(const L3G4200DDriver* const l3gd, uint8_t* const cfg)  | 
      
| 279 | d6728c5b | Thomas Schöpping | {
 | 
      
| 280 |   apalDbgAssert(cfg != NULL);
 | 
      ||
| 281 | |||
| 282 | return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_INT1_SRC, cfg, 1);  | 
      ||
| 283 | }  | 
      ||
| 284 | |||
| 285 | 5e2f673b | Marc Rothmann | /** @} */
 |