[单片机][at32][填坑日记] [USB卡包] usb快速发包导致卡包
一、usb避免卡包全流程解释
- 上电初始化USB外设
#if BS_POWER_SWITCH
static uint8_t bat_soc_tick = 0;
if (get_sys_state() == SYS_STATE_POWER_ON)
{
main_send_signal(SIGNAL_SYS_POWER_ON);
}
#else
main_send_signal(SIGNAL_REFRESH_USB);
#endif
if (event.value.signals & SIGNAL_REFRESH_USB)
{
if (!get_usb_state())
{
biz_usb_queue_init();
#if BS_POWER_SWITCH
bat_soc_tick = 0;
#endif
}
else
{
os_delay(100);
usb_init();
bsp_gpio_set_pin(BOARD_USB_DP_PORT, BOARD_USB_DP_PIN, BOARD_USB_DP_PRESS_LEVEL);
if(get_sys_state() != SYS_STATE_POWER_ON)
{
main_send_signal(SIGNAL_SYS_POWER_ON);
}
#if BS_POWER_SWITCH
bat_soc_tick = 0;
#endif
}
if (!get_usb_state() && !get_usb_device_status())
{
LOG_D("<DEBUG> [USB] usb_deinit!!!\r\n");
bsp_gpio_set_pin(BOARD_USB_DP_PORT, BOARD_USB_DP_PIN, !BOARD_USB_DP_PRESS_LEVEL);
usb_deinit();
#if !BS_POWER_SWITCH
g_sys_run_times = 0;
#endif
}
- 延时800ms再发送USB数据(通道0无视)
- 每包数据需要有时间间隔
- main检测到空闲3ms-5ms,且USB_FIFO存在异常。则主动推送FIFO数据。(USB中断未能及时响应,则会造成FIFO数据无法发送)
static void idle_state_handle(void)
{
static uint16_t g_usb_send_delay_tick = 1;
static uint16_t g_free_tick = 1;
usb_state_changes_callback();
if(g_sys_run_times == 10 && get_usb_state() == false)
{
set_usb_state(true);
main_send_signal(SIGNAL_REFRESH_USB);
}
if (g_sys_run_times < 8)
{
return;
}
if (g_sys_run_times == 9)
{
biz_usb_queue_init();
g_sys_run_times ++;
g_key_trigger_flag = false;
g_combined_key_val = 0;
return;
}
g_free_tick++;
g_usb_send_delay_tick++;
if (g_free_tick % 10000 == 0 && g_free_tick != 0)
{
get_usb_fifo_state();
}
if (g_usb_send_delay_tick % 1600 == 0 && g_key_trigger_flag)
{
g_free_tick = 0;
g_key_trigger_flag = false;
biz_prot_hwic_usb_send_key_data(g_combined_key_val);
sys_time_reset();
}
if (g_usb_send_delay_tick % 2200 == 0)
{
if (get_usb_device_status())
{
if (biz_usb_data_dispose())
{
LOG_D("<DEBUG> [USB] send ok!!!\r\n");
g_free_tick = 0;
return;
}
}
if (g_free_tick > 3000 && flash_write_sys_cfg())
{
return;
}
if (g_free_tick > 50000)
{
sys_sleep();
}
}
}
文件目录 文件下载:https://download.csdn.net/download/qq_29246181/63248624
|