@@ -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) {
0 commit comments