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

BLUETOOTH & USBD CDC ACM WRITE PACKETS LOSS

Hi I'm new with programming nrf52840 dongle (I'm using Ubuntu),

I try to wrote this code taking inspiration from the nRF5_SDK_15.0.0_a53641a (peripheral/usbd_cdc_acm and peripheral/radio/trasmitter) because I want to capture bluetooth data and send them via serial.

Once I program my dongle, then I use screen to see /dev/ttyACMx but I realized I did not capture all the data.
Can someone help me to understand where I'm wrong?

Thanks in advance.

this is the main function:

int main(void)
{
    ret_code_t 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
    }

    ret = app_timer_init();
    APP_ERROR_CHECK(ret);

    ret = nrf_cli_init(&m_cli_cdc_acm, NULL, true, true, NRF_LOG_SEVERITY_INFO);
    APP_ERROR_CHECK(ret);

    usbd_init();

    ret = nrf_cli_start(&m_cli_cdc_acm);
    APP_ERROR_CHECK(ret);

    int channel = 37;
    my_radio_configure(BLE_1MB, channel);

    NRF_RADIO->PACKETPTR = (uint32_t) packet;

    int last_atts = 0;

    while (true)
    {
        UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
       #if CLI_OVER_USB_CDC_ACM && APP_USBD_CONFIG_EVENT_QUEUE_ENABLE
    berga
        while (app_usbd_event_queue_process())
        {
            /* Nothing to do */
        }
       #endif
        uint32_t received = read_packet();

        if(received == 0) continue;
        char m_tx_buffer[2000];
        sprintf(m_tx_buffer, "[%d]:packet: ", cnt);
        cnt++;

        int kk;
        for(kk = 0; kk < 20; kk ++) {
                sprintf(m_tx_buffer + strlen(m_tx_buffer), "%02X ", packet[kk]);
        }
        sprintf(m_tx_buffer + strlen(m_tx_buffer), "\r\n");
        size_t size = strlen(m_tx_buffer) + 1;
        last_atts = 0;

        do{     last_atts ++;
                ret = app_usbd_cdc_acm_write(&nrf_cli_cdc_acm, m_tx_buffer, size);
                //if(ret == NRF_SUCCESS) break;
        }while(ret != NRF_SUCCESS);
        
        //nrf_delay_ms(1000);
        // nrf_cli_process(&m_cli_cdc_acm);
    }
}

