able to build
This commit is contained in:
@ -1,33 +1,62 @@
|
||||
/* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
|
||||
*
|
||||
* The information contained herein is property of Nordic Semiconductor ASA.
|
||||
* Terms and conditions of usage are described in detail in NORDIC
|
||||
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
|
||||
*
|
||||
* Licensees are granted free, non-transferable use of the information. NO
|
||||
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
|
||||
* the file.
|
||||
*
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, 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(APP_UART)
|
||||
#include "app_uart.h"
|
||||
#include "nrf_drv_uart.h"
|
||||
#include "nrf_assert.h"
|
||||
#include "sdk_common.h"
|
||||
|
||||
static uint8_t tx_buffer[1];
|
||||
static uint8_t rx_buffer[1];
|
||||
static volatile bool rx_done;
|
||||
static app_uart_event_handler_t m_event_handler; /**< Event handler function. */
|
||||
static nrf_drv_uart_t app_uart_inst = NRF_DRV_UART_INSTANCE(APP_UART_DRIVER_INSTANCE);
|
||||
|
||||
void uart_event_handler(nrf_drv_uart_event_t * p_event, void* p_context)
|
||||
static void uart_event_handler(nrf_drv_uart_event_t * p_event, void* p_context)
|
||||
{
|
||||
if (p_event->type == NRF_DRV_UART_EVT_RX_DONE)
|
||||
{
|
||||
app_uart_evt_t app_uart_event;
|
||||
app_uart_event.evt_type = APP_UART_DATA;
|
||||
app_uart_event.data.value = p_event->data.rxtx.p_data[0];
|
||||
(void)nrf_drv_uart_rx(rx_buffer,1);
|
||||
(void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
|
||||
rx_done = true;
|
||||
m_event_handler(&app_uart_event);
|
||||
}
|
||||
@ -36,7 +65,7 @@ void uart_event_handler(nrf_drv_uart_event_t * p_event, void* p_context)
|
||||
app_uart_evt_t app_uart_event;
|
||||
app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR;
|
||||
app_uart_event.data.error_communication = p_event->data.error.error_mask;
|
||||
(void)nrf_drv_uart_rx(rx_buffer,1);
|
||||
(void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
|
||||
m_event_handler(&app_uart_event);
|
||||
}
|
||||
else if (p_event->type == NRF_DRV_UART_EVT_TX_DONE)
|
||||
@ -69,21 +98,25 @@ uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params,
|
||||
|
||||
rx_done = false;
|
||||
|
||||
if (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_LOW_POWER)
|
||||
{
|
||||
return NRF_ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
uint32_t err_code = nrf_drv_uart_init(&config, uart_event_handler);
|
||||
uint32_t err_code = nrf_drv_uart_init(&app_uart_inst, &config, uart_event_handler);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
|
||||
#ifdef NRF52
|
||||
if (!config.use_easy_dma)
|
||||
#endif
|
||||
// Turn on receiver if RX pin is connected
|
||||
if (p_comm_params->rx_pin_no != UART_PIN_DISCONNECTED)
|
||||
{
|
||||
nrf_drv_uart_rx_enable();
|
||||
#ifdef UARTE_PRESENT
|
||||
if (!config.use_easy_dma)
|
||||
#endif
|
||||
{
|
||||
nrf_drv_uart_rx_enable(&app_uart_inst);
|
||||
}
|
||||
|
||||
return nrf_drv_uart_rx(&app_uart_inst, rx_buffer,1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
return nrf_drv_uart_rx(rx_buffer,1);
|
||||
}
|
||||
|
||||
|
||||
@ -94,6 +127,7 @@ uint32_t app_uart_get(uint8_t * p_byte)
|
||||
if (rx_done)
|
||||
{
|
||||
*p_byte = rx_buffer[0];
|
||||
rx_done = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -105,7 +139,7 @@ uint32_t app_uart_get(uint8_t * p_byte)
|
||||
uint32_t app_uart_put(uint8_t byte)
|
||||
{
|
||||
tx_buffer[0] = byte;
|
||||
ret_code_t ret = nrf_drv_uart_tx(tx_buffer,1);
|
||||
ret_code_t ret = nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
|
||||
if (NRF_ERROR_BUSY == ret)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
@ -127,6 +161,7 @@ uint32_t app_uart_flush(void)
|
||||
|
||||
uint32_t app_uart_close(void)
|
||||
{
|
||||
nrf_drv_uart_uninit();
|
||||
nrf_drv_uart_uninit(&app_uart_inst);
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
#endif //NRF_MODULE_ENABLED(APP_UART)
|
||||
|
@ -1,15 +1,42 @@
|
||||
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
|
||||
*
|
||||
* The information contained herein is property of Nordic Semiconductor ASA.
|
||||
* Terms and conditions of usage are described in detail in NORDIC
|
||||
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
|
||||
*
|
||||
* Licensees are granted free, non-transferable use of the information. NO
|
||||
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
|
||||
* the file.
|
||||
*
|
||||
/**
|
||||
* Copyright (c) 2013 - 2017, 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.
|
||||
*
|
||||
*/
|
||||
|
||||
/**@file
|
||||
*
|
||||
* @defgroup app_uart UART module
|
||||
@ -26,6 +53,10 @@
|
||||
#include <stdbool.h>
|
||||
#include "app_util_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define UART_PIN_DISCONNECTED 0xFFFFFFFF /**< Value indicating that no pin is connected to this UART register. */
|
||||
|
||||
/**@brief UART Flow Control modes for the peripheral.
|
||||
@ -34,17 +65,16 @@ typedef enum
|
||||
{
|
||||
APP_UART_FLOW_CONTROL_DISABLED, /**< UART Hw Flow Control is disabled. */
|
||||
APP_UART_FLOW_CONTROL_ENABLED, /**< Standard UART Hw Flow Control is enabled. */
|
||||
APP_UART_FLOW_CONTROL_LOW_POWER /**< Specialized UART Hw Flow Control is used. The Low Power setting allows the \nRFXX to Power Off the UART module when CTS is in-active, and re-enabling the UART when the CTS signal becomes active. This allows the \nRFXX to safe power by only using the UART module when it is needed by the remote site. */
|
||||
} app_uart_flow_control_t;
|
||||
|
||||
/**@brief UART communication structure holding configuration settings for the peripheral.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t rx_pin_no; /**< RX pin number. */
|
||||
uint8_t tx_pin_no; /**< TX pin number. */
|
||||
uint8_t rts_pin_no; /**< RTS pin number, only used if flow control is enabled. */
|
||||
uint8_t cts_pin_no; /**< CTS pin number, only used if flow control is enabled. */
|
||||
uint32_t rx_pin_no; /**< RX pin number. */
|
||||
uint32_t tx_pin_no; /**< TX pin number. */
|
||||
uint32_t rts_pin_no; /**< RTS pin number, only used if flow control is enabled. */
|
||||
uint32_t cts_pin_no; /**< CTS pin number, only used if flow control is enabled. */
|
||||
app_uart_flow_control_t flow_control; /**< Flow control setting, if flow control is used, the system will use low power UART mode, based on CTS signal. */
|
||||
bool use_parity; /**< Even parity if TRUE, no parity if FALSE. */
|
||||
uint32_t baud_rate; /**< Baud rate configuration. */
|
||||
@ -222,6 +252,11 @@ uint32_t app_uart_flush(void);
|
||||
uint32_t app_uart_close(void);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //APP_UART_H__
|
||||
|
||||
/** @} */
|
||||
|
@ -1,20 +1,50 @@
|
||||
/* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
|
||||
*
|
||||
* The information contained herein is property of Nordic Semiconductor ASA.
|
||||
* Terms and conditions of usage are described in detail in NORDIC
|
||||
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
|
||||
*
|
||||
* Licensees are granted free, non-transferable use of the information. NO
|
||||
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
|
||||
* the file.
|
||||
*
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, 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(APP_UART)
|
||||
#include "app_uart.h"
|
||||
#include "app_fifo.h"
|
||||
#include "nrf_drv_uart.h"
|
||||
#include "nrf_assert.h"
|
||||
#include "sdk_common.h"
|
||||
|
||||
static nrf_drv_uart_t app_uart_inst = NRF_DRV_UART_INSTANCE(APP_UART_DRIVER_INSTANCE);
|
||||
|
||||
static __INLINE uint32_t fifo_length(app_fifo_t * const fifo)
|
||||
{
|
||||
@ -28,6 +58,7 @@ static __INLINE uint32_t fifo_length(app_fifo_t * const fifo)
|
||||
static app_uart_event_handler_t m_event_handler; /**< Event handler function. */
|
||||
static uint8_t tx_buffer[1];
|
||||
static uint8_t rx_buffer[1];
|
||||
static bool m_rx_ovf;
|
||||
|
||||
static app_fifo_t m_rx_fifo; /**< RX FIFO buffer for storing data received on the UART until the application fetches them using app_uart_get(). */
|
||||
static app_fifo_t m_tx_fifo; /**< TX FIFO buffer for storing data to be transmitted on the UART when TXD is ready. Data is put to the buffer on using app_uart_put(). */
|
||||
@ -35,55 +66,66 @@ static app_fifo_t m_tx_fifo; /**<
|
||||
static void uart_event_handler(nrf_drv_uart_event_t * p_event, void* p_context)
|
||||
{
|
||||
app_uart_evt_t app_uart_event;
|
||||
uint32_t err_code;
|
||||
|
||||
if (p_event->type == NRF_DRV_UART_EVT_RX_DONE)
|
||||
switch (p_event->type)
|
||||
{
|
||||
// Write received byte to FIFO
|
||||
uint32_t err_code = app_fifo_put(&m_rx_fifo, p_event->data.rxtx.p_data[0]);
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
app_uart_event.evt_type = APP_UART_FIFO_ERROR;
|
||||
app_uart_event.data.error_code = err_code;
|
||||
case NRF_DRV_UART_EVT_RX_DONE:
|
||||
// Write received byte to FIFO.
|
||||
err_code = app_fifo_put(&m_rx_fifo, p_event->data.rxtx.p_data[0]);
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
app_uart_event.evt_type = APP_UART_FIFO_ERROR;
|
||||
app_uart_event.data.error_code = err_code;
|
||||
m_event_handler(&app_uart_event);
|
||||
}
|
||||
// Notify that there are data available.
|
||||
else if (FIFO_LENGTH(m_rx_fifo) != 0)
|
||||
{
|
||||
app_uart_event.evt_type = APP_UART_DATA_READY;
|
||||
m_event_handler(&app_uart_event);
|
||||
}
|
||||
|
||||
// Start new RX if size in buffer.
|
||||
if (FIFO_LENGTH(m_rx_fifo) <= m_rx_fifo.buf_size_mask)
|
||||
{
|
||||
(void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Overflow in RX FIFO.
|
||||
m_rx_ovf = true;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case NRF_DRV_UART_EVT_ERROR:
|
||||
app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR;
|
||||
app_uart_event.data.error_communication = p_event->data.error.error_mask;
|
||||
(void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
|
||||
m_event_handler(&app_uart_event);
|
||||
}
|
||||
// Notify that new data is available if this was first byte put in the buffer.
|
||||
else if (FIFO_LENGTH(m_rx_fifo) == 1)
|
||||
{
|
||||
app_uart_event.evt_type = APP_UART_DATA_READY;
|
||||
m_event_handler(&app_uart_event);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Do nothing, only send event if first byte was added or overflow in FIFO occurred.
|
||||
}
|
||||
if (FIFO_LENGTH(m_rx_fifo) <= m_rx_fifo.buf_size_mask)
|
||||
{
|
||||
(void)nrf_drv_uart_rx(rx_buffer, 1);
|
||||
}
|
||||
}
|
||||
else if (p_event->type == NRF_DRV_UART_EVT_ERROR)
|
||||
{
|
||||
app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR;
|
||||
app_uart_event.data.error_communication = p_event->data.error.error_mask;
|
||||
(void)nrf_drv_uart_rx(rx_buffer, 1);
|
||||
m_event_handler(&app_uart_event);
|
||||
}
|
||||
else if (p_event->type == NRF_DRV_UART_EVT_TX_DONE)
|
||||
{
|
||||
// Get next byte from FIFO.
|
||||
if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS)
|
||||
{
|
||||
(void)nrf_drv_uart_tx(tx_buffer, 1);
|
||||
}
|
||||
if (FIFO_LENGTH(m_tx_fifo) == 0)
|
||||
{
|
||||
// Last byte from FIFO transmitted, notify the application.
|
||||
app_uart_event.evt_type = APP_UART_TX_EMPTY;
|
||||
m_event_handler(&app_uart_event);
|
||||
}
|
||||
break;
|
||||
|
||||
case NRF_DRV_UART_EVT_TX_DONE:
|
||||
// Get next byte from FIFO.
|
||||
if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS)
|
||||
{
|
||||
(void)nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Last byte from FIFO transmitted, notify the application.
|
||||
app_uart_event.evt_type = APP_UART_TX_EMPTY;
|
||||
m_event_handler(&app_uart_event);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params,
|
||||
app_uart_buffers_t * p_buffers,
|
||||
app_uart_event_handler_t event_handler,
|
||||
@ -117,18 +159,29 @@ uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params,
|
||||
config.pselrxd = p_comm_params->rx_pin_no;
|
||||
config.pseltxd = p_comm_params->tx_pin_no;
|
||||
|
||||
err_code = nrf_drv_uart_init(&config, uart_event_handler);
|
||||
err_code = nrf_drv_uart_init(&app_uart_inst, &config, uart_event_handler);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
m_rx_ovf = false;
|
||||
|
||||
#ifdef NRF52
|
||||
if (!config.use_easy_dma)
|
||||
#endif
|
||||
// Turn on receiver if RX pin is connected
|
||||
if (p_comm_params->rx_pin_no != UART_PIN_DISCONNECTED)
|
||||
{
|
||||
nrf_drv_uart_rx_enable();
|
||||
#ifdef UARTE_PRESENT
|
||||
if (!config.use_easy_dma)
|
||||
#endif
|
||||
{
|
||||
nrf_drv_uart_rx_enable(&app_uart_inst);
|
||||
}
|
||||
|
||||
return nrf_drv_uart_rx(&app_uart_inst, rx_buffer,1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
return nrf_drv_uart_rx(rx_buffer,1);
|
||||
}
|
||||
|
||||
|
||||
uint32_t app_uart_flush(void)
|
||||
{
|
||||
uint32_t err_code;
|
||||
@ -142,25 +195,31 @@ uint32_t app_uart_flush(void)
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
uint32_t app_uart_get(uint8_t * p_byte)
|
||||
{
|
||||
ASSERT(p_byte);
|
||||
bool rx_ovf = m_rx_ovf;
|
||||
|
||||
ret_code_t err_code = app_fifo_get(&m_rx_fifo, p_byte);
|
||||
|
||||
// If FIFO was full new request to receive one byte was not scheduled. Must be done here.
|
||||
if (FIFO_LENGTH(m_rx_fifo) == m_rx_fifo.buf_size_mask)
|
||||
if (rx_ovf)
|
||||
{
|
||||
uint32_t err_code = nrf_drv_uart_rx(rx_buffer,1);
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return NRF_ERROR_NOT_FOUND;
|
||||
}
|
||||
m_rx_ovf = false;
|
||||
uint32_t uart_err_code = nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
|
||||
|
||||
// RX resume should never fail.
|
||||
APP_ERROR_CHECK(uart_err_code);
|
||||
}
|
||||
return app_fifo_get(&m_rx_fifo, p_byte);
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
|
||||
uint32_t app_uart_put(uint8_t byte)
|
||||
{
|
||||
uint32_t err_code;
|
||||
|
||||
err_code = app_fifo_put(&m_tx_fifo, byte);
|
||||
if (err_code == NRF_SUCCESS)
|
||||
{
|
||||
@ -168,7 +227,7 @@ uint32_t app_uart_put(uint8_t byte)
|
||||
// (in 'uart_event_handler') when all preceding bytes are transmitted.
|
||||
// But if UART is not transmitting anything at the moment, we must start
|
||||
// a new transmission here.
|
||||
if (!nrf_drv_uart_tx_in_progress())
|
||||
if (!nrf_drv_uart_tx_in_progress(&app_uart_inst))
|
||||
{
|
||||
// This operation should be almost always successful, since we've
|
||||
// just added a byte to FIFO, but if some bigger delay occurred
|
||||
@ -176,16 +235,17 @@ uint32_t app_uart_put(uint8_t byte)
|
||||
// that time, FIFO might be empty already.
|
||||
if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS)
|
||||
{
|
||||
err_code = nrf_drv_uart_tx(tx_buffer, 1);
|
||||
err_code = nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
|
||||
uint32_t app_uart_close(void)
|
||||
{
|
||||
nrf_drv_uart_uninit();
|
||||
nrf_drv_uart_uninit(&app_uart_inst);
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
#endif //NRF_MODULE_ENABLED(APP_UART)
|
||||
|
@ -1,36 +1,72 @@
|
||||
/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
|
||||
*
|
||||
* The information contained herein is property of Nordic Semiconductor ASA.
|
||||
* Terms and conditions of usage are described in detail in NORDIC
|
||||
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
|
||||
*
|
||||
* Licensees are granted free, non-transferable use of the information. NO
|
||||
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
|
||||
* the file.
|
||||
*
|
||||
/**
|
||||
* Copyright (c) 2014 - 2017, 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"
|
||||
/** @file
|
||||
*
|
||||
* @defgroup retarget Retarget layer for stdio functions
|
||||
* @{
|
||||
* @ingroup app_common
|
||||
* @} */
|
||||
#if NRF_MODULE_ENABLED(RETARGET)
|
||||
#if !defined(NRF_LOG_USES_RTT) || NRF_LOG_USES_RTT != 1
|
||||
#if !defined(HAS_SIMPLE_UART_RETARGET)
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include "app_uart.h"
|
||||
#include "nordic_common.h"
|
||||
#include "nrf_error.h"
|
||||
|
||||
#if !defined(__ICCARM__)
|
||||
struct __FILE
|
||||
{
|
||||
int handle;
|
||||
};
|
||||
#endif
|
||||
|
||||
#if defined(__CC_ARM)
|
||||
|
||||
// This part is taken from MDK-ARM template file and is required here to prevent
|
||||
// linker from selecting libraries functions that use semihosting and failing
|
||||
// because of multiple definitions of fgetc() and fputc().
|
||||
// Refer to: http://www.keil.com/support/man/docs/gsac/gsac_retargetcortex.htm
|
||||
// -- BEGIN --
|
||||
struct __FILE { int handle; /* Add whatever you need here */ };
|
||||
FILE __stdout;
|
||||
FILE __stdin;
|
||||
// --- END ---
|
||||
|
||||
|
||||
#if defined(__CC_ARM) || defined(__ICCARM__)
|
||||
int fgetc(FILE * p_file)
|
||||
{
|
||||
uint8_t input;
|
||||
@ -41,7 +77,6 @@ int fgetc(FILE * p_file)
|
||||
return input;
|
||||
}
|
||||
|
||||
|
||||
int fputc(int ch, FILE * p_file)
|
||||
{
|
||||
UNUSED_PARAMETER(p_file);
|
||||
@ -50,8 +85,27 @@ int fputc(int ch, FILE * p_file)
|
||||
return ch;
|
||||
}
|
||||
|
||||
#elif defined(__GNUC__)
|
||||
#elif defined(__GNUC__) && defined(__SES_ARM)
|
||||
|
||||
int __getchar(FILE * p_file)
|
||||
{
|
||||
uint8_t input;
|
||||
while (app_uart_get(&input) == NRF_ERROR_NOT_FOUND)
|
||||
{
|
||||
// No implementation needed.
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
int __putchar(int ch, FILE * p_file)
|
||||
{
|
||||
UNUSED_PARAMETER(p_file);
|
||||
|
||||
UNUSED_VARIABLE(app_uart_put((uint8_t)ch));
|
||||
return ch;
|
||||
}
|
||||
|
||||
#elif defined(__GNUC__) && !defined(__SES_ARM)
|
||||
|
||||
int _write(int file, const char * p_char, int len)
|
||||
{
|
||||
@ -67,7 +121,6 @@ int _write(int file, const char * p_char, int len)
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
int _read(int file, char * p_char, int len)
|
||||
{
|
||||
UNUSED_PARAMETER(file);
|
||||
@ -78,24 +131,46 @@ int _read(int file, char * p_char, int len)
|
||||
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
#elif defined(__ICCARM__)
|
||||
|
||||
#if defined(__ICCARM__)
|
||||
|
||||
__ATTRIBUTES size_t __write(int file, const unsigned char * p_char, size_t len)
|
||||
size_t __write(int handle, const unsigned char * buffer, size_t size)
|
||||
{
|
||||
int i;
|
||||
|
||||
UNUSED_PARAMETER(file);
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
UNUSED_PARAMETER(handle);
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
UNUSED_VARIABLE(app_uart_put(*p_char++));
|
||||
UNUSED_VARIABLE(app_uart_put(*buffer++));
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
size_t __read(int handle, unsigned char * buffer, size_t size)
|
||||
{
|
||||
UNUSED_PARAMETER(handle);
|
||||
UNUSED_PARAMETER(size);
|
||||
while (app_uart_get((uint8_t *)buffer) == NRF_ERROR_NOT_FOUND)
|
||||
{
|
||||
// No implementation needed.
|
||||
}
|
||||
|
||||
return len;
|
||||
return 1;
|
||||
}
|
||||
|
||||
long __lseek(int handle, long offset, int whence)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
int __close(int handle)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int remove(const char * filename)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif // !defined(HAS_SIMPLE_UART_RETARGET)
|
||||
#endif // NRF_LOG_USES_RTT != 1
|
||||
#endif //NRF_MODULE_ENABLED(RETARGET)
|
||||
|
Reference in New Issue
Block a user