Reference firmware for PixArt's PAA5100 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.
commHeaders/SPIcommFunctions.h@1:782127a132a3, 2018-07-18 (annotated)
- Committer:
- PixArtVY
- Date:
- Wed Jul 18 18:37:03 2018 +0000
- Revision:
- 1:782127a132a3
- Parent:
- 0:c8a2256e02c2
Added Apache License notice.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PixArtVY | 0:c8a2256e02c2 | 1 | //========================================================================= |
PixArtVY | 0:c8a2256e02c2 | 2 | //Communication pinouts for serial COM port, SPI, and interrupts |
PixArtVY | 0:c8a2256e02c2 | 3 | //========================================================================= |
PixArtVY | 0:c8a2256e02c2 | 4 | static Serial pc(USBTX, USBRX); //PC comm |
PixArtVY | 0:c8a2256e02c2 | 5 | static SPI spi(p23, p24, p25); //mosi, miso, sclk |
PixArtVY | 0:c8a2256e02c2 | 6 | static DigitalOut cs(p22); //chip select |
PixArtVY | 0:c8a2256e02c2 | 7 | |
PixArtVY | 0:c8a2256e02c2 | 8 | |
PixArtVY | 0:c8a2256e02c2 | 9 | //========================================================================= |
PixArtVY | 0:c8a2256e02c2 | 10 | //Variables and arrays used for communications and data storage |
PixArtVY | 0:c8a2256e02c2 | 11 | //========================================================================= |
PixArtVY | 0:c8a2256e02c2 | 12 | int8_t deltaX_low, deltaY_low; //Stores the low-bits of movement data. |
PixArtVY | 0:c8a2256e02c2 | 13 | int16_t deltaX_high, deltaY_high; //Stores the high-bits of movement data. |
PixArtVY | 0:c8a2256e02c2 | 14 | int16_t deltaX, deltaY; //Stores the combined value of low and high bits. |
PixArtVY | 0:c8a2256e02c2 | 15 | int16_t totalX, totalY = 0; //Stores the total deltaX and deltaY moved during runtime. |
PixArtVY | 0:c8a2256e02c2 | 16 | int startupFail; |
PixArtVY | 0:c8a2256e02c2 | 17 | |
PixArtVY | 0:c8a2256e02c2 | 18 | |
PixArtVY | 0:c8a2256e02c2 | 19 | //========================================================================= |
PixArtVY | 0:c8a2256e02c2 | 20 | //Functions used to communicate with the sensor and grab/print data |
PixArtVY | 0:c8a2256e02c2 | 21 | //========================================================================= |
PixArtVY | 0:c8a2256e02c2 | 22 | uint8_t readRegister(uint8_t addr); |
PixArtVY | 0:c8a2256e02c2 | 23 | //This function takes an 8-bit address in the form 0x00 and returns an 8-bit value in the form 0x00. |
PixArtVY | 0:c8a2256e02c2 | 24 | |
PixArtVY | 0:c8a2256e02c2 | 25 | void writeRegister(uint8_t addr, uint8_t data); |
PixArtVY | 0:c8a2256e02c2 | 26 | //This function takes an 8-bit address and 8-bit data. Writes the given data to the given address. |
PixArtVY | 0:c8a2256e02c2 | 27 | |
PixArtVY | 0:c8a2256e02c2 | 28 | void startupCheck(void); |
PixArtVY | 1:782127a132a3 | 29 | //Checks whether or not the sensor was started up properly. |
PixArtVY | 0:c8a2256e02c2 | 30 | |
PixArtVY | 0:c8a2256e02c2 | 31 | void initializeSensor(void); |
PixArtVY | 0:c8a2256e02c2 | 32 | //Sets all of the registers needed for initialization and performance optimization. |
PixArtVY | 0:c8a2256e02c2 | 33 | |
PixArtVY | 0:c8a2256e02c2 | 34 | void grabData(void); |
PixArtVY | 0:c8a2256e02c2 | 35 | //Grabs the deltaX and deltaY information from the proper registers and formats it into the proper format. |
PixArtVY | 0:c8a2256e02c2 | 36 | |
PixArtVY | 0:c8a2256e02c2 | 37 | void printData(void); |
PixArtVY | 0:c8a2256e02c2 | 38 | //Prints the data out to a serial terminal. |
PixArtVY | 0:c8a2256e02c2 | 39 | |
PixArtVY | 0:c8a2256e02c2 | 40 | |
PixArtVY | 0:c8a2256e02c2 | 41 | |
PixArtVY | 0:c8a2256e02c2 | 42 | |
PixArtVY | 0:c8a2256e02c2 | 43 | |
PixArtVY | 0:c8a2256e02c2 | 44 | //========================================================================= |
PixArtVY | 0:c8a2256e02c2 | 45 | //Functions definitions |
PixArtVY | 0:c8a2256e02c2 | 46 | //========================================================================= |
PixArtVY | 0:c8a2256e02c2 | 47 | uint8_t readRegister(uint8_t addr) |
PixArtVY | 0:c8a2256e02c2 | 48 | { |
PixArtVY | 0:c8a2256e02c2 | 49 | cs = 0; //Set chip select low/active |
PixArtVY | 0:c8a2256e02c2 | 50 | addr = addr & 0x7F; //Set MSB to 0 to indicate read operation |
PixArtVY | 0:c8a2256e02c2 | 51 | spi.write(addr); //Write the given address |
PixArtVY | 0:c8a2256e02c2 | 52 | wait_us(35); //Add a tiny delay after sending address for some internal cycle timing. |
PixArtVY | 0:c8a2256e02c2 | 53 | uint8_t data_read = spi.write(0x00); //Throw dummy byte after sending address to receieve data |
PixArtVY | 0:c8a2256e02c2 | 54 | cs = 1; //Set chip select back to high/inactive |
PixArtVY | 0:c8a2256e02c2 | 55 | return data_read; //Returns 8-bit data from register |
PixArtVY | 0:c8a2256e02c2 | 56 | } |
PixArtVY | 0:c8a2256e02c2 | 57 | |
PixArtVY | 0:c8a2256e02c2 | 58 | |
PixArtVY | 0:c8a2256e02c2 | 59 | //========================================================================= |
PixArtVY | 0:c8a2256e02c2 | 60 | void writeRegister(uint8_t addr, uint8_t data) |
PixArtVY | 0:c8a2256e02c2 | 61 | { |
PixArtVY | 0:c8a2256e02c2 | 62 | cs = 0; //Set chip select low/active |
PixArtVY | 0:c8a2256e02c2 | 63 | addr = addr | 0x80; //Set MSB to 1 to indicate write operation |
PixArtVY | 0:c8a2256e02c2 | 64 | spi.write(addr); //Write the given address |
PixArtVY | 0:c8a2256e02c2 | 65 | spi.write(data); //Write the given data |
PixArtVY | 0:c8a2256e02c2 | 66 | cs = 1; //Set chip select back to high/inactive |
PixArtVY | 0:c8a2256e02c2 | 67 | |
PixArtVY | 0:c8a2256e02c2 | 68 | //pc.printf("R:%2X, D:%2X\n\r", addr, readRegister(addr)); |
PixArtVY | 0:c8a2256e02c2 | 69 | //Uncomment this line for debugging. Prints every register write operation. |
PixArtVY | 0:c8a2256e02c2 | 70 | } |
PixArtVY | 0:c8a2256e02c2 | 71 | |
PixArtVY | 0:c8a2256e02c2 | 72 | |
PixArtVY | 0:c8a2256e02c2 | 73 | //========================================================================= |
PixArtVY | 0:c8a2256e02c2 | 74 | void startupCheck(void) |
PixArtVY | 0:c8a2256e02c2 | 75 | { |
PixArtVY | 0:c8a2256e02c2 | 76 | startupFail = 0; |
PixArtVY | 0:c8a2256e02c2 | 77 | writeRegister(0x7F, 0x00); |
PixArtVY | 0:c8a2256e02c2 | 78 | writeRegister(0x55, 0x01); |
PixArtVY | 0:c8a2256e02c2 | 79 | writeRegister(0x50, 0x07); |
PixArtVY | 0:c8a2256e02c2 | 80 | writeRegister(0x7F, 0x0E); |
PixArtVY | 0:c8a2256e02c2 | 81 | writeRegister(0x43, 0x10); |
PixArtVY | 0:c8a2256e02c2 | 82 | |
PixArtVY | 1:782127a132a3 | 83 | if(readRegister(0x47) != 0x08) |
PixArtVY | 0:c8a2256e02c2 | 84 | { |
PixArtVY | 1:782127a132a3 | 85 | for(int i=0; i<3; i++) //Checks register 0x47 three times. If the value is incorrect 3 times, throw a fail condition. |
PixArtVY | 0:c8a2256e02c2 | 86 | { |
PixArtVY | 1:782127a132a3 | 87 | if(readRegister(0x47) != 0x08) |
PixArtVY | 1:782127a132a3 | 88 | { |
PixArtVY | 1:782127a132a3 | 89 | writeRegister(0x43, 0x10) |
PixArtVY | 1:782127a132a3 | 90 | startupFail++; |
PixArtVY | 1:782127a132a3 | 91 | } |
PixArtVY | 0:c8a2256e02c2 | 92 | } |
PixArtVY | 0:c8a2256e02c2 | 93 | } |
PixArtVY | 0:c8a2256e02c2 | 94 | } |
PixArtVY | 0:c8a2256e02c2 | 95 | |
PixArtVY | 0:c8a2256e02c2 | 96 | |
PixArtVY | 0:c8a2256e02c2 | 97 | //========================================================================= |
PixArtVY | 0:c8a2256e02c2 | 98 | void initializeSensor(void) |
PixArtVY | 0:c8a2256e02c2 | 99 | { |
PixArtVY | 0:c8a2256e02c2 | 100 | writeRegister(0x7F, 0x00); |
PixArtVY | 0:c8a2256e02c2 | 101 | writeRegister(0x51, 0x7B); |
PixArtVY | 0:c8a2256e02c2 | 102 | writeRegister(0x50, 0x00); |
PixArtVY | 0:c8a2256e02c2 | 103 | writeRegister(0x55, 0x00); |
PixArtVY | 0:c8a2256e02c2 | 104 | writeRegister(0x7F, 0x0E); |
PixArtVY | 0:c8a2256e02c2 | 105 | |
PixArtVY | 0:c8a2256e02c2 | 106 | if(readRegister(0x73) == 0x00) |
PixArtVY | 0:c8a2256e02c2 | 107 | { |
PixArtVY | 0:c8a2256e02c2 | 108 | writeRegister(0x7F, 0x00); |
PixArtVY | 0:c8a2256e02c2 | 109 | writeRegister(0x61, 0xAD); |
PixArtVY | 0:c8a2256e02c2 | 110 | writeRegister(0x51, 0x70); |
PixArtVY | 0:c8a2256e02c2 | 111 | writeRegister(0x7F, 0x0E); |
PixArtVY | 0:c8a2256e02c2 | 112 | |
PixArtVY | 0:c8a2256e02c2 | 113 | if(readRegister(0x70) <= 28) |
PixArtVY | 0:c8a2256e02c2 | 114 | writeRegister(0x70, readRegister(0x70) + 14); |
PixArtVY | 0:c8a2256e02c2 | 115 | |
PixArtVY | 0:c8a2256e02c2 | 116 | else |
PixArtVY | 0:c8a2256e02c2 | 117 | writeRegister(0x70, readRegister(0x70) + 11); |
PixArtVY | 0:c8a2256e02c2 | 118 | |
PixArtVY | 0:c8a2256e02c2 | 119 | writeRegister(0x71, readRegister(0x71) * 45/100); |
PixArtVY | 0:c8a2256e02c2 | 120 | } |
PixArtVY | 0:c8a2256e02c2 | 121 | |
PixArtVY | 0:c8a2256e02c2 | 122 | writeRegister(0x7F, 0x00); |
PixArtVY | 0:c8a2256e02c2 | 123 | writeRegister(0x61, 0xAD); |
PixArtVY | 0:c8a2256e02c2 | 124 | writeRegister(0x7F, 0x03); |
PixArtVY | 0:c8a2256e02c2 | 125 | writeRegister(0x40, 0x00); |
PixArtVY | 0:c8a2256e02c2 | 126 | writeRegister(0x7F, 0x05); |
PixArtVY | 0:c8a2256e02c2 | 127 | |
PixArtVY | 0:c8a2256e02c2 | 128 | writeRegister(0x41, 0xB3); |
PixArtVY | 0:c8a2256e02c2 | 129 | writeRegister(0x43, 0xF1); |
PixArtVY | 0:c8a2256e02c2 | 130 | writeRegister(0x45, 0x14); |
PixArtVY | 0:c8a2256e02c2 | 131 | writeRegister(0x5B, 0x32); |
PixArtVY | 0:c8a2256e02c2 | 132 | writeRegister(0x5F, 0x34); |
PixArtVY | 0:c8a2256e02c2 | 133 | writeRegister(0x7B, 0x08); |
PixArtVY | 0:c8a2256e02c2 | 134 | writeRegister(0x5E, 0x34); |
PixArtVY | 0:c8a2256e02c2 | 135 | writeRegister(0x70, 0xE5); |
PixArtVY | 0:c8a2256e02c2 | 136 | writeRegister(0x71, 0xE5); |
PixArtVY | 0:c8a2256e02c2 | 137 | writeRegister(0x7F, 0x06); |
PixArtVY | 0:c8a2256e02c2 | 138 | writeRegister(0x44, 0x1B); |
PixArtVY | 0:c8a2256e02c2 | 139 | writeRegister(0x40, 0xBF); |
PixArtVY | 0:c8a2256e02c2 | 140 | writeRegister(0x4E, 0x3F); |
PixArtVY | 0:c8a2256e02c2 | 141 | writeRegister(0x7F, 0x08); |
PixArtVY | 0:c8a2256e02c2 | 142 | writeRegister(0x66, 0x44); |
PixArtVY | 0:c8a2256e02c2 | 143 | writeRegister(0x65, 0x20); |
PixArtVY | 0:c8a2256e02c2 | 144 | writeRegister(0x6A, 0x3A); |
PixArtVY | 0:c8a2256e02c2 | 145 | writeRegister(0x61, 0x01); |
PixArtVY | 0:c8a2256e02c2 | 146 | writeRegister(0x62, 0x01); |
PixArtVY | 0:c8a2256e02c2 | 147 | writeRegister(0x7F, 0x09); |
PixArtVY | 0:c8a2256e02c2 | 148 | writeRegister(0x4F, 0xAF); |
PixArtVY | 0:c8a2256e02c2 | 149 | writeRegister(0x5F, 0x40); |
PixArtVY | 0:c8a2256e02c2 | 150 | writeRegister(0x48, 0x80); |
PixArtVY | 0:c8a2256e02c2 | 151 | writeRegister(0x49, 0x80); |
PixArtVY | 0:c8a2256e02c2 | 152 | writeRegister(0x57, 0x77); |
PixArtVY | 0:c8a2256e02c2 | 153 | writeRegister(0x60, 0x78); |
PixArtVY | 0:c8a2256e02c2 | 154 | writeRegister(0x61, 0x78); |
PixArtVY | 0:c8a2256e02c2 | 155 | writeRegister(0x62, 0x08); |
PixArtVY | 0:c8a2256e02c2 | 156 | writeRegister(0x63, 0x50); |
PixArtVY | 0:c8a2256e02c2 | 157 | writeRegister(0x7F, 0x0A); |
PixArtVY | 0:c8a2256e02c2 | 158 | writeRegister(0x45, 0x60); |
PixArtVY | 0:c8a2256e02c2 | 159 | writeRegister(0x7F, 0x00); |
PixArtVY | 0:c8a2256e02c2 | 160 | writeRegister(0x4D, 0x11); |
PixArtVY | 0:c8a2256e02c2 | 161 | writeRegister(0x55, 0x80); |
PixArtVY | 0:c8a2256e02c2 | 162 | writeRegister(0x74, 0x1F); |
PixArtVY | 0:c8a2256e02c2 | 163 | writeRegister(0x75, 0x1F); |
PixArtVY | 0:c8a2256e02c2 | 164 | writeRegister(0x4A, 0x78); |
PixArtVY | 0:c8a2256e02c2 | 165 | writeRegister(0x4B, 0x78); |
PixArtVY | 0:c8a2256e02c2 | 166 | writeRegister(0x44, 0x08); |
PixArtVY | 0:c8a2256e02c2 | 167 | writeRegister(0x45, 0x50); |
PixArtVY | 0:c8a2256e02c2 | 168 | writeRegister(0x64, 0xFF); |
PixArtVY | 0:c8a2256e02c2 | 169 | writeRegister(0x65, 0x1F); |
PixArtVY | 0:c8a2256e02c2 | 170 | writeRegister(0x7F, 0x14); |
PixArtVY | 0:c8a2256e02c2 | 171 | writeRegister(0x65, 0x67); |
PixArtVY | 0:c8a2256e02c2 | 172 | writeRegister(0x66, 0x08); |
PixArtVY | 0:c8a2256e02c2 | 173 | writeRegister(0x63, 0x70); |
PixArtVY | 0:c8a2256e02c2 | 174 | writeRegister(0x6F, 0x1C); |
PixArtVY | 0:c8a2256e02c2 | 175 | writeRegister(0x7F, 0x15); |
PixArtVY | 0:c8a2256e02c2 | 176 | writeRegister(0x48, 0x48); |
PixArtVY | 0:c8a2256e02c2 | 177 | writeRegister(0x7F, 0x07); |
PixArtVY | 0:c8a2256e02c2 | 178 | writeRegister(0x41, 0x0D); |
PixArtVY | 0:c8a2256e02c2 | 179 | writeRegister(0x43, 0x14); |
PixArtVY | 0:c8a2256e02c2 | 180 | writeRegister(0x4B, 0x0E); |
PixArtVY | 0:c8a2256e02c2 | 181 | writeRegister(0x45, 0x0F); |
PixArtVY | 0:c8a2256e02c2 | 182 | writeRegister(0x44, 0x42); |
PixArtVY | 0:c8a2256e02c2 | 183 | writeRegister(0x4C, 0x80); |
PixArtVY | 0:c8a2256e02c2 | 184 | writeRegister(0x7F, 0x10); |
PixArtVY | 0:c8a2256e02c2 | 185 | writeRegister(0x5B, 0x02); |
PixArtVY | 0:c8a2256e02c2 | 186 | writeRegister(0x7F, 0x07); |
PixArtVY | 0:c8a2256e02c2 | 187 | writeRegister(0x40, 0x41); |
PixArtVY | 0:c8a2256e02c2 | 188 | |
PixArtVY | 0:c8a2256e02c2 | 189 | wait_ms(10); |
PixArtVY | 0:c8a2256e02c2 | 190 | |
PixArtVY | 0:c8a2256e02c2 | 191 | writeRegister(0x7F, 0x00); |
PixArtVY | 0:c8a2256e02c2 | 192 | writeRegister(0x32, 0x00); |
PixArtVY | 0:c8a2256e02c2 | 193 | writeRegister(0x7F, 0x07); |
PixArtVY | 0:c8a2256e02c2 | 194 | writeRegister(0x68, 0xF0); |
PixArtVY | 0:c8a2256e02c2 | 195 | writeRegister(0x69, 0x00); |
PixArtVY | 0:c8a2256e02c2 | 196 | writeRegister(0x7F, 0x0D); |
PixArtVY | 0:c8a2256e02c2 | 197 | writeRegister(0x48, 0xC0); |
PixArtVY | 0:c8a2256e02c2 | 198 | writeRegister(0x6F, 0xD5); |
PixArtVY | 0:c8a2256e02c2 | 199 | writeRegister(0x7F, 0x00); |
PixArtVY | 0:c8a2256e02c2 | 200 | writeRegister(0x5B, 0xA0); |
PixArtVY | 0:c8a2256e02c2 | 201 | writeRegister(0x4E, 0xA8); |
PixArtVY | 0:c8a2256e02c2 | 202 | writeRegister(0x5A, 0x90); |
PixArtVY | 0:c8a2256e02c2 | 203 | writeRegister(0x40, 0x80); |
PixArtVY | 0:c8a2256e02c2 | 204 | writeRegister(0x7F, 0x08); |
PixArtVY | 0:c8a2256e02c2 | 205 | writeRegister(0x6F, 0x9F); |
PixArtVY | 0:c8a2256e02c2 | 206 | |
PixArtVY | 0:c8a2256e02c2 | 207 | wait_ms(10): |
PixArtVY | 0:c8a2256e02c2 | 208 | |
PixArtVY | 0:c8a2256e02c2 | 209 | writeRegister(0x6F, 0x00); |
PixArtVY | 0:c8a2256e02c2 | 210 | writeRegister(0x7F, 0x14); |
PixArtVY | 0:c8a2256e02c2 | 211 | writeRegister(0x6F, 0x1C); |
PixArtVY | 0:c8a2256e02c2 | 212 | } |
PixArtVY | 0:c8a2256e02c2 | 213 | |
PixArtVY | 0:c8a2256e02c2 | 214 | |
PixArtVY | 0:c8a2256e02c2 | 215 | //========================================================================= |
PixArtVY | 0:c8a2256e02c2 | 216 | void grabData(void) |
PixArtVY | 0:c8a2256e02c2 | 217 | { |
PixArtVY | 0:c8a2256e02c2 | 218 | deltaX_low = readRegister(0x03); //Grabs data from the proper registers. |
PixArtVY | 0:c8a2256e02c2 | 219 | deltaX_high = (readRegister(0x04)<<8) & 0xFF00; //Grabs data and shifts it to make space to be combined with lower bits. |
PixArtVY | 0:c8a2256e02c2 | 220 | deltaY_low = readRegister(0x05); |
PixArtVY | 0:c8a2256e02c2 | 221 | deltaY_high = (readRegister(0x06)<<8) & 0xFF00; |
PixArtVY | 0:c8a2256e02c2 | 222 | |
PixArtVY | 0:c8a2256e02c2 | 223 | deltaX = deltaX_high | deltaX_low; //Combines the low and high bits. |
PixArtVY | 0:c8a2256e02c2 | 224 | deltaY = deltaY_high | deltaY_low; |
PixArtVY | 0:c8a2256e02c2 | 225 | } |
PixArtVY | 0:c8a2256e02c2 | 226 | |
PixArtVY | 0:c8a2256e02c2 | 227 | |
PixArtVY | 0:c8a2256e02c2 | 228 | //========================================================================= |
PixArtVY | 0:c8a2256e02c2 | 229 | void printData(void) |
PixArtVY | 0:c8a2256e02c2 | 230 | { |
PixArtVY | 0:c8a2256e02c2 | 231 | if((deltaX != 0) || (deltaY != 0)) //If there is deltaX or deltaY movement, print the data. |
PixArtVY | 0:c8a2256e02c2 | 232 | { |
PixArtVY | 0:c8a2256e02c2 | 233 | totalX += deltaX; |
PixArtVY | 0:c8a2256e02c2 | 234 | totalY += deltaY; |
PixArtVY | 0:c8a2256e02c2 | 235 | |
PixArtVY | 0:c8a2256e02c2 | 236 | pc.printf("deltaX: %d\t\t\tdeltaY: %d\n\r", deltaX, deltaY); //Prints each individual count of deltaX and deltaY. |
PixArtVY | 0:c8a2256e02c2 | 237 | pc.printf("X-axis Counts: %d\t\tY-axis Counts: %d\n\r", totalX, totalY); //Prints the total movement made during runtime. |
PixArtVY | 0:c8a2256e02c2 | 238 | } |
PixArtVY | 0:c8a2256e02c2 | 239 | |
PixArtVY | 0:c8a2256e02c2 | 240 | deltaX = 0; //Resets deltaX and Y values to zero, otherwise previous data is stored until overwritten. |
PixArtVY | 0:c8a2256e02c2 | 241 | deltaY = 0; |
PixArtVY | 0:c8a2256e02c2 | 242 | } |