00001 #include "LPC214x.h" /* LPC21XX Peripheral Registers */ 00002 #include "type.h" 00003 #include "irq.h" 00004 #include "ssp.h" 00005 #include "main.h" 00006 #include "system.h" 00007 #include "LL_HL_comm.h" 00008 00009 char SPIWRData[128]; 00010 char SPIRDData[128]; 00011 int CurrentTxIndex; 00012 int CurrentRxIndex; 00013 unsigned int SPIWR_num_bytes; 00014 00015 volatile unsigned int SSP_trans_cnt=0; 00016 00017 unsigned char data_sent_to_LL=1; 00018 00019 unsigned char SSP_receiption_complete=1; 00020 unsigned char IMU_CalcData_updated=0; 00021 00022 00023 char data_sent_to_HL=1; 00024 00025 inline void SSPReceive(unsigned char); 00026 00027 void SSPHandler (void) __irq 00028 { 00029 int regValue; 00030 unsigned short input_data; 00031 // unsigned char timeout=0; 00032 00033 IENABLE; /* handles nested interrupt */ 00034 00035 regValue = SSPMIS; 00036 if ( regValue & SSPMIS_RORMIS ) /* Receive overrun interrupt */ 00037 { 00038 SSPICR = SSPICR_RORIC; /* clear interrupt */ 00039 } 00040 if ( regValue & SSPMIS_RTMIS ) /* Receive timeout interrupt */ 00041 { 00042 SSPICR = SSPICR_RTIC; /* clear interrupt */ 00043 } 00044 00045 if ( regValue & SSPMIS_RXMIS ) /* Rx at least half full */ 00046 { 00047 /* receive until it's empty */ 00048 while ( SSPSR & SSPSR_RNE ) 00049 { 00050 input_data=SSPDR; 00051 //SSPReceive(input_data&0xFF); 00052 //SSPReceive(input_data>>8); 00053 00054 SSP_rx_handler_HL(input_data&0xFF); 00055 SSP_rx_handler_HL(input_data>>8); 00056 00057 //SSP_trans_cnt+=2; 00058 /* Wait until the Busy bit is cleared */ 00059 // while ( (!(SSPSR & SSPSR_BSY) )&&(timeout++<50) ); 00060 } /* interrupt will be cleared when */ 00061 /* data register is read or written */ 00062 } 00063 00064 if ( regValue & SSPMIS_TXMIS ) /* Tx at least half empty */ 00065 { 00066 /* transmit until it's full */ 00067 while ( (SSPSR & SSPSR_TNF) ) 00068 { 00069 if(CurrentTxIndex<SPIWR_num_bytes) 00070 { 00071 SSPDR = SPIWRData[CurrentTxIndex]|(SPIWRData[CurrentTxIndex+1]<<8); 00072 CurrentTxIndex+=2; 00073 } 00074 else 00075 { 00076 CurrentTxIndex=0; 00077 SPIWR_num_bytes=0; 00078 data_sent_to_LL=1; 00079 SSPDR=0; 00080 } 00081 00082 /* Wait until the Busy bit is cleared */ 00083 // while ( !(SSPSR & SSPSR_BSY) ); 00084 } /* interrupt will be cleared when */ 00085 /* data register is read or written */ 00086 } 00087 00088 IDISABLE; 00089 VICVectAddr = 0; /* Acknowledge Interrupt */ 00090 } 00091 00092 00093 inline void SSPReceive(unsigned char SPI_rxdata) 00094 { 00095 static unsigned char SPI_syncstate=0; 00096 static unsigned char SPI_rxcount=0; 00097 static unsigned char *SPI_rxptr; 00098 static volatile unsigned char incoming_data; 00099 00100 //receive handler 00101 if (SPI_syncstate==0) 00102 { 00103 if (SPI_rxdata=='>') SPI_syncstate++; else SPI_syncstate=0; 00104 } 00105 else if (SPI_syncstate==1) 00106 { 00107 if (SPI_rxdata=='*') SPI_syncstate++; else SPI_syncstate=0; 00108 } 00109 else if (SPI_syncstate==2) 00110 { 00111 if (SPI_rxdata=='>') SPI_syncstate++; else SPI_syncstate=0; 00112 } 00113 else if (SPI_syncstate==3) 00114 { 00115 if (SPI_rxdata==PD_IMUCALCDATA) //IMU CalcData 00116 { 00117 SPI_rxcount=sizeof(IMU_CalcData); 00118 SPI_rxptr=(unsigned char *)&IMU_CalcData_tmp; 00119 SPI_syncstate=4; 00120 incoming_data=PD_IMUCALCDATA; 00121 } 00122 else if (SPI_rxdata==PD_IMURAWDATA) //IMU CalcData 00123 { 00124 SPI_rxcount=sizeof(IMU_RawData); 00125 SPI_rxptr=(unsigned char *)&IMU_RawData; 00126 SPI_syncstate=4; 00127 incoming_data=PD_IMURAWDATA; 00128 } 00129 else SPI_syncstate=0; 00130 } 00131 else if (SPI_syncstate==4) 00132 { 00133 SPI_rxcount--; 00134 *SPI_rxptr=SPI_rxdata; 00135 SPI_rxptr++; 00136 if (SPI_rxcount==0) 00137 { 00138 SPI_syncstate=5; 00139 if(incoming_data==PD_IMUCALCDATA) 00140 { 00141 IMU_CalcData_updated=1; 00142 } 00143 incoming_data=0; 00144 } 00145 } 00146 else if(SPI_syncstate==5) //check if another packet is pending 00147 { 00148 if(SPI_rxdata==0) 00149 { 00150 SPI_syncstate=0; 00151 } 00152 else SPI_syncstate=1; 00153 } 00154 else SPI_syncstate=0; 00155 00156 if(!SPI_syncstate) SSP_receiption_complete=1; 00157 else SSP_receiption_complete=0; 00158 } 00159 00160 void LL_write_init(void) 00161 { 00162 SPIWRData[0]='>'; 00163 SPIWRData[1]='*'; 00164 SPIWRData[2]='>'; 00165 } 00166 00167 int LL_write(unsigned char *data, unsigned short cnt, unsigned char PD ) //write data to high-level processor 00168 { 00169 unsigned int i; 00170 /* 00171 if(data_sent_to_LL) 00172 { 00173 //SSP_trans_cnt++; 00174 if(!SPIWR_num_bytes) 00175 { 00176 SPIWRData[3]=PD; 00177 for(i=0; i<cnt; i++) 00178 { 00179 SPIWRData[i+4]=data[i]; 00180 } 00181 SPIWRData[cnt+4]=0; 00182 SPIWR_num_bytes=cnt+5; 00183 00184 } 00185 } 00186 */ 00187 if(data_sent_to_LL) 00188 { 00189 SPIWRData[3]=PD; 00190 for(i=0; i<cnt; i++) 00191 { 00192 SPIWRData[i+4]=data[i]; 00193 } 00194 SPIWRData[cnt+4]=0; 00195 SPIWR_num_bytes=cnt+5; 00196 } 00197 else if(SPIWR_num_bytes+cnt<127) 00198 { 00199 SPIWRData[SPIWR_num_bytes-1]='>'; 00200 SPIWRData[0+SPIWR_num_bytes]='*'; 00201 SPIWRData[1+SPIWR_num_bytes]='>'; 00202 SPIWRData[2+SPIWR_num_bytes]=PD; 00203 for(i=SPIWR_num_bytes; i<cnt+SPIWR_num_bytes; i++) 00204 { 00205 SPIWRData[i+3]=data[i-SPIWR_num_bytes]; 00206 } 00207 SPIWR_num_bytes+=cnt+5; 00208 SPIWRData[SPIWR_num_bytes-1]=0; 00209 } 00210 else return(0); 00211 data_sent_to_LL=0; 00212 00213 return(1); 00214 } 00215 00216