int rt_hw_uart_init(void) { DEV_UART_PTR uart; struct serial_configure config; int ret; config.baud_rate = BAUD_RATE_115200; config.bit_order = BIT_ORDER_LSB; config.data_bits = DATA_BITS_8; config.parity = PARITY_NONE; config.stop_bits = STOP_BITS_1; config.invert = NRZ_NORMAL; config.bufsz = RT_SERIAL_RB_BUFSZ; _emsk_uart0.ops = &_emsk_uart0_ops; _emsk_uart0.config = config; _emsk_uart1.ops = &_emsk_uart1_ops; _emsk_uart1.config = config; /* open UART1 for USB-UART interface */ uart = uart_get_dev(DW_UART_1_ID); /* default format: 8bits, no parity, 1 stop bits */ ret = uart->uart_open(config.baud_rate); if (ret != E_OPNED && ret != E_OK) { return RT_ERROR; } /* enable rx int */ uart->uart_control(UART_CMD_SET_RXINT, (void *)1); /* use customized int isr */ uart->uart_control(UART_CMD_SET_RXCB, _emsk_uart1_isr); uart->uart_control(UART_CMD_SET_RXINT_BUF, NULL); rt_hw_serial_register(&_emsk_uart1, "uart1", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM, (void *)DW_UART_1_ID); /* open UART0 in PMOD A*/ uart = uart_get_dev(DW_UART_0_ID); /* default format: 8bits, no parity, 1 stop bits */ ret = uart->uart_open(config.baud_rate); if (ret != E_OPNED && ret != E_OK) { return RT_ERROR; } /* enable rx int */ uart->uart_control(UART_CMD_SET_RXINT, (void *)1); /* use customized int isr */ uart->uart_control(UART_CMD_SET_RXCB, _emsk_uart0_isr); uart->uart_control(UART_CMD_SET_RXINT_BUF, NULL); rt_hw_serial_register(&_emsk_uart0, "uart0", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM, (void *)DW_UART_0_ID); return RT_EOK; }
static rt_err_t _control(struct rt_serial_device *serial, int cmd, void *arg) { DEV_UART_PTR uart; unsigned int id; id = (unsigned int)(serial->parent.user_data); uart = uart_get_dev(id); switch (cmd) { case RT_DEVICE_CTRL_CLR_INT: uart->uart_control(UART_CMD_SET_RXINT, (void *)0); break; case RT_DEVICE_CTRL_SET_INT: uart->uart_control(UART_CMD_SET_RXINT, (void *)1); break; case RT_DEVICE_CTRL_SUSPEND: uart->uart_control(UART_CMD_DIS_DEV, (void *)0); break; case RT_DEVICE_CTRL_RESUME: uart->uart_control(UART_CMD_ENA_DEV, (void *)0); break; default: return RT_ERROR; break; } return RT_EOK; }
static int _getc(struct rt_serial_device *serial) { DEV_UART_PTR uart; unsigned int id; unsigned int data; int ret; int rd_avail = 0; id = (unsigned int)(serial->parent.user_data); uart = uart_get_dev(id); uart->uart_control(UART_CMD_GET_RXAVAIL, (void *)(&rd_avail)); if (rd_avail > 0) { ret = uart->uart_read(&data, 1); } else { return -1; } if (ret < 0) { return -1; } else { return data; } }
static int _putc(struct rt_serial_device *serial, char c) { DEV_UART_PTR uart; unsigned int id; int ret; id = (unsigned int)(serial->parent.user_data); uart = uart_get_dev(id); ret = uart->uart_write(&c, 1); if (ret < 0) { return -1; } else { return 1; } }
static rt_err_t _configure(struct rt_serial_device *serial, struct serial_configure *cfg) { DEV_UART_PTR uart; unsigned int id; int ret; id = (unsigned int)(serial->parent.user_data); uart = uart_get_dev(id); ret = uart->uart_control(UART_CMD_SET_BAUD, (void *)(cfg->baud_rate)); if (ret != E_OK) { return RT_ERROR; } return RT_EOK; }
/** xprintf need functions api setup */ void xprintf_setup(void) { if (xprintf_setup_flag) { return; } console_uart = uart_get_dev(CONSOLE_UART_ID); console_uart->uart_open(BOARD_CONSOLE_UART_BAUD); xdev_in(console_getchar); xdev_out(console_putchar); #if ENABLE_BANNER == 1 embarc_print_banner(); #endif xprintf("embARC Build Time: %s, %s\r\n", __DATE__, __TIME__); #if defined(__GNU__) xprintf("Compiler Version: ARC GNU, %s\r\n", __VERSION__); #else xprintf("Compiler Version: Metaware, %s\r\n", __VERSION__); #endif xprintf_setup_flag = 1; }