NRF52840 USB串口例程

打开nRF5_SDK_17.0.2_d674dde目录下examplesperipheralusbd_ble_uartpca10056s140arm5_no_packs中的工程

#define TICK_1HZ_INTERVAL     32768                                  /*1HZ 定时器 */
APP_TIMER_DEF(m_1hz_id); 

static nrf_atomic_u32_t m_1hz_evt;
static void tick_1hz_timeout_handler(void * p_context)
{
    UNUSED_PARAMETER(p_context);
	  UNUSED_RETURN_VALUE(nrf_atomic_u32_or(&m_1hz_evt, 1));
}


/** @brief Function for initializing the timer module. */
static void timers_init(void)
{
    ret_code_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);
    err_code = app_timer_create(&m_1hz_id,
                                APP_TIMER_MODE_REPEATED,
                                tick_1hz_timeout_handler);
	 APP_ERROR_CHECK(err_code);
	
   err_code = app_timer_start(m_1hz_id, TICK_1HZ_INTERVAL, NULL);
   APP_ERROR_CHECK(err_code);
}

-

 - 在main主循环中读取1hz事件,并发送串口数据

```c
static uint32_t tick_cnt;
/** @brief Application main function. */
int main(void)
{
    ret_code_t ret;
    static const app_usbd_config_t usbd_config = {
        .ev_state_proc = usbd_user_ev_handler
    };
    // Initialize.
    log_init();
    timers_init();

 

    app_usbd_serial_num_generate();

    ret = nrf_drv_clock_init();
    APP_ERROR_CHECK(ret);

    NRF_LOG_INFO("USBD BLE UART example started.");

    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(&m_app_cdc_acm);
    ret = app_usbd_class_append(class_cdc_acm);
    APP_ERROR_CHECK(ret);

    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();

    // Start execution.
    advertising_start();

    ret = app_usbd_power_events_enable();
    APP_ERROR_CHECK(ret);

    // Enter main loop.
    for (;;)
    {
        while (app_usbd_event_queue_process())
        {
            /* Nothing to do */
        }
		 uint32_t events =  nrf_atomic_u32_fetch_store(&m_1hz_evt, 0);
        if (events)
        {				
					if(m_uart_connected)
					{
						tick_cnt++;
						char buf[32];
						
						 uint32_t length = sprintf(buf,"Tick %d
",tick_cnt);
						 memcpy(m_nus_data_array,buf,length);
						
						  ret_code_t ret = app_usbd_cdc_acm_write(&m_app_cdc_acm,
                                                m_nus_data_array,
                                                length);
						 NRF_LOG_INFO("tick_cnt %d",tick_cnt);
							if(ret != NRF_SUCCESS)
							{
									NRF_LOG_INFO("CDC ACM unavailable, data received: %s", m_nus_data_array);
							}
					}
        }
        idle_state_handle();
    }
}
 case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
        {
            ret_code_t ret;
            uint8_t index = 1;
          
            do
            {
                

                /*Get amount of data transferred*/
                size_t size = app_usbd_cdc_acm_rx_size(p_cdc_acm);

                /* Fetch data until internal buffer is empty */
                ret = app_usbd_cdc_acm_read(&m_app_cdc_acm,
                                            &m_cdc_data_array[index],
                                            1);
                if (ret == NRF_SUCCESS)
                {
                    index++;
                }
            }
            while (ret == NRF_SUCCESS);
						
						NRF_LOG_HEXDUMP_INFO(m_cdc_data_array,index);
				
            break;
        }

android USB串口例程下载链接

USB串口安卓例程

//  APP_USBD_VID - Vendor ID.

//  Note: This value is not editable in Configuration Wizard.
//  Vendor ID ordered from USB IF: http://www.usb.org/developers/vendor/
#ifndef APP_USBD_VID
#define APP_USBD_VID 0x1915
#endif

//  APP_USBD_PID - Product ID.

//  Note: This value is not editable in Configuration Wizard.
//  Selected Product ID
#ifndef APP_USBD_PID
#define APP_USBD_PID 0x521A
#endif

完整NRF52840及安卓例程下载链接

点击下载

页面更新:2024-04-15

标签:串口   例程   波特率   定时器   函数   定义   工具   数据   工程   设备

1 2 3 4 5

上滑加载更多 ↓
Top