Stephan Schnittger
/
mbed-os-example-serial
Serial control with STM32 Nucleo
Diff: main.cpp
- Revision:
- 1:457c0c664181
- Parent:
- 0:fde0aa5b370d
diff -r fde0aa5b370d -r 457c0c664181 main.cpp --- a/main.cpp Thu Apr 29 09:23:17 2021 +0000 +++ b/main.cpp Fri May 21 06:40:02 2021 +0000 @@ -1,28 +1,57 @@ -/* mbed Microcontroller Library - * Copyright (c) 2019 ARM Limited +/* + * Copyright (c) 2020 Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 */ #include "mbed.h" -#include "platform/mbed_thread.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); + +InterruptIn mybutton(USER_BUTTON); +DigitalOut myled(LED1); + +float delay = 1.0; // 1 sec + +void pressed() +{ + char c = 'X'; + serial_port.write(&c, 1); +} -// Blinking rate in milliseconds -#define BLINKING_RATE_MS 500 - -Serial pc(USBTX,USBRX); - -int main() +void on_rx_interrupt() { - // Initialise the digital pin LED1 as an output - DigitalOut led(LED1); + char c; + + // Toggle the LED. + led = !led; - pc.printf("Hello World !\r\n"); - pc.printf("Here I'm !\r\n"); - pc.printf("It works !\r\n"); - - while (true) { - led = !led; - thread_sleep_for(BLINKING_RATE_MS); + // 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(115200); + 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); + + while (1) { + mybutton.fall(&pressed); + } + +}