Skip to content

[miniclient] SWO, TRNG for NRF52_DK only #5963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions platform/mbed_lib.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
{
"name": "platform",
"config": {
"stdio-enable": {
"help": "Enable stdin/stdout on serial",
"value": true
},
"stdio-convert-newlines": {
"help": "Enable conversion to standard newlines on stdin/stdout",
"value": false
},

"stdio-baud-rate": {
"help": "Baud rate for stdio",
"value": 9600
},

"stdio-flush-at-exit": {
"help": "Enable or disable the flush of standard I/O's at exit.",
"value": true
},

"default-serial-baud-rate": {
"help": "Default baud rate for a Serial or RawSerial instance (if not specified in the constructor)",
"value": 9600
},

"swo-enable": {
"help": "Enable stdout on SWO",
"value": false,
"macro_name": "ENABLE_SWO"
},
"force-non-copyable-error": {
"help": "Force compile time error when a NonCopyable object is copied",
"value": false
Expand All @@ -35,3 +40,4 @@
}
}
}

36 changes: 26 additions & 10 deletions platform/mbed_retarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,29 @@ void remove_filehandle(FileHandle *file) {
}
}

#if defined(DEVICE_ITM) && ENABLE_SWO
#define MACRO_ITM_PUTC ITM_SendChar
#else
#define MACRO_ITM_PUTC(X)
#endif

#if !MBED_CONF_PLATFORM_STDIO_ENABLE
#undef DEVICE_SERIAL
#endif

#if DEVICE_SERIAL
extern int stdio_uart_inited;
extern serial_t stdio_uart;
#define MACRO_SERIAL_PUTC serial_putc
#else
#define MACRO_SERIAL_PUTC(X, Y)
#endif

#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
#if DEVICE_SERIAL
static char stdio_in_prev;
static char stdio_out_prev;
#endif
static char stdio_out_prev;
#endif

static void init_serial() {
Expand Down Expand Up @@ -309,21 +325,23 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
#endif

if (fh < 3) {
#if DEVICE_SERIAL
if (!stdio_uart_inited) init_serial();
init_serial();

#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
for (unsigned int i = 0; i < length; i++) {
if (buffer[i] == '\n' && stdio_out_prev != '\r') {
serial_putc(&stdio_uart, '\r');
MACRO_ITM_PUTC('\r');
MACRO_SERIAL_PUTC(&stdio_uart, '\r');
}
serial_putc(&stdio_uart, buffer[i]);
MACRO_ITM_PUTC(buffer[i]);
MACRO_SERIAL_PUTC(&stdio_uart, buffer[i]);
stdio_out_prev = buffer[i];
}
#else
for (unsigned int i = 0; i < length; i++) {
serial_putc(&stdio_uart, buffer[i]);
MACRO_ITM_PUTC(buffer[i]);
MACRO_SERIAL_PUTC(&stdio_uart, buffer[i]);
}
#endif
#endif
n = length;
} else {
Expand Down Expand Up @@ -351,9 +369,7 @@ extern "C" void PREFIX(_exit)(int return_code) {
}

extern "C" void _ttywrch(int ch) {
#if DEVICE_SERIAL
serial_putc(&stdio_uart, ch);
#endif
MACRO_SERIAL_PUTC(&stdio_uart, ch);
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export symbol __ICFEDIT_region_RAM_end__;
/*-Sizes-*/
/*Heap 1/4 of ram and stack 1/8*/
define symbol __ICFEDIT_size_cstack__ = 0x800;
define symbol __ICFEDIT_size_heap__ = 0x1800;
define symbol __ICFEDIT_size_heap__ = 0x3000;
/**** End of ICF editor section. ###ICF###*/

define symbol __code_start_soft_device__ = 0x0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,36 @@ void SystemCoreClockUpdate(void)
SystemCoreClock = __SYSTEM_CLOCK_64M;
}

#if defined(ENABLE_SWO)
void InitSwoOutput(void)
{
uint32_t StimulusRegs;
static const uint32_t _ITMPort = 0;

// Enable access to SWO registers
ITM->LAR = 0xC5ACCE55;

// Initially disable ITM and stimulus port
// To make sure that nothing is transferred via SWO
// when changing the SWO prescaler etc.
StimulusRegs = ITM->TER;
StimulusRegs &= ~(1 << _ITMPort);
ITM->TER = StimulusRegs; // Disable ITM stimulus port
ITM->TCR = 0; // Disable ITM

// Initialize SWO (prescaler, etc.)
TPI->SPPR = 0x00000002; // Select NRZ mode
TPI->ACPR = 0; // prescaler
ITM->TPR = 0x00000000;
DWT->CTRL = 0x400003FE;
TPI->FFCR = 0x00000100;

// Enable ITM and stimulus port
ITM->TCR = 0x1000D; // Enable ITM
ITM->TER = StimulusRegs | (1 << _ITMPort); // Enable ITM stimulus port
}
#endif // #if defined(ENABLE_SWO)

void SystemInit(void)
{
/* Workaround for Errata 16 "System: RAM may be corrupt on wakeup from CPU IDLE" found at the Errata document
Expand Down Expand Up @@ -181,7 +211,13 @@ void SystemInit(void)
#if defined (ENABLE_SWO)
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
NRF_CLOCK->TRACECONFIG |= CLOCK_TRACECONFIG_TRACEMUX_Serial << CLOCK_TRACECONFIG_TRACEMUX_Pos;
/* set SWO clock speed to 4 MHz */
NRF_CLOCK->TRACECONFIG = (NRF_CLOCK->TRACECONFIG & ~CLOCK_TRACECONFIG_TRACEPORTSPEED_Msk) | (CLOCK_TRACECONFIG_TRACEPORTSPEED_4MHz << CLOCK_TRACECONFIG_TRACEPORTSPEED_Pos);

NRF_P0->PIN_CNF[18] = (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);

// Initialize SWO output
InitSwoOutput();
#endif

/* Enable Trace functionality. If ENABLE_TRACE is not defined, TRACE pins will be used as GPIOs (see Product
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
#define NRF_MAXIMUM_LATENCY_US 2000

/* RNG */
#define RNG_ENABLED 0
#define RNG_ENABLED 1

#if (RNG_ENABLED == 1)
#define RNG_CONFIG_ERROR_CORRECTION true
Expand Down
Loading