OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2717|回复: 7

C代码问题请教,结构体怎么能执行呢?

[复制链接]

63

主题

305

帖子

1

精华

高级会员

Rank: 4

积分
853
金钱
853
注册时间
2012-8-3
在线时间
79 小时
发表于 2017-8-15 00:17:07 | 显示全部楼层 |阅读模式
1金钱
本帖最后由 EDA3rd 于 2017-8-15 00:19 编辑

结构体怎么能执行呢?

    static USB_ClassInfo_HID_Host_t Keyboard_HID_Interface = {
        .Config = {
            .DataINPipeNumber       = 1,
            .DataINPipeDoubleBank   = false,

            .DataOUTPipeNumber      = 2,
            .DataOUTPipeDoubleBank  = false,

            .HIDInterfaceProtocol   = HID_CSCP_KeyboardBootProtocol,
            .PortNumber = 0,
        },
    };
    static inline void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
    static inline void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
    {
        (void)HIDInterfaceInfo;
    }

    int main(void)
    {
        SetupHardware();
        DEBUGOUT("Keyboard Host Demo running.\r\n");

        for (;; ) {
            KeyboardHost_Task();

            HID_Host_USBTask(&Keyboard_HID_Interface);
            USB_USBTask(Keyboard_HID_Interface.Config.PortNumber, USB_MODE_Host);
        }
    }

注意 HID_Host_USBTask(&Keyboard_HID_Interface); 这句代码,传递参数是结构体指针,执行 HID_Host_USBTask 就等于执行  (void) &Keyboard_HID_Interface; 不会跑飞吗?事实上板子运行没出现什么问题


最佳答案

