Need some help setting up UART5

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
gradoj
Posts: 27
Joined: Mon Aug 11, 2014 5:47 pm

Need some help setting up UART5

Post by gradoj » Sat May 30, 2015 8:02 pm

I am trying to get UART 5 up and running on a custom board. I've made a couple of modifications but when I .read it always returns with no data and when I .write nothing goes out on uart 5.

If anyone can point out where I've gone wrong I'd appreciate it. Is there a way to query the pins to see if they are setup for their alternate function?

mpconfigboard.h

Code: Select all

#define MICROPY_HW_UART5_PORT (GPIOC)
#define MICROPY_HW_UART5_PINS (GPIO_PIN_12)
uart.c in uart_init2 I tried with the commented lines in and out

Code: Select all

#if defined(UART5) && defined(MICROPY_HW_UART5_PORT) && defined(MICROPY_HW_UART5_PINS)
case PYB_UART_5:
	UARTx = UART5;
        irqn = UART5_IRQn;
        GPIO_AF_UARTx = GPIO_AF8_UART5;
			
        GPIO_Port = MICROPY_HW_UART5_PORT;
        GPIO_Pin = MICROPY_HW_UART5_PINS;
	__UART5_CLK_ENABLE();
			
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.Pin = GPIO_PIN_2;
	//GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
	//GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
	//GPIO_InitStructure.Pull = GPIO_PULLUP;
	GPIO_InitStructure.Alternate = GPIO_AF_UARTx;
	HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
break;
#endif
and in uart.c deinit

Code: Select all

