This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Using custom RX/TX UART pins on nrf51822

Hi,

I have developed an application on an nrf51dk, with SDK12.3 (as it was the latest that I could make it work with). It is running fine on the DK, but when moving to the custom pcb i ran into a few problems, one of them being that the DK uses pins TX=P0.09 and RX=P0.11 and our custom board uses TX=P0.17 and RX=P0.18.

Coincidentally, my code is still monitoring the buttons on the DK board, and because of this, and although the UART is not working on my custom board, while debugging it I was seeing logs for bsp_event_t BSP_EVENT_KEY_0, which is actually pin P0.17 (the pin my board uses for UART RX), so my guess is that UART activity on my custom board is being interpreted as a button event.

So, the next step was to disable the call to bsp_init(), in the hopes I would be disabling button monitoring, but that is still not enough to make the UART work.

Question: what are the steps to allow using custom pins for UART on the nrf51822, under SDK12.3?

Best regards,

Ricardo

  • Hi,

    Simply changing the pins and make sure no other peripheral is controlling the pins should be enough.

    Can you share the code you used for configuring and initializing the UART peripheral/library?

    Best regards,
    Jørgen

  • void uart_init(on_uart_message_handler_t incoming_message_callback)
    {
        uint32_t err_code;

        on_uart_incoming_message_handler = incoming_message_callback;

        const app_uart_comm_params_t comm_params = {
            //RX_PIN_NUMBER, TX_PIN_NUMBER,
            18,17,
            RTS_PIN_NUMBER, CTS_PIN_NUMBER,
            APP_UART_FLOW_CONTROL_DISABLED,
            false,
            MACRO_CONCAT_2(UART_BAUDRATE_BAUDRATE_Baud9600)
        };

        APP_UART_FIFO_INIT(&comm_params, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE,
                           uart_event_handle, APP_IRQ_PRIORITY_LOWEST, err_code);
        APP_ERROR_CHECK(err_code);
    }

  • As a note, I am also not using logs through UART.

    On sdk_config, i have:

    #define NRF_LOG_BACKEND_SERIAL_USES_UART 0

    #define NRF_LOG_BACKEND_SERIAL_USES_RTT 1

    #define NRF_LOG_ENABLED 1

  • I tested your code in the UART example in SDK 12.3.0:

    /**
     * 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.
     * 
     */
    
    /** @file
     * @defgroup uart_example_main main.c
     * @{
     * @ingroup uart_example
     * @brief UART Example Application main file.
     *
     * This file contains the source code for a sample application using UART.
     *
     */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include "app_uart.h"
    #include "app_error.h"
    #include "nrf_delay.h"
    #include "nrf.h"
    #include "bsp.h"
    
    //#define ENABLE_LOOPBACK_TEST  /**< if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback. */
    
    #define MAX_TEST_DATA_BYTES     (15U)                /**< max number of test bytes to be used for tx and rx. */
    #define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
    #define UART_RX_BUF_SIZE 256                         /**< UART RX buffer size. */
    
    void uart_error_handle(app_uart_evt_t * p_event)
    {
        if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
        {
            APP_ERROR_HANDLER(p_event->data.error_communication);
        }
        else if (p_event->evt_type == APP_UART_FIFO_ERROR)
        {
            APP_ERROR_HANDLER(p_event->data.error_code);
        }
    }
    
    
    void uart_init(void)
    {
        uint32_t err_code;
    
        const app_uart_comm_params_t comm_params = {
            //RX_PIN_NUMBER, TX_PIN_NUMBER,
            18,17,
            RTS_PIN_NUMBER, CTS_PIN_NUMBER,
            APP_UART_FLOW_CONTROL_DISABLED,
            false,
            UART_BAUDRATE_BAUDRATE_Baud9600
        };
    
        APP_UART_FIFO_INIT(&comm_params, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE,
                           uart_error_handle, APP_IRQ_PRIORITY_LOWEST, err_code);
        APP_ERROR_CHECK(err_code);
    }
    
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        bsp_board_leds_init();
    
        uart_init();
    
        printf("\r\nStart: \r\n");
    
        while (true)
        {
            uint8_t cr;
            while (app_uart_get(&cr) != NRF_SUCCESS);
            while (app_uart_put(cr) != NRF_SUCCESS);
    
            if (cr == 'q' || cr == 'Q')
            {
                printf(" \r\nExit!\r\n");
    
                while (true)
                {
                    // Do nothing.
                }
            }
        }
    }
    
    
    /** @} */
    

    I tested this on a nRF51 DK by connecting P0.17 to P0.09 and P0.18 to P0.11. It seems to work fine. Can you test this on your board?

  • Hi Jorgen,

    My problem was actually of the PEBCAK category. The lines were named from the perspective of the other chip, so in reversing the TX and RX pins on my comm_params, all started working.

    Many thanks for your support.

    I accepted your last reply as the answer because I find that the code you posted my be useful for someone else wanting to debug their UART configuration.

    Best regards,

    Ricardo

Related