aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/serial
diff options
context:
space:
mode:
Diffstat (limited to 'thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/serial')
-rw-r--r--thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/serial/nrf_serial.c675
-rw-r--r--thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/serial/nrf_serial.h397
2 files changed, 1072 insertions, 0 deletions
diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/serial/nrf_serial.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/serial/nrf_serial.c
new file mode 100644
index 0000000..9cf89b8
--- /dev/null
+++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/serial/nrf_serial.c
@@ -0,0 +1,675 @@
+/**
+ * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form, except as embedded into a Nordic
+ * Semiconductor ASA integrated circuit in a product or a software update for
+ * such product, must reproduce the above copyright notice, this list of
+ * conditions and the following disclaimer in the documentation and/or other
+ * materials provided with the distribution.
+ *
+ * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
+ * contributors may be used to endorse or promote products derived from this
+ * software without specific prior written permission.
+ *
+ * 4. This software, with or without modification, must only be used with a
+ * Nordic Semiconductor ASA integrated circuit.
+ *
+ * 5. Any software provided in binary form under this license must not be reverse
+ * engineered, decompiled, modified and/or disassembled.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+#include "sdk_common.h"
+#if NRF_MODULE_ENABLED(NRF_SERIAL)
+#include "nrf_serial.h"
+
+#if defined (UART_PRESENT)
+
+static void event_handler(nrf_serial_t const * p_serial,
+ nrf_serial_event_t event)
+{
+ if (p_serial->p_ctx->p_config->ev_handler)
+ {
+ p_serial->p_ctx->p_config->ev_handler(p_serial, event);
+ }
+}
+
+static void sleep_handler(nrf_serial_t const * p_serial)
+{
+ if (p_serial->p_ctx->p_config->mode == NRF_SERIAL_MODE_POLLING)
+ {
+ return;
+ }
+
+ if (p_serial->p_ctx->p_config->sleep_handler)
+ {
+ p_serial->p_ctx->p_config->sleep_handler();
+ }
+}
+
+static size_t serial_rx(nrf_serial_t const * p_serial,
+ uint8_t * p_buff,
+ size_t length)
+{
+ if (p_serial->p_ctx->p_config->mode == NRF_SERIAL_MODE_POLLING)
+ {
+ size_t rx_len = MIN(length, UINT8_MAX);
+ size_t len = rx_len;
+
+ while (nrf_drv_uart_rx_ready(&p_serial->instance) && len)
+ {
+ ret_code_t ret = nrf_drv_uart_rx(&p_serial->instance, p_buff, 1);
+ if (ret != NRF_SUCCESS)
+ {
+ break;
+ }
+ p_buff++;
+ len--;
+ }
+
+ return rx_len - len;
+ }
+
+ nrf_queue_t const * p_rxq = p_serial->p_ctx->p_config->p_queues->p_rxq;
+ return nrf_queue_out(p_rxq, p_buff, length);
+}
+
+static size_t serial_tx(nrf_serial_t const * p_serial,
+ uint8_t const * p_buff,
+ size_t length)
+{
+ size_t tx_len = 0;
+
+ if (p_serial->p_ctx->p_config->mode == NRF_SERIAL_MODE_POLLING)
+ {
+ tx_len = MIN(length, UINT8_MAX);
+ ret_code_t ret = nrf_drv_uart_tx(&p_serial->instance, p_buff, tx_len);
+ ASSERT(ret == NRF_SUCCESS)
+ return tx_len;
+ }
+
+ nrf_queue_t const * p_txq = p_serial->p_ctx->p_config->p_queues->p_txq;
+ nrf_serial_buffers_t const * p_buffs = p_serial->p_ctx->p_config->p_buffers;
+
+ /* Try to enqueue data. */
+ size_t queue_in_len = nrf_queue_in(p_txq, p_buff, length);
+ if (nrf_drv_uart_tx_in_progress(&p_serial->instance))
+ {
+ return queue_in_len;
+ }
+
+ size_t len = nrf_queue_out(p_txq, p_buffs->p_txb, p_buffs->tx_size);
+ ASSERT(len > 0);
+ ret_code_t ret = nrf_drv_uart_tx(&p_serial->instance, p_buffs->p_txb, len);
+ ASSERT(ret == NRF_SUCCESS);
+
+ return queue_in_len;
+}
+
+static void uart_event_handler(nrf_drv_uart_event_t * p_event, void * p_context)
+{
+ uint32_t ret;
+ nrf_serial_t const * p_serial = p_context;
+
+ switch (p_event->type)
+ {
+ case NRF_DRV_UART_EVT_RX_DONE:
+ {
+ nrf_queue_t const * p_rxq =
+ p_serial->p_ctx->p_config->p_queues->p_rxq;
+ size_t len = nrf_queue_in(p_rxq,
+ p_event->data.rxtx.p_data,
+ p_event->data.rxtx.bytes);
+
+ if (len < p_event->data.rxtx.bytes)
+ {
+ event_handler(p_serial, NRF_SERIAL_EVENT_FIFO_ERR);
+ break;
+ }
+
+ event_handler(p_serial, NRF_SERIAL_EVENT_RX_DATA);
+ nrf_serial_buffers_t const * p_buffs =
+ p_serial->p_ctx->p_config->p_buffers;
+
+ ret = nrf_drv_uart_rx(&p_serial->instance,
+ p_buffs->p_rxb,
+ p_buffs->rx_size);
+ ASSERT(ret == NRF_SUCCESS);
+ break;
+ }
+ case NRF_DRV_UART_EVT_ERROR:
+ {
+ event_handler(p_serial, NRF_SERIAL_EVENT_DRV_ERR);
+ break;
+ }
+ case NRF_DRV_UART_EVT_TX_DONE:
+ {
+ nrf_queue_t const * p_txq =
+ p_serial->p_ctx->p_config->p_queues->p_txq;
+ nrf_serial_buffers_t const * p_buffs =
+ p_serial->p_ctx->p_config->p_buffers;
+
+ event_handler(p_serial, NRF_SERIAL_EVENT_TX_DONE);
+ size_t len = nrf_queue_out(p_txq, p_buffs->p_txb, p_buffs->tx_size);
+ if (len == 0)
+ {
+ break;
+ }
+
+ ret = nrf_drv_uart_tx(&p_serial->instance, p_buffs->p_txb, len);
+ ASSERT(ret == NRF_SUCCESS);
+ break;
+ }
+ default:
+ break;
+ }
+}
+
+ret_code_t nrf_serial_init(nrf_serial_t const * p_serial,
+ nrf_drv_uart_config_t const * p_drv_uart_config,
+ nrf_serial_config_t const * p_config)
+{
+ ret_code_t ret;
+ ASSERT(p_serial && p_drv_uart_config && p_config);
+
+ if (p_serial->p_ctx->p_config)
+ {
+ /*Already initialized.*/
+ return NRF_ERROR_MODULE_ALREADY_INITIALIZED;
+ }
+
+ if (p_config->mode != NRF_SERIAL_MODE_POLLING)
+ {
+ ASSERT(p_config->p_queues && p_config->p_buffers);
+ }
+
+ nrf_drv_uart_config_t drv_config;
+ memcpy(&drv_config, p_drv_uart_config, sizeof(nrf_drv_uart_config_t));
+ drv_config.p_context = (void *)p_serial;
+#if defined(UARTE_PRESENT) && defined(UART_PRESENT)
+ drv_config.use_easy_dma = (p_config->mode == NRF_SERIAL_MODE_DMA);
+#endif
+ ret = nrf_drv_uart_init(&p_serial->instance,
+ &drv_config,
+ p_config->mode == NRF_SERIAL_MODE_POLLING ?
+ NULL : uart_event_handler);
+ if (ret != NRF_SUCCESS)
+ {
+ return ret;
+ }
+
+ p_serial->p_ctx->p_config = p_config;
+
+ if (p_serial->p_ctx->p_config->p_queues)
+ {
+ nrf_queue_reset(p_serial->p_ctx->p_config->p_queues->p_txq);
+ nrf_queue_reset(p_serial->p_ctx->p_config->p_queues->p_rxq);
+ }
+
+ nrf_mtx_init(&p_serial->p_ctx->read_lock);
+ nrf_mtx_init(&p_serial->p_ctx->write_lock);
+
+ p_serial->p_ctx->flags = NRF_SERIAL_RX_ENABLED_FLAG |
+ NRF_SERIAL_TX_ENABLED_FLAG;
+
+ if (drv_config.pseltxd == NRF_UART_PSEL_DISCONNECTED)
+ {
+ p_serial->p_ctx->flags &= ~NRF_SERIAL_TX_ENABLED_FLAG;
+ }
+
+ if (drv_config.pselrxd == NRF_UART_PSEL_DISCONNECTED)
+ {
+ p_serial->p_ctx->flags &= ~NRF_SERIAL_RX_ENABLED_FLAG;
+ return NRF_SUCCESS;
+ }
+
+ if (p_serial->p_ctx->p_config->mode != NRF_SERIAL_MODE_DMA)
+ {
+ nrf_drv_uart_rx_enable(&p_serial->instance);
+ if (p_serial->p_ctx->p_config->mode == NRF_SERIAL_MODE_POLLING)
+ {
+ return NRF_SUCCESS;
+ }
+ }
+
+ return nrf_drv_uart_rx(&p_serial->instance,
+ p_serial->p_ctx->p_config->p_buffers->p_rxb,
+ p_serial->p_ctx->p_config->p_buffers->rx_size);
+}
+
+ret_code_t nrf_serial_uninit(nrf_serial_t const * p_serial)
+{
+ ASSERT(p_serial);
+
+ if (!p_serial->p_ctx->p_config)
+ {
+ /*Already uninitialized.*/
+ return NRF_ERROR_MODULE_NOT_INITIALIZED;
+ }
+
+ if (!nrf_mtx_trylock(&p_serial->p_ctx->write_lock))
+ {
+ return NRF_ERROR_BUSY;
+ }
+ if (!nrf_mtx_trylock(&p_serial->p_ctx->read_lock))
+ {
+ nrf_mtx_unlock(&p_serial->p_ctx->write_lock);
+ return NRF_ERROR_BUSY;
+ }
+
+ nrf_drv_uart_uninit(&p_serial->instance);
+ if (p_serial->p_ctx->p_config->p_queues)
+ {
+ nrf_queue_reset(p_serial->p_ctx->p_config->p_queues->p_txq);
+ nrf_queue_reset(p_serial->p_ctx->p_config->p_queues->p_rxq);
+ }
+
+ memset(p_serial->p_ctx, 0, sizeof(nrf_serial_ctx_t));
+ return NRF_SUCCESS;
+}
+
+typedef struct {
+ volatile bool expired;
+} nrf_serial_timeout_ctx_t;
+
+static void serial_timeout_handler(void * p_context)
+{
+ nrf_serial_timeout_ctx_t * p_tout_ctx = p_context;
+ p_tout_ctx->expired = true;
+}
+
+
+static ret_code_t timeout_setup(nrf_serial_t const * p_serial,
+ app_timer_id_t const * p_timer_id,
+ uint32_t timeout_ms,
+ nrf_serial_timeout_ctx_t * p_tout_ctx)
+{
+ uint32_t ticks = APP_TIMER_TICKS(timeout_ms);
+
+ if (ticks < APP_TIMER_MIN_TIMEOUT_TICKS)
+ {
+ p_tout_ctx->expired = true;
+ return NRF_SUCCESS;
+ }
+
+ ret_code_t ret = app_timer_create(p_timer_id,
+ APP_TIMER_MODE_SINGLE_SHOT,
+ serial_timeout_handler);
+ if (ret != NRF_SUCCESS)
+ {
+ return ret;
+ }
+
+ return app_timer_start(*p_timer_id, ticks, p_tout_ctx);
+}
+
+ret_code_t nrf_serial_write(nrf_serial_t const * p_serial,
+ void const * p_data,
+ size_t size,
+ size_t * p_written,
+ uint32_t timeout_ms)
+{
+ ret_code_t ret;
+
+ ASSERT(p_serial);
+ if (!p_serial->p_ctx->p_config)
+ {
+ return NRF_ERROR_MODULE_NOT_INITIALIZED;
+ }
+
+ if (!(p_serial->p_ctx->flags & NRF_SERIAL_TX_ENABLED_FLAG))
+ {
+ return NRF_ERROR_INVALID_STATE;
+ }
+
+ if (size == 0)
+ {
+ return NRF_SUCCESS;
+ }
+
+ if (!nrfx_is_in_ram(p_data) &&
+ p_serial->p_ctx->p_config->mode == NRF_SERIAL_MODE_DMA)
+ {
+ return NRF_ERROR_INVALID_ADDR;
+ }
+
+ if (!nrf_mtx_trylock(&p_serial->p_ctx->write_lock))
+ {
+ return NRF_ERROR_BUSY;
+ }
+
+ nrf_serial_timeout_ctx_t tout_ctx = {
+ .expired = false,
+ };
+
+ if (timeout_ms != NRF_SERIAL_MAX_TIMEOUT)
+ {
+ ret = timeout_setup(p_serial,
+ p_serial->p_tx_timer,
+ timeout_ms,
+ &tout_ctx);
+ if (ret != NRF_SUCCESS)
+ {
+ nrf_mtx_unlock(&p_serial->p_ctx->write_lock);
+ return ret;
+ }
+ }
+
+ size_t left = size;
+ uint8_t const * p_buff = p_data;
+
+ do
+ {
+ size_t wcnt = serial_tx(p_serial, p_buff, left);
+ left -= wcnt;
+ p_buff += wcnt;
+ if (!left)
+ {
+ break;
+ }
+
+ sleep_handler(p_serial);
+ } while (!tout_ctx.expired);
+
+ if (p_written)
+ {
+ *p_written = size - left;
+ }
+
+ if (!tout_ctx.expired && (timeout_ms != NRF_SERIAL_MAX_TIMEOUT))
+ {
+ (void)app_timer_stop(*p_serial->p_tx_timer);
+ }
+
+ nrf_mtx_unlock(&p_serial->p_ctx->write_lock);
+ if (left && tout_ctx.expired)
+ {
+ return NRF_ERROR_TIMEOUT;
+ }
+
+ return NRF_SUCCESS;
+}
+
+ret_code_t nrf_serial_read(nrf_serial_t const * p_serial,
+ void * p_data,
+ size_t size,
+ size_t * p_read,
+ uint32_t timeout_ms)
+{
+ ret_code_t ret;
+
+ ASSERT(p_serial);
+ if (!p_serial->p_ctx->p_config)
+ {
+ return NRF_ERROR_MODULE_NOT_INITIALIZED;
+ }
+
+ if (!(p_serial->p_ctx->flags & NRF_SERIAL_RX_ENABLED_FLAG))
+ {
+ return NRF_ERROR_INVALID_STATE;
+ }
+
+ if (size == 0)
+ {
+ return NRF_SUCCESS;
+ }
+
+ if (!nrf_mtx_trylock(&p_serial->p_ctx->read_lock))
+ {
+ return NRF_ERROR_BUSY;
+ }
+
+ nrf_serial_timeout_ctx_t tout_ctx = {
+ .expired = false,
+ };
+
+ if (timeout_ms != NRF_SERIAL_MAX_TIMEOUT)
+ {
+ ret = timeout_setup(p_serial,
+ p_serial->p_rx_timer,
+ timeout_ms,
+ &tout_ctx);
+
+ if (ret != NRF_SUCCESS)
+ {
+ nrf_mtx_unlock(&p_serial->p_ctx->read_lock);
+ return ret;
+ }
+ }
+
+ size_t left = size;
+ uint8_t * p_buff = p_data;
+ do
+ {
+ size_t rcnt = serial_rx(p_serial, p_buff, left);
+ left -= rcnt;
+ p_buff += rcnt;
+ if (!left)
+ {
+ break;
+ }
+
+ if (tout_ctx.expired)
+ {
+ if (p_serial->p_ctx->p_config->mode != NRF_SERIAL_MODE_POLLING)
+ {
+ nrf_drv_uart_rx_abort(&p_serial->instance);
+ }
+ break;
+ }
+
+ sleep_handler(p_serial);
+ } while (1);
+
+ if (p_read)
+ {
+ *p_read = size - left;
+ }
+
+ if (!tout_ctx.expired && (timeout_ms != NRF_SERIAL_MAX_TIMEOUT))
+ {
+ (void)app_timer_stop(*p_serial->p_rx_timer);
+ }
+
+ nrf_mtx_unlock(&p_serial->p_ctx->read_lock);
+ if (left && tout_ctx.expired)
+ {
+ return NRF_ERROR_TIMEOUT;
+ }
+
+ return NRF_SUCCESS;
+}
+
+ret_code_t nrf_serial_flush(nrf_serial_t const * p_serial, uint32_t timeout_ms)
+{
+
+ ret_code_t ret;
+
+ ASSERT(p_serial);
+ if (!p_serial->p_ctx->p_config)
+ {
+ return NRF_ERROR_MODULE_NOT_INITIALIZED;
+ }
+
+ if (!(p_serial->p_ctx->flags & NRF_SERIAL_TX_ENABLED_FLAG))
+ {
+ return NRF_ERROR_INVALID_STATE;
+ }
+
+ if (p_serial->p_ctx->p_config->mode == NRF_SERIAL_MODE_POLLING)
+ {
+ return NRF_SUCCESS;
+ }
+
+ if (!nrf_mtx_trylock(&p_serial->p_ctx->write_lock))
+ {
+ return NRF_ERROR_BUSY;
+ }
+
+ nrf_serial_timeout_ctx_t tout_ctx = {
+ .expired = false,
+ };
+
+ if (timeout_ms != NRF_SERIAL_MAX_TIMEOUT)
+ {
+ ret = timeout_setup(p_serial,
+ p_serial->p_tx_timer,
+ timeout_ms,
+ &tout_ctx);
+ if (ret != NRF_SUCCESS)
+ {
+ nrf_mtx_unlock(&p_serial->p_ctx->write_lock);
+ return ret;
+ }
+ }
+
+ bool empty;
+ do
+ {
+ empty = nrf_queue_is_empty(p_serial->p_ctx->p_config->p_queues->p_txq)
+ && !nrf_drv_uart_tx_in_progress(&p_serial->instance);
+ if (empty)
+ {
+ break;
+ }
+
+ sleep_handler(p_serial);
+ } while (!tout_ctx.expired);
+
+ if (!tout_ctx.expired && (timeout_ms != NRF_SERIAL_MAX_TIMEOUT))
+ {
+ (void)app_timer_stop(*p_serial->p_tx_timer);
+ }
+
+ nrf_mtx_unlock(&p_serial->p_ctx->write_lock);
+ if (!empty && tout_ctx.expired)
+ {
+ return NRF_ERROR_TIMEOUT;
+ }
+
+ return NRF_SUCCESS;
+}
+
+ret_code_t nrf_serial_tx_abort(nrf_serial_t const * p_serial)
+{
+ ASSERT(p_serial);
+ if (!p_serial->p_ctx->p_config)
+ {
+ return NRF_ERROR_MODULE_NOT_INITIALIZED;
+ }
+
+ if (!(p_serial->p_ctx->flags & NRF_SERIAL_TX_ENABLED_FLAG))
+ {
+ return NRF_ERROR_INVALID_STATE;
+ }
+
+ if (!nrf_mtx_trylock(&p_serial->p_ctx->write_lock))
+ {
+ return NRF_ERROR_BUSY;
+ }
+
+ nrf_drv_uart_tx_abort(&p_serial->instance);
+ if (p_serial->p_ctx->p_config->p_queues->p_txq)
+ {
+ nrf_queue_reset(p_serial->p_ctx->p_config->p_queues->p_txq);
+ }
+
+ nrf_mtx_unlock(&p_serial->p_ctx->write_lock);
+ return NRF_SUCCESS;
+}
+
+ret_code_t nrf_serial_rx_drain(nrf_serial_t const * p_serial)
+{
+ ASSERT(p_serial);
+ if (!p_serial->p_ctx->p_config)
+ {
+ return NRF_ERROR_MODULE_NOT_INITIALIZED;
+ }
+
+ if (!(p_serial->p_ctx->flags & NRF_SERIAL_RX_ENABLED_FLAG))
+ {
+ return NRF_ERROR_INVALID_STATE;
+ }
+
+ if (!nrf_mtx_trylock(&p_serial->p_ctx->read_lock))
+ {
+ return NRF_ERROR_BUSY;
+ }
+
+ uint8_t c;
+ /*Drain HW FIFO*/
+ while (serial_rx(p_serial, &c, sizeof(c)))
+ {
+
+ }
+
+ /*Drain SW FIFO*/
+ if (p_serial->p_ctx->p_config->p_queues->p_rxq)
+ {
+ nrf_queue_reset(p_serial->p_ctx->p_config->p_queues->p_rxq);
+ }
+ nrf_mtx_unlock(&p_serial->p_ctx->read_lock);
+ return NRF_SUCCESS;
+}
+#else
+ret_code_t nrf_serial_init(nrf_serial_t const * p_serial,
+ nrf_drv_uart_config_t const * p_drv_uart_config,
+ nrf_serial_config_t const * p_config)
+{
+ return NRF_ERROR_NOT_SUPPORTED;
+}
+
+ret_code_t nrf_serial_uninit(nrf_serial_t const * p_serial)
+{
+ return NRF_ERROR_NOT_SUPPORTED;
+}
+ret_code_t nrf_serial_write(nrf_serial_t const * p_serial,
+ void const * p_data,
+ size_t size,
+ size_t * p_written,
+ uint32_t timeout_ms)
+{
+ return NRF_ERROR_NOT_SUPPORTED;
+}
+ret_code_t nrf_serial_read(nrf_serial_t const * p_serial,
+ void * p_data,
+ size_t size,
+ size_t * p_read,
+ uint32_t timeout_ms)
+{
+ return NRF_ERROR_NOT_SUPPORTED;
+}
+ret_code_t nrf_serial_flush(nrf_serial_t const * p_serial, uint32_t timeout_ms)
+{
+ return NRF_ERROR_NOT_SUPPORTED;
+}
+ret_code_t nrf_serial_tx_abort(nrf_serial_t const * p_serial)
+{
+ return NRF_ERROR_NOT_SUPPORTED;
+}
+ret_code_t nrf_serial_rx_drain(nrf_serial_t const * p_serial)
+{
+ return NRF_ERROR_NOT_SUPPORTED;
+}
+
+#endif // UART_PRESENT
+#endif //NRF_MODULE_ENABLED(NRF_SERIAL)
+
diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/serial/nrf_serial.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/serial/nrf_serial.h
new file mode 100644
index 0000000..167e373
--- /dev/null
+++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/serial/nrf_serial.h
@@ -0,0 +1,397 @@
+/**
+ * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form, except as embedded into a Nordic
+ * Semiconductor ASA integrated circuit in a product or a software update for
+ * such product, must reproduce the above copyright notice, this list of
+ * conditions and the following disclaimer in the documentation and/or other
+ * materials provided with the distribution.
+ *
+ * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
+ * contributors may be used to endorse or promote products derived from this
+ * software without specific prior written permission.
+ *
+ * 4. This software, with or without modification, must only be used with a
+ * Nordic Semiconductor ASA integrated circuit.
+ *
+ * 5. Any software provided in binary form under this license must not be reverse
+ * engineered, decompiled, modified and/or disassembled.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+#ifndef NRF_SERIAL_H__
+#define NRF_SERIAL_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "nrf_drv_uart.h"
+#include "nrf_queue.h"
+#include "nrf_mtx.h"
+#include "app_timer.h"
+
+/**@file
+ *
+ * @defgroup nrf_serial Serial port abstraction layer
+ * @{
+ * @ingroup app_common
+ *
+ * @brief Serial module interface.
+ * @details This module is more sophisticated than @ref nrf_drv_uart. It internally uses
+ * mutex, queues, and app_timer. You can configure it to work in three different modes
+ * (polling, interrupt, DMA). API can be configured to work in synchronous mode. Both read and write
+ * methods have a timeout parameter. Asynchronous mode is available by passing 0 as the
+ * timeout parameter.
+ * @warning Do not use synchronous API (timeout_ms parameter > 0) in IRQ
+ * context. It may lead to a deadlock because the timeout interrupt cannot
+ * preempt the current IRQ context.
+ */
+
+typedef struct nrf_serial_s nrf_serial_t;
+
+/**
+ * @brief Serial port mode.
+ * */
+typedef enum {
+ NRF_SERIAL_MODE_POLLING, /**< Polling mode.*/
+ NRF_SERIAL_MODE_IRQ, /**< Interrupt mode.*/
+ NRF_SERIAL_MODE_DMA, /**< DMA mode.*/
+} nrf_serial_mode_t;
+
+
+
+/**
+ * @brief Creates an instance of @ref nrf_drv_uart_config_t.
+ *
+ * @param _name Instance name.
+ * @param _rx_pin RX pin number.
+ * @param _tx_pin TX pin number.
+ * @param _rts_pin RTS pin number.
+ * @param _cts_pin CTS pin number.
+ * @param _flow_control Flow control enable/disable (@ref nrf_uart_hwfc_t).
+ * @param _parity Parity enable/disable (@ref nrf_uart_parity_t).
+ * @param _baud_rate Baud rate (@ref nrf_uart_baudrate_t).
+ * @param _irq_prio Interrupt priority.
+ *
+ * */
+#define NRF_SERIAL_DRV_UART_CONFIG_DEF(_name, \
+ _rx_pin, \
+ _tx_pin, \
+ _rts_pin, \
+ _cts_pin, \
+ _flow_control, \
+ _parity, \
+ _baud_rate, \
+ _irq_prio) \
+ static const nrf_drv_uart_config_t _name = { \
+ .pselrxd = _rx_pin, \
+ .pseltxd = _tx_pin, \
+ .pselrts = _rts_pin, \
+ .pselcts = _cts_pin, \
+ .hwfc = _flow_control, \
+ .parity = _parity, \
+ .baudrate = _baud_rate, \
+ .interrupt_priority = _irq_prio, \
+ }
+
+/**
+ * @brief Serial port RX and TX queues.
+ *
+ * @note Use the @ref NRF_SERIAL_QUEUES_DEF macro to create an instance of this
+ * structure.
+ * */
+typedef struct {
+ nrf_queue_t const * p_rxq; //!< Receive queue handle.
+ nrf_queue_t const * p_txq; //!< Transmit queue handle.
+} nrf_serial_queues_t;
+
+/**
+ * @brief Creates an instance of serial port queues.
+ *
+ * @param _name Instance name.
+ * @param _tx_size TX queue size.
+ * @param _rx_size RX queue size.
+ * */
+#define NRF_SERIAL_QUEUES_DEF(_name, _tx_size, _rx_size) \
+ NRF_QUEUE_DEF(uint8_t, _name##_rxq, _rx_size, NRF_QUEUE_MODE_NO_OVERFLOW); \
+ NRF_QUEUE_DEF(uint8_t, _name##_txq, _tx_size, NRF_QUEUE_MODE_NO_OVERFLOW); \
+ static const nrf_serial_queues_t _name = { \
+ .p_rxq = &_name##_rxq, \
+ .p_txq = &_name##_txq, \
+ }
+
+/**
+ * @brief Serial buffers. Data slices used by @ref nrf_drv_uart_tx and
+ * @ref nrf_drv_uart_rx.
+ *
+ * @note Use the @ref NRF_SERIAL_BUFFERS_DEF macro to create an instance of this
+ * structure.
+ * */
+typedef struct {
+ void * p_txb; //!< TX buffer.
+ void * p_rxb; //!< RX buffer.
+ uint8_t tx_size; //!< TX buffer size.
+ uint8_t rx_size; //!< RX buffer size.
+} nrf_serial_buffers_t;
+
+/**
+ * @brief Creates an instance of serial port buffers.
+ *
+ * @param _name Instance name.
+ * @param _tx_size TX buffer size.
+ * @param _rx_size RX buffer size.
+ * */
+#define NRF_SERIAL_BUFFERS_DEF(_name, _tx_size, _rx_size) \
+ STATIC_ASSERT((_tx_size) <= UINT8_MAX); \
+ STATIC_ASSERT((_rx_size) <= UINT8_MAX); \
+ static uint8_t _name##_txb[_tx_size]; \
+ static uint8_t _name##_rxb[_rx_size]; \
+ static const nrf_serial_buffers_t _name = { \
+ .p_txb = _name##_txb, \
+ .p_rxb = _name##_rxb, \
+ .tx_size = sizeof(_name##_txb), \
+ .rx_size = sizeof(_name##_rxb), \
+ }
+
+/**
+ * @brief Events generated by this module.
+ * */
+typedef enum {
+ NRF_SERIAL_EVENT_TX_DONE, //!< Chunk of data has been sent.
+ NRF_SERIAL_EVENT_RX_DATA, //!< New chunk of data has been received.
+ NRF_SERIAL_EVENT_DRV_ERR, //!< Internal driver error.
+ NRF_SERIAL_EVENT_FIFO_ERR, //!< RX FIFO overrun.
+} nrf_serial_event_t;
+
+/**
+ * @brief Event handler type.
+ * */
+typedef void (*nrf_serial_evt_handler_t)(struct nrf_serial_s const * p_serial,
+ nrf_serial_event_t event);
+
+/**
+ * @brief Sleep handler type.
+ * */
+typedef void (*nrf_serial_sleep_handler_t)(void);
+
+/**
+ * @brief Configuration of UART serial interface.
+ *
+ * @note Use the @ref NRF_SERIAL_CONFIG_DEF macro to create an instance of this
+ * structure.
+ *
+ */
+typedef struct {
+ nrf_serial_mode_t mode; //!< Serial port mode.
+
+ nrf_serial_queues_t const * p_queues; //!< Serial port queues.
+ nrf_serial_buffers_t const * p_buffers; //!< DMA buffers.
+ nrf_serial_evt_handler_t ev_handler; //!< Event handler.
+ nrf_serial_sleep_handler_t sleep_handler; //!< Sleep mode handler.
+} nrf_serial_config_t;
+
+/**
+ * @brief Creates an instance of serial port configuration.
+ *
+ * @param _name Instance name.
+ * @param _mode Serial port mode.
+ * @param _queues Serial port queues. NULL can be passed in @ref NRF_SERIAL_MODE_POLLING mode.
+ * @param _buffers Serial port buffers. NULL can be passed in @ref NRF_SERIAL_MODE_POLLING mode.
+ * @param _ev_handler Serial port event handler. NULL can be passed in any mode.
+ * @param _sleep Serial port sleep handler. NULL can be passed in any mode.
+ * */
+#define NRF_SERIAL_CONFIG_DEF(_name, _mode, _queues, _buffers, _ev_handler, _sleep) \
+ static const nrf_serial_config_t _name = { \
+ .mode = _mode, \
+ .p_queues = _queues, \
+ .p_buffers = _buffers, \
+ .ev_handler = _ev_handler, \
+ .sleep_handler = _sleep, \
+ }
+
+#define NRF_SERIAL_RX_ENABLED_FLAG (1u << 0) //!< Receiver enable flag.
+#define NRF_SERIAL_TX_ENABLED_FLAG (1u << 1) //!< Transmitter enable flag.
+
+/**
+ * @brief Serial port context. Contains all data structures that need
+ * to be stored in RAM memory.
+ * */
+typedef struct {
+ nrf_serial_config_t const * p_config; //!< Serial port configuration.
+
+ nrf_mtx_t write_lock; //!< Write operation lock.
+ nrf_mtx_t read_lock; //!< Read operation lock.
+
+ uint8_t flags; //!< Transmitter/receiver enable flags.
+} nrf_serial_ctx_t;
+
+/**
+ * @brief Serial port instance declaration.
+ *
+ * @note Use @ref NRF_SERIAL_UART_DEF macro to create an instance of this
+ * structure.
+ * */
+struct nrf_serial_s {
+ nrf_drv_uart_t instance; //!< Driver instance.
+ nrf_serial_ctx_t * p_ctx; //!< Driver context.
+
+ app_timer_id_t const * p_tx_timer; //!< TX timeout timer.
+ app_timer_id_t const * p_rx_timer; //!< RX timeout timer.
+};
+
+/**
+ * @brief Creates an instance of a serial port.
+ *
+ * @param _name Instance name.
+ * @param _instance_number Driver instance number (@ref NRF_DRV_UART_INSTANCE).
+ * */
+#define NRF_SERIAL_UART_DEF(_name, _instance_number) \
+ APP_TIMER_DEF(_name##_rx_timer); \
+ APP_TIMER_DEF(_name##_tx_timer); \
+ static nrf_serial_ctx_t _name##_ctx; \
+ static const nrf_serial_t _name = { \
+ .instance = NRF_DRV_UART_INSTANCE(_instance_number), \
+ .p_ctx = &_name##_ctx, \
+ .p_tx_timer = &_name##_tx_timer, \
+ .p_rx_timer = &_name##_rx_timer, \
+ }
+
+
+/**
+ * @brief Maximum value of timeout. API might be blocked indefinitely if this value is
+ * not set.*/
+#define NRF_SERIAL_MAX_TIMEOUT UINT32_MAX
+
+/**
+ * @brief Function for initializing a serial port. Serial port can be initialized
+ * in various modes that are defined by nrf_serial_mode_t.
+ * - NRF_SERIAL_MODE_POLLING - Simple polling mode. API calls will be
+ * synchronous. There is no need to define queues or buffers.
+ * No events will be generated.
+ * - NRF_SERIAL_MODE_IRQ - Interrupt mode. API can be set to work in synchronous or
+ * asynchronous mode. Queues and buffers must be passed
+ * during initialization. Events will be generated if
+ * a non NULL handler is passed as the ev_handler parameter.
+ * - NRF_SERIAL_MODE_DMA - Similar to @ref NRF_SERIAL_MODE_IRQ. Uses EasyDMA.
+ *
+ *
+ * @param p_serial Serial port instance.
+ * @param p_drv_uart_config UART driver configuration. Cannot be NULL.
+ * @param p_config Serial port configuration. Cannot be NULL. This object must be created
+ * using the @ref NRF_SERIAL_CONFIG_DEF macro.
+ *
+ * @return Standard error code.
+ * */
+ret_code_t nrf_serial_init(nrf_serial_t const * p_serial,
+ nrf_drv_uart_config_t const * p_drv_uart_config,
+ nrf_serial_config_t const * p_config);
+
+
+/**
+ * @brief Function for uninitializing a serial port.
+ *
+ * @param p_serial Serial port instance.
+ *
+ * @return Standard error code.
+ * */
+ret_code_t nrf_serial_uninit(nrf_serial_t const * p_serial);
+
+
+/**
+ * @brief Function for writing to a serial port.
+ *
+ * @param p_serial Serial port instance.
+ * @param p_data Transmit buffer pointer.
+ * @param size Transmit buffer size.
+ * @param p_written Amount of data actually written to the serial port.
+ * NULL pointer can be passed.
+ * @param timeout_ms Operation timeout, in milliseconds. Pass 0 to operate in
+ * non blocking mode.
+ *
+ * @return Standard error code.
+ * */
+ret_code_t nrf_serial_write(nrf_serial_t const * p_serial,
+ void const * p_data,
+ size_t size,
+ size_t * p_written,
+ uint32_t timeout_ms);
+
+/**
+ * @brief Function for reading from a serial port.
+ *
+ * @param p_serial Serial port instance.
+ * @param p_data Receive buffer pointer.
+ * @param size Receive buffer size.
+ * @param p_read Amount of data actually read from the serial port.
+ * NULL pointer can be passed.
+ * @param timeout_ms Operation timeout, in milliseconds. Pass 0 to operate in
+ * non blocking mode.
+ *
+ * @return Standard error code.
+ * */
+ret_code_t nrf_serial_read(nrf_serial_t const * p_serial,
+ void * p_data,
+ size_t size,
+ size_t * p_read,
+ uint32_t timeout_ms);
+
+/**
+ * @brief Function for flushing a serial port TX queue.
+ *
+ * @param p_serial Serial port instance.
+ * @param timeout_ms Operation timeout, in milliseconds. Pass 0 to operate in
+ * non blocking mode.
+ * @return Standard error code.
+ * */
+ret_code_t nrf_serial_flush(nrf_serial_t const * p_serial, uint32_t timeout_ms);
+
+
+/**
+ * @brief Function for aborting a serial port transmission.
+ * Aborts the current ongoing transmission and resets TX FIFO.
+ *
+ * @param p_serial Serial port instance.
+ *
+ * @return Standard error code.
+ * */
+ret_code_t nrf_serial_tx_abort(nrf_serial_t const * p_serial);
+
+/**
+ * @brief Function for draining the serial port receive RX FIFO.
+ * Drains HW FIFO and resets RX FIFO.
+ *
+ * @param p_serial Serial port instance.
+ *
+ * @return Standard error code.
+ * */
+ret_code_t nrf_serial_rx_drain(nrf_serial_t const * p_serial);
+
+/** @} */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* NRF_SERIAL_H__ */