📄 serial_line_probe.c
字号:
/*
*********************************************************************************************************
* 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
*
* SERIAL LINE DRIVER
* PROBE
*
* Filename : serial_line_probe.c
* Version : V2.00
* Programmer(s) : FGK
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define SERIAL_LINE_PROBE_MODULE
#include <serial.h>
#include <serial_line_probe.h>
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* SERIAL PACKET FORMAT
*
* Note(s): (1) All packets include the following parts:
*
* (A) 4 1-byte start delimiters, forming the ASCII representation of "uCPr". These
* are the constants PROBE_SERIAL_PROTOCOL_?X_SD0-PROBE_SERIAL_PROTOCOL_?X_SD4;
* (B) 1 2-byte length, the length of the data segment;
* (C) 1 2-byte padding, unused;
* (D) n bytes of data; and
* (E) 1 1-byte checksum; and
* (F) 1 1-byte end delimiter, the character '/', which is the constant PROBE_SERIAL_PROTOCOL_?X_ED.
*
* +-------------------+-------------------+
* | 'u' | 'C' | 'P' | 'r' |
* +-------------------+-------------------+
* | Length | PktCtr | Padding |
* +-------------------+-------------------+
* | Data | The data segment does not need to end on
* | . | a four-byte boundary, as might be inferred
* | . | from this diagram.
* | . |
* +-------------------+-------------------+
* | Checksum| '/' |
* +-------------------+
*********************************************************************************************************
*/
/* ------------- INBOUND PACKET DELIMITERS ------------ */
#define SERIAL_LINE_PROBE_PROTOCOL_RX_SD0 0x75 /* Start delimiters. */
#define SERIAL_LINE_PROBE_PROTOCOL_RX_SD1 0x43
#define SERIAL_LINE_PROBE_PROTOCOL_RX_SD2 0x50
#define SERIAL_LINE_PROBE_PROTOCOL_RX_SD3 0x72
#define SERIAL_LINE_PROBE_PROTOCOL_RX_ED 0x2F /* End delimiter. */
/* ------------ OUTBOUND PACKET DELIMITERS ------------ */
#define SERIAL_LINE_PROBE_PROTOCOL_TX_SD0 0x75 /* Start delimiters. */
#define SERIAL_LINE_PROBE_PROTOCOL_TX_SD1 0x43
#define SERIAL_LINE_PROBE_PROTOCOL_TX_SD2 0x50
#define SERIAL_LINE_PROBE_PROTOCOL_TX_SD3 0x72
#define SERIAL_LINE_PROBE_PROTOCOL_TX_ED 0x2F /* End delimiter. */
/* ----------- RECEIVE STATE MACHINE STATES ----------- */
#define SERIAL_LINE_PROBE_RX_STATE_SD0 0 /* Waiting for start first start delimiter (SD0). */
#define SERIAL_LINE_PROBE_RX_STATE_SD1 1 /* Waiting for start second start delimiter (SD1). */
#define SERIAL_LINE_PROBE_RX_STATE_SD2 2 /* Waiting for start third start delimiter (SD2). */
#define SERIAL_LINE_PROBE_RX_STATE_SD3 3 /* Waiting for start fourth start delimiter (SD3). */
#define SERIAL_LINE_PROBE_RX_STATE_LEN1 4 /* Waiting for length, first byte. */
#define SERIAL_LINE_PROBE_RX_STATE_LEN2 5 /* Waiting for length, second byte. */
#define SERIAL_LINE_PROBE_RX_STATE_CTR 6 /* Waiting for packet counter. */
#define SERIAL_LINE_PROBE_RX_STATE_PAD 7 /* Waiting for padding byte. */
#define SERIAL_LINE_PROBE_RX_STATE_DATA 8 /* Waiting for data. */
#define SERIAL_LINE_PROBE_RX_STATE_CHKSUM 9 /* Waiting for checksum. */
#define SERIAL_LINE_PROBE_RX_STATE_ED 10 /* Waiting for end delimiter. */
/* ---------- TRANSMIT STATE MACHINE STATES ----------- */
#define SERIAL_LINE_PROBE_TX_STATE_SD0 0 /* Waiting to send start first start delim. (SD0). */
#define SERIAL_LINE_PROBE_TX_STATE_SD1 1 /* Waiting to send start second start delim. (SD1). */
#define SERIAL_LINE_PROBE_TX_STATE_SD2 2 /* Waiting to send start third start delim. (SD2). */
#define SERIAL_LINE_PROBE_TX_STATE_SD3 3 /* Waiting to send start fourth start delim. (SD3). */
#define SERIAL_LINE_PROBE_TX_STATE_LEN1 4 /* Waiting to send length, first byte. */
#define SERIAL_LINE_PROBE_TX_STATE_LEN2 5 /* Waiting to send length, second byte. */
#define SERIAL_LINE_PROBE_TX_STATE_CTR 6 /* Waiting to send packet counter. */
#define SERIAL_LINE_PROBE_TX_STATE_PAD 7 /* Waiting to send padding byte. */
#define SERIAL_LINE_PROBE_TX_STATE_DATA 8 /* Waiting to send data. */
#define SERIAL_LINE_PROBE_TX_STATE_CHKSUM 9 /* Waiting to send checksum. */
#define SERIAL_LINE_PROBE_TX_STATE_ED 10 /* Waiting to send end delimiter. */
#define SERIAL_LINE_PROBE_TX_STATE_LAST 11 /* Final byte sent. */
#define SERIAL_LINE_PROBE_USE_CHECKSUM DEF_FALSE /* DO NOT CHANGE. */
/*
*********************************************************************************************************
* FORWARD DECLARATIONS
*********************************************************************************************************
*/
typedef struct serial_line_probe_info SERIAL_LINE_INFO;
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
struct serial_line_probe_info {
SERIAL_LINE_INFO *NextPtr; /* Ptr to next serial line info struct. */
CPU_INT08U RxState; /* Rx state machine state. */
CPU_SIZE_T RxLenRem;
CPU_INT08U RxPktCtr;
#if (SERIAL_LINE_PROBE_USE_CHECKSUM == DEF_TRUE)
CPU_INT08U RxChkSum;
#endif
CPU_INT08U TxState; /* Tx state machine state. */
#if (SERIAL_LINE_PROBE_USE_CHECKSUM == DEF_TRUE)
CPU_INT08U TxChkSum;
#endif
};
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
static SERIAL_LINE_INFO *SerialLine_Probe_Info = (SERIAL_LINE_INFO *)0;
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
/* Open line driver. */
static void *SerialLine_Open (SERIAL_DEV *pdev,
SERIAL_ERR *perr);
/* Close line driver. */
static void SerialLine_Close (SERIAL_DEV *pdev,
void *pline_data,
SERIAL_ERR *perr);
/* Rd octet from buf via protocol. */
static void SerialLine_RdOctet(SERIAL_DEV *pdev,
SERIAL_BUF *pbuf,
CPU_INT08U *pdatum,
SERIAL_ERR *perr);
/* Wr octet into buf via protocol. */
static void SerialLine_WrOctet(SERIAL_DEV *pdev,
SERIAL_BUF *pbuf,
CPU_INT08U datum,
SERIAL_ERR *perr);
/*
*********************************************************************************************************
*********************************************************************************************************
* SERIAL INTERFACE LINE DRIVER API
*********************************************************************************************************
*********************************************************************************************************
*/
SERIAL_LINE_DRV_API SerialLine_Probe = {
SerialLine_Open,
SerialLine_Close,
SerialLine_RdOctet,
SerialLine_WrOctet
};
/*$PAGE*/
/*
*********************************************************************************************************
*********************************************************************************************************
* DRIVER INTERFACE FUNCTIONS
*********************************************************************************************************
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* SerialLine_Open()
*
* Description : Open serial line driver.
*
* Argument(s) : pdev Pointer to device.
*
* perr Pointer to variable that will receive the return error code from this function :
*
* SERIAL_ERR_NONE Serial line driver opened.
* SERIAL_ERR_LINE_OPEN Serial line driver could NOT be opened.
*
* Return(s) : Pointer to line driver data.
*
* Caller(s) : Serial_Open().
*
* Note(s) : (1) Interrupts are assumed to be disabled when this function is called.
*********************************************************************************************************
*/
static void *SerialLine_Open (SERIAL_DEV *pdev,
SERIAL_ERR *perr)
{
SERIAL_LINE_INFO *pinfo;
CPU_SIZE_T octets_reqd;
LIB_ERR lib_err;
(void)&pdev;
pinfo = SerialLine_Probe_Info;
if (pinfo == (SERIAL_LINE_INFO *)0) { /* Find avail Probe info struct. */
pinfo = (SERIAL_LINE_INFO *)Mem_HeapAlloc((CPU_SIZE_T ) sizeof(SERIAL_LINE_INFO),
(CPU_SIZE_T ) sizeof(CPU_ALIGN),
(CPU_SIZE_T *)&octets_reqd,
(LIB_ERR *)&lib_err);
if (pinfo == (SERIAL_LINE_INFO *)0) {
*perr = SERIAL_ERR_LINE_DRV_OPEN;
return ((void *)0);
}
} else {
SerialLine_Probe_Info = pinfo->NextPtr;
}
pinfo->NextPtr = (SERIAL_LINE_INFO *)0;
pinfo->RxState = SERIAL_LINE_PROBE_RX_STATE_SD0;
pinfo->RxLenRem = 0;
pinfo->RxPktCtr = 0;
#if (SERIAL_LINE_PROBE_USE_CHECKSUM == DEF_TRUE)
pinfo->RxChkSum = 0;
#endif
pinfo->TxState = SERIAL_LINE_PROBE_TX_STATE_SD0;
#if (SERIAL_LINE_PROBE_USE_CHECKSUM == DEF_TRUE)
pinfo->TxChkSum = 0;
#endif
*perr = SERIAL_ERR_NONE;
return ((void *)pinfo);
}
/*$PAGE*/
/*
*********************************************************************************************************
* SerialLine_Close()
*
* Description : Close serial line driver.
*
* Argument(s) : pdev Pointer to device.
*
* pline_data Pointer to line driver data structure.
*
* perr Pointer to variable that will receive the return error code from this function :
*
* SERIAL_ERR_NONE Serial line driver closed.
*
* Return(s) : None.
*
* Caller(s) : Serial_Close().
*
* Note(s) : (1) Interrupts are assumed to be disabled when this function is called.
*********************************************************************************************************
*/
static void SerialLine_Close (SERIAL_DEV *pdev,
void *pline_data,
SERIAL_ERR *perr)
{
SERIAL_LINE_INFO *pinfo;
(void)&pdev;
pinfo = (SERIAL_LINE_INFO *)pline_data;
pinfo->RxState = SERIAL_LINE_PROBE_RX_STATE_SD0;
pinfo->RxLenRem = 0;
#if (SERIAL_LINE_PROBE_USE_CHECKSUM == DEF_TRUE)
pinfo->RxChkSum = 0;
#endif
pinfo->TxState = SERIAL_LINE_PROBE_TX_STATE_SD0;
#if (SERIAL_LINE_PROBE_USE_CHECKSUM == DEF_TRUE)
pinfo->TxChkSum = 0;
#endif
pinfo->NextPtr = SerialLine_Probe_Info;
SerialLine_Probe_Info = pinfo;
*perr = SERIAL_ERR_NONE;
}
/*$PAGE*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -