forked from skriachko/bq76952
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c_hal.cpp
More file actions
177 lines (146 loc) · 4.27 KB
/
i2c_hal.cpp
File metadata and controls
177 lines (146 loc) · 4.27 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Wrapper class for i2c communication on stm32
* Copyright (c) 2011 BroLab. All right reserved.
* Author : Sergii Kriachko
* Description : Source file of BQ76952 10-series multicell.
*/
extern "C" {
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
}
#include "main.h"
#include "i2c_hal.h"
// Initialize Class Variables //////////////////////////////////////////////////
uint8_t I2C_HAL::rxBuffer[BUFFER_LENGTH];
uint8_t I2C_HAL::rxBufferIndex = 0;
uint8_t I2C_HAL::rxBufferLength = 0;
uint8_t I2C_HAL::txAddress = 0;
uint8_t I2C_HAL::txBuffer[BUFFER_LENGTH];
uint8_t I2C_HAL::txBufferIndex = 0;
uint8_t I2C_HAL::txBufferLength = 0;
uint8_t I2C_HAL::transmitting = 0;
// Constructors ////////////////////////////////////////////////////////////////
I2C_HAL::I2C_HAL(I2C_HandleTypeDef *i2c) :
hi2c(i2c) {
}
// Public Methods //////////////////////////////////////////////////////////////
void I2C_HAL::begin(void) {
rxBufferIndex = 0;
rxBufferLength = 0;
txBufferIndex = 0;
txBufferLength = 0;
}
void I2C_HAL::begin(int address) {
begin((uint8_t) address);
}
static int Timeout = 1000;
uint8_t I2C_HAL::requestFrom(uint8_t address, uint8_t quantity,
uint8_t sendStop) {
// clamp to buffer length
if (quantity > BUFFER_LENGTH) {
quantity = BUFFER_LENGTH;
}
// perform blocking read into buffer
HAL_I2C_Master_Receive(hi2c, address, rxBuffer, quantity, Timeout);
// set rx buffer iterator vars
rxBufferIndex = 0;
rxBufferLength = quantity;
return quantity;
}
uint8_t I2C_HAL::requestFrom(uint8_t address, uint8_t quantity) {
return requestFrom((uint8_t) address, (uint8_t) quantity, (uint8_t) true);
}
uint8_t I2C_HAL::requestFrom(int address, int quantity) {
return requestFrom((uint8_t) address, (uint8_t) quantity, (uint8_t) true);
}
uint8_t I2C_HAL::requestFrom(int address, int quantity, int sendStop) {
return requestFrom((uint8_t) address, (uint8_t) quantity,
(uint8_t) sendStop);
}
void I2C_HAL::beginTransmission(uint8_t address) {
// indicate that we are transmitting
transmitting = 1;
// set address of targeted slave
txAddress = address;
// reset tx buffer iterator vars
txBufferIndex = 0;
txBufferLength = 0;
}
void I2C_HAL::beginTransmission(int address) {
beginTransmission((uint8_t) address);
}
// Transmits data to battery monitor
uint8_t I2C_HAL::endTransmission(uint8_t sendStop) {
// transmit buffer (blocking)
auto status = HAL_I2C_Master_Transmit(hi2c, txAddress, txBuffer,
txBufferLength, Timeout);
if (status != HAL_OK) {
if (HAL_I2C_GetError(hi2c) != HAL_I2C_ERROR_AF)
{
Error_Handler();
}
}
txBufferIndex = 0;
txBufferLength = 0;
// indicate that we are done transmitting
transmitting = 0;
return status==HAL_OK;
}
// Calls data transmission to battery monitor
uint8_t I2C_HAL::endTransmission(void) {
return endTransmission(true);
}
// must be called in:
// slave tx event callback
// or after beginTransmission(address)
size_t I2C_HAL::write(uint8_t data) {
if (transmitting) {
// don't care if buffer is full
if (txBufferLength >= BUFFER_LENGTH) {
setWriteError();
return 0;
}
// add byte in tx buffer
txBuffer[txBufferIndex] = data;
++txBufferIndex;
txBufferLength = txBufferIndex;
} else {
// Send response to master
HAL_I2C_Slave_Transmit_IT(hi2c, &data, 1);
}
return 1;
}
// must be called in:
// slave tx event callback
// or after beginTransmission(address)
size_t I2C_HAL::write(const uint8_t *data, size_t quantity) {
if (transmitting) {
// in master transmitter mode
for (size_t i = 0; i < quantity; ++i) {
write(data[i]);
}
} else {
// reply to master
HAL_I2C_Slave_Transmit_IT(hi2c, (uint8_t *)data, quantity);
}
return quantity;
}
// must be called in:
// slave rx event callback
// or after requestFrom(address, numBytes)
int I2C_HAL::available(void) {
return rxBufferLength - rxBufferIndex;
}
// must be called in:
// slave rx event callback
// or after requestFrom(address, numBytes)
int I2C_HAL::read(void) {
int value = -1;
// get each successive byte on each call
if (rxBufferIndex < rxBufferLength) {
value = rxBuffer[rxBufferIndex];
++rxBufferIndex;
}
return value;
}