drivers/hcs12/canOpenDriver.c
author lbessard
Mon, 15 May 2006 08:02:47 +0200
changeset 5 e4365e7d47f0
parent 0 4472ee7c6c3e
child 7 126e37bcca0f
permissions -rw-r--r--
Bug on number in hexa computed by gen_cfile corrected
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
#define DEBUG_WAR_CONSOLE_ON
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    24
#define DEBUG_ERR_CONSOLE_ON
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    25
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    26
#include <stddef.h> /* for NULL */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    27
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    28
#include "../include/hcs12/asm-m68hc12/portsaccess.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    29
#include "../include/hcs12/asm-m68hc12/ports_def.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    30
#include "../include/hcs12/asm-m68hc12/ports.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    31
#include "../include/hcs12/applicfg.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    32
#include "../include/hcs12/candriver.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    33
#include "../include/hcs12/interrupt.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    34
#include "../include/hcs12/canOpenDriver.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    35
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    36
#include "../include/can.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    37
#include "../include/objdictdef.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    38
#include "../include/timer.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    39
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    40
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    41
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    42
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    43
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    44
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    45
volatile static Message stackMsgRcv[NB_LINE_CAN][MAX_STACK_MSG_RCV]; 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    46
volatile static t_pointerStack ptrMsgRcv[NB_LINE_CAN];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    47
 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    48
