1651|0

3836

帖子

19

TA的资源

纯净的硅(中级)

楼主
 

DSP 2812: 使用C++封装GPIO [复制链接]

2812中的GPIO被分成很多组:GPA,GPB,GPD,GPE,GPF,GPG。
我们将组进行抽象。

组抽象:CGpio类
  • 使用命名空间对类进行包裹
namespace NF281x{class CGpio{    CGpio( const CGpio& );public:    CGpio( volatile unsigned int& mux,volatile unsigned int& dir,            volatile unsigned int& dat,volatile unsigned int& set,volatile unsigned int& clr,            volatile unsigned int& tog );
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

这里对拷贝构造进行了限制。
一下包含了相关寄存器的引用。定义这些寄存器,是为了在头文件中暴露出来,可以使用内联函数访问到。在一些需要快速操作的应用中,这样的设计是必要的。

protected:    volatile unsigned int& m_mux;    volatile unsigned int& m_dir;    volatile unsigned int& m_dat;    volatile unsigned int& m_set;    volatile unsigned int& m_clr;    volatile unsigned int& m_toggle;};}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 对MUX进行封装
    /**     * 获取该组GPIO第i个io口的MUX     * @param i 0~15     * @return     * - 0 作为IO     * - 1 作为外设     */    inline unsigned int getMux( const int& i )const{        return m_mux&(0x0001<const int& i )const{        return getMux(i)==0;    }    inline bool isMuxPeripheral( const int& i )const{        return getMux(i)!=0;    }    /**     * 设置GPIO作为IO     * @param i     */    void setMuxIo( const int& i );    /**     * 设置GPIO为外设模式     * @param i     */    void setMuxPeripheral( const int& i );    /**     * 设置GPIO的MUX     * @param i     * @param mux     * - 0 IO模式     * - 1 外设模式     */    inline void setMux( const int& i,const unsigned int& mux ){        if( mux==0 )            setMuxIo(i);        else            setMuxPeripheral(i);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 对IO模式的操作进行封装
    inline unsigned int getIoDir( const int& i )const{        return m_dir&(0x0001<inline void setIoDir( const int& i,const unsigned int& dir ){        if( dir==0 )            setIoIn(i);        else            setIoOut(i);    }    void setIoIn( const int& i );    void setIoOut( const int& i );    inline bool isIoIn( const int& i )const{        return getIoDir(i)==0;    }    inline bool isIoOut( const int& i )const{        return getIoDir(i)!=0;    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 对于作为DI的封装
    inline unsigned int getIoState( const int& i )const{        return m_dat&(0x0001<inline bool isIoSet( const int& i )const{        return getIoState(i)!=0;    }    inline bool isIoUnset( const int& i )const{        return getIoState(i)==0;    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 对于DO进行封装
    inline void set( const int& i ){        m_set |= (0x0001<inline void clear( const int& i ){        m_clr |= (0x0001<inline void toggle( const int& i ){        m_toggle |= (0x0001<
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    子类CGpa,CGpb等

    - 这里举一个例子,CGpa的类设计

    namespace NF281x{class CGpa:public CGpio{    CGpa( const CGpa& );public:    CGpa();};}
  • 点赞 关注
     

    回复
    举报
    您需要登录后才可以回帖 登录 | 注册

    随便看看
    查找数据手册?

    EEWorld Datasheet 技术支持

    相关文章 更多>>
    关闭
    站长推荐上一条 2/10 下一条
    有奖直播 | TI 助力机器人电机控制系统设计
    直播时间:2月27日(周四)上午10:00
    活动奖励:家具壶、保温杯、充电线

    查看 »

     
    EEWorld订阅号

     
    EEWorld服务号

     
    汽车开发圈

    About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

    站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

    北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

    电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
    快速回复 返回顶部 返回列表