examples/AppliSlave_HCS12/appli.c
author etisserant
Thu, 10 Aug 2006 17:38:53 +0200
changeset 45 45885c4e3e56
parent 0 4472ee7c6c3e
permissions -rwxr-xr-x
Updated doc
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 <stddef.h> /* for NULL */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    24
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    25
#include <asm-m68hc12/portsaccess.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    26
#include <asm-m68hc12/ports_def.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    27
#include <asm-m68hc12/ports.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    28
#include  <interrupt.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 <candriver.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    32
#include <timerhw.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    33
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    34
#include "../include/def.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    35
#include "../include/can.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    36
#include "../include/objdictdef.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    37
#include "../include/objacces.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    38
#include "../include/canOpenDriver.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    39
#include "../include/sdo.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    40
#include "../include/pdo.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    41
#include "../include/init.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    42
#include "../include/timer.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    43
#include "../include/lifegrd.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    44
#include "../include/sync.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    45
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    46
#include "../include/nmtSlave.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    47
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    48
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    49
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    50
// HCS12 configuration
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    51
// -----------------------------------------------------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    52
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    53
enum E_CanBaudrate 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    54
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    55
   CAN_BAUDRATE_250K,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    56
   CAN_BAUDRATE_500K,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    57
   CAN_BAUDRATE_1M,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    58
   CAN_BAUDRATE_OLD_VALUE
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    59
};
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    60
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    61
const canBusTime CAN_Baudrates[] =
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    62
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    63
   {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    64
      1,  /* clksrc: Use the bus clock : 16 MHz, the freq. of the quartz's board        */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    65
      3,  /* brp :  chose btw 0 and 63 (6 bits).  freq time quantum = 16MHz / (brp + 1) */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    66
      0,  /* sjw : chose btw 0 and 3 (2 bits). Sync on (sjw + 1 ) time quantum          */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    67
      0,  /* samp : chose btw 0 and 3 (2 bits) (samp + 1 ) samples per bit              */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    68
      1,  /* tseg2 : chose btw 0 and 7 (3 bits) Segment 2 width = (tseg2 + 1)  tq       */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    69
     12,  /* tseg1 : chose btw 0 and 15 (4 bits) Segment 1 width = (tseg1 + 1)  tq      */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    70
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    71
      /*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    72
      With these values, 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    73
      - The width of the bit time is 16 time quantum :
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    74
          - 1 tq for the SYNC segment (could not be modified)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    75
          - 13 tq for the TIME 1 segment (tseg1 = 12)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    76
          - 2 tq for the TIME 2 segment (tseg2 = 1)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    77
      - Because the bus clock of the MSCAN is 16 MHZ, and the 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    78
        freq of the time quantum is 4 MHZ (brp = 3+1), and  there are 16 tq in the bit time,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    79
        so the freq of the bit time is 250 kHz.
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
   {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    84
      1,  /* clksrc: Use the bus clock : 16 MHz, the freq. of the quartz's board        */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    85
      1,  /* brp :  chose btw 0 and 63 (6 bits).  freq time quantum = 16MHz / (brp + 1) */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    86
      0,  /* sjw : chose btw 0 and 3 (2 bits). Sync on (sjw + 1 ) time quantum          */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    87
      0,  /* samp : chose btw 0 and 3 (2 bits) (samp + 1 ) samples per bit              */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    88
      1,  /* tseg2 : chose btw 0 and 7 (3 bits) Segment 2 width = (tseg2 + 1)  tq       */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    89
     12,  /* tseg1 : chose btw 0 and 15 (4 bits) Segment 1 width = (tseg1 + 1)  tq      */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    90
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    91
      /*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    92
      With these values, 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    93
      - The width of the bit time is 16 time quantum :
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    94
          - 1 tq for the SYNC segment (could not be modified)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    95
          - 13 tq for the TIME 1 segment (tseg1 = 12)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    96
          - 2 tq for the TIME 2 segment (tseg2 = 1)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    97
      - Because the bus clock of the MSCAN is 16 MHZ, and the 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    98
        freq of the time quantum is 8 MHZ (brp = 1+1), and  there are 16 tq in the bit time,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    99
        so the freq of the bit time is 500 kHz.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   100
      */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   101
    },
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   102
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   103
	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   104
      1,  /* clksrc: Use the bus clock : 16 MHz, the freq. of the quartz's board        */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   105
      1,  /* brp :  chose btw 0 and 63 (6 bits).  freq time quantum = 16MHz / (brp + 1) */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   106
      0,  /* sjw : chose btw 0 and 3 (2 bits). Sync on (sjw + 1 ) time quantum          */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   107
      0,  /* samp : chose btw 0 and 3 (2 bits) (samp + 1 ) samples per bit              */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   108
      1,  /* tseg2 : chose btw 0 and 7 (3 bits) Segment 2 width = (tseg2 + 1)  tq       */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   109
      4,  /* tseg1 : chose btw 0 and 15 (4 bits) Segment 1 width = (tseg1 + 1)  tq      */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   110
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   111
      /*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   112
      With these values, 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   113
      - The width of the bit time is 16 time quantum :
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   114
          - 1 tq for the SYNC segment (could not be modified)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   115
          - 5 tq for the TIME 1 segment (tseg1 = 4)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   116
          - 2 tq for the TIME 2 segment (tseg2 = 1)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   117
      - Because the bus clock of the MSCAN is 16 MHZ, and the 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   118
        freq of the time quantum is 8 MHZ (brp = 1+1), and  there are 8 tq in the bit time,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   119
        so the freq of the bit time is 1 MHz.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   120
      */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   121
    },
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   122
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   123
	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   124
      1,  /* clksrc: Use the bus clock : 16 MHz, the freq. of the quartz's board        */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   125
      0,  /* brp :  chose btw 0 and 63 (6 bits).  freq time quantum = 16MHz / (brp + 1) */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   126
      1,  /* sjw : chose btw 0 and 3 (2 bits). Sync on (sjw + 1 ) time quantum          */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   127
      1,  /* samp : chose btw 0 and 3 (2 bits) (samp + 1 ) samples per bit              */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   128
      4,  /* tseg2 : chose btw 0 and 7 (3 bits) Segment 2 width = (tseg2 + 1)  tq       */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   129
      9,  /* tseg1 : chose btw 0 and 15 (4 bits) Segment 1 width = (tseg1 + 1)  tq      */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   130
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   131
      /*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   132
      With these values, 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   133
      - The width of the bit time is 16 time quantum :
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   134
          - 1 tq for the SYNC segment (could not be modified)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   135
          - 10 tq for the TIME 1 segment (tseg1 = 9)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   136
          - 5 tq for the TIME 2 segment (tseg2 = 4)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   137
      - Because the bus clock of the MSCAN is 16 MHZ, and the 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   138
        freq of the time quantum is 16 MHZ (brp = 0), and  there are 16 tq in the bit time,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   139
        so the freq of the bit time is 1 MHz.
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
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   145
// The variables sent or updated by PDO
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   146
// -----------------------------------------------------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   147
extern UNS8 seconds;		// Mapped at index 0x2000, subindex 0x1
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   148
extern UNS8 minutes;		// Mapped at index 0x2000, subindex 0x2
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   149
extern UNS8 hours;		// Mapped at index 0x2000, subindex 0x3
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   150
extern UNS8 day;		// Mapped at index 0x2000, subindex 0x4
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   151
extern UNS32 canopenErrNB;	// Mapped at index 0x6000, subindex 0x0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   152
extern UNS32 canopenErrVAL;	// Mapped at index 0x6001, subindex 0x0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   153
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   154
// Required definition variables
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   155
// -----------------------------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   156
// The variables that you should define for debugging.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   157
// They are used by the macro MSG_ERR and MSG_WAR in applicfg.h
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   158
// if the node is a slave, they can be mapped in the object dictionnary.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   159
// if not null, allow the printing of message to the console
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   160
// Could be managed by PDO
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   161
UNS8 printMsgErrToConsole = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   162
UNS8 printMsgWarToConsole = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   163
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   164
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   165
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   166
/*************************User's variables declaration**************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   167
UNS8 softCount = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   168
UNS8 lastMinute = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   169
UNS8 lastSecond = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   170
UNS8 sendingError = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   171
//--------------------------------FONCTIONS-------------------------------------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   172
/* You *must* have these 2 functions in your code*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   173
void heartbeatError(UNS8 heartbeatID);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   174
void SD0timeoutError(UNS8 bus_id, UNS8 line);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   175
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   176
// Interruption timer 3. (The timer 4 is used by CanOpen)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   177
void __attribute__((interrupt)) timer3Hdl (void);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   178
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   179
void incDate(void);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   180
void initLeds(void);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   181
void initTimerClk(void);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   182
void initCanHCS12 (void);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   183
void initialisation(void);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   184
void preOperational(void);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   185
void operational(void);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   186
void stopped(void);
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
// Interruption timer 3
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   192
void __attribute__((interrupt)) timer3Hdl (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   193
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   194
  //IO_PORTS_8(PORTB) ^= 0x10;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   195
  //IO_PORTS_8(PORTB) &= ~0x20;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   196
  IO_PORTS_8(TFLG1) = 0x08; // RAZ du flag interruption timer 3
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   197
  // Calcul evt suivant. Clock 8 MHz -> 8000 evt de 1 ms!! Doit tenir sur 16 bits
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   198
  // Attention, ça change si on utilise la pll
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   199
  // Lorsque le timer atteindra la valeur de TC3 (16 bits), l'interruption timer3Hdl sera déclenchée
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   200
  // Si on utilise la PLL à 24 MHZ, alors la vitesse du bus est multipliée par 3.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   201
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   202
/*   Assume that our board uses a 16 MHz quartz */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   203
/*   Without pre-division, 8000 counts takes 1 ms. */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   204
/*   We are using a pre-divisor of 32. (register TSCR2) See in CanOpenDriverHC12/timerhw.c  */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   205
/*   So 10000 counts takes 40 ms. */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   206
/*   We must have a soft counter of 25 to count a second. */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   207
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   208
  IO_PORTS_16(TC3H) += (10000); // IT every 40000 count.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   209
  softCount++;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   210
  if (softCount == 25) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   211
    softCount = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   212
    incDate();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   213
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   214
}
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
void heartbeatError(UNS8 heartbeatID)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   218
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   219
  MSG_ERR(0x1F00, "!!! No heart beat received from node : ", heartbeatID);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   220
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   221
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   222
//------------------------------------------------------------------------------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   223
void SD0timeoutError (UNS8 bus_id, UNS8 line)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   224
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   225
  // Informations on what occurs are in transfers[bus_id][line]....
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   226
  // See scanSDOtimeout() in sdo.c
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   227
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   228
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   229
//------------------------------------------------------------------------------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   230
// Incrementation of the date, every second
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   231
void incDate(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   232
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   233
  if (seconds == 59) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   234
    seconds = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   235
    if (minutes == 59) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   236
      minutes = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   237
      if (hours == 23) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   238
	hours = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   239
	day++;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   240
      } 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   241
      else
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   242
	hours++;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   243
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   244
    else 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   245
      minutes++;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   246
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   247
  else
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   248
    seconds++;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   249
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   250
  // Toggle the led 4 every seconds
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   251
  IO_PORTS_8(PORTB) ^= 0x10;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   252
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   253
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   254
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   255
//Initialisation of the port B for the leds.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   256
void initLeds(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   257
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   258
  // Port B is output
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   259
  IO_PORTS_8(DDRB)= 0XFF;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   260
  // RAZ
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   261
  IO_PORTS_8(PORTB) = 0xFF;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   262
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   263
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   264
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   265
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   266
//------------------------------------------------------------------------------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   267
// Init the timer for the clock demo
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   268
void initTimerClk(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   269
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   270
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   271
  lock();   // Inhibe les interruptions
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   272
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   273
  // Configuration du Channel 3
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   274
  IO_PORTS_8(TIOS) |= 0x08;     // Canal 3 en sortie
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   275
  IO_PORTS_8(TCTL2) &= ~(0xC0); // Canal 3 déconnecté du pin de sortie
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   276
  IO_PORTS_8(TIE) |= 0x08;      // Autorise interruption Canal 3
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   277
  IO_PORTS_8(TSCR1) |= 0x80;    // Mise en route du timer
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   278
  unlock(); // Autorise les interruptions
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   279
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   280
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   281
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   282
//------------------------------------------------------------------------------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   283
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   284
// A placer avant initTimer de la bibliothèque CanOpen
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   285
/* void initTimerbis(void) */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   286
/* {   */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   287
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   288
/*   lock();                      // Inhibe les interruptions */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   289
/*   // Configuration des IT Channels (0..3) */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   290
/*   IO_PORTS_8(TIOS)  &= 0xF0;  // Canals 0->3 en entrées.    */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   291
/*   IO_PORTS_8(TCTL4) &= 0XFD;  // Canal 0 détection sur front montant. */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   292
/*   IO_PORTS_8(TCTL4) |= 0X01;    */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   293
/*   IO_PORTS_8(TCTL4) &= 0XF7;  // Canal 1 détection sur front montant. */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   294
/*   IO_PORTS_8(TCTL4) |= 0X04; */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   295
/*   IO_PORTS_8(TCTL4) &= 0XDF;  // Canal 2 détection sur front montant. */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   296
/*   IO_PORTS_8(TCTL4) |= 0X10;   */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   297
/*   IO_PORTS_8(TCTL4) &= 0X7F;  // Canal 3 détection sur front montant. */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   298
/*   IO_PORTS_8(TCTL4) |= 0X40;        */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   299
/*   IO_PORTS_8(TSCR2) |= 0X05;  // Pre-scaler = 32.  */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   300
   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   301
/*   IO_PORTS_8(ICOVW) |= 0x0F;  // La sauvgrade des valeures de TC0 et TC0H  */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   302
/*                               // correspondant aux canals (0..3) jusqu'a la */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   303
/*                               // prochaine lecture dans ces registres.  */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   304
/*   MASK  = IO_PORTS_8(ICSYS); */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   305
/*   MASK &= 0xFE;               // Canals (0..3) en IC QUEUE MODE. */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   306
/*   MASK |= 0x08;               // Canals (0..3) : génére une interruption aprés  */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   307
/*                               // la capture de deux valeures du timer sur detection */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   308
/*                               // d'un front montant à l'entrée des canals (0..3). */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   309
/*   MASK |= 0x02;               */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   310
/*   IO_PORTS_8(ICSYS) = MASK; */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   311
/*   IO_PORTS_16(TC0HH);         // Vider le registre holding correspondant au canal0. */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   312
/*   IO_PORTS_8(TSCR1) |= 0x10;  // RAZ automatique des flags d'interruption aprés lecture */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   313
/*                               // dans les registres correspondant.   */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   314
                              
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   315
/*   IO_PORTS_8(TIE)   |= 0x0F;  // Autorise interruption Canals (0..3). */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   316
/*   IO_PORTS_8(TSCR2) |= 0X80;  // Autorise interruption sur l'Overflow.  */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   317
/*   unlock();                   // Autorise les interruptions  */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   318
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   319
/* } */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   320
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   321
//------------------------------------------------------------------------------
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   322
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   323
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   324
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   325
void initCanHCS12 (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   326
{  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   327
  //Init the HCS12 microcontroler for CanOpen 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   328
  initHCS12();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   329
   // Init the HCS12  CAN driver
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   330
  const canBusInit bi0 = {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   331
    0,    /* no low power                 */ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   332
    0,    /* no time stamp                */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   333
    1,    /* enable MSCAN                 */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   334
    0,    /* clock source : oscillator (In fact, it is not used)   */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   335
    0,    /* no loop back                 */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   336
    0,    /* no listen only               */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   337
    0,    /* no low pass filter for wk up */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   338
	CAN_Baudrates[CAN_BAUDRATE_250K],
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   339
    {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   340
      0x00,    /* Filter on 16 bits. See Motorola Block Guide V02.14 fig 4-3 */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   341
      0x00, 0xFF, /* filter 0 hight accept all msg      */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   342
      0x00, 0xFF, /* filter 0 low accept all msg        */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   343
      0x00, 0xFF, /* filter 1 hight filter all of  msg  */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   344
      0x00, 0xFF, /* filter 1 low filter all of  msg    */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   345
      0x00, 0xFF, /* filter 2 hight filter most of  msg */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   346
      0x00, 0xFF, /* filter 2 low filter most of  msg   */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   347
      0x00, 0xFF, /* filter 3 hight filter most of  msg */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   348
      0x00, 0xFF, /* filter 3 low filter most of  msg   */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   349
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   350
  };   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   351
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   352
  canInit(CANOPEN_LINE_NUMBER_USED, bi0);  //initialize filters...
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   353
  unlock(); // Allow interruptions
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   354
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   355
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   356
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   357
/*********************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   358
void initialisation( void )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   359
{ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   360
  //initcapteur();      //initialisation du capteur, timer, compteurs logiciels
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   361
  initCanHCS12();   //initialisation du bus Can
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   362
  MSG_WAR(0X3F05, "I am in INITIALISATION mode ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   363
  /* Defining the node Id */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   364
  setNodeId(0x05);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   365
  MSG_WAR(0x3F06, "My node ID is : ", getNodeId()); 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   366
  {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   367
    UNS8 *data;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   368
    UNS8 size;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   369
    UNS8 dataType;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   370
    // Manufacturer Device name (default = empty string)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   371
    getODentry(0x1008, 0x0, (void **)&data, &size, &dataType, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   372
    MSG_WAR(0x3F09, data, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   373
    // Manufacturer Hardware version. (default = compilation. date)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   374
    getODentry(0x1009, 0x0, (void **)&data, &size, &dataType, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   375
    MSG_WAR(0x3F09, data, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   376
    // Manufacturer Software version. (default = compilation. time)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   377
    getODentry(0x100A, 0x0, (void **)&data, &size, &dataType, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   378
    MSG_WAR(0x3F09, data, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   379
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   380
  initCANopenMain();    //initialisation du canopen 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   381
  heartbeatInit();      //initialisation du lifeguarding
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   382
  initResetMode();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   383
  initTimer();          //initialisation of the timer used by Canopen
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   384
  initTimerClk();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   385
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   386
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   387
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   388
/*********************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   389
void preOperational(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   390
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   391
  // Test if the heartBeat have been received. Send headbeat
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   392
  heartbeatMGR();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   393
  // Read message
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   394
  receiveMsgHandler(0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   395
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   396
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   397
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   398
/********************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   399
void operational( void )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   400
{ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   401
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   402
  // Init the errors
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   403
  canopenErrNB = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   404
  canopenErrVAL = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   405
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   406
  // Test if the heartBeat have been received. Send headbeat
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   407
  heartbeatMGR();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   408
  // Read message
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   409
  receiveMsgHandler(0); 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   410
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   411
  if (lastMinute != minutes) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   412
    MSG_WAR(0x3F00, "event : minutes change -> node decides to send it. Value : ", minutes);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   413
    sendPDOevent( 0, &minutes );
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   414
    lastMinute = minutes;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   415
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   416
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   417
  if (canopenErrNB == 0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   418
    sendingError = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   419
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   420
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   421
  if (lastSecond != seconds) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   422
    MSG_WAR (0x3F50, "Seconds = ", seconds);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   423
    if ((seconds == 50) && (sendingError == 0))
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   424
      {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   425
	MSG_ERR(0x1F55, "DEMO of ERROR. Sent by PDO. Value : ", 0xABCD);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   426
	sendingError = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   427
      }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   428
    
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   429
    if (canopenErrNB) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   430
      MSG_WAR(0x3F56, "ERROR nb : ",  canopenErrNB);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   431
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   432
    lastSecond = seconds;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   433
    
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   434
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   435
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   436
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   437
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   438
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   439
/*****************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   440
void stopped( void )
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   441
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   442
  heartbeatMGR();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   443
  // Read message
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   444
  receiveMsgHandler(0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   445
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   446
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   447
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   448
/*****************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   449
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   450
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   451
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   452
/********************************* MAIN ***************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   453
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   454
 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   455
int main ()
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   456
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   457
  e_nodeState lastState = Unknown_state;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   458
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   459
  /* CanOpen slave state machine         */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   460
  /* ------------------------------------*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   461
    
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   462
  while(1) { /* slave's state machine */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   463
      
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   464
    switch( getState() ) {				
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   465
    case Initialisation:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   466
      if (lastState != getState()) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   467
	initLeds();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   468
	IO_PORTS_8(PORTB) &= ~ 0x01; // led  0         : ON
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   469
	IO_PORTS_8(PORTB) |=   0x0E; // leds 1, 2, 3   : OFF
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   470
	MSG_WAR(0X3F10, "Entering in INITIALISATION mode ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   471
      }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   472
      initialisation();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   473
      /* change automatically into pre_operational state */ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   474
      lastState = Initialisation;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   475
      setState(Pre_operational);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   476
      break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   477
					
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   478
    case Pre_operational:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   479
      if (lastState != getState()) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   480
	IO_PORTS_8(PORTB) &= ~ 0x03; // leds 0, 1      : ON
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   481
	IO_PORTS_8(PORTB) |=   0x0C; // leds 2, 3      : OFF
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   482
	MSG_WAR(0X3F11, "Entering in PRE_OPERATIONAL mode ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   483
	initPreOperationalMode();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   484
      }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   485
      preOperational();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   486
      if (lastState == Initialisation)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   487
	slaveSendBootUp(0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   488
      lastState = Pre_operational;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   489
      break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   490
					
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   491
    case Operational:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   492
      if (lastState != getState()) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   493
	IO_PORTS_8(PORTB) &= ~ 0x07; // leds 0, 1, 2   : ON
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   494
	IO_PORTS_8(PORTB) |=   0x08; // leds 3         : OFF
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   495
	MSG_WAR(0X3F12, "Entering in OPERATIONAL mode ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   496
      }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   497
      operational();    
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   498
      lastState = Operational;	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   499
      break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   500
	  		
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   501
    case Stopped:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   502
      if (lastState != getState()) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   503
	IO_PORTS_8(PORTB) |=   0x0F; // leds 0, 1, 2, 3 : OFF
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   504
	MSG_WAR(0X3F13, "Entering in  STOPPED mode", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   505
      }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   506
      stopped();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   507
      lastState = Stopped;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   508
      break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   509
    }//end switch case	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   510
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   511
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   512
  return (0); 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   513
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   514