-
Notifications
You must be signed in to change notification settings - Fork 26
Add code snippet for UnbufferedSerial and BufferedSerial API documentation #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jamesbeyond
merged 1 commit into
ARMmbed:master
from
hugueskamba:hk-add-unbuffered-snipets
Feb 10, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) 2020 Arm Limited and affiliates. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "mbed.h" | ||
|
||
// Maximum number of element the application buffer can contain | ||
#define MAXIMUM_BUFFER_SIZE 32 | ||
|
||
// Create a DigitalOutput object to toggle an LED whenever data is received. | ||
static DigitalOut led(LED1); | ||
|
||
// Create a BufferedSerial object with a default baud rate. | ||
static BufferedSerial serial_port(USBTX, USBRX); | ||
|
||
int main(void) { | ||
// Set desired properties (9600-8-N-1). | ||
serial_port.set_baud(9600); | ||
serial_port.set_format( | ||
/* bits */ 8, | ||
/* parity */ BufferedSerial::None, | ||
/* stop bit */ 1 | ||
); | ||
|
||
// Application buffer to receive the data | ||
char buf[MAXIMUM_BUFFER_SIZE] = {0}; | ||
|
||
while (1) { | ||
if (uint32_t num = serial_port.read(buf, sizeof(buf))) { | ||
// Toggle the LED. | ||
led = !led; | ||
|
||
// Echo the input back to the terminal. | ||
serial_port.write(buf, num); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (c) 2020 Arm Limited and affiliates. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "mbed.h" | ||
|
||
// Specify different pins to test printing on UART other than the console UART. | ||
#define TARGET_TX_PIN USBTX | ||
#define TARGET_RX_PIN USBRX | ||
|
||
// Create a BufferedSerial object to be used by the system I/O retarget code. | ||
static BufferedSerial serial_port(TARGET_TX_PIN, TARGET_RX_PIN, 9600); | ||
|
||
FileHandle *mbed::mbed_override_console(int fd) { | ||
return &serial_port; | ||
} | ||
|
||
int main(void) { | ||
// print to the console using the `serial_port` object. | ||
printf( | ||
"Mbed OS version %d.%d.%d\n", | ||
MBED_MAJOR_VERSION, | ||
MBED_MINOR_VERSION, | ||
MBED_PATCH_VERSION | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) 2020 Arm Limited and affiliates. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "mbed.h" | ||
|
||
// Create a DigitalOutput object to toggle an LED whenever data is received. | ||
static DigitalOut led(LED1); | ||
|
||
// Create a UnbufferedSerial object with a default baud rate. | ||
static UnbufferedSerial serial_port(USBTX, USBRX); | ||
|
||
void on_rx_interrupt() { | ||
char c; | ||
|
||
// Toggle the LED. | ||
led = !led; | ||
|
||
// Read the data to clear the receive interrupt. | ||
if (serial_port.read(&c, 1)) { | ||
// Echo the input back to the terminal. | ||
serial_port.write(&c, 1); | ||
} | ||
} | ||
|
||
int main(void) { | ||
// Set desired properties (9600-8-N-1). | ||
serial_port.baud(9600); | ||
serial_port.format( | ||
/* bits */ 8, | ||
/* parity */ SerialBase::None, | ||
/* stop bit */ 1 | ||
); | ||
|
||
// Register a callback to process a Rx (receive) interrupt. | ||
serial_port.attach(&on_rx_interrupt, SerialBase::RxIrq); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.