-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.cpp
40 lines (32 loc) · 928 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* 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);
}