查看完整内容[请看2#楼]

(void)HIDInterfaceInf; 相当于 int a; a; 实际上相当于NOP操作,既然是nop,自然就不会跑飞了,楼上说法确实也有道理,估计就是类似c++的虚函数,等用户去具体实现。
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

63

主题

305

帖子

1

精华

高级会员

Rank: 4

积分
853
金钱
853
注册时间
2012-8-3
在线时间
79 小时
 楼主| 发表于 2017-8-15 00:17:08 | 显示全部楼层
本帖最后由 EDA3rd 于 2019-5-9 10:27 编辑

(void)HIDInterfaceInf;   相当于  
int a;  
a;
实际上相当于NOP操作,既然是nop,自然就不会跑飞了,楼上说法确实也有道理,估计就是类似c++的虚函数,等用户去具体实现。



回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2017-8-15 00:49:25 | 显示全部楼层
函数指针吧?你仿真看看
回复

使用道具 举报

2

主题

685

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
3448
金钱
3448
注册时间
2017-7-4
在线时间
869 小时
发表于 2017-8-15 09:00:22 | 显示全部楼层
没看明白传递结构体指针怎么变成结构体执行了?不管什么指针,跟你平常用的都一样,传递一个地址而已。
回复

使用道具 举报

15

主题

317

帖子

0

精华

高级会员

Rank: 4

积分
863
金钱
863
注册时间
2015-2-12
在线时间
354 小时
发表于 2017-8-15 10:02:56 | 显示全部楼层
请把结构USB_ClassInfo_HID_Host_t成员贴出,它一定有个回调函数。
回复

使用道具 举报

63

主题

305

帖子

1

精华

高级会员

Rank: 4

积分
853
金钱
853
注册时间
2012-8-3
在线时间
79 小时
 楼主| 发表于 2017-8-16 11:33:53 | 显示全部楼层
本帖最后由 EDA3rd 于 2017-8-16 11:48 编辑
csmjmcc 发表于 2017-8-15 10:02
请把结构USB_ClassInfo_HID_Host_t成员贴出,它一定有个回调函数。

来了来了

[mw_shl_code=c,true]                        typedef struct
                        {
                                struct
                                {
                                        uint8_t  DataINPipeNumber; /**< Pipe number of the HID interface's IN data pipe. */
                                        bool     DataINPipeDoubleBank; /**< Indicates if the HID interface's IN data pipe should use double banking. */

                                        uint8_t  DataOUTPipeNumber; /**< Pipe number of the HID interface's OUT data pipe. */
                                        bool     DataOUTPipeDoubleBank; /**< Indicates if the HID interface's OUT data pipe should use double banking. */

                                        uint8_t  HIDInterfaceProtocol; /**< HID interface protocol value to match against if a specific
                                                                        *   boot subclass protocol is required, a protocol value from the
                                                                        *   @ref HID_Descriptor_ClassSubclassProtocol_t enum.
                                                                        */
                                        #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
                                        HID_ReportInfo_t* HIDParserData; /**< HID parser data to store the parsed HID report data, when boot protocol
                                                                          *   is not used.
                                                                                                          *
                                                                          *  @note When the \c HID_HOST_BOOT_PROTOCOL_ONLY compile time token is defined,
                                                                          *        this method is unavailable.
                                                                          */
                                        #endif

                                        uint8_t  PortNumber;                /**< Port number that this interface is running.
                                                                                                */
                                } Config; /**< Config data for the USB class interface within the device. All elements in this section
                                           *   <b>must</b> be set or the interface will fail to enumerate and operate correctly.
                                           */
                                struct
                                {
                                        bool IsActive; /**< Indicates if the current interface instance is connected to an attached device, valid
                                                        *   after @ref HID_Host_ConfigurePipes() is called and the Host state machine is in the
                                                        *   Configured state.
                                                        */
                                        uint8_t InterfaceNumber; /**< Interface index of the HID interface within the attached device. */

                                        uint16_t DataINPipeSize; /**< Size in bytes of the HID interface's IN data pipe. */
                                        uint16_t DataOUTPipeSize;  /**< Size in bytes of the HID interface's OUT data pipe. */

                                        bool SupportsBootProtocol; /**< Indicates if the current interface instance supports the HID Boot
                                                                    *   Protocol when enabled via @ref HID_Host_SetBootProtocol().
                                                                    */
                                        bool DeviceUsesOUTPipe; /**< Indicates if the current interface instance uses a separate OUT data pipe for
                                                                 *   OUT reports, or if OUT reports are sent via the control pipe instead.
                                                                 */
                                        bool UsingBootProtocol; /**< Indicates that the interface is currently initialized in Boot Protocol mode */
                                        uint16_t HIDReportSize; /**< Size in bytes of the HID report descriptor in the device. */

                                        uint8_t LargestReportSize; /**< Largest report the device will send, in bytes. */
                                } State; /**< State data for the USB class interface within the device. All elements in this section
                                                  *   <b>may</b> be set to initial values, but may also be ignored to default to sane values when
                                                  *   the interface is enumerated.
                                                  */
                        } USB_ClassInfo_HID_Host_t;[/mw_shl_code]
代码来自LPCOpen中 2.10 中的usbhost_keyboard.uvproj 例程
[url=http://www.nxp.com/products/developer-resources/software-development-tools/software-tools/lpcopen-libraries-and-examples/lpcopen-software-development-platform-lpc17xxPCOPEN-SOFTWARE-FOR-LPC17XX]http://www.nxp.com/downloads/en/software/lpcopen_2_10_keil_iar_ea_devkit_1788.zip[/url]
回复

使用道具 举报

15

主题

317

帖子

0

精华

高级会员

Rank: 4

积分
863
金钱
863
注册时间
2015-2-12
在线时间
354 小时
发表于 2017-8-16 16:50:38 | 显示全部楼层
对你的问题很感兴趣,特意下载了源程序,编译了下,结果我将其改成:
int main(void)
{
        char *pp = NULL;  //这里添加

        SetupHardware();

        DEBUGOUT("Keyboard Host Demo running.\r\n");

        for (;; ) {
                KeyboardHost_Task();

                //HID_Host_USBTask(&Keyboard_HID_Interface);
                (void)pp;                                                                        //这里添加
                USB_USBTask(Keyboard_HID_Interface.Config.PortNumber, USB_MODE_Host);
        }
}

1、无论屏蔽或不屏蔽HID_Host_USBTask(&Keyboard_HID_Interface);和 (void)pp; 编译后代码量均没有变化。
2、说明这内联函数HID_Host_USBTask(&Keyboard_HID_Interface);当前是无意义的。估计是提供一个接口,让用户添加。
3、我是菜鸟,仅是为了求知。呈送拙见!
回复

使用道具 举报

37

主题

596

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1574
金钱
1574
注册时间
2017-7-17
在线时间
308 小时
发表于 2019-5-8 17:48:21 | 显示全部楼层
不明白你提的问题。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-20 18:06

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表