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