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
commHeaders/SPIcommFunctions.h@0:2c144b6813d1, 2018-03-19 (annotated)
- Committer:
- PixArtVY
- Date:
- Mon Mar 19 21:18:40 2018 +0000
- Revision:
- 0:2c144b6813d1
- Child:
- 1:469063631a05
First release.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PixArtVY | 0:2c144b6813d1 | 1 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 2 | //Communication pinouts for serial COM port, SPI, and interrupts |
PixArtVY | 0:2c144b6813d1 | 3 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 4 | static Serial pc(USBTX, USBRX); //PC serial communication. |
PixArtVY | 0:2c144b6813d1 | 5 | 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). |
PixArtVY | 0:2c144b6813d1 | 6 | static DigitalOut cs(p22); //Chip select pin. |
PixArtVY | 0:2c144b6813d1 | 7 | static DigitalOut LDP(p20); //Laser diode pin. |
PixArtVY | 0:2c144b6813d1 | 8 | |
PixArtVY | 0:2c144b6813d1 | 9 | |
PixArtVY | 0:2c144b6813d1 | 10 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 11 | //Variables and arrays used for communications and data storage |
PixArtVY | 0:2c144b6813d1 | 12 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 13 | int16_t deltaX = 0; //Stores X-axis output data. |
PixArtVY | 0:2c144b6813d1 | 14 | int16_t deltaY = 0; //Stores Y-axis output data. |
PixArtVY | 0:2c144b6813d1 | 15 | |
PixArtVY | 0:2c144b6813d1 | 16 | uint8_t frameCounter = 0; //Looping variable to track when we have grabbed 8 frames of data. |
PixArtVY | 0:2c144b6813d1 | 17 | 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). |
PixArtVY | 0:2c144b6813d1 | 18 | uint16_t imageQuality_total = 0; //Stores the sum of all raw values from the 8 frames from imageQuality[8]. |
PixArtVY | 0:2c144b6813d1 | 19 | uint8_t imageQualityOK = 0; |
PixArtVY | 0:2c144b6813d1 | 20 | uint8_t mode = 0; //Modes: 0 = Laser, 1 = LED. Laser is default. |
PixArtVY | 0:2c144b6813d1 | 21 | #define laser2LED_threshold 0x700 //Stores threshold level for when we need to swap to LED mode. |
PixArtVY | 0:2c144b6813d1 | 22 | #define LED2laser_threshold 0x500 //Stores threshold level for when we need to swap to laser mode. |
PixArtVY | 0:2c144b6813d1 | 23 | |
PixArtVY | 0:2c144b6813d1 | 24 | uint8_t currentBank; //Stores current register bank. |
PixArtVY | 0:2c144b6813d1 | 25 | |
PixArtVY | 0:2c144b6813d1 | 26 | |
PixArtVY | 0:2c144b6813d1 | 27 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 28 | //Functions used to communicate with the sensor and grab/print data |
PixArtVY | 0:2c144b6813d1 | 29 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 30 | uint8_t readRegister(uint8_t addr); |
PixArtVY | 0:2c144b6813d1 | 31 | //This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00. |
PixArtVY | 0:2c144b6813d1 | 32 | |
PixArtVY | 0:2c144b6813d1 | 33 | void writeRegister(uint8_t addr, uint8_t data); |
PixArtVY | 0:2c144b6813d1 | 34 | //This function takes an 8-bit address and 8-bit data. Writes the given data to the given address. |
PixArtVY | 0:2c144b6813d1 | 35 | |
PixArtVY | 0:2c144b6813d1 | 36 | void load(const uint8_t array[][2], uint8_t arraySize); |
PixArtVY | 0:2c144b6813d1 | 37 | //Takes an array of registers/data (found in registerArrays.h) and their size and writes in all the values. |
PixArtVY | 0:2c144b6813d1 | 38 | |
PixArtVY | 0:2c144b6813d1 | 39 | void checkMode(void); |
PixArtVY | 0:2c144b6813d1 | 40 | //This function checks the image quality of each frame and switches between LED and laser modes when necessary. |
PixArtVY | 0:2c144b6813d1 | 41 | |
PixArtVY | 0:2c144b6813d1 | 42 | void grabData(void); |
PixArtVY | 0:2c144b6813d1 | 43 | //Takes data from the sensor and stores it into variables deltaX and deltaY. |
PixArtVY | 0:2c144b6813d1 | 44 | |
PixArtVY | 0:2c144b6813d1 | 45 | void printData(void); |
PixArtVY | 0:2c144b6813d1 | 46 | //Prints the deltaX and deltaY values to a serial terminal whenever the values change. |
PixArtVY | 0:2c144b6813d1 | 47 | |
PixArtVY | 0:2c144b6813d1 | 48 | |
PixArtVY | 0:2c144b6813d1 | 49 | |
PixArtVY | 0:2c144b6813d1 | 50 | |
PixArtVY | 0:2c144b6813d1 | 51 | |
PixArtVY | 0:2c144b6813d1 | 52 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 53 | //Functions definitions |
PixArtVY | 0:2c144b6813d1 | 54 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 55 | uint8_t readRegister(uint8_t addr) |
PixArtVY | 0:2c144b6813d1 | 56 | { |
PixArtVY | 0:2c144b6813d1 | 57 | cs = 0; //Set chip select low/active |
PixArtVY | 0:2c144b6813d1 | 58 | addr = addr & 0x7F; //Set MSB to 0 to indicate read operation |
PixArtVY | 0:2c144b6813d1 | 59 | spi.write(addr); //Write the given address |
PixArtVY | 0:2c144b6813d1 | 60 | wait_ms(1); |
PixArtVY | 0:2c144b6813d1 | 61 | uint8_t data_read = spi.write(0x00); //Throw dummy byte after sending address to receieve data |
PixArtVY | 0:2c144b6813d1 | 62 | cs = 1; //Set chip select back to high/inactive |
PixArtVY | 0:2c144b6813d1 | 63 | return data_read; //Returns 8-bit data from register |
PixArtVY | 0:2c144b6813d1 | 64 | } |
PixArtVY | 0:2c144b6813d1 | 65 | |
PixArtVY | 0:2c144b6813d1 | 66 | |
PixArtVY | 0:2c144b6813d1 | 67 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 68 | void writeRegister(uint8_t addr, uint8_t data) |
PixArtVY | 0:2c144b6813d1 | 69 | { |
PixArtVY | 0:2c144b6813d1 | 70 | cs = 0; //Set chip select low/active |
PixArtVY | 0:2c144b6813d1 | 71 | addr = addr | 0x80; //Set MSB to 1 to indicate write operation |
PixArtVY | 0:2c144b6813d1 | 72 | spi.write(addr); //Write the given address |
PixArtVY | 0:2c144b6813d1 | 73 | spi.write(data); //Write the given data |
PixArtVY | 0:2c144b6813d1 | 74 | cs = 1; //Set chip select back to high/inactive |
PixArtVY | 0:2c144b6813d1 | 75 | |
PixArtVY | 0:2c144b6813d1 | 76 | if(addr == 0x7F) //You can't read register 0x7F for the current bank so we store the value before writing it |
PixArtVY | 0:2c144b6813d1 | 77 | { |
PixArtVY | 0:2c144b6813d1 | 78 | currentBank = data; //Store the current bank for printing later. |
PixArtVY | 0:2c144b6813d1 | 79 | //pc.printf("B:%2X, R:%2X, D:%2X\n\r", currentBank, (addr & 0x7F), currentBank); |
PixArtVY | 0:2c144b6813d1 | 80 | //Uncomment this line and the other print line below for debugging. Prints every register bank change. |
PixArtVY | 0:2c144b6813d1 | 81 | } |
PixArtVY | 0:2c144b6813d1 | 82 | |
PixArtVY | 0:2c144b6813d1 | 83 | else |
PixArtVY | 0:2c144b6813d1 | 84 | { |
PixArtVY | 0:2c144b6813d1 | 85 | //pc.printf("B:%2X, R:%2X, D:%2X\n\r", currentBank, (addr & 0x7F), readRegister(addr)); |
PixArtVY | 0:2c144b6813d1 | 86 | //Uncomment this line and the other print line above for debugging. Prints every register write operation. |
PixArtVY | 0:2c144b6813d1 | 87 | } |
PixArtVY | 0:2c144b6813d1 | 88 | |
PixArtVY | 0:2c144b6813d1 | 89 | wait_ms(2); |
PixArtVY | 0:2c144b6813d1 | 90 | } |
PixArtVY | 0:2c144b6813d1 | 91 | |
PixArtVY | 0:2c144b6813d1 | 92 | |
PixArtVY | 0:2c144b6813d1 | 93 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 94 | void load(const uint8_t array[][2], uint8_t arraySize) |
PixArtVY | 0:2c144b6813d1 | 95 | { |
PixArtVY | 0:2c144b6813d1 | 96 | for(uint8_t q = 0; q < arraySize; q++) |
PixArtVY | 0:2c144b6813d1 | 97 | { |
PixArtVY | 0:2c144b6813d1 | 98 | writeRegister(array[q][0], array[q][1]); //Writes the given array of registers/data. |
PixArtVY | 0:2c144b6813d1 | 99 | } |
PixArtVY | 0:2c144b6813d1 | 100 | } |
PixArtVY | 0:2c144b6813d1 | 101 | |
PixArtVY | 0:2c144b6813d1 | 102 | |
PixArtVY | 0:2c144b6813d1 | 103 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 104 | void checkMode(void) |
PixArtVY | 0:2c144b6813d1 | 105 | { |
PixArtVY | 0:2c144b6813d1 | 106 | uint16_t data_msb, data_lsb = 0; //These variables store the high and low bits of image quality data. |
PixArtVY | 0:2c144b6813d1 | 107 | imageQuality_total = 0; //This variable holds the sum of 8 frames of data. |
PixArtVY | 0:2c144b6813d1 | 108 | |
PixArtVY | 0:2c144b6813d1 | 109 | data_msb = readRegister(0x75); //Reads upper 8 bits of image quality. |
PixArtVY | 0:2c144b6813d1 | 110 | data_lsb = readRegister(0x76); //Reads lower 8 bits of image quality. |
PixArtVY | 0:2c144b6813d1 | 111 | imageQuality[frameCounter] = data_msb*256 + data_lsb; //Combines the low/high bits to be a single element in this array. |
PixArtVY | 0:2c144b6813d1 | 112 | |
PixArtVY | 0:2c144b6813d1 | 113 | if(frameCounter == 7) //We check image quality every EIGHT samples before determining if a mode-switch is necessary. |
PixArtVY | 0:2c144b6813d1 | 114 | { |
PixArtVY | 0:2c144b6813d1 | 115 | for(int i=0; i<8; i++) //Sums up 8 frames of image quality data into 1 variable. |
PixArtVY | 0:2c144b6813d1 | 116 | { |
PixArtVY | 0:2c144b6813d1 | 117 | imageQuality_total = imageQuality_total + imageQuality[i]; |
PixArtVY | 0:2c144b6813d1 | 118 | } |
PixArtVY | 0:2c144b6813d1 | 119 | |
PixArtVY | 0:2c144b6813d1 | 120 | if(imageQualityOK == 0 && imageQuality_total > LED2laser_threshold) //If imageQuality is over 0x500, we can continue to select the proper mode. |
PixArtVY | 0:2c144b6813d1 | 121 | { |
PixArtVY | 0:2c144b6813d1 | 122 | imageQualityOK = 1; |
PixArtVY | 0:2c144b6813d1 | 123 | |
PixArtVY | 0:2c144b6813d1 | 124 | } |
PixArtVY | 0:2c144b6813d1 | 125 | |
PixArtVY | 0:2c144b6813d1 | 126 | 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. |
PixArtVY | 0:2c144b6813d1 | 127 | { |
PixArtVY | 0:2c144b6813d1 | 128 | imageQualityOK = 0; |
PixArtVY | 0:2c144b6813d1 | 129 | pc.printf("Image quality too low! Value: %4X\n\r", imageQuality_total); |
PixArtVY | 0:2c144b6813d1 | 130 | } |
PixArtVY | 0:2c144b6813d1 | 131 | |
PixArtVY | 0:2c144b6813d1 | 132 | |
PixArtVY | 0:2c144b6813d1 | 133 | if(imageQualityOK) |
PixArtVY | 0:2c144b6813d1 | 134 | { |
PixArtVY | 0:2c144b6813d1 | 135 | pc.printf("Image quality OK! Value: %4X\n\r", imageQuality_total); |
PixArtVY | 0:2c144b6813d1 | 136 | |
PixArtVY | 0:2c144b6813d1 | 137 | if(mode == 1 && imageQuality_total < LED2laser_threshold) //Check the condition for when we need to change to laser. |
PixArtVY | 0:2c144b6813d1 | 138 | { |
PixArtVY | 0:2c144b6813d1 | 139 | load(modeLaser, modeLaser_size); //Loads the register array that has the settings for laser mode. |
PixArtVY | 0:2c144b6813d1 | 140 | mode = 0; //Sets the mode-tracker to be 0 to reflect laser mode. |
PixArtVY | 0:2c144b6813d1 | 141 | LDP = 0; //Sets the laser diode GPIO pin to be LOW (active because of PMOS pull-up). |
PixArtVY | 0:2c144b6813d1 | 142 | wait_ms(40); //Delay for timing. |
PixArtVY | 0:2c144b6813d1 | 143 | writeRegister(0x03, 0x00); //Delta_XL?????? TRY TO FIND OUT WHAT THIS REGISTER IS FOR********************************************************** |
PixArtVY | 0:2c144b6813d1 | 144 | |
PixArtVY | 0:2c144b6813d1 | 145 | //pc.printf("Image Quality: %4X\n\r", imageQuality_total); |
PixArtVY | 0:2c144b6813d1 | 146 | //pc.printf("Now in laser mode. Value of 'mode': %1X\n\r", mode); |
PixArtVY | 0:2c144b6813d1 | 147 | //Uncomment these lines and the other print lines below for debugging. Prints image quality and tracks mode-switches. |
PixArtVY | 0:2c144b6813d1 | 148 | } |
PixArtVY | 0:2c144b6813d1 | 149 | |
PixArtVY | 0:2c144b6813d1 | 150 | if(mode == 0 && imageQuality_total < laser2LED_threshold) //Check the condition for when we need to change to LED. |
PixArtVY | 0:2c144b6813d1 | 151 | { |
PixArtVY | 0:2c144b6813d1 | 152 | load(modeLED, modeLED_size); //Loads the register array that has the settings for LED mode. |
PixArtVY | 0:2c144b6813d1 | 153 | mode = 1; //Sets the mode-tracker to be 1 to reflect LED mode. |
PixArtVY | 0:2c144b6813d1 | 154 | LDP = 1; //Sets the laser diode GPIO pin to be HIGH (inactive because of PMOS pull-up). |
PixArtVY | 0:2c144b6813d1 | 155 | wait_ms(40); //Delay for timing. |
PixArtVY | 0:2c144b6813d1 | 156 | writeRegister(0x03, 0x00); //Delta_XL?????? TRY TO FIND OUT WHAT THIS REGISTER IS FOR********************************************************** |
PixArtVY | 0:2c144b6813d1 | 157 | |
PixArtVY | 0:2c144b6813d1 | 158 | //pc.printf("Image Quality: %4X\n\r", imageQuality_total); |
PixArtVY | 0:2c144b6813d1 | 159 | //pc.printf("Now in LED mode. Value of 'mode': %1X\n\r", mode); |
PixArtVY | 0:2c144b6813d1 | 160 | //Uncomment these lines and the other print lines above for debugging. Prints image quality and tracks mode-switches. |
PixArtVY | 0:2c144b6813d1 | 161 | } |
PixArtVY | 0:2c144b6813d1 | 162 | } |
PixArtVY | 0:2c144b6813d1 | 163 | } |
PixArtVY | 0:2c144b6813d1 | 164 | |
PixArtVY | 0:2c144b6813d1 | 165 | 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. |
PixArtVY | 0:2c144b6813d1 | 166 | } |
PixArtVY | 0:2c144b6813d1 | 167 | |
PixArtVY | 0:2c144b6813d1 | 168 | |
PixArtVY | 0:2c144b6813d1 | 169 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 170 | void grabData(void) |
PixArtVY | 0:2c144b6813d1 | 171 | { |
PixArtVY | 0:2c144b6813d1 | 172 | int16_t deltaX_high, deltaX_low = 0; //These four variables store the low/high bits of the deltaX and deltaY data. |
PixArtVY | 0:2c144b6813d1 | 173 | int16_t deltaY_high, deltaY_low = 0; |
PixArtVY | 0:2c144b6813d1 | 174 | |
PixArtVY | 0:2c144b6813d1 | 175 | deltaX_low = (int16_t)readRegister(0x03); //Grabbing the data from registers. |
PixArtVY | 0:2c144b6813d1 | 176 | deltaY_low = (int16_t)readRegister(0x04); |
PixArtVY | 0:2c144b6813d1 | 177 | deltaX_high = ((int16_t)readRegister(0x11))<<8; //Shifts high bits left by 8 so that we can combine high and low bytes together. |
PixArtVY | 0:2c144b6813d1 | 178 | deltaY_high = ((int16_t)readRegister(0x12))<<8; |
PixArtVY | 0:2c144b6813d1 | 179 | |
PixArtVY | 0:2c144b6813d1 | 180 | deltaX = deltaX_high | deltaX_low; //Combines the low/high bits of X and Y to be one variable. |
PixArtVY | 0:2c144b6813d1 | 181 | deltaY = deltaY_high | deltaY_low; |
PixArtVY | 0:2c144b6813d1 | 182 | } |
PixArtVY | 0:2c144b6813d1 | 183 | |
PixArtVY | 0:2c144b6813d1 | 184 | |
PixArtVY | 0:2c144b6813d1 | 185 | //========================================================================= |
PixArtVY | 0:2c144b6813d1 | 186 | void printData(void) |
PixArtVY | 0:2c144b6813d1 | 187 | { |
PixArtVY | 0:2c144b6813d1 | 188 | if((deltaX != 0) || (deltaY != 0)) //If there is deltaX or deltaY movement, print the data. |
PixArtVY | 0:2c144b6813d1 | 189 | { |
PixArtVY | 0:2c144b6813d1 | 190 | pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY); |
PixArtVY | 0:2c144b6813d1 | 191 | } |
PixArtVY | 0:2c144b6813d1 | 192 | |
PixArtVY | 0:2c144b6813d1 | 193 | deltaX = 0; //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten. |
PixArtVY | 0:2c144b6813d1 | 194 | deltaY = 0; |
PixArtVY | 0:2c144b6813d1 | 195 | } |