csr_ssp.c
00001 #include <includes.h>
00002
00003
00004 #define CSR_SEG_SIZE 31
00005 #define CSR_RX_SEG_NUM 8
00006
00007 volatile unsigned short int csr_rx_buffer[CSR_RX_SEG_NUM][CSR_SEG_SIZE];
00008 volatile short int csr_rx_index1 = 0;
00009 volatile short int csr_rx_index2 = 0;
00010
00011
00012
00013
00014 #define CSR_TX_BUFFER_SIZE 500
00015 volatile unsigned short int csr_tx_buffer[CSR_TX_BUFFER_SIZE];
00016 volatile short int csr_tx_index1 = 0;
00017 volatile short int csr_tx_index2 = 0;
00018
00020
00021 void csr_global_variable_init(void)
00022 {
00023 short unsigned int i, j;
00024
00025
00026 csr_rx_index1 = 0;
00027 csr_rx_index2 = 0;
00028
00029 for (i = 0; i < CSR_RX_SEG_NUM; ++i)
00030 {
00031 for (j = 0; j < CSR_SEG_SIZE; ++j)
00032 {
00033 csr_rx_buffer[i][j] = 0;
00034 }
00035 }
00036
00037
00038 csr_tx_index1 = 0;
00039 csr_tx_index2 = 0;
00040
00041 for (i = 0; i < CSR_TX_BUFFER_SIZE; ++i)
00042 {
00043 csr_tx_buffer[i] = 0;
00044 }
00045 }
00046
00048
00049
00050 void csr_ssp_isr(void)
00051 {
00052 static short int rx_index = 0, tx_index = 0, checksum = 0, send_zeroes = 0;
00053 short unsigned int dummy;
00054
00055 #ifdef DEBUG
00056 if (FIO0PIN & 1<<14)
00057 {
00058 VICIntEnClr = 0xFFFFFFFF;
00059 VICVectAddr = 0;
00060 return;
00061 }
00062 #endif
00063
00064
00065 if (VICFIQStatus & 1<<5)
00066 {
00067
00068
00069 csr_clock_tick();
00070
00071
00072 if (rx_index != CSR_SEG_SIZE)
00073 {
00074
00075
00076
00077
00078 error_occurred_fiq(ERROR_SSP_LOW_RATE);
00079 b10a_mcu_red_led_blink(50);
00080 }
00081 rx_index = 0;
00082 if (++csr_rx_index1 == CSR_RX_SEG_NUM) {csr_rx_index1 = 0;}
00083
00084
00085 if (csr_rx_index1 == csr_rx_index2)
00086 {
00087
00088 if (--csr_rx_index1 < 0) {csr_rx_index1 = CSR_RX_SEG_NUM - 1;}
00089 error_occurred_fiq(ERROR_SSP_RX_BUF_OF);
00090 b10a_mcu_red_led_blink(50);
00091
00092 }
00093
00094
00095 checksum = 1;
00096 tx_index = 0;
00097 send_zeroes = 0;
00098
00099
00100 SSPIMSC = 0xF;
00101 T1IR = 0xFF;
00102 }
00103
00104
00105 if (VICFIQStatus & 1<<11)
00106 {
00107
00108 while(SSPSR & (1<<2))
00109 {
00110 if (rx_index < CSR_SEG_SIZE)
00111 {
00112 csr_rx_buffer[csr_rx_index1][rx_index] = SSPDR;
00113 ++rx_index;
00114 }
00115 else
00116 {
00117 dummy = SSPDR;
00118 }
00119 }
00120
00121 while ((SSPSR & 1<<1) && (SSPIMSC & (1<<3)))
00122 {
00123
00124 if (csr_tx_index2 == csr_tx_index1) {send_zeroes = 1;}
00125
00126 if (tx_index < CSR_SEG_SIZE - 1)
00127 {
00128
00129
00130
00131
00132
00133
00134
00135
00136 if (!send_zeroes)
00137 {
00138 SSPDR = csr_tx_buffer[csr_tx_index2];
00139 checksum += csr_tx_buffer[csr_tx_index2];
00140 if (++csr_tx_index2 == CSR_TX_BUFFER_SIZE) {csr_tx_index2 = 0;}
00141 }
00142 else
00143 {
00144 SSPDR = 0;
00145 }
00146 }
00147 else if (tx_index == CSR_SEG_SIZE - 1)
00148 {
00149 SSPDR = checksum;
00150 SSPIMSC = (SSPIMSC & ~(1<<3)) & (0xF);
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 else
00165 {
00166 error_occurred_fiq(ERROR_SSP_TX_BUF_IDX);
00167 b10a_mcu_red_led_blink(50);
00168
00169 }
00170 ++tx_index;
00171 }
00172
00173 if (SSPMIS & (1<<1))
00174 {
00175 SSPICR = (1<<1);
00176 }
00177
00178 if (SSPMIS & (1<<0))
00179 {
00180 b10a_mcu_red_led_blink(50);
00181 error_occurred_fiq(ERROR_SSP_RX_FIFO);
00182 SSPICR = (1<<0);
00183
00184 }
00185 }
00186 }
00188
00190
00191 unsigned short int csr_pop_ssp_frame(CAN_FRAME * frameptr)
00192 {
00193 static unsigned short int j = 0;
00194 unsigned short int i, cal_checksum, no_data;
00195
00196 no_data = 1;
00197
00198
00199 if (csr_rx_index2 != csr_rx_index1)
00200 {
00201
00202 if (j == 0)
00203 {
00204 while (1)
00205 {
00206
00207 if (csr_rx_buffer[csr_rx_index2][0] != 0)
00208 {
00209 cal_checksum = 1;
00210 for (i = 0; i < CSR_SEG_SIZE - 1; ++i)
00211 {
00212 cal_checksum += csr_rx_buffer[csr_rx_index2][i];
00213 }
00214
00215 if (cal_checksum == csr_rx_buffer[csr_rx_index2][CSR_SEG_SIZE - 1])
00216 {
00217 b10a_mcu_green_led_blink(50);
00218 goto PARSE;
00219
00220 }
00221 else
00222 {
00223 error_occurred(ERROR_SSP_BAD_CHKSM);
00224 b10a_mcu_red_led_blink(50);
00225 }
00226 }
00227
00228
00229 if (csr_rx_index2 == CSR_RX_SEG_NUM - 1)
00230 {
00231 csr_rx_index2 = 0;
00232 }
00233 else
00234 {
00235 ++csr_rx_index2;
00236 }
00237
00238
00239 if (csr_rx_index2 == csr_rx_index1)
00240 {
00241 return no_data;
00242 }
00243 }
00244 }
00245
00246 PARSE:
00247
00248
00249
00250
00251 if ((csr_rx_buffer[csr_rx_index2][j] & 0xF800) == 0x7800)
00252 {
00253
00254 frameptr->chan = CHAN_SSP;
00255 frameptr->addr = csr_rx_buffer[csr_rx_index2][j];
00256
00257
00258 frameptr->dlc = 8;
00259 frameptr->rtr = csr_rx_buffer[csr_rx_index2][j++] >> 15;
00260 frameptr->payload.s.s2 = csr_rx_buffer[csr_rx_index2][j++];
00261 frameptr->payload.s.s1 = csr_rx_buffer[csr_rx_index2][j++];
00262 frameptr->payload.s.s4 = csr_rx_buffer[csr_rx_index2][j++];
00263 frameptr->payload.s.s3 = csr_rx_buffer[csr_rx_index2][j++];
00264 no_data = 0;
00265 }
00266 else if ((csr_rx_buffer[csr_rx_index2][j] & 0xF800) == 0xF800)
00267 {
00268
00269 j += 5;
00270 }
00271 else
00272 {
00273 j = CSR_SEG_SIZE - 1;
00274 }
00275
00276 if (j == CSR_SEG_SIZE - 1)
00277 {
00278
00279 if (csr_rx_index2 == CSR_RX_SEG_NUM - 1)
00280 {
00281 csr_rx_index2 = 0;
00282 }
00283 else
00284 {
00285 ++csr_rx_index2;
00286 }
00287
00288
00289 j = 0;
00290 }
00291 else if (j > CSR_SEG_SIZE - 1)
00292 {
00293 error_occurred(ERROR_SSP_RX_BUF_IDX);
00294 b10a_mcu_red_led_blink(50);
00295 }
00296 return no_data;
00297 }
00298 else
00299 {
00300 return no_data;
00301 }
00302 }
00304
00306 unsigned short int csr_push_ssp_frame(CAN_FRAME * frame)
00307 {
00308 short int temp_index1;
00309
00310 temp_index1 = csr_tx_index1;
00311
00312
00313 csr_tx_buffer[temp_index1] = frame->addr
00314 | (0xF << 11)
00315 | (frame->rtr << 15)
00316 ;
00317
00318 if (++temp_index1 == CSR_TX_BUFFER_SIZE) {temp_index1 = 0;}
00319
00320 if (temp_index1 == csr_tx_index2)
00321 {
00322 error_occurred(ERROR_SSP_TX_BUF_OF);
00323 return 1;
00324 }
00325
00326
00327 csr_tx_buffer[temp_index1] = frame->payload.s.s2;
00328
00329 if (++temp_index1 == CSR_TX_BUFFER_SIZE) {temp_index1 = 0;}
00330
00331 if (temp_index1 == csr_tx_index2)
00332 {
00333 error_occurred(ERROR_SSP_TX_BUF_OF);
00334 return 1;
00335 }
00336
00337
00338 csr_tx_buffer[temp_index1] = frame->payload.s.s1;
00339
00340 if (++temp_index1 == CSR_TX_BUFFER_SIZE) {temp_index1 = 0;}
00341
00342 if (temp_index1 == csr_tx_index2)
00343 {
00344 error_occurred(ERROR_SSP_TX_BUF_OF);
00345 return 1;
00346 }
00347
00348
00349 csr_tx_buffer[temp_index1] = frame->payload.s.s4;
00350
00351 if (++temp_index1 == CSR_TX_BUFFER_SIZE) {temp_index1 = 0;}
00352
00353 if (temp_index1 == csr_tx_index2)
00354 {
00355 error_occurred(ERROR_SSP_TX_BUF_OF);
00356 return 1;
00357 }
00358
00359
00360 csr_tx_buffer[temp_index1] = frame->payload.s.s3;
00361
00362 if (++temp_index1 == CSR_TX_BUFFER_SIZE) {temp_index1 = 0;}
00363
00364 if (temp_index1 == csr_tx_index2)
00365 {
00366 error_occurred(ERROR_SSP_TX_BUF_OF);
00367 return 1;
00368 }
00369
00370 csr_tx_index1 = temp_index1;
00371
00372 return 0;
00373 }