Serial control with STM32 Nucleo

Dependencies:   JSON

Files at this revision

API Documentation at this revision

Comitter:
sng_hws
Date:
Fri May 21 06:40:02 2021 +0000
Parent:
0:fde0aa5b370d
Commit message:
commit for studio

Changed in this revision

JSON.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/JSON.lib	Fri May 21 06:40:02 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/38domo/code/JSON/#43e1e35bb7ec
--- 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);
+    }
+
+}
--- a/mbed-os.lib	Thu Apr 29 09:23:17 2021 +0000
+++ b/mbed-os.lib	Fri May 21 06:40:02 2021 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#565ab149819481224ab43f878c3921b14b11d180
+https://github.com/ARMmbed/mbed-os/#c73413893fb98aaaeda74513c981ac68adc8645d