nrf_twi_sensor.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Copyright (c) 2017 - 2020, 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 "nrf_twi_sensor.h"
  41. #include <string.h>
  42. #define NRF_LOG_MODULE_NAME twi_sensor
  43. #if NRF_TWI_SENSOR_CONFIG_LOG_ENABLED
  44. #define NRF_LOG_LEVEL NRF_TWI_SENSOR_CONFIG_LOG_LEVEL
  45. #define NRF_LOG_INFO_COLOR NRF_TWI_SENSOR_CONFIG_INFO_COLOR
  46. #define NRF_LOG_DEBUG_COLOR NRF_TWI_SENSOR_CONFIG_DEBUG_COLOR
  47. #else
  48. #define NRF_LOG_LEVEL 0
  49. #endif // NRF_TWI_SENSOR_CONFIG_LOG_ENABLED
  50. #include "nrf_log.h"
  51. NRF_LOG_MODULE_REGISTER();
  52. ret_code_t nrf_twi_sensor_init(nrf_twi_sensor_t * p_twi_sensor)
  53. {
  54. return nrf_balloc_init(p_twi_sensor->p_pool);
  55. }
  56. static void sensor_read_reg_cb(ret_code_t result, void * p_user_data)
  57. {
  58. nrf_twi_sensor_read_cmd_t * p_cmd = &((nrf_twi_sensor_cmd_t *) p_user_data)->read;
  59. NRF_LOG_INFO("Read cb reg addr: 0x%02X, result %d", p_cmd->reg_address, result);
  60. NRF_LOG_DEBUG("\r\nCallback pointer: %p\r\nData:", p_cmd->user_cb);
  61. NRF_LOG_HEXDUMP_DEBUG(p_cmd->transfers[1].p_data, p_cmd->transfers[1].length);
  62. if (p_cmd->user_cb != NULL)
  63. {
  64. p_cmd->user_cb(result, (void*)p_cmd->transfers[1].p_data);
  65. }
  66. nrf_balloc_free(p_cmd->p_instance->p_pool, p_user_data);
  67. }
  68. ret_code_t nrf_twi_sensor_reg_read(nrf_twi_sensor_t const * p_instance,
  69. uint8_t sensor_addr,
  70. uint8_t reg_address,
  71. nrf_twi_sensor_reg_cb_t user_cb,
  72. uint8_t * p_data,
  73. uint8_t length)
  74. {
  75. ASSERT(p_instance != NULL);
  76. ASSERT(p_data != NULL);
  77. NRF_LOG_INFO("Sensor addr: 0x%02X"
  78. "\r\nRead reg addr: 0x%02X, bytes %d",
  79. sensor_addr,
  80. reg_address,
  81. length);
  82. nrf_twi_sensor_read_cmd_t * p_cmd =
  83. (nrf_twi_sensor_read_cmd_t *) nrf_balloc_alloc(p_instance->p_pool);
  84. if (p_cmd == NULL)
  85. {
  86. NRF_LOG_WARNING("Memory not allocated.");
  87. return NRF_ERROR_NO_MEM;
  88. }
  89. p_cmd->p_instance = p_instance;
  90. p_cmd->user_cb = user_cb;
  91. p_cmd->reg_address = reg_address;
  92. p_cmd->transfers[0] = (nrf_twi_mngr_transfer_t) NRF_TWI_MNGR_WRITE(sensor_addr,
  93. &p_cmd->reg_address,
  94. 1,
  95. NRF_TWI_MNGR_NO_STOP);
  96. p_cmd->transfers[1] = (nrf_twi_mngr_transfer_t) NRF_TWI_MNGR_READ(sensor_addr,
  97. p_data,
  98. length,
  99. NRF_TWI_MNGR_NO_STOP);
  100. p_cmd->transaction = (nrf_twi_mngr_transaction_t) {
  101. .callback = sensor_read_reg_cb,
  102. .p_user_data = p_cmd,
  103. .p_transfers = p_cmd->transfers,
  104. .number_of_transfers = ARRAY_SIZE(p_cmd->transfers)
  105. };
  106. ret_code_t err_code = nrf_twi_mngr_schedule(p_instance->p_twi_mngr, &p_cmd->transaction);
  107. if (err_code != NRF_SUCCESS)
  108. {
  109. NRF_LOG_WARNING("Transaction not scheduled.\r\nSensor addr: 0x%02X Error code: %d",
  110. sensor_addr,
  111. err_code);
  112. nrf_balloc_free(p_instance->p_pool, p_cmd);
  113. }
  114. return err_code;
  115. }
  116. static void sensor_write_reg_cb(ret_code_t result, void * p_user_data)
  117. {
  118. nrf_twi_sensor_write_cmd_t * p_cmd = &((nrf_twi_sensor_cmd_t *) p_user_data)->write;
  119. NRF_LOG_INFO("Write cb reg addr: 0x%02X, result %d", p_cmd->send_msg[0], result);
  120. nrf_balloc_free(p_cmd->p_instance->p_pool, p_user_data);
  121. }
  122. ret_code_t nrf_twi_sensor_write(nrf_twi_sensor_t const * p_instance,
  123. uint8_t sensor_addr,
  124. uint8_t const * p_data,
  125. uint8_t length,
  126. bool copy_flag)
  127. {
  128. ASSERT(p_instance != NULL);
  129. ASSERT(p_data != NULL);
  130. NRF_LOG_INFO("Sensor addr: 0x%02X Write length %d", sensor_addr, length);
  131. NRF_LOG_DEBUG("Data: ");
  132. NRF_LOG_HEXDUMP_DEBUG(p_data, length);
  133. nrf_twi_sensor_write_cmd_t * p_cmd =
  134. (nrf_twi_sensor_write_cmd_t *) nrf_balloc_alloc(p_instance->p_pool);
  135. if (p_cmd == NULL)
  136. {
  137. NRF_LOG_WARNING("Memory not allocated. Sensor addr: 0x%02X",
  138. sensor_addr);
  139. return NRF_ERROR_NO_MEM;
  140. }
  141. p_cmd->p_instance = p_instance;
  142. p_cmd->transfer = (nrf_twi_mngr_transfer_t) NRF_TWI_MNGR_WRITE(sensor_addr,
  143. p_data,
  144. length,
  145. 0);
  146. if (copy_flag == true)
  147. {
  148. if (length > NRF_TWI_SENSOR_SEND_BUF_SIZE)
  149. {
  150. NRF_LOG_ERROR("Data too long to copy. Sensor addr: 0x%02X"
  151. "\r\nRequested write length: %d, max length: %d",
  152. sensor_addr,
  153. length,
  154. NRF_TWI_SENSOR_SEND_BUF_SIZE);
  155. nrf_balloc_free(p_instance->p_pool, p_cmd);
  156. return NRF_ERROR_INVALID_LENGTH;
  157. }
  158. memcpy(p_cmd->send_msg, p_data, length);
  159. p_cmd->transfer.p_data = p_cmd->send_msg;
  160. }
  161. p_cmd->transaction = (nrf_twi_mngr_transaction_t) {
  162. .callback = sensor_write_reg_cb,
  163. .p_user_data = p_cmd,
  164. .p_transfers = &p_cmd->transfer,
  165. .number_of_transfers = 1
  166. };
  167. ret_code_t err_code = nrf_twi_mngr_schedule(p_instance->p_twi_mngr, &p_cmd->transaction);
  168. if (err_code != NRF_SUCCESS)
  169. {
  170. NRF_LOG_WARNING("Transaction not scheduled.\r\nSensor addr: 0x%02X Error code: %d",
  171. sensor_addr,
  172. err_code);
  173. nrf_balloc_free(p_instance->p_pool, p_cmd);
  174. }
  175. return err_code;
  176. }
  177. ret_code_t nrf_twi_sensor_reg_write(nrf_twi_sensor_t const * p_instance,
  178. uint8_t sensor_addr,
  179. uint8_t reg_address,
  180. uint8_t * p_data,
  181. uint8_t length)
  182. {
  183. ASSERT(p_instance != NULL);
  184. ASSERT(p_data != NULL);
  185. NRF_LOG_INFO("Write register: 0x%02X", reg_address);
  186. if (length > NRF_TWI_SENSOR_SEND_BUF_SIZE - 1) // Subtracting one byte for address
  187. {
  188. NRF_LOG_ERROR("Data too long to copy. Sensor addr: 0x%02X"
  189. "\r\nRequested write length: %d, max length: %d",
  190. sensor_addr,
  191. length,
  192. NRF_TWI_SENSOR_SEND_BUF_SIZE - 1);
  193. return NRF_ERROR_INVALID_LENGTH;
  194. }
  195. uint8_t buf[NRF_TWI_SENSOR_SEND_BUF_SIZE];
  196. buf[0] = reg_address;
  197. memcpy(&buf[1], p_data, length);
  198. return nrf_twi_sensor_write(p_instance, sensor_addr, buf, length + 1, true);
  199. }