⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serial.c

📁 STM3240G-Eval_uCOS-III
💻 C
📖 第 1 页 / 共 5 页
字号:
/*
*********************************************************************************************************
*                                     SERIAL (BYTE) COMMUNICATION
*
*                         (c) Copyright 2007-2009; Micrium, Inc.; Weston, FL
*
*               All rights reserved.  Protected by international copyright laws.
*               Knowledge of the source code may NOT be used to develop a similar product.
*               Please help us continue to provide the Embedded community with the finest
*               software available.  Your honesty is greatly appreciated.
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*
*                                     SERIAL (BYTE) COMMUNICATION
*
* Filename      : serial.c
* Version       : V2.00
* Programmer(s) : FGK
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                            INCLUDE FILES
*********************************************************************************************************
*/

#define    SERIAL_MODULE
#include  <serial.h>
#include  <serial_os.h>


/*
*********************************************************************************************************
*                                            LOCAL DEFINES
*********************************************************************************************************
*/

#define  SERIAL_RD_STATE_CLOSED                            0u
#define  SERIAL_RD_STATE_OPENED                            1u
#define  SERIAL_RD_STATE_RD                                2u
#define  SERIAL_RD_STATE_CHK                               3u

#define  SERIAL_WR_STATE_CLOSED                            0u
#define  SERIAL_WR_STATE_OPENED                            1u
#define  SERIAL_WR_STATE_WR                                2u
#define  SERIAL_WR_STATE_WR_BUF                            3u


/*
*********************************************************************************************************
*                                                FLAGS
*********************************************************************************************************
*/

#define  SERIAL_STATE_NONE                                 0u
#define  SERIAL_STATE_CLOSED                               1u
#define  SERIAL_STATE_CLOSING                              2u
#define  SERIAL_STATE_OPENED                               3u


/*
*********************************************************************************************************
*                                        FORWARD DECLARATIONS
*********************************************************************************************************
*/

typedef  struct  serial_buf_desc  SERIAL_BUF_DESC;


/*
*********************************************************************************************************
*                                      SERIAL CALLBACK INFO TYPE
*********************************************************************************************************
*/

typedef  struct  serial_callback_info {                         /* ---------------- RD/WR CALLBACK INFO --------------- */
    void        *SignalPtr;                                     /* Ptr to signal to wait.                               */
    CPU_INT32U   Timeout;                                       /* Timeout in milliseconds. Wait indefinitely, if 0.    */
    CPU_SIZE_T   Len;                                           /* Nbr of octets successfully read/written.             */
    SERIAL_ERR   Err;                                           /* Rtn err code.                                        */
} SERIAL_CALLBACK_INFO;


/*
*********************************************************************************************************
*                                    SERIAL BUFFER DESCRIPTOR TYPE
*********************************************************************************************************
*/

struct  serial_buf_desc {
    CPU_SIZE_T             Len;                                 /* Buf len.                                             */
    CPU_INT08U            *DataPtr;                             /* Ptr to buf data.                                     */

    SERIAL_CALLBACK_FNCT  *Callback;                            /* Rd/Wr complete callback.                             */
    void                  *CallbackArg;                         /* Rd/Wr complete context.                              */
    void                  *CallbackBuf;                         /* Rd/Wr complete buffer pointer.                       */

    SERIAL_BUF_DESC       *NextPtr;
    SERIAL_BUF_DESC       *PrevPtr;
};


/*
*********************************************************************************************************
*                                        SERIAL INTERFACE TYPE
*
* Note(s) : (1) Serial interface initialization flag set when an interface has been successfully added
*               & initialized to the interface table.  Once set, this flag is never cleared since the
*               removal of interfaces is currently not allowed.
*********************************************************************************************************
*/

typedef  struct  serial_if {
    CPU_BOOLEAN            Init;                                /* IF init status (see Note #1).                        */

    SERIAL_IF_NBR          Nbr;                                 /* IF nbr.                                              */
    CPU_INT08U             State;                               /* IF state.                                            */

    CPU_CHAR              *NamePtr;                             /* Ptr to dev name (cfg'd @ dev add).                   */

    SERIAL_DEV             Dev;                                 /* Dev             (cfg'd @ dev add).                   */

#if (SERIAL_CFG_TX_DESC_NBR > 0)
    MEM_POOL               TxBufPool;
    SERIAL_BUF_DESC       *TxBufListStart;
    SERIAL_BUF_DESC       *TxBufListEnd;
#endif

                                                                /* --------------- RD COMMUNICATION DATA -------------- */
    CPU_INT08U             RdState;                             /* Rd state.                                            */
    void                  *RdSem;                               /* Rd semaphore.                                        */
    SERIAL_CALLBACK_FNCT  *RdCallback;                          /* Rd complete callback.                                */
    void                  *RdCallbackArg;                       /* Rd complete context.                                 */
    void                  *RdCallbackBuf;                       /* Rd complete buffer pointer.                          */
#if (SERIAL_CFG_RD_BUF_EN == DEF_ENABLED)
    CPU_SIZE_T             RdCallbackLen;                       /* Rd complete buffer len.                              */
#endif
    SERIAL_BUF             RdUserBuf;                           /* Rd user buf.                                         */



                                                                /* --------------- WR COMMUNICATION DATA -------------- */
    CPU_INT08U             WrState;                             /* Wr state.                                            */
    void                  *WrSem;                               /* Wr semaphore.                                        */
    SERIAL_CALLBACK_FNCT  *WrCallback;                          /* Wr complete callback.                                */
    void                  *WrCallbackArg;                       /* Wr complete context.                                 */
    void                  *WrCallbackBuf;                       /* Wr complete buffer pointer.                          */
    SERIAL_BUF             WrUserBuf;                           /* Wr user buf.                                         */



#if (SERIAL_CFG_RD_BUF_EN == DEF_ENABLED)                       /* -------------------- RD BUFFER --------------------- */
    CPU_BOOLEAN            RdBufEn;                             /* Indicates buf presence.                              */
    SERIAL_ERR             RdBufErr;                            /* Rd err (while rd'ing into rd buf).                   */
    SERIAL_BUF             RdBuf;                               /* Buf.                                                 */
#endif

#if (SERIAL_CFG_WR_BUF_EN == DEF_ENABLED)                       /* -------------------- WR BUFFER --------------------- */
    CPU_BOOLEAN            WrBufEn;                             /* Indicates buf presence.                              */
    SERIAL_BUF             WrBuf;                               /* Buf.                                                 */
#endif
} SERIAL_IF;


