[En-Nut-Discussion] DBGU interrupt code..

Remi Bilodeau remi.bilodeau at gmail.com
Wed Jan 20 21:15:44 CET 2010


I am using this code for year without problem... It written for Code
sourcery without Nut/OS, but very easy to mod for this... I am just begining
to use Nut/OS for now...

Code is not complete, fifo.c is not include, some other DBGU function,
etc.., if anyone want complete code, I can post it or send direct to e-mail.

I have also modded AT91 assembler start-up code and linker script to remap
SRAM at 0 and copy interrupt vector at 0x0000 to use my XMODEM bootloader
from 0x0000 to 0x0800. Application start at 0x0800.

This is DBGU code:

void sys_interrupt() {

    AT91PS_USART USART_pt = (AT91PS_USART)AT91C_BASE_DBGU;
    unsigned int status;

    // get Usart status register
    //-----------------------------------
    status = USART_pt->US_CSR;

    // Verify for incoming caracter
    //---------------------------------
    if(status&AT91C_US_RXRDY)
fifo_put(DBGU_inbuf,AT91F_US_GetChar(USART_pt));

    // Send caracter in anyone in queue
    //-----------------------------------
    if(status&AT91C_US_TXRDY) {
        if(fifo_empty(DBGU_outbuf))
            AT91F_US_DisableIt(USART_pt, AT91C_US_TXRDY);
         else
            AT91F_US_PutChar(USART_pt, fifo_get(DBGU_outbuf));

    }

    // Reset USART status
    //-----------------------------------
    USART_pt->US_CR = AT91C_US_RSTSTA;

}

void open_DBGU(unsigned int speed, unsigned int inbufsize, unsigned int
outbufsize) {
    AT91PS_USART USART_pt = (AT91PS_USART)AT91C_BASE_DBGU;

    if(DBGU_status==1) return;

    /* CREATE IN AND OUTPUT BUFFER */
    DBGU_outbuf = fifo_create(outbufsize);
    DBGU_inbuf  = fifo_create(inbufsize);

    /* CONFIGURE I/O PIN FOR DBGU */
    AT91F_DBGU_CfgPIO();

    /* CONFIGURE DBGU */
    AT91F_US_Configure
(USART_pt,BOARD_MCK,AT91C_US_ASYNC_MODE,speed,0);


    /* ENABLE SYS INTERRUPT */
    AT91F_AIC_ConfigureIt(AT91C_BASE_AIC, AT91C_ID_SYS,
SYS_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, (void*)sys_interrupt);
    AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_SYS);

    // ENABLE RECEIVE INTERRUPT (RXRDY)
    AT91F_US_EnableIt(USART_pt, AT91C_US_RXRDY);

    /* ENABLE RX AND TX */
    USART_pt->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;

    /* DBGU IS OPEN */
    DBGU_status = 1;

}


void close_DBGU() {
    if(DBGU_status==0) return;

    AT91F_US_Close((AT91PS_USART)AT91C_BASE_DBGU);

    if(DBGU_outbuf!=NULL) fifo_destroy(DBGU_outbuf);
    if(DBGU_inbuf!=NULL) fifo_destroy(DBGU_inbuf);

    // DBGU is close
    DBGU_status = 0;
}


unsigned char getc_DBGU() {
    unsigned char c;

    if(DBGU_status==0) return 0;

    while(fifo_empty(DBGU_inbuf));
    AT91F_AIC_DisableIt(AT91C_BASE_AIC, AT91C_ID_SYS); //
AT91F_disable_interrupt();
    c = fifo_get(DBGU_inbuf);
    AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_SYS); //
AT91F_enable_interrupt();
    return c;
}

void putc_DBGU(unsigned char c) {
    if(DBGU_status==0) return;

    // Put caracter in fifo (disable IRQ fifo acces when put caracter)
    //------------------------------------------------------------------
    while(fifo_full(DBGU_outbuf));
    AT91F_AIC_DisableIt(AT91C_BASE_AIC, AT91C_ID_SYS); //
AT91F_disable_interrupt();
    fifo_put(DBGU_outbuf, c);
    AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_SYS); //
AT91F_enable_interrupt();

    // Enable TX interrupt
    //------------------------------------------------
    AT91F_US_EnableIt((AT91PS_USART)AT91C_BASE_DBGU, AT91C_US_TXRDY );
}


int kbhit_DBGU() {
  if(DBGU_status==0) return 0;

  return !fifo_empty(DBGU_inbuf);
}



More information about the En-Nut-Discussion mailing list