Skip to content

STM32L053C8 UART Error in Mbed2 Latter Than Rev159 #8045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tony-YI opened this issue Sep 9, 2018 · 11 comments
Closed

STM32L053C8 UART Error in Mbed2 Latter Than Rev159 #8045

Tony-YI opened this issue Sep 9, 2018 · 11 comments

Comments

@Tony-YI
Copy link

Tony-YI commented Sep 9, 2018

Description

I am using a STM32L053C8 MCU and got some problems with it.

#include "mbed.h"
 
int main()
{
    DigitalOut  led(PA_14);
//    Serial      serial(PB_6, PB_7);

    while(1)
    {
        led = !led;
        wait(0.1);
    }
}

The code above works fine. However, after I un-commented the Serial, the LED stop blinking. As far as I know, (PB_6, PB_7) is the UART1 serial port. I tried the UART2 and LPUART1 and both of them works fine. It looks like I can not define any UART1 port. I have tried (PA_9, PA_10) which is utilizing the UART1 and it worked. It looks like I could not define serial on (PB_6, PB_7).

According to my previous experience, I switched the mbed-2 version to Rev.159:7130f32 (I am using the online compiler) and un-commented the Serial and it worked fine.

Jerome Coutant from ST said it is because the UART1 is already used by the default STDIO.

My question is that can I disable the default STDIO and use the UART1 for my own purpose? Note that I am using the online compiler.

Thanks in advance.

Issue request type

[X] Question
[ ] Enhancement
[ ] Bug

@Tony-YI
Copy link
Author

Tony-YI commented Sep 9, 2018

After I un-commented the Serial line, I probed the PB_4 pin (default LED1 on DISCO-L053C8 board) and its patterns matched the mbed_die(void) function in platform/mbed_board.c.

Referring to #6019, I have added the following code into main.cpp and tried to redirect the stdio to UART2. I have probed the pin PA_2 and it was outputting the string in printf("1\r\n");. However, once I un-commented the Serial, the MCU executed the mbed_die routine.

#include "mbed.h"

void mbed_die(void)
{
    gpio_t led_err;
    gpio_init_out(&led_err, PA_14);

    while (1) {
        for (int i = 0; i < 4; ++i) {
            gpio_write(&led_err, 1);
            wait_ms(150);
            gpio_write(&led_err, 0);
            wait_ms(150);
        }

        for (int i = 0; i < 4; ++i) {
            gpio_write(&led_err, 1);
            wait_ms(400);
            gpio_write(&led_err, 0);
            wait_ms(400);
        }
    }
}

FileHandle *mbed::mbed_override_console(int fd)
{
    static UARTSerial console(PA_2, PA_3, MBED_CONF_PLATFORM_STDIO_BAUD_RATE);
    return &console;
}
 
int main()
{
    DigitalOut  led(PA_14);
//    Serial      serial(PB_6, PB_7);

    while(1)
    {
        printf("1\r\n");
        led = !led;
        wait(0.1);
    }
}

Still looking for a way to disable the STDIO or maybe make it using the UART2.
[Mirrored to Jira]

@Tony-YI
Copy link
Author

Tony-YI commented Sep 9, 2018

After some digging, it looks like it is caused by the following code in /targets/TARGET_STM/serial_api.c:

void serial_init(serial_t *obj, PinName tx, PinName rx)
{
    ...

    if ((tx == STDIO_UART_TX) || (rx == STDIO_UART_RX)) {
        stdio_config = 1;
    } else {
        if (uart_tx == pinmap_peripheral(STDIO_UART_TX, PinMap_UART_TX)) {
            error("Error: new serial object is using same UART as STDIO");
        }
    }
    ...
}

I have double checked it on mbed-dev sources, and the above code was added since Release version 159, about 6 months ago.

So I need to redefine the STDIO_UART_TX and STDIO_UART_RX.
[Mirrored to Jira]

@jeromecoutant
Copy link
Collaborator

jeromecoutant commented Sep 10, 2018

Hi

Wiki page: https://os.mbed.com/teams/ST/wiki/STDIO

So I need to redefine the STDIO_UART_TX and STDIO_UART_RX.

Yes, in a mbed_app.json file like :

"target_overrides": {     
"DISCO_L053C8": {
        "target.stdio_uart_tx": "PB_6",
        "target.stdio_uart_rx": "PB_7"
    } }

[Mirrored to Jira]

@Tony-YI
Copy link
Author

Tony-YI commented Sep 10, 2018

Thanks @jeromecoutant , however, since I am using the online compiler, I suppose I do not have the mbed_app.json file.

