|
楼主 |
发表于 2014-12-24 10:23:47
|
显示全部楼层
本帖最后由 Gorgon_Meducer 于 2014-12-24 10:25 编辑
你可以理解为下面的代码是我定义的usart接口,并根据芯片具体usart的数量实现了USART_COUNT个静态实例。
这个接口是抽象出来的,和具体的芯片无关,上层可以依赖这个接口开发,而不需要太担心库移植的问题。
其实这是用OO概念写了,用C++实现起来其实更方便。使用方法上也是很方便的,比如给你看个例子:
- IO_CFG(
- //! configure PA20 as USART0 txd
- {PA20, IO_WORKS_AS_USART1_TXD},
- //! configure PA21 as usart0 RXD with bulid-in pull-up resistor enabled
- {PA21, IO_WORKS_AS_USART1_RXD, IO_PULL_UP},
- );
- //! configure USART1
- USART_CFG(
- USART1,
- //! baudrate configuration
- //USART_NO_AUTO_BAUD |
- USART_AUTO_BAUD_MODE0 | //!< enable auto baud rate mode
- USART_AUTO_RESTART | //!< auto restart auto baud detect if failed
-
- //! parity configuration
- USART_NO_PARITY | //!< no parity (default)
- //USART_EVEN_PARITY |
- //USART_ODD_PARITY |
-
- USART_1_STOPBIT | //!< 1 stop bit (default)
- //USART_2_STOPBIT |
- USART_ENABLE_FIFO | //!< enable build-in FIFO (default)
- //USART_DISABLE_FIFO |
- USART_8_BIT_LENGTH | //!< 8 bit length
- //USART_7_BIT_LENGTH |
- //USART_6_BIT_LENGTH |
- //USART_5_BIT_LENGTH |
- USART_ASYNC_MODE | //!< UART
- //USART_SYNC_MODE |
- 0,
- //9600 //!< baudrate (no use if autobaud mode is enabled)
- );
- //! Enable USART1
- USART1.Enable();
- ...
- while(1) {
- //! software loop-back demo
- if (USART1.ReadByte(&chByte)) {
- while(!USART1.WriteByte(chByte));
- }
- ...
- }
复制代码
- #ifndef __USART1_H__
- #define __USART1_H__
- /*============================ INCLUDES ======================================*/
- #include ".\app_cfg.h"
- #include ".\i_io_usart.h"
- #include "..\device.h"
- /*============================ MACROS ========================================*/
- #define USART_CFG(__USART, ...) \
- do { \
- usart_cfg_t tCFG = {__VA_ARGS__}; \
- __USART.Init(&tCFG); \
- } while(0)
- /*============================ MACROFIED FUNCTIONS ===========================*/
- /*============================ TYPES =========================================*/
- //! \name usart working mode
- //! @{
- typedef enum {
- USART_NO_AUTO_BAUD = 0x00,
- USART_AUTO_BAUD_MODE0 = 0x01,
- USART_AUTO_BAUD_MODE1 = 0x03,
- USART_AUTO_RESTART = 0x04,
- USART_NO_AUTO_RESTART = 0x00,
- USART_NO_PARITY = 0x00,
- USART_EVEN_PARITY = 0x18,
- USART_ODD_PARITY = 0x08,
- USART_FORCE_1_PARITY = 0x28,
- USART_FORCE_0_PARITY = 0x38,
- USART_1_STOPBIT = 0x00,
- USART_2_STOPBIT = 0x40,
- USART_ENABLE_FIFO = 0x00,
- USART_DISABLE_FIFO = 0x80,
- USART_5_BIT_LENGTH = 0x0000,
- USART_6_BIT_LENGTH = 0x0100,
- USART_7_BIT_LENGTH = 0x0200,
- USART_8_BIT_LENGTH = 0x0300,
-
- USART_SYNC_MODE = 0x0400,
- USART_ASYNC_MODE = 0x0000
- }em_usart_mode_t;
- //! @}
- //! \name usart configuration
- //! @{
- typedef struct {
- uint16_t hwMode;
- uint32_t wBaudrate;
- } usart_cfg_t;
- //! @}
- //! \name usart_irda_mode_t
- //! @{
- typedef enum {
- //! \name IRDA EN
- //! @{
- USART_IRDA_DISABLE = 0x00, //!< disable irda
- USART_IRDA_ENABLE = 0x01, //!< enable irda
- //! @}
-
- //! \name Inverted set
- //! @{
- USART_IRDA_NOT_INVERTED = 0x00, //!< not inverted the input
- USART_IRDA_INVERTED = 0x02, //!< inverted the input
- //! @}
-
- //! \name Fix pulse en
- //! @{
- USART_IRDA_FIXPULSE_DISABLE = 0x00, //!< disable fixpulse
- USART_IRDA_FIXPULSE_ENABLE = 0x04, //!< enable fixpulse
- //! @}
-
- //! \name Pluse Div Set
- //! @{
- USART_IRDA_PULSEDIV_2 = 0x00, //!< 2* T_pclk(fixpulse enable)
- USART_IRDA_PULSEDIV_4 = 0x08, //!< 4* T_pclk(fixpulse enable)
- USART_IRDA_PULSEDIV_8 = 0x10, //!< 8* T_pclk(fixpulse enable)
- USART_IRDA_PULSEDIV_16 = 0x18, //!< 16* T_pclk(fixpulse enable)
- USART_IRDA_PULSEDIV_32 = 0x20, //!< 32* T_pclk(fixpulse enable)
- USART_IRDA_PULSEDIV_64 = 0x28, //!< 64* T_pclk(fixpulse enable)
- USART_IRDA_PULSEDIV_128 = 0x30, //!< 128* T_pclk(fixpulse enable)
- USART_IRDA_PULSEDIV_256 = 0x38 //!< 256* T_pclk(fixpulse enable)
- //! @}
- } em_usart_irda_mode_t;
- //! @}
- //! \name usart_IrDA_cfg_t
- //! @{
- typedef struct {
- uint16_t hwIrDAMode; //!< IrDA config word
- } usart_IrDA_cfg_t;
- //! @}
-
- //! \name smart card working mode
- //! @{
- typedef enum {
- SMART_CARD_DISABLE =0x00,
- SMART_CARD_ENABLE =0x01,
- SMART_CARD_NACK_DISABLE =(1<<1),
- SMART_CARD_NACK_ENABLE =0x00,
- SMART_CARD_PROT_T0 =0x00,
- SMART_CARD_PROT_T1 =(1<<2),
- SMART_CARD_TXRETRY_DISABLE =0x00,
- SMART_CARD_TXRETRY_1 =(1<<5),
- SMART_CARD_TXRETRY_2 =(2<<5),
- SMART_CARD_TXRETRY_3 =(3<<5),
- SMART_CARD_TXRETRY_4 =(4<<5),
- SMART_CARD_TXRETRY_5 =(5<<5),
- SMART_CARD_TXRETRY_6 =(6<<5),
- SMART_CARD_TXRETRY_7 =(7<<5),
- }em_smartcard_mode_t;
- //! @}
- //! \name smart card config
- //! @{
- typedef struct {
- uint8_t chMode;
- uint8_t chGuardTime;
- }usart_smartcard_cfg_t;
- //! @}
- //! \name class: usart_irda_t
- //! @{
- DEF_INTERFACE(usart_smartcard_t)
- bool (*Init)(usart_smartcard_cfg_t *ptCFG);
- END_DEF_INTERFACE(usart_smartcard_t)
- //! @}
- //! \name usart 485 working mode
- //! @{
- typedef enum {
- USART_485_DISABLE = 0x00,
- USART_485_MULTI_STATION = 0x01,
- USART_485_DISABLE_RX = 0x02,
- USART_485_AUTO_ADDRESS_MATCH = 0x04,
- USART_485_USE_DTR_AS_DIR_CTRL = 0x08,
- USART_485_ENABLE_AUTO_DIR_CTRL = 0x10,
- USART_485_DIR_PIN_IDLE_LOW = 0x20
- }em_usart_485_mode_t;
- //! @}
- //! \name usart 485 configuration
- //! @{
- typedef struct {
- uint8_t chMode;
- uint8_t chAddress;
- uint8_t chDelay;
- } usart_485_cfg_t;
- //! @}
- //! \name usart sync working mode
- //! @{
- typedef enum {
- //! \name SYNC SCLK
- //! @{
- USART_SYNC_SCLK_IN = 0x00, //!< Synchronous slave mode (SCLK in)
- USART_SYNC_SCLK_OUT = 0x02, //!< Synchronous master mode (SCLK out)
- //! @}
- //! \name SYNC SAMP
- //! @{
- USART_SYNC_FALLING_EDGE_SAMP = 0x04, //!< RxD is sampled on the falling edge of SCLK
- USART_SYNC_RISING_EDGE_SAMP = 0x00, //!< RxD is sampled on the rising edge of SCLK
- //! @}
- //! \name SYNC TSBYPASS
- //! @{
- USART_SYNC_TSBYPASS_DISABLE = 0x00, //!< Transmit synchronization bypass in slave mode disable
- USART_SYNC_TSBYPASS_ENABLE = 0x08, //!< Transmit synchronization bypass in slave mode enable
- //! @}
- //! \name SYNC CSCEN
- //! @{
- USART_SYNC_CSCEN_DISABLE = 0x00, //!< Continuous master clock disable
- USART_SYNC_CSCEN_ENABLE = 0x10, //!< Continuous master clock enable
- //! @}
- //! \name SYNC SSSDIS
- //! @{
- USART_SYNC_SEND_START_STOP_DISABLE = 0x20, //!< Do not send start/stop bits
- USART_SYNC_SEND_START_STOP_ENABLE = 0x00, //!< Send start and stop bits as in other modes.
- //! @}
- //! \name SYNC CCCLR
- //! @{
- USART_SYNC_CCCLR_DISABLE = 0x00, //!< CSCEN is under software control
- USART_SYNC_CCCLR_ENABLE = 0x40, //!< Hardware clears CSCEN after each character is received
- //! @}
- }em_usart_sync_mode_t;
- //! @}
- //! \name usart sync configuration
- //! @{
- typedef struct {
- uint8_t chMode;
- } usart_sync_cfg_t;
- //! @}
- //! \name class: usart_irda_t
- //! @{
- DEF_INTERFACE(usart_irda_t)
- bool (*Init)(usart_IrDA_cfg_t *ptCFG);
- END_DEF_INTERFACE(usart_irda_t)
- //! @}
- //! \name class: usart_485_t
- //! @{
- DEF_INTERFACE(usart_485_t)
- bool (*Init)(usart_485_cfg_t *ptCFG);
- fsm_rt_t (*SendAddress)(uint8_t chAddress);
- END_DEF_INTERFACE(usart_485_t)
- //! @}
- //! \name class: usart_sync_t
- //! @{
- DEF_INTERFACE(usart_sync_t)
- bool (*Init)(usart_sync_cfg_t *ptCFG);
- END_DEF_INTERFACE(usart_sync_t)
- //! @}
- //! \name class: usart_t
- //! @{
- DEF_INTERFACE(usart_t)
- bool (*Init)(usart_cfg_t *ptCFG);
- bool (*Idle)(void);
- bool (*Enable)(void);
- bool (*Disable)(void);
- bool (*WriteByte)(uint8_t chByte);
- bool (*ReadByte)(uint8_t *pchByte);
- struct {
- dma_tsf_info_t (*Output)(void);
- dma_tsf_info_t (*Input)(void);
- } DMAInfo;
-
- u32_property_t Baudrate;
- usart_485_t RS485;
- usart_irda_t IrDA;
- usart_smartcard_t SmartCard;
- usart_sync_t SYNC;
- END_DEF_INTERFACE(usart_t)
- //! @}
- /*============================ GLOBAL VARIABLES ==============================*/
- extern const usart_t USART[USART_COUNT];
- /*============================ LOCAL VARIABLES ===============================*/
- /*============================ PROTOTYPES ====================================*/
- #endif
复制代码
希望通过这个能引导你从面向接口开发的方面思考问题。 |
|