df220_defs.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * Copyright (C) 2012 - 2019 MiraMEMS
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. *
  10. * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * Neither the name of the copyright holder nor the names of the
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
  23. * OR CONTRIBUTORS BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  33. *
  34. * The information provided is believed to be accurate and reliable.
  35. * The copyright holder assumes no responsibility
  36. * for the consequences of use
  37. * of such information nor for any infringement of patents or
  38. * other rights of third parties which may result from its use.
  39. * No license is granted by implication or otherwise under any patent or
  40. * patent rights of the copyright holder.
  41. *
  42. * @file df220_defs.h
  43. * @date 17 Apr 2019
  44. * @version 0.0.1
  45. * @brief
  46. *
  47. */
  48. /*! \file df220_defs.h */
  49. /*!
  50. * @defgroup DF220 SENSOR API
  51. * @brief
  52. * @{
  53. */
  54. #ifndef DF220_DEFS_H_
  55. #define DF220_DEFS_H_
  56. /*********************************************************************/
  57. /**\ header files */
  58. #ifdef __KERNEL__
  59. #include <linux/types.h>
  60. #include <linux/kernel.h>
  61. #else
  62. #include <stdint.h>
  63. #include <stddef.h>
  64. #endif
  65. /*********************************************************************/
  66. /** \name Common macros */
  67. /*********************************************************************/
  68. #if !defined(UINT8_C) && !defined(INT8_C)
  69. #define INT8_C(x) S8_C(x)
  70. #define UINT8_C(x) U8_C(x)
  71. #endif
  72. #if !defined(UINT16_C) && !defined(INT16_C)
  73. #define INT16_C(x) S16_C(x)
  74. #define UINT16_C(x) U16_C(x)
  75. #endif
  76. #if !defined(INT32_C) && !defined(UINT32_C)
  77. #define INT32_C(x) S32_C(x)
  78. #define UINT32_C(x) U32_C(x)
  79. #endif
  80. #if !defined(INT64_C) && !defined(UINT64_C)
  81. #define INT64_C(x) S64_C(x)
  82. #define UINT64_C(x) U64_C(x)
  83. #endif
  84. /**\name C standard macros */
  85. #ifndef NULL
  86. #ifdef __cplusplus
  87. #define NULL 0
  88. #else
  89. #define NULL ((void *) 0)
  90. #endif
  91. #endif
  92. #ifndef TRUE
  93. #define TRUE UINT8_C(1)
  94. #endif
  95. #ifndef FALSE
  96. #define FALSE UINT8_C(0)
  97. #endif
  98. /********************************************************/
  99. /**\name Macro definitions */
  100. /**\name API success code */
  101. #define DF220_OK INT8_C(0)
  102. /**\name API error codes */
  103. #define DF220_E_NULL_PTR INT8_C(-1)
  104. #define DF220_E_COM_FAIL INT8_C(-2)
  105. #define DF220_E_DEV_NOT_FOUND INT8_C(-3)
  106. #define DF220_E_INVALID_CONFIG INT8_C(-4)
  107. /**\name API warning codes */
  108. #define DF220_W_SELF_TEST_FAIL INT8_C(1)
  109. /**\name CHIP ID VALUE */
  110. #define DF220_CHIP_ID UINT8_C(0x13)
  111. /********************************************************/
  112. /**\name USER CONFIGURATION MACROS */
  113. /**\name Power mode configurations */
  114. #define DF220_NORMAL_MODE UINT8_C(0)
  115. #define DF220_SLEEP_MODE UINT8_C(1)
  116. #define DF220_LOW_POWER_MODE UINT8_C(0)
  117. /**\name Enable / Disable macros */
  118. #define DF220_DISABLE UINT8_C(0)
  119. #define DF220_ENABLE UINT8_C(1)
  120. /**\name Data/sensortime selection macros */
  121. #define DF220_DATA_ONLY UINT8_C(0x00)
  122. #define DF220_DATA_SENSOR_TIME UINT8_C(0x01)
  123. /**\name ODR configurations */
  124. #define DF220_ODR_1_HZ UINT8_C(0x00)
  125. #define DF220_ODR_1_95HZ UINT8_C(0x01)
  126. #define DF220_ODR_3_9HZ UINT8_C(0x02)
  127. #define DF220_ODR_7_81HZ UINT8_C(0x03)
  128. #define DF220_ODR_15_63HZ UINT8_C(0x04)
  129. #define DF220_ODR_31_25HZ UINT8_C(0x05)
  130. #define DF220_ODR_62_5HZ UINT8_C(0x06)
  131. #define DF220_ODR_125HZ UINT8_C(0x07)
  132. #define DF220_ODR_250HZ UINT8_C(0x08)
  133. #define DF220_ODR_500HZ UINT8_C(0x09)
  134. #define DF220_ODR_1000HZ UINT8_C(0x0A)
  135. /**\name Force sensor Range configuration */
  136. #define DF220_2G_RANGE UINT8_C(0x00)
  137. #define DF220_4G_RANGE UINT8_C(0x01)
  138. #define DF220_8G_RANGE UINT8_C(0x02)
  139. #define DF220_16G_RANGE UINT8_C(0x03)
  140. /**\name Force sensor bw settings */
  141. /* Force bw = 0.50 * ODR */
  142. #define DF220_FORCE_FILT1_BW_0 UINT8_C(0x00)
  143. /* Force bw = 0.25 * ODR */
  144. #define DF220_FORCE_FILT1_BW_1 UINT8_C(0x01)
  145. /* Force bw = 0.10 * ODR */
  146. #define DF220_FORCE_FILT1_BW_2 UINT8_C(0x10)
  147. /**\name Auto wake-up timeout value of 10.24s */
  148. #define DF220_AUTO_WAKEUP_TIMEOUT_MAX UINT16_C(0x0FFF)
  149. /**\name Auto low power timeout value of 10.24s */
  150. #define DF220_AUTO_LP_TIMEOUT_MAX UINT16_C(0x0FFF)
  151. /**********************************************************************/
  152. /**\name DF220 Register Address */
  153. #define DF220_CHIP_ID_ADDR UINT8_C(0x01)
  154. #define DF220_FORCE_DATA_ADDR UINT8_C(0x06)
  155. #define DF220_INT_STAT0_ADDR UINT8_C(0x0E)
  156. #define DF220_FORCE_MODE_BW UINT8_C(0x11)
  157. #define DF220_FORCE_LSB_RANGE UINT8_C(0x0F)
  158. #define DF220_INT_MAP1_ADDR UINT8_C(0x19)
  159. #define DF220_INT_MAP2_ADDR UINT8_C(0x1A)
  160. #define DF220_INT_MAP3_ADDR UINT8_C(0x1B)
  161. #define DF220_SPI_CONFIG UINT8_C(0x00)
  162. /**\name DF220 Command register */
  163. #define DF220_SOFT_RESET_CMD UINT8_C(0x24)
  164. #define DF220_SOFT_RESET_CMD_MSK DF220_SOFT_RESET_CMD
  165. /**\name DF220 Delay definitions */
  166. #define DF220_SOFT_RESET_DELAY_MS UINT8_C(10)
  167. #define DF220_SELF_TEST_DELAY_MS UINT8_C(7)
  168. #define DF220_SELF_TEST_DATA_READ_MS UINT8_C(50)
  169. /**\name Interface selection macro */
  170. #define DF220_SPI_WR_MASK UINT8_C(0x7F)
  171. #define DF220_SPI_RD_MASK UINT8_C(0x80)
  172. /**\name DF220 bit mask definitions */
  173. #define DF220_POWER_MODE_STATUS_MSK UINT8_C(0x06)
  174. #define DF220_POWER_MODE_STATUS_POS UINT8_C(1)
  175. #define DF220_POWER_MODE_MSK UINT8_C(0x80)
  176. #define DF220_POWER_MODE_POS UINT8_C(7)
  177. #define DF220_FORCEL_ODR_MSK UINT8_C(0x0F)
  178. #define DF220_FORCE_RANGE_MSK UINT8_C(0x03)
  179. #define DF220_FORCE_RANGE_POS UINT8_C(0)
  180. #define DF220_DATA_FILTER_MSK UINT8_C(0x0C)
  181. #define DF220_DATA_FILTER_POS UINT8_C(2)
  182. #define DF220_BW_MSK UINT8_C(0x06)
  183. #define DF220_BW_POS UINT8_C(1)
  184. /**\name Macro to SET and GET BITS of a register */
  185. #define DF220_SET_BITS(reg_data, bitname, data) \
  186. ((reg_data & ~(bitname ## _MSK)) | \
  187. ((data << bitname ## _POS) & bitname ## _MSK))
  188. #define DF220_GET_BITS(reg_data, bitname) ((reg_data & (bitname ## _MSK)) >> \
  189. (bitname ## _POS))
  190. #define DF220_SET_BITS_POS_0(reg_data, bitname, data) \
  191. ((reg_data & ~(bitname ## _MSK)) | \
  192. (data & bitname ## _MSK))
  193. #define DF220_GET_BITS_POS_0(reg_data, bitname) (reg_data & (bitname ## _MSK))
  194. /********************************************************/
  195. /*!
  196. * @brief Interface selection enums
  197. */
  198. enum df220_intf {
  199. /*! SPI interface */
  200. DF220_SPI_INTF,
  201. /*! I2C interface */
  202. DF220_I2C_INTF
  203. };
  204. /********************************************************/
  205. /**\name TYPE DEFINITIONS */
  206. /*!
  207. * @brief Bus communication function pointer which should be mapped to
  208. * the platform specific read and write functions of the user
  209. */
  210. typedef int8_t (*df220_com_fptr_t)(void *intf_ptr, uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data,
  211. uint16_t length);
  212. /*! Delay function pointer */
  213. typedef void (*df220_delay_fptr_t)(uint32_t period);
  214. /********************************************************/
  215. /**\name STRUCTURE DEFINITIONS*/
  216. /*!
  217. * @brief Sensor selection enums
  218. */
  219. enum df220_sensor {
  220. DF220_FORCE
  221. };
  222. /*!
  223. * @brief Force sensor basic configuration
  224. */
  225. struct df220_force_conf {
  226. /*! Output data rate
  227. * Assignable macros :
  228. * - DF220_ODR_1_HZ - DF220_ODR_1_95HZ
  229. * - DF220_ODR_3_9HZ - DF220_ODR_7_81HZ - DF220_ODR_15_63HZ
  230. * - DF220_ODR_31_25HZ - DF220_ODR_62_5HZ - DF220_ODR_125HZ
  231. * - DF220_ODR_250HZ - DF220_ODR_500HZ - DF220_ODR_1000HZ
  232. */
  233. uint8_t odr;
  234. /*! Range of sensor
  235. * Assignable macros :
  236. * - DF220_2G_RANGE - DF220_8G_RANGE
  237. * - DF220_4G_RANGE - DF220_16G_RANGE
  238. */
  239. uint8_t range;
  240. /*! Filter 1 Bandwidth
  241. * Assignable macros :
  242. * - DF220_FORCE_FILT1_BW_0
  243. * - DF220_FORCE_FILT1_BW_1
  244. * - DF220_FORCE_FILT1_BW_2
  245. */
  246. uint8_t bw;
  247. };
  248. /*!
  249. * @brief Union of sensor Configurations
  250. */
  251. union df220_set_param {
  252. /* Force sensor configurations */
  253. struct df220_force_conf force;
  254. };
  255. /*!
  256. * @brief Sensor selection and their configurations
  257. */
  258. struct df220_sensor_conf {
  259. /* Sensor selection */
  260. enum df220_sensor type;
  261. /* Sensor configuration */
  262. union df220_set_param param;
  263. };
  264. /*!
  265. * @brief DF220 sensor data
  266. */
  267. struct df220_sensor_data {
  268. /*! Force sensor data */
  269. int16_t f;
  270. /*! sensor time */
  271. uint32_t sensortime;
  272. };
  273. /*!
  274. * @brief df220 device structure
  275. */
  276. struct df220_dev {
  277. /*! Chip Id */
  278. uint8_t chip_id;
  279. /*! Device Id */
  280. uint8_t dev_id;
  281. /*! SPI/I2C Interface selection */
  282. enum df220_intf intf;
  283. /*! Interface handle pointer */
  284. void *intf_ptr;
  285. /*! Decide SPI or I2C read mechanism */
  286. uint8_t dummy_byte;
  287. /*! Bus read function pointer */
  288. df220_com_fptr_t read;
  289. /*! Bus write function pointer */
  290. df220_com_fptr_t write;
  291. /*! delay(in ms) function pointer */
  292. df220_delay_fptr_t delay_ms;
  293. };
  294. #endif /* DF220_DEFS_H_ */
  295. /** @}*/
  296. /** @}*/