Skip to content

Commit

Permalink
Add override for CLI lib, because of MSDK github issue #1177 analogde…
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanAceto committed Sep 18, 2024
1 parent 2cbb5bd commit 44252b3
Show file tree
Hide file tree
Showing 5 changed files with 637 additions and 0 deletions.
49 changes: 49 additions & 0 deletions main_microcontroller_firmware/MSDK_overrides/CLI/CLI.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
###############################################################################
#
# Copyright (C) 2022-2023 Maxim Integrated Products, Inc. All Rights Reserved.
# (now owned by Analog Devices, Inc.),
# Copyright (C) 2023 Analog Devices, Inc. All Rights Reserved. This software
# is proprietary to Analog Devices, Inc. and its licensors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################

################################################################################
# This file can be included in a project makefile to build the library for the
# project.
###############################################################################

ifeq "$(LIB_CLI_DIR)" ""
# If CLI_DIR is not specified, this Makefile will locate itself.
LIB_CLI_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
endif

IPATH += ${LIB_CLI_DIR}/inc
VPATH += ${LIB_CLI_DIR}/src
SRCS += cli.c

# By default, with USE_CLI_LIB_IRQHANDLER defined, the CLI library will handle the
# UART interrupts internally. Users have the option to define their own UART IRQ
# Handler for the CLI UART in their application. If users choose to define their own
# IRQ handler they should delete this definition of USE_CLI_LIB_IRQHANDLER and call
# MXC_CLI_Handler in their handler function when the CLI is in use.
LIB_CLI_USE_DEFAULT_HANDLER ?= 1
ifeq "$(LIB_CLI_USE_DEFAULT_HANDLER)" "1"
PROJ_CFLAGS += -DUSE_CLI_LIB_IRQHANDLER
endif

# Use absolute paths if building within eclipse environment.
ifeq "$(ECLIPSE)" "1"
SRCS := $(abspath $(SRCS))
endif
81 changes: 81 additions & 0 deletions main_microcontroller_firmware/MSDK_overrides/CLI/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# MSDK CLI Library

## Description

#### What is a Command Line Interface(CLI)?

A command-line interface or command language interpreter (CLI), also known as command-line user interface, console user interface, and character user interface (CUI), is a means of interacting with an embedded system where the user (or client) issues commands to the program in the form of successive lines of text (command lines).

This library provides an extensible command processor to:

- Quickly and easily wrap user functions with a CLI
- Allow developers to get diagnostics and change device parameters interactively
- Easily automate testing of the device, through PC-side scripting

The current CLI Library includes the following features:

- UART interface support
- Custom commands with support for multiple arguments
- No modification of functions required. Users implement a command table and simple wrapper functions.
- Case insensitive commands & arguments.
- White-space insensitive.
- Easy built-in 'help' command and custom help strings.

## Usage Guide

To use the CLI library, users are expected to implement the following steps in their application:

