This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/MSP430F5529: Using scanf for user input in an embedded program

Part Number: MSP430F5529
Other Parts Discussed in Thread: ADS1231, ADS1231REF,

Tool/software: Code Composer Studio

Hi,

I'm interfacing an ADS1231REF board with a MSP430F5529LP, in which the 24 bit digital code from the ADS1231 is converted into a meaningful weight value. At the beginning of my program, I ask the user what unit they would like to display their weight in using the scanf command. Depending on the integer (between 0 and 4) the user inputs, the corresponding unit is selected.

However, when I play my program, it properly executes the printf function, but skips over scanning for the user input and returning the "unit" variable. My question is, how can I debug scanf commands in embedded programs? I've attached the code below. I appreciate any help!

uint8_t selectUnit(void)

{

uint8_t unit;

printf("What unit would you like your weight to display in?");

fflush(stdout);

scanf("%d", &unit);

printf("The unit value is %d\n", unit);

return unit;

}

  • It might be best to build the input line yourself and pass it to sscnf(), with fgetc()

    Are you pressing return so stdio knows that a full line is available?

  • By building the input line myself, do you mean creating a text file with predefined user inputs for the sscanf() and testing my program with different values?

    Yes, I am pressing return. When I run the program, it prints the question asking which unit but does not give me an opportunity to provide input because it continues on with the rest of the program. When I single step into the program and make sure to input a 1 and return in the console, I get no luck either.

  • Yamen Alimam said:
    However, when I play my program, it properly executes the printf function, but skips over scanning for the user input and returning the "unit" variable.

    scanf will require a decent amount of heap. Make sure --printf_support is set to full and the heap size is set to 600+ bytes

    Thanks

    ki

  • How do I set the heap size and --printf_support? I'm using the GNU compiler and linker. It looks like my heap size is already set to 835M total.

  • No, I mean put the line in a buffer using fgetc().

    However, it really looks like stdin is not working properly, which is another reason to use fgetc().

    Did you check errno?

  • I'm not very familiar with the fgetc() function. Do I pass a string to fgetc() and sscanf the result? For example, at one point in my program I ask the user to enter the mass they plan to use to calibrate the scale. How would I use fgetc()?

    How can I access the errno file?

  • Yamen Alimam said:
    How do I set the heap size and --printf_support? I'm using the GNU compiler and linker.

    Sorry, I thought you were using the TI compiler. I have not tried this with GCC. I'll try to see if I can get this to work with gcc

  • Ki said:
    I have not tried this with GCC. I'll try to see if I can get this to work with gcc

    With CCS 10.1 I created a new project for a MSP430F5529 using the GNU v9.2.0.50 compiler.

    The msp430f5529.ld linker script sets up the memory between the top of .bss and the bottom on the .stack section for the heap - there is no specific linker option to set the heap size. I did note that if attempt to make an allocation for which their is insufficient space in the heap then GNU malloc reports "Heap and stack collision" on the console and aborts the program by calling exit(), whereas with the TI compiler malloc() returns NULL if there is insufficient space.

    scanf works with the TI v20.2.2 compiler, but fails with the GNU v9.2.0.50 compiler.

    With the GNU compiler scanf() returns -1 and sets errno to ENOSYS (Function not implemented). I will look and see if the run-time library supplied with the GNU v9.2.0.50 compiler can be configured to enable scanf support.

  • Chester Gillon said:
    With the GNU compiler scanf() returns -1 and sets errno to ENOSYS (Function not implemented).

    From debugging found that the read function in msp430-gcc-9.2.0.50_linux64/bin/../lib/gcc/msp430-elf/9.2.0/../../../../msp430-elf/lib/large/libnosys.a(ciosyscalls.o) is setting errno to ENOSYS and returning -1.

    Downloaded the full source from https://www.ti.com/tool/download/MSP430-GCC-OPENSOURCE. The msp430-gcc-9.2.0.50-source-full\newlib\libgloss\msp430\ciosyscalls.S source file shows the read function is a stub which sets errno to ENOSYS and returns -1.

    I.e. to get scanf to work with the GNU v9.2.0.50 compiler supplied with CCS 10 I think you would need to create a read function which makes use of CIO. msp430-gcc-9.2.0.50-source-full\newlib\libgloss\msp430\write.c shows the write function is implemented to support CIO.

  • Chester Gillon said:
    I.e. to get scanf to work with the GNU v9.2.0.50 compiler supplied with CCS 10 I think you would need to create a read function which makes use of CIO.

    The attached example shows scanf working with the GNU v9.2.0.50 compiler (using the Debug_GNU build configuration). It contains:

    a. The cio.h file copied from the GNU v9.2.0.50 compiler source code.

    b. The read.c function which implements the _DTREAD CIO read call.

    Note that example will only only work with reading standard-input, since the GNU v9.2.0.50 run-time libraries for open, close, fstat and lseek functions are still stubs which return a failure.

    MSP430F5529_GCC_scanf.zip

  • Thanks Chester! Your example worked for me.

    ki