// UART communication code //written by BRL4 #pragma regalloc- //need this to ensure no reg variables char uart_recv_index ; char uart_send_index ; char* uart_send_ptr ; char* uart_recv_ptr ; aos_semaphore *sem_uart_xmit; /* Semaphore for writing UART */ aos_semaphore *sem_uart_recv; /* Semaphore for reading UART */ //variable set by break and unsigned char aos_break_id; #pragma regalloc+ /**********************************************************/ //macros to package up semaphores and hide interrupts #define aos_puts(str) \ do{ \ aos_wait(sem_uart_xmit); \ uart_send_index = 0; \ uart_send_ptr = &str[0]; \ if (str[0]>0) \ { \ putchar(str[0]); \ UCSRB.5=1; \ aos_wait(sem_uart_xmit); \ } ; \ aos_signal(sem_uart_xmit); \ }while(0) #define aos_gets(str) \ do{ \ aos_wait(sem_uart_recv);\ uart_recv_index=0; UCSRB.7=1;\ uart_recv_ptr = str; \ aos_wait(sem_uart_recv);\ aos_signal(sem_uart_recv);\ }while(0) //macro to force entry into cmd shell //break resumes cmd task, and //freezes ISRs and clocks //It is defined here because of the tight relationship with //receive complete ISR #define aos_break(id) \\ do{ \\ aos_break_id = id; \\ aos_resume_task(cmd_tcb); \\ }while(0) /**********************************************************/ //define the structures and turn on the UART void aos_init_uart(long baud) { sem_uart_xmit = (aos_semaphore *)aos_sem_create( 1 ); sem_uart_recv = (aos_semaphore *)aos_sem_create( 1 ); UCSRB=0x18; // activate UART //uses the clock freq set in the config dialog box UBRRL= _MCU_CLOCK_FREQUENCY_ /(baud*16L) - 1L; UCSRB.7=1; } //receive ISR //********************************************************** //UART character-ready ISR interrupt [USART_RXC] void uart_rec(void) { char r_char; r_char=UDR; //get a char //build the input string if ((r_char=='\r') || (r_char=='\n')) { // putchar('\n'); //use putchar to aoid overwrite uart_recv_ptr[uart_recv_index] = 0x00 ; //zero terminate //UCSRB.7=0; //stop ISR after aos_signal(sem_uart_recv); } else if (r_char==0x03) //cntl-c aos_break(255); else uart_recv_ptr[uart_recv_index++] = r_char; } /**********************************************************/ //UART xmit-empty ISR interrupt [USART_DRE] void uart_send(void) { char t_char; t_char = uart_send_ptr[++uart_send_index]; if (t_char == 0) { UCSRB.5=0; //kill isr aos_signal(sem_uart_xmit); } else { putchar(t_char); //send the char } } /**********************************************************/