Page 1 of 1

Serial port in SmartCard mode

Posted: Sat Apr 13, 2013 12:22 pm
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.

Re: Serial port in SmartCard mode

Posted: Sat Apr 13, 2013 1:49 pm
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

Re: Serial port in SmartCard mode

Posted: Sat Apr 13, 2013 2:13 pm
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.

Re: Serial port in SmartCard mode

Posted: Sat Apr 13, 2013 4:32 pm
by Giovanni
How many bytes are sent by the ATR on that card?

Giovanni

Re: Serial port in SmartCard mode

Posted: Sat Apr 13, 2013 4:41 pm
by serdz
Hi,

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

Thanks

Re: Serial port in SmartCard mode

Posted: Sat Apr 13, 2013 4:47 pm
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

Re: Serial port in SmartCard mode

Posted: Sat Apr 13, 2013 5:14 pm
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..

Re:[SOLVED] Serial port in SmartCard mode

Posted: Sat Apr 13, 2013 8:27 pm
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 ).

Re: Serial port in SmartCard mode

Posted: Sat Apr 13, 2013 8:35 pm
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

Re: Serial port in SmartCard mode

Posted: Sat Apr 13, 2013 8:42 pm
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 :).