|
| 1 | +/* |
| 2 | + Copyright (c) 2019 Arduino. All right reserved. |
| 3 | +
|
| 4 | + This library is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU Lesser General Public |
| 6 | + License as published by the Free Software Foundation; either |
| 7 | + version 2.1 of the License, or (at your option) any later version. |
| 8 | +
|
| 9 | + This library is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + See the GNU Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with this library; if not, write to the Free Software |
| 16 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | +*/ |
| 18 | + |
| 19 | +/****************************************************************************** |
| 20 | + INCLUDE |
| 21 | + ******************************************************************************/ |
| 22 | + |
| 23 | +#include "PF1550_Io_C33.h" |
| 24 | + |
| 25 | +#ifdef ARDUINO_PORTENTA_H33 |
| 26 | + |
| 27 | +#include "Wire.h" |
| 28 | +#include "Arduino.h" |
| 29 | + |
| 30 | +#include "PF1550_Defines.h" |
| 31 | + |
| 32 | +/****************************************************************************** |
| 33 | + CTOR/DTOR |
| 34 | + ******************************************************************************/ |
| 35 | + |
| 36 | +PF1550_Io_C33::PF1550_Io_C33(uint8_t const i2c_addr) |
| 37 | +: _i2c_addr(i2c_addr) |
| 38 | +{ |
| 39 | + |
| 40 | +} |
| 41 | + |
| 42 | +/****************************************************************************** |
| 43 | + PUBLIC MEMBER FUNCTIONS |
| 44 | + ******************************************************************************/ |
| 45 | + |
| 46 | +int PF1550_Io_C33::begin() |
| 47 | +{ |
| 48 | + Wire3.begin(); |
| 49 | + Wire3.setClock(100000); |
| 50 | + |
| 51 | + /* Enable LED. */ |
| 52 | + setBit(Register::CHARGER_LED_PWM, REG_LED_PWM_LED_EN_bp); |
| 53 | + /* Allow LED control by software. */ |
| 54 | + setBit(Register::CHARGER_LED_CNFG, REG_LED_CNFG_LEDOVRD_bp); |
| 55 | + |
| 56 | + return 1; |
| 57 | +} |
| 58 | + |
| 59 | +void PF1550_Io_C33::readRegister(Register const reg_addr, uint8_t * data) |
| 60 | +{ |
| 61 | + uint8_t i2c_data[2]; |
| 62 | + |
| 63 | + i2c_data[0] = (uint8_t)reg_addr; |
| 64 | + |
| 65 | + writeRegister(_i2c_addr, i2c_data, 1, 1); |
| 66 | + |
| 67 | + uint8_t retVal = Wire3.requestFrom(_i2c_addr, 1, true); |
| 68 | + |
| 69 | + if (_debug) { |
| 70 | + _debug->print("requestFrom: "); |
| 71 | + _debug->println(retVal); |
| 72 | + } |
| 73 | + |
| 74 | + int i = 0; |
| 75 | + while (Wire3.available() && i < 1) { |
| 76 | + data[0] = Wire3.read(); |
| 77 | + i++; |
| 78 | + } |
| 79 | + |
| 80 | + if (_debug) { |
| 81 | + _debug->println("Read:"); |
| 82 | + for (i = 0; i < 1; i++) { |
| 83 | + _debug->println(data[i], HEX); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void PF1550_Io_C33::writeRegister(uint8_t slave_addr, uint8_t *data, uint8_t data_len, uint8_t restart) |
| 89 | +{ |
| 90 | + if (_debug) |
| 91 | + { |
| 92 | + _debug->print("Restart: "); |
| 93 | + _debug->println(restart); |
| 94 | + if (!restart) |
| 95 | + { |
| 96 | + _debug->print("PF1550_Io_C33::writeRegister at address="); |
| 97 | + _debug->print(data[0], HEX); |
| 98 | + _debug->print(" data="); |
| 99 | + _debug->println(data[1], HEX); |
| 100 | + _debug->print("i2c slave address: "); |
| 101 | + _debug->println(slave_addr); |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + _debug->print("PF1550_Io_C33::Read from register at address="); |
| 106 | + _debug->println(data[0], HEX); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + Wire3.beginTransmission(slave_addr); |
| 111 | + Wire3.write(data, data_len); |
| 112 | + uint8_t retVal = Wire3.endTransmission(restart == 0 ? true : false); |
| 113 | + |
| 114 | + if (_debug) { |
| 115 | + _debug->print("End transmission: "); |
| 116 | + _debug->println(retVal); |
| 117 | + } |
| 118 | + /* |
| 119 | + if (_debug) { |
| 120 | + _debug->println("Write:"); |
| 121 | + for (int i = 0; i < data_len; i++) { |
| 122 | + _debug->println(data[i], HEX); |
| 123 | + } |
| 124 | + } |
| 125 | + */ |
| 126 | +} |
| 127 | + |
| 128 | +#endif /* ARDUINO_PORTENTA_H33 */ |
0 commit comments