yas53x.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2012-2013 Yamaha Corporation
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. */
  20. #ifndef __YAS53X_H__
  21. #define __YAS53X_H__
  22. /* ----------------------------------------------------------------------------
  23. * Macro definition
  24. *--------------------------------------------------------------------------- */
  25. #define YAS_VERSION "1.0.3"
  26. #if defined(__KERNEL__)
  27. #include <linux/types.h>
  28. #else
  29. #include <stdint.h>
  30. #endif
  31. #ifndef NULL
  32. #define NULL ((void *)(0))
  33. #endif
  34. #define YAS_NO_ERROR (0)
  35. #define YAS_ERROR_DEVICE_COMMUNICATION (-1)
  36. #define YAS_ERROR_I2C YAS_ERROR_DEVICE_COMMUNICATION
  37. #define YAS_ERROR_POWER (-2)
  38. #define YAS_ERROR_TEST_ORDER (-3)
  39. #define YAS_ERROR_NOT_INITIALIZED YAS_ERROR_TEST_ORDER
  40. #define YAS_ERROR_INTERRUPT (-4)
  41. #define YAS_ERROR_BUSY (-5)
  42. #define YAS_ERROR_OVERFLOW (-6)
  43. #define YAS_ERROR_UNDERFLOW (-7)
  44. #define YAS_ERROR_DIRCALC (-8)
  45. #define YAS_ERROR_NOT_SUPPORTED (-9)
  46. #define YAS_ERROR_CALREG (-10)
  47. #define YAS_ERROR_CHIP_ID (-11)
  48. #define YAS_ERROR_ARG (-128)
  49. #define YAS_X_OVERFLOW (0x01)
  50. #define YAS_X_UNDERFLOW (0x02)
  51. #define YAS_Y1_OVERFLOW (0x04)
  52. #define YAS_Y1_UNDERFLOW (0x08)
  53. #define YAS_Y2_OVERFLOW (0x10)
  54. #define YAS_Y2_UNDERFLOW (0x20)
  55. #define YAS_REPORT_HARD_OFFSET_CHANGED (0x40)
  56. #define YAS_OVERFLOW (YAS_X_OVERFLOW|YAS_Y1_OVERFLOW|YAS_Y2_OVERFLOW)
  57. #define YAS_UNDERFLOW (YAS_X_UNDERFLOW|YAS_Y1_UNDERFLOW|YAS_Y2_UNDERFLOW)
  58. #define NELEMS(a) ((int)(sizeof(a)/sizeof(a[0])))
  59. /* ----------------------------------------------------------------------------
  60. * Configuration
  61. *--------------------------------------------------------------------------- */
  62. #define YAS_DEFAULT_FILTER_LEN (20)
  63. #define YAS_DEFAULT_FILTER_THRESH (300) /* 300 nT */
  64. #define YAS_DEFAULT_FILTER_NOISE (12*12) /* standard deviation 1200 nT */
  65. #define YAS_VCORE (18)
  66. /* ----------------------------------------------------------------------------
  67. * Structure definition
  68. *--------------------------------------------------------------------------- */
  69. struct yas_vector {
  70. int32_t v[3];
  71. };
  72. struct yas_matrix {
  73. int32_t m[9];
  74. };
  75. struct yas_quaternion {
  76. int32_t q[4];
  77. };
  78. struct yas_acc_data {
  79. struct yas_vector xyz;
  80. int16_t raw[3];
  81. };
  82. struct yas_acc_driver_callback {
  83. int (*device_open)(void);
  84. int (*device_close)(void);
  85. int (*device_write)(uint8_t addr, const uint8_t *buf, int len);
  86. int (*device_read)(uint8_t addr, uint8_t *buf, int len);
  87. void (*msleep)(int msec);
  88. };
  89. struct yas_acc_driver {
  90. int (*init)(void);
  91. int (*term)(void);
  92. int (*get_delay)(void);
  93. int (*set_delay)(int delay);
  94. int (*get_enable)(void);
  95. int (*set_enable)(int enable);
  96. int (*get_position)(void);
  97. int (*set_position)(int position);
  98. int (*measure)(struct yas_acc_data *data);
  99. struct yas_acc_driver_callback callback;
  100. };
  101. struct yas_mag_data {
  102. struct yas_vector xyz;
  103. int16_t xy1y2[3];
  104. int32_t xy1y2_linear[3];
  105. int16_t temperature;
  106. };
  107. struct yas_mag_driver_callback {
  108. int (*device_open)(void);
  109. int (*device_close)(void);
  110. int (*device_write)(uint8_t addr, const uint8_t *buf, int len);
  111. int (*device_read)(uint8_t addr, uint8_t *buf, int len);
  112. void (*msleep)(int msec);
  113. void (*current_time)(uint32_t *msec);
  114. };
  115. struct yas_self_test_result {
  116. int32_t id;
  117. int8_t xy1y2[3];
  118. int32_t dir;
  119. int32_t sx, sy;
  120. int32_t xyz[3];
  121. };
  122. struct yas_mag_driver {
  123. int (*init)(void);
  124. int (*term)(void);
  125. int (*self_test)(struct yas_self_test_result *r);
  126. int (*self_test_noise)(struct yas_vector *raw_xyz);
  127. int (*get_offset)(int8_t *offset);
  128. int (*set_offset)(const int8_t *offset);
  129. int (*get_position)(void);
  130. int (*set_position)(int position);
  131. int (*measure)(struct yas_mag_data *data);
  132. struct yas_mag_driver_callback callback;
  133. };
  134. struct yas_mag_filter {
  135. int (*init)(void);
  136. int (*filter)(struct yas_vector *input, struct yas_vector *filtered);
  137. };
  138. #undef EVAL
  139. #ifdef EVAL
  140. struct yas_mag_calibration_result {
  141. int32_t spread;
  142. int32_t variation;
  143. int32_t radius;
  144. int8_t axis;
  145. int8_t level;
  146. int8_t accuracy;
  147. struct yas_matrix dynamic_matrix;
  148. };
  149. struct yas_mag_calibration_threshold {
  150. int32_t spread;
  151. int32_t variation[3];
  152. };
  153. #endif
  154. struct yas_mag_calibration {
  155. int (*init)(void);
  156. #ifdef EVAL
  157. int (*update)(struct yas_vector *mag,
  158. struct yas_mag_calibration_result *r);
  159. #else
  160. int (*update)(struct yas_vector *mag, struct yas_vector *calibrated,
  161. int *accuracy);
  162. #endif
  163. int (*get_offset)(struct yas_vector *offset);
  164. int (*set_offset)(struct yas_vector *offset);
  165. int (*get_static_matrix)(struct yas_matrix *matrix);
  166. int (*set_static_matrix)(struct yas_matrix *matrix);
  167. int (*get_accuracy)(void);
  168. int (*set_accuracy)(int accuracy);
  169. };
  170. struct yas_util {
  171. int (*get_euler)(struct yas_vector *acc, struct yas_vector *mag,
  172. struct yas_vector *euler);
  173. };
  174. /* ----------------------------------------------------------------------------
  175. * Global function definition
  176. *--------------------------------------------------------------------------- */
  177. int yas_acc_driver_init(struct yas_acc_driver *f);
  178. int yas_mag_driver_init(struct yas_mag_driver *f);
  179. int yas_mag_filter_init(struct yas_mag_filter *f);
  180. int yas_mag_calibration_init(struct yas_mag_calibration *f);
  181. int yas_util_init(struct yas_util *f);
  182. #endif /* __YAS53X_H__ */