PixArt / Mbed OS ADBM-A350_referenceCode
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPIcommFunctions.h Source File

SPIcommFunctions.h

00001 #define SPImode                                     //Used to check if the sensor is in I2C mode or SPI mode.
00002 
00003 //=========================================================================
00004 //Communication pinouts for serial COM port, SPI, and interrupts
00005 //=========================================================================
00006 static Serial pc(USBTX, USBRX);                     //PC comm
00007 static SPI spi(p23, p24, p25);                      //mosi, miso, sclk
00008 static DigitalOut cs(p22);                          //chip select
00009 static DigitalOut shutdown(p20);                    //Shutdown pin
00010 static DigitalOut IO_sel(p19);                      //IO interface selection pin (I2C and SPI)
00011 
00012 
00013 //=========================================================================
00014 //Variables and arrays used for communications and data storage
00015 //=========================================================================
00016 int8_t deltaX, deltaY;                              //Stores the value of one individual motion report.
00017 int totalX, totalY = 0;                          //Stores the total deltaX and deltaY moved during runtime.
00018 
00019 
00020 //=========================================================================
00021 //Functions used to communicate with the sensor and grab/print data
00022 //=========================================================================
00023 uint8_t readRegister(uint8_t addr);
00024 //This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00.
00025 
00026 void writeRegister(uint8_t addr, uint8_t data);
00027 //This function takes an 8-bit address and 8-bit data. Writes the given data to the given address.
00028 
00029 void load(const uint8_t array[][2], uint8_t arraySize);
00030 //Takes an array of registers/data (found in registerArrays.h) and their size and writes in all the values.
00031 
00032 void grabData(void);
00033 //Grabs the deltaX and deltaY information from the proper registers and formats it into the proper format.
00034 
00035 void printData(void);
00036 //Prints the data out to a serial terminal.
00037 
00038 
00039 
00040 
00041 
00042 //=========================================================================
00043 //Functions definitions
00044 //=========================================================================
00045 uint8_t readRegister(uint8_t addr)
00046 {
00047     cs = 0;                                 //Set chip select low/active
00048     addr = addr & 0x7F;                     //Set MSB to 0 to indicate read operation
00049     spi.write(addr);                        //Write the given address
00050     wait_us(5);
00051     uint8_t data_read = spi.write(0x00);    //Throw dummy byte after sending address to receieve data
00052     wait_us(30);
00053     cs = 1;                                 //Set chip select back to high/inactive
00054     return data_read;                       //Returns 8-bit data from register
00055 }
00056 
00057 
00058 //=========================================================================
00059 void writeRegister(uint8_t addr, uint8_t data)
00060 {
00061     cs = 0;                         //Set chip select low/active
00062     addr = addr | 0x80;             //Set MSB to 1 to indicate write operation
00063     spi.write(addr);                //Write the given address
00064     spi.write(data);                //Write the given data
00065     cs = 1;                         //Set chip select back to high/inactive
00066     wait_us(30);                    //Wait time between write commands.
00067     
00068     //pc.printf("R:%2X, D:%2X\n\r", addr, readRegister(addr));
00069             //Uncomment this line for debugging. Prints every register write operation.
00070 }
00071 
00072 
00073 //=========================================================================
00074 void load(const uint8_t array[][2], uint8_t arraySize)
00075 {
00076     for(uint8_t q = 0; q < arraySize; q++)
00077     {
00078         writeRegister(array[q][0], array[q][1]);    //Writes the given array of registers/data.
00079     }
00080 }
00081 
00082 
00083 //=========================================================================
00084 void grabData(void)
00085 {
00086     deltaX = readRegister(0x03);        //Grabs data from the proper registers.
00087     deltaY = readRegister(0x04);
00088     writeRegister(0x02, 0x00);          //Clear EVENT and motion registers.
00089 }
00090 
00091 
00092 //=========================================================================
00093 void printData(void)
00094 {
00095     if((deltaX != 0) || (deltaY != 0))      //If there is deltaX or deltaY movement, print the data.
00096     {
00097         totalX += deltaX;
00098         totalY += deltaY;
00099         
00100         pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY);    //Prints each individual count of deltaX and deltaY.
00101         pc.printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY);  //Prints the total movement made during runtime.
00102     }
00103     
00104     deltaX = 0;                             //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten.
00105     deltaY = 0;
00106 }