examples/AVR/Slave/main.c
author edouard
Thu, 24 Sep 2009 10:14:59 +0200
changeset 595 3a9b09a0dd06
parent 525 9b81decd4662
child 675 e5c5101c4f0b
permissions -rw-r--r--
Updated memento with new Francis' version
413
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
     1
/*
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
     3
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
     5
AVR Port: Andreas GLAUSER and Peter CHRISTEN
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
     6
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
     7
See COPYING file for copyrights details.
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
     8
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
     9
This library is free software; you can redistribute it and/or
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    10
modify it under the terms of the GNU Lesser General Public
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    11
License as published by the Free Software Foundation; either
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    12
version 2.1 of the License, or (at your option) any later version.
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    13
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    14
This library is distributed in the hope that it will be useful,
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    15
but WITHOUT ANY WARRANTY; without even the implied warranty of
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    17
Lesser General Public License for more details.
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    18
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    19
You should have received a copy of the GNU Lesser General Public
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    20
License along with this library; if not, write to the Free Software
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    21
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    22
*/
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    23
/******************************************************************************
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    24
Project description:
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    25
Test projekt for a DS 401 slave, running on Atmel's STK500 with AT90CAN128
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    26
Short description:
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    27
  PORTA:	Inputs (Keys, low active)
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    28
  PORTB:	Outputs (LEDs, low active)
525
peter
parents: 413
diff changeset
    29
  PORTC:	Node ID (1 BCD switch, low active)
413
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    30
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    31
******************************************************************************/
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    32
#include "hardware.h"
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    33
#include "canfestival.h"
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    34
#include "can_AVR.h"
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    35
#include "objdict.h"
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    36
#include "ds401.h"
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    37
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    38
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    39
unsigned char timer_interrupt = 0;		// Set if timer interrupt eclapsed
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    40
unsigned char inputs;
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    41
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    42
// CAN
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    43
unsigned char nodeID;
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    44
unsigned char digital_input[1] = {0};
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    45
unsigned char digital_output[1] = {0};
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    46
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    47
static Message m = Message_Initializer;		// contain a CAN message
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    48
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    49
void sys_init();
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    50
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    51
// macros to handle the schedule timer
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    52
#define sys_timer			timer_interrupt
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    53
#define reset_sys_timer()		timer_interrupt = 0
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    54
#define CYCLE_TIME	        	1000     	// Sample Timebase [us]
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    55
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    56
int main(void)
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    57
{
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    58
  sys_init();                                   // Initialize system
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    59
  canInit(CAN_BAUDRATE);         		// Initialize the CANopen bus
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    60
  initTimer();                                 	// Start timer for the CANopen stack
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    61
  nodeID = read_bcd();				// Read node ID first
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    62
  setNodeId (&ObjDict_Data, nodeID);
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    63
  setState(&ObjDict_Data, Initialisation);	// Init the state
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    64
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    65
  for(;;)		                        // forever loop
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    66
  {
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    67
    if (sys_timer)	                        // Cycle timer, invoke action on every time slice
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    68
    {
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    69
      reset_sys_timer();	                // Reset timer
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    70
      digital_input[0] = get_inputs();
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    71
      digital_input_handler(&ObjDict_Data, digital_input, sizeof(digital_input));
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    72
      digital_output_handler(&ObjDict_Data, digital_output, sizeof(digital_output));
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    73
      set_outputs(digital_output[0]);
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    74
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    75
      // Check if CAN address has been changed
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    76
      if(!( nodeID == read_bcd()))
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    77
      {
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    78
        nodeID = read_bcd();                    // Save the new CAN adress
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    79
        setState(&ObjDict_Data, Stopped);         // Stop the node, to change the node ID
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    80
        setNodeId(&ObjDict_Data, nodeID);         // Now the CAN adress is changed
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    81
        setState(&ObjDict_Data, Pre_operational); // Set to Pre_operational, master must boot it again
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    82
      }
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    83
    }
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    84
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    85
    // a message was received pass it to the CANstack
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    86
    if (canReceive(&m))			// a message reveived
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    87
      canDispatch(&ObjDict_Data, &m);         // process it
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    88
    else
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    89
    {
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    90
      // Enter sleep mode
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    91
      #ifdef WD_SLEEP		// Watchdog and Sleep
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    92
      wdt_reset();
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    93
      sleep_enable();
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    94
      sleep_cpu();
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    95
      #endif				// Watchdog and Sleep
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    96
    }
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    97
  }
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    98
}
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    99
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   100
void sys_init()
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   101
/******************************************************************************
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   102
Initialize the relays, the main states and the modbus protocol stack.
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   103
INPUT	LOCK_STATES *lock_states
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   104
OUTPUT	void
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   105
******************************************************************************/
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   106
{
525
peter
parents: 413
diff changeset
   107
  OSCCAL = 0x43;                        // adjust the RC oszillator
413
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   108
  
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   109
  PORTA = 0xFF;	                        // Inputs (Keys, low active) with pullup
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   110
  DDRA  = 0x00;		                // 
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   111
  PORTB = 0xFF;	                        // Outputs (LEDs, low active) all 1
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   112
  DDRB  = 0xFF;		                // 
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   113
  PORTC = 0xFF;	                        // 1 BCD switch with pullup
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   114
  DDRC  = 0x00;		                // 
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   115
  PORTD = 0x2C;	                        // 2xCOM, unused, CAN, unused
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   116
  DDRD  = 0x2A;		                // All init 0 or without pullup
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   117
  PORTE = 0x00;	                        // Output
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   118
  DDRE  = 0x3C;		                // 2x not used, 2x not used
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   119
  PORTF = 0x00;	                        // Not used
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   120
  DDRF  = 0xFF;		                // All output
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   121
  PORTG = 0x00;	                        // Not used
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   122
  DDRG  = 0x1F;		                // Output for debug (only 5 pins)
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   123
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   124
// Set timer 0 for main schedule time
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   125
  TCCR0A |= 1 << WGM01 | 1 << CS01 | 1 << CS00;// Timer 0 CTC , Timer 0 mit CK/64 starten
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   126
  TIMSK0 = 1 << OCIE0A;		        // Timer Interrupts: Timer 0 Compare
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   127
  OCR0A = (unsigned char)(F_CPU / 64 * CYCLE_TIME/1000000 - 1);	// Reloadvalue for timer 0
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   128
  #ifdef WD_SLEEP		// Watchdog and Sleep
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   129
  wdt_reset();
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   130
  wdt_enable(WDTO_15MS);   	// Watchdogtimer start with 16 ms timeout
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   131
  #endif			// Watchdog and Sleep
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   132
  sei();         // Enable Interrupts
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   133
}
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   134
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   135
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   136
#ifdef  __IAR_SYSTEMS_ICC__
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   137
#pragma type_attribute = __interrupt
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   138
#pragma vector=TIMER0_COMP_vect
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   139
void TIMER0_COMP_interrupt(void)
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   140
#else	// GCC
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   141
ISR(TIMER0_COMP_vect)
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   142
#endif	// GCC
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   143
/******************************************************************************
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   144
Interruptserviceroutine Timer 2 Compare A for the main cycle
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   145
******************************************************************************/
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   146
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   147
{
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   148
  timer_interrupt = 1;	// Set flag
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   149
}