Skip to content
Merged
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
2 changes: 1 addition & 1 deletion docs/api/platform/Callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The Callback class is what’s known in C++ as a “Concrete Type”. That is, t
[![View code](https://www.mbed.com/embed/?type=library)](https://os.mbed.com/docs/mbed-os/development/mbed-os-api-doxy/classmbed_1_1_callback.html)

## Serial passthrough example with callbacks
[![View code](https://www.mbed.com/embed/?url=https://os.mbed.com/teams/mbed_example/code/rtos_threading_with_callback/)](https://os.mbed.com/users/mbedAustin/code/SerialPassthrough/file/96cb82af9996/main.cpp)
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_Platform/Callback_SerialPassthrough/)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_Platform/Callback_SerialPassthrough/main.cpp)

## Thread example with callbacks

Expand Down
17 changes: 1 addition & 16 deletions docs/api/platform/platform.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,7 @@ A callback is a user provided function that a user may pass to an API. The callb

For example, the following code allows a user to provide a customized response whenever the serial line receives data:

```c++
// Create a serial object
Serial serial(USBTX, USBRX);

// A function that echoes any received data back
void echo() {
while (serial.readable()) {
serial.putc(serial.getc());
}
}

int main(void) {
// Call our function echo whenever the serial line receives data
serial.attach(&echo, Serial::RxIrq);
}
```
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_Platform/PlatfromOverview_Callbacks/)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_Platform/PlatfromOverview_Callbacks/main.cpp)

The Callback class manages C/C++ function pointers so you don't have to. If you are asking yourself why you should use the Callback class, you should read the [Importance of State](platform.html#the-importance-of-state) section.

Expand Down
94 changes: 7 additions & 87 deletions docs/program-setup/serial/serial_communication.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,8 @@ This allows you to:

This program prints a "Hello World" message that you can view on a [terminal application](#using-terminal-applications). Communication over the USB serial port uses the standard serial interface. Specify the internal (USBTX, USBRX) pins to connect to the serial port routed over USB:

```cpp
#include "mbed.h"
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_SerialComm/Serial_HelloWorld/)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_SerialComm/Serial_HelloWorld/main.cpp)

Serial pc(USBTX, USBRX); // tx, rx

int main() {
pc.printf("Hello World!\n");
while(1);
}
```

## Using terminal applications

Expand Down Expand Up @@ -73,104 +65,32 @@ If you're not sure how to build these examples and run them on your board, pleas

### Echo back characters you type

```cpp
#include "mbed.h"

Serial pc(USBTX, USBRX);
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_SerialComm/Serial_EchoBack/)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_SerialComm/Serial_EchoBack/main.cpp)

int main() {
pc.printf("Echoes back to the screen anything you type\n");
while(1) {
pc.putc(pc.getc());
}
}
```

### Use the U and D keys to make LED1 brighter or dimmer

<span class="tips">**Note:** This example only works if LED1 is on the Pwm pin of the board you are using, such as the NUCLEO-F401RE. </span>

<span class="images">![](../../images/NUCLEOF401RE.png)<span>The pin map of the NUCLEO-F401RE shows LED1 on the Pwm pin.</span></span>

```cpp
#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx
PwmOut led(LED1);

float brightness = 0.0;

int main() {
pc.printf("Press U to turn LED1 brightness up, D to turn it down\n");
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_SerialComm/Serial_LEDControl/)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_SerialComm/Serial_LEDControl/main.cpp)

while(1) {
char c = pc.getc();
if((c == 'u') && (brightness < 0.5)) {
brightness += 0.01;
led = brightness;
}
if((c == 'd') && (brightness > 0.0)) {
brightness -= 0.01;
led = brightness;
}
}
}
```

### Pass characters in both directions

Tie pins together to see characters echoed back.

```cpp
#include "mbed.h"

Serial pc(USBTX, USBRX);
Serial uart(D1, D0);

DigitalOut pc_activity(LED1);
DigitalOut uart_activity(LED2);

int main() {
while(1) {
if(pc.readable()) {
uart.putc(pc.getc());
pc_activity = !pc_activity;
}
if(uart.readable()) {
pc.putc(uart.getc());
uart_activity = !uart_activity;
}
}
}
```
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_SerialComm/Serial_PassCharacters/)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_SerialComm/Serial_PassCharacters/main.cpp)


### Using stdin, stdout and stderr

By default, the C `stdin`, `stdout` and `stderr file` handles map to the PC serial connection:

```cpp
#include "mbed.h"

int main() {
printf("Hello World!\n");
while(1);
}
```
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_SerialComm/Serial_STDOUT/)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_SerialComm/Serial_STDOUT/main.cpp)

### Read to a buffer

```cpp
#include "mbed.h"

DigitalOut myled(LED1);
Serial pc(USBTX, USBRX);

int main() {
char c;
char buffer[128];
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_SerialComm/Serial_ReadToBuffer/)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_SerialComm/Serial_ReadToBuffer/main.cpp)

pc.gets(buffer, 4);
pc.printf("I got '%s'\n", buffer);
while(1);
}
```
29 changes: 2 additions & 27 deletions docs/tools/debug/debug_with_printf.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,8 @@ To send data over the serial connection, use the [Serial](../apis/serial.html) o

This program blinks the LED on your development board and prints a message every time the LED changes state:

```cpp
#include "mbed.h"

// define the Serial object
Serial pc(USBTX, USBRX);

DigitalOut led1(LED1);
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_Debugging/DebugPrintf_BlinksLED/)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_Debugging/DebugPrintf_BlinksLED/main.cpp)

int main() {
while (true) {
led1 = !led1;

// Print something over the serial connection
pc.printf("Blink! LED is now %d\r\n", led1.read());

wait(0.5);
}
}
```

Compile this program, and flash it on your development board. You now can inspect these messages using a serial monitor.

Expand Down Expand Up @@ -103,16 +86,8 @@ Compile this program, and flash it on your development board. You now can inspec

By default, the speed at which the microcontroller and your computer communicate (the baud rate) is set to 9600 baud. This setting fits most use cases, but you can change it by calling the `baud` function on the serial object:

```cpp
#include "mbed.h"

Serial pc(USBTX, USBRX);
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_Debugging/DebugPrintf_SetBaudRate/)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/Tutorials_Debugging/DebugPrintf_SetBaudRate/main.cpp)

int main() {
pc.baud(115200);
pc.printf("Hello World!\r\n");
}
```

If you change the baud rate on the device, you also need to change it on your serial monitor:

Expand Down