TA的每日心情 | 奋斗 14 小时前 |
---|
签到天数: 1970 天 连续签到: 26 天 [LV.Master]伴坛终老
|
本次测试一下USB设备。
导入通用驱动项目后,选择USB_Demo。
然后打开app_config.h配置一下。
main.c如下
- int main (void)
- {
- #if(MCU_CORE_B91)
- sys_init(LDO_1P4_LDO_1P8, VBAT_MAX_VALUE_GREATER_THAN_3V6);
- //Note: This function can improve the performance of some modules, which is described in the function comments.
- //Called immediately after sys_init, set in other positions, some calibration values may not take effect.
- user_read_flash_value_calib();
- CCLK_24M_HCLK_24M_PCLK_24M;
- #elif(MCU_CORE_B92)
- sys_init();
- write_reg8(0x1401fb, 192/48);
- #endif
- user_init();
- while (1)
- {
- main_loop ();
- }
- return 0;
- }
复制代码 mouse_app.c初始化和主循环代码如下。修改了一下原来的例子,利用板上4个按键模拟鼠标X,Y轴。
- /********************************************************************************************************
- * @file mouse_app.c
- *
- * @brief This is the source file for B91m
- *
- * @author Driver Group
- * @date 2019
- *
- * @par Copyright (c) 2019, Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
- * All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- *******************************************************************************************************/
- #include "app_config.h"
- #if(USB_DEMO_TYPE==USB_MOUSE)
- #include "application/usbstd/usb.h"
- #include "application/usb_app/usbmouse.h"
- char mouse[4];
- volatile unsigned long g_led_time;
- volatile unsigned long g_key_time;
- void user_init(void)
- {
- //1.enable USB DP pull up 1.5k
- usb_set_pin_en();
- //2.enable USB manual interrupt(in auto interrupt mode,USB device would be USB printer device)
- usb_init_interrupt();
- //3.enable global interrupt
- core_interrupt_enable();
- #if(MCU_CORE_B91)
- usbhw_set_irq_mask(USB_IRQ_RESET_MASK|USB_IRQ_SUSPEND_MASK);
- #endif
- //initiate LED for indication
- gpio_function_en(LED1|LED2|LED3|LED4);
- gpio_input_dis(LED1|LED2|LED3|LED4);
- gpio_output_en(LED1|LED2|LED3|LED4);
- gpio_set_high_level(LED1|LED2|LED3|LED4);
- delay_us(100000);
- gpio_set_low_level(LED1|LED2|LED3|LED4);
- //initiate Button for Mouse input
- gpio_function_en(GPIO_PC0);
- gpio_input_en(GPIO_PC0);
- gpio_output_dis(GPIO_PC0);
- gpio_set_up_down_res(GPIO_PC0, GPIO_PIN_PULLUP_10K);
- gpio_function_en(GPIO_PC2);
- gpio_input_en(GPIO_PC2);
- gpio_output_dis(GPIO_PC2);
- gpio_set_up_down_res(GPIO_PC2, GPIO_PIN_PULLUP_10K);
- //output
- gpio_function_en(GPIO_PC1);
- gpio_input_dis(GPIO_PC1);
- gpio_output_en(GPIO_PC1);
- gpio_set_high_level(GPIO_PC1);
- gpio_function_en(GPIO_PC3);
- gpio_input_dis(GPIO_PC3);
- gpio_output_en(GPIO_PC3);
- gpio_set_low_level(GPIO_PC3);
- g_led_time = stimer_get_tick();
- g_key_time = stimer_get_tick();
- }
- /* enum to USB input device and simulate the left click and right click of mouse */
- void main_loop (void)
- {
- usb_handle_irq();
- if(clock_time_exceed(g_led_time, 500*1000))
- {
- g_led_time = stimer_get_tick();
- gpio_toggle(LED2); //BLINK LED
- }
- if(clock_time_exceed(g_key_time, 30*1000))
- {
- g_key_time = stimer_get_tick();
- // gpio_toggle(LED3); //BLINK LED
- gpio_set_high_level(GPIO_PC1);
- gpio_set_low_level(GPIO_PC3);
- delay_ms(2);
- if(gpio_get_level(GPIO_PC0)==0)
- {
- gpio_set_high_level(LED1);
- //printf("Key:Mouse Click ! \r\n");
- mouse[0] = 0;// BIT(0) - left key; BIT(1) - right key; BIT(2) - middle key; BIT(3) - side key; BIT(4) - external key
- mouse[1] = -5; // Displacement relative to x coordinate
- mouse[2] = 0; // Displacement relative to y coordinate
- mouse[3] = 0; // Displacement relative to the roller
- if(g_usb_config != 0 ) usbmouse_hid_report(USB_HID_MOUSE,(unsigned char*)mouse,4);
- }
- if(gpio_get_level(GPIO_PC2)==0)
- {
- gpio_set_low_level(LED1);
- //printf("Key:Mouse Click ! \r\n");
- mouse[0] = 0;// BIT(0) - left key; BIT(1) - right key; BIT(2) - middle key; BIT(3) - side key; BIT(4) - external key
- mouse[1] = 5; // Displacement relative to x coordinate
- mouse[2] = 0; // Displacement relative to y coordinate
- mouse[3] = 0; // Displacement relative to the roller
- if(g_usb_config != 0 ) usbmouse_hid_report(USB_HID_MOUSE,(unsigned char*)mouse,4);
- }
- gpio_set_high_level(GPIO_PC3);
- gpio_set_low_level(GPIO_PC1);
- delay_ms(2);
- if(gpio_get_level(GPIO_PC0)==0)
- {
- gpio_set_high_level(LED4);
- //printf("Key:Mouse Click ! \r\n");
- mouse[0] = 0;// BIT(0) - left key; BIT(1) - right key; BIT(2) - middle key; BIT(3) - side key; BIT(4) - external key
- mouse[1] = 0; // Displacement relative to x coordinate
- mouse[2] = 5; // Displacement relative to y coordinate
- mouse[3] = 0; // Displacement relative to the roller
- if(g_usb_config != 0 ) usbmouse_hid_report(USB_HID_MOUSE,(unsigned char*)mouse,4);
- }
- if(gpio_get_level(GPIO_PC2)==0)
- {
- gpio_set_low_level(LED4);
- //printf("Key:Mouse Click ! \r\n");
- mouse[0] = 0;// BIT(0) - left key; BIT(1) - right key; BIT(2) - middle key; BIT(3) - side key; BIT(4) - external key
- mouse[1] = 0; // Displacement relative to x coordinate
- mouse[2] = -5; // Displacement relative to y coordinate
- mouse[3] = 0; // Displacement relative to the roller
- if(g_usb_config != 0 ) usbmouse_hid_report(USB_HID_MOUSE,(unsigned char*)mouse,4);
- }
- }
- }
- #endif
复制代码
板上4个按键是矩阵扫描方式的。见如下电路图。使用io是PC0,PC1,PC2,PC3.
编译下载
打开BDT下载软件,找到编译的固件资料。然后点Download下载。
复位板子,就可以识别到鼠标设备了。
可以通过板上4个按键,可以看到鼠标的指针移动了。
|
|