打开nRF5_SDK_17.0.2_d674dde目录下examples\peripheral\usbd_ble_uart\pca10056\s140\arm5_no_packs中的工程
#define TICK_1HZ_INTERVAL 32768
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));
}
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;
int main(void)
{
ret_code_t ret;
static const app_usbd_config_t usbd_config = {
.ev_state_proc = usbd_user_ev_handler
};
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();
advertising_start();
ret = app_usbd_power_events_enable();
APP_ERROR_CHECK(ret);
for (;;)
{
while (app_usbd_event_queue_process())
{
}
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\n",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();
}
}
- 在cdc_acm_user_ev_handler函数APP_USBD_CDC_ACM_USER_EVT_RX_DONE事件中读取USB串口数据
case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
{
ret_code_t ret;
uint8_t index = 1;
do
{
size_t size = app_usbd_cdc_acm_rx_size(p_cdc_acm);
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;
}
- 烧录程序及S140协议栈到NRF52840开发板,连接USB到PC端,win10会自动安装驱动,也可以手动安装nRF5_SDK_17.0.2_d674dde\examples\usb_drivers目录下的驱动,打开PC串口工具,选择115200波特率,及DTR选中,然后打开串口,串口工具会每秒收到NRF5840发来的TICK数据,PC发送数据,RTT VIEW会打印接收到的数据
android USB串口例程下载链接
USB串口安卓例程
- 下载后需要修改USB Vendor ID和Product ID
以下是NRF52840例程sdk_config.h中定义的USB Vendor ID和Product ID
#ifndef APP_USBD_VID
#define APP_USBD_VID 0x1915
#endif
#ifndef APP_USBD_PID
#define APP_USBD_PID 0x521A
#endif
- 打开下载后的安卓工程,修改CustomProber.java中的USB Vendor ID和Product ID
- 安装APP后连接到NRF52840开发板,点击USB Devices
- 确定允许设备
- 选择波特率及Control Lines选中,连接设备,每秒会收到NRF52840发来的TICK
- 输入12345678点击发送,NRF52840接收到数据通过RTT VIEW打印
完整NRF52840及安卓例程下载链接
点击下载
|