Ejemplo n.º 1
0
static void
vInitTask( void *pvParameters )
{
    const unsigned char ucSlaveIDAdditonal[] = { 0xAA, 0xBB, 0xCC };
    eMBErrorCode    eStatus;

    /* Select either ASCII or RTU Mode. */
    eStatus = eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_EVEN );
    assert( eStatus == MB_ENOERR );

    /* Configure the slave id of the device. */
    eStatus = eMBSetSlaveID( 44, TRUE, ucSlaveIDAdditonal, 3 );
    assert( eStatus == MB_ENOERR );

    /* Enable the Modbus Protocol Stack. */
    eStatus = eMBEnable(  );

    for( ;; )
    {
        /* Call the main polling loop of the Modbus protocol stack. Internally
         * the polling loop waits for a new event by calling the port 
         * dependent function xMBPortEventGet(  ). In the FreeRTOS port the
         * event layer is built with queues.
         */
        ( void )eMBPoll(  );

        /* Here we simply count the number of poll cycles. */
        usRegInputBuf[0]++;
    }
}
Ejemplo n.º 2
0
/*
 * logical level switch I/O thread
 */
msg_t thdJbus485IO(void *arg)
{
    (void)arg;

    chRegSetThreadName("thd JBUS 485 IO");
    while  (initJbus485 () != TRUE) {
        chThdSleepMilliseconds(1000);
        if (chThdShouldTerminate())
            goto cleanAndExit;
    }

    chThdSleepMilliseconds(10);

    do {
        eMBPoll ();
    }  while (!chThdShouldTerminate());


cleanAndExit:
    eMBDisable ();
    eMBClose ();
    DebugTrace ("thdJbusIO 485 Thread Is stopping");

    return 0;
}
Ejemplo n.º 3
0
/**
 * MCU: Atmega328
 * Fuses: Oscilador interno a 8 Mhz (sin dividir por 8)
 * 		-U lfuse:w:0xe2:m -U hfuse:w:0xd1:m -U efuse:w:0x07:m
 */
