Here is an example sketch that uses printf() to write the serial port on an ARM-based Teensy. I did not see this documented anywhere.
N.B., if I remove the error checking, "if (!stdout) Serial.println(...)", then the build fails with an unresolved reference to _write. (I'm using Teensyduino 1.22.)
This adds about 4,500 bytes to the sketch's code size and 124 byte of RAM vs the same sketch that uses Serial directly. But stdio allegedly also calls malloc(). I don't know how much additional memory is malloc'd.
If you need to print floating point values, see Float in sscanf on Teensy 3.1.
Code:
int Serial_write(void *cookie, const char *buf, int size)
{
for (int i = 0; i < size; i++)
Serial.print(buf[i]);
return size;
}
void setup()
{
Serial.begin(9600);
while (!Serial)
continue;
stdout = funopen(NULL, NULL, Serial_write, NULL, NULL);
if (!stdout)
Serial.println("funopen failed");
}
void loop() {
printf("Hello World\n");
}
N.B., if I remove the error checking, "if (!stdout) Serial.println(...)", then the build fails with an unresolved reference to _write. (I'm using Teensyduino 1.22.)
This adds about 4,500 bytes to the sketch's code size and 124 byte of RAM vs the same sketch that uses Serial directly. But stdio allegedly also calls malloc(). I don't know how much additional memory is malloc'd.
If you need to print floating point values, see Float in sscanf on Teensy 3.1.