diff --git a/ARM.CMSIS-Compiler.pdsc b/ARM.CMSIS-Compiler.pdsc index c1346f8..04f78c6 100644 --- a/ARM.CMSIS-Compiler.pdsc +++ b/ARM.CMSIS-Compiler.pdsc @@ -15,6 +15,7 @@ Active Development ... + Add components that enable stdio retarget to UART AC6: - Corrected _sys_read return value when rt_fs_read returns with error (#62) IAR: @@ -108,6 +109,11 @@ + + CMSIS-Compiler CORE component and CMSIS-Driver USART + + + ARMCC and CORE component @@ -155,6 +161,13 @@ + + + STDIO retarget to CMSIS Driver UART + + + + Compiler Specific Interfaces @@ -215,28 +228,36 @@ - + Standard C Library Retarget Core - + + + - + Standard C Library Retarget Core - + + + - + Standard C Library Retarget Core - + + + - + Standard C Library Retarget Core - + + + @@ -259,7 +280,7 @@ #define RTE_CMSIS_Compiler_File_Interface_Breakpoint /* Compiler File Interface: Breakpoint */ - + @@ -281,7 +302,7 @@ #define RTE_CMSIS_Compiler_STDERR_Breakpoint /* CMSIS-Compiler STDERR: Breakpoint */ - + @@ -291,7 +312,7 @@ #define RTE_CMSIS_Compiler_STDERR_Event_Recorder /* CMSIS-Compiler STDERR: Event Recorder */ - + @@ -301,7 +322,17 @@ #define RTE_CMSIS_Compiler_STDERR_ITM /* CMSIS-Compiler STDERR: ITM */ - + + + + + Redirect STDERR to a CMSIS-Driver USART + + #define RTE_CMSIS_Compiler_STDERR /* CMSIS-Compiler STDERR */ + #define RTE_CMSIS_Compiler_STDERR_UART_CMSIS /* CMSIS-Compiler STDERR: UART: CMSIS */ + + + @@ -323,7 +354,7 @@ #define RTE_CMSIS_Compiler_STDIN_Breakpoint /* CMSIS-Compiler STDIN: Breakpoint */ - + @@ -333,7 +364,17 @@ #define RTE_CMSIS_Compiler_STDIN_ITM /* CMSIS-Compiler STDIN: ITM */ - + + + + + Retrieve STDIN from a CMSIS-Driver USART + + #define RTE_CMSIS_Compiler_STDIN /* CMSIS-Compiler STDIN */ + #define RTE_CMSIS_Compiler_STDIN_UART_CMSIS /* CMSIS-Compiler STDIN: UART: CMSIS */ + + + @@ -355,7 +396,7 @@ #define RTE_CMSIS_Compiler_STDOUT_Breakpoint /* CMSIS-Compiler STDOUT: Breakpoint */ - + @@ -365,7 +406,7 @@ #define RTE_CMSIS_Compiler_STDOUT_Event_Recorder /* CMSIS-Compiler STDOUT: Event Recorder */ - + @@ -375,7 +416,17 @@ #define RTE_CMSIS_Compiler_STDOUT_ITM /* CMSIS-Compiler STDOUT: ITM */ - + + + + + Redirect STDOUT to a CMSIS-Driver USART + + #define RTE_CMSIS_Compiler_STDOUT /* CMSIS-Compiler STDOUT */ + #define RTE_CMSIS_Compiler_STDOUT_UART_CMSIS /* CMSIS-Compiler STDOUT: UART: CMSIS */ + + + @@ -397,7 +448,7 @@ #define RTE_CMSIS_Compiler_TTY_Breakpoint /* CMSIS-Compiler TTY: Breakpoint */ - + @@ -407,7 +458,7 @@ #define RTE_CMSIS_Compiler_TTY_ITM /* CMSIS-Compiler TTY: ITM */ - + @@ -443,7 +494,7 @@ #define RTE_CMSIS_Compiler_OS_Interface_RTOS2_LIBSPACE /* Implements per-thread static data management */ - + @@ -455,7 +506,7 @@ #define RTE_CMSIS_Compiler_OS_Interface_RTOS2_LOCKS /* Implements locking routines */ - + @@ -467,7 +518,7 @@ #define RTE_CMSIS_Compiler_OS_Interface_RTOS2_LOCKS /* Implements locking routines */ - + diff --git a/config/stderr_cmsis_uart_config.h b/config/stderr_cmsis_uart_config.h new file mode 100644 index 0000000..192648e --- /dev/null +++ b/config/stderr_cmsis_uart_config.h @@ -0,0 +1,59 @@ +/*--------------------------------------------------------------------------- + * Copyright (c) 2026 Arm Limited (or its affiliates). + * All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -------------------------------------------------------------------------- + * + * Purpose: Configure STDERR to CMSIS-Driver USART retarget implementation + * Revision: V1.0.0 + * + * --------------------------------------------------------------------------*/ + +#ifndef STDERR_CMSIS_UART_CONFIG_H_ +#define STDERR_CMSIS_UART_CONFIG_H_ + +//-------- <<< Use Configuration Wizard in Context Menu >>> -------------------- +//------ With VS Code: Open Preview for Configuration Wizard ------------------- + +// UART STDERR Buffer Size +// UART STDERR buffer is used to store STDERR data before sending it via UART +// Default: 0 (unbuffered) +#ifndef STDERR_UART_BUFFER_SIZE +#define STDERR_UART_BUFFER_SIZE 0 +#endif + +// UART STDERR Buffer Section Name +// Specify the memory section name for UART STDERR buffer +// Default: .stderr_uart_buf +#ifndef STDERR_UART_BUFFER_SECTION +#define STDERR_UART_BUFFER_SECTION ".stderr_uart_buf" +#endif + +// UART STDERR Buffer Alignment +// Define the STDERR buffer alignment in bytes +// Default: 32 +#ifndef STDERR_UART_BUFFER_ALIGNMENT +#define STDERR_UART_BUFFER_ALIGNMENT 32 +#endif + +// Convert LF to CRLF +// If enabled, LF characters are preceded by CR characters on output +// Default: 0 +#ifndef STDERR_CR_LF +#define STDERR_CR_LF 0 +#endif + +#endif /* STDERR_CMSIS_UART_CONFIG_H_ */ diff --git a/config/stdin_cmsis_uart_config.h b/config/stdin_cmsis_uart_config.h new file mode 100644 index 0000000..bce0ba2 --- /dev/null +++ b/config/stdin_cmsis_uart_config.h @@ -0,0 +1,59 @@ +/*--------------------------------------------------------------------------- + * Copyright (c) 2026 Arm Limited (or its affiliates). + * All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -------------------------------------------------------------------------- + * + * Purpose: Configure STDIN to CMSIS-Driver USART retarget implementation + * Revision: V1.0.0 + * + * --------------------------------------------------------------------------*/ + +#ifndef STDIN_CMSIS_UART_CONFIG_H_ +#define STDIN_CMSIS_UART_CONFIG_H_ + +//-------- <<< Use Configuration Wizard in Context Menu >>> -------------------- +//------ With VS Code: Open Preview for Configuration Wizard ------------------- + +// UART STDIN Buffer Size +// UART STDIN buffer is used to store STDIN data received via UART +// Default: 64 +#ifndef STDIN_UART_BUFFER_SIZE +#define STDIN_UART_BUFFER_SIZE 64 +#endif + +// UART STDIN Buffer Section Name +// Specify the memory section name for UART STDIN buffer +// Default: .stdin_uart_buf +#ifndef STDIN_UART_BUFFER_SECTION +#define STDIN_UART_BUFFER_SECTION ".stdin_uart_buf" +#endif + +// UART STDIN Buffer Alignment +// Define the STDIN buffer alignment in bytes +// Default: 32 +#ifndef STDIN_UART_BUFFER_ALIGNMENT +#define STDIN_UART_BUFFER_ALIGNMENT 32 +#endif + +// Echo Input Characters +// If enabled, received characters are echoed to STDOUT +// Default: 0 +#ifndef STDIN_ECHO +#define STDIN_ECHO 0 +#endif + +#endif /* STDIN_CMSIS_UART_CONFIG_H_ */ diff --git a/config/stdio_cmsis_uart_config.h b/config/stdio_cmsis_uart_config.h new file mode 100644 index 0000000..aeeca61 --- /dev/null +++ b/config/stdio_cmsis_uart_config.h @@ -0,0 +1,52 @@ +/*--------------------------------------------------------------------------- + * Copyright (c) 2026 Arm Limited (or its affiliates). + * All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -------------------------------------------------------------------------- + * + * Purpose: STDIO to CMSIS Driver USART Retarget Configuration + * Revision: V1.0.0 + * + * --------------------------------------------------------------------------*/ + +#ifndef STDIO_CMSIS_UART_CONFIG_H_ +#define STDIO_CMSIS_UART_CONFIG_H_ + +#ifdef CMSIS_target_header +#include CMSIS_target_header +#else +#define RETARGET_STDIO_UART 0 +#endif + +//-------- <<< Use Configuration Wizard in Context Menu >>> -------------------- +//------ With VS Code: Open Preview for Configuration Wizard ------------------- + +// Connect to hardware via Driver_USART# +// Select driver control block for hardware interface +// Default: 0 +#ifndef STDIO_UART_DRIVER +#define STDIO_UART_DRIVER RETARGET_STDIO_UART +#endif + +// UART baudrate +// Specifies the baudrate for UART communication +// Common values: 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600 +// Default: 115200 +#ifndef STDIO_UART_BAUDRATE +#define STDIO_UART_BAUDRATE 115200 +#endif + +#endif /* STDIO_CMSIS_UART_CONFIG_H_ */ diff --git a/config/stdout_cmsis_uart_config.h b/config/stdout_cmsis_uart_config.h new file mode 100644 index 0000000..323d217 --- /dev/null +++ b/config/stdout_cmsis_uart_config.h @@ -0,0 +1,59 @@ +/*--------------------------------------------------------------------------- + * Copyright (c) 2026 Arm Limited (or its affiliates). + * All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -------------------------------------------------------------------------- + * + * Purpose: Configure STDOUT to CMSIS-Driver USART retarget implementation + * Revision: V1.0.0 + * + * --------------------------------------------------------------------------*/ + +#ifndef STDOUT_CMSIS_UART_CONFIG_H_ +#define STDOUT_CMSIS_UART_CONFIG_H_ + +//-------- <<< Use Configuration Wizard in Context Menu >>> -------------------- +//------ With VS Code: Open Preview for Configuration Wizard ------------------- + +// UART STDOUT Buffer Size +// UART STDOUT buffer is used to store STDOUT data before sending it via UART +// Default: 0 (unbuffered) +#ifndef STDOUT_UART_BUFFER_SIZE +#define STDOUT_UART_BUFFER_SIZE 0 +#endif + +// UART STDOUT Buffer Section Name +// Specify the memory section name for UART STDOUT buffer +// Default: .stdout_uart_buf +#ifndef STDOUT_UART_BUFFER_SECTION +#define STDOUT_UART_BUFFER_SECTION ".stdout_uart_buf" +#endif + +// UART STDOUT Buffer Alignment +// Define the buffer alignment in bytes +// Default: 32 +#ifndef STDOUT_UART_BUFFER_ALIGNMENT +#define STDOUT_UART_BUFFER_ALIGNMENT 32 +#endif + +// Convert LF to CRLF +// If enabled, LF characters are preceded by CR characters on output +// Default: 0 +#ifndef STDOUT_CR_LF +#define STDOUT_CR_LF 0 +#endif + +#endif /* STDOUT_CMSIS_UART_CONFIG_H_ */ diff --git a/documentation/doxygen/src/images/blocks.pptx b/documentation/doxygen/src/images/blocks.pptx index d1a36fa..c005bd3 100644 Binary files a/documentation/doxygen/src/images/blocks.pptx and b/documentation/doxygen/src/images/blocks.pptx differ diff --git a/documentation/doxygen/src/images/retarget_io_std.png b/documentation/doxygen/src/images/retarget_io_std.png index 9e8a071..1328245 100644 Binary files a/documentation/doxygen/src/images/retarget_io_std.png and b/documentation/doxygen/src/images/retarget_io_std.png differ diff --git a/documentation/doxygen/src/rt_io.md b/documentation/doxygen/src/rt_io.md index 841cc61..8d62328 100644 --- a/documentation/doxygen/src/rt_io.md +++ b/documentation/doxygen/src/rt_io.md @@ -39,7 +39,8 @@ The following subcomponents are available: **Breakpoint** | When the I/O stream is used, the application stops with [BKPT](https://developer.arm.com/documentation/100073/latest/The-Arm-C-and-C---Libraries/Support-for-building-an-application-with-the-C-library/Using-the-C-and-C---libraries-with-an-application-in-a-semihosting-environment?lang=en) instruction. No additional code is required. **Event Recorder** | STDOUT and STDERR can be redirected using the [Event Recorder](https://arm-software.github.io/CMSIS-View/main/evr.html) (especially interesting for targets without ITM (such as Cortex-M0/M0+/M23)). **ITM** | Use [Instrumentation Trace Macrocell (ITM)](https://developer.arm.com/documentation/ddi0314/h/Instrumentation-Trace-Macrocell?lang=en) for I/O communication via the debugger (only available for Cortex-M3/M4/M7/M33/M55/M85 processors).
Usually, data is shown in a dedicated window.
No additional code is required to output or input data through the ITM channel. However, you have to configure the ITM channel for tracing. -**Custom** | Retarget I/O stream to a user defined interface (such as UART or other application specific interface).
See \subpage custom_subcomponent for further details. +**UART** | Retarget I/O streams to UART. Default implementation variant uses CMSIS-Driver USART. +**Custom** | Retarget I/O stream to a user defined interface (such as custom UART implementation or other application specific interface).
See \subpage custom_subcomponent for further details. > **Note** diff --git a/gen_pack.sh b/gen_pack.sh index 97bf999..5edf691 100755 --- a/gen_pack.sh +++ b/gen_pack.sh @@ -29,6 +29,7 @@ DEFAULT_ARGS=(-c "v") # Default: empty (all folders) # PACK_DIRS=" + config documentation example include diff --git a/source/armcc/retarget_io.c b/source/core/armcc/retarget_io.c similarity index 97% rename from source/armcc/retarget_io.c rename to source/core/armcc/retarget_io.c index 0332232..75db9af 100644 --- a/source/armcc/retarget_io.c +++ b/source/core/armcc/retarget_io.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2025 ARM Limited or its affiliates. All rights reserved. + * Copyright (C) 2023-2026 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * @@ -32,14 +32,23 @@ #ifdef RTE_CMSIS_Compiler_STDERR #include "retarget_stderr.h" +#ifdef RTE_CMSIS_Compiler_STDERR_UART_CMSIS +#include "stderr_cmsis_uart_config.h" +#endif #endif #ifdef RTE_CMSIS_Compiler_STDIN #include "retarget_stdin.h" +#ifdef RTE_CMSIS_Compiler_STDIN_UART_CMSIS +#include "stdin_cmsis_uart_config.h" +#endif #endif #ifdef RTE_CMSIS_Compiler_STDOUT #include "retarget_stdout.h" +#ifdef RTE_CMSIS_Compiler_STDOUT_UART_CMSIS +#include "stdout_cmsis_uart_config.h" +#endif #endif #ifdef RTE_CMSIS_Compiler_TTY diff --git a/source/clang/retarget_syscalls.c b/source/core/clang/retarget_syscalls.c similarity index 100% rename from source/clang/retarget_syscalls.c rename to source/core/clang/retarget_syscalls.c diff --git a/source/gcc/retarget_syscalls.c b/source/core/gcc/retarget_syscalls.c similarity index 97% rename from source/gcc/retarget_syscalls.c rename to source/core/gcc/retarget_syscalls.c index 13a7b0e..516c8d6 100644 --- a/source/gcc/retarget_syscalls.c +++ b/source/core/gcc/retarget_syscalls.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2024 ARM Limited or its affiliates. All rights reserved. + * Copyright (C) 2023-2026 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * @@ -35,14 +35,23 @@ #ifdef RTE_CMSIS_Compiler_STDERR #include "retarget_stderr.h" +#ifdef RTE_CMSIS_Compiler_STDERR_UART_CMSIS +#include "stderr_cmsis_uart_config.h" +#endif #endif #ifdef RTE_CMSIS_Compiler_STDIN #include "retarget_stdin.h" +#ifdef RTE_CMSIS_Compiler_STDIN_UART_CMSIS +#include "stdin_cmsis_uart_config.h" +#endif #endif #ifdef RTE_CMSIS_Compiler_STDOUT #include "retarget_stdout.h" +#ifdef RTE_CMSIS_Compiler_STDOUT_UART_CMSIS +#include "stdout_cmsis_uart_config.h" +#endif #endif #ifndef STDIN_ECHO diff --git a/source/iar/retarget_io.c b/source/core/iar/retarget_io.c similarity index 100% rename from source/iar/retarget_io.c rename to source/core/iar/retarget_io.c diff --git a/source/core/stdio_cmsis_uart.c b/source/core/stdio_cmsis_uart.c new file mode 100644 index 0000000..7f4780e --- /dev/null +++ b/source/core/stdio_cmsis_uart.c @@ -0,0 +1,313 @@ +/*--------------------------------------------------------------------------- + * Copyright (c) 2026 Arm Limited (or its affiliates). + * All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *---------------------------------------------------------------------------*/ + +#include +#include "cmsis_compiler.h" + +#include "RTE_Components.h" + +#ifdef RTE_CMSIS_Compiler_STDERR_UART_CMSIS +#include "retarget_stderr.h" +#include "stderr_cmsis_uart_config.h" +#endif +#ifdef RTE_CMSIS_Compiler_STDIN_UART_CMSIS +#include "retarget_stdin.h" +#include "stdin_cmsis_uart_config.h" +#endif +#ifdef RTE_CMSIS_Compiler_STDOUT_UART_CMSIS +#include "retarget_stdout.h" +#include "stdout_cmsis_uart_config.h" +#endif + +#include "stdio_cmsis_uart_config.h" + +/* Global initialization function */ +extern int stdio_cmsis_uart_init (void); + +/* Reference to the underlying USART driver */ +#define ptrUSART (&ARM_Driver_USART_(STDIO_UART_DRIVER)) + +/* Global Initialization Flag */ +static uint8_t Initialized = 0U; + +/* UART Error Flags */ +#define UART_FLAG_RX_ERROR (0x01U) +#define UART_FLAG_TX_ERROR (0x02U) + +/* UART Input Buffer Structure */ +typedef struct { + uint8_t *data; /* Pointer to data buffer */ + uint32_t size; /* Size of data buffer */ + volatile uint32_t cnt; /* Number of characters transferred */ + volatile uint32_t idx; /* Buffer index */ + volatile uint32_t flags; /* Error flags */ +} UART_InBuffer; + +/* UART Output Buffer Structure */ +typedef struct { + uint8_t *data; /* Pointer to data buffer */ + uint32_t size; /* Size of data buffer */ + volatile uint32_t idx; /* Buffer index */ + volatile uint32_t flags; /* Error flags */ +} UART_OutBuffer; + +#if defined(RTE_CMSIS_Compiler_STDERR_UART_CMSIS) +/* STDERR Buffer Attribute */ +#define STDERR_UART_BUFFER_ATTRIBUTE __attribute__((section(STDERR_UART_BUFFER_SECTION), aligned(STDERR_UART_BUFFER_ALIGNMENT))) +/* STDERR Buffer Array */ +static uint8_t StdErrBuf[(STDERR_UART_BUFFER_SIZE == 0) ? 1 : STDERR_UART_BUFFER_SIZE] STDERR_UART_BUFFER_ATTRIBUTE; +/* STDERR Buffer Object */ +static UART_OutBuffer UART_StdErrBuf = { StdErrBuf, sizeof(StdErrBuf), 0, 0 }; +#endif + +#if defined(RTE_CMSIS_Compiler_STDIN_UART_CMSIS) +/* STDIN Buffer Attribute */ +#define STDIN_UART_BUFFER_ATTRIBUTE __attribute__((section(STDIN_UART_BUFFER_SECTION), aligned(STDIN_UART_BUFFER_ALIGNMENT))) +/* STDIN Buffer Array */ +static uint8_t StdInBuf[(STDIN_UART_BUFFER_SIZE == 0) ? 1 : STDIN_UART_BUFFER_SIZE] STDIN_UART_BUFFER_ATTRIBUTE; +/* STDIN Buffer Object */ +static UART_InBuffer UART_StdInBuf = { StdInBuf, sizeof(StdInBuf), 0, 0, 0 }; +#endif + +#if defined(RTE_CMSIS_Compiler_STDOUT_UART_CMSIS) +/* STDOUT Buffer Attribute */ +#define STDOUT_UART_BUFFER_ATTRIBUTE __attribute__((section(STDOUT_UART_BUFFER_SECTION), aligned(STDOUT_UART_BUFFER_ALIGNMENT))) +/* STDOUT Buffer Array */ +static uint8_t StdOutBuf[(STDOUT_UART_BUFFER_SIZE == 0) ? 1 : STDOUT_UART_BUFFER_SIZE] STDOUT_UART_BUFFER_ATTRIBUTE; +/* STDOUT Buffer Object */ +static UART_OutBuffer UART_StdOutBuf = { StdOutBuf, sizeof(StdOutBuf), 0, 0 }; +#endif + +/** + UART driver event callback + + \param event ARM_USART_EVENT_* notification mask + */ +void UART_Event_Callback(uint32_t event) { +#if defined(RTE_CMSIS_Compiler_STDIN_UART_CMSIS) + int32_t status; + + if (event & ARM_USART_EVENT_RECEIVE_COMPLETE) { + /* Increment number of received characters */ + UART_StdInBuf.cnt += UART_StdInBuf.size; + + /* Initiate new receive */ + status = ptrUSART->Receive(&UART_StdInBuf.data[0], UART_StdInBuf.size); + + if (status != ARM_DRIVER_OK) { + /* Set error flag */ + UART_StdInBuf.flags |= UART_FLAG_RX_ERROR; + } + } +#endif +} + +/* + Initialize stdio +*/ +__WEAK +int stdio_init (void) { + return stdio_cmsis_uart_init(); +} + +/** + Initialize stdio retarget to CMSIS-Driver UART + + \return 0 on success, or -1 on error. +*/ +int stdio_cmsis_uart_init (void) { + int rval; + + if (Initialized != 0U) { + /* Already initialized */ + return 0; + } + + if (ptrUSART->Initialize(UART_Event_Callback) != ARM_DRIVER_OK) { + /* CMSIS-Driver USART initialize failed */ + rval = -1; + } + else if (ptrUSART->PowerControl(ARM_POWER_FULL) != ARM_DRIVER_OK) { + /* CMSIS-Driver USART power full failed */ + rval = -2; + } + else if (ptrUSART->Control(ARM_USART_MODE_ASYNCHRONOUS | + ARM_USART_DATA_BITS_8 | + ARM_USART_PARITY_NONE | + ARM_USART_STOP_BITS_1 | + ARM_USART_FLOW_CONTROL_NONE, + STDIO_UART_BAUDRATE) != ARM_DRIVER_OK) { + /* CMSIS-Driver USART mode configuration failed */ + rval = -3; + } +#if defined(RTE_CMSIS_Compiler_STDERR_UART_CMSIS) || defined(RTE_CMSIS_Compiler_STDOUT_UART_CMSIS) + else if (ptrUSART->Control(ARM_USART_CONTROL_TX, 1U) != ARM_DRIVER_OK) { + /* CMSIS-Driver USART transmitter enable failed */ + rval = -4; + } +#endif +#if defined(RTE_CMSIS_Compiler_STDIN_UART_CMSIS) + else if (ptrUSART->Control(ARM_USART_CONTROL_RX, 1U) != ARM_DRIVER_OK) { + /* CMSIS-Driver USART receiver enable failed */ + rval = -5; + } + else if (ptrUSART->Receive(&UART_StdInBuf.data[0], UART_StdInBuf.size) != ARM_DRIVER_OK) { + /* CMSIS-Driver USART receive operation failed */ + rval = -6; + } +#endif + else { + /* CMSIS-Driver USART setup completed successfully */ + rval = 0; + + /* Set global initialized flag */ + Initialized = 1U; + } + + return rval; +} + +#if defined(RTE_CMSIS_Compiler_STDERR_UART_CMSIS) +/** + Put a character to the stderr + + \param[in] ch Character to output + \return The character written, or -1 on write error. +*/ +int stderr_putchar (int ch) { + int32_t status; + + if (Initialized == 0U) { + /* Initialize stdio if not already initialized */ + if (stdio_cmsis_uart_init() != 0) { + return -1; + } + } + + /* Write character at current index */ + UART_StdErrBuf.data[UART_StdErrBuf.idx] = (uint8_t)ch; + + /* Increment number of characters written */ + UART_StdErrBuf.idx++; + + /* Send when the buffer is full or on newline character */ + if ((UART_StdErrBuf.idx == UART_StdErrBuf.size) || (ch == '\n')) { + /* Send buffered characters */ + status = ptrUSART->Send(&UART_StdErrBuf.data[0], UART_StdErrBuf.idx); + + if (status != ARM_DRIVER_OK) { + /* Set error flag */ + UART_StdErrBuf.flags |= UART_FLAG_TX_ERROR; + } + + /* Wait for send to complete */ + while (ptrUSART->GetStatus().tx_busy != 0U); + + /* Reset transmit index */ + UART_StdErrBuf.idx = 0U; + } + + return ch; +} +#endif + +#if defined(RTE_CMSIS_Compiler_STDIN_UART_CMSIS) +/** + Get a character from the stdio + + \return The next character from the input, or -1 on read error. +*/ +int stdin_getchar (void) { + uint32_t rx_num; + uint32_t rx_cnt; + uint32_t rx_idx; + int ch = -1; + + if (Initialized == 0U) { + /* Initialize stdio if not already initialized */ + if (stdio_cmsis_uart_init() != 0) { + return -1; + } + } + + while (ch == -1) { + /* Get the number of received characters since last Receive operation */ + rx_num = ptrUSART->GetRxCount(); + + /* Determine total number of received characters */ + rx_cnt = UART_StdInBuf.cnt + rx_num; + + if (UART_StdInBuf.idx < rx_cnt) { + /* There are characters available to read */ + rx_idx = UART_StdInBuf.idx % UART_StdInBuf.size; + /* Read character at current index */ + ch = UART_StdInBuf.data[rx_idx]; + + /* Increment number of characters read */ + UART_StdInBuf.idx++; + } + } + + return ch; +} +#endif + +#if defined(RTE_CMSIS_Compiler_STDOUT_UART_CMSIS) +/** + Put a character to the stdout + + \param[in] ch Character to output + \return The character written, or -1 on write error. +*/ +int stdout_putchar (int ch) { + int32_t status; + + if (Initialized == 0U) { + /* Initialize stdio if not already initialized */ + if (stdio_cmsis_uart_init() != 0) { + return -1; + } + } + + /* Write character at current index */ + UART_StdOutBuf.data[UART_StdOutBuf.idx] = (uint8_t)ch; + + /* Increment number of characters written */ + UART_StdOutBuf.idx++; + + /* Send when the buffer is full or on newline character */ + if ((UART_StdOutBuf.idx == UART_StdOutBuf.size) || (ch == '\n')) { + /* Send buffered characters */ + status = ptrUSART->Send(&UART_StdOutBuf.data[0], UART_StdOutBuf.idx); + + if (status != ARM_DRIVER_OK) { + /* Set error flag */ + UART_StdOutBuf.flags |= UART_FLAG_TX_ERROR; + } + + /* Wait for send to complete */ + while (ptrUSART->GetStatus().tx_busy != 0U); + + /* Reset transmit index */ + UART_StdOutBuf.idx = 0U; + } + + return ch; +} +#endif diff --git a/source/retarget_fs_bkpt.c b/source/file_interface/retarget_fs_bkpt.c similarity index 100% rename from source/retarget_fs_bkpt.c rename to source/file_interface/retarget_fs_bkpt.c diff --git a/source/armcc/retarget_os_rtos2.c b/source/os_interface/armcc/retarget_os_rtos2.c similarity index 100% rename from source/armcc/retarget_os_rtos2.c rename to source/os_interface/armcc/retarget_os_rtos2.c diff --git a/source/gcc/retarget_lock_rtos2.c b/source/os_interface/gcc/retarget_lock_rtos2.c similarity index 100% rename from source/gcc/retarget_lock_rtos2.c rename to source/os_interface/gcc/retarget_lock_rtos2.c diff --git a/source/iar/retarget_os_rtos2.c b/source/os_interface/iar/retarget_os_rtos2.c similarity index 100% rename from source/iar/retarget_os_rtos2.c rename to source/os_interface/iar/retarget_os_rtos2.c diff --git a/source/stderr_bkpt.c b/source/stderr/stderr_bkpt.c similarity index 100% rename from source/stderr_bkpt.c rename to source/stderr/stderr_bkpt.c diff --git a/source/stderr_evr.c b/source/stderr/stderr_evr.c similarity index 100% rename from source/stderr_evr.c rename to source/stderr/stderr_evr.c diff --git a/source/stderr_itm.c b/source/stderr/stderr_itm.c similarity index 100% rename from source/stderr_itm.c rename to source/stderr/stderr_itm.c diff --git a/source/stdin_bkpt.c b/source/stdin/stdin_bkpt.c similarity index 100% rename from source/stdin_bkpt.c rename to source/stdin/stdin_bkpt.c diff --git a/source/stdin_itm.c b/source/stdin/stdin_itm.c similarity index 100% rename from source/stdin_itm.c rename to source/stdin/stdin_itm.c diff --git a/source/stdout_bkpt.c b/source/stdout/stdout_bkpt.c similarity index 100% rename from source/stdout_bkpt.c rename to source/stdout/stdout_bkpt.c diff --git a/source/stdout_evr.c b/source/stdout/stdout_evr.c similarity index 100% rename from source/stdout_evr.c rename to source/stdout/stdout_evr.c diff --git a/source/stdout_itm.c b/source/stdout/stdout_itm.c similarity index 100% rename from source/stdout_itm.c rename to source/stdout/stdout_itm.c diff --git a/source/tty_bkpt.c b/source/tty/tty_bkpt.c similarity index 100% rename from source/tty_bkpt.c rename to source/tty/tty_bkpt.c diff --git a/source/tty_itm.c b/source/tty/tty_itm.c similarity index 100% rename from source/tty_itm.c rename to source/tty/tty_itm.c