/*********************************************************************//** * @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 PINSEL_CFG_Type PinCfg; // RS485 configuration UART1_RS485_CTRLCFG_Type rs485cfg; // Temp. data uint32_t idx, len; uint8_t buffer[10]; int32_t exit_flag, addr_toggle; // 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 // 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 disable * - 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 = DISABLE; rs485cfg.AutoAddrDetect_State = DISABLE; rs485cfg.MatchAddrValue = 0; rs485cfg.Rx_State = ENABLE; UART_RS485Config(LPC_UART1, &rs485cfg); // Setup callback --------------- // Receive callback UART_SetupCbs((LPC_UART_TypeDef *)LPC_UART1, 0, (void *)UART_IntReceive); // Line Status Error callback UART_SetupCbs((LPC_UART_TypeDef *)LPC_UART1, 3, (void *)UART_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); /* 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); addr_toggle = 1; // for testing... while (1){ // Send slave addr ----------------------------------------- UART_Send(LPC_UART0, send_menu, sizeof(send_menu), BLOCKING); // Send slave addr on RS485 bus if (addr_toggle){ UART_RS485SendSlvAddr(LPC_UART1, SLAVE_ADDR_A); } else { UART_RS485SendSlvAddr(LPC_UART1, SLAVE_ADDR_B); } // delay for a while for (len = 0; len < 1000; len++); // Send data ----------------------------------------------- if (addr_toggle){ UART_RS485SendData(LPC_UART1, slaveA_msg, sizeof(slaveA_msg)); } else { UART_RS485SendData(LPC_UART1, slaveB_msg, sizeof(slaveB_msg)); } // Send terminator UART_RS485SendData(LPC_UART1, &terminator, 1); // delay for a while for (len = 0; len < 1000; len++); // Receive data from slave -------------------------------- UART_Send(LPC_UART0, recv_menu, sizeof(recv_menu), BLOCKING); // If address 'A' required response... if (addr_toggle){ exit_flag = 0; while (!exit_flag){ len = UARTReceive((LPC_UART_TypeDef *)LPC_UART1, buffer, sizeof(buffer)); /* Got some data */ idx = 0; while (idx < len) { if (buffer[idx] == 13){ exit_flag = 1; } else { /* Echo it back */ UART_Send(LPC_UART0, &buffer[idx], 1, BLOCKING); } idx++; } } } UART_Send(LPC_UART0, nextline, sizeof(nextline), BLOCKING); addr_toggle = (addr_toggle ? 0 : 1); // long delay here for (len = 0; len < 10000000; len++); } return 1; }
/*********************************************************************//** * @brief c_entry: Main UART-RS485 program body * @param[in] None * @return None **********************************************************************/ void c_entry(void) { // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; // RS485 configuration UART1_RS485_CTRLCFG_Type rs485cfg; // Temp. data uint32_t idx, retryCnt = 0; volatile uint32_t len; uint8_t buffer[10]; int32_t exit_flag, addr_toggle; // UART0 section ---------------------------------------------------- // Initialize UART0 pin connect PINSEL_ConfigPin(0, 2, 1);//TXD0 PINSEL_ConfigPin(0, 3, 1);//RXD0 /* Initialize UART Configuration parameter structure to default state: * Baudrate = 115200 bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); // Initialize UART0 peripheral with given to corresponding parameter UART_Init(UART_0, &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(UART_0, &UARTFIFOConfigStruct); // Enable UART Transmit UART_TxCmd(UART_0, ENABLE); // print welcome screen print_menu(); #if (UART_TEST_NUM == 1) // UART1 - RS485 section ------------------------------------------------- // Initialize UART1 pin connect //TXD2 PINSEL_ConfigPin(0, 15, 1); //RXD2 PINSEL_ConfigPin(0, 16, 1); //P0.20, UART OE1 Output Enable for UART1 PINSEL_ConfigPin(0, 20, 1); #elif (UART_TEST_NUM == 2) // UART1 - RS485 section ------------------------------------------------- // Initialize UART1 pin connect //TXD2 PINSEL_ConfigPin(0, 10, 1); //RXD2 PINSEL_ConfigPin(0, 11, 1); //OE2: UART OE2 Output Enable for UART2 PINSEL_ConfigPin(1, 19, 6); #elif (UART_TEST_NUM == 3) // UART3 - RS485 section ------------------------------------------------- // Initialize UART3 pin connect //TXD3 PINSEL_ConfigPin(0, 25, 3); //RXD3 PINSEL_ConfigPin(0, 26, 3); //OE3: UART OE3 Output Enable for UART3 PINSEL_ConfigPin(1, 30, 5); #elif (UART_TEST_NUM == 4) // UART4 - RS485 section ------------------------------------------------- // Initialize UART1 pin connect //TXD4 PINSEL_ConfigPin(0, 22, 3); //RXD4 PINSEL_ConfigPin(2, 9, 3); //OE4: UART OE4 Output Enable for UART4 PINSEL_ConfigPin(0, 21, 3); #endif /* 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); UARTConfigStruct.Baud_rate = 9600; // Initialize UART0 peripheral with given to corresponding parameter UART_Init(_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); // Initialize FIFO for UART0 peripheral UART_FIFOConfig(_LPC_UART, &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 disable * - Auto detect address is disabled * - Receive state is enable */ rs485cfg.AutoDirCtrl_State = ENABLE; rs485cfg.DirCtrlPin = UART_RS485_DIRCTRL_DTR; rs485cfg.DirCtrlPol_Level = SET; rs485cfg.DelayValue = 50; rs485cfg.NormalMultiDropMode_State = DISABLE; rs485cfg.AutoAddrDetect_State = DISABLE; rs485cfg.MatchAddrValue = 0; rs485cfg.Rx_State = ENABLE; UART_RS485Config(_LPC_UART, &rs485cfg); /* Enable UART Rx interrupt */ UART_IntConfig(_LPC_UART, UART_INTCFG_RBR, ENABLE); /* Enable UART line status interrupt */ UART_IntConfig(_LPC_UART, UART_INTCFG_RLS, ENABLE); // Priorities settings for UART RS485: here we use UART2 for RS485 communication // They should be changed if using another UART /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(_UART_IRQ, ((0x01<<3)|0x01)); /* Enable Interrupt for UART0 channel */ NVIC_EnableIRQ(_UART_IRQ); // Enable UART Transmit UART_TxCmd(_LPC_UART, ENABLE); addr_toggle = 1; // for testing... while (1) { // Send slave addr on RS485 bus if (addr_toggle) { UART_Send(UART_0, send_menuA, sizeof(send_menuA), BLOCKING); UART_RS485SendSlvAddr(_LPC_UART, SLAVE_ADDR_A); } else { UART_Send(UART_0, send_menuB, sizeof(send_menuB), BLOCKING); UART_RS485SendSlvAddr(_LPC_UART, SLAVE_ADDR_B); } // delay for a while for (len = 0; len < 1000; len++); // Send data ----------------------------------------------- if (addr_toggle) { UART_RS485SendData(_LPC_UART, slaveA_msg, sizeof(slaveA_msg)); } else { UART_RS485SendData(_LPC_UART, slaveB_msg, sizeof(slaveB_msg)); } // Send terminator UART_RS485SendData(_LPC_UART, &terminator, 1); // delay for a while for (len = 0; len < 1000; len++); // Receive data from slave -------------------------------- UART_Send(UART_0, recv_menu, sizeof(recv_menu), BLOCKING); retryCnt = 0; // If address 'A' required response... if (addr_toggle) { exit_flag = 0; while (!exit_flag) { len = UARTReceive(_LPC_UART, buffer, sizeof(buffer)); /* Got some data */ idx = 0; while (idx < len) { if (buffer[idx] == 13) { exit_flag = 1; } else { /* Echo it back */ UART_Send(UART_0, &buffer[idx], 1, BLOCKING); } idx++; retryCnt = 0; } retryCnt++; if(retryCnt == NUM_OF_WAITING) { exit_flag = 1; } } } else { // To clarify that there's no reply from Device B UART_Send(UART_0, noreply, sizeof(noreply), BLOCKING); } UART_Send(UART_0, nextline, sizeof(nextline), BLOCKING); addr_toggle = (addr_toggle ? 0 : 1); // long delay here for (len = 0; len < 10000000; len++); } }