From 3061ecca3d0fdfb87dabbf5f63c9e06c2a30f53a Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 23 Aug 2018 17:08:59 +0200 Subject: o Initial import. --- .../examples/peripheral/cli/main.c | 368 +++++++++++++++++++++ 1 file changed, 368 insertions(+) create mode 100644 thirdparty/nRF5_SDK_15.0.0_a53641a/examples/peripheral/cli/main.c (limited to 'thirdparty/nRF5_SDK_15.0.0_a53641a/examples/peripheral/cli/main.c') diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/examples/peripheral/cli/main.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/examples/peripheral/cli/main.c new file mode 100644 index 0000000..8a277ab --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/examples/peripheral/cli/main.c @@ -0,0 +1,368 @@ +/** + * Copyright (c) 2016 - 2018, 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 +#include +#include + +#include "nrf.h" +#include "nrf_drv_clock.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_log_backend_flash.h" +#include "nrf_fstorage_nvmc.h" + +#if defined(APP_USBD_ENABLED) && APP_USBD_ENABLED +#define CLI_OVER_USB_CDC_ACM 1 +#else +#define CLI_OVER_USB_CDC_ACM 0 +#endif + +#if CLI_OVER_USB_CDC_ACM +#include "nrf_cli_cdc_acm.h" +#include "nrf_drv_usbd.h" +#include "app_usbd_core.h" +#include "app_usbd.h" +#include "app_usbd_string_desc.h" +#include "app_usbd_cdc_acm.h" +#endif //CLI_OVER_USB_CDC_ACM + +#if defined(TX_PIN_NUMBER) && defined(RX_PIN_NUMBER) +#define CLI_OVER_UART 1 +#else +#define CLI_OVER_UART 0 +#endif + +#if CLI_OVER_UART +#include "nrf_cli_uart.h" +#endif + +/* If enabled then CYCCNT (high resolution) timestamp is used for the logger. */ +#define USE_CYCCNT_TIMESTAMP_FOR_LOG 0 + +/**@file + * @defgroup CLI_example main.c + * + * @{ + * + */ + +#if NRF_LOG_BACKEND_FLASHLOG_ENABLED +NRF_LOG_BACKEND_FLASHLOG_DEF(m_flash_log_backend); +#endif + +#if NRF_LOG_BACKEND_CRASHLOG_ENABLED +NRF_LOG_BACKEND_CRASHLOG_DEF(m_crash_log_backend); +#endif + +/* Counter timer. */ +APP_TIMER_DEF(m_timer_0); + +/* Declared in demo_cli.c */ +extern uint32_t m_counter; +extern bool m_counter_active; + +#if CLI_OVER_USB_CDC_ACM + +/** + * @brief Enable power USB detection + * + * Configure if example supports USB port connection + */ +#ifndef USBD_POWER_DETECTION +#define USBD_POWER_DETECTION true +#endif + + +static void usbd_user_ev_handler(app_usbd_event_type_t event) +{ + switch (event) + { + case APP_USBD_EVT_STOPPED: + app_usbd_disable(); + break; + case APP_USBD_EVT_POWER_DETECTED: + if (!nrf_drv_usbd_is_enabled()) + { + app_usbd_enable(); + } + break; + case APP_USBD_EVT_POWER_REMOVED: + app_usbd_stop(); + break; + case APP_USBD_EVT_POWER_READY: + app_usbd_start(); + break; + default: + break; + } +} + +#endif //CLI_OVER_USB_CDC_ACM + +/** + * @brief Command line interface instance + * */ +#define CLI_EXAMPLE_LOG_QUEUE_SIZE (4) + +#if CLI_OVER_USB_CDC_ACM +NRF_CLI_CDC_ACM_DEF(m_cli_cdc_acm_transport); +NRF_CLI_DEF(m_cli_cdc_acm, + "usb_cli:~$ ", + &m_cli_cdc_acm_transport.transport, + '\r', + CLI_EXAMPLE_LOG_QUEUE_SIZE); +#endif //CLI_OVER_USB_CDC_ACM + +#if CLI_OVER_UART +NRF_CLI_UART_DEF(m_cli_uart_transport, 0, 64, 16); +NRF_CLI_DEF(m_cli_uart, + "uart_cli:~$ ", + &m_cli_uart_transport.transport, + '\r', + CLI_EXAMPLE_LOG_QUEUE_SIZE); +#endif + +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\r\n", m_counter); + } +} + +static void cli_start(void) +{ + ret_code_t ret; + +#if CLI_OVER_USB_CDC_ACM + ret = nrf_cli_start(&m_cli_cdc_acm); + APP_ERROR_CHECK(ret); +#endif + +#if CLI_OVER_UART + ret = nrf_cli_start(&m_cli_uart); + APP_ERROR_CHECK(ret); +#endif + + ret = nrf_cli_start(&m_cli_rtt); + APP_ERROR_CHECK(ret); +} + +static void cli_init(void) +{ + ret_code_t ret; + +#if CLI_OVER_USB_CDC_ACM + ret = nrf_cli_init(&m_cli_cdc_acm, NULL, true, true, NRF_LOG_SEVERITY_INFO); + APP_ERROR_CHECK(ret); +#endif + +#if CLI_OVER_UART + nrf_drv_uart_config_t uart_config = NRF_DRV_UART_DEFAULT_CONFIG; + uart_config.pseltxd = TX_PIN_NUMBER; + uart_config.pselrxd = RX_PIN_NUMBER; + uart_config.hwfc = NRF_UART_HWFC_DISABLED; + ret = nrf_cli_init(&m_cli_uart, &uart_config, true, true, NRF_LOG_SEVERITY_INFO); + APP_ERROR_CHECK(ret); +#endif + + ret = nrf_cli_init(&m_cli_rtt, NULL, true, true, NRF_LOG_SEVERITY_INFO); + APP_ERROR_CHECK(ret); +} + + +static void usbd_init(void) +{ +#if CLI_OVER_USB_CDC_ACM + ret_code_t ret; + static const app_usbd_config_t usbd_config = { + .ev_handler = app_usbd_event_execute, + .ev_state_proc = usbd_user_ev_handler + }; + ret = app_usbd_init(&usbd_config); + APP_ERROR_CHECK(ret); + + app_usbd_class_inst_t const * class_cdc_acm = + app_usbd_cdc_acm_class_inst_get(&nrf_cli_cdc_acm); + ret = app_usbd_class_append(class_cdc_acm); + APP_ERROR_CHECK(ret); + + if (USBD_POWER_DETECTION) + { + ret = app_usbd_power_events_enable(); + APP_ERROR_CHECK(ret); + } + else + { + NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now"); + + app_usbd_enable(); + app_usbd_start(); + } + + /* Give some time for the host to enumerate and connect to the USB CDC port */ + nrf_delay_ms(1000); +#endif +} + + +static void cli_process(void) +{ +#if CLI_OVER_USB_CDC_ACM + nrf_cli_process(&m_cli_cdc_acm); +#endif + +#if CLI_OVER_UART + nrf_cli_process(&m_cli_uart); +#endif + + nrf_cli_process(&m_cli_rtt); +} + + +static void flashlog_init(void) +{ + ret_code_t ret; + int32_t backend_id; + + ret = nrf_log_backend_flash_init(&nrf_fstorage_nvmc); + APP_ERROR_CHECK(ret); +#if NRF_LOG_BACKEND_FLASHLOG_ENABLED + backend_id = nrf_log_backend_add(&m_flash_log_backend.backend, NRF_LOG_SEVERITY_WARNING); + APP_ERROR_CHECK_BOOL(backend_id >= 0); + + nrf_log_backend_enable(&m_flash_log_backend.backend); +#endif + +#if NRF_LOG_BACKEND_CRASHLOG_ENABLED + backend_id = nrf_log_backend_add(&m_crash_log_backend.backend, NRF_LOG_SEVERITY_INFO); + APP_ERROR_CHECK_BOOL(backend_id >= 0); + + nrf_log_backend_enable(&m_crash_log_backend.backend); +#endif +} + + +uint32_t cyccnt_get(void) +{ + return DWT->CYCCNT; +} + + +int main(void) +{ + ret_code_t ret; + + if (USE_CYCCNT_TIMESTAMP_FOR_LOG) + { + CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; + DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; + DWT->CYCCNT = 0; + APP_ERROR_CHECK(NRF_LOG_INIT(cyccnt_get, 64000000)); + } + else + { + 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(); + + usbd_init(); + + cli_start(); + + flashlog_init(); + + NRF_LOG_RAW_INFO("Command Line Interface example started.\r\n"); + NRF_LOG_RAW_INFO("Please press the Tab key to see all available commands.\r\n"); + + while (true) + { + UNUSED_RETURN_VALUE(NRF_LOG_PROCESS()); +#if CLI_OVER_USB_CDC_ACM && APP_USBD_CONFIG_EVENT_QUEUE_ENABLE + while (app_usbd_event_queue_process()) + { + /* Nothing to do */ + } +#endif + cli_process(); + } +} + +/** @} */ -- cgit v1.2.3