Serial port in SmartCard mode

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
serdz
Posts: 11
Joined: Sat Mar 30, 2013 7:37 pm

Serial port in SmartCard mode

Post by serdz »

Hi All,

I'm trying to enable SmartCard mode for SD3, everything looks fine except my application not receive any data from IO line.
In oscilloscope I see that SIM card response with ATR.

That how I initialize serial port:

Code: Select all


/*
 * Port C setup.
 * Everything input with pull-up except:
 * PC8 - Push Pull output   (SMART_CARD1_RST).
 * PC9 - Push Pull output   (SMART_CARD1_3_5V).
 * PC10 - Alternate open drain  (SMART_CARD1_IO).
 * PC11 - Normal input      (SMART_CARD1_OFF).
 * PC12 - Alternate output  (SMART_CARD1_CLK).
 * PC13 - Push Pull output  (SMART_CARD1_VCC).
 */
#define VAL_GPIOCCRL            0x88888888      /*  PC7...PC0 */
#define VAL_GPIOCCRH            0x883B4F33      /* PC15...PC8 */
#define VAL_GPIOCODR            0xFFFFFEFF

static SerialConfig scard1_serial_cfg = {
    9677,                                       // 3.6 MHZ / 372
    USART_CR1_M | USART_CR1_PCE,
    USART_CR2_CLKEN | USART_CR2_STOP1P5_BITS | USART_CR2_LBCL,
    USART_CR3_SCEN | USART_CR3_NACK
};
...

AFIO->MAPR |= AFIO_MAPR_USART3_REMAP_PARTIALREMAP;
sdStart( &SD3,  scard1_serial_cfg);
/* Clear the USART prescaler */
    USART3->GTPR &= GTPR_MSB_Mask;
    /* Set the USART prescaler */
    // 36 HMz / 10 = 3.6 MHz clock for CmartCart ( 9677 b/s ) 
    USART3->GTPR |= 0x05;       // clock / 10 ; see datasheat
 
    /* Clear the USART Guard time */
    USART3->GTPR &= GTPR_LSB_Mask;
    /* Set the USART guard time */
    USART3->GTPR |= (uint16_t)((uint16_t)16 << 0x08);

// Here I powerup smartcart and waiting for ATR

if ( chSequentialStreamRead((BaseChannel *)&SD3, (uint8_t *)&atr, 40) )
        {
            DBGHEXDUMP(DBG_TRACE,atr,0,40,"Got ART");
            sleep(60000);   
        }



Thanks.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Serial port in SmartCard mode

Post by Giovanni »

Hi,

I never tested the smart card mode, I would check if I/O pins are configured correctly, the external pull-up.

Is RX wired with TX in smart card mode or you have to wire it externally? not sure about this detail.

Also note that chSequentialStreamRead() does not return until the whole specified amount has been read.

Giovanni
serdz
Posts: 11
Joined: Sat Mar 30, 2013 7:37 pm

Re: Serial port in SmartCard mode

Post by serdz »

Hi,

Actually I'm using design from http://www.st.com/st-web-ui/static/acti ... 166510.pdf.
I'm also modified example code from ST and it's works fine.
I double checked all USART3 and GPIOC registers in both projects and they are almost the same.

Thanks.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Serial port in SmartCard mode

Post by Giovanni »

How many bytes are sent by the ATR on that card?

Giovanni
serdz
Posts: 11
Joined: Sat Mar 30, 2013 7:37 pm

Re: Serial port in SmartCard mode

Post by serdz »

Hi,

Should be 40, but I'm also tryied to wait for single byte without success.

Thanks
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Serial port in SmartCard mode

Post by Giovanni »

The only reasons I can think of are:
1) I/O setup
2) Wrong baud rate.
3) Wrong communication parameters (parity, frame length).

Try placing a breakpoint in the ISR and see if it is triggered at all, because an input byte or a communication error.

Giovanni
serdz
Posts: 11
Joined: Sat Mar 30, 2013 7:37 pm

Re: Serial port in SmartCard mode

Post by serdz »

ISR not executed at all.

I guess in case of wrong baud rate or parameters ISR should be triggered in any case.
Only thing that come in my mind is I/O setup. But when I'm sending something to serial port
I see that in oscilloscope, so transmission works well.
I will try to connect logic analyzer, maybe it will help a little bit.

Thanks..
serdz
Posts: 11
Joined: Sat Mar 30, 2013 7:37 pm

Re:[SOLVED] Serial port in SmartCard mode

Post by serdz »

Hi all,

Just solve my problem. Very strange but after these changes I able to receive data from SmartCard:

Code: Select all

static SerialConfig scard1_serial_cfg = {
    9677,                                       // 3.6 MHZ / 372
    USART_CR1_M | USART_CR1_PCE,
    USART_CR2_CLKEN | USART_CR2_STOP1P5_BITS | USART_CR2_LBCL,
    0
};
...
sdStart( &SD3,  scard1_serial_cfg );
USART3->CR3 |= USART_CR3_NACK | USART_CR3_SCEN;



From serial configuration I removed CR3 registers and manually put it
after after stating serial driver.

Looks like undocumented feature of stm32f103 ( I've never seen in datasheet something about "spatial sequence" for initialization serial port ).
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Serial port in SmartCard mode

Post by Giovanni »

Hi,

Good to know. You may also look into the "UART" driver, it uses the DMA and has an unbuffered, callback-based, API, it is more suited for block-oriented protocols than the serial driver (but it is harder to use probably).

Giovanni
serdz
Posts: 11
Joined: Sat Mar 30, 2013 7:37 pm

Re: Serial port in SmartCard mode

Post by serdz »

Thanks for comment,

Actually I'm not decided yet what driver to use.
I just want to quick check if my board working with SmartCard. But as usually quick maybe more than few days :).
Post Reply