csr_clock.c

00001 #include <includes.h>
00002 
00003 unsigned long int csr_ms_counter = 0;
00004 
00005 // CAN transmit frame count global variables:
00006 extern volatile unsigned short can_tx_frame_count_1;
00007 extern volatile unsigned short can_tx_frame_count_2;
00008 extern volatile unsigned short can_tx_frame_count_3;
00009 extern volatile unsigned short can_tx_frame_count_4;
00010 
00012 // ARM7 synchronization code
00013 // Run this in software setup immediately before enabling interrupts
00015 void csr_synchronize_arm9(void)
00016 { 
00017   short unsigned int i = 0, temp = 0;
00018     
00019   while (temp != 0x0550)  // Look for first code from ARM9, while sending out first ARM7 code
00020   {
00021     //LED blink code, slow
00022     ++i;
00023     if (i > 100000) {i = 0;}
00024     if (i > 50000) {MCU_LED_RED_ON;}
00025     else {MCU_LED_RED_OFF;}
00026     //Update display of "green ARM9 LED" on local MCU green LED (Hardware to do: add real ARM9 LED to board)
00027     if (FIO0PIN & 1<<15)     //LPC3250 GPIO_0
00028     {
00029       MCU_LED_GREEN_ON;     //turn on green ARM7 MCU LED
00030     }
00031     else
00032     {
00033       MCU_LED_GREEN_OFF;    //turn off green ARM7 MCU LED
00034     }
00035     
00036     if (!(SSPSR & (1<<4))) //SSP not busy
00037     {
00038       SSPDR = 0xFAAF;  //Send first ready signal to ARM9 
00039     }
00040     
00041     while (SSPSR & (1<<4)) //Wait until SSP is no longer busy
00042     {
00043       ++temp;
00044     }
00045        
00046     while (SSPSR & (1<<2)) //SSP receive FIFO not empty
00047     {
00048       temp = SSPDR;
00049     }  
00050   }
00051   
00052   while (temp != 0xFAAF)  // Look for second code from ARM9, while sending out second ARM7 code
00053   {
00054     //LED blink code, fast
00055     ++i;
00056     if (i > 20000) {i = 0;}
00057     if (i > 10000) {MCU_LED_RED_ON;}
00058     else {MCU_LED_RED_OFF;}
00059     //Update display of "green ARM9 LED" on local MCU green LED (Hardware to do: add real ARM9 LED to board)
00060     if (FIO0PIN & 1<<15)     //LPC3250 GPIO_0
00061     {
00062       MCU_LED_GREEN_ON;     //turn on green ARM7 MCU LED
00063     }
00064     else
00065     {
00066       MCU_LED_GREEN_OFF;    //turn off green ARM7 MCU LED
00067     }
00068     
00069     if (!(SSPSR & (1<<4))) //SSP not busy
00070     {
00071       SSPDR = 0x0550;  //Send first ready signal to ARM9 
00072     }
00073     
00074     while (SSPSR & (1<<4)) //Wait until SSP is no longer busy
00075     {
00076       ++temp;
00077     }
00078        
00079     while (SSPSR & (1<<2)) //SSP receive FIFO not empty
00080     {
00081       temp = SSPDR;
00082     }  
00083   }
00084   
00085   while (SSPSR & (1<<4)) //Wait until SSP is no longer busy
00086   {
00087     ++temp;
00088   }
00089   
00090   while (SSPSR & (1<<2)) //Receive FIFO is not empty
00091   {
00092     temp = SSPDR;    //Empty receive buffer
00093   }
00094 }
00096 
00097 
00098 /*
00100 // ARM7 synchronization code
00101 // Run this in software setup immediately before enabling interrupts
00103 void csr_synchronize_arm9(void)
00104 { 
00105   short unsigned int i = 0, temp, code_received_1 = 0, code_received_2 = 0;
00106   
00107   MCU_LED_RED_OFF;  // **** TEST CODE ****
00108     
00109   while (!(code_received_1 && code_received_2))
00110   {
00111     ++i;
00112     if (i > 30000) {i = 0;}
00113     if (i > 15000) {MCU_LED_RED_ON;} // **** TEST CODE ****
00114     else {MCU_LED_RED_OFF;}
00115     if (FIO0PIN & 1<<15)     //LPC3250 GPIO_0
00116     {
00117       MCU_LED_GREEN_ON;     //turn on green ARM7 MCU LED
00118     }
00119     else
00120     {
00121       MCU_LED_GREEN_OFF;    //turn off green ARM7 MCU LED
00122     }
00123     
00124     if (!(SSPSR & (1<<4))) //SSP not busy
00125     {
00126       SSPDR = 0xAAAA;  //Send first ready signal to ARM9 
00127       SSPDR = 0x5555;  //Send second ready signal to ARM9  
00128     }
00129     
00130     while (SSPSR & (1<<4)) //Wait until SSP is no longer busy
00131     {
00132       ++temp;
00133     }
00134        
00135     while (SSPSR & (1<<2)) //SSP receive FIFO not empty
00136     {
00137       temp = SSPDR;
00138       if (temp == 0x5555)       //First ready signal from ARM9.
00139       {
00140         code_received_1 = 1;
00141       }
00142       else if (temp == 0xAAAA)  //Second ready signal from ARM9.
00143       {
00144         code_received_2 = 1;
00145       }
00146     }  
00147   }
00148   
00149   MCU_LED_RED_OFF; // **** TEST CODE ****
00150   if (FIO0PIN & 1<<15)     //LPC3250 GPIO_0
00151   {
00152     MCU_LED_GREEN_ON;     //turn on green ARM7 MCU LED
00153   }
00154   else
00155   {
00156    MCU_LED_GREEN_OFF;    //turn off green ARM7 MCU LED
00157   }
00158    
00159   while (SSPSR & (1<<4)) //Wait until SSP is no longer busy
00160   {
00161     ++temp;
00162   }
00163   
00164   while (SSPSR & (1<<2)) //Receive FIFO is not empty
00165   {
00166     temp = SSPDR;    //Empty receive buffer
00167   }
00168 }
00170 */
00171 
00173 void csr_clock_tick(void)
00174 {
00175   static short int i = 0;
00176   
00177   ++i;
00178   if (i >= CSR_TIMER_TICKS_PER_MS)
00179   {
00180     i = 0;
00181     ++csr_ms_counter;
00182     csr_send_can_time_packets(csr_ms_counter);
00183   }
00184 }
00186 
00188 unsigned long int csr_elapsed_ms(void)
00189 {
00190   return csr_ms_counter;
00191 }
00193 
00195 
00196 void csr_send_can_time_packets(long unsigned int time)
00197 {       
00198   //Transmitter buffer 3 should be available for time packet transmit; error if not
00199   if (C1SR & (1<<18))
00200   {
00201     C1TFI3 = 1<<19;   //Time stamp has length 8 bytes
00202     C1TID3 = 0x0;     //Time stamp CAN_ID is 0
00203     C1TDA3 = time;
00204     C1TDB3 = time;    //Send time value in milliseconds
00205     C1CMR = (1<<0) | (1<<1)  | (1<<7);  //Select TX buffer 3 for transmission with
00206                                          //simultaneous transmit request and abort.
00207                                          //This is supposed to give a single transmit
00208                                          //attempt 
00209     can_tx_frame_count_1++;
00210   }
00211   else
00212   {
00213     //Error - buffer 3 not available for CAN1. Previous packet unsent?
00214   }
00215   
00216   
00217   //Transmitter buffer 3 should be available for time packet transmit; error if not
00218   if (C2SR & (1<<18))
00219 
00220  { 
00221     C2TFI3 = 1<<19;   //Time stamp has length 8 bytes
00222     C2TID3 = 0x0;     //Time stamp CAN_ID is 0
00223     C2TDA3 = time;    //Time stamp float value is 0
00224     C2TDB3 = time;    //Send time value in milliseconds
00225     
00226     C2CMR = (1<<0) | (1<<1) | (1<<7);     //Select TX buffer 3 for transmission with
00227                                           //simultaneous transmit request and abort.
00228                                           //This is supposed to give a single transmit
00229                                           //attempt 
00230     can_tx_frame_count_2++;
00231   }
00232   else
00233   {
00234     //Error - buffer 3 not available for CAN2. Previous packet unsent?
00235   }
00236   
00237   //Transmitter buffer 3 should be available for time packet transmit; error if not
00238   if (C3SR & (1<<18))
00239   {
00240     C3TFI3 = 8<<16;   //Time stamp has length 8 bytes
00241     C3TID3 = 0x0;     //Time stamp CAN_ID is 0
00242     C3TDA3 = time;     //Time stamp float value is 0
00243     C3TDB3 = time;    //Send time value in milliseconds
00244     
00245     C3CMR = (1<<0) | (1<<1) | (1<<7);     //Select TX buffer 3 for transmission with
00246                                           //simultaneous transmit request and abort.
00247                                           //This is supposed to give a single transmit
00248                                           //attempt 
00249     can_tx_frame_count_3++;
00250   }
00251   else
00252   {
00253     //Error - buffer 3 not available for CAN3. Previous packet unsent?
00254   }
00255   
00256     
00257     //Transmitter buffer 3 should be available for time packet transmit; error if not
00258   if (C4SR & (1<<18))
00259   {
00260     C4TFI3 = 8<<16;   //Time stamp has length 8 bytes
00261     C4TID3 = 0x0;     //Time stamp CAN_ID is 0
00262     C4TDA3 = time;     //Time stamp float value is 0
00263     C4TDB3 = time;    //Send time value in milliseconds
00264     
00265     C4CMR = (1<<0) | (1<<1) | (1<<7);   //Select TX buffer 3 for transmission with
00266                                         //simultaneous transmit request and abort.
00267                                         //This is supposed to give a single transmit
00268                                         //attempt
00269     can_tx_frame_count_4++; 
00270   }
00271   else
00272   {
00273     //Error - buffer 4 not available for CAN1. Previous packet unsent?
00274   } 
00275   
00276 }
00277 
Generated on Tue Jun 29 16:36:14 2010 by  doxygen 1.6.3