microstrain_imu.c File Reference

Control code for the Microstrain IMU on the UI board. More...

Go to the source code of this file.

Functions

float msimu_get_data_float (MSIMU_DATA index)
 Returns data from the IMU as a float.
int msimu_get_data_int (MSIMU_DATA index)
 Returns data from the IMU as an int.
int msimu_get_length (MSIMU_COMMAND command)
 Returns the length of a return packet associated with a given command.
void msimu_init (MSIMU_COMMAND command)
 Initializes the microstrin IMU module.
void msimu_isr (void)
 The interrupt service routine for communicating with the IMU over UART.
void msimu_parse_buffer (void)
 Parses the UART buffer to check for a new packet, saving the data if there has been a new packet.
void msimu_send_all (unsigned char *bytes, unsigned int length)
 Sends bytes to the IMU over UART.
void msimu_set_continuous (MSIMU_COMMAND command)
 Starts the IMU in continuous mode with the given command.
void msimu_update (void)
 Updates the IMU module.

Variables

MSIMU_COMMAND msimu_continuous_command
unsigned long msimu_data_buffer [20]
unsigned char msimu_rb_index1 = 0
unsigned char msimu_rb_index2 = 0
unsigned char msimu_read_buffer [128]
unsigned char msimu_tb_index1 = 0
unsigned char msimu_tb_index2 = 0
unsigned char msimu_trans_buffer [128]

Detailed Description

Control code for the Microstrain IMU on the UI board.

Example hardware setup for communicating with IMU over UART:

  // *******************************************************************************
  // Initialize Microstrain IMU over UART1
  // *******************************************************************************
  U1LCR=0x83;  //Step 1: DLAB=1, enable baud rate adjustments
  //Set to 8 data bits, 1 stop bit, no parity
  //Step 2: Set baud rate divisor and fractional baud rate divisors
  U1DLL=25;   //Baud rate divisor for 115200 kbaud, with (1+0.3) fractional divider 
  U1DLM=0;    //and peripheral clock = 60MHz.
  U1FDR = 3;    // DIVADDVAL = 3
  U1FDR |= 10<<4; // MULTVAL = 10
  //U1FCR = 0x81;     // Enable receive and transmit FIFO; interrupt on receive at 8 bytes.
  U1FCR = 0x1;    // Enable FIFO
  U1FCR = 0x87;   //Clear FIFOs and set to generate interrupt when RX FIFO reaches 8 bytes.
  U1MCR = 0;      // Loopback mode, CTS and RTS flow control disabled.
  U1ACR = 0;      // Autobaud disabled
  U1TER = 0x80;     // Transmit enabled
  U1LCR &= ~(1<<7); // Step 3: DLAB=0, disable baud rate adjustments
  PINSEL0 |= 1<<16;    // UART1 TXD active on MCU pin P0[8]
  PINSEL0 &= ~(1<<17); //
  PINSEL0 |= 1<<18;    // UART1 RXD active on MCU pin P0[9]
  PINSEL0 &= ~(1<<19); //
  U1IER = (1<<0); // enable RBR interrupts
  U1ACR = (1<<8) | (1<<9); //clear the corresponding interrupt in the U1IIR
Author:
Nicolas Williamson
Jason Cortell
Date:
December 2009

Definition in file microstrain_imu.c.


Function Documentation

float msimu_get_data_float ( MSIMU_DATA  index  ) 

Returns data from the IMU as a float.

Parameters:
index The index of the data from the packet.
See also:
MSIMU_DATA
Returns:
The requested data interpreted as a float.
Note:
This function only interprets the bits coming in over the UART as a floating point number, it does NOT convert from an integer (1) to float (1.0).

Definition at line 207 of file microstrain_imu.c.

00207                                             {
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 }

int msimu_get_data_int ( MSIMU_DATA  index  ) 

Returns data from the IMU as an int.

Parameters:
index The index of the data from the packet.
See also:
MSIMU_DATA
Returns:
The requested data interpreted as an int.
Note:
This function only interprets the bits coming in over the UART as an integer, it does NOT convert from an float (1.4) to int (1).

Definition at line 224 of file microstrain_imu.c.

00224                                         {
00225   return (int)msimu_data_buffer[index];
00226 }

int msimu_get_length ( MSIMU_COMMAND  command  ) 

Returns the length of a return packet associated with a given command.

Note:
Not all commands have been added. If you do not see yours listed, add it to the switch statement.
Parameters:
command The command to get the return packet length of.
Returns:
Length of the expected return packet, or 31 if command is unknown.

Definition at line 75 of file microstrain_imu.c.

Referenced by msimu_parse_buffer().

00075                                            {
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 }

void msimu_init ( MSIMU_COMMAND  command  ) 

Initializes the microstrin IMU module.

Parameters:
command The data that the IMU will continuously calculate and send to the board over UART.

Definition at line 63 of file microstrain_imu.c.

References msimu_set_continuous().

00064 {
00065   msimu_set_continuous(command);
00066 }

void msimu_isr ( void   ) 

The interrupt service routine for communicating with the IMU over UART.

Stores incoming data into buffers.

Definition at line 89 of file microstrain_imu.c.

References error_occurred().

00090 { 
00091   unsigned long int interrupt_id, line_status;
00092   unsigned short int i;
00093 
00094   interrupt_id = U1IIR;
00095   //test_counter++;
00096   
00097   switch (interrupt_id & 0xE) //Look at bits 1 - 3 of U1IIR
00098   {
00099    case 4:  //receive data available (>=8 bytes in hardware receive FIFO, as set in U1FCR)
00100      for (i=0;i<8;i++)
00101      {
00102        line_status = U1LSR;
00103        msimu_read_buffer[msimu_rb_index1] = U1RBR;  //Put new data in next buffer location
00104        msimu_rb_index1 = (++msimu_rb_index1 & 127);   //Increment index; roll over to 0 after reaching 127  
00105 
00106        if (msimu_rb_index1 == msimu_rb_index2)      //software receive software overflow
00107        {
00108          msimu_rb_index1 = (--msimu_rb_index1 & 127); //overwrite most recent byte
00109          error_occurred(ERROR_MSIMU_RX_OF);
00110        }  
00111      }
00112    break;
00113 
00114    case 12: //character time-out - at least one left-over byte in hardware receive FIFO
00115      line_status = U1LSR;
00116      msimu_read_buffer[msimu_rb_index1] = U1RBR;  //Put new data in next buffer location
00117      msimu_rb_index1 = (++msimu_rb_index1 & 127);   //Increment index; roll over to 0 after reaching 127  
00118 
00119      if (msimu_rb_index1 == msimu_rb_index2)    //software receive buffer overflow
00120      {
00121        msimu_rb_index1 = (--msimu_rb_index1 & 127);  //overwrite most recent byte
00122        error_occurred(ERROR_MSIMU_RX_OF);
00123      }  
00124    
00125    break;
00126    
00127    case 2:  //transmitter hardware FIFO empty
00128      for (i=0;i<16;i++)   //Move up to 16 bytes (FIFO capacity) to FIFO
00129      {
00130        if (msimu_tb_index1 != msimu_tb_index2)  //Transmit software FIFO not empty
00131        {
00132        U1THR = msimu_trans_buffer[msimu_tb_index2]; //Transmit oldest byte from software FIFO
00133        msimu_tb_index2 = ((++msimu_tb_index2) & 127);   //Increment Index2, roll over at 128
00134        }
00135        else
00136        {
00137        U1IER &= ~(1<<1);    //Disable tranmitter holding register empty interrupt, bit 1
00138        break;
00139        }
00140      }   
00141    break;
00142    
00143    case 6:  //receive line status interrupt
00144     line_status = U1LSR;
00145     switch(line_status & 30){
00146       case 1: //Overrun Error(OE)
00147         //mcu_led_red_blink(500);
00148         break;
00149       case 2: //Parity Error(PE)
00150         //mcu_led_green_blink(500);
00151         break;
00152       case 4: //Framing Error(FE)
00153         //mcu_led_red_blink(2000);
00154         break;
00155       case 8: //Break Interrupt(BI)
00156         //mcu_led_green_blink(2000);
00157         break;
00158     }
00159    break; 
00160        
00161   }      
00162   
00163   VICVectAddr = 0;    //Reset interrupt priorities
00164 }  

void msimu_send_all ( unsigned char *  bytes,
unsigned int  length 
)

Sends bytes to the IMU over UART.

Parameters:
bytes An array of bytes to send.
length The length of the array of bytes.

Definition at line 183 of file microstrain_imu.c.

References error_occurred().

Referenced by msimu_set_continuous().

00184 {
00185   unsigned int i;
00186   if ((length <= 16))   //length must be smaller than 16-byte FIFO and FIFO must be empty
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 }

void msimu_set_continuous ( MSIMU_COMMAND  command  ) 

Starts the IMU in continuous mode with the given command.

Parameters:
command The command for the IMU to continuously compute.

Definition at line 170 of file microstrain_imu.c.

References msimu_send_all().

Referenced by msimu_init().

00170                                                 {
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 }

void msimu_update ( void   ) 

Updates the IMU module.

Parses data from UART and check if there is a new packet. Call this from every row of the schedule.

Definition at line 232 of file microstrain_imu.c.

References msimu_parse_buffer().

00232 {msimu_parse_buffer();}

Generated on Tue Jun 29 16:36:15 2010 by  doxygen 1.6.3