I tired to switch to mbed-cli, however, I facing the same issue mentioned here.

Currently I would like to stick to the online compiler because its .bin file is smaller than that complied using GCC_ARM.

If there is no way to redifine the STDIO_UART_TX and STDIO_UART_RX on the online compiler, I would redesign the custom board.
[Mirrored to Jira]

@jeromecoutant
Copy link
Collaborator

jeromecoutant commented Sep 10, 2018

See for ex https://os.mbed.com/teams/mbed-os-examples/code/mbed-os-example-wifi/

You can add a mbed_app.json file with online compiler

[Mirrored to Jira]

@Tony-YI
Copy link
Author

Tony-YI commented Sep 10, 2018

@jeromecoutant It works!!! Thanks a lot. You are really helpful!

@jeromecoutant After adding the mbed_app.json file as follows:

{
"target_overrides": {     
    "DISCO_L053C8": {
        "target.stdio_uart_tx": "PB_6",
        "target.stdio_uart_rx": "PB_7"
        }
    }
}

The macro STD_UART_TX and STD_UART_RX do change to PB_6 and PB_7. When I executed the following code, the led is blinking with 1 second interval, which means the STD_UART_TX becomes PB_6. However, when I probed the PA_9, it is outputting serial data, instead of PB_6. Moreover, once I un-comment the Serial line, the MCU is executing mbed_die() routine.

#include "mbed.h"
int main()
{
    DigitalOut  led(PA_14);

//    Serial      serial(PB_6, PB_7);
 
    while(1)
    {
        led = !led;
        printf("a\r\n");
        if(STDIO_UART_TX == PA_9)
        {
            wait(0.1);
        }
        else if(STDIO_UART_TX == PB_6)
        {
            wait(1);
        }
    }
}

[Mirrored to Jira]

@Tony-YI Tony-YI closed this as completed Sep 10, 2018
@Tony-YI Tony-YI reopened this Sep 10, 2018
@jeromecoutant
Copy link
Collaborator

jeromecoutant commented Sep 10, 2018

Why do you want to re-configure PB_6 and PB_7.
Thanks to the mbed_app.json, you new serial console is already configured.

[Mirrored to Jira]

@Tony-YI
Copy link
Author

Tony-YI commented Sep 16, 2018

Why do you want to re-configure PB_6 and PB_7.
Thanks to the mbed_app.json, you new serial console is already configured.

Dear @jeromecoutant, in my last comment, the line Serial serial(PB_6, PB_7) was commented. Then I probed the pin PA_9 and PB_6 respectively, and PA_9 was outputting something instead of PB_6 (mbed_app.json was added). This is contradictory to what we expect, i.e. PB_6 should output something.

In fact, I have tried (PA_14, PA_15), (PB_10, PB_11) and (PA_2, PA_3) in mbed_app.json. I must add the following code so that the corresponding TX pin could output something:

FileHandle *mbed::mbed_override_console(int fd)
{
    static UARTSerial console(STDIO_UART_TX, STDIO_UART_RX, MBED_CONF_PLATFORM_STDIO_BAUD_RATE);
    return &console;
}

However, after I added the code above and tried (PB_6, PB_7) in mbed_app.json, the MCU went into mbed_die() routine. I am sure it was caused by the code below in /targets/TARGET_STM/serial_api.c:

void serial_init(serial_t *obj, PinName tx, PinName rx)
{
    ...

    if ((tx == STDIO_UART_TX) || (rx == STDIO_UART_RX)) {
        stdio_config = 1;
    } else {
        if (uart_tx == pinmap_peripheral(STDIO_UART_TX, PinMap_UART_TX)) {
            error("Error: new serial object is using same UART as STDIO");
        }
    }
    ...
}

It looks like in serial_api.c, the STDIO_UART_TX and STDIO_UART_RX are still referring to PA_9 and PA_10 respectively.

Another interesting thing is that, I compile the above program (without FileHandle *mbed::mbed_override_console(int fd)) using mbed-cli (1.8.1) and probed the PA_9 and PB_6. None of them was outputting anything.

[Mirrored to Jira]

@adbridge
Copy link
Contributor

adbridge commented Oct 4, 2018

Internal Jira reference: https://jira.arm.com/browse/IOTPART-6500

@jeromecoutant
Copy link
Collaborator

@Tony-YI Should we close this issue?

@wubaojun1982
Copy link

My custom board is STM32F103ZCT6, there is the same problem. After methods mentioned like above, the problem has been solved. Thx very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants