Reference firmware for PixArt's PAT9125EL 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 PAT9125EL sensor and evaluation board.

For general information about this product, please visit this product's components page here:
https://os.mbed.com/components/PAT9125EL-Evaluation-Board/

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

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

Files at this revision

API Documentation at this revision

Comitter:
PixArtVY
Date:
Fri Feb 23 23:32:40 2018 +0000
Child:
1:4ea157704c58
Commit message:
First release of 9125 firmware.

Changed in this revision

commHeaders/I2CcommFunctions.h Show annotated file Show diff for this revision Revisions of this file
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/I2CcommFunctions.h	Fri Feb 23 23:32:40 2018 +0000
@@ -0,0 +1,111 @@
+#define I2Cmode
+#define I2C_Slave_ID 0x75                           //ONLY WHEN ID PIN IS TIED TO GROUND! ID changes when tied to VDD (0x73) or when left floating (0x79).
+
+//=========================================================================
+//Communication pinouts for serial COM port, I2C, and interrupts
+//=========================================================================
+static Serial pc(USBTX, USBRX);                     //PC comm
+static I2C i2c(p26, p27);                           //SDA, SCL
+
+
+//=========================================================================
+//Variables and arrays used for communications and data storage
+//=========================================================================
+int8_t deltaX_low, deltaY_low;                      //Stores the low-bits of movement data.
+int16_t deltaX_high, deltaY_high;                   //Stores the high-bits of movement data.
+int16_t deltaX, deltaY;                             //Stores the combined value of low and high bits.
+int16_t totalX, totalY = 0;                         //Stores the total deltaX and deltaY moved during runtime.
+
+
+//=========================================================================
+//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 grabData(void);
+//Grabs the deltaX and deltaY information from the proper registers and formats it into the proper format.
+
+void printData(void);
+//Prints the data out to a serial terminal.
+
+
+
+
+
+//=========================================================================
+//Functions definitions
+//=========================================================================
+uint8_t readRegister(uint8_t addr)
+{
+    uint8_t data;
+    i2c.write((I2C_Slave_ID << 1), (const char*)&addr, 1, 0);   //Send the address to the chip
+    wait_us(1);
+    i2c.read((I2C_Slave_ID << 1), (char*)&data, 1, 0);          //Send the memory address where you want to store the read data
+    return(data);
+}
+
+
+//=========================================================================
+void writeRegister(uint8_t addr, uint8_t data)
+{
+    char data_write[2];             //Create an array to store the address/data to pass them at the same time.
+    data_write[0] = addr;           //Store the address in the first byte
+    data_write[1] = data;           //Store the data in the second byte
+    i2c.write((I2C_Slave_ID << 1), data_write, 2, 0);   //Send both over at once
+    
+    pc.printf("R:%2X, D:%2X\n\r", addr, readRegister(addr));
+            //Uncomment this line for debugging. Prints every register write operation.
+}
+
+
+//=========================================================================
+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 grabData(void)
+{
+    deltaX_low = readRegister(0x03);        //Grabs data from the proper registers.
+    deltaY_low = readRegister(0x04);
+    deltaX_high = (readRegister(0x12)<<4) & 0xF00;      //Grabs data and shifts it to make space to be combined with lower bits.
+    deltaY_high = (readRegister(0x12)<<8) & 0xF00;
+    
+    if(deltaX_high & 0x800)
+        deltaX_high |= 0xf000; // 12-bit data convert to 16-bit (two's comp)
+        
+    if(deltaY_high & 0x800)
+        deltaY_high |= 0xf000; // 12-bit data convert to 16-bit (2's comp)
+        
+    deltaX = deltaX_high | deltaX_low;      //Combined the low and high bits.
+    deltaY = deltaY_high | deltaY_low;
+}
+
+
+//=========================================================================
+void printData(void)
+{
+    if((deltaX != 0) || (deltaY != 0))      //If there is deltaX or deltaY movement, print the data.
+    {
+        totalX += deltaX;
+        totalY += deltaY;
+        
+        pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY);    //Prints each individual count of deltaX and deltaY.
+        pc.printf("X-axis Counts: %d\t\t\tY-axis Counts: %d\n\r", totalX, totalY);  //Prints the total movement made during runtime.
+    }
+    
+    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/SPIcommFunctions.h	Fri Feb 23 23:32:40 2018 +0000
@@ -0,0 +1,114 @@
+#define SPImode
+
+//=========================================================================
+//Communication pinouts for serial COM port, SPI, and interrupts
+//=========================================================================
+static Serial pc(USBTX, USBRX);                     //PC comm
+static SPI spi(p23, p24, p25);                      //mosi, miso, sclk
+static DigitalOut cs(p22);                          //chip select
+
+
+//=========================================================================
+//Variables and arrays used for communications and data storage
+//=========================================================================
+int8_t deltaX_low, deltaY_low;                      //Stores the low-bits of movement data.
+int16_t deltaX_high, deltaY_high;                   //Stores the high-bits of movement data.
+int16_t deltaX, deltaY;                             //Stores the combined value of low and high bits.
+int16_t totalX, totalY = 0;                         //Stores the total deltaX and deltaY moved during runtime.
+
+
+//=========================================================================
+//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 grabData(void);
+//Grabs the deltaX and deltaY information from the proper registers and formats it into the proper format.
+
+void printData(void);
+//Prints the data out to a serial terminal.
+
+
+
+
+
+//=========================================================================
+//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_us(1);                             //Add a tiny delay after sending address for some internal cycle timing.
+    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
+    
+    //pc.printf("R:%2X, D:%2X\n\r", addr, readRegister(addr));
+            //Uncomment this line for debugging. Prints every register write operation.
+}
+
+
+//=========================================================================
+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 grabData(void)
+{
+    deltaX_low = readRegister(0x03);        //Grabs data from the proper registers.
+    deltaY_low = readRegister(0x04);
+    deltaX_high = (readRegister(0x12)<<4) & 0xF00;      //Grabs data and shifts it to make space to be combined with lower bits.
+    deltaY_high = (readRegister(0x12)<<8) & 0xF00;
+    
+    if(deltaX_high & 0x800)
+        deltaX_high |= 0xf000; // 12-bit data convert to 16-bit (two's comp)
+        
+    if(deltaY_high & 0x800)
+        deltaY_high |= 0xf000; // 12-bit data convert to 16-bit (2's comp)
+        
+    deltaX = deltaX_high | deltaX_low;      //Combined the low and high bits.
+    deltaY = deltaY_high | deltaY_low;
+}
+
+
+//=========================================================================
+void printData(void)
+{
+    if((deltaX != 0) || (deltaY != 0))      //If there is deltaX or deltaY movement, print the data.
+    {
+        totalX += deltaX;
+        totalY += deltaY;
+        
+        pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY);    //Prints each individual count of deltaX and deltaY.
+        pc.printf("X-axis Counts: %d\t\t\tY-axis Counts: %d\n\r", totalX, totalY);  //Prints the total movement made during runtime.
+    }
+    
+    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	Fri Feb 23 23:32:40 2018 +0000
@@ -0,0 +1,8 @@
+const uint8_t initialize[][2] = {
+    { 0x09, 0x5A },     //Disables write-protect.
+    { 0x0D, 0xB5 },     //Sets X-axis resolution (necessary value here will depend on physical design and application).
+    { 0x0E, 0xFF },     //Sets Y-axis resolution (necessary value here will depend on physical design and application).
+    { 0x19, 0x04 },     //Sets 12-bit X/Y data format. 0x04 is default (neither axis inverted, using 12-bit data instead of 8-bit data).
+    //{ 0x4B, 0x04 },     //Needed for when using low-voltage rail (1.7-1.9V): Power saving configuration.
+};
+#define initialize_size (sizeof(initialize)/sizeof(initialize[0]))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Feb 23 23:32:40 2018 +0000
@@ -0,0 +1,72 @@
+// PAT9125EL: Miniature Optical Navigation Chip reference code.
+// Version: 1.0
+// Latest Revision Date: 16 Feb. 2018
+// By PixArt Imaging Inc.
+// Primary Engineer: Vincent Yeh (PixArt USA)
+
+/*
+//=======================
+//Revision History
+//=======================
+Version 1.0 -- 16 Feb. 2018
+First release.
+*/
+
+#include "mbed.h"
+#include "registerArrays.h"
+//#include "SPIcommFunctions.h"
+#include "I2CcommFunctions.h"
+//Make sure you only have SPIcommFunctions or I2CcommFunctions enabled. You cannot include both headers.
+
+int main()
+{
+    pc.baud(115200);                    // Set baud rate to 115200. Remember to sync serial terminal baud rate to the same value.
+
+    #ifdef SPImode
+    spi.format(8,3);                    // Set SPI to 8 bits with inverted polarity and phase-shifted to second edge.
+    spi.frequency(1000000);             // Set frequency for SPI communication.
+    cs = 1;                             // Initialize chip select as inactive.
+    #endif
+    
+    #ifdef I2Cmode
+    i2c.frequency(400000);              // Set frequency for I2C communication.
+    #endif
+    
+    pc.printf("Program START\n\r");
+    
+    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, 0x97);          //Software reset (i.e. set bit7 to 1)
+    wait_ms(1);                         //Delay 1 ms for chip reset timing.
+    writeRegister(0x06, 0x17);          //Ensure software reset is done and chip is no longer in that state.
+    
+    load(initialize, initialize_size);  //Load register settings from the "initialize" array
+    
+    
+    
+    if(readRegister(0x5E) == 0x04)      //These unlisted registers are used for internal recommended settings.
+    {
+        writeRegister(0x5E, 0x08);
+        
+        if(readRegister(0x5D) == 0x10)
+            writeRegister(0x5D, 0x19);
+    }
+    
+    writeRegister(0x09, 0x00);  // enable write protect 
+    
+    while(1)
+    {
+        //pc.printf("MOTION bit: %2X\n\r", (readRegister(0x02) & 0x80) >> 7);   //Prints motion bit for debugging. 1 = motion detected. 0 = no motion detected.
+        
+        if(readRegister(0x02) & 0x80)
+        {
+            grabData();
+            printData();
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Fri Feb 23 23:32:40 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#569159b784f70feaa32ce226aaca896fb83452f7