BeRTOS
kdebug_sam3.c
Go to the documentation of this file.
00001 
00038 #include <cfg/cfg_debug.h>
00039 #include <cfg/macros.h> /* for BV() */
00040 
00041 #include <cpu/types.h>
00042 
00043 #include <io/sam3.h>
00044 
00045 
00046 #if (CONFIG_KDEBUG_PORT == 0)
00047     #define UART_BASE       UART0_BASE
00048     #define UART_ID         UART0_ID
00049     #define UART_PIO_BASE   UART0_PORT
00050     #define UART_PERIPH     UART0_PERIPH
00051     #define UART_PINS       (BV(URXD0) | BV(UTXD0))
00052 #elif (CONFIG_KDEBUG_PORT == 1) && UART_PORTS > 1
00053     #define UART_BASE       UART1_BASE
00054     #define UART_ID         UART1_ID
00055     #define UART_PIO_BASE   UART1_PORT
00056     #define UART_PERIPH     UART1_PERIPH
00057     #define UART_PINS       (BV(URXD1) | BV(UTXD1))
00058 #else
00059     #error "UART port not supported in this board"
00060 #endif
00061 
00062 // TODO: refactor serial simple functions and use them, see lm3s kdebug
00063 #define KDBG_WAIT_READY()     while (!(HWREG(UART_BASE + UART_SR_OFF) & BV(UART_SR_TXRDY))) {}
00064 #define KDBG_WAIT_TXDONE()    while (!(HWREG(UART_BASE + UART_SR_OFF) & BV(UART_SR_TXEMPTY))) {}
00065 
00066 #define KDBG_WRITE_CHAR(c)    do { HWREG(UART_BASE + UART_THR_OFF) = (c); } while(0)
00067 
00068 /* Debug unit is used only for debug purposes so does not generate interrupts. */
00069 #define KDBG_MASK_IRQ(old)    do { (void)old; } while(0)
00070 
00071 /* Debug unit is used only for debug purposes so does not generate interrupts. */
00072 #define KDBG_RESTORE_IRQ(old) do { (void)old; } while(0)
00073 
00074 typedef uint32_t kdbg_irqsave_t;
00075 
00076 
00077 INLINE void kdbg_hw_init(void)
00078 {
00079     /*
00080      * Disable PIO mode and set appropriate UART pins peripheral mode.
00081      * SAM3X,A,N,S,U: all of them has all UARTs on peripheral A.
00082      */
00083     HWREG(UART_PIO_BASE + PIO_PDR_OFF) = UART_PINS;
00084     PIO_PERIPH_SEL(UART_PIO_BASE, UART_PINS, UART_PERIPH);
00085 
00086     /* Enable the peripheral clock */
00087     pmc_periphEnable(UART_ID);
00088 
00089     /* Reset and disable receiver & transmitter */
00090     HWREG(UART_BASE + UART_CR_OFF) = BV(UART_CR_RSTRX) | BV(UART_CR_RSTTX) | BV(UART_CR_RXDIS) | BV(UART_CR_TXDIS);
00091 
00092     /* Set mode: normal, no parity */
00093     HWREG(UART_BASE + UART_MR_OFF) = UART_MR_PAR_NO;
00094 
00095     /* Set baud rate */
00096     HWREG(UART_BASE + UART_BRGR_OFF) = CPU_FREQ / CONFIG_KDEBUG_BAUDRATE / 16;
00097 
00098     /* Enable receiver & transmitter */
00099     HWREG(UART_BASE + UART_CR_OFF) = BV(UART_CR_RXEN) | BV(UART_CR_TXEN);
00100 }