examples/AT91/Master/main.c
author Mongo
Thu, 13 Oct 2011 17:51:27 +0200
changeset 669 50da44ebaf00
parent 521 731bbe1b67b4
permissions -rw-r--r--
Configuration manager with DCF in object 0x1F22 rewritten almost from scratch.
1. The boot-up message from a device starts a verification of entries
2. If all entries matches the node is started
3. If an entry differs the whole dcf is written and a save is done
4. A reset is send to the node
5. If several boot-up are received at the same time they will be managed one
by one thus only one free sdo client is needed for the whole process.
521
peter
parents:
diff changeset
     1
/*
peter
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
peter
parents:
diff changeset
     3
peter
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
peter
parents:
diff changeset
     5
ARM Port: Peter CHRISTEN
peter
parents:
diff changeset
     6
peter
parents:
diff changeset
     7
See COPYING file for copyrights details.
peter
parents:
diff changeset
     8
peter
parents:
diff changeset
     9
This library is free software; you can redistribute it and/or
peter
parents:
diff changeset
    10
modify it under the terms of the GNU Lesser General Public
peter
parents:
diff changeset
    11
License as published by the Free Software Foundation; either
peter
parents:
diff changeset
    12
version 2.1 of the License, or (at your option) any later version.
peter
parents:
diff changeset
    13
peter
parents:
diff changeset
    14
This library is distributed in the hope that it will be useful,
peter
parents:
diff changeset
    15
but WITHOUT ANY WARRANTY; without even the implied warranty of
peter
parents:
diff changeset
    16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
peter
parents:
diff changeset
    17
Lesser General Public License for more details.
peter
parents:
diff changeset
    18
peter
parents:
diff changeset
    19
You should have received a copy of the GNU Lesser General Public
peter
parents:
diff changeset
    20
License along with this library; if not, write to the Free Software
peter
parents:
diff changeset
    21
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
peter
parents:
diff changeset
    22
*/
peter
parents:
diff changeset
    23
peter
parents:
diff changeset
    24
// Include Standard LIB  files
peter
parents:
diff changeset
    25
#include "AT91SAM7X-EK.h"
peter
parents:
diff changeset
    26
#include <string.h>
peter
parents:
diff changeset
    27
#include "config.h"
peter
parents:
diff changeset
    28
#include "io_macro.h"
peter
parents:
diff changeset
    29
/************************* CANopen includes **********************************/
peter
parents:
diff changeset
    30
#include "canfestival.h"
peter
parents:
diff changeset
    31
#include "objdict.h"
peter
parents:
diff changeset
    32
#include "can_AT91.h"
peter
parents:
diff changeset
    33
peter
parents:
diff changeset
    34
//   Waiting time between AT91B_LED1 and AT91B_LED2
peter
parents:
diff changeset
    35
#define     WAIT_TIME       AT91B_MCK
peter
parents:
diff changeset
    36
peter
parents:
diff changeset
    37
#define PIO_INTERRUPT_LEVEL     6
peter
parents:
diff changeset
    38
#define SOFT_INTERRUPT_LEVEL	2
peter
parents:
diff changeset
    39
#define FIQ_INTERRUPT_LEVEL     7  // Always high
peter
parents:
diff changeset
    40
peter
parents:
diff changeset
    41
// External Function Prototype
peter
parents:
diff changeset
    42
extern void timer_init (unsigned int time);
peter
parents:
diff changeset
    43
void sys_init();
peter
parents:
diff changeset
    44
peter
parents:
diff changeset
    45
// Global variable
peter
parents:
diff changeset
    46
extern int timer0_interrupt;
peter
parents:
diff changeset
    47
peter
parents:
diff changeset
    48
unsigned int leds = 0;
peter
parents:
diff changeset
    49
unsigned int keys,keys_old,keys_edge;
peter
parents:
diff changeset
    50
peter
parents:
diff changeset
    51
unsigned char string_down[] = "1234567890x";
peter
parents:
diff changeset
    52
unsigned char string_up[20];
peter
parents:
diff changeset
    53
unsigned char val_down = 1;
peter
parents:
diff changeset
    54
unsigned char val_up;
peter
parents:
diff changeset
    55
unsigned char val_pdo = 1;
peter
parents:
diff changeset
    56
peter
parents:
diff changeset
    57
// nodes
peter
parents:
diff changeset
    58
