📄 probe_rs232.c
字号:
/*
*********************************************************************************************************
* uC/Probe Communication
*
* (c) Copyright 2007; 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.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* uC/Probe
*
* Communication: RS-232
*
* Filename : probe_rs232.c
* Version : V1.30
* Programmer(s) : BAN
* Note(s) : (1) The abbreviations RX and TX refer to communication from the target's perspective.
*
* (2) The abbreviations RD and WR refer to reading data from the target memory and
* writing data to the target memory, respectively.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define PROBE_RS232_GLOBALS
#include <probe_rs232.h>
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* RS-232 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_RS232_PROTOCOL_RX_SD0-PROBE_RS232_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_RS232_PROTOCOL_?X_ED.
*
* +-------------------+-------------------+
* | 'u' | 'C' | 'P' | 'r' |
* +-------------------+-------------------+
* | Length | 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 PROBE_RS232_PROTOCOL_RX_SD0 0x75 /* (A) Start delimiters */
#define PROBE_RS232_PROTOCOL_RX_SD1 0x43
#define PROBE_RS232_PROTOCOL_RX_SD2 0x50
#define PROBE_RS232_PROTOCOL_RX_SD3 0x72
#define PROBE_RS232_PROTOCOL_RX_ED 0x2F /* (E) End delimiter */
/* ------------ OUTBOUND PACKET DELIMITERS ------------ */
#define PROBE_RS232_PROTOCOL_TX_SD0 0x75 /* (A) Start delimiters */
#define PROBE_RS232_PROTOCOL_TX_SD1 0x43
#define PROBE_RS232_PROTOCOL_TX_SD2 0x50
#define PROBE_RS232_PROTOCOL_TX_SD3 0x72
#define PROBE_RS232_PROTOCOL_TX_ED 0x2F /* (E) End delimiter */
/* ----------- RECEIVE STATE MACHINE STATES ----------- */
#define PROBE_RS232_RX_STATE_SD0 0 /* (0) waiting for start first start delimiter (SD0) */
#define PROBE_RS232_RX_STATE_SD1 1 /* (1) waiting for start second start delimiter (SD1) */
#define PROBE_RS232_RX_STATE_SD2 2 /* (2) waiting for start third start delimiter (SD2) */
#define PROBE_RS232_RX_STATE_SD3 3 /* (3) waiting for start fourth start delimiter (SD3) */
#define PROBE_RS232_RX_STATE_LEN1 4 /* (4) waiting for length, first byte */
#define PROBE_RS232_RX_STATE_LEN2 5 /* (5) waiting for length, second byte */
#define PROBE_RS232_RX_STATE_PAD1 6 /* (6) waiting for padding, first byte */
#define PROBE_RS232_RX_STATE_PAD2 7 /* (6) waiting for padding, second byte */
#define PROBE_RS232_RX_STATE_DATA 8 /* (6) waiting for data */
#define PROBE_RS232_RX_STATE_CHKSUM 9 /* (7) waiting for checksum */
#define PROBE_RS232_RX_STATE_ED 10 /* (8) waiting for end delimiter */
/* ---------- TRANSMIT STATE MACHINE STATES ----------- */
#define PROBE_RS232_TX_STATE_SD0 0 /* (0) waiting to send start first start delim. (SD0) */
#define PROBE_RS232_TX_STATE_SD1 1 /* (1) waiting to send start second start delim. (SD1) */
#define PROBE_RS232_TX_STATE_SD2 2 /* (2) waiting to send start third start delim. (SD2) */
#define PROBE_RS232_TX_STATE_SD3 3 /* (3) waiting to send start fourth start delim. (SD3) */
#define PROBE_RS232_TX_STATE_LEN1 4 /* (4) waiting to send length, first byte */
#define PROBE_RS232_TX_STATE_LEN2 5 /* (5) waiting to send length, second byte */
#define PROBE_RS232_TX_STATE_PAD1 6 /* (6) waiting to send padding, first byte */
#define PROBE_RS232_TX_STATE_PAD2 7 /* (7) waiting to send padding, second byte */
#define PROBE_RS232_TX_STATE_DATA 8 /* (8) waiting to send data */
#define PROBE_RS232_TX_STATE_CHKSUM 9 /* (9) waiting to send checksum */
#define PROBE_RS232_TX_STATE_ED 10 /* (10) waiting to send end delimiter */
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
/* -------------- RX state variables ------------------ */
static CPU_INT08U ProbeRS232_RxState; /* ... Current state of RX state machine */
static CPU_INT16U ProbeRS232_RxRemainLen; /* ... Remaining bytes of data to read */
/* -------------- RX packet variables ----------------- */
static CPU_INT16U ProbeRS232_RxLen; /* ... Length of data in current packet */
static CPU_INT08U ProbeRS232_RxBuf[PROBE_RS232_RX_BUF_SIZE]; /* ... Data of current packet */
static CPU_INT08U ProbeRS232_RxChkSum; /* ... Checksum of current packet */
/* ------------ RX data buffer variable --------------- */
static CPU_INT16U ProbeRS232_RxBufWrIx; /* ... Index of next write; also number of bytes in buf*/
/* -------------- TX state variables ------------------ */
static CPU_INT08U ProbeRS232_TxState; /* ... Current state of TX state machine */
static CPU_BOOLEAN ProbeRS232_TxActiveFlag; /* ... Indicates TX is currently active */
/* -------------- TX packet variables ---------------- */
static CPU_INT16U ProbeRS232_TxLen; /* ... Length of data in current packet */
static CPU_INT08U ProbeRS232_TxBuf[PROBE_RS232_TX_BUF_SIZE]; /* ... Data of current packet */
static CPU_INT08U ProbeRS232_TxChkSum; /* ... Checksum of current packet */
/* ----------- TX data buffer variables --------------- */
static CPU_BOOLEAN ProbeRS232_TxBufInUse; /* ... Indicates TX buffer currently holds a packet */
static CPU_INT16U ProbeRS232_TxBufRdIx; /* ... Index of next read */
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
static CPU_INT16U ProbeRS232_ParseRxPkt(void);
static void ProbeRS232_RxPkt (void);
static void ProbeRS232_RxStoINT8U(CPU_INT08U rx_data);
static void ProbeRS232_RxBufClr (void);
static void ProbeRS232_TxStart (void);
/*
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*********************************************************************************************************
** Initialization
*********************************************************************************************************
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* ProbeRS232_Init()
*
* Description : Initialize the RS-232 communication module.
*
* Argument(s) : baud_rate The RS-232 baud rate which will be passed to the hardware initialization.
*
* Return(s) : none.
*********************************************************************************************************
*/
void ProbeRS232_Init (CPU_INT32U baud_rate)
{
ProbeRS232_RxState = PROBE_RS232_RX_STATE_SD0; /* Setup Rx and Tx state machines */
ProbeRS232_TxState = PROBE_RS232_TX_STATE_SD0;
ProbeRS232_TxLen = 0;
ProbeRS232_TxActiveFlag = DEF_FALSE;
ProbeRS232_TxBufInUse = DEF_FALSE;
#if (PROBE_COM_STAT_EN > 0)
ProbeRS232_RxCtr = 0;
ProbeRS232_RxPktCtr = 0;
ProbeRS232_TxCtr = 0;
ProbeRS232_TxPktCtr = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -