arilink 发表于 2022-6-19 13:33

先楫半导体 HPM6750测评 Step3 (RT-Thread + TinyUSB)

<p>&nbsp; &nbsp; 最近看到RT-Thread Studio有了大版本的更新(V4.1),并且增加了TinyUSB软件包支持,TinyUSB是我之前项目上一直使用的,所以本节使用RT-Thread Studio进行项目的创建</p>

<p>&nbsp; &nbsp; TinyUSB是用于嵌入式系统的开源跨平台USB主机/设备堆栈,旨在实现无动态分配的内存安全和延迟所有中断事件的线程安全,然后在非ISR任务功能中进行处理。TinyUSB支持的很多厂商的芯片比如ST、NXP、MicroChip等&nbsp; 乐鑫的ESP32 S系列也是支持使用TinyUSB进行USB开发。</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;TinyUSB的移植和操作都很简单 搭配上RT-Thread Studio更加简单。</p>

<p>&nbsp; &nbsp; 1.安装所需要的先楫SDK</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;2.在项目中添加TinyUSB软件包</p>

<p>3.在硬件配置中使能USB Device 设备</p>

<p>4.调整TinyUSB软件包配置</p>

<p>5.新建hid_test文件 写入测试hid通讯的代码</p>

<p>#include &lt;rtthread.h&gt;<br />
#include &lt;rtdevice.h&gt;<br />
#include &quot;rtt_board.h&quot;<br />
#include &lt;tusb.h&gt;</p>

<p>struct &nbsp;__attribute__((__packed__)) reportHID_t {<br />
&nbsp; &nbsp; &nbsp; &nbsp; //uint8_t reportId;<br />
&nbsp; &nbsp; &nbsp; &nbsp; int8_t X;<br />
&nbsp; &nbsp; &nbsp; &nbsp; int8_t Y;<br />
&nbsp; &nbsp; &nbsp; &nbsp; int8_t Z;<br />
&nbsp; &nbsp; &nbsp; &nbsp; int8_t Rz;<br />
&nbsp; &nbsp; &nbsp; &nbsp; int8_t Ry;<br />
&nbsp; &nbsp; &nbsp; &nbsp; int8_t Rx;<br />
&nbsp; &nbsp; &nbsp; &nbsp; uint8_t HatSwitch;<br />
&nbsp; &nbsp; &nbsp; &nbsp; uint32_t button;<br />
};</p>

<p>struct reportHID_t reportHID;<br />
static void hid_test_entry(void *parameter)<br />
{<br />
&nbsp; &nbsp; (void) parameter;<br />
&nbsp; &nbsp; static uint32_t send_failed_cnt;<br />
&nbsp; &nbsp; reportHID.X = 127;<br />
&nbsp; &nbsp; reportHID.Y = 127;<br />
&nbsp; &nbsp; reportHID.Z = -127;<br />
&nbsp; &nbsp; reportHID.Rx = 0;<br />
&nbsp; &nbsp; reportHID.Ry = 127;<br />
&nbsp; &nbsp; reportHID.Rz = 127;<br />
&nbsp; &nbsp; reportHID.button = 1;<br />
&nbsp; &nbsp; uint8_t res = false;<br />
&nbsp; &nbsp; while (1)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; if (tud_suspended())<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rt_kprintf(&quot;tud_suspended!!!\r\n&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tud_remote_wakeup();<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; else<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res = tud_hid_report(3,(uint8_t*)&amp;reportHID, 11);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(reportHID.button&lt;2147483648)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reportHID.button*=2;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reportHID.button = 1;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!res)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; send_failed_cnt++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rt_kprintf(&quot;Send Failed :%d Times\r\n&quot;,send_failed_cnt);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; rt_thread_mdelay(500);<br />
&nbsp; &nbsp; }<br />
}</p>

<p>static int init_hid_test(void)<br />
{<br />
&nbsp; &nbsp; rt_thread_t hid_test;</p>

<p>&nbsp; &nbsp; hid_test = rt_thread_create(&quot;hid test&quot;, hid_test_entry, RT_NULL,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PKG_TINYUSB_STACK_SIZE,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PKG_TINYUSB_THREAD_PRIORITY, 10);<br />
&nbsp; &nbsp; if (hid_test == RT_NULL)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; rt_kprintf(&quot;Fail to create HID Test thread&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; return -1;<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; rt_kprintf(&quot;init_hid_test create success!!!\r\n&quot;);<br />
&nbsp; &nbsp; rt_thread_startup(hid_test);<br />
&nbsp; &nbsp; return 0;<br />
}</p>

<p>MSH_CMD_EXPORT(init_hid_test, init hid test)</p>

<p>&nbsp;</p>

<p>6.编译 下载 测试</p>

<p></p>

<p>7.HID按照我们的程序逻辑在工作。 完成!</p>

Jacktang 发表于 2022-6-20 07:10

<p>TinyUSB的移植和操作看着是不复杂</p>

freebsder 发表于 2022-6-20 20:21

<p>rt支持蛮快的。</p>
页: [1]
查看完整版本: 先楫半导体 HPM6750测评 Step3 (RT-Thread + TinyUSB)