Icm20948Transport.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * ________________________________________________________________________________________________________
  3. * Copyright (c) 2015-2015 InvenSense Inc. All rights reserved.
  4. *
  5. * This software, related documentation and any modifications thereto (collectively “Software”) is subject
  6. * to InvenSense and its licensors' intellectual property rights under U.S. and international copyright
  7. * and other intellectual property rights laws.
  8. *
  9. * InvenSense and its licensors retain all intellectual property and proprietary rights in and to the Software
  10. * and any use, reproduction, disclosure or distribution of the Software without an express license agreement
  11. * from InvenSense is strictly prohibited.
  12. *
  13. * EXCEPT AS OTHERWISE PROVIDED IN A LICENSE AGREEMENT BETWEEN THE PARTIES, THE SOFTWARE IS
  14. * PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  15. * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  16. * EXCEPT AS OTHERWISE PROVIDED IN A LICENSE AGREEMENT BETWEEN THE PARTIES, IN NO EVENT SHALL
  17. * INVENSENSE BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY
  18. * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  19. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THE SOFTWARE.
  21. * ________________________________________________________________________________________________________
  22. */
  23. /** @defgroup DriverIcm20948Transport Icm20948 driver transport
  24. * @brief Low-level ICM20948 register access
  25. * @ingroup DriverIcm20948
  26. * @{
  27. */
  28. #ifndef _INV_ICM20948_TRANSPORT_H_
  29. #define _INV_ICM20948_TRANSPORT_H_
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #include "InvExport.h"
  34. #include <stdint.h>
  35. /* forward declaration */
  36. struct inv_icm20948;
  37. /** @brief Max size that can be read across I2C or SPI data lines */
  38. #define INV_MAX_SERIAL_READ 16
  39. /** @brief Max size that can be written across I2C or SPI data lines */
  40. #define INV_MAX_SERIAL_WRITE 16
  41. void INV_EXPORT inv_icm20948_transport_init(struct inv_icm20948 * s);
  42. int INV_EXPORT inv_icm20948_read_reg(struct inv_icm20948 * s, uint8_t reg, uint8_t * buf, uint32_t len);
  43. int INV_EXPORT inv_icm20948_write_reg(struct inv_icm20948 * s, uint8_t reg, const uint8_t * buf, uint32_t len);
  44. void INV_EXPORT inv_icm20948_sleep_100us(unsigned long nHowMany100MicroSecondsToSleep);
  45. long INV_EXPORT inv_icm20948_get_tick_count(void);
  46. static inline int inv_icm20948_write_reg_one(struct inv_icm20948 * s, uint8_t reg, uint8_t reg_value)
  47. {
  48. return inv_icm20948_write_reg(s, reg, &reg_value, 1);
  49. }
  50. static inline int inv_icm20948_read_reg_one(struct inv_icm20948 * s, uint8_t reg, uint8_t * reg_value)
  51. {
  52. return inv_icm20948_read_reg(s, reg, reg_value, 1);
  53. }
  54. static inline int inv_icm20948_set_reg_bits(struct inv_icm20948 * s, uint8_t reg, uint8_t bits_mask)
  55. {
  56. int rc;
  57. uint8_t reg_value;
  58. if((rc = inv_icm20948_read_reg_one(s, reg, &reg_value)) != 0)
  59. return rc;
  60. reg_value |= bits_mask;
  61. if((rc = inv_icm20948_write_reg_one(s, reg, reg_value)) != 0)
  62. return rc;
  63. return 0;
  64. }
  65. static inline int inv_icm20948_clear_reg_bits(struct inv_icm20948 * s, uint8_t reg, uint8_t bits_mask)
  66. {
  67. int rc;
  68. uint8_t reg_value;
  69. if((rc = inv_icm20948_read_reg_one(s, reg, &reg_value)) != 0)
  70. return rc;
  71. reg_value &= ~bits_mask;
  72. if((rc = inv_icm20948_write_reg_one(s, reg, reg_value)) != 0)
  73. return rc;
  74. return 0;
  75. }
  76. static inline int inv_icm20948_get_reg_bits(struct inv_icm20948 * s, uint8_t reg,
  77. uint8_t bits_mask, uint8_t * bits_mask_state)
  78. {
  79. int rc;
  80. if((rc = inv_icm20948_read_reg_one(s, reg, bits_mask_state)) != 0)
  81. return rc;
  82. *bits_mask_state &= bits_mask;
  83. return 0;
  84. }
  85. /**
  86. * @brief Write data to a register on MEMs.
  87. * @param[in] Register address
  88. * @param[in] Length of data
  89. * @param[in] Data to be written
  90. * @return 0 if successful.
  91. */
  92. int INV_EXPORT inv_icm20948_write_mems_reg(struct inv_icm20948 * s, uint16_t reg, unsigned int length, const unsigned char *data);
  93. /**
  94. * @brief Write single byte of data to a register on MEMs.
  95. * @param[in] Register address
  96. * @param[in] Data to be written
  97. * @return 0 if successful.
  98. */
  99. int INV_EXPORT inv_icm20948_write_single_mems_reg(struct inv_icm20948 * s, uint16_t reg, const unsigned char data);
  100. /**
  101. * @brief Read data from a register on MEMs.
  102. * @param[in] Register address
  103. * @param[in] Length of data
  104. * @param[in] Data to be written
  105. * @return 0 if successful.
  106. */
  107. int INV_EXPORT inv_icm20948_read_mems_reg(struct inv_icm20948 * s, uint16_t reg, unsigned int length, unsigned char *data);
  108. /**
  109. * @brief Read data from a register in DMP memory
  110. * @param[in] DMP memory address
  111. * @param[in] number of byte to be read
  112. * @param[in] input data from the register
  113. * @return 0 if successful.
  114. */
  115. int INV_EXPORT inv_icm20948_read_mems(struct inv_icm20948 * s, unsigned short reg, unsigned int length, unsigned char *data);
  116. /**
  117. * @brief Write data to a register in DMP memory
  118. * @param[in] DMP memory address
  119. * @param[in] number of byte to be written
  120. * @param[out] output data from the register
  121. * @return 0 if successful.
  122. */
  123. int INV_EXPORT inv_icm20948_write_mems(struct inv_icm20948 * s, unsigned short reg, unsigned int length, const unsigned char *data);
  124. /** @brief Writes a single byte of data from a register on mems with no power control
  125. * @param[in] reg DMP memory address
  126. * @param[out] data Data to be written
  127. * @return 0 in case of success, -1 for any error
  128. */
  129. int INV_EXPORT inv_icm20948_write_single_mems_reg_core(struct inv_icm20948 * s, uint16_t reg, const uint8_t data);
  130. #ifdef __cplusplus
  131. }
  132. #endif
  133. #endif /* _INV_ICM20948_TRANSPORT_H_ */
  134. /** @} */