Commit 6ff49dc1 authored by Dmitry Frank's avatar Dmitry Frank Committed by Cesanta Bot

Fix nRF51 example and add RTT support to it

Now it works in hardware.

PUBLISHED_FROM=f223bde2d2bebe97513ce073eb887552bc3d93af
parent cd778396
...@@ -160,7 +160,7 @@ ...@@ -160,7 +160,7 @@
<Group> <Group>
<GroupName>Application</GroupName> <GroupName>Application</GroupName>
<tvExp>1</tvExp> <tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel> <cbSel>0</cbSel>
<RteFlg>0</RteFlg> <RteFlg>0</RteFlg>
...@@ -416,7 +416,7 @@ ...@@ -416,7 +416,7 @@
<Group> <Group>
<GroupName>nRF_Libraries</GroupName> <GroupName>nRF_Libraries</GroupName>
<tvExp>0</tvExp> <tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel> <cbSel>0</cbSel>
<RteFlg>0</RteFlg> <RteFlg>0</RteFlg>
...@@ -537,30 +537,17 @@ ...@@ -537,30 +537,17 @@
<RteFlg>0</RteFlg> <RteFlg>0</RteFlg>
<bShared>0</bShared> <bShared>0</bShared>
</File> </File>
<File>
<GroupNumber>7</GroupNumber>
<FileNumber>26</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\..\..\..\nrf51_iot_sdk\components\libraries\uart\retarget.c</PathWithFileName>
<FilenameWithoutPath>retarget.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
</Group> </Group>
<Group> <Group>
<GroupName>nRF_SoftDevice</GroupName> <GroupName>nRF_SoftDevice</GroupName>
<tvExp>0</tvExp> <tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel> <cbSel>0</cbSel>
<RteFlg>0</RteFlg> <RteFlg>0</RteFlg>
<File> <File>
<GroupNumber>8</GroupNumber> <GroupNumber>8</GroupNumber>
<FileNumber>27</FileNumber> <FileNumber>26</FileNumber>
<FileType>5</FileType> <FileType>5</FileType>
<tvExp>0</tvExp> <tvExp>0</tvExp>
<Focus>0</Focus> <Focus>0</Focus>
...@@ -573,7 +560,7 @@ ...@@ -573,7 +560,7 @@
</File> </File>
<File> <File>
<GroupNumber>8</GroupNumber> <GroupNumber>8</GroupNumber>
<FileNumber>28</FileNumber> <FileNumber>27</FileNumber>
<FileType>1</FileType> <FileType>1</FileType>
<tvExp>0</tvExp> <tvExp>0</tvExp>
<Focus>0</Focus> <Focus>0</Focus>
...@@ -586,7 +573,7 @@ ...@@ -586,7 +573,7 @@
</File> </File>
<File> <File>
<GroupNumber>8</GroupNumber> <GroupNumber>8</GroupNumber>
<FileNumber>29</FileNumber> <FileNumber>28</FileNumber>
<FileType>1</FileType> <FileType>1</FileType>
<tvExp>0</tvExp> <tvExp>0</tvExp>
<Focus>0</Focus> <Focus>0</Focus>
...@@ -607,7 +594,7 @@ ...@@ -607,7 +594,7 @@
<RteFlg>0</RteFlg> <RteFlg>0</RteFlg>
<File> <File>
<GroupNumber>9</GroupNumber> <GroupNumber>9</GroupNumber>
<FileNumber>30</FileNumber> <FileNumber>29</FileNumber>
<FileType>1</FileType> <FileType>1</FileType>
<tvExp>0</tvExp> <tvExp>0</tvExp>
<Focus>0</Focus> <Focus>0</Focus>
...@@ -620,6 +607,79 @@ ...@@ -620,6 +607,79 @@
</File> </File>
</Group> </Group>
<Group>
<GroupName>RTT</GroupName>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>30</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\..\..\rtt\RTT\SEGGER_RTT.c</PathWithFileName>
<FilenameWithoutPath>SEGGER_RTT.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>31</FileNumber>
<FileType>5</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\..\..\rtt\RTT\SEGGER_RTT.h</PathWithFileName>
<FilenameWithoutPath>SEGGER_RTT.h</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>32</FileNumber>
<FileType>5</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\..\..\rtt\RTT\SEGGER_RTT_Conf.h</PathWithFileName>
<FilenameWithoutPath>SEGGER_RTT_Conf.h</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>33</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\..\..\rtt\RTT\SEGGER_RTT_printf.c</PathWithFileName>
<FilenameWithoutPath>SEGGER_RTT_printf.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>34</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\..\..\rtt\Syscalls\RTT_Syscalls_KEIL.c</PathWithFileName>
<FilenameWithoutPath>RTT_Syscalls_KEIL.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
</Group>
<Group> <Group>
<GroupName>::CMSIS</GroupName> <GroupName>::CMSIS</GroupName>
<tvExp>0</tvExp> <tvExp>0</tvExp>
...@@ -635,8 +695,8 @@ ...@@ -635,8 +695,8 @@
<cbSel>0</cbSel> <cbSel>0</cbSel>
<RteFlg>1</RteFlg> <RteFlg>1</RteFlg>
<File> <File>
<GroupNumber>11</GroupNumber> <GroupNumber>12</GroupNumber>
<FileNumber>31</FileNumber> <FileNumber>35</FileNumber>
<FileType>2</FileType> <FileType>2</FileType>
<tvExp>0</tvExp> <tvExp>0</tvExp>
<Focus>0</Focus> <Focus>0</Focus>
...@@ -648,8 +708,8 @@ ...@@ -648,8 +708,8 @@
<bShared>0</bShared> <bShared>0</bShared>
</File> </File>
<File> <File>
<GroupNumber>11</GroupNumber> <GroupNumber>12</GroupNumber>
<FileNumber>32</FileNumber> <FileNumber>36</FileNumber>
<FileType>1</FileType> <FileType>1</FileType>
<tvExp>0</tvExp> <tvExp>0</tvExp>
<Focus>0</Focus> <Focus>0</Focus>
......
...@@ -224,7 +224,7 @@ ...@@ -224,7 +224,7 @@
<hadIRAM2>0</hadIRAM2> <hadIRAM2>0</hadIRAM2>
<hadIROM2>0</hadIROM2> <hadIROM2>0</hadIROM2>
<StupSel>8</StupSel> <StupSel>8</StupSel>
<useUlib>1</useUlib> <useUlib>0</useUlib>
<EndSel>0</EndSel> <EndSel>0</EndSel>
<uLtcg>0</uLtcg> <uLtcg>0</uLtcg>
<RoSelD>3</RoSelD> <RoSelD>3</RoSelD>
...@@ -363,9 +363,9 @@ ...@@ -363,9 +363,9 @@
<useXO>0</useXO> <useXO>0</useXO>
<VariousControls> <VariousControls>
<MiscControls>--c99</MiscControls> <MiscControls>--c99</MiscControls>
<Define>__HEAP_SIZE=1024 BOARD_PCA10028 BLE_STACK_SUPPORT_REQD S110 BSP_DEFINES_ONLY NRF51 SOFTDEVICE_PRESENT SWI_DISABLE0 MG_DISABLE_HTTP_DIGEST_AUTH MG_DISABLE_MD5 MG_DISABLE_HTTP_KEEP_ALIVE MG_ENABLE_HTTP_SSI=0 MG_ENABLE_HTTP_STREAMING_MULTIPART MG_NO_BSD_SOCKETS CS_PLATFORM=CS_P_NRF51 CS_ENABLE_STDIO</Define> <Define>__HEAP_SIZE=1024 BOARD_PCA10028 BLE_STACK_SUPPORT_REQD S110 BSP_DEFINES_ONLY NRF51 SOFTDEVICE_PRESENT SWI_DISABLE0 MG_DISABLE_HTTP_DIGEST_AUTH MG_DISABLE_MD5 MG_DISABLE_HTTP_KEEP_ALIVE MG_ENABLE_HTTP_SSI=0 MG_ENABLE_HTTP_STREAMING_MULTIPART CS_PLATFORM=CS_P_NRF51 CS_ENABLE_STDIO NRF_LOG_USES_RTT ENABLE_DEBUG_LOG_SUPPORT</Define>
<Undefine></Undefine> <Undefine></Undefine>
<IncludePath>..\..\..\config;..\..\..;..\..\..\..\nrf51_iot_sdk\examples\bsp;..\..\..\..\nrf51_iot_sdk\components\softdevice\s1xx_iot\headers;..\..\..\..\nrf51_iot_sdk\components\ble\common;..\..\..\..\nrf51_iot_sdk\components\drivers_nrf\uart;..\..\..\..\nrf51_iot_sdk\components\libraries\timer;..\..\..\..\nrf51_iot_sdk\components\softdevice\common\softdevice_handler;..\..\..\..\nrf51_iot_sdk\components\libraries\scheduler;..\..\..\..\nrf51_iot_sdk\components\libraries\button;..\..\..\..\nrf51_iot_sdk\external\lwip\src\include;..\..\..\..\nrf51_iot_sdk\external\lwip\src\include\netif;..\..\..\..\nrf51_iot_sdk\external\lwip\src\port;..\..\..\..\nrf51_iot_sdk\external\lwip\src\port\arch;..\..\..\..\nrf51_iot_sdk\components\iot\ble_6lowpan;..\..\..\..\nrf51_iot_sdk\components\device;..\..\..\..\nrf51_iot_sdk\components\toolchain;..\..\..\..\nrf51_iot_sdk\components\libraries\util;..\..\..\..\nrf51_iot_sdk\components\libraries\fifo;..\..\..\..\nrf51_iot_sdk\components\drivers_nrf\gpiote;..\..\..\..\nrf51_iot_sdk\components\drivers_nrf\config;..\..\..\..\nrf51_iot_sdk\components\drivers_nrf\common;..\..\..\..\nrf51_iot_sdk\components\drivers_nrf\hal;..\..\..\..\nrf51_iot_sdk\components\libraries\mem_manager;..\..\..\..\nrf51_iot_sdk\components\iot\include;..\..\..\..\nrf51_iot_sdk\components\iot\context_manager;..\..\..\..\nrf51_iot_sdk\components\iot\ble_ipsp;..\..\..\..\nrf51_iot_sdk\components\libraries\trace;..\..\..\..\..\..</IncludePath> <IncludePath>..\..\..\config;..\..\..;..\..\..\..\nrf51_iot_sdk\examples\bsp;..\..\..\..\nrf51_iot_sdk\components\softdevice\s1xx_iot\headers;..\..\..\..\nrf51_iot_sdk\components\ble\common;..\..\..\..\nrf51_iot_sdk\components\drivers_nrf\uart;..\..\..\..\nrf51_iot_sdk\components\libraries\timer;..\..\..\..\nrf51_iot_sdk\components\softdevice\common\softdevice_handler;..\..\..\..\nrf51_iot_sdk\components\libraries\scheduler;..\..\..\..\nrf51_iot_sdk\components\libraries\button;..\..\..\..\nrf51_iot_sdk\external\lwip\src\include;..\..\..\..\nrf51_iot_sdk\external\lwip\src\include\netif;..\..\..\..\nrf51_iot_sdk\external\lwip\src\port;..\..\..\..\nrf51_iot_sdk\external\lwip\src\port\arch;..\..\..\..\nrf51_iot_sdk\components\iot\ble_6lowpan;..\..\..\..\nrf51_iot_sdk\components\device;..\..\..\..\nrf51_iot_sdk\components\toolchain;..\..\..\..\nrf51_iot_sdk\components\libraries\util;..\..\..\..\nrf51_iot_sdk\components\libraries\fifo;..\..\..\..\nrf51_iot_sdk\components\drivers_nrf\gpiote;..\..\..\..\nrf51_iot_sdk\components\drivers_nrf\config;..\..\..\..\nrf51_iot_sdk\components\drivers_nrf\common;..\..\..\..\nrf51_iot_sdk\components\drivers_nrf\hal;..\..\..\..\nrf51_iot_sdk\components\libraries\mem_manager;..\..\..\..\nrf51_iot_sdk\components\iot\include;..\..\..\..\nrf51_iot_sdk\components\iot\context_manager;..\..\..\..\nrf51_iot_sdk\components\iot\ble_ipsp;..\..\..\..\nrf51_iot_sdk\components\libraries\trace;..\..\..\..\..\..;..\..\..\rtt\RTT</IncludePath>
</VariousControls> </VariousControls>
</Cads> </Cads>
<Aads> <Aads>
...@@ -380,7 +380,7 @@ ...@@ -380,7 +380,7 @@
<useXO>0</useXO> <useXO>0</useXO>
<VariousControls> <VariousControls>
<MiscControls></MiscControls> <MiscControls></MiscControls>
<Define> __HEAP_SIZE=1024 BOARD_PCA10028 BLE_STACK_SUPPORT_REQD S110 BSP_DEFINES_ONLY NRF51 SOFTDEVICE_PRESENT SWI_DISABLE0</Define> <Define>__HEAP_SIZE=1024 BOARD_PCA10028 BLE_STACK_SUPPORT_REQD S110 BSP_DEFINES_ONLY NRF51 SOFTDEVICE_PRESENT SWI_DISABLE0 NRF_LOG_USES_RTT ENABLE_DEBUG_LOG_SUPPORT</Define>
<Undefine></Undefine> <Undefine></Undefine>
<IncludePath></IncludePath> <IncludePath></IncludePath>
</VariousControls> </VariousControls>
...@@ -563,11 +563,6 @@ ...@@ -563,11 +563,6 @@
<FileType>1</FileType> <FileType>1</FileType>
<FilePath>..\..\..\..\nrf51_iot_sdk\components\libraries\util\nrf_assert.c</FilePath> <FilePath>..\..\..\..\nrf51_iot_sdk\components\libraries\util\nrf_assert.c</FilePath>
</File> </File>
<File>
<FileName>retarget.c</FileName>
<FileType>1</FileType>
<FilePath>..\..\..\..\nrf51_iot_sdk\components\libraries\uart\retarget.c</FilePath>
</File>
</Files> </Files>
</Group> </Group>
<Group> <Group>
...@@ -600,6 +595,36 @@ ...@@ -600,6 +595,36 @@
</File> </File>
</Files> </Files>
</Group> </Group>
<Group>
<GroupName>RTT</GroupName>
<Files>
<File>
<FileName>SEGGER_RTT.c</FileName>
<FileType>1</FileType>
<FilePath>..\..\..\rtt\RTT\SEGGER_RTT.c</FilePath>
</File>
<File>
<FileName>SEGGER_RTT.h</FileName>
<FileType>5</FileType>
<FilePath>..\..\..\rtt\RTT\SEGGER_RTT.h</FilePath>
</File>
<File>
<FileName>SEGGER_RTT_Conf.h</FileName>
<FileType>5</FileType>
<FilePath>..\..\..\rtt\RTT\SEGGER_RTT_Conf.h</FilePath>
</File>
<File>
<FileName>SEGGER_RTT_printf.c</FileName>
<FileType>1</FileType>
<FilePath>..\..\..\rtt\RTT\SEGGER_RTT_printf.c</FilePath>
</File>
<File>
<FileName>RTT_Syscalls_KEIL.c</FileName>
<FileType>1</FileType>
<FilePath>..\..\..\rtt\Syscalls\RTT_Syscalls_KEIL.c</FilePath>
</File>
</Files>
</Group>
<Group> <Group>
<GroupName>::CMSIS</GroupName> <GroupName>::CMSIS</GroupName>
</Group> </Group>
......
README.txt for the SEGGER RTT Implementation Pack.
Date: 17 Dec 2014
Included files:
===============
Root Directory
- Examples
- RTT_MenuApp.c - Sample application to demonstrate RTT bi-directional functionality. No OS.
- RTT_embOS_SpeedTestApp.c - Sample application for measuring RTT performance. embOS needed.
- RTT
- SEGGER_RTT.c - The RTT implementation.
- SEGGER_RTT.h - Header for RTT implementation.
- SEGGER_RTT_Conf.h - Pre-processor configuration for the RTT implementation.
- SEGGER_RTT_Printf.c - Simple implementation of printf to write formatted strings via RTT.
- Syscalls
- RTT_Syscalls_GCC.c - Low-level syscalls to retarget printf() to RTT with GCC / Newlib.
- RTT_Syscalls_IAR.c - Low-level syscalls to retarget printf() to RTT with IAR compiler.
- RTT_Syscalls_KEIL.c - Low-level syscalls to retarget printf() to RTT with KEIL/uVision compiler.
\ No newline at end of file
/* clang-format off */
/*********************************************************************
* 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
Date : 17 Dec 2014
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 <string.h> // 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 char _ActiveTerminal;
/*********************************************************************
*
* 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_LOCK();
_Init();
RdOff = _SEGGER_RTT.aDown[BufferIndex].RdOff;
WrOff = _SEGGER_RTT.aDown[BufferIndex].WrOff;
NumBytesRead = 0;
//
// Read from current read position to wrap-around of buffer, first
//
if (RdOff > WrOff) {
NumBytesRem = _SEGGER_RTT.aDown[BufferIndex].SizeOfBuffer - RdOff;
NumBytesRem = MIN(NumBytesRem, (int)BufferSize);
MEMCPY(pBuffer, _SEGGER_RTT.aDown[BufferIndex].pBuffer + RdOff, NumBytesRem);
NumBytesRead += NumBytesRem;
pBuffer += NumBytesRem;
BufferSize -= NumBytesRem;
RdOff += NumBytesRem;
//
// Handle wrap-around of buffer
//
if (RdOff == _SEGGER_RTT.aDown[BufferIndex].SizeOfBuffer) {
RdOff = 0;
}
}
//
// Read remaining items of buffer
//
NumBytesRem = WrOff - RdOff;
NumBytesRem = MIN(NumBytesRem, (int)BufferSize);
if (NumBytesRem > 0) {
MEMCPY(pBuffer, _SEGGER_RTT.aDown[BufferIndex].pBuffer + RdOff, NumBytesRem);
NumBytesRead += NumBytesRem;
pBuffer += NumBytesRem;
BufferSize -= NumBytesRem;
RdOff += NumBytesRem;
}
if (NumBytesRead) {
_SEGGER_RTT.aDown[BufferIndex].RdOff = RdOff;
}
SEGGER_RTT_UNLOCK();
return NumBytesRead;
}
/*********************************************************************
*
* SEGGER_RTT_Write
*
* Function description
* Stores a specified number of characters in SEGGER RTT
* control block which is then read by the host.
*
* Parameters
* BufferIndex Index of "Up"-buffer to be used. (e.g. 0 for "Terminal")
* pBuffer Pointer to character array. Does not need to point to a \0 terminated string.
* NumBytes Number of bytes to be stored in the SEGGER RTT control block.
*
* Return values
* Number of bytes which have been stored in the "Up"-buffer.
*
* Notes
* (1) If there is not enough space in the "Up"-buffer, remaining characters of pBuffer are dropped.
*/
int SEGGER_RTT_Write(unsigned BufferIndex, const char* pBuffer, unsigned NumBytes) {
int NumBytesToWrite;
unsigned NumBytesWritten;
int RdOff;
//
// Target is not allowed to perform other RTT operations while string still has not been stored completely.
// Otherwise we would probably end up with a mixed string in the buffer.
//
SEGGER_RTT_LOCK();
_Init();
//
// In case we are not in blocking mode,
// we need to calculate, how many bytes we can put into the buffer at all.
//
if ((_SEGGER_RTT.aUp[BufferIndex].Flags & SEGGER_RTT_MODE_MASK) != SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL) {
RdOff = _SEGGER_RTT.aUp[BufferIndex].RdOff;
NumBytesToWrite = RdOff - _SEGGER_RTT.aUp[BufferIndex].WrOff - 1;
if (NumBytesToWrite < 0) {
NumBytesToWrite += _SEGGER_RTT.aUp[BufferIndex].SizeOfBuffer;
}
//
// If the complete data does not fit in the buffer, check if we have to skip it completely or trim the data
//
if ((int)NumBytes > NumBytesToWrite) {
if ((_SEGGER_RTT.aUp[BufferIndex].Flags & SEGGER_RTT_MODE_MASK) == SEGGER_RTT_MODE_NO_BLOCK_SKIP) {
SEGGER_RTT_UNLOCK();
return 0;
} else {
NumBytes = NumBytesToWrite;
}
}
}
//
// Early out if nothing is to do
//
if (NumBytes == 0) {
SEGGER_RTT_UNLOCK();
return 0;
}
//
// Write data to buffer and handle wrap-around if necessary
//
NumBytesWritten = 0;
do {
RdOff = _SEGGER_RTT.aUp[BufferIndex].RdOff; // May be changed by host (debug probe) in the meantime
NumBytesToWrite = RdOff - _SEGGER_RTT.aUp[BufferIndex].WrOff - 1;
if (NumBytesToWrite < 0) {
NumBytesToWrite += _SEGGER_RTT.aUp[BufferIndex].SizeOfBuffer;
}
NumBytesToWrite = MIN(NumBytesToWrite, (_SEGGER_RTT.aUp[BufferIndex].SizeOfBuffer - _SEGGER_RTT.aUp[BufferIndex].WrOff)); // Number of bytes that can be written until buffer wrap-around
NumBytesToWrite = MIN(NumBytesToWrite, (int)NumBytes);
MEMCPY(_SEGGER_RTT.aUp[BufferIndex].pBuffer + _SEGGER_RTT.aUp[BufferIndex].WrOff, pBuffer, NumBytesToWrite);
NumBytesWritten += NumBytesToWrite;
pBuffer += NumBytesToWrite;
NumBytes -= NumBytesToWrite;
_SEGGER_RTT.aUp[BufferIndex].WrOff += NumBytesToWrite;
if (_SEGGER_RTT.aUp[BufferIndex].WrOff == _SEGGER_RTT.aUp[BufferIndex].SizeOfBuffer) {
_SEGGER_RTT.aUp[BufferIndex].WrOff = 0;
}
} while (NumBytes);
SEGGER_RTT_UNLOCK();
return NumBytesWritten;
}
/*********************************************************************
*
* SEGGER_RTT_WriteString
*
* Function description
* Stores string in SEGGER RTT control block.
* This data is read by the host.
*
* Parameters
* BufferIndex Index of "Up"-buffer to be used. (e.g. 0 for "Terminal")
* s Pointer to string.
*
* Return values
* Number of bytes which have been stored in the "Up"-buffer.
*
* Notes
* (1) If there is not enough space in the "Up"-buffer, depending on configuration,
* remaining characters may be dropped or RTT module waits until there is more space in the buffer.
* (2) String passed to this function has to be \0 terminated
* (3) \0 termination character is *not* stored in RTT buffer
*/
int SEGGER_RTT_WriteString(unsigned BufferIndex, const char* s) {
int Len;
Len = _strlen(s);
return SEGGER_RTT_Write(BufferIndex, s, Len);
}
/*********************************************************************
*
* SEGGER_RTT_GetKey
*
* Function description
* Reads one character from the SEGGER RTT buffer.
* Host has previously stored data there.
*
* Return values
* < 0 No character available (buffer empty).
* >= 0 Character which has been read. (Possible values: 0 - 255)
*
* Notes
* (1) This function is only specified for accesses to RTT buffer 0.
*/
int SEGGER_RTT_GetKey(void) {
char c;
int r;
r = SEGGER_RTT_Read(0, &c, 1);
if (r == 1) {
return (int)(unsigned char)c;
}
return -1;
}
/*********************************************************************
*
* SEGGER_RTT_WaitKey
*
* Function description
* Waits until at least one character is avaible in the SEGGER RTT buffer.
* Once a character is available, it is read and this function returns.
*
* Return values
* >=0 Character which has been read.
*
* Notes
* (1) This function is only specified for accesses to RTT buffer 0
* (2) This function is blocking if no character is present in RTT buffer
*/
int SEGGER_RTT_WaitKey(void) {
int r;
do {
r = SEGGER_RTT_GetKey();
} while (r < 0);
return r;
}
/*********************************************************************
*
* SEGGER_RTT_HasKey
*
* Function description
* Checks if at least one character for reading is available in the SEGGER RTT buffer.
*
* Return values
* 0 No characters are available to read.
* 1 At least one character is available.
*
* Notes
* (1) This function is only specified for accesses to RTT buffer 0
*/
int SEGGER_RTT_HasKey(void) {
int RdOff;
_Init();
RdOff = _SEGGER_RTT.aDown[0].RdOff;
if (RdOff != _SEGGER_RTT.aDown[0].WrOff) {
return 1;
}
return 0;
}
/*********************************************************************
*
* SEGGER_RTT_ConfigUpBuffer
*
* Function description
* Run-time configuration of a specific up-buffer (T->H).
* Buffer to be configured is specified by index.
* This includes: Buffer address, size, name, flags, ...
*
* Return value
* >= 0 O.K.
* < 0 Error
*/
int SEGGER_RTT_ConfigUpBuffer(unsigned BufferIndex, const char* sName, char* pBuffer, int BufferSize, int Flags) {
_Init();
if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumUpBuffers) {
SEGGER_RTT_LOCK();
if (BufferIndex > 0) {
_SEGGER_RTT.aUp[BufferIndex].sName = sName;
_SEGGER_RTT.aUp[BufferIndex].pBuffer = pBuffer;
_SEGGER_RTT.aUp[BufferIndex].SizeOfBuffer = BufferSize;
_SEGGER_RTT.aUp[BufferIndex].RdOff = 0;
_SEGGER_RTT.aUp[BufferIndex].WrOff = 0;
}
_SEGGER_RTT.aUp[BufferIndex].Flags = Flags;
SEGGER_RTT_UNLOCK();
return 0;
}
return -1;
}
/*********************************************************************
*
* SEGGER_RTT_ConfigDownBuffer
*
* Function description
* Run-time configuration of a specific down-buffer (H->T).
* Buffer to be configured is specified by index.
* This includes: Buffer address, size, name, flags, ...
*
* Return value
* >= 0 O.K.
* < 0 Error
*/
int SEGGER_RTT_ConfigDownBuffer(unsigned BufferIndex, const char* sName, char* pBuffer, int BufferSize, int Flags) {
_Init();
if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumDownBuffers) {
SEGGER_RTT_LOCK();
if (BufferIndex > 0) {
_SEGGER_RTT.aDown[BufferIndex].sName = sName;
_SEGGER_RTT.aDown[BufferIndex].pBuffer = pBuffer;
_SEGGER_RTT.aDown[BufferIndex].SizeOfBuffer = BufferSize;
_SEGGER_RTT.aDown[BufferIndex].RdOff = 0;
_SEGGER_RTT.aDown[BufferIndex].WrOff = 0;
}
_SEGGER_RTT.aDown[BufferIndex].Flags = Flags;
SEGGER_RTT_UNLOCK();
return 0;
}
return -1;
}
/*********************************************************************
*
* SEGGER_RTT_Init
*
* Function description
* Initializes the RTT Control Block.
* Should be used in RAM targets, at start of the application.
*
*/
void SEGGER_RTT_Init (void) {
_Init();
}
/*********************************************************************
*
* SEGGER_RTT_SetTerminal
*
* Function description
* Sets the terminal to be used for output on channel 0.
*
*/
void SEGGER_RTT_SetTerminal (char TerminalId) {
char ac[2];
ac[0] = 0xFF;
if (TerminalId < 10) {
ac[1] = '0' + TerminalId;
} else if (TerminalId < 16) {
ac[1] = 'A' + (TerminalId - 0x0A);
} else {
return; // RTT only supports up to 16 virtual terminals.
}
_ActiveTerminal = TerminalId;
SEGGER_RTT_Write(0, ac, 2);
}
/*********************************************************************
*
* SEGGER_RTT_TerminalOut
*
* Function description
* Writes a string to the given terminal
* without changing the terminal for channel 0.
*
*/
int SEGGER_RTT_TerminalOut (char TerminalId, const char* s) {
char ac[2];
int r;
ac[0] = 0xFF;
if (TerminalId < 10) {
ac[1] = '0' + TerminalId;
} else if (TerminalId < 16) {
ac[1] = 'A' + (TerminalId - 0x0A);
} else {
return -1; // RTT only supports up to 16 virtual terminals.
}
SEGGER_RTT_Write(0, ac, 2);
r = SEGGER_RTT_WriteString(0, s);
if (TerminalId < 10) {
ac[1] = '0' + _ActiveTerminal;
} else if (TerminalId < 16) {
ac[1] = 'A' + (_ActiveTerminal - 0x0A);
}
SEGGER_RTT_Write(0, ac, 2);
return r;
}
/*************************** End of file ****************************/
/* clang-format off */
/*********************************************************************
* SEGGER MICROCONTROLLER SYSTEME GmbH *
* Solutions for real time microcontroller applications *
**********************************************************************
* *
* (c) 1996-2014 SEGGER Microcontroller Systeme GmbH *
* *
* Internet: www.segger.com Support: support@segger.com *
* *
**********************************************************************
----------------------------------------------------------------------
File : SEGGER_RTT.h
Date : 17 Dec 2014
Purpose : Implementation of SEGGER real-time terminal which allows
real-time terminal communication on targets which support
debugger memory accesses while the CPU is running.
---------------------------END-OF-HEADER------------------------------
*/
/*********************************************************************
*
* Defines
*
**********************************************************************
*/
#define SEGGER_RTT_MODE_MASK (3 << 0)
#define SEGGER_RTT_MODE_NO_BLOCK_SKIP (0)
#define SEGGER_RTT_MODE_NO_BLOCK_TRIM (1 << 0)
#define SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL (1 << 1)
#define RTT_CTRL_RESET ""
#define RTT_CTRL_CLEAR ""
#define RTT_CTRL_TEXT_BLACK ""
#define RTT_CTRL_TEXT_RED ""
#define RTT_CTRL_TEXT_GREEN ""
#define RTT_CTRL_TEXT_YELLOW ""
#define RTT_CTRL_TEXT_BLUE ""
#define RTT_CTRL_TEXT_MAGENTA ""
#define RTT_CTRL_TEXT_CYAN ""
#define RTT_CTRL_TEXT_WHITE ""
#define RTT_CTRL_TEXT_BRIGHT_BLACK ""
#define RTT_CTRL_TEXT_BRIGHT_RED ""
#define RTT_CTRL_TEXT_BRIGHT_GREEN ""
#define RTT_CTRL_TEXT_BRIGHT_YELLOW ""
#define RTT_CTRL_TEXT_BRIGHT_BLUE ""
#define RTT_CTRL_TEXT_BRIGHT_MAGENTA ""
#define RTT_CTRL_TEXT_BRIGHT_CYAN ""
#define RTT_CTRL_TEXT_BRIGHT_WHITE ""
#define RTT_CTRL_BG_BLACK ""
#define RTT_CTRL_BG_RED ""
#define RTT_CTRL_BG_GREEN ""
#define RTT_CTRL_BG_YELLOW ""
#define RTT_CTRL_BG_BLUE ""
#define RTT_CTRL_BG_MAGENTA ""
#define RTT_CTRL_BG_CYAN ""
#define RTT_CTRL_BG_WHITE ""
#define RTT_CTRL_BG_BRIGHT_BLACK ""
#define RTT_CTRL_BG_BRIGHT_RED ""
#define RTT_CTRL_BG_BRIGHT_GREEN ""
#define RTT_CTRL_BG_BRIGHT_YELLOW ""
#define RTT_CTRL_BG_BRIGHT_BLUE ""
#define RTT_CTRL_BG_BRIGHT_MAGENTA ""
#define RTT_CTRL_BG_BRIGHT_CYAN ""
#define RTT_CTRL_BG_BRIGHT_WHITE ""
/*********************************************************************
*
* RTT API functions
*
**********************************************************************
*/
int SEGGER_RTT_Read (unsigned BufferIndex, char* pBuffer, unsigned BufferSize);
int SEGGER_RTT_Write (unsigned BufferIndex, const char* pBuffer, unsigned NumBytes);
int SEGGER_RTT_WriteString (unsigned BufferIndex, const char* s);
int SEGGER_RTT_GetKey (void);
int SEGGER_RTT_WaitKey (void);
int SEGGER_RTT_HasKey (void);
int SEGGER_RTT_ConfigUpBuffer (unsigned BufferIndex, const char* sName, char* pBuffer, int BufferSize, int Flags);
int SEGGER_RTT_ConfigDownBuffer (unsigned BufferIndex, const char* sName, char* pBuffer, int BufferSize, int Flags);
void SEGGER_RTT_Init (void);
/*********************************************************************
*
* RTT "Terminal" API functions
*
**********************************************************************
*/
void SEGGER_RTT_SetTerminal (char TerminalId);
int SEGGER_RTT_TerminalOut (char TerminalId, const char* s);
/*********************************************************************
*
* RTT printf functions (require SEGGER_RTT_printf.c)
*
**********************************************************************
*/
int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...);
/*************************** End of file ****************************/
/* clang-format off */
/*********************************************************************
* SEGGER MICROCONTROLLER SYSTEME GmbH *
* Solutions for real time microcontroller applications *
**********************************************************************
* *
* (c) 1996-2014 SEGGER Microcontroller Systeme GmbH *
* *
* Internet: www.segger.com Support: support@segger.com *
* *
**********************************************************************
----------------------------------------------------------------------
File : SEGGER_RTT_Conf.h
Date : 17 Dec 2014
Purpose : Implementation of SEGGER real-time terminal which allows
real-time terminal communication on targets which support
debugger memory accesses while the CPU is running.
---------------------------END-OF-HEADER------------------------------
*/
/*********************************************************************
*
* Defines, configurable
*
**********************************************************************
*/
#define SEGGER_RTT_MAX_NUM_UP_BUFFERS (2) // Max. number of up-buffers (T->H) available on this target (Default: 2)
#define SEGGER_RTT_MAX_NUM_DOWN_BUFFERS (2) // Max. number of down-buffers (H->T) available on this target (Default: 2)
#define BUFFER_SIZE_UP (2048) // Size of the buffer for terminal output of target, up to host (Default: 1k)
#define BUFFER_SIZE_DOWN (16) // Size of the buffer for terminal input to target from host (Usually keyboard input) (Default: 16)
#define SEGGER_RTT_PRINTF_BUFFER_SIZE (64) // Size of buffer for RTT printf to bulk-send chars via RTT (Default: 64)
//
// Target is not allowed to perform other RTT operations while string still has not been stored completely.
// Otherwise we would probably end up with a mixed string in the buffer.
// If using RTT from within interrupts, multiple tasks or multi processors, define the SEGGER_RTT_LOCK() and SEGGER_RTT_UNLOCK() function here.
//
#define SEGGER_RTT_LOCK()
#define SEGGER_RTT_UNLOCK()
//
// Define SEGGER_RTT_IN_RAM as 1
// when using RTT in RAM targets (init and data section both in RAM).
// This prevents the host to falsly identify the RTT Callback Structure
// in the init segment as the used Callback Structure.
//
// When defined as 1,
// the first call to an RTT function will modify the ID of the RTT Callback Structure.
// To speed up identifying on the host,
// especially when RTT functions are not called at the beginning of execution,
// SEGGER_RTT_Init() should be called at the start of the application.
//
#define SEGGER_RTT_IN_RAM (0)
/*************************** End of file ****************************/
/* clang-format off */
/*********************************************************************
* 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_printf.c
Date : 17 Dec 2014
Purpose : Replacement for printf to write formatted data via RTT
---------------------------END-OF-HEADER------------------------------
*/
#include "SEGGER_RTT.h"
#include "SEGGER_RTT_Conf.h"
/*********************************************************************
*
* Defines, configurable
*
**********************************************************************
*/
#ifndef SEGGER_RTT_PRINTF_BUFFER_SIZE
#define SEGGER_RTT_PRINTF_BUFFER_SIZE (64)
#endif
#include <stdlib.h>
#include <stdarg.h>
#define FORMAT_FLAG_LEFT_JUSTIFY (1 << 0)
#define FORMAT_FLAG_PAD_ZERO (1 << 1)
#define FORMAT_FLAG_PRINT_SIGN (1 << 2)
#define FORMAT_FLAG_ALTERNATE (1 << 3)
/*********************************************************************
*
* Types
*
**********************************************************************
*/
typedef struct {
char* pBuffer;
int BufferSize;
int Cnt;
int ReturnValue;
unsigned RTTBufferIndex;
} SEGGER_RTT_PRINTF_DESC;
/*********************************************************************
*
* Function prototypes
*
**********************************************************************
*/
int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList);
/*********************************************************************
*
* Static code
*
**********************************************************************
*/
/*********************************************************************
*
* _StoreChar
*/
static void _StoreChar(SEGGER_RTT_PRINTF_DESC * p, char c) {
int Cnt;
Cnt = p->Cnt;
if ((Cnt + 1) <= p->BufferSize) {
*(p->pBuffer + Cnt) = c;
p->Cnt = Cnt + 1;
p->ReturnValue++;
}
//
// Write part of string, when the buffer is full
//
if (p->Cnt == p->BufferSize) {
if (SEGGER_RTT_Write(p->RTTBufferIndex, p->pBuffer, p->Cnt) != p->Cnt) {
p->ReturnValue = -1;
} else {
p->Cnt = 0;
}
}
}
/*********************************************************************
*
* _PrintUnsigned
*/
static void _PrintUnsigned(SEGGER_RTT_PRINTF_DESC * pBufferDesc, unsigned v, unsigned Base, int NumDigits, unsigned FieldWidth, unsigned FormatFlags) {
static const char _aV2C[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
unsigned Div;
unsigned Digit = 1;
unsigned Number;
unsigned Width;
char c;
Number = v;
//
// Get actual field width
//
Width = 1;
while (Number >= Base) {
Number = (Number / Base);
Width++;
}
if ((unsigned)NumDigits > Width) {
Width = NumDigits;
}
//
// Print leading chars if necessary
//
if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0) {
if (FieldWidth != 0) {
if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && (NumDigits == 0)) {
c = '0';
} else {
c = ' ';
}
while ((FieldWidth != 0) && (Width < FieldWidth--)) {
_StoreChar(pBufferDesc, c);
if (pBufferDesc->ReturnValue < 0) {
return;
}
}
}
}
//
// Count how many digits are required by precision
//
while (((v / Digit) >= Base) | (NumDigits-- > 1)) {
Digit *= Base;
}
//
// Output digits
//
do {
Div = v / Digit;
v -= Div * Digit;
_StoreChar(pBufferDesc, _aV2C[Div]);
if (pBufferDesc->ReturnValue < 0) {
break;
}
Digit /= Base;
} while (Digit);
//
// Print trailing spaces if necessary
//
if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == FORMAT_FLAG_LEFT_JUSTIFY) {
if (FieldWidth != 0) {
while ((FieldWidth != 0) && (Width < FieldWidth--)) {
_StoreChar(pBufferDesc, ' ');
if (pBufferDesc->ReturnValue < 0) {
return;
}
}
}
}
}
/*********************************************************************
*
* _PrintInt
*/
static void _PrintInt(SEGGER_RTT_PRINTF_DESC * pBufferDesc, int v, unsigned Base, unsigned NumDigits, unsigned FieldWidth, unsigned FormatFlags) {
unsigned Width;
unsigned Number;
Number = (v < 0) ? -v : v;
//
// Get actual field width
//
Width = 1;
while (Number >= Base) {
Number = (Number / Base);
Width++;
}
if (NumDigits > Width) {
Width = NumDigits;
}
if ((FieldWidth > 0) && ((v < 0) || ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN))) {
FieldWidth--;
}
//
// Print leading spaces if necessary
//
if ((((FormatFlags & FORMAT_FLAG_PAD_ZERO) == 0) || (NumDigits != 0)) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0)) {
if (FieldWidth != 0) {
while ((FieldWidth != 0) && (Width < FieldWidth--)) {
_StoreChar(pBufferDesc, ' ');
if (pBufferDesc->ReturnValue < 0) {
return;
}
}
}
}
//
// Print sign if necessary
//
if (v < 0) {
v = -v;
_StoreChar(pBufferDesc, '-');
if (pBufferDesc->ReturnValue < 0) {
return;
}
} else if ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN) {
_StoreChar(pBufferDesc, '+');
if (pBufferDesc->ReturnValue < 0) {
return;
}
}
//
// Print leading zeros if necessary
//
if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0) && (NumDigits == 0)) {
if (FieldWidth != 0) {
while ((FieldWidth != 0) && (Width < FieldWidth--)) {
_StoreChar(pBufferDesc, '0');
if (pBufferDesc->ReturnValue < 0) {
return;
}
}
}
}
//
// Print number without sign
//
_PrintUnsigned(pBufferDesc, v, Base, NumDigits, FieldWidth, FormatFlags);
}
/*********************************************************************
*
* Public code
*
**********************************************************************
*/
/*********************************************************************
*
* SEGGER_RTT_vprintf
*
* Function description
* Stores a formatted string in SEGGER RTT control block.
* This data is read by the host.
*
* Parameters
* BufferIndex Index of "Up"-buffer to be used. (e.g. 0 for "Terminal")
* sFormat Pointer to format string
* pParamList Pointer to the list of arguments for the format string
*
* Return values
* >= 0: Number of bytes which have been stored in the "Up"-buffer.
* < 0: Error
*/
int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList) {
char c;
SEGGER_RTT_PRINTF_DESC BufferDesc;
int v;
unsigned NumDigits;
unsigned FormatFlags;
unsigned FieldWidth;
char acBuffer[SEGGER_RTT_PRINTF_BUFFER_SIZE];
BufferDesc.pBuffer = acBuffer;
BufferDesc.BufferSize = SEGGER_RTT_PRINTF_BUFFER_SIZE;
BufferDesc.Cnt = 0;
BufferDesc.RTTBufferIndex = BufferIndex;
BufferDesc.ReturnValue = 0;
do {
c = *sFormat++;
if (c == 0) {
break;
}
if (c == '%') {
//
// Filter out flags
//
FormatFlags = 0;
do {
c = *sFormat;
switch (c) {
case '-': FormatFlags |= FORMAT_FLAG_LEFT_JUSTIFY; sFormat++; break;
case '0': FormatFlags |= FORMAT_FLAG_PAD_ZERO; sFormat++; break;
case '+': FormatFlags |= FORMAT_FLAG_PRINT_SIGN; sFormat++; break;
case '#': FormatFlags |= FORMAT_FLAG_ALTERNATE; sFormat++; break;
default: goto FilterFieldWidth; break;
}
} while (1);
//
// filter out field with
//
FilterFieldWidth:
FieldWidth = 0;
do {
c = *sFormat;
if (c < '0' || c > '9') {
break;
}
sFormat++;
FieldWidth = FieldWidth * 10 + (c - '0');
} while (1);
//
// Filter out precision (number of digits to display)
//
NumDigits = 0;
c = *sFormat;
if (c == '.') {
sFormat++;
do {
c = *sFormat;
if (c < '0' || c > '9') {
break;
}
sFormat++;
NumDigits = NumDigits * 10 + (c - '0');
} while (1);
}
//
// Filter out length modifier
//
c = *sFormat;
do {
if (c == 'l' || c == 'h') {
c = *sFormat++;
continue;
}
break;
} while (1);
//
// Handle specifiers
//
switch (c) {
case 'c': {
char c0;
v = va_arg(*pParamList, int);
c0 = (char)v;
_StoreChar(&BufferDesc, c0);
break;
}
case 'd':
v = va_arg(*pParamList, int);
_PrintInt(&BufferDesc, v, 10, NumDigits, FieldWidth, FormatFlags);
break;
case 'u':
v = va_arg(*pParamList, int);
_PrintUnsigned(&BufferDesc, v, 10, NumDigits, FieldWidth, FormatFlags);
break;
case 'x':
case 'X':
v = va_arg(*pParamList, int);
_PrintUnsigned(&BufferDesc, v, 16, NumDigits, FieldWidth, FormatFlags);
break;
case 's':
{
const char * s = va_arg(*pParamList, const char *);
do {
c = *s++;
if (c == 0) {
break;
}
_StoreChar(&BufferDesc, c);
} while (BufferDesc.ReturnValue >= 0);
}
break;
case 'p':
v = va_arg(*pParamList, int);
_PrintUnsigned(&BufferDesc, v, 16, 8, 8, 0);
break;
case '%':
_StoreChar(&BufferDesc, '%');
break;
}
sFormat++;
} else {
_StoreChar(&BufferDesc, c);
}
} while (BufferDesc.ReturnValue >= 0);
if (BufferDesc.ReturnValue > 0) {
//
// Write remaining data, if any
//
if (BufferDesc.Cnt != 0) {
SEGGER_RTT_Write(BufferIndex, acBuffer, BufferDesc.Cnt);
}
BufferDesc.ReturnValue += BufferDesc.Cnt;
}
return BufferDesc.ReturnValue;
}
/*********************************************************************
*
* SEGGER_RTT_printf
*
* Function description
* Stores a formatted string in SEGGER RTT control block.
* This data is read by the host.
*
* Parameters
* BufferIndex Index of "Up"-buffer to be used. (e.g. 0 for "Terminal")
* sFormat Pointer to format string, followed by the arguments for conversion
*
* Return values
* >= 0: Number of bytes which have been stored in the "Up"-buffer.
* < 0: Error
*
* Notes
* (1) Conversion specifications have following syntax:
* %[flags][FieldWidth][.Precision]ConversionSpecifier
* (2) Supported flags:
* -: Left justify within the field width
* +: Always print sign extension for signed conversions
* 0: Pad with 0 instead of spaces. Ignored when using '-'-flag or precision
* Supported conversion specifiers:
* c: Print the argument as one char
* d: Print the argument as a signed integer
* u: Print the argument as an unsigned integer
* x: Print the argument as an hexadecimal integer
* s: Print the string pointed to by the argument
* p: Print the argument as an 8-digit hexadecimal integer. (Argument shall be a pointer to void.)
*/
int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...) {
va_list ParamList;
va_start(ParamList, sFormat);
return SEGGER_RTT_vprintf(BufferIndex, sFormat, &ParamList);
}
/*************************** End of file ****************************/
/* clang-format off */
/*********************************************************************
* SEGGER MICROCONTROLLER GmbH & Co KG *
* Solutions for real time microcontroller applications *
**********************************************************************
* *
* (c) 2014 SEGGER Microcontroller GmbH & Co KG *
* *
* www.segger.com Support: support@segger.com *
* *
**********************************************************************
----------------------------------------------------------------------
File : RTT_Syscalls.c
Purpose : Low-level functions for using printf() via RTT in GCC
-------- END-OF-HEADER ---------------------------------------------
*/
#include <stdlib.h>
#include "SEGGER_RTT.h"
/*********************************************************************
*
* Function prototypes
*
**********************************************************************
*/
int _write(int file, char *ptr, int len);
int _write_r(struct _reent *r, int file, char *ptr, int len);
/*********************************************************************
*
* Global functions
*
**********************************************************************
*/
/*********************************************************************
*
* _write()
*
* Function description
* Low-level write function.
* libc subroutines will use this system routine for output to all files,
* including stdout.
* Write data via RTT.
*/
int _write(int file, char *ptr, int len) {
(void) file; /* Not used, avoid warning */
SEGGER_RTT_Write(0, ptr, len);
return len;
}
/*********************************************************************
*
* _write_r()
*
* Function description
* Low-level reentrant write function.
* libc subroutines will use this system routine for output to all files,
* including stdout.
* Write data via RTT.
*/
int _write_r(struct _reent *r, int file, char *ptr, int len) {
(void) file; /* Not used, avoid warning */
(void) r; /* Not used, avoid warning */
SEGGER_RTT_Write(0, ptr, len);
return len;
}
/* clang-format off */
/*********************************************************************
* SEGGER MICROCONTROLLER GmbH & Co KG *
* Solutions for real time microcontroller applications *
**********************************************************************
* *
* (c) 2014 SEGGER Microcontroller GmbH & Co KG *
* *
* www.segger.com Support: support@segger.com *
* *
**********************************************************************
----------------------------------------------------------------------
File : RTT_Syscalls.c
Purpose : Low-level functions for using printf() via RTT in IAR
-------- END-OF-HEADER ---------------------------------------------
*/
#include <yfuns.h>
#include "SEGGER_RTT.h"
#pragma module_name = "?__write"
/*********************************************************************
*
* Function prototypes
*
**********************************************************************
*/
size_t __write(int handle, const unsigned char * buffer, size_t size);
/*********************************************************************
*
* Global functions
*
**********************************************************************
*/
/*********************************************************************
*
* __write()
*
* Function description
* Low-level write function.
* libc subroutines will use this system routine for output to all files,
* including stdout.
* Write data via RTT.
*/
size_t __write(int handle, const unsigned char * buffer, size_t size) {
(void) handle; /* Not used, avoid warning */
SEGGER_RTT_Write(0, buffer, size);
return size;
}
/****** End Of File *************************************************/
/* clang-format off */
/*********************************************************************
* SEGGER MICROCONTROLLER GmbH & Co KG *
* Solutions for real time microcontroller applications *
**********************************************************************
* *
* (c) 2014 SEGGER Microcontroller GmbH & Co KG *
* *
* www.segger.com Support: support@segger.com *
* *
**********************************************************************
----------------------------------------------------------------------
File : RTT_Syscalls_KEIL.c
Purpose : Retargeting module for KEIL MDK-CM3.
Low-level functions for using printf() via RTT
--------- END-OF-HEADER --------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rt_sys.h>
#include <rt_misc.h>
#include "SEGGER_RTT.h"
/*********************************************************************
*
* #pragmas
*
**********************************************************************
*/
#pragma import(__use_no_semihosting)
#ifdef _MICROLIB
#pragma import(__use_full_stdio)
#endif
/*********************************************************************
*
* Defines non-configurable
*
**********************************************************************
*/
/* Standard IO device handles - arbitrary, but any real file system handles must be
less than 0x8000. */
#define STDIN 0x8001 // Standard Input Stream
#define STDOUT 0x8002 // Standard Output Stream
#define STDERR 0x8003 // Standard Error Stream
/*********************************************************************
*
* Public const
*
**********************************************************************
*/
const char __stdin_name[] = "STDIN";
const char __stdout_name[] = "STDOUT";
const char __stderr_name[] = "STDERR";
/*********************************************************************
*
* Public code
*
**********************************************************************
*/
/*********************************************************************
*
* _ttywrch
*
* Function description:
* Outputs a character to the console
*
* Parameters:
* c - character to output
*
*/
void _ttywrch(int c) {
fputc(c, stdout); // stdout
fflush(stdout);
}
/*********************************************************************
*
* _sys_open
*
* Function description:
* Opens the device/file in order to do read/write operations
*
* Parameters:
* sName - sName of the device/file to open
* OpenMode - This parameter is currently ignored
*
* Return value:
* != 0 - Handle to the object to open, otherwise
* == 0 -"device" is not handled by this module
*
*/
FILEHANDLE _sys_open(const char * sName, int OpenMode) {
// Register standard Input Output devices.
if (strcmp(sName, __stdout_name) == 0) {
return (STDOUT);
} else if (strcmp(sName, __stderr_name) == 0) {
return (STDERR);
} else
return (0); // Not implemented
}
/*********************************************************************
*
* _sys_close
*
* Function description:
* Closes the handle to the open device/file
*
* Parameters:
* hFile - Handle to a file opened via _sys_open
*
* Return value:
* 0 - device/file closed
*
*/
int _sys_close(FILEHANDLE hFile) {
return 0; // Not implemented
}
/*********************************************************************
*
* _sys_write
*
* Function description:
* Writes the data to an open handle.
* Currently this function only outputs data to the console
*
* Parameters:
* hFile - Handle to a file opened via _sys_open
* pBuffer - Pointer to the data that shall be written
* NumBytes - Number of bytes to write
* Mode - The Mode that shall be used
*
* Return value:
* Number of bytes *not* written to the file/device
*
*/
int _sys_write(FILEHANDLE hFile, const unsigned char * pBuffer, unsigned NumBytes, int Mode) {
int r = 0;
if (hFile == STDOUT) {
return NumBytes - SEGGER_RTT_Write(0, (const char*)pBuffer, NumBytes);
}
return r;
}
/*********************************************************************
*
* _sys_read
*
* Function description:
* Reads data from an open handle.
* Currently this modules does nothing.
*
* Parameters:
* hFile - Handle to a file opened via _sys_open
* pBuffer - Pointer to buffer to store the read data
* NumBytes - Number of bytes to read
* Mode - The Mode that shall be used
*
* Return value:
* Number of bytes read from the file/device
*
*/
int _sys_read(FILEHANDLE hFile, unsigned char * pBuffer, unsigned NumBytes, int Mode) {
return (0); // Not implemented
}
/*********************************************************************
*
* _sys_istty
*
* Function description:
* This function shall return whether the opened file
* is a console device or not.
*
* Parameters:
* hFile - Handle to a file opened via _sys_open
*
* Return value:
* 1 - Device is a console
* 0 - Device is not a console
*
*/
int _sys_istty(FILEHANDLE hFile) {
if (hFile > 0x8000) {
return (1);
}
return (0); // Not implemented
}
/*********************************************************************
*
* _sys_seek
*
* Function description:
* Seeks via the file to a specific position
*
* Parameters:
* hFile - Handle to a file opened via _sys_open
* Pos -
*
* Return value:
* int -
*
*/
int _sys_seek(FILEHANDLE hFile, long Pos) {
return (0); // Not implemented
}
/*********************************************************************
*
* _sys_ensure
*
* Function description:
*
*
* Parameters:
* hFile - Handle to a file opened via _sys_open
*
* Return value:
* int -
*
*/
int _sys_ensure(FILEHANDLE hFile) {
return (-1); // Not implemented
}
/*********************************************************************
*
* _sys_flen
*
* Function description:
* Returns the length of the opened file handle
*
* Parameters:
* hFile - Handle to a file opened via _sys_open
*
* Return value:
* Length of the file
*
*/
long _sys_flen(FILEHANDLE hFile) {
return (0); // Not implemented
}
/*********************************************************************
*
* _sys_tmpnam
*
* Function description:
* This function converts the file number fileno for a temporary
* file to a unique filename, for example, tmp0001.
*
* Parameters:
* pBuffer - Pointer to a buffer to store the name
* FileNum - file number to convert
* MaxLen - Size of the buffer
*
* Return value:
* 1 - Error
* 0 - Success
*
*/
int _sys_tmpnam(char * pBuffer, int FileNum, unsigned MaxLen) {
return (1); // Not implemented
}
/*********************************************************************
*
* _sys_command_string
*
* Function description:
* This function shall execute a system command.
*
* Parameters:
* cmd - Pointer to the command string
* len - Length of the string
*
* Return value:
* == NULL - Command was not successfully executed
* == sCmd - Command was passed successfully
*
*/
char * _sys_command_string(char * cmd, int len) {
return cmd; // Not implemented
}
/*********************************************************************
*
* _sys_exit
*
* Function description:
* This function is called when the application returns from main
*
* Parameters:
* ReturnCode - Return code from the main function
*
*
*/
void _sys_exit(int ReturnCode) {
while (1); // Not implemented
}
/* clang-format off */
/********************************************************************* /*********************************************************************
* SEGGER MICROCONTROLLER GmbH & Co. KG * * SEGGER MICROCONTROLLER GmbH & Co. KG *
* Solutions for real time microcontroller applications * * Solutions for real time microcontroller applications *
......
/* clang-format off */
/********************************************************************* /*********************************************************************
* SEGGER MICROCONTROLLER SYSTEME GmbH * * SEGGER MICROCONTROLLER SYSTEME GmbH *
* Solutions for real time microcontroller applications * * Solutions for real time microcontroller applications *
......
/* clang-format off */
/********************************************************************* /*********************************************************************
* SEGGER MICROCONTROLLER SYSTEME GmbH * * SEGGER MICROCONTROLLER SYSTEME GmbH *
* Solutions for real time microcontroller applications * * Solutions for real time microcontroller applications *
......
/* clang-format off */
/********************************************************************* /*********************************************************************
* SEGGER MICROCONTROLLER GmbH & Co. KG * * SEGGER MICROCONTROLLER GmbH & Co. KG *
* Solutions for real time microcontroller applications * * Solutions for real time microcontroller applications *
......
/* clang-format off */
/********************************************************************* /*********************************************************************
* SEGGER MICROCONTROLLER GmbH & Co KG * * SEGGER MICROCONTROLLER GmbH & Co KG *
* Solutions for real time microcontroller applications * * Solutions for real time microcontroller applications *
......
/* clang-format off */
/********************************************************************* /*********************************************************************
* SEGGER MICROCONTROLLER GmbH & Co KG * * SEGGER MICROCONTROLLER GmbH & Co KG *
* Solutions for real time microcontroller applications * * Solutions for real time microcontroller applications *
......
/* clang-format off */
/********************************************************************* /*********************************************************************
* SEGGER MICROCONTROLLER GmbH & Co KG * * SEGGER MICROCONTROLLER GmbH & Co KG *
* Solutions for real time microcontroller applications * * Solutions for real time microcontroller applications *
......
...@@ -928,6 +928,7 @@ int inet_pton(int af, const char *src, void *dst); ...@@ -928,6 +928,7 @@ int inet_pton(int af, const char *src, void *dst);
#define to64(x) strtoll(x, NULL, 10) #define to64(x) strtoll(x, NULL, 10)
#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL #define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL
#define MG_LWIP 1
#define MG_ENABLE_IPV6 1 #define MG_ENABLE_IPV6 1
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment