MCUXpresso_MIMXRT1052xxxxB/boards/evkbimxrt1050/demo_apps/shell/shell.c
2022-04-08 22:46:35 +08:00

164 lines
4.4 KiB
C

/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
*
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "fsl_debug_console.h"
#include "fsl_component_serial_manager.h"
#include "fsl_shell.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define LED_NUMBERS 1U
#define LED_1_INIT() USER_LED_INIT(LOGIC_LED_OFF)
#define LED_1_ON() USER_LED_ON()
#define LED_1_OFF() USER_LED_OFF()
#define SHELL_Printf PRINTF
/*******************************************************************************
* Prototypes
******************************************************************************/
void Led_Init(void);
static shell_status_t LedControl(shell_handle_t shellHandle, int32_t argc, char **argv);
/*******************************************************************************
* Variables
******************************************************************************/
SHELL_COMMAND_DEFINE(led,
"\r\n\"led arg1 arg2\":\r\n Usage:\r\n arg1: 1|2|3|4... "
" Led index\r\n arg2: on|off Led status\r\n",
LedControl,
2);
SDK_ALIGN(static uint8_t s_shellHandleBuffer[SHELL_HANDLE_SIZE], 4);
static shell_handle_t s_shellHandle;
extern serial_handle_t g_serialHandle;
/*******************************************************************************
* Code
******************************************************************************/
void Led_Init(void)
{
LED_1_INIT();
}
static shell_status_t LedControl(shell_handle_t shellHandle, int32_t argc, char **argv)
{
int32_t kLedIndex = ((int32_t)atoi(argv[1]));
char *kLedCommand = argv[2];
/* Check second argument to control led */
switch (kLedIndex)
{
#if defined(LED_NUMBERS) && LED_NUMBERS > 0
case 1:
if (strcmp(kLedCommand, "on") == 0)
{
LED_1_ON();
}
else if (strcmp(kLedCommand, "off") == 0)
{
LED_1_OFF();
}
else
{
SHELL_Printf("Control conmmand is wrong!\r\n");
}
break;
#endif
#if defined(LED_NUMBERS) && LED_NUMBERS > 1
case 2:
if (strcmp(kLedCommand, "on") == 0)
{
LED_2_ON();
}
else if (strcmp(kLedCommand, "off") == 0)
{
LED_2_OFF();
}
else
{
SHELL_Printf("Control conmmand is wrong!\r\n");
}
break;
#endif
#if defined(LED_NUMBERS) && LED_NUMBERS > 2
case 3:
if (strcmp(kLedCommand, "on") == 0)
{
LED_3_ON();
}
else if (strcmp(kLedCommand, "off") == 0)
{
LED_3_OFF();
}
else
{
SHELL_Printf("Control conmmand is wrong!\r\n");
}
break;
#endif
#if defined(LED_NUMBERS) && LED_NUMBERS > 3
case 4:
if (strcmp(kLedCommand, "on") == 0)
{
LED_4_ON();
}
else if (strcmp(kLedCommand, "off") == 0)
{
LED_4_OFF();
}
else
{
SHELL_Printf("Control conmmand is wrong!\r\n");
}
break;
#endif
default:
SHELL_Printf("LED index is wrong\r\n");
break;
}
return kStatus_SHELL_Success;
}
/*! @brief Main function */
int main(void)
{
BOARD_ConfigMPU();
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
/* Enable clock gate for GPIO1 */
CLOCK_EnableClock(kCLOCK_Gpio1);
/* Init led */
Led_Init();
/* Init SHELL */
s_shellHandle = &s_shellHandleBuffer[0];
SHELL_Init(s_shellHandle, g_serialHandle, "SHELL>> ");
/* Add new command to commands list */
SHELL_RegisterCommand(s_shellHandle, SHELL_COMMAND(led));
while (1)
{
#if !(defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
SHELL_Task(s_shellHandle);
#endif
}
}