Files
2023-05-04 20:56:36 +08:00

338 lines
10 KiB
C

/**
* Copyright (c) 2018 - 2021, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdio.h>
#include <stdbool.h>
#include <stddef.h>
#include <ctype.h>
#include "nrf.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "app_timer.h"
#include "app_error.h"
#include "app_util.h"
#include "nrf_cli.h"
#include "nrf_cli_rtt.h"
#include "nrf_cli_types.h"
#include "boards.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_cli_libuarte.h"
#include "nrf_drv_clock.h"
/* Counter timer. */
APP_TIMER_DEF(m_timer_0);
static uint32_t m_counter;
static bool m_counter_active = false;
/** @brief Command line interface instance. */
#define CLI_EXAMPLE_LOG_QUEUE_SIZE (4)
NRF_CLI_LIBUARTE_DEF(m_cli_libuarte_transport, 256, 256);
NRF_CLI_DEF(m_cli_libuarte,
"libuarte_cli:~$ ",
&m_cli_libuarte_transport.transport,
'\r',
CLI_EXAMPLE_LOG_QUEUE_SIZE);
NRF_CLI_RTT_DEF(m_cli_rtt_transport);
NRF_CLI_DEF(m_cli_rtt,
"rtt_cli:~$ ",
&m_cli_rtt_transport.transport,
'\n',
CLI_EXAMPLE_LOG_QUEUE_SIZE);
static void timer_handle(void * p_context)
{
UNUSED_PARAMETER(p_context);
if (m_counter_active)
{
m_counter++;
NRF_LOG_RAW_INFO("counter = %d\n", m_counter);
}
}
static void cli_start(void)
{
ret_code_t ret;
ret = nrf_cli_start(&m_cli_libuarte);
APP_ERROR_CHECK(ret);
ret = nrf_cli_start(&m_cli_rtt);
APP_ERROR_CHECK(ret);
}
static void cli_init(void)
{
ret_code_t ret;
cli_libuarte_config_t libuarte_config;
libuarte_config.tx_pin = TX_PIN_NUMBER;
libuarte_config.rx_pin = RX_PIN_NUMBER;
libuarte_config.baudrate = NRF_UARTE_BAUDRATE_115200;
libuarte_config.parity = NRF_UARTE_PARITY_EXCLUDED;
libuarte_config.hwfc = NRF_UARTE_HWFC_DISABLED;
ret = nrf_cli_init(&m_cli_libuarte, &libuarte_config, true, true, NRF_LOG_SEVERITY_INFO);
APP_ERROR_CHECK(ret);
ret = nrf_cli_init(&m_cli_rtt, NULL, true, true, NRF_LOG_SEVERITY_INFO);
APP_ERROR_CHECK(ret);
}
static void cli_process(void)
{
nrf_cli_process(&m_cli_libuarte);
nrf_cli_process(&m_cli_rtt);
}
int main(void)
{
ret_code_t ret;
APP_ERROR_CHECK(NRF_LOG_INIT(app_timer_cnt_get));
ret = nrf_drv_clock_init();
APP_ERROR_CHECK(ret);
nrf_drv_clock_lfclk_request(NULL);
ret = app_timer_init();
APP_ERROR_CHECK(ret);
ret = app_timer_create(&m_timer_0, APP_TIMER_MODE_REPEATED, timer_handle);
APP_ERROR_CHECK(ret);
ret = app_timer_start(m_timer_0, APP_TIMER_TICKS(1000), NULL);
APP_ERROR_CHECK(ret);
cli_init();
cli_start();
NRF_LOG_RAW_INFO("Command Line Interface example started.\n");
NRF_LOG_RAW_INFO("Please press the Tab key to see all available commands.\n");
while (true)
{
UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
cli_process();
}
}
/* Command handlers */
static void cmd_print_param(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
for (size_t i = 1; i < argc; i++)
{
nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "argv[%d] = %s\n", i, argv[i]);
}
}
static void cmd_print_all(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
for (size_t i = 1; i < argc; i++)
{
nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "%s ", argv[i]);
}
nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\n");
}
static void cmd_print(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
ASSERT(p_cli);
ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
if ((argc == 1) || nrf_cli_help_requested(p_cli))
{
nrf_cli_help_print(p_cli, NULL, 0);
return;
}
if (argc != 2)
{
nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: bad parameter count\n", argv[0]);
return;
}
nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: unknown parameter: %s\n", argv[0], argv[1]);
}
static void cmd_counter_start(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
if (argc != 1)
{
nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: bad parameter count\n", argv[0]);
return;
}
m_counter_active = true;
}
static void cmd_counter_stop(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
if (argc != 1)
{
nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: bad parameter count\n", argv[0]);
return;
}
m_counter_active = false;
}
static void cmd_counter_reset(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
if (argc != 1)
{
nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: bad parameter count\n", argv[0]);
return;
}
m_counter = 0;
}
static void cmd_counter(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
ASSERT(p_cli);
ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
/* Extra defined dummy option */
static const nrf_cli_getopt_option_t opt[] = {
NRF_CLI_OPT(
"--test",
"-t",
"dummy option help string"
)
};
if ((argc == 1) || nrf_cli_help_requested(p_cli))
{
nrf_cli_help_print(p_cli, opt, ARRAY_SIZE(opt));
return;
}
if (argc != 2)
{
nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: bad parameter count\n", argv[0]);
return;
}
if (!strcmp(argv[1], "-t") || !strcmp(argv[1], "--test"))
{
nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "Dummy test option.\n");
return;
}
/* subcommands have their own handlers and they are not processed here */
nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: unknown parameter: %s\n", argv[0], argv[1]);
}
static void cmd_nordic(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
UNUSED_PARAMETER(argc);
UNUSED_PARAMETER(argv);
if (nrf_cli_help_requested(p_cli))
{
nrf_cli_help_print(p_cli, NULL, 0);
return;
}
nrf_cli_fprintf(p_cli, NRF_CLI_OPTION,
"\n"
" .co:. 'xo, \n"
" .,collllc,. 'ckOOo::,.. \n"
" .:ooooollllllll:'. .;dOOOOOOo:::;;;'. \n"
" 'okxddoooollllllllllll;'ckOOOOOOOOOo:::;;;,,,' \n"
" OOOkxdoooolllllllllllllllldxOOOOOOOo:::;;;,,,'.\n"
" OOOOOOkdoolllllllllllllllllllldxOOOo:::;;;,,,'.\n"
" OOOOOOOOOkxollllllllllllllllllcccldl:::;;;,,,'.\n"
" OOOOOOOOOOOOOxdollllllllllllllccccc::::;;;,,,'.\n"
" OOOOOOOOOOOOOOOOkxdlllllllllllccccc::::;;;,,,'.\n"
" kOOOOOOOOOOOOOOOOOOOkdolllllllccccc::::;;;,,,'.\n"
" kOOOOOOOOOOOOOOOOOOOOOOOxdllllccccc::::;;;,,,'.\n"
" kOOOOOOOOOOOOOOOOOOOOOOOOOOkxolcccc::::;;;,,,'.\n"
" kOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOkdlc::::;;;,,,'.\n"
" xOOOOOOOOOOOxdkOOOOOOOOOOOOOOOOOOOOxoc:;;;,,,'.\n"
" xOOOOOOOOOOOdc::ldkOOOOOOOOOOOOOOOOOOOkdc;,,,''\n"
" xOOOOOOOOOOOdc::;;,;cdkOOOOOOOOOOOOOOOOOOOxl;''\n"
" .lkOOOOOOOOOdc::;;,,''..;oOOOOOOOOOOOOOOOOOOOx'\n"
" .;oOOOOOOdc::;;,. .:xOOOOOOOOOOOOd;. \n"
" .:xOOdc:,. 'ckOOOOkl' \n"
" .od' 'xk, \n"
"\n");
nrf_cli_fprintf(p_cli,NRF_CLI_NORMAL,
" Nordic Semiconductor \n\n");
}
/** @brief Command set array. */
NRF_CLI_CMD_REGISTER(nordic, NULL, "Print Nordic Semiconductor logo.", cmd_nordic);
NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_print)
{
NRF_CLI_CMD(all, NULL, "Print all entered parameters.", cmd_print_all),
NRF_CLI_CMD(param, NULL, "Print each parameter in new line.", cmd_print_param),
NRF_CLI_SUBCMD_SET_END
};
NRF_CLI_CMD_REGISTER(print, &m_sub_print, "print", cmd_print);
NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_counter)
{
NRF_CLI_CMD(reset, NULL, "Reset seconds counter.", cmd_counter_reset),
NRF_CLI_CMD(start, NULL, "Start seconds counter.", cmd_counter_start),
NRF_CLI_CMD(stop, NULL, "Stop seconds counter.", cmd_counter_stop),
NRF_CLI_SUBCMD_SET_END
};
NRF_CLI_CMD_REGISTER(counter,
&m_sub_counter,
"Display seconds on terminal screen",
cmd_counter);