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