lis2dh12.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Copyright (c) 2017 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "lis2dh12.h"
  41. #define RETURN_IF_ERR(err) \
  42. if (err != NRF_SUCCESS) \
  43. { \
  44. return err; \
  45. }
  46. ret_code_t lis2dh12_init(lis2dh12_instance_t * p_inst)
  47. {
  48. ASSERT(p_inst != NULL);
  49. memset(&p_inst->temp_cfg, 0, &p_inst->act_dur - &p_inst->temp_cfg);
  50. p_inst->ctrl0 = LIS2DH12_CTRL_REG0_VALID_SET;
  51. p_inst->ctrl1 = 0x07;
  52. p_inst->ctrl4 = 0x80;
  53. return lis2dh12_cfg_commit(p_inst);
  54. }
  55. ret_code_t lis2dh12_cfg_commit(lis2dh12_instance_t * p_inst)
  56. {
  57. ASSERT(p_inst != NULL);
  58. ret_code_t err;
  59. p_inst->ctrl0 &= ~LIS2DH12_CTRL_REG0_VALID_MASK;
  60. p_inst->ctrl0 |= LIS2DH12_CTRL_REG0_VALID_SET;
  61. uint8_t ctrl_msg[] = {
  62. LIS2DH12_REG_CTRL_REG0 | LIS2DH12_AUTO_INCR_MASK,
  63. p_inst->ctrl0,
  64. p_inst->temp_cfg,
  65. p_inst->ctrl1,
  66. p_inst->ctrl2,
  67. p_inst->ctrl3,
  68. p_inst->ctrl4,
  69. p_inst->ctrl5,
  70. p_inst->ctrl6,
  71. p_inst->reference
  72. };
  73. err = nrf_twi_sensor_write(p_inst->p_sensor_data,
  74. p_inst->sensor_addr,
  75. ctrl_msg,
  76. ARRAY_SIZE(ctrl_msg),
  77. true);
  78. RETURN_IF_ERR(err);
  79. uint8_t fifo_msg[] = {
  80. LIS2DH12_REG_FIFO_CTRL | LIS2DH12_AUTO_INCR_MASK,
  81. p_inst->fifo_ctrl,
  82. 0,
  83. p_inst->int1_cfg,
  84. 0,
  85. p_inst->int1_ths,
  86. p_inst->int1_dur,
  87. p_inst->int2_cfg,
  88. 0,
  89. p_inst->int2_ths,
  90. p_inst->int2_dur,
  91. p_inst->click_cfg
  92. };
  93. err = nrf_twi_sensor_write(p_inst->p_sensor_data,
  94. p_inst->sensor_addr,
  95. fifo_msg,
  96. ARRAY_SIZE(fifo_msg),
  97. true);
  98. RETURN_IF_ERR(err);
  99. uint8_t time_msg[] = {
  100. LIS2DH12_REG_CLICK_THS | LIS2DH12_AUTO_INCR_MASK,
  101. p_inst->click_ths,
  102. p_inst->time_lim,
  103. p_inst->latency,
  104. p_inst->time_win,
  105. p_inst->act_ths,
  106. p_inst->act_dur
  107. };
  108. err = nrf_twi_sensor_write(p_inst->p_sensor_data,
  109. p_inst->sensor_addr,
  110. time_msg,
  111. ARRAY_SIZE(time_msg),
  112. true);
  113. return err;
  114. }
  115. ret_code_t lis2dh12_data_read(lis2dh12_instance_t * p_inst,
  116. lis2dh12_data_cb_t user_cb,
  117. lis2dh12_data_t * p_data,
  118. uint8_t samples)
  119. {
  120. ASSERT(p_inst != NULL);
  121. return nrf_twi_sensor_reg_read(p_inst->p_sensor_data,
  122. p_inst->sensor_addr,
  123. LIS2DH12_REG_OUT_X_L | LIS2DH12_AUTO_INCR_MASK,
  124. (nrf_twi_sensor_reg_cb_t) user_cb,
  125. (uint8_t *) p_data,
  126. samples * LIS2DH12_BYTES_PER_SAMPLE);
  127. }
  128. ret_code_t lis2dh12_temp_enable(lis2dh12_instance_t * p_inst, bool temp_en)
  129. {
  130. ASSERT(p_inst != NULL);
  131. if (temp_en == true)
  132. {
  133. NRF_TWI_SENSOR_REG_SET(p_inst->temp_cfg, LIS2DH12_TEMP_EN_MASK, LIS2DH12_TEMP_EN_POS, 3);
  134. }
  135. else
  136. {
  137. NRF_TWI_SENSOR_REG_SET(p_inst->temp_cfg, LIS2DH12_TEMP_EN_MASK, LIS2DH12_TEMP_EN_POS, 0);
  138. }
  139. uint8_t send_msg[] = {
  140. LIS2DH12_REG_TEMP_CFG_REG,
  141. p_inst->temp_cfg
  142. };
  143. return nrf_twi_sensor_write(p_inst->p_sensor_data,
  144. p_inst->sensor_addr,
  145. send_msg,
  146. ARRAY_SIZE(send_msg),
  147. true);
  148. }
  149. ret_code_t lis2dh12_temp_read(lis2dh12_instance_t * p_inst,
  150. lis2dh12_temp_cb_t user_cb,
  151. int16_t * p_temp)
  152. {
  153. ASSERT(p_inst != NULL);
  154. return nrf_twi_sensor_reg_read(p_inst->p_sensor_data,
  155. p_inst->sensor_addr,
  156. LIS2DH12_REG_OUT_TEMP_L | LIS2DH12_AUTO_INCR_MASK,
  157. (nrf_twi_sensor_reg_cb_t) user_cb,
  158. (uint8_t *) p_temp,
  159. LIS2DH12_BYTES_PER_TEMP);
  160. }