blob: 47232dfa5a5fc938b3cd60ea55386fa459d2f63c [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>
37
Christina Quast95d66162015-04-09 22:38:47 +020038volatile uint32_t char_stat;
Christina Quastc0aa7692015-02-25 14:02:01 +010039
Christina Quast4ba4d222015-03-02 16:14:09 +010040// FIXME: Remove:
Christina Quasta1dd0ef2015-03-22 19:06:12 +010041#define PR TRACE_INFO
Christina Quast4ba4d222015-03-02 16:14:09 +010042//#define PR printf
43
Christina Quast4db82e02015-04-11 18:14:41 +020044volatile ringbuf sim_rcv_buf = { {0}, 0, 0 };
Christina Quast4ba4d222015-03-02 16:14:09 +010045
Christina Quastc0aa7692015-02-25 14:02:01 +010046/** 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!
49void _ISO7816_Init( void )
50{
Christina Quast4ba4d222015-03-02 16:14:09 +010051 printf("ISO_Init\n\r");
Christina Quastc0aa7692015-02-25 14:02:01 +010052 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 Quast4db82e02015-04-11 18:14:41 +020090/*
91 * char_stat is zero if no error occured.
92 * Otherwise it is filled with the content of the status register.
Christina Quastc0aa7692015-02-25 14:02:01 +010093 */
Christina Quast4db82e02015-04-11 18:14:41 +020094void USART1_IrqHandler( void )
95{
96 uint32_t stat;
Christina Quastc0aa7692015-02-25 14:02:01 +010097 char_stat = 0;
98 // Rcv buf full
Christina Quast4db82e02015-04-11 18:14:41 +020099/* if((stat & US_CSR_RXBUFF) == US_CSR_RXBUFF) {
Christina Quastc0aa7692015-02-25 14:02:01 +0100100 TRACE_DEBUG("Rcv buf full");
Christina Quast4db82e02015-04-11 18:14:41 +0200101 USART_DisableIt(USART1, US_IDR_RXBUFF);
102 }
103*/
104 uint32_t csr = USART_PHONE->US_CSR;
Christina Quast4ba4d222015-03-02 16:14:09 +0100105
Christina Quastc0aa7692015-02-25 14:02:01 +0100106 if (csr & US_CSR_TXRDY) {
Christina Quast4db82e02015-04-11 18:14:41 +0200107 /* transmit buffer empty, nothing to transmit */
108 }
Christina Quastc0aa7692015-02-25 14:02:01 +0100109 if (csr & US_CSR_RXRDY) {
110 stat = (csr&(US_CSR_OVRE|US_CSR_FRAME|
Christina Quast4ba4d222015-03-02 16:14:09 +0100111 US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK|
112 (1<<10)));
Christina Quast2b8a18b2015-04-12 09:31:36 +0200113 uint8_t c = (USART_PHONE->US_RHR) & 0xFF;
Christina Quasta1dd0ef2015-03-22 19:06:12 +0100114// printf(" %x", c);
Christina Quast4ba4d222015-03-02 16:14:09 +0100115
Christina Quastc0aa7692015-02-25 14:02:01 +0100116 if (stat == 0 ) {
Christina Quast4ba4d222015-03-02 16:14:09 +0100117 /* Fill char into buffer */
Christina Quast2b8a18b2015-04-12 09:31:36 +0200118 rbuf_write(&sim_rcv_buf, c);
Christina Quast4ba4d222015-03-02 16:14:09 +0100119 } else {
Christina Quast2b8a18b2015-04-12 09:31:36 +0200120 rbuf_write(&sim_rcv_buf, c);
121 PR("e %x st: %x\n", c, stat);
Christina Quast4ba4d222015-03-02 16:14:09 +0100122 } /* else: error occured */
Christina Quastc0aa7692015-02-25 14:02:01 +0100123
Christina Quast4ba4d222015-03-02 16:14:09 +0100124 char_stat = stat;
125 }
126}