Skip to content

Commit bfc8d92

Browse files
author
Qinghao Shi
authored
add serial communication snippets (#108)
* add serial snippets * add debug printf snippets
1 parent f96f284 commit bfc8d92

File tree

10 files changed

+229
-0
lines changed

10 files changed

+229
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
static UnbufferedSerial pc(USBTX, USBRX);
9+
static UnbufferedSerial uart(D1, D0);
10+
DigitalOut led1(LED1);
11+
DigitalOut led4(LED4);
12+
char *pc2uart = new char[1];
13+
char *uart2pc = new char[1];
14+
15+
void uart_recv()
16+
{
17+
led1 = !led1;
18+
while (uart.readable()) {
19+
uart.read(uart2pc, sizeof(uart2pc));
20+
pc.write(uart2pc, sizeof(uart2pc));
21+
}
22+
}
23+
24+
void pc_recv()
25+
{
26+
while (pc.readable()) {
27+
led4 = !led4;
28+
pc.read(pc2uart, sizeof(pc2uart));
29+
uart.write(pc2uart, sizeof(pc2uart));
30+
}
31+
}
32+
33+
int main()
34+
{
35+
pc.attach(&pc_recv, UnbufferedSerial::RxIrq);
36+
uart.attach(&uart_recv, UnbufferedSerial::RxIrq);
37+
38+
while (1) {
39+
sleep();
40+
}
41+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 serial object
9+
static UnbufferedSerial serial(USBTX, USBRX);
10+
char *c = new char[1];
11+
12+
// A function that echoes any received data back
13+
void echo()
14+
{
15+
while (serial.readable()) {
16+
serial.read(c, sizeof(c));
17+
serial.write(c, sizeof(c));
18+
}
19+
}
20+
21+
int main(void)
22+
{
23+
// Call our function echo whenever the serial line receives data
24+
serial.attach(&echo, UnbufferedSerial::RxIrq);
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
DigitalOut led1(LED1);
9+
10+
int main()
11+
{
12+
while (true) {
13+
led1 = !led1;
14+
15+
// Print something over the serial connection
16+
printf("Blink! LED is now %d\r\n", led1.read());
17+
ThisThread::sleep_for(500);
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
BufferedSerial pc(USBTX, USBRX);
9+
10+
int main()
11+
{
12+
pc.set_baud(9600);
13+
char msg[] = "Hello World!\r\n";
14+
pc.write(msg, sizeof(msg));
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
static BufferedSerial pc(USBTX, USBRX);
9+
10+
int main()
11+
{
12+
char msg[] = "Echoes back to the screen anything you type\n";
13+
char *buff = new char[1];
14+
pc.write(msg, sizeof(msg));
15+
while (1) {
16+
pc.read(buff, sizeof(buff));
17+
pc.write(buff, sizeof(buff));
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
static BufferedSerial pc(USBTX, USBRX); // tx, rx
9+
10+
int main()
11+
{
12+
char msg[] = "Hello World!\n";
13+
pc.write(msg, sizeof(msg));
14+
while (1);
15+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
static BufferedSerial pc(USBTX, USBRX); // tx, rx
9+
PwmOut led(LED1);
10+
11+
float brightness = 0.0;
12+
13+
int main()
14+
{
15+
char msg[] = "Press 'u' to turn LED1 brightness up, 'd' to turn it down\n";
16+
char *c = new char[1];
17+
pc.write(msg, sizeof(msg));
18+
19+
while (1) {
20+
pc.read(c, sizeof(c));
21+
pc.write(c, sizeof(c));
22+
if ((*c == 'u') && (brightness < 0.5)) {
23+
brightness += 0.01;
24+
led = brightness;
25+
}
26+
if ((*c == 'd') && (brightness > 0.0)) {
27+
brightness -= 0.01;
28+
led = brightness;
29+
}
30+
}
31+
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
static BufferedSerial pc(USBTX, USBRX);
9+
static BufferedSerial uart(D1, D0);
10+
11+
DigitalOut pc_activity(LED1);
12+
DigitalOut uart_activity(LED2);
13+
14+
int main()
15+
{
16+
char *pc2uart = new char[1];
17+
char *uart2pc = new char[1];
18+
while (1) {
19+
if (pc.readable()) {
20+
pc.read(pc2uart, sizeof(pc2uart));
21+
uart.write(pc2uart, sizeof(pc2uart));
22+
pc_activity = !pc_activity;
23+
}
24+
if (uart.readable()) {
25+
uart.read(uart2pc, sizeof(uart2pc));
26+
pc.write(uart2pc, sizeof(uart2pc));
27+
uart_activity = !uart_activity;
28+
}
29+
}
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 serial object
9+
static BufferedSerial pc(USBTX, USBRX);
10+
11+
int main(void)
12+
{
13+
char buffer[10] = {};
14+
while (1) {
15+
if (pc.readable()) {
16+
ThisThread::sleep_for(100);
17+
pc.read(buffer, 10);
18+
printf("I got '%s'\n", buffer);
19+
}
20+
}
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
int main()
9+
{
10+
printf("Hello World!\n");
11+
while (1);
12+
}

0 commit comments

Comments
 (0)