int main(void) {
	adc_init();

	timer0_init(timer0_callback);

	i2c_init();

	rtc_init(rtc);
	rtc_sqw_rate(rtc, 1);
	rtc_sqw_enable(rtc);
	rtc_clock_start(rtc);

	eMBInit(MB_RTU, 0x03, 0, 9600, MB_PAR_NONE);
	eMBSetSlaveID(0x3, TRUE, (UCHAR*) "demeter", 8);
	eMBEnable();

	blinkenlight(5, 100);

	parameters_init();

	ports_init();

	f_mount(&fs, "", 0);
	update_log_filename();

	while (1) {
		eMBPoll();
		update_state();

		_delay_ms(100);
	}

	return (0);
}
Ejemplo n.º 4
0
/* ----------------------- Start implementation -----------------------------*/
int
main( void )
{
    const UCHAR     ucSlaveID[] = { 0xAA, 0xBB, 0xCC };
    eMBErrorCode    eStatus;

//    DDRA |= 0b00001110;
	 

    eStatus = eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_NONE );

    eStatus = eMBSetSlaveID( 0x34, TRUE, ucSlaveID, 3 );
    sei(  );

    /* Enable the Modbus Protocol Stack. */
    eStatus = eMBEnable(  );

    for( ;; )
    {
        ( void )eMBPoll(  );

        /* Here we simply count the number of poll cycles. */
        usRegInputBuf[0]++;
    }
}
Ejemplo n.º 5
0
/* ----------------------- Start implementation -----------------------------*/
int
main( void )
{
    const UCHAR     ucSlaveID[] = { 0xAA, 0xBB, 0xCC };
    eMBErrorCode    eStatus;
	ULONG			my_counter = 0;

    eStatus = eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_EVEN );

    eStatus = eMBSetSlaveID( 0x34, TRUE, ucSlaveID, 3 );
    sei(  );

    /* Enable the Modbus Protocol Stack. */
    eStatus = eMBEnable(  );

    for( ;; )
    {
        ( void )eMBPoll(  );

        /* Here we simply count the number of poll cycles. */
		if( my_counter++ > 100000) {
			my_counter = 0;
			usRegInputBuf[0]++;
		}
        /* The constant value. */
        usRegInputBuf[1] = 33;
    }
}
Ejemplo n.º 6
0
static void Modbus_RS485_Task(void *pvParameters)
{
    eMBEnable(&stContext_RS485);
    Watchdog_SetTaskStatus(RS485_TASK,TASK_ACTIVE);
    for( ;; )
    {
        eMBPoll(&stContext_RS485);
        vTaskDelay(MODBUS_POLL_TIME);
        Watchdog_IncrementCouter(RS485_TASK);
    }
}
static void *modbus_pollthread(void *pvarg)
{
  eMBErrorCode mberr;
  int ret;

  /* Initialize the modbus */

  ret = modbus_initialize();
  if (ret != OK)
    {
      fprintf(stderr, "modbus_main: "
              "ERROR: modbus_initialize failed: %d\n", ret);
      return NULL;
    }

  srand(time(NULL));

  /* Then loop until we are commanded to shutdown */

  do
    {
      /* Poll */

      mberr = eMBPoll();
      if (mberr != MB_ENOERR)
        {
           break;
        }

      /* Generate some random input */

      g_modbus.reginput[0] = (uint16_t)rand();
    }
  while (g_modbus.threadstate != SHUTDOWN);

  /* Disable */

  (void)eMBDisable();

  /* Release hardware resources. */

  (void)eMBClose();

  /* Free/uninitialize data structures */

  (void)pthread_mutex_destroy(&g_modbus.lock);
  g_modbus.threadstate = STOPPED;
  return NULL;
}
Ejemplo n.º 8
0
/* ----------------------- Start implementation -----------------------------*/
int
main( void )
{

    /* Select either ASCII or RTU Mode. */
    ( void )eMBInit( MB_RTU, 0x0A, 0, 9600, MB_PAR_EVEN );

    /* Enable the Modbus Protocol Stack. */
    ( void )eMBEnable(  );
    for( ;; )
    {
        /* Call the main polling loop of the Modbus protocol stack. */
        ( void )eMBPoll(  );
    }
}
Ejemplo n.º 9
0
static void
vModbusTask( void *pvParameters )
{
    /* Select either ASCII or RTU Mode. */
    ( void )eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_EVEN );

    /* Enable the Modbus Protocol Stack. */
    ( void )eMBEnable(  );

    /* Enter main loop. */
    for( ;; )
    {
        /* Call the main polling loop of the Modbus protocol stack. */
        ( void )eMBPoll(  );
    }
}
Ejemplo n.º 10
0
/* ==============================================
 main task routine
 ============================================== */
int main(void) {
	pool_memadd((uint32_t) pool, sizeof(pool));

#ifdef DEBUG
	dbg.start();
#endif

	eMBErrorCode eStatus;
//	dbg.waitToDebugMode();

#if MB_TCP_ENABLED == 1
	eStatus = eMBTCPInit( MB_TCP_PORT_USE_DEFAULT );
#endif

	if (eStatus != MB_ENOERR)
		dbg.println("can't initialize modbus stack!");

	/* Enable the Modbus Protocol Stack. */
	eStatus = eMBEnable();
	if (eStatus != MB_ENOERR)
		dbg.println("can't enable modbus stack!");

	// Initialise some registers
	usRegInputBuf[1] = 0x1234;
	usRegInputBuf[2] = 0x5678;
	usRegInputBuf[3] = 0x9abc;

	// debug LED
	CPin led(LED1);
	CTimeout tm;

	// Enter an endless loop
	while (1) {
		if ( tm.read()>0.5 ) {
			led = !led;
			tm.reset();
		}

	    eStatus = eMBPoll(  );

	    /* Here we simply count the number of poll cycles. */
	    usRegInputBuf[0]++;
	}
	return 0;
}
Ejemplo n.º 11
0
/* ----------------------- Start implementation -----------------------------*/
int
main( void )
{
    eMBErrorCode    eStatus;

    eStatus = eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_EVEN );

    /* Enable the Modbus Protocol Stack. */
    eStatus = eMBEnable(  );

    for( ;; )
    {
        ( void )eMBPoll(  );

        /* Here we simply count the number of poll cycles. */
        usRegInputBuf[0]++;
    }
}
Ejemplo n.º 12
0
/**
  * @功能
  * @参数
  * @返回值
  */