Parents
  • Hi,

    Can you elaborate on what you mean by:

    but I realized I did not capture all the data.
    • What are you seeing, and what are you expecting to see?
    • Have you tested it with another OS or terminal software?
    • Can you provide the entire project? This way we can try to reproduce your issues and help debugging it.

    Best regards,
    Jørgen

  • Hi Jorgen,

    What are you seeing, and what are you expecting to see?

    [113]:packet: 42 25 A9 35 49 16 15 31 1E FF 06 00 01 09 20 02 9D C1 12 2D
    [114]:packet: 42 25 A9 35 49 16 15 31 1E FF 06 00 01 09 20 02 9D C1 12 2D
    [117]:packet: 42 25 A9 35 49 16 15 31 1E FF 06 00 01 09 20 02 9D C1 12 2D
    [118]:packet: 42 25 A9 35 49 16 15 31 1E FF 06 00 01 09 20 02 9D C1 12 2D
    [119]:packet: 40 14 F6 4A 5F 8E B5 6C 02 01 1A 0A FF 4C 00 10 05 03 1C 95

    this is an example of output using screen, the packets 115/116 were not captured.

    Have you tested it with another OS or terminal software?

    No I tried only with Ubuntu because I found it difficult to program the dongle with other operating system.

    Can you provide the entire project?

    Yes of course! thanks for the quick response!

    #include <stdio.h>
    #include <stdbool.h>
    #include <stddef.h>
    
    #include "radio_config.h"
    
    #include "nrf.h"
    #include "nrf_drv_clock.h"
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    
    #include "app_timer.h"
    
    #include "app_error.h"
    #include "app_util.h"
    
    #include "nrf_cli.h"
    #include "nrf_cli_rtt.h"
    #include "nrf_cli_types.h"
    
    #include "boards.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    
    #include "nrf_cli_cdc_acm.h"
    #include "nrf_drv_usbd.h"
    #include "app_usbd_core.h"
    #include "app_usbd.h"
    #include "app_usbd_string_desc.h"
    #include "app_usbd_cdc_acm.h"
    
    /* If enabled then CYCCNT (high resolution) timestamp is used for the logger. */
    #define USE_CYCCNT_TIMESTAMP_FOR_LOG 0
    
    /**
     * @brief Enable power USB detection
     *
     * Configure if example supports USB port connection
     */
    #ifndef USBD_POWER_DETECTION
    #define USBD_POWER_DETECTION true
    #endif
    
    static int cnt = 0;
    
    static void usbd_user_ev_handler(app_usbd_event_type_t event)
    {
        switch (event)
        {
            case APP_USBD_EVT_STOPPED:
                app_usbd_disable();
                break;
            case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
    	    cnt ++;
    	    break;
            case APP_USBD_EVT_POWER_DETECTED:
                if (!nrf_drv_usbd_is_enabled())
                {
                    app_usbd_enable();
                }
                break;
            case APP_USBD_EVT_POWER_REMOVED:
                app_usbd_stop();
                break;
            case APP_USBD_EVT_POWER_READY:
                app_usbd_start();
                break;
            default:
                break;
        }
    }
    
    /**
     * @brief Command line interface instance
     * */
    #define CLI_EXAMPLE_LOG_QUEUE_SIZE  (4)
    
    NRF_CLI_CDC_ACM_DEF(m_cli_cdc_acm_transport);
    NRF_CLI_DEF(m_cli_cdc_acm,
                "usb_cli:~$ ",
                &m_cli_cdc_acm_transport.transport,
                '\r',
                CLI_EXAMPLE_LOG_QUEUE_SIZE);
    
    /*
    NRF_CLI_RTT_DEF(m_cli_rtt_transport);
    NRF_CLI_DEF(m_cli_rtt,
                "rtt_cli:~$ ",
                &m_cli_rtt_transport.transport,
                '\n',
                CLI_EXAMPLE_LOG_QUEUE_SIZE);
    */
    
    static void usbd_init(void)
    {
        ret_code_t ret;
        static const app_usbd_config_t usbd_config = {
            .ev_handler = app_usbd_event_execute,
            .ev_state_proc = usbd_user_ev_handler
        };
        ret = app_usbd_init(&usbd_config);
        APP_ERROR_CHECK(ret);
    
        app_usbd_class_inst_t const * class_cdc_acm =
                app_usbd_cdc_acm_class_inst_get(&nrf_cli_cdc_acm);
        ret = app_usbd_class_append(class_cdc_acm);
        APP_ERROR_CHECK(ret);
    
        if (USBD_POWER_DETECTION)
        {
            ret = app_usbd_power_events_enable();
            APP_ERROR_CHECK(ret);
        }
        else
        {
            NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
    
            app_usbd_enable();
            app_usbd_start();
        }
    
        /* Give some time for the host to enumerate and connect to the USB CDC port */
        nrf_delay_ms(1000);
    }
    
    uint32_t cyccnt_get(void)
    {
        return DWT->CYCCNT;
    }
    
    #define m_packetHeaderS1len 0
    #define m_packetHeaderS0len 1
    #define m_packetHeaderLFlen 8
    
    typedef enum {
    	BLE_1MB,
    	BLE_2MB,
    	BLE_CODED_FAST,
    	BLE_CODED_SLOW,
    } radio_mode_t;
    
    #define PACKET_S1_FIELD_SIZE      (2UL)  /**< Packet S1 field size in bits. */
    #define PACKET_S0_FIELD_SIZE      (1UL)  /**< Packet S0 field size in bits. */
    #define PACKET_LENGTH_FIELD_SIZE  (6UL)  /**< Packet length field size in bits. */
    #define MAX_PDU_SIZE    (64UL)
    #define MY_SET_BIT(n)      (1UL << n)
    
    static uint8_t packet[256];
    
    static __inline int8_t ch2freq(uint8_t ch)
    {
      switch (ch) {
      case 37:
        return 2;
      case 38:
        return 26;
      case 39:
        return 80;
      default:
        if (ch > 39)
          return -1;
        else if (ch < 11)
          return 4 + (2 * ch);
        else
          return 6 + (2 * ch);
      }
    }
    
    void my_radio_configure(radio_mode_t rm, int channel)
    {
        // Radio config
        NRF_RADIO->TXPOWER     = (RADIO_TXPOWER_TXPOWER_Pos8dBm << RADIO_TXPOWER_TXPOWER_Pos);
        NRF_RADIO->DATAWHITEIV = channel & 0x3F;
        NRF_RADIO->FREQUENCY   = ch2freq(channel);
        NRF_RADIO->PREFIX0     = 0x0000008E;
        NRF_RADIO->BASE0       = 0x89BED600;
        NRF_RADIO->RXADDRESSES = 0x00000001;
    
        // Packet configuration
    #define m_packetHeaderPlen1MB RADIO_PCNF0_PLEN_8bit
    #define m_packetHeaderPlen2MB RADIO_PCNF0_PLEN_16bit
    #define m_packetHeaderPlenCODED RADIO_PCNF0_PLEN_LongRange
        switch(rm) {
        case BLE_1MB:
        	NRF_RADIO->MODE  = (RADIO_MODE_MODE_Ble_1Mbit << RADIO_MODE_MODE_Pos);
        	NRF_RADIO->PCNF0 = (m_packetHeaderS1len << RADIO_PCNF0_S1LEN_Pos) |
        	                   (m_packetHeaderS0len << RADIO_PCNF0_S0LEN_Pos) |
        	                   (m_packetHeaderLFlen << RADIO_PCNF0_LFLEN_Pos) |
        	                   (m_packetHeaderPlen1MB << RADIO_PCNF0_PLEN_Pos);
    	break;
    
        case BLE_2MB:
            NRF_RADIO->MODE  = (RADIO_MODE_MODE_Ble_2Mbit << RADIO_MODE_MODE_Pos);
        	NRF_RADIO->PCNF0 = (m_packetHeaderS1len << RADIO_PCNF0_S1LEN_Pos) |
        	                   (m_packetHeaderS0len << RADIO_PCNF0_S0LEN_Pos) |
        	                   (m_packetHeaderLFlen << RADIO_PCNF0_LFLEN_Pos) |
        	                   (m_packetHeaderPlen2MB << RADIO_PCNF0_PLEN_Pos);
    	break;
    
        case BLE_CODED_FAST:
        	NRF_RADIO->MODE  = (RADIO_MODE_MODE_Ble_LR500Kbit << RADIO_MODE_MODE_Pos);
        	NRF_RADIO->PCNF0 = (m_packetHeaderS1len << RADIO_PCNF0_S1LEN_Pos) |
        	                   (m_packetHeaderS0len << RADIO_PCNF0_S0LEN_Pos) |
        	                   (m_packetHeaderLFlen << RADIO_PCNF0_LFLEN_Pos) |
        	                   (3 << RADIO_PCNF0_TERMLEN_Pos) |
        	                   (2 << RADIO_PCNF0_CILEN_Pos) |
        	                   (m_packetHeaderPlenCODED << RADIO_PCNF0_PLEN_Pos);
    	break;
    
        case BLE_CODED_SLOW:
        	NRF_RADIO->MODE  = (RADIO_MODE_MODE_Ble_LR125Kbit << RADIO_MODE_MODE_Pos);
        	NRF_RADIO->PCNF0 = (m_packetHeaderS1len << RADIO_PCNF0_S1LEN_Pos) |
        	                   (m_packetHeaderS0len << RADIO_PCNF0_S0LEN_Pos) |
        	                   (m_packetHeaderLFlen << RADIO_PCNF0_LFLEN_Pos) |
        	                   (3 << RADIO_PCNF0_TERMLEN_Pos) |
        	                   (2 << RADIO_PCNF0_CILEN_Pos) |
        	                   (m_packetHeaderPlenCODED << RADIO_PCNF0_PLEN_Pos);
    	break;
        }
    
        // Packet configuration
        NRF_RADIO->PCNF1 = 3UL << RADIO_PCNF1_BALEN_Pos;
        NRF_RADIO->PCNF1 |= RADIO_PCNF1_WHITEEN_Enabled << RADIO_PCNF1_WHITEEN_Pos;
        NRF_RADIO->PCNF1 |= MAX_PDU_SIZE << RADIO_PCNF1_MAXLEN_Pos;
    
        // CRC Config
        NRF_RADIO->CRCCNF = RADIO_CRCCNF_LEN_Three << RADIO_CRCCNF_LEN_Pos |
                            RADIO_CRCCNF_SKIP_ADDR_Skip << RADIO_CRCCNF_SKIP_ADDR_Pos;
        NRF_RADIO->CRCINIT = 0x555555UL;
        NRF_RADIO->CRCPOLY = MY_SET_BIT(24) | MY_SET_BIT(10) | MY_SET_BIT(9) |
                             MY_SET_BIT(6) | MY_SET_BIT(4) | MY_SET_BIT(3) |
                             MY_SET_BIT(1) | MY_SET_BIT(0);
    }
    
    uint32_t read_packet()
    {
        uint32_t result = 0;
    
        NRF_RADIO->EVENTS_READY = 0U;
        // Enable radio and wait for ready
        NRF_RADIO->TASKS_RXEN = 1U;
    
        while (NRF_RADIO->EVENTS_READY == 0U)
        {
            // wait
        }
        NRF_RADIO->EVENTS_END = 0U;
        // Start listening and wait for address received event
        NRF_RADIO->TASKS_START = 1U;
    
        // Wait for end of packet or buttons state changed
        while (NRF_RADIO->EVENTS_END == 0U)
        {
            // wait
        }
    
        if (NRF_RADIO->CRCSTATUS == 1U)
        {
            result = 1;
        }
        NRF_RADIO->EVENTS_DISABLED = 0U;
        // Disable radio
        NRF_RADIO->TASKS_DISABLE = 1U;
    
        while (NRF_RADIO->EVENTS_DISABLED == 0U)
        {
            // wait
        }
        return result;
    }
    
    
    int main(void)
    {
        ret_code_t ret;
    
    /*
        if (USE_CYCCNT_TIMESTAMP_FOR_LOG)
        {
            CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
            DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
            DWT->CYCCNT = 0;
            APP_ERROR_CHECK(NRF_LOG_INIT(cyccnt_get, 64000000));
        }
        else
        {
            APP_ERROR_CHECK(NRF_LOG_INIT(app_timer_cnt_get));
        }
    */
    
    /*
        // DON'T DO THIS, pca10059 would not start
        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
        }
    
        ret = app_timer_init();
        APP_ERROR_CHECK(ret);
    
        ret = nrf_cli_init(&m_cli_cdc_acm, NULL, true, true, NRF_LOG_SEVERITY_INFO);
        APP_ERROR_CHECK(ret);
    
        usbd_init();
    
        ret = nrf_cli_start(&m_cli_cdc_acm);
        APP_ERROR_CHECK(ret);
    
        int channel = 37;
        my_radio_configure(BLE_1MB, channel);
    
        NRF_RADIO->PACKETPTR = (uint32_t) packet;
    
        int last_atts = 0;
    
        while (true)
        {
            UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
    #if CLI_OVER_USB_CDC_ACM && APP_USBD_CONFIG_EVENT_QUEUE_ENABLE
    berga
            while (app_usbd_event_queue_process())
            {
                /* Nothing to do */
            }
    #endif
    
            uint32_t received = read_packet();
            if(received == 0) continue;
    
    	// static char m_tx_buffer[NRF_DRV_USBD_EPSIZE];
    	//static int frame_counter = 0;
    
    	char m_tx_buffer[2000];
    	sprintf(m_tx_buffer, "[%d]:packet: ", cnt);
    	cnt++;
        int kk;
    
    	for(kk = 0; kk < 20; kk ++) {
    		sprintf(m_tx_buffer + strlen(m_tx_buffer), "%02X ", packet[kk]);
    	}
    
    	sprintf(m_tx_buffer + strlen(m_tx_buffer), "\r\n");
            size_t size = strlen(m_tx_buffer) + 1;
    
            last_atts = 0;
    
    	do{	last_atts ++;
    		ret = app_usbd_cdc_acm_write(&nrf_cli_cdc_acm, m_tx_buffer, size);
            	//        if(ret == NRF_SUCCESS) break;
    	}while(ret != NRF_SUCCESS);
    	//frame_counter ++;
            //nrf_delay_ms(1000);
            // nrf_cli_process(&m_cli_cdc_acm);
        }
    }

