microstrain_imu.c
Go to the documentation of this file.00001
00039 #include <includes.h>
00040
00041
00042 unsigned char msimu_read_buffer[128];
00043 unsigned char msimu_rb_index1 = 0;
00044 unsigned char msimu_rb_index2 = 0;
00045
00046
00047
00048 unsigned char msimu_trans_buffer[128];
00049 unsigned char msimu_tb_index1 = 0;
00050 unsigned char msimu_tb_index2 = 0;
00051
00052
00053 unsigned long msimu_data_buffer[20];
00054
00055 MSIMU_COMMAND msimu_continuous_command;
00056
00057
00063 void msimu_init(MSIMU_COMMAND command)
00064 {
00065 msimu_set_continuous(command);
00066 }
00067
00075 int msimu_get_length(MSIMU_COMMAND command){
00076 int length = 0;
00077 switch (command){
00078 case MSIMU_RAW_ACCEL_ANG_RATE: length = 31; break;
00079 case MSIMU_EULER_ANGS_ANG_RATE: length = 31; break;
00080 default: length = 31;
00081 }
00082 return length;
00083 }
00084
00089 void msimu_isr(void) __irq
00090 {
00091 unsigned long int interrupt_id, line_status;
00092 unsigned short int i;
00093
00094 interrupt_id = U1IIR;
00095
00096
00097 switch (interrupt_id & 0xE)
00098 {
00099 case 4:
00100 for (i=0;i<8;i++)
00101 {
00102 line_status = U1LSR;
00103 msimu_read_buffer[msimu_rb_index1] = U1RBR;
00104 msimu_rb_index1 = (++msimu_rb_index1 & 127);
00105
00106 if (msimu_rb_index1 == msimu_rb_index2)
00107 {
00108 msimu_rb_index1 = (--msimu_rb_index1 & 127);
00109 error_occurred(ERROR_MSIMU_RX_OF);
00110 }
00111 }
00112 break;
00113
00114 case 12:
00115 line_status = U1LSR;
00116 msimu_read_buffer[msimu_rb_index1] = U1RBR;
00117 msimu_rb_index1 = (++msimu_rb_index1 & 127);
00118
00119 if (msimu_rb_index1 == msimu_rb_index2)
00120 {
00121 msimu_rb_index1 = (--msimu_rb_index1 & 127);
00122 error_occurred(ERROR_MSIMU_RX_OF);
00123 }
00124
00125 break;
00126
00127 case 2:
00128 for (i=0;i<16;i++)
00129 {
00130 if (msimu_tb_index1 != msimu_tb_index2)
00131 {
00132 U1THR = msimu_trans_buffer[msimu_tb_index2];
00133 msimu_tb_index2 = ((++msimu_tb_index2) & 127);
00134 }
00135 else
00136 {
00137 U1IER &= ~(1<<1);
00138 break;
00139 }
00140 }
00141 break;
00142
00143 case 6:
00144 line_status = U1LSR;
00145 switch(line_status & 30){
00146 case 1:
00147
00148 break;
00149 case 2:
00150
00151 break;
00152 case 4:
00153
00154 break;
00155 case 8:
00156
00157 break;
00158 }
00159 break;
00160
00161 }
00162
00163 VICVectAddr = 0;
00164 }
00165
00170 void msimu_set_continuous(MSIMU_COMMAND command){
00171 unsigned char c = command;
00172 unsigned char bytes[] = {0xC4,0xC1,0x29,0x00};
00173 bytes[3] = c;
00174 msimu_continuous_command = command;
00175 msimu_send_all(bytes, 4);
00176 }
00177
00183 void msimu_send_all(unsigned char *bytes, unsigned int length)
00184 {
00185 unsigned int i;
00186 if ((length <= 16))
00187 {
00188 if (U1LSR & (1<<5)){
00189 for (i=0;i<length;i++){
00190 U1THR = *(bytes + i);
00191 }
00192 } else {
00193 error_occurred(ERROR_MSIMU_TX_FULL);
00194 }
00195 } else {
00196 error_occurred(ERROR_MSIMU_NUM_BYT);
00197 }
00198 }
00199
00207 float msimu_get_data_float(MSIMU_DATA index){
00208 void *vpointer;
00209 float *fpointer;
00210 float data;
00211 vpointer = msimu_data_buffer + index;
00212 fpointer = vpointer;
00213 data = *fpointer;
00214 return data;
00215 }
00216
00224 int msimu_get_data_int(MSIMU_DATA index){
00225 return (int)msimu_data_buffer[index];
00226 }
00227
00232 void msimu_update(void){msimu_parse_buffer();}
00233
00238 void msimu_parse_buffer(void)
00239 {
00240 unsigned short int tempindex1 = 0, tempindex2 = 0, i;
00241 unsigned long int cal_checksum = 0, rec_checksum = 0, ulitemp = 0;
00242 int data_count = 0;
00243 int packet_length = 0;
00244 packet_length = msimu_get_length(msimu_continuous_command);
00245
00246 tempindex2 = msimu_rb_index2;
00247 if (tempindex2 > msimu_rb_index1)
00248 {
00249 tempindex1 = msimu_rb_index1 + 128;
00250 }
00251 else
00252 {
00253 tempindex1 = msimu_rb_index1;
00254 }
00255
00256 while ((msimu_read_buffer[tempindex2 & 127] != msimu_continuous_command) && ((tempindex1 - tempindex2) > packet_length))
00257 {
00258 tempindex2++;
00259
00260
00261 }
00262
00263
00264 msimu_rb_index2 = (tempindex2 & 127);
00265
00266
00267
00268
00269
00270 if (msimu_read_buffer[msimu_rb_index2] == msimu_continuous_command && ((tempindex1 - tempindex2) > packet_length))
00271 {
00272
00273 cal_checksum = 0;
00274
00275 for (i = 0; i < (packet_length-2); i++)
00276 {
00277 cal_checksum += msimu_read_buffer[(msimu_rb_index2 + i) & 127];
00278 }
00279
00280 cal_checksum &= 0xFFFF;
00281
00282
00283 rec_checksum = msimu_read_buffer[(msimu_rb_index2 + packet_length - 1) & 127];
00284 rec_checksum |= ((msimu_read_buffer[(msimu_rb_index2 + packet_length - 2) & 127])<<8);
00285
00286
00287 if (cal_checksum == rec_checksum)
00288 {
00289 i = 1;
00290 while (i < packet_length - 2) {
00291 ulitemp = (unsigned long int)msimu_read_buffer[(msimu_rb_index2 + i++) & 127];
00292 ulitemp <<= 8;
00293 ulitemp |= (unsigned long int)msimu_read_buffer[(msimu_rb_index2 + i++) & 127];
00294 ulitemp <<= 8;
00295 ulitemp |= (unsigned long int)msimu_read_buffer[(msimu_rb_index2 + i++) & 127];
00296 ulitemp <<= 8;
00297 ulitemp |= (unsigned long int)msimu_read_buffer[(msimu_rb_index2 + i++) & 127];
00298 msimu_data_buffer[data_count] = ulitemp;
00299 data_count++;
00300 }
00301
00302
00303 msimu_rb_index2 = ((msimu_rb_index2 + packet_length) & 127);
00304 }
00305
00306 else
00307 {
00308 msimu_rb_index2 = (++msimu_rb_index2 & 127);
00309 }
00310
00311 }
00312 }