debugprobe icon indicating copy to clipboard operation
debugprobe copied to clipboard

stdio output disabled if within a conditional

Open drankinatty opened this issue 3 years ago • 0 comments

I've recently picked up a couple of pico boards and I'm using picoprobe and openocd from Linux to flash and run with minicom as the output terminal. All works great. However, there is a nasty bug, and I'm not sure if it is in picoprobe or the SDK itself that disables output if the output is conditional within the main program loop.

This pops up when you don't want output generated each iteration of the main loop. Most times, I just want output if some condition has changed. Like if the new temperature is different from the one already displayed, etc. I wrote a short test case that demonstrates the issue:

#include <stdio.h>
#include "pico/stdlib.h"

#define CEILING 20
#define FLOOR   -CEILING

#define USECONDITIONAL 1

static volatile int value = 0;
static volatile bool dir = true;

int main (void) {
  
  stdio_init_all();
  
  while (1) {
    
    bool current_dir = dir;
    
    value = dir ? value + 1 : value - 1;
    
    if (value == CEILING || value == FLOOR) {
      dir = !dir;
    }
    
#ifndef USECONDITIONAL
    printf ("value: % 2d   dir: %s\n", value, dir ? "up" : "down");
#else
    if (dir != current_dir) {
      printf ("value: % 2d   dir: %s\n", value, dir ? "up" : "down");
    }
#endif    
    
    sleep_ms (500);
  }
}

Above, if USECONDITIONAL is not defined, output to the terminal is fine, counts up, reverses direction, counts down, etc.. However, if it IS defined, all out is disabled. That should not occur. Instead, the only output shown should be when value reaches 20 and the direction changes from up to down and at -20 when the opposite happens.

A short CMakeLists.txt using bug-testcase as the filename could be:

# Set minimum required version of CMake
cmake_minimum_required (VERSION 3.12)

#include build functions from Pico SDK
include ($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)

# Set name of project (as PROJECT_NAME) and C/C++ Standards
project (bug-testcase C CXX ASM)
set (CMAKE_C_STANDARD 11)
set (CMAKE_CXX_STANDARD 17)

# Creates a pico-sdk subdirectory in our project for the libraries
pico_sdk_init()

# point out the CMake, where to find the executable source file
add_executable (${PROJECT_NAME} ${PROJECT_NAME}.c)

target_link_libraries (${PROJECT_NAME} pico_stdlib hardware_adc)

# enable uart output, disable usb output
pico_enable_stdio_uart (${PROJECT_NAME} 1)
pico_enable_stdio_usb (${PROJECT_NAME} 0)

# create map/bin/hex file etc.
pico_add_extra_outputs (${PROJECT_NAME})

Like I said, I'm not sure if this is picoprobe itself or the SDK, but this issue makes debugging with conditional output almost impossible. Let me know if this is SDK itself and I'm happy to move the bug there (if you can migrate it, that's fine too if it needs to move).

drankinatty avatar Sep 06 '22 03:09 drankinatty