blob: 466bd657de99796402c803eb6ea2e940d29e807d [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 Welte7dd3dfd2016-03-03 12:32:04 +010049static void Callback_PhoneRST_ISR(uint8_t * pArg, uint8_t status,
50 uint32_t transferred, uint32_t remaining)
Christina Quaste4cbfe62015-04-20 13:07:28 +020051{
Harald Welte7dd3dfd2016-03-03 12:32:04 +010052 printf("rstCB\n\r");
53 PIO_EnableIt(&pinPhoneRST);
Christina Quaste4cbfe62015-04-20 13:07:28 +020054}
Harald Welte7dd3dfd2016-03-03 12:32:04 +010055
56void ISR_PhoneRST(const Pin * pPin)
Christina Quaste4cbfe62015-04-20 13:07:28 +020057{
Harald Welte7dd3dfd2016-03-03 12:32:04 +010058 int ret;
59 // FIXME: no printfs in ISRs?
60 printf("+++ Int!! %x\n\r", pinPhoneRST.pio->PIO_ISR);
61 if (((pinPhoneRST.pio->PIO_ISR & pinPhoneRST.mask) != 0)) {
62 if (PIO_Get(&pinPhoneRST) == 0) {
63 printf(" 0 ");
64 } else {
65 printf(" 1 ");
66 }
67 }
Christina Quaste4cbfe62015-04-20 13:07:28 +020068
Harald Welte7dd3dfd2016-03-03 12:32:04 +010069 if ((ret =
70 USBD_Write(PHONE_INT, "R", 1,
71 (TransferCallback) & Callback_PhoneRST_ISR,
72 0)) != USBD_STATUS_SUCCESS) {
73 TRACE_ERROR("USB err status: %d (%s)\n", ret, __FUNCTION__);
74 return;
75 }
Christina Quaste4cbfe62015-04-20 13:07:28 +020076
Harald Welte7dd3dfd2016-03-03 12:32:04 +010077 /* Interrupt enabled after ATR is sent to phone */
78 PIO_DisableIt(&pinPhoneRST);
Christina Quaste4cbfe62015-04-20 13:07:28 +020079}
80
Christina Quast4db82e02015-04-11 18:14:41 +020081/*
82 * char_stat is zero if no error occured.
83 * Otherwise it is filled with the content of the status register.
Christina Quastc0aa7692015-02-25 14:02:01 +010084 */
Harald Welte3bafe432016-03-20 16:43:12 +010085void mode_trace_usart1_irq(void)
Christina Quast4db82e02015-04-11 18:14:41 +020086{
Harald Welte7dd3dfd2016-03-03 12:32:04 +010087 uint32_t stat;
88 char_stat = 0;
89 // Rcv buf full
90/* if((stat & US_CSR_RXBUFF) == US_CSR_RXBUFF) {
91 TRACE_DEBUG("Rcv buf full");
92 USART_DisableIt(USART1, US_IDR_RXBUFF);
93 }
Christina Quast4db82e02015-04-11 18:14:41 +020094*/
Harald Welte7dd3dfd2016-03-03 12:32:04 +010095 uint32_t csr = USART_PHONE->US_CSR;
Christina Quast4ba4d222015-03-02 16:14:09 +010096
Harald Welte7dd3dfd2016-03-03 12:32:04 +010097 if (csr & US_CSR_TXRDY) {
98 /* transmit buffer empty, nothing to transmit */
99 }
100 if (csr & US_CSR_RXRDY) {
101 stat = (csr & (US_CSR_OVRE | US_CSR_FRAME |
102 US_CSR_PARE | US_CSR_TIMEOUT | US_CSR_NACK |
103 (1 << 10)));
104 uint8_t c = (USART_PHONE->US_RHR) & 0xFF;
105// printf(" %x", c);
Christina Quast4ba4d222015-03-02 16:14:09 +0100106
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100107 if (stat == 0) {
108 /* Fill char into buffer */
109 rbuf_write(&sim_rcv_buf, c);
110 } else {
111 TRACE_DEBUG("e %x st: %x\n", c, stat);
112 } /* else: error occured */
Christina Quastc0aa7692015-02-25 14:02:01 +0100113
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100114 char_stat = stat;
115 }
Christina Quast4ba4d222015-03-02 16:14:09 +0100116}
Christina Quastcb646bc2015-05-03 14:21:26 +0200117
118/* FIDI update functions */
Kévin Redon4fe99fa2018-06-25 15:53:19 +0200119void update_fidi(Usart_info *usart, uint8_t fidi)
Christina Quastcb646bc2015-05-03 14:21:26 +0200120{
Kévin Redon4fe99fa2018-06-25 15:53:19 +0200121 if (NULL==usart) {
122 return;
123 }
Christina Quastcb646bc2015-05-03 14:21:26 +0200124
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100125 uint8_t fi = fidi >> 4;
126 uint8_t di = fidi & 0xf;
Kévin Redon4fe99fa2018-06-25 15:53:19 +0200127 int ratio = compute_fidi_ratio(fi, di);
Christina Quastcb646bc2015-05-03 14:21:26 +0200128
Kévin Redon4fe99fa2018-06-25 15:53:19 +0200129 if (ratio > 0 && ratio < 0x8000) {
130 /* make sure USART uses new F/D ratio */
131 usart->base->US_CR |= US_CR_RXDIS | US_CR_RSTRX;
132 /* disable write protection */
133 if (usart->base->US_WPMR) {
134 usart->base->US_WPMR = US_WPMR_WPKEY(0x555341);
135 }
136 usart->base->US_FIDI = (ratio & 0x7ff);
137 usart->base->US_CR |= US_CR_RXEN | US_CR_STTTO;
Kévin Redon3113e3d2018-06-27 16:29:38 +0200138 //TRACE_INFO("updated USART(%u) Fi(%u)/Di(%u) ratio(%d): %u\n\r", usart->id, fi, di, ratio, usart->base->US_FIDI);
Kévin Redon4fe99fa2018-06-25 15:53:19 +0200139 } else {
Kévin Redon3113e3d2018-06-27 16:29:38 +0200140 //TRACE_WARNING("computed Fi/Di ratio %d unsupported\n\r", ratio); /* don't print since this is function is also called by ISRs */
Kévin Redon4fe99fa2018-06-25 15:53:19 +0200141 }
Christina Quastcb646bc2015-05-03 14:21:26 +0200142}