volatile static TIMEVAL last_time_set = TIMEVAL_MAX;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    49
static UNS8 timer_is_set = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    50
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    51
/* Prototypes */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    52
UNS8 f_can_receive(UNS8 notused, Message *m);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    53
UNS8 canSend(UNS8 notused, Message *m);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    54
void __attribute__((interrupt)) timer4Hdl (void);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    55
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    56
#define max(a,b) a>b?a:b
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    57
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    58
/******************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    59
void setTimer(TIMEVAL value)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    60
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    61
  IO_PORTS_16(TC4H) += value;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    62
  timer_is_set = 1; 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    63
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    64
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    65
/******************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    66
TIMEVAL getElapsedTime()
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    67
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    68
  return (IO_PORTS_16(TC4H) > last_time_set ? IO_PORTS_16(TC4H) - last_time_set : last_time_set - IO_PORTS_16(TC4H));
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    69
}
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
/******************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    73
void resetTimer(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    74
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    75
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    76
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    77
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    78
/******************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    79
void initTimer(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    80
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    81
  lock();   // Inhibition of interruptions
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    82
 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    83
  // Configure the timer channel 4
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    84
  IO_PORTS_8(TIOS) |= 0x10; // Canal 4 in output
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    85
  IO_PORTS_8(TCTL1) &= ~(0x01 + 0x02); // Canal 4 unconnected to pin output
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    86
  IO_PORTS_8(TIE) |= 0x10; // allow interruption channel 4
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    87
  IO_PORTS_8(TSCR2) |= 0X05; // Pre-scaler = 32 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    88
                             // If this value is changed, change must be done also
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    89
                             // in void __attribute__((interrupt)) timer4Hdl (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    90
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    91
  IO_PORTS_8(TSCR1) |= 0x80; // Start timer
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    92
  unlock(); // Allow interruptions
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
/******************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    96
void __attribute__((interrupt)) timer4Hdl (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    97
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    98
  lock();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    99
  last_time_set = IO_PORTS_16(TC4H);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   100
  IO_PORTS_8(TFLG1) = 0x10; // RAZ flag interruption timer channel 4
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   101
  // Compute the next event : When the timer reach the value of TC4, an interrupt is
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   102
  // started 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   103
  //IO_PORTS_16(TC4H) += 250; // To have an interruption every 1 ms
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   104
  //timerInterrupt(0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   105
  //MSG_WAR(0xFFFF, "timer4 IT", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   106
  {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   107
    //MSG_WAR(0xFFFF, "t4 ", IO_PORTS_16(TCNTH) - IO_PORTS_16(TC4H));
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   108
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   109
  TimeDispatch();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   110
  unlock();
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
/******************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   115
void initSCI_0(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   116
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   117
  IO_PORTS_16(SCI0 + SCIBDH) = 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   118
    ((1000000 / SERIAL_SCI0_BAUD_RATE) * BUS_CLOCK) >> 4 ;  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   119
  IO_PORTS_8(SCI0  + SCICR1) = 0;    // format 8N1
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   120
  IO_PORTS_8(SCI0  + SCICR2) = 0x08; // Transmit enable only
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
void initSCI_1(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   125
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   126
  IO_PORTS_16(SCI1 + SCIBDH) = 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   127
    ((1000000 / SERIAL_SCI1_BAUD_RATE) * BUS_CLOCK) >> 4 ;  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   128
  IO_PORTS_8(SCI1  + SCICR1) = 0;    // format 8N1
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   129
  IO_PORTS_8(SCI1  + SCICR2) = 0x08; // Transmit enable only
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
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   133
/******************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   134
char *
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   135
hex_convert (char *buf, unsigned long value, char lastCar)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   136
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   137
  //Thanks to Stéphane Carrez for this function
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   138
  char num[32];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   139
  int pos;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   140
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   141
  *buf++ = '0';
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   142
  *buf++ = 'x';
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   143
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   144
  pos = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   145
  while (value != 0) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   146
    char c = value & 0x0F;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   147
    num[pos++] = "0123456789ABCDEF"[(unsigned) c];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   148
    value = (value >> 4) & (0x0fffffffL);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   149
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   150
  if (pos == 0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   151
    num[pos++] = '0';
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   152
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   153
  while (--pos >= 0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   154
    *buf++ = num[pos];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   155
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   156
  *buf++ = lastCar;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   157
  *buf = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   158
  return buf;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   159
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   160
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   161
/******************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   162
void printSCI_str(char sci, const char * str) 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   163
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   164
  char i = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   165
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   166
  while ((*(str + i) != 0) && (i < 0xFF)) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   167
    if (*(str + i) == '\n')
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   168
      {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   169
	while ((IO_PORTS_8(sci + SCISR1) & 0X80) == 0); // wait if buffer not empty	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   170
	IO_PORTS_8(sci + SCIDRL) = 13; // return to start of line
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   171
      }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   172
    while ((IO_PORTS_8(sci + SCISR1) & 0X80) == 0); // wait if buffer not empty
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   173
    IO_PORTS_8(sci + SCIDRL) = *(str + i++);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   174
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   175
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   176
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   177
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   178
/******************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   179
void printSCI_nbr(char sci, unsigned long nbr, char lastCar) 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   180
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   181
  char strNbr[12];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   182
  hex_convert(strNbr, nbr, lastCar);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   183
  printSCI_str(sci, strNbr);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   184
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   185
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   186
/******************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   187
// PLL 24 MHZ if quartz on board is 16 MHZ
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   188
void initPLL(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   189
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   190
  IO_PORTS_8(CLKSEL) &= ~0x80; // unselect the PLL
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   191
  IO_PORTS_8(PLLCTL) |= 0X60;  // PLL ON and bandwidth auto
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   192
  IO_PORTS_8(SYNR) = 0x02;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   193
  IO_PORTS_8(REFDV) = 0x01;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   194
  while ((IO_PORTS_8(CRGFLG) & 0x08) == 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   195
  IO_PORTS_8(CLKSEL) |= 0x80;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   196
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   197
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   198
/******************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   199
void initHCS12(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   200
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   201
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   202
# ifdef USE_PLL
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   203
  MSG_WAR(0x3620, "Use the PLL ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   204
  initPLL();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   205
# endif
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   206
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   207
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   208
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   209
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   210
char canAddIdToFilter(UNS16 adrCAN, UNS8 nFilter, UNS16 id)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   211
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   212
  UNS8 fiMsb;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   213
  UNS8 fiLsb;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   214
  UNS8 idMsb = (UNS8) (id >> 3);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   215
  UNS8 idLsb = (UNS8) (id << 5);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   216
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   217
  if (! canTestInitMode(adrCAN)) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   218
    /* Error because not init mode */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   219
    MSG_WAR(0X2600, "Not in init mode ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   220
    return 1;   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   221
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   222
  switch (nFilter) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   223
    case 0:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   224
      nFilter = CANIDAR0; /* First  bank */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   225
      break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   226
    case 1:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   227
      nFilter = CANIDAR2; /* First  bank */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   228
      break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   229
    case 2:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   230
      nFilter = CANIDAR4; /* Second bank */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   231
      break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   232
    case 3:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   233
      nFilter = CANIDAR6; /* Second bank */   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   234
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   235
  if (! IO_PORTS_16(adrCAN + nFilter)) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   236
    /* if CANIDARx = 0 */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   237
    IO_PORTS_8(adrCAN + nFilter) = idMsb;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   238
    IO_PORTS_8(adrCAN + nFilter + 1) = idLsb;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   239
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   240
  fiMsb = IO_PORTS_8(adrCAN + nFilter) ^ idMsb;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   241
  fiLsb = IO_PORTS_8(adrCAN + nFilter + 1) ^ idLsb;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   242
  /* address of CANIDMRx */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   243
  IO_PORTS_8(adrCAN + nFilter + 4) = IO_PORTS_8(adrCAN + nFilter + 4) | fiMsb;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   244
  IO_PORTS_8(adrCAN + nFilter + 5) = IO_PORTS_8(adrCAN + nFilter + 5) | fiLsb;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   245
  IO_PORTS_8(adrCAN + nFilter + 5) |= 0x10; /* Not filtering on rtr value */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   246
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   247
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   248
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   249
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   250
char canEnable(UNS16 adrCAN)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   251
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   252
  /* Register CANCTL1
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   253
  bit 7 : 1 MSCAN enabled
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   254
  Other bits : default reset values
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   255
  */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   256
  IO_PORTS_8(adrCAN + CANCTL1) = 0X80;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   257
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   258
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   259
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   260
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   261
char canInit(UNS16 adrCAN, canBusInit bi)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   262
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   263
  /* If not in init mode, go to sleep before going in init mode*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   264
  if (! canTestInitMode(adrCAN)) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   265
    canSleepMode(adrCAN);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   266
    canInitMode(adrCAN); 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   267
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   268
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   269
  canEnable(adrCAN);                  /* Does nothing if already enable */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   270
     /* The most secure way to go in init mode : put before MSCAN in sleep mode */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   271
      //canSleepMode(adrCAN);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   272
     /* Put MSCAN in Init mode */ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   273
     //canInitMode(adrCAN);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   274
  canInitClock(adrCAN, bi.clk); 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   275
  /* Init CANCTL1 register. Must be in init mode */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   276
  IO_PORTS_8(adrCAN + CANCTL1) &=0xC4;// 0xCB; /* Clr the bits that may be modified */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   277
  IO_PORTS_8(adrCAN + CANCTL1) = (bi.cane << 7) | (bi.loopb << 5 ) | 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   278
    (bi.listen << 4) | (bi.wupm << 2);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   279
  /* Initialize the filters for received msgs */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   280
  /* We should decide to accept all the msgs  */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   281
  canInitFilter(adrCAN, bi.fi);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   282
  /* Before to modify CANCTL0, we must leave the init mode */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   283
  canInitModeQ(adrCAN);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   284
  /* Init CANCTL0 register. MSCAN must not be in init mode */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   285
  /* Do not change the value of wupe (should be 0) and slprq (should be 1) */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   286
  /* Do not change the value of initrq (should be 0) */	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   287
  /* rxfrm is cleared, mupe also (should be before)*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   288
    IO_PORTS_8(adrCAN + CANCTL0) &= 0x53; /* Clr the bits that may be modified */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   289
    IO_PORTS_8(adrCAN + CANCTL0) = (bi.cswai << 5) | (bi.time << 3);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   290
    IO_PORTS_8(adrCAN + CANRIER) = 0X01; /* Allow interruptions on receive */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   291
    IO_PORTS_8(adrCAN + CANTIER) = 0X00; /* disallow  interruptions on transmit */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   292
    canInitModeQ(adrCAN); /* Leave the init mode */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   293
    canSleepModeQ(adrCAN); /* Leave the sleep mode */    
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   294
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   295
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   296
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   297
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   298
char canInitClock(UNS16 adrCAN, canBusTime clk)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   299
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   300
  if (! canTestInitMode(adrCAN)) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   301
    /* Not in Init mode */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   302
    MSG_WAR(0X2601, "not in init mode ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   303
    return 1;   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   304
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   305
  /* Set or reset CLKSRC (register CANCTL1). Does not change the other bits*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   306
  clk.clksrc = clk.clksrc << 6;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   307
  IO_PORTS_8(adrCAN + CANCTL1) &= 0xBF;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   308
  IO_PORTS_8(adrCAN + CANCTL1) |= clk.clksrc;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   309
  /* Build the CANBTR0 register */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   310
  IO_PORTS_8(adrCAN + CANBTR0) = 0x00;        /* Clear before changes */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   311
  IO_PORTS_8(adrCAN + CANBTR0) = (clk.sjw << 6) | (clk.brp);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   312
  /* Build the CANBTR1 register */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   313
  IO_PORTS_8(adrCAN + CANBTR1) = 0x00;        /* Clear before changes */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   314
  IO_PORTS_8(adrCAN + CANBTR1) = (clk.samp << 7) | (clk.tseg2 << 4) | 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   315
    (clk.tseg1);  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   316
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   317
}
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
char canInit1Filter(UNS16 adrCAN, UNS8 nFilter, UNS16 ar, UNS16 mr)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   321
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   322
  if (! canTestInitMode(adrCAN)) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   323
    /* Error because not init mode */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   324
    MSG_WAR(0X2602, "not in init mode ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   325
    return 1;   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   326
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   327
  switch (nFilter) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   328
    case 0:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   329
      nFilter = CANIDAR0; /* First  bank */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   330
      break; 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   331
    case 1:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   332
      nFilter = CANIDAR2; /* First  bank */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   333
      break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   334
    case 2:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   335
      nFilter = CANIDAR4; /* Second bank */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   336
      break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   337
    case 3:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   338
      nFilter = CANIDAR6; /* Second bank */   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   339
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   340
  /* address of CANIDARx */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   341
  IO_PORTS_8(adrCAN + nFilter)     = (UNS8) (ar >> 8);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   342
  IO_PORTS_8(adrCAN + nFilter + 1) = (UNS8) (ar);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   343
  IO_PORTS_8(adrCAN + nFilter + 4) = (UNS8) (mr >> 8);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   344
  IO_PORTS_8(adrCAN + nFilter + 5) = (UNS8) (mr);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   345
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   346
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   347
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   348
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   349
char canInitFilter(UNS16 adrCAN, canBusFilterInit fi)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   350
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   351
if (! canTestInitMode(adrCAN)) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   352
    /* Error because not init mode */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   353
    MSG_WAR(0X2603, "not in init mode ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   354
    return 1;   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   355
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   356
  IO_PORTS_8(adrCAN + CANIDAC)  = fi.idam << 4;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   357
  IO_PORTS_8(adrCAN + CANIDAR0) = fi.canidar0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   358
  IO_PORTS_8(adrCAN + CANIDMR0) = fi.canidmr0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   359
  IO_PORTS_8(adrCAN + CANIDAR1) = fi.canidar1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   360
  IO_PORTS_8(adrCAN + CANIDMR1) = fi.canidmr1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   361
  IO_PORTS_8(adrCAN + CANIDAR2) = fi.canidar2;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   362
  IO_PORTS_8(adrCAN + CANIDMR2) = fi.canidmr2;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   363
  IO_PORTS_8(adrCAN + CANIDAR3) = fi.canidar3;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   364
  IO_PORTS_8(adrCAN + CANIDMR3) = fi.canidmr3;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   365
  IO_PORTS_8(adrCAN + CANIDAR4) = fi.canidar4;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   366
  IO_PORTS_8(adrCAN + CANIDMR4) = fi.canidmr4;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   367
  IO_PORTS_8(adrCAN + CANIDAR5) = fi.canidar5;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   368
  IO_PORTS_8(adrCAN + CANIDMR5) = fi.canidmr5;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   369
  IO_PORTS_8(adrCAN + CANIDAR6) = fi.canidar6;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   370
  IO_PORTS_8(adrCAN + CANIDMR6) = fi.canidmr6;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   371
  IO_PORTS_8(adrCAN + CANIDAR7) = fi.canidar7;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   372
  IO_PORTS_8(adrCAN + CANIDMR7) = fi.canidmr7;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   373
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   374
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   375
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   376
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   377
char canInitMode(UNS16 adrCAN)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   378
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   379
  IO_PORTS_8(adrCAN + CANCTL0) |= 0x01; /* Set the bit INITRQ */ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   380
  while (! canTestInitMode(adrCAN)) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   381
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   382
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   383
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   384
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   385
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   386
char canInitModeQ(UNS16 adrCAN)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   387
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   388
  IO_PORTS_8(adrCAN + CANCTL0) &= 0xFE; /* Clear the bit INITRQ */ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   389
  while (canTestInitMode(adrCAN)) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   390
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   391
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   392
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   393
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   394
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   395
char canMsgTransmit(UNS16 adrCAN, Message msg)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   396
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   397
  /* Remind : only CAN A msg implemented. ie id on 11 bits, not 29 */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   398
  UNS8 cantflg;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   399
  UNS8 i;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   400
  /* Looking for a free buffer */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   401
  cantflg = IO_PORTS_8(adrCAN + CANTFLG);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   402
  if ( cantflg == 0) { /* all the TXEx are set */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   403
    MSG_WAR(0X2604, "No buffer free. Msg to transmit is losted ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   404
    return 1; /* No buffer free */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   405
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   406
  else{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   407
    /* Selecting a buffer */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   408
    IO_PORTS_8(adrCAN + CANTBSEL) = cantflg;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   409
    /* We put ide = 0 because id is on 11 bits only */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   410
     IO_PORTS_8(adrCAN + CANTRSID) = (UNS8)(msg.cob_id.w >> 3);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   411
    IO_PORTS_8(adrCAN + CANTRSID + 1) = (UNS8)((msg.cob_id.w << 5)|
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   412
    (msg.rtr << 4));
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   413
   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   414
    IO_PORTS_8(adrCAN + CANTRSLEN) = msg.len & 0X0F;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   415
    /* For the priority, we put the highter bits of the cob_id */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   416
    IO_PORTS_8(adrCAN + CANTRSPRI) = IO_PORTS_8(adrCAN + CANTRSID);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   417
    for (i = 0 ; i < msg.len ; i++) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   418
      IO_PORTS_8(adrCAN + CANTRSDTA + i) = msg.data[i];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   419
    } 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   420
    /* Transmitting the message */ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   421
    cantflg = IO_PORTS_8(adrCAN + CANTBSEL);/* to know which buf is selected */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   422
    IO_PORTS_8(adrCAN + CANTBSEL) = 0x00;  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   423
    IO_PORTS_8(adrCAN + CANTFLG) = cantflg;  /* Ready to transmit ! */  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   424
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   425
 }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   426
  return 0;
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
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   430
char canSleepMode(UNS16 adrCAN)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   431
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   432
  IO_PORTS_8(adrCAN + CANCTL0) &= 0xFB;   /* clr the bit WUPE  to avoid a wake-up*/ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   433
  IO_PORTS_8(adrCAN + CANCTL0) |= 0x02;   /* Set the bit SLPRQ. go to Sleep !*/ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   434
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   435
   // IO_PORTS_8(adrCAN + CANCTL1) |= 0x04;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   436
   // IO_PORTS_8(adrCAN + CANCTL0) |= 0x02;   /* Set the bit SLPRQ */ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   437
   while ( ! canTestSleepMode(adrCAN)) {
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
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   441
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   442
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   443
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   444
char canSleepModeQ(UNS16 adrCAN)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   445
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   446
  if (canTestInitMode(adrCAN)) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   447
    /* Error because in init mode */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   448
    MSG_WAR(0X2606, "not in init mode ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   449
    return 1;   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   450
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   451
  IO_PORTS_8(adrCAN + CANCTL0) &= 0xFD;   /* clr the bit SLPRQ */ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   452
    while ( canTestSleepMode(adrCAN)) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   453
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   454
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   455
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   456
 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   457
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   458
char canSleepWupMode(UNS16 adrCAN)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   459
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   460
  if (canTestInitMode(adrCAN)) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   461
    MSG_WAR(0X2607, "not in init mode ", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   462
    return 1;   
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   463
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   464
  IO_PORTS_8(adrCAN + CANCTL0) |= 0x06;   /* Set the bits WUPE & SLPRQ */ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   465
  while ( ! canTestSleepMode(adrCAN)) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   466
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   467
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   468
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   469
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   470
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   471
char canTestInitMode(UNS16 adrCAN)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   472
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   473
  return IO_PORTS_8(adrCAN + CANCTL1) & 0x01; /* Test the bit INITAK */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   474
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   475
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   476
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   477
char canTestSleepMode(UNS16 adrCAN)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   478
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   479
  return IO_PORTS_8(adrCAN + CANCTL1) & 0x02; /* Test the bit SLPAK */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   480
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   481
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   482
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   483
UNS8 canSend(UNS8 notused, Message *m)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   484
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   485
  canMsgTransmit(CANOPEN_LINE_NUMBER_USED, *m);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   486
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   487
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   488
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   489
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   490
/**************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   491
UNS8 f_can_receive(UNS8 notused, Message *msgRcv)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   492
{ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   493
  UNS8 i, j;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   494
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   495
  switch (CANOPEN_LINE_NUMBER_USED) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   496
  case CAN0 : j = 0; break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   497
  case CAN1 : j = 1; break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   498
  case CAN2 : j = 2; break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   499
  case CAN3 : j = 3; break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   500
  case CAN4 : j = 4; break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   501
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   502
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   503
  /* See if a message is pending in the stack */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   504
  if (ptrMsgRcv[j].r == ptrMsgRcv[j].w)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   505
    return 0x0; // No new message
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   506
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   507
  /* Increment the reading pointer of the stack */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   508
  if (ptrMsgRcv[j].r == (MAX_STACK_MSG_RCV - 1)) 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   509
     ptrMsgRcv[j].r = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   510
  else
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   511
    ptrMsgRcv[j].r ++;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   512
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   513
  /* Store the message from the stack*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   514
  msgRcv->cob_id.w = stackMsgRcv[j][ptrMsgRcv[j].r].cob_id.w;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   515
  msgRcv->len = stackMsgRcv[j][ptrMsgRcv[j].r].len;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   516
  msgRcv->rtr = stackMsgRcv[j][ptrMsgRcv[j].r].rtr;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   517
  for (i = 0 ; i < stackMsgRcv[j][ptrMsgRcv[j].r].len ; i++)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   518
    msgRcv->data[i] = stackMsgRcv[j][ptrMsgRcv[j].r].data[i];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   519
  return 0xFF;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   520
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   521
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   522
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   523
/******************************************************************************
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   524
 ******************************* CAN INTERRUPT  *******************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   525
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   526
void __attribute__((interrupt)) can0HdlTra (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   527
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   528
 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   529
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   530
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   531
void __attribute__((interrupt)) can0HdlRcv (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   532
{ 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   533
  UNS8 i;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   534
  lock();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   535
  IO_PORTS_8(PORTB) &= ~ 0x40; // led 6 port B : ON
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   536
  UNS8 NewPtrW; 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   537
  /* We are obliged to save the message while the interruption is pending */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   538
  /* Increment the writing stack pointer before writing the msg */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   539
  if (ptrMsgRcv[0].w == (MAX_STACK_MSG_RCV - 1)) 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   540
    NewPtrW = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   541
  else
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   542
    NewPtrW = ptrMsgRcv[0].w + 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   543
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   544
  if (NewPtrW == ptrMsgRcv[0].r) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   545
    /* The stack is full. The last msg received before this one is lost */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   546
    MSG_WAR(0X1620, "Stack for received msg is full", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   547
    //IO_PORTS_8(PORTB) &= ~0x40; // led  6         : ON (for debogue)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   548
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   549
  else
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   550
    ptrMsgRcv[0].w = NewPtrW;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   551
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   552
  /* Store the message */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   553
  stackMsgRcv[0][ptrMsgRcv[0].w].cob_id.w = IO_PORTS_16(CAN0 + CANRCVID) >> 5;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   554
  stackMsgRcv[0][ptrMsgRcv[0].w].len = IO_PORTS_8(CAN0 + CANRCVLEN) & 0x0F;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   555
  stackMsgRcv[0][ptrMsgRcv[0].w].rtr = (IO_PORTS_8(CAN0 + CANRCVID + 1) >> 4) & 0x01;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   556
  for (i = 0 ; i < stackMsgRcv[0][ptrMsgRcv[0].w].len ; i++)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   557
    stackMsgRcv[0][ptrMsgRcv[0].w].data[i] = IO_PORTS_8(CAN0 + CANRCVDTA + i);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   558
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   559
  // The message is stored , so
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   560
  // we can now release the receive foreground buffer
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   561
  // and acknowledge the interruption
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   562
  IO_PORTS_8(CAN0 + CANRFLG) |= 0x01;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   563
  // Not very usefull
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   564
  IO_PORTS_8(CAN0 + CANCTL0) |= 0x80;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   565
  IO_PORTS_8(PORTB) |= 0x40; // led 6 port B : ON
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   566
  unlock();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   567
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   568
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   569
void __attribute__((interrupt)) can0HdlWup (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   570
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   571
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   572
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   573
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   574
void __attribute__((interrupt)) can0HdlErr (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   575
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   576
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   577
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   578
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   579
void __attribute__((interrupt)) can1HdlTra (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   580
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   581
 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   582
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   583
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   584
void __attribute__((interrupt)) can1HdlRcv (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   585
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   586
  UNS8 i;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   587
  lock();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   588
  UNS8 NewPtrW; 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   589
  /* We are obliged to save the message while the interruption is pending */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   590
  /* Increment the writing stack pointer before writing the msg */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   591
  if (ptrMsgRcv[1].w == (MAX_STACK_MSG_RCV - 1)) 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   592
    NewPtrW = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   593
  else
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   594
    NewPtrW = ptrMsgRcv[1].w + 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   595
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   596
  if (NewPtrW == ptrMsgRcv[1].r) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   597
    /* The stack is full. The last msg received before this one is lost */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   598
    MSG_WAR(0X2620, "Stack for received msg is full", 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   599
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   600
  else
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   601
    ptrMsgRcv[1].w = NewPtrW;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   602
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   603
  /* Store the message */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   604
  stackMsgRcv[1][ptrMsgRcv[1].w].cob_id.w = IO_PORTS_16(CAN1 + CANRCVID) >> 5;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   605
  stackMsgRcv[1][ptrMsgRcv[1].w].len = IO_PORTS_8(CAN1 + CANRCVLEN) & 0x0F;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   606
  stackMsgRcv[0][ptrMsgRcv[0].w].rtr = (IO_PORTS_8(CAN1 + CANRCVID + 1) >> 4) & 0x01;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   607
  for (i = 0 ; i < stackMsgRcv[1][ptrMsgRcv[1].w].len ; i++)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   608
    stackMsgRcv[1][ptrMsgRcv[1].w].data[i] = IO_PORTS_8(CAN1 + CANRCVDTA + i);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   609
  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   610
  // The message is stored , so
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   611
  // we can now release the receive foreground buffer
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   612
  // and acknowledge the interruption
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   613
  IO_PORTS_8(CAN1 + CANRFLG) |= 0x01;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   614
  // Not very usefull
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   615
  IO_PORTS_8(CAN1 + CANCTL0) |= 0x80;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   616
  unlock();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   617
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   618
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   619
void __attribute__((interrupt)) can1HdlWup (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   620
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   621
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   622
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   623
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   624
void __attribute__((interrupt)) can1HdlErr (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   625
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   626
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   627
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   628
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   629
void __attribute__((interrupt)) can2HdlTra (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   630
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   631
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   632
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   633
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   634
void __attribute__((interrupt)) can2HdlRcv (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   635
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   636
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   637
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   638
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   639
void __attribute__((interrupt)) can2HdlWup (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   640
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   641
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   642
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   643
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   644
void __attribute__((interrupt)) can2HdlErr (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   645
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   646
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   647
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   648
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   649
void __attribute__((interrupt)) can3HdlTra (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   650
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   651
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   652
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   653
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   654
void __attribute__((interrupt)) can3HdlRcv (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   655
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   656
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   657
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   658
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   659
void __attribute__((interrupt)) can3HdlWup (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   660
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   661
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   662
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   663
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   664
void __attribute__((interrupt)) can3HdlErr (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   665
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   666
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   667
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   668
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   669
void __attribute__((interrupt)) can4HdlTra (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   670
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   671
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   672
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   673
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   674
void __attribute__((interrupt)) can4HdlRcv (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   675
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   676
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   677
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   678
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   679
void __attribute__((interrupt)) can4HdlWup (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   680
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   681
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   682
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   683
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   684
void __attribute__((interrupt)) can4HdlErr (void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   685
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   686
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   687
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   688
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   689
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   690