USART2

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
pagano.paganino
Posts: 89
Joined: Fri Sep 11, 2015 10:47 pm
Location: Italy

USART2

Post by pagano.paganino » Sat Sep 12, 2015 3:52 pm

Hi,
I wanted to know if it possible use the USART (in particular USART2 - from stm32f405_af.csv AF7 PA2 as TX and PA4 as CLOCK) for interfacing with a Smart Card with the pyboard 1.0 and if yes, can you please direct me?

STM32CubeF4 includes the necessary libraries but are not included in hal folder how can I use them?

Is there any measure to be taken to include them and use them properly?

Thanks Francesco

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

Re: USART2

Post by dhylands » Sun Sep 13, 2015 7:36 am

Damien made a decision to only include the HAL files which were actually being used in order to keep the size of the HAL reasonable (there are currently 3 different HALs - one for F2, F4, and F7)

So you'd need to goto the STMHAL and grab the files you need, copy them in, and then make appropriate changes to use them.

You probably want to bring this up as an issue on github to discuss the best way to make use of the new functions (i.e. what arguments should be added to the uart class etc).

It may also be possible to use the stm module to directly manipulate the registers to get the functionality you need. It depends on how complicated that would turn out to be.

pagano.paganino
Posts: 89
Joined: Fri Sep 11, 2015 10:47 pm
Location: Italy

Re: USART2

Post by pagano.paganino » Sun Sep 13, 2015 10:48 am

Hi Dave,
Thanks for the reply.

I tried to add the hal libraries stm32f4xx_hal_usart. c/h stm32f4xx_hal_smartcard. c/h into f4 enabled in stm32f4xx_hal_conf. h file pybv10 and added a simple example of a smartcard. c/h and all are built correctly.

I initialize the gpio and USART2 needed, but I don't have any signal from the USART2 clock.

Paste the init function:

Code: Select all

/* USART2 init function */
void MX_USART2_SMARTCARD_Init(SMARTCARD_HandleTypeDef smartcard) {

    /* GPIO Ports Clock Enable */
    mp_hal_gpio_clock_enable(GPIOA);

    /* Configure Smartcard TX pin */
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.Pin = GPIO_PIN_2;
    GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
    GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStructure.Pull = GPIO_PULLUP;
    GPIO_InitStructure.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);

    __USART2_CLK_ENABLE();

    smartcard.Instance = USART2;
    smartcard.Init.BaudRate = 10753;
    smartcard.Init.WordLength = SMARTCARD_WORDLENGTH_9B;
    smartcard.Init.StopBits = SMARTCARD_STOPBITS_1_5;
    smartcard.Init.Parity = SMARTCARD_PARITY_EVEN;
    smartcard.Init.Mode = SMARTCARD_MODE_TX_RX;
    smartcard.Init.CLKPolarity = SMARTCARD_POLARITY_LOW;
    smartcard.Init.CLKPhase = SMARTCARD_PHASE_1EDGE;
    smartcard.Init.CLKLastBit = SMARTCARD_LASTBIT_DISABLE;
    smartcard.Init.Prescaler = 6;
    smartcard.Init.GuardTime = 12;
    smartcard.Init.NACKState = SMARTCARD_NACK_ENABLED;

    HAL_SMARTCARD_Init(&smartcard);

    HAL_NVIC_DisableIRQ(USART2_IRQn);
    __SMARTCARD_DISABLE_IT(&smartcard, SMARTCARD_IT_RXNE);

}

Any suggestions?
I skipped some steps to enable USART2?

Thanks Francesco

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

Re: USART2

Post by dhylands » Mon Sep 14, 2015 4:23 pm

Your code seems to have initialized the Tx pin, but I don't see any code to initialize the clock pin.

By default, when the processor RESETs, all pins are configured as GPIO inputs.

pagano.paganino
Posts: 89
Joined: Fri Sep 11, 2015 10:47 pm
Location: Italy

Re: USART2

Post by pagano.paganino » Mon Sep 14, 2015 8:27 pm

Thanks Dave,
added this:

Code: Select all

    /* Configure USART Clock pin as alternate function push-pull */
    GPIO_InitStruct.Pin = GPIO_PIN_4;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
and now i have usart2 clock signal.

Thanks ;)

pagano.paganino
Posts: 89
Joined: Fri Sep 11, 2015 10:47 pm
Location: Italy

Re: USART2

Post by pagano.paganino » Tue Sep 15, 2015 1:49 pm

Hi Dave,
how i can set USART2 clock to work at 4Mhz?

Thanks Francesco

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

Re: USART2

Post by dhylands » Tue Sep 15, 2015 3:15 pm

Isn't that just by setting BaudRate to 4 MHz?

pagano.paganino
Posts: 89
Joined: Fri Sep 11, 2015 10:47 pm
Location: Italy

Re: USART2

Post by pagano.paganino » Fri Sep 18, 2015 7:30 am

Hi Dave,
i must set 4MHz via prescaler of HAL_SmartCard:

Code: Select all

    smartcard.Instance = USART2;
    smartcard.Init.BaudRate = 10753;
    smartcard.Init.WordLength = SMARTCARD_WORDLENGTH_9B;
    smartcard.Init.StopBits = SMARTCARD_STOPBITS_1_5;
    smartcard.Init.Parity = SMARTCARD_PARITY_EVEN;
    smartcard.Init.Mode = SMARTCARD_MODE_TX_RX;
    smartcard.Init.CLKPolarity = SMARTCARD_POLARITY_LOW;
    smartcard.Init.CLKPhase = SMARTCARD_PHASE_1EDGE;
    smartcard.Init.CLKLastBit = SMARTCARD_LASTBIT_DISABLE;
    smartcard.Init.Prescaler = 21;
    smartcard.Init.GuardTime = 12;
    smartcard.Init.NACKState = SMARTCARD_NACK_ENABLED;
USART2 is on 84MHz for set to 4MHz is correct set prescaler to 21?

Thanks Francesco

manitou
Posts: 73
Joined: Wed Feb 25, 2015 12:15 am

Re: USART2

Post by manitou » Fri Sep 18, 2015 12:02 pm

pagano.paganino wrote: USART2 is on 84MHz for set to 4MHz is correct set prescaler to 21?
As I read chapter 7 of ref manual, USART2 is on APB1 which runs at 42MHz. you could verify your 4MHz with scope or analyzer.

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

Re: USART2

Post by dhylands » Fri Sep 18, 2015 4:27 pm

The datasheet also mentions (in the section that USART register section for USART_GPTR register) that for smartcard mode that the value given in the register is multiplied by 2 to come up with the divider.

Given that USART2 is on APB1, which runs at 42 MHz, this means that the closest you can get is to use a prescalar value of 5 which will result in 4.2 MHz.

If you drop the CPU frequency down from 168MHz to 160MHz, then APB1 will be clocked at 40 MHz, and a prescalar value of 5 will give you your desired 4 MHz frequency.

Post Reply