Serial control with STM32 Nucleo

Dependencies:   JSON

Committer:
sng_hws
Date:
Fri May 21 06:40:02 2021 +0000
Revision:
1:457c0c664181
Parent:
0:fde0aa5b370d
commit for studio

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sng_hws 1:457c0c664181 1 /*
sng_hws 1:457c0c664181 2 * Copyright (c) 2020 Arm Limited and affiliates.
lucadevito 0:fde0aa5b370d 3 * SPDX-License-Identifier: Apache-2.0
lucadevito 0:fde0aa5b370d 4 */
lucadevito 0:fde0aa5b370d 5
lucadevito 0:fde0aa5b370d 6 #include "mbed.h"
sng_hws 1:457c0c664181 7
sng_hws 1:457c0c664181 8 // Create a DigitalOutput object to toggle an LED whenever data is received.
sng_hws 1:457c0c664181 9 static DigitalOut led(LED1);
sng_hws 1:457c0c664181 10
sng_hws 1:457c0c664181 11 // Create a UnbufferedSerial object with a default baud rate.
sng_hws 1:457c0c664181 12 static UnbufferedSerial serial_port(USBTX, USBRX);
sng_hws 1:457c0c664181 13
sng_hws 1:457c0c664181 14 InterruptIn mybutton(USER_BUTTON);
sng_hws 1:457c0c664181 15 DigitalOut myled(LED1);
sng_hws 1:457c0c664181 16
sng_hws 1:457c0c664181 17 float delay = 1.0; // 1 sec
sng_hws 1:457c0c664181 18
sng_hws 1:457c0c664181 19 void pressed()
sng_hws 1:457c0c664181 20 {
sng_hws 1:457c0c664181 21 char c = 'X';
sng_hws 1:457c0c664181 22 serial_port.write(&c, 1);
sng_hws 1:457c0c664181 23 }
lucadevito 0:fde0aa5b370d 24
lucadevito 0:fde0aa5b370d 25
sng_hws 1:457c0c664181 26 void on_rx_interrupt()
lucadevito 0:fde0aa5b370d 27 {
sng_hws 1:457c0c664181 28 char c;
sng_hws 1:457c0c664181 29
sng_hws 1:457c0c664181 30 // Toggle the LED.
sng_hws 1:457c0c664181 31 led = !led;
lucadevito 0:fde0aa5b370d 32
sng_hws 1:457c0c664181 33 // Read the data to clear the receive interrupt.
sng_hws 1:457c0c664181 34 if (serial_port.read(&c, 1)) {
sng_hws 1:457c0c664181 35 // Echo the input back to the terminal.
sng_hws 1:457c0c664181 36 serial_port.write(&c, 1);
lucadevito 0:fde0aa5b370d 37 }
lucadevito 0:fde0aa5b370d 38 }
sng_hws 1:457c0c664181 39
sng_hws 1:457c0c664181 40 int main(void)
sng_hws 1:457c0c664181 41 {
sng_hws 1:457c0c664181 42 // Set desired properties (9600-8-N-1).
sng_hws 1:457c0c664181 43 serial_port.baud(115200);
sng_hws 1:457c0c664181 44 serial_port.format(
sng_hws 1:457c0c664181 45 /* bits */ 8,
sng_hws 1:457c0c664181 46 /* parity */ SerialBase::None,
sng_hws 1:457c0c664181 47 /* stop bit */ 1
sng_hws 1:457c0c664181 48 );
sng_hws 1:457c0c664181 49
sng_hws 1:457c0c664181 50 // Register a callback to process a Rx (receive) interrupt.
sng_hws 1:457c0c664181 51 serial_port.attach(&on_rx_interrupt, SerialBase::RxIrq);
sng_hws 1:457c0c664181 52
sng_hws 1:457c0c664181 53 while (1) {
sng_hws 1:457c0c664181 54 mybutton.fall(&pressed);
sng_hws 1:457c0c664181 55 }
sng_hws 1:457c0c664181 56
sng_hws 1:457c0c664181 57 }