Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: eye_r_eye-htpa32x32-v_1_8-ticker mbed
Fork of eye_r_eye-htpa32x32-v_1_8-ticker by
heimann32x32.cpp@2:270ef0ab1a2e, 2018-03-15 (annotated)
- Committer:
- withboobs
- Date:
- Thu Mar 15 13:16:22 2018 +0000
- Revision:
- 2:270ef0ab1a2e
- Parent:
- 1:7fd5cc2052cf
Use eoc between the conversion, rather than an external timer. I2C speed set to 400kHz which seems to work fine. If some blocks don't get downloaded right, decrease that to 100kHz.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
withboobs | 0:851be67e4df5 | 1 | #include "mbed.h" |
withboobs | 0:851be67e4df5 | 2 | #include "heimann32x32.h" |
withboobs | 0:851be67e4df5 | 3 | #include "heimann32x32_table.cpp" |
withboobs | 0:851be67e4df5 | 4 | |
withboobs | 0:851be67e4df5 | 5 | HTPA32x32::HTPA32x32(I2C * _i2c, I2C * _i2ce, uint8_t addr1, uint8_t addr2) |
withboobs | 0:851be67e4df5 | 6 | { |
withboobs | 0:851be67e4df5 | 7 | i2c = _i2c; |
withboobs | 2:270ef0ab1a2e | 8 | i2c->frequency(400000); |
withboobs | 1:7fd5cc2052cf | 9 | //i2c->frequency(1000000); |
withboobs | 0:851be67e4df5 | 10 | |
withboobs | 0:851be67e4df5 | 11 | i2ce = _i2ce; |
withboobs | 2:270ef0ab1a2e | 12 | i2ce->frequency(400000); |
withboobs | 0:851be67e4df5 | 13 | |
withboobs | 0:851be67e4df5 | 14 | i2c_addr_htpa = addr1; |
withboobs | 0:851be67e4df5 | 15 | i2c_addr_eeprom = addr2; |
withboobs | 0:851be67e4df5 | 16 | |
withboobs | 0:851be67e4df5 | 17 | proc = HTPA_PROC_ELOFFS | HTPA_PROC_THOFFS | HTPA_PROC_CONVERT; |
withboobs | 0:851be67e4df5 | 18 | available = 0; |
withboobs | 0:851be67e4df5 | 19 | } |
withboobs | 0:851be67e4df5 | 20 | |
withboobs | 0:851be67e4df5 | 21 | uint8_t |
withboobs | 0:851be67e4df5 | 22 | HTPA32x32::ADC( void ) |
withboobs | 0:851be67e4df5 | 23 | { |
withboobs | 0:851be67e4df5 | 24 | return mbit; |
withboobs | 0:851be67e4df5 | 25 | } |
withboobs | 0:851be67e4df5 | 26 | |
withboobs | 0:851be67e4df5 | 27 | uint8_t |
withboobs | 0:851be67e4df5 | 28 | HTPA32x32::ADC( uint8_t m ) |
withboobs | 0:851be67e4df5 | 29 | { |
withboobs | 0:851be67e4df5 | 30 | char data_write[2]; |
withboobs | 0:851be67e4df5 | 31 | data_write[0] = HTP_TRIM1_ADC; |
withboobs | 0:851be67e4df5 | 32 | |
withboobs | 0:851be67e4df5 | 33 | if (m < 4) |
withboobs | 0:851be67e4df5 | 34 | m = 4; |
withboobs | 0:851be67e4df5 | 35 | if (m > 12) |
withboobs | 0:851be67e4df5 | 36 | m = 12; |
withboobs | 0:851be67e4df5 | 37 | mbit = m; |
withboobs | 0:851be67e4df5 | 38 | |
withboobs | 0:851be67e4df5 | 39 | data_write[1] = m & TRIM1_ADC_MASK; |
withboobs | 0:851be67e4df5 | 40 | |
withboobs | 0:851be67e4df5 | 41 | i2c->write((int) i2c_addr_htpa, data_write, 2, 1); |
withboobs | 2:270ef0ab1a2e | 42 | wait_ms(5); |
withboobs | 0:851be67e4df5 | 43 | return mbit; |
withboobs | 0:851be67e4df5 | 44 | } |
withboobs | 0:851be67e4df5 | 45 | |
withboobs | 0:851be67e4df5 | 46 | uint8_t HTPA32x32::BIAS ( void ) |
withboobs | 0:851be67e4df5 | 47 | { |
withboobs | 0:851be67e4df5 | 48 | return bias; |
withboobs | 0:851be67e4df5 | 49 | } |
withboobs | 0:851be67e4df5 | 50 | |
withboobs | 0:851be67e4df5 | 51 | uint8_t HTPA32x32::BIAS ( uint8_t m ) |
withboobs | 0:851be67e4df5 | 52 | { |
withboobs | 0:851be67e4df5 | 53 | char data_write[2]; |
withboobs | 0:851be67e4df5 | 54 | |
withboobs | 0:851be67e4df5 | 55 | data_write[0] = HTP_TRIM2_BIAS1; |
withboobs | 0:851be67e4df5 | 56 | if (m > 31) |
withboobs | 0:851be67e4df5 | 57 | m = 31; |
withboobs | 0:851be67e4df5 | 58 | bias = m; |
withboobs | 0:851be67e4df5 | 59 | data_write[1] = m & TRIM_BIAS_MASK; |
withboobs | 0:851be67e4df5 | 60 | i2c->write(i2c_addr_htpa, data_write, 2, 1); |
withboobs | 2:270ef0ab1a2e | 61 | wait_ms(5); |
withboobs | 0:851be67e4df5 | 62 | |
withboobs | 0:851be67e4df5 | 63 | data_write[0] = HTP_TRIM3_BIAS2; |
withboobs | 0:851be67e4df5 | 64 | i2c->write(i2c_addr_htpa, data_write, 2, 1); |
withboobs | 2:270ef0ab1a2e | 65 | wait_ms(5); |
withboobs | 2:270ef0ab1a2e | 66 | |
withboobs | 0:851be67e4df5 | 67 | return bias; |
withboobs | 0:851be67e4df5 | 68 | } |
withboobs | 0:851be67e4df5 | 69 | |
withboobs | 0:851be67e4df5 | 70 | uint8_t HTPA32x32::CLOCK( void ) |
withboobs | 0:851be67e4df5 | 71 | { |
withboobs | 0:851be67e4df5 | 72 | return clk; |
withboobs | 0:851be67e4df5 | 73 | } |
withboobs | 0:851be67e4df5 | 74 | |
withboobs | 0:851be67e4df5 | 75 | uint8_t HTPA32x32::CLOCK( uint8_t m ) |
withboobs | 0:851be67e4df5 | 76 | { |
withboobs | 0:851be67e4df5 | 77 | char data_write[2]; |
withboobs | 0:851be67e4df5 | 78 | data_write[0] = HTP_TRIM4_FREQ; |
withboobs | 0:851be67e4df5 | 79 | if (m > 63) |
withboobs | 0:851be67e4df5 | 80 | m = 63; |
withboobs | 0:851be67e4df5 | 81 | clk = m; |
withboobs | 0:851be67e4df5 | 82 | |
withboobs | 0:851be67e4df5 | 83 | data_write[1] = m & TRIM_FREQ_MASK; |
withboobs | 0:851be67e4df5 | 84 | i2c->write(i2c_addr_htpa, data_write, 2, 1); |
withboobs | 2:270ef0ab1a2e | 85 | wait_ms(5); |
withboobs | 0:851be67e4df5 | 86 | |
withboobs | 0:851be67e4df5 | 87 | return clk; |
withboobs | 0:851be67e4df5 | 88 | } |
withboobs | 0:851be67e4df5 | 89 | |
withboobs | 0:851be67e4df5 | 90 | uint8_t HTPA32x32::PU( void ) |
withboobs | 0:851be67e4df5 | 91 | { |
withboobs | 0:851be67e4df5 | 92 | return (pu>>4); |
withboobs | 0:851be67e4df5 | 93 | } |
withboobs | 0:851be67e4df5 | 94 | |
withboobs | 0:851be67e4df5 | 95 | uint8_t HTPA32x32::PU( uint8_t m ) |
withboobs | 0:851be67e4df5 | 96 | { |
withboobs | 0:851be67e4df5 | 97 | char data_write[2]; |
withboobs | 0:851be67e4df5 | 98 | data_write[0] = HTP_TRIM7_PU; |
withboobs | 0:851be67e4df5 | 99 | if (m == 1 || m ==2 || m==4 || m==8) |
withboobs | 0:851be67e4df5 | 100 | { |
withboobs | 0:851be67e4df5 | 101 | pu = m<<4 | m; |
withboobs | 0:851be67e4df5 | 102 | data_write[1] = pu; |
withboobs | 0:851be67e4df5 | 103 | i2c->write(i2c_addr_htpa, data_write, 2, 1); |
withboobs | 2:270ef0ab1a2e | 104 | wait_ms(5); |
withboobs | 0:851be67e4df5 | 105 | } |
withboobs | 0:851be67e4df5 | 106 | |
withboobs | 0:851be67e4df5 | 107 | return pu; |
withboobs | 0:851be67e4df5 | 108 | } |
withboobs | 0:851be67e4df5 | 109 | |
withboobs | 0:851be67e4df5 | 110 | uint8_t HTPA32x32::BPA ( void ) |
withboobs | 0:851be67e4df5 | 111 | { |
withboobs | 0:851be67e4df5 | 112 | return bpa; |
withboobs | 0:851be67e4df5 | 113 | } |
withboobs | 0:851be67e4df5 | 114 | |
withboobs | 0:851be67e4df5 | 115 | uint8_t HTPA32x32::BPA ( uint8_t m ) |
withboobs | 0:851be67e4df5 | 116 | { |
withboobs | 0:851be67e4df5 | 117 | char data_write[2]; |
withboobs | 0:851be67e4df5 | 118 | data_write[0] = HTP_TRIM5_BPA1; |
withboobs | 0:851be67e4df5 | 119 | |
withboobs | 0:851be67e4df5 | 120 | if (m > 31) |
withboobs | 0:851be67e4df5 | 121 | m = 31; |
withboobs | 0:851be67e4df5 | 122 | bpa = m & TRIM_BPA_MASK; |
withboobs | 0:851be67e4df5 | 123 | data_write[1] = bpa; |
withboobs | 0:851be67e4df5 | 124 | i2c->write(i2c_addr_htpa, data_write, 2, 1); |
withboobs | 2:270ef0ab1a2e | 125 | wait_ms(5); |
withboobs | 0:851be67e4df5 | 126 | |
withboobs | 0:851be67e4df5 | 127 | data_write[0] = HTP_TRIM6_BPA2; |
withboobs | 0:851be67e4df5 | 128 | i2c->write(i2c_addr_htpa, data_write, 2, 1); |
withboobs | 2:270ef0ab1a2e | 129 | wait_ms(5); |
withboobs | 2:270ef0ab1a2e | 130 | |
withboobs | 0:851be67e4df5 | 131 | return bpa; |
withboobs | 0:851be67e4df5 | 132 | } |
withboobs | 0:851be67e4df5 | 133 | |
withboobs | 0:851be67e4df5 | 134 | void HTPA32x32::read_eeprom(uint8_t HiReg, uint8_t LoReg, char *data_ptr, int DCnt) |
withboobs | 0:851be67e4df5 | 135 | { |
withboobs | 0:851be67e4df5 | 136 | char data_write[2]; |
withboobs | 0:851be67e4df5 | 137 | data_write[0] = HiReg; |
withboobs | 0:851be67e4df5 | 138 | data_write[1] = LoReg; |
withboobs | 0:851be67e4df5 | 139 | i2ce->write((int) i2c_addr_eeprom, data_write,2,1); |
withboobs | 0:851be67e4df5 | 140 | |
withboobs | 0:851be67e4df5 | 141 | while (DCnt) |
withboobs | 0:851be67e4df5 | 142 | { |
withboobs | 0:851be67e4df5 | 143 | if (DCnt > 32) |
withboobs | 0:851be67e4df5 | 144 | { |
withboobs | 0:851be67e4df5 | 145 | i2ce->read ((int) i2c_addr_eeprom, data_ptr, 32, 0); |
withboobs | 0:851be67e4df5 | 146 | DCnt -= 32; |
withboobs | 0:851be67e4df5 | 147 | data_ptr += 32; |
withboobs | 0:851be67e4df5 | 148 | } |
withboobs | 0:851be67e4df5 | 149 | else |
withboobs | 0:851be67e4df5 | 150 | { |
withboobs | 0:851be67e4df5 | 151 | i2ce->read ((int) i2c_addr_eeprom, data_ptr, DCnt, 0); |
withboobs | 0:851be67e4df5 | 152 | DCnt=0; |
withboobs | 0:851be67e4df5 | 153 | } |
withboobs | 0:851be67e4df5 | 154 | } |
withboobs | 0:851be67e4df5 | 155 | } |
withboobs | 0:851be67e4df5 | 156 | |
withboobs | 0:851be67e4df5 | 157 | void HTPA32x32::download_PIJ( void ) |
withboobs | 0:851be67e4df5 | 158 | { |
withboobs | 0:851be67e4df5 | 159 | int8_t i,j; |
withboobs | 0:851be67e4df5 | 160 | uint16_t * dummyPix = (uint16_t *) PixC; |
withboobs | 0:851be67e4df5 | 161 | float rcp_eps, a, b; |
withboobs | 0:851be67e4df5 | 162 | |
withboobs | 0:851be67e4df5 | 163 | read_eeprom(HTP_EEPROM_PIX_HI, HTP_EEPROM_PIX_LO, (char *) dummyPix, 2048); |
withboobs | 0:851be67e4df5 | 164 | read_eeprom(HTP_EEPROM_THGRAD_HI, HTP_EEPROM_THGRAD_LO, (char *) ThGrad, 2048); |
withboobs | 0:851be67e4df5 | 165 | read_eeprom(HTP_EEPROM_THOFFS_HI, HTP_EEPROM_THOFFS_LO, (char *) ThOffs, 2048); |
withboobs | 0:851be67e4df5 | 166 | |
withboobs | 1:7fd5cc2052cf | 167 | rcp_eps = (float) 100.0 / epsilon; |
withboobs | 0:851be67e4df5 | 168 | a = (PixCmax - PixCmin) / (float) 65535.0 * rcp_eps; |
withboobs | 0:851be67e4df5 | 169 | b = rcp_eps * PixCmin; |
withboobs | 0:851be67e4df5 | 170 | |
withboobs | 0:851be67e4df5 | 171 | /* top */ |
withboobs | 0:851be67e4df5 | 172 | for (i=31; i>=0; i--) |
withboobs | 0:851be67e4df5 | 173 | { |
withboobs | 0:851be67e4df5 | 174 | for (j=31; j>=0; j--) |
withboobs | 0:851be67e4df5 | 175 | { |
withboobs | 0:851be67e4df5 | 176 | PixC[i*32 + j] = (float) dummyPix[i*32 + j]; |
withboobs | 0:851be67e4df5 | 177 | PixC[i*32 + j] = a * PixC[i*32 + j] + b; |
withboobs | 1:7fd5cc2052cf | 178 | PixC[i*32 + j] = (float) 1e8 / (float) PixC[i*32 + j]; |
withboobs | 0:851be67e4df5 | 179 | } |
withboobs | 0:851be67e4df5 | 180 | } |
withboobs | 0:851be67e4df5 | 181 | |
withboobs | 0:851be67e4df5 | 182 | return; |
withboobs | 0:851be67e4df5 | 183 | } |
withboobs | 0:851be67e4df5 | 184 | |
withboobs | 0:851be67e4df5 | 185 | void HTPA32x32::init( void ) |
withboobs | 0:851be67e4df5 | 186 | { |
withboobs | 2:270ef0ab1a2e | 187 | char data_write[2] = { HTP_CONFIG, CONFIG_WAKEUP }; |
withboobs | 2:270ef0ab1a2e | 188 | i2c->write(i2c_addr_htpa, data_write, 2, 1); |
withboobs | 2:270ef0ab1a2e | 189 | |
withboobs | 0:851be67e4df5 | 190 | uint8_t raw[5] = {0}; |
withboobs | 0:851be67e4df5 | 191 | read_eeprom(HTP_EEPROM_CALIB_HI, HTP_EEPROM_CALIB_LO, (char *) raw, 5); |
withboobs | 0:851be67e4df5 | 192 | mbit = raw[0]; |
withboobs | 0:851be67e4df5 | 193 | bias = raw[1]; |
withboobs | 0:851be67e4df5 | 194 | clk = raw[2]; |
withboobs | 0:851be67e4df5 | 195 | bpa = raw[3]; |
withboobs | 0:851be67e4df5 | 196 | pu = raw[4]; |
withboobs | 0:851be67e4df5 | 197 | |
withboobs | 0:851be67e4df5 | 198 | read_eeprom(HTP_EEPROM_PTAT_HI, HTP_EEPROM_PTAT_GRAD_LO, (char *) &PTATgrad, 4); |
withboobs | 0:851be67e4df5 | 199 | read_eeprom(HTP_EEPROM_PTAT_HI, HTP_EEPROM_PTAT_OFFS_LO, (char *) &PTAToffs, 4); |
withboobs | 0:851be67e4df5 | 200 | read_eeprom(HTP_EEPROM_PIXC_HI, HTP_EEPROM_PIXCMIN_LO, (char *) &PixCmin, 4); |
withboobs | 0:851be67e4df5 | 201 | read_eeprom(HTP_EEPROM_PIXC_HI, HTP_EEPROM_PIXCMAX_LO, (char *) &PixCmax, 4); |
withboobs | 0:851be67e4df5 | 202 | read_eeprom(HTP_EEPROM_TN_EPS_HI, HTP_EEPROM_TN_LO, (char *) raw, 2); |
withboobs | 0:851be67e4df5 | 203 | tn=raw[0]; |
withboobs | 0:851be67e4df5 | 204 | epsilon=raw[1]; |
withboobs | 0:851be67e4df5 | 205 | read_eeprom(HTP_EEPROM_GRADSCALE_HI, HTP_EEPROM_GRADSCALE_LO, (char *) &gradScale, 1); |
withboobs | 0:851be67e4df5 | 206 | |
withboobs | 0:851be67e4df5 | 207 | download_PIJ(); |
withboobs | 0:851be67e4df5 | 208 | |
withboobs | 0:851be67e4df5 | 209 | Ta_dK = Ta_dK_prev = Ta_dK_prev_prev = 0; |
withboobs | 0:851be67e4df5 | 210 | return; |
withboobs | 0:851be67e4df5 | 211 | } |
withboobs | 0:851be67e4df5 | 212 | |
withboobs | 2:270ef0ab1a2e | 213 | uint8_t HTPA32x32::eoc ( ) |
withboobs | 0:851be67e4df5 | 214 | { |
withboobs | 0:851be67e4df5 | 215 | uint8_t rval = HTP_STATUS; |
withboobs | 0:851be67e4df5 | 216 | i2c->write(i2c_addr_htpa, (char *) &rval, 1, 1); |
withboobs | 0:851be67e4df5 | 217 | i2c->read (i2c_addr_htpa, (char *) &rval, 1, 0); |
withboobs | 0:851be67e4df5 | 218 | rval &= STATUS_EOC; |
withboobs | 0:851be67e4df5 | 219 | return rval; |
withboobs | 0:851be67e4df5 | 220 | } |
withboobs | 0:851be67e4df5 | 221 | |
withboobs | 0:851be67e4df5 | 222 | void HTPA32x32::start() |
withboobs | 0:851be67e4df5 | 223 | { |
withboobs | 0:851be67e4df5 | 224 | char data_write[2] = { HTP_CONFIG, CONFIG_WAKEUP | CONFIG_START }; |
withboobs | 0:851be67e4df5 | 225 | b = 0; |
withboobs | 0:851be67e4df5 | 226 | i2c->write(i2c_addr_htpa, data_write, 2, 1); |
withboobs | 0:851be67e4df5 | 227 | return; |
withboobs | 0:851be67e4df5 | 228 | } |
withboobs | 0:851be67e4df5 | 229 | |
withboobs | 0:851be67e4df5 | 230 | void HTPA32x32::readb() |
withboobs | 0:851be67e4df5 | 231 | { |
withboobs | 0:851be67e4df5 | 232 | uint8_t |
withboobs | 0:851be67e4df5 | 233 | i, j, data_write[2], data_buffer[258]; |
withboobs | 0:851be67e4df5 | 234 | |
withboobs | 0:851be67e4df5 | 235 | if (!b) |
withboobs | 0:851be67e4df5 | 236 | { |
withboobs | 0:851be67e4df5 | 237 | Ta_dK_prev_prev = Ta_dK_prev; |
withboobs | 0:851be67e4df5 | 238 | Ta_dK_prev = Ta_dK; |
withboobs | 0:851be67e4df5 | 239 | Ta_dK = PTAToffs; |
withboobs | 0:851be67e4df5 | 240 | available = 0; |
withboobs | 0:851be67e4df5 | 241 | } |
withboobs | 0:851be67e4df5 | 242 | |
withboobs | 0:851be67e4df5 | 243 | /** |
withboobs | 0:851be67e4df5 | 244 | * download top |
withboobs | 0:851be67e4df5 | 245 | */ |
withboobs | 0:851be67e4df5 | 246 | data_write[0] = HTP_DATA1; |
withboobs | 0:851be67e4df5 | 247 | i2c->write(i2c_addr_htpa, (char *) data_write, 1, 1); |
withboobs | 0:851be67e4df5 | 248 | i2c->read (i2c_addr_htpa, (char *) data_buffer, 258, 0); |
withboobs | 0:851be67e4df5 | 249 | |
withboobs | 0:851be67e4df5 | 250 | if (b < 4) |
withboobs | 0:851be67e4df5 | 251 | { |
withboobs | 0:851be67e4df5 | 252 | // process |
withboobs | 0:851be67e4df5 | 253 | Ta_dK += ( (data_buffer[0]<<5) + (data_buffer[1]>>3) ) * PTATgrad; |
withboobs | 0:851be67e4df5 | 254 | for(i=0; i<4; i++) |
withboobs | 0:851be67e4df5 | 255 | { |
withboobs | 0:851be67e4df5 | 256 | for (j=0; j<32; j++) |
withboobs | 0:851be67e4df5 | 257 | { |
withboobs | 0:851be67e4df5 | 258 | // endianess flip-flopping |
withboobs | 0:851be67e4df5 | 259 | uint8_t * d = (uint8_t *) &Data[i * 32 + j + b*128]; |
withboobs | 0:851be67e4df5 | 260 | d[0] = data_buffer[64*i + 2*j + 3]; // LO |
withboobs | 0:851be67e4df5 | 261 | d[1] = data_buffer[64*i + 2*j + 2]; // HI |
withboobs | 0:851be67e4df5 | 262 | } |
withboobs | 0:851be67e4df5 | 263 | } |
withboobs | 0:851be67e4df5 | 264 | } |
withboobs | 0:851be67e4df5 | 265 | else |
withboobs | 0:851be67e4df5 | 266 | { |
withboobs | 0:851be67e4df5 | 267 | /* electrical offsets */ |
withboobs | 0:851be67e4df5 | 268 | for(i=0; i<4; i++) |
withboobs | 0:851be67e4df5 | 269 | { |
withboobs | 0:851be67e4df5 | 270 | for (j=0; j<32; j++) |
withboobs | 0:851be67e4df5 | 271 | { |
withboobs | 0:851be67e4df5 | 272 | // endianess flip-flopping |
withboobs | 0:851be67e4df5 | 273 | uint8_t * d = (uint8_t *) &ElOffs[i * 32 + j ]; |
withboobs | 0:851be67e4df5 | 274 | d[0] = data_buffer[64*i + 2*j + 3]; // LO |
withboobs | 0:851be67e4df5 | 275 | d[1] = data_buffer[64*i + 2*j + 2]; // HI |
withboobs | 0:851be67e4df5 | 276 | } |
withboobs | 0:851be67e4df5 | 277 | } |
withboobs | 0:851be67e4df5 | 278 | } |
withboobs | 0:851be67e4df5 | 279 | |
withboobs | 0:851be67e4df5 | 280 | /** |
withboobs | 0:851be67e4df5 | 281 | * download bottom |
withboobs | 0:851be67e4df5 | 282 | */ |
withboobs | 0:851be67e4df5 | 283 | data_write[0] = HTP_DATA2; |
withboobs | 0:851be67e4df5 | 284 | i2c->write(i2c_addr_htpa, (char *) data_write, 1, 1); |
withboobs | 0:851be67e4df5 | 285 | i2c->read (i2c_addr_htpa, (char *) data_buffer, 258, 0); |
withboobs | 0:851be67e4df5 | 286 | if (b<4) |
withboobs | 0:851be67e4df5 | 287 | { |
withboobs | 0:851be67e4df5 | 288 | // process temperatures |
withboobs | 0:851be67e4df5 | 289 | Ta_dK += ( (data_buffer[0]<<5) + (data_buffer[1]>>3) ) * PTATgrad; |
withboobs | 0:851be67e4df5 | 290 | for(i=0; i<4; i++) |
withboobs | 0:851be67e4df5 | 291 | { |
withboobs | 0:851be67e4df5 | 292 | for (j=0; j<32; j++) |
withboobs | 0:851be67e4df5 | 293 | { |
withboobs | 0:851be67e4df5 | 294 | // endianess flip-flopping |
withboobs | 0:851be67e4df5 | 295 | uint8_t * d = (uint8_t *) &Data[(3-i) * 32 + j + (7-b)*128]; |
withboobs | 0:851be67e4df5 | 296 | d[0] = data_buffer[64*i + 2*j + 3]; // LO |
withboobs | 0:851be67e4df5 | 297 | d[1] = data_buffer[64*i + 2*j + 2]; // HI |
withboobs | 0:851be67e4df5 | 298 | } |
withboobs | 0:851be67e4df5 | 299 | } |
withboobs | 0:851be67e4df5 | 300 | } |
withboobs | 0:851be67e4df5 | 301 | else |
withboobs | 0:851be67e4df5 | 302 | { |
withboobs | 0:851be67e4df5 | 303 | /* electrical offsets */ |
withboobs | 0:851be67e4df5 | 304 | for(i=0; i<4; i++) |
withboobs | 0:851be67e4df5 | 305 | { |
withboobs | 0:851be67e4df5 | 306 | for (j=0; j<32; j++) |
withboobs | 0:851be67e4df5 | 307 | { |
withboobs | 0:851be67e4df5 | 308 | // endianess flip-flopping |
withboobs | 0:851be67e4df5 | 309 | uint8_t * d = (uint8_t *) &ElOffs[(3-i) * 32 + j + 128]; |
withboobs | 0:851be67e4df5 | 310 | d[0] = data_buffer[64*i + 2*j + 3]; // LO |
withboobs | 0:851be67e4df5 | 311 | d[1] = data_buffer[64*i + 2*j + 2]; // HI |
withboobs | 0:851be67e4df5 | 312 | } |
withboobs | 0:851be67e4df5 | 313 | } |
withboobs | 0:851be67e4df5 | 314 | } |
withboobs | 0:851be67e4df5 | 315 | |
withboobs | 0:851be67e4df5 | 316 | // initiate conversion for next call to readb: |
withboobs | 0:851be67e4df5 | 317 | data_write[0] = HTP_CONFIG; |
withboobs | 0:851be67e4df5 | 318 | data_write[1] = CONFIG_WAKEUP | CONFIG_START; |
withboobs | 0:851be67e4df5 | 319 | if (proc & HTPA_PROC_ELOFFS) |
withboobs | 0:851be67e4df5 | 320 | b = (b+1) % 5; |
withboobs | 0:851be67e4df5 | 321 | else |
withboobs | 0:851be67e4df5 | 322 | b = (b+1) % 4; |
withboobs | 0:851be67e4df5 | 323 | |
withboobs | 0:851be67e4df5 | 324 | if (b<4) |
withboobs | 0:851be67e4df5 | 325 | { |
withboobs | 0:851be67e4df5 | 326 | data_write[1] |= ((b & 0x03)<<4); |
withboobs | 0:851be67e4df5 | 327 | } |
withboobs | 0:851be67e4df5 | 328 | else |
withboobs | 0:851be67e4df5 | 329 | { |
withboobs | 0:851be67e4df5 | 330 | data_write[1] |= CONFIG_BLIND; |
withboobs | 0:851be67e4df5 | 331 | } |
withboobs | 0:851be67e4df5 | 332 | i2c->write(i2c_addr_htpa, (char *) data_write, 2, 1); |
withboobs | 0:851be67e4df5 | 333 | |
withboobs | 0:851be67e4df5 | 334 | if (!b) |
withboobs | 0:851be67e4df5 | 335 | available = 1; |
withboobs | 0:851be67e4df5 | 336 | } |
withboobs | 0:851be67e4df5 | 337 | |
withboobs | 0:851be67e4df5 | 338 | |
withboobs | 0:851be67e4df5 | 339 | /** |
withboobs | 0:851be67e4df5 | 340 | * prepare interpolation table based on Ta_dK |
withboobs | 0:851be67e4df5 | 341 | * calculate table every time Ta_dK changes |
withboobs | 0:851be67e4df5 | 342 | */ |
withboobs | 0:851be67e4df5 | 343 | void HTPA32x32::find_temptable_from_ta ( void ) |
withboobs | 0:851be67e4df5 | 344 | { |
withboobs | 0:851be67e4df5 | 345 | uint16_t j, i, i_star; |
withboobs | 0:851be67e4df5 | 346 | |
withboobs | 0:851be67e4df5 | 347 | if (Ta_dK <= XTATemps[0]) |
withboobs | 0:851be67e4df5 | 348 | { |
withboobs | 0:851be67e4df5 | 349 | for (j=0;j<NROFADELEMENTS;j++) |
withboobs | 0:851be67e4df5 | 350 | { |
withboobs | 0:851be67e4df5 | 351 | TempTable_TA[j] = TempTable[j][0]; |
withboobs | 0:851be67e4df5 | 352 | } |
withboobs | 0:851be67e4df5 | 353 | } |
withboobs | 0:851be67e4df5 | 354 | else if (Ta_dK >= XTATemps[NROFTAELEMENTS-1]) |
withboobs | 0:851be67e4df5 | 355 | { |
withboobs | 0:851be67e4df5 | 356 | for (j=0;j<NROFADELEMENTS;j++) |
withboobs | 0:851be67e4df5 | 357 | { |
withboobs | 0:851be67e4df5 | 358 | TempTable_TA[j] = TempTable[j][NROFTAELEMENTS-1]; |
withboobs | 0:851be67e4df5 | 359 | } |
withboobs | 0:851be67e4df5 | 360 | } |
withboobs | 0:851be67e4df5 | 361 | else |
withboobs | 0:851be67e4df5 | 362 | { |
withboobs | 0:851be67e4df5 | 363 | /* construct interpolant for Ta_dK for all V_j */ |
withboobs | 0:851be67e4df5 | 364 | for (i=1;i<(NROFTAELEMENTS-1);i++) |
withboobs | 0:851be67e4df5 | 365 | { |
withboobs | 0:851be67e4df5 | 366 | if (XTATemps[i] == Ta_dK) |
withboobs | 0:851be67e4df5 | 367 | { |
withboobs | 0:851be67e4df5 | 368 | i_star = i; |
withboobs | 0:851be67e4df5 | 369 | break; |
withboobs | 0:851be67e4df5 | 370 | } |
withboobs | 0:851be67e4df5 | 371 | if (XTATemps[i+1] == Ta_dK) |
withboobs | 0:851be67e4df5 | 372 | { |
withboobs | 0:851be67e4df5 | 373 | i++; |
withboobs | 0:851be67e4df5 | 374 | i_star = i; |
withboobs | 0:851be67e4df5 | 375 | break; |
withboobs | 0:851be67e4df5 | 376 | } |
withboobs | 0:851be67e4df5 | 377 | if ( (XTATemps[i] < Ta_dK) && (Ta_dK < XTATemps[i+1]) ) |
withboobs | 0:851be67e4df5 | 378 | { |
withboobs | 0:851be67e4df5 | 379 | i_star = i+1; |
withboobs | 0:851be67e4df5 | 380 | break; |
withboobs | 0:851be67e4df5 | 381 | } |
withboobs | 0:851be67e4df5 | 382 | } |
withboobs | 0:851be67e4df5 | 383 | |
withboobs | 0:851be67e4df5 | 384 | if (i == i_star) |
withboobs | 0:851be67e4df5 | 385 | { |
withboobs | 0:851be67e4df5 | 386 | for (j=0;j<NROFADELEMENTS;j++) |
withboobs | 0:851be67e4df5 | 387 | { |
withboobs | 0:851be67e4df5 | 388 | TempTable_TA[j] = TempTable[j][i]; |
withboobs | 0:851be67e4df5 | 389 | } |
withboobs | 0:851be67e4df5 | 390 | } |
withboobs | 0:851be67e4df5 | 391 | else |
withboobs | 0:851be67e4df5 | 392 | { |
withboobs | 0:851be67e4df5 | 393 | uint16_t d1 = Ta_dK - XTATemps[i]; |
withboobs | 0:851be67e4df5 | 394 | uint16_t d2 = XTATemps[i_star] - Ta_dK; |
withboobs | 0:851be67e4df5 | 395 | |
withboobs | 0:851be67e4df5 | 396 | for (j=0;j<NROFADELEMENTS;j++) |
withboobs | 0:851be67e4df5 | 397 | { |
withboobs | 0:851be67e4df5 | 398 | float d = (d2 * TempTable[j][i] + d1 * TempTable[j][i_star]) / TAEQUIDISTANCE; |
withboobs | 0:851be67e4df5 | 399 | TempTable_TA[j] = d; |
withboobs | 0:851be67e4df5 | 400 | } |
withboobs | 0:851be67e4df5 | 401 | } |
withboobs | 0:851be67e4df5 | 402 | } |
withboobs | 0:851be67e4df5 | 403 | } |
withboobs | 0:851be67e4df5 | 404 | |
withboobs | 0:851be67e4df5 | 405 | uint16_t HTPA32x32::find_dk_from_v ( int16_t v) |
withboobs | 0:851be67e4df5 | 406 | { |
withboobs | 0:851be67e4df5 | 407 | uint16_t t_dk, k; |
withboobs | 0:851be67e4df5 | 408 | |
withboobs | 0:851be67e4df5 | 409 | if ( v <= YADValues[0] ) |
withboobs | 0:851be67e4df5 | 410 | { |
withboobs | 0:851be67e4df5 | 411 | t_dk = TempTable_TA[0]; |
withboobs | 0:851be67e4df5 | 412 | } |
withboobs | 0:851be67e4df5 | 413 | else if ( v >= YADValues[NROFADELEMENTS-1] ) |
withboobs | 0:851be67e4df5 | 414 | { |
withboobs | 0:851be67e4df5 | 415 | t_dk = TempTable_TA[NROFADELEMENTS-1]; |
withboobs | 0:851be67e4df5 | 416 | } |
withboobs | 0:851be67e4df5 | 417 | else |
withboobs | 0:851be67e4df5 | 418 | { |
withboobs | 0:851be67e4df5 | 419 | for (k=1; k<NROFADELEMENTS; k++) |
withboobs | 0:851be67e4df5 | 420 | { |
withboobs | 0:851be67e4df5 | 421 | if ( v == YADValues[k] ) |
withboobs | 0:851be67e4df5 | 422 | { |
withboobs | 0:851be67e4df5 | 423 | t_dk = TempTable_TA[k]; |
withboobs | 0:851be67e4df5 | 424 | break; |
withboobs | 0:851be67e4df5 | 425 | } |
withboobs | 0:851be67e4df5 | 426 | else if ( v < YADValues[k] ) |
withboobs | 0:851be67e4df5 | 427 | { |
withboobs | 0:851be67e4df5 | 428 | float dummy = TempTable_TA[k-1]; |
withboobs | 0:851be67e4df5 | 429 | dummy += (TempTable_TA[k] - TempTable_TA[k-1]) * ( v - YADValues[k-1]) / ADEQUIDISTANCE; |
withboobs | 0:851be67e4df5 | 430 | t_dk = dummy; |
withboobs | 0:851be67e4df5 | 431 | break; |
withboobs | 0:851be67e4df5 | 432 | } |
withboobs | 0:851be67e4df5 | 433 | } |
withboobs | 0:851be67e4df5 | 434 | } // else |
withboobs | 0:851be67e4df5 | 435 | |
withboobs | 0:851be67e4df5 | 436 | return t_dk; |
withboobs | 0:851be67e4df5 | 437 | } |
withboobs | 0:851be67e4df5 | 438 | |
withboobs | 0:851be67e4df5 | 439 | void HTPA32x32::apply_offsets( void ) |
withboobs | 0:851be67e4df5 | 440 | { |
withboobs | 0:851be67e4df5 | 441 | uint16_t i,j; |
withboobs | 0:851be67e4df5 | 442 | float dummyData; |
withboobs | 0:851be67e4df5 | 443 | |
withboobs | 0:851be67e4df5 | 444 | // do we need to do apply any offsets? |
withboobs | 0:851be67e4df5 | 445 | if (!proc) |
withboobs | 0:851be67e4df5 | 446 | return; |
withboobs | 0:851be67e4df5 | 447 | |
withboobs | 0:851be67e4df5 | 448 | if (proc & HTPA_PROC_CONVERT) |
withboobs | 0:851be67e4df5 | 449 | { |
withboobs | 0:851be67e4df5 | 450 | if (Ta_dK_prev != Ta_dK) |
withboobs | 0:851be67e4df5 | 451 | { |
withboobs | 0:851be67e4df5 | 452 | find_temptable_from_ta (); |
withboobs | 0:851be67e4df5 | 453 | } |
withboobs | 0:851be67e4df5 | 454 | } |
withboobs | 0:851be67e4df5 | 455 | |
withboobs | 0:851be67e4df5 | 456 | /** |
withboobs | 0:851be67e4df5 | 457 | * finish calculation of temperatures: |
withboobs | 0:851be67e4df5 | 458 | * thermal offset |
withboobs | 0:851be67e4df5 | 459 | */ |
withboobs | 0:851be67e4df5 | 460 | for (i=0; i<16; i++) |
withboobs | 0:851be67e4df5 | 461 | { |
withboobs | 0:851be67e4df5 | 462 | for (j=0; j<32; j++) |
withboobs | 0:851be67e4df5 | 463 | { |
withboobs | 0:851be67e4df5 | 464 | /* thermal offsets */ |
withboobs | 0:851be67e4df5 | 465 | if (proc & HTPA_PROC_THOFFS) |
withboobs | 0:851be67e4df5 | 466 | { |
withboobs | 0:851be67e4df5 | 467 | Data[i * 32 + j] -= ThOffs[i * 32 + j]; |
withboobs | 0:851be67e4df5 | 468 | Data[i * 32 + j] -= ThGrad[i * 32 + j] * Ta_dK / (2<<gradScale); |
withboobs | 0:851be67e4df5 | 469 | } |
withboobs | 0:851be67e4df5 | 470 | /* electric (blind) offsets */ |
withboobs | 0:851be67e4df5 | 471 | if (proc & HTPA_PROC_ELOFFS) |
withboobs | 0:851be67e4df5 | 472 | Data[i * 32 + j] -= ElOffs[(i%4) * 32 + j]; |
withboobs | 0:851be67e4df5 | 473 | /* do interpolation */ |
withboobs | 0:851be67e4df5 | 474 | if (proc & HTPA_PROC_CONVERT) |
withboobs | 0:851be67e4df5 | 475 | { |
withboobs | 0:851be67e4df5 | 476 | dummyData = (float) Data[i * 32 + j] * PixC[i*32 + j] + TABLEOFFSET; |
withboobs | 0:851be67e4df5 | 477 | Data[i * 32 + j] = find_dk_from_v( dummyData ); |
withboobs | 0:851be67e4df5 | 478 | } |
withboobs | 0:851be67e4df5 | 479 | } |
withboobs | 0:851be67e4df5 | 480 | } |
withboobs | 0:851be67e4df5 | 481 | for (;i<32; i++) |
withboobs | 0:851be67e4df5 | 482 | { |
withboobs | 0:851be67e4df5 | 483 | for (j=0; j<32; j++) |
withboobs | 0:851be67e4df5 | 484 | { |
withboobs | 0:851be67e4df5 | 485 | /* thermal offsets */ |
withboobs | 0:851be67e4df5 | 486 | if (proc & HTPA_PROC_THOFFS) |
withboobs | 0:851be67e4df5 | 487 | { |
withboobs | 0:851be67e4df5 | 488 | Data[i * 32 + j] -= ThOffs[ (47-i) * 32 + j]; |
withboobs | 0:851be67e4df5 | 489 | Data[i * 32 + j] -= ThGrad[ (47-i) * 32 + j] * Ta_dK / (2<<gradScale); |
withboobs | 0:851be67e4df5 | 490 | } |
withboobs | 0:851be67e4df5 | 491 | /* electric (blind) offsets */ |
withboobs | 0:851be67e4df5 | 492 | if (proc & HTPA_PROC_ELOFFS) |
withboobs | 0:851be67e4df5 | 493 | Data[i * 32 + j] -= ElOffs[(i%4) * 32 + j + 128]; |
withboobs | 0:851be67e4df5 | 494 | /* do interpolation */ |
withboobs | 0:851be67e4df5 | 495 | if (proc & HTPA_PROC_CONVERT) |
withboobs | 0:851be67e4df5 | 496 | { |
withboobs | 0:851be67e4df5 | 497 | dummyData = (float) Data[i * 32 + j] * PixC[(47 - i)*32 + j] + TABLEOFFSET; |
withboobs | 0:851be67e4df5 | 498 | Data[i * 32 + j] = find_dk_from_v( dummyData ); |
withboobs | 0:851be67e4df5 | 499 | } |
withboobs | 0:851be67e4df5 | 500 | } |
withboobs | 0:851be67e4df5 | 501 | } |
withboobs | 0:851be67e4df5 | 502 | |
withboobs | 0:851be67e4df5 | 503 | } |
withboobs | 0:851be67e4df5 | 504 | |
withboobs | 0:851be67e4df5 | 505 | |
withboobs | 0:851be67e4df5 | 506 | |
withboobs | 0:851be67e4df5 | 507 | |
withboobs | 0:851be67e4df5 | 508 | |
withboobs | 0:851be67e4df5 | 509 | |
withboobs | 0:851be67e4df5 | 510 | |
withboobs | 0:851be67e4df5 | 511 |