Reference firmware for PixArt's PAA5101 sensor and evaluation board. "Hello World" and "Library" contain the exact same files. Please import just one of the two into your mBed compiler as a new program and not as a library.

Welcome to the code repository for PixArt's PAA5101 sensor and evaluation board.

For general information about this product, please visit this product's components page here:
https://os.mbed.com/components/PAA5101-Floor-Tracking-Sensor-with-Wide-/

For guides and tips on how to setup and evaluate the PAA5101 sensor with the Nordic nRF52-DK microcontroller using this reference code, please visit this guide:
https://os.mbed.com/teams/PixArt/code/5101_referenceCode/wiki/Guide-for-nRF52-DK-Platform

For guides and tips on how to setup and evaluate the PAA5101 sensor with any microcontroller using this reference code, please visit this guide:
https://os.mbed.com/teams/PixArt/code/5101_referenceCode/wiki/Guide-for-Any-Platform

Files at this revision

API Documentation at this revision

Comitter:
PixArtVY
Date:
Mon Mar 19 21:18:40 2018 +0000
Child:
1:469063631a05
Commit message:
First release.

Changed in this revision

commHeaders/SPIcommFunctions.h Show annotated file Show diff for this revision Revisions of this file
commHeaders/registerArrays.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/commHeaders/SPIcommFunctions.h	Mon Mar 19 21:18:40 2018 +0000
@@ -0,0 +1,195 @@
+//=========================================================================
+//Communication pinouts for serial COM port, SPI, and interrupts
+//=========================================================================
+static Serial pc(USBTX, USBRX);                     //PC serial communication.
+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).
+static DigitalOut cs(p22);                          //Chip select pin.
+static DigitalOut LDP(p20);                         //Laser diode pin.
+
+
+//=========================================================================
+//Variables and arrays used for communications and data storage
+//=========================================================================
+int16_t deltaX = 0;                                 //Stores X-axis output data.
+int16_t deltaY = 0;                                 //Stores Y-axis output data.
+
+uint8_t frameCounter = 0;                           //Looping variable to track when we have grabbed 8 frames of data.
+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).
+uint16_t imageQuality_total = 0;                    //Stores the sum of all raw values from the 8 frames from imageQuality[8].
+uint8_t imageQualityOK = 0;
+uint8_t mode = 0;                                   //Modes: 0 = Laser, 1 = LED.    Laser is default.
+#define laser2LED_threshold 0x700                   //Stores threshold level for when we need to swap to LED mode.
+#define LED2laser_threshold 0x500                   //Stores threshold level for when we need to swap to laser mode.
+
+uint8_t currentBank;                                //Stores current register bank.
+
+
+//=========================================================================
+//Functions used to communicate with the sensor and grab/print data
+//=========================================================================
+uint8_t readRegister(uint8_t addr);
+//This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00.
+
+void writeRegister(uint8_t addr, uint8_t data);
+//This function takes an 8-bit address and 8-bit data. Writes the given data to the given address.
+
+void load(const uint8_t array[][2], uint8_t arraySize);
+//Takes an array of registers/data (found in registerArrays.h) and their size and writes in all the values.
+
+void checkMode(void);
+//This function checks the image quality of each frame and switches between LED and laser modes when necessary.
+
+void grabData(void);
+//Takes data from the sensor and stores it into variables deltaX and deltaY.
+
+void printData(void);
+//Prints the deltaX and deltaY values to a serial terminal whenever the values change.
+
+
+
+
+
+//=========================================================================
+//Functions definitions
+//=========================================================================
+uint8_t readRegister(uint8_t addr)
+{
+    cs = 0;                                 //Set chip select low/active
+    addr = addr & 0x7F;                     //Set MSB to 0 to indicate read operation
+    spi.write(addr);                        //Write the given address
+    wait_ms(1);
+    uint8_t data_read = spi.write(0x00);    //Throw dummy byte after sending address to receieve data
+    cs = 1;                                 //Set chip select back to high/inactive
+    return data_read;                       //Returns 8-bit data from register
+}
+
+
+//=========================================================================
+void writeRegister(uint8_t addr, uint8_t data)
+{
+    cs = 0;                         //Set chip select low/active
+    addr = addr | 0x80;             //Set MSB to 1 to indicate write operation
+    spi.write(addr);                //Write the given address
+    spi.write(data);                //Write the given data
+    cs = 1;                         //Set chip select back to high/inactive
+    
+    if(addr == 0x7F)                //You can't read register 0x7F for the current bank so we store the value before writing it
+    {
+        currentBank = data;         //Store the current bank for printing later.
+        //pc.printf("B:%2X, R:%2X, D:%2X\n\r", currentBank, (addr & 0x7F), currentBank);
+            //Uncomment this line and the other print line below for debugging. Prints every register bank change.
+    }
+    
+    else
+    {
+        //pc.printf("B:%2X, R:%2X, D:%2X\n\r", currentBank, (addr & 0x7F), readRegister(addr));
+            //Uncomment this line and the other print line above for debugging. Prints every register write operation.
+    }
+    
+    wait_ms(2);
+}
+
+
+//=========================================================================
+void load(const uint8_t array[][2], uint8_t arraySize)
+{
+    for(uint8_t q = 0; q < arraySize; q++)
+    {
+        writeRegister(array[q][0], array[q][1]);    //Writes the given array of registers/data.
+    }
+}
+
+
+//=========================================================================
+void checkMode(void)
+{
+    uint16_t data_msb, data_lsb = 0;                 //These variables store the high and low bits of image quality data.
+    imageQuality_total = 0;                         //This variable holds the sum of 8 frames of data.
+    
+    data_msb = readRegister(0x75);                  //Reads upper 8 bits of image quality.
+    data_lsb = readRegister(0x76);                  //Reads lower 8 bits of image quality.
+    imageQuality[frameCounter] = data_msb*256 + data_lsb;   //Combines the low/high bits to be a single element in this array.
+    
+    if(frameCounter == 7)                           //We check image quality every EIGHT samples before determining if a mode-switch is necessary.
+    {
+        for(int i=0; i<8; i++)                      //Sums up 8 frames of image quality data into 1 variable.
+        {
+            imageQuality_total = imageQuality_total + imageQuality[i];
+        }
+        
+        if(imageQualityOK == 0 && imageQuality_total > LED2laser_threshold) //If imageQuality is over 0x500, we can continue to select the proper mode.
+        {
+            imageQualityOK = 1;
+            
+        }
+        
+        if(mode == 0 && imageQuality_total < LED2laser_threshold)  //If image quality is too low even with brighter laser setting, LED/laser states will keep oscillating and sensor can't function.
+        {
+            imageQualityOK = 0;
+            pc.printf("Image quality too low! Value: %4X\n\r", imageQuality_total);
+        }
+        
+        
+        if(imageQualityOK)
+        {
+            pc.printf("Image quality OK! Value: %4X\n\r", imageQuality_total);
+            
+            if(mode == 1 && imageQuality_total < LED2laser_threshold)       //Check the condition for when we need to change to laser.
+            {
+                load(modeLaser, modeLaser_size);        //Loads the register array that has the settings for laser mode.
+                mode = 0;                               //Sets the mode-tracker to be 0 to reflect laser mode.
+                LDP = 0;                                //Sets the laser diode GPIO pin to be LOW (active because of PMOS pull-up).
+                wait_ms(40);                            //Delay for timing.
+                writeRegister(0x03, 0x00);              //Delta_XL?????? TRY TO FIND OUT WHAT THIS REGISTER IS FOR**********************************************************
+                
+                //pc.printf("Image Quality: %4X\n\r", imageQuality_total);
+                //pc.printf("Now in laser mode. Value of 'mode': %1X\n\r", mode);
+                //Uncomment these lines and the other print lines below for debugging. Prints image quality and tracks mode-switches.
+            }
+            
+            if(mode == 0 && imageQuality_total < laser2LED_threshold)  //Check the condition for when we need to change to LED.
+            {
+                load(modeLED, modeLED_size);            //Loads the register array that has the settings for LED mode.
+                mode = 1;                               //Sets the mode-tracker to be 1 to reflect LED mode.
+                LDP = 1;                                //Sets the laser diode GPIO pin to be HIGH (inactive because of PMOS pull-up).
+                wait_ms(40);                            //Delay for timing.
+                writeRegister(0x03, 0x00);              //Delta_XL?????? TRY TO FIND OUT WHAT THIS REGISTER IS FOR**********************************************************
+                
+                //pc.printf("Image Quality: %4X\n\r", imageQuality_total);
+                //pc.printf("Now in LED mode. Value of 'mode': %1X\n\r", mode);
+                //Uncomment these lines and the other print lines above for debugging. Prints image quality and tracks mode-switches.
+            }
+        }
+    }
+    
+    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.
+}
+
+
+//=========================================================================
+void grabData(void)
+{
+    int16_t deltaX_high, deltaX_low = 0;            //These four variables store the low/high bits of the deltaX and deltaY data.
+    int16_t deltaY_high, deltaY_low = 0;
+    
+    deltaX_low = (int16_t)readRegister(0x03);       //Grabbing the data from registers.
+    deltaY_low = (int16_t)readRegister(0x04);
+    deltaX_high = ((int16_t)readRegister(0x11))<<8; //Shifts high bits left by 8 so that we can combine high and low bytes together.
+    deltaY_high = ((int16_t)readRegister(0x12))<<8;
+    
+    deltaX = deltaX_high | deltaX_low;              //Combines the low/high bits of X and Y to be one variable.
+    deltaY = deltaY_high | deltaY_low;
+}
+
+
+//=========================================================================
+void printData(void)
+{
+    if((deltaX != 0) || (deltaY != 0))      //If there is deltaX or deltaY movement, print the data.
+    {
+        pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY);
+    }
+    
+    deltaX = 0;                             //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten.
+    deltaY = 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/commHeaders/registerArrays.h	Mon Mar 19 21:18:40 2018 +0000
@@ -0,0 +1,205 @@
+const uint8_t initialize[][2] = {
+    { 0x7F,0x00 }, // Bank0, not allowed to perform SPIWriteRead
+    { 0x05,0xA8 },
+    { 0x07,0xCC },
+    { 0x0A,0x17 },
+    { 0x0D,0x05 },
+    { 0x0E,0x05 },
+    { 0x1B,0x43 },
+    { 0x25,0x2E },
+    { 0x26,0x35 },
+    { 0x2E,0x40 },
+    { 0x32,0x40 },
+    { 0x33,0x02 },
+    { 0x34,0x00 },
+    { 0x36,0xE0 },
+    { 0x3E,0x14 },
+    { 0x44,0x02 },
+    { 0x51,0x06 },
+    { 0x52,0x0C },
+    { 0x57,0x05 },
+    { 0x59,0x03 },
+    { 0x5B,0x04 },
+    { 0x5D,0x3B },
+    { 0x7C,0xC8 },
+    
+    { 0x7F,0x01 }, // Bank1, not allowed to perform SPIWriteRead
+    { 0x00,0x2F },
+    { 0x08,0x1C },
+    { 0x0A,0x02 },
+    { 0x19,0x40 },
+    { 0x1B,0x10 },
+    { 0x1D,0x18 },
+    { 0x1F,0x12 },
+    { 0x20,0x00 },
+    { 0x21,0x80 },
+    { 0x23,0x60 },
+    { 0x25,0x64 },
+    { 0x27,0x64 },
+    { 0x2B,0x78 },
+    { 0x2F,0x78 },
+    { 0x39,0x78 },
+    { 0x3B,0x78 },
+    { 0x3D,0x78 },
+    { 0x3F,0x78 },
+    { 0x44,0x7E },
+    { 0x45,0xF4 },
+    { 0x46,0x01 },
+    { 0x47,0x2C },
+    { 0x49,0x90 },
+    { 0x4A,0x05 },
+    { 0x4B,0xDC },
+    { 0x4C,0x07 },
+    { 0x4D,0x08 },
+    { 0x51,0x02 },
+    { 0x52,0xBC },
+    { 0x53,0x02 },
+    { 0x54,0xBC },
+    { 0x55,0x07 },
+    { 0x56,0x08 },
+    { 0x57,0x07 },
+    { 0x58,0x08 },
+    { 0x59,0x08 },
+    { 0x5A,0x08 },
+    
+    { 0x7F,0x02 }, // Bank2, not allowed to perform SPIWriteRead
+    { 0x07,0x1B },
+    { 0x08,0x1F },
+    { 0x09,0x23 },
+    { 0x51,0x01 },
+    
+    { 0x7F,0x03 }, // Bank3, not allowed to perform SPIWriteRead
+    { 0x07,0x07 },
+    { 0x08,0x06 },
+    { 0x2F,0x00 },
+    { 0x30,0x20 },
+    { 0x32,0x59 },
+    { 0x33,0xD8 },
+    { 0x34,0x4E },
+    { 0x35,0x20 },
+    { 0x36,0x5B },
+    { 0x37,0xCC },
+    { 0x38,0x50 },
+    { 0x39,0x14 },
+    
+    { 0x7F,0x04 }, // Bank4, not allowed to perform SPIWriteRead
+    { 0x05,0x01 },
+    { 0x2C,0x06 },
+    { 0x2E,0x0C },
+    { 0x30,0x0C },
+    { 0x32,0x06 },
+    { 0x34,0x03 },
+    { 0x38,0x17 },
+    { 0x39,0x71 },
+    { 0x3A,0x18 },
+    { 0x3B,0x4D },
+    { 0x3C,0x18 },
+    { 0x3D,0x4D },
+    { 0x3E,0x14 },
+    { 0x3F,0xD1 },
+    { 0x40,0x14 },
+    { 0x41,0xDD },
+    { 0x42,0x0A },
+    { 0x43,0x6C },
+    { 0x44,0x08 },
+    { 0x45,0xAD },
+    { 0x46,0x06 },
+    { 0x47,0xF2 },
+    { 0x48,0x06 },
+    { 0x49,0xEC },
+    { 0x4A,0x06 },
+    { 0x4B,0xEC },
+    { 0x53,0x08 },
+    
+    { 0x7F,0x05 }, // Bank5, not allowed to perform SPIWriteRead
+    { 0x03,0x00 },
+    { 0x09,0x01 },
+    { 0x0B,0xFF },
+    { 0x0D,0xFF },
+    { 0x0F,0xFF },
+    { 0x11,0xFF },
+    { 0x12,0xD2 },
+    { 0x13,0xD2 },
+    { 0x19,0xFF },
+    { 0x1B,0xFF },
+    { 0x1D,0xFF },
+    { 0x1F,0xFF },
+    { 0x20,0xD2 },
+    { 0x21,0xD2 },
+    { 0x2F,0x7C },
+    { 0x30,0x05 },
+    { 0x41,0x02 },
+    { 0x53,0xFF },
+    { 0x5F,0x02 },
+    
+    { 0x7F,0x06 }, // Bank6, not allowed to perform SPIWriteRead
+    { 0x2A,0x05 }, // Write ONLY address, not allowed to perform SPIWriteRead
+    { 0x35,0x19 },
+    
+    { 0x7F,0x07 }, // Bank7, not allowed to perform SPIWriteRead
+    { 0x00,0x01 },
+    { 0x14,0x03 },
+    { 0x15,0x14 },
+    { 0x46,0x03 },
+    
+    { 0x7F,0x00 }, // Bank0, not allowed to perform SPIWriteRead
+};
+#define initialize_size (sizeof(initialize)/sizeof(initialize[0]))
+
+
+
+const uint8_t modeLaser[][2] = {
+    { 0x7F, 0x00 }, // Bank0, not allowed to perform SPIWriteRead
+    { 0x09, 0x5A }, // disable write protect
+    { 0x53, 0x01 },
+    { 0x07, 0xCC },
+    { 0x0D, 0x05 },
+    { 0x0E, 0x05 },
+    { 0x19, 0x24 },
+    { 0x7F, 0x01 }, // Bank1, not allowed to perform SPIWriteRead
+    { 0x1D, 0x18 },
+    { 0x1F, 0x12 },
+    { 0x42, 0x40 },
+    { 0x37, 0x60 },
+    { 0x43, 0x0A },
+    { 0x7F, 0x04 }, // Bank4, not allowed to perform SPIWriteRead
+    { 0x06, 0x03 },
+    { 0x7F, 0x05 }, // Bank5, not allowed to perform SPIWriteRead
+    { 0x2E, 0x02 },
+    { 0x48, 0x00 },
+    { 0x3E, 0x05 },
+    { 0x7F, 0x06 }, // Bank6, not allowed to perform SPIWriteRead
+    { 0x34, 0x01 },
+    { 0x7F, 0x00 }, // Bank0, not allowed to perform SPIWriteRead
+    { 0x09, 0x00 }, // enable write protect
+};
+#define modeLaser_size (sizeof(modeLaser)/sizeof(modeLaser[0]))
+
+
+
+const uint8_t modeLED[][2] = {
+    { 0x7F, 0x00 }, // Bank0, not allowed to perform SPIWriteRead
+    { 0x09, 0x5A }, // disable write protect
+    { 0x07, 0x55 },
+    { 0x0D, 0x7D },
+    { 0x0E, 0x7D },
+    { 0x19, 0x3C },
+    { 0x7F, 0x01 }, // Bank1, not allowed to perform SPIWriteRead
+    { 0x1D, 0x00 },
+    { 0x1F, 0x00 },
+    { 0x42, 0x20 },
+    { 0x37, 0x18 },
+    { 0x43, 0x02 },
+    { 0x7F, 0x04 }, // Bank4, not allowed to perform SPIWriteRead
+    { 0x06, 0x00 },
+    { 0x7F, 0x05 }, // Bank5, not allowed to perform SPIWriteRead
+    { 0x2E, 0x08 },
+    { 0x48, 0x02 },
+    { 0x3E, 0x85 },
+    { 0x7F, 0x06 }, // Bank6, not allowed to perform SPIWriteRead
+    { 0x34, 0x09 },
+    { 0x7F, 0x00 }, // Bank0, not allowed to perform SPIWriteRead
+    { 0x53, 0x00 },
+    { 0x09, 0x00 }, // enable write protect
+};
+#define modeLED_size (sizeof(modeLED)/sizeof(modeLED[0]))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Mar 19 21:18:40 2018 +0000
@@ -0,0 +1,75 @@
+// PAA5101 Optical Tracking Minuature Chip reference code.
+// Version: 1.0
+// Latest Revision Date: 29 Jan. 2018
+// By PixArt Imaging Inc.
+// Primary Engineer: Vincent Yeh (PixArt USA)
+
+/*
+//=======================
+//Revision History
+//=======================
+Version 1.0 -- ????????????????????????????????????
+First release.
+*/
+
+#include "mbed.h"
+#include "registerArrays.h"
+#include "SPIcommFunctions.h"
+
+int main()
+{
+    pc.baud(115200);                    // Set baud rate to 115200. Remember to sync serial terminal baud rate to the same value.
+
+    spi.format(8,3);                    // Set SPI to 8 bits with inverted polarity and phase-shifted to second edge.
+    spi.frequency(10000000);            // Set frequency for SPI communication.
+    cs = 1;                             // Initialize chip-select pin to be HIGH (inactive).
+    
+    pc.printf("Program START\n\r");
+    
+    writeRegister(0x7F, 0x00);          //Reset to bank 0 before initializing again
+    
+    pc.printf("ID Check: %2X\n\r", readRegister(0x00)); //Checks product ID to make sure communication protocol is working properly.
+    if(readRegister(0x00) != 0x31)
+    {
+        pc.printf("Communication protocol error! Terminating program.\n\r");
+        return 0;
+    }
+    
+    writeRegister(0x06, 0x80);          //Resets the chip now that we have verified that the communication works.
+    wait_ms(1);                         //Wait at least 1 millisecond after resetting for timing.
+    
+    pc.printf("ID Check #2: %2X\n\r", readRegister(0x00)); //Checks product ID to make sure communication protocol is working properly after the reset.
+    if(readRegister(0x00) != 0x31)
+    {
+        pc.printf("Communication protocol error! Terminating program.\n\r");
+        return 0;
+    }
+    
+    writeRegister(0x09, 0x5A);          //Disables write-protect
+    writeRegister(0x51, 0x06);          //Sets laser diode power. Value should be <= 6
+    
+    load(initialize, initialize_size);  //Loads initial register settings
+    
+    writeRegister(0x5D, 0x3E);          //Unlisted register. Internal recommendation...
+    wait_ms(10);
+    writeRegister(0x5D, 0x3F);          //Unlisted register. Internal recommendation...
+
+    load(modeLaser, modeLaser_size);    //Goes into laser mode which is the default mode (not LED)
+    mode = 0;                           //This variable tracks if we are in laser mode or LED mode. 0 = laser, 1 = LED
+    LDP = 0;                            //Change GPIO pin to the laser diode to be LOW to activate the laser.
+    
+    writeRegister(0x09, 0x00);          //Enables write-protect
+    
+    
+    
+    while(1)                            //After all setup/initialization is done, we can loop for interrupts and mode detection.
+    {   
+        checkMode();                    //Checks image quality and switches to laser or LED mode accordingly.
+        
+        if(imageQualityOK && readRegister(0x02) & 0x80)   //If motion bit (bit 7 of register 0x02) is 1, movement has been detected. Also checks if imageQuality is good enough.
+        {
+            grabData();                 //Grabs data into variables deltaX and deltaY.
+            printData();                //Prints deltaX and deltaY, but only if they have changed from their previous values.
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Mon Mar 19 21:18:40 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#569159b784f70feaa32ce226aaca896fb83452f7