int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    NVIC_Configuration();
    LED_Config();

    /*       模式 从机地址 端口 波特率 校验位*/
    eMBInit( MB_RTU, 0x02, 0, 9600, MB_PAR_NONE );

    /* Enable the Modbus Protocol Stack. */
    eMBEnable(  );

    for( ;; )
    {
        ( void )eMBPoll(  );
        /* Here we simply count the number of poll cycles. */
//     usRegInputBuf[0]++;
    }
}
Ejemplo n.º 13
0
/* ----------------------- Start implementation -----------------------------*/
int main(void)
{
  eMBErrorCode eStatus;

  eStatus = eMBInit(MB_RTU, 0x0A, 0, BAUD_MODBUS, MB_PAR_EVEN); /* Port 0 is defined in portserial.c */

  sei( );

  /* Enable the Modbus Protocol Stack. */
  eStatus = eMBEnable();

  for (;;)
  {
    (void) eMBPoll();

    /* Here we simply count the number of poll cycles. */
    usRegInputBuf[0]++;
  }
}
Ejemplo n.º 14
0
/* ----------------------- Start implementation -----------------------------*/
int
main( void )
{
    _SetupHardware(  );
    
    const UCHAR     ucSlaveID[] = { 0xAA, 0xBB, 0xCC };
    eMBErrorCode    eStatus;

    for( ;; )
    {
        if( MB_ENOERR != ( eStatus = eMBInit( MB_RTU, 0x0A, 1, 38400, MB_PAR_EVEN ) ) )
        {
            /* Can not initialize. Add error handling code here. */
        }
        else
        {      
            if( MB_ENOERR != ( eStatus = eMBSetSlaveID( 0x34, TRUE, ucSlaveID, 3 ) ) )
            {
                /* Can not set slave id. Check arguments */
            }
            else if( MB_ENOERR != ( eStatus = eMBEnable(  ) ) )
            {
                /* Enable failed. */
            }
            else
            {      
                usRegHoldingBuf[0] = 1;
                do
                {
                    ( void )eMBPoll(  );
            
                    /* Here we simply count the number of poll cycles. */
                    usRegInputBuf[0]++;
                }
                while( usRegHoldingBuf[0] );
                ( void )eMBDisable(  );
                ( void )eMBClose(  );                
            }
        }
    }    
    return 1;
}
Ejemplo n.º 15
0
/**
 * Poll the modbus.
 *
 * @param pvParameter
 */
