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
  • Hello, 

    I started a project from scratch and I want to write a simple UART printf solution for debugging for my NRF52832 on PCA10040 DK.

    Are you familiar with the RETARGET option for stdio functions?
    In essence, it does exactly what you are looking for, for the printf function. 

    I know there's a LOG module but it seems too complicated for my use case.

    I understand. If I could ask, is there anything in particular that makes you think it is too complicated for your project?
    I highly recommend checking out the features that come with the logger module - especially its deferred logging feature, and the possibility to have different logging severity levels.

    I'm using a custom build system (it's googlable) using cmake. The system basically includes a module and all of its dependencies.

    To minimize potential future confusion, could you elaborate on this?

    I'm flashing the code alongside SoftDevice, could that make a difference??? Any help is greatly appreciated. Thanks!

    Yes, inclusion of the SoftDevice could definitely make a difference. 
    If you would like to create a BLE peripheral application I would recommend that you rather start from the BLE peripheral template example, and then add peripherals etc. to this example, rather than having to add the BLE portion to a non-BLE example. This way is almost always easier, than having to add the BLE part to a peripheral example.
    This ensures that the general project configurations are already in place to accommodate the SoftDevice - plus, you do not then have to 'reinvent the wheel' of adding the BLE stack configuration and initiation.

    Would you consider instead adding the UART part to the BLE template application, and see if this does not resolve the issues you mention?
    If not, could you please elaborate on what you mean when you say that you have tried lots of stuff and nothing seems to work. Does it compile without warning? How is it behaving differently than you had expected? if so, how does the behavior differ?

    Looking forward to resolving this issue together!

    Best regards,
    Karl

  • I'm aware of the RETARGET option and I have it enabled in my sdk_config.h

    I didn't want to look into LOG module in more detail because all I need is a simple printf statement here and there.Do you think I should if I'll be just reading the data from 4 TWI sensors and sending it to SD card or over BLE for further processing?

    I'm using https://www.polidea.com/blog/nrf5-cmake-tutorial-building-nrf52-projects-made-easy/ for my build system. Application compiles without warnings.

    I will try adding my code to the BLE peripheral example although I'm pretty sure I took a linker script from there. Eventually I intend to add BLE to my project so thank you for your recommendation.

    Not sure if you saw my reply to my own post, I described there how it's behaving differently than I expected in current setup.

  • squeeky said:

    I'm aware of the RETARGET option and I have it enabled in my sdk_config.h

    I didn't want to look into LOG module in more detail because all I need is a simple printf statement here and there.Do you think I should if I'll be just reading the data from 4 TWI sensors and sending it to SD card or over BLE for further processing?

    I am glad to hear that! I do think that the logger module is a good idea to look into, yes.
    I dare say that familiarizing with and making use of the logger module guaranteed will make future debugging easier for you. Fortunately, the logger module is already added to the BLE peripheral template - so if you should want to use it, you need only read up on the documentation I linked in my previous reply to know how to use it.
    Please be advised that the BLE peripheral example by default uses the deferred logging feature, and only does log processing as part of its idle_state_handler. So, no logs will be processed and output until the idle_state_handler is entered - which might be different from what you would have expected.

    squeeky said:
    I will try adding my code to the BLE peripheral example although I'm pretty sure I took a linker script from there. Eventually I intend to add BLE to my project so thank you for your recommendation.

    Great, I am happy to hear that! It will definitely save you time and potential headaches when it comes to adding the BLE part to your project. If you do not intend to use BLE right away, you can just remove the advertising_start functions from the main function.

    squeeky said:
    Not sure if you saw my reply to my own post, I described there how it's behaving differently than I expected in current setup.

    You are absolutely correct - I did not see your other comment - my mistake.
    If you are moving your project to the BLE peripheral template then I suggest that we come back to that issue when you have it up and running. If you are not doing this immediately then I ask that you share your sdk_config file that you used when your saw the strange behavior in your previous comment.

    Best regards,
    Karl 

  • I did what you suggested and I got the same problem. I built ble_app_template and it worked fine, the led was flashing until I connected with my phone. Then I commented everything out and added my code. I enabled UART and RETARGET in the new sdk_config.h.

    I checked settings in PUTTY, baud rate is 115200,  parity is disabled so it's not that nor the hardware because example/peripherals/uart works.

    Could it be the build system? Everything builds without warnings so I'm not sure how that would be the case. I'll try to build that example with my build system just to rule that out.

    One difference as well is that my app is flashed alongside softdevice while examples/peripherals/uart is flashed from address 0x00000000.

    Here's my sdk_config.h

    https://gofile.io/d/88F68c

  • I tried building examples/peripheral/uart with my build system and it doesn't work so there's something wrong with the build system. It probably doesn't compile and link retarget.c file correctly. Thank you for your time.

  • It works! My build system didn't include retarget.c. Thank you for your time and recommendations. You probably saved me from a lot of headache. Take care.

  • Hello again Squeeky,

    squeeky said:
    It works!

    That is great, I am glad to hear that you were able to identify and resolve the issue!

    squeeky said:
    Thank you for your time and recommendations. You probably saved me from a lot of headache. Take care.

    Thank you for saying so, Squeeky!
    It is no problem at all, I am happy to help!

    Please do not hesitate to open a new ticket if you should encounter any issues or questions in the future.

    Good luck with your development!

    Best regards,
    Karl

Reply
  • Hello again Squeeky,

    squeeky said:
    It works!

    That is great, I am glad to hear that you were able to identify and resolve the issue!

    squeeky said:
    Thank you for your time and recommendations. You probably saved me from a lot of headache. Take care.

    Thank you for saying so, Squeeky!
    It is no problem at all, I am happy to help!

    Please do not hesitate to open a new ticket if you should encounter any issues or questions in the future.

    Good luck with your development!

    Best regards,
    Karl

Children
No Data
Related