This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

With NRF52840 UARTE0, I need to increase the buffer size from 1 byte to 64 bytes. But with this cannot receive 1 byte packet.

Hi,

I'm using a buffer size of 64 bytes using function

nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 64);

When I receive only one byte packet from the remote, I get EVENT_RXDRDY (which I expect). I then wait for about 10 ms.

1. EVENT_RXDRDY

2. delay_ms(10)

3. trigger FLUSHRX

4. Read UARTE register AMOUNT

I get AMOUNT = 0 all the time

Why I'm not getting AMOUNT = 1?

This code works if the buffer size is set to 1.

Please help. Let me know I need to do some trick.

Parents
  • Hi,

    • Is an EVENT_RXDRDY event generated between the step 1 and step 4
    • Do you stop the peripheral with TASKS_STOPRX before you read the AMOUNT register? 
    • Can you share a minimal project that reproduce the error?

    regards

    Jared 

  • Hi Jared,

    From the source of the UART character, I send one character to NRF52840 and I get EVENT_RXDRDY in step 1. All other steps do not see EVENT_RXDRDY. According to the document of NRF52840, after TASK_FLUSHRX in step 3, I'm supposed to see EVENT_ENDRX (please correct me) but I never do. In my application, I cannot send any stop command and here I've not sent TASK_STOPRX at all. I've attached the whole project herewith. The directory it is

    C:\nordic\nRF5SDK160098a08e2\myprojects\peripheral

    7271.usbd_cdc_acm.zip

  • I've changed the libraries also. That's why there are errors. I removed the errors and tried it myself. The problem is still there. Attached is the same project with errors removed. Please let me know if you still get errors.

    6131.usbd_cdc_acm.zip

  • Hi,

    The implicit declaration errors are removed but you still have errors regarding "too many arguments" in app_usbd_cdc_acm_read(). The function takes in 3 parameters not 4. It seems that you try to pass both a size parameter and a read_len. This should be fixed.

    regards

    Jared 

  • app_usbd_cdc_acm.c

    app_usbd_cdc_acm.h

    Please copy these files to your directory (overwrite the existing ones). I've added the fourth parameter to get the actual number of bytes read.

    myprojects\..\components\libraries\usbd\class\cdc\acm\

  • I see there is a typecast error (the third error above). Could you tell me which line number is it and which function?

  • Please replace the file main.c with the following

    /**
     * Copyright (c) 2017 - 2019, 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 <stdint.h>
    #include <stdbool.h>
    #include <stddef.h>
    #include <stdio.h>
    
    #include "nrf.h"
    #include "nrf_drv_usbd.h"
    #include "nrf_drv_clock.h"
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "nrf_drv_power.h"
    
    #include "app_error.h"
    #include "app_util.h"
    #include "app_usbd_core.h"
    #include "app_usbd.h"
    #include "app_usbd_string_desc.h"
    #include "app_usbd_cdc_acm.h"
    #include "app_usbd_serial_num.h"
    #include "app_uart.h"
    
    
    #include "boards.h"
    #include "bsp.h"
    #include "bsp_cli.h"
    #include "nrf_cli.h"
    #include "nrf_cli_uart.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #if NRF_CLI_ENABLED
    /**
     * @brief CLI interface 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',
                4);
    #endif
    //#define UART_SOFT_LOOPBACK 1
    
    #define UART_TX_BUF_SIZE                128                                         /**< UART TX buffer size. */
    #define UART_RX_BUF_SIZE                128                                         /**< UART RX buffer size. */
    
    /**@file
     * @defgroup usbd_cdc_acm_example main.c
     * @{
     * @ingroup usbd_cdc_acm_example
     * @brief USBD CDC ACM example
     *
     */
    
    #define LED_USB_RESUME      (BSP_BOARD_LED_0)
    #define LED_CDC_ACM_OPEN    (BSP_BOARD_LED_1)
    #define LED_CDC_ACM_RX      (BSP_BOARD_LED_2)
    #define LED_CDC_ACM_TX      (BSP_BOARD_LED_3)
    
    #define BTN_CDC_DATA_SEND       0
    #define BTN_CDC_NOTIFY_SEND     1
    
    #define BTN_CDC_DATA_KEY_RELEASE        (bsp_event_t)(BSP_EVENT_KEY_LAST + 1)
    
    /**
     * @brief Enable power USB detection
     *
     * Configure if example supports USB port connection
     */
    #ifndef USBD_POWER_DETECTION
    #define USBD_POWER_DETECTION true
    #endif
    
    
    static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                        app_usbd_cdc_acm_user_event_t event);
    
    #define CDC_ACM_COMM_INTERFACE  0
    #define CDC_ACM_COMM_EPIN       NRF_DRV_USBD_EPIN2
    
    #define CDC_ACM_DATA_INTERFACE  1
    #define CDC_ACM_DATA_EPIN       NRF_DRV_USBD_EPIN1
    #define CDC_ACM_DATA_EPOUT      NRF_DRV_USBD_EPOUT1
    
    
    /**
     * @brief CDC_ACM class instance
     * */
    APP_USBD_CDC_ACM_GLOBAL_DEF(m_app_cdc_acm,
                                cdc_acm_user_ev_handler,
                                CDC_ACM_COMM_INTERFACE,
                                CDC_ACM_DATA_INTERFACE,
                                CDC_ACM_COMM_EPIN,
                                CDC_ACM_DATA_EPIN,
                                CDC_ACM_DATA_EPOUT,
                                APP_USBD_CDC_COMM_PROTOCOL_AT_V250
    );
    
    #if 0
    #define CDC_ACM_COMM_INTERFACE1  2
    #define CDC_ACM_COMM_EPIN1       NRF_DRV_USBD_EPIN4
    
    #define CDC_ACM_DATA_INTERFACE1  3
    #define CDC_ACM_DATA_EPIN1       NRF_DRV_USBD_EPIN3
    #define CDC_ACM_DATA_EPOUT1      NRF_DRV_USBD_EPOUT2
    
    /**
     * @brief CDC_ACM class instance
     * */
    APP_USBD_CDC_ACM_GLOBAL_DEF(m_app_cdc_acm1,
                                cdc_acm_user_ev_handler,
                                CDC_ACM_COMM_INTERFACE1,
                                CDC_ACM_DATA_INTERFACE1,
                                CDC_ACM_COMM_EPIN1,
                                CDC_ACM_DATA_EPIN1,
                                CDC_ACM_DATA_EPOUT1,
                                APP_USBD_CDC_COMM_PROTOCOL_AT_V250
    );
    
    
    
    #define CDC_ACM_COMM_INTERFACE2  4
    #define CDC_ACM_COMM_EPIN2       NRF_DRV_USBD_EPIN6
    
    #define CDC_ACM_DATA_INTERFACE2  5
    #define CDC_ACM_DATA_EPIN2       NRF_DRV_USBD_EPIN5
    #define CDC_ACM_DATA_EPOUT2      NRF_DRV_USBD_EPOUT3
    
    /**
     * @brief CDC_ACM class instance
     * */
    APP_USBD_CDC_ACM_GLOBAL_DEF(m_app_cdc_acm2,
                                cdc_acm_user_ev_handler,
                                CDC_ACM_COMM_INTERFACE2,
                                CDC_ACM_DATA_INTERFACE2,
                                CDC_ACM_COMM_EPIN2,
                                CDC_ACM_DATA_EPIN2,
                                CDC_ACM_DATA_EPOUT2,
                                APP_USBD_CDC_COMM_PROTOCOL_AT_V250
    );
    #endif
    
    #define READ_SIZE 128
    
    static uint8_t m_rx_buffer[3][READ_SIZE];
    //static char m_tx_buffer[3][NRF_DRV_USBD_EPSIZE];
    //static bool m_send_flag = 0;
    static unsigned int write_offset[3] = {0}, read_offset[3] = {0};
    unsigned char mybuffer[READ_SIZE];
    unsigned int m_read_usb_flag = 0;
    ret_code_t app_usbd_cdc_acm_init(app_usbd_cdc_acm_t const * p_cdc_acm,  void *p_buf, size_t  length, unsigned int *read_len);
    uint32_t app_uart_write(uint8_t *byte, uint32_t *len);
    uint32_t app_uart_read(uint8_t * p_byte, uint32_t *size);
    void enable_uarte_int(int idx);
    void disable_uarte_int(int idx);
    void uarte_irq_handler0(void);
    void nrfx_systick_init(void);
    void comp_stop(void);
    void comp_start(void);
    void comp_sample(void);
    int comp_read(void);
    void comp_init(void);
    void init_gpio(void);
    
    /**
     * @brief User event handler @ref app_usbd_cdc_acm_user_ev_handler_t (headphones)
     * */
    static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                        app_usbd_cdc_acm_user_event_t event)
    {
        app_usbd_cdc_acm_t const * p_cdc_acm = app_usbd_cdc_acm_class_get(p_inst);
        int index;
        unsigned int read_len;
        
    #if 0
        if (p_cdc_acm == &m_app_cdc_acm1)
            index = 1;
        else if (p_cdc_acm == &m_app_cdc_acm2)
            index = 2;
        else
     #endif
            index = 0;
    
    
        switch (event)
        {
            case APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN:
            {
                bsp_board_led_on(LED_CDC_ACM_OPEN);
    
                /*Setup first transfer*/
                ret_code_t ret = app_usbd_cdc_acm_init(p_cdc_acm, &m_rx_buffer[index][write_offset[index]], 1, &read_len);
                
                UNUSED_VARIABLE(ret);
                break;
            }
            case APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE:
                bsp_board_led_off(LED_CDC_ACM_OPEN);
                break;
            case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
                bsp_board_led_invert(LED_CDC_ACM_TX);
                break;
            case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
                {
                    ret_code_t ret;
                    //uint8_t *ptr = p_inst->pstartbuf;
                    size_t size;
                    NRF_LOG_INFO("Bytes waiting: %d", app_usbd_cdc_acm_bytes_stored(p_cdc_acm));
                    index = 0;
                    do
                    {
                        if (write_offset[0] >= read_offset[0])
                          size = READ_SIZE - write_offset[0];
                        else if ((write_offset[0]+1) < read_offset[0])
                          size = read_offset[0] - write_offset[0] - 1;
                        else {
                          m_read_usb_flag = 1;
                          break;
                        }
                        if (((write_offset[index] +1) % READ_SIZE) == read_offset[index]) {
                          m_read_usb_flag = 1;
                          break;
                        }
                        m_read_usb_flag = 0;
                        /* Fetch data until internal buffer is empty */
                        /* Fetch data until internal buffer is empty */
                        ret = app_usbd_cdc_acm_read(p_cdc_acm,
                                                &m_rx_buffer[index][write_offset[index]], 
                                               size, &read_len);
                        if (read_len > 0) {
                          if (((write_offset[index] +1) % READ_SIZE) != read_offset[index]) {
                              write_offset[index]+= read_len;
                              write_offset[index] %= READ_SIZE;
                          } else if(ret == NRF_SUCCESS){
                              m_read_usb_flag = 1;
                              break;
                          }
                      }
                    } while (ret == NRF_SUCCESS);
    
                    bsp_board_led_invert(LED_CDC_ACM_RX);
                }
                break;
            
            default:
                break;
        }
    }
    
    #if 0
    /**
     * @brief User event handler @ref app_usbd_cdc_acm_user_ev_handler_t (headphones)
     * */
    static void _cdc_acm_user_ev_handler1(app_usbd_class_inst_t const * p_inst,
                                        app_usbd_cdc_acm_user_event_t event)
    {
        app_usbd_cdc_acm_t const * p_cdc_acm = app_usbd_cdc_acm_class_get(p_inst);
    
        switch (event)
        {
            case APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN:
            {
                bsp_board_led_on(LED_CDC_ACM_OPEN);
    
                /*Setup first transfer*/
                ret_code_t ret = app_usbd_cdc_acm_read(&m_app_cdc_acm,
                                                       m_rx_buffer,
                                                       READ_SIZE);
                UNUSED_VARIABLE(ret);
                break;
            }
            case APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE:
                bsp_board_led_off(LED_CDC_ACM_OPEN);
                break;
            case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
                bsp_board_led_invert(LED_CDC_ACM_TX);
                break;
            case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
            {
                ret_code_t ret;
                NRF_LOG_INFO("Bytes waiting: %d", app_usbd_cdc_acm_bytes_stored(p_cdc_acm));
                do
                {
                    /*Get amount of data transfered*/
                    size_t size = app_usbd_cdc_acm_rx_size(p_cdc_acm);
                    NRF_LOG_INFO("RX: size: %lu char: %c", size, m_rx_buffer[0]);
    
                    /* Fetch data until internal buffer is empty */
                    ret = app_usbd_cdc_acm_read(&m_app_cdc_acm1,
                                                m_rx_buffer,
                                                READ_SIZE);
                } while (ret == NRF_SUCCESS);
    
                bsp_board_led_invert(LED_CDC_ACM_RX);
                break;
            }
            default:
                break;
        }
    }
    #endif
    extern uint16_t m_cdc_setup_flag;
    static void usbd_user_ev_handler(app_usbd_event_type_t event)
    {
        switch (event)
        {
            case APP_USBD_EVT_DRV_SUSPEND:
                bsp_board_led_off(LED_USB_RESUME);
                break;
            case APP_USBD_EVT_DRV_RESUME:
                bsp_board_led_on(LED_USB_RESUME);
                break;
            case APP_USBD_EVT_STARTED:
                break;
            case APP_USBD_EVT_STOPPED:
                app_usbd_disable();
                bsp_board_leds_off();
                break;
            case APP_USBD_EVT_POWER_DETECTED:
                NRF_LOG_INFO("USB power detected");
    
                if (!nrf_drv_usbd_is_enabled())
                {
                    app_usbd_enable();
                }
                break;
            case APP_USBD_EVT_POWER_REMOVED:
                NRF_LOG_INFO("USB power removed");
                app_usbd_stop();
                break;
            case APP_USBD_EVT_POWER_READY:
                NRF_LOG_INFO("USB ready");
                app_usbd_start();
                break;
            default:
                break;
        }
    }
    
    #if 0
    static void bsp_event_callback(bsp_event_t ev)
    {
        ret_code_t ret;
        switch ((unsigned int)ev)
        {
            case CONCAT_2(BSP_EVENT_KEY_, BTN_CDC_DATA_SEND):
            {
                m_send_flag = 1;
                break;
            }
            
            case BTN_CDC_DATA_KEY_RELEASE :
            {
                m_send_flag = 0;
                break;
            }
    
            case CONCAT_2(BSP_EVENT_KEY_, BTN_CDC_NOTIFY_SEND):
            {
                ret = app_usbd_cdc_acm_serial_state_notify(&m_app_cdc_acm,
                                                           APP_USBD_CDC_ACM_SERIAL_STATE_BREAK,
                                                           false);
                UNUSED_VARIABLE(ret);
                break;
            }
    
            default:
                return; // no implementation needed
        }
    }
    
    
    static void init_bsp(void)
    {
        ret_code_t ret;
        ret = bsp_init(BSP_INIT_BUTTONS, bsp_event_callback);
        APP_ERROR_CHECK(ret);
        
        UNUSED_RETURN_VALUE(bsp_event_to_button_action_assign(BTN_CDC_DATA_SEND,
                                                              BSP_BUTTON_ACTION_RELEASE,
                                                              BTN_CDC_DATA_KEY_RELEASE));
        
        /* Configure LEDs */
        bsp_board_init(BSP_INIT_LEDS);
    }
    #endif
    
    #define delay_ms  nrf_delay_ms
    #define delay_us  nrf_delay_us
    
    #if NRF_CLI_ENABLED
    static void init_cli(void)
    {
        ret_code_t ret;
        ret = bsp_cli_init(bsp_event_callback);
        APP_ERROR_CHECK(ret);
        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);
        ret = nrf_cli_start(&m_cli_uart);
        APP_ERROR_CHECK(ret);
    }
    #endif
    
    #if 1
    void init_gpio()
    {
    
        /* P0.00 Output-OC. 0- WLAN green LED on, 1 - off */
        nrf_gpio_cfg(0,
                NRF_GPIO_PIN_DIR_OUTPUT,
                NRF_GPIO_PIN_INPUT_DISCONNECT,
                NRF_GPIO_PIN_NOPULL,
                NRF_GPIO_PIN_S0D1,
                NRF_GPIO_PIN_NOSENSE);
    
        // P0.01 - Output-OC. 0- WLAN blue LED on, 1 - off 
        nrf_gpio_cfg(1,
                NRF_GPIO_PIN_DIR_OUTPUT,
                NRF_GPIO_PIN_INPUT_DISCONNECT,
                NRF_GPIO_PIN_NOPULL,
                NRF_GPIO_PIN_S0D1,
                NRF_GPIO_PIN_NOSENSE);
        // AIN4 CC2 pin. Select device type connected to USB-C port 
        nrf_gpio_cfg(2,
                NRF_GPIO_PIN_DIR_INPUT,
                NRF_GPIO_PIN_INPUT_DISCONNECT,
                NRF_GPIO_PIN_NOPULL,
                NRF_GPIO_PIN_S0S1,
                NRF_GPIO_PIN_NOSENSE);
        // AIN5 CC1 pin. Select device type connected to USB-C port 
        nrf_gpio_cfg(3,
                NRF_GPIO_PIN_DIR_INPUT,
                NRF_GPIO_PIN_INPUT_DISCONNECT,
                NRF_GPIO_PIN_NOPULL,
                NRF_GPIO_PIN_S0S1,
                NRF_GPIO_PIN_NOSENSE);
        // AIN2 comparator input. Below 1.4V the CPU is in reset 
        nrf_gpio_cfg(4,
                NRF_GPIO_PIN_DIR_INPUT,
                NRF_GPIO_PIN_INPUT_DISCONNECT,
                NRF_GPIO_PIN_NOPULL,
                NRF_GPIO_PIN_S0S1,
                NRF_GPIO_PIN_NOSENSE);
    
        // P0.05 Output-OC. 0- MOD1 Blue LED on, off - LED off
        nrf_gpio_cfg(5,
                NRF_GPIO_PIN_DIR_OUTPUT,
                NRF_GPIO_PIN_INPUT_DISCONNECT,
                NRF_GPIO_PIN_NOPULL,
                NRF_GPIO_PIN_S0D1,
                NRF_GPIO_PIN_NOSENSE);
    
        // P0.06 - Debug Uart RXD from CPU
        nrf_gpio_cfg(6,
                NRF_GPIO_PIN_DIR_INPUT,
                NRF_GPIO_PIN_INPUT_DISCONNECT,
                NRF_GPIO_PIN_NOPULL,
                //NRF_GPIO_PIN_S0S1,
                NRF_GPIO_PIN_H0H1,
                NRF_GPIO_PIN_NOSENSE);
          // P0.07 Output-OC. 0- MOD1 Blue LED on, off - LED off
          nrf_gpio_cfg(7,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
          // P0.08 Debug Uart TXD to CPU
          nrf_gpio_cfg(8,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  //NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_H0H1,
                  NRF_GPIO_PIN_NOSENSE);
          // P0.09 Output-OC, with pullup - 0 - reset Ublox wifi, off - normal operation
          nrf_gpio_cfg(9,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
          // P0.10 Output-OC, 0 - MOD2 Red LED on, off - LED off
          nrf_gpio_cfg(10,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
          // P0.11 Output-OC, 0 - Sys greenLED on, off - LED off
          nrf_gpio_cfg(11,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
          // P0.12 Output-OC, 0 - Sys green LED on, off - LED off
          nrf_gpio_cfg(12,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
          // P0.13 I2c SDA -slave
          nrf_gpio_cfg(13,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.14 I2c SDCLK -slave
          nrf_gpio_cfg(14,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.15 Input with pullup - 0-  Mod1 SIM1 detected, 1 - SIM not detected
          nrf_gpio_cfg(15,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.16 Input with pullup - Mod2 SIM detected or Mod1 SIM2 detected. 0-  SIM detected, 1 - SIM not detected
          nrf_gpio_cfg(16,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.17 Output - push/pull: 0- normal Mod1 uses SIM1 & MOD2 uses SIM2; 1 - swap SIM: SIM1 is unsed, Mod1 uses SIM2
          nrf_gpio_cfg(17,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
    
         // P0.18 JTAG/Debug reset
          nrf_gpio_cfg(18,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.19 For 1.8V modules: Output-OC: 0 -Mod1 reset, off - normal operation
         //       For 3.3.V modules- Output - push/pull: 0 - MOD1 reset, 1 - normal operation
          nrf_gpio_cfg(19,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.20 Output-push/pull: 0 -  power down MOD1, 1 - normall operation
          nrf_gpio_cfg(20,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.21 Input with pullup - 0-  Mod1 WAKE request(Normal operation), 1- no wake request
          nrf_gpio_cfg(21,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.22 Output-OC 0 -power down the board, off normal operation
          nrf_gpio_cfg(22,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.23 Input with pullup - 0-  Sourcing power to ETH1 failed, 1-normal operation
          nrf_gpio_cfg(23,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.24 Input with pullup - 0-  Sourcing power to ETH0 failed, 1-normal operation
          nrf_gpio_cfg(24,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.25 Output - push/pull: 0-  Mod2 5V disable, 1 - MOD2 5V enable
          nrf_gpio_cfg(25,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.26 Output-OC: 0-MOD2 Blue LED on, off -LED off
          nrf_gpio_cfg(26,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.27 Output-OC: 0 - MOD2 Green LED on, off - LED off
               nrf_gpio_cfg(27,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.28 AIN0 : USB-C input power/2
          nrf_gpio_cfg(28,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.29 AIN5: Input power/2
          nrf_gpio_cfg(29,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.30 Input 0-  Main power failed, 1-normal operation
          nrf_gpio_cfg(30,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.31 AIN7 MOD3 ID: not used at this time
          nrf_gpio_cfg(31,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.00 Input with pullup - not used
          nrf_gpio_cfg(32,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.01 Output: 0 -power down MOD2, 1 - normal operation
          nrf_gpio_cfg(33,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.02 For 1.8V modules: Output-OC: 0 -Mod2 reset, off - normal operation
         //       For 3.3.V modules: Output-push/pull: 0 - MOD2 reset, 1 - normal operation
          nrf_gpio_cfg(34,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.03 input, pullup: 0 -MOD2 WAKE request(normal operation), 1 - no wake request
          nrf_gpio_cfg(35,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.04 Output-OC: 0 - DRAM VTT disable and power down, off - normal normal operation
          nrf_gpio_cfg(36,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.05 Output-OC: 0 - Source power to ETH0; off - normal operation
          nrf_gpio_cfg(37,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.06 Output-OC: 0 - Source power to ETH1; off - normal operation
          nrf_gpio_cfg(38,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.07 Output-push/pull: 0 - Disable power to IO controller; 1 - normal operation
          nrf_gpio_cfg(39,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.08 Output-OC: 0 - MOD1 green light on; off - LED off
          nrf_gpio_cfg(40,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.09 Output-OC: 0 - MOD1 red LED on; off - LED off
          nrf_gpio_cfg(41,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.10 bidirectional, Output-OC: 0 - System reset, 1 - normal operation
         nrf_gpio_cfg(42,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.11 Output: 0 - Set USB-C as host, 1 - as device
          nrf_gpio_cfg(43,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         nrf_gpio_pin_set(43);
         // P1.12 Output: 0 - Do not source power to USB-C, Source power to USB-C
          nrf_gpio_cfg(44,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.13 Output: 0 - CPU boot from SPI NOR; 1 - CPU boot from UART
          nrf_gpio_cfg(45,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.14 input - 0 - Power failure in IO power, MOD2 5V power or USB-C power, 1 - normal operation
          nrf_gpio_cfg(46,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.15 input - 0 - config switch pressed, 1 - normal operation
          nrf_gpio_cfg(47,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
          nrf_gpio_pin_clear(42);
          nrf_gpio_pin_clear(22);
          nrf_gpio_pin_clear(9);
          nrf_gpio_pin_clear(39);
          nrf_gpio_pin_clear(20);
          nrf_gpio_pin_clear(33);
          nrf_gpio_pin_clear(25);
          nrf_gpio_pin_clear(44);
          delay_ms(12);
    
          //nrf_gpio_pin_clear(45);
           
          if(nrf_gpio_pin_read(47) == 0) {
            delay_ms(1);
            if (nrf_gpio_pin_read(47) == 0) {
              nrf_gpio_pin_set(45);
            }
          }
         
          //nrf_gpio_pin_set(45);
    
          nrf_gpio_pin_set(22);     
          delay_ms(20);
          nrf_gpio_pin_set(42);
          while(nrf_gpio_pin_read(47) == 0) {
    
          }
          nrf_gpio_pin_set(42);
          delay_ms(2);
          nrf_gpio_pin_set(22);
          delay_ms(2);
          nrf_gpio_pin_set(9);
          delay_ms(2);
          nrf_gpio_pin_set(39);
          delay_ms(2);
          nrf_gpio_pin_set(20);
          delay_ms(2);
          nrf_gpio_pin_set(33);
          delay_ms(2);
          nrf_gpio_pin_set(25);
          delay_ms(2);
          nrf_gpio_pin_set(44);
          delay_ms(0);
          
           
    }
    #endif
    
    //#define NRF_COMP_BASE 0x40013000
    #define NRF_COMP_START (NRF_COMP_BASE + 0x000)
    #define NRF_COMP_STOP  (NRF_COMP_BASE + 0x004)
    #define NRF_COMP_SAMPLE (NRF_COMP_BASE + 0x008)
    #define NRF_COMP_EVENTS_READY (NRF_COMP_BASE + 0x100)
    #define NRF_COMP_EVENTS_DOWN  (NRF_COMP_BASE + 0x104)
    #define NRF_COMP_EVENTS_UP    (NRF_COMP_BASE + 0x108)
    #define NRF_COMP_EVENTS_CROSS (NRF_COMP_BASE + 0x10C)
    #define NRF_COMP_SHORTS       (NRF_COMP_BASE + 0x200)
    #define NRF_COMP_INTEN        (NRF_COMP_BASE + 0x300)
    #define NRF_COMP_INTENSET     (NRF_COMP_BASE + 0x304)
    #define NRF_COMP_INTENCLR     (NRF_COMP_BASE + 0x308)
    #define NRF_COMP_RESULT       (NRF_COMP_BASE + 0x400)
    #define NRF_COMP_ENABLE       (NRF_COMP_BASE + 0x500)
    #define NRF_COMP_PSEL         (NRF_COMP_BASE + 0x504)
    #define NRF_COMP_REFSEL       (NRF_COMP_BASE + 0x508)
    #define NRF_COMP_EXTREFSEL       (NRF_COMP_BASE + 0x50C)
    #define NRF_COMP_TH           (NRF_COMP_BASE + 0x530)
    #define NRF_COMP_MODE         (NRF_COMP_BASE + 0x534)
    #define NRF_COMP_HYST         (NRF_COMP_BASE + 0x538)
    
    
    void comp_init()
    {
          unsigned int *comp = (unsigned int *) NRF_COMP_MODE;
    
          *comp = 0;
          *(unsigned int *)NRF_COMP_REFSEL = 0x4; //VDD
          *(unsigned int *) NRF_COMP_PSEL = 2;
          //Nominal VDD = 4.6V
          //Declare the power to be down when below 1.4V (THDOWN = 20)
          //Declare the power to be up when above 4V (THUP = 56)
          *(unsigned int *) NRF_COMP_TH =(55 <<8 )+ 19;
          *(unsigned int *) NRF_COMP_ENABLE = 0x01;
    }
    
    int comp_read()
    {
      
      return 0;
    }
    
    void comp_sample()
    {
      *((unsigned int *) NRF_COMP_SAMPLE) = 0x01;
    }
    
    void comp_start()
    {
      *((unsigned int *) NRF_COMP_START) = 0x01;
    }
    
    void comp_stop()
    {
      *((unsigned int *) NRF_COMP_STOP) = 0x01;
    }
    
    #define NRF_COMP_AIN0     0
    #define NRF_COMP_AIN2     2
    #define NRF_COMP_AIN4     4
    #define NRF_COMP_AIN5     5
    
    unsigned short board_power, usb_cc1, usb_cc2, input_power;
    
    void comp_process_result(unsigned short ain)
    {
      switch(ain)
      {
      case NRF_COMP_AIN2:
          break;
      case NRF_COMP_AIN4:
          break;
      case NRF_COMP_AIN0:
          break;
      }
    }
    
    int my_ble_init(void);
    void bsp_event_handler(bsp_event_t event);
    
    
    #define UART_READ_SIZE 256
    
    unsigned int uart_write_offset = 0, uart_read_offset = 0;
    unsigned char m_uart_rx_buffer[UART_READ_SIZE];
    unsigned long uart_rx_error = 0, uart_tx_error = 0, usb_rx_error = 0, usb_tx_error = 0, m_read_uart_flag = 0;
    extern uint32_t uart_rx_bytes;
    /**@brief   Function for handling app_uart events.
     *
     * @details This function will receive a single character from the app_uart module and append it to
     *          a string. The string will be be sent over USB.
     */
    /**@snippet [Handling the data received over UART] */
    void uart_event_handle(app_uart_evt_t * p_event)
    {
        uint32_t       err_code;
        uint32_t        len;
    
        switch (p_event->evt_type)
        {
            case APP_UART_DATA_READY:
                if (((uart_write_offset+1) %UART_READ_SIZE) == uart_read_offset) {
                    m_read_uart_flag = 1;
                    break;
                }
                m_read_uart_flag = 0;
    #if 0
                len = UART_READ_SIZE - uart_write_offset;
                err_code = app_uart_get(&m_uart_rx_buffer[uart_write_offset]);
    #ifndef UART_SOFT_LOOPBACK
                if (err_code == NRF_SUCCESS)
                  uart_write_offset++;
                else {
                  uart_rx_error++;
                  NRF_LOG_DEBUG("Error reading UART");
                }
    
    #endif
    #else
                if (uart_write_offset >= uart_read_offset)
                  len = UART_READ_SIZE - uart_write_offset;
                else 
                  len = uart_read_offset - uart_write_offset;
                if (len == 0)
                {
                    m_read_uart_flag = 1;
                    break;
                }
                err_code = app_uart_read(&m_uart_rx_buffer[uart_write_offset], &len);
    #ifndef UART_SOFT_LOOPBACK
                if (err_code == NRF_SUCCESS) {
                  uart_write_offset+= len;
                  uart_write_offset %= UART_READ_SIZE;
                  uart_rx_bytes += len;
                }
                else {
                  uart_rx_error++;
                  NRF_LOG_DEBUG("Error reading UART");
                }
    
    #endif
    #endif
                        
    #if 0
                if ((data_array[uart_write_offset - 1] == '\n') ||
                    (data_array[uart_write_offset - 1] == '\r') ||
                    (index >= m_ble_nus_max_data_len))
                {
                    if (index > 1)
                    {
                        NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                        NRF_LOG_HEXDUMP_DEBUG(data_array, index);
    #if 0
                        do
                        {
                            uint16_t length = (uint16_t)index;
                            err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                            if ((err_code != NRF_ERROR_INVALID_STATE) &&
                                (err_code != NRF_ERROR_RESOURCES) &&
                                (err_code != NRF_ERROR_NOT_FOUND))
                            {
                                APP_ERROR_CHECK(err_code);
                            }
                        } while (err_code == NRF_ERROR_RESOURCES);
    
    #else                
                        do
                        {
                            uint16_t length;
                            if (uart_read_offset < uart_write_offset)
                                length = uart_write_offset - uart_read_offset;
                            else if (uart_read_offset > uart_write_offset)
                                length = UART_READ_SIZE - uart_read_offset; 
                            else
                                break;
    
                            err_code = usb_cdc_acm_write(&m_nus, &m_uart_rx_buffer[uart_read_offset], &length, m_conn_handle);
                            if ((err_code != NRF_ERROR_INVALID_STATE) &&
                                (err_code != NRF_ERROR_RESOURCES) &&
                                (err_code != NRF_ERROR_NOT_FOUND))
                            {
                                APP_ERROR_CHECK(err_code);
                            }
                        } while (err_code == NRF_ERROR_RESOURCES);
                        if (uart_read_offset && uart_read_offset == uart_write_offset)
                            uart_read_offset = uart_write_offset = 0;
    #endif
                    }
               }
    #endif
                
                break;
    
            case APP_UART_COMMUNICATION_ERROR:
                APP_ERROR_HANDLER(p_event->data.error_communication);
                break;
    
            case APP_UART_FIFO_ERROR:
                APP_ERROR_HANDLER(p_event->data.error_code);
                break;
    
            default:
                break;
        }
    }
    /**@snippet [Handling the data received over UART] */
    
    
    /**@brief  Function for initializing the UART module.
     */
    /**@snippet [UART Initialization] */
    static void uart_init(void)
    {
        uint32_t                     err_code;
        app_uart_comm_params_t const comm_params =
        {
    #if 0
            .rx_pin_no    = RX_PIN_NUMBER,
            .tx_pin_no    = TX_PIN_NUMBER,
    #else
            .rx_pin_no    = TX_PIN_NUMBER,
            .tx_pin_no    = RX_PIN_NUMBER,
    #endif
            .rts_pin_no   = RTS_PIN_NUMBER,
            .cts_pin_no   = CTS_PIN_NUMBER,
            .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
            .use_parity   = false,
    #define ENCOREDEVICE
    #ifdef ENCOREDEVICE
            .baud_rate    = NRF_UARTE_BAUDRATE_115200
    #else
    #if defined (UART_PRESENT)
            .baud_rate    = NRF_UART_BAUDRATE_115200
    #else
            .baud_rate    = NRF_UARTE_BAUDRATE_115200
    #endif
    #endif
        };
    
        APP_UART_FIFO_INIT(&comm_params,
                           UART_RX_BUF_SIZE,
                           UART_TX_BUF_SIZE,
                           uart_event_handle,
                           APP_IRQ_PRIORITY_LOWEST,
                           err_code);
        APP_ERROR_CHECK(err_code);
    }
    //uint32_t nrfx_systick_ms_tick(uint32_t ms);
    //uint32_t get_ms_tick(uint32_t ms)
    //{
    //  return (nrf_systick_ms_tick(ms) + nrf_cur_ms_tick());
    //}
    
    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "nrf_drv_timer.h"
    #include "bsp.h"
    #include "app_error.h"
    
    const nrf_drv_timer_t TIMER_TICK = NRF_DRV_TIMER_INSTANCE(0);
    static uint64_t ticks;
        
    /**
     * @brief Handler for timer events.
     */
    void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context)
    {    
        (void)(p_context);
        switch (event_type)
        {
            case NRF_TIMER_EVENT_COMPARE0:
                ticks++;
                break;
    
            default:
                //Do nothing.
                break;
        }
    }
    
    
    /**
     * @brief Function for main application entry.
     */
    int init_timer(void)
    {
        uint32_t time_ms = 1; //Time(in miliseconds) between consecutive compare events.
        uint32_t time_ticks;
        uint32_t err_code = NRF_SUCCESS;
        
        ticks++;
        //Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.
        nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
        err_code = nrf_drv_timer_init(&TIMER_TICK, &timer_cfg, timer_led_event_handler);
        APP_ERROR_CHECK(err_code);
    
        time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_TICK, time_ms);
    
        nrf_drv_timer_extended_compare(
             &TIMER_TICK, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
    
        nrf_drv_timer_enable(&TIMER_TICK);
    
        return 0;
    }
    
    
    uint64_t get_ms_ticks(uint32_t ms)
    {
        return (ticks + ms);
    }
    
    int check_ms_ticks(uint64_t ms)
    {
        if (ticks >= ms)
          return 1;
        return 0;
    }
    uint32_t uart_rx_bytes, uart_tx_bytes, usb_tx_bytes;
    extern uint32_t usb_rx_bytes;
    int main(void)
    {
    #if 1
        ret_code_t ret;
        //int loop_count = 0;
        //unsigned short comp_state = 0;
        uint32_t len;
        volatile unsigned int *ptr;
        extern int m_uarte_rx_flag;
        extern uint64_t m_uarte_rx_timer;
        unsigned int read_len;
        size_t size;
                    
    
        static const app_usbd_config_t usbd_config = {
            .ev_state_proc = usbd_user_ev_handler
        };
    #if 1
        ptr = (unsigned int *) 0x10001304;
        if ((ptr[0] & 0x07) != 0x05) {
            ptr = (unsigned int *) 0x4001E504;
            *ptr = 0x01;
            *(unsigned int *) 0x10001304 = 0x05;
            *ptr = 0;
        }
        *(unsigned int *) 0x40000578 |= 0x01;
        *(unsigned int *) 0x40000580 = 0x00;
    #endif
     
        ret = NRF_LOG_INIT(NULL);
        APP_ERROR_CHECK(ret);
    
        ret = nrf_drv_clock_init();
        APP_ERROR_CHECK(ret);
        
        nrf_drv_clock_lfclk_request(NULL);
    
     
        while(!nrf_drv_clock_lfclk_is_running())
        {
            /* Just waiting */
        }
        nrfx_systick_init();
        init_gpio();
        uart_init();
        init_timer();       
        //ret = app_timer_init();
        //APP_ERROR_CHECK(ret);
    
        //uint32_t err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler);
        //APP_ERROR_CHECK(err_code);
        //init_bsp();
    #if NRF_CLI_ENABLED
        init_cli();
    #endif
    
        app_usbd_serial_num_generate();
    
        ret = app_usbd_init(&usbd_config);
        APP_ERROR_CHECK(ret);
        NRF_LOG_INFO("USBD CDC ACM example started.");
    
        app_usbd_class_inst_t const * class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&m_app_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();
        }
        
        //my_ble_init();    
    
    #if 0
    
        init_gpio();
        comp_init();
        comp_start();
    #endif
        while (true) {
            if (m_read_usb_flag == 1) {
              //app_usbd_class_inst_t const * p_inst = app_usbd_cdc_acm_class_inst_get(&m_app_cdc_acm);
              do {
                    if (write_offset[0] >= read_offset[0])
                         size = READ_SIZE - write_offset[0];
                    else if (((write_offset[0]+1) %READ_SIZE) < read_offset[0])
                         size = read_offset[0] - write_offset[0] - 1;
                    else {
                         m_read_usb_flag = 1;
                         break;
                    }
                    if (size == 0)
                         break;
                    ret = app_usbd_cdc_acm_read(&m_app_cdc_acm,
                                                &m_rx_buffer[0][write_offset[0]],
                                                size, &read_len);
                    if (read_len > 0) {
                         write_offset[0] += read_len;
                         write_offset[0] %= READ_SIZE;
                         m_read_usb_flag = 0;
                    }
                } while(ret == NRF_SUCCESS);
            }
            if (m_uarte_rx_flag) {
                disable_uarte_int(0);
                if (check_ms_ticks(m_uarte_rx_timer)) {
                  uarte_irq_handler0();
                }
                enable_uarte_int(0);
            }
            if (m_read_uart_flag == 1) {
                if (uart_write_offset >= uart_read_offset)
                  len = UART_READ_SIZE - uart_write_offset;
                else 
                  len = uart_read_offset - uart_write_offset;
                if (len == 0)
                {
                    m_read_uart_flag = 1;
                    break;
                }
                m_read_uart_flag = 0;
                ret_code_t err_code = app_uart_read(&m_uart_rx_buffer[uart_write_offset], &len);
    #ifndef UART_SOFT_LOOPBACK
                if (err_code == NRF_SUCCESS) {
                  uart_write_offset+= len;
                  uart_rx_bytes += len;
                  uart_write_offset %= UART_READ_SIZE;
                }
                else {
                  uart_rx_error++;
                  NRF_LOG_DEBUG("Error reading UART");
                }
    
    #endif
          }
          if (uart_read_offset != uart_write_offset) {
                  uint32_t len;
                  if (uart_write_offset > uart_read_offset)
                    len = uart_write_offset - uart_read_offset;
                  else
                    len = UART_READ_SIZE - uart_read_offset;
                  ret = app_usbd_cdc_acm_write(&m_app_cdc_acm, m_uart_rx_buffer + uart_read_offset, len);
                  if (ret == NRF_SUCCESS) {
                      uart_read_offset += len;
                      usb_tx_bytes += len;
                      uart_read_offset %= UART_READ_SIZE;
                  }
          } 
    
    
          while (read_offset[0] != write_offset[0]) {
                  ret = NRF_ERROR_BUSY;
    #if UART_SOFT_LOOPBACK
                  if (uart_write_offset < UART_READ_SIZE) {
                      m_uart_rx_buffer[uart_write_offset++] = m_rx_buffer[0][read_offset[0]];
                      ret = NRF_SUCCESS;
                  }
    #else
                  //ret = app_uart_put( m_rx_buffer[0][read_offset[0]]);
                  if (write_offset[0] > read_offset[0])
                      len =  write_offset[0] - read_offset[0];
                  else
                      len = READ_SIZE - read_offset[0];
                  if (len > 1)
                    len = 1;
                  ret = app_uart_write( &m_rx_buffer[0][read_offset[0]], &len);
                                                  
    #endif
                  if (ret == NRF_SUCCESS) {
                      read_offset[0]+= len;
                      uart_tx_bytes += len;
                      read_offset[0] %= READ_SIZE;
                  } else
                      break;
            }
    #if 0
            while(((write_offset[0]+1) % READ_SIZE) != read_offset[0]) {
                  if (write_offset[0] >= read_offset[0])
                      size = READ_SIZE - write_offset[0];
                  else if (((write_offset[0]+1) %READ_SIZE) < read_offset[0])
                      size = read_offset[0] - write_offset[0] - 1;
                  else {
                      break;
                  }
                  if (size == 0)
                      break;
                  ret = _app_usbd_cdc_acm_read(&m_app_cdc_acm,
                                                &m_rx_buffer[0][write_offset[0]],
                                                size, &read_len);
                  if (read_len > 0) {
                      write_offset[0] += read_len;
                      write_offset[0] %= READ_SIZE;
                      m_read_usb_flag = 0;
                 } else
                      break;
            }           
    #endif
    #if 0
            if (++loop_count % 10000) {
              switch(comp_state++) {
              default:
                comp_state = 0;
              case 0:
                comp_stop();
                delay_ms(1);
                comp_init(NRF_COMP_AIN2);
                comp_start();
                break;
              case 1:
                comp_sample(NRF_COMP_AIN2);
                comp_process_result(NRF_COMP_AIN2);
                break;
    
              case 2:
                comp_stop();
                delay_ms(1);
                comp_init(NRF_COMP_AIN4);
                comp_start();
                break;
    
              case 3:
                comp_sample(NRF_COMP_AIN4);
                break;
    
              case 4:
                comp_stop();
                delay_ms(1);
                comp_init(NRF_COMP_AIN5);
                comp_start();
                break;
    
              case 5:
                comp_sample(NRF_COMP_AIN5);
                comp_process_result(NRF_COMP_AIN5);
                break;
    
              case 6:
                comp_stop();
                delay_ms(1);
                comp_init(NRF_COMP_AIN0);
                comp_start();
                break;
    
              case 7:
                comp_sample(NRF_COMP_AIN0);
                comp_process_result(NRF_COMP_AIN0);            
                comp_state = 0;
                break;
              }
            }
    
            if (comp_read() < 0) {
    
            }
    #endif
          
            app_usbd_event_queue_process();
            
            
    #if NRF_CLI_ENABLED
            nrf_cli_process(&m_cli_uart);
    #endif
    
            //UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
            /* Sleep CPU only if there was no interrupt since last loop processing */
            //__WFE();
        }
    #else
        my_ble_init();
        // Enter main loop.
        for (;;)
        {
            //idle_state_handle();
        }
    
    #endif
    }
    
    
    
    /** @} */
    

Reply
  • Please replace the file main.c with the following

    /**
     * Copyright (c) 2017 - 2019, 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 <stdint.h>
    #include <stdbool.h>
    #include <stddef.h>
    #include <stdio.h>
    
    #include "nrf.h"
    #include "nrf_drv_usbd.h"
    #include "nrf_drv_clock.h"
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "nrf_drv_power.h"
    
    #include "app_error.h"
    #include "app_util.h"
    #include "app_usbd_core.h"
    #include "app_usbd.h"
    #include "app_usbd_string_desc.h"
    #include "app_usbd_cdc_acm.h"
    #include "app_usbd_serial_num.h"
    #include "app_uart.h"
    
    
    #include "boards.h"
    #include "bsp.h"
    #include "bsp_cli.h"
    #include "nrf_cli.h"
    #include "nrf_cli_uart.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #if NRF_CLI_ENABLED
    /**
     * @brief CLI interface 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',
                4);
    #endif
    //#define UART_SOFT_LOOPBACK 1
    
    #define UART_TX_BUF_SIZE                128                                         /**< UART TX buffer size. */
    #define UART_RX_BUF_SIZE                128                                         /**< UART RX buffer size. */
    
    /**@file
     * @defgroup usbd_cdc_acm_example main.c
     * @{
     * @ingroup usbd_cdc_acm_example
     * @brief USBD CDC ACM example
     *
     */
    
    #define LED_USB_RESUME      (BSP_BOARD_LED_0)
    #define LED_CDC_ACM_OPEN    (BSP_BOARD_LED_1)
    #define LED_CDC_ACM_RX      (BSP_BOARD_LED_2)
    #define LED_CDC_ACM_TX      (BSP_BOARD_LED_3)
    
    #define BTN_CDC_DATA_SEND       0
    #define BTN_CDC_NOTIFY_SEND     1
    
    #define BTN_CDC_DATA_KEY_RELEASE        (bsp_event_t)(BSP_EVENT_KEY_LAST + 1)
    
    /**
     * @brief Enable power USB detection
     *
     * Configure if example supports USB port connection
     */
    #ifndef USBD_POWER_DETECTION
    #define USBD_POWER_DETECTION true
    #endif
    
    
    static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                        app_usbd_cdc_acm_user_event_t event);
    
    #define CDC_ACM_COMM_INTERFACE  0
    #define CDC_ACM_COMM_EPIN       NRF_DRV_USBD_EPIN2
    
    #define CDC_ACM_DATA_INTERFACE  1
    #define CDC_ACM_DATA_EPIN       NRF_DRV_USBD_EPIN1
    #define CDC_ACM_DATA_EPOUT      NRF_DRV_USBD_EPOUT1
    
    
    /**
     * @brief CDC_ACM class instance
     * */
    APP_USBD_CDC_ACM_GLOBAL_DEF(m_app_cdc_acm,
                                cdc_acm_user_ev_handler,
                                CDC_ACM_COMM_INTERFACE,
                                CDC_ACM_DATA_INTERFACE,
                                CDC_ACM_COMM_EPIN,
                                CDC_ACM_DATA_EPIN,
                                CDC_ACM_DATA_EPOUT,
                                APP_USBD_CDC_COMM_PROTOCOL_AT_V250
    );
    
    #if 0
    #define CDC_ACM_COMM_INTERFACE1  2
    #define CDC_ACM_COMM_EPIN1       NRF_DRV_USBD_EPIN4
    
    #define CDC_ACM_DATA_INTERFACE1  3
    #define CDC_ACM_DATA_EPIN1       NRF_DRV_USBD_EPIN3
    #define CDC_ACM_DATA_EPOUT1      NRF_DRV_USBD_EPOUT2
    
    /**
     * @brief CDC_ACM class instance
     * */
    APP_USBD_CDC_ACM_GLOBAL_DEF(m_app_cdc_acm1,
                                cdc_acm_user_ev_handler,
                                CDC_ACM_COMM_INTERFACE1,
                                CDC_ACM_DATA_INTERFACE1,
                                CDC_ACM_COMM_EPIN1,
                                CDC_ACM_DATA_EPIN1,
                                CDC_ACM_DATA_EPOUT1,
                                APP_USBD_CDC_COMM_PROTOCOL_AT_V250
    );
    
    
    
    #define CDC_ACM_COMM_INTERFACE2  4
    #define CDC_ACM_COMM_EPIN2       NRF_DRV_USBD_EPIN6
    
    #define CDC_ACM_DATA_INTERFACE2  5
    #define CDC_ACM_DATA_EPIN2       NRF_DRV_USBD_EPIN5
    #define CDC_ACM_DATA_EPOUT2      NRF_DRV_USBD_EPOUT3
    
    /**
     * @brief CDC_ACM class instance
     * */
    APP_USBD_CDC_ACM_GLOBAL_DEF(m_app_cdc_acm2,
                                cdc_acm_user_ev_handler,
                                CDC_ACM_COMM_INTERFACE2,
                                CDC_ACM_DATA_INTERFACE2,
                                CDC_ACM_COMM_EPIN2,
                                CDC_ACM_DATA_EPIN2,
                                CDC_ACM_DATA_EPOUT2,
                                APP_USBD_CDC_COMM_PROTOCOL_AT_V250
    );
    #endif
    
    #define READ_SIZE 128
    
    static uint8_t m_rx_buffer[3][READ_SIZE];
    //static char m_tx_buffer[3][NRF_DRV_USBD_EPSIZE];
    //static bool m_send_flag = 0;
    static unsigned int write_offset[3] = {0}, read_offset[3] = {0};
    unsigned char mybuffer[READ_SIZE];
    unsigned int m_read_usb_flag = 0;
    ret_code_t app_usbd_cdc_acm_init(app_usbd_cdc_acm_t const * p_cdc_acm,  void *p_buf, size_t  length, unsigned int *read_len);
    uint32_t app_uart_write(uint8_t *byte, uint32_t *len);
    uint32_t app_uart_read(uint8_t * p_byte, uint32_t *size);
    void enable_uarte_int(int idx);
    void disable_uarte_int(int idx);
    void uarte_irq_handler0(void);
    void nrfx_systick_init(void);
    void comp_stop(void);
    void comp_start(void);
    void comp_sample(void);
    int comp_read(void);
    void comp_init(void);
    void init_gpio(void);
    
    /**
     * @brief User event handler @ref app_usbd_cdc_acm_user_ev_handler_t (headphones)
     * */
    static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                        app_usbd_cdc_acm_user_event_t event)
    {
        app_usbd_cdc_acm_t const * p_cdc_acm = app_usbd_cdc_acm_class_get(p_inst);
        int index;
        unsigned int read_len;
        
    #if 0
        if (p_cdc_acm == &m_app_cdc_acm1)
            index = 1;
        else if (p_cdc_acm == &m_app_cdc_acm2)
            index = 2;
        else
     #endif
            index = 0;
    
    
        switch (event)
        {
            case APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN:
            {
                bsp_board_led_on(LED_CDC_ACM_OPEN);
    
                /*Setup first transfer*/
                ret_code_t ret = app_usbd_cdc_acm_init(p_cdc_acm, &m_rx_buffer[index][write_offset[index]], 1, &read_len);
                
                UNUSED_VARIABLE(ret);
                break;
            }
            case APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE:
                bsp_board_led_off(LED_CDC_ACM_OPEN);
                break;
            case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
                bsp_board_led_invert(LED_CDC_ACM_TX);
                break;
            case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
                {
                    ret_code_t ret;
                    //uint8_t *ptr = p_inst->pstartbuf;
                    size_t size;
                    NRF_LOG_INFO("Bytes waiting: %d", app_usbd_cdc_acm_bytes_stored(p_cdc_acm));
                    index = 0;
                    do
                    {
                        if (write_offset[0] >= read_offset[0])
                          size = READ_SIZE - write_offset[0];
                        else if ((write_offset[0]+1) < read_offset[0])
                          size = read_offset[0] - write_offset[0] - 1;
                        else {
                          m_read_usb_flag = 1;
                          break;
                        }
                        if (((write_offset[index] +1) % READ_SIZE) == read_offset[index]) {
                          m_read_usb_flag = 1;
                          break;
                        }
                        m_read_usb_flag = 0;
                        /* Fetch data until internal buffer is empty */
                        /* Fetch data until internal buffer is empty */
                        ret = app_usbd_cdc_acm_read(p_cdc_acm,
                                                &m_rx_buffer[index][write_offset[index]], 
                                               size, &read_len);
                        if (read_len > 0) {
                          if (((write_offset[index] +1) % READ_SIZE) != read_offset[index]) {
                              write_offset[index]+= read_len;
                              write_offset[index] %= READ_SIZE;
                          } else if(ret == NRF_SUCCESS){
                              m_read_usb_flag = 1;
                              break;
                          }
                      }
                    } while (ret == NRF_SUCCESS);
    
                    bsp_board_led_invert(LED_CDC_ACM_RX);
                }
                break;
            
            default:
                break;
        }
    }
    
    #if 0
    /**
     * @brief User event handler @ref app_usbd_cdc_acm_user_ev_handler_t (headphones)
     * */
    static void _cdc_acm_user_ev_handler1(app_usbd_class_inst_t const * p_inst,
                                        app_usbd_cdc_acm_user_event_t event)
    {
        app_usbd_cdc_acm_t const * p_cdc_acm = app_usbd_cdc_acm_class_get(p_inst);
    
        switch (event)
        {
            case APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN:
            {
                bsp_board_led_on(LED_CDC_ACM_OPEN);
    
                /*Setup first transfer*/
                ret_code_t ret = app_usbd_cdc_acm_read(&m_app_cdc_acm,
                                                       m_rx_buffer,
                                                       READ_SIZE);
                UNUSED_VARIABLE(ret);
                break;
            }
            case APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE:
                bsp_board_led_off(LED_CDC_ACM_OPEN);
                break;
            case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
                bsp_board_led_invert(LED_CDC_ACM_TX);
                break;
            case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
            {
                ret_code_t ret;
                NRF_LOG_INFO("Bytes waiting: %d", app_usbd_cdc_acm_bytes_stored(p_cdc_acm));
                do
                {
                    /*Get amount of data transfered*/
                    size_t size = app_usbd_cdc_acm_rx_size(p_cdc_acm);
                    NRF_LOG_INFO("RX: size: %lu char: %c", size, m_rx_buffer[0]);
    
                    /* Fetch data until internal buffer is empty */
                    ret = app_usbd_cdc_acm_read(&m_app_cdc_acm1,
                                                m_rx_buffer,
                                                READ_SIZE);
                } while (ret == NRF_SUCCESS);
    
                bsp_board_led_invert(LED_CDC_ACM_RX);
                break;
            }
            default:
                break;
        }
    }
    #endif
    extern uint16_t m_cdc_setup_flag;
    static void usbd_user_ev_handler(app_usbd_event_type_t event)
    {
        switch (event)
        {
            case APP_USBD_EVT_DRV_SUSPEND:
                bsp_board_led_off(LED_USB_RESUME);
                break;
            case APP_USBD_EVT_DRV_RESUME:
                bsp_board_led_on(LED_USB_RESUME);
                break;
            case APP_USBD_EVT_STARTED:
                break;
            case APP_USBD_EVT_STOPPED:
                app_usbd_disable();
                bsp_board_leds_off();
                break;
            case APP_USBD_EVT_POWER_DETECTED:
                NRF_LOG_INFO("USB power detected");
    
                if (!nrf_drv_usbd_is_enabled())
                {
                    app_usbd_enable();
                }
                break;
            case APP_USBD_EVT_POWER_REMOVED:
                NRF_LOG_INFO("USB power removed");
                app_usbd_stop();
                break;
            case APP_USBD_EVT_POWER_READY:
                NRF_LOG_INFO("USB ready");
                app_usbd_start();
                break;
            default:
                break;
        }
    }
    
    #if 0
    static void bsp_event_callback(bsp_event_t ev)
    {
        ret_code_t ret;
        switch ((unsigned int)ev)
        {
            case CONCAT_2(BSP_EVENT_KEY_, BTN_CDC_DATA_SEND):
            {
                m_send_flag = 1;
                break;
            }
            
            case BTN_CDC_DATA_KEY_RELEASE :
            {
                m_send_flag = 0;
                break;
            }
    
            case CONCAT_2(BSP_EVENT_KEY_, BTN_CDC_NOTIFY_SEND):
            {
                ret = app_usbd_cdc_acm_serial_state_notify(&m_app_cdc_acm,
                                                           APP_USBD_CDC_ACM_SERIAL_STATE_BREAK,
                                                           false);
                UNUSED_VARIABLE(ret);
                break;
            }
    
            default:
                return; // no implementation needed
        }
    }
    
    
    static void init_bsp(void)
    {
        ret_code_t ret;
        ret = bsp_init(BSP_INIT_BUTTONS, bsp_event_callback);
        APP_ERROR_CHECK(ret);
        
        UNUSED_RETURN_VALUE(bsp_event_to_button_action_assign(BTN_CDC_DATA_SEND,
                                                              BSP_BUTTON_ACTION_RELEASE,
                                                              BTN_CDC_DATA_KEY_RELEASE));
        
        /* Configure LEDs */
        bsp_board_init(BSP_INIT_LEDS);
    }
    #endif
    
    #define delay_ms  nrf_delay_ms
    #define delay_us  nrf_delay_us
    
    #if NRF_CLI_ENABLED
    static void init_cli(void)
    {
        ret_code_t ret;
        ret = bsp_cli_init(bsp_event_callback);
        APP_ERROR_CHECK(ret);
        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);
        ret = nrf_cli_start(&m_cli_uart);
        APP_ERROR_CHECK(ret);
    }
    #endif
    
    #if 1
    void init_gpio()
    {
    
        /* P0.00 Output-OC. 0- WLAN green LED on, 1 - off */
        nrf_gpio_cfg(0,
                NRF_GPIO_PIN_DIR_OUTPUT,
                NRF_GPIO_PIN_INPUT_DISCONNECT,
                NRF_GPIO_PIN_NOPULL,
                NRF_GPIO_PIN_S0D1,
                NRF_GPIO_PIN_NOSENSE);
    
        // P0.01 - Output-OC. 0- WLAN blue LED on, 1 - off 
        nrf_gpio_cfg(1,
                NRF_GPIO_PIN_DIR_OUTPUT,
                NRF_GPIO_PIN_INPUT_DISCONNECT,
                NRF_GPIO_PIN_NOPULL,
                NRF_GPIO_PIN_S0D1,
                NRF_GPIO_PIN_NOSENSE);
        // AIN4 CC2 pin. Select device type connected to USB-C port 
        nrf_gpio_cfg(2,
                NRF_GPIO_PIN_DIR_INPUT,
                NRF_GPIO_PIN_INPUT_DISCONNECT,
                NRF_GPIO_PIN_NOPULL,
                NRF_GPIO_PIN_S0S1,
                NRF_GPIO_PIN_NOSENSE);
        // AIN5 CC1 pin. Select device type connected to USB-C port 
        nrf_gpio_cfg(3,
                NRF_GPIO_PIN_DIR_INPUT,
                NRF_GPIO_PIN_INPUT_DISCONNECT,
                NRF_GPIO_PIN_NOPULL,
                NRF_GPIO_PIN_S0S1,
                NRF_GPIO_PIN_NOSENSE);
        // AIN2 comparator input. Below 1.4V the CPU is in reset 
        nrf_gpio_cfg(4,
                NRF_GPIO_PIN_DIR_INPUT,
                NRF_GPIO_PIN_INPUT_DISCONNECT,
                NRF_GPIO_PIN_NOPULL,
                NRF_GPIO_PIN_S0S1,
                NRF_GPIO_PIN_NOSENSE);
    
        // P0.05 Output-OC. 0- MOD1 Blue LED on, off - LED off
        nrf_gpio_cfg(5,
                NRF_GPIO_PIN_DIR_OUTPUT,
                NRF_GPIO_PIN_INPUT_DISCONNECT,
                NRF_GPIO_PIN_NOPULL,
                NRF_GPIO_PIN_S0D1,
                NRF_GPIO_PIN_NOSENSE);
    
        // P0.06 - Debug Uart RXD from CPU
        nrf_gpio_cfg(6,
                NRF_GPIO_PIN_DIR_INPUT,
                NRF_GPIO_PIN_INPUT_DISCONNECT,
                NRF_GPIO_PIN_NOPULL,
                //NRF_GPIO_PIN_S0S1,
                NRF_GPIO_PIN_H0H1,
                NRF_GPIO_PIN_NOSENSE);
          // P0.07 Output-OC. 0- MOD1 Blue LED on, off - LED off
          nrf_gpio_cfg(7,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
          // P0.08 Debug Uart TXD to CPU
          nrf_gpio_cfg(8,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  //NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_H0H1,
                  NRF_GPIO_PIN_NOSENSE);
          // P0.09 Output-OC, with pullup - 0 - reset Ublox wifi, off - normal operation
          nrf_gpio_cfg(9,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
          // P0.10 Output-OC, 0 - MOD2 Red LED on, off - LED off
          nrf_gpio_cfg(10,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
          // P0.11 Output-OC, 0 - Sys greenLED on, off - LED off
          nrf_gpio_cfg(11,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
          // P0.12 Output-OC, 0 - Sys green LED on, off - LED off
          nrf_gpio_cfg(12,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
          // P0.13 I2c SDA -slave
          nrf_gpio_cfg(13,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.14 I2c SDCLK -slave
          nrf_gpio_cfg(14,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.15 Input with pullup - 0-  Mod1 SIM1 detected, 1 - SIM not detected
          nrf_gpio_cfg(15,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.16 Input with pullup - Mod2 SIM detected or Mod1 SIM2 detected. 0-  SIM detected, 1 - SIM not detected
          nrf_gpio_cfg(16,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.17 Output - push/pull: 0- normal Mod1 uses SIM1 & MOD2 uses SIM2; 1 - swap SIM: SIM1 is unsed, Mod1 uses SIM2
          nrf_gpio_cfg(17,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
    
         // P0.18 JTAG/Debug reset
          nrf_gpio_cfg(18,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.19 For 1.8V modules: Output-OC: 0 -Mod1 reset, off - normal operation
         //       For 3.3.V modules- Output - push/pull: 0 - MOD1 reset, 1 - normal operation
          nrf_gpio_cfg(19,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.20 Output-push/pull: 0 -  power down MOD1, 1 - normall operation
          nrf_gpio_cfg(20,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.21 Input with pullup - 0-  Mod1 WAKE request(Normal operation), 1- no wake request
          nrf_gpio_cfg(21,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.22 Output-OC 0 -power down the board, off normal operation
          nrf_gpio_cfg(22,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.23 Input with pullup - 0-  Sourcing power to ETH1 failed, 1-normal operation
          nrf_gpio_cfg(23,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.24 Input with pullup - 0-  Sourcing power to ETH0 failed, 1-normal operation
          nrf_gpio_cfg(24,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.25 Output - push/pull: 0-  Mod2 5V disable, 1 - MOD2 5V enable
          nrf_gpio_cfg(25,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.26 Output-OC: 0-MOD2 Blue LED on, off -LED off
          nrf_gpio_cfg(26,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.27 Output-OC: 0 - MOD2 Green LED on, off - LED off
               nrf_gpio_cfg(27,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.28 AIN0 : USB-C input power/2
          nrf_gpio_cfg(28,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.29 AIN5: Input power/2
          nrf_gpio_cfg(29,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.30 Input 0-  Main power failed, 1-normal operation
          nrf_gpio_cfg(30,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P0.31 AIN7 MOD3 ID: not used at this time
          nrf_gpio_cfg(31,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.00 Input with pullup - not used
          nrf_gpio_cfg(32,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.01 Output: 0 -power down MOD2, 1 - normal operation
          nrf_gpio_cfg(33,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.02 For 1.8V modules: Output-OC: 0 -Mod2 reset, off - normal operation
         //       For 3.3.V modules: Output-push/pull: 0 - MOD2 reset, 1 - normal operation
          nrf_gpio_cfg(34,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.03 input, pullup: 0 -MOD2 WAKE request(normal operation), 1 - no wake request
          nrf_gpio_cfg(35,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.04 Output-OC: 0 - DRAM VTT disable and power down, off - normal normal operation
          nrf_gpio_cfg(36,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.05 Output-OC: 0 - Source power to ETH0; off - normal operation
          nrf_gpio_cfg(37,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.06 Output-OC: 0 - Source power to ETH1; off - normal operation
          nrf_gpio_cfg(38,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.07 Output-push/pull: 0 - Disable power to IO controller; 1 - normal operation
          nrf_gpio_cfg(39,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.08 Output-OC: 0 - MOD1 green light on; off - LED off
          nrf_gpio_cfg(40,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.09 Output-OC: 0 - MOD1 red LED on; off - LED off
          nrf_gpio_cfg(41,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0D1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.10 bidirectional, Output-OC: 0 - System reset, 1 - normal operation
         nrf_gpio_cfg(42,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_NOPULL,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.11 Output: 0 - Set USB-C as host, 1 - as device
          nrf_gpio_cfg(43,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         nrf_gpio_pin_set(43);
         // P1.12 Output: 0 - Do not source power to USB-C, Source power to USB-C
          nrf_gpio_cfg(44,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_DISCONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.13 Output: 0 - CPU boot from SPI NOR; 1 - CPU boot from UART
          nrf_gpio_cfg(45,
                  NRF_GPIO_PIN_DIR_OUTPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.14 input - 0 - Power failure in IO power, MOD2 5V power or USB-C power, 1 - normal operation
          nrf_gpio_cfg(46,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
         // P1.15 input - 0 - config switch pressed, 1 - normal operation
          nrf_gpio_cfg(47,
                  NRF_GPIO_PIN_DIR_INPUT,
                  NRF_GPIO_PIN_INPUT_CONNECT,
                  NRF_GPIO_PIN_PULLUP,
                  NRF_GPIO_PIN_S0S1,
                  NRF_GPIO_PIN_NOSENSE);
          nrf_gpio_pin_clear(42);
          nrf_gpio_pin_clear(22);
          nrf_gpio_pin_clear(9);
          nrf_gpio_pin_clear(39);
          nrf_gpio_pin_clear(20);
          nrf_gpio_pin_clear(33);
          nrf_gpio_pin_clear(25);
          nrf_gpio_pin_clear(44);
          delay_ms(12);
    
          //nrf_gpio_pin_clear(45);
           
          if(nrf_gpio_pin_read(47) == 0) {
            delay_ms(1);
            if (nrf_gpio_pin_read(47) == 0) {
              nrf_gpio_pin_set(45);
            }
          }
         
          //nrf_gpio_pin_set(45);
    
          nrf_gpio_pin_set(22);     
          delay_ms(20);
          nrf_gpio_pin_set(42);
          while(nrf_gpio_pin_read(47) == 0) {
    
          }
          nrf_gpio_pin_set(42);
          delay_ms(2);
          nrf_gpio_pin_set(22);
          delay_ms(2);
          nrf_gpio_pin_set(9);
          delay_ms(2);
          nrf_gpio_pin_set(39);
          delay_ms(2);
          nrf_gpio_pin_set(20);
          delay_ms(2);
          nrf_gpio_pin_set(33);
          delay_ms(2);
          nrf_gpio_pin_set(25);
          delay_ms(2);
          nrf_gpio_pin_set(44);
          delay_ms(0);
          
           
    }
    #endif
    
    //#define NRF_COMP_BASE 0x40013000
    #define NRF_COMP_START (NRF_COMP_BASE + 0x000)
    #define NRF_COMP_STOP  (NRF_COMP_BASE + 0x004)
    #define NRF_COMP_SAMPLE (NRF_COMP_BASE + 0x008)
    #define NRF_COMP_EVENTS_READY (NRF_COMP_BASE + 0x100)
    #define NRF_COMP_EVENTS_DOWN  (NRF_COMP_BASE + 0x104)
    #define NRF_COMP_EVENTS_UP    (NRF_COMP_BASE + 0x108)
    #define NRF_COMP_EVENTS_CROSS (NRF_COMP_BASE + 0x10C)
    #define NRF_COMP_SHORTS       (NRF_COMP_BASE + 0x200)
    #define NRF_COMP_INTEN        (NRF_COMP_BASE + 0x300)
    #define NRF_COMP_INTENSET     (NRF_COMP_BASE + 0x304)
    #define NRF_COMP_INTENCLR     (NRF_COMP_BASE + 0x308)
    #define NRF_COMP_RESULT       (NRF_COMP_BASE + 0x400)
    #define NRF_COMP_ENABLE       (NRF_COMP_BASE + 0x500)
    #define NRF_COMP_PSEL         (NRF_COMP_BASE + 0x504)
    #define NRF_COMP_REFSEL       (NRF_COMP_BASE + 0x508)
    #define NRF_COMP_EXTREFSEL       (NRF_COMP_BASE + 0x50C)
    #define NRF_COMP_TH           (NRF_COMP_BASE + 0x530)
    #define NRF_COMP_MODE         (NRF_COMP_BASE + 0x534)
    #define NRF_COMP_HYST         (NRF_COMP_BASE + 0x538)
    
    
    void comp_init()
    {
          unsigned int *comp = (unsigned int *) NRF_COMP_MODE;
    
          *comp = 0;
          *(unsigned int *)NRF_COMP_REFSEL = 0x4; //VDD
          *(unsigned int *) NRF_COMP_PSEL = 2;
          //Nominal VDD = 4.6V
          //Declare the power to be down when below 1.4V (THDOWN = 20)
          //Declare the power to be up when above 4V (THUP = 56)
          *(unsigned int *) NRF_COMP_TH =(55 <<8 )+ 19;
          *(unsigned int *) NRF_COMP_ENABLE = 0x01;
    }
    
    int comp_read()
    {
      
      return 0;
    }
    
    void comp_sample()
    {
      *((unsigned int *) NRF_COMP_SAMPLE) = 0x01;
    }
    
    void comp_start()
    {
      *((unsigned int *) NRF_COMP_START) = 0x01;
    }
    
    void comp_stop()
    {
      *((unsigned int *) NRF_COMP_STOP) = 0x01;
    }
    
    #define NRF_COMP_AIN0     0
    #define NRF_COMP_AIN2     2
    #define NRF_COMP_AIN4     4
    #define NRF_COMP_AIN5     5
    
    unsigned short board_power, usb_cc1, usb_cc2, input_power;
    
    void comp_process_result(unsigned short ain)
    {
      switch(ain)
      {
      case NRF_COMP_AIN2:
          break;
      case NRF_COMP_AIN4:
          break;
      case NRF_COMP_AIN0:
          break;
      }
    }
    
    int my_ble_init(void);
    void bsp_event_handler(bsp_event_t event);
    
    
    #define UART_READ_SIZE 256
    
    unsigned int uart_write_offset = 0, uart_read_offset = 0;
    unsigned char m_uart_rx_buffer[UART_READ_SIZE];
    unsigned long uart_rx_error = 0, uart_tx_error = 0, usb_rx_error = 0, usb_tx_error = 0, m_read_uart_flag = 0;
    extern uint32_t uart_rx_bytes;
    /**@brief   Function for handling app_uart events.
     *
     * @details This function will receive a single character from the app_uart module and append it to
     *          a string. The string will be be sent over USB.
     */
    /**@snippet [Handling the data received over UART] */
    void uart_event_handle(app_uart_evt_t * p_event)
    {
        uint32_t       err_code;
        uint32_t        len;
    
        switch (p_event->evt_type)
        {
            case APP_UART_DATA_READY:
                if (((uart_write_offset+1) %UART_READ_SIZE) == uart_read_offset) {
                    m_read_uart_flag = 1;
                    break;
                }
                m_read_uart_flag = 0;
    #if 0
                len = UART_READ_SIZE - uart_write_offset;
                err_code = app_uart_get(&m_uart_rx_buffer[uart_write_offset]);
    #ifndef UART_SOFT_LOOPBACK
                if (err_code == NRF_SUCCESS)
                  uart_write_offset++;
                else {
                  uart_rx_error++;
                  NRF_LOG_DEBUG("Error reading UART");
                }
    
    #endif
    #else
                if (uart_write_offset >= uart_read_offset)
                  len = UART_READ_SIZE - uart_write_offset;
                else 
                  len = uart_read_offset - uart_write_offset;
                if (len == 0)
                {
                    m_read_uart_flag = 1;
                    break;
                }
                err_code = app_uart_read(&m_uart_rx_buffer[uart_write_offset], &len);
    #ifndef UART_SOFT_LOOPBACK
                if (err_code == NRF_SUCCESS) {
                  uart_write_offset+= len;
                  uart_write_offset %= UART_READ_SIZE;
                  uart_rx_bytes += len;
                }
                else {
                  uart_rx_error++;
                  NRF_LOG_DEBUG("Error reading UART");
                }
    
    #endif
    #endif
                        
    #if 0
                if ((data_array[uart_write_offset - 1] == '\n') ||
                    (data_array[uart_write_offset - 1] == '\r') ||
                    (index >= m_ble_nus_max_data_len))
                {
                    if (index > 1)
                    {
                        NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                        NRF_LOG_HEXDUMP_DEBUG(data_array, index);
    #if 0
                        do
                        {
                            uint16_t length = (uint16_t)index;
                            err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                            if ((err_code != NRF_ERROR_INVALID_STATE) &&
                                (err_code != NRF_ERROR_RESOURCES) &&
                                (err_code != NRF_ERROR_NOT_FOUND))
                            {
                                APP_ERROR_CHECK(err_code);
                            }
                        } while (err_code == NRF_ERROR_RESOURCES);
    
    #else                
                        do
                        {
                            uint16_t length;
                            if (uart_read_offset < uart_write_offset)
                                length = uart_write_offset - uart_read_offset;
                            else if (uart_read_offset > uart_write_offset)
                                length = UART_READ_SIZE - uart_read_offset; 
                            else
                                break;
    
                            err_code = usb_cdc_acm_write(&m_nus, &m_uart_rx_buffer[uart_read_offset], &length, m_conn_handle);
                            if ((err_code != NRF_ERROR_INVALID_STATE) &&
                                (err_code != NRF_ERROR_RESOURCES) &&
                                (err_code != NRF_ERROR_NOT_FOUND))
                            {
                                APP_ERROR_CHECK(err_code);
                            }
                        } while (err_code == NRF_ERROR_RESOURCES);
                        if (uart_read_offset && uart_read_offset == uart_write_offset)
                            uart_read_offset = uart_write_offset = 0;
    #endif
                    }
               }
    #endif
                
                break;
    
            case APP_UART_COMMUNICATION_ERROR:
                APP_ERROR_HANDLER(p_event->data.error_communication);
                break;
    
            case APP_UART_FIFO_ERROR:
                APP_ERROR_HANDLER(p_event->data.error_code);
                break;
    
            default:
                break;
        }
    }
    /**@snippet [Handling the data received over UART] */
    
    
    /**@brief  Function for initializing the UART module.
     */
    /**@snippet [UART Initialization] */
    static void uart_init(void)
    {
        uint32_t                     err_code;
        app_uart_comm_params_t const comm_params =
        {
    #if 0
            .rx_pin_no    = RX_PIN_NUMBER,
            .tx_pin_no    = TX_PIN_NUMBER,
    #else
            .rx_pin_no    = TX_PIN_NUMBER,
            .tx_pin_no    = RX_PIN_NUMBER,
    #endif
            .rts_pin_no   = RTS_PIN_NUMBER,
            .cts_pin_no   = CTS_PIN_NUMBER,
            .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
            .use_parity   = false,
    #define ENCOREDEVICE
    #ifdef ENCOREDEVICE
            .baud_rate    = NRF_UARTE_BAUDRATE_115200
    #else
    #if defined (UART_PRESENT)
            .baud_rate    = NRF_UART_BAUDRATE_115200
    #else
            .baud_rate    = NRF_UARTE_BAUDRATE_115200
    #endif
    #endif
        };
    
        APP_UART_FIFO_INIT(&comm_params,
                           UART_RX_BUF_SIZE,
                           UART_TX_BUF_SIZE,
                           uart_event_handle,
                           APP_IRQ_PRIORITY_LOWEST,
                           err_code);
        APP_ERROR_CHECK(err_code);
    }
    //uint32_t nrfx_systick_ms_tick(uint32_t ms);
    //uint32_t get_ms_tick(uint32_t ms)
    //{
    //  return (nrf_systick_ms_tick(ms) + nrf_cur_ms_tick());
    //}
    
    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "nrf_drv_timer.h"
    #include "bsp.h"
    #include "app_error.h"
    
    const nrf_drv_timer_t TIMER_TICK = NRF_DRV_TIMER_INSTANCE(0);
    static uint64_t ticks;
        
    /**
     * @brief Handler for timer events.
     */
    void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context)
    {    
        (void)(p_context);
        switch (event_type)
        {
            case NRF_TIMER_EVENT_COMPARE0:
                ticks++;
                break;
    
            default:
                //Do nothing.
                break;
        }
    }
    
    
    /**
     * @brief Function for main application entry.
     */
    int init_timer(void)
    {
        uint32_t time_ms = 1; //Time(in miliseconds) between consecutive compare events.
        uint32_t time_ticks;
        uint32_t err_code = NRF_SUCCESS;
        
        ticks++;
        //Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.
        nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
        err_code = nrf_drv_timer_init(&TIMER_TICK, &timer_cfg, timer_led_event_handler);
        APP_ERROR_CHECK(err_code);
    
        time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_TICK, time_ms);
    
        nrf_drv_timer_extended_compare(
             &TIMER_TICK, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
    
        nrf_drv_timer_enable(&TIMER_TICK);
    
        return 0;
    }
    
    
    uint64_t get_ms_ticks(uint32_t ms)
    {
        return (ticks + ms);
    }
    
    int check_ms_ticks(uint64_t ms)
    {
        if (ticks >= ms)
          return 1;
        return 0;
    }
    uint32_t uart_rx_bytes, uart_tx_bytes, usb_tx_bytes;
    extern uint32_t usb_rx_bytes;
    int main(void)
    {
    #if 1
        ret_code_t ret;
        //int loop_count = 0;
        //unsigned short comp_state = 0;
        uint32_t len;
        volatile unsigned int *ptr;
        extern int m_uarte_rx_flag;
        extern uint64_t m_uarte_rx_timer;
        unsigned int read_len;
        size_t size;
                    
    
        static const app_usbd_config_t usbd_config = {
            .ev_state_proc = usbd_user_ev_handler
        };
    #if 1
        ptr = (unsigned int *) 0x10001304;
        if ((ptr[0] & 0x07) != 0x05) {
            ptr = (unsigned int *) 0x4001E504;
            *ptr = 0x01;
            *(unsigned int *) 0x10001304 = 0x05;
            *ptr = 0;
        }
        *(unsigned int *) 0x40000578 |= 0x01;
        *(unsigned int *) 0x40000580 = 0x00;
    #endif
     
        ret = NRF_LOG_INIT(NULL);
        APP_ERROR_CHECK(ret);
    
        ret = nrf_drv_clock_init();
        APP_ERROR_CHECK(ret);
        
        nrf_drv_clock_lfclk_request(NULL);
    
     
        while(!nrf_drv_clock_lfclk_is_running())
        {
            /* Just waiting */
        }
        nrfx_systick_init();
        init_gpio();
        uart_init();
        init_timer();       
        //ret = app_timer_init();
        //APP_ERROR_CHECK(ret);
    
        //uint32_t err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler);
        //APP_ERROR_CHECK(err_code);
        //init_bsp();
    #if NRF_CLI_ENABLED
        init_cli();
    #endif
    
        app_usbd_serial_num_generate();
    
        ret = app_usbd_init(&usbd_config);
        APP_ERROR_CHECK(ret);
        NRF_LOG_INFO("USBD CDC ACM example started.");
    
        app_usbd_class_inst_t const * class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&m_app_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();
        }
        
        //my_ble_init();    
    
    #if 0
    
        init_gpio();
        comp_init();
        comp_start();
    #endif
        while (true) {
            if (m_read_usb_flag == 1) {
              //app_usbd_class_inst_t const * p_inst = app_usbd_cdc_acm_class_inst_get(&m_app_cdc_acm);
              do {
                    if (write_offset[0] >= read_offset[0])
                         size = READ_SIZE - write_offset[0];
                    else if (((write_offset[0]+1) %READ_SIZE) < read_offset[0])
                         size = read_offset[0] - write_offset[0] - 1;
                    else {
                         m_read_usb_flag = 1;
                         break;
                    }
                    if (size == 0)
                         break;
                    ret = app_usbd_cdc_acm_read(&m_app_cdc_acm,
                                                &m_rx_buffer[0][write_offset[0]],
                                                size, &read_len);
                    if (read_len > 0) {
                         write_offset[0] += read_len;
                         write_offset[0] %= READ_SIZE;
                         m_read_usb_flag = 0;
                    }
                } while(ret == NRF_SUCCESS);
            }
            if (m_uarte_rx_flag) {
                disable_uarte_int(0);
                if (check_ms_ticks(m_uarte_rx_timer)) {
                  uarte_irq_handler0();
                }
                enable_uarte_int(0);
            }
            if (m_read_uart_flag == 1) {
                if (uart_write_offset >= uart_read_offset)
                  len = UART_READ_SIZE - uart_write_offset;
                else 
                  len = uart_read_offset - uart_write_offset;
                if (len == 0)
                {
                    m_read_uart_flag = 1;
                    break;
                }
                m_read_uart_flag = 0;
                ret_code_t err_code = app_uart_read(&m_uart_rx_buffer[uart_write_offset], &len);
    #ifndef UART_SOFT_LOOPBACK
                if (err_code == NRF_SUCCESS) {
                  uart_write_offset+= len;
                  uart_rx_bytes += len;
                  uart_write_offset %= UART_READ_SIZE;
                }
                else {
                  uart_rx_error++;
                  NRF_LOG_DEBUG("Error reading UART");
                }
    
    #endif
          }
          if (uart_read_offset != uart_write_offset) {
                  uint32_t len;
                  if (uart_write_offset > uart_read_offset)
                    len = uart_write_offset - uart_read_offset;
                  else
                    len = UART_READ_SIZE - uart_read_offset;
                  ret = app_usbd_cdc_acm_write(&m_app_cdc_acm, m_uart_rx_buffer + uart_read_offset, len);
                  if (ret == NRF_SUCCESS) {
                      uart_read_offset += len;
                      usb_tx_bytes += len;
                      uart_read_offset %= UART_READ_SIZE;
                  }
          } 
    
    
          while (read_offset[0] != write_offset[0]) {
                  ret = NRF_ERROR_BUSY;
    #if UART_SOFT_LOOPBACK
                  if (uart_write_offset < UART_READ_SIZE) {
                      m_uart_rx_buffer[uart_write_offset++] = m_rx_buffer[0][read_offset[0]];
                      ret = NRF_SUCCESS;
                  }
    #else
                  //ret = app_uart_put( m_rx_buffer[0][read_offset[0]]);
                  if (write_offset[0] > read_offset[0])
                      len =  write_offset[0] - read_offset[0];
                  else
                      len = READ_SIZE - read_offset[0];
                  if (len > 1)
                    len = 1;
                  ret = app_uart_write( &m_rx_buffer[0][read_offset[0]], &len);
                                                  
    #endif
                  if (ret == NRF_SUCCESS) {
                      read_offset[0]+= len;
                      uart_tx_bytes += len;
                      read_offset[0] %= READ_SIZE;
                  } else
                      break;
            }
    #if 0
            while(((write_offset[0]+1) % READ_SIZE) != read_offset[0]) {
                  if (write_offset[0] >= read_offset[0])
                      size = READ_SIZE - write_offset[0];
                  else if (((write_offset[0]+1) %READ_SIZE) < read_offset[0])
                      size = read_offset[0] - write_offset[0] - 1;
                  else {
                      break;
                  }
                  if (size == 0)
                      break;
                  ret = _app_usbd_cdc_acm_read(&m_app_cdc_acm,
                                                &m_rx_buffer[0][write_offset[0]],
                                                size, &read_len);
                  if (read_len > 0) {
                      write_offset[0] += read_len;
                      write_offset[0] %= READ_SIZE;
                      m_read_usb_flag = 0;
                 } else
                      break;
            }           
    #endif
    #if 0
            if (++loop_count % 10000) {
              switch(comp_state++) {
              default:
                comp_state = 0;
              case 0:
                comp_stop();
                delay_ms(1);
                comp_init(NRF_COMP_AIN2);
                comp_start();
                break;
              case 1:
                comp_sample(NRF_COMP_AIN2);
                comp_process_result(NRF_COMP_AIN2);
                break;
    
              case 2:
                comp_stop();
                delay_ms(1);
                comp_init(NRF_COMP_AIN4);
                comp_start();
                break;
    
              case 3:
                comp_sample(NRF_COMP_AIN4);
                break;
    
              case 4:
                comp_stop();
                delay_ms(1);
                comp_init(NRF_COMP_AIN5);
                comp_start();
                break;
    
              case 5:
                comp_sample(NRF_COMP_AIN5);
                comp_process_result(NRF_COMP_AIN5);
                break;
    
              case 6:
                comp_stop();
                delay_ms(1);
                comp_init(NRF_COMP_AIN0);
                comp_start();
                break;
    
              case 7:
                comp_sample(NRF_COMP_AIN0);
                comp_process_result(NRF_COMP_AIN0);            
                comp_state = 0;
                break;
              }
            }
    
            if (comp_read() < 0) {
    
            }
    #endif
          
            app_usbd_event_queue_process();
            
            
    #if NRF_CLI_ENABLED
            nrf_cli_process(&m_cli_uart);
    #endif
    
            //UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
            /* Sleep CPU only if there was no interrupt since last loop processing */
            //__WFE();
        }
    #else
        my_ble_init();
        // Enter main loop.
        for (;;)
        {
            //idle_state_handle();
        }
    
    #endif
    }
    
    
    
    /** @} */
    

Children
No Data
Related