Optical Sensor for Heart Rate Monitor IC

Committer:
mcm
Date:
Thu Sep 16 14:25:08 2021 +0000
Revision:
4:55075fea807c
Parent:
2:eb95a49c8a29
The data must be shifted! This bug was fixed for BH1790GLC_GetRawDataOut function.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mcm 2:eb95a49c8a29 1 /**
mcm 2:eb95a49c8a29 2 * @brief BH1790GLC.cpp
mcm 2:eb95a49c8a29 3 * @details Optical Sensor for Heart Rate Monitor IC.
mcm 2:eb95a49c8a29 4 * Function file.
mcm 2:eb95a49c8a29 5 *
mcm 2:eb95a49c8a29 6 *
mcm 2:eb95a49c8a29 7 * @return N/A
mcm 2:eb95a49c8a29 8 *
mcm 2:eb95a49c8a29 9 * @author Manuel Caballero
mcm 2:eb95a49c8a29 10 * @date 07/December/2019
mcm 2:eb95a49c8a29 11 * @version 07/December/2019 The ORIGIN
mcm 2:eb95a49c8a29 12 * @pre N/A.
mcm 2:eb95a49c8a29 13 * @warning N/A
mcm 2:eb95a49c8a29 14 * @pre This code belongs to AqueronteBlog ( http://unbarquero.blogspot.com ).
mcm 2:eb95a49c8a29 15 */
mcm 2:eb95a49c8a29 16
mcm 2:eb95a49c8a29 17 #include "BH1790GLC.h"
mcm 2:eb95a49c8a29 18
mcm 2:eb95a49c8a29 19
mcm 2:eb95a49c8a29 20 BH1790GLC::BH1790GLC ( PinName sda, PinName scl, uint32_t addr, uint32_t freq )
mcm 2:eb95a49c8a29 21 : _i2c ( sda, scl )
mcm 2:eb95a49c8a29 22 , _BH1790GLC_Addr ( addr )
mcm 2:eb95a49c8a29 23 {
mcm 2:eb95a49c8a29 24 _i2c.frequency( freq );
mcm 2:eb95a49c8a29 25 }
mcm 2:eb95a49c8a29 26
mcm 2:eb95a49c8a29 27
mcm 2:eb95a49c8a29 28 BH1790GLC::~BH1790GLC()
mcm 2:eb95a49c8a29 29 {
mcm 2:eb95a49c8a29 30 }
mcm 2:eb95a49c8a29 31
mcm 2:eb95a49c8a29 32
mcm 2:eb95a49c8a29 33 /**
mcm 2:eb95a49c8a29 34 * @brief BH1790GLC_GetManufacturerID ( BH1790GLC_data_t* )
mcm 2:eb95a49c8a29 35 *
mcm 2:eb95a49c8a29 36 * @details It gets the manufacturer ID.
mcm 2:eb95a49c8a29 37 *
mcm 2:eb95a49c8a29 38 * @param[in] N/A.
mcm 2:eb95a49c8a29 39 *
mcm 2:eb95a49c8a29 40 * @param[out] myManufacturerID: Manufacturer ID.
mcm 2:eb95a49c8a29 41 *
mcm 2:eb95a49c8a29 42 *
mcm 2:eb95a49c8a29 43 * @return Status of BH1790GLC_GetManufacturerID.
mcm 2:eb95a49c8a29 44 *
mcm 2:eb95a49c8a29 45 *
mcm 2:eb95a49c8a29 46 * @author Manuel Caballero
mcm 2:eb95a49c8a29 47 * @date 07/December/2019
mcm 2:eb95a49c8a29 48 * @version 07/December/2019 The ORIGIN
mcm 2:eb95a49c8a29 49 * @pre N/A
mcm 2:eb95a49c8a29 50 * @warning N/A.
mcm 2:eb95a49c8a29 51 */
mcm 2:eb95a49c8a29 52 BH1790GLC::BH1790GLC_status_t BH1790GLC::BH1790GLC_GetManufacturerID ( BH1790GLC_data_t* myManufacturerID )
mcm 2:eb95a49c8a29 53 {
mcm 2:eb95a49c8a29 54 char cmd = 0;
mcm 2:eb95a49c8a29 55 uint32_t aux;
mcm 2:eb95a49c8a29 56
mcm 2:eb95a49c8a29 57 /* Read the register */
mcm 2:eb95a49c8a29 58 cmd = BH1790GLC_MANUFACTURER_ID;
mcm 2:eb95a49c8a29 59 aux = _i2c.write ( _BH1790GLC_Addr, &cmd, 1U, true );
mcm 2:eb95a49c8a29 60 aux = _i2c.read ( _BH1790GLC_Addr, &cmd, 1U );
mcm 2:eb95a49c8a29 61
mcm 2:eb95a49c8a29 62
mcm 2:eb95a49c8a29 63 /* Parse data */
mcm 2:eb95a49c8a29 64 myManufacturerID->manufacturer_id = cmd;
mcm 2:eb95a49c8a29 65
mcm 2:eb95a49c8a29 66
mcm 2:eb95a49c8a29 67
mcm 2:eb95a49c8a29 68 if ( aux == I2C_SUCCESS ) {
mcm 2:eb95a49c8a29 69 return BH1790GLC_SUCCESS;
mcm 2:eb95a49c8a29 70 } else {
mcm 2:eb95a49c8a29 71 return BH1790GLC_FAILURE;
mcm 2:eb95a49c8a29 72 }
mcm 2:eb95a49c8a29 73 }
mcm 2:eb95a49c8a29 74
mcm 2:eb95a49c8a29 75
mcm 2:eb95a49c8a29 76
mcm 2:eb95a49c8a29 77 /**
mcm 2:eb95a49c8a29 78 * @brief BH1790GLC_GetPartID ( BH1790GLC_data_t* )
mcm 2:eb95a49c8a29 79 *
mcm 2:eb95a49c8a29 80 * @details It gets the part ID.
mcm 2:eb95a49c8a29 81 *
mcm 2:eb95a49c8a29 82 * @param[in] N/A.
mcm 2:eb95a49c8a29 83 *
mcm 2:eb95a49c8a29 84 * @param[out] myPartID: Part ID.
mcm 2:eb95a49c8a29 85 *
mcm 2:eb95a49c8a29 86 *
mcm 2:eb95a49c8a29 87 * @return Status of BH1790GLC_GetPartID.
mcm 2:eb95a49c8a29 88 *
mcm 2:eb95a49c8a29 89 *
mcm 2:eb95a49c8a29 90 * @author Manuel Caballero
mcm 2:eb95a49c8a29 91 * @date 07/December/2019
mcm 2:eb95a49c8a29 92 * @version 07/December/2019 The ORIGIN
mcm 2:eb95a49c8a29 93 * @pre N/A
mcm 2:eb95a49c8a29 94 * @warning N/A.
mcm 2:eb95a49c8a29 95 */
mcm 2:eb95a49c8a29 96 BH1790GLC::BH1790GLC_status_t BH1790GLC::BH1790GLC_GetPartID ( BH1790GLC_data_t* myPartID )
mcm 2:eb95a49c8a29 97 {
mcm 2:eb95a49c8a29 98 char cmd = 0;
mcm 2:eb95a49c8a29 99 uint32_t aux;
mcm 2:eb95a49c8a29 100
mcm 2:eb95a49c8a29 101 /* Read the register */
mcm 2:eb95a49c8a29 102 cmd = BH1790GLC_PART_ID;
mcm 2:eb95a49c8a29 103 aux = _i2c.write ( _BH1790GLC_Addr, &cmd, 1U, true );
mcm 2:eb95a49c8a29 104 aux = _i2c.read ( _BH1790GLC_Addr, &cmd, 1U );
mcm 2:eb95a49c8a29 105
mcm 2:eb95a49c8a29 106
mcm 2:eb95a49c8a29 107 /* Parse data */
mcm 2:eb95a49c8a29 108 myPartID->part_id = cmd;
mcm 2:eb95a49c8a29 109
mcm 2:eb95a49c8a29 110
mcm 2:eb95a49c8a29 111
mcm 2:eb95a49c8a29 112 if ( aux == I2C_SUCCESS ) {
mcm 2:eb95a49c8a29 113 return BH1790GLC_SUCCESS;
mcm 2:eb95a49c8a29 114 } else {
mcm 2:eb95a49c8a29 115 return BH1790GLC_FAILURE;
mcm 2:eb95a49c8a29 116 }
mcm 2:eb95a49c8a29 117 }
mcm 2:eb95a49c8a29 118
mcm 2:eb95a49c8a29 119
mcm 2:eb95a49c8a29 120
mcm 2:eb95a49c8a29 121 /**
mcm 2:eb95a49c8a29 122 * @brief BH1790GLC_SoftReset ( void )
mcm 2:eb95a49c8a29 123 *
mcm 2:eb95a49c8a29 124 * @details It performs a software reset.
mcm 2:eb95a49c8a29 125 *
mcm 2:eb95a49c8a29 126 * @param[in] N/A.
mcm 2:eb95a49c8a29 127 *
mcm 2:eb95a49c8a29 128 * @param[out] N/A.
mcm 2:eb95a49c8a29 129 *
mcm 2:eb95a49c8a29 130 *
mcm 2:eb95a49c8a29 131 * @return Status of BH1790GLC_SoftReset.
mcm 2:eb95a49c8a29 132 *
mcm 2:eb95a49c8a29 133 *
mcm 2:eb95a49c8a29 134 * @author Manuel Caballero
mcm 2:eb95a49c8a29 135 * @date 07/December/2019
mcm 2:eb95a49c8a29 136 * @version 07/December/2019 The ORIGIN
mcm 2:eb95a49c8a29 137 * @pre N/A
mcm 2:eb95a49c8a29 138 * @warning N/A.
mcm 2:eb95a49c8a29 139 */
mcm 2:eb95a49c8a29 140 BH1790GLC::BH1790GLC_status_t BH1790GLC::BH1790GLC_SoftReset ( void )
mcm 2:eb95a49c8a29 141 {
mcm 2:eb95a49c8a29 142 char cmd[2] = { 0 };
mcm 2:eb95a49c8a29 143 uint32_t aux;
mcm 2:eb95a49c8a29 144
mcm 2:eb95a49c8a29 145 /* Update the register */
mcm 2:eb95a49c8a29 146 cmd[0] = BH1790GLC_RESET;
mcm 2:eb95a49c8a29 147 cmd[1] = RESET_SWRESET_ENABLED;
mcm 2:eb95a49c8a29 148 aux = _i2c.write ( _BH1790GLC_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false );
mcm 2:eb95a49c8a29 149
mcm 2:eb95a49c8a29 150
mcm 2:eb95a49c8a29 151
mcm 2:eb95a49c8a29 152 if ( aux == I2C_SUCCESS ) {
mcm 2:eb95a49c8a29 153 return BH1790GLC_SUCCESS;
mcm 2:eb95a49c8a29 154 } else {
mcm 2:eb95a49c8a29 155 return BH1790GLC_FAILURE;
mcm 2:eb95a49c8a29 156 }
mcm 2:eb95a49c8a29 157 }
mcm 2:eb95a49c8a29 158
mcm 2:eb95a49c8a29 159
mcm 2:eb95a49c8a29 160 /**
mcm 2:eb95a49c8a29 161 * @brief BH1790GLC_StartMeasurement ( BH1790GLC_data_t )
mcm 2:eb95a49c8a29 162 *
mcm 2:eb95a49c8a29 163 * @details It triggers a new measurement sample.
mcm 2:eb95a49c8a29 164 *
mcm 2:eb95a49c8a29 165 * @param[in] myConfData: System control setting and Measurement control setting.
mcm 2:eb95a49c8a29 166 *
mcm 2:eb95a49c8a29 167 * @param[out] N/A.
mcm 2:eb95a49c8a29 168 *
mcm 2:eb95a49c8a29 169 *
mcm 2:eb95a49c8a29 170 * @return Status of BH1790GLC_StartMeasurement.
mcm 2:eb95a49c8a29 171 *
mcm 2:eb95a49c8a29 172 *
mcm 2:eb95a49c8a29 173 * @author Manuel Caballero
mcm 2:eb95a49c8a29 174 * @date 07/December/2019
mcm 2:eb95a49c8a29 175 * @version 07/December/2019 The ORIGIN
mcm 2:eb95a49c8a29 176 * @pre Start measurement by writing MEAS_ST=1 after writing RDY=1. Measurement doesn`t
mcm 2:eb95a49c8a29 177 * restart if writing MEAS_ST=1 after start measurement. When stop measurement, write SWRESET=1
mcm 2:eb95a49c8a29 178 * without writing MEAS_ST=0 (for changing some parameters as well).
mcm 2:eb95a49c8a29 179 * @pre The user MUST RESPECT the measurement time, T_INT = 28ms ( maximum ).
mcm 2:eb95a49c8a29 180 * @pre The function uses auto-increment.
mcm 2:eb95a49c8a29 181 * @warning System control setting and measurement control setting must be initialized in order to guarantee
mcm 2:eb95a49c8a29 182 * the correct behavior of the device.
mcm 2:eb95a49c8a29 183 */
mcm 2:eb95a49c8a29 184 BH1790GLC::BH1790GLC_status_t BH1790GLC::BH1790GLC_StartMeasurement ( BH1790GLC_data_t myConfData )
mcm 2:eb95a49c8a29 185 {
mcm 2:eb95a49c8a29 186 char cmd[4] = { 0 };
mcm 2:eb95a49c8a29 187 uint32_t aux;
mcm 2:eb95a49c8a29 188
mcm 2:eb95a49c8a29 189 /* Write the register */
mcm 2:eb95a49c8a29 190 cmd[0] = BH1790GLC_MEAS_CONTROL1;
mcm 2:eb95a49c8a29 191 cmd[1] = ( myConfData.rdy | myConfData.led_lighting_freq | myConfData.rcycle );
mcm 2:eb95a49c8a29 192 cmd[2] = ( myConfData.led_en | myConfData.led_on_time | myConfData.led_current );
mcm 2:eb95a49c8a29 193 cmd[3] = MEAS_START_MEAS_ST_MEASUREMENT_START;
mcm 2:eb95a49c8a29 194 aux = _i2c.write ( _BH1790GLC_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false );
mcm 2:eb95a49c8a29 195
mcm 2:eb95a49c8a29 196
mcm 2:eb95a49c8a29 197
mcm 2:eb95a49c8a29 198 if ( aux == I2C_SUCCESS ) {
mcm 2:eb95a49c8a29 199 return BH1790GLC_SUCCESS;
mcm 2:eb95a49c8a29 200 } else {
mcm 2:eb95a49c8a29 201 return BH1790GLC_FAILURE;
mcm 2:eb95a49c8a29 202 }
mcm 2:eb95a49c8a29 203 }
mcm 2:eb95a49c8a29 204
mcm 2:eb95a49c8a29 205
mcm 2:eb95a49c8a29 206
mcm 2:eb95a49c8a29 207 /**
mcm 2:eb95a49c8a29 208 * @brief BH1790GLC_GetRawDataOut ( void )
mcm 2:eb95a49c8a29 209 *
mcm 2:eb95a49c8a29 210 * @details It gets the DATAOUT ( DATAOUT_LEDOFF and DATAOUT_LEDON data ). Raw data value.
mcm 2:eb95a49c8a29 211 *
mcm 2:eb95a49c8a29 212 * @param[in] N/A.
mcm 2:eb95a49c8a29 213 *
mcm 2:eb95a49c8a29 214 * @param[out] myRawDataOut: Raw data for DATAOUT_LEDOFF and DATAOUT_LEDON.
mcm 2:eb95a49c8a29 215 *
mcm 2:eb95a49c8a29 216 *
mcm 2:eb95a49c8a29 217 * @return Status of BH1790GLC_GetRawDataOut.
mcm 2:eb95a49c8a29 218 *
mcm 2:eb95a49c8a29 219 *
mcm 2:eb95a49c8a29 220 * @author Manuel Caballero
mcm 2:eb95a49c8a29 221 * @date 07/December/2019
mcm 2:eb95a49c8a29 222 * @version 07/December/2019 The ORIGIN
mcm 2:eb95a49c8a29 223 * @pre This function uses auto-increment.
mcm 2:eb95a49c8a29 224 * @warning N/A.
mcm 2:eb95a49c8a29 225 */
mcm 2:eb95a49c8a29 226 BH1790GLC::BH1790GLC_status_t BH1790GLC::BH1790GLC_GetRawDataOut ( BH1790GLC_data_t* myRawDataOut )
mcm 2:eb95a49c8a29 227 {
mcm 2:eb95a49c8a29 228 char cmd[4] = { 0 };
mcm 2:eb95a49c8a29 229 uint32_t aux;
mcm 2:eb95a49c8a29 230
mcm 2:eb95a49c8a29 231 /* Read the register */
mcm 2:eb95a49c8a29 232 cmd[0] = BH1790GLC_DATAOUT_LEDOFF_LSB;
mcm 2:eb95a49c8a29 233 aux = _i2c.write ( _BH1790GLC_Addr, &cmd[0], 1U, true );
mcm 2:eb95a49c8a29 234 aux = _i2c.read ( _BH1790GLC_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ) );
mcm 2:eb95a49c8a29 235
mcm 2:eb95a49c8a29 236 /* Parse the data */
mcm 2:eb95a49c8a29 237 myRawDataOut->dataOut_LED_OFF = cmd[1];
mcm 2:eb95a49c8a29 238 myRawDataOut->dataOut_LED_OFF <<= 8U;
mcm 4:55075fea807c 239 myRawDataOut->dataOut_LED_OFF |= cmd[0];
mcm 2:eb95a49c8a29 240
mcm 2:eb95a49c8a29 241 myRawDataOut->dataOut_LED_ON = cmd[3];
mcm 2:eb95a49c8a29 242 myRawDataOut->dataOut_LED_ON <<= 8U;
mcm 4:55075fea807c 243 myRawDataOut->dataOut_LED_ON |= cmd[2];
mcm 2:eb95a49c8a29 244
mcm 2:eb95a49c8a29 245
mcm 2:eb95a49c8a29 246
mcm 2:eb95a49c8a29 247 if ( aux == I2C_SUCCESS ) {
mcm 2:eb95a49c8a29 248 return BH1790GLC_SUCCESS;
mcm 2:eb95a49c8a29 249 } else {
mcm 2:eb95a49c8a29 250 return BH1790GLC_FAILURE;
mcm 2:eb95a49c8a29 251 }
mcm 2:eb95a49c8a29 252 }