Skip to content

Instantly share code, notes, and snippets.

@varadgautam
Created June 7, 2014 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save varadgautam/b505e90acb6cb938733e to your computer and use it in GitHub Desktop.
Save varadgautam/b505e90acb6cb938733e to your computer and use it in GitHub Desktop.
simple uart interrupt handler
/* simple uart interrupt : echo when something received on serial */
/* does not work -- u-boot remaps the interrupt table :
* http://stackoverflow.com/questions/23964400/am335x-freertos-port-unable-to-handle-irqs-and-swi */
#define INTC_BASE 0x48200000
#define INTC_SIR_IRQ 0x40
#define IRQ_HANDLER 0x4030CE38 /* RAM exception vector : TRM 26.1.3.2 */
#define CM_PER_TIMER7_CLKCTRL 0x44E0007C
#define DMTIMER7_TCLR 0x4804A038
#define UART0_BASE 0x44E09000
#define UART_LCR 0x0C
#define IIR_UART 0x08
#define IER_UART 0x04
#define UART0INT 72
#define IT_TYPE_RHR 0x02
#define REG(x) (*((volatile unsigned int *) x))
void timer_init() {
REG(CM_PER_TIMER7_CLKCTRL) = 0x2;
REG(DMTIMER7_TCLR) = 0x3;
}
void uart_putc(int c){
REG(UART0_BASE + 0) = c;
}
void __attribute__((interrupt("IRQ"))) uart_isr(void){
uart_putc((int) 'A');
/* check INTC_SIR_IRQ[0:6] for UART0INT : TRM 6.5.1 */
if((REG(INTC_BASE + INTC_SIR_IRQ) & 0x7F) == UART0INT){
/* identify the UART interrupt type, if RHR: begin echo */
unsigned int lcr = REG(UART0_BASE + UART_LCR); /* preserve LCR */
REG(UART0_BASE + UART_LCR) &= 0x7F; /* switch to register op
* mode */
unsigned int it_type = (REG(UART0_BASE + IIR_UART) & 0x3E) >> 1;
/* TRM 19.5.1.9 */
REG(UART0_BASE + UART_LCR) = lcr; /* restore LCR */
if(it_type == IT_TYPE_RHR){ /* something received */
while(1){ /* start echo */
uart_putc((int) 'Z');
}
}
}
}
void sleep(){
int T = 1000000;
while(T--){
int TT = 100000;
while(TT--){
;
}
}
}
#define INTC_PROTECTION 0x4C
void hello_world(){
REG(IRQ_HANDLER) = uart_isr;
REG(UART0_BASE + IER_UART) |= 0x01; /* enable RHR interrupt */
//REG(INTC_BASE + INTC_PROTECTION) = 0x00; // disable protection
uart_putc((int) 'X');
while(1){
sleep();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment