lsm303m.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. $License:
  3. Copyright (C) 2010 InvenSense Corporation, All Rights Reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. $
  15. */
  16. /**
  17. * @brief Provides the interface to setup and handle a compass
  18. * connected to the primary I2C interface of the gyroscope.
  19. *
  20. * @{
  21. * @file lsm303m.c
  22. * @brief Magnetometer setup and handling methods for ST LSM303.
  23. */
  24. /* ------------------ */
  25. /* - Include Files. - */
  26. /* ------------------ */
  27. #ifdef __KERNEL__
  28. #include <linux/module.h>
  29. #endif
  30. #include "mpu.h"
  31. #include "mlsl.h"
  32. #include "mlos.h"
  33. #include <log.h>
  34. #undef MPL_LOG_TAG
  35. #define MPL_LOG_TAG "MPL-compass"
  36. /*----- ST LSM303 Registers ------*/
  37. enum LSM_REG {
  38. LSM_REG_CONF_A = 0x0,
  39. LSM_REG_CONF_B = 0x1,
  40. LSM_REG_MODE = 0x2,
  41. LSM_REG_X_M = 0x3,
  42. LSM_REG_X_L = 0x4,
  43. LSM_REG_Z_M = 0x5,
  44. LSM_REG_Z_L = 0x6,
  45. LSM_REG_Y_M = 0x7,
  46. LSM_REG_Y_L = 0x8,
  47. LSM_REG_STATUS = 0x9,
  48. LSM_REG_ID_A = 0xA,
  49. LSM_REG_ID_B = 0xB,
  50. LSM_REG_ID_C = 0xC
  51. };
  52. enum LSM_CONF_A {
  53. LSM_CONF_A_DRATE_MASK = 0x1C,
  54. LSM_CONF_A_DRATE_0_75 = 0x00,
  55. LSM_CONF_A_DRATE_1_5 = 0x04,
  56. LSM_CONF_A_DRATE_3 = 0x08,
  57. LSM_CONF_A_DRATE_7_5 = 0x0C,
  58. LSM_CONF_A_DRATE_15 = 0x10,
  59. LSM_CONF_A_DRATE_30 = 0x14,
  60. LSM_CONF_A_DRATE_75 = 0x18,
  61. LSM_CONF_A_MEAS_MASK = 0x3,
  62. LSM_CONF_A_MEAS_NORM = 0x0,
  63. LSM_CONF_A_MEAS_POS = 0x1,
  64. LSM_CONF_A_MEAS_NEG = 0x2
  65. };
  66. enum LSM_CONF_B {
  67. LSM_CONF_B_GAIN_MASK = 0xE0,
  68. LSM_CONF_B_GAIN_0_9 = 0x00,
  69. LSM_CONF_B_GAIN_1_2 = 0x20,
  70. LSM_CONF_B_GAIN_1_9 = 0x40,
  71. LSM_CONF_B_GAIN_2_5 = 0x60,
  72. LSM_CONF_B_GAIN_4_0 = 0x80,
  73. LSM_CONF_B_GAIN_4_6 = 0xA0,
  74. LSM_CONF_B_GAIN_5_5 = 0xC0,
  75. LSM_CONF_B_GAIN_7_9 = 0xE0
  76. };
  77. enum LSM_MODE {
  78. LSM_MODE_MASK = 0x3,
  79. LSM_MODE_CONT = 0x0,
  80. LSM_MODE_SINGLE = 0x1,
  81. LSM_MODE_IDLE = 0x2,
  82. LSM_MODE_SLEEP = 0x3
  83. };
  84. /* --------------------- */
  85. /* - Variables. - */
  86. /* --------------------- */
  87. /*****************************************
  88. Accelerometer Initialization Functions
  89. *****************************************/
  90. int lsm303dlhm_suspend(void *mlsl_handle,
  91. struct ext_slave_descr *slave,
  92. struct ext_slave_platform_data *pdata)
  93. {
  94. int result = ML_SUCCESS;
  95. result =
  96. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  97. LSM_REG_MODE, LSM_MODE_SLEEP);
  98. ERROR_CHECK(result);
  99. MLOSSleep(3);
  100. return result;
  101. }
  102. int lsm303dlhm_resume(void *mlsl_handle,
  103. struct ext_slave_descr *slave,
  104. struct ext_slave_platform_data *pdata)
  105. {
  106. int result = ML_SUCCESS;
  107. /* Use single measurement mode. Start at sleep state. */
  108. result =
  109. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  110. LSM_REG_MODE, LSM_MODE_SLEEP);
  111. ERROR_CHECK(result);
  112. /* Config normal measurement */
  113. result =
  114. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  115. LSM_REG_CONF_A, 0);
  116. ERROR_CHECK(result);
  117. /* Adjust gain to 320 LSB/Gauss */
  118. result =
  119. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  120. LSM_REG_CONF_B, LSM_CONF_B_GAIN_5_5);
  121. ERROR_CHECK(result);
  122. return result;
  123. }
  124. int lsm303dlhm_read(void *mlsl_handle,
  125. struct ext_slave_descr *slave,
  126. struct ext_slave_platform_data *pdata,
  127. unsigned char *data)
  128. {
  129. unsigned char stat;
  130. tMLError result = ML_SUCCESS;
  131. short axisFixed;
  132. /* Read status reg. to check if data is ready */
  133. result =
  134. MLSLSerialRead(mlsl_handle, pdata->address, LSM_REG_STATUS, 1,
  135. &stat);
  136. ERROR_CHECK(result);
  137. if (stat & 0x01) {
  138. result =
  139. MLSLSerialRead(mlsl_handle, pdata->address,
  140. LSM_REG_X_M, 6, (unsigned char *) data);
  141. ERROR_CHECK(result);
  142. /*drop data if overflows */
  143. if ((data[0] == 0xf0) || (data[2] == 0xf0)
  144. || (data[4] == 0xf0)) {
  145. /* trigger next measurement read */
  146. result =
  147. MLSLSerialWriteSingle(mlsl_handle,
  148. pdata->address,
  149. LSM_REG_MODE,
  150. LSM_MODE_SINGLE);
  151. ERROR_CHECK(result);
  152. return ML_ERROR_COMPASS_DATA_OVERFLOW;
  153. }
  154. /* convert to fixed point and apply sensitivity correction for
  155. Z-axis */
  156. axisFixed =
  157. (short) ((unsigned short) data[5] +
  158. (unsigned short) data[4] * 256);
  159. /* scale up by 1.125 (36/32) approximate of 1.122 (320/285) */
  160. axisFixed = (short) (axisFixed * 36);
  161. data[4] = axisFixed >> 8;
  162. data[5] = axisFixed & 0xFF;
  163. axisFixed =
  164. (short) ((unsigned short) data[3] +
  165. (unsigned short) data[2] * 256);
  166. axisFixed = (short) (axisFixed * 32);
  167. data[2] = axisFixed >> 8;
  168. data[3] = axisFixed & 0xFF;
  169. axisFixed =
  170. (short) ((unsigned short) data[1] +
  171. (unsigned short) data[0] * 256);
  172. axisFixed = (short) (axisFixed * 32);
  173. data[0] = axisFixed >> 8;
  174. data[1] = axisFixed & 0xFF;
  175. /* trigger next measurement read */
  176. result =
  177. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  178. LSM_REG_MODE, LSM_MODE_SINGLE);
  179. ERROR_CHECK(result);
  180. return ML_SUCCESS;
  181. } else {
  182. /* trigger next measurement read */
  183. result =
  184. MLSLSerialWriteSingle(mlsl_handle, pdata->address,
  185. LSM_REG_MODE, LSM_MODE_SINGLE);
  186. ERROR_CHECK(result);
  187. return ML_ERROR_COMPASS_DATA_NOT_READY;
  188. }
  189. }
  190. struct ext_slave_descr lsm303dlhm_descr = {
  191. /*.init = */ NULL,
  192. /*.exit = */ NULL,
  193. /*.suspend = */ lsm303dlhm_suspend,
  194. /*.resume = */ lsm303dlhm_resume,
  195. /*.read = */ lsm303dlhm_read,
  196. /*.config = */ NULL,
  197. /*.get_config = */ NULL,
  198. /*.name = */ "lsm303dlhm",
  199. /*.type = */ EXT_SLAVE_TYPE_COMPASS,
  200. /*.id = */ COMPASS_ID_LSM303,
  201. /*.reg = */ 0x06,
  202. /*.len = */ 6,
  203. /*.endian = */ EXT_SLAVE_BIG_ENDIAN,
  204. /*.range = */ {10240, 0},
  205. };
  206. struct ext_slave_descr *lsm303dlhm_get_slave_descr(void)
  207. {
  208. return &lsm303dlhm_descr;
  209. }
  210. EXPORT_SYMBOL(lsm303dlhm_get_slave_descr);
  211. /**
  212. * @}
  213. **/