Mbed-os 6.15 UART NOT working on stm32L432KC

Hi,

I am upgrade my mbed-os from mbed-os 2 to mbed-os 6.15 for stm32L432KC.

My code has been test on stm32H7xxx, and it proved to work. However, the code is not work on stm32L4xxx.

Could you please let me know that do I need to modify any files in target to make the code work in stm32L4xxx?

/*
 * Copyright (c) 2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */
#include "mbed.h"

// Maximum number of element the application buffer can contain
#define MAXIMUM_BUFFER_SIZE                                                  32

// Create a DigitalOutput object to toggle an LED whenever data is received.
static DigitalOut led(PB_0, 1);

// Create a BufferedSerial object with a default baud rate.
static BufferedSerial serial_port(USBTX, USBRX);
BufferedSerial port1(PA_2, PA_3, 921600);

int main(void)
{
    // Set desired properties (9600-8-N-1).
    serial_port.set_baud(9600);
    serial_port.set_format(
        /* bits */ 8,
        /* parity */ BufferedSerial::None,
        /* stop bit */ 1
    );

    // Application buffer to receive the data
    char buf[MAXIMUM_BUFFER_SIZE] = {0};

    while (1) {
        if (uint32_t num = port1.read(buf, sizeof(buf))) {
            // Toggle the LED.
            led = !led;

            // Echo the input back to the terminal.
            port1.write(buf, num);
        }
    }
}
```````

Hello,

Missing info:

  • your stm32L432KC is Nucleo-L432KC or your own custom target?
  • what do you mean with “not working”?
  • did the code work on L432kc and Mbed2?

Tips:

  • In default is the pin PA_3 not allowed for any serial usage. Probably because is also connected to RX of UART2 that is assigned for default STDOUT.
  • Pin PA_2 is already use for default STOUT. So if you did not rewrite it via mbed_app.json file, then you have same TX pin of same UART controller for both objects - USBTX = PA_2 in default. If you do not want change default settings, then rather use UART1 on pins PA_9 and PA_10.

Br, Jan

Dear Jan,

Thank you for your reply.
We used our customized PCB board. We can not change the board layout.
The PA3 is connected with PA15. So I changed the PA3 to PA15, RX works. But the TX still does not work.

I am new to mbed os. How can I rewrite PA2 to normal UART2 TX in mbed_app_json.file?

And, yes, the code work on L432KC and mbed2 with Serial. But I am not sure any changes has been made in mbed-os previously by senior engineers.

Thank you so much.
Best wishes.

-Ailin-

When we look to your example

Macros USBTX, USBRX, used in first object with name serial_port, are pins PA_2 and PA_15 of UART2.
Your second object with name port1 use pins PA_2 and PA_3 (your changed to PA_15), that is also UART2. So you have two objects for one UART and also with two baud rates, that can not work ever. Because…
You declared first object which sets the UART2 to default baud rate 9600. Then you declare second object, which sets baud rate of UART2 to 921600. Then finaly in the main, you set the baud rate of UART2 back to 9600 via first object. So, your code will probably working, but on 9600 instead of 921600.

Another problem is the default debug console which is set to UART2. So you have to disable or redirect it via mbed_app.json

{
    "target_overrides": {
        "*": {
            "target.console-uart": false
        }
    }
}

BR, Jan

Hi, Jan,

I have changed my code to

/*
 * Copyright (c) 2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */
#include "mbed.h"

// Maximum number of element the application buffer can contain
#define MAXIMUM_BUFFER_SIZE                                                  32

// Create a DigitalOutput object to toggle an LED whenever data is received.
static DigitalOut led(PB_0, 1);
BufferedSerial port1(PA_2, PA_15, 921600);

int main(void)
{
   // Application buffer to receive the data
    char buf[MAXIMUM_BUFFER_SIZE] = {0};

    while (1) {
        if (uint32_t num = port1.read(buf, sizeof(buf))) {
            // Toggle the LED.
            led = !led;

            // Echo the input back to the terminal.
            port1.write(buf, num);
        }
    }
}

And I changed the mbed_app.json to

{
    "target_overrides": {
        "*": {
            "target.console-uart": false
        }
    }
}

But the TX (PA_2) still not work. nothing get received from the other side.

Any other suggestions?

Thank you so much.
Best wishes.
-Ailin-

What is “the other side” ?
I already tested the code what you posted and it is working. However I have only Nucleo-L432KC so my pins are connected to ST-link’s VCP and I have it connected to YAT (Serial terminal application) on Windows 10.

(09:52:31.117) (<<) Hello<CR><LF> (7)
(09:52:31.134) (>>) Hello<CR><LF> (7)
(09:52:31.300) (<<) Hello<CR><LF> (7)
(09:52:31.317) (>>) Hello<CR><LF> (7)
(09:52:31.451) (<<) Hello<CR><LF> (7)
(09:52:31.472) (>>) Hlo<CR><LF> (5)
(09:52:31.612) (<<) Hello<CR><LF> (7)
(09:52:31.636) (>>) Hello<CR><LF> (7)
(09:52:31.775) (<<) Hello<CR><LF> (7)
(09:52:31.800) (>>) Helo<CR><LF> (6)
(09:52:31.918) (<<) Hello<CR><LF> (7)
(09:52:31.947) (>>) Helo<CR><LF> (6)
(09:52:32.071) (<<) Hello<CR><LF> (7)
(09:52:32.103) (>>) Hllo<CR><LF> (6)
(09:52:32.213) (<<) Hello<CR><LF> (7)
(09:52:32.243) (>>) Hello<CR><LF> (7)
(09:52:32.360) (<<) Hello<CR><LF> (7)
(09:52:32.395) (>>) Hello<CR><LF> (7)
(09:52:32.514) (<<) Hello<CR><LF> (7)
(09:52:32.527) (>>) Helo<CR><LF> (6)
(09:52:32.656) (<<) Hello<CR><LF> (7)
(09:52:32.669) (>>) Hello<CR><LF> (7)
(09:52:32.797) (<<) Hello<CR><LF> (7)
(09:52:32.815) (>>) Hello<CR><LF> (7)
(09:52:32.950) (<<) Hello<CR><LF> (7)
(09:52:32.993) (>>) Hello<CR><LF> (7)
(09:52:33.118) (<<) Hello<CR><LF> (7)
(09:52:33.167) (>>) Ho<LF> (3)
(09:52:35.041) (<<) Hello<CR><LF> (7)
(09:52:35.122) (>>) Helo<CR><LF> (6)

BR, Jan