Reply
  • Hi Jorgen,

    What are you seeing, and what are you expecting to see?

    [113]:packet: 42 25 A9 35 49 16 15 31 1E FF 06 00 01 09 20 02 9D C1 12 2D
    [114]:packet: 42 25 A9 35 49 16 15 31 1E FF 06 00 01 09 20 02 9D C1 12 2D
    [117]:packet: 42 25 A9 35 49 16 15 31 1E FF 06 00 01 09 20 02 9D C1 12 2D
    [118]:packet: 42 25 A9 35 49 16 15 31 1E FF 06 00 01 09 20 02 9D C1 12 2D
    [119]:packet: 40 14 F6 4A 5F 8E B5 6C 02 01 1A 0A FF 4C 00 10 05 03 1C 95

    this is an example of output using screen, the packets 115/116 were not captured.

    Have you tested it with another OS or terminal software?

    No I tried only with Ubuntu because I found it difficult to program the dongle with other operating system.

    Can you provide the entire project?

    Yes of course! thanks for the quick response!

    #include <stdio.h>
    #include <stdbool.h>
    #include <stddef.h>
    
    #include "radio_config.h"
    
    #include "nrf.h"
    #include "nrf_drv_clock.h"
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    
    #include "app_timer.h"
    
    #include "app_error.h"
    #include "app_util.h"
    
    #include "nrf_cli.h"
    #include "nrf_cli_rtt.h"
    #include "nrf_cli_types.h"
    
    #include "boards.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    
    #include "nrf_cli_cdc_acm.h"
    #include "nrf_drv_usbd.h"
    #include "app_usbd_core.h"
    #include "app_usbd.h"
    #include "app_usbd_string_desc.h"
    #include "app_usbd_cdc_acm.h"
    
    /* If enabled then CYCCNT (high resolution) timestamp is used for the logger. */
    #define USE_CYCCNT_TIMESTAMP_FOR_LOG 0
    
    /**
     * @brief Enable power USB detection
     *
     * Configure if example supports USB port connection
     */
    #ifndef USBD_POWER_DETECTION
    #define USBD_POWER_DETECTION true
    #endif
    
    static int cnt = 0;
    
    static void usbd_user_ev_handler(app_usbd_event_type_t event)
    {
        switch (event)
        {
            case APP_USBD_EVT_STOPPED:
                app_usbd_disable();
                break;
            case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
    	    cnt ++;
    	    break;
            case APP_USBD_EVT_POWER_DETECTED:
                if (!nrf_drv_usbd_is_enabled())
                {
                    app_usbd_enable();
                }
                break;
            case APP_USBD_EVT_POWER_REMOVED:
                app_usbd_stop();
                break;
            case APP_USBD_EVT_POWER_READY:
                app_usbd_start();
                break;
            default:
                break;
        }
    }
    
    /**
     * @brief Command line interface instance
     * */
    #define CLI_EXAMPLE_LOG_QUEUE_SIZE  (4)
    
    NRF_CLI_CDC_ACM_DEF(m_cli_cdc_acm_transport);
    NRF_CLI_DEF(m_cli_cdc_acm,
                "usb_cli:~$ ",
                &m_cli_cdc_acm_transport.transport,
                '\r',
                CLI_EXAMPLE_LOG_QUEUE_SIZE);
    
    /*
    NRF_CLI_RTT_DEF(m_cli_rtt_transport);
    NRF_CLI_DEF(m_cli_rtt,
                "rtt_cli:~$ ",
                &m_cli_rtt_transport.transport,
                '\n',
                CLI_EXAMPLE_LOG_QUEUE_SIZE);
    */
    
    static void usbd_init(void)
    {
        ret_code_t ret;
        static const app_usbd_config_t usbd_config = {
            .ev_handler = app_usbd_event_execute,
            .ev_state_proc = usbd_user_ev_handler
        };
        ret = app_usbd_init(&usbd_config);
        APP_ERROR_CHECK(ret);
    
        app_usbd_class_inst_t const * class_cdc_acm =
                app_usbd_cdc_acm_class_inst_get(&nrf_cli_cdc_acm);
        ret = app_usbd_class_append(class_cdc_acm);
        APP_ERROR_CHECK(ret);
    
        if (USBD_POWER_DETECTION)
        {
            ret = app_usbd_power_events_enable();
            APP_ERROR_CHECK(ret);
        }
        else
        {
            NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
    
            app_usbd_enable();
            app_usbd_start();
        }
    
        /* Give some time for the host to enumerate and connect to the USB CDC port */
        nrf_delay_ms(1000);
    }
    
    uint32_t cyccnt_get(void)
    {
        return DWT->CYCCNT;
    }
    
    #define m_packetHeaderS1len 0
    #define m_packetHeaderS0len 1
    #define m_packetHeaderLFlen 8
    
    typedef enum {
    	BLE_1MB,
    	BLE_2MB,
    	BLE_CODED_FAST,
    	BLE_CODED_SLOW,
    } radio_mode_t;
    
    #define PACKET_S1_FIELD_SIZE      (2UL)  /**< Packet S1 field size in bits. */
    #define PACKET_S0_FIELD_SIZE      (1UL)  /**< Packet S0 field size in bits. */
    #define PACKET_LENGTH_FIELD_SIZE  (6UL)  /**< Packet length field size in bits. */
    #define MAX_PDU_SIZE    (64UL)
    #define MY_SET_BIT(n)      (1UL << n)
    
    static uint8_t packet[256];
    
    static __inline int8_t ch2freq(uint8_t ch)
    {
      switch (ch) {
      case 37:
        return 2;
      case 38:
        return 26;
      case 39:
        return 80;
      default:
        if (ch > 39)
          return -1;
        else if (ch < 11)
          return 4 + (2 * ch);
        else
          return 6 + (2 * ch);
      }
    }
    
    void my_radio_configure(radio_mode_t rm, int channel)
    {
        // Radio config
        NRF_RADIO->TXPOWER     = (RADIO_TXPOWER_TXPOWER_Pos8dBm << RADIO_TXPOWER_TXPOWER_Pos);
        NRF_RADIO->DATAWHITEIV = channel & 0x3F;
        NRF_RADIO->FREQUENCY   = ch2freq(channel);
        NRF_RADIO->PREFIX0     = 0x0000008E;
        NRF_RADIO->BASE0       = 0x89BED600;
        NRF_RADIO->RXADDRESSES = 0x00000001;
    
        // Packet configuration
    #define m_packetHeaderPlen1MB RADIO_PCNF0_PLEN_8bit
    #define m_packetHeaderPlen2MB RADIO_PCNF0_PLEN_16bit
    #define m_packetHeaderPlenCODED RADIO_PCNF0_PLEN_LongRange
        switch(rm) {
        case BLE_1MB:
        	NRF_RADIO->MODE  = (RADIO_MODE_MODE_Ble_1Mbit << RADIO_MODE_MODE_Pos);
        	NRF_RADIO->PCNF0 = (m_packetHeaderS1len << RADIO_PCNF0_S1LEN_Pos) |
        	                   (m_packetHeaderS0len << RADIO_PCNF0_S0LEN_Pos) |
        	                   (m_packetHeaderLFlen << RADIO_PCNF0_LFLEN_Pos) |
        	                   (m_packetHeaderPlen1MB << RADIO_PCNF0_PLEN_Pos);
    	break;
    
        case BLE_2MB:
            NRF_RADIO->MODE  = (RADIO_MODE_MODE_Ble_2Mbit << RADIO_MODE_MODE_Pos);
        	NRF_RADIO->PCNF0 = (m_packetHeaderS1len << RADIO_PCNF0_S1LEN_Pos) |
        	                   (m_packetHeaderS0len << RADIO_PCNF0_S0LEN_Pos) |
        	                   (m_packetHeaderLFlen << RADIO_PCNF0_LFLEN_Pos) |
        	                   (m_packetHeaderPlen2MB << RADIO_PCNF0_PLEN_Pos);
    	break;
    
        case BLE_CODED_FAST:
        	NRF_RADIO->MODE  = (RADIO_MODE_MODE_Ble_LR500Kbit << RADIO_MODE_MODE_Pos);
        	NRF_RADIO->PCNF0 = (m_packetHeaderS1len << RADIO_PCNF0_S1LEN_Pos) |
        	                   (m_packetHeaderS0len << RADIO_PCNF0_S0LEN_Pos) |
        	                   (m_packetHeaderLFlen << RADIO_PCNF0_LFLEN_Pos) |
        	                   (3 << RADIO_PCNF0_TERMLEN_Pos) |
        	                   (2 << RADIO_PCNF0_CILEN_Pos) |
        	                   (m_packetHeaderPlenCODED << RADIO_PCNF0_PLEN_Pos);
    	break;
    
        case BLE_CODED_SLOW:
        	NRF_RADIO->MODE  = (RADIO_MODE_MODE_Ble_LR125Kbit << RADIO_MODE_MODE_Pos);
        	NRF_RADIO->PCNF0 = (m_packetHeaderS1len << RADIO_PCNF0_S1LEN_Pos) |
        	                   (m_packetHeaderS0len << RADIO_PCNF0_S0LEN_Pos) |
        	                   (m_packetHeaderLFlen << RADIO_PCNF0_LFLEN_Pos) |
        	                   (3 << RADIO_PCNF0_TERMLEN_Pos) |
        	                   (2 << RADIO_PCNF0_CILEN_Pos) |
        	                   (m_packetHeaderPlenCODED << RADIO_PCNF0_PLEN_Pos);
    	break;
        }
    
        // Packet configuration
        NRF_RADIO->PCNF1 = 3UL << RADIO_PCNF1_BALEN_Pos;
        NRF_RADIO->PCNF1 |= RADIO_PCNF1_WHITEEN_Enabled << RADIO_PCNF1_WHITEEN_Pos;
        NRF_RADIO->PCNF1 |= MAX_PDU_SIZE << RADIO_PCNF1_MAXLEN_Pos;
    
        // CRC Config
        NRF_RADIO->CRCCNF = RADIO_CRCCNF_LEN_Three << RADIO_CRCCNF_LEN_Pos |
                            RADIO_CRCCNF_SKIP_ADDR_Skip << RADIO_CRCCNF_SKIP_ADDR_Pos;
        NRF_RADIO->CRCINIT = 0x555555UL;
        NRF_RADIO->CRCPOLY = MY_SET_BIT(24) | MY_SET_BIT(10) | MY_SET_BIT(9) |
                             MY_SET_BIT(6) | MY_SET_BIT(4) | MY_SET_BIT(3) |
                             MY_SET_BIT(1) | MY_SET_BIT(0);
    }
    
    uint32_t read_packet()
    {
        uint32_t result = 0;
    
        NRF_RADIO->EVENTS_READY = 0U;
        // Enable radio and wait for ready
        NRF_RADIO->TASKS_RXEN = 1U;
    
        while (NRF_RADIO->EVENTS_READY == 0U)
        {
            // wait
        }
        NRF_RADIO->EVENTS_END = 0U;
        // Start listening and wait for address received event
        NRF_RADIO->TASKS_START = 1U;
    
        // Wait for end of packet or buttons state changed
        while (NRF_RADIO->EVENTS_END == 0U)
        {
            // wait
        }
    
        if (NRF_RADIO->CRCSTATUS == 1U)
        {
            result = 1;
        }
        NRF_RADIO->EVENTS_DISABLED = 0U;
        // Disable radio
        NRF_RADIO->TASKS_DISABLE = 1U;
    
        while (NRF_RADIO->EVENTS_DISABLED == 0U)
        {
            // wait
        }
        return result;
    }
    
    
    int main(void)
    {
        ret_code_t ret;
    
    /*
        if (USE_CYCCNT_TIMESTAMP_FOR_LOG)
        {
            CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
            DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
            DWT->CYCCNT = 0;
            APP_ERROR_CHECK(NRF_LOG_INIT(cyccnt_get, 64000000));
        }
        else
        {
            APP_ERROR_CHECK(NRF_LOG_INIT(app_timer_cnt_get));
        }
    */
    
    /*
        // DON'T DO THIS, pca10059 would not start
        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
        }
    
        ret = app_timer_init();
        APP_ERROR_CHECK(ret);
    
        ret = nrf_cli_init(&m_cli_cdc_acm, NULL, true, true, NRF_LOG_SEVERITY_INFO);
        APP_ERROR_CHECK(ret);
    
        usbd_init();
    
        ret = nrf_cli_start(&m_cli_cdc_acm);
        APP_ERROR_CHECK(ret);
    
        int channel = 37;
        my_radio_configure(BLE_1MB, channel);
    
        NRF_RADIO->PACKETPTR = (uint32_t) packet;
    
        int last_atts = 0;
    
        while (true)
        {
            UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
    #if CLI_OVER_USB_CDC_ACM && APP_USBD_CONFIG_EVENT_QUEUE_ENABLE
    berga
            while (app_usbd_event_queue_process())
            {
                /* Nothing to do */
            }
    #endif
    
            uint32_t received = read_packet();
            if(received == 0) continue;
    
    	// static char m_tx_buffer[NRF_DRV_USBD_EPSIZE];
    	//static int frame_counter = 0;
    
    	char m_tx_buffer[2000];
    	sprintf(m_tx_buffer, "[%d]:packet: ", cnt);
    	cnt++;
        int kk;
    
    	for(kk = 0; kk < 20; kk ++) {
    		sprintf(m_tx_buffer + strlen(m_tx_buffer), "%02X ", packet[kk]);
    	}
    
    	sprintf(m_tx_buffer + strlen(m_tx_buffer), "\r\n");
            size_t size = strlen(m_tx_buffer) + 1;
    
            last_atts = 0;
    
    	do{	last_atts ++;
    		ret = app_usbd_cdc_acm_write(&nrf_cli_cdc_acm, m_tx_buffer, size);
            	//        if(ret == NRF_SUCCESS) break;
    	}while(ret != NRF_SUCCESS);
    	//frame_counter ++;
            //nrf_delay_ms(1000);
            // nrf_cli_process(&m_cli_cdc_acm);
        }
    }

Children
No Data
Related