![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
text export
Dependencies: X_NUCLEO_IKS01A3
INA237/INA237.c@9:8c7774aca512, 2021-05-19 (annotated)
- Committer:
- gpmbed
- Date:
- Wed May 19 15:54:08 2021 +0000
- Revision:
- 9:8c7774aca512
test export
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gpmbed | 9:8c7774aca512 | 1 | /* |
gpmbed | 9:8c7774aca512 | 2 | * ======== INA237.c ======== |
gpmbed | 9:8c7774aca512 | 3 | * INA237 APIs for initialization and use of the INA237 peripheral |
gpmbed | 9:8c7774aca512 | 4 | * |
gpmbed | 9:8c7774aca512 | 5 | * DO NOT EDIT - This file is generated by the SysConfig tool for |
gpmbed | 9:8c7774aca512 | 6 | * the TI Sensors in this application. |
gpmbed | 9:8c7774aca512 | 7 | */ |
gpmbed | 9:8c7774aca512 | 8 | |
gpmbed | 9:8c7774aca512 | 9 | #include <stddef.h> |
gpmbed | 9:8c7774aca512 | 10 | #include <stdint.h> |
gpmbed | 9:8c7774aca512 | 11 | |
gpmbed | 9:8c7774aca512 | 12 | #include "INA237.h" |
gpmbed | 9:8c7774aca512 | 13 | //#include "mcu.h" |
gpmbed | 9:8c7774aca512 | 14 | |
gpmbed | 9:8c7774aca512 | 15 | |
gpmbed | 9:8c7774aca512 | 16 | #define INA237_0_config_register_VALUE (INA237_config_register_rst_NormalOperation | \ |
gpmbed | 9:8c7774aca512 | 17 | 0x0000U | \ |
gpmbed | 9:8c7774aca512 | 18 | INA237_config_register_tempcomp_Shunttemperaturecompensationdisabled | \ |
gpmbed | 9:8c7774aca512 | 19 | INA237_config_register_adcrange_16384mV) |
gpmbed | 9:8c7774aca512 | 20 | #define INA237_0_adc_config_register_VALUE (INA237_adc_config_register_mode_Continuousbusvoltageshuntvoltageandtemperature | \ |
gpmbed | 9:8c7774aca512 | 21 | INA237_adc_config_register_vbusct_1052us | \ |
gpmbed | 9:8c7774aca512 | 22 | INA237_adc_config_register_vshct_1052us | \ |
gpmbed | 9:8c7774aca512 | 23 | INA237_adc_config_register_vtct_1052us | \ |
gpmbed | 9:8c7774aca512 | 24 | INA237_adc_config_register_avg_1) |
gpmbed | 9:8c7774aca512 | 25 | #define INA237_0_shunt_cal_register_VALUE 0x0000U |
gpmbed | 9:8c7774aca512 | 26 | #define INA237_0_diag_alrt_register_VALUE (INA237_diag_alrt_register_alrlen_Transparent | \ |
gpmbed | 9:8c7774aca512 | 27 | INA237_diag_alrt_register_cnvr_DisableconversionreadyflagonALERTpin | \ |
gpmbed | 9:8c7774aca512 | 28 | INA237_diag_alrt_register_slwalrt_ALERTcomparisononnonaveragedADCvalue | \ |
gpmbed | 9:8c7774aca512 | 29 | INA237_diag_alrt_register_apol_Normalactivelowopendrain) |
gpmbed | 9:8c7774aca512 | 30 | #define INA237_0_sovl_register_VALUE 0x7FFFU |
gpmbed | 9:8c7774aca512 | 31 | #define INA237_0_suvl_register_VALUE 0x8000U |
gpmbed | 9:8c7774aca512 | 32 | #define INA237_0_bovl_register_VALUE 0x7FFFU |
gpmbed | 9:8c7774aca512 | 33 | #define INA237_0_buvl_register_VALUE 0x0000U |
gpmbed | 9:8c7774aca512 | 34 | #define INA237_0_temp_limit_register_VALUE 0x7FF0U |
gpmbed | 9:8c7774aca512 | 35 | #define INA237_0_pwr_limit_register_VALUE 0xFFFFU |
gpmbed | 9:8c7774aca512 | 36 | |
gpmbed | 9:8c7774aca512 | 37 | static INA237_State INA237_0_state = { |
gpmbed | 9:8c7774aca512 | 38 | /* Configuration and Settings */ |
gpmbed | 9:8c7774aca512 | 39 | .config = INA237_0_config_register_VALUE, |
gpmbed | 9:8c7774aca512 | 40 | .adcconfig = INA237_0_adc_config_register_VALUE, |
gpmbed | 9:8c7774aca512 | 41 | .shuntcal = INA237_0_shunt_cal_register_VALUE, |
gpmbed | 9:8c7774aca512 | 42 | .diagalrt = INA237_0_diag_alrt_register_VALUE, |
gpmbed | 9:8c7774aca512 | 43 | .sovl = INA237_0_sovl_register_VALUE, |
gpmbed | 9:8c7774aca512 | 44 | .suvl = INA237_0_suvl_register_VALUE, |
gpmbed | 9:8c7774aca512 | 45 | .bovl = INA237_0_bovl_register_VALUE, |
gpmbed | 9:8c7774aca512 | 46 | .buvl = INA237_0_buvl_register_VALUE, |
gpmbed | 9:8c7774aca512 | 47 | .templimit = INA237_0_temp_limit_register_VALUE, |
gpmbed | 9:8c7774aca512 | 48 | .pwrlimit = INA237_0_pwr_limit_register_VALUE, |
gpmbed | 9:8c7774aca512 | 49 | |
gpmbed | 9:8c7774aca512 | 50 | .adcrange = INA237_0_config_register_VALUE & INA237_config_register_adcrange_4096mV, |
gpmbed | 9:8c7774aca512 | 51 | .currentlsb = 0, |
gpmbed | 9:8c7774aca512 | 52 | |
gpmbed | 9:8c7774aca512 | 53 | /* Sensor's I2C bus ID and address */ |
gpmbed | 9:8c7774aca512 | 54 | .busId = 0, |
gpmbed | 9:8c7774aca512 | 55 | .devAddr = 0x40U, |
gpmbed | 9:8c7774aca512 | 56 | |
gpmbed | 9:8c7774aca512 | 57 | }; |
gpmbed | 9:8c7774aca512 | 58 | const INA237_Handle INA237_0 = &INA237_0_state; |
gpmbed | 9:8c7774aca512 | 59 | |
gpmbed | 9:8c7774aca512 | 60 | |
gpmbed | 9:8c7774aca512 | 61 | |
gpmbed | 9:8c7774aca512 | 62 | #define MSB(u16) (((u16) & 0xFF00U) >> 8) |
gpmbed | 9:8c7774aca512 | 63 | #define LSB(u16) ((u16) & 0xFFU) |
gpmbed | 9:8c7774aca512 | 64 | |
gpmbed | 9:8c7774aca512 | 65 | #define maxRegAddress 0x3F |
gpmbed | 9:8c7774aca512 | 66 | |
gpmbed | 9:8c7774aca512 | 67 | // Register size in bytes |
gpmbed | 9:8c7774aca512 | 68 | const uint8_t INA237_regSize[maxRegAddress+1] = { |
gpmbed | 9:8c7774aca512 | 69 | 2,2,2,2,2,2,2,2,\ |
gpmbed | 9:8c7774aca512 | 70 | 3,2,2,2,2,2,2,2,\ |
gpmbed | 9:8c7774aca512 | 71 | 2,2,0,0,0,0,0,0,\ |
gpmbed | 9:8c7774aca512 | 72 | 0,0,0,0,0,0,0,0,\ |
gpmbed | 9:8c7774aca512 | 73 | 0,0,0,0,0,0,0,0,\ |
gpmbed | 9:8c7774aca512 | 74 | 0,0,0,0,0,0,0,0,\ |
gpmbed | 9:8c7774aca512 | 75 | 0,0,0,0,0,0,0,0,\ |
gpmbed | 9:8c7774aca512 | 76 | 0,0,0,0,0,0,2,2 |
gpmbed | 9:8c7774aca512 | 77 | }; |
gpmbed | 9:8c7774aca512 | 78 | |
gpmbed | 9:8c7774aca512 | 79 | void mcu_i2cInit(uint8_t busId); |
gpmbed | 9:8c7774aca512 | 80 | int8_t mcu_i2cTransfer(uint8_t busId, uint8_t sensorAddress, |
gpmbed | 9:8c7774aca512 | 81 | uint8_t *dataToWrite, uint8_t writeLength, |
gpmbed | 9:8c7774aca512 | 82 | uint8_t *dataToRead, uint8_t readLength); |
gpmbed | 9:8c7774aca512 | 83 | void mcu_msWait(unsigned long msWait); |
gpmbed | 9:8c7774aca512 | 84 | |
gpmbed | 9:8c7774aca512 | 85 | /* |
gpmbed | 9:8c7774aca512 | 86 | * ======== INA237_writeReg ======== |
gpmbed | 9:8c7774aca512 | 87 | * Write register |
gpmbed | 9:8c7774aca512 | 88 | */ |
gpmbed | 9:8c7774aca512 | 89 | void INA237_writeReg(INA237_Handle sensor, uint8_t regAddr, uint16_t value) |
gpmbed | 9:8c7774aca512 | 90 | { |
gpmbed | 9:8c7774aca512 | 91 | uint8_t txBuf[3] = {0}; //All writable registers are 2 bytes |
gpmbed | 9:8c7774aca512 | 92 | |
gpmbed | 9:8c7774aca512 | 93 | txBuf[0] = regAddr; |
gpmbed | 9:8c7774aca512 | 94 | txBuf[1] = MSB(value); |
gpmbed | 9:8c7774aca512 | 95 | txBuf[2] = LSB(value); |
gpmbed | 9:8c7774aca512 | 96 | mcu_i2cTransfer(sensor->busId, sensor->devAddr, txBuf, 3, NULL, 0); |
gpmbed | 9:8c7774aca512 | 97 | |
gpmbed | 9:8c7774aca512 | 98 | //check for change in ADCRANGE |
gpmbed | 9:8c7774aca512 | 99 | if(regAddr == INA237_vshunt_register) |
gpmbed | 9:8c7774aca512 | 100 | { |
gpmbed | 9:8c7774aca512 | 101 | sensor->adcrange = value & INA237_config_register_adcrange_4096mV; |
gpmbed | 9:8c7774aca512 | 102 | } |
gpmbed | 9:8c7774aca512 | 103 | } |
gpmbed | 9:8c7774aca512 | 104 | |
gpmbed | 9:8c7774aca512 | 105 | /* |
gpmbed | 9:8c7774aca512 | 106 | * ======== INA237_config ======== |
gpmbed | 9:8c7774aca512 | 107 | * Configure device with current settings. |
gpmbed | 9:8c7774aca512 | 108 | */ |
gpmbed | 9:8c7774aca512 | 109 | void INA237_config(INA237_Handle sensor) |
gpmbed | 9:8c7774aca512 | 110 | { |
gpmbed | 9:8c7774aca512 | 111 | //Initialize the bus containing this sensor |
gpmbed | 9:8c7774aca512 | 112 | mcu_i2cInit(sensor->busId); |
gpmbed | 9:8c7774aca512 | 113 | |
gpmbed | 9:8c7774aca512 | 114 | //Write sensor Configuration Register |
gpmbed | 9:8c7774aca512 | 115 | INA237_writeReg(sensor, INA237_config_register, sensor->config); |
gpmbed | 9:8c7774aca512 | 116 | INA237_writeReg(sensor, INA237_adc_config_register, sensor->adcconfig); |
gpmbed | 9:8c7774aca512 | 117 | INA237_writeReg(sensor, INA237_shunt_cal_register, sensor->shuntcal); |
gpmbed | 9:8c7774aca512 | 118 | INA237_writeReg(sensor, INA237_diag_alrt_register, sensor->diagalrt); |
gpmbed | 9:8c7774aca512 | 119 | INA237_writeReg(sensor, INA237_sovl_register, sensor->sovl); |
gpmbed | 9:8c7774aca512 | 120 | INA237_writeReg(sensor, INA237_suvl_register, sensor->suvl); |
gpmbed | 9:8c7774aca512 | 121 | INA237_writeReg(sensor, INA237_bovl_register, sensor->bovl); |
gpmbed | 9:8c7774aca512 | 122 | INA237_writeReg(sensor, INA237_buvl_register, sensor->buvl); |
gpmbed | 9:8c7774aca512 | 123 | INA237_writeReg(sensor, INA237_temp_limit_register, sensor->templimit); |
gpmbed | 9:8c7774aca512 | 124 | INA237_writeReg(sensor, INA237_pwr_limit_register, sensor->pwrlimit); |
gpmbed | 9:8c7774aca512 | 125 | } |
gpmbed | 9:8c7774aca512 | 126 | |
gpmbed | 9:8c7774aca512 | 127 | /* |
gpmbed | 9:8c7774aca512 | 128 | * ======== INA237_setCURRENT_LSB ======== |
gpmbed | 9:8c7774aca512 | 129 | * Set the CURRENT_LSB value used for calculations |
gpmbed | 9:8c7774aca512 | 130 | */ |
gpmbed | 9:8c7774aca512 | 131 | void INA237_setCURRENT_LSB(INA237_Handle sensor, float CURRENT_LSB) |
gpmbed | 9:8c7774aca512 | 132 | { |
gpmbed | 9:8c7774aca512 | 133 | sensor->currentlsb = CURRENT_LSB; |
gpmbed | 9:8c7774aca512 | 134 | } |
gpmbed | 9:8c7774aca512 | 135 | |
gpmbed | 9:8c7774aca512 | 136 | /* |
gpmbed | 9:8c7774aca512 | 137 | * ======== INA237_readReg ======== |
gpmbed | 9:8c7774aca512 | 138 | * Read register |
gpmbed | 9:8c7774aca512 | 139 | */ |
gpmbed | 9:8c7774aca512 | 140 | uint64_t INA237_readReg(INA237_Handle sensor, uint8_t regAddr) |
gpmbed | 9:8c7774aca512 | 141 | { |
gpmbed | 9:8c7774aca512 | 142 | uint64_t value; |
gpmbed | 9:8c7774aca512 | 143 | int i; |
gpmbed | 9:8c7774aca512 | 144 | |
gpmbed | 9:8c7774aca512 | 145 | uint8_t txBuf[1] = {0}; |
gpmbed | 9:8c7774aca512 | 146 | uint8_t rxBuf[5] = {0}; //max buffer size |
gpmbed | 9:8c7774aca512 | 147 | |
gpmbed | 9:8c7774aca512 | 148 | txBuf[0] = regAddr; |
gpmbed | 9:8c7774aca512 | 149 | |
gpmbed | 9:8c7774aca512 | 150 | //Read register |
gpmbed | 9:8c7774aca512 | 151 | mcu_i2cTransfer(sensor->busId, sensor->devAddr, txBuf, 1, rxBuf, INA237_regSize[regAddr]); |
gpmbed | 9:8c7774aca512 | 152 | |
gpmbed | 9:8c7774aca512 | 153 | //Combine bytes |
gpmbed | 9:8c7774aca512 | 154 | value = rxBuf[0]; |
gpmbed | 9:8c7774aca512 | 155 | for(i = 1; i < INA237_regSize[regAddr]; i++) |
gpmbed | 9:8c7774aca512 | 156 | { |
gpmbed | 9:8c7774aca512 | 157 | value = (value << 8) | rxBuf[i]; |
gpmbed | 9:8c7774aca512 | 158 | } |
gpmbed | 9:8c7774aca512 | 159 | |
gpmbed | 9:8c7774aca512 | 160 | return value; |
gpmbed | 9:8c7774aca512 | 161 | } |
gpmbed | 9:8c7774aca512 | 162 | |
gpmbed | 9:8c7774aca512 | 163 | /* |
gpmbed | 9:8c7774aca512 | 164 | * ======== INA237_getVSHUNT_mV ======== |
gpmbed | 9:8c7774aca512 | 165 | * Get VSHUNT value (mV) |
gpmbed | 9:8c7774aca512 | 166 | */ |
gpmbed | 9:8c7774aca512 | 167 | float INA237_getVSHUNT_mV(INA237_Handle sensor) |
gpmbed | 9:8c7774aca512 | 168 | { |
gpmbed | 9:8c7774aca512 | 169 | uint64_t value = INA237_readReg(sensor, INA237_vshunt_register); |
gpmbed | 9:8c7774aca512 | 170 | float data; |
gpmbed | 9:8c7774aca512 | 171 | |
gpmbed | 9:8c7774aca512 | 172 | //Convert for 2's compliment and signed value |
gpmbed | 9:8c7774aca512 | 173 | if(value > 0x7FFF) |
gpmbed | 9:8c7774aca512 | 174 | { |
gpmbed | 9:8c7774aca512 | 175 | data = (float)value - 0x10000; |
gpmbed | 9:8c7774aca512 | 176 | } |
gpmbed | 9:8c7774aca512 | 177 | else |
gpmbed | 9:8c7774aca512 | 178 | { |
gpmbed | 9:8c7774aca512 | 179 | data = (float)value; |
gpmbed | 9:8c7774aca512 | 180 | } |
gpmbed | 9:8c7774aca512 | 181 | |
gpmbed | 9:8c7774aca512 | 182 | //Convert to mV |
gpmbed | 9:8c7774aca512 | 183 | |
gpmbed | 9:8c7774aca512 | 184 | if(sensor->adcrange == INA237_config_register_adcrange_4096mV) |
gpmbed | 9:8c7774aca512 | 185 | { |
gpmbed | 9:8c7774aca512 | 186 | data = (data * 1.25) / 1000; |
gpmbed | 9:8c7774aca512 | 187 | } |
gpmbed | 9:8c7774aca512 | 188 | else |
gpmbed | 9:8c7774aca512 | 189 | { |
gpmbed | 9:8c7774aca512 | 190 | data = (data * 5) / 1000; |
gpmbed | 9:8c7774aca512 | 191 | } |
gpmbed | 9:8c7774aca512 | 192 | |
gpmbed | 9:8c7774aca512 | 193 | return data; |
gpmbed | 9:8c7774aca512 | 194 | } |
gpmbed | 9:8c7774aca512 | 195 | |
gpmbed | 9:8c7774aca512 | 196 | /* |
gpmbed | 9:8c7774aca512 | 197 | * ======== INA237_getVBUS_V ======== |
gpmbed | 9:8c7774aca512 | 198 | * Get VBUS value (V) |
gpmbed | 9:8c7774aca512 | 199 | */ |
gpmbed | 9:8c7774aca512 | 200 | float INA237_getVBUS_V(INA237_Handle sensor) |
gpmbed | 9:8c7774aca512 | 201 | { |
gpmbed | 9:8c7774aca512 | 202 | uint64_t value = INA237_readReg(sensor, INA237_vbus_register); |
gpmbed | 9:8c7774aca512 | 203 | float data; |
gpmbed | 9:8c7774aca512 | 204 | |
gpmbed | 9:8c7774aca512 | 205 | //Convert for 2's compliment and signed value (though always positive) |
gpmbed | 9:8c7774aca512 | 206 | if(value > 0x7FFF) |
gpmbed | 9:8c7774aca512 | 207 | { |
gpmbed | 9:8c7774aca512 | 208 | data = (float)value - 0x10000; //left for redundancy and error checking, should never get used |
gpmbed | 9:8c7774aca512 | 209 | } |
gpmbed | 9:8c7774aca512 | 210 | else |
gpmbed | 9:8c7774aca512 | 211 | { |
gpmbed | 9:8c7774aca512 | 212 | data = (float)value; |
gpmbed | 9:8c7774aca512 | 213 | } |
gpmbed | 9:8c7774aca512 | 214 | |
gpmbed | 9:8c7774aca512 | 215 | //Convert to V |
gpmbed | 9:8c7774aca512 | 216 | data = (data * 3.125) / 1000; |
gpmbed | 9:8c7774aca512 | 217 | |
gpmbed | 9:8c7774aca512 | 218 | return data; |
gpmbed | 9:8c7774aca512 | 219 | } |
gpmbed | 9:8c7774aca512 | 220 | |
gpmbed | 9:8c7774aca512 | 221 | /* |
gpmbed | 9:8c7774aca512 | 222 | * ======== INA237_getDIETEMP_C ======== |
gpmbed | 9:8c7774aca512 | 223 | * Get DIETMEP value (C) |
gpmbed | 9:8c7774aca512 | 224 | */ |
gpmbed | 9:8c7774aca512 | 225 | float INA237_getDIETEMP_C(INA237_Handle sensor) |
gpmbed | 9:8c7774aca512 | 226 | { |
gpmbed | 9:8c7774aca512 | 227 | uint64_t value = INA237_readReg(sensor, INA237_dietemp_register); |
gpmbed | 9:8c7774aca512 | 228 | float data; |
gpmbed | 9:8c7774aca512 | 229 | |
gpmbed | 9:8c7774aca512 | 230 | //Remove reserved bits |
gpmbed | 9:8c7774aca512 | 231 | value = value >> 4; |
gpmbed | 9:8c7774aca512 | 232 | |
gpmbed | 9:8c7774aca512 | 233 | //Convert for 2's compliment and signed value |
gpmbed | 9:8c7774aca512 | 234 | if(value > 0x7FF) |
gpmbed | 9:8c7774aca512 | 235 | { |
gpmbed | 9:8c7774aca512 | 236 | data = (float)value - 0x1000; |
gpmbed | 9:8c7774aca512 | 237 | } |
gpmbed | 9:8c7774aca512 | 238 | else |
gpmbed | 9:8c7774aca512 | 239 | { |
gpmbed | 9:8c7774aca512 | 240 | data = (float)value; |
gpmbed | 9:8c7774aca512 | 241 | } |
gpmbed | 9:8c7774aca512 | 242 | |
gpmbed | 9:8c7774aca512 | 243 | //Convert to C |
gpmbed | 9:8c7774aca512 | 244 | data = (data * 125) / 1000; |
gpmbed | 9:8c7774aca512 | 245 | |
gpmbed | 9:8c7774aca512 | 246 | return data; |
gpmbed | 9:8c7774aca512 | 247 | } |
gpmbed | 9:8c7774aca512 | 248 | |
gpmbed | 9:8c7774aca512 | 249 | /* |
gpmbed | 9:8c7774aca512 | 250 | * ======== INA237_getDIETEMP_F ======== |
gpmbed | 9:8c7774aca512 | 251 | * Get DIETMEP value (F) |
gpmbed | 9:8c7774aca512 | 252 | */ |
gpmbed | 9:8c7774aca512 | 253 | float INA237_getDIETEMP_F(INA237_Handle sensor) |
gpmbed | 9:8c7774aca512 | 254 | { |
gpmbed | 9:8c7774aca512 | 255 | float data = INA237_getDIETEMP_C(sensor); |
gpmbed | 9:8c7774aca512 | 256 | |
gpmbed | 9:8c7774aca512 | 257 | //Convert to F |
gpmbed | 9:8c7774aca512 | 258 | data = (data * (9/5)) + 32; |
gpmbed | 9:8c7774aca512 | 259 | |
gpmbed | 9:8c7774aca512 | 260 | return data; |
gpmbed | 9:8c7774aca512 | 261 | } |
gpmbed | 9:8c7774aca512 | 262 | |
gpmbed | 9:8c7774aca512 | 263 | /* |
gpmbed | 9:8c7774aca512 | 264 | * ======== INA237_getCURRENT_signedLSB ======== |
gpmbed | 9:8c7774aca512 | 265 | * Get CURRENT value (signed value in LSBs) |
gpmbed | 9:8c7774aca512 | 266 | */ |
gpmbed | 9:8c7774aca512 | 267 | float INA237_getCURRENT_signedLSB(INA237_Handle sensor) |
gpmbed | 9:8c7774aca512 | 268 | { |
gpmbed | 9:8c7774aca512 | 269 | uint64_t value = INA237_readReg(sensor, INA237_current_register); |
gpmbed | 9:8c7774aca512 | 270 | float data; |
gpmbed | 9:8c7774aca512 | 271 | |
gpmbed | 9:8c7774aca512 | 272 | //Convert for 2's compliment and signed value |
gpmbed | 9:8c7774aca512 | 273 | if(value > 0x7FFF) |
gpmbed | 9:8c7774aca512 | 274 | { |
gpmbed | 9:8c7774aca512 | 275 | data = (float)value - 0x10000; |
gpmbed | 9:8c7774aca512 | 276 | } |
gpmbed | 9:8c7774aca512 | 277 | else |
gpmbed | 9:8c7774aca512 | 278 | { |
gpmbed | 9:8c7774aca512 | 279 | data = (float)value; |
gpmbed | 9:8c7774aca512 | 280 | } |
gpmbed | 9:8c7774aca512 | 281 | |
gpmbed | 9:8c7774aca512 | 282 | return data; |
gpmbed | 9:8c7774aca512 | 283 | } |
gpmbed | 9:8c7774aca512 | 284 | |
gpmbed | 9:8c7774aca512 | 285 | /* |
gpmbed | 9:8c7774aca512 | 286 | * ======== INA237_getCURRENT_A ======== |
gpmbed | 9:8c7774aca512 | 287 | * Get CURRENT value (A) |
gpmbed | 9:8c7774aca512 | 288 | */ |
gpmbed | 9:8c7774aca512 | 289 | float INA237_getCURRENT_A(INA237_Handle sensor) |
gpmbed | 9:8c7774aca512 | 290 | { |
gpmbed | 9:8c7774aca512 | 291 | float data = INA237_getCURRENT_signedLSB(sensor); |
gpmbed | 9:8c7774aca512 | 292 | |
gpmbed | 9:8c7774aca512 | 293 | data = data * sensor->currentlsb; |
gpmbed | 9:8c7774aca512 | 294 | |
gpmbed | 9:8c7774aca512 | 295 | return data; |
gpmbed | 9:8c7774aca512 | 296 | } |
gpmbed | 9:8c7774aca512 | 297 | |
gpmbed | 9:8c7774aca512 | 298 | /* |
gpmbed | 9:8c7774aca512 | 299 | * ======== INA237_getPOWER_signedLSB ======== |
gpmbed | 9:8c7774aca512 | 300 | * Get POWER value (signed value in LSBs) |
gpmbed | 9:8c7774aca512 | 301 | */ |
gpmbed | 9:8c7774aca512 | 302 | float INA237_getPOWER_signedLSB(INA237_Handle sensor) |
gpmbed | 9:8c7774aca512 | 303 | { |
gpmbed | 9:8c7774aca512 | 304 | uint64_t value = INA237_readReg(sensor, INA237_power_register); |
gpmbed | 9:8c7774aca512 | 305 | float data; |
gpmbed | 9:8c7774aca512 | 306 | |
gpmbed | 9:8c7774aca512 | 307 | data = (float)value; |
gpmbed | 9:8c7774aca512 | 308 | |
gpmbed | 9:8c7774aca512 | 309 | return data; |
gpmbed | 9:8c7774aca512 | 310 | } |
gpmbed | 9:8c7774aca512 | 311 | |
gpmbed | 9:8c7774aca512 | 312 | /* |
gpmbed | 9:8c7774aca512 | 313 | * ======== INA237_getPOWER_W ======== |
gpmbed | 9:8c7774aca512 | 314 | * Get POWER value (W) |
gpmbed | 9:8c7774aca512 | 315 | */ |
gpmbed | 9:8c7774aca512 | 316 | float INA237_getPOWER_W(INA237_Handle sensor) |
gpmbed | 9:8c7774aca512 | 317 | { |
gpmbed | 9:8c7774aca512 | 318 | float data = INA237_getPOWER_signedLSB(sensor); |
gpmbed | 9:8c7774aca512 | 319 | |
gpmbed | 9:8c7774aca512 | 320 | data = data * sensor->currentlsb * 0.2; |
gpmbed | 9:8c7774aca512 | 321 | |
gpmbed | 9:8c7774aca512 | 322 | return data; |
gpmbed | 9:8c7774aca512 | 323 | } |
gpmbed | 9:8c7774aca512 | 324 | |
gpmbed | 9:8c7774aca512 | 325 | void mcu_i2cInit(uint8_t busId) |
gpmbed | 9:8c7774aca512 | 326 | { |
gpmbed | 9:8c7774aca512 | 327 | /* Add MCU specific init necessary for I2C to be used */ |
gpmbed | 9:8c7774aca512 | 328 | } |
gpmbed | 9:8c7774aca512 | 329 | |
gpmbed | 9:8c7774aca512 | 330 | int8_t mcu_i2cTransfer( uint8_t busId, uint8_t i2cAddr, |
gpmbed | 9:8c7774aca512 | 331 | uint8_t *dataToWrite, uint8_t writeLength, |
gpmbed | 9:8c7774aca512 | 332 | uint8_t *dataToRead, uint8_t readLength) |
gpmbed | 9:8c7774aca512 | 333 | { |
gpmbed | 9:8c7774aca512 | 334 | /* |
gpmbed | 9:8c7774aca512 | 335 | * Add MCU specific I2C read/write code here. |
gpmbed | 9:8c7774aca512 | 336 | */ |
gpmbed | 9:8c7774aca512 | 337 | |
gpmbed | 9:8c7774aca512 | 338 | /* |
gpmbed | 9:8c7774aca512 | 339 | * Add MCU specific return code for error handling |
gpmbed | 9:8c7774aca512 | 340 | */ |
gpmbed | 9:8c7774aca512 | 341 | |
gpmbed | 9:8c7774aca512 | 342 | return (0); |
gpmbed | 9:8c7774aca512 | 343 | } |
gpmbed | 9:8c7774aca512 | 344 | /********* MCU SPECIFIC I2C CODE ENDS HERE**********/ |
gpmbed | 9:8c7774aca512 | 345 | |
gpmbed | 9:8c7774aca512 | 346 | |
gpmbed | 9:8c7774aca512 | 347 | |
gpmbed | 9:8c7774aca512 | 348 | |
gpmbed | 9:8c7774aca512 | 349 | /********* MCU SPECIFIC DELAY CODE STARTS HERE************/ |
gpmbed | 9:8c7774aca512 | 350 | void mcu_msWait(unsigned long msWait) |
gpmbed | 9:8c7774aca512 | 351 | { |
gpmbed | 9:8c7774aca512 | 352 | /* |
gpmbed | 9:8c7774aca512 | 353 | * Add MCU specific wait loop for msWait. The unit is in milli-seconds |
gpmbed | 9:8c7774aca512 | 354 | */ |
gpmbed | 9:8c7774aca512 | 355 | } |