static rt_err_t lpc_configure(struct rt_serial_device *serial, struct serial_configure *cfg) { struct lpc_uart *uart; UART_BAUD_T baud; UART_CFG_T UART_cfg; RT_ASSERT(serial != RT_NULL); uart = (struct lpc_uart *)serial->parent.user_data; /* Initialize UART Configuration parameter structure to default state: * Baudrate = 115200 b * 8 data bit * 1 Stop bit * None parity */ /* Set up baudrate parameters */ baud.clk = Chip_Clock_GetAsyncSyscon_ClockRate(); /* Clock frequency */ baud.baud = cfg->baud_rate; /* Required baud rate */ baud.ovr = 0; /* Set the oversampling to the recommended rate */ baud.mul = baud.div = 0; if(!baud.mul) { _UART_CalcMul(&baud); } _UART_CalcDiv(&baud); /* Set fractional control register */ LPC_ASYNC_SYSCON->FRGCTRL = ((uint32_t) baud.mul << 8) | 0xFF; /* Configure the UART */ UART_cfg.cfg = UART_CFG_8BIT; UART_cfg.div = baud.div; /* Use the calculated div value */ UART_cfg.ovr = baud.ovr; /* Use oversampling rate from baud */ UART_cfg.res = UART_BIT_DLY(cfg->baud_rate); /* P254,255,246 */ uart->UART->OSR = (UART_cfg.ovr - 1) & 0x0F; uart->UART->BRG = (UART_cfg.div - 1) & 0xFFFF; uart->UART->CFG = UART_CFG_ENABLE | (UART_cfg.cfg & ~UART_CFG_RES); return RT_EOK; }
/* Configure UART ROM Driver and pripheral */ static int uartrom_config(void) { UART_CFG_T cfg; UART_BAUD_T baud; /* Set up baudrate parameters */ baud.clk = Chip_Clock_GetAsyncSyscon_ClockRate(); /* Clock frequency */ baud.baud = UART_BAUD_RATE; /* Required baud rate */ baud.ovr = 0; /* Set the oversampling to the recommended rate */ baud.mul = baud.div = 0; if (ROM_UART_CalBaud(&baud) != LPC_OK) { /* Unable to calculate the baud rate parameters */ while (1) {} } /* Set fractional control register */ Chip_SYSCON_SetUSARTFRGCtrl(baud.mul, 255); /* See if the calculated baud is < +/- UART_BUAD_ERR% of the required baud */ if (ABS(baud.baud - UART_BAUD_RATE) > (UART_BAUD_RATE * UART_BUAD_ERR) / 100) { /* WARNING: Baud rate is has more than UART_BUAD_ERR percentage */ /* Try to auto-detect the Oversampling rate by setting baud.ovr to 0 */ while (1) {} } /* Configure the UART */ cfg.cfg = UART_CFG_8BIT | UART_CFG_BRKRX; cfg.div = baud.div; /* Use the calculated div value */ cfg.ovr = baud.ovr; /* Use oversampling rate from baud */ cfg.res = UART_BIT_DLY(UART_BAUD_RATE); /* Configure the UART */ ROM_UART_Configure(hUART, &cfg); NVIC_ClearPendingIRQ(UART0_IRQn); NVIC_EnableIRQ(UART0_IRQn); return 0; }