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" |
| 35 | |
| 36 | #include <string.h> |
| 37 | |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 38 | volatile uint32_t char_stat; |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 39 | |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 40 | // FIXME: Remove: |
Christina Quast | a1dd0ef | 2015-03-22 19:06:12 +0100 | [diff] [blame] | 41 | #define PR TRACE_INFO |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 42 | //#define PR printf |
| 43 | |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 44 | volatile ringbuf sim_rcv_buf = { {0}, 0, 0 }; |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 45 | |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 46 | /** Initializes a ISO driver |
| 47 | */ |
| 48 | // FIXME: This function is implemented in iso7816_4.c !! Only MCK instead of SCK is always taken. Change that! |
| 49 | void _ISO7816_Init( void ) |
| 50 | { |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 51 | printf("ISO_Init\n\r"); |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 52 | TRACE_DEBUG("ISO_Init\n\r"); |
| 53 | |
| 54 | USART_Configure( USART_PHONE, |
| 55 | US_MR_USART_MODE_IS07816_T_0 |
| 56 | // Nope, we aren't master: |
| 57 | // | US_MR_USCLKS_MCK |
| 58 | | US_MR_USCLKS_SCK |
| 59 | | US_MR_NBSTOP_1_BIT |
| 60 | | US_MR_PAR_EVEN |
| 61 | | US_MR_CHRL_8_BIT |
| 62 | | US_MR_CLKO /** TODO: This field was set in the original simtrace firmware..why? */ |
| 63 | | (3<<24), /* MAX_ITERATION */ |
| 64 | 1, |
| 65 | 0); |
| 66 | /* |
| 67 | SYNC = 0 (async mode) |
| 68 | OVER = 0 (oversampling by 8?) |
| 69 | FIDI = 372 (default val on startup before other value is negotiated) |
| 70 | USCLKS = 3 (Select SCK as input clock) --> US_MR_USCLKS_SCK |
| 71 | CD = 1 ? --> US_BRGR_CD(1) |
| 72 | */ |
| 73 | USART_PHONE->US_FIDI = 372; |
| 74 | // USART_PHONE->US_IDR = (uint32_t) -1; |
| 75 | USART_PHONE->US_BRGR = US_BRGR_CD(1); |
| 76 | // USART_PHONE->US_BRGR = BOARD_MCK / (372*9600); |
| 77 | USART_PHONE->US_TTGR = 5; |
| 78 | |
| 79 | /* Configure USART */ |
| 80 | PMC_EnablePeripheral(ID_USART_PHONE); |
| 81 | |
| 82 | USART1->US_IDR = 0xffffffff; |
| 83 | USART_EnableIt( USART1, US_IER_RXRDY) ; |
| 84 | /* enable USART1 interrupt */ |
| 85 | NVIC_EnableIRQ( USART1_IRQn ) ; |
| 86 | |
| 87 | // USART_PHONE->US_IER = US_IER_RXRDY | US_IER_OVRE | US_IER_FRAME | US_IER_PARE | US_IER_NACK | US_IER_ITER; |
| 88 | } |
| 89 | |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 90 | /* |
| 91 | * char_stat is zero if no error occured. |
| 92 | * Otherwise it is filled with the content of the status register. |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 93 | */ |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 94 | void USART1_IrqHandler( void ) |
| 95 | { |
| 96 | uint32_t stat; |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 97 | char_stat = 0; |
| 98 | // Rcv buf full |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 99 | /* if((stat & US_CSR_RXBUFF) == US_CSR_RXBUFF) { |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 100 | TRACE_DEBUG("Rcv buf full"); |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 101 | USART_DisableIt(USART1, US_IDR_RXBUFF); |
| 102 | } |
| 103 | */ |
| 104 | uint32_t csr = USART_PHONE->US_CSR; |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 105 | |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 106 | if (csr & US_CSR_TXRDY) { |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 107 | /* transmit buffer empty, nothing to transmit */ |
| 108 | } |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 109 | if (csr & US_CSR_RXRDY) { |
| 110 | stat = (csr&(US_CSR_OVRE|US_CSR_FRAME| |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 111 | US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK| |
| 112 | (1<<10))); |
Christina Quast | 2b8a18b | 2015-04-12 09:31:36 +0200 | [diff] [blame^] | 113 | uint8_t c = (USART_PHONE->US_RHR) & 0xFF; |
Christina Quast | a1dd0ef | 2015-03-22 19:06:12 +0100 | [diff] [blame] | 114 | // printf(" %x", c); |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 115 | |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 116 | if (stat == 0 ) { |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 117 | /* Fill char into buffer */ |
Christina Quast | 2b8a18b | 2015-04-12 09:31:36 +0200 | [diff] [blame^] | 118 | rbuf_write(&sim_rcv_buf, c); |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 119 | } else { |
Christina Quast | 2b8a18b | 2015-04-12 09:31:36 +0200 | [diff] [blame^] | 120 | rbuf_write(&sim_rcv_buf, c); |
| 121 | PR("e %x st: %x\n", c, stat); |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 122 | } /* else: error occured */ |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 123 | |
Christina Quast | 4ba4d22 | 2015-03-02 16:14:09 +0100 | [diff] [blame] | 124 | char_stat = stat; |
| 125 | } |
| 126 | } |