/*********************************************************************//** * @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; }
/*********************************************************************//** * @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; }
/************************************************************* 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-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++); } }