Skip to content

Commit 4a2947a

Browse files
authored
Merge pull request #541 from fpistm/I2C_generalCallMode
[I2C] Add general call mode settings
2 parents e663686 + f7e9716 commit 4a2947a

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

cores/arduino/stm32/twi.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static I2C_HandleTypeDef *i2c_handles[I2C_NUM];
7979
*/
8080
void i2c_init(i2c_t *obj)
8181
{
82-
i2c_custom_init(obj, I2C_100KHz, I2C_ADDRESSINGMODE_7BIT, 0x33, 1);
82+
i2c_custom_init(obj, I2C_100KHz, I2C_ADDRESSINGMODE_7BIT, 0x33);
8383
}
8484

8585
/**
@@ -88,10 +88,9 @@ void i2c_init(i2c_t *obj)
8888
* @param timing : one of the i2c_timing_e
8989
* @param addressingMode : I2C_ADDRESSINGMODE_7BIT or I2C_ADDRESSINGMODE_10BIT
9090
* @param ownAddress : device address
91-
* @param master : set to 1 to choose the master mode
9291
* @retval none
9392
*/
94-
void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, uint32_t ownAddress, uint8_t master)
93+
void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, uint32_t ownAddress)
9594
{
9695
if (obj == NULL) {
9796
return;
@@ -193,7 +192,7 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
193192
handle->Init.OwnAddress2 = 0xFF;
194193
handle->Init.AddressingMode = addressingMode;
195194
handle->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
196-
handle->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
195+
handle->Init.GeneralCallMode = (obj->generalCall == 0) ? I2C_GENERALCALL_DISABLE : I2C_GENERALCALL_ENABLE;
197196
handle->Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
198197

199198
handle->State = HAL_I2C_STATE_RESET;
@@ -211,7 +210,6 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
211210
Error_Handler();
212211
}
213212

214-
obj->isMaster = master;
215213
/* Initialize default values */
216214
obj->slaveRxNbData = 0;
217215
obj->slaveMode = SLAVE_MODE_LISTEN;

cores/arduino/stm32/twi.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,14 @@ struct i2c_s {
100100
#if !defined(STM32F0xx) && !defined(STM32L0xx)
101101
IRQn_Type irqER;
102102
#endif //!defined(STM32F0xx) && !defined(STM32L0xx)
103-
volatile uint8_t slaveMode;
104-
uint8_t isMaster;
105103
volatile int slaveRxNbData; // Number of accumulated bytes received in Slave mode
106104
void (*i2c_onSlaveReceive)(uint8_t *, int);
107105
void (*i2c_onSlaveTransmit)(void);
108106
volatile uint8_t i2cTxRxBuffer[I2C_TXRX_BUFFER_SIZE];
109107
volatile uint8_t i2cTxRxBufferSize;
108+
volatile uint8_t slaveMode;
109+
uint8_t isMaster;
110+
uint8_t generalCall;
110111
};
111112

112113
///@brief I2C state
@@ -153,7 +154,7 @@ typedef enum {
153154
/* Exported functions ------------------------------------------------------- */
154155
void i2c_init(i2c_t *obj);
155156
void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode,
156-
uint32_t ownAddress, uint8_t master);
157+
uint32_t ownAddress);
157158
void i2c_deinit(i2c_t *obj);
158159
void i2c_setTiming(i2c_t *obj, uint32_t frequency);
159160
i2c_status_e i2c_master_write(i2c_t *obj, uint8_t dev_address, uint8_t *data, uint16_t size);

libraries/Wire/src/Wire.cpp

+12-14
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ TwoWire::TwoWire(uint8_t sda, uint8_t scl)
5959

6060
// Public Methods //////////////////////////////////////////////////////////////
6161

62-
void TwoWire::begin(void)
62+
void TwoWire::begin(bool generalCall)
6363
{
64-
begin(MASTER_ADDRESS);
64+
begin(MASTER_ADDRESS, generalCall);
6565
}
6666

67-
void TwoWire::begin(uint8_t address)
67+
void TwoWire::begin(uint8_t address, bool generalCall)
6868
{
6969
rxBufferIndex = 0;
7070
rxBufferLength = 0;
@@ -78,15 +78,13 @@ void TwoWire::begin(uint8_t address)
7878

7979
ownAddress = address << 1;
8080

81-
if (address == MASTER_ADDRESS) {
82-
master = true;
83-
} else {
84-
master = false;
85-
}
81+
_i2c.isMaster = (address == MASTER_ADDRESS) ? 1 : 0;
82+
83+
_i2c.generalCall = (generalCall == true) ? 1 : 0;
8684

87-
i2c_custom_init(&_i2c, I2C_100KHz, I2C_ADDRESSINGMODE_7BIT, ownAddress, master);
85+
i2c_custom_init(&_i2c, I2C_100KHz, I2C_ADDRESSINGMODE_7BIT, ownAddress);
8886

89-
if (master == false) {
87+
if (_i2c.isMaster == 0) {
9088
// i2c_attachSlaveTxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*)>(&TwoWire::onRequestService));
9189
// i2c_attachSlaveRxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*, uint8_t*, int)>(&TwoWire::onReceiveService));
9290

@@ -95,9 +93,9 @@ void TwoWire::begin(uint8_t address)
9593
}
9694
}
9795

98-
void TwoWire::begin(int address)
96+
void TwoWire::begin(int address, bool generalCall)
9997
{
100-
begin((uint8_t)address);
98+
begin((uint8_t)address, generalCall);
10199
}
102100

103101
void TwoWire::end(void)
@@ -119,7 +117,7 @@ void TwoWire::setClock(uint32_t frequency)
119117
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop)
120118
{
121119
UNUSED(sendStop);
122-
if (master == true) {
120+
if (_i2c.isMaster == 1) {
123121
allocateRxBuffer(quantity);
124122
// error if no memory block available to allocate the buffer
125123
if (rxBuffer == nullptr) {
@@ -216,7 +214,7 @@ uint8_t TwoWire::endTransmission(uint8_t sendStop)
216214
UNUSED(sendStop);
217215
int8_t ret = 4;
218216

219-
if (master == true) {
217+
if (_i2c.isMaster == 1) {
220218
// transmit buffer (blocking)
221219
switch (i2c_master_write(&_i2c, txAddress, txBuffer, txBufferLength)) {
222220
case I2C_OK :

libraries/Wire/src/Wire.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ class TwoWire : public Stream {
4949
static uint8_t transmitting;
5050

5151
uint8_t ownAddress;
52-
bool master;
5352
i2c_t _i2c;
5453

5554
static void (*user_onRequest)(void);
@@ -83,9 +82,9 @@ class TwoWire : public Stream {
8382
{
8483
_i2c.sda = sda;
8584
};
86-
void begin();
87-
void begin(uint8_t);
88-
void begin(int);
85+
void begin(bool generalCall = false);
86+
void begin(uint8_t, bool generalCall = false);
87+
void begin(int, bool generalCall = false);
8988
void end();
9089
void setClock(uint32_t);
9190
void beginTransmission(uint8_t);

0 commit comments

Comments
 (0)