Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 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 Welte | 1605564 | 2016-03-03 11:02:45 +0100 | [diff] [blame] | 35 | #include "simtrace.h" |
Kévin Redon | a1012b1 | 2018-07-01 18:11:01 +0200 | [diff] [blame] | 36 | #include "simtrace_usb.h" |
Harald Welte | 1605564 | 2016-03-03 11:02:45 +0100 | [diff] [blame] | 37 | #include "ringbuffer.h" |
Harald Welte | 30a53f8 | 2015-11-08 14:29:55 +0100 | [diff] [blame] | 38 | #include "iso7816_fidi.h" |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 39 | |
| 40 | #include <string.h> |
Christina Quast | cb646bc | 2015-05-03 14:21:26 +0200 | [diff] [blame] | 41 | #include <errno.h> |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 42 | |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 43 | volatile uint32_t char_stat; |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 44 | |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 45 | volatile ringbuf sim_rcv_buf = { {0}, 0, 0 }; |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 46 | |
Christina Quast | e4cbfe6 | 2015-04-20 13:07:28 +0200 | [diff] [blame] | 47 | /*----------------------------------------------------------------------------- |
| 48 | * Interrupt routines |
| 49 | *-----------------------------------------------------------------------------*/ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 50 | static void Callback_PhoneRST_ISR(uint8_t * pArg, uint8_t status, |
| 51 | uint32_t transferred, uint32_t remaining) |
Christina Quast | e4cbfe6 | 2015-04-20 13:07:28 +0200 | [diff] [blame] | 52 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 53 | printf("rstCB\n\r"); |
| 54 | PIO_EnableIt(&pinPhoneRST); |
Christina Quast | e4cbfe6 | 2015-04-20 13:07:28 +0200 | [diff] [blame] | 55 | } |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 56 | |
| 57 | void ISR_PhoneRST(const Pin * pPin) |
Christina Quast | e4cbfe6 | 2015-04-20 13:07:28 +0200 | [diff] [blame] | 58 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 59 | int ret; |
| 60 | // FIXME: no printfs in ISRs? |
Harald Welte | c394109 | 2018-08-26 09:53:13 +0200 | [diff] [blame] | 61 | printf("+++ Int!! %lx\n\r", pinPhoneRST.pio->PIO_ISR); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 62 | if (((pinPhoneRST.pio->PIO_ISR & pinPhoneRST.mask) != 0)) { |
| 63 | if (PIO_Get(&pinPhoneRST) == 0) { |
| 64 | printf(" 0 "); |
| 65 | } else { |
| 66 | printf(" 1 "); |
| 67 | } |
| 68 | } |
Christina Quast | e4cbfe6 | 2015-04-20 13:07:28 +0200 | [diff] [blame] | 69 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 70 | if ((ret = |
Kévin Redon | a1012b1 | 2018-07-01 18:11:01 +0200 | [diff] [blame] | 71 | USBD_Write(SIMTRACE_USB_EP_PHONE_INT, "R", 1, |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 72 | (TransferCallback) & Callback_PhoneRST_ISR, |
| 73 | 0)) != USBD_STATUS_SUCCESS) { |
| 74 | TRACE_ERROR("USB err status: %d (%s)\n", ret, __FUNCTION__); |
| 75 | return; |
| 76 | } |
Christina Quast | e4cbfe6 | 2015-04-20 13:07:28 +0200 | [diff] [blame] | 77 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 78 | /* Interrupt enabled after ATR is sent to phone */ |
| 79 | PIO_DisableIt(&pinPhoneRST); |
Christina Quast | e4cbfe6 | 2015-04-20 13:07:28 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 82 | /* |
Kévin Redon | 1cfc25e | 2019-05-14 21:26:48 +0200 | [diff] [blame^] | 83 | * char_stat is zero if no error occurred. |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 84 | * Otherwise it is filled with the content of the status register. |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 85 | */ |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 86 | void mode_trace_usart1_irq(void) |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 87 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 88 | uint32_t stat; |
| 89 | char_stat = 0; |
| 90 | // Rcv buf full |
| 91 | /* if((stat & US_CSR_RXBUFF) == US_CSR_RXBUFF) { |
| 92 | TRACE_DEBUG("Rcv buf full"); |
| 93 | USART_DisableIt(USART1, US_IDR_RXBUFF); |
| 94 | } |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 95 | */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 96 | uint32_t csr = USART_PHONE->US_CSR; |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 97 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 98 | if (csr & US_CSR_TXRDY) { |
| 99 | /* transmit buffer empty, nothing to transmit */ |
| 100 | } |
| 101 | if (csr & US_CSR_RXRDY) { |
| 102 | stat = (csr & (US_CSR_OVRE | US_CSR_FRAME | |
| 103 | US_CSR_PARE | US_CSR_TIMEOUT | US_CSR_NACK | |
| 104 | (1 << 10))); |
| 105 | uint8_t c = (USART_PHONE->US_RHR) & 0xFF; |
| 106 | // printf(" %x", c); |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 107 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 108 | if (stat == 0) { |
| 109 | /* Fill char into buffer */ |
| 110 | rbuf_write(&sim_rcv_buf, c); |
| 111 | } else { |
| 112 | TRACE_DEBUG("e %x st: %x\n", c, stat); |
Kévin Redon | 1cfc25e | 2019-05-14 21:26:48 +0200 | [diff] [blame^] | 113 | } /* else: error occurred */ |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 114 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 115 | char_stat = stat; |
| 116 | } |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 117 | } |
Christina Quast | cb646bc | 2015-05-03 14:21:26 +0200 | [diff] [blame] | 118 | |
| 119 | /* FIDI update functions */ |
Kévin Redon | 4fe99fa | 2018-06-25 15:53:19 +0200 | [diff] [blame] | 120 | void update_fidi(Usart_info *usart, uint8_t fidi) |
Christina Quast | cb646bc | 2015-05-03 14:21:26 +0200 | [diff] [blame] | 121 | { |
Kévin Redon | 4fe99fa | 2018-06-25 15:53:19 +0200 | [diff] [blame] | 122 | if (NULL==usart) { |
| 123 | return; |
| 124 | } |
Christina Quast | cb646bc | 2015-05-03 14:21:26 +0200 | [diff] [blame] | 125 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 126 | uint8_t fi = fidi >> 4; |
| 127 | uint8_t di = fidi & 0xf; |
Kévin Redon | 4fe99fa | 2018-06-25 15:53:19 +0200 | [diff] [blame] | 128 | int ratio = compute_fidi_ratio(fi, di); |
Christina Quast | cb646bc | 2015-05-03 14:21:26 +0200 | [diff] [blame] | 129 | |
Kévin Redon | 4fe99fa | 2018-06-25 15:53:19 +0200 | [diff] [blame] | 130 | if (ratio > 0 && ratio < 0x8000) { |
| 131 | /* make sure USART uses new F/D ratio */ |
| 132 | usart->base->US_CR |= US_CR_RXDIS | US_CR_RSTRX; |
| 133 | /* disable write protection */ |
| 134 | if (usart->base->US_WPMR) { |
| 135 | usart->base->US_WPMR = US_WPMR_WPKEY(0x555341); |
| 136 | } |
| 137 | usart->base->US_FIDI = (ratio & 0x7ff); |
| 138 | usart->base->US_CR |= US_CR_RXEN | US_CR_STTTO; |
Kévin Redon | 3113e3d | 2018-06-27 16:29:38 +0200 | [diff] [blame] | 139 | //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 Redon | 4fe99fa | 2018-06-25 15:53:19 +0200 | [diff] [blame] | 140 | } else { |
Kévin Redon | 3113e3d | 2018-06-27 16:29:38 +0200 | [diff] [blame] | 141 | //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 Redon | 4fe99fa | 2018-06-25 15:53:19 +0200 | [diff] [blame] | 142 | } |
Christina Quast | cb646bc | 2015-05-03 14:21:26 +0200 | [diff] [blame] | 143 | } |