examples/gene_SYNC_HCS12/appli.c
changeset 54 a2ca09ac1523
parent 0 4472ee7c6c3e
child 56 904abfb26abf
equal deleted inserted replaced
53:73bc47a0db55 54:a2ca09ac1523
    18 You should have received a copy of the GNU Lesser General Public
    18 You should have received a copy of the GNU Lesser General Public
    19 License along with this library; if not, write to the Free Software
    19 License along with this library; if not, write to the Free Software
    20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    21 */
    21 */
    22 
    22 
    23 
    23 // Uncomment if you don't need console informations.
    24 #define DEBUG_WAR_CONSOLE_ON
    24 #define DEBUG_WAR_CONSOLE_ON
    25 #define DEBUG_ERR_CONSOLE_ON
    25 #define DEBUG_ERR_CONSOLE_ON
    26 
    26 
    27 #include <stddef.h> /* for NULL */
    27 #include <stddef.h> /* for NULL */
    28 
    28 
    29 #include <asm-m68hc12/portsaccess.h>
    29 #include <asm-m68hc12/portsaccess.h>
    30 #include <asm-m68hc12/ports_def.h>
    30 #include <asm-m68hc12/ports_def.h>
    31 #include <asm-m68hc12/ports.h>
    31 #include <asm-m68hc12/ports.h>
    32 #include  <interrupt.h>
    32 #include  <interrupt.h>
    33 
    33 
       
    34 #include "../include/data.h"
    34 #include <applicfg.h>
    35 #include <applicfg.h>
    35 
    36 
    36 
    37 
    37 
    38 
    38 #include <../include/hcs12/candriver.h>
    39 #include "../include/hcs12/candriver.h"
    39 
    40 #include "../include/hcs12/canOpenDriver.h"
    40 #include "../include/def.h"
    41 #include "../include/def.h"
    41 #include "../include/can.h"
    42 #include "../include/can.h"
    42 #include "../include/objdictdef.h"
    43 #include "../include/objdictdef.h"
    43 #include "../include/objacces.h"
    44 #include "../include/objacces.h"
    44 #include "../include/sdo.h"
    45 #include "../include/sdo.h"
    45 #include "../include/pdo.h"
    46 #include "../include/pdo.h"
    46 #include "../include/timer.h"
    47 #include "../include/timer.h"
    47 #include "../include/lifegrd.h"
    48 #include "../include/lifegrd.h"
    48 #include "../include/sync.h"
    49 #include "../include/sync.h"
    49 
    50 
    50 
       
    51 #include "../include/nmtSlave.h"
    51 #include "../include/nmtSlave.h"
    52 #include "objdict.h"
    52 #include "objdict.h"
       
    53 
       
    54 // Variables defined in the object dictionary (See objdict.c)
       
    55 extern UNS16 acceptanceFilter1;		// Mapped at index 0x2015, subindex 0x00
       
    56 extern UNS16 acceptanceFilter2;		// Mapped at index 0x2016, subindex 0x00
       
    57 extern UNS16 acceptanceFilter3;		// Mapped at index 0x2017, subindex 0x00
       
    58 extern UNS16 acceptanceFilter4;		// Mapped at index 0x2018, subindex 0x00
       
    59 extern UNS16 mask1;		// Mapped at index 0x2019, subindex 0x00
       
    60 extern UNS16 mask2;		// Mapped at index 0x2020, subindex 0x00
       
    61 extern UNS16 mask3;		// Mapped at index 0x2021, subindex 0x00
       
    62 extern UNS16 mask4;		// Mapped at index 0x2022, subindex 0x00
       
    63 extern UNS8 applyDownloadedFilters;
       
    64 
       
    65 
    53 
    66 
    54 
    67 
    55 // HCS12 configuration
    68 // HCS12 configuration
    56 // -----------------------------------------------------
    69 // -----------------------------------------------------
    57 
    70 
   129 
   142 
   130 
   143 
   131 
   144 
   132 /**************************prototypes*****************************************/
   145 /**************************prototypes*****************************************/
   133 
   146 
   134 //fonction d'initialisation du bus can et la couche CANOPEN pour le capteur
   147 //Init can bus and Canopen
   135 void initCanopencapteur (void);
   148 void initCanopencapteur (void);
   136 // les fonctions d'initialisation du capteur: timer, compteurs logiciel
   149 // Init the sensor
   137 void initSensor(void);
   150 void initSensor(void);
   138 void initPortB(void);
   151 void initPortB(void);
       
   152 void initPortH(void);
       
   153 
       
   154 // Functions needed by Canfestival
       
   155 // Notice that gene_SYNC is the name of the sensor defined in python GUI.
       
   156 // Do not change it !.
       
   157 void gene_SYNC_initialisation(void);
       
   158 void gene_SYNC_preOperational(void);
       
   159 void gene_SYNC_operational(void);
       
   160 void gene_SYNC_stopped(void);
       
   161 void gene_SYNC_post_sync(void);
       
   162 
   139 
   163 
   140 //------------------------------------------------------------------------------
   164 //------------------------------------------------------------------------------
   141 //Initialisation of the port B for the leds.
   165 //Initialisation of the port B for the leds.
   142 void initPortB(void)
   166 void initPortB(void)
   143 {
   167 {
   145   IO_PORTS_8(DDRB)= 0XFF;
   169   IO_PORTS_8(DDRB)= 0XFF;
   146   // RAZ
   170   // RAZ
   147   IO_PORTS_8(PORTB) = 0xFF;
   171   IO_PORTS_8(PORTB) = 0xFF;
   148 }
   172 }
   149 
   173 
   150 
   174 //------------------------------------------------------------------------------
       
   175 // Init of port H to choose the CAN rate by switch, and the nodeId
       
   176 void initPortH(void)
       
   177 {
       
   178   // Port H is input
       
   179   IO_PORTS_8(DDRH)= 0X00;
       
   180   // Enable pull device
       
   181   IO_PORTS_8(PERH) = 0XFF;
       
   182   // Choose the pull-up device
       
   183   IO_PORTS_8(PPSH) = 0X00;
       
   184 }
   151 
   185 
   152 //------------------------------------------------------------------------------
   186 //------------------------------------------------------------------------------
   153 void initSensor(void)
   187 void initSensor(void)
   154 { 
   188 { 
   155   UNS8 baudrate = 0;
   189   UNS8 baudrate = 0; 
   156   MSG_WAR(0x3F33, "I am the node :  ", getNodeId(&gene_SYNC_Data));  
   190   UNS8 nodeId = 0; 
   157   // Init led control
   191   // Init led control
   158   initPortB(); 
   192   initPortB(); 
   159   IO_PORTS_8(PORTB) &= ~ 0x01; //One led ON
   193   IO_PORTS_8(PORTB) &= ~ 0x01; //One led ON
   160   // Init port to choose se CAN baudrate by switch
   194   initPortH();
   161   IO_PORTS_8(ATD0DIEN) = 0x03;
   195   
       
   196     /* Defining the node Id */
       
   197   // Uncomment to have a fixed nodeId
       
   198   //setNodeId(&gene_SYNC_Data, 0x03);
       
   199   
       
   200   // Comment below to have a fixed nodeId
       
   201   nodeId = ~(IO_PORTS_8(PTH)) & 0x3F;
       
   202   if (nodeId == 0) {
       
   203   	MSG_WAR(0x3F33, "Using default nodeId :  ", getNodeId(&gene_SYNC_Data));
       
   204   }
       
   205   else	
       
   206     setNodeId(&gene_SYNC_Data, nodeId);
       
   207   
       
   208   MSG_WAR(0x3F33, "My nodeId is :  ", getNodeId(&gene_SYNC_Data));
   162   
   209   
   163   canBusInit bi0 = {
   210   canBusInit bi0 = {
   164     0,    /* no low power                 */ 
   211     0,    /* no low power                 */ 
   165     0,    /* no time stamp                */
   212     0,    /* no time stamp                */
   166     1,    /* enable MSCAN                 */
   213     1,    /* enable MSCAN                 */
   183   };
   230   };
   184   
   231   
   185   //Init the HCS12 microcontroler for CanOpen 
   232   //Init the HCS12 microcontroler for CanOpen 
   186   initHCS12();
   233   initHCS12();
   187    
   234    
   188   // Chose the CAN rate
   235   // Chose the CAN rate (On our board, whe have switch for all purpose)
   189   baudrate = IO_PORTS_8(PORTAD0) & 0x03;
   236   //  7    8
       
   237   //  ON   ON   => 1000 kpbs
       
   238   //  OFF  ON   =>  500 kpbs
       
   239   //  ON   OFF  =>  250 kpbs
       
   240   
       
   241   baudrate = ~(IO_PORTS_8(PTH)) & 0xC0;
       
   242   
       
   243   // Uncomment to have a fixed baudrate of 250 kbps
       
   244   //baudrate = 1;
       
   245   
   190   switch (baudrate) {
   246   switch (baudrate) {
   191   case 1:
   247   case 0x40:
   192     bi0.clk = CAN_Baudrates[CAN_BAUDRATE_250K];
   248     bi0.clk = CAN_Baudrates[CAN_BAUDRATE_250K];
   193     MSG_WAR(0x3F30, "CAN 250 kbps ", 0);
   249     MSG_WAR(0x3F30, "CAN 250 kbps ", 0);
   194     break;
   250     break;
   195   case 2:
   251   case 0x80:
   196     bi0.clk = CAN_Baudrates[CAN_BAUDRATE_500K];
   252     bi0.clk = CAN_Baudrates[CAN_BAUDRATE_500K];
   197     MSG_WAR(0x3F31, "CAN 500 kbps ", 0);
   253     MSG_WAR(0x3F31, "CAN 500 kbps ", 0);
   198     break;
   254     break;
   199   case 3:
   255   case 0xC0:
   200     bi0.clk = CAN_Baudrates[CAN_BAUDRATE_1M];
   256     bi0.clk = CAN_Baudrates[CAN_BAUDRATE_1M];
   201     MSG_WAR(0x3F31, "CAN 1000 kbps ", 0);
   257     MSG_WAR(0x3F31, "CAN 1000 kbps ", 0);
   202     break;   
   258     break;   
   203   default:
   259   default:
   204     MSG_WAR(0x2F32, "CAN BAUD RATE NOT DEFINED ", 0);
   260     bi0.clk = CAN_Baudrates[CAN_BAUDRATE_1M];
       
   261     MSG_WAR(0x2F32, "CAN BAUD RATE NOT DEFINED => 250 kbps ", 0);
   205   }
   262   }
   206 
   263 
   207   MSG_WAR(0x3F33, "SYNC signal generator", 0);
   264    
       
   265 
       
   266   MSG_WAR(0x3F33, "SYNC signal generator ", 0);
   208 
   267 
   209   canInit(CANOPEN_LINE_NUMBER_USED, bi0);  //initialize filters...
   268   canInit(CANOPEN_LINE_NUMBER_USED, bi0);  //initialize filters...
   210   initTimer(); // Init hcs12 timer used by CanFestival. (see timerhw.c)
   269   initTimer(); // Init hcs12 timer used by CanFestival. (see timerhw.c)
   211   unlock(); // Allow interruptions  
   270   unlock(); // Allow interruptions  
   212 }
   271 }
   242 
   301 
   243 //------------------------------------------------------------------------------
   302 //------------------------------------------------------------------------------
   244 void gene_SYNC_initialisation()
   303 void gene_SYNC_initialisation()
   245 {  
   304 {  
   246   MSG_WAR (0x3F00, "Entering in INIT ", 0); 
   305   MSG_WAR (0x3F00, "Entering in INIT ", 0); 
   247   initSensor();
   306   initSensor();  
       
   307 
   248   IO_PORTS_8(PORTB) &= ~ 0x01; // led  0         : ON
   308   IO_PORTS_8(PORTB) &= ~ 0x01; // led  0         : ON
   249   IO_PORTS_8(PORTB) |=   0x0E; // leds 1, 2, 3   : OFF
   309   IO_PORTS_8(PORTB) |=   0x0E; // leds 1, 2, 3   : OFF
       
   310   
   250 }
   311 }
   251 
   312 
   252 
   313 
   253 //------------------------------------------------------------------------------
   314 //------------------------------------------------------------------------------
   254 void gene_SYNC_preOperational()
   315 void gene_SYNC_preOperational()
   255 {
   316 {
   256   MSG_WAR (0x3F01, "Entering in PRE-OPERATIONAL ", 0); 
   317   MSG_WAR (0x3F01, "Entering in PRE-OPERATIONAL ", 0); 
   257   IO_PORTS_8(PORTB) &= ~ 0x03; // leds 0, 1      : ON
   318   IO_PORTS_8(PORTB) &= ~ 0x03; // leds 0, 1      : ON
   258   IO_PORTS_8(PORTB) |=   0x0C; // leds 2, 3      : OFF
   319   IO_PORTS_8(PORTB) |=   0x0C; // leds 2, 3      : OFF
       
   320     /* default values for the msg CAN filters */
       
   321   /* Accept all */
       
   322     {
       
   323    	  canBusFilterInit filterConfiguration = 
       
   324    	  {
       
   325    	  0x01,  /* Filter on 16 bits. See Motorola Block Guide V02.14 */
       
   326       /*canidarx, canidmrx */                        
       
   327       0x00, 0xFF,          /* filter 0 */
       
   328       0x00, 0xFF, 		   /* filter 0 */
       
   329       0x00, 0xFF,          /* filter 1 */
       
   330       0x00, 0xFF, 		   /* filter 1 */
       
   331       0x00, 0xFF,          /* filter 2 */
       
   332       0x00, 0xFF, 		   /* filter 2 */
       
   333       0x00, 0xFF,          /* filter 3 */
       
   334       0x00, 0xFF, 	       /* filter 3 */
       
   335    	 };
       
   336    	 canChangeFilter(CANOPEN_LINE_NUMBER_USED, filterConfiguration);
       
   337    } 
       
   338    // Reset the automatic change by SDO
       
   339    applyDownloadedFilters = 0;
       
   340    
   259 }
   341 }
   260 
   342 
   261 
   343 
   262 //------------------------------------------------------------------------------
   344 //------------------------------------------------------------------------------
   263 void gene_SYNC_operational()
   345 void gene_SYNC_operational()
   264 { 
   346 { 
   265    MSG_WAR (0x3F02, "Entering in OPERATIONAL ", 0); 
   347    MSG_WAR (0x3F02, "Entering in OPERATIONAL ", 0); 
   266    IO_PORTS_8(PORTB) &= ~ 0x07; // leds 0, 1, 2   : ON
   348    IO_PORTS_8(PORTB) &= ~ 0x07; // leds 0, 1, 2   : ON
   267    IO_PORTS_8(PORTB) |=   0x08; // leds 3         : OFF
   349    IO_PORTS_8(PORTB) |=   0x08; // leds 3         : OFF
       
   350    
       
   351    // Filtering the CAN received msgs.
       
   352    // 2 ways
       
   353    // First :applying an automatic filter
       
   354    // Second : The values of the filtering registers are mapped in the object dictionary,
       
   355    // So that a filtering configuration can be downloaded by SDO in pre-operational mode
       
   356    	
       
   357    	if (applyDownloadedFilters == 0) {// No downloaded configuration to apply
       
   358   		UNS16 accept1 = 0x0000; // Accept NMT
       
   359    		UNS16 mask1 = 0x0FFF;   // Mask NMT
       
   360    		UNS16 accept2 = 0xE000; // Accept NMT error control (heartbeat, nodeguard)
       
   361    		UNS16 mask2 = 0x0FFF;   // Mask NMT error control (heartbeat, nodeguard)
       
   362    		
       
   363    		canBusFilterInit filterConfiguration = 
       
   364 				{// filters 3 and 4 not used, so configured like filter 1.
       
   365    		  	0x01,  /* Filter on 16 bits. See Motorola Block Guide V02.14 */
       
   366      	    /*canidarx, canidmrx */                        
       
   367       		(UNS8)(accept1 >> 8), (UNS8)(mask1 >> 8),    /* filter 1 id10...3*/
       
   368       		(UNS8)accept1       , (UNS8)mask1, 		       /* filter 1 id2 ... */
       
   369       		(UNS8)(accept2 >> 8), (UNS8)(mask2 >> 8),    /* filter 2 id10...3*/
       
   370       		(UNS8)accept2       , (UNS8)mask2, 		       /* filter 2 id2 ... */
       
   371        		(UNS8)(accept1 >> 8), (UNS8)(mask1 >> 8),    /* filter 3 id10...3*/
       
   372       		(UNS8)accept1       , (UNS8)mask1, 		       /* filter 3 id2 ... */
       
   373       		(UNS8)(accept1 >> 8), (UNS8)(mask1 >> 8),    /* filter 4 id10...3*/
       
   374       		(UNS8)accept1       , (UNS8)mask1 		       /* filter 4 id2 ... */     	
       
   375    	 	 	};
       
   376    	 		canChangeFilter(CANOPEN_LINE_NUMBER_USED, filterConfiguration);
       
   377    	 		MSG_WAR (0x3F03, "Internal CAN Rcv filter applied ", 0); 
       
   378     	} 
       
   379    	  else { // Apply filters downnloaded
       
   380    			canBusFilterInit filterConfiguration = 
       
   381 				{// filters 3 and 4 not used, so configured like filter 1.
       
   382    		  	0x01,  /* Filter on 16 bits. See Motorola Block Guide V02.14 */
       
   383      	    /*canidarx, canidmrx */                        
       
   384       		(UNS8)( acceptanceFilter1>> 8), (UNS8)(mask1 >> 8),    /* filter 1 id10...3*/
       
   385       		(UNS8)acceptanceFilter1       , (UNS8)mask1, 		       /* filter 1 id2 ... */
       
   386       		(UNS8)(acceptanceFilter2 >> 8), (UNS8)(mask2 >> 8),     /* filter 2 id10...3*/
       
   387       		(UNS8)acceptanceFilter2       , (UNS8)mask2, 		       /* filter 2 id2 ... */
       
   388        		(UNS8)(acceptanceFilter3 >> 8), (UNS8)(mask3 >> 8),    /* filter 3 id10...3*/
       
   389       		(UNS8)acceptanceFilter3       , (UNS8)mask3, 		       /* filter 3 id2 ... */
       
   390       		(UNS8)(acceptanceFilter4 >> 8), (UNS8)(mask4 >> 8),    /* filter 4 id10...3*/
       
   391       		(UNS8)acceptanceFilter4       , (UNS8)mask4 		       /* filter 4 id2 ... */     	
       
   392    	 	 	};
       
   393    	 		canChangeFilter(CANOPEN_LINE_NUMBER_USED, filterConfiguration);  		
       
   394    	 		MSG_WAR (0x3F04, "Downloaded CAN Rcv filter applied ", 0); 
       
   395    	}
   268 }
   396 }
   269 
   397 
   270 //------------------------------------------------------------------------------
   398 //------------------------------------------------------------------------------
   271 void gene_SYNC_stopped()
   399 void gene_SYNC_stopped()
   272 {
   400 {
   273   MSG_WAR (0x3F02, "Entering in STOPPED ", 0); 
   401   MSG_WAR (0x3F02, "Entering in STOPPED ", 0); 
       
   402   IO_PORTS_8(PORTB) |=   0x0E; // leds 1, 2, 3, 4   : OFF
   274 }
   403 }
   275 
   404 
   276 //------------------------------------------------------------------------------
   405 //------------------------------------------------------------------------------
   277 void gene_SYNC_post_sync()
   406 void gene_SYNC_post_sync()
   278 {
   407 {
   293 
   422 
   294  
   423  
   295 UNS8 main (void)
   424 UNS8 main (void)
   296 {
   425 {
   297 
   426 
   298   MSG_WAR(0x3F34, "Entering in the main", 0);
   427   MSG_WAR(0x3F34, "Entering in the main ", 0);
   299   //----------------------------- INITIALISATION --------------------------------
   428   //----------------------------- INITIALISATION --------------------------------
   300   /* Defining the node Id */
   429   
   301   setNodeId(&gene_SYNC_Data, 0x03);
       
   302 
       
   303   /* Put the node in Initialisation mode */
   430   /* Put the node in Initialisation mode */
   304   MSG_WAR(0x3F35, "va passer en init", 0);
   431   MSG_WAR(0x3F35, "Will entering in INIT ", 0);
   305   setState(&gene_SYNC_Data, Initialisation);
   432   setState(&gene_SYNC_Data, Initialisation);
   306 
   433 
   307   //----------------------------- START -----------------------------------------
   434   //----------------------------- START -----------------------------------------
   308   /* Put the node in pre-operational mode */
   435   /* Put the node in pre-operational mode */
   309   //MSG_WAR(0x3F36, "va passer en pre-op", 0);
   436   //MSG_WAR(0x3F36, "va passer en pre-op", 0);
   310   //setState(&gene_SYNC_Data, Pre_operational);
   437   //setState(&gene_SYNC_Data, Pre_operational);
   311 
   438 
   312     while (1) {
   439 	// Loop of receiving messages
   313       {
   440   while (1) {
   314 	Message m;
   441 	Message m;
   315 	if (f_can_receive(0, &m)) {
   442 	if (f_can_receive(0, &m)) {
   316 	  MSG_WAR(0x3F36, "Msg received", m.cob_id.w);
   443 	  //MSG_WAR(0x3F36, "Msg received", m.cob_id.w);
   317 	  canDispatch(&gene_SYNC_Data, &m);
   444 	  canDispatch(&gene_SYNC_Data, &m);
   318 	}
   445 	}  
   319 	  
   446     	}
   320 	
       
   321       }
       
   322       
       
   323     }
       
   324 
   447 
   325   return (0); 
   448   return (0); 
   326 }
   449 }
   327 
   450 
   328 
   451