#if defined(UART5)
    } else if (uart->Instance == UART5) {
        HAL_NVIC_DisableIRQ(UART5_IRQn);
        __UART5_FORCE_RESET();
        __UART5_RELEASE_RESET();
        __UART5_CLK_DISABLE();
#endif

gradoj
Posts: 27
Joined: Mon Aug 11, 2014 5:47 pm

Re: Need some help setting up UART5

Post by gradoj » Sat May 30, 2015 8:36 pm

It looks like those pins are not getting set to their alternate functions:

Code: Select all

>>> a=pyb.Pin.board.PD2
>>> b=pyb.Pin.board.PC12
>>> a
Pin(Pin.cpu.D2, mode=Pin.IN)
>>> b
Pin(Pin.cpu.C12, mode=Pin.IN)
>>> a.af()
0
>>> b.af()
0
>>> a.af_list()
[Pin.AF2_TIM3, Pin.AF8_UART5]
>>> b.af_list()
[Pin.AF6_SPI3, Pin.AF7_USART3, Pin.AF8_UART5]

User avatar
dhylands
Posts: 3214
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Need some help setting up UART5

Post by dhylands » Sat May 30, 2015 9:04 pm

Your code seems to have the line which sets the alternate function commented out. In particular:

Code: Select all

   //GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
You really should initialize all of the fields, otherwise they get random values off the stack.

gradoj
Posts: 27
Joined: Mon Aug 11, 2014 5:47 pm

Re: Need some help setting up UART5

Post by gradoj » Sun May 31, 2015 3:37 am

I put the commented out lines back in but the pins still do not get configured for their alternate function. It appears something isn't working in the new code I added to initialize UART5. When I configured the pins manually for their alternate function of uart5 and send bytes to that uart the REPL stops responding. Any strategies on debugging something like this?

Damien
Site Admin
Posts: 614
Joined: Mon Dec 09, 2013 5:02 pm

Re: Need some help setting up UART5

Post by Damien » Sun May 31, 2015 6:55 am

A few things to check:

1. You need to define both the RX and TX pins in the MICROPY_HW_UART5_PINS macro.
2. You will need to add a UART5 irq handler in stm32f4xx_it.c.
3. You need to create and instance of the UART to set it up: pyb.UART(5, 9600).

gradoj
Posts: 27
Joined: Mon Aug 11, 2014 5:47 pm

Re: Need some help setting up UART5

Post by gradoj » Sun May 31, 2015 7:09 am

1. You need to define both the RX and TX pins in the MICROPY_HW_UART5_PINS macro.
2. You will need to add a UART5 irq handler in stm32f4xx_it.c.
3. You need to create and instance of the UART to set it up: pyb.UART(5, 9600).
1. tx and rx pins for uart5 are on different ports PC12 and PD12. I didnt see how I could use the existing macros. That is why I hardcoded the one pin in uart.c init for now. This may be messing something up?
2. I missed the irq handler. Thanks
3. I can send and receive on other uart ports so pretty sure im using it correctly

Damien
Site Admin
Posts: 614
Joined: Mon Dec 09, 2013 5:02 pm

Re: Need some help setting up UART5

Post by Damien » Sun May 31, 2015 7:52 am

Note that PD2 is used as the SD card CMD pin, so the SD card will need to be disabled.

Here is what I get for UART(4):

Code: Select all

>>> pyb.UART(4, 9600)
UART(4, baudrate=9600, ...)
>>> pyb.Pin.board.X1
Pin(Pin.cpu.A0, mode=Pin.AF_PP, pull=Pin.PULL_UP, af=Pin.AF8_UART4)
>>> pyb.Pin.board.X2
Pin(Pin.cpu.A1, mode=Pin.AF_PP, pull=Pin.PULL_UP, af=Pin.AF8_UART4)

gradoj
Posts: 27
Joined: Mon Aug 11, 2014 5:47 pm

Re: Need some help setting up UART5

Post by gradoj » Sun May 31, 2015 6:32 pm

I was assuming from the af() that the pins were not in alternative function mode but it looks like they are. What does the af() function tell me?

Code: Select all

>>> import pyb
>>> pyb.UART(5,115200)
UART(5, baudrate=115384, bits=8, parity=None, stop=1, timeout=1000, timeout_char=0, read_buf_len=64)
>>> pyb.Pin.board.PD2
Pin(Pin.cpu.D2, mode=Pin.AF_PP, pull=Pin.PULL_UP, af=Pin.AF8_UART5)
>>> pyb.Pin.board.PC12
Pin(Pin.cpu.C12, mode=Pin.AF_PP, pull=Pin.PULL_UP, af=Pin.AF8_UART5)
I don't see anything come out the uart with the write command. The read returns nothing like it should but as soon as I send a character to UART5 the REPL(which is on UART1) stops responding.

Code: Select all

>>> a=pyb.UART(5,115200)
>>> a.write('asdf')
4
>>> a.read()
b''

User avatar
dhylands
Posts: 3214
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Need some help setting up UART5

Post by dhylands » Sun May 31, 2015 7:04 pm

The Pin.af() functions tells you the currently configured alternate function for the pin.

But the af() is only meaningful when Pin.mode() reports one of pyb.Pin.AF_PP or pyb.Pin.AF_OD. For all other modes, what af() reports is irrelevant.

I wrote a little script which you can find in the examples directory called pins.py. If you copy pins.py and the pins_af.py (from the stmhal/build-PYBV10 directory for your board) onto the pyboard and then from the pyboard REPL do this:

Code: Select all

>>> import pins
>>> pins.pins()
X1         IN                          
X2         IN                          
X3         IN                          
X4         IN                          
X5         IN                          
X6         IN                          
X7         IN                          
X8         IN                          
X9         IN                          
X10        IN                          
X11        IN                          
X12        IN                          
X17        IN     PULL_UP              
X18        IN                          
X19        IN                          
X20        IN                          
X21        IN                          
X22        IN                          
Y1         AF_PP  PULL_UP 8: USART6_TX 
Y2         AF_PP  PULL_UP 8: USART6_RX 
Y3         IN                          
Y4         IN                          
Y5         IN                          
Y6         IN                          
Y7         IN                          
Y8         IN                          
Y9         IN                          
Y10        IN                          
Y11        IN                          
Y12        IN                          
SW         IN     PULL_UP              
LED_RED    OUT_PP                      
LED_GREEN  OUT_PP                      
LED_YELLOW OUT_PP                      
LED_BLUE   AF_PP          2: TIM3_CH1  
MMA_INT    IN                          
MMA_AVDD   OUT_PP                      
SD_D0      AF_PP  PULL_UP 12           
SD_D1      AF_PP  PULL_UP 12           
SD_D2      AF_PP  PULL_UP 12           
SD_D3      AF_PP  PULL_UP 12           
SD_CMD     AF_PP  PULL_UP 12           
SD_CK      AF_PP  PULL_UP 12           
SD         IN     PULL_UP              
SD_SW      IN     PULL_UP              
USB_VBUS   IN                          
USB_ID     AF_OD  PULL_UP 10           
USB_DM     AF_PP          10           
USB_DP     AF_PP          10 
Basically it reports the current configuration for each pin.

gradoj
Posts: 27
Joined: Mon Aug 11, 2014 5:47 pm

Re: Need some help setting up UART5

Post by gradoj » Sun May 31, 2015 8:34 pm

Thanks. Pins.py will be a handy little script. I forgot to mention that I have disabled the SD card as one of the UART5 pins overlaps with it so I have no external filesystem for now.

I guess I ran .af() before I initialized the uart before and got fooled into thinking the alternate function wasn't setup properly.

Code: Select all

>>> pc12=pyb.Pin.board.PC12
>>> pc12
Pin(Pin.cpu.C12, mode=Pin.IN)
>>> pc12.af()
0
>>> u5=pyb.UART(5,115200)
>>> u5
UART(5, baudrate=115384, bits=8, parity=None, stop=1, timeout=1000, timeout_char=0, read_buf_len=64)
>>> pc12
Pin(Pin.cpu.C12, mode=Pin.AF_PP, pull=Pin.PULL_UP, af=Pin.AF8_UART5)
>>> pc12.af()
8
>>>
So this is strange. It seems the uart5 is setup correctly but I cannot get anything out of it and if I write anything in to it something seems to crash.

Post Reply