blob: 452bcc8615aa07dab9b64c8d2521a85ef4cb03c6 [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"
35
36#include <string.h>
Christina Quastcb646bc2015-05-03 14:21:26 +020037#include <errno.h>
Christina Quastc0aa7692015-02-25 14:02:01 +010038
Christina Quast95d66162015-04-09 22:38:47 +020039volatile uint32_t char_stat;
Christina Quastc0aa7692015-02-25 14:02:01 +010040
Christina Quast4ba4d222015-03-02 16:14:09 +010041// FIXME: Remove:
Christina Quasta1dd0ef2015-03-22 19:06:12 +010042#define PR TRACE_INFO
Christina Quast4ba4d222015-03-02 16:14:09 +010043//#define PR printf
44
Christina Quast4db82e02015-04-11 18:14:41 +020045volatile ringbuf sim_rcv_buf = { {0}, 0, 0 };
Christina Quast4ba4d222015-03-02 16:14:09 +010046
Christina Quaste4cbfe62015-04-20 13:07:28 +020047/*-----------------------------------------------------------------------------
48 * Interrupt routines
49 *-----------------------------------------------------------------------------*/
50void Callback_PhoneRST_ISR( uint8_t *pArg, uint8_t status, uint32_t transferred, uint32_t remaining)
51{
52 printf("rstCB\n\r");
53 PIO_EnableIt( &pinPhoneRST ) ;
54}
55void ISR_PhoneRST( const Pin *pPin)
56{
57 int ret;
58 // FIXME: no printfs in ISRs?
59 printf("+++ Int!! %x\n\r", pinPhoneRST.pio->PIO_ISR);
60 if ( ((pinPhoneRST.pio->PIO_ISR & pinPhoneRST.mask) != 0) )
61 {
62 if(PIO_Get( &pinPhoneRST ) == 0) {
63 printf(" 0 ");
64 } else {
65 printf(" 1 ");
66 }
67 }
68
69 if ((ret = USBD_Write( PHONE_INT, "R", 1, (TransferCallback)&Callback_PhoneRST_ISR, 0 )) != USBD_STATUS_SUCCESS) {
70 TRACE_ERROR("USB err status: %d (%s)\n", ret, __FUNCTION__);
71 return;
72 }
73
74 /* Interrupt enabled after ATR is sent to phone */
75 PIO_DisableIt( &pinPhoneRST ) ;
76}
77
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{
84 uint32_t stat;
Christina Quastc0aa7692015-02-25 14:02:01 +010085 char_stat = 0;
86 // Rcv buf full
Christina Quast4db82e02015-04-11 18:14:41 +020087/* if((stat & US_CSR_RXBUFF) == US_CSR_RXBUFF) {
Christina Quastc0aa7692015-02-25 14:02:01 +010088 TRACE_DEBUG("Rcv buf full");
Christina Quast4db82e02015-04-11 18:14:41 +020089 USART_DisableIt(USART1, US_IDR_RXBUFF);
90 }
91*/
92 uint32_t csr = USART_PHONE->US_CSR;
Christina Quast4ba4d222015-03-02 16:14:09 +010093
Christina Quastc0aa7692015-02-25 14:02:01 +010094 if (csr & US_CSR_TXRDY) {
Christina Quast4db82e02015-04-11 18:14:41 +020095 /* transmit buffer empty, nothing to transmit */
96 }
Christina Quastc0aa7692015-02-25 14:02:01 +010097 if (csr & US_CSR_RXRDY) {
98 stat = (csr&(US_CSR_OVRE|US_CSR_FRAME|
Christina Quast4ba4d222015-03-02 16:14:09 +010099 US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK|
100 (1<<10)));
Christina Quast2b8a18b2015-04-12 09:31:36 +0200101 uint8_t c = (USART_PHONE->US_RHR) & 0xFF;
Christina Quasta1dd0ef2015-03-22 19:06:12 +0100102// printf(" %x", c);
Christina Quast4ba4d222015-03-02 16:14:09 +0100103
Christina Quastc0aa7692015-02-25 14:02:01 +0100104 if (stat == 0 ) {
Christina Quast4ba4d222015-03-02 16:14:09 +0100105 /* Fill char into buffer */
Christina Quast2b8a18b2015-04-12 09:31:36 +0200106 rbuf_write(&sim_rcv_buf, c);
Christina Quast4ba4d222015-03-02 16:14:09 +0100107 } else {
Christina Quast2b8a18b2015-04-12 09:31:36 +0200108 PR("e %x st: %x\n", c, stat);
Christina Quast4ba4d222015-03-02 16:14:09 +0100109 } /* else: error occured */
Christina Quastc0aa7692015-02-25 14:02:01 +0100110
Christina Quast4ba4d222015-03-02 16:14:09 +0100111 char_stat = stat;
112 }
113}
Christina Quastcb646bc2015-05-03 14:21:26 +0200114
115/* FIDI update functions */
116#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
117
118/* Table 6 from ISO 7816-3 */
119static const uint16_t fi_table[] = {
120 0, 372, 558, 744, 1116, 1488, 1860, 0,
121 0, 512, 768, 1024, 1536, 2048, 0, 0
122};
123
124/* Table 7 from ISO 7816-3 */
125static const uint8_t di_table[] = {
126 0, 1, 2, 4, 8, 16, 0, 0,
127 0, 0, 2, 4, 8, 16, 32, 64,
128};
129
130/* compute the F/D ratio based on Fi and Di values */
131static int compute_fidi_ratio(uint8_t fi, uint8_t di)
132{
133 uint16_t f, d;
134 int ret;
135
136 if (fi >= ARRAY_SIZE(fi_table) ||
137 di >= ARRAY_SIZE(di_table))
138 return -EINVAL;
139
140 f = fi_table[fi];
141 if (f == 0)
142 return -EINVAL;
143
144 d = di_table[di];
145 if (d == 0)
146 return -EINVAL;
147
148 if (di < 8)
149 ret = f / d;
150 else
151 ret = f * d;
152
153 return ret;
154}
155
156void update_fidi(uint8_t fidi)
157{
158 int rc;
159
160 uint8_t fi = fidi >> 4;
161 uint8_t di = fidi & 0xf;
162
163 rc = compute_fidi_ratio(fi, di);
164 if (rc > 0 && rc < 0x400) {
165 TRACE_INFO("computed Fi(%u) Di(%u) ratio: %d", fi, di, rc);
166/* make sure UART uses new F/D ratio */
167 USART_PHONE->US_CR |= US_CR_RXDIS | US_CR_RSTRX;
168 USART_PHONE->US_FIDI = rc & 0x3ff;
169 USART_PHONE->US_CR |= US_CR_RXEN | US_CR_STTTO;
170 } else
171 TRACE_INFO("computed FiDi ratio %d unsupported", rc);
172}