#define MEMORY_MODULE		0
peter
parents:
diff changeset
    59
#define OUTPUT_MODULE		1
peter
parents:
diff changeset
    60
#define IO_MODULE		2
peter
parents:
diff changeset
    61
peter
parents:
diff changeset
    62
// node ids
peter
parents:
diff changeset
    63
#define ID_MEMORY_MODULE	0x20
peter
parents:
diff changeset
    64
#define ID_OUTPUT_MODULE	0x21
peter
parents:
diff changeset
    65
#define ID_IO_MODULE		0x22
peter
parents:
diff changeset
    66
peter
parents:
diff changeset
    67
//----------------------------------------------------------------------------
peter
parents:
diff changeset
    68
// Function Name       : main
peter
parents:
diff changeset
    69
// Object              : Main interrupt function
peter
parents:
diff changeset
    70
// Input Parameters    : none
peter
parents:
diff changeset
    71
// Output Parameters   : TRUE
peter
parents:
diff changeset
    72
//----------------------------------------------------------------------------
peter
parents:
diff changeset
    73
int main(void)
peter
parents:
diff changeset
    74
// Begin
peter
parents:
diff changeset
    75
{
peter
parents:
diff changeset
    76
  sys_init();                                   // Initialize system
peter
parents:
diff changeset
    77
  timer_init(10);
peter
parents:
diff changeset
    78
  canInit(CAN_BAUDRATE);         		// Initialize the CANopen bus
peter
parents:
diff changeset
    79
  initTimer();                                 	// Start timer for the CANopen stack
peter
parents:
diff changeset
    80
  __enable_interrupt();
peter
parents:
diff changeset
    81
  setState(&ObjDict_Data, Initialisation);	// Init the state
peter
parents:
diff changeset
    82
  setNodeId (&ObjDict_Data, 0x7F);
peter
parents:
diff changeset
    83
  setState(&ObjDict_Data, Operational);		// Put the master in operational mode
peter
parents:
diff changeset
    84
	
peter
parents:
diff changeset
    85
peter
parents:
diff changeset
    86
  for (;;)
peter
parents:
diff changeset
    87
  {
peter
parents:
diff changeset
    88
    if (timer0_interrupt)
peter
parents:
diff changeset
    89
    {
peter
parents:
diff changeset
    90
      timer0_interrupt = 0;
peter
parents:
diff changeset
    91
      AT91F_PIO_SetOutput(AT91C_BASE_PIOB, 0x01);
peter
parents:
diff changeset
    92
peter
parents:
diff changeset
    93
      keys = ~AT91F_PIO_GetInput(AT91D_BASE_PIO_SW)>>21 & 0x1F;
peter
parents:
diff changeset
    94
peter
parents:
diff changeset
    95
      keys_edge = keys & ~keys_old;	// edge detection
peter
parents:
diff changeset
    96
      keys_old = keys;
peter
parents:
diff changeset
    97
peter
parents:
diff changeset
    98
//      static Message m = Message_Initializer;		// contain a CAN message
peter
parents:
diff changeset
    99
peter
parents:
diff changeset
   100
//      if (canReceive(&m))			// a message received
peter
parents:
diff changeset
   101
//        canDispatch(&ObjDict_Data, &m);         // process it
peter
parents:
diff changeset
   102
peter
parents:
diff changeset
   103
      if (checkbit(keys_edge,0))	// edge on key 0
peter
parents:
diff changeset
   104
      {
peter
parents:
diff changeset
   105
	masterSendNMTstateChange (&ObjDict_Data, 0x00, NMT_Start_Node);
peter
parents:
diff changeset
   106
//	startSYNC(&ObjDict_Data);
peter
parents:
diff changeset
   107
      }
peter
parents:
diff changeset
   108
peter
parents:
diff changeset
   109
      if (checkbit(keys_edge,1))	// edge on key 1
peter
parents:
diff changeset
   110
      {
peter
parents:
diff changeset
   111
	masterSendNMTstateChange (&ObjDict_Data, 0x00, NMT_Reset_Node);
peter
parents:
diff changeset
   112
//	stopSYNC(&ObjDict_Data);
peter
parents:
diff changeset
   113
      }
peter
parents:
diff changeset
   114
peter
parents:
diff changeset
   115
      if (checkbit(keys,2))	// edge on key 2
peter
parents:
diff changeset
   116
      {
peter
parents:
diff changeset
   117
	if (!(DO1 <<= 1))					// generate running light
peter
parents:
diff changeset
   118
	  DO1 = 1;
peter
parents:
diff changeset
   119
	sendPDOevent (&ObjDict_Data);
peter
parents:
diff changeset
   120
      }
peter
parents:
diff changeset
   121
peter
parents:
diff changeset
   122
      if (checkbit(keys_edge,3))	// edge on key 3
peter
parents:
diff changeset
   123
      {
peter
parents:
diff changeset
   124
      }
peter
parents:
diff changeset
   125
peter
parents:
diff changeset
   126
peter
parents:
diff changeset
   127
      leds = DI1;
peter
parents:
diff changeset
   128
peter
parents:
diff changeset
   129
      AT91F_PIO_ClearOutput(AT91C_BASE_PIOB, (leds << 19));
peter
parents:
diff changeset
   130
      AT91F_PIO_SetOutput(AT91C_BASE_PIOB, ~(leds << 19) & AT91B_LED_MASK);
peter
parents:
diff changeset
   131
peter
parents:
diff changeset
   132
      AT91F_PIO_ClearOutput(AT91C_BASE_PIOB, 0x01);
peter
parents:
diff changeset
   133
    }
peter
parents:
diff changeset
   134
  }
peter
parents:
diff changeset
   135
}
peter
parents:
diff changeset
   136
