Command Line Interface: Difference between revisions

From Embedded Workshop
Jump to navigation Jump to search
No edit summary
No edit summary
Line 6: Line 6:
  testing many of your functions, and validating your recently added code.
  testing many of your functions, and validating your recently added code.


'''Components needed:'''
'''Software components needed:'''
  * Serial character output method - printf() support
  * Serial character output method - printf() support
  * Serial character input method
  * Serial character input method
Line 12: Line 12:
  * Line buffer parser - split the command line up into '''words''', with the expectation that the first '''word'''
  * Line buffer parser - split the command line up into '''words''', with the expectation that the first '''word'''
     is a command, with any following '''words''' being command line parameters/arguments
     is a command, with any following '''words''' being command line parameters/arguments
  * Command table with expected number of arguments count
  * Command table with expected number of arguments
  * Argument/'''word''' counter - '''argc'''
  * Argument/'''word''' counter - '''argc'''
  * Array of character pointers - '''argv'''
  * Array of character pointers - '''argv'''

Revision as of 14:45, 19 November 2020

Command Line Interface

Once you have a serial interface for debug / printf() output, and have the ability to read serial characters entered from a terminal program, you're ready to implement a command line interface.
Why a Command Line Interface?

Main reason - control
With the command line interface, you can interact with different functionality you've created within your project,
testing many of your functions, and validating your recently added code.

Software components needed:

* Serial character output method - printf() support
* Serial character input method
* Line buffer editor - minimal
* Line buffer parser - split the command line up into words, with the expectation that the first word
   is a command, with any following words being command line parameters/arguments
* Command table with expected number of arguments
* Argument/word counter - argc
* Array of character pointers - argv

Serial Character Input and Output

When using many of the STM32 NEUCLEO boards, the development board will often connect one of the processor's
serial ports to the on-board STM32F103CBT6 debugger/loader/JTAG/USB-Serial interface device.  This on-board device creates
a USB-Serial interface when you connect your development board to a host computer.  (Check your NEUCLEO board's schematic.)
To use this interface, you need to indicate, via the new project setup wizard, that you need a "connectivity" module,
usually, UART2.  Enable this UART2 for full duplex operation, 115,200 baud, 8 data bits, no parity, 1 stop bit.  When you
connect from the host side, using a terminal program, such at Tera-term, you'll connect to the STM32-STLink USB
serial port, and use the same configuration options.

Serial Output, Serial Input, and printf() Support

To use the printf() library, sending printf() output to the USB serial port, we need to define our own fputc().
I put the following in main.c, in the "Private user code" section, #0 as follows:

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
// Defining an fputc() function allows the printf() to work as expected
int fputc(int ch, FILE *f)
{
   UNUSED(f);
   uint8_t c = (uint8_t)ch;
   HAL_StatusTypeDef status = HAL_UART_Transmit(&huart2,&c,1,50); // send character to UART2 with 50ms timeout
   if(HAL_OK == status)
       return ch;
   else
       return EOF;
}

// For serial input - read a character from the serial port:
int fgetc(FILE *f)
{
   UNUSED(f);
   uint8_t c;
   HAL_StatusTypeDef status = HAL_UART_Receive(&huart2,&c,1,50); // read character from UART2 with 50ms timeout
   if(HAL_OK == status)
       return c;
   else
       return EOF;
}
/* USER CODE END 0 */