Reference firmware for PixArt's PAT9130EW 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.

Committer:
PixArtVY
Date:
Wed Jul 18 18:33:03 2018 +0000
Revision:
1:efa5e5a1e308
Parent:
0:5a34a4387fb0
Added Apache License notice.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
PixArtVY 0:5a34a4387fb0 1 //=========================================================================
PixArtVY 0:5a34a4387fb0 2 //Communication pinouts for serial COM port, SPI, and interrupts
PixArtVY 0:5a34a4387fb0 3 //=========================================================================
PixArtVY 0:5a34a4387fb0 4 static Serial pc(USBTX, USBRX); //PC comm
PixArtVY 0:5a34a4387fb0 5 static SPI spi(p23, p24, p25); //mosi, miso, sclk
PixArtVY 0:5a34a4387fb0 6 static DigitalOut cs(p22); //chip select
PixArtVY 0:5a34a4387fb0 7
PixArtVY 0:5a34a4387fb0 8
PixArtVY 0:5a34a4387fb0 9 //=========================================================================
PixArtVY 0:5a34a4387fb0 10 //Variables and arrays used for communications and data storage
PixArtVY 0:5a34a4387fb0 11 //=========================================================================
PixArtVY 0:5a34a4387fb0 12 int8_t deltaX_low, deltaY_low; //Stores the low-bits of movement data.
PixArtVY 0:5a34a4387fb0 13 int16_t deltaX_high, deltaY_high; //Stores the high-bits of movement data.
PixArtVY 0:5a34a4387fb0 14 int16_t deltaX, deltaY; //Stores the combined value of low and high bits.
PixArtVY 0:5a34a4387fb0 15 int16_t totalX, totalY = 0; //Stores the total deltaX and deltaY moved during runtime.
PixArtVY 0:5a34a4387fb0 16 int8_t currentBank; //Stores the current register bank (for tracking/debugging).
PixArtVY 0:5a34a4387fb0 17
PixArtVY 0:5a34a4387fb0 18
PixArtVY 0:5a34a4387fb0 19 //=========================================================================
PixArtVY 0:5a34a4387fb0 20 //Functions used to communicate with the sensor and grab/print data
PixArtVY 0:5a34a4387fb0 21 //=========================================================================
PixArtVY 0:5a34a4387fb0 22 uint8_t readRegister(uint8_t addr);
PixArtVY 0:5a34a4387fb0 23 //This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00.
PixArtVY 0:5a34a4387fb0 24
PixArtVY 0:5a34a4387fb0 25 void writeRegister(uint8_t addr, uint8_t data);
PixArtVY 0:5a34a4387fb0 26 //This function takes an 8-bit address and 8-bit data. Writes the given data to the given address.
PixArtVY 0:5a34a4387fb0 27
PixArtVY 0:5a34a4387fb0 28 void load(const uint8_t array[][2], uint8_t arraySize);
PixArtVY 0:5a34a4387fb0 29 //Takes an array of registers/data (found in registerArrays.h) and their size and writes in all the values.
PixArtVY 0:5a34a4387fb0 30
PixArtVY 0:5a34a4387fb0 31 void grabData(void);
PixArtVY 0:5a34a4387fb0 32 //Grabs the deltaX and deltaY information from the proper registers and formats it into the proper format.
PixArtVY 0:5a34a4387fb0 33
PixArtVY 0:5a34a4387fb0 34 void printData(void);
PixArtVY 0:5a34a4387fb0 35 //Prints the data out to a serial terminal.
PixArtVY 0:5a34a4387fb0 36
PixArtVY 0:5a34a4387fb0 37
PixArtVY 0:5a34a4387fb0 38
PixArtVY 0:5a34a4387fb0 39
PixArtVY 0:5a34a4387fb0 40
PixArtVY 0:5a34a4387fb0 41 //=========================================================================
PixArtVY 0:5a34a4387fb0 42 //Functions definitions
PixArtVY 0:5a34a4387fb0 43 //=========================================================================
PixArtVY 0:5a34a4387fb0 44 uint8_t readRegister(uint8_t addr)
PixArtVY 0:5a34a4387fb0 45 {
PixArtVY 0:5a34a4387fb0 46 cs = 0; //Set chip select low/active
PixArtVY 0:5a34a4387fb0 47 addr = addr & 0x7F; //Set MSB to 0 to indicate read operation
PixArtVY 0:5a34a4387fb0 48 spi.write(addr); //Write the given address
PixArtVY 0:5a34a4387fb0 49 wait_us(1); //Add a tiny delay after sending address for some internal cycle timing.
PixArtVY 0:5a34a4387fb0 50 uint8_t data_read = spi.write(0x00); //Throw dummy byte after sending address to receieve data
PixArtVY 0:5a34a4387fb0 51 cs = 1; //Set chip select back to high/inactive
PixArtVY 0:5a34a4387fb0 52 return data_read; //Returns 8-bit data from register
PixArtVY 0:5a34a4387fb0 53 }
PixArtVY 0:5a34a4387fb0 54
PixArtVY 0:5a34a4387fb0 55
PixArtVY 0:5a34a4387fb0 56 //=========================================================================
PixArtVY 0:5a34a4387fb0 57 void writeRegister(uint8_t addr, uint8_t data)
PixArtVY 0:5a34a4387fb0 58 {
PixArtVY 0:5a34a4387fb0 59 cs = 0; //Set chip select low/active
PixArtVY 0:5a34a4387fb0 60 addr = addr | 0x80; //Set MSB to 1 to indicate write operation
PixArtVY 0:5a34a4387fb0 61 spi.write(addr); //Write the given address
PixArtVY 0:5a34a4387fb0 62 spi.write(data); //Write the given data
PixArtVY 0:5a34a4387fb0 63 cs = 1; //Set chip select back to high/inactive
PixArtVY 0:5a34a4387fb0 64
PixArtVY 0:5a34a4387fb0 65 if(addr == 0x7F) //You can't read register 0x7F for the current bank so we store the value before writing it
PixArtVY 0:5a34a4387fb0 66 {
PixArtVY 0:5a34a4387fb0 67 currentBank = data; //Store the current bank for printing later.
PixArtVY 0:5a34a4387fb0 68 //pc.printf("B:%2X, R:%2X, D:%2X\n\r", currentBank, addr, currentBank);
PixArtVY 0:5a34a4387fb0 69 //Uncomment this line and the other print line below for debugging. Prints every register bank change.
PixArtVY 0:5a34a4387fb0 70 }
PixArtVY 0:5a34a4387fb0 71
PixArtVY 0:5a34a4387fb0 72 else
PixArtVY 0:5a34a4387fb0 73 {
PixArtVY 0:5a34a4387fb0 74 //pc.printf("B:%2X, R:%2X, D:%2X\n\r", currentBank, addr, readRegister(addr));
PixArtVY 0:5a34a4387fb0 75 //Uncomment this line and the other print line above for debugging. Prints every register write operation.
PixArtVY 0:5a34a4387fb0 76 }
PixArtVY 0:5a34a4387fb0 77 }
PixArtVY 0:5a34a4387fb0 78
PixArtVY 0:5a34a4387fb0 79
PixArtVY 0:5a34a4387fb0 80 //=========================================================================
PixArtVY 0:5a34a4387fb0 81 void load(const uint8_t array[][2], uint8_t arraySize)
PixArtVY 0:5a34a4387fb0 82 {
PixArtVY 0:5a34a4387fb0 83 for(uint8_t q = 0; q < arraySize; q++)
PixArtVY 0:5a34a4387fb0 84 {
PixArtVY 0:5a34a4387fb0 85 writeRegister(array[q][0], array[q][1]); //Writes the given array of registers/data.
PixArtVY 0:5a34a4387fb0 86 }
PixArtVY 0:5a34a4387fb0 87 }
PixArtVY 0:5a34a4387fb0 88
PixArtVY 0:5a34a4387fb0 89
PixArtVY 0:5a34a4387fb0 90 //=========================================================================
PixArtVY 0:5a34a4387fb0 91 void grabData(void)
PixArtVY 0:5a34a4387fb0 92 {
PixArtVY 0:5a34a4387fb0 93 deltaX_low = readRegister(0x03); //Grabs data from the proper registers.
PixArtVY 0:5a34a4387fb0 94 deltaY_low = readRegister(0x04);
PixArtVY 0:5a34a4387fb0 95 deltaX_high = (readRegister(0x11)<<8) & 0xFF00; //Grabs data and shifts it to make space to be combined with lower bits.
PixArtVY 0:5a34a4387fb0 96 deltaY_high = (readRegister(0x12)<<8) & 0xFF00;
PixArtVY 0:5a34a4387fb0 97
PixArtVY 0:5a34a4387fb0 98 if(deltaX_high & 0x8000)
PixArtVY 0:5a34a4387fb0 99 deltaX_high |= 0xf000; // 12-bit data convert to 16-bit (two's comp)
PixArtVY 0:5a34a4387fb0 100
PixArtVY 0:5a34a4387fb0 101 if(deltaY_high & 0x8000)
PixArtVY 0:5a34a4387fb0 102 deltaY_high |= 0xf000; // 12-bit data convert to 16-bit (2's comp)
PixArtVY 0:5a34a4387fb0 103
PixArtVY 0:5a34a4387fb0 104 deltaX = deltaX_high | deltaX_low; //Combined the low and high bits.
PixArtVY 0:5a34a4387fb0 105 deltaY = deltaY_high | deltaY_low;
PixArtVY 0:5a34a4387fb0 106 }
PixArtVY 0:5a34a4387fb0 107
PixArtVY 0:5a34a4387fb0 108
PixArtVY 0:5a34a4387fb0 109 //=========================================================================
PixArtVY 0:5a34a4387fb0 110 void printData(void)
PixArtVY 0:5a34a4387fb0 111 {
PixArtVY 0:5a34a4387fb0 112 if((deltaX != 0) || (deltaY != 0)) //If there is deltaX or deltaY movement, print the data.
PixArtVY 0:5a34a4387fb0 113 {
PixArtVY 0:5a34a4387fb0 114 totalX += deltaX;
PixArtVY 0:5a34a4387fb0 115 totalY += deltaY;
PixArtVY 0:5a34a4387fb0 116
PixArtVY 0:5a34a4387fb0 117 pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY); //Prints each individual count of deltaX and deltaY.
PixArtVY 0:5a34a4387fb0 118 pc.printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY); //Prints the total movement made during runtime.
PixArtVY 0:5a34a4387fb0 119 }
PixArtVY 0:5a34a4387fb0 120
PixArtVY 0:5a34a4387fb0 121 deltaX = 0; //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten.
PixArtVY 0:5a34a4387fb0 122 deltaY = 0;
PixArtVY 0:5a34a4387fb0 123 }