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

printf / UART debug does not work properly on nRF51822 s110 ble_app_hts example.

Hi,

Thanks for taking the time looking into issue. We are using Keil uVision 5.14. Our target is a custom board (BMD200 eval Module based on nRF51822) We were able to run uart example with expected outputs to Putty through the UART by following the example from: developer.nordicsemi.com/.../a00054.html Which is awesome.

We switched the project to ble_app_hts (developer.nordicsemi.com/.../a00071.html), copied what worked in the uart example and add printf() to places like main(), device_manager_evt_handler(), on_ble_evt(), on_adv_evt, etc, etc. However only the printfs in main() works. We also tried adding the "ENABLE_DEBUG_LOG_SUPPORT" define to the project c/c++ tab, and use the app_trace_init(). The only thing that's printed was a '[' character. We then added "printf("Hello!: \n\r");" right after app_trace_init();, and "Hel" was then printed, nothing more.

What's more interesting is in the uart example, instead of initializing uart in main(), we use the app_trace.c API, and it still works as expected.

So in summary, without "ENABLE_DEBUG_LOG_SUPPORT" defined, by using printf() and uart initialization in main() of the ble_app_hts example, every print statement within main() work, just not outside of main(). with "ENABLE_DEBUG_LOG_SUPPORT" defined, and using the app_trace.c API, only a few characters are printed. Just wondering if anyone has encountered this...

Also, this might be considered a separate question (if so, I can start a new thread.) I thought the printf() did not work in functions other than main() because they are callbacks running from different context invoked by the softdevice layer (I am really new at this, so just make assumptions here.), So I decided to use an enum to track which functions/callbacks the program has executed and print the enum in the main() loop. However, the enum value doesn't change at all after the "advertise" state.

It doesn't seem to make sense because we have the nRF master control app running, and connected to it, and requesting notifications (battery level) from it. We also changed the enum value in "hts_sim_measurement(ble_hts_meas_t * p_meas)" function, which is supposed to be executed very x seconds. But still, the enum value just doesn't change. Did I miss anything here?

Thank you! (Please find attached the main.c. It's quite messy with our modification. The purpose is to illustrate the issues I have mentioned, where we used printf()s and where we use the enum value to indicate the state of the program.)main.c

  • When using softdevice, the timings for UART read and writes has been changed. The interrupt handler execution could be delayed causing buffer overflows. Try to increase the value for UART_RX_BUF_SIZE and UART_TX_BUF_SIZE to a greater value so that fifo library has more space to buffer. I am not sure if this will solve the problem, but it will at least rule out insufficient buffer problem.

  • Sorry if it is not your case but if you are planning to use UART only for debugging purposes while running with JLink then I'd advise to use SEGGER_RTT_WriteString instead of printf and JLink RTT viewer on PC. It doesn't have delays or buffer overflow issues.

  • Thanks for your reply Aryan. I have tried increased the TX buffer size from 256 to 1024, and 2048; RX Buffer size from 1 to 32 and 256; Code compiles and runs, but still, only "hel" is printed instead of "hello" and the rest of the debug messages are not printed as well. I just found a similar issue (devzone.nordicsemi.com/.../) where Ole provided "ble_app_hrs_printf.zip". I think I will give this approach a try and update my findings.

    Meanwhile, I am just wondering if you would have any thoughts on the enum value not being update issue (I assign different "state enum" values in the functions in main.c, and then print out the values in the main() loop. But somehow, the enum variable just doesn't seem to change value..)

    Thanks.

  • Alex, Thanks for your input! That's exactly what I'd like to do, print some debug messages through UART. This SEGGER_RTT_WriteString API seems very interesting and worth a try - I will provide updates on whether it works or not. Thanks again, really appreciate the help!

  • The enum might not be changing because the the advertiser is probably always on and there is no connection event that has happened to change the state? Did you make sure that the device was in connection? Also the enum is operating in different contexts so i think you should make it volatile and hopefully perform operations on it in critical sections, Just thinking aloud here.

Related