---
title: "Configure UART0 (PB_2,PB_3) on mbed"
url: "https://maker.wiznet.io/forum/12306"
markdown_url: "https://maker.wiznet.io/forum/12306/md"
type: "Forum topic"
category: "MCU & Boards"
author: "joaopaulob"
created: "2017-05-29T19:42:28+09:00"
last_activity: "2017-06-03T14:22:19+09:00"
language: "en"
views: 1313
replies: 7
source: "WIZnet Makers forum (https://maker.wiznet.io/forum/)"
---

# Configure UART0 (PB_2,PB_3) on mbed

## Question

Asked by joaopaulob on 2017-05-29 in MCU & Boards.

Hi,

The mbed freezes when configuring the UART0 on PB_02/PB_03 (W7500).

I’ve also tried to set the alternate function but does’t work.

> //configure alternate function
> HAL_PAD_AFConfig(PAD_PB,GPIO_Pin_0, PAD_AF2);
> HAL_PAD_AFConfig(PAD_PB,GPIO_Pin_1, PAD_AF2);
> HAL_PAD_AFConfig(PAD_PB,GPIO_Pin_2, PAD_AF2);
> HAL_PAD_AFConfig(PAD_PB,GPIO_Pin_3, PAD_AF2);

> //initialize serial on UART0
> Serial serial(PB_2, PB_3); // tx, rx

Any idea?

Best regards

## Replies

### Reply 1 by lawrence, 2017-05-30

Hello, joaopaulob

I think you do not need to use HAL_PAD_AFConfig()

Only you need to declare Serial Class : Serial serial(PB_2, PB_3);

But if even you success the compile, it does not work.

You must change D0(U0_RX), D1(U0_TX) alternative function to GPIO.

Because the D0, D1 order is stronger then PB_2, PB_3 ( I think… )

Thank you,

lawrence

### Reply 2 by lawrence, 2017-05-30

like this…

Digital Output(D0);
Digital Output(D1);

Serial serial(PB_2, PB_3);

Change the D0, D1 function and declare PB_2, PB_3 as UART0.

### Reply 3 by joaopaulob, 2017-05-30 (in reply to reply 2)

Lawrence,

I did:

DigitalOut dummy1(PA_13); //D1
DigitalOut dummy2(PA_14); //D0

When I call the function ( Serial serial(PB_2, PB_3) ) I got the error “pinmap not found for peripheral”.

It looks like the mbed do not allow to use these pins for UART0.

Any idea?

Best regards,

### Reply 4 by lawrence, 2017-05-30

hello, joaopaulob

This is what I captured on my web compiler.

[![](https://maker.wiznet.io/forum_uploads/wiznet/original/2X/1/10c2f76a7d56fd31a1126469e5a0844a07d67171.png) 949×536 40.4 KB](https://maker.wiznet.io/forum_uploads/wiznet/original/2X/1/10c2f76a7d56fd31a1126469e5a0844a07d67171.png)

Could check your compiler with below lists?

1. Did you select WIZwiki-W7500 as Web compiler Platform board?
   → refer the right side and up in that capture.

2. Try to update mbed library.

Thank you

lawrence

### Reply 5 by joaopaulob, 2017-05-31 (in reply to reply 4)

Hi Lawrence,

The program compile successfully, but it freezes on running.

Try to run the following code:

```
#include "mbed.h"

Serial debug(PC_2, PC_3); //This is my terminal debug UART1
 
int main() {
    
    DigitalOut myled(PA_13);
    DigitalOut urled(PA_14);
    
    debug.baud(115200);
    
    debug.printf("Initializing... \r\n");
    
    Serial device(PB_2, PB_3); // This is the UART0. ->> It freezes here and prints on terminal debug "pinmap not found for peripheral"
    device.baud(9600);

    while(1)
    {
        device.printf("Device UART0 debug \r\n");
        debug.printf("Terminal UART1 debug \r\n");
        wait_ms(600);
    }
}
```

Best regards

### Reply 6 by lawrence, 2017-05-31

hello, joaopaulob

Thank you so much!

You are Right!..

It is my fault… I never thought about mbed library.

WIZnet missed add PB_2 and PB_3 in Serial pin map.

Refer below capture.

[![](https://maker.wiznet.io/forum_uploads/wiznet/original/2X/7/76029e75d5866987f33d36353479359e89b92896.png) 1226×911 115 KB](https://maker.wiznet.io/forum_uploads/wiznet/original/2X/7/76029e75d5866987f33d36353479359e89b92896.png)

So… Now you have two solutions.

1. Change mbed to mbed-dev and Add PB_2 , PB_3 in PeripheralPins.c

```
    //*** SERIAL ***
    const PinMap PinMap_UART_TX[] = {
        {PA_13, UART_0, WIZ_PIN_DATA(WIZ_MODE_AF, WIZ_GPIO_PULLUP, Px_AFSR_AF0)},
        {PC_2,  UART_1, WIZ_PIN_DATA(WIZ_MODE_AF, WIZ_GPIO_PULLUP, Px_AFSR_AF0)},
        {PB_2,  UART_1, WIZ_PIN_DATA(WIZ_MODE_AF, WIZ_GPIO_PULLUP, Px_AFSR_AF0)},
        {NC,    NC,     0}
    };

    const PinMap PinMap_UART_RX[] = {
        {PA_14, UART_0, WIZ_PIN_DATA(WIZ_MODE_AF, WIZ_GPIO_PULLUP, Px_AFSR_AF0)},
        {PC_3,  UART_1, WIZ_PIN_DATA(WIZ_MODE_AF, WIZ_GPIO_PULLUP, Px_AFSR_AF0)},
        {PB_3,  UART_1, WIZ_PIN_DATA(WIZ_MODE_AF, WIZ_GPIO_PULLUP, Px_AFSR_AF0)},
        {NC,    NC,     0}
    };
```

1. Use another pin as UART0 , PC_2, PC_3.

Thank you

lawrence

### Reply 7 by joaopaulob, 2017-06-03

Found a way to “hack” the mbed:

```
#include "mbed.h"
#include "W7500x_uart.h"

UART_InitTypeDef UART_InitStructure0;

int main() {
    
    Serial rfid(PA_13, PA_14);
    
    DigitalOut dummy1(PA_13);
    DigitalOut dummy2(PA_14);
    
    // Reconfigure to PB2 and PB3
    HAL_PAD_AFConfig(PAD_PB,GPIO_Pin_2, PAD_AF2);
    HAL_PAD_AFConfig(PAD_PB,GPIO_Pin_3, PAD_AF2);
    
    UART_StructInit(&UART_InitStructure0);
    UART_InitStructure0.UART_BaudRate = 9600;
    UART_Init(UART0,&UART_InitStructure0);


    rfid.putc(0xAA);

}
```

---

Source: https://maker.wiznet.io/forum/12306
