nrf_drv_twi.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "nrf_drv_twi.h"
  41. #include <nrf_delay.h>
  42. #include <hal/nrf_gpio.h>
  43. #ifdef TWIM_PRESENT
  44. #define INSTANCE_COUNT TWIM_COUNT
  45. #else
  46. #define INSTANCE_COUNT TWI_COUNT
  47. #endif
  48. #define SCL_PIN_INIT_CONF \
  49. ( (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
  50. | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) \
  51. | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) \
  52. | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) \
  53. | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos))
  54. #define SDA_PIN_INIT_CONF SCL_PIN_INIT_CONF
  55. #define SDA_PIN_UNINIT_CONF \
  56. ( (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
  57. | (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) \
  58. | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) \
  59. | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) \
  60. | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos))
  61. #define SCL_PIN_UNINIT_CONF SDA_PIN_UNINIT_CONF
  62. #define SCL_PIN_INIT_CONF_CLR \
  63. ( (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
  64. | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) \
  65. | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) \
  66. | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) \
  67. | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos))
  68. #define SDA_PIN_INIT_CONF_CLR SCL_PIN_INIT_CONF_CLR
  69. static nrf_drv_twi_evt_handler_t m_handlers[INSTANCE_COUNT];
  70. static void * m_contexts[INSTANCE_COUNT];
  71. static void twi_clear_bus(nrf_drv_twi_config_t const * p_config)
  72. {
  73. NRF_GPIO->PIN_CNF[p_config->scl] = SCL_PIN_INIT_CONF;
  74. NRF_GPIO->PIN_CNF[p_config->sda] = SDA_PIN_INIT_CONF;
  75. nrf_gpio_pin_set(p_config->scl);
  76. nrf_gpio_pin_set(p_config->sda);
  77. NRF_GPIO->PIN_CNF[p_config->scl] = SCL_PIN_INIT_CONF_CLR;
  78. NRF_GPIO->PIN_CNF[p_config->sda] = SDA_PIN_INIT_CONF_CLR;
  79. nrf_delay_us(4);
  80. for (int i = 0; i < 9; i++)
  81. {
  82. if (nrf_gpio_pin_read(p_config->sda))
  83. {
  84. if (i == 0)
  85. {
  86. return;
  87. }
  88. else
  89. {
  90. break;
  91. }
  92. }
  93. nrf_gpio_pin_clear(p_config->scl);
  94. nrf_delay_us(4);
  95. nrf_gpio_pin_set(p_config->scl);
  96. nrf_delay_us(4);
  97. }
  98. nrf_gpio_pin_clear(p_config->sda);
  99. nrf_delay_us(4);
  100. nrf_gpio_pin_set(p_config->sda);
  101. }
  102. #ifdef TWIM_PRESENT
  103. static void twim_evt_handler(nrfx_twim_evt_t const * p_event,
  104. void * p_context)
  105. {
  106. uint32_t inst_idx = (uint32_t)p_context;
  107. nrf_drv_twi_evt_t const event =
  108. {
  109. .type = (nrf_drv_twi_evt_type_t)p_event->type,
  110. .xfer_desc =
  111. {
  112. .type = (nrf_drv_twi_xfer_type_t)p_event->xfer_desc.type,
  113. .address = p_event->xfer_desc.address,
  114. .primary_length = p_event->xfer_desc.primary_length,
  115. .secondary_length = p_event->xfer_desc.secondary_length,
  116. .p_primary_buf = p_event->xfer_desc.p_primary_buf,
  117. .p_secondary_buf = p_event->xfer_desc.p_secondary_buf,
  118. }
  119. };
  120. m_handlers[inst_idx](&event, m_contexts[inst_idx]);
  121. }
  122. #endif // TWIM_PRESENT
  123. #ifdef TWI_PRESENT
  124. static void twi_evt_handler(nrfx_twi_evt_t const * p_event,
  125. void * p_context)
  126. {
  127. uint32_t inst_idx = (uint32_t)p_context;
  128. nrf_drv_twi_evt_t const event =
  129. {
  130. .type = (nrf_drv_twi_evt_type_t)p_event->type,
  131. .xfer_desc =
  132. {
  133. .type = (nrf_drv_twi_xfer_type_t)p_event->xfer_desc.type,
  134. .address = p_event->xfer_desc.address,
  135. .primary_length = p_event->xfer_desc.primary_length,
  136. .secondary_length = p_event->xfer_desc.secondary_length,
  137. .p_primary_buf = p_event->xfer_desc.p_primary_buf,
  138. .p_secondary_buf = p_event->xfer_desc.p_secondary_buf,
  139. }
  140. };
  141. m_handlers[inst_idx](&event, m_contexts[inst_idx]);
  142. }
  143. #endif // TWI_PRESENT
  144. ret_code_t nrf_drv_twi_init(nrf_drv_twi_t const * p_instance,
  145. nrf_drv_twi_config_t const * p_config,
  146. nrf_drv_twi_evt_handler_t event_handler,
  147. void * p_context)
  148. {
  149. uint32_t inst_idx = p_instance->inst_idx;
  150. m_handlers[inst_idx] = event_handler;
  151. m_contexts[inst_idx] = p_context;
  152. if(p_config->clear_bus_init)
  153. {
  154. /* Send clocks (max 9) until slave device back from stuck mode */
  155. twi_clear_bus(p_config);
  156. }
  157. ret_code_t result = 0;
  158. if (NRF_DRV_TWI_USE_TWIM)
  159. {
  160. result = nrfx_twim_init(&p_instance->u.twim,
  161. (nrfx_twim_config_t const *)p_config,
  162. event_handler ? twim_evt_handler : NULL,
  163. (void *)inst_idx);
  164. }
  165. else if (NRF_DRV_TWI_USE_TWI)
  166. {
  167. result = nrfx_twi_init(&p_instance->u.twi,
  168. (nrfx_twi_config_t const *)p_config,
  169. event_handler ? twi_evt_handler : NULL,
  170. (void *)inst_idx);
  171. }
  172. return result;
  173. }