Release version 2.5.1.
[bertos.git] / 2.5 / bertos / cpu / arm / drv / kdebug_at91.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief ARM debug support (implementation).
34  *
35  * \version $Id$
36  * \author Francesco Sacchi <batt@develer.com>
37  */
38
39 #include "kdebug_at91.h"
40 #include <hw/hw_cpufreq.h>     /* for CPU_FREQ */
41 #include "hw/hw_ser.h"     /* Required for bus macros overrides */
42
43 #include "cfg/cfg_debug.h"
44 #include <cfg/macros.h> /* for BV(), DIV_ROUND */
45
46 #include <io/arm.h>
47
48 #if CONFIG_KDEBUG_PORT == KDEBUG_PORT_DBGU
49         #define KDBG_WAIT_READY()     while (!(DBGU_SR & BV(US_TXRDY))) {}
50         #define KDBG_WAIT_TXDONE()    while (!(DBGU_SR & BV(US_TXEMPTY))) {}
51
52         #define KDBG_WRITE_CHAR(c)    do { DBGU_THR = (c); } while(0)
53
54         /* Debug unit is used only for debug purposes so does not generate interrupts. */
55         #define KDBG_MASK_IRQ(old)    do { (void)old; } while(0)
56
57         /* Debug unit is used only for debug purposes so does not generate interrupts. */
58         #define KDBG_RESTORE_IRQ(old) do { (void)old; } while(0)
59
60         typedef uint32_t kdbg_irqsave_t;
61
62 #else
63         #error CONFIG_KDEBUG_PORT should be KDEBUG_PORT_DBGU
64 #endif
65
66
67 INLINE void kdbg_hw_init(void)
68 {
69         #if CONFIG_KDEBUG_PORT == KDEBUG_PORT_DBGU
70                 /* Disable all DBGU interrupts. */
71                 DBGU_IDR =  0xFFFFFFFF;
72                 /* Reset DBGU */
73                 DBGU_CR =  BV(US_RSTRX) | BV(US_RSTTX) | BV(US_RXDIS) | BV(US_TXDIS);
74                 /* Set baudrate */
75                 DBGU_BRGR = DIV_ROUND(CPU_FREQ, 16 * CONFIG_KDEBUG_BAUDRATE);
76                 /* Set DBGU mode to 8 data bits, no parity and 1 stop bit. */
77                 DBGU_MR =  US_CHMODE_NORMAL | US_CHRL_8 | US_PAR_NO | US_NBSTOP_1;
78                 /* Enable DBGU transmitter. */
79                 DBGU_CR = BV(US_TXEN);
80                 /* Disable PIO on DGBU tx pin. */
81                 PIOA_PDR = BV(DTXD);
82                 PIOA_ASR = BV(DTXD);
83                 
84                 #if 0 /* Disable Rx for now */
85                 /* Enable DBGU receiver. */
86                 DBGU_CR = BV(US_RXEN);
87                 /* Disable PIO on DGBU rx pin. */
88                 PIOA_PDR = BV(DRXD);
89                 PIOA_ASR = BV(DRXD);
90                 #endif
91         #else
92                 #error CONFIG_KDEBUG_PORT should be KDEBUG_PORT_DBGU
93         #endif /* CONFIG_KDEBUG_PORT == KDEBUG_PORT_DBGU */
94 }