1157873Simp/*-
2157873Simp * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
3157873Simp *
4157873Simp * Redistribution and use in source and binary forms, with or without
5157873Simp * modification, are permitted provided that the following conditions
6157873Simp * are met:
7157873Simp * 1. Redistributions of source code must retain the above copyright
8157873Simp *    notice, this list of conditions and the following disclaimer.
9157873Simp * 2. Redistributions in binary form must reproduce the above copyright
10157873Simp *    notice, this list of conditions and the following disclaimer in the
11157873Simp *    documentation and/or other materials provided with the distribution.
12157873Simp *
13157873Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14157873Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15157873Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16157873Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17157873Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18157873Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19157873Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20157873Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21157873Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22157873Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23157873Simp *
24157873Simp * This software is derived from software provided by kwikbyte without
25157873Simp * copyright as follows:
26157873Simp *
27157873Simp * No warranty, expressed or implied, is included with this software.  It is
28157873Simp * provided "AS IS" and no warranty of any kind including statutory or aspects
29157873Simp * relating to merchantability or fitness for any purpose is provided.  All
30157873Simp * intellectual property rights of others is maintained with the respective
31157873Simp * owners.  This software is not copyrighted and is intended for reference
32157873Simp * only.
33157873Simp *
34157873Simp * $FreeBSD$
35157873Simp */
36157873Simp
37157873Simp#include "at91rm9200.h"
38157873Simp#include "at91rm9200_lowlevel.h"
39157873Simp#include "lib.h"
40157873Simp
41157873Simp/*
42157873Simp * int getc(int seconds)
43157873Simp *
44157873Simp * Reads a character from the DBGU port, if one is available within about
45157873Simp * seconds seconds.  It assumes that DBGU has already been initialized.
46157873Simp */
47157873Simpint
48157873Simpgetc(int seconds)
49157873Simp{
50157873Simp	AT91PS_USART pUSART = (AT91PS_USART)AT91C_BASE_DBGU;
51157873Simp	unsigned	thisSecond;
52157873Simp
53168013Simp	// Clamp to 20s
54168013Simp	if (seconds > 20)
55168013Simp	    seconds = 20;
56157873Simp	thisSecond = GetSeconds();
57157873Simp	seconds = thisSecond + seconds;
58157940Simp	do {
59157873Simp		if ((pUSART->US_CSR & AT91C_US_RXRDY))
60157873Simp			return (pUSART->US_RHR & 0xFF);
61157873Simp		thisSecond = GetSeconds();
62168013Simp	} while (thisSecond != seconds);
63157873Simp	return (-1);
64157873Simp}
65