int main(void) { int i; WDTCTL = WDTPW | WDTHOLD; // Disable watchdog timer // enable fifo iowrite16(STAT_FIFO_EN, BASE_UART0+SER_STATUS); iowrite16(BAUD_115200, BASE_UART0+SER_BAUD); iowrite16(STAT_FIFO_EN, BASE_UART1+SER_STATUS); iowrite16(BAUD_115200, BASE_UART1+SER_BAUD); printf("\r\n====== UART loopback example ======\r\n"); iowrite16(0xFF, BASE_GPIO0+GPIO_DIR); iowrite16(0x80, BASE_GPIO0+GPIO_OUT); while(1) { /* echo loop */ if ( serial_getchar_check(BASE_UART0) ) { i = serial_getchar(BASE_UART0); serial_putchar(BASE_UART0, i); iowrite16( (ioread16(BASE_GPIO0+GPIO_OUT) ^ 0x1), BASE_GPIO0+GPIO_OUT); } if ( serial_getchar_check(BASE_UART1) ) { i = serial_getchar(BASE_UART1); serial_putchar(BASE_UART1, i); iowrite16( (ioread16(BASE_GPIO0+GPIO_OUT) ^ 0x2), BASE_GPIO0+GPIO_OUT); } } }
int main(){ int i; /* -------------- serial port --------------------- */ UCSR0B = _BV(RXEN0)|_BV(TXEN0); UCSR0C = _BV(UCSZ01)|_BV(UCSZ00); // UBRR0 = 103; /* 9k6 bps @ 16 MHz (U2X0=0) */ // UCSR0A = 0; UBRR0 = 16; /* 115k2 bps @ 16 MHz (U2X0=1) */ UCSR0A = _BV(U2X0); for(i=0;i<sizeof(hello);i++) serial_putchar(pgm_read_byte(&hello[i])); avr_redeye_init(); sei(); while(1){ if(UCSR0A & _BV(RXC0)){ /* received a byte */ char c=UDR0; if(avr_redeye_put(c)) serial_putchar('O'); } } }
int test_main() { serial_putstr("Hello world.\n"); int k; for(k=0; k<10; k++) { serial_putstr("Loop!\n"); } for(k=0; k<int_buffer_size; k++) int_buffer[k] = k; for(k=0; k<(int_buffer_size); k++) { int j = int_buffer[k]; j = j * j; int_buffer[k] = j; } for(k=0; k<(int_buffer_size); k++) { char print = (char)int_buffer[k]; print = print % 10; serial_putchar(print + '0'); } serial_putchar('\n'); serial_putstr("Bye world.\n"); stop(); return 0; }
int main(void) { watchdog_off(); clock_init_1MHz(); /* set all pins to output high */ port1_direction = 0xFF; port1_output = 0xFF; /* initialize serial clock, tx and rx parts */ serial_init_clock(); serial_init_tx(); serial_init_rx(); /* enable interrupts */ __eint(); pin_low(LED1); pin_high(LED2); while (1) { unsigned char c; pin_toggle(LED1); pin_toggle(LED2); c = serial_getchar(); serial_putchar(c); if (c == '\r') serial_putchar('\n'); } }
void puts(const char *s) { char c; while ( ( c = *s++ ) != '\0' ) { serial_putchar(c); if ( c == '\n' ) serial_putchar('\r'); } }
static void poll_dials ( int id ) { int data; static int dial_state = DIAL_NEW; static int dial_which; static int dial_value; static int dials[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; if ( !dialbox_port ) return; while ( (data=serial_getchar(dialbox_port)) != EOF ) { if ( ( dial_state > DIAL_WHICH_DEVICE ) || IS_DIAL_EVENT ( data ) ) { switch ( dial_state ) { case DIAL_WHICH_DEVICE: dial_which = data - DIAL_BASE; dial_state++; break; case DIAL_VALUE_HIGH: dial_value = ( data << 8 ); dial_state++; break; case DIAL_VALUE_LOW: dial_value |= data; if ( dial_value & 0x8000 ) dial_value -= 0x10000; dials[dial_which] = dial_value; send_dial_event ( dial_which + 1, dial_value * 360 / 256 ); dial_state = DIAL_WHICH_DEVICE; break; default: /* error: Impossible state value! */ break; } } else if ( data == DIAL_INITIALIZED ) { fgState.InputDevsInitialised = GL_TRUE; dial_state = DIAL_WHICH_DEVICE; serial_putchar(dialbox_port,DIAL_SET_AUTO_DIALS); serial_putchar(dialbox_port,0xff); serial_putchar(dialbox_port,0xff); } else /* Unknown data; try flushing. */ serial_flush(dialbox_port); } glutTimerFunc ( 2, poll_dials, 0 ); }
void game_frame() { // if button pressed, send 'a' quickly if (button_state()) serial_putchar('a'); // low level output if (vga_frame%16) return; led=1-led; set_led(led); // echo input first character if (serial_rx_ready()) serial_putchar(serial_getchar()); // could block }
void serial_putchar(unsigned int c) { while (!(readl(&uart_regs->uart_lsr) & UART_LSR_THRE)); writel((c & 0xff), &uart_regs->uart_thr); if (c == '\n') serial_putchar('\r'); }
/* * Try initializing the input device(s) */ void fgInitialiseInputDevices ( void ) { if( !fgState.InputDevsInitialised ) { const char *dial_device=NULL; dial_device = getenv ( "GLUT_DIALS_SERIAL" ); #if TARGET_HOST_MS_WINDOWS if (!dial_device){ static char devname[256]; DWORD size=sizeof(devname); DWORD type = REG_SZ; HKEY key; if (RegOpenKeyA(HKEY_LOCAL_MACHINE,"SOFTWARE\\FreeGLUT",&key)==ERROR_SUCCESS) { if (RegQueryValueExA(key,"DialboxSerialPort",NULL,&type,(LPBYTE)devname,&size)==ERROR_SUCCESS){ dial_device=devname; } RegCloseKey(key); } } #endif if ( !dial_device ) return; if ( !( dialbox_port = serial_open ( dial_device ) ) ) return; serial_putchar(dialbox_port,DIAL_INITIALIZE); glutTimerFunc ( 10, poll_dials, 0 ); fgState.InputDevsInitialised = GL_TRUE; } }
/* Functions related to serial console output device driver */ static void serial_putchar(uint8_t ch) { while((port_in8(SERIAL_IO_ADDR + 0x05) & 0x20) == 0); // Wait till empty port_out8(SERIAL_IO_ADDR, ch); if(ch == '\n') serial_putchar('\r'); }
void serial_putstring(char s[]) { int len = strlen(s); int i; for(i=0;i<len;i++){ serial_putchar(s[i]); } }
static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen) { char *s; if (*np == 0) { return (p); } if (*(--p) == '\t') { /* will retype the whole line */ while (*colp > plen) { printf (erase_seq); (*colp)--; } for (s=buffer; s<p; ++s) { if (*s == '\t') { printf (tab_seq+((*colp) & 07)); *colp += 8 - ((*colp) & 07); } else { ++(*colp); serial_putchar (*s); } } } else { printf (erase_seq); (*colp)--; } (*np)--; return (p); }
/* Puts and value, with a name, onto serial */ void serial_putdebug(char s[], int n) { serial_putstring(s); serial_putstring(": "); char buf[MAX_SERIAL_NUMBER_LEN]; //Assume it's going to be <= this number itoa(n, buf, 16); serial_putstring(buf); serial_putchar('\n'); }
void serial_putchar(unsigned int c) { if (!serial_hardware_is_present) return; while ((serial_read_reg(0x05) & 0x20) == 0) ; serial_write_reg(c, 0x00); if (c == '\n') serial_putchar('\r'); }
void serial_putstr(const char *str) { unsigned int i; i = 0; while (str[i] != '\0') { serial_putchar(str[i++]); } }
void tx(char c) { if (USB_SERIAL_ON) { usb_serial_putchar(c); } #ifdef USE_UART serial_putchar(c); #endif }
static int write(int id, const char* buf, int len) { if(!serial_enabled) return -1; for(int i = 0; i < len; i++) serial_putchar(buf[i]); return 0; }
static int serial_putchar(char c, FILE *stream) { if (c == '\n') serial_putchar('\r', stream); loop_until_bit_is_set(UCSR0A, UDRE0); UDR0 = c; return 0; }
/** * \brief Prints a single character to a serial port. */ void serial_putchar(char c) { // we need to send \r\n over the serial line for a newline if (c == '\n') serial_putchar('\r'); // Wait until FIFO can hold more characters (i.e. TX_FIFO_E == 1) while ( (*uart_lsr & 0x10) == 0x10); // Write character *uart_thr = c; }
void __attribute__((section(".inittext"))) putchar(int ch) { if (ch == '\n') putchar('\r'); /* \n -> \r\n */ bios_putchar(ch); if (early_serial_base != 0) serial_putchar(ch); }
void serial_putchar(unsigned int c) { if (!serial_hardware_is_present) return; #if !IS_ENABLED(CONFIG_LP_PL011_SERIAL_CONSOLE) while ((serial_read_reg(0x05) & 0x20) == 0) ; #endif serial_write_reg(c, 0x00); if (c == '\n') serial_putchar('\r'); }
void gdb_printf(const char *format, ...) { va_list arg; char text_buf[MAX_PRINTF_LENGTH]; va_start(arg, format); vsnprintf(text_buf, MAX_PRINTF_LENGTH, format, arg); va_end(arg); int UNUSED error; error = serial_lock(); for (int i = 0; i < strlen(text_buf); i++) { serial_putchar(text_buf[i]); } error = serial_unlock(); }
/* * serial_putstring * ------------------------------------- * put a string to the serial port * * Assumes null termination of string. * * non zero return implies error */ U32 serial_putstring(const char *s) { U32 error; int i, length; length = strlen(s); for (i=0; i < length; ++i) { serial_putchar(*s); // put the char s++; // increment to next char } error = 0; return(error); }
short console_getline(console_color_t color, char *str, unsigned short *len) { unsigned short i=0; char c; unsigned char c233 = vga_color233(vga_console_color(color)); while (i < *len-1) { if ((sysio[0] & 0x2) == 0x2) c = keyboard_getchar(); else c = serial_getchar(0); if (c >= ' ' && c <= '~') { if ((sysio[0] & 0x2) == 0x2) vga_putchar(c233,c); else serial_putchar(0, c); str[i++] = c; } if (c == '\r' || c == '\n') { str[i] = '\0'; return i; } if (c == 0x03) { str[0] = '\0'; return -1; } if ((c == 0x7f || c == 0x08) && i > 0) { if ((sysio[0] & 0x2) == 0x2) vga_putchar(c233, c); else serial_putchar(0, c); i--; } } str[i+1] = '\0'; return i; }
/* * Try initializing the input device(s) */ void fgInitialiseInputDevices ( void ) { if( !fgState.InputDevsInitialised ) { const char *dial_device=NULL; dial_device = getenv ( "GLUT_DIALS_SERIAL" ); fgPlatformRegisterDialDevice ( dial_device ); if ( !dial_device ) return; if ( !( dialbox_port = serial_open ( dial_device ) ) ) return; serial_putchar(dialbox_port,DIAL_INITIALIZE); glutTimerFunc ( 10, poll_dials, 0 ); fgState.InputDevsInitialised = GL_TRUE; } }
int serial_printk(const char *fmt, ...) { va_list a; va_start(a, fmt); char *s=iobuf; int rv=vsnprintk(iobuf, IOBUFSIZE, fmt, a); if (rv) while (*s) if (0>serial_putchar(*(s++))) break; va_end(a); return rv; }
static int serial_putchar(ps_chardevice_t* device, int c) { uint32_t io_port = (uint32_t) device->vaddr; /* Check if serial is ready. */ if (!serial_ready(device)) { return -1; } /* Write out the next character. */ ps_io_port_out(&device->ioops.io_port_ops, CONSOLE(io_port, THR), 1, c); if (c == '\n') { /* If we output immediately then odds are the transmit buffer * will be full, so we have to wait */ while (!serial_ready(device)); serial_putchar(device, '\r'); } return c; }
// Write a string to the output queue static void serial_puts (const char *s) { while (*s) serial_putchar (*s++); }
int ubreadline_into_buffer(const char *const prompt, char *buffer, int timeout) { char *p = buffer; unsigned int len = CONFIG_SYS_CBSIZE; int rc; static int initted = 0; /* * History uses a global array which is not * writable until after relocation to RAM. * Revert to non-history version if still * running from flash. */ if (0) { if (!initted) { hist_init(); initted = 1; } if (prompt) printf (prompt); rc = cread_line(prompt, p, &len, timeout); return rc < 0 ? rc : len; } else { char * p_buf = p; int n = 0; /* buffer index */ int plen = 0; /* prompt length */ int col; /* output column cnt */ char c; /* print prompt */ if (prompt) { plen = strlen (prompt); printf (prompt); } col = plen; for (;;) { c = serial_getchar(); /* * Special character handling */ switch (c) { case '\r': /* Enter */ case '\n': *p = '\0'; printf ("\r\n"); return p - p_buf; case '\0': /* nul */ continue; case 0x03: /* ^C - break */ p_buf[0] = '\0'; /* discard input */ return -1; case 0x15: /* ^U - erase line */ while (col > plen) { printf (erase_seq); --col; } p = p_buf; n = 0; continue; case 0x17: /* ^W - erase word */ p=delete_char(p_buf, p, &col, &n, plen); while ((n > 0) && (*p != ' ')) { p=delete_char(p_buf, p, &col, &n, plen); } continue; case 0x08: /* ^H - backspace */ case 0x7F: /* DEL - backspace */ p=delete_char(p_buf, p, &col, &n, plen); continue; default: /* * Must be a normal character then */ if (n < CONFIG_SYS_CBSIZE-2) { if (c == '\t') { /* expand TABs */ /* if auto completion triggered just continue */ *p = '\0'; if (cmd_auto_complete(prompt, console_buffer, &n, &col)) { p = p_buf + n; /* reset */ continue; } printf (tab_seq+(col&07)); col += 8 - (col&07); } else { char buf[2]; /* * Echo input using printf() to force an * LCD flush if we are using an LCD */ ++col; buf[0] = c; buf[1] = '\0'; printf(buf); } *p++ = c; ++n; } else { /* Buffer full */ serial_putchar('\a'); } } } } }
static int cread_line(const char *const prompt, char *buf, unsigned int *len, int timeout) { unsigned long num = 0; unsigned long eol_num = 0; unsigned long wlen; char ichar; int insert = 1; int esc_len = 0; char esc_save[8]; int init_len = strlen(buf); //int first = 1; if (init_len) cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); while (1) { #if 0 #ifdef CONFIG_BOOT_RETRY_TIME while (!tstc()) { /* while no incoming data */ if (retry_time >= 0 && get_ticks() > endtime) return (-2); /* timed out */ // WATCHDOG_RESET(); } #endif if (first && timeout) { uint64_t etime = endtick(timeout); while (!tstc()) { /* while no incoming data */ if (get_ticks() >= etime) return -2; /* timed out */ WATCHDOG_RESET(); } first = 0; } #endif ichar = getcmd_getch(); if ((ichar == '\n') || (ichar == '\r')) { serial_putchar('\n'); break; } /* * handle standard linux xterm esc sequences for arrow key, etc. */ if (esc_len != 0) { if (esc_len == 1) { if (ichar == '[') { esc_save[esc_len] = ichar; esc_len = 2; } else { cread_add_str(esc_save, esc_len, insert, &num, &eol_num, buf, *len); esc_len = 0; } continue; } switch (ichar) { case 'D': /* <- key */ ichar = CTL_CH('b'); esc_len = 0; break; case 'C': /* -> key */ ichar = CTL_CH('f'); esc_len = 0; break; /* pass off to ^F handler */ case 'H': /* Home key */ ichar = CTL_CH('a'); esc_len = 0; break; /* pass off to ^A handler */ case 'A': /* up arrow */ ichar = CTL_CH('p'); esc_len = 0; break; /* pass off to ^P handler */ case 'B': /* down arrow */ ichar = CTL_CH('n'); esc_len = 0; break; /* pass off to ^N handler */ default: esc_save[esc_len++] = ichar; cread_add_str(esc_save, esc_len, insert, &num, &eol_num, buf, *len); esc_len = 0; continue; } } switch (ichar) { case 0x1b: if (esc_len == 0) { esc_save[esc_len] = ichar; esc_len = 1; } else { printf("impossible condition #876\n"); esc_len = 0; } break; case CTL_CH('a'): BEGINNING_OF_LINE(); break; case CTL_CH('c'): /* ^C - break */ *buf = '\0'; /* discard input */ return (-1); case CTL_CH('f'): if (num < eol_num) { getcmd_putch(buf[num]); num++; } break; case CTL_CH('b'): if (num) { getcmd_putch(CTL_BACKSPACE); num--; } break; case CTL_CH('d'): if (num < eol_num) { wlen = eol_num - num - 1; if (wlen) { memmove(&buf[num], &buf[num+1], wlen); putnstr(buf + num, wlen); } getcmd_putch(' '); do { getcmd_putch(CTL_BACKSPACE); } while (wlen--); eol_num--; } break; case CTL_CH('k'): ERASE_TO_EOL(); break; case CTL_CH('e'): REFRESH_TO_EOL(); break; case CTL_CH('o'): insert = !insert; break; case CTL_CH('x'): case CTL_CH('u'): BEGINNING_OF_LINE(); ERASE_TO_EOL(); break; case DEL: case DEL7: case 8: if (num) { wlen = eol_num - num; num--; memmove(&buf[num], &buf[num+1], wlen); getcmd_putch(CTL_BACKSPACE); putnstr(buf + num, wlen); getcmd_putch(' '); do { getcmd_putch(CTL_BACKSPACE); } while (wlen--); eol_num--; } break; case CTL_CH('p'): case CTL_CH('n'): { char * hline; esc_len = 0; if (ichar == CTL_CH('p')) hline = hist_prev(); else hline = hist_next(); if (!hline) { getcmd_cbeep(); continue; } /* nuke the current line */ /* first, go home */ BEGINNING_OF_LINE(); /* erase to end of line */ ERASE_TO_EOL(); /* copy new line into place and display */ strcpy(buf, hline); eol_num = strlen(buf); REFRESH_TO_EOL(); continue; } #ifdef CONFIG_AUTO_COMPLETE case '\t': { int num2, col; /* do not autocomplete when in the middle */ if (num < eol_num) { getcmd_cbeep(); break; } buf[num] = '\0'; col = strlen(prompt) + eol_num; num2 = num; if (cmd_auto_complete(prompt, buf, &num2, &col)) { col = num2 - num; num += col; eol_num += col; } break; } #endif default: cread_add_char(ichar, insert, &num, &eol_num, buf, *len); break; } } *len = eol_num; buf[eol_num] = '\0'; /* lose the newline */ if (buf[0] && buf[0] != CREAD_HIST_CHAR) cread_add_to_hist(buf); hist_cur = hist_add_idx; return 0; }