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 );

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

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;
};
}
  • 对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<<i);
    }

    inline bool isMuxIo( 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);
    }
  • 对IO模式的操作进行封装
    inline unsigned int getIoDir( const int& i )const{
        return m_dir&(0x0001<<i);
    }

    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;
    }
  • 对于作为DI的封装
    inline unsigned int getIoState( const int& i )const{
        return m_dat&(0x0001<<i);
    }

    inline bool isIoSet( const int& i )const{
        return getIoState(i)!=0;
    }

    inline bool isIoUnset( const int& i )const{
        return getIoState(i)==0;
    }
  • 对于DO进行封装
    inline void set( const int& i ){
        m_set |= (0x0001<<i);
    }

    inline void clear( const int& i ){
        m_clr |= (0x0001<<i);
    }

    inline void toggle( const int& i ){
        m_toggle |= (0x0001<<i);
    }

子类CGpa,CGpb等

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

namespace NF281x{

class CGpa:public CGpio{
    CGpa( const CGpa& );
public:
    CGpa();
};
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值