Skip to content

Commit 1c9b086

Browse files
authored
Merge pull request arduino#46 from facchinm/i2c_setspeed
Wire: fix setClock
2 parents 5ee4274 + 3c46426 commit 1c9b086

File tree

2 files changed

+61
-60
lines changed

2 files changed

+61
-60
lines changed

Diff for: libraries/Wire/Wire.cpp

+60-51
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,12 @@ void TwoWire::WireSlaveCallback(i2c_slave_callback_args_t *arg) {
184184

185185

186186
/* -------------------------------------------------------------------------- */
187-
TwoWire::TwoWire(int scl, int sda, WireSpeed_t wp /*= SPEED_STANDARD*/, WireAddressMode_t am /*= ADDRESS_MODE_7_BITS*/, bool prefer_sci /*= false*/) :
187+
TwoWire::TwoWire(int scl, int sda, WireAddressMode_t am /*= ADDRESS_MODE_7_BITS*/, bool prefer_sci /*= false*/) :
188188
scl_pin(scl),
189189
sda_pin(sda),
190190
init_ok(false),
191191
is_master(true),
192192
is_sci(false),
193-
speed_mode(wp),
194193
address_mode(am),
195194
timeout(1000),
196195
transmission_begun(false),
@@ -281,11 +280,14 @@ void TwoWire::begin(void) {
281280
init_ok &= cfg_pins(max_index);
282281

283282
if(init_ok) {
284-
283+
285284
/* -----------------------------------
286285
->>>>> MASTER initialization
287286
* ----------------------------------- */
288287
if(is_master) {
288+
289+
setClock(I2C_MASTER_RATE_STANDARD);
290+
289291
if(is_sci) {
290292
TwoWire::g_SCIWires[channel] = this;
291293

@@ -315,40 +317,21 @@ void TwoWire::begin(void) {
315317

316318
m_i2c_cfg.p_extend = &m_i2c_extend;
317319
m_i2c_cfg.p_callback = WireMasterCallback;
318-
}
319-
320-
m_sci_i2c_extend.clock_settings.clk_divisor_value = 0;
321-
m_sci_i2c_extend.clock_settings.brr_value = 14;
322-
m_sci_i2c_extend.clock_settings.mddr_value = 255;
323-
m_sci_i2c_extend.clock_settings.bitrate_modulation = false;
324-
m_sci_i2c_extend.clock_settings.cycles_value = 15;
325-
m_sci_i2c_extend.clock_settings.snfr_value = (1);
326320

327-
328-
/* Actual calculated bitrate: 99272. Actual calculated duty cycle: 49%. */
329-
m_i2c_extend.timeout_mode = IIC_MASTER_TIMEOUT_MODE_SHORT;
330-
m_i2c_extend.timeout_scl_low = IIC_MASTER_TIMEOUT_SCL_LOW_DISABLED;
331-
m_i2c_extend.clock_settings.brl_value = 27;
332-
m_i2c_extend.clock_settings.brh_value = 26;
333-
m_i2c_extend.clock_settings.cks_value = 2;
321+
m_i2c_extend.timeout_mode = IIC_MASTER_TIMEOUT_MODE_SHORT;
322+
m_i2c_extend.timeout_scl_low = IIC_MASTER_TIMEOUT_SCL_LOW_DISABLED;
323+
}
334324

335325
m_i2c_cfg.channel = channel;
336326
m_i2c_cfg.rate = I2C_MASTER_RATE_STANDARD;
337-
if(speed_mode == SPEED_FAST) {
338-
m_i2c_cfg.rate = I2C_MASTER_RATE_FAST;
339-
}
340-
else if(speed_mode == SPEED_VERY_FAST) {
341-
m_i2c_cfg.rate = I2C_MASTER_RATE_FASTPLUS;
342-
}
343327
m_i2c_cfg.slave = 0x00;
344328
m_i2c_cfg.addr_mode = (address_mode == ADDRESS_MODE_7_BITS) ? I2C_MASTER_ADDR_MODE_7BIT : I2C_MASTER_ADDR_MODE_10BIT;
345329
m_i2c_cfg.p_transfer_tx = NULL;
346330
m_i2c_cfg.p_transfer_rx = NULL;
347-
331+
348332
m_i2c_cfg.p_context = &m_i2c_cfg;
349333
m_i2c_cfg.ipl = (12);
350-
351-
334+
352335
} // if(is_master) {
353336
/* -----------------------------------
354337
->>>>> SLAVE initialization
@@ -369,12 +352,6 @@ void TwoWire::begin(void) {
369352

370353
s_i2c_cfg.channel = channel;
371354
s_i2c_cfg.rate = I2C_SLAVE_RATE_STANDARD;
372-
if(speed_mode == SPEED_FAST) {
373-
s_i2c_cfg.rate = I2C_SLAVE_RATE_FAST;
374-
}
375-
else if(speed_mode == SPEED_VERY_FAST) {
376-
s_i2c_cfg.rate = I2C_SLAVE_RATE_FASTPLUS;
377-
}
378355
s_i2c_cfg.slave = slave_address;
379356
s_i2c_cfg.addr_mode = (address_mode == ADDRESS_MODE_7_BITS) ? I2C_SLAVE_ADDR_MODE_7BIT : I2C_SLAVE_ADDR_MODE_10BIT;
380357
s_i2c_cfg.general_call_enable = false;
@@ -483,13 +460,13 @@ uint8_t TwoWire::read_from(uint8_t address, uint8_t* data, uint8_t length, unsig
483460
}
484461
if(err == FSP_SUCCESS) {
485462
if(m_read != nullptr) {
463+
bus_status = WIRE_STATUS_UNSET;
486464
err = m_read(&m_i2c_ctrl,data,length,!sendStop);
487465
}
488466
}
489-
bus_status = WIRE_STATUS_UNSET;
490-
while(timeout_ms > 0 && bus_status == WIRE_STATUS_UNSET) {
491-
timeout_ms--;
492-
delay(1);
467+
timeout_ms = millis() + timeout_ms;
468+
while(millis() < timeout_ms && bus_status == WIRE_STATUS_UNSET && err == FSP_SUCCESS) {
469+
493470
}
494471
}
495472

@@ -511,13 +488,13 @@ uint8_t TwoWire::write_to(uint8_t address, uint8_t* data, uint8_t length, unsign
511488
}
512489
if(err == FSP_SUCCESS) {
513490
if(m_write != nullptr) {
491+
bus_status = WIRE_STATUS_UNSET;
514492
err = m_write(&m_i2c_ctrl,data,length,!sendStop);
515493
}
516494
}
517-
bus_status = WIRE_STATUS_UNSET;
518-
while(err == FSP_SUCCESS && timeout_ms > 0 && bus_status == WIRE_STATUS_UNSET) {
519-
timeout_ms--;
520-
delay(1);
495+
timeout_ms = millis() + timeout_ms;
496+
while(millis() < timeout_ms && bus_status == WIRE_STATUS_UNSET && err == FSP_SUCCESS) {
497+
521498
}
522499

523500
if(err != FSP_SUCCESS) {
@@ -544,7 +521,7 @@ uint8_t TwoWire::write_to(uint8_t address, uint8_t* data, uint8_t length, unsign
544521
}
545522

546523
/* -------------------------------------------------------------------------- */
547-
void TwoWire::setClock(uint32_t ws) {
524+
void TwoWire::setClock(uint32_t freq) {
548525
/* -------------------------------------------------------------------------- */
549526
if(init_ok && is_master) {
550527
if(m_close != nullptr) {
@@ -553,16 +530,48 @@ void TwoWire::setClock(uint32_t ws) {
553530
}
554531

555532
if(is_master) {
556-
if((WireSpeed_t)ws == SPEED_STANDARD) {
557-
m_i2c_cfg.rate = I2C_MASTER_RATE_STANDARD;
558-
}
559-
else if((WireSpeed_t)ws == SPEED_FAST) {
560-
m_i2c_cfg.rate = I2C_MASTER_RATE_FAST;
561-
}
562-
else if((WireSpeed_t)ws == SPEED_VERY_FAST) {
563-
m_i2c_cfg.rate = I2C_MASTER_RATE_FASTPLUS;
564-
}
533+
m_i2c_cfg.rate = (i2c_master_rate_t)freq;
565534

535+
int clock_divisor = (R_FSP_SystemClockHzGet(BSP_FEATURE_SCI_CLOCK) / 48000000u) - 1;
536+
537+
if (is_sci) {
538+
m_sci_i2c_extend.clock_settings.clk_divisor_value = 0;
539+
m_sci_i2c_extend.clock_settings.cycles_value = 15;
540+
m_sci_i2c_extend.clock_settings.snfr_value = (1);
541+
switch (m_i2c_cfg.rate) {
542+
case I2C_MASTER_RATE_STANDARD:
543+
m_sci_i2c_extend.clock_settings.brr_value = 14;
544+
m_sci_i2c_extend.clock_settings.mddr_value = 255;
545+
m_sci_i2c_extend.clock_settings.bitrate_modulation = false;
546+
break;
547+
case I2C_MASTER_RATE_FAST:
548+
default:
549+
m_sci_i2c_extend.clock_settings.brr_value = 2;
550+
m_sci_i2c_extend.clock_settings.mddr_value = 204;
551+
m_sci_i2c_extend.clock_settings.bitrate_modulation = true;
552+
break;
553+
}
554+
} else {
555+
switch (m_i2c_cfg.rate) {
556+
case I2C_MASTER_RATE_STANDARD:
557+
m_i2c_extend.clock_settings.brl_value = 27;
558+
m_i2c_extend.clock_settings.brh_value = 26;
559+
m_i2c_extend.clock_settings.cks_value = 2 + clock_divisor;
560+
break;
561+
case I2C_MASTER_RATE_FAST:
562+
m_i2c_extend.clock_settings.brl_value = 16;
563+
m_i2c_extend.clock_settings.brh_value = 15;
564+
m_i2c_extend.clock_settings.cks_value = 0 + clock_divisor;
565+
break;
566+
#if BSP_FEATURE_IIC_FAST_MODE_PLUS
567+
case I2C_MASTER_RATE_FASTPLUS:
568+
m_i2c_extend.clock_settings.brl_value = 6;
569+
m_i2c_extend.clock_settings.brh_value = 5;
570+
m_i2c_extend.clock_settings.cks_value = 0;
571+
break;
572+
#endif
573+
}
574+
}
566575
}
567576

568577
if(init_ok) {

Diff for: libraries/Wire/Wire.h

+1-9
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,6 @@ typedef enum {
8383
ADDRESS_MODE_10_BITS
8484
} WireAddressMode_t;
8585

86-
typedef enum {
87-
SPEED_STANDARD = 0, //100 kHz
88-
SPEED_FAST, //400 kHz
89-
SPEED_VERY_FAST //1 Mhz
90-
} WireSpeed_t;
91-
92-
9386
typedef enum {
9487
WIRE_STATUS_UNSET,
9588
WIRE_STATUS_RX_COMPLETED,
@@ -103,7 +96,7 @@ typedef enum {
10396
class TwoWire : public arduino::HardwareI2C {
10497

10598
public:
106-
TwoWire(int scl_pin, int sda_pin, WireSpeed_t wp = SPEED_STANDARD, WireAddressMode_t am = ADDRESS_MODE_7_BITS, bool prefer_sci = false);
99+
TwoWire(int scl_pin, int sda_pin, WireAddressMode_t am = ADDRESS_MODE_7_BITS, bool prefer_sci = false);
107100
void begin();
108101
void begin(uint8_t);
109102
void begin(uint16_t);
@@ -172,7 +165,6 @@ class TwoWire : public arduino::HardwareI2C {
172165
bool is_master;
173166
int channel;
174167
bool is_sci;
175-
WireSpeed_t speed_mode;
176168
WireAddressMode_t address_mode;
177169

178170
unsigned int timeout;

0 commit comments

Comments
 (0)