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

UART communication between nrf52 DK & other microcontroller

Hi,

I would like to establish UART communication between nrf52 DK & other microcontroller. I checked the uart example (peripheral folder) in SDK 15. I was not able to flash the code to nrf52 DK. But then I figured out that it does not use a softdevice. To which pins should I connect the microcontroller? So could you point out what all do I need to change? I am combining this with hid keyboard example.

  • Hi Sonal,

    This example does not use a softdevice since you are not sending data over BLE. The DK use pins 6 and 9 for RX and TX in UART, but you can use any GPIO pin for UART as long as the pin is not used by another peripheral already.

    Since pins 6 and 8 are routed to the interface MCU, I recommend you to configure other pins for UART, for instance pins 11 and 12, changing RX_PIN_NUMBER and TX_PIN_NUMBER in the SDK.

    You should also connect the other microcontroller to the same ground as your DK. You can see the nRF52 DK board connectors here.

    Best Regards,

    Marjeris

  • Hi msromero,

    Thank you for your reply. I'm trying to combine hid keyboard with .../peripheral/uart example. In bsp_event_handler, which enum do I use to continuously send data received from uart (connected to microcontroller) instead of key presses? Note data needs to be sent only after bluetooth connection made. This data needs to be sent in input report. Should there be a time assigned to it similar to what is done in heart rate project. I was trying something as below-

    typedef struct
    {
    uint8_t arr;
    } arr_var;

    static arr_var arr_data;

    static uint32_t send_values()
    {
    uint32_t err_code;
    err_code = ble_hids_inp_rep_send(&m_hids, 0, 8, (unsigned char*)&arr_data, m_conn_handle);
    return err_code;
    }

    void Data_Init()
    {
    memset(&arr_data,0,sizeof(arr_data));
    }

    Inside bsp_event_handler------->
    default:
    if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
    {
    while (app_uart_get(&(arr_data.arr)) != NRF_SUCCESS);
    // while (app_uart_put(cr) != NRF_SUCCESS);
    if (arr_data.arr)// == 'q' || cr == 'Q')
    {

    bsp_board_led_invert(4);
    nrf_delay_ms(500);
    }
    send_values();
    }
    break;

    Is this correct?

  • Hi Sonal,

    There are some limitations about how many characters (letters/keys) you can send in one HID transfer (Input Report). Please see this post here for more information.

    I recommend you to open a new case for this project (HID keyboard + UART), since the original post was about which pins to use in UART communication.

    Best regards,

    Marjeris

  • Hi msromero,

    nrf isnt sending out any data. My code is as follows -

    #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"
    #include "boards.h"
    #include "nrf_uart.h"
    #include "nrf_uarte.h"
    
    #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. */
    
    uint8_t cr;
    
    void uart_error_handle(app_uart_evt_t * p_event)
    {
    //	if (p_event->evt_type == APP_UART_DATA_READY)
    //    {
    //		while (app_uart_get(&cr) != NRF_SUCCESS){};
    //		if (cr == '9')
    //        {
    //            bsp_board_led_invert(1);
    //            nrf_delay_ms(500);
    //        }
    //		//while (app_uart_put('?') != NRF_SUCCESS);
    //	}
    //	else if (p_event->evt_type == APP_UART_TX_EMPTY)
    //    {
    //        while (app_uart_put('?') != NRF_SUCCESS);
    //    }
        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);
        }	
    }
    
    #define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED /* When UART is used for communication with the host do not use flow control.*/
    
    int main(void)
    {
        uint32_t err_code;
        bsp_board_init(BSP_INIT_LEDS);
        const app_uart_comm_params_t comm_params = 
    	{
              RX_PIN_NUMBER,
              TX_PIN_NUMBER,
              0,/*RTS_PIN_NUMBER,*/
              0,/*CTS_PIN_NUMBER,*/
              UART_HWFC,
              false,
              NRF_UART_BAUDRATE_9600//460800
          };
        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);
        printf("\r\nUART example started.\r\n");
    	
    	for (uint8_t i = 0; i < 10; i++)
    	{
    		while (app_uart_put(i) != NRF_SUCCESS);
    	}
    
        while(true)
        {
    		
            //while (app_uart_get(&cr) != NRF_SUCCESS);
            //while (app_uart_put(cr) != NRF_SUCCESS);
    //        if (cr == '9')
    //        {
    //            bsp_board_led_invert(1);
    //            nrf_delay_ms(500);
    //        }
        }
    }

Related