nrf_drv_spi.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_drv_spi.h"
  41. #ifdef SPIM_PRESENT
  42. #define INSTANCE_COUNT SPIM_COUNT
  43. #else
  44. #define INSTANCE_COUNT SPI_COUNT
  45. #endif
  46. static nrf_drv_spi_evt_handler_t m_handlers[INSTANCE_COUNT];
  47. static void * m_contexts[INSTANCE_COUNT];
  48. #ifdef SPIM_PRESENT
  49. static void spim_evt_handler(nrfx_spim_evt_t const * p_event,
  50. void * p_context)
  51. {
  52. uint32_t inst_idx = (uint32_t)p_context;
  53. nrf_drv_spi_evt_t const event =
  54. {
  55. .type = (nrf_drv_spi_evt_type_t)p_event->type,
  56. .data =
  57. {
  58. .done =
  59. {
  60. .p_tx_buffer = p_event->xfer_desc.p_tx_buffer,
  61. .tx_length = p_event->xfer_desc.tx_length,
  62. .p_rx_buffer = p_event->xfer_desc.p_rx_buffer,
  63. .rx_length = p_event->xfer_desc.rx_length,
  64. }
  65. }
  66. };
  67. m_handlers[inst_idx](&event, m_contexts[inst_idx]);
  68. }
  69. #endif // SPIM_PRESENT
  70. #ifdef SPI_PRESENT
  71. static void spi_evt_handler(nrfx_spi_evt_t const * p_event,
  72. void * p_context)
  73. {
  74. uint32_t inst_idx = (uint32_t)p_context;
  75. nrf_drv_spi_evt_t const event =
  76. {
  77. .type = (nrf_drv_spi_evt_type_t)p_event->type,
  78. .data =
  79. {
  80. .done =
  81. {
  82. .p_tx_buffer = p_event->xfer_desc.p_tx_buffer,
  83. .tx_length = p_event->xfer_desc.tx_length,
  84. .p_rx_buffer = p_event->xfer_desc.p_rx_buffer,
  85. .rx_length = p_event->xfer_desc.rx_length,
  86. }
  87. }
  88. };
  89. m_handlers[inst_idx](&event, m_contexts[inst_idx]);
  90. }
  91. #endif // SPI_PRESENT
  92. ret_code_t nrf_drv_spi_init(nrf_drv_spi_t const * const p_instance,
  93. nrf_drv_spi_config_t const * p_config,
  94. nrf_drv_spi_evt_handler_t handler,
  95. void * p_context)
  96. {
  97. uint32_t inst_idx = p_instance->inst_idx;
  98. m_handlers[inst_idx] = handler;
  99. m_contexts[inst_idx] = p_context;
  100. ret_code_t result = 0;
  101. if (NRF_DRV_SPI_USE_SPIM)
  102. {
  103. #ifdef SPIM_PRESENT
  104. nrfx_spim_config_t config_spim = NRFX_SPIM_DEFAULT_CONFIG;
  105. config_spim.sck_pin = p_config->sck_pin;
  106. config_spim.mosi_pin = p_config->mosi_pin;
  107. config_spim.miso_pin = p_config->miso_pin;
  108. config_spim.ss_pin = p_config->ss_pin;
  109. config_spim.irq_priority = p_config->irq_priority;
  110. config_spim.orc = p_config->orc;
  111. config_spim.frequency = (nrf_spim_frequency_t)p_config->frequency;
  112. config_spim.mode = (nrf_spim_mode_t)p_config->mode;
  113. config_spim.bit_order = (nrf_spim_bit_order_t)p_config->bit_order;
  114. result = nrfx_spim_init(&p_instance->u.spim,
  115. &config_spim,
  116. handler ? spim_evt_handler : NULL,
  117. (void *)inst_idx);
  118. #endif
  119. }
  120. else if (NRF_DRV_SPI_USE_SPI)
  121. {
  122. result = nrfx_spi_init(&p_instance->u.spi,
  123. (nrfx_spi_config_t const *)p_config,
  124. handler ? spi_evt_handler : NULL,
  125. (void *)inst_idx);
  126. }
  127. return result;
  128. }