void           *
pvPollingThread( void *pvParameter )
{
    vSetPollingThreadState( RUNNING );
    DEBUG_PUTSTRING("Thread started!");
    if( eMBEnable(  ) == MB_ENOERR )
    {
        do
        {
            if( eMBPoll(  ) != MB_ENOERR )
                break;
        }
        while( eGetPollingThreadState(  ) != SHUTDOWN );
    }

    ( void )eMBDisable(  );
    vSetPollingThreadState( STOPPED );
    DEBUG_PUTSTRING("Thread stopped!");
    cmd_done(BOOT_EXIT);
    return 0;
}
Ejemplo n.º 16
0
DWORD           WINAPI
dwPollingThread( LPVOID lpParameter )
{
    eSetPollingThreadState( RUNNING );

    if( eMBEnable(  ) == MB_ENOERR )
    {
        do
        {
            if( eMBPoll(  ) != MB_ENOERR )
                break;
        }
        while( eGetPollingThreadState(  ) != SHUTDOWN );
    }

    ( void )eMBDisable(  );

    eSetPollingThreadState( STOPPED );

    return 0;
}
Ejemplo n.º 17
0
static void
vTaskMODBUS( void *pvArg )
{
    const UCHAR     ucSlaveID[] = { 0xAA, 0xBB, 0xCC };
    eMBErrorCode    eStatus;

    for( ;; )
    {
        if( MB_ENOERR != ( eStatus = eMBInit( MB_ASCII, 0x0A, 1, 38400, MB_PAR_EVEN ) ) )
        {
            /* Can not initialize. Add error handling code here. */
        }
        else
        {
            if( MB_ENOERR != ( eStatus = eMBSetSlaveID( 0x34, TRUE, ucSlaveID, 3 ) ) )
            {
                /* Can not set slave id. Check arguments */
            }
            else if( MB_ENOERR != ( eStatus = eMBEnable(  ) ) )
            {
                /* Enable failed. */
            }
            else
            {
                usRegHoldingBuf[0] = 1;
                do
                {
                    ( void )eMBPoll(  );

                    /* Here we simply count the number of poll cycles. */
                    usRegInputBuf[0]++;
                }
                while( usRegHoldingBuf[0] );
            }
            ( void )eMBDisable(  );
            ( void )eMBClose(  );
        }
        vTaskDelay( 50 );
    }
}
Ejemplo n.º 18
0
Archivo: main.c Proyecto: Def152/AVR
/* ----------------------- Start implementation -----------------------------*/
int main( void )
{
    const UCHAR     ucSlaveID[] = { 0xAA, 0xBB, 0xCC };
    eMBErrorCode    eStatus;

    eStatus = eMBInit( MB_RTU, 0x0A, 0, 9600, MB_PAR_NONE ); // 0x0A slave address

    eStatus = eMBSetSlaveID( 0x34, TRUE, ucSlaveID, 3 );
    /* Enable the Modbus Protocol Stack. */
    eStatus = eMBEnable(  );



    sei(  );
    while(1)
    {
        ( void )eMBPoll(  );

        usRegInputBuf[0] = 1234;
        usRegHoldingBuf[0] = 5678;
    }
}
Ejemplo n.º 19
0
/* ----------------------- Start implementation -----------------------------*/
int
main( void )
{
    const UCHAR     ucSlaveID[] = { 0x01 };
    eMBErrorCode    eStatus;

    eStatus = eMBInit( MB_RTU, 0x01, 0, 38400, MB_PAR_EVEN );

    eStatus = eMBSetSlaveID(ucSlaveID[0], TRUE, ucSlaveID, 3 );
    sei(  );

    /* Enable the Modbus Protocol Stack. */
    eStatus = eMBEnable(  );

    for( ;; )
    {
        ( void )eMBPoll(  );

        /* Here we simply count the number of poll cycles. */
        usRegInputBuf[0]++;
    }
}
Ejemplo n.º 20
0
/* ----------------------- Start implementation -----------------------------*/
int
main( void )
{
    eMBErrorCode    eStatus;

    eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 9600, MB_PAR_NONE );

    /* Enable the Modbus Protocol Stack. */
    eStatus = eMBEnable(  );
    
    // Initialise some registers
    usRegInputBuf[1] = 0x1234;
    usRegInputBuf[2] = 0x5678;
    usRegInputBuf[3] = 0x9abc;        

    for( ;; )
    {
        ( void )eMBPoll(  );
                
        /* Here we simply count the number of poll cycles. */
        usRegInputBuf[0]++;
    }
}
Ejemplo n.º 21
0
static void
vModbusTask( void *pvParameters )
{
    int             i;

    /* Select either ASCII or RTU Mode. */
    ( void )eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_EVEN );

    /* Initialize the holding register values before starting the
     * Modbus stack. */
    for( i = 0; i < REG_HOLDING_NREGS; i++ )
    {
        usRegHoldingBuf[i] = ( unsigned short )i;
    }

    /* Enable the Modbus Protocol Stack. */
    ( void )eMBEnable(  );
    for( ;; )
    {
        /* Call the main polling loop of the Modbus protocol stack. */
        ( void )eMBPoll(  );
    }
}
int main(void)
{
	/*Ждем пока все включится*/
	_delay_ms(100);
	
	/*Настраиваем порты ввода-вывода*/
	DDRB = 1<<PORTB0|1<<PORTB1|1<<PORTB2|1<<PORTB3|1<<PORTB4|1<<PORTB5|1<<PORTB6|1<<PORTB7;
	DDRC = 1<<PORTC0|1<<PORTC1|1<<PORTC2|0<<PORTC3|0<<PORTC4|0<<PORTC5|0<<PORTC6|0<<PORTC7;
	DDRD = 0<<PORTD0|0<<PORTD1|0<<PORTD2|0<<PORTD3|1<<PORTD4|0<<PORTD5|0<<PORTD6|1<<PORTD7;

	PORTB = 1;
	PORTD = 1 << PORTD2;
    
	/*Тяга двигателей на минимум*/
    for(uint8_t k = 0; k < CHANNELS_COUNT; ++k)
    {
        counter[k] = LOW;
    }
	
	/*Настраиваем I2C*/
	TWSR = 0x00;
	TWBR = ((F_CPU / I2C_SPEED) - 16) / 2;  
	_delay_us(10);

	/*Включаем Таймер0*/
	TCCR0 = 1<<CS02 | 0<<CS01 | 0<<CS00;
	
	/*Включаем Таймер1*/
	OCR1A=HIGH; //TOP
	TCCR1A=0<<COM1A1|0<<COM1A0|1<<COM1B1|0<<COM1B0|0<<FOC1A|0<<FOC1B|1<<WGM11|1<<WGM10;
	TCCR1B=0<<ICNC1|0<<ICES1|1<<WGM13|1<<WGM12|0<<CS12|0<<CS11|1<<CS10;
	TIMSK= 1<<TOIE2 | 1<<OCIE1A|1<<OCIE1B|0<<TOIE1|1<<TOIE0|0<<OCIE0;
	OCR1B=LOW;
	
	/*Включаем АЦП*/
	ADC_Init();   
    
	/*Включаем прерывание INT0(высотомер)*/
	INT0_Init();
	
	/*Разрешаем работу прерываний*/
	sei();
	
	/*Настраиваем Modbus*/
	eMBErrorCode eStatus = eMBInit( MB_RTU, 0x01, 0, 57600, MB_PAR_NONE );
	eStatus = eMBEnable();
    
	/*Настраиваем сенсоры*/    
	SensorsInit();
	
	/*Загружаем в Holding Registers и в массив параметров значения из EEPROM*/
	ModbusInitValues();
	
	filterInit();	
		
	while(1)
	{
		/*Актуализируем значения Modbus-регистров в соответствии со значениями параметров*/
		ModbusLoader();
		/*Актуализируем значения параметров в соответствии со значениями Holding Registers*/
		ModbusSaver();
		
		/*Итерация Modbus*/
		eMBPoll();
		
		/*Ресурсоемкий расчет курса*/		
		Course_Calc();
	}
}
Ejemplo n.º 23
0
int main(void)
{
    systemInit();

    TIM_Cmd(TIM3, ENABLE);
    //启动FreeModbus
    eMBEnable();
    while(1)
    {
        //FreeModbus不断查询
        eMBPoll();
        //Botton_Control();

        //本地按键控制查询
        bottonValue=Botton_Scan(1);
        if(bottonValue)
        {
            bottonPressed=True;
            switch(bottonValue)
            {
            case BottonUp:
                bollardControlType=Control_Bollard_Up;
                break;
            case BottonDown:
                bollardControlType=Control_Bollard_Down;
                break;
            case BottonStop:
                bollardControlType=Control_Bollard_Stop;
                break;
            }
        }

        //限位到达查询
        limitValuePre=limitValue;
        limitValue=Limit_Scan();
        if((limitValue)&&(limitValue!=limitValuePre))
        {
            limitReach=True;
        }

        //升降柱状态查询
        bollardStatusPre=bollardStatus;
        bollardStatus=Bollard_Status_Scan();
        if(bollardStatus!=bollardStatusPre)
        {
            statusChange=True;
        }

        if(synchroOnOff==1)
        {
            //级联输入查询
            cascadeConnectionPre=cascadeConnection;
            cascadeConnection=Cascade_Connection_Scan();
            if(cascadeConnection&&(cascadeConnection!=cascadeConnectionPre))
            {
                cascadeConnectionAft=cascadeConnection;
                cascadeChange=True;
            }
        }

        if(groundCoilOnOff==1)
        {
            groundCoilStatus=Ground_Coil_Scan();
            if((bollardStatus!=Emergency) && groundCoilStatus && controlOn && (BollardControlUp==ControlEnable))
            {
                BollardControlUp=ControlDisable;
                controlOn=0;
                TIM_Cmd(TIM2,DISABLE);
                TIM_SetCounter(TIM2,0);//
                //event save

            }
        }

    }
}
Ejemplo n.º 24
0
void
main( void )
{
    eMBErrorCode    eStatus;

    /* Use external 32.768 Hz crystal to generate 4.194.304 Hz bus clock */
    ICGC1 = 0x38;               // ??=0,RANGE=0,REFS=1,CLKS=1:1,OSCSTEN=0,??=0:0
    while( ICGS2_DCOS == 0 );

#if 0
    /* Test code for porting
     */
#if 1
    /* Timer test
     * Comment out call to pxMBPortCBTimerExpired() in prvvTIMERExpiredISR when testing the timer
     */

    /* Disable the COP watchdog */
    SOPT = 0x53;                // COPE=0,COPT=1,STOPE=0,??=1:0:0,BKGDPE=1,??=1

    ( void )xMBPortTimersInit( 20000 );

    EnableInterrupts;
    for( ;; )
    {
        vMBPortTimersEnable(  );
        _Wait;                  // wait for an interrupt

        /* toggle LED1 */
        PTFD_PTFD0 = !PTFD_PTFD0;
        PTFDD_PTFDD0 = 1;
    }

#else
    /* Receiver test
     * Comment out call to pxMBFrameCBByteReceived() in prvvUARTRxISR() when testing the receiver
     */

    /* Disable the COP watchdog */
    SOPT = 0x53;                // COPE=0,COPT=1,STOPE=0,??=1:0:0,BKGDPE=1,??=1

    /* Enable the receiver. */
    assert( xMBPortSerialInit( 0, 9600, 8, MB_PAR_NONE ) );

    EnableInterrupts;
    for( ;; )
    {
        UCHAR           ucByte;

        vMBPortSerialEnable( TRUE, FALSE );
        _Wait;                  // wait for an interrupt
        assert( xMBPortSerialGetByte( &ucByte ) );

        /* toggle LED1 */
        PTFD_PTFD0 = !PTFD_PTFD0;
        PTFDD_PTFDD0 = 1;

        /* Transmitter test
         * Comment out call to pxMBFrameCBTransmitterEmpty() in prvvUARTTxReadyISR() when testing the transmitter
         */
#if 0
        vMBPortSerialEnable( FALSE, TRUE );
        assert( xMBPortSerialPutByte( ucByte ) );
        _Wait;                  // wait for an interrupt

        /* toggle LED1 */
        PTFD_PTFD0 = !PTFD_PTFD0;
#endif // Transmitter test
    }
#endif // Receiver test

#else
    /* Demo
     * NOTE: Make sure the callbacks in the three ISPs have been restored after above testing
     */

    /* Initialization */
    eStatus = eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_EVEN );
