system: enable changing stdout/stderr to different Stream, for example printf

This commit is contained in:
Daniel Fekete 2017-09-10 11:48:24 +02:00
parent b407fd553b
commit 1899a93dff
5 changed files with 30 additions and 9 deletions

View File

@ -28,7 +28,9 @@
static const int print_fileno = 3;
static Print *print;
static Print *print = NULL;
Stream *stdoutStream = NULL;
Stream *stderrStream = NULL;
int stm32SetPrintOutput(Print *p) {
if (p == NULL) {
@ -50,12 +52,12 @@ extern "C" int _write( int file, char *ptr, int len ) {
if (file == STDOUT_FILENO) {
Serial.write(ptr, len);
if (stdoutStream) stdoutStream->write(ptr, len);
} else if (file == STDERR_FILENO) {
Serial.write(ptr, len);
Serial.flush();
if (stderrStream) stderrStream->write(ptr, len);
if (stderrStream) stderrStream->flush();
} else if (file == print_fileno) {

View File

@ -30,7 +30,10 @@ inline bool isInterrupt() {
return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0 ;
}
// Internal: what will be the
extern "C" int stm32SetPrintOutput(Print *p);
extern Stream *stdoutStream;
extern Stream *stderrStream;
#endif

View File

@ -27,6 +27,7 @@
* TODO: add constructor with custom buffer parameter
*/
#include "SerialUART.h"
#include "STM32System.h"
#include "stm32_gpio_af.h"
/**
@ -158,6 +159,15 @@ void SerialUART::begin(const uint32_t baud) {
HAL_UART_Init(handle);
HAL_UART_Receive_IT(handle, &receive_buffer, 1);
if (stdoutStream == NULL) {
#if defined(MENU_SERIAL) && MENU_SERIAL == SerialUART1
stdoutStream = this;
#elif defined(MENU_SERIAL_AUTO) && MENU_SERIAL_AUTO == SerialUART1
stdoutStream = this;
#endif
}
}

View File

@ -24,7 +24,8 @@
*
****************************************************************************/
#include <SerialUSB.h>
#include "SerialUSB.h"
#include "STM32System.h"
#include "variant.h"
#include "USBDevice.h"
@ -37,8 +38,13 @@ SerialUSBClass::SerialUSBClass(){
}
void SerialUSBClass::init(void) {
//
//USBDeviceFS.beginCDC();
if (stdoutStream == NULL) {
#if defined(MENU_SERIAL) && MENU_SERIAL == SerialUSB
stdoutStream = this;
#elif defined(MENU_SERIAL_AUTO) && MENU_SERIAL_AUTO == SerialUSB
stdoutStream = this;
#endif
}
}

View File

@ -90,7 +90,7 @@ recipe.S.o.pattern="{compiler.path}{compiler.S.cmd}" {compiler.S.flags} -D{build
recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{archive_file_path}" "{object_file}"
## Combine gc-sections, archives, and objects
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" {compiler.c.elf.extra_flags} -o "{build.path}/{build.project_name}.elf" "-L{build.path}" -Wl,--start-group {object_files} "{build.path}/{archive_file}" -lc -Wl,--end-group -lm -lgcc --specs=nano.specs
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" {compiler.c.elf.extra_flags} -o "{build.path}/{build.project_name}.elf" "-L{build.path}" -Wl,--start-group {object_files} "{build.path}/{archive_file}" -lstdc++ -lc -Wl,--end-group -lm -lgcc --specs=nano.specs
## Create output (.bin file)
recipe.objcopy.bin.pattern="{compiler.path}{compiler.elf2hex.cmd}" {compiler.elf2hex.flags} {compiler.elf2hex.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.bin"