examples/AppliSlave_Linux/appli.c
author frdupin
Wed, 13 Dec 2006 14:48:04 +0100
changeset 55 96173dd44f2c
parent 0 4472ee7c6c3e
permissions -rwxr-xr-x
unused variable deleted
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     1
/*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     3
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     5
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     6
See COPYING file for copyrights details.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     7
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    12
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    16
Lesser General Public License for more details.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    17
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    21
*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    22
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    23
#include <stdio.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    24
#include <string.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    25
#include <unistd.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    26
#include <stdlib.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    27
#include <time.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    28
#include <pthread.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    29
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    30
#include <applicfg.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    31
#include <timerhw.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    32
#include <linuxCan.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    33
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    34
#include "def.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    35
#include "can.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    36
#include "canOpenDriver.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    37
#include "sdo.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    38
#include "pdo.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    39
#include "init.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    40
#include "timer.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    41
#include "lifegrd.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    42
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    43
#include "nmtSlave.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    44
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    45
// Adlink 7841 or Peak PCI/CAN board
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    46
// ---------------------------------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    47
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    48
// Baudrate values for Peak board :
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    49
// CAN_BAUD_1M CAN_BAUD_500K CAN_BAUD_250K CAN_BAUD_125K CAN_BAUD_100K CAN_BAUD_50K
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    50
// CAN_BAUD_20K CAN_BAUD_10K CAN_BAUD_5K
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    51
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    52
#ifdef CAN_BAUD_250K
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    53
# define BAUDRATE CAN_BAUD_250K
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    54
#else
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    55
// Appli have been compiled for Adlink-arbraca. Baudrate not used
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    56
# define BAUDRATE 0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    57
#endif
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    58
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    59
s_BOARD board = {"1", BAUDRATE};
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    60
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    61
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    62
// The variables sent or updated by PDO
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    63
// -----------------------------------------------------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    64
extern UNS8 seconds;		// Mapped at index 0x2000, subindex 0x1
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    65
extern UNS8 minutes;		// Mapped at index 0x2000, subindex 0x2
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    66
extern UNS8 hours;		// Mapped at index 0x2000, subindex 0x3
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    67
extern UNS8 day;		// Mapped at index 0x2000, subindex 0x4
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    68
extern UNS32 canopenErrNB;	// Mapped at index 0x6000, subindex 0x0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    69
extern UNS32 canopenErrVAL;	// Mapped at index 0x6001, subindex 0x0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    70
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    71
// Required definition variables
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    72
// -----------------------------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    73
// The variables that you should define for debugging.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    74
// They are used by the macro MSG_ERR and MSG_WAR in applicfg.h
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    75
// if the node is a slave, they can be mapped in the object dictionnary.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    76
// if not null, allow the printing of message to the console
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    77
// Could be managed by PDO
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    78
UNS8 printMsgErrToConsole = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    79
UNS8 printMsgWarToConsole = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    80
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    81
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    82
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    83
/*************************User's variables declaration**************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    84
struct tm date;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    85
struct tm *ptrdate;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    86
struct tm date_prec;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    87
time_t  tme;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    88
UNS8 lastSecond;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    89
e_nodeState lastState;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    90
pthread_t threadRcvMsg;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    91
pthread_t threadHeartbeatAndSDO;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    92
UNS8 sendingError = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    93
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    94
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    95
/******************************prototypes*****************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    96
/* You *must* have these 2 functions in your code*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    97
void heartbeatError(UNS8 heartbeatID);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    98
void SD0timeoutError(UNS8 bus_id, UNS8 line);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    99
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   100
UNS8 scanSDOtimeout(void);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   101
void preOperational(void);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   102
void operational(void); 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   103
void stopped(void);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   104
void waitMessage(void);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   105
void waitMessage_heartbeat(void);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   106
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   107
/*********************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   108
void heartbeatError(UNS8 heartbeatID)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   109
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   110
  MSG_ERR(0x1F00, "!!! No heart beat received from node : ", heartbeatID);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   111
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   112
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   113
/*****************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   114
void SD0timeoutError (UNS8 bus_id, UNS8 line)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   115
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   116
  // Informations on what occurs are in transfers[bus_id][line]....
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   117
  // See scanSDOtimeout() in sdo.c
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   118
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   119
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   120
/*********************************** THREADS **********************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   121
//------------------------------------------------------------------------------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   122
// Wait for a received message
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   123
void waitMessage( void )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   124
{       
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   125
  while (1) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   126
    receiveMsgHandler(0); // blocked until new message
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   127
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   128
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   129
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   130
//------------------------------------------------------------------------------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   131
// Heartbeat Manager (sending and receiving) and test SDO timeout
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   132
void heartbeatAndSDO(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   133
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   134
  while (1) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   135
    heartbeatMGR();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   136
    // Check if some SDO response are missing
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   137
    scanSDOtimeout();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   138
    // Sleep 10 ms
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   139
    usleep(10000);	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   140
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   141
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   142
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   143
/*********************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   144
void preOperational( void )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   145
{	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   146
  /* Init some variables */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   147
  canopenErrNB = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   148
  canopenErrVAL = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   149
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   150
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   151
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   152
/********************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   153
void operational( void )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   154
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   155
  /* read systeme date */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   156
  tme = time (&tme);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   157
  ptrdate = gmtime(&tme);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   158
  date = *ptrdate;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   159
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   160
  /* Update the dictionary */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   161
  day = date.tm_mday;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   162
  hours = date.tm_hour;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   163
  minutes = date.tm_min;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   164
  seconds = date.tm_sec;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   165
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   166
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   167
  if ( date_prec.tm_min != date.tm_min ) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   168
    MSG_WAR(0x3F00, "event : minutes change -> node decides to send it. Value : ", date.tm_min);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   169
    sendPDOevent( 0, &minutes );
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   170
    date_prec = date;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   171
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   172
  if (canopenErrNB == 0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   173
    sendingError = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   174
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   175
  if (lastSecond != seconds) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   176
    MSG_WAR (0x3F50, "Seconds = ", seconds);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   177
    if ((seconds == 50) && (sendingError == 0))
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   178
      {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   179
	MSG_ERR(0x1F55, "DEMO of ERROR. Sent by PDO. Value : ", 0xABCD);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   180
	sendingError = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   181
      }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   182
    
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   183
    if (canopenErrNB) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   184
      MSG_WAR(0x3F56, "ERROR nb : ",  canopenErrNB);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   185
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   186
    lastSecond = seconds;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   187
    
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   188
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   189
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   190
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   191
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   192
/*****************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   193
void stopped( void )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   194
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   195
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   196
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   197
void help()
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   198
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   199
  printf("**************************************************************\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   200
  printf("*  AppliSlave                                                *\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   201
  printf("*                [-b b]                                      *\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   202
  printf("*                                                            *\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   203
  printf("*     b : bus [default 1]                                    *\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   204
  printf("*                                                            *\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   205
  printf("*  This exemple run AppliSlave on bus 0                      *\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   206
  printf("*   AppliSlave -b 0                                          *\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   207
  printf("*                                                            *\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   208
  printf("**************************************************************\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   209
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   210
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   211
/****************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   212
/***************************  MAIN  *****************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   213
/****************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   214
int main(int argc,char **argv)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   215
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   216
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   217
  HANDLE ok;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   218
  UNS32 *    pSize;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   219
  UNS32      size;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   220
  pSize = &size;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   221
  char c;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   222
  extern char *optarg;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   223
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   224
  while ((c = getopt(argc, argv, "-b:")) != EOF)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   225
  {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   226
    switch(c)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   227
    {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   228
      case 'b' :
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   229
        if (optarg[0] == 0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   230
        {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   231
          help();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   232
          exit(1);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   233
        }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   234
        board.busname = optarg;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   235
        break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   236
      default:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   237
        help();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   238
        exit(1);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   239
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   240
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   241
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   242
  // Global initialization before launching the threads. (also done in init mode.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   243
  /* Defining the node Id */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   244
  setNodeId(0x05);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   245
  MSG_WAR(0x3F06, "My node ID is : ", getNodeId()); 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   246
  initCANopenMain();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   247
  initTimer( );
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   248
  heartbeatInit();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   249
  initResetMode();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   250
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   251
  /* Launch the thread to receive the messages */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   252
  pthread_create( &threadRcvMsg, NULL, (void *)&waitMessage, NULL); 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   253
  /* Launch the thread to manage the heartbeat */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   254
  pthread_create( &threadHeartbeatAndSDO, NULL, (void *)&heartbeatAndSDO, NULL); 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   255
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   256
  /* open the communication with the board */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   257
  ok = f_can_open(& board);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   258
  if (ok == NULL) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   259
    MSG_ERR(0x1F02,"Unable to open the board", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   260
    MSG_ERR(0x1F03,"Edit includeMakefileLinux to verify that the application is configured for the good board", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   261
    exit (-1);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   262
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   263
  else {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   264
    MSG_WAR(0x3F03, "Board 1 opened ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   265
    /* slave's state initialization */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   266
    setState(Initialisation);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   267
    lastState = Unknown_state;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   268
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   269
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   270
    while(1) { /* slave's state machine */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   271
      switch( getState() ) {	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   272
      case Initialisation:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   273
	if (lastState != getState()) 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   274
	  MSG_WAR(0X3F05, "I am in INITIALISATION mode ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   275
        /* Defining the node Id */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   276
	setNodeId(0x05);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   277
	MSG_WAR(0x3F06, "My node ID is : ", getNodeId()); 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   278
	  // Node identity ?
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   279
	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   280
	  UNS8 *data;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   281
	  UNS8 size;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   282
	  UNS8 dataType;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   283
	  // Manufacturer Device name (default = empty string)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   284
	  getODentry(0x1008, 0x0, (void **)&data, &size, &dataType, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   285
	  MSG_WAR(0x3F09, data, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   286
	  // Manufacturer Hardware version. (default = compilation. date)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   287
	  getODentry(0x1009, 0x0, (void **)&data, &size, &dataType, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   288
	  MSG_WAR(0x3F09, data, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   289
	  // Manufacturer Software version. (default = compilation. time)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   290
	  getODentry(0x100A, 0x0, (void **)&data, &size, &dataType, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   291
	  MSG_WAR(0x3F09, data, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   292
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   293
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   294
	initCANopenMain();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   295
	initTimer( );
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   296
	heartbeatInit();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   297
	initResetMode();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   298
	/* the slave send an NMT trame to say to the master 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   299
	   that it is going to enter into operational state 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   300
	   In fact, you must send the boot-up when you are in 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   301
	   operational mode !
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   302
	*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   303
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   304
	/* change automatically into pre_operational state */ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   305
	lastState = getState();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   306
	setState(Pre_operational);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   307
	break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   308
					
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   309
      case Pre_operational:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   310
	if (lastState != getState())
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   311
	  {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   312
	  MSG_WAR(0X3F11, "I am in PRE_OPERATIONAL mode ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   313
	  // Some stuff to do when the node enter in pre-operational mode
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   314
	  initPreOperationalMode();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   315
	  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   316
	if (lastState == Initialisation)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   317
	  slaveSendBootUp(0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   318
	lastState = getState();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   319
	preOperational( );	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   320
	break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   321
					
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   322
      case Operational:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   323
	if (lastState != getState())
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   324
	  MSG_WAR(0X3F12, "I am in OPERATIONAL mode ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   325
	lastState = getState();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   326
	operational( );
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   327
	break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   328
			
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   329
      case Stopped:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   330
	if (lastState !=  getState())
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   331
	  MSG_WAR(0X3F13, "I am in STOPPED mode", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   332
	lastState = getState();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   333
	stopped( );
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   334
	break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   335
      }//end switch case
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   336
      // Sleep 10 ms
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   337
      usleep(10000);	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   338
    }//end while
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   339
  }//end else
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   340
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   341
 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   342
  return 0;	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   343
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   344