uint32_t serial_dfu_transport_init(void) { uint32_t err_code; leds_init(); m_dfu.slip.p_buffer = m_dfu.recv_buffer; m_dfu.slip.current_index = 0; m_dfu.slip.buffer_len = sizeof(m_dfu.recv_buffer); m_dfu.slip.state = SLIP_STATE_DECODING; nrf_drv_uart_config_t uart_config = NRF_DRV_UART_DEFAULT_CONFIG; uart_config.pseltxd = TX_PIN_NUMBER; uart_config.pselrxd = RX_PIN_NUMBER; uart_config.pselcts = CTS_PIN_NUMBER; uart_config.pselrts = RTS_PIN_NUMBER; uart_config.hwfc = NRF_UART_HWFC_ENABLED; uart_config.p_context = &m_dfu; nrf_drv_uart_t instance = NRF_DRV_UART_INSTANCE(0); memcpy(&m_dfu.uart_instance, &instance, sizeof(instance)); err_code = nrf_drv_uart_init(&m_dfu.uart_instance, &uart_config, uart_event_handler); if (err_code != NRF_SUCCESS) { NRF_LOG_ERROR("Failed initializing uart\n"); return err_code; } nrf_drv_uart_rx_enable(&m_dfu.uart_instance); err_code = nrf_drv_uart_rx(&m_dfu.uart_instance, &m_dfu.uart_buffer, 1); if (err_code != NRF_SUCCESS) { NRF_LOG_ERROR("Failed initializing rx\n"); } NRF_LOG_DEBUG("UART initialized\n"); return err_code; }
* 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" 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); 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(&app_uart_inst, rx_buffer, 1); rx_done = true; m_event_handler(&app_uart_event); } else if (p_event->type == NRF_DRV_UART_EVT_ERROR) { app_uart_evt_t app_uart_event;
* 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_LOG) && NRF_MODULE_ENABLED(NRF_LOG_BACKEND_UART) #include "nrf_log_backend_uart.h" #include "nrf_log_backend_serial.h" #include "nrf_log_internal.h" #include "nrf_drv_uart.h" #include "app_error.h" nrf_drv_uart_t m_uart = NRF_DRV_UART_INSTANCE(0); static uint8_t m_string_buff[NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE]; static volatile bool m_xfer_done; static bool m_async_mode; static void uart_evt_handler(nrf_drv_uart_event_t * p_event, void * p_context) { m_xfer_done = true; } static void uart_init(bool async_mode) { nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG; config.pseltxd = NRF_LOG_BACKEND_UART_TX_PIN; config.pselrxd = NRF_UART_PSEL_DISCONNECTED; config.pselcts = NRF_UART_PSEL_DISCONNECTED;
static struct rt_serial_device _serial0_0; #if USE_UART0_1 static struct rt_serial_device _serial0_1; #endif typedef struct { struct rt_serial_device *serial; nrf_drv_uart_t uart; uint32_t rx_pin; uint32_t tx_pin; } UART_CFG_T; UART_CFG_T uart0 = { .uart = NRF_DRV_UART_INSTANCE(0), #ifdef RT_USING_CONSOLE .rx_pin = 8, .tx_pin = 6 #else .rx_pin = 19, .tx_pin = 20 #endif }; #if USE_UART0_1 UART_CFG_T uart1 = { .uart = NRF_DRV_UART_INSTANCE(0), .rx_pin = 3, .tx_pin = 4 };
#include "ser_phy_config_app.h" #endif #include "nrf_drv_uart.h" #include "app_error.h" #include "app_util_platform.h" #define APP_SLIP_END 0xC0 /**< SLIP code for identifying the beginning and end of a packet frame.. */ #define APP_SLIP_ESC 0xDB /**< SLIP escape code. This code is used to specify that the following character is specially encoded. */ #define APP_SLIP_ESC_END 0xDC /**< SLIP special code. When this code follows 0xDB, this character is interpreted as payload data 0xC0.. */ #define APP_SLIP_ESC_ESC 0xDD /**< SLIP special code. When this code follows 0xDB, this character is interpreted as payload data 0xDB. */ #define HDR_SIZE 4 #define CRC_SIZE 2 #define PKT_SIZE (SER_HAL_TRANSPORT_MAX_PKT_SIZE + HDR_SIZE + CRC_SIZE) static const nrf_drv_uart_t m_uart = NRF_DRV_UART_INSTANCE(0); static const nrf_drv_uart_config_t m_uart_config = { .pseltxd = SER_PHY_UART_TX, .pselrxd = SER_PHY_UART_RX, .pselrts = SER_PHY_UART_RTS, .pselcts = SER_PHY_UART_CTS, .p_context = NULL, .interrupt_priority = UART_IRQ_PRIORITY, #if defined(UARTE_PRESENT) && defined(UART_PRESENT) .use_easy_dma = true, #endif // These values are common for application and connectivity, they are // defined in "ser_config.h". .hwfc = SER_PHY_UART_FLOW_CTRL, .parity = SER_PHY_UART_PARITY, .baudrate = (nrf_uart_baudrate_t)SER_PHY_UART_BAUDRATE