Skip to content

Commit 60ca0c2

Browse files
committed
Add code snippet for UnbufferedSerial and BufferedSerial API documentation
1 parent e1d5095 commit 60ca0c2

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
// Maximum number of element the application buffer can contain
9+
#define MAXIMUM_BUFFER_SIZE 32
10+
11+
// Create a DigitalOutput object to toggle an LED whenever data is received.
12+
static DigitalOut led(LED1);
13+
14+
// Create a BufferedSerial object with a default baud rate.
15+
static BufferedSerial serial_port(USBTX, USBRX);
16+
17+
int main(void) {
18+
// Set desired properties (9600-8-N-1).
19+
serial_port.set_baud(9600);
20+
serial_port.set_format(
21+
/* bits */ 8,
22+
/* parity */ BufferedSerial::None,
23+
/* stop bit */ 1
24+
);
25+
26+
// Application buffer to receive the data
27+
char buf[MAXIMUM_BUFFER_SIZE] = {0};
28+
29+
while (1) {
30+
if (uint32_t num = serial_port.read(buf, sizeof(buf))) {
31+
// Toggle the LED.
32+
led = !led;
33+
34+
// Echo the input back to the terminal.
35+
serial_port.write(buf, num);
36+
}
37+
}
38+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
// Specify different pins to test printing on UART other than the console UART.
9+
#define TARGET_TX_PIN USBTX
10+
#define TARGET_RX_PIN USBRX
11+
12+
// Create a BufferedSerial object to be used by the system I/O retarget code.
13+
static BufferedSerial serial_port(TARGET_TX_PIN, TARGET_RX_PIN, 9600);
14+
15+
FileHandle *mbed::mbed_override_console(int fd) {
16+
return &serial_port;
17+
}
18+
19+
int main(void) {
20+
// print to the console using the `serial_port` object.
21+
printf(
22+
"Mbed OS version %d.%d.%d\n",
23+
MBED_MAJOR_VERSION,
24+
MBED_MINOR_VERSION,
25+
MBED_PATCH_VERSION
26+
);
27+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
// Create a DigitalOutput object to toggle an LED whenever data is received.
9+
static DigitalOut led(LED1);
10+
11+
// Create a UnbufferedSerial object with a default baud rate.
12+
static UnbufferedSerial serial_port(USBTX, USBRX);
13+
14+
void on_rx_interrupt() {
15+
char c;
16+
17+
// Toggle the LED.
18+
led = !led;
19+
20+
// Read the data to clear the receive interrupt.
21+
if (serial_port.read(&c, 1)) {
22+
// Echo the input back to the terminal.
23+
serial_port.write(&c, 1);
24+
}
25+
}
26+
27+
int main(void) {
28+
// Set desired properties (9600-8-N-1).
29+
serial_port.baud(9600);
30+
serial_port.format(
31+
/* bits */ 8,
32+
/* parity */ SerialBase::None,
33+
/* stop bit */ 1
34+
);
35+
36+
// Register a callback to process a Rx (receive) interrupt.
37+
serial_port.attach(&on_rx_interrupt, SerialBase::RxIrq);
38+
}

0 commit comments

Comments
 (0)