peter
parents:
diff changeset
   137
void sys_init()
peter
parents:
diff changeset
   138
/******************************************************************************
peter
parents:
diff changeset
   139
Initialize the relays, the main states and the modbus protocol stack.
peter
parents:
diff changeset
   140
INPUT	LOCK_STATES *lock_states
peter
parents:
diff changeset
   141
OUTPUT	void
peter
parents:
diff changeset
   142
******************************************************************************/
peter
parents:
diff changeset
   143
{
peter
parents:
diff changeset
   144
  // Enable User Reset and set its minimal assertion to 960 us
peter
parents:
diff changeset
   145
  AT91C_BASE_RSTC->RSTC_RMR = AT91C_RSTC_URSTEN | (0x4<<8) | (unsigned int)(0xA5<<24);
peter
parents:
diff changeset
   146
peter
parents:
diff changeset
   147
  // First, enable the clock of the PIOs
peter
parents:
diff changeset
   148
  AT91F_PMC_EnablePeriphClock (AT91C_BASE_PMC, 1 << AT91C_ID_PIOA) ;
peter
parents:
diff changeset
   149
  AT91F_PMC_EnablePeriphClock (AT91C_BASE_PMC, 1 << AT91C_ID_PIOB) ;
peter
parents:
diff changeset
   150
peter
parents:
diff changeset
   151
  // then, we configure the PIO Lines corresponding to switches
peter
parents:
diff changeset
   152
  // to be inputs.
peter
parents:
diff changeset
   153
  AT91F_PIO_CfgInput(AT91C_BASE_PIOA, AT91B_SW_MASK) ;
peter
parents:
diff changeset
   154
  // then, we configure the PIO Lines corresponding to AT91B_LEDx
peter
parents:
diff changeset
   155
  // to be outputs. No need to set these pins to be driven by the PIO because it is GPIO pins only.
peter
parents:
diff changeset
   156
  AT91F_PIO_CfgOutput(AT91C_BASE_PIOB, AT91B_LED_MASK | 0xFF) ;
peter
parents:
diff changeset
   157
  // Clear the AT91B_LED's. On the EK we must apply a "1" to turn off AT91B_LEDs
peter
parents:
diff changeset
   158
  AT91F_PIO_SetOutput(AT91C_BASE_PIOB, AT91B_LED_MASK) ;
peter
parents:
diff changeset
   159
peter
parents:
diff changeset
   160
  AT91F_PIO_CfgOutput(AT91C_BASE_PIOA,AT91B_CAN_TRANSCEIVER_RS) ;
peter
parents:
diff changeset
   161
  // Clear PA2 <=> Enable Transceiver Normal Mode (versus Standby mode)
peter
parents:
diff changeset
   162
  AT91F_PIO_ClearOutput(AT91C_BASE_PIOA,AT91B_CAN_TRANSCEIVER_RS) ;
peter
parents:
diff changeset
   163
peter
parents:
diff changeset
   164
}