emNet Start ChibiOS (Sample)

From SEGGER Knowledge Base
Jump to navigation Jump to search
IP_Start_ChibiOS_Sample.c
Requires modifications No
Download IP_Start_ChibiOS_Sample.c

This Sample demonstrates the use of emNet (with ChibiOS), without any server or client program.

To ping the Target, use the command line:

    > ping <target-ip>

Where <target-ip> represents the IP address of the target, which depends on the configuration used in IP_X_Config().

Code

/*********************************************************************
*                   (c) SEGGER Microcontroller GmbH                  *
*                        The Embedded Experts                        *
*                           www.segger.com                           *
**********************************************************************

-------------------------- END-OF-HEADER -----------------------------

Purpose : Sample program for ChibiOS & emNet
          Demonstrates use of the IP stack without any server or
          client program.
          To ping the target, use the command line:
          > ping <target-ip>
          Where <target-ip> represents the IP address of the target,
          which depends on the configuration.
Notes   : For compatibility with interfaces that need to connect in
          any way this sample calls connect and disconnect routines
          that might not be needed in all cases.

          This sample can be used for Ethernet and dial-up interfaces
          and is configured to use the last registered interface as
          its main interface.
*/

#include "ch.h"
#include "BSP.h"
#include "IP.h"

/*********************************************************************
*
*       Configuration
*
**********************************************************************
*/

#define USE_RX_TASK  0  // 0: Packets are read in ISR, 1: Packets are read in a task of its own.

//
// Task priorities.
//
enum {
   TASK_PRIO_IP_TASK = 1  // Priority should be higher than all IP application tasks.
#if USE_RX_TASK
  ,TASK_PRIO_IP_RX_TASK   // Must be the highest priority of all IP related tasks.
#endif
};

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

static IP_HOOK_ON_STATE_CHANGE _StateChangeHook;
static int                     _IFaceId;

//
// Task stacks.
//
static THD_WORKING_AREA(_IPStack  , TASK_STACK_SIZE_IP_TASK);     // Stack of the IP_Task.
#if USE_RX_TASK
static THD_WORKING_AREA(_IPRxStack, TASK_STACK_SIZE_IP_RX_TASK);  // Stack of the IP_RxTask.
#endif

/*********************************************************************
*
*       Prototypes
*
**********************************************************************
*/
#ifdef __cplusplus
extern "C" {     /* Make sure we have C-declarations in C++ programs */
#endif
void MainTask(void);
#ifdef __cplusplus
}
#endif

/*********************************************************************
*
*       Local functions
*
**********************************************************************
*/

/*********************************************************************
*
*       _OnStateChange()
*
* Function description
*   Callback that will be notified once the state of an interface
*   changes.
*
* Parameters
*   IFaceId   : Zero-based interface index.
*   AdminState: Is this interface enabled ?
*   HWState   : Is this interface physically ready ?
*/
static void _OnStateChange(unsigned IFaceId, U8 AdminState, U8 HWState) {
  //
  // Check if this is a disconnect from the peer or a link down.
  // In this case call IP_Disconnect() to get into a known state.
  //
  if (((AdminState == IP_ADMIN_STATE_DOWN) && (HWState == 1)) ||  // Typical for dial-up connection e.g. PPP when closed from peer. Link up but app. closed.
      ((AdminState == IP_ADMIN_STATE_UP)   && (HWState == 0))) {  // Typical for any Ethernet connection e.g. PPPoE. App. opened but link down.
    IP_Disconnect(IFaceId);                                       // Disconnect the interface to a clean state.
  }
}

/*********************************************************************
*
*       _IP_Task()
*
* Function description
*   Wrapper for void functions to ignore the given parameter.
*
* Parameters
*   p: Parameter passed on thread creation.
*/
static void _IP_Task(void* p) {
  IP_USE_PARA(p);

  IP_Task();  // Is (by default) an endless loop by itself.
}

#if USE_RX_TASK
/*********************************************************************
*
*       _IP_RxTask()
*
* Function description
*   Wrapper for void functions to ignore the given parameter.
*
* Parameters
*   p: Parameter passed on thread creation.
*/
static void _IP_RxTask(void* p) {
  IP_USE_PARA(p);

  IP_RxTask();  // Is (by default) an endless loop by itself.
}
#endif

/*********************************************************************
*
*       Global functions
*
**********************************************************************
*/

/*********************************************************************
*
*       MainTask()
*
* Function description
*   Main task executed by the RTOS to create further resources and
*   running the main application.
*/
void MainTask(void) {
  IP_Init();
  _IFaceId = IP_INFO_GetNumInterfaces() - 1;                                                                         // Get the last registered interface ID as this is most likely the interface we want to use in this sample.
  chThdSetPriority(NORMALPRIO + TASK_PRIO_IP_TASK - 1);                                                              // For now, this task has highest prio except IP management tasks.
  (void)chThdCreateStatic(&_IPStack[0]  , sizeof(_IPStack)  , NORMALPRIO + TASK_PRIO_IP_TASK   , _IP_Task  , NULL);  // Start the IP_Task.
#if USE_RX_TASK
  (void)chThdCreateStatic(&_IPRxStack[0], sizeof(_IPRxStack), NORMALPRIO + TASK_PRIO_IP_RX_TASK, _IP_RxTask, NULL);  // Start the IP_RxTask, optional.
#endif
  IP_AddStateChangeHook(&_StateChangeHook, _OnStateChange);                                                          // Register hook to be notified on disconnects.
  IP_Connect(_IFaceId);                                                                                              // Connect the interface if necessary.
  chThdSetPriority(HIGHPRIO);                                                                                        // Now this task has highest prio for real-time application. This is only allowed when this task does not use blocking IP API after this point.
  while (IP_IFaceIsReadyEx(_IFaceId) == 0) {
    chThdSleep(50);
  }
  while (1) {
    BSP_ToggleLED(1);
    chThdSleep(200);
  }
}

/*************************** End of file ****************************/