HI,
In the attached code we are facing issue of ethernet disconnection when we send "L: or "D" command to device from HOST, "S" and "E" commands works perfectly. device will act as a client (192.168.1.139)and host will be server on IP192.168.1.130. For "S" command - it start timer and sends data over ethernet, "E" command stops timer and stops data send over ethernet. For this communication device must have MASTER value as 0 otherwise it will act as SLAVE and will not connect with HOST. Initially, it is mandatory that to send "S" command as it start data transfer . the as we send "L" command it turns ON two pins PL4 & PL5 as well as sends data to UART. as this lines gets executed ethernet stops working and disconnects after few seconds.
Problem occurs when below lines gets executed. Entire code is also attached.
if(SNAP_ON==1)
{
SNAP_ON=0;
SNAP1_ON;
SNAP2_ON;
MODBUS_TRANSMIT;
MODBUS_REC_BUF=0x4C;
UARTSend((uint8_t *)&MODBUS_REC_BUF,1); // Send command to slave
//UARTSend((uint8_t *)"L",1);
//SysCtlDelay(SOFT_DELAY);
}
if(SNAP_OFF==1)
{
SNAP_OFF=0;
SNAP1_OFF;
SNAP2_OFF;
MODBUS_TRANSMIT;
MODBUS_REC_BUF=0x55;
UARTSend((uint8_t *)&MODBUS_REC_BUF,1); // Send command to slave
//UARTSend((uint8_t *)"U",1);
//SysCtlDelay(SOFT_DELAY);
}
/* * Author : - KHODIDAS DOMADIYA\ * Organization: - Optimzed Solutions Ltd. * Department: -Product ENgineering services. * * Date:- 12-05-2021 *=> IP ADDRESS USED IS STATIC * * * * Indication OF leds - For ethernet , default LEDs used * * */ /* * ======== tcpEcho.c ======== */ /* XDCtools Header files */ #include <stdint.h> #include <stdio.h> #include <stdbool.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <xdc/std.h> #include <xdc/cfg/global.h> #include <ti/sysbios/hal/Hwi.h> #include <xdc/runtime/Error.h> #include <xdc/runtime/Memory.h> #include <xdc/runtime/System.h> /* BIOS Header files */ #include <ti/sysbios/BIOS.h> #include <ti/sysbios/knl/Clock.h> #include <ti/sysbios/knl/Task.h> /* NDK Header files */ #include <ti/ndk/inc/netmain.h> #include <ti/ndk/inc/_stack.h> /* TI-RTOS Header files */ #include <ti/drivers/GPIO.h> /* Example/Board Header files */ #include "Board.h" //#include <time.h> #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "inc/hw_sysctl.h" #include "driverlib/sysctl.h" #include "driverlib/pin_map.h" #include "inc/hw_gpio.h" #include "driverlib/gpio.h" #include "inc/hw_ints.h" #include "driverlib/interrupt.h" #include "inc/hw_timer.h" #include "driverlib/timer.h" #include <xdc/cfg/global.h> #include <ti/sysbios/hal/Timer.h> #include "inc/hw_flash.h" #include "driverlib/flash.h" #include "driverlib/uart.h" #include "inc/hw_uart.h" #include "utils/uartstdio.h" #define SOFT_DELAY 4000 Timer_Params Timer2; Timer_Handle myTimer; SOCKET lSocket; struct sockaddr_in sLocalAddr; #define TCPPACKETSIZE 256 #define NUMTCPWORKERS 3 /**************************************************************** Global variable ****************************************************************/ bool MASTER; #define MODBUS_TRANSMIT (GPIOPinWrite(GPIO_PORTP_BASE, GPIO_PIN_2, GPIO_PIN_2)) #define MODBUS_RECIEVE (GPIOPinWrite(GPIO_PORTP_BASE, GPIO_PIN_2, 0x00)) #define SYNC_START (GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, GPIO_PIN_0)) #define SYNC_STOP (GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, 0x00)) #define SYNC_READ (GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_0)) #define SLAVE (!MASTER) #define MODBUS_COM_LED_ON (GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_2, 0)) #define START_STP_LED_ON (GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_1, 0)) #define MASTER_SLAVE_LED_ON (GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_0, 0)) #define ETH_LED_ON (GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_3, GPIO_PIN_3)) #define MODBUS_COM_LED_OFF (GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_2, GPIO_PIN_2)) #define START_STP_LED_OFF (GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_1, GPIO_PIN_1)) #define MASTER_SLAVE_LED_OFF (GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_0, GPIO_PIN_0)) #define ETH_LED_OFF (GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_3, 0)) #define SNAP1_ON GPIOPinWrite(GPIO_PORTL_BASE,GPIO_PIN_4,GPIO_PIN_4) #define SNAP2_ON GPIOPinWrite(GPIO_PORTL_BASE,GPIO_PIN_5,GPIO_PIN_5) #define SNAP1_OFF GPIOPinWrite(GPIO_PORTL_BASE,GPIO_PIN_4,0) #define SNAP2_OFF GPIOPinWrite(GPIO_PORTL_BASE,GPIO_PIN_5,0) //**************************************************************************** // System clock rate in Hz. //**************************************************************************** uint32_t g_ui32SysClock; #define MODBUS_DATA_SIZE 28 //***************************************************************************** // Global flag to indicate data was received //***************************************************************************** volatile uint32_t g_bReceiveFlag = 0; //***************************************************************************** // Flags that contain the current value of the interrupt indicator as displayed // on the UART. //***************************************************************************** uint32_t g_ui32Flags; //uint8_t Eth_Buffer[50]="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmn"; uint8_t Eth_Buffer[56]; uint8_t Modbus_Buffer[28]; // UART INT variable uint32_t c; uint32_t C; // for looping purpose of GPIO uint8_t i = 0; uint8_t j = 0; // Different buffer for signals bool pin_data[208]; uint8_t SET_1[8]; uint8_t SET_2[8]; uint8_t SET_3[8]; uint8_t snap_status; /*uint8_t portD_data; uint8_t portE_data; uint8_t portN_data; uint8_t portP_data; uint8_t portB_data; uint8_t portL_data; uint8_t portM_data;*/ // Start and Stop command from host char rcvd_data[1]; char MODBUS_REC_BUF; // Slave command receive buffer uint8_t slave_cmd[2]; // data store in slave buffer uint8_t SLAVE_1_BUFFER[MODBUS_DATA_SIZE],SLAVE_2_BUFFER[MODBUS_DATA_SIZE],SLAVE_3_BUFFER[MODBUS_DATA_SIZE],SLAVE_4_BUFFER[MODBUS_DATA_SIZE],SLAVE_5_BUFFER[MODBUS_DATA_SIZE]; // timer initial define 0 bool TIMER_ON=0,START_FLAG; // Master and Slave address uint8_t SLAVE_ADDRESS; bool SNAP_ON,SNAP_OFF; uint8_t ETHSIZE; //counting verible uint16_t timerCount; uint32_t worktimecount=0; //**************************************************************** TIMER INIT FUNCTIONS****************************************************************/ void timer_init(void); void timerIntHandler(void); //**************************************************************** APPLICATION FUNCTIONS****************************************************************/ void decoderAddrLineSelect(uint8_t temp); void readSignal(); void Snap_Config(void); void modbusUART6init(void); void DebugUART0(void); void UARTSend( const uint8_t *pui8Buffer, uint32_t ui32Count); void UARTRecieve(uint8_t *BUFF, uint32_t ui32Count); void Timer_init(); void Debug_Send( const uint8_t *pui8Buffer, uint32_t ui32Count); /**************************************************************** LOGIC *****************************************************************/ //*********************************inialaization 24 GPIO for different input signals*******************************// void signalconfigure(void) { HWREG(GPIO_PORTD_BASE+GPIO_O_LOCK) = GPIO_LOCK_KEY; // HWREG(GPIO_PORTD_BASE+GPIO_O_CR) |= GPIO_PIN_7; // Unlock port D pin 7 GPIODirModeSet(GPIO_PORTD_BASE,GPIO_PIN_7,GPIO_DIR_MODE_IN);// GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_7|GPIO_PIN_6|GPIO_PIN_4); HWREG(GPIO_PORTE_AHB_BASE+GPIO_O_LOCK) = GPIO_LOCK_KEY; // HWREG(GPIO_PORTE_AHB_BASE+GPIO_O_CR) |= GPIO_PIN_4; // Unlock port E pin 4 GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_4); // GPIODirModeSet(GPIO_PORTE_BASE,GPIO_PIN_4,GPIO_DIR_MODE_IN); // GPIOPinTypeGPIOInput(GPIO_PORTN_BASE, GPIO_PIN_0|GPIO_PIN_1); GPIOPinTypeGPIOInput(GPIO_PORTP_BASE, GPIO_PIN_5|GPIO_PIN_4|GPIO_PIN_3); GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4); GPIOPinTypeGPIOInput(GPIO_PORTL_BASE, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_3|GPIO_PIN_1|GPIO_PIN_0); GPIOPinTypeGPIOInput(GPIO_PORTM_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_6|GPIO_PIN_7); } //*********************************Signal read of 24 GPIO*******************************// /*void readSignal() { portD_data = GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_7 |GPIO_PIN_6|GPIO_PIN_4) & 0xD0; portE_data = GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_4) & 0x10; portB_data = GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_4|GPIO_PIN_3|GPIO_PIN_2) & 0x1C; portN_data = GPIOPinRead(GPIO_PORTN_BASE, GPIO_PIN_0|GPIO_PIN_1) & 0x03; portP_data = GPIOPinRead(GPIO_PORTP_BASE, GPIO_PIN_5|GPIO_PIN_4|GPIO_PIN_3) & 0x38; portL_data = GPIOPinRead(GPIO_PORTL_BASE, GPIO_PIN_7|GPIO_PIN_6|GPIO_PIN_3|GPIO_PIN_1|GPIO_PIN_0) & 0xC7; portM_data = GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_6|GPIO_PIN_7) & 0xDF; }*/ void readSignal() { /* pin_data[j] = GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_7); pin_data[j+1] = GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_6); pin_data[j+2] = GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_4); pin_data[j+3] = GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_4); pin_data[j+4] = GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_4); pin_data[j+5] = GPIOPinRead(GPIO_PORTN_BASE, GPIO_PIN_0); pin_data[j+6] = GPIOPinRead(GPIO_PORTN_BASE, GPIO_PIN_1); pin_data[j+7] = GPIOPinRead(GPIO_PORTP_BASE, GPIO_PIN_5); pin_data[j+8] = GPIOPinRead(GPIO_PORTP_BASE, GPIO_PIN_4); pin_data[j+9] = GPIOPinRead(GPIO_PORTP_BASE, GPIO_PIN_3); pin_data[j+10] = GPIOPinRead(GPIO_PORTL_BASE, GPIO_PIN_6); pin_data[j+11] = GPIOPinRead(GPIO_PORTL_BASE, GPIO_PIN_7); pin_data[j+12] = GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_3); pin_data[j+13] = GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_2); pin_data[j+14] = GPIOPinRead(GPIO_PORTL_BASE, GPIO_PIN_3); pin_data[j+15] = GPIOPinRead(GPIO_PORTL_BASE, GPIO_PIN_1); pin_data[j+16] = GPIOPinRead(GPIO_PORTL_BASE, GPIO_PIN_0); pin_data[j+17] = GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_0); pin_data[j+18] = GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_1); pin_data[j+19] = GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_2); pin_data[j+20] = GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_3); pin_data[j+21] = GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_4); pin_data[j+22] = GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_6); pin_data[j+23] = GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_7); j+=24; if( j == 192) { j = 0; }*/ SET_1[j]= (GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_7) << 0) + (GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_6) << 1) \ + (GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_4) << 2) + (GPIOPinRead(GPIO_PORTE_BASE, GPIO_PIN_4) << 3) \ + (GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_4) << 4) + (GPIOPinRead(GPIO_PORTN_BASE, GPIO_PIN_0) << 5) \ + (GPIOPinRead(GPIO_PORTN_BASE, GPIO_PIN_1) << 6) + (GPIOPinRead(GPIO_PORTP_BASE, GPIO_PIN_5) << 7); SET_2[j]= (GPIOPinRead(GPIO_PORTP_BASE, GPIO_PIN_4) << 0) + (GPIOPinRead(GPIO_PORTP_BASE, GPIO_PIN_3) << 1) \ + (GPIOPinRead(GPIO_PORTL_BASE, GPIO_PIN_6) << 2) + (GPIOPinRead(GPIO_PORTL_BASE, GPIO_PIN_7) << 3) \ + (GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_3) << 4) + (GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_2) << 5) \ + (GPIOPinRead(GPIO_PORTL_BASE, GPIO_PIN_3) << 6) + (GPIOPinRead(GPIO_PORTL_BASE, GPIO_PIN_1) << 7); SET_3[j]= (GPIOPinRead(GPIO_PORTL_BASE, GPIO_PIN_0) << 0) + (GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_0) << 1) \ + (GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_0) << 2) + (GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_1) << 3) \ + (GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_2) << 4) + (GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_4) << 5) \ + (GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_6) << 6) + (GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_7) << 7); } //*********************************SNAP RELAY PIN CONFIGURATION*******************************// void Snap_Config(void) { GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_4|GPIO_PIN_5); GPIOPinWrite(GPIO_PORTL_BASE,GPIO_PIN_4|GPIO_PIN_5, 0|0); } //********************************* EXTERNAL MUX ADDRESS SELECTOR RESET*******************************// void resetExternalMuxSelect(void) { GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_5); // Pin low GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, 0); // GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_5); // Pin high GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_5, GPIO_PIN_5); // } //********************************* MASTER SLAVE ADDRESS SELECT PIN CONFIGURATION*******************************// void Master_Slave_Pin_Config(void) { // Enable the GPIO Peripheral SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOQ); // select GPIO for MODBUS address GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_0); GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_1); GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_2); GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_3); GPIOPinTypeGPIOInput(GPIO_PORTQ_BASE, GPIO_PIN_0); } //********************************* MUX ADDRESS GENERATOR PIN (S0, S1, S2) CONFIGURATION*******************************// void MuxAddress_Pin_Configuration() { GPIOPinTypeGPIOOutput(GPIO_PORTK_BASE, GPIO_PIN_0); // s1 GPIOPinTypeGPIOOutput(GPIO_PORTK_BASE, GPIO_PIN_1); // s0 GPIOPinTypeGPIOOutput(GPIO_PORTK_BASE, GPIO_PIN_2); //s2 } //********************************* MUX ADDRESS GENERATOR FUNCTION (S0, S1, S2) *******************************// void decoderAddrLineSelect(uint8_t temp) { GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_0, temp); GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_1, temp); GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_2, temp); } //***************************************************************************** // Master and slave address selection //***************************************************************************** bool Master_Slave_Select() { bool t1,t2,t3; t1=GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_0); t2=GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_1); t3=GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_2); SLAVE_ADDRESS = (t1<<0) + (t2<<1) +(t3<<2); if(SLAVE_ADDRESS==0x00) { MASTER_SLAVE_LED_ON; Debug_Send((uint8_t *)"Device is Master",20); return 1; } else { MASTER_SLAVE_LED_OFF; char debug_buf[]="Device is slave:-"; sprintf(debug_buf,"Device is slave:-%d",SLAVE_ADDRESS); Debug_Send((uint8_t *)debug_buf,20); return 0; } } //***************************************************************************** // MODBUS UART6 interrupt handler. //***************************************************************************** void modbusUART6IntHandler(void) { uint32_t ui32Status; // Get the interrupt status. ui32Status = UARTIntStatus(UART6_BASE, true); // Clear the asserted interrupts. UARTIntClear(UART6_BASE, ui32Status); if( ( (UART_INT_RX & ui32Status) == UART_INT_RX ) || ( (UART_INT_RT & ui32Status) == UART_INT_RT) ) { // Receive UART char while(UARTCharsAvail(UART6_BASE)) { // Read the next character from the UART and write it back to the UART. UARTCharGetNonBlocking(UART6_BASE); C = UARTCharGet(UART6_BASE); } } } //***************************************************************************** // MODBUS UART6 configure //***************************************************************************** void modbusUART6init(void) { // Enable UART6. SysCtlPeripheralEnable(SYSCTL_PERIPH_UART6); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOP); // Enable processor interrupts. IntMasterEnable(); // Configure GPIO Pins for UART mode. GPIOPinConfigure(GPIO_PP0_U6RX); GPIOPinConfigure(GPIO_PP1_U6TX); GPIOPinTypeUART(GPIO_PORTP_BASE, GPIO_PIN_0 | GPIO_PIN_1); // Initialize the UART for console I/O. UARTConfigSetExpClk(UART6_BASE, g_ui32SysClock, 2000000,(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |UART_CONFIG_PAR_NONE)); //IntEnable(INT_UART6); //UARTIntEnable(UART6_BASE, UART_INT_RX | UART_INT_TX |UART_INT_RT); UARTEnable(UART6_BASE); GPIOPinTypeGPIOOutput(GPIO_PORTP_BASE, GPIO_PIN_2); //MODBUS_TX_RX_CNTRL } void DebugUART0(void) { // Enable the GPIO Peripheral used by the UART. SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // Enable UART0. SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); // Enable processor interrupts. IntMasterEnable(); // Configure GPIO Pins for UART mode. GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTConfigSetExpClk(UART0_BASE, g_ui32SysClock, 115200,(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |UART_CONFIG_PAR_NONE)); UARTFIFOLevelSet(UART0_BASE, UART_FIFO_TX1_8, UART_FIFO_RX1_8); // Enable the UART interrupt. UARTEnable(UART0_BASE); } //***************************************************************************** // Send a string to the UART. //***************************************************************************** void UARTSend( const uint8_t *pui8Buffer, uint32_t ui32Count) { // Loop while there are more characters to send. while(ui32Count--) { // Write the next character to the UART. UARTCharPut(UART6_BASE, *pui8Buffer++); } } //***************************************************************************** // Receive a string to the UART. //***************************************************************************** void UARTRecieve(uint8_t *BUFF, uint32_t ui32Count) { while(UARTCharsAvail(UART6_BASE)) { while(ui32Count--) { // Read the next character from the UART UARTCharGetNonBlocking(UART6_BASE); *BUFF++ = UARTCharGet(UART6_BASE); } } } void Debug_Send( const uint8_t *pui8Buffer, uint32_t ui32Count) { // Loop while there are more characters to send. while(ui32Count--) { // Write the next character to the UART. UARTCharPut(UART0_BASE, *pui8Buffer++); } } // Timer configuration as RTOS base void Timer_init() { timer_init(); Timer_Params_init(&Timer2); Timer2.arg=0; // Timer2.period= 10; // 20us Timer2.period= 1000; //1 ms (Milliseconds) //Timer2.period= 800; //800 us (Microseconds) Timer2.periodType=Timer_PeriodType_MICROSECS; myTimer=Timer_create(2,(Timer_FuncPtr)timerIntHandler,&Timer2,NULL); //Timer_start(myTimer); Timer_stop(myTimer); } // Timer start void Timer_Start(void) { Timer_start(myTimer); } // Timer stop void Timer_Stop(void) { Timer_stop(myTimer); } void timer_init(void) { uint32_t ui32Period; //Set CPU Clock to 40MHz. 400MHz PLL/2 = 200 DIV 5 = 40MHz SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1); // Timer 2 setup code SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2); // enable Timer 2 periph clks TimerConfigure(TIMER2_BASE, TIMER_CFG_PERIODIC); // cfg Timer 2 mode - periodic ui32Period = (SysCtlClockGet() /100); TimerLoadSet(TIMER2_BASE, TIMER_A, ui32Period); // set Timer 2 period TimerIntEnable(TIMER2_BASE, TIMER_TIMA_TIMEOUT); // enables Timer 2 to interrupt CPU TimerEnable(TIMER2_BASE, TIMER_A); // enable Timer 2 } void Sync_Config() { if(MASTER) { GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0); // SYNCH PIN SYNC_STOP; } else { GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_0); // SYNCH PIN } } void Indication_Led_config() { GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_2); // MODBUS COM GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_1); //START/ STOP LED GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_0); //MASTER/SLAVE LED GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE, GPIO_PIN_3); //ETH_LED MODBUS_COM_LED_OFF; START_STP_LED_OFF; MASTER_SLAVE_LED_OFF; ETH_LED_OFF; } //int checkLinkState(void) // { // return EMAC_isLinkUp(0); // } /* * ======== tcpHandler ======== * Creates new Task to handle new TCP connections. */ Void tcpHandler(UArg arg0, UArg arg1) { fdOpenSession(TaskSelf()); lSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (lSocket < 0) { // System_printf("tcpHandler: socket failed\n"); Task_exit(); return; } memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr)); sLocalAddr.sin_family = AF_INET; sLocalAddr.sin_addr.s_addr = inet_addr("192.168.1.130");//htonl(INADDR_ANY); sLocalAddr.sin_port = htons(arg0); Timer_init(); //status = checkLinkState(); while(SLAVE_ADDRESS>0) { MODBUS_RECIEVE; if(START_FLAG==0) { //if(UARTCharGetNonBlocking(UART6_BASE)==0x53) MODBUS_RECIEVE; MODBUS_REC_BUF= UARTCharGet(UART6_BASE); if(MODBUS_REC_BUF==0x53) { MODBUS_REC_BUF=0x00; Timer_Start(); START_STP_LED_ON; START_FLAG=1; TIMER_ON=1; } } } while(connect(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr)) < 0) { ETH_LED_OFF; SysCtlDelay(400000); } ETH_LED_ON; while (1) { recv(lSocket,(char *)rcvd_data,sizeof(rcvd_data), MSG_DONTWAIT); while(MASTER) { recv(lSocket,(char *)rcvd_data,sizeof(rcvd_data), MSG_DONTWAIT); if(strcmp(rcvd_data, "S" )==0) //START { Timer_Start(); MODBUS_TRANSMIT; SYNC_START; UARTSend((uint8_t *)"S",1); SysCtlDelay(SOFT_DELAY); START_STP_LED_ON; TIMER_ON=1; } //send(lSocket, flashbuffer,sizeof(flashbuffer), 0 ); while(TIMER_ON) { recv(lSocket,(char *)rcvd_data,sizeof(rcvd_data), MSG_DONTWAIT); if(strcmp(rcvd_data,"E" )==0) //STOP { START_STP_LED_OFF; TIMER_ON=0; SYNC_STOP; MODBUS_TRANSMIT; UARTSend((uint8_t *)"E",1); SysCtlDelay(SOFT_DELAY); Timer_Stop(); timerCount=0; } if(strcmp(rcvd_data, "L" )==0) { //SNAP1_ON; //SNAP2_ON; snap_status=1; SNAP_ON=1; rcvd_data[0]=0; //MODBUS_TRANSMIT; //UARTSend((uint8_t *)"L",1); //SysCtlDelay(SOFT_DELAY); } if(strcmp(rcvd_data,"D" )==0) { //SNAP1_OFF; //SNAP2_OFF; SNAP_OFF=1; snap_status=0; rcvd_data[0]=0; //MODBUS_TRANSMIT; //UARTSend((uint8_t *)"U",1); //SysCtlDelay(SOFT_DELAY); } if(ETHSIZE==56) { ETHSIZE=0; send(lSocket, Eth_Buffer,sizeof(Eth_Buffer), 0 ); } } } } } /* * ======== main ======== */ int main(void) { // Task_Handle taskHandle; // Task_Params taskParams; // Error_Block eb; g_ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |SYSCTL_OSC_MAIN |SYSCTL_USE_PLL |SYSCTL_CFG_VCO_320), 120000000); /* Call board init functions */ Board_initGeneral(); Board_initGPIO(); Board_initEMAC(); Indication_Led_config(); signalconfigure(); Snap_Config(); resetExternalMuxSelect(); DebugUART0(); modbusUART6init(); Master_Slave_Pin_Config(); MuxAddress_Pin_Configuration(); MASTER = Master_Slave_Select(); Sync_Config(); /* Start BIOS */ BIOS_start(); return (0); } //--------------------------------------- ------------------------------------ // timer interrupt //--------------------------------------------------------------------------- void timerIntHandler(void) { uint8_t ui32count=0; TimerIntClear(TIMER2_BASE, TIMER_TIMA_TIMEOUT); // must clear timer flag FROM timer worktimecount++; timerCount++; // counting time if(timerCount>4000) { timerCount=1; } /* Read 26 signals 8 times */ while(i<8) { decoderAddrLineSelect(i); // MUX line selection readSignal(); i++; } i=0; /* Master and Slave logic */ if(MASTER) { Eth_Buffer[0]=SLAVE_ADDRESS; memcpy(Eth_Buffer+1,&SET_1,8); memcpy(Eth_Buffer+9,&SET_2,8); memcpy(Eth_Buffer+17,&SET_3,8); Eth_Buffer[25] = (timerCount >> 8); Eth_Buffer[26] = timerCount; Eth_Buffer[27] = snap_status; //=====================================command to salve 01==================================// MODBUS_TRANSMIT; MODBUS_REC_BUF=0x01; UARTSend((uint8_t *)&MODBUS_REC_BUF,1); // Send command to slave SysCtlDelay(SOFT_DELAY); // MODBUS_REC_BUF=0x54; // UARTSend((uint8_t *)&MODBUS_REC_BUF,1); // SysCtlDelay(SOFT_DELAY); ui32count=0; while(ui32count<MODBUS_DATA_SIZE) { MODBUS_RECIEVE; SLAVE_1_BUFFER[ui32count]= UARTCharGet(UART6_BASE); // data receive from the slave ui32count++; } SysCtlDelay(SOFT_DELAY); memcpy(Eth_Buffer+28,SLAVE_1_BUFFER,sizeof(SLAVE_1_BUFFER)); ETHSIZE=56; if(SNAP_ON==1) { SNAP_ON=0; SNAP1_ON; SNAP2_ON; MODBUS_TRANSMIT; MODBUS_REC_BUF=0x4C; UARTSend((uint8_t *)&MODBUS_REC_BUF,1); // Send command to slave //UARTSend((uint8_t *)"L",1); //SysCtlDelay(SOFT_DELAY); } if(SNAP_OFF==1) { SNAP_OFF=0; SNAP1_OFF; SNAP2_OFF; MODBUS_TRANSMIT; MODBUS_REC_BUF=0x55; UARTSend((uint8_t *)&MODBUS_REC_BUF,1); // Send command to slave //UARTSend((uint8_t *)"U",1); //SysCtlDelay(SOFT_DELAY); } /* //=====================================command to salve 02==================================// MODBUS_TRANSMIT; MODBUS_REC_BUF=0x02; UARTSend((uint8_t *)&MODBUS_REC_BUF,1); // Send command to slave MODBUS_REC_BUF=0x54; UARTSend((uint8_t *)&MODBUS_REC_BUF,1); SysCtlDelay(SOFT_DELAY); while(ui32count<MODBUS_DATA_SIZE) { MODBUS_RECIEVE; SLAVE_1_BUFFER[ui32count]= UARTCharGet(UART6_BASE); // data receive from the slave ui32count++; } SysCtlDelay(SOFT_DELAY); memcpy(Eth_Buffer+56,SLAVE_1_BUFFER,sizeof(SLAVE_1_BUFFER)); //=====================================command to salve 03==================================// MODBUS_TRANSMIT; MODBUS_REC_BUF=0x03; UARTSend((uint8_t *)&MODBUS_REC_BUF,1); // Send command to slave MODBUS_REC_BUF=0x54; UARTSend((uint8_t *)&MODBUS_REC_BUF,1); SysCtlDelay(SOFT_DELAY); while(ui32count<MODBUS_DATA_SIZE) { MODBUS_RECIEVE; SLAVE_1_BUFFER[ui32count]= UARTCharGet(UART6_BASE); // data receive from the slave ui32count++; } SysCtlDelay(SOFT_DELAY); memcpy(Eth_Buffer+84,SLAVE_1_BUFFER,sizeof(SLAVE_1_BUFFER)); //=====================================command to salve 04==================================// MODBUS_TRANSMIT; MODBUS_REC_BUF=0x04; UARTSend((uint8_t *)&MODBUS_REC_BUF,1); // Send command to slave MODBUS_REC_BUF=0x54; UARTSend((uint8_t *)&MODBUS_REC_BUF,1); SysCtlDelay(SOFT_DELAY); while(ui32count<MODBUS_DATA_SIZE) { MODBUS_RECIEVE; SLAVE_1_BUFFER[ui32count]= UARTCharGet(UART6_BASE); // data receive from the slave ui32count++; } SysCtlDelay(SOFT_DELAY); memcpy(Eth_Buffer+112,SLAVE_1_BUFFER,sizeof(SLAVE_1_BUFFER)); //=====================================command to salve 05==================================// MODBUS_TRANSMIT; MODBUS_REC_BUF=0x05; UARTSend((uint8_t *)&MODBUS_REC_BUF,1); // Send command to slave MODBUS_REC_BUF=0x54; UARTSend((uint8_t *)&MODBUS_REC_BUF,1); SysCtlDelay(SOFT_DELAY); while(ui32count<MODBUS_DATA_SIZE) { MODBUS_RECIEVE; SLAVE_1_BUFFER[ui32count]= UARTCharGet(UART6_BASE); // data receive from the slave ui32count++; } SysCtlDelay(SOFT_DELAY); memcpy(Eth_Buffer+140,SLAVE_1_BUFFER,sizeof(SLAVE_1_BUFFER)); */ } else { MODBUS_RECIEVE; slave_cmd[0]= UARTCharGet(UART6_BASE); //slave_cmd[1]= UARTCharGet(UART6_BASE); // Command receive from master SysCtlDelay(SOFT_DELAY); if(slave_cmd[0]==0x01) { //if(slave_cmd[1]==0x54) { MODBUS_TRANSMIT; Modbus_Buffer[0]=SLAVE_ADDRESS; memcpy(Modbus_Buffer+1,&SET_1,8); memcpy(Modbus_Buffer+9,&SET_2,8); memcpy(Modbus_Buffer+17,&SET_3,8); Modbus_Buffer[25] = (timerCount >> 8); Modbus_Buffer[26] = timerCount; Modbus_Buffer[27] = snap_status; // UARTSend((uint8_t *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ01",28); UARTSend(Modbus_Buffer, sizeof(Modbus_Buffer)); SysCtlDelay(SOFT_DELAY); slave_cmd[0]=0x00; slave_cmd[1]=0x00; } } if(slave_cmd[0]==0x45) { //if(slave_cmd[1]==0x01) { timerCount=0; TIMER_ON=0; START_FLAG=0; Timer_Stop(); //while(1) { MODBUS_RECIEVE; MODBUS_REC_BUF= UARTCharGet(UART6_BASE); SysCtlDelay(SOFT_DELAY); if(MODBUS_REC_BUF==0x53) { MODBUS_REC_BUF=0x00; Timer_Start(); START_STP_LED_ON; START_FLAG=1; TIMER_ON=1; //break; } } } } if(slave_cmd[0]==0x4C) { SNAP1_ON; SNAP2_ON; snap_status=1; } if(slave_cmd[0]==0x55) { SNAP1_OFF; SNAP2_OFF; snap_status=0; } /*switch (slave_cmd[0]) { case 0x01: if(slave_cmd[1]==0x54) { MODBUS_TRANSMIT; memcpy(Modbus_Buffer,&SLAVE_ADDRESS,sizeof(SLAVE_ADDRESS)); memcpy(Modbus_Buffer+1,SET_1,sizeof(SET_1)); memcpy(Modbus_Buffer+9,SET_2,sizeof(SET_2)); memcpy(Modbus_Buffer+17,SET_3,sizeof(SET_3)); memcpy(Modbus_Buffer+25,(timerCount<<8),sizeof(timerCount)/2); memcpy(Modbus_Buffer+26,timerCount,sizeof(timerCount)/2); memcpy(Modbus_Buffer+27,&snap_status,sizeof(snap_status)); // UARTSend((uint8_t *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ01",28); UARTSend(Modbus_Buffer, sizeof(Modbus_Buffer)); SysCtlDelay(SOFT_DELAY); slave_cmd[0]=0x00; slave_cmd[1]=0x00; } break; case 0x02: if(slave_cmd[1]==0x54) { MODBUS_TRANSMIT; memcpy(Modbus_Buffer,&SLAVE_ADDRESS,sizeof(SLAVE_ADDRESS)); memcpy(Modbus_Buffer+1,SET_1,sizeof(SET_1)); memcpy(Modbus_Buffer+9,SET_2,sizeof(SET_2)); memcpy(Modbus_Buffer+17,SET_3,sizeof(SET_3)); memcpy(Modbus_Buffer+25,(timerCount<<8),sizeof(timerCount)/2); memcpy(Modbus_Buffer+26,timerCount,sizeof(timerCount)/2); memcpy(Modbus_Buffer+27,&snap_status,sizeof(snap_status));memcpy(Modbus_Buffer+27,&timerCount,sizeof(timerCount)/2); // UARTSend((uint8_t *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ01",28); UARTSend(Modbus_Buffer, sizeof(Modbus_Buffer)); SysCtlDelay(SOFT_DELAY); slave_cmd[0]=0x00; slave_cmd[1]=0x00; } break; case 0x03: if(slave_cmd[1]==0x54) { MODBUS_TRANSMIT; memcpy(Modbus_Buffer,&SLAVE_ADDRESS,sizeof(SLAVE_ADDRESS)); memcpy(Modbus_Buffer+1,SET_1,sizeof(SET_1)); memcpy(Modbus_Buffer+9,SET_2,sizeof(SET_2)); memcpy(Modbus_Buffer+17,SET_3,sizeof(SET_3)); memcpy(Modbus_Buffer+25,(timerCount<<8),sizeof(timerCount)/2); memcpy(Modbus_Buffer+26,timerCount,sizeof(timerCount)/2); memcpy(Modbus_Buffer+27,&snap_status,sizeof(snap_status));memcpy(Modbus_Buffer+27,&timerCount,sizeof(timerCount)/2); // UARTSend((uint8_t *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ01",28); UARTSend(Modbus_Buffer, sizeof(Modbus_Buffer)); SysCtlDelay(SOFT_DELAY); slave_cmd[0]=0x00; slave_cmd[1]=0x00; } break; case 0x04: if(slave_cmd[1]==0x54) { MODBUS_TRANSMIT; memcpy(Modbus_Buffer,&SLAVE_ADDRESS,sizeof(SLAVE_ADDRESS)); memcpy(Modbus_Buffer+1,SET_1,sizeof(SET_1)); memcpy(Modbus_Buffer+9,SET_2,sizeof(SET_2)); memcpy(Modbus_Buffer+17,SET_3,sizeof(SET_3)); memcpy(Modbus_Buffer+25,(timerCount<<8),sizeof(timerCount)/2); memcpy(Modbus_Buffer+26,timerCount,sizeof(timerCount)/2); memcpy(Modbus_Buffer+27,&snap_status,sizeof(snap_status)); // UARTSend((uint8_t *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ01",28); UARTSend(Modbus_Buffer, sizeof(Modbus_Buffer)); SysCtlDelay(SOFT_DELAY); slave_cmd[0]=0x00; slave_cmd[1]=0x00; } break; case 0x05: if(slave_cmd[1]==0x54) { MODBUS_TRANSMIT; memcpy(Modbus_Buffer,&SLAVE_ADDRESS,sizeof(SLAVE_ADDRESS)); memcpy(Modbus_Buffer+1,SET_1,sizeof(SET_1)); memcpy(Modbus_Buffer+9,SET_2,sizeof(SET_2)); memcpy(Modbus_Buffer+17,SET_3,sizeof(SET_3)); memcpy(Modbus_Buffer+25,(timerCount<<8),sizeof(timerCount)/2); memcpy(Modbus_Buffer+26,timerCount,sizeof(timerCount)/2); memcpy(Modbus_Buffer+27,&snap_status,sizeof(snap_status)); // UARTSend((uint8_t *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ01",28); UARTSend(Modbus_Buffer, sizeof(Modbus_Buffer)); SysCtlDelay(SOFT_DELAY); slave_cmd[0]=0x00; slave_cmd[1]=0x00; } break; }*/ } }
Regards
Khodidas