RTT Shell控制台
立即下载
资源介绍:
RTT Shell控制台
/*********************************************************************
* SEGGER MICROCONTROLLER GmbH & Co. KG *
* Solutions for real time microcontroller applications *
**********************************************************************
* *
* (c) 2014-2014 SEGGER Microcontroller GmbH & Co. KG *
* *
* Internet: www.segger.com Support: support@segger.com *
* *
**********************************************************************
----------------------------------------------------------------------
File : SEGGER_RTT.c
Purpose : Implementation of SEGGER real-time terminal (RTT) which allows
real-time terminal communication on targets which support
debugger memory accesses while the CPU is running.
Type "int" is assumed to be 32-bits in size
H->T Host to target communication
T->H Target to host communication
RTT channel 0 is always present and reserved for Terminal usage.
Name is fixed to "Terminal"
---------------------------END-OF-HEADER------------------------------
*/
#include "SEGGER_RTT_Conf.h"
#include "SEGGER_RTT.h"
#include // for memcpy
/*********************************************************************
*
* Defines, configurable
*
**********************************************************************
*/
#ifndef BUFFER_SIZE_UP
#define BUFFER_SIZE_UP (1024) // Size of the buffer for terminal output of target, up to host
#endif
#ifndef BUFFER_SIZE_DOWN
#define BUFFER_SIZE_DOWN (16) // Size of the buffer for terminal input to target from host (Usually keyboard input)
#endif
#ifndef SEGGER_RTT_MAX_NUM_UP_BUFFERS
#define SEGGER_RTT_MAX_NUM_UP_BUFFERS (1) // Number of up-buffers (T->H) available on this target
#endif
#ifndef SEGGER_RTT_MAX_NUM_DOWN_BUFFERS
#define SEGGER_RTT_MAX_NUM_DOWN_BUFFERS (1) // Number of down-buffers (H->T) available on this target
#endif
#ifndef SEGGER_RTT_LOCK
#define SEGGER_RTT_LOCK()
#endif
#ifndef SEGGER_RTT_UNLOCK
#define SEGGER_RTT_UNLOCK()
#endif
#ifndef SEGGER_RTT_IN_RAM
#define SEGGER_RTT_IN_RAM (0)
#endif
/*********************************************************************
*
* Defines, fixed
*
**********************************************************************
*/
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MEMCPY(a, b, c) memcpy((a),(b),(c))
//
// For some environments, NULL may not be defined until certain headers are included
//
#ifndef NULL
#define NULL 0
#endif
/*********************************************************************
*
* Types
*
**********************************************************************
*/
//
// Description for a circular buffer (also called "ring buffer")
// which is used as up- (T->H) or down-buffer (H->T)
//
typedef struct {
const char* sName; // Optional name. Standard names so far are: "Terminal", "VCom"
char* pBuffer; // Pointer to start of buffer
int SizeOfBuffer; // Buffer size in bytes. Note that one byte is lost, as this implementation does not fill up the buffer in order to avoid the problem of being unable to distinguish between full and empty.
volatile int WrOff; // Position of next item to be written by either host (down-buffer) or target (up-buffer). Must be volatile since it may be modified by host (down-buffer)
volatile int RdOff; // Position of next item to be read by target (down-buffer) or host (up-buffer). Must be volatile since it may be modified by host (up-buffer)
int Flags; // Contains configuration flags
} RING_BUFFER;
//
// RTT control block which describes the number of buffers available
// as well as the configuration for each buffer
//
//
typedef struct {
char acID[16]; // Initialized to "SEGGER RTT"
int MaxNumUpBuffers; // Initialized to SEGGER_RTT_MAX_NUM_UP_BUFFERS (type. 2)
int MaxNumDownBuffers; // Initialized to SEGGER_RTT_MAX_NUM_DOWN_BUFFERS (type. 2)
RING_BUFFER aUp[SEGGER_RTT_MAX_NUM_UP_BUFFERS]; // Up buffers, transferring information up from target via debug probe to host
RING_BUFFER aDown[SEGGER_RTT_MAX_NUM_DOWN_BUFFERS]; // Down buffers, transferring information down from host via debug probe to target
} SEGGER_RTT_CB;
/*********************************************************************
*
* Static data
*
**********************************************************************
*/
//
// Allocate buffers for channel 0
//
static char _acUpBuffer [BUFFER_SIZE_UP];
static char _acDownBuffer[BUFFER_SIZE_DOWN];
//
// Initialize SEGGER Real-time-Terminal control block (CB)
//
static SEGGER_RTT_CB _SEGGER_RTT = {
#if SEGGER_RTT_IN_RAM
"SEGGER RTTI",
#else
"SEGGER RTT",
#endif
SEGGER_RTT_MAX_NUM_UP_BUFFERS,
SEGGER_RTT_MAX_NUM_DOWN_BUFFERS,
{{ "Terminal", &_acUpBuffer[0], sizeof(_acUpBuffer), 0, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP }},
{{ "Terminal", &_acDownBuffer[0], sizeof(_acDownBuffer), 0, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP }},
};
/*********************************************************************
*
* Static code
*
**********************************************************************
*/
/*********************************************************************
*
* _strlen
*
* Function description
* ANSI compatible function to determine the length of a string
*
* Return value
* Length of string in bytes.
*
* Parameters
* s Pointer to \0 terminated string.
*
* Notes
* (1) s needs to point to an \0 terminated string. Otherwise proper functionality of this function is not guaranteed.
*/
static int _strlen(const char* s) {
int Len;
Len = 0;
if (s == NULL) {
return 0;
}
do {
if (*s == 0) {
break;
}
Len++;
s++;
} while (1);
return Len;
}
/*********************************************************************
*
* _Init
*
* Function description
* In case SEGGER_RTT_IN_RAM is defined,
* _Init() modifies the ID of the RTT CB to allow identifying the
* RTT Control Block Structure in the data segment.
*/
static void _Init(void) {
#if SEGGER_RTT_IN_RAM
if (_SEGGER_RTT.acID[10] == 'I') {
_SEGGER_RTT.acID[10] = '\0';
}
#endif
}
/*********************************************************************
*
* Public code
*
**********************************************************************
*/
/*********************************************************************
*
* SEGGER_RTT_Read
*
* Function description
* Reads characters from SEGGER real-time-terminal control block
* which have been previously stored by the host.
*
* Parameters
* BufferIndex Index of Down-buffer to be used. (e.g. 0 for "Terminal")
* pBuffer Pointer to buffer provided by target application, to copy characters from RTT-down-buffer to.
* BufferSize Size of the target application buffer
*
* Return values
* Number of bytes that have been read
*/
int SEGGER_RTT_Read(unsigned BufferIndex, char* pBuffer, unsigned BufferSize) {
int NumBytesRem;
unsigned NumBytesRead;
int RdOff;
int WrOff;
SEGGER_RTT