//      eStatus = eMBInit( MB_ASCII, 0x0A, 0, 38400, MB_PAR_EVEN );

    /* Enable the Modbus Protocol Stack. */
    eStatus = eMBEnable(  );

    /* Start polling */
    EnableInterrupts;
    for( ;; )
    {
        /* Poll for Modbus events */
        ( void )eMBPoll(  );

        /* Count the number of polls */
        usRegInputBuf[0]++;

        /* Count the number of timer overflows */
        if( TPM1SC_TOF )
        {
            TPM1SC_TOF = 0;
            ENTER_CRITICAL_SECTION(  );
            if( ++usRegInputBuf[1] == 0 )       // Happens every 2 seconds
                usRegInputBuf[2]++;     // Happens every 36.4 hours
            EXIT_CRITICAL_SECTION(  );
        }

        /* Keep the COP watchdog happy */
        __RESET_WATCHDOG(  );
    }
#endif // Test code when porting
}
Ejemplo n.º 25
0
void main(void)
{
    static DWORD currentTick = 0;
	static DWORD bsdTick = 0;
    static DWORD dwLastIP = 0;
	UINT8 i;

    // Initialize application specific hardware
    BRD_init();

    // Initialize stack-related hardware components that may be 
    // required by the UART configuration routines
    TickInit();
    #if defined(STACK_USE_MPFS2)
    MPFSInit();
    #endif

    // Initialize Stack and application related NV variables into AppConfig.
    InitAppConfig();

    // Initialize core stack layers (MAC, ARP, TCP, UDP) and
    // application modules (HTTP, SNMP, etc.)
    StackInit();

   

	#ifdef __MODBUS__
	APP_init( );
	UART_init( 62500 );
	#endif

	EnableInterrupts( );

/*	for( i = 0; i < 26; i++ )
	{
		UART_write( 'A' + i );
		UART_transmit();
	}*/



    // Now that all items are initialized, begin the co-operative
    // multitasking loop.  This infinite loop will continuously 
    // execute all stack-related tasks, as well as your own
    // application's functions.  Custom functions should be added
    // at the end of this loop.
    // Note that this is a "co-operative mult-tasking" mechanism
    // where every task performs its tasks (whether all in one shot
    // or part of it) and returns so that other tasks can do their
    // job.
    // If a task needs very lgong time to do its job, it must be broken
    // down into smaller pieces so that other tasks can have CPU time.
    while(1)
    {
        // Blink LED0 (right most one) every second.
        if(TickGet() - currentTick >= TICK_SECOND/2ul)
        {
            currentTick = TickGet();
//			COM_task( );
 //           LED0_IO ^= 1;
//			TxData = TRUE;
        }

        // This task performs normal stack task including checking
        // for incoming packet, type of packet and calling
        // appropriate stack entity to process it.
        StackTask();
        
      

        // This tasks invokes each of the core stack application tasks
        StackApplications();

        // Process application specific tasks here.
        // For this demo app, this will include the Generic TCP 
        // client and servers, and the SNMP, Ping, and SNMP Trap
        // demos.  Following that, we will process any IO from
        // the inputs on the board itself.
        // Any custom modules or processing you need to do should
        // go here.
  

        #if defined(STACK_USE_ICMP_CLIENT)
        PingDemo();
        #endif
     
        #if defined(STACK_USE_BERKELEY_API)
       // BerkeleyTCPClientDemo();
		if(TickGet() - bsdTick >= TICK_SECOND)
        {
			bsdTick = currentTick;
            BerkeleyTCPServerDemo();
        }
        
        //BerkeleyUDPClientDemo();
        #endif

      

		#ifdef __MODBUS__
		eMBPoll( );
		COM_task( );

		#endif
	
    }
}
Ejemplo n.º 26
0
/* Private function ----------------------------------------------------------*/
int main(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    uint8_t cnt=0;
    
    /* Set the Vector Table base adress at 0x8004000 */
    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x4000);    
    
    /* LED0 -> PB0     LED1 -> PB1  */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
    GPIO_Init(GPIOB, &GPIO_InitStructure);	
    
    // Initialize protocol stack in RTU mode for a slave with address 10 = 0x0A
	  // MB_RTU, Device ID: 1, USART portL: 1 (este configurat in portserial.h, Baud rate: 38400, Parity: NONE)
    eMBInit(MB_RTU, 1, 1, 38400, MB_PAR_NONE);
	  // Enable the Modbus Protocol Stack.
    eMBEnable();								

    while(1)
    {
        // Reset the flag. It will only be set if the modbus pooling has a request
		Modbus_Request_Flag = 0;
  		// Call the main polling loop of the Modbus protocol stack.
        eMBPoll();				        
  		if (Modbus_Request_Flag) GPIO_SetBits(GPIOB , GPIO_Pin_0);
		Delay(0xffff);
		if (Modbus_Request_Flag) GPIO_ResetBits(GPIOB , GPIO_Pin_0);
        
        
        cnt++;

        if (cnt == 4) \
        {            
            writeInputRegister(1, 111);
            writeInputRegister(2, 222);
            writeInputRegister(98, 111);
            writeInputRegister(99, 222);
                        
            writeHoldingRegister(1, 333);
            writeHoldingRegister(2, 444);
            writeHoldingRegister(98, 333);
            writeHoldingRegister(99, 444);
            
            writeCoil(1, 0);           
            writeCoil(2, 1);                       
            writeCoil(58, 1);
            writeCoil(59, 0);
        }
        
        if (cnt == 8) 
        {                      
            writeInputRegister(1, 222);
            writeInputRegister(2, 111);
            writeInputRegister(98, 222);
            writeInputRegister(99, 111);
                        
            writeHoldingRegister(1, 444);
            writeHoldingRegister(2, 333);
            writeHoldingRegister(98, 444);
            writeHoldingRegister(99, 333);
            
            writeCoil(1, 1);           
            writeCoil(2, 0);                       
            writeCoil(58, 0);
            writeCoil(59, 1);            
            
            cnt = 0;
        }
        
        
								   
    }
}