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