void xl_UART_Config(unsigned char port) { UART_CFG_Type UARTConfigStruct; UART_FIFO_CFG_Type UARTFIFOConfigStruct; UART_ConfigStructInit(&UARTConfigStruct); UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); switch(port) { case 0: PINSEL_ConfigPin(0,2,1); PINSEL_ConfigPin(0,3,1); UARTConfigStruct.Baud_rate = 115200; UART_Init( (UART_ID_Type)port, &UARTConfigStruct ); UART_FIFOConfig( (UART_ID_Type)port, &UARTFIFOConfigStruct ); UART_TxCmd( (UART_ID_Type)port, ENABLE ); UART_IntConfig(UART_0, UART_INTCFG_RBR, ENABLE); UART_IntConfig(UART_0, UART_INTCFG_RLS, ENABLE); NVIC_SetPriority(UART0_IRQn, ((0x02<<3)|0x02)); NVIC_EnableIRQ(UART0_IRQn); break; case 2: PINSEL_ConfigPin(0,10,1); PINSEL_ConfigPin(0,11,1); /** */ PINSEL_ConfigPin(1, 19, 6); UARTConfigStruct.Baud_rate = 9600; UART_Init( (UART_ID_Type)port, &UARTConfigStruct ); LPC_UART2->RS485CTRL = 0x31; LPC_UART2->RS485DLY = 0x10; UART_FIFOConfig( (UART_ID_Type)port, &UARTFIFOConfigStruct ); UART_TxCmd( (UART_ID_Type)port, ENABLE ); UART_IntConfig(UART_2, UART_INTCFG_RBR, ENABLE); UART_IntConfig(UART_2, UART_INTCFG_RLS, ENABLE); // NVIC_SetPriority(UART2_IRQn, ((0x02<<3)|0x03)); NVIC_DisableIRQ(UART2_IRQn); break; default: break; } }
void USART_Init(void) { UART_CFG_Type UARTConfigStruct; UART_FIFO_CFG_Type UARTFIFOConfigStruct; PINSEL_ConfigPin(0,2,1); PINSEL_ConfigPin(0,3,1); UART_ConfigStructInit(&UARTConfigStruct); UART_Init(USART_ID, &UARTConfigStruct); UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); #if USART_FIFO_LEVEL == 1 UARTFIFOConfigStruct.FIFO_Level = UART_FIFO_TRGLEV0; #elif USART_FIFO_LEVEL == 4 UARTFIFOConfigStruct.FIFO_Level = UART_FIFO_TRGLEV1; #elif USART_FIFO_LEVEL == 8 UARTFIFOConfigStruct.FIFO_Level = UART_FIFO_TRGLEV2; #elif USART_FIFO_LEVEL == 14 UARTFIFOConfigStruct.FIFO_Level = UART_FIFO_TRGLEV3; #endif UART_FIFOConfig(USART_ID, &UARTFIFOConfigStruct); UART_TxCmd(USART_ID, ENABLE); UART_IntConfig(USART_ID, UART_INTCFG_RBR, ENABLE); UART_IntConfig(USART_ID, UART_INTCFG_RLS, ENABLE); UART_IntConfig(USART_ID, UART_INTCFG_THRE, ENABLE); NVIC_SetPriority(USART_IRQ, ((0x01<<3)|0x01)); NVIC_EnableIRQ(USART_IRQ); }
void uart_init() { UART_CFG_Type cfg; UART_FIFO_CFG_Type fifo_cfg; UART_ConfigStructInit(&cfg); cfg.Baud_rate = 57600; PINSEL_CFG_Type PinCfg; PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 2; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 3; PINSEL_ConfigPin(&PinCfg); UART_Init(LPC_UART0, &cfg); UART_FIFOConfigStructInit(&fifo_cfg); UART_FIFOConfig(LPC_UART0, &fifo_cfg); UART_TxCmd(LPC_UART0, ENABLE); }
/********************************************************************************************************* ** Function name: uartInit ** Descriptions: 串口初始化,设置为8位数据位,1位停止位,无奇偶校验,波特率为115200 ** input parameters: 无 ** output parameters: 无 ** Returned value: 无 *********************************************************************************************************/ void UART0Init (void) { UART_CFG_Type UART_ConfigStruct_Test; UART_FIFO_CFG_Type FIFOCfg_Test; UART_INT_Type UARTIntCfg_Test; GPIO_PinselConfig(RXD0,1); GPIO_PinselConfig(TXD0,1); GPIO_PinselConfig(EN_485_0,0); WriteEN_485_0(0); UART_ConfigStruct_Test.Baud_rate = UART0_BPS; UART_ConfigStruct_Test.Databits = UART_DATABIT_8; UART_ConfigStruct_Test.Parity = UART_PARITY_NONE; UART_ConfigStruct_Test.Stopbits = UART_STOPBIT_1; UART_Init((LPC_UART_TypeDef *)LPC_UART0,(UART_CFG_Type *)&UART_ConfigStruct_Test); UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0,ENABLE); FIFOCfg_Test.FIFO_DMAMode = DISABLE; FIFOCfg_Test.FIFO_Level = UART_FIFO_TRGLEV2; FIFOCfg_Test.FIFO_ResetRxBuf = ENABLE; FIFOCfg_Test.FIFO_ResetTxBuf = ENABLE; UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART0, (UART_FIFO_CFG_Type *)&FIFOCfg_Test); UARTIntCfg_Test = UART_INTCFG_RBR; UART_IntConfig((LPC_UART_TypeDef *)LPC_UART0, UARTIntCfg_Test,ENABLE); InitFIFO((stcFIFO *)&sUART0RecFIFO,UART0RecBuf,sizeof(UART0RecBuf)); NVIC_EnableIRQ(UART0_IRQn); NVIC_SetPriority(UART0_IRQn, UART0_IRQn); }
void serial_init(void) { PINSEL_CFG_Type PinCfg; UART_CFG_Type UARTConfigStruct; UART_FIFO_CFG_Type UARTFIFOConfigStruct; //Init pins PinCfg.Funcnum = PINSEL_FUNC_1; PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL; PinCfg.Pinmode = PINSEL_PINMODE_PULLUP; PinCfg.Portnum = PINSEL_PORT_0; PinCfg.Pinnum = PINSEL_PIN_2; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = PINSEL_PIN_3; PINSEL_ConfigPin(&PinCfg); //Init UART Configuration parameter structure to default state: 115200,8,1,N UART_ConfigStructInit(&UARTConfigStruct); UARTConfigStruct.Baud_rate = 115200; //Init FIFOConfigStruct using defaults UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); //Start the devices USB serial UART_Init((LPC_UART_TypeDef *)LPC_UART0, &UARTConfigStruct); UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART0, &UARTFIFOConfigStruct); UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0, ENABLE); }
/* Desc:Configures the PINSEL data structure to setup pin P0.2 for use with the I2C bus and enables it */ void setupUART(){ //UART works with 4 main steps PINSEL_CFG_Type PinCfg; //Configure Pin select register UART_CFG_Type UARTConfigStruct; //Configure the UART Register UART_FIFO_CFG_Type FifoCfg; //Configure the UART FIFO Register //Init UART PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 0; PinCfg.Pinnum = 2; PINSEL_ConfigPin(&PinCfg); UART_ConfigStructInit(&UARTConfigStruct); UART_Init((LPC_UART_TypeDef *)LPC_UART0, &UARTConfigStruct); UART_FIFOConfigStructInit(&FifoCfg); UART_FIFOConfig((LPC_UART_TypeDef *) LPC_UART0, &FifoCfg); //Enable UART Transmit UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0, ENABLE); }
void sio_init(void) { //LPC_USART_3 PINSEL_CFG_Type PinSelCfg; UART_CFG_Type UartCFG_Struct; UART_FIFO_CFG_Type UART_FIFO_CFG_Struct; // P4_28 PinSelCfg.Portnum = PINSEL_PORT_4; PinSelCfg.Pinnum = PINSEL_PIN_28; PinSelCfg.Funcnum = PINSEL_FUNC_3; PinSelCfg.OpenDrain = PINSEL_PINMODE_NORMAL; PinSelCfg.Pinmode = PINSEL_PINMODE_PULLUP; PINSEL_ConfigPin(&PinSelCfg); // P4_29 PinSelCfg.Pinnum = PINSEL_PIN_29; PINSEL_ConfigPin(&PinSelCfg); uartDataToSend.currPtr = 0; uartDataToSend.lastPtr = 0; /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UartCFG_Struct); /* Set Baudrate to 115200 */ UartCFG_Struct.Baud_rate = 115200; /* Initialize UART3 peripheral with given to corresponding parameter */ UART_Init(SERIAL_USART, &UartCFG_Struct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UART_FIFO_CFG_Struct); /* Initialize FIFO for UART3 peripheral */ UART_FIFOConfig(SERIAL_USART, &UART_FIFO_CFG_Struct); /* Enable UART Transmit */ UART_TxCmd(SERIAL_USART, ENABLE); UART_IntConfig(SERIAL_USART, UART_INTCFG_THRE, ENABLE); NVIC_SetPriorityGrouping(UART3_PriorGrup); NVIC_SetPriority(UART3_IRQn, UART3_Prior); NVIC_EnableIRQ(UART3_IRQn); }
/* *@描述:初始化串口 *@参数:void *@返回:无 */ void InitUart(void) { // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; // Pin configuration for UART0 PINSEL_CFG_Type PinCfg; /* * Initialize UART pin connect */ PinCfg.OpenDrain = UARTOpendrain; PinCfg.Pinmode = UARTPinMode; PinCfg.Portnum = UARTPortTX; PinCfg.Pinnum = UARTPinTX; PinCfg.Funcnum = UARTFuncTX; PINSEL_ConfigPin(&PinCfg); PinCfg.Portnum = UARTPortRX; PinCfg.Pinnum = UARTPinRX; PinCfg.Funcnum = UARTFuncRX; PINSEL_ConfigPin(&PinCfg); /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); // Re-configure baudrate to 57600bps UARTConfigStruct.Baud_rate = 57600; // Initialize UART peripheral with given to corresponding parameter UART_Init((LPC_UART_TypeDef *)_LPC_UART, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); UARTFIFOConfigStruct.FIFO_Level = UART_FIFO_TRGLEV3;//8 character // Initialize FIFO for UART0 peripheral UART_FIFOConfig((LPC_UART_TypeDef *)_LPC_UART, &UARTFIFOConfigStruct); // Enable UART Transmit UART_TxCmd((LPC_UART_TypeDef *)_LPC_UART, ENABLE); /* Enable UART Rx interrupt */ UART_IntConfig((LPC_UART_TypeDef *)_LPC_UART, UART_INTCFG_RBR, ENABLE); /* Enable UART line status interrupt */ UART_IntConfig((LPC_UART_TypeDef *)_LPC_UART, UART_INTCFG_RLS, DISABLE); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(_UART_IRQ, ((0x01<<3)|0x01)); /* Enable Interrupt for UART channel */ NVIC_EnableIRQ(_UART_IRQ); BufInit(&bufSerialRec); }
void uart_init(void) { // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; // Pin configuration for UART PINSEL_CFG_Type PinCfg; /* * Initialize UART3 pin connect: P4.28 -> TXD3; P4.29 -> RXD3 * or P0.2 -> TXD0, P0.3 -> RXD0 */ PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL; PinCfg.Pinmode = PINSEL_PINMODE_PULLUP; #if DBG_UART_NUM == 3 PinCfg.Funcnum = PINSEL_FUNC_3; PinCfg.Portnum = 4; PinCfg.Pinnum = 28; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 29; PINSEL_ConfigPin(&PinCfg); #else PinCfg.Funcnum = PINSEL_FUNC_1; PinCfg.Portnum = 0; PinCfg.Pinnum = 2; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 3; PINSEL_ConfigPin(&PinCfg); #endif /* Initialize UART Configuration parameter structure to default state: * Baudrate = as below * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); UARTConfigStruct.Baud_rate = 115200; // Initialize UART peripheral with given to corresponding parameter UART_Init(DBG_UART, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Initialize FIFO for UART peripheral UART_FIFOConfig(DBG_UART, &UARTFIFOConfigStruct); // Enable UART Transmit UART_TxCmd(DBG_UART, ENABLE); }
/*********************************************************************//** * @brief Client Hardware Initialize * @param[in] None * @return None ***********************************************************************/ void ClientHardwareInit(void) { // UART Configuration structure variable UART_CFG_Type uart_config; // UART FIFO Configuration Struct variable UART_FIFO_CFG_Type uart_fifo; // Pin configuration for UART0 PINSEL_CFG_Type pin; // Initialize UART0 pin connect pin.Funcnum = PINSEL_FUNC_1; pin.OpenDrain = PINSEL_PINMODE_OPENDRAIN; pin.Pinmode = PINSEL_PINMODE_PULLUP; pin.Pinnum = PINSEL_PIN_2; pin.Portnum = PINSEL_PORT_0; PINSEL_ConfigPin(&pin); pin.Pinnum = PINSEL_PIN_3; PINSEL_ConfigPin(&pin); // Initialize UART Configration parameter structure to default state: // Baudrate = 9600bps // 8 data bit // 1 stop bit // None parity UART_ConfigStructInit(&uart_config); // Set baudrate to 115200 */ uart_config.Baud_rate = 115200; // Initialize UART0 peripheral with give to corresponding parameter */ UART_Init((LPC_UART_TypeDef *)LPC_UART0, &uart_config); // Initialize FIFOConfigStruct to default state: // - FIFO_DMAMode = DISABLE // - FIFO_Level = UART_FIFO_TRGLEV0 // - FIFO_ResetRxBuf = ENABLE // - FIFO_ResetTxBuf = ENABLE // - FIFO_State = ENABLE UART_FIFOConfigStructInit(&uart_fifo); // Initialize FIFO for UART0 peripheral UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART0, &uart_fifo); // Enable UART Transmit UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0, ENABLE); // Enable UART Rx interrupt UART_IntConfig((LPC_UART_TypeDef *)LPC_UART0, UART_INTCFG_RBR, ENABLE); // Enable UART line status interrupt UART_IntConfig((LPC_UART_TypeDef *)LPC_UART0, UART_INTCFG_RLS, ENABLE); // preemption = 1, sub-priority = 1 NVIC_SetPriority(UART0_IRQn, ((0x01 << 3) | 0x01)); // Enable Interrupt for UART0 channel NVIC_EnableIRQ(UART0_IRQn); }
void serial_init(uint32_t baud_rate) { UART_CFG_Type UART_config; UART_FIFO_CFG_Type UART_fifo_config; PINSEL_CFG_Type pin_config; // Configure TXD and RXD Pins pin_config.OpenDrain = PINSEL_PINMODE_NORMAL; pin_config.Pinmode = PINSEL_PINMODE_PULLUP; pin_config.Funcnum = UART_TXD_FUNC; pin_config.Portnum = UART_TXD_PORT; pin_config.Pinnum = UART_TXD_PIN; PINSEL_ConfigPin(&pin_config); pin_config.Funcnum = UART_RXD_FUNC; pin_config.Portnum = UART_RXD_PORT; pin_config.Pinnum = UART_RXD_PIN; PINSEL_ConfigPin(&pin_config); #pragma message "BT TXD on Pin : P" STRING(UART_TXD_PORT) "_" STRING(UART_TXD_PIN) #pragma message "BT RXD on Pin : P" STRING(UART_RXD_PORT) "_" STRING(UART_RXD_PIN) // Configure UART UART_config.Baud_rate = baud_rate; UART_config.Databits = UART_DATABIT_8; UART_config.Parity = UART_PARITY_NONE; UART_config.Stopbits = UART_STOPBIT_1; UART_Init((LPC_UART_TypeDef *)UART_PORT, &UART_config); // Configure FIFO UART_fifo_config.FIFO_DMAMode = DISABLE; UART_fifo_config.FIFO_Level = UART_FIFO_TRGLEV0; UART_fifo_config.FIFO_ResetRxBuf = ENABLE; UART_fifo_config.FIFO_ResetTxBuf = ENABLE; UART_FIFOConfig((LPC_UART_TypeDef *)UART_PORT, &UART_fifo_config); // Enable UART Transmit UART_TxCmd((LPC_UART_TypeDef *)UART_PORT, ENABLE); // Enable UART interrupts UART_IntConfig((LPC_UART_TypeDef *)UART_PORT, UART_INTCFG_RLS, ENABLE); UART_IntConfig((LPC_UART_TypeDef *)UART_PORT, UART_INTCFG_RBR, ENABLE); UART_IntConfig((LPC_UART_TypeDef *)UART_PORT, UART_INTCFG_THRE, ENABLE); // preemption = 1, sub-priority = 1 NVIC_SetPriority(UARTx_IRQn, ((0x01 << 3) | 0x01)); // Enable Interrupt for UART NVIC_EnableIRQ(UARTx_IRQn); }
static void uart3_init(rt_uint32_t baudrate) { PINSEL_CFG_Type PinSelCfgType; UART_CFG_Type UartCfgType; UART_FIFO_CFG_Type UartFIFOCfgType; //pin config //TXD PinSelCfgType.Portnum = PINSEL_PORT_0; PinSelCfgType.Pinnum = PINSEL_PIN_0; PinSelCfgType.Funcnum = PINSEL_FUNC_2; PinSelCfgType.Pinmode = PINSEL_PINMODE_PULLUP; PinSelCfgType.OpenDrain = PINSEL_PINMODE_NORMAL; PINSEL_ConfigPin(&PinSelCfgType); //RXD PinSelCfgType.Portnum = PINSEL_PORT_0; PinSelCfgType.Pinnum = PINSEL_PIN_1; PinSelCfgType.Funcnum = PINSEL_FUNC_2; PinSelCfgType.Pinmode = PINSEL_PINMODE_TRISTATE; PinSelCfgType.OpenDrain = PINSEL_PINMODE_NORMAL; PINSEL_ConfigPin(&PinSelCfgType); //init UartCfgType.Baud_rate = baudrate; UartCfgType.Parity = UART_PARITY_NONE; UartCfgType.Databits = UART_DATABIT_8; UartCfgType.Stopbits = UART_STOPBIT_1; UART_Init(LPC_DEV_UART, &UartCfgType); //FIFO UartFIFOCfgType.FIFO_ResetRxBuf = ENABLE; UartFIFOCfgType.FIFO_ResetTxBuf = ENABLE; UartFIFOCfgType.FIFO_DMAMode = DISABLE; UartFIFOCfgType.FIFO_Level = UART_FIFO_TRGLEV2; UART_FIFOConfig(LPC_DEV_UART, &UartFIFOCfgType); //int UART_IntConfig(LPC_DEV_UART, UART_INTCFG_RBR, ENABLE); //enable UART_TxCmd(LPC_DEV_UART, ENABLE); }
static void uart_regs_init(uint8_t id) { uart_tab[id]->bit_len = SystemCoreClock / uart_tab[id]->config.Baud_rate; UART_DeInit(uart_tab[id]->uart); UART_Init(uart_tab[id]->uart, &(uart_tab[id]->config)); /////////////////////////////////////// // init hardware fifo // Initialize FIFO for UART0 peripheral UART_FIFO_CFG_Type UARTFIFOConfigStruct; UARTFIFOConfigStruct.FIFO_DMAMode = DISABLE; UARTFIFOConfigStruct.FIFO_Level = UART_FIFO_TRGLEV2; UARTFIFOConfigStruct.FIFO_ResetRxBuf = ENABLE; UARTFIFOConfigStruct.FIFO_ResetTxBuf = ENABLE; UART_FIFOConfig(uart_tab[id]->uart, &UARTFIFOConfigStruct); UART_TxCmd(uart_tab[id]->uart, ENABLE); // enable transmit uart_tab[id]->uart->IER |= UART_IER_RBRINT_EN; // enable rbr interrupt uart_tab[id]->uart->IER |= UART_IER_THREINT_EN; // enable thre interrupt // Do not enable transmit interrupt here, since it is handled by // UART_Send() function, just to reset Tx Interrupt state for the // first time switch(id) { case 0: NVIC_SetPriority(UART0_IRQn, ((0x01<<3)|0x01)); // preemption = 1, sub-priority = 1 NVIC_EnableIRQ(UART0_IRQn); // Enable Interrupt for UART0 channel break; case 1: NVIC_SetPriority(UART1_IRQn, ((0x01<<3)|0x01)); // preemption = 1, sub-priority = 1 NVIC_EnableIRQ(UART1_IRQn); // Enable Interrupt for UART0 channel break; case 2: NVIC_SetPriority(UART2_IRQn, ((0x01<<3)|0x01)); // preemption = 1, sub-priority = 1 NVIC_EnableIRQ(UART2_IRQn); // Enable Interrupt for UART0 channel break; case 3: NVIC_SetPriority(UART3_IRQn, ((0x01<<3)|0x01)); // preemption = 1, sub-priority = 1 NVIC_EnableIRQ(UART3_IRQn); // Enable Interrupt for UART0 channel default: break; } }
void DEBUG_init(void) // init code for the USB serial line { UART_CFG_Type UARTConfigStruct; // UART Configuration structure variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; // UART FIFO configuration Struct variable PINSEL_CFG_Type PinCfg; // Pin configuration for UART /* * Initialize UART pin connect */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; // USB serial first PinCfg.Portnum = 0; PinCfg.Pinnum = 2; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 3; PINSEL_ConfigPin(&PinCfg); /* Initialize UART Configuration parameter structure to default state: * - Baudrate = 9600bps * - 8 data bit * - 1 Stop bit * - None parity */ UART_ConfigStructInit(&UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Built the basic structures, lets start the devices/ // USB serial UART_Init((LPC_UART_TypeDef *)LPC_UART0, &UARTConfigStruct); // Initialize UART0 peripheral with given to corresponding parameter UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART0, &UARTFIFOConfigStruct); // Initialize FIFO for UART0 peripheral UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0, ENABLE); // Enable UART Transmit }
void SER_Init(uint32_t baudrate) { UART_CFG_Type UART_ConfigStruct; UART_FIFO_CFG_Type UART_FIFOInitStruct; UART_ConfigStruct.Baud_rate = baudrate; UART_ConfigStruct.Parity = UART_PARITY_NONE; UART_ConfigStruct.Stopbits = UART_STOPBIT_1; UART_ConfigStruct.Databits = UART_DATABIT_8; UART_Init(LPC_UART0, &UART_ConfigStruct); //Init UART0 with parameters, defined above UART_TxCmd(LPC_UART0, ENABLE); //Enable transmission on UART TxD pin UART_FIFOConfigStructInit(&UART_FIFOInitStruct); //Fills each UART_FIFOInitStruct member with its default value UART_FIFOConfig(LPC_UART0, &UART_FIFOInitStruct); //Configure FIFO function on UART0 peripheral UART_IntConfig(LPC_UART0,UART_INTCFG_RBR,ENABLE); //Enable UART0 RBR Interrupt NVIC_EnableIRQ(UART0_IRQn); //Enable UART0 Interrupts }
void serialUSBInit() { UART_CFG_Type uartConfigStruct; UART_FIFO_CFG_Type uartFIFOConfigStruct; PINSEL_CFG_Type pinCfg; //Transmission setup pinCfg.Funcnum = 1; pinCfg.OpenDrain = 0; pinCfg.Pinmode = 0; pinCfg.Portnum = 0; pinCfg.Pinnum = 2; PINSEL_ConfigPin(&pinCfg); //Reciever setup pinCfg.Pinnum = 3; PINSEL_ConfigPin(&pinCfg); UART_ConfigStructInit(&uartConfigStruct); UART_FIFOConfigStructInit(&uartFIFOConfigStruct); UART_Init((LPC_UART_TypeDef *)LPC_UART0, &uartConfigStruct); UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART0, &uartFIFOConfigStruct); UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0, ENABLE); }
/*********************************************************************//** * @brief Main UART testing example sub-routine * Print welcome screen first, then press any key to have it * read in from the terminal and returned back to the terminal. * - Press ESC to exit * - Press 'r' to print welcome screen menu again **********************************************************************/ int c_entry(void) { // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; // Pin configuration for UART0 PINSEL_CFG_Type PinCfg; uint32_t idx, len; __IO FlagStatus exitflag; uint8_t buffer[10]; // DeInit NVIC and SCBNVIC NVIC_DeInit(); NVIC_SCBDeInit(); /* Configure the NVIC Preemption Priority Bits: * two (2) bits of preemption priority, six (6) bits of sub-priority. * Since the Number of Bits used for Priority Levels is five (5), so the * actual bit number of sub-priority is three (3) */ NVIC_SetPriorityGrouping(0x05); // Set Vector table offset value #if (__RAM_MODE__==1) NVIC_SetVTOR(0x10000000); #else NVIC_SetVTOR(0x00000000); #endif #if (UART_PORT == 0) /* * Initialize UART0 pin connect */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 2; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 3; PINSEL_ConfigPin(&PinCfg); #endif #if (UART_PORT == 1) /* * Initialize UART1 pin connect */ PinCfg.Funcnum = 2; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 0; PinCfg.Portnum = 2; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 1; PINSEL_ConfigPin(&PinCfg); #endif /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); // Initialize UART0 peripheral with given to corresponding parameter UART_Init(TEST_UART, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Initialize FIFO for UART0 peripheral UART_FIFOConfig(TEST_UART, &UARTFIFOConfigStruct); // Enable UART Transmit UART_TxCmd(TEST_UART, ENABLE); // print welcome screen print_menu(); // Reset exit flag exitflag = RESET; /* Read some data from the buffer */ while (exitflag == RESET) { len = 0; while (len == 0) { len = UART_Receive(TEST_UART, buffer, sizeof(buffer), NONE_BLOCKING); } /* Got some data */ idx = 0; while (idx < len) { if (buffer[idx] == 27) { /* ESC key, set exit flag */ UART_Send(TEST_UART, menu3, sizeof(menu3), BLOCKING); exitflag = SET; } else if (buffer[idx] == 'r') { print_menu(); } else { /* Echo it back */ UART_Send(TEST_UART, &buffer[idx], 1, BLOCKING); } idx++; } } // wait for current transmission complete - THR must be empty while (UART_CheckBusy(TEST_UART) == SET); // DeInitialize UART0 peripheral UART_DeInit(TEST_UART); /* Loop forever */ while(1); return 1; }
/*********************************************************************//** * @brief c_entry: Main UART program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; uint32_t idx, len; __IO FlagStatus exitflag; uint8_t buffer[10]; /* * Initialize UART1 pin connect */ PINSEL_ConfigPin(0, 15, 1);//UART1 - TXD PINSEL_ConfigPin(0, 16, 1);//UART1 - RXD PINSEL_ConfigPin(0, 17, 1);//UART1 - CTS PINSEL_ConfigPin(0, 22, 1);//UART1 - RTS /* Initialize UART Configuration parameter structure to default state: * Baudrate = 115200bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); // Initialize UART0 peripheral with given to corresponding parameter UART_Init(TEST_UART, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Initialize FIFO for UART0 peripheral UART_FIFOConfig(TEST_UART, &UARTFIFOConfigStruct); // Configure UART1 hardware flow control RTS/CTS UART_FullModemForcePinState(TEST_UART,UART1_MODEM_PIN_RTS,ACTIVE); // Enable UART Transmit UART_TxCmd(TEST_UART, ENABLE); // print welcome screen print_menu(); // Reset exit flag exitflag = RESET; /* Read some data from the buffer */ while (exitflag == RESET) { len = 0; while (len == 0) { len = UART_Receive(TEST_UART, buffer, sizeof(buffer), NONE_BLOCKING); } /* Got some data */ idx = 0; while (idx < len) { if (buffer[idx] == 27) { /* ESC key, set exit flag */ UART_Send(TEST_UART, menu2, sizeof(menu2), BLOCKING); exitflag = SET; } else if (buffer[idx] == 'r') { print_menu(); } else { /* Echo it back */ UART_Send(TEST_UART, &buffer[idx], 1, BLOCKING); } idx++; } } // wait for current transmission complete - THR must be empty while (UART_CheckBusy(TEST_UART) == SET); // DeInitialize UART0 peripheral UART_DeInit(TEST_UART); /* Loop forever */ while(1); }
int comm_init(int which_port,int baudrate,FUNC_CB_COMM_RCV cb_rcv) { // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; #if 0 uint32_t idx, len; __IO FlagStatus exitflag; uint8_t buffer[10]; #endif #if 0 #if (UART_TEST_NUM == 0) /* * Initialize UART0 pin connect * P0.2: U0_TXD * P0.3: U0_RXD */ PINSEL_ConfigPin(0,2,1); PINSEL_ConfigPin(0,3,1); #elif (UART_TEST_NUM == 1) /* * Initialize UART1 pin connect * P3.16: U1_TXD * P3.17: U1_RXD */ PINSEL_ConfigPin(3,16,3); PINSEL_ConfigPin(3,17,3); #elif (UART_TEST_NUM == 2) /* * Initialize UART2 pin connect * P0.10: U2_TXD * P0.11: U2_RXD */ PINSEL_ConfigPin(0,10,1); PINSEL_ConfigPin(0,11,1); #endif #else if (which_port == 0){ /* * Initialize UART0 pin connect * P0.2: U0_TXD * P0.3: U0_RXD */ PINSEL_ConfigPin(0,2,1); PINSEL_ConfigPin(0,3,1); }else if(which_port == 1){ /* * Initialize UART1 pin connect * P3.16: U1_TXD * P3.17: U1_RXD */ PINSEL_ConfigPin(0, 15, 1); PINSEL_ConfigPin(0, 16, 1); }else if(which_port == 2){ /* * Initialize UART2 pin connect * P0.10: U2_TXD * P0.11: U2_RXD */ PINSEL_ConfigPin(0,10,1); PINSEL_ConfigPin(0,11,1); } #endif /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct,115200); // Initialize UART0 peripheral with given to corresponding parameter UART_Init((LPC_UART_TypeDef *)uartDrvDataArray[which_port].reg_base, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Initialize FIFO for UART0 peripheral UART_FIFOConfig((LPC_UART_TypeDef *)uartDrvDataArray[which_port].reg_base, &UARTFIFOConfigStruct); // Enable UART Transmit UART_TxCmd((LPC_UART_TypeDef *)uartDrvDataArray[which_port].reg_base, ENABLE); /* Enable UART Rx interrupt */ UART_IntConfig((LPC_UART_TypeDef *)uartDrvDataArray[which_port].reg_base, UART_INTCFG_RBR, ENABLE); /* Enable UART line status interrupt */ UART_IntConfig((LPC_UART_TypeDef *)uartDrvDataArray[which_port].reg_base, UART_INTCFG_RLS, ENABLE); /* * Do not enable transmit interrupt here, since it is handled by * UART_Send() function, just to reset Tx Interrupt state for the * first time */ // TxIntStat = RESET; #if 0 // Reset ring buf head and tail idx __BUF_RESET(rb.rx_head); __BUF_RESET(rb.rx_tail); __BUF_RESET(rb.tx_head); __BUF_RESET(rb.tx_tail); #endif /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(uartDrvDataArray[which_port].irqn, ((0x01<<3)|0x01)); /* Enable Interrupt for UART0 channel */ NVIC_EnableIRQ(uartDrvDataArray[which_port].irqn); if(cb_rcv){ uartDrvDataArray[which_port].cb_rcv=cb_rcv; } return 0; }
/*********************************************************************//** * @brief c_entry: Main UART-RS485 program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; // Pin configuration PINSEL_CFG_Type PinCfg; // RS485 configuration UART1_RS485_CTRLCFG_Type rs485cfg; uint32_t idx, len; uint8_t buffer[10]; uint32_t tmp; // UART0 section ---------------------------------------------------- /* * Initialize UART0 pin connect */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 2; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 3; PINSEL_ConfigPin(&PinCfg); /* Initialize UART Configuration parameter structure to default state: * Baudrate = 115200 bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); UARTConfigStruct.Baud_rate = 115200; // Initialize UART0 peripheral with given to corresponding parameter UART_Init(LPC_UART0, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Initialize FIFO for UART0 peripheral UART_FIFOConfig(LPC_UART0, &UARTFIFOConfigStruct); // Enable UART Transmit UART_TxCmd(LPC_UART0, ENABLE); // print welcome screen print_menu(); // UART1 - RS485 section ------------------------------------------------- /* * Initialize UART1 pin connect */ PinCfg.Funcnum = 2; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; // TXD1 - P2.0 PinCfg.Pinnum = 0; PinCfg.Portnum = 2; PINSEL_ConfigPin(&PinCfg); // RXD1 - P2.1 PinCfg.Pinnum = 1; PINSEL_ConfigPin(&PinCfg); // DTR1 - P2.5 PinCfg.Pinnum = 5; PINSEL_ConfigPin(&PinCfg); /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600 bps * 8 data bit * 1 Stop bit * Parity: None * Note: Parity will be enabled later in UART_RS485Config() function. */ UART_ConfigStructInit(&UARTConfigStruct); // Initialize UART0 peripheral with given to corresponding parameter UART_Init((LPC_UART_TypeDef *)LPC_UART1, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Initialize FIFO for UART0 peripheral UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART1, &UARTFIFOConfigStruct); // Configure RS485 /* * - Auto Direction in Tx/Rx driving is enabled * - Direction control pin is set to DTR1 * - Direction control pole is set to "1" that means direction pin * will drive to high state before transmit data. * - Multidrop mode is enable * - Auto detect address is disabled * - Receive state is enable */ rs485cfg.AutoDirCtrl_State = ENABLE; rs485cfg.DirCtrlPin = UART1_RS485_DIRCTRL_DTR; rs485cfg.DirCtrlPol_Level = SET; rs485cfg.DelayValue = 50; rs485cfg.NormalMultiDropMode_State = ENABLE; #if AUTO_SLVADDR_DETECT rs485cfg.AutoAddrDetect_State = ENABLE; rs485cfg.MatchAddrValue = SLAVE_ADDR; #else rs485cfg.AutoAddrDetect_State = DISABLE; #endif #if RECEIVER_ALWAYS_EN rs485cfg.Rx_State = ENABLE; #else rs485cfg.Rx_State = DISABLE; #endif UART_RS485Config(LPC_UART1, &rs485cfg); /* Enable UART Rx interrupt */ UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RBR, ENABLE); /* Enable UART line status interrupt */ UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RLS, ENABLE); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(UART1_IRQn, ((0x01<<3)|0x01)); /* Enable Interrupt for UART0 channel */ NVIC_EnableIRQ(UART1_IRQn); // Enable UART Transmit UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE); // for testing... while (1){ len = 0; while (len == 0) { len = UARTReceive((LPC_UART_TypeDef *)LPC_UART1, buffer, sizeof(buffer)); } /* Got some data */ idx = 0; while (idx < len) { if (buffer[idx] == 13){ for (tmp = 0; tmp < 1000000; tmp++); UART_RS485SendData(LPC_UART1, ack_msg, sizeof(ack_msg)); UART_Send(LPC_UART0, nextline, sizeof(nextline), BLOCKING); UART_RS485SendData(LPC_UART1, &terminator, 1); } else { /* Echo it back */ UART_Send(LPC_UART0, &buffer[idx], 1, BLOCKING); } idx++; } } return 1; }
// // // //lecture - UART - specific uart open method invoked from vFreeRTOS_lpc17xx_PopulateFunctionPointers() // in FreeRTOS_lpc17xx_DriverInterface.c - refer to that file, if needed !!! // // portBASE_TYPE FreeRTOS_UART_open( Peripheral_Control_t * const pxPeripheralControl ) { // // //lecture - UART - refer to respective header for typedef of objects - for instance LPC17xx.h // refer to that LPC17xx.h file if needed // // PINSEL_CFG_Type xPinConfig; UART_CFG_Type xUARTConfig; UART_FIFO_CFG_Type xUARTFIFOConfig; LPC_UART_TypeDef * const pxUART = ( LPC_UART_TypeDef * const ) diGET_PERIPHERAL_BASE_ADDRESS( pxPeripheralControl ); portBASE_TYPE xReturn; const uint8_t cPeripheralNumber = diGET_PERIPHERAL_NUMBER( pxPeripheralControl ); // //lecture - UART - we are initializing the specific driver methods for UART // //lecture - UART - these methods are described in this file !!! // /* Sanity check the peripheral number. */ if( cPeripheralNumber < boardNUM_UARTS ) { pxPeripheralControl->read = FreeRTOS_UART_read; pxPeripheralControl->write = FreeRTOS_UART_write; pxPeripheralControl->ioctl = FreeRTOS_UART_ioctl; /* Setup the pins for the UART being used. */ taskENTER_CRITICAL(); { // // //lecture - refer to LPCXpresso17xx-base-board.h // used to configure the pin selection to enable UART3 pins // // boardCONFIGURE_UART_PINS( cPeripheralNumber, xPinConfig ); // // //lecture - refer to cmsis firmware lib. file lpc17xx_uart.c // //lecture - refer to LPCXpresso17xx-base-board.h // //lecture - refer to lpc17xx user-manual for hw specific details - chapter 14 and further, // relevant sections ??? // // /* Set up the default UART configuration. */ xUARTConfig.Baud_rate = boardDEFAULT_UART_BAUD; xUARTConfig.Databits = UART_DATABIT_8; xUARTConfig.Parity = UART_PARITY_NONE; xUARTConfig.Stopbits = UART_STOPBIT_1; UART_Init( pxUART, &xUARTConfig ); /* Enable the FIFO. */ xUARTFIFOConfig.FIFO_ResetRxBuf = ENABLE; xUARTFIFOConfig.FIFO_ResetTxBuf = ENABLE; xUARTFIFOConfig.FIFO_DMAMode = DISABLE; xUARTFIFOConfig.FIFO_Level = UART_FIFO_TRGLEV2; UART_FIFOConfig( pxUART, &xUARTFIFOConfig ); /* Enable Tx. */ UART_TxCmd( pxUART, ENABLE ); } taskEXIT_CRITICAL(); xReturn = pdPASS; } else { xReturn = pdFAIL; } return xReturn; }
/*********************************************************************//** * @brief c_entry: Main UART program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; // Pin configuration for UART PINSEL_CFG_Type PinCfg; uint32_t idx,len; __IO FlagStatus exitflag; uint8_t buffer,temp; #if (UART_PORT == 0) /* * Initialize UART0 pin connect */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 2; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg);//P0.2 TXD0 PinCfg.Pinnum = 3; PINSEL_ConfigPin(&PinCfg);//P0.3 RXD0 #endif #if (UART_PORT == 1) /* * Initialize UART1 pin connect */ PinCfg.Funcnum = 2; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 0; PinCfg.Portnum = 2; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 1; PINSEL_ConfigPin(&PinCfg); #endif /* * Initialize UART3 pin connect */ PinCfg.Funcnum = 3; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 25; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg);//P0.25 TXD3 /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); // Initialize UART0 & UART3 peripheral with given to corresponding parameter UART_Init(TEST_UART, &UARTConfigStruct); UART_Init(TEST_IRDA, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Initialize FIFO for UART0 & UART3 peripheral UART_FIFOConfig(TEST_UART, &UARTFIFOConfigStruct); UART_FIFOConfig(TEST_IRDA, &UARTFIFOConfigStruct); //Configure and enable IrDA mode on UART UART_IrDACmd(TEST_IRDA,ENABLE); // Enable UART Transmit UART_TxCmd(TEST_UART, ENABLE); UART_TxCmd(TEST_IRDA, ENABLE); // print welcome screen print_menu(); // Reset exit flag exitflag = RESET; idx=0;buffer=0; /* Read some data from the buffer */ while (exitflag == RESET) { if(idx==0) { UART_Send(TEST_UART, menu5, sizeof(menu5), BLOCKING); } len=0; while(len==0) { len = UART_Receive(TEST_UART, &temp, 1, NONE_BLOCKING); } if(temp==27) { UART_Send(TEST_UART, menu4, sizeof(menu4), BLOCKING); exitflag=SET; } else if(temp=='r') { idx=0;buffer=0; print_menu(); UART_Send(TEST_IRDA, &buffer, 1, BLOCKING); } else { idx++; switch(temp) { case '0': buffer=(buffer<<4)|0x00;break; case '1': buffer=(buffer<<4)|0x01;break; case '2': buffer=(buffer<<4)|0x02;break; case '3': buffer=(buffer<<4)|0x03;break; case '4': buffer=(buffer<<4)|0x04;break; case '5': buffer=(buffer<<4)|0x05;break; case '6': buffer=(buffer<<4)|0x06;break; case '7': buffer=(buffer<<4)|0x07;break; case '8': buffer=(buffer<<4)|0x08;break; case '9': buffer=(buffer<<4)|0x09;break; case 'a': buffer=(buffer<<4)|0x0A;break; case 'A': buffer=(buffer<<4)|0x0A;break; case 'b': buffer=(buffer<<4)|0x0B;break; case 'B': buffer=(buffer<<4)|0x0B;break; case 'c': buffer=(buffer<<4)|0x0C;break; case 'C': buffer=(buffer<<4)|0x0C;break; case 'd': buffer=(buffer<<4)|0x0D;break; case 'D': buffer=(buffer<<4)|0x0D;break; case 'e': buffer=(buffer<<4)|0x0E;break; case 'E': buffer=(buffer<<4)|0x0E;break; case 'f': buffer=(buffer<<4)|0x0F;break; case 'F': buffer=(buffer<<4)|0x0F;break; default: idx=0;buffer=0;break; } if(idx==2) { temp=buffer>>4; if(temp <= 9)temp=temp+ 0x30; else temp=temp+0x37; UART_Send(TEST_UART, &temp, 1, BLOCKING); temp=(buffer&0x0F); if(temp <= 9)temp=temp+ 0x30; else temp=temp+0x37; UART_Send(TEST_UART, &temp, 1, BLOCKING); UART_Send(TEST_IRDA, &buffer, 1, BLOCKING); idx=0;buffer=0; } } }
/*********************************************************************//** * @brief Main UART testing example sub-routine * Print welcome screen first, then press any key to have it * read in from the terminal and returned back to the terminal. * - Press ESC to exit * - Press 'r' to print welcome screen menu again **********************************************************************/ int c_entry(void) { // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; // Pin configuration for UART0 PINSEL_CFG_Type PinCfg; uint32_t idx, len; __IO FlagStatus exitflag; uint8_t buffer[10]; // DeInit NVIC and SCBNVIC NVIC_DeInit(); NVIC_SCBDeInit(); /* Configure the NVIC Preemption Priority Bits: * two (2) bits of preemption priority, six (6) bits of sub-priority. * Since the Number of Bits used for Priority Levels is five (5), so the * actual bit number of sub-priority is three (3) */ NVIC_SetPriorityGrouping(0x05); // Set Vector table offset value #if (__RAM_MODE__==1) NVIC_SetVTOR(0x10000000); #else NVIC_SetVTOR(0x00000000); #endif /* * Initialize UART1 pin connect */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 0; for (idx = 15; idx <= 22; idx++){ PinCfg.Pinnum = idx; PINSEL_ConfigPin(&PinCfg); } /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); // Initialize UART1 peripheral with given to corresponding parameter UART_Init((LPC_UART_TypeDef *)LPC_UART1, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Initialize FIFO for UART1 peripheral UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART1, &UARTFIFOConfigStruct); #if (AUTO_RTS_CTS_USE==0) /* * Determine current state of CTS pin to enable Tx * activity */ if (UART_FullModemGetStatus(LPC_UART1) & UART1_MODEM_STAT_CTS) { // Enable UART Transmit UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE); } #else // Enable UART Transmit UART_TxCmd((UART_TypeDef *)UART1, ENABLE); #endif // Reset ring buf head and tail idx __BUF_RESET(rb.rx_head); __BUF_RESET(rb.rx_tail); __BUF_RESET(rb.tx_head); __BUF_RESET(rb.tx_tail); #if AUTO_RTS_CTS_USE UART_FullModemConfigMode(UART1, UART1_MODEM_MODE_AUTO_RTS, ENABLE); UART_FullModemConfigMode(UART1, UART1_MODEM_MODE_AUTO_CTS, ENABLE); #else // Enable Modem status interrupt UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART1_INTCFG_MS, ENABLE); // Enable CTS1 signal transition interrupt UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART1_INTCFG_CTS, ENABLE); // Modem Status interrupt call back UART_SetupCbs((LPC_UART_TypeDef *)LPC_UART1, 4, (void *)UART1_ModemCallBack); // Force RTS pin state to ACTIVE UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, ACTIVE); //RESET RTS State flag RTS_State = ACTIVE; #endif // Setup callback --------------- // Receive callback UART_SetupCbs((LPC_UART_TypeDef *)LPC_UART1, 0, (void *)UART1_IntReceive); // Transmit callback UART_SetupCbs((LPC_UART_TypeDef *)LPC_UART1, 1, (void *)UART1_IntTransmit); // Line Status Error callback UART_SetupCbs((LPC_UART_TypeDef *)LPC_UART1, 3, (void *)UART1_IntErr); /* Enable UART Rx interrupt */ UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RBR, ENABLE); /* Enable UART line status interrupt */ UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RLS, ENABLE); /* * Do not enable transmit interrupt here, since it is handled by * UART_Send() function, just to reset Tx Interrupt state for the * first time */ TxIntStat = RESET; /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(UART1_IRQn, ((0x01<<3)|0x01)); /* Enable Interrupt for UART1 channel */ NVIC_EnableIRQ(UART1_IRQn); // print welcome screen print_menu(); // reset exit flag exitflag = RESET; /* Read some data from the buffer */ while (exitflag == RESET) { len = 0; while (len == 0) { len = UARTReceive((LPC_UART_TypeDef *)LPC_UART1, buffer, sizeof(buffer)); } /* Got some data */ idx = 0; while (idx < len) { if (buffer[idx] == 27) { /* ESC key, set exit flag */ UARTSend((LPC_UART_TypeDef *)LPC_UART1, menu3, sizeof(menu3)); exitflag = SET; } else if (buffer[idx] == 'r') { print_menu(); } else { /* Echo it back */ UARTSend((LPC_UART_TypeDef *)LPC_UART1, &buffer[idx], 1); } idx++; } } // wait for current transmission complete - THR must be empty while (UART_CheckBusy((LPC_UART_TypeDef *)LPC_UART1) == SET); // DeInitialize UART1 peripheral UART_DeInit((LPC_UART_TypeDef *)LPC_UART1); /* Loop forever */ while(1); return 1; }
/*********************************************************************//** * @brief c_entry: Main UART program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; // Pin configuration for UART0 PINSEL_CFG_Type PinCfg; uint32_t idx, len; __IO FlagStatus exitflag; uint8_t buffer[10]; /* * Initialize UART0 pin connect */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 2; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 3; PINSEL_ConfigPin(&PinCfg); /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); // Initialize UART0 peripheral with given to corresponding parameter UART_Init((LPC_UART_TypeDef *)LPC_UART0, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Initialize FIFO for UART0 peripheral UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART0, &UARTFIFOConfigStruct); // Enable UART Transmit UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0, ENABLE); /* Enable UART Rx interrupt */ UART_IntConfig((LPC_UART_TypeDef *)LPC_UART0, UART_INTCFG_RBR, ENABLE); /* Enable UART line status interrupt */ UART_IntConfig((LPC_UART_TypeDef *)LPC_UART0, UART_INTCFG_RLS, ENABLE); /* * Do not enable transmit interrupt here, since it is handled by * UART_Send() function, just to reset Tx Interrupt state for the * first time */ TxIntStat = RESET; // Reset ring buf head and tail idx __BUF_RESET(rb.rx_head); __BUF_RESET(rb.rx_tail); __BUF_RESET(rb.tx_head); __BUF_RESET(rb.tx_tail); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(UART0_IRQn, ((0x01<<3)|0x01)); /* Enable Interrupt for UART0 channel */ NVIC_EnableIRQ(UART0_IRQn); // print welcome screen print_menu(); // reset exit flag exitflag = RESET; /* Read some data from the buffer */ while (exitflag == RESET) { len = 0; while (len == 0) { len = UARTReceive((LPC_UART_TypeDef *)LPC_UART0, buffer, sizeof(buffer)); } /* Got some data */ idx = 0; while (idx < len) { if (buffer[idx] == 27) { /* ESC key, set exit flag */ UARTSend((LPC_UART_TypeDef *)LPC_UART0, menu3, sizeof(menu3)); exitflag = SET; } else if (buffer[idx] == 'r') { print_menu(); } else { /* Echo it back */ UARTSend((LPC_UART_TypeDef *)LPC_UART0, &buffer[idx], 1); } idx++; } } // wait for current transmission complete - THR must be empty while (UART_CheckBusy((LPC_UART_TypeDef *)LPC_UART0)); // DeInitialize UART0 peripheral UART_DeInit((LPC_UART_TypeDef *)LPC_UART0); /* Loop forever */ while(1); return 1; }
/*********************************************************************//** * @brief c_entry: Main UART-FULLMODEM program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; // Pin configuration for UART1 PINSEL_CFG_Type PinCfg; uint32_t idx, len; __IO FlagStatus exitflag; uint8_t buffer[10]; /* * Initialize UART1 pin connect * If using MCB1700 eval board, assign pin P2.0 - P2.7 * If using IAR 1768 KS board, assign pin P0.7 - P0.15 */ #ifdef MCB_LPC_1768 PinCfg.Funcnum = 2; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 2; for (idx = 0; idx <= 7; idx++){ PinCfg.Pinnum = idx; PINSEL_ConfigPin(&PinCfg); } #elif defined(IAR_LPC_1768) PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 0; for (idx = 15; idx <= 22; idx++){ PinCfg.Pinnum = idx; PINSEL_ConfigPin(&PinCfg); } #endif /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); // Initialize UART1 peripheral with given to corresponding parameter UART_Init((LPC_UART_TypeDef *)LPC_UART1, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Initialize FIFO for UART1 peripheral UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART1, &UARTFIFOConfigStruct); #if (AUTO_RTS_CTS_USE==0) /* * Determine current state of CTS pin to enable Tx * activity */ if (UART_FullModemGetStatus(LPC_UART1) & UART1_MODEM_STAT_CTS) { // Enable UART Transmit UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE); } #else // Enable UART Transmit UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE); #endif // Reset ring buf head and tail idx __BUF_RESET(rb.rx_head); __BUF_RESET(rb.rx_tail); __BUF_RESET(rb.tx_head); __BUF_RESET(rb.tx_tail); #if AUTO_RTS_CTS_USE UART_FullModemConfigMode(LPC_UART1, UART1_MODEM_MODE_AUTO_RTS, ENABLE); UART_FullModemConfigMode(LPC_UART1, UART1_MODEM_MODE_AUTO_CTS, ENABLE); #else // Enable Modem status interrupt UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART1_INTCFG_MS, ENABLE); // Enable CTS1 signal transition interrupt UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART1_INTCFG_CTS, ENABLE); // Force RTS pin state to ACTIVE UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, ACTIVE); //RESET RTS State flag RTS_State = ACTIVE; #endif /* Enable UART Rx interrupt */ UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RBR, ENABLE); /* Enable UART line status interrupt */ UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RLS, ENABLE); /* * Do not enable transmit interrupt here, since it is handled by * UART_Send() function, just to reset Tx Interrupt state for the * first time */ TxIntStat = RESET; /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(UART1_IRQn, ((0x01<<3)|0x01)); /* Enable Interrupt for UART1 channel */ NVIC_EnableIRQ(UART1_IRQn); // print welcome screen print_menu(); // reset exit flag exitflag = RESET; /* Read some data from the buffer */ while (exitflag == RESET) { len = 0; while (len == 0) { len = UARTReceive((LPC_UART_TypeDef *)LPC_UART1, buffer, sizeof(buffer)); } /* Got some data */ idx = 0; while (idx < len) { if (buffer[idx] == 27) { /* ESC key, set exit flag */ UARTSend((LPC_UART_TypeDef *)LPC_UART1, menu3, sizeof(menu3)); exitflag = SET; } else if (buffer[idx] == 'r') { print_menu(); } else { /* Echo it back */ UARTSend((LPC_UART_TypeDef *)LPC_UART1, &buffer[idx], 1); } idx++; } } // wait for current transmission complete - THR must be empty while (UART_CheckBusy((LPC_UART_TypeDef *)LPC_UART1) == SET); // DeInitialize UART1 peripheral UART_DeInit((LPC_UART_TypeDef *)LPC_UART1); /* Loop forever */ while(1); return 1; }
/************************************************************* Function: void UartInit(uint8_t num,uint32_t baudrate,uint8_t parity) Description: 串口初始化函数用于初始化RS232及RS485 将UART配置为中断接收,DMA发送,RS485自动切换方向 Calls: Called By: main() Input: num 串口号0、1、2 baudrate 波特率 parity 校验方式 Output: 无 Return: 无 Others: 无 *************************************************************/ void UartInit ( uint8_t num, uint32_t baudrate, uint8_t parity ) { // uint32_t idx; // RS485 configuration UART1_RS485_CTRLCFG_Type rs485cfg; // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; GPDMA_Channel_CFG_Type GPDMACfg; UART_ConfigStructInit ( &UARTConfigStruct ); UARTConfigStruct.Baud_rate = baudrate; UARTConfigStruct.Parity = parity; UART_FIFOConfigStructInit ( &UARTFIFOConfigStruct ); // Enable DMA mode in UART UARTFIFOConfigStruct.FIFO_DMAMode = ENABLE; // Destination memory - don't care GPDMACfgTx.DstMemAddr = 0; // Transfer width - don't care GPDMACfgTx.TransferWidth = 0; // Transfer type GPDMACfgTx.TransferType = GPDMA_TRANSFERTYPE_M2P; // Source connection - don't care GPDMACfgTx.SrcConn = 0; // Linker List Item - unused GPDMACfgTx.DMALLI = 0; rs485cfg.AutoDirCtrl_State = ENABLE; rs485cfg.DirCtrlPol_Level = SET; rs485cfg.DelayValue = 50; rs485cfg.NormalMultiDropMode_State = ENABLE; rs485cfg.AutoAddrDetect_State = DISABLE; rs485cfg.Rx_State = ENABLE; if ( num == 0 ) { //PINSEL_ConfigPin(0,2,1); //UART0 //PINSEL_ConfigPin(0,3,1); //UART0 PINSEL_ConfigPin ( 0, 25, 0x03 ); PINSEL_ConfigPin ( 0, 26, 0xb3 ); // Initalize UART0 peripheral with given to corresponding parameter UART_Init ( RS232_UART, &UARTConfigStruct ); // Initialize FIFO for UART0 peripheral UART_FIFOConfig ( RS232_UART, &UARTFIFOConfigStruct ); // Enable UART Transmit UART_TxCmd ( RS232_UART, ENABLE ); // channel 0 GPDMACfgTx.ChannelNum = 0; // Source memory GPDMACfgTx.SrcMemAddr = ( uint32_t ) &RS232Tx.Buff; // Transfer size GPDMACfgTx.TransferSize = sizeof ( RS232Tx.Buff ); // Destination connection GPDMACfgTx.DstConn = RS232_TX_PIN; RS232Tx.Flag = 0; RS232_Err = 0; UART_IntConfig ( RS232_UART, UART_INTCFG_RBR, ENABLE ); UART_IntConfig ( RS232_UART, UART_INTCFG_RLS, ENABLE ); NVIC_SetPriority ( RS232_IRQN, ( ( 0x01 << 3 ) | 0x01 ) ); NVIC_EnableIRQ ( RS232_IRQN ); RS232Rx.Flag = 0 ; RS232Rx.Len = 0 ; RS232Rx.Idx = 0 ; } if ( num == 1 ) { PINSEL_ConfigPin ( 2, 0, 2 ); PINSEL_ConfigPin ( 2, 1, 2 ); PINSEL_ConfigPin ( 2, 5, 2 ); //U1_DTR rs485cfg.DirCtrlPin = UART1_RS485_DIRCTRL_DTR; UART_Init ( ( LPC_UART_TypeDef * ) LPC_UART1, &UARTConfigStruct ); UART_FIFOConfig ( ( LPC_UART_TypeDef * ) LPC_UART1, &UARTFIFOConfigStruct ); UART_RS485Config ( ( LPC_UART_TypeDef * ) LPC_UART1, &rs485cfg ); // Enable UART Transmit UART_TxCmd ( ( LPC_UART_TypeDef * ) LPC_UART1, ENABLE ); GPDMACfgTx.ChannelNum = 1; // Source memory GPDMACfgTx.SrcMemAddr = ( uint32_t ) &RS485Tx1.Buff; // Transfer size GPDMACfgTx.TransferSize = sizeof ( RS485Tx1.Buff ); // Destination connection GPDMACfgTx.DstConn = GPDMA_CONN_UART1_Tx; /* Reset terminal counter */ RS485Tx1.Flag = 0; /* Reset Error counter */ RS4851_Err = 0; /* Enable UART Rx interrupt */ UART_IntConfig ( ( LPC_UART_TypeDef * ) LPC_UART1, UART_INTCFG_RBR, ENABLE ); /* Enable UART line status interrupt */ UART_IntConfig ( ( LPC_UART_TypeDef * ) LPC_UART1, UART_INTCFG_RLS, ENABLE ); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority ( UART1_IRQn, ( ( 0x01 << 3 ) | 0x01 ) ); /* Enable Interrupt for UART0 channel */ NVIC_EnableIRQ ( UART1_IRQn ); } if ( num == 2 ) { PINSEL_ConfigPin ( 2, 8, 2 ); PINSEL_ConfigPin ( 2, 9, 2 ); PINSEL_ConfigPin ( 2, 6, 4 ); //U2_OE UART_Init ( LPC_UART2, &UARTConfigStruct ); UART_FIFOConfig ( LPC_UART2, &UARTFIFOConfigStruct ); UART_RS485Config ( ( LPC_UART_TypeDef * ) LPC_UART2, &rs485cfg ); // Enable UART Transmit UART_TxCmd ( LPC_UART2, ENABLE ); GPDMACfgTx.ChannelNum = 2; // Source memory GPDMACfgTx.SrcMemAddr = ( uint32_t ) &RS485Tx2.Buff; // Transfer size GPDMACfgTx.TransferSize = sizeof ( RS485Tx2.Buff ); // Destination connection GPDMACfgTx.DstConn = GPDMA_CONN_UART2_Tx; /* Reset terminal counter */ RS485Tx2.Flag = 0; /* Reset Error counter */ RS4852_Err = 0; /* Enable UART Rx interrupt */ UART_IntConfig ( LPC_UART2, UART_INTCFG_RBR, ENABLE ); /* Enable UART line status interrupt */ UART_IntConfig ( LPC_UART2, UART_INTCFG_RLS, ENABLE ); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority ( UART2_IRQn, ( ( 0x01 << 3 ) | 0x01 ) ); /* Enable Interrupt for UART0 channel */ NVIC_EnableIRQ ( UART2_IRQn ); } /* Initialize GPDMA controller */ GPDMA_Init(); /* Setting GPDMA interrupt */ // Disable interrupt for DMA NVIC_DisableIRQ ( DMA_IRQn ); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority ( DMA_IRQn, ( ( 0x01 << 3 ) | 0x01 ) ); // Setup channel with given parameter GPDMA_Setup ( &GPDMACfgTx ); // Enable interrupt for DMA NVIC_EnableIRQ ( DMA_IRQn ); // Enable GPDMA channel 0 //GPDMA_ChannelCmd(0, ENABLE); CRC_Init ( CRC_POLY_CRC16 ); }
/*********************************************************************//** * @brief c_entry: Main UART program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; // Pin configuration for UART0 PINSEL_CFG_Type PinCfg; // Auto baudrate configuration structure UART_AB_CFG_Type ABConfig; uint32_t idx, len; __IO FlagStatus exitflag; uint8_t buffer[10]; /* * Initialize UART0 pin connect */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 2; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 3; PINSEL_ConfigPin(&PinCfg); /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); /* Initialize UART0 peripheral with given to corresponding parameter * in this case, don't care the baudrate value UART initialized * since this will be determine when running auto baudrate */ UART_Init(LPC_UART0, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Initialize FIFO for UART0 peripheral UART_FIFOConfig(LPC_UART0, &UARTFIFOConfigStruct); // Enable UART Transmit UART_TxCmd(LPC_UART0, ENABLE); /* Enable UART End of Auto baudrate interrupt */ UART_IntConfig(LPC_UART0, UART_INTCFG_ABEO, ENABLE); /* Enable UART Auto baudrate timeout interrupt */ UART_IntConfig(LPC_UART0, UART_INTCFG_ABTO, ENABLE); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(UART0_IRQn, ((0x01<<3)|0x01)); /* Enable Interrupt for UART0 channel */ NVIC_EnableIRQ(UART0_IRQn); /* ---------------------- Auto baud rate section ----------------------- */ // Reset Synchronous flag for auto-baudrate mode Synchronous = RESET; // Configure Auto baud rate mode ABConfig.ABMode = UART_AUTOBAUD_MODE0; ABConfig.AutoRestart = ENABLE; // Start auto baudrate mode UART_ABCmd(LPC_UART0, &ABConfig, ENABLE); print_menu(); /* Loop until auto baudrate mode complete */ while (Synchronous == RESET); // Print status of auto baudrate UART_Send(LPC_UART0, syncmenu, sizeof(syncmenu), BLOCKING); /* ---------------------- End of Auto baud rate section ----------------------- */ // print welcome screen print_menu(); // reset exit flag exitflag = RESET; /* Read some data from the buffer */ while (exitflag == RESET) { len = 0; while (len == 0) { len = UART_Receive(LPC_UART0, buffer, sizeof(buffer), NONE_BLOCKING); } /* Got some data */ idx = 0; while (idx < len) { if (buffer[idx] == 27) { /* ESC key, set exit flag */ UART_Send(LPC_UART0, menu3, sizeof(menu3), BLOCKING); exitflag = SET; } else if (buffer[idx] == 'r') { print_menu(); } else { /* Echo it back */ UART_Send(LPC_UART0, &buffer[idx], 1, BLOCKING); } idx++; } } // wait for current transmission complete - THR must be empty while (UART_CheckBusy(LPC_UART0) == SET); // DeInitialize UART0 peripheral UART_DeInit(LPC_UART0); /* Loop forever */ while(1); return 1; }
void initSerial() { if (DBG_LEVEL >= 2) _DBG_("Starting Serial Init"); // UART Configuration structure variable UART_CFG_Type uartConfig; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type uartFifoConfig; // Pin configuration for UART PINSEL_CFG_Type PinCfg; if (DBG_LEVEL >= 2) _DBG_("Structs assigned"); // Initialize UART pin connect PinCfg.Funcnum = PINSEL_FUNC_2; PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL; PinCfg.Pinmode = PINSEL_PINMODE_PULLUP; PinCfg.Portnum = PINSEL_PORT_0; PinCfg.Pinnum = 0; //Out pin 9 PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 1; //Out pin 10 PINSEL_ConfigPin(&PinCfg); if (DBG_LEVEL >= 2) _DBG_("Pins Configured"); /* Initialize UART Configuration parameter structure: * - Baudrate = 115200bps * - 8 data bit * - 1 Stop bit * - No parity */ UART_ConfigStructInit(&uartConfig); uartConfig.Parity = UART_PARITY_NONE; uartConfig.Databits = UART_DATABIT_8; uartConfig.Stopbits = UART_STOPBIT_1; uartConfig.Baud_rate = 115200; if (DBG_LEVEL >= 2) _DBG_("UART Configured"); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&uartFifoConfig); if (DBG_LEVEL >= 2) _DBG_("FIFO Configured"); // Initialize UART3 UART_Init(LPC_UART3, &uartConfig); if (DBG_LEVEL >= 2) _DBG_("UART Initialised"); // Initialize FIFO for UART3 UART_FIFOConfig(LPC_UART3, &uartFifoConfig); if (DBG_LEVEL >= 2) _DBG_("FIFO Initialised"); // Enable UART Transmit UART_TxCmd(LPC_UART3, ENABLE); if (DBG_LEVEL >= 2) _DBG_("UART Started"); }
static void UART_FullModemConfig(void) { PINSEL_CFG_Type PinSelectConfigStruct; UART_CFG_Type UARTConfigStruct; UART_FIFO_CFG_Type UARTFIFOConfigStruct; //GSM_RXD_PIN PinSelectConfigStruct.Funcnum = PINSEL_FUNC_2; PinSelectConfigStruct.OpenDrain = PINSEL_PINMODE_NORMAL; PinSelectConfigStruct.Pinmode = PINSEL_PINMODE_PULLUP; PinSelectConfigStruct.Pinnum = gsmRxdPin; PinSelectConfigStruct.Portnum = GPIO2; PINSEL_ConfigPin(&PinSelectConfigStruct); //GSM_TXD_PIN PinSelectConfigStruct.Pinnum = gsmTxdPin; PINSEL_ConfigPin(&PinSelectConfigStruct); //GSM_CTS_PIN PinSelectConfigStruct.Pinnum = gsmCtsPin; PINSEL_ConfigPin(&PinSelectConfigStruct); //GSM_DCD_PIN PinSelectConfigStruct.Pinnum = gsmDcdPin; PINSEL_ConfigPin(&PinSelectConfigStruct); //GSM_DTR_PIN PinSelectConfigStruct.Pinnum = gsmDtrPin; PINSEL_ConfigPin(&PinSelectConfigStruct); //GSM_RI_PIN PinSelectConfigStruct.Pinnum = gsmRiPin; PINSEL_ConfigPin(&PinSelectConfigStruct); //GSM_RTS_PIN PinSelectConfigStruct.Pinnum = gsmRtsPin; PINSEL_ConfigPin(&PinSelectConfigStruct); /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); /* Set Baudrate */ UARTConfigStruct.Baud_rate = 9600; /* Initialize UART0 peripheral with given to corresponding parameter */ UART_Init(UARTx, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); /* Initialize FIFO for UART1 peripheral */ UART_FIFOConfig(UARTx, &UARTFIFOConfigStruct); UART_FullModemConfigMode(LPC_UART1, UART1_MODEM_MODE_AUTO_CTS, ENABLE); UART_FullModemConfigMode(LPC_UART1, UART1_MODEM_MODE_AUTO_RTS, ENABLE); //UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, ACTIVE); /* Enable UART Transmit */ UART_TxCmd( UARTx, ENABLE); UART_IntConfig(UARTx, UART_INTCFG_RBR , ENABLE); //UART_IntConfig(UARTx, UART1_INTCFG_MS, ENABLE); UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_DTR, ACTIVE); }
/*********************************************************************//** * @brief c_entry: Main UART program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { uint8_t *rx_char; uint32_t idx; // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; GPDMA_Channel_CFG_Type GPDMACfg; // Pin configuration for UART0 PINSEL_CFG_Type PinCfg; /* * Initialize UART0 pin connect */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 2; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 3; PINSEL_ConfigPin(&PinCfg); /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); // Initialize UART0 peripheral with given to corresponding parameter UART_Init(LPC_UART0, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Enable DMA mode in UART UARTFIFOConfigStruct.FIFO_DMAMode = ENABLE; // Initialize FIFO for UART0 peripheral UART_FIFOConfig(LPC_UART0, &UARTFIFOConfigStruct); // Enable UART Transmit UART_TxCmd(LPC_UART0, ENABLE); /* GPDMA Interrupt configuration section ------------------------------------------------- */ /* Initialize GPDMA controller */ GPDMA_Init(); /* Setting GPDMA interrupt */ // Disable interrupt for DMA NVIC_DisableIRQ (DMA_IRQn); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(DMA_IRQn, ((0x01<<3)|0x01)); // Setup GPDMA channel -------------------------------- // channel 0 GPDMACfg.ChannelNum = 0; // Source memory GPDMACfg.SrcMemAddr = (uint32_t) &menu1; // Destination memory - don't care GPDMACfg.DstMemAddr = 0; // Transfer size GPDMACfg.TransferSize = sizeof(menu1); // Transfer width - don't care GPDMACfg.TransferWidth = 0; // Transfer type GPDMACfg.TransferType = GPDMA_TRANSFERTYPE_M2P; // Source connection - don't care GPDMACfg.SrcConn = 0; // Destination connection GPDMACfg.DstConn = GPDMA_CONN_UART0_Tx; // Linker List Item - unused GPDMACfg.DMALLI = 0; // Setup channel with given parameter GPDMA_Setup(&GPDMACfg); // Setup GPDMA channel -------------------------------- // channel 1 GPDMACfg.ChannelNum = 1; // Source memory - don't care GPDMACfg.SrcMemAddr = 0; // Destination memory GPDMACfg.DstMemAddr = (uint32_t) &rx_buf; // Transfer size GPDMACfg.TransferSize = sizeof(rx_buf); // Transfer width - don't care GPDMACfg.TransferWidth = 0; // Transfer type GPDMACfg.TransferType = GPDMA_TRANSFERTYPE_P2M; // Source connection GPDMACfg.SrcConn = GPDMA_CONN_UART0_Rx; // Destination connection - don't care GPDMACfg.DstConn = 0; // Linker List Item - unused GPDMACfg.DMALLI = 0; GPDMA_Setup(&GPDMACfg); /* Reset terminal counter */ Channel0_TC = 0; /* Reset Error counter */ Channel0_Err = 0; // Enable interrupt for DMA NVIC_EnableIRQ (DMA_IRQn); // Enable GPDMA channel 0 GPDMA_ChannelCmd(0, ENABLE); // Make sure GPDMA channel 1 is disabled GPDMA_ChannelCmd(1, DISABLE); /* Wait for GPDMA on UART0 Tx processing complete */ while ((Channel0_TC == 0) && (Channel0_Err == 0)); // Main loop - echos back to the terminal while (1) { /* Reset terminal counter */ Channel1_TC = 0; /* Reset Error counter */ Channel1_Err = 0; // Setup channel with given parameter GPDMA_Setup(&GPDMACfg); // Enable GPDMA channel 1 GPDMA_ChannelCmd(1, ENABLE); // Clear Rx buffer using DMA for (idx = 0; idx < RX_BUF_SIZE; idx++){ rx_buf[idx] = 0; } // now, start receive character using GPDMA rx_char = (uint8_t *) &rx_buf; while ((Channel1_TC == 0) && (Channel1_Err == 0)){ // Check whether if there's any character received, then print it back if (*rx_char != 0) { UART_Send(LPC_UART0, rx_char, 1, BLOCKING); rx_char++; } } } // DeInitialize UART0 peripheral UART_DeInit(LPC_UART0); /* Loop forever */ while(1); return 1; }