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

Simple UART code not working

Hi,

I started a project from scratch and I want to write a simple UART printf solution for debugging for my NRF52832 on PCA10040 DK. I know there's a LOG module but it seems too complicated for my use case.

I'm using sdk_config.h from examples/peripherals/uart, and I'm using a custom build system (it's googlable) using cmake. The system basically includes a module and all of its dependencies.

I tried uart example and everything works as it should so the pin numbers are correct and serial settings on PUTTY are correct. I tried lots of stuff but nothing seems to work. I'm flashing the code alongside SoftDevice, could that make a difference??? Any help is greatly appreciated. Thanks!

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#include "app_uart.h"
#include "bsp.h"
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif

const app_uart_comm_params_t uart_conf = {
    .rx_pin_no = 30,
    .tx_pin_no = 31,
    .rts_pin_no = RTS_PIN_NUMBER,
    .cts_pin_no = CTS_PIN_NUMBER,
    .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
    .use_parity = false,
    .baud_rate = NRF_UART_BAUDRATE_115200
};

int main(void)
{
    uint32_t err_code;
    bsp_board_init(BSP_INIT_LEDS);
    APP_UART_INIT(&uart_conf,
        NULL, APP_IRQ_PRIORITY_LOWEST, err_code);
    if (err_code == NRF_SUCCESS)
        bsp_board_leds_on();
    else
        bsp_board_leds_off();

    printf("\r\nHello\r\n");
    for(;;)
    {
        uint8_t ch = 0;
        while (app_uart_get(&ch) != NRF_SUCCESS);
        while (app_uart_put(ch) != NRF_SUCCESS);
        if (ch == 't')
        {
            bsp_board_leds_off();
            while(1)
            {
                ;
            }
        }
    }
    return 0;
}

Parents
  • Ok, I got it semi working with using app_uart_fifo module. I'm not sure why basic UART module doesn't work.

    But I'm not getting the output that I want.

    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    
    #include "app_uart.h"
    #include "bsp.h"
    #if defined (UART_PRESENT)
    #include "nrf_uart.h"
    #endif
    #if defined (UARTE_PRESENT)
    #include "nrf_uarte.h"
    #endif
    
    #define UART_TX_BUF_SIZE 256
    #define UART_RX_BUF_SIZE 256
    
    void uart_error_handle(app_uart_evt_t * p_event)
    {
        ;
    }
    
    const app_uart_comm_params_t uart_conf = {
        .rx_pin_no = 30,
        .tx_pin_no = 31,
        .rts_pin_no = RTS_PIN_NUMBER,
        .cts_pin_no = CTS_PIN_NUMBER,
        .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
        .use_parity = false,
        .baud_rate = NRF_UART_BAUDRATE_115200
    };
    
    int main(void)
    {
        uint32_t err_code;
        bsp_board_init(BSP_INIT_LEDS);
        APP_UART_FIFO_INIT(&uart_conf, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE,
                           uart_error_handle, APP_IRQ_PRIORITY_LOWEST,
                           err_code);
        if (err_code == NRF_SUCCESS)
            bsp_board_leds_on();
        else
            bsp_board_leds_off();
    
        printf("\r\n1234567890123456789012345678901234567890\r\n");
        for(;;)
        {
            uint8_t ch = 0;
            while (app_uart_get(&ch) != NRF_SUCCESS);
            while (app_uart_put(ch) != NRF_SUCCESS);
            if (ch == 't')
            {
                bsp_board_leds_off();
                while(1)
                {
                    printf("\r\n1234567890\r\n");
                }
            }
        }
        return 0;
    }
    

    With this code I'm getting "47" printed out at start. When I press ' t ', I get "14578990" printed continuously which is obviously wrong. My baud rate is correct.

Reply
  • Ok, I got it semi working with using app_uart_fifo module. I'm not sure why basic UART module doesn't work.

    But I'm not getting the output that I want.

    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    
    #include "app_uart.h"
    #include "bsp.h"
    #if defined (UART_PRESENT)
    #include "nrf_uart.h"
    #endif
    #if defined (UARTE_PRESENT)
    #include "nrf_uarte.h"
    #endif
    
    #define UART_TX_BUF_SIZE 256
    #define UART_RX_BUF_SIZE 256
    
    void uart_error_handle(app_uart_evt_t * p_event)
    {
        ;
    }
    
    const app_uart_comm_params_t uart_conf = {
        .rx_pin_no = 30,
        .tx_pin_no = 31,
        .rts_pin_no = RTS_PIN_NUMBER,
        .cts_pin_no = CTS_PIN_NUMBER,
        .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
        .use_parity = false,
        .baud_rate = NRF_UART_BAUDRATE_115200
    };
    
    int main(void)
    {
        uint32_t err_code;
        bsp_board_init(BSP_INIT_LEDS);
        APP_UART_FIFO_INIT(&uart_conf, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE,
                           uart_error_handle, APP_IRQ_PRIORITY_LOWEST,
                           err_code);
        if (err_code == NRF_SUCCESS)
            bsp_board_leds_on();
        else
            bsp_board_leds_off();
    
        printf("\r\n1234567890123456789012345678901234567890\r\n");
        for(;;)
        {
            uint8_t ch = 0;
            while (app_uart_get(&ch) != NRF_SUCCESS);
            while (app_uart_put(ch) != NRF_SUCCESS);
            if (ch == 't')
            {
                bsp_board_leds_off();
                while(1)
                {
                    printf("\r\n1234567890\r\n");
                }
            }
        }
        return 0;
    }
    

    With this code I'm getting "47" printed out at start. When I press ' t ', I get "14578990" printed continuously which is obviously wrong. My baud rate is correct.

Children
No Data
Related