/*
*********************************************************************************************************
*                                            LOCAL TABLES
*********************************************************************************************************
*/

static  SERIAL_IF  Serial_IF_Tbl[SERIAL_IF_NBR_TOT];

/*
*********************************************************************************************************
*                                       LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/

static  SERIAL_IF_NBR  SerialIF_NbrNext;


/*
*********************************************************************************************************
*                                      LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
                                                                /* Clr if.                                              */
static  void              Serial_IF_Clr      (SERIAL_IF             *pif);
                                                                /* Clr rd callback arg and user buf.                    */
static  void              Serial_IF_RdClr    (SERIAL_IF             *pif);
                                                                /* Clr wr callback arg and user buf.                    */
static  void              Serial_IF_WrClr    (SERIAL_IF             *pif);
                                                                /* Reset rx'er.                                         */
static  void              Serial_IF_RxReset  (SERIAL_IF             *pif);
                                                                /* Start rx'er.                                         */
static  void              Serial_IF_RxStart  (SERIAL_IF             *pif);
                                                                /* Stop  rx'er.                                         */
static  void              Serial_IF_RxStop   (SERIAL_IF             *pif);

                                                                /* Validate serial interface number.                    */
static  CPU_BOOLEAN       Serial_IF_IsValid  (SERIAL_IF_NBR          if_nbr,
                                              SERIAL_ERR            *perr);


                                                                /* Internal rd callback.                                */
static  void              Serial_RdCallback  (SERIAL_IF_NBR          if_nbr,
                                              void                  *parg,
                                              void                  *pbuf,
                                              CPU_SIZE_T             len,
                                              SERIAL_ERR             err);

                                                                /* Rd async & sync handler.                             */
static  void              Serial_RdHandler   (SERIAL_IF             *pif,
                                              void                  *pdest,
                                              CPU_SIZE_T             len,
                                              SERIAL_CALLBACK_FNCT  *phandler,
                                              void                  *parg,
                                              CPU_BOOLEAN            async,
                                              CPU_INT32U             timeout_ms,
                                              SERIAL_ERR            *perr);

                                                                /* Start rd.                                            */
static  void              Serial_RdStart     (SERIAL_IF             *pif,
                                              void                  *pdest,
                                              CPU_SIZE_T             len);

                                                                /* Wait for rd end.                                     */
static  CPU_SIZE_T        Serial_RdEnd       (SERIAL_IF             *pif,
                                              SERIAL_CALLBACK_INFO  *pinfo,
                                              SERIAL_ERR            *perr);


                                                                /* Internal wr callback.                                */
static  void              Serial_WrCallback  (SERIAL_IF_NBR          if_nbr,
                                              void                  *parg,
                                              void                  *pbuf,
                                              CPU_SIZE_T             len,
                                              SERIAL_ERR             err);

                                                                /* Insert wr buf into tx list.                          */
#if (SERIAL_CFG_TX_DESC_NBR > 0)
static  void              Serial_WrBufInsert (SERIAL_IF             *pif,
                                              SERIAL_BUF_DESC       *pbuf,
                                              SERIAL_ERR            *perr);
#endif

                                                                /* Remove wr buf from tx list.                          */
static  CPU_BOOLEAN       Serial_WrBufRemove (SERIAL_IF             *pif,
                                              SERIAL_BUF_DESC       *pbuf);

                                                                /* Init wr buf and start tx'er.                         */
static  void              Serial_WrBufTxStart(SERIAL_IF             *pif,
                                              SERIAL_BUF_DESC       *pbuf,
                                              SERIAL_ERR            *perr);

                                                                /* Start wr.                                            */
static  void              Serial_WrStart     (SERIAL_IF             *pif,
                                              SERIAL_BUF_DESC       *pbuf,
                                              SERIAL_ERR            *perr);

                                                                /* Tx next wr buf from tx list.                         */
static  void              Serial_WrNext      (SERIAL_IF             *pif);

                                                                /* Wait for wr end.                                     */
static  CPU_SIZE_T        Serial_WrEnd       (SERIAL_IF             *pif,
                                              SERIAL_CALLBACK_INFO  *pinfo,
                                              SERIAL_ERR            *perr);


/*$PAGE*/
/*
*********************************************************************************************************
*********************************************************************************************************
*                                     SERIAL INTERFACE FUNCTIONS
*********************************************************************************************************
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                            Serial_Init()
*
* Description : Initialize the serial communication module:
*
* Argument(s) : None.
*
* Return(s)   : SERIAL_ERR_NONE,                    if NO errors.
*
*               Specific initialization error code, otherwise.
*
* Caller(s)   : Application.
*
* Note(s)     : (1) SerialInit() MUST be called ... :
*
*                   (a) ONLY ONCE from a product's application; ...
*                   (b) (1) AFTER  product's OS has been initialized
*                       (2) BEFORE product's application calls any network protocol suite function(s)
*********************************************************************************************************
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -