// 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 */ //char t2str[20] ; #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) // before while printf("%d\n\r", sem_uart_recv->count); \\ /**********************************************************/ //define the structures and turn on the UART void aos_init_uart(long baud) { UCSRB=0x18; // activate UART //uses the clock freq set in the config dialog box UBRRL= _MCU_CLOCK_FREQUENCY_ /(baud*16L) - 1L; sem_uart_xmit = (aos_semaphore *)aos_sem_create( 1 ); sem_uart_recv = (aos_semaphore *)aos_sem_create( 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 //printf("%s ",t2str); if ((r_char!='\r') && (r_char!='\n')) uart_recv_ptr[uart_recv_index++] = r_char; else { // 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); } } /**********************************************************/ //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 } } /**********************************************************/