TEST

Dependencies:   max32630fthr Adafruit_FeatherOLED USBDevice

Committer:
wwwarunraj
Date:
Sun Apr 19 11:19:57 2020 +0000
Revision:
4:291477e8690d
Parent:
1:f60eafbf009a
19/04

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gmehmet 1:f60eafbf009a 1 /*******************************************************************************
gmehmet 1:f60eafbf009a 2 * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
gmehmet 1:f60eafbf009a 3 *
gmehmet 1:f60eafbf009a 4 * Permission is hereby granted, free of charge, to any person obtaining a
gmehmet 1:f60eafbf009a 5 * copy of this software and associated documentation files (the "Software"),
gmehmet 1:f60eafbf009a 6 * to deal in the Software without restriction, including without limitation
gmehmet 1:f60eafbf009a 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
gmehmet 1:f60eafbf009a 8 * and/or sell copies of the Software, and to permit persons to whom the
gmehmet 1:f60eafbf009a 9 * Software is furnished to do so, subject to the following conditions:
gmehmet 1:f60eafbf009a 10 *
gmehmet 1:f60eafbf009a 11 * The above copyright notice and this permission notice shall be included
gmehmet 1:f60eafbf009a 12 * in all copies or substantial portions of the Software.
gmehmet 1:f60eafbf009a 13 *
gmehmet 1:f60eafbf009a 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
gmehmet 1:f60eafbf009a 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
gmehmet 1:f60eafbf009a 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
gmehmet 1:f60eafbf009a 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
gmehmet 1:f60eafbf009a 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
gmehmet 1:f60eafbf009a 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
gmehmet 1:f60eafbf009a 20 * OTHER DEALINGS IN THE SOFTWARE.
gmehmet 1:f60eafbf009a 21 *
gmehmet 1:f60eafbf009a 22 * Except as contained in this notice, the name of Maxim Integrated
gmehmet 1:f60eafbf009a 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
gmehmet 1:f60eafbf009a 24 * Products, Inc. Branding Policy.
gmehmet 1:f60eafbf009a 25 *
gmehmet 1:f60eafbf009a 26 * The mere transfer of this software does not imply any licenses
gmehmet 1:f60eafbf009a 27 * of trade secrets, proprietary technology, copyrights, patents,
gmehmet 1:f60eafbf009a 28 * trademarks, maskwork rights, or any other form of intellectual
gmehmet 1:f60eafbf009a 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
gmehmet 1:f60eafbf009a 30 * ownership rights.
gmehmet 1:f60eafbf009a 31 *******************************************************************************
gmehmet 1:f60eafbf009a 32 */
gmehmet 1:f60eafbf009a 33
gmehmet 1:f60eafbf009a 34
gmehmet 1:f60eafbf009a 35 #include "MAX20303.h"
gmehmet 1:f60eafbf009a 36
gmehmet 1:f60eafbf009a 37
gmehmet 1:f60eafbf009a 38
gmehmet 1:f60eafbf009a 39 //******************************************************************************
gmehmet 1:f60eafbf009a 40 MAX20303::MAX20303(I2C *i2c):
gmehmet 1:f60eafbf009a 41 m_i2c(i2c), m_writeAddress(MAX20303_SLAVE_WR_ADDR),
gmehmet 1:f60eafbf009a 42 m_readAddress(MAX20303_SLAVE_RD_ADDR)
gmehmet 1:f60eafbf009a 43 {
gmehmet 1:f60eafbf009a 44 }
gmehmet 1:f60eafbf009a 45
gmehmet 1:f60eafbf009a 46
gmehmet 1:f60eafbf009a 47 //******************************************************************************
gmehmet 1:f60eafbf009a 48 MAX20303::~MAX20303(void)
gmehmet 1:f60eafbf009a 49 {
gmehmet 1:f60eafbf009a 50 //empty block
gmehmet 1:f60eafbf009a 51 }
gmehmet 1:f60eafbf009a 52
gmehmet 1:f60eafbf009a 53
gmehmet 1:f60eafbf009a 54 //******************************************************************************
gmehmet 1:f60eafbf009a 55 int MAX20303::LDO1Config()
gmehmet 1:f60eafbf009a 56 {
gmehmet 1:f60eafbf009a 57 int32_t ret = 0;
gmehmet 1:f60eafbf009a 58 // uint8_t val;
gmehmet 1:f60eafbf009a 59 // ret |= writeReg(MAX20303::REG_AP_CMDOUT, 0x40);
gmehmet 1:f60eafbf009a 60 // ret |= writeReg(MAX20303::REG_AP_DATOUT0, 0x05);
gmehmet 1:f60eafbf009a 61 // ret |= writeReg(MAX20303::REG_AP_DATOUT1, 0x34);
gmehmet 1:f60eafbf009a 62 //
gmehmet 1:f60eafbf009a 63 // readReg(MAX20303::REG_AP_CMDOUT, val);
gmehmet 1:f60eafbf009a 64 // readReg(MAX20303::REG_AP_DATOUT0, val);
gmehmet 1:f60eafbf009a 65 // readReg(MAX20303::REG_AP_DATOUT1, val);
gmehmet 1:f60eafbf009a 66 appcmdoutvalue_ = 0x40;
gmehmet 1:f60eafbf009a 67 appdatainoutbuffer_[0] = 0x05;
gmehmet 1:f60eafbf009a 68 appdatainoutbuffer_[1] = 0x34;
gmehmet 1:f60eafbf009a 69 AppWrite(2);
gmehmet 1:f60eafbf009a 70
gmehmet 1:f60eafbf009a 71 return ret;
gmehmet 1:f60eafbf009a 72 }
gmehmet 1:f60eafbf009a 73
gmehmet 1:f60eafbf009a 74 //******************************************************************************
gmehmet 1:f60eafbf009a 75 int MAX20303::LDO2Config()
gmehmet 1:f60eafbf009a 76 {
gmehmet 1:f60eafbf009a 77 int32_t ret = 0;
gmehmet 1:f60eafbf009a 78 // uint8_t val;
gmehmet 1:f60eafbf009a 79 appcmdoutvalue_ = 0x42;
gmehmet 1:f60eafbf009a 80 appdatainoutbuffer_[0] = 0x01;
gmehmet 1:f60eafbf009a 81 appdatainoutbuffer_[1] = 0x15; // 0.9V + (0.1V * number) = 3V
gmehmet 1:f60eafbf009a 82 AppWrite(2);
gmehmet 1:f60eafbf009a 83
gmehmet 1:f60eafbf009a 84 return ret;
gmehmet 1:f60eafbf009a 85 }
gmehmet 1:f60eafbf009a 86
gmehmet 1:f60eafbf009a 87
gmehmet 1:f60eafbf009a 88 //******************************************************************************
gmehmet 1:f60eafbf009a 89 int MAX20303::writeReg(registers_t reg, uint8_t value)
gmehmet 1:f60eafbf009a 90 {
gmehmet 1:f60eafbf009a 91 int ret;
gmehmet 1:f60eafbf009a 92
gmehmet 1:f60eafbf009a 93 char cmdData[2] = {reg, value};
gmehmet 1:f60eafbf009a 94
gmehmet 1:f60eafbf009a 95 ret = m_i2c->write(m_writeAddress, cmdData, sizeof(cmdData));
gmehmet 1:f60eafbf009a 96 //printf("MAX20303 write reg[0x%X]=0x%X, ret=%d\r\n", (uint32_t)reg, value, ret)
gmehmet 1:f60eafbf009a 97
gmehmet 1:f60eafbf009a 98 if (ret != 0)
gmehmet 1:f60eafbf009a 99 return MAX20303_ERROR;
gmehmet 1:f60eafbf009a 100
gmehmet 1:f60eafbf009a 101 return MAX20303_NO_ERROR;
gmehmet 1:f60eafbf009a 102 }
gmehmet 1:f60eafbf009a 103
gmehmet 1:f60eafbf009a 104
gmehmet 1:f60eafbf009a 105 //******************************************************************************
gmehmet 1:f60eafbf009a 106 int MAX20303::readReg(registers_t reg, uint8_t &value)
gmehmet 1:f60eafbf009a 107 {
gmehmet 1:f60eafbf009a 108 int ret;
gmehmet 1:f60eafbf009a 109
gmehmet 1:f60eafbf009a 110 char data = reg;
gmehmet 1:f60eafbf009a 111
gmehmet 1:f60eafbf009a 112 ret = m_i2c->write(m_writeAddress, &data, sizeof(data));
gmehmet 1:f60eafbf009a 113 if (ret != 0) {
gmehmet 1:f60eafbf009a 114 printf("%s - failed - ret: %d\n", __func__, ret);
gmehmet 1:f60eafbf009a 115 return MAX20303_ERROR;
gmehmet 1:f60eafbf009a 116 }
gmehmet 1:f60eafbf009a 117
gmehmet 1:f60eafbf009a 118 ret = m_i2c->read(m_readAddress, &data, sizeof(data));
gmehmet 1:f60eafbf009a 119 if (ret != 0) {
gmehmet 1:f60eafbf009a 120 printf("%s - failed - ret: %d\n", __func__, ret);
gmehmet 1:f60eafbf009a 121 return MAX20303_ERROR;
gmehmet 1:f60eafbf009a 122 }
gmehmet 1:f60eafbf009a 123
gmehmet 1:f60eafbf009a 124 value = data;
gmehmet 1:f60eafbf009a 125 printf("MAX20303 read reg[0x%X]=0x%X, ret=%d\r\n", (unsigned int)reg, (unsigned int)value, ret);
gmehmet 1:f60eafbf009a 126 return MAX20303_NO_ERROR;
gmehmet 1:f60eafbf009a 127 }
gmehmet 1:f60eafbf009a 128
gmehmet 1:f60eafbf009a 129 //******************************************************************************
gmehmet 1:f60eafbf009a 130 int MAX20303::readRegMulti(registers_t reg, uint8_t *value, uint8_t len){
gmehmet 1:f60eafbf009a 131 int ret;
gmehmet 1:f60eafbf009a 132 char data = reg;
gmehmet 1:f60eafbf009a 133
gmehmet 1:f60eafbf009a 134 ret = m_i2c->write(m_writeAddress, &data, sizeof(data));
gmehmet 1:f60eafbf009a 135 if (ret != 0) {
gmehmet 1:f60eafbf009a 136 printf("%s - failed - ret: %d\n", __func__, ret);
gmehmet 1:f60eafbf009a 137 return MAX20303_ERROR;
gmehmet 1:f60eafbf009a 138 }
gmehmet 1:f60eafbf009a 139
gmehmet 1:f60eafbf009a 140 ret = m_i2c->read(m_readAddress, (char *)value, len);
gmehmet 1:f60eafbf009a 141 if (ret != 0) {
gmehmet 1:f60eafbf009a 142 printf("%s - failed - ret: %d\n", __func__, ret);
gmehmet 1:f60eafbf009a 143 return MAX20303_ERROR;
gmehmet 1:f60eafbf009a 144 }
gmehmet 1:f60eafbf009a 145
gmehmet 1:f60eafbf009a 146 printf("MAX20303 read reg[0x%X]=0x%X, ret=%d\r\n", (unsigned int)reg, (unsigned int)value, ret);
gmehmet 1:f60eafbf009a 147 return MAX20303_NO_ERROR;
gmehmet 1:f60eafbf009a 148 }
gmehmet 1:f60eafbf009a 149
gmehmet 1:f60eafbf009a 150 //******************************************************************************
gmehmet 1:f60eafbf009a 151 int MAX20303::writeRegMulti(registers_t reg, uint8_t *value, uint8_t len){
gmehmet 1:f60eafbf009a 152 int32_t ret;
gmehmet 1:f60eafbf009a 153 i2cbuffer_[0] = reg;
gmehmet 1:f60eafbf009a 154 memcpy(&i2cbuffer_[1], value, len);
gmehmet 1:f60eafbf009a 155
gmehmet 1:f60eafbf009a 156 ret = m_i2c->write(m_writeAddress, (char *)i2cbuffer_, (len+1));
gmehmet 1:f60eafbf009a 157 //printf("MAX20303 write reg[0x%X]=0x%X, ret=%d\r\n", (uint32_t)reg, value, ret)
gmehmet 1:f60eafbf009a 158
gmehmet 1:f60eafbf009a 159 if (ret != 0)
gmehmet 1:f60eafbf009a 160 return MAX20303_ERROR;
gmehmet 1:f60eafbf009a 161
gmehmet 1:f60eafbf009a 162 return MAX20303_NO_ERROR;
gmehmet 1:f60eafbf009a 163 }
gmehmet 1:f60eafbf009a 164 //******************************************************************************
gmehmet 1:f60eafbf009a 165 int MAX20303::mv2bits(int mV)
gmehmet 1:f60eafbf009a 166 {
gmehmet 1:f60eafbf009a 167 int regBits;
gmehmet 1:f60eafbf009a 168
gmehmet 1:f60eafbf009a 169 if (( MAX20303_LDO_MIN_MV <= mV) && (mV <= MAX20303_LDO_MAX_MV)) {
gmehmet 1:f60eafbf009a 170 regBits = (mV - MAX20303_LDO_MIN_MV) / MAX20303_LDO_STEP_MV;
gmehmet 1:f60eafbf009a 171 } else {
gmehmet 1:f60eafbf009a 172 return -1;
gmehmet 1:f60eafbf009a 173 }
gmehmet 1:f60eafbf009a 174
gmehmet 1:f60eafbf009a 175 return regBits;
gmehmet 1:f60eafbf009a 176 }
gmehmet 1:f60eafbf009a 177 //******************************************************************************
gmehmet 1:f60eafbf009a 178 int MAX20303::PowerOffthePMIC(){
gmehmet 1:f60eafbf009a 179 int ret;
gmehmet 1:f60eafbf009a 180 appdatainoutbuffer_[0] = 0xB2;
gmehmet 1:f60eafbf009a 181 appcmdoutvalue_ = 0x80;
gmehmet 1:f60eafbf009a 182 ret = AppWrite(1);
gmehmet 1:f60eafbf009a 183
gmehmet 1:f60eafbf009a 184 if(appcmdoutvalue_ != 0x80){
gmehmet 1:f60eafbf009a 185 ret |= MAX20303_ERROR;
gmehmet 1:f60eafbf009a 186 }
gmehmet 1:f60eafbf009a 187
gmehmet 1:f60eafbf009a 188 return ret;
gmehmet 1:f60eafbf009a 189 }
gmehmet 1:f60eafbf009a 190 //******************************************************************************
gmehmet 1:f60eafbf009a 191 int MAX20303::PowerOffDelaythePMIC(){
gmehmet 1:f60eafbf009a 192 int ret;
gmehmet 1:f60eafbf009a 193 appdatainoutbuffer_[0] = 0xB2;
gmehmet 1:f60eafbf009a 194 appcmdoutvalue_ = 0x84;
gmehmet 1:f60eafbf009a 195 ret = AppWrite(1);
gmehmet 1:f60eafbf009a 196
gmehmet 1:f60eafbf009a 197 if(appcmdoutvalue_ != 0x80){
gmehmet 1:f60eafbf009a 198 ret |= MAX20303_ERROR;
gmehmet 1:f60eafbf009a 199 }
gmehmet 1:f60eafbf009a 200
gmehmet 1:f60eafbf009a 201 return ret;
gmehmet 1:f60eafbf009a 202 }
gmehmet 1:f60eafbf009a 203
gmehmet 1:f60eafbf009a 204 //******************************************************************************
gmehmet 1:f60eafbf009a 205 int MAX20303::SoftResetthePMIC(){
gmehmet 1:f60eafbf009a 206 int ret;
gmehmet 1:f60eafbf009a 207 appdatainoutbuffer_[0] = 0xB3;
gmehmet 1:f60eafbf009a 208 appcmdoutvalue_ = 0x81;
gmehmet 1:f60eafbf009a 209 ret = AppWrite(1);
gmehmet 1:f60eafbf009a 210
gmehmet 1:f60eafbf009a 211 if(appcmdoutvalue_ != 0x81){
gmehmet 1:f60eafbf009a 212 ret |= MAX20303_ERROR;
gmehmet 1:f60eafbf009a 213 }
gmehmet 1:f60eafbf009a 214
gmehmet 1:f60eafbf009a 215 return ret;
gmehmet 1:f60eafbf009a 216 }
gmehmet 1:f60eafbf009a 217 //******************************************************************************
gmehmet 1:f60eafbf009a 218 int MAX20303::HardResetthePMIC(){
gmehmet 1:f60eafbf009a 219 int ret;
gmehmet 1:f60eafbf009a 220 appdatainoutbuffer_[0] = 0xB4;
gmehmet 1:f60eafbf009a 221 appcmdoutvalue_ = 0x82;
gmehmet 1:f60eafbf009a 222 ret = AppWrite(1);
gmehmet 1:f60eafbf009a 223
gmehmet 1:f60eafbf009a 224 if(appcmdoutvalue_ != 0x82){
gmehmet 1:f60eafbf009a 225 ret |= MAX20303_ERROR;
gmehmet 1:f60eafbf009a 226 }
gmehmet 1:f60eafbf009a 227
gmehmet 1:f60eafbf009a 228 return ret;
gmehmet 1:f60eafbf009a 229 }
gmehmet 1:f60eafbf009a 230
gmehmet 1:f60eafbf009a 231 //******************************************************************************
gmehmet 1:f60eafbf009a 232 int MAX20303::AppWrite(uint8_t dataoutlen){
gmehmet 1:f60eafbf009a 233 int ret;
gmehmet 1:f60eafbf009a 234
gmehmet 1:f60eafbf009a 235 ret = writeRegMulti(MAX20303::REG_AP_DATOUT0, appdatainoutbuffer_, dataoutlen);
gmehmet 1:f60eafbf009a 236 ret |= writeReg(MAX20303::REG_AP_CMDOUT, appcmdoutvalue_);
gmehmet 1:f60eafbf009a 237 wait_ms(10);
gmehmet 1:f60eafbf009a 238 ret |= readReg(MAX20303::REG_AP_RESPONSE, appcmdoutvalue_);
gmehmet 1:f60eafbf009a 239
gmehmet 1:f60eafbf009a 240 if(ret != 0)
gmehmet 1:f60eafbf009a 241 return MAX20303_ERROR;
gmehmet 1:f60eafbf009a 242
gmehmet 1:f60eafbf009a 243 return MAX20303_NO_ERROR;
gmehmet 1:f60eafbf009a 244 }
gmehmet 1:f60eafbf009a 245
gmehmet 1:f60eafbf009a 246
gmehmet 1:f60eafbf009a 247 //******************************************************************************
gmehmet 1:f60eafbf009a 248 int MAX20303::AppRead(uint8_t datainlen){
gmehmet 1:f60eafbf009a 249 int ret;
gmehmet 1:f60eafbf009a 250
gmehmet 1:f60eafbf009a 251 ret = writeReg(MAX20303::REG_AP_CMDOUT, appcmdoutvalue_);
gmehmet 1:f60eafbf009a 252 wait_ms(10);
gmehmet 1:f60eafbf009a 253 ret |= readRegMulti(MAX20303::REG_AP_RESPONSE, i2cbuffer_, datainlen);
gmehmet 1:f60eafbf009a 254 if(ret != 0)
gmehmet 1:f60eafbf009a 255 return MAX20303_ERROR;
gmehmet 1:f60eafbf009a 256
gmehmet 1:f60eafbf009a 257 return MAX20303_NO_ERROR;
gmehmet 1:f60eafbf009a 258 }
gmehmet 1:f60eafbf009a 259
gmehmet 1:f60eafbf009a 260 //******************************************************************************
gmehmet 1:f60eafbf009a 261 char MAX20303::CheckPMICHWID(){
gmehmet 1:f60eafbf009a 262 int ret;
gmehmet 1:f60eafbf009a 263 uint8_t value = 0x00;
gmehmet 1:f60eafbf009a 264
gmehmet 1:f60eafbf009a 265 ret = readReg(MAX20303::REG_HARDWARE_ID, value);
gmehmet 1:f60eafbf009a 266 if(ret != MAX20303_NO_ERROR)
gmehmet 1:f60eafbf009a 267 return false;
gmehmet 1:f60eafbf009a 268
gmehmet 1:f60eafbf009a 269 if(value == 0x02)
gmehmet 1:f60eafbf009a 270 return true;
gmehmet 1:f60eafbf009a 271 else
gmehmet 1:f60eafbf009a 272 return false;
gmehmet 1:f60eafbf009a 273 }
gmehmet 1:f60eafbf009a 274
gmehmet 1:f60eafbf009a 275 //******************************************************************************
gmehmet 1:f60eafbf009a 276 int MAX20303::CheckPMICStatusRegisters(unsigned char buf_results[5]){
gmehmet 1:f60eafbf009a 277 int ret;
gmehmet 1:f60eafbf009a 278 ret = readReg(MAX20303::REG_STATUS0, buf_results[0]);
gmehmet 1:f60eafbf009a 279 ret |= readReg(MAX20303::REG_STATUS1, buf_results[1]);
gmehmet 1:f60eafbf009a 280 ret |= readReg(MAX20303::REG_STATUS2, buf_results[2]);
gmehmet 1:f60eafbf009a 281 ret |= readReg(MAX20303::REG_STATUS3, buf_results[3]);
gmehmet 1:f60eafbf009a 282 ret |= readReg(MAX20303::REG_SYSTEM_ERROR, buf_results[4]);
gmehmet 1:f60eafbf009a 283 return ret;
gmehmet 1:f60eafbf009a 284 }
gmehmet 1:f60eafbf009a 285
gmehmet 1:f60eafbf009a 286 //******************************************************************************
gmehmet 1:f60eafbf009a 287 int MAX20303::Max20303_BatteryGauge(unsigned char *batterylevel){
gmehmet 1:f60eafbf009a 288 int ret;
gmehmet 1:f60eafbf009a 289 char data[2];
gmehmet 1:f60eafbf009a 290 //uint8_t value;
gmehmet 1:f60eafbf009a 291 //printf("m_battery_is_connected is:%d\r\n", m_battery_is_connected);
gmehmet 1:f60eafbf009a 292 if(!m_battery_is_connected) {
gmehmet 1:f60eafbf009a 293 *batterylevel = 0;
gmehmet 1:f60eafbf009a 294 return 0;
gmehmet 1:f60eafbf009a 295 }
gmehmet 1:f60eafbf009a 296
gmehmet 1:f60eafbf009a 297 data[0] = 0x04;
gmehmet 1:f60eafbf009a 298 ret = m_i2c->write(MAX20303_I2C_ADDR_FUEL_GAUGE, data, 1);
gmehmet 1:f60eafbf009a 299 if(ret != 0){
gmehmet 1:f60eafbf009a 300 printf("Max20303_FuelGauge has failed\r\n");
gmehmet 1:f60eafbf009a 301 }
gmehmet 1:f60eafbf009a 302
gmehmet 1:f60eafbf009a 303 ret = m_i2c->read(MAX20303_I2C_ADDR_FUEL_GAUGE | 1, data, 2);
gmehmet 1:f60eafbf009a 304 if(ret != 0){
gmehmet 1:f60eafbf009a 305 printf("Max20303_FuelGauge has failed\r\n");
gmehmet 1:f60eafbf009a 306 }
gmehmet 1:f60eafbf009a 307 //printf("battery level is:%d\r\n", data[0]);
gmehmet 1:f60eafbf009a 308 *batterylevel = data[0];
gmehmet 1:f60eafbf009a 309
gmehmet 1:f60eafbf009a 310 return 0;
gmehmet 1:f60eafbf009a 311 }
gmehmet 1:f60eafbf009a 312
gmehmet 1:f60eafbf009a 313 //******************************************************************************
gmehmet 1:f60eafbf009a 314 char MAX20303::Max20303_IsBattery_Connected(){
gmehmet 1:f60eafbf009a 315 AnalogIn ain(AIN_5);
gmehmet 1:f60eafbf009a 316 float adc_value;
gmehmet 1:f60eafbf009a 317 int ret;
gmehmet 1:f60eafbf009a 318 char result;
gmehmet 1:f60eafbf009a 319 // config the mux for the monitor pin
gmehmet 1:f60eafbf009a 320 appcmdoutvalue_ = 0x50;
gmehmet 1:f60eafbf009a 321 appdatainoutbuffer_[0] = 0x80;
gmehmet 1:f60eafbf009a 322 AppWrite(1);
gmehmet 1:f60eafbf009a 323 // disable the charger
gmehmet 1:f60eafbf009a 324 appcmdoutvalue_ = 0x1A;
gmehmet 1:f60eafbf009a 325 appdatainoutbuffer_[0] = 0x02;
gmehmet 1:f60eafbf009a 326 AppWrite(1);
gmehmet 1:f60eafbf009a 327 wait_ms(250);
gmehmet 1:f60eafbf009a 328 // adc measure launch
gmehmet 1:f60eafbf009a 329 appcmdoutvalue_ = 0x53;
gmehmet 1:f60eafbf009a 330 appdatainoutbuffer_[0] = 0x09;
gmehmet 1:f60eafbf009a 331 AppWrite(1);
gmehmet 1:f60eafbf009a 332 wait_ms(10);
gmehmet 1:f60eafbf009a 333 ret = readRegMulti(MAX20303::REG_AP_RESPONSE, i2cbuffer_, 5);
gmehmet 1:f60eafbf009a 334 adc_value = ain.read();
gmehmet 1:f60eafbf009a 335 if(ret != 0){
gmehmet 1:f60eafbf009a 336 result = false;
gmehmet 1:f60eafbf009a 337 } else {
gmehmet 1:f60eafbf009a 338 if((i2cbuffer_[4] < 0x40) | (adc_value < 0.3)) {
gmehmet 1:f60eafbf009a 339 result = false;
gmehmet 1:f60eafbf009a 340 } else {
gmehmet 1:f60eafbf009a 341 result = true;
gmehmet 1:f60eafbf009a 342 }
gmehmet 1:f60eafbf009a 343 }
gmehmet 1:f60eafbf009a 344 // enable the charger
gmehmet 1:f60eafbf009a 345 appcmdoutvalue_ = 0x1A;
gmehmet 1:f60eafbf009a 346 appdatainoutbuffer_[0] = 0x03;
gmehmet 1:f60eafbf009a 347 AppWrite(1);
gmehmet 1:f60eafbf009a 348 // for(int i = 1; i < 5; ++i){
gmehmet 1:f60eafbf009a 349 // printf("reg value at:%d is:%02X\r\n", i, i2cbuffer_[i]);
gmehmet 1:f60eafbf009a 350 // }
gmehmet 1:f60eafbf009a 351 m_battery_is_connected = result;
gmehmet 1:f60eafbf009a 352 return result;
gmehmet 1:f60eafbf009a 353 }
gmehmet 1:f60eafbf009a 354
gmehmet 1:f60eafbf009a 355
gmehmet 1:f60eafbf009a 356 //******************************************************************************
gmehmet 1:f60eafbf009a 357 int MAX20303::led0on(char enable) {
gmehmet 1:f60eafbf009a 358
gmehmet 1:f60eafbf009a 359 if(enable)
gmehmet 1:f60eafbf009a 360 return writeReg(REG_LED0_DIRECT, 0x21);
gmehmet 1:f60eafbf009a 361 else
gmehmet 1:f60eafbf009a 362 return writeReg(REG_LED0_DIRECT, 0x01);
gmehmet 1:f60eafbf009a 363 }
gmehmet 1:f60eafbf009a 364
gmehmet 1:f60eafbf009a 365 //******************************************************************************
gmehmet 1:f60eafbf009a 366 int MAX20303::led1on(char enable) {
gmehmet 1:f60eafbf009a 367 if(enable)
gmehmet 1:f60eafbf009a 368 return writeReg(REG_LED1_DIRECT, 0x21);
gmehmet 1:f60eafbf009a 369 else
gmehmet 1:f60eafbf009a 370 return writeReg(REG_LED1_DIRECT, 0x01);
gmehmet 1:f60eafbf009a 371 }
gmehmet 1:f60eafbf009a 372
gmehmet 1:f60eafbf009a 373 //******************************************************************************
gmehmet 1:f60eafbf009a 374 int MAX20303::led2on(char enable) {
gmehmet 1:f60eafbf009a 375 if(enable)
gmehmet 1:f60eafbf009a 376 return writeReg(REG_LED2_DIRECT, 0x21);
gmehmet 1:f60eafbf009a 377 else
gmehmet 1:f60eafbf009a 378 return writeReg(REG_LED2_DIRECT, 0x01);
gmehmet 1:f60eafbf009a 379 }
gmehmet 1:f60eafbf009a 380
gmehmet 1:f60eafbf009a 381
gmehmet 1:f60eafbf009a 382 //******************************************************************************
gmehmet 1:f60eafbf009a 383 int MAX20303::BoostEnable(void) {
gmehmet 1:f60eafbf009a 384 writeReg(REG_AP_DATOUT3, 0x00); // 00 : 5V
gmehmet 1:f60eafbf009a 385 writeReg(REG_AP_DATOUT0, 0x01); // Boost Enabled
gmehmet 1:f60eafbf009a 386 writeReg(REG_AP_CMDOUT, 0x30);
gmehmet 1:f60eafbf009a 387 return MAX20303_NO_ERROR;
gmehmet 1:f60eafbf009a 388 }
gmehmet 1:f60eafbf009a 389
gmehmet 1:f60eafbf009a 390 //******************************************************************************
gmehmet 1:f60eafbf009a 391 int MAX20303::BuckBoostEnable(void)
gmehmet 1:f60eafbf009a 392 {
gmehmet 1:f60eafbf009a 393 int ret = 0;
gmehmet 1:f60eafbf009a 394
gmehmet 1:f60eafbf009a 395 ret |= writeReg( REG_AP_DATOUT0, 0x00); // Reserved = 0x00
gmehmet 1:f60eafbf009a 396 ret |= writeReg( REG_AP_DATOUT1, 0x04); // BBstlSet = 0b'100 Buck Boost Peak current Limit = 200mA
gmehmet 1:f60eafbf009a 397 ret |= writeReg( REG_AP_DATOUT2, 0x19); // BBstVSet = 0b'11001 Buck Boost Output Voltage = 5V
gmehmet 1:f60eafbf009a 398 ret |= writeReg( REG_AP_DATOUT3, 0x01); // BBstRipRed = 1 Ripple Reduction
gmehmet 1:f60eafbf009a 399 // BBstAct = 1 Actively discharged in Hard-Reset or Enable Low
gmehmet 1:f60eafbf009a 400 // BBstPas = 1 Passively discharged in Hard-Reset or Enable Low
gmehmet 1:f60eafbf009a 401 // BBstMd = 1 Damping Enabled
gmehmet 1:f60eafbf009a 402 // BBstInd = 0 Inductance is 4.7uH
gmehmet 1:f60eafbf009a 403 // BBstEn = 0b'01 Enabled
gmehmet 1:f60eafbf009a 404 ret |= writeReg( REG_AP_CMDOUT, 0x70);
gmehmet 1:f60eafbf009a 405 if (ret != 0)
gmehmet 1:f60eafbf009a 406 return MAX20303_ERROR;
gmehmet 1:f60eafbf009a 407
gmehmet 1:f60eafbf009a 408 return MAX20303_NO_ERROR;
gmehmet 1:f60eafbf009a 409 }