examples/AVR/Slave/main.c
author Edouard Tisserant
Wed, 27 Mar 2013 09:32:05 +0900
changeset 779 5bbf6e4521bb
parent 675 e5c5101c4f0b
permissions -rw-r--r--
Merge
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"
675
e5c5101c4f0b FIXED: - The AVR example now compiles in linux.
Christian Taedcke <hacking@taedcke.com>
parents: 525
diff changeset
    35
#include "ObjDict.h"
413
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
unsigned char timer_interrupt = 0;		// Set if timer interrupt eclapsed
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    39
unsigned char inputs;
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    40
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    41
// CAN
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    42
unsigned char nodeID;
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    43
unsigned char digital_input[1] = {0};
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    44
unsigned char digital_output[1] = {0};
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    45
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    46
static Message m = Message_Initializer;		// contain a CAN message
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    47
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    48
void sys_init();
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    49
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    50
// macros to handle the schedule timer
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    51
#define sys_timer			timer_interrupt
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    52
#define reset_sys_timer()		timer_interrupt = 0
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    53
#define CYCLE_TIME	        	1000     	// Sample Timebase [us]
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    54
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    55
int main(void)
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    56
{
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    57
  sys_init();                                   // Initialize system
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    58
  canInit(CAN_BAUDRATE);         		// Initialize the CANopen bus
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    59
  initTimer();                                 	// Start timer for the CANopen stack
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    60
  nodeID = read_bcd();				// Read node ID first
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    61
  setNodeId (&ObjDict_Data, nodeID);
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    62
  setState(&ObjDict_Data, Initialisation);	// Init the state
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    63
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    64
  for(;;)		                        // forever loop
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    65
  {
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    66
    if (sys_timer)	                        // Cycle timer, invoke action on every time slice
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    67
    {
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    68
      reset_sys_timer();	                // Reset timer
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    69
      digital_input[0] = get_inputs();
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    70
      digital_input_handler(&ObjDict_Data, digital_input, sizeof(digital_input));
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    71
      digital_output_handler(&ObjDict_Data, digital_output, sizeof(digital_output));
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    72
      set_outputs(digital_output[0]);
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    73
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    74
      // Check if CAN address has been changed
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    75
      if(!( nodeID == read_bcd()))
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    76
      {
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    77
        nodeID = read_bcd();                    // Save the new CAN adress
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    78
        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
    79
        setNodeId(&ObjDict_Data, nodeID);         // Now the CAN adress is changed
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    80
        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
    81
      }
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
    // a message was received pass it to the CANstack
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    85
    if (canReceive(&m))			// a message reveived
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    86
      canDispatch(&ObjDict_Data, &m);         // process it
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    87
    else
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    88
    {
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    89
      // Enter sleep mode
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    90
      #ifdef WD_SLEEP		// Watchdog and Sleep
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    91
      wdt_reset();
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    92
      sleep_enable();
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    93
      sleep_cpu();
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    94
      #endif				// Watchdog and Sleep
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
    95
    }
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
void sys_init()
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   100
/******************************************************************************
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   101
Initialize the relays, the main states and the modbus protocol stack.
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   102
INPUT	LOCK_STATES *lock_states
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   103
OUTPUT	void
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   104
******************************************************************************/
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   105
{
525
peter
parents: 413
diff changeset
   106
  OSCCAL = 0x43;                        // adjust the RC oszillator
413
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   107
  
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   108
  PORTA = 0xFF;	                        // Inputs (Keys, low active) with pullup
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   109
  DDRA  = 0x00;		                // 
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   110
  PORTB = 0xFF;	                        // Outputs (LEDs, low active) all 1
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   111
  DDRB  = 0xFF;		                // 
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   112
  PORTC = 0xFF;	                        // 1 BCD switch with pullup
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   113
  DDRC  = 0x00;		                // 
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   114
  PORTD = 0x2C;	                        // 2xCOM, unused, CAN, unused
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   115
  DDRD  = 0x2A;		                // All init 0 or without pullup
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   116
  PORTE = 0x00;	                        // Output
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   117
  DDRE  = 0x3C;		                // 2x not used, 2x not used
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   118
  PORTF = 0x00;	                        // Not used
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   119
  DDRF  = 0xFF;		                // All output
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   120
  PORTG = 0x00;	                        // Not used
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   121
  DDRG  = 0x1F;		                // Output for debug (only 5 pins)
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   122
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   123
// Set timer 0 for main schedule time
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   124
  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
   125
  TIMSK0 = 1 << OCIE0A;		        // Timer Interrupts: Timer 0 Compare
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   126
  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
   127
  #ifdef WD_SLEEP		// Watchdog and Sleep
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   128
  wdt_reset();
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   129
  wdt_enable(WDTO_15MS);   	// Watchdogtimer start with 16 ms timeout
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   130
  #endif			// Watchdog and Sleep
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   131
  sei();         // Enable Interrupts
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   132
}
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
#ifdef  __IAR_SYSTEMS_ICC__
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   136
#pragma type_attribute = __interrupt
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   137
#pragma vector=TIMER0_COMP_vect
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   138
void TIMER0_COMP_interrupt(void)
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   139
#else	// GCC
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   140
ISR(TIMER0_COMP_vect)
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   141
#endif	// GCC
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   142
/******************************************************************************
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   143
Interruptserviceroutine Timer 2 Compare A for the main cycle
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   144
******************************************************************************/
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
  timer_interrupt = 1;	// Set flag
57c7a451c671 New named folder, first DS401 profile module, Makefile
peter
parents:
diff changeset
   148
}