blob: 2d0b8c7a1d3255799712fc1a12594175fe229684 [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
38extern uint8_t rcvdChar;
39extern uint32_t char_stat;
40
Christina Quast4ba4d222015-03-02 16:14:09 +010041//#define BUFLEN 14
42// FIXME: Remove:
43#define PR TRACE_DEBUG
44//#define PR printf
45
46/*typedef struct ring_buffer
47{
48 uint8_t buf[BUFLEN*2]; // data buffer
49 uint8_t idx; // number of items in the buffer
50} ring_buffer;
51*/
52ring_buffer buf = { {0}, 0 };
53
54void buf_push(uint8_t item)
55{
56 buf.buf[buf.idx % (BUFLEN*2)] = item;
57 PR("----- Push: %x %x\n\r", buf.idx, buf.buf[buf.idx]);
58 buf.idx = (buf.idx+1) % (BUFLEN*2);
59}
60
61uint8_t get_buf_start(uint8_t *buf_start)
62{
63 *buf_start = &(buf.buf[buf.idx]);
64 return 2*BUFLEN-buf.idx;
65}
66
Christina Quastc0aa7692015-02-25 14:02:01 +010067/** Initializes a ISO driver
68 */
69// FIXME: This function is implemented in iso7816_4.c !! Only MCK instead of SCK is always taken. Change that!
70void _ISO7816_Init( void )
71{
Christina Quast4ba4d222015-03-02 16:14:09 +010072 printf("ISO_Init\n\r");
Christina Quastc0aa7692015-02-25 14:02:01 +010073 TRACE_DEBUG("ISO_Init\n\r");
74
75 USART_Configure( USART_PHONE,
76 US_MR_USART_MODE_IS07816_T_0
77// Nope, we aren't master:
78 // | US_MR_USCLKS_MCK
79 | US_MR_USCLKS_SCK
80 | US_MR_NBSTOP_1_BIT
81 | US_MR_PAR_EVEN
82 | US_MR_CHRL_8_BIT
83 | US_MR_CLKO /** TODO: This field was set in the original simtrace firmware..why? */
84 | (3<<24), /* MAX_ITERATION */
85 1,
86 0);
87 /*
88 SYNC = 0 (async mode)
89 OVER = 0 (oversampling by 8?)
90 FIDI = 372 (default val on startup before other value is negotiated)
91 USCLKS = 3 (Select SCK as input clock) --> US_MR_USCLKS_SCK
92 CD = 1 ? --> US_BRGR_CD(1)
93 */
94 USART_PHONE->US_FIDI = 372;
95// USART_PHONE->US_IDR = (uint32_t) -1;
96 USART_PHONE->US_BRGR = US_BRGR_CD(1);
97// USART_PHONE->US_BRGR = BOARD_MCK / (372*9600);
98 USART_PHONE->US_TTGR = 5;
99
100 /* Configure USART */
101 PMC_EnablePeripheral(ID_USART_PHONE);
102
103 USART1->US_IDR = 0xffffffff;
104 USART_EnableIt( USART1, US_IER_RXRDY) ;
105 /* enable USART1 interrupt */
106 NVIC_EnableIRQ( USART1_IRQn ) ;
107
108// USART_PHONE->US_IER = US_IER_RXRDY | US_IER_OVRE | US_IER_FRAME | US_IER_PARE | US_IER_NACK | US_IER_ITER;
109}
110
111/*
112 * Initializes rcvdChar with the char received on USART interface
113 * char_stat is zero if no error occured.
114 * Otherwise it is filled with the content of the status register.
115 */
116void USART1_IrqHandler( void )
117{
118 uint32_t stat;
119 char_stat = 0;
120 // Rcv buf full
121/* if((stat & US_CSR_RXBUFF) == US_CSR_RXBUFF) {
122 TRACE_DEBUG("Rcv buf full");
123 USART_DisableIt(USART1, US_IDR_RXBUFF);
124 }
125*/
126 uint32_t csr = USART_PHONE->US_CSR;
127
Christina Quast4ba4d222015-03-02 16:14:09 +0100128 PR("---- stat: %x\n\r", csr);
129
Christina Quastc0aa7692015-02-25 14:02:01 +0100130 if (csr & US_CSR_TXRDY) {
131 /* transmit buffer empty, nothing to transmit */
132 }
133 if (csr & US_CSR_RXRDY) {
134 stat = (csr&(US_CSR_OVRE|US_CSR_FRAME|
Christina Quast4ba4d222015-03-02 16:14:09 +0100135 US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK|
136 (1<<10)));
137
Christina Quastc0aa7692015-02-25 14:02:01 +0100138 if (stat == 0 ) {
Christina Quast4ba4d222015-03-02 16:14:09 +0100139 /* Fill char into buffer */
140 PR("---- BUFLEN %x\n\r", buf.idx);
141 buf_push((USART_PHONE->US_RHR) & 0xFF);
142 } else {
143 // buf_push((USART_PHONE->US_RHR) & 0xFF);
144 PR("%x\n\r", (USART_PHONE->US_RHR) & 0xFF);
145 } /* else: error occured */
Christina Quastc0aa7692015-02-25 14:02:01 +0100146
Christina Quast4ba4d222015-03-02 16:14:09 +0100147 if ((buf.idx % BUFLEN) == 0) {
148 rcvdChar = 1;
149 }
Christina Quastc0aa7692015-02-25 14:02:01 +0100150
Christina Quast4ba4d222015-03-02 16:14:09 +0100151 char_stat = stat;
152 }
153}