Skip to content

Commit dcc65be

Browse files
committed
Wire: fix setClock
clock source is fixed for RA4M1, so the rates will be wrong for C33. This will be fixed in the next commit
1 parent fb6f722 commit dcc65be

File tree

2 files changed

+41
-51
lines changed

2 files changed

+41
-51
lines changed

libraries/Wire/Wire.cpp

Lines changed: 40 additions & 42 deletions
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

@@ -317,38 +319,16 @@ void TwoWire::begin(void) {
317319
m_i2c_cfg.p_callback = WireMasterCallback;
318320
}
319321

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);
326-
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;
334-
335322
m_i2c_cfg.channel = channel;
336323
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-
}
343324
m_i2c_cfg.slave = 0x00;
344325
m_i2c_cfg.addr_mode = (address_mode == ADDRESS_MODE_7_BITS) ? I2C_MASTER_ADDR_MODE_7BIT : I2C_MASTER_ADDR_MODE_10BIT;
345326
m_i2c_cfg.p_transfer_tx = NULL;
346327
m_i2c_cfg.p_transfer_rx = NULL;
347328

348329
m_i2c_cfg.p_context = &m_i2c_cfg;
349330
m_i2c_cfg.ipl = (12);
350-
351-
331+
352332
} // if(is_master) {
353333
/* -----------------------------------
354334
->>>>> SLAVE initialization
@@ -369,12 +349,6 @@ void TwoWire::begin(void) {
369349

370350
s_i2c_cfg.channel = channel;
371351
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-
}
378352
s_i2c_cfg.slave = slave_address;
379353
s_i2c_cfg.addr_mode = (address_mode == ADDRESS_MODE_7_BITS) ? I2C_SLAVE_ADDR_MODE_7BIT : I2C_SLAVE_ADDR_MODE_10BIT;
380354
s_i2c_cfg.general_call_enable = false;
@@ -544,7 +518,7 @@ uint8_t TwoWire::write_to(uint8_t address, uint8_t* data, uint8_t length, unsign
544518
}
545519

546520
/* -------------------------------------------------------------------------- */
547-
void TwoWire::setClock(uint32_t ws) {
521+
void TwoWire::setClock(uint32_t freq) {
548522
/* -------------------------------------------------------------------------- */
549523
if(init_ok && is_master) {
550524
if(m_close != nullptr) {
@@ -553,16 +527,40 @@ void TwoWire::setClock(uint32_t ws) {
553527
}
554528

555529
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;
530+
m_i2c_cfg.rate = (i2c_master_rate_t)freq;
531+
532+
if (is_sci) {
533+
switch (m_i2c_cfg.rate) {
534+
case I2C_MASTER_RATE_STANDARD:
535+
m_sci_i2c_extend.clock_settings.brr_value = 14;
536+
m_sci_i2c_extend.clock_settings.mddr_value = 255;
537+
m_sci_i2c_extend.clock_settings.bitrate_modulation = false;
538+
break;
539+
case I2C_MASTER_RATE_FAST:
540+
m_sci_i2c_extend.clock_settings.brr_value = 2;
541+
m_sci_i2c_extend.clock_settings.mddr_value = 204;
542+
m_sci_i2c_extend.clock_settings.bitrate_modulation = true;
543+
break;
544+
}
545+
} else {
546+
switch (m_i2c_cfg.rate) {
547+
case I2C_MASTER_RATE_STANDARD:
548+
m_i2c_extend.clock_settings.brl_value = 27;
549+
m_i2c_extend.clock_settings.brh_value = 26;
550+
m_i2c_extend.clock_settings.cks_value = 2;
551+
break;
552+
case I2C_MASTER_RATE_FAST:
553+
m_i2c_extend.clock_settings.brl_value = 24;
554+
m_i2c_extend.clock_settings.brh_value = 23;
555+
m_i2c_extend.clock_settings.cks_value = 0;
556+
break;
557+
case I2C_MASTER_RATE_FASTPLUS:
558+
m_i2c_extend.clock_settings.brl_value = 6;
559+
m_i2c_extend.clock_settings.brh_value = 5;
560+
m_i2c_extend.clock_settings.cks_value = 0;
561+
break;
562+
}
564563
}
565-
566564
}
567565

568566
if(init_ok) {

libraries/Wire/Wire.h

Lines changed: 1 addition & 9 deletions
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)