1. Add the following line to your [project.mk](../../USERGUIDE.md#build-configuration-variables) file to enable the CLI library for your project:

:::Makefile
LIB_CLI = 1

2. `#include "cli.h"` in your application code.

3. Define an array of type `const command_t`. This is your command table. Include an array element for each command you want your CLI to support. Each element should define:

1. The name of the command
2. A string showing how to enter the command in the terminal
3. A description of what the command does
4. A function pointer to a wrapper function.

For example, the following is subset of the SDHC example command set:

:::C
const command_t user_commands[] = {{ "format", "format", "Format the Card", handle_format },
{ "mkdir", "mkdir <directory name>", "Create a directory", handle_mkdir }}

4. Implement a handler function for each command.

The handler functions provided to the `user_commands` table are "wrappers" around the code that should be run for each received command. The handlers must conform to the following definition:

Parameters

- `int argc` - This tells the handler function how many arguments were received.
- `char* argv[]` - This array holds the received arguments (in order) as strings. (Note: argv[0] holds the command string, argv[1:argc] are the arguments (if any are passed), and the last element in the argument vector is always a NULL pointer.)

Return Value

- The function needs to return an integer type. It should return 0 (E_NO_ERROR) if the command was executed successfuly, otherwise it should return a negative integer as an error code.

Below is a sample handler function for a "make directory" command, where `mkdir` is some function in the user's application code that does the work.

:::C
int handle_mkdir(int argc, char *argv[]) {
mkdir(argv[1]);
}

As an example, suppose a user entered the command:

:::C
mkdir new_folder

The CLI library will tokenize the command string "mkdir new_folder" into "mkdir" and "new_folder" and assigns them to argv[0] and argv[1] respectively. The library would then determine that this is the "make directory" command and would call "handle_mkdir" with argc=2 and a pointer to the argument vector.

5. Add a call to `MXC_CLI_Init` to the application's startup code.

Pass in the UART instance for the CLI to use, a pointer to the command table, and the number of commands in the command table.

6. (OPTIONAL) The CLI library will enable and handle UART interrupts automatically. Users who would like more control over the interrupt handling have the option to disable the default handler and define their own.

To do so, set the `LIB_CLI_USE_DEFAULT_HANDLER` [build configuration variable](../../USERGUIDE.md#build-configuration-variables) to `0`. Users may now enable and handle the interrupt with a custom function. However, users must call `MXC_CLI_Handler()` from the custom function for the CLI to work.
97 changes: 97 additions & 0 deletions main_microcontroller_firmware/MSDK_overrides/CLI/inc/cli.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/******************************************************************************
*
* Copyright (C) 2022-2023 Maxim Integrated Products, Inc. All Rights Reserved.
* (now owned by Analog Devices, Inc.),
* Copyright (C) 2023 Analog Devices, Inc. All Rights Reserved. This software
* is proprietary to Analog Devices, Inc. and its licensors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/

/*! \file cli.h
\brief A CLI Implementation using a command table to store command string, function pointer and a help string is dispatched.
Details.
1. Line Accumlator
Reads the incoming bytes
Accumulates into a line buffer
Echos char back to the emulator
Handles backspace
2. Prcoess Command
Processes input into a series of tokens
All tokes are seperated by whitespace characters
Lookup first token in a table of functions
Dispatch to handler functions
*/

#ifndef LIBRARIES_CLI_INC_CLI_H_
#define LIBRARIES_CLI_INC_CLI_H_

#include <stdint.h>
#include "uart.h"

/**
* @brief Command handler function prototype. Once a command is entered in the CLI, it is parsed
* ("tokenized") into an argument vector. The argument counter and argument vector are
* passed to the command's handler function where the command is executed.
*
* @param argc Number of tokens in the argument vector
* @param argv[] Array of arguments storing different tokens of the command string in the
* same order as they were passed in the command line. (argv[0] is the command,
* argv[1:argc] are the arguments (if any are passed), and the last element in the
* argument vector is always a NULL pointer.)
*
* @returns E_NO_ERROR if successful, otherwise an error code (error code must be a negative integer)
*/
typedef int (*command_handler_t)(int argc, char *argv[]);

/**
* @brief Structure used to define the commands supported by the CLI
*/
typedef struct {
const char *cmd; /**< name of the command (as it should be entered on the command line) */
const char *usage; /**< string to show how the command should be entered on the command line */
const char *description; /**< string describing what the command does */
command_handler_t handler; /**< function pointer of the command handler function */
} command_t;

/**
* @brief Initializes the CLI state variables and configures the uart for CLI operations.
*
* @param uart Pointer to UART instance to use for the CLI
* @param commands Pointer to the command table storing user-defined CLI commands
* @param num_commands Number of commands in the command table
*
* @return E_NO_ERROR if successful, otherwise an error code.
*/
int MXC_CLI_Init(mxc_uart_regs_t *uart, const command_t *commands, unsigned int num_commands);

/**
* @brief Shuts down the CLI. (UART will remain enabled.)
*
* @return E_NO_ERROR if successful, otheriwse an error code.
*/
int MXC_CLI_Shutdown(void);

/**
* @brief IRQ Handler for the CLI UART. This function should be called from the
* MXC_UARTx_Handler in the user application.
*/
void MXC_CLI_Handler(void);

#endif // LIBRARIES_CLI_INC_CLI_H_
10 changes: 10 additions & 0 deletions main_microcontroller_firmware/MSDK_overrides/CLI/libinfo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name":"CLI",
"ipaths":[
"inc"
],
"vpaths":[
"src"
],
"whitelist":"False"
}
Loading

0 comments on commit 44252b3

Please sign in to comment.