PixArt / Mbed OS 5101_referenceCode
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPIcommFunctions.h Source File

SPIcommFunctions.h

00001 //=========================================================================
00002 //Communication pinouts for serial COM port, SPI, and interrupts
00003 //=========================================================================
00004 static Serial pc(USBTX, USBRX);                     //PC serial communication.
00005 static SPI spi(p23, p24, p25);                      //MOSI, MISO, SCLK  (because this is 3-Wire SPI, we tie MOSI and MISO together on the SDIO pin, but separate them when we get to the MCU).
00006 static DigitalOut cs(p22);                          //Chip select pin.
00007 static DigitalOut LDP(p20);                         //Laser diode pin.
00008 
00009 
00010 //=========================================================================
00011 //Variables and arrays used for communications and data storage
00012 //=========================================================================
00013 int16_t deltaX = 0;                                 //Stores X-axis output data.
00014 int16_t deltaY = 0;                                 //Stores Y-axis output data.
00015 int16_t totalX, totalY = 0;                         //Stores the total deltaX and deltaY moved during runtime.
00016 uint8_t frameCounter = 0;                           //Looping variable to track when we have grabbed 8 frames of data.
00017 uint16_t imageQuality[8];                           //Stores 8 frames of image data to determine when quality changes to a point where we need to switch modes (LED/laser).
00018 uint16_t imageQuality_total = 0;                    //Stores the sum of all raw values from the 8 frames from imageQuality[8].
00019 uint8_t mode = 0;                                   //Modes: 0 = Laser, 1 = LED.    Laser is default.
00020 #define laser2LED_threshold 0x700                   //Stores threshold level for when we need to swap to LED mode.
00021 #define LED2laser_threshold 0x500                   //Stores threshold level for when we need to swap to laser mode.
00022 
00023 uint8_t currentBank;                                //Stores current register bank.
00024 
00025 
00026 //=========================================================================
00027 //Functions used to communicate with the sensor and grab/print data
00028 //=========================================================================
00029 uint8_t readRegister(uint8_t addr);
00030 //This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00.
00031 
00032 void writeRegister(uint8_t addr, uint8_t data);
00033 //This function takes an 8-bit address and 8-bit data. Writes the given data to the given address.
00034 
00035 void load(const uint8_t array[][2], uint8_t arraySize);
00036 //Takes an array of registers/data (found in registerArrays.h) and their size and writes in all the values.
00037 
00038 void checkMode(void);
00039 //This function checks the image quality of each frame and switches between LED and laser modes when necessary.
00040 
00041 void grabData(void);
00042 //Takes data from the sensor and stores it into variables deltaX and deltaY.
00043 
00044 void printData(void);
00045 //Prints the deltaX and deltaY values to a serial terminal whenever the values change.
00046 
00047 
00048 
00049 
00050 
00051 //=========================================================================
00052 //Functions definitions
00053 //=========================================================================
00054 uint8_t readRegister(uint8_t addr)
00055 {
00056     cs = 0;                                 //Set chip select low/active
00057     addr = addr & 0x7F;                     //Set MSB to 0 to indicate read operation
00058     spi.write(addr);                        //Write the given address
00059     wait_ms(1);
00060     uint8_t data_read = spi.write(0x00);    //Throw dummy byte after sending address to receieve data
00061     cs = 1;                                 //Set chip select back to high/inactive
00062     return data_read;                       //Returns 8-bit data from register
00063 }
00064 
00065 
00066 //=========================================================================
00067 void writeRegister(uint8_t addr, uint8_t data)
00068 {
00069     cs = 0;                         //Set chip select low/active
00070     addr = addr | 0x80;             //Set MSB to 1 to indicate write operation
00071     spi.write(addr);                //Write the given address
00072     spi.write(data);                //Write the given data
00073     cs = 1;                         //Set chip select back to high/inactive
00074     
00075     if(addr == 0x7F)                //You can't read register 0x7F for the current bank so we store the value before writing it
00076     {
00077         currentBank = data;         //Store the current bank for printing later.
00078         //pc.printf("B:%2X, R:%2X, D:%2X\n\r", currentBank, (addr & 0x7F), currentBank);
00079             //Uncomment this line and the other print line below for debugging. Prints every register bank change.
00080     }
00081     
00082     else
00083     {
00084         //pc.printf("B:%2X, R:%2X, D:%2X\n\r", currentBank, (addr & 0x7F), readRegister(addr));
00085             //Uncomment this line and the other print line above for debugging. Prints every register write operation.
00086     }
00087 }
00088 
00089 
00090 //=========================================================================
00091 void load(const uint8_t array[][2], uint8_t arraySize)
00092 {
00093     for(uint8_t q = 0; q < arraySize; q++)
00094     {
00095         writeRegister(array[q][0], array[q][1]);    //Writes the given array of registers/data.
00096     }
00097 }
00098 
00099 
00100 //=========================================================================
00101 void checkMode(void)
00102 {
00103     uint16_t data_msb, data_lsb = 0;                 //These variables store the high and low bits of image quality data.
00104     imageQuality_total = 0;                         //This variable holds the sum of 8 frames of data.
00105     
00106     data_msb = readRegister(0x75);                  //Reads upper 8 bits of image quality.
00107     data_lsb = readRegister(0x76);                  //Reads lower 8 bits of image quality.
00108     imageQuality[frameCounter] = data_msb*256 + data_lsb;   //Combines the low/high bits to be a single element in this array.
00109     
00110     if(frameCounter == 7)                           //We check image quality every EIGHT samples before determining if a mode-switch is necessary.
00111     {
00112         for(int i=0; i<8; i++)                      //Sums up 8 frames of image quality data into 1 variable.
00113         {
00114             imageQuality_total = imageQuality_total + imageQuality[i];
00115         }
00116         
00117         if(mode == 1 && imageQuality_total < LED2laser_threshold)       //Check the condition for when we need to change to laser.
00118         {
00119             load(modeLaser, modeLaser_size);        //Loads the register array that has the settings for laser mode.
00120             mode = 0;                               //Sets the mode-tracker to be 0 to reflect laser mode.
00121             LDP = 0;                                //Sets the laser diode GPIO pin to be LOW (active because of PMOS pull-up).
00122             wait_ms(40);                            //Delay for timing.
00123             writeRegister(0x03, 0x00);              //Unlisted...
00124             
00125             //pc.printf("Image Quality: %4X\n\r", imageQuality_total);
00126             //pc.printf("Now in laser mode. Value of 'mode': %1X\n\r", mode);                
00127             //Uncomment these lines and the other print lines below for debugging. Prints image quality and tracks mode-switches.
00128         }
00129         
00130         if(mode == 0 && imageQuality_total < laser2LED_threshold)  //Check the condition for when we need to change to LED.
00131         {
00132             load(modeLED, modeLED_size);            //Loads the register array that has the settings for LED mode.
00133             mode = 1;                               //Sets the mode-tracker to be 1 to reflect LED mode.
00134             LDP = 1;                                //Sets the laser diode GPIO pin to be HIGH (inactive because of PMOS pull-up).
00135             wait_ms(40);                            //Delay for timing.
00136             writeRegister(0x03, 0x00);              //Unlisted...
00137             
00138             //pc.printf("Image Quality: %4X\n\r", imageQuality_total);
00139             //pc.printf("Now in LED mode. Value of 'mode': %1X\n\r", mode);
00140             //Uncomment these lines and the other print lines above for debugging. Prints image quality and tracks mode-switches.
00141         }
00142     }
00143     
00144     frameCounter = (frameCounter + 1) & 0x07;       //This variable loops from zero to 7 without using a looper or variable. Every time this function is called, it moves up 1 towards 7 or resets to zero.
00145 }
00146 
00147 
00148 //=========================================================================
00149 void grabData(void)
00150 {
00151     int16_t deltaX_high, deltaX_low = 0;            //These four variables store the low/high bits of the deltaX and deltaY data.
00152     int16_t deltaY_high, deltaY_low = 0;
00153     
00154     deltaX_low = (int16_t)readRegister(0x03);       //Grabbing the data from registers.
00155     deltaY_low = (int16_t)readRegister(0x04);
00156     deltaX_high = ((int16_t)readRegister(0x11))<<8; //Shifts high bits left by 8 so that we can combine high and low bytes together.
00157     deltaY_high = ((int16_t)readRegister(0x12))<<8;
00158     
00159     deltaX = deltaX_high | deltaX_low;              //Combines the low/high bits of X and Y to be one variable.
00160     deltaY = deltaY_high | deltaY_low;
00161 }
00162 
00163 
00164 //=========================================================================
00165 void printData(void)
00166 {
00167     if((deltaX != 0) || (deltaY != 0))      //If there is deltaX or deltaY movement, print the data.
00168     {
00169         totalX += deltaX;
00170         totalY += deltaY;
00171         
00172         pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY);    //Prints each individual count of deltaX and deltaY.
00173         pc.printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY);  //Prints the total movement made during runtime.
00174     }
00175     
00176     deltaX = 0;                             //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten.
00177     deltaY = 0;
00178 }