nrf_drv_uart.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * Copyright (c) 2017 - 2018, 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_uart.h"
  41. #ifdef UARTE_PRESENT
  42. #define INSTANCE_COUNT UARTE_COUNT
  43. #else
  44. #define INSTANCE_COUNT UART_COUNT
  45. #endif
  46. static nrf_uart_event_handler_t m_handlers[INSTANCE_COUNT];
  47. static void * m_contexts[INSTANCE_COUNT];
  48. #if defined(UARTE_PRESENT) && defined(UART_PRESENT)
  49. uint8_t nrf_drv_uart_use_easy_dma[INSTANCE_COUNT];
  50. #endif
  51. #if defined(NRF_DRV_UART_WITH_UARTE)
  52. static void uarte_evt_handler(nrfx_uarte_event_t const * p_event,
  53. void * p_context)
  54. {
  55. uint32_t inst_idx = (uint32_t)p_context;
  56. nrf_drv_uart_event_t event =
  57. {
  58. .type = (nrf_drv_uart_evt_type_t)p_event->type,
  59. .data =
  60. {
  61. .error =
  62. {
  63. .rxtx =
  64. {
  65. .p_data = p_event->data.error.rxtx.p_data,
  66. .bytes = p_event->data.error.rxtx.bytes,
  67. },
  68. .error_mask = p_event->data.error.error_mask,
  69. }
  70. }
  71. };
  72. m_handlers[inst_idx](&event, m_contexts[inst_idx]);
  73. }
  74. #endif // defined(NRF_DRV_UART_WITH_UARTE)
  75. #if defined(NRF_DRV_UART_WITH_UART)
  76. static void uart_evt_handler(nrfx_uart_event_t const * p_event,
  77. void * p_context)
  78. {
  79. uint32_t inst_idx = (uint32_t)p_context;
  80. nrf_drv_uart_event_t event =
  81. {
  82. .type = (nrf_drv_uart_evt_type_t)p_event->type,
  83. .data =
  84. {
  85. .error =
  86. {
  87. .rxtx =
  88. {
  89. .p_data = p_event->data.error.rxtx.p_data,
  90. .bytes = p_event->data.error.rxtx.bytes,
  91. },
  92. .error_mask = p_event->data.error.error_mask,
  93. }
  94. }
  95. };
  96. m_handlers[inst_idx](&event, m_contexts[inst_idx]);
  97. }
  98. #endif // defined(NRF_DRV_UART_WITH_UART)
  99. ret_code_t nrf_drv_uart_init(nrf_drv_uart_t const * p_instance,
  100. nrf_drv_uart_config_t const * p_config,
  101. nrf_uart_event_handler_t event_handler)
  102. {
  103. uint32_t inst_idx = p_instance->inst_idx;
  104. m_handlers[inst_idx] = event_handler;
  105. m_contexts[inst_idx] = p_config->p_context;
  106. #if defined(NRF_DRV_UART_WITH_UARTE) && defined(NRF_DRV_UART_WITH_UART)
  107. #ifdef NRF52840_XXAA
  108. if (inst_idx == 1)
  109. {
  110. ASSERT(p_config->use_easy_dma);
  111. }
  112. #endif
  113. nrf_drv_uart_use_easy_dma[inst_idx] = p_config->use_easy_dma;
  114. #endif
  115. nrf_drv_uart_config_t config = *p_config;
  116. config.p_context = (void *)inst_idx;
  117. ret_code_t result = 0;
  118. if (NRF_DRV_UART_USE_UARTE)
  119. {
  120. result = nrfx_uarte_init(&p_instance->uarte,
  121. (nrfx_uarte_config_t const *)&config,
  122. event_handler ? uarte_evt_handler : NULL);
  123. }
  124. else if (NRF_DRV_UART_USE_UART)
  125. {
  126. result = nrfx_uart_init(&p_instance->uart,
  127. (nrfx_uart_config_t const *)&config,
  128. event_handler ? uart_evt_handler : NULL);
  129. }
  130. return result;
  131. }