// // Generic method for sending a response. // void SendResponse(unsigned char *buffer, unsigned char length) { _txBufferLength = length; _currentTxByte = buffer; _currentTxCount = 0; UARTPORT(DR = _txBufferLength + 1); UARTPORT(CR2_TIEN = 1); }
__interrupt void UART_T_TXE_IRQHandler(void) { if (_currentTxCount < _txBufferLength) { UARTPORT(DR = *_currentTxByte++); _currentTxCount++; } else { UARTPORT(CR2_TIEN = 0); } }
/* -------------------------------------------------------------------------------- Function name : void UARTRest(void) Author : Description : Input : Return : History: <author> <time> <version> desc: -------------------------------------------------------------------------------- */ void UARTRest(eUART_CH uartPort) { UART_REG * pReg = UARTPORT(uartPort); pReg->UART_SRR = UART_RESET | RCVR_FIFO_REST | XMIT_FIFO_RESET; pReg->UART_IER = 0; pReg->UART_DMASA = 1; }
/* -------------------------------------------------------------------------------- Function name : UARTSetFifoEnabledNumb() Author : Description : Input : Return : 0:OK -1:fail History: <author> <time> <version> desc: -------------------------------------------------------------------------------- */ void UARTSetFifoEnabledNumb(eUART_CH uartPort,uint32 param) { UART_REG * pReg = UARTPORT(uartPort); pReg->UART_SFE = (param & 0x01); pReg->UART_SRT = (param >> 1) & 0x03; pReg->UART_STET = (param >> 3) & 0x03; }
void UARTSetAutoRTS(eUART_CH uartPort) { UART_REG * pReg ; pReg = UARTPORT(uartPort); pReg->UART_FCR = IRDA_SIR_DISABLED <<6 | 0<<4 | 1<<0; pReg->UART_MCR = AUTO_FLOW_ENSABLED | 1<<1; //request to send }
void UARTEnableFlowControl(eUART_CH uartPort) { UART_REG * pReg ; pReg = UARTPORT(uartPort); pReg->UART_MCR = AUTO_FLOW_ENSABLED; pReg->UART_FCR = 0x1; }
/*0 = cts_n input is de-asserted (logic 1) 1 = cts_n input is asserted (logic 0) */ uint32 UARTGetCTSState(eUART_CH uartPort) { UART_REG * pReg ; uint32 cts_status; pReg = UARTPORT(uartPort); cts_status = pReg->UART_MSR & (1<<4); return cts_status; }
/* -------------------------------------------------------------------------------- Function name : int32 UARTSetIOP(uint8 useIrDA) Author : Description : set uart role as serial port or IRDA. Input : Return : 0:OK -1:fail History: <author> <time> <version> desc: -------------------------------------------------------------------------------- */ int32 UARTSetIOP(eUART_CH uartPort,uint8 useIrDA) { //pUART_REG phwHead = (pUART_REG)UART1_BASE_ADDR; UART_REG * pReg = UARTPORT(uartPort); if ((useIrDA == IRDA_SIR_DISABLED) || (useIrDA == IRDA_SIR_ENSABLED)) { pReg->UART_MCR = useIrDA; return (0); } return (-1); }
uint32 Memory_to_Uart( eDMA_CHN dmaPort, eUART_CH uartPort, uint32 srcAddr, uint32 size ) { UART_REG * pReg ; uint32 dstAddr; DMA_CFGX DmaCfg; pReg = UARTPORT(uartPort); dstAddr = (uint32)&(pReg->UART_THR); switch( uartPort) { case UART_DEV0: case UART_DEV2: DmaCfg.CTLL = DMA_CTLL_M2UART_02; DmaCfg.CFGL = DMA_CFGL_M2UART_02; DmaCfg.CFGH = DMA_CFGH_M2UART_02; DmaCfg.pLLP = 0; break; case UART_DEV1: DmaCfg.CTLL = DMA_CTLL_M2UART_1; DmaCfg.CFGL = DMA_CFGL_M2UART_1; DmaCfg.CFGH = DMA_CFGH_M2UART_1; DmaCfg.pLLP = 0; break; case UART_DEV3: break; case UART_DEV4: break; case UART_DEV5: break; } DmaConfig(dmaPort, srcAddr,dstAddr, size, &DmaCfg, NULL); return 0; }
int32 UARTIntInit(eUART_CH uartPort,eUART_BaudRate baudRate,eUART_dataLen dataBit,eUART_stopBit stopBit,eUART_parity_en parity) { UART_REG * pReg = UARTPORT(uartPort); UARTRest(uartPort); UARTSetIOP(uartPort,IRDA_SIR_DISABLED); UARTSetFifoEnabledNumb(uartPort,SHADOW_FIFI_ENABLED | RCVR_TRIGGER_ONE | TX_TRIGGER_EMPTY); UARTSetLcrReg(uartPort,dataBit,parity,stopBit); UARTSetBaudRate(uartPort,24 * 1000 * 1000, baudRate); pReg->UART_SRR = RCVR_FIFO_REST | XMIT_FIFO_RESET; return (0); }
/* -------------------------------------------------------------------------------- Function name : UARTWriteByte(uint8 ch) Author : Description : 串口写一个字节 Input : byte:输入的字节值 uartTimeOut:等待延时时间 Return : 0:OK -1:fail History: <author> <time> <version> desc: -------------------------------------------------------------------------------- */ rk_size_t UARTWriteByte(eUART_CH uartPort,uint8 * pdata, uint32 cnt) { rk_size_t dwRealSize; UART_REG * pReg = UARTPORT(uartPort); dwRealSize = 0; while (cnt--) { if(!(pReg->UART_USR & UART_TRANSMIT_FIFO_NOT_FULL)) { break; } pReg->UART_THR = *pdata++; dwRealSize++; } return dwRealSize; }
/* -------------------------------------------------------------------------------- Function name : UARTSetBaudRate(pUART_REG phead, uint32 baudRate) Author : Description : baudRate:serial initial speed,get by table seek, Input : Return : 0:OK -1:fail History: <author> <time> <version> desc: -------------------------------------------------------------------------------- */ int32 UARTSetBaudRate(eUART_CH uartPort,uint32 clk, uint32 baudRate) { uint32 DivLatch; UART_REG * pReg = UARTPORT(uartPort); // DLL and THR(RBR) are mapped the same offset, set LCR[7] to access to // DLL register, otherwise the THR(RBR) DivLatch = clk / MODE_X_DIV / baudRate; pReg->UART_LCR |= LCR_DLA_EN; pReg->UART_DLL = (DivLatch >> 0) & 0xff; pReg->UART_DLH = (DivLatch >> 8) & 0xff; pReg->UART_LCR &= (~LCR_DLA_EN); return (0); }
/* -------------------------------------------------------------------------------- Function name : int32 UARTReadByte(uint8 *pdata, uint32 uartTimeOut) Author : Description : 串口读取一个字节 Input : uartTimeOut:等待延时时间 Return : 0:OK -1:fail History: <author> <time> <version> desc: -------------------------------------------------------------------------------- */ rk_size_t UARTReadByte(eUART_CH uartPort,uint8 *pdata, uint32 cnt) { rk_size_t dwRealSize; UART_REG * pReg = UARTPORT(uartPort); dwRealSize = 0; while (cnt--) { if (!(pReg->UART_USR & UART_RECEIVE_FIFO_NOT_EMPTY)) { break; } *pdata++ = (uint8 )pReg->UART_RBR; dwRealSize++; } return dwRealSize; }
/* ********************************************************************************************************* * UARTGetIntType(void) * * Description : 判断串口中断类型. * * Argument(s) : void *p_arg * * Return(s) : none * * Note(s) : none. ********************************************************************************************************* */ uint32 UARTGetIntType(eUART_CH uartPort) { uint32 dwUartIntStaus; uint event; UART_REG * pReg = UARTPORT(uartPort); dwUartIntStaus = pReg->UART_IIR & UART_IF_MASK; event = 0; if(dwUartIntStaus & UART_IF_REC_DATA) { event |= UART_INT_READ_FIFO_NOT_EMPTY; } if(dwUartIntStaus & UART_IF_THR_EMPTY) { event |= UART_INT_WRITE_FIFO_EMPTY; } return event; }
__interrupt void UART_R_RXNE_IRQHandler(void) { unsigned char dataByte = UARTPORT(DR); if ((_uartMode == UART_MODE_WAITING_FOR_DATA) && (dataByte == 0xaa)) { SetupRxBuffer(); _uartMode = UART_MODE_RECEIVING_DATA; } else { if (_uartMode == UART_MODE_RECEIVING_DATA) { if (_currentRxCount < (UART_RX_BUFFER_SIZE - 1)) { *_currentRxByte++ = dataByte; _currentRxCount++; if (_currentRxCount > 1) { if ((_rxBuffer[0] - 1) == _currentRxCount) { ProcessUARTData(); } } } else { // // If we get here we have filled the UART Rx buffer. // Not a lot we can do really so reset the system to // wait for a new command. // SendNAK(EC_RX_BUFFER_OVERFLOW); _uartMode = UART_MODE_WAITING_FOR_DATA; } } } }
//-------------------------------------------------------------------------------- // // Setup the UART to run at 57600 baud, no parity, one stop bit, 8 data bits. // // Important: This relies upon the system clock being set to run at 2 MHz. // void SetupUART() { unsigned char tmp = UARTPORT(SR); tmp = UARTPORT(DR); // // Reset the UART registers to the reset values. // UARTPORT(CR1 = 0); UARTPORT(CR2 = 0); UARTPORT(CR4 = 0); UARTPORT(CR3 = 0); UARTPORT(GTR = 0); UARTPORT(PSCR = 0); // // Now setup the port to 57600,n,8,1. // UARTPORT(CR1_M = 0); // 8 Data bits. UARTPORT(CR1_PCEN = 0); // Disable parity. UARTPORT(CR3_STOP = 0); // 1 stop bit. UARTPORT(BRR2 = 0x03); // Set the baud rate registers to 57600 baud UARTPORT(BRR1 = 0x02); // based upon a 2 MHz system clock. // // Disable the transmitter and receiver. // UARTPORT(CR2_TEN = 0); // Disable transmit. UARTPORT(CR2_REN = 0); // Disable receive. SetupRxBuffer(); // // Turn on the UART transmit, receive and the UART clock. // UARTPORT(CR2_TEN = 1); UARTPORT(CR2_RIEN = 1); UARTPORT(CR2_REN = 1); }
/* -------------------------------------------------------------------------------- Function name : int32 UARTSetLcrReg(uint8 byteSize, uint8 parity,uint8 stopBits ) Author : Description : Input : Return : 0:OK -1:fail History: <author> <time> <version> desc: -------------------------------------------------------------------------------- */ int32 UARTSetLcrReg(eUART_CH uartPort,uint8 byteSize, uint8 parity,uint8 stopBits ) { uint32 lcr; int32 bRet = 0; UART_REG * pReg = UARTPORT(uartPort); lcr = (pReg->UART_LCR) & (~(uint32)(UART_DATABIT_MASK)); //Data length select switch ( byteSize ) { case UART_DATA_5B: lcr |= LCR_WLS_5; break; case UART_DATA_6B: lcr |= LCR_WLS_6; break; case UART_DATA_7B: lcr |= LCR_WLS_7; break; case UART_DATA_8B: lcr |= LCR_WLS_8; break; default: bRet = -1; break; } //Parity set switch ( parity ) { case UART_ODD_PARITY: case UART_EVEN_PARITY: lcr |= PARITY_ENABLED; lcr |= ((parity) << 4); break; case UART_PARITY_DISABLE: lcr &= ~PARITY_ENABLED; break; default: bRet = -1; break; } //StopBits set switch ( stopBits ) { case 0: lcr |= ONE_STOP_BIT; break; case 1: lcr |= ONE_HALF_OR_TWO_BIT; break; default: bRet = -1; break; } if (bRet == 0) { pReg->UART_LCR = lcr; } return(bRet); }
/* -------------------------------------------------------------------------------- Function name : void UARTSetIntEnabled(uint32 uartIntNumb) Author : Description : 设置UART口的中断使能打开。 Input : uartIntNumb:UART的中断号 Return : 0:OK -1:fail History: <author> <time> <version> desc: -------------------------------------------------------------------------------- */ void UARTSetIntEnabled(eUART_CH uartPort,uint32 uartIntNumb) { UART_REG * pReg = UARTPORT(uartPort); pReg->UART_IER |= uartIntNumb; }
/* -------------------------------------------------------------------------------- Function name : void UARTSetIntDisabled(uint32 uartIntNumb) Author : Description : 关中断使能 Input : uartIntNumb:UART的中断号 Return : 0:OK -1:fail History: <author> <time> <version> desc: -------------------------------------------------------------------------------- */ void UARTSetIntDisabled(eUART_CH uartPort,uint32 uartIntNumb) { UART_REG * pReg = UARTPORT(uartPort); pReg->UART_IER &= (~uartIntNumb); }