GSMA version

Dependencies:   FXOS8700CQ mbed sfh7779

Fork of StarterKit by Rick McConney

Committer:
stefanrousseau
Date:
Wed Jul 20 04:01:03 2016 +0000
Revision:
30:48b43538e98b
Parent:
29:e6c8bd41caa6
Changed the motion sensor instantiation from static to dynamic inside the function that uses it.  This is needed because the WNC on the shield corrupts the I2C bus during initialization.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stefanrousseau 4:f83bedd9cab4 1 #include "mbed.h"
stefanrousseau 4:f83bedd9cab4 2 #include "sensors.h"
stefanrousseau 4:f83bedd9cab4 3
stefanrousseau 4:f83bedd9cab4 4 //I2C for pmod sensors:
stefanrousseau 4:f83bedd9cab4 5 #define Si1145_PMOD_I2C_ADDR 0xC0 //this is for 7-bit addr 0x60 for the Si7020
stefanrousseau 4:f83bedd9cab4 6 #define Si7020_PMOD_I2C_ADDR 0x80 //this is for 7-bit addr 0x4 for the Si7020
stefanrousseau 4:f83bedd9cab4 7
stefanrousseau 11:e6602513730f 8 #include "hardware.h"
stefanrousseau 4:f83bedd9cab4 9
stefanrousseau 4:f83bedd9cab4 10 #include "FXOS8700CQ.h"
stefanrousseau 30:48b43538e98b 11 // Storage for the data from the motion sensor
stefanrousseau 4:f83bedd9cab4 12 SRAWDATA accel_data;
stefanrousseau 4:f83bedd9cab4 13 SRAWDATA magn_data;
stefanrousseau 4:f83bedd9cab4 14 //InterruptIn fxos_int1(PTC6); // unused, common with SW2 on FRDM-K64F
stefanrousseau 4:f83bedd9cab4 15 InterruptIn fxos_int2(PTC13); // should just be the Data-Ready interrupt
stefanrousseau 4:f83bedd9cab4 16 bool fxos_int2_triggered = false;
stefanrousseau 4:f83bedd9cab4 17 void trigger_fxos_int2(void)
stefanrousseau 4:f83bedd9cab4 18 {
stefanrousseau 4:f83bedd9cab4 19 fxos_int2_triggered = true;
stefanrousseau 4:f83bedd9cab4 20 }
stefanrousseau 4:f83bedd9cab4 21
stefanrousseau 4:f83bedd9cab4 22 /*------------------------------------------------------------------------------
stefanrousseau 4:f83bedd9cab4 23 * Perform I2C single read.
stefanrousseau 4:f83bedd9cab4 24 *------------------------------------------------------------------------------*/
stefanrousseau 4:f83bedd9cab4 25 unsigned char I2C_ReadSingleByte(unsigned char ucDeviceAddress)
stefanrousseau 4:f83bedd9cab4 26 {
stefanrousseau 4:f83bedd9cab4 27 char rxbuffer [1];
stefanrousseau 11:e6602513730f 28 i2c.read(ucDeviceAddress, rxbuffer, 1 );
stefanrousseau 4:f83bedd9cab4 29 return (unsigned char)rxbuffer[0];
stefanrousseau 4:f83bedd9cab4 30 } //I2C_ReadSingleByte()
stefanrousseau 4:f83bedd9cab4 31
stefanrousseau 4:f83bedd9cab4 32 /*------------------------------------------------------------------------------
stefanrousseau 4:f83bedd9cab4 33 * Perform I2C single read from address.
stefanrousseau 4:f83bedd9cab4 34 *------------------------------------------------------------------------------*/
stefanrousseau 4:f83bedd9cab4 35 unsigned char I2C_ReadSingleByteFromAddr(unsigned char ucDeviceAddress, unsigned char Addr)
stefanrousseau 4:f83bedd9cab4 36 {
stefanrousseau 4:f83bedd9cab4 37 char txbuffer [1];
stefanrousseau 4:f83bedd9cab4 38 char rxbuffer [1];
stefanrousseau 4:f83bedd9cab4 39 txbuffer[0] = (char)Addr;
stefanrousseau 11:e6602513730f 40 i2c.write(ucDeviceAddress, txbuffer, 1 );
stefanrousseau 11:e6602513730f 41 i2c.read(ucDeviceAddress, rxbuffer, 1 );
stefanrousseau 4:f83bedd9cab4 42 return (unsigned char)rxbuffer[0];
stefanrousseau 4:f83bedd9cab4 43 } //I2C_ReadSingleByteFromAddr()
stefanrousseau 4:f83bedd9cab4 44
stefanrousseau 4:f83bedd9cab4 45 /*------------------------------------------------------------------------------
stefanrousseau 4:f83bedd9cab4 46 * Perform I2C read of more than 1 byte.
stefanrousseau 4:f83bedd9cab4 47 *------------------------------------------------------------------------------*/
stefanrousseau 4:f83bedd9cab4 48 int I2C_ReadMultipleBytes(unsigned char ucDeviceAddress, char *ucData, unsigned char ucLength)
stefanrousseau 4:f83bedd9cab4 49 {
stefanrousseau 4:f83bedd9cab4 50 int status;
stefanrousseau 11:e6602513730f 51 status = i2c.read(ucDeviceAddress, ucData, ucLength);
stefanrousseau 4:f83bedd9cab4 52 return status;
stefanrousseau 4:f83bedd9cab4 53 } //I2C_ReadMultipleBytes()
stefanrousseau 4:f83bedd9cab4 54
stefanrousseau 4:f83bedd9cab4 55 /*------------------------------------------------------------------------------
stefanrousseau 4:f83bedd9cab4 56 * Perform I2C write of a single byte.
stefanrousseau 4:f83bedd9cab4 57 *------------------------------------------------------------------------------*/
stefanrousseau 4:f83bedd9cab4 58 int I2C_WriteSingleByte(unsigned char ucDeviceAddress, unsigned char Data, bool bSendStop)
stefanrousseau 4:f83bedd9cab4 59 {
stefanrousseau 4:f83bedd9cab4 60 int status;
stefanrousseau 4:f83bedd9cab4 61 char txbuffer [1];
stefanrousseau 4:f83bedd9cab4 62 txbuffer[0] = (char)Data; //data
stefanrousseau 11:e6602513730f 63 status = i2c.write(ucDeviceAddress, txbuffer, 1, !bSendStop); //true: do not send stop
stefanrousseau 4:f83bedd9cab4 64 return status;
stefanrousseau 4:f83bedd9cab4 65 } //I2C_WriteSingleByte()
stefanrousseau 4:f83bedd9cab4 66
stefanrousseau 4:f83bedd9cab4 67 /*------------------------------------------------------------------------------
stefanrousseau 4:f83bedd9cab4 68 * Perform I2C write of 1 byte to an address.
stefanrousseau 4:f83bedd9cab4 69 *------------------------------------------------------------------------------*/
stefanrousseau 4:f83bedd9cab4 70 int I2C_WriteSingleByteToAddr(unsigned char ucDeviceAddress, unsigned char Addr, unsigned char Data, bool bSendStop)
stefanrousseau 4:f83bedd9cab4 71 {
stefanrousseau 4:f83bedd9cab4 72 int status;
stefanrousseau 4:f83bedd9cab4 73 char txbuffer [2];
stefanrousseau 4:f83bedd9cab4 74 txbuffer[0] = (char)Addr; //address
stefanrousseau 4:f83bedd9cab4 75 txbuffer[1] = (char)Data; //data
stefanrousseau 11:e6602513730f 76 //status = i2c.write(ucDeviceAddress, txbuffer, 2, false); //stop at end
stefanrousseau 11:e6602513730f 77 status = i2c.write(ucDeviceAddress, txbuffer, 2, !bSendStop); //true: do not send stop
stefanrousseau 4:f83bedd9cab4 78 return status;
stefanrousseau 4:f83bedd9cab4 79 } //I2C_WriteSingleByteToAddr()
stefanrousseau 4:f83bedd9cab4 80
stefanrousseau 4:f83bedd9cab4 81 /*------------------------------------------------------------------------------
stefanrousseau 4:f83bedd9cab4 82 * Perform I2C write of more than 1 byte.
stefanrousseau 4:f83bedd9cab4 83 *------------------------------------------------------------------------------*/
stefanrousseau 4:f83bedd9cab4 84 int I2C_WriteMultipleBytes(unsigned char ucDeviceAddress, char *ucData, unsigned char ucLength, bool bSendStop)
stefanrousseau 4:f83bedd9cab4 85 {
stefanrousseau 4:f83bedd9cab4 86 int status;
stefanrousseau 11:e6602513730f 87 status = i2c.write(ucDeviceAddress, ucData, ucLength, !bSendStop); //true: do not send stop
stefanrousseau 4:f83bedd9cab4 88 return status;
stefanrousseau 4:f83bedd9cab4 89 } //I2C_WriteMultipleBytes()
stefanrousseau 4:f83bedd9cab4 90
stefanrousseau 4:f83bedd9cab4 91 bool bSi7020_present = false;
stefanrousseau 4:f83bedd9cab4 92 void Init_Si7020(void)
stefanrousseau 4:f83bedd9cab4 93 {
stefanrousseau 4:f83bedd9cab4 94 char SN_7020 [8];
stefanrousseau 4:f83bedd9cab4 95 //SN part 1:
stefanrousseau 4:f83bedd9cab4 96 I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0xFA, 0x0F, false);
stefanrousseau 4:f83bedd9cab4 97 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &SN_7020[0], 4);
stefanrousseau 4:f83bedd9cab4 98
stefanrousseau 4:f83bedd9cab4 99 //SN part 1:
stefanrousseau 4:f83bedd9cab4 100 I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0xFC, 0xC9, false);
stefanrousseau 4:f83bedd9cab4 101 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &SN_7020[4], 4);
stefanrousseau 4:f83bedd9cab4 102
stefanrousseau 4:f83bedd9cab4 103 char Ver_7020 [2];
stefanrousseau 4:f83bedd9cab4 104 //FW version:
stefanrousseau 4:f83bedd9cab4 105 I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0x84, 0xB8, false);
stefanrousseau 4:f83bedd9cab4 106 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Ver_7020[0], 2);
stefanrousseau 4:f83bedd9cab4 107
stefanrousseau 4:f83bedd9cab4 108 if (SN_7020[4] != 0x14)
stefanrousseau 4:f83bedd9cab4 109 {
stefanrousseau 4:f83bedd9cab4 110 bSi7020_present = false;
stefanrousseau 4:f83bedd9cab4 111 printf("Si7020 sensor not found\n");
stefanrousseau 4:f83bedd9cab4 112 }
stefanrousseau 4:f83bedd9cab4 113 else
stefanrousseau 4:f83bedd9cab4 114 {
stefanrousseau 4:f83bedd9cab4 115 bSi7020_present = true;
stefanrousseau 4:f83bedd9cab4 116 printf("Si7020 SN = 0x%02X%02X%02X%02X%02X%02X%02X%02X\n", SN_7020[0], SN_7020[1], SN_7020[2], SN_7020[3], SN_7020[4], SN_7020[5], SN_7020[6], SN_7020[7]);
stefanrousseau 4:f83bedd9cab4 117 printf("Si7020 Version# = 0x%02X\n", Ver_7020[0]);
stefanrousseau 4:f83bedd9cab4 118 } //bool bSi7020_present = true
stefanrousseau 4:f83bedd9cab4 119
stefanrousseau 4:f83bedd9cab4 120 } //Init_Si7020()
stefanrousseau 4:f83bedd9cab4 121
stefanrousseau 4:f83bedd9cab4 122 void Read_Si7020(void)
stefanrousseau 4:f83bedd9cab4 123 {
stefanrousseau 4:f83bedd9cab4 124 if (bSi7020_present)
stefanrousseau 4:f83bedd9cab4 125 {
stefanrousseau 4:f83bedd9cab4 126 char Humidity [2];
stefanrousseau 4:f83bedd9cab4 127 char Temperature [2];
stefanrousseau 4:f83bedd9cab4 128 //Command to measure humidity (temperature also gets measured):
stefanrousseau 4:f83bedd9cab4 129 I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xF5, false); //no hold, must do dummy read
stefanrousseau 4:f83bedd9cab4 130 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Humidity[0], 1); //dummy read, should get an nack until it is done
stefanrousseau 4:f83bedd9cab4 131 wait (0.05); //wait for measurement. Can also keep reading until no NACK is received
stefanrousseau 4:f83bedd9cab4 132 //I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xE5, false); //Hold mod, the device does a clock stretch on the read until it is done (crashes the I2C bus...
stefanrousseau 4:f83bedd9cab4 133 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Humidity[0], 2); //read humidity
stefanrousseau 4:f83bedd9cab4 134 //printf("Read Si7020 Humidity = 0x%02X%02X\n", Humidity[0], Humidity[1]);
stefanrousseau 4:f83bedd9cab4 135 int rh_code = (Humidity[0] << 8) + Humidity[1];
stefanrousseau 4:f83bedd9cab4 136 float fRh = (125.0*rh_code/65536.0) - 6.0; //from datasheet
stefanrousseau 4:f83bedd9cab4 137 //printf("Si7020 Humidity = %*.*f %%\n", 4, 2, fRh); //double % sign for escape //printf("%*.*f\n", myFieldWidth, myPrecision, myFloatValue);
stefanrousseau 4:f83bedd9cab4 138 sprintf(SENSOR_DATA.Humidity_Si7020, "%0.2f", fRh);
stefanrousseau 4:f83bedd9cab4 139
stefanrousseau 4:f83bedd9cab4 140 //Command to read temperature when humidity is already done:
stefanrousseau 4:f83bedd9cab4 141 I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xE0, false);
stefanrousseau 4:f83bedd9cab4 142 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Temperature[0], 2); //read temperature
stefanrousseau 4:f83bedd9cab4 143 //printf("Read Si7020 Temperature = 0x%02X%02X\n", Temperature[0], Temperature[1]);
stefanrousseau 4:f83bedd9cab4 144 int temp_code = (Temperature[0] << 8) + Temperature[1];
stefanrousseau 4:f83bedd9cab4 145 float fTemp = (175.72*temp_code/65536.0) - 46.85; //from datasheet in Celcius
stefanrousseau 4:f83bedd9cab4 146 //printf("Si7020 Temperature = %*.*f deg C\n", 4, 2, fTemp);
stefanrousseau 4:f83bedd9cab4 147 sprintf(SENSOR_DATA.Temperature_Si7020, "%0.2f", fTemp);
stefanrousseau 4:f83bedd9cab4 148 } //bool bSi7020_present = true
stefanrousseau 4:f83bedd9cab4 149
stefanrousseau 4:f83bedd9cab4 150 } //Read_Si7020()
stefanrousseau 4:f83bedd9cab4 151
stefanrousseau 4:f83bedd9cab4 152 /*------------------------------------------------------------------------------
stefanrousseau 4:f83bedd9cab4 153 * The following are aliases so that the Si1145 coding examples can be used as-is.
stefanrousseau 4:f83bedd9cab4 154 *------------------------------------------------------------------------------*/
stefanrousseau 4:f83bedd9cab4 155 unsigned char ReadFrom_Si1145_Register(unsigned char reg) //returns byte from I2C Register 'reg'
stefanrousseau 4:f83bedd9cab4 156 {
stefanrousseau 4:f83bedd9cab4 157 unsigned char result = I2C_ReadSingleByteFromAddr(Si1145_PMOD_I2C_ADDR, reg);
stefanrousseau 4:f83bedd9cab4 158 return (result);
stefanrousseau 4:f83bedd9cab4 159 } //ReadFrom_Si1145_Register()
stefanrousseau 4:f83bedd9cab4 160
stefanrousseau 4:f83bedd9cab4 161 void WriteTo_Si1145_Register(unsigned char reg, unsigned char value) //writes 'value' into I2C Register reg'
stefanrousseau 4:f83bedd9cab4 162 {
stefanrousseau 4:f83bedd9cab4 163 I2C_WriteSingleByteToAddr(Si1145_PMOD_I2C_ADDR, reg, value, true);
stefanrousseau 4:f83bedd9cab4 164 } //WriteTo_Si1145_Register()
stefanrousseau 4:f83bedd9cab4 165
stefanrousseau 4:f83bedd9cab4 166 #define REG_PARAM_WR 0x17
stefanrousseau 4:f83bedd9cab4 167 #define REG_PARAM_RD 0x2E
stefanrousseau 4:f83bedd9cab4 168 #define REG_COMMAND 0x18
stefanrousseau 4:f83bedd9cab4 169 #define REG_RESPONSE 0x20
stefanrousseau 4:f83bedd9cab4 170 #define REG_HW_KEY 0x07
stefanrousseau 4:f83bedd9cab4 171 #define HW_KEY_VAL0 0x17
stefanrousseau 4:f83bedd9cab4 172 #define REG_MEAS_RATE_LSB 0x08
stefanrousseau 4:f83bedd9cab4 173 #define REG_MEAS_RATE_MSB 0x09
stefanrousseau 4:f83bedd9cab4 174 #define REG_PS_LED21 0x0F
stefanrousseau 4:f83bedd9cab4 175 #define REG_PS_LED3 0x10
stefanrousseau 4:f83bedd9cab4 176 #define MAX_LED_CURRENT 0xF
stefanrousseau 4:f83bedd9cab4 177 #define PARAM_CH_LIST 0x01
stefanrousseau 4:f83bedd9cab4 178 #define REG_ALS_VIS_DATA0 0x22
stefanrousseau 4:f83bedd9cab4 179 #define REG_ALS_VIS_DATA1 0x23
stefanrousseau 4:f83bedd9cab4 180 #define REG_ALS_IR_DATA0 0x24
stefanrousseau 4:f83bedd9cab4 181 #define REG_ALS_IR_DATA1 0x25
stefanrousseau 4:f83bedd9cab4 182 #define REG_PS1_DATA0 0x26
stefanrousseau 4:f83bedd9cab4 183 #define REG_PS1_DATA1 0x27
stefanrousseau 4:f83bedd9cab4 184 #define REG_PS2_DATA0 0x28
stefanrousseau 4:f83bedd9cab4 185 #define REG_PS2_DATA1 0x29
stefanrousseau 4:f83bedd9cab4 186 #define REG_PS3_DATA0 0x2A
stefanrousseau 4:f83bedd9cab4 187 #define REG_PS3_DATA1 0x2B
stefanrousseau 4:f83bedd9cab4 188 #define REG_UVINDEX0 0x2C
stefanrousseau 4:f83bedd9cab4 189 #define REG_UVINDEX1 0x2D
stefanrousseau 4:f83bedd9cab4 190 int Si1145_ParamSet(unsigned char address, unsigned char value) //writes 'value' into Parameter 'address'
stefanrousseau 4:f83bedd9cab4 191 {
stefanrousseau 4:f83bedd9cab4 192 char txbuffer [3];
stefanrousseau 4:f83bedd9cab4 193 txbuffer[0] = (char)REG_PARAM_WR; //destination
stefanrousseau 4:f83bedd9cab4 194 txbuffer[1] = (char)value;
stefanrousseau 4:f83bedd9cab4 195 txbuffer[2] = (char)(0xA0 + (address & 0x1F));
stefanrousseau 4:f83bedd9cab4 196 int retval;
stefanrousseau 4:f83bedd9cab4 197 //if((retval = _waitUntilSleep(si114x_handle))!=0) return retval;
stefanrousseau 4:f83bedd9cab4 198 retval = I2C_WriteMultipleBytes(Si1145_PMOD_I2C_ADDR, &txbuffer[0], 3, true);
stefanrousseau 4:f83bedd9cab4 199 if(retval!=0) return retval;
stefanrousseau 4:f83bedd9cab4 200 while(1)
stefanrousseau 4:f83bedd9cab4 201 {
stefanrousseau 4:f83bedd9cab4 202 retval=ReadFrom_Si1145_Register(REG_PARAM_RD);
stefanrousseau 4:f83bedd9cab4 203 if (retval==value) break;
stefanrousseau 4:f83bedd9cab4 204 }
stefanrousseau 4:f83bedd9cab4 205 return (0);
stefanrousseau 4:f83bedd9cab4 206 } //Si1145_ParamSet()
stefanrousseau 4:f83bedd9cab4 207
stefanrousseau 4:f83bedd9cab4 208 void PsAlsForce(void) //equivalent to WriteTo_Si1145_Register(REG_COMMAND,0x07). This forces PS and ALS measurements
stefanrousseau 4:f83bedd9cab4 209 {
stefanrousseau 4:f83bedd9cab4 210 WriteTo_Si1145_Register(REG_COMMAND,0x07);
stefanrousseau 4:f83bedd9cab4 211 } //PsAlsForce()
stefanrousseau 4:f83bedd9cab4 212
stefanrousseau 4:f83bedd9cab4 213 bool bSi1145_present = false;
stefanrousseau 4:f83bedd9cab4 214 void Init_Si1145(void)
stefanrousseau 4:f83bedd9cab4 215 {
stefanrousseau 4:f83bedd9cab4 216 unsigned char readbyte;
stefanrousseau 4:f83bedd9cab4 217 //Read Si1145 part ID:
stefanrousseau 4:f83bedd9cab4 218 readbyte = ReadFrom_Si1145_Register(0x00);
stefanrousseau 4:f83bedd9cab4 219 if (readbyte != 0x45)
stefanrousseau 4:f83bedd9cab4 220 {
stefanrousseau 4:f83bedd9cab4 221 bSi1145_present = false;
stefanrousseau 4:f83bedd9cab4 222 printf("Si1145 sensor not found\n");
stefanrousseau 4:f83bedd9cab4 223 }
stefanrousseau 4:f83bedd9cab4 224 else
stefanrousseau 4:f83bedd9cab4 225 {
stefanrousseau 4:f83bedd9cab4 226 bSi1145_present = true;
stefanrousseau 4:f83bedd9cab4 227 printf("Si1145 Part ID : 0x%02X\n", readbyte);
stefanrousseau 4:f83bedd9cab4 228 //Initialize Si1145 by writing to HW_KEY (I2C Register 0x07 = 0x17)
stefanrousseau 4:f83bedd9cab4 229 WriteTo_Si1145_Register(REG_HW_KEY, HW_KEY_VAL0);
stefanrousseau 4:f83bedd9cab4 230
stefanrousseau 4:f83bedd9cab4 231 // Initialize LED Current
stefanrousseau 4:f83bedd9cab4 232 // I2C Register 0x0F = 0xFF
stefanrousseau 4:f83bedd9cab4 233 // I2C Register 0x10 = 0x0F
stefanrousseau 4:f83bedd9cab4 234 WriteTo_Si1145_Register(REG_PS_LED21,(MAX_LED_CURRENT<<4) + MAX_LED_CURRENT);
stefanrousseau 4:f83bedd9cab4 235 WriteTo_Si1145_Register(REG_PS_LED3, MAX_LED_CURRENT);
stefanrousseau 4:f83bedd9cab4 236
stefanrousseau 4:f83bedd9cab4 237 // Parameter 0x01 = 0x37
stefanrousseau 4:f83bedd9cab4 238 //Si1145_ParamSet(PARAM_CH_LIST, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK);
stefanrousseau 4:f83bedd9cab4 239 //Si1145_ParamSet(0x01, 0x37); //CHLIST is address 0x01 in the parameter RAM. It defines which sensors are enabled (here, some)
stefanrousseau 4:f83bedd9cab4 240 Si1145_ParamSet(0x01, 0x7F); //CHLIST is address 0x01 in the parameter RAM. It defines which sensors are enabled (here, all but UV. One can only use AUX or UV but here we use AUX because UV does not work...)
stefanrousseau 4:f83bedd9cab4 241 // I2C Register 0x18 = 0x0x07 //This is PSALS_FORCE to the Command register => Force a single PS (proximity sensor) and ALS (ambient light sensor) reading - The factory code has this as 0x05 which only does PS...
stefanrousseau 4:f83bedd9cab4 242 PsAlsForce(); // can also be written as WriteTo_Si1145_Register(REG_COMMAND,0x07);
stefanrousseau 4:f83bedd9cab4 243 WriteTo_Si1145_Register(REG_COMMAND, 0x0F);//command to put it into auto mode
stefanrousseau 4:f83bedd9cab4 244 //Set MES_RATE to 0x1000. I.e. the device will automatically wake up every 16 * 256* 31.25 us = 0.128 seconds to measure
stefanrousseau 4:f83bedd9cab4 245 WriteTo_Si1145_Register(REG_MEAS_RATE_LSB, 0x00);
stefanrousseau 4:f83bedd9cab4 246 WriteTo_Si1145_Register(REG_MEAS_RATE_MSB, 0x10);
stefanrousseau 4:f83bedd9cab4 247 } //bSi1145_present = true
stefanrousseau 4:f83bedd9cab4 248 } //Init_Si1145()
stefanrousseau 4:f83bedd9cab4 249
stefanrousseau 4:f83bedd9cab4 250 void Read_Si1145(void)
stefanrousseau 4:f83bedd9cab4 251 {
stefanrousseau 4:f83bedd9cab4 252 if (bSi1145_present)
stefanrousseau 4:f83bedd9cab4 253 {
stefanrousseau 4:f83bedd9cab4 254 // Once the measurements are completed, here is how to reconstruct them
stefanrousseau 4:f83bedd9cab4 255 // Note very carefully that 16-bit registers are in the 'Little Endian' byte order
stefanrousseau 4:f83bedd9cab4 256 // It may be more efficient to perform block I2C Reads, but this example shows
stefanrousseau 4:f83bedd9cab4 257 // individual reads of registers
stefanrousseau 4:f83bedd9cab4 258
stefanrousseau 4:f83bedd9cab4 259 int PS1 = ReadFrom_Si1145_Register(REG_PS1_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS1_DATA1);
stefanrousseau 4:f83bedd9cab4 260 int PS2 = ReadFrom_Si1145_Register(REG_PS2_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS2_DATA1);
stefanrousseau 4:f83bedd9cab4 261 int PS3 = ReadFrom_Si1145_Register(REG_PS3_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS3_DATA1);
stefanrousseau 4:f83bedd9cab4 262 //printf("PS1_Data = %d\n", PS1);
stefanrousseau 4:f83bedd9cab4 263 //printf("PS2_Data = %d\n", PS2);
stefanrousseau 4:f83bedd9cab4 264 //printf("PS3_Data = %d\n", PS3);
stefanrousseau 4:f83bedd9cab4 265 //OBJECT PRESENT?
stefanrousseau 29:e6c8bd41caa6 266 #if (0)
stefanrousseau 4:f83bedd9cab4 267 if(PS1 < 22000){
stefanrousseau 4:f83bedd9cab4 268 //printf("Object Far\n");
stefanrousseau 29:e6c8bd41caa6 269 sprintf(SENSOR_DATA.Proximity, "Object Far\0");
stefanrousseau 4:f83bedd9cab4 270 }
stefanrousseau 4:f83bedd9cab4 271 else if(PS1 < 24000)
stefanrousseau 4:f83bedd9cab4 272 {
stefanrousseau 4:f83bedd9cab4 273 //printf("Object in Vicinity\n");
stefanrousseau 29:e6c8bd41caa6 274 sprintf(SENSOR_DATA.Proximity, "Object in Vicinity\0");
stefanrousseau 4:f83bedd9cab4 275 }
stefanrousseau 4:f83bedd9cab4 276 else if (PS1 < 30000)
stefanrousseau 4:f83bedd9cab4 277 {
stefanrousseau 4:f83bedd9cab4 278 //printf("Object Near\n");
stefanrousseau 29:e6c8bd41caa6 279 sprintf(SENSOR_DATA.Proximity, "Object Near\0");
stefanrousseau 4:f83bedd9cab4 280 }
stefanrousseau 4:f83bedd9cab4 281 else
stefanrousseau 4:f83bedd9cab4 282 {
stefanrousseau 4:f83bedd9cab4 283 //printf("Object Very Near\n");
stefanrousseau 29:e6c8bd41caa6 284 sprintf(SENSOR_DATA.Proximity, "Object Very Near\0");
stefanrousseau 4:f83bedd9cab4 285 }
stefanrousseau 29:e6c8bd41caa6 286 #else
stefanrousseau 29:e6c8bd41caa6 287 sprintf(SENSOR_DATA.Proximity, "%d\0", PS1);
stefanrousseau 29:e6c8bd41caa6 288 #endif
stefanrousseau 4:f83bedd9cab4 289
stefanrousseau 4:f83bedd9cab4 290 //Force ALS read:
stefanrousseau 4:f83bedd9cab4 291 //WriteTo_Si1145_Register(REG_COMMAND, 0x06);
stefanrousseau 4:f83bedd9cab4 292 //wait (0.1);
stefanrousseau 4:f83bedd9cab4 293 int ALS_VIS = ReadFrom_Si1145_Register(REG_ALS_VIS_DATA0) + 256 * ReadFrom_Si1145_Register(REG_ALS_VIS_DATA1);
stefanrousseau 4:f83bedd9cab4 294 int ALS_IR = ReadFrom_Si1145_Register(REG_ALS_IR_DATA0) + 256 * ReadFrom_Si1145_Register(REG_ALS_IR_DATA1);
stefanrousseau 4:f83bedd9cab4 295 int UV_INDEX = ReadFrom_Si1145_Register(REG_UVINDEX0) + 256 * ReadFrom_Si1145_Register(REG_UVINDEX1);
stefanrousseau 4:f83bedd9cab4 296 //printf("ALS_VIS_Data = %d\n", ALS_VIS);
stefanrousseau 4:f83bedd9cab4 297 //printf("ALS_IR_Data = %d\n", ALS_IR);
stefanrousseau 4:f83bedd9cab4 298 //printf("UV_INDEX_Data = %d\n", UV_INDEX);
stefanrousseau 4:f83bedd9cab4 299
stefanrousseau 4:f83bedd9cab4 300 //printf("Ambient Light Visible Sensor = %d\n", ALS_VIS);
stefanrousseau 4:f83bedd9cab4 301 sprintf(SENSOR_DATA.AmbientLightVis, "%d", ALS_VIS);
stefanrousseau 4:f83bedd9cab4 302 //printf("Ambient Light Infrared Sensor = %d\n", ALS_IR);
stefanrousseau 4:f83bedd9cab4 303 sprintf(SENSOR_DATA.AmbientLightIr, "%d", ALS_IR);
stefanrousseau 4:f83bedd9cab4 304 //float fUV_value = (UV_INDEX -50.0)/10000.0;
stefanrousseau 4:f83bedd9cab4 305 float fUV_value = (UV_INDEX)/100.0; //this is the aux reading
stefanrousseau 4:f83bedd9cab4 306 //printf("UV_Data = %0.2f\n", fUV_value);
stefanrousseau 4:f83bedd9cab4 307 sprintf(SENSOR_DATA.UVindex, "%0.2f", fUV_value);
stefanrousseau 4:f83bedd9cab4 308 } //bSi1145_present = true
stefanrousseau 4:f83bedd9cab4 309 } //Read_Si1145()
stefanrousseau 4:f83bedd9cab4 310
stefanrousseau 4:f83bedd9cab4 311 //********************************************************************************************************************************************
stefanrousseau 4:f83bedd9cab4 312 //* Read the FXOS8700CQ - 6-axis combo Sensor Accelerometer and Magnetometer
stefanrousseau 4:f83bedd9cab4 313 //********************************************************************************************************************************************
stefanrousseau 11:e6602513730f 314 bool bMotionSensor_present = false;
stefanrousseau 4:f83bedd9cab4 315 void init_motion_sensor()
stefanrousseau 4:f83bedd9cab4 316 {
stefanrousseau 30:48b43538e98b 317 // Note: this class is instantiated here because if it is statically declared, the cellular shield init kills the I2C bus...
stefanrousseau 30:48b43538e98b 318 // Class instantiation with pin names for the motion sensor on the FRDM-K64F board:
stefanrousseau 30:48b43538e98b 319 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
stefanrousseau 11:e6602513730f 320 int iWhoAmI = fxos.get_whoami();
stefanrousseau 11:e6602513730f 321 printf("FXOS8700CQ WhoAmI = %X\r\n", iWhoAmI);
stefanrousseau 4:f83bedd9cab4 322 // Iterrupt for active-low interrupt line from FXOS
stefanrousseau 4:f83bedd9cab4 323 // Configured with only one interrupt on INT2 signaling Data-Ready
stefanrousseau 4:f83bedd9cab4 324 //fxos_int2.fall(&trigger_fxos_int2);
stefanrousseau 11:e6602513730f 325 if (iWhoAmI != 0xC7)
stefanrousseau 11:e6602513730f 326 {
stefanrousseau 11:e6602513730f 327 bMotionSensor_present = false;
stefanrousseau 11:e6602513730f 328 printf("FXOS8700CQ motion sensor not found\n");
stefanrousseau 11:e6602513730f 329 }
stefanrousseau 11:e6602513730f 330 else
stefanrousseau 11:e6602513730f 331 {
stefanrousseau 11:e6602513730f 332 bMotionSensor_present = true;
stefanrousseau 11:e6602513730f 333 fxos.enable();
stefanrousseau 11:e6602513730f 334 }
stefanrousseau 4:f83bedd9cab4 335 } //init_motion_sensor
stefanrousseau 4:f83bedd9cab4 336
stefanrousseau 11:e6602513730f 337 void read_motion_sensor()
stefanrousseau 11:e6602513730f 338 {
stefanrousseau 30:48b43538e98b 339 // Note: this class is instantiated here because if it is statically declared, the cellular shield init kills the I2C bus...
stefanrousseau 30:48b43538e98b 340 // Class instantiation with pin names for the motion sensor on the FRDM-K64F board:
stefanrousseau 30:48b43538e98b 341 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
stefanrousseau 11:e6602513730f 342 if (bMotionSensor_present)
stefanrousseau 11:e6602513730f 343 {
stefanrousseau 30:48b43538e98b 344 fxos.enable();
stefanrousseau 11:e6602513730f 345 fxos.get_data(&accel_data, &magn_data);
stefanrousseau 11:e6602513730f 346 //printf("Roll=%5d, Pitch=%5d, Yaw=%5d;\r\n", magn_data.x, magn_data.y, magn_data.z);
stefanrousseau 11:e6602513730f 347 sprintf(SENSOR_DATA.MagnetometerX, "%5d", magn_data.x);
stefanrousseau 11:e6602513730f 348 sprintf(SENSOR_DATA.MagnetometerY, "%5d", magn_data.y);
stefanrousseau 11:e6602513730f 349 sprintf(SENSOR_DATA.MagnetometerZ, "%5d", magn_data.z);
stefanrousseau 11:e6602513730f 350
stefanrousseau 11:e6602513730f 351 //Try to normalize (/2048) the values so they will match the eCompass output:
stefanrousseau 11:e6602513730f 352 float fAccelScaled_x, fAccelScaled_y, fAccelScaled_z;
stefanrousseau 11:e6602513730f 353 fAccelScaled_x = (accel_data.x/2048.0);
stefanrousseau 11:e6602513730f 354 fAccelScaled_y = (accel_data.y/2048.0);
stefanrousseau 11:e6602513730f 355 fAccelScaled_z = (accel_data.z/2048.0);
stefanrousseau 11:e6602513730f 356 //printf("Acc: X=%2.3f Y=%2.3f Z=%2.3f;\r\n", fAccelScaled_x, fAccelScaled_y, fAccelScaled_z);
stefanrousseau 11:e6602513730f 357 sprintf(SENSOR_DATA.AccelX, "%2.3f", fAccelScaled_x);
stefanrousseau 11:e6602513730f 358 sprintf(SENSOR_DATA.AccelY, "%2.3f", fAccelScaled_y);
stefanrousseau 11:e6602513730f 359 sprintf(SENSOR_DATA.AccelZ, "%2.3f", fAccelScaled_z);
stefanrousseau 11:e6602513730f 360 } //bMotionSensor_present
stefanrousseau 11:e6602513730f 361 } //read_motion_sensor
stefanrousseau 11:e6602513730f 362
stefanrousseau 4:f83bedd9cab4 363 void sensors_init(void)
stefanrousseau 4:f83bedd9cab4 364 {
stefanrousseau 4:f83bedd9cab4 365 Init_Si7020();
stefanrousseau 4:f83bedd9cab4 366 Init_Si1145();
stefanrousseau 4:f83bedd9cab4 367 init_motion_sensor();
stefanrousseau 4:f83bedd9cab4 368 } //sensors_init
stefanrousseau 4:f83bedd9cab4 369
stefanrousseau 4:f83bedd9cab4 370 void read_sensors(void)
stefanrousseau 4:f83bedd9cab4 371 {
stefanrousseau 4:f83bedd9cab4 372 Read_Si7020();
stefanrousseau 4:f83bedd9cab4 373 Read_Si1145();
stefanrousseau 4:f83bedd9cab4 374 read_motion_sensor();
stefanrousseau 4:f83bedd9cab4 375 } //read_sensors