int serial_getc(serial_t *obj) { PHAL_RUART_ADAPTER pHalRuartAdapter=(PHAL_RUART_ADAPTER)&(obj->hal_uart_adp); u8 uart_idx = pHalRuartAdapter->UartIndex; while (!serial_readable(obj)); return (int)((HAL_RUART_READ32(uart_idx, RUART_REV_BUF_REG_OFF)) & 0xFF); }
void serial_break_clear(serial_t *obj) { PHAL_RUART_ADAPTER pHalRuartAdapter=(PHAL_RUART_ADAPTER)&(obj->hal_uart_adp); u8 uart_idx = pHalRuartAdapter->UartIndex; u32 RegValue; RegValue = HAL_RUART_READ32(uart_idx, RUART_LINE_CTL_REG_OFF); RegValue &= ~(BIT_UART_LCR_BREAK_CTRL); HAL_RUART_WRITE32(uart_idx, RUART_LINE_CTL_REG_OFF, RegValue); }
int serial_readable(serial_t *obj) { PHAL_RUART_ADAPTER pHalRuartAdapter=(PHAL_RUART_ADAPTER)&(obj->hal_uart_adp); u8 uart_idx = pHalRuartAdapter->UartIndex; if ((HAL_RUART_READ32(uart_idx, RUART_LINE_STATUS_REG_OFF)) & RUART_LINE_STATUS_REG_DR) { return 1; } else { return 0; } }
int serial_getc(serial_t *obj) { #ifdef CONFIG_MBED_ENABLED if (obj->index == UART_3) { return log_uart_getc(&stdio_uart_log); } #endif PHAL_RUART_ADAPTER pHalRuartAdapter=(PHAL_RUART_ADAPTER)&(obj->hal_uart_adp); u8 uart_idx = pHalRuartAdapter->UartIndex; while (!serial_readable(obj)); return (int)((HAL_RUART_READ32(uart_idx, RUART_REV_BUF_REG_OFF)) & 0xFF); }
void serial_break_clear(serial_t *obj) { #ifdef CONFIG_MBED_ENABLED if (obj->index == UART_3) { log_uart_break_clear(&stdio_uart_log); return; } #endif PHAL_RUART_ADAPTER pHalRuartAdapter=(PHAL_RUART_ADAPTER)&(obj->hal_uart_adp); u8 uart_idx = pHalRuartAdapter->UartIndex; u32 RegValue; RegValue = HAL_RUART_READ32(uart_idx, RUART_LINE_CTL_REG_OFF); RegValue &= ~(BIT_UART_LCR_BREAK_CTRL); HAL_RUART_WRITE32(uart_idx, RUART_LINE_CTL_REG_OFF, RegValue); }
int serial_writable(serial_t *obj) { #ifdef CONFIG_MBED_ENABLED if (obj->index == UART_3) { return log_uart_writable(&stdio_uart_log); } #endif PHAL_RUART_ADAPTER pHalRuartAdapter=(PHAL_RUART_ADAPTER)&(obj->hal_uart_adp); u8 uart_idx = pHalRuartAdapter->UartIndex; if (HAL_RUART_READ32(uart_idx, RUART_LINE_STATUS_REG_OFF) & (RUART_LINE_STATUS_REG_THRE)) { return 1; } else { return 0; } }
HAL_Status HalRuartInitRtl8195a_Patch( IN VOID *Data ///< RUART Adapter ) { /* DBG_ENTRANCE; */ u32 RegValue; u32 Divisor; u32 Dll; u32 Dlm; u8 UartIndex; s32 ElementIndex; RUART_SPEED_SETTING RuartSpeedSetting; u8 PinmuxSelect; PHAL_RUART_ADAPTER pHalRuartAdapter = (PHAL_RUART_ADAPTER) Data; UartIndex = pHalRuartAdapter->UartIndex; PinmuxSelect = pHalRuartAdapter->PinmuxSelect; if (UartIndex > 2) { DBG_UART_ERR(ANSI_COLOR_MAGENTA"HalRuartInitRtl8195a: Invalid UART Index\n"ANSI_COLOR_RESET); return HAL_ERR_PARA; } DBG_UART_INFO("%s==>\n", __FUNCTION__); DBG_UART_INFO("HalRuartInitRtl8195a: [UART %d] PinSel=%d\n", UartIndex, PinmuxSelect); if(( PinmuxSelect == RUART0_MUX_TO_GPIOE ) && ((UartIndex == 0) || (UartIndex == 1))) { // confict with J-Teg pin, so we disable the J-Teg DBG_UART_WARN(ANSI_COLOR_MAGENTA"Disable JTAG function due to pin conflict\n"ANSI_COLOR_RESET); JTAG_PIN_FCTRL(OFF); } // switch Pin from EEPROM to UART0 if(( PinmuxSelect == RUART0_MUX_TO_GPIOC ) && (UartIndex == 0)) { RegValue = HAL_READ32(SYSTEM_CTRL_BASE, 0xa4); if (RegValue & 0x10) { DBG_UART_WARN("Disable EEPROM Pin for UART 0\n"); HAL_WRITE32(SYSTEM_CTRL_BASE, 0xa4, (RegValue & (~0x10))); } } switch (UartIndex) { case 0: /* UART 0 */ ACTCK_UART0_CCTRL(ON); SLPCK_UART0_CCTRL(ON); PinCtrl(UART0, PinmuxSelect, ON); UART0_FCTRL(ON); UART0_BD_FCTRL(ON); break; case 1: /* UART 1 */ ACTCK_UART1_CCTRL(ON); SLPCK_UART1_CCTRL(ON); PinCtrl(UART1, PinmuxSelect, ON); UART1_FCTRL(ON); UART1_BD_FCTRL(ON); break; case 2: /* UART 1 */ ACTCK_UART2_CCTRL(ON); SLPCK_UART2_CCTRL(ON); PinCtrl(UART2, PinmuxSelect, ON); UART2_FCTRL(ON); UART2_BD_FCTRL(ON); break; default: DBG_UART_ERR("Invalid UART Index(%d)\n", UartIndex); return HAL_ERR_PARA; } /* Reset RX FIFO */ HalRuartResetRxFifoRtl8195a(Data); DBG_UART_INFO(ANSI_COLOR_CYAN"HAL UART Init[UART %d]\n"ANSI_COLOR_RESET, UartIndex); /* Disable all interrupts */ HAL_RUART_WRITE32(UartIndex, RUART_INTERRUPT_EN_REG_OFF, 0x00); /* Set DLAB bit to 1 to access DLL/DLM */ RegValue = HAL_RUART_READ32(UartIndex, RUART_LINE_CTL_REG_OFF); RegValue |= RUART_LINE_CTL_REG_DLAB_ENABLE; HAL_RUART_WRITE32(UartIndex, RUART_LINE_CTL_REG_OFF, RegValue); DBG_UART_INFO("[R] RUART_LINE_CTL_REG_OFF(0x0C) = %x\n", HAL_RUART_READ32(UartIndex, RUART_LINE_CTL_REG_OFF)); /* Set Baudrate Division */ #if 1 ElementIndex = FindElementIndex(pHalRuartAdapter->BaudRate, BAUDRATE); if (ElementIndex < 0) { ElementIndex = 5; DBG_UART_ERR("Invalid BaudRate(%d), Force it as default(%d)\n", pHalRuartAdapter->BaudRate, BAUDRATE[ElementIndex]); } RuartSpeedSetting.BaudRate = BAUDRATE[ElementIndex]; RuartSpeedSetting.Ovsr = OVSR_PATCH[ElementIndex]; RuartSpeedSetting.Div = DIV_PATCH[ElementIndex]; RuartSpeedSetting.Ovsr_adj = OVSR_ADJ_PATCH[ElementIndex]; #else RuartSpeedSetting.BaudRate = 38400; RuartSpeedSetting.Ovsr = 10; RuartSpeedSetting.Div = 217; RuartSpeedSetting.Ovsr_adj = 0x0; #endif DBG_UART_INFO("Baud %d, Ovsr %d, Div %d, OvsrAdj 0x%X\n", RuartSpeedSetting.BaudRate, RuartSpeedSetting.Ovsr, RuartSpeedSetting.Div, RuartSpeedSetting.Ovsr_adj ); /* Divisor = (SYSTEM_CLK / ((ovsr + 5 + ovsr_adj/11) * (UartAdapter.BaudRate))); */ /* Set Divisor */ Divisor = RuartSpeedSetting.Div; Dll = Divisor & 0xFF; Dlm = (Divisor & 0xFF00) >> 8; DBG_UART_INFO("Calculated Dll, Dlm = %02x, %02x\n", Dll, Dlm); DBG_UART_INFO("---- Before setting baud rate ----\n"); DBG_UART_INFO(" [R] RUART_DLL_OFF(0x00) = %x\n", HAL_RUART_READ32(UartIndex, RUART_DLL_OFF)); DBG_UART_INFO(" [R] RUART_DLM_OFF(0x04) = %x\n", HAL_RUART_READ32(UartIndex, RUART_DLM_OFF)); HAL_RUART_WRITE32(UartIndex, RUART_DLL_OFF, Dll); HAL_RUART_WRITE32(UartIndex, RUART_DLM_OFF, Dlm); DBG_UART_INFO("---- After setting baud rate ----\n"); DBG_UART_INFO(" [R] RUART_DLL_OFF(0x00) = %x\n", HAL_RUART_READ32(UartIndex, RUART_DLL_OFF)); DBG_UART_INFO(" [R] RUART_DLM_OFF(0x04) = %x\n", HAL_RUART_READ32(UartIndex, RUART_DLM_OFF)); DBG_UART_INFO(ANSI_COLOR_CYAN"---- Befor OVSR & OVSR_ADJ ----\n"ANSI_COLOR_RESET); RegValue = HAL_RUART_READ32(UartIndex, RUART_SCRATCH_PAD_REG_OFF); DBG_UART_INFO("UART%d SPR(0x1C) = %X\n", UartIndex, RegValue); RegValue = HAL_RUART_READ32(UartIndex, RUART_STS_REG_OFF); DBG_UART_INFO("UART%d SIS(0x20) = %X\n", UartIndex, RegValue); /** * Clean Rx break signal interrupt status at initial stage. */ RegValue = HAL_RUART_READ32(UartIndex, RUART_SCRATCH_PAD_REG_OFF); RegValue |= RUART_SP_REG_RXBREAK_INT_STATUS; HAL_RUART_WRITE32(UartIndex, RUART_SCRATCH_PAD_REG_OFF, RegValue); /* Set OVSR(xfactor) */ RegValue = HAL_RUART_READ32(UartIndex, RUART_STS_REG_OFF); RegValue &= ~(RUART_STS_REG_XFACTOR); RegValue |= (((RuartSpeedSetting.Ovsr - 5) << 4) & RUART_STS_REG_XFACTOR); HAL_RUART_WRITE32(UartIndex, RUART_STS_REG_OFF, RegValue); /* Set OVSR_ADJ[10:0] (xfactor_adj[26:16]) */ RegValue = HAL_RUART_READ32(UartIndex, RUART_SCRATCH_PAD_REG_OFF); RegValue &= ~(RUART_SP_REG_XFACTOR_ADJ); RegValue |= ((RuartSpeedSetting.Ovsr_adj << 16) & RUART_SP_REG_XFACTOR_ADJ); HAL_RUART_WRITE32(UartIndex, RUART_SCRATCH_PAD_REG_OFF, RegValue); DBG_UART_INFO(ANSI_COLOR_CYAN"---- After OVSR & OVSR_ADJ ----\n"ANSI_COLOR_RESET); RegValue = HAL_RUART_READ32(UartIndex, RUART_SCRATCH_PAD_REG_OFF); DBG_UART_INFO("UART%d SPR(0x1C) = %X\n", UartIndex, RegValue); RegValue = HAL_RUART_READ32(UartIndex, RUART_STS_REG_OFF); DBG_UART_INFO("UART%d SIS(0x20) = %X\n", UartIndex, RegValue); /* clear DLAB bit */ RegValue = HAL_RUART_READ32(UartIndex, RUART_LINE_CTL_REG_OFF); RegValue &= ~(RUART_LINE_CTL_REG_DLAB_ENABLE); HAL_RUART_WRITE32(UartIndex, RUART_LINE_CTL_REG_OFF, RegValue); DBG_UART_INFO("[R] RUART_LINE_CTL_REG_OFF(0x0C) = %x\n", HAL_RUART_READ32(UartIndex, RUART_LINE_CTL_REG_OFF)); DBG_UART_INFO("[R] UART%d INT_EN(0x04) = %x\n", UartIndex, pHalRuartAdapter->Interrupts); RegValue = ((pHalRuartAdapter->Interrupts) & 0xFF); HAL_RUART_WRITE32(UartIndex, RUART_INTERRUPT_EN_REG_OFF, RegValue); DBG_UART_INFO("[W] UART%d INT_EN(0x04) = %x\n", UartIndex, RegValue); /* Configure FlowControl */ if (pHalRuartAdapter->FlowControl == AUTOFLOW_ENABLE) { RegValue = HAL_RUART_READ32(UartIndex, RUART_MODEM_CTL_REG_OFF); RegValue |= RUART_MCL_AUTOFLOW_ENABLE; HAL_RUART_WRITE32(UartIndex, RUART_MODEM_CTL_REG_OFF, RegValue); } /* RUART DMA Initialization */ HalRuartDmaInitRtl8195a(pHalRuartAdapter); DBG_UART_INFO("[R] UART%d LCR(0x%02X): %X\n", UartIndex, RUART_LINE_CTL_REG_OFF, HAL_RUART_READ32(UartIndex, RUART_LINE_CTL_REG_OFF)); RegValue = HAL_RUART_READ32(UartIndex, RUART_LINE_CTL_REG_OFF); /* PARITY CONTROL */ RegValue &= BIT_CLR_LCR_WLS; RegValue |= BIT_LCR_WLS(pHalRuartAdapter->WordLen); RegValue &= BIT_INVC_LCR_STB_EN; RegValue |= BIT_LCR_STB_EN(pHalRuartAdapter->StopBit); RegValue &= BIT_INVC_LCR_PARITY_EN; RegValue |= BIT_LCR_PARITY_EN(pHalRuartAdapter->Parity); /* PARITY TYPE SELECT */ RegValue &= BIT_INVC_LCR_PARITY_TYPE; RegValue |= BIT_LCR_PARITY_TYPE(pHalRuartAdapter->ParityType); /* STICK PARITY CONTROL */ RegValue &= BIT_INVC_LCR_STICK_PARITY_EN; RegValue |= BIT_LCR_STICK_PARITY_EN(pHalRuartAdapter->StickParity); HAL_RUART_WRITE32(UartIndex, RUART_LINE_CTL_REG_OFF, RegValue); DBG_UART_INFO("[W] UART%d LCR(0x%02X): %X\n", UartIndex, RUART_LINE_CTL_REG_OFF, HAL_RUART_READ32(UartIndex, RUART_LINE_CTL_REG_OFF)); /* Need to assert RTS during initial stage. */ if (pHalRuartAdapter->FlowControl == AUTOFLOW_ENABLE) { HalRuartRTSCtrlRtl8195a(Data, 1); } pHalRuartAdapter->State = HAL_UART_STATE_READY; return HAL_OK; }