app_uart.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "app_uart.h"
  13. #include "nrf_drv_uart.h"
  14. #include "nrf_assert.h"
  15. #include "sdk_common.h"
  16. static uint8_t tx_buffer[1];
  17. static uint8_t rx_buffer[1];
  18. static volatile bool rx_done;
  19. static app_uart_event_handler_t m_event_handler; /**< Event handler function. */
  20. void uart_event_handler(nrf_drv_uart_event_t * p_event, void* p_context)
  21. {
  22. if (p_event->type == NRF_DRV_UART_EVT_RX_DONE)
  23. {
  24. app_uart_evt_t app_uart_event;
  25. app_uart_event.evt_type = APP_UART_DATA;
  26. app_uart_event.data.value = p_event->data.rxtx.p_data[0];
  27. (void)nrf_drv_uart_rx(rx_buffer,1);
  28. rx_done = true;
  29. m_event_handler(&app_uart_event);
  30. }
  31. else if (p_event->type == NRF_DRV_UART_EVT_ERROR)
  32. {
  33. app_uart_evt_t app_uart_event;
  34. app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR;
  35. app_uart_event.data.error_communication = p_event->data.error.error_mask;
  36. (void)nrf_drv_uart_rx(rx_buffer,1);
  37. m_event_handler(&app_uart_event);
  38. }
  39. else if (p_event->type == NRF_DRV_UART_EVT_TX_DONE)
  40. {
  41. // Last byte from FIFO transmitted, notify the application.
  42. // Notify that new data is available if this was first byte put in the buffer.
  43. app_uart_evt_t app_uart_event;
  44. app_uart_event.evt_type = APP_UART_TX_EMPTY;
  45. m_event_handler(&app_uart_event);
  46. }
  47. }
  48. uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params,
  49. app_uart_buffers_t * p_buffers,
  50. app_uart_event_handler_t event_handler,
  51. app_irq_priority_t irq_priority)
  52. {
  53. nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG;
  54. config.baudrate = (nrf_uart_baudrate_t)p_comm_params->baud_rate;
  55. config.hwfc = (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_DISABLED) ?
  56. NRF_UART_HWFC_DISABLED : NRF_UART_HWFC_ENABLED;
  57. config.interrupt_priority = irq_priority;
  58. config.parity = p_comm_params->use_parity ? NRF_UART_PARITY_INCLUDED : NRF_UART_PARITY_EXCLUDED;
  59. config.pselcts = p_comm_params->cts_pin_no;
  60. config.pselrts = p_comm_params->rts_pin_no;
  61. config.pselrxd = p_comm_params->rx_pin_no;
  62. config.pseltxd = p_comm_params->tx_pin_no;
  63. m_event_handler = event_handler;
  64. rx_done = false;
  65. if (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_LOW_POWER)
  66. {
  67. return NRF_ERROR_NOT_SUPPORTED;
  68. }
  69. uint32_t err_code = nrf_drv_uart_init(&config, uart_event_handler);
  70. VERIFY_SUCCESS(err_code);
  71. #ifdef NRF52
  72. if (!config.use_easy_dma)
  73. #endif
  74. {
  75. nrf_drv_uart_rx_enable();
  76. }
  77. return nrf_drv_uart_rx(rx_buffer,1);
  78. }
  79. uint32_t app_uart_get(uint8_t * p_byte)
  80. {
  81. ASSERT(p_byte);
  82. uint32_t err_code = NRF_SUCCESS;
  83. if (rx_done)
  84. {
  85. *p_byte = rx_buffer[0];
  86. }
  87. else
  88. {
  89. err_code = NRF_ERROR_NOT_FOUND;
  90. }
  91. return err_code;
  92. }
  93. uint32_t app_uart_put(uint8_t byte)
  94. {
  95. tx_buffer[0] = byte;
  96. ret_code_t ret = nrf_drv_uart_tx(tx_buffer,1);
  97. if (NRF_ERROR_BUSY == ret)
  98. {
  99. return NRF_ERROR_NO_MEM;
  100. }
  101. else if (ret != NRF_SUCCESS)
  102. {
  103. return NRF_ERROR_INTERNAL;
  104. }
  105. else
  106. {
  107. return NRF_SUCCESS;
  108. }
  109. }
  110. uint32_t app_uart_flush(void)
  111. {
  112. return NRF_SUCCESS;
  113. }
  114. uint32_t app_uart_close(void)
  115. {
  116. nrf_drv_uart_uninit();
  117. return NRF_SUCCESS;
  118. }