blob: c24ac0a65fcaf0165bfa7d065ca94315ec926361 [file] [log] [blame]
Christina Quastc0aa7692015-02-25 14:02:01 +01001/* ----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2009, Atmel Corporation
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
13 *
14 * Atmel's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * ----------------------------------------------------------------------------
28 */
29
30/*------------------------------------------------------------------------------
31 * Headers
32 *------------------------------------------------------------------------------*/
33
34#include "board.h"
Harald Welte16055642016-03-03 11:02:45 +010035#include "simtrace.h"
36#include "ringbuffer.h"
Harald Welte30a53f82015-11-08 14:29:55 +010037#include "iso7816_fidi.h"
Christina Quastc0aa7692015-02-25 14:02:01 +010038
39#include <string.h>
Christina Quastcb646bc2015-05-03 14:21:26 +020040#include <errno.h>
Christina Quastc0aa7692015-02-25 14:02:01 +010041
Christina Quast95d66162015-04-09 22:38:47 +020042volatile uint32_t char_stat;
Christina Quastc0aa7692015-02-25 14:02:01 +010043
Christina Quast4db82e02015-04-11 18:14:41 +020044volatile ringbuf sim_rcv_buf = { {0}, 0, 0 };
Christina Quast4ba4d222015-03-02 16:14:09 +010045
Christina Quaste4cbfe62015-04-20 13:07:28 +020046/*-----------------------------------------------------------------------------
47 * Interrupt routines
48 *-----------------------------------------------------------------------------*/
Harald Welteec4fe232015-11-07 18:41:25 +010049static void Callback_PhoneRST_ISR( uint8_t *pArg, uint8_t status, uint32_t transferred, uint32_t remaining)
Christina Quaste4cbfe62015-04-20 13:07:28 +020050{
51 printf("rstCB\n\r");
52 PIO_EnableIt( &pinPhoneRST ) ;
53}
54void ISR_PhoneRST( const Pin *pPin)
55{
56 int ret;
57 // FIXME: no printfs in ISRs?
58 printf("+++ Int!! %x\n\r", pinPhoneRST.pio->PIO_ISR);
59 if ( ((pinPhoneRST.pio->PIO_ISR & pinPhoneRST.mask) != 0) )
60 {
61 if(PIO_Get( &pinPhoneRST ) == 0) {
62 printf(" 0 ");
63 } else {
64 printf(" 1 ");
65 }
66 }
67
68 if ((ret = USBD_Write( PHONE_INT, "R", 1, (TransferCallback)&Callback_PhoneRST_ISR, 0 )) != USBD_STATUS_SUCCESS) {
69 TRACE_ERROR("USB err status: %d (%s)\n", ret, __FUNCTION__);
70 return;
71 }
72
73 /* Interrupt enabled after ATR is sent to phone */
74 PIO_DisableIt( &pinPhoneRST ) ;
75}
76
Harald Welteacd48c52016-03-02 10:34:24 +010077extern void usart_irq_rx(uint8_t num);
Christina Quast4db82e02015-04-11 18:14:41 +020078/*
79 * char_stat is zero if no error occured.
80 * Otherwise it is filled with the content of the status register.
Christina Quastc0aa7692015-02-25 14:02:01 +010081 */
Christina Quast4db82e02015-04-11 18:14:41 +020082void USART1_IrqHandler( void )
83{
Harald Welteacd48c52016-03-02 10:34:24 +010084#if 0
Christina Quast4db82e02015-04-11 18:14:41 +020085 uint32_t stat;
Christina Quastc0aa7692015-02-25 14:02:01 +010086 char_stat = 0;
87 // Rcv buf full
Christina Quast4db82e02015-04-11 18:14:41 +020088/* if((stat & US_CSR_RXBUFF) == US_CSR_RXBUFF) {
Christina Quastc0aa7692015-02-25 14:02:01 +010089 TRACE_DEBUG("Rcv buf full");
Christina Quast4db82e02015-04-11 18:14:41 +020090 USART_DisableIt(USART1, US_IDR_RXBUFF);
91 }
92*/
93 uint32_t csr = USART_PHONE->US_CSR;
Christina Quast4ba4d222015-03-02 16:14:09 +010094
Christina Quastc0aa7692015-02-25 14:02:01 +010095 if (csr & US_CSR_TXRDY) {
Christina Quast4db82e02015-04-11 18:14:41 +020096 /* transmit buffer empty, nothing to transmit */
97 }
Christina Quastc0aa7692015-02-25 14:02:01 +010098 if (csr & US_CSR_RXRDY) {
99 stat = (csr&(US_CSR_OVRE|US_CSR_FRAME|
Christina Quast4ba4d222015-03-02 16:14:09 +0100100 US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK|
101 (1<<10)));
Christina Quast2b8a18b2015-04-12 09:31:36 +0200102 uint8_t c = (USART_PHONE->US_RHR) & 0xFF;
Christina Quasta1dd0ef2015-03-22 19:06:12 +0100103// printf(" %x", c);
Christina Quast4ba4d222015-03-02 16:14:09 +0100104
Christina Quastc0aa7692015-02-25 14:02:01 +0100105 if (stat == 0 ) {
Christina Quast4ba4d222015-03-02 16:14:09 +0100106 /* Fill char into buffer */
Christina Quast2b8a18b2015-04-12 09:31:36 +0200107 rbuf_write(&sim_rcv_buf, c);
Christina Quast4ba4d222015-03-02 16:14:09 +0100108 } else {
Christina Quast4a30a372015-05-08 15:34:25 +0200109 TRACE_DEBUG("e %x st: %x\n", c, stat);
Christina Quast4ba4d222015-03-02 16:14:09 +0100110 } /* else: error occured */
Christina Quastc0aa7692015-02-25 14:02:01 +0100111
Christina Quast4ba4d222015-03-02 16:14:09 +0100112 char_stat = stat;
113 }
Harald Welteacd48c52016-03-02 10:34:24 +0100114#else
115 usart_irq_rx(0);
116#endif
Christina Quast4ba4d222015-03-02 16:14:09 +0100117}
Christina Quastcb646bc2015-05-03 14:21:26 +0200118
119/* FIDI update functions */
Christina Quastcb646bc2015-05-03 14:21:26 +0200120void update_fidi(uint8_t fidi)
121{
122 int rc;
123
124 uint8_t fi = fidi >> 4;
125 uint8_t di = fidi & 0xf;
126
127 rc = compute_fidi_ratio(fi, di);
128 if (rc > 0 && rc < 0x400) {
129 TRACE_INFO("computed Fi(%u) Di(%u) ratio: %d", fi, di, rc);
130/* make sure UART uses new F/D ratio */
131 USART_PHONE->US_CR |= US_CR_RXDIS | US_CR_RSTRX;
132 USART_PHONE->US_FIDI = rc & 0x3ff;
133 USART_PHONE->US_CR |= US_CR_RXEN | US_CR_STTTO;
134 } else
135 TRACE_INFO("computed FiDi ratio %d unsupported", rc);
136}