@@ -184,13 +184,12 @@ void TwoWire::WireSlaveCallback(i2c_slave_callback_args_t *arg) {
184
184
185
185
186
186
/* -------------------------------------------------------------------------- */
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*/ ) :
188
188
scl_pin(scl),
189
189
sda_pin(sda),
190
190
init_ok(false ),
191
191
is_master(true ),
192
192
is_sci(false ),
193
- speed_mode(wp),
194
193
address_mode(am),
195
194
timeout(1000 ),
196
195
transmission_begun(false ),
@@ -281,11 +280,14 @@ void TwoWire::begin(void) {
281
280
init_ok &= cfg_pins (max_index);
282
281
283
282
if (init_ok) {
284
-
283
+
285
284
/* -----------------------------------
286
285
->>>>> MASTER initialization
287
286
* ----------------------------------- */
288
287
if (is_master) {
288
+
289
+ setClock (I2C_MASTER_RATE_STANDARD);
290
+
289
291
if (is_sci) {
290
292
TwoWire::g_SCIWires[channel] = this ;
291
293
@@ -315,40 +317,21 @@ void TwoWire::begin(void) {
315
317
316
318
m_i2c_cfg.p_extend = &m_i2c_extend;
317
319
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 );
326
320
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
+ }
334
324
335
325
m_i2c_cfg.channel = channel;
336
326
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
- }
343
327
m_i2c_cfg.slave = 0x00 ;
344
328
m_i2c_cfg.addr_mode = (address_mode == ADDRESS_MODE_7_BITS) ? I2C_MASTER_ADDR_MODE_7BIT : I2C_MASTER_ADDR_MODE_10BIT;
345
329
m_i2c_cfg.p_transfer_tx = NULL ;
346
330
m_i2c_cfg.p_transfer_rx = NULL ;
347
-
331
+
348
332
m_i2c_cfg.p_context = &m_i2c_cfg;
349
333
m_i2c_cfg.ipl = (12 );
350
-
351
-
334
+
352
335
} // if(is_master) {
353
336
/* -----------------------------------
354
337
->>>>> SLAVE initialization
@@ -369,12 +352,6 @@ void TwoWire::begin(void) {
369
352
370
353
s_i2c_cfg.channel = channel;
371
354
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
- }
378
355
s_i2c_cfg.slave = slave_address;
379
356
s_i2c_cfg.addr_mode = (address_mode == ADDRESS_MODE_7_BITS) ? I2C_SLAVE_ADDR_MODE_7BIT : I2C_SLAVE_ADDR_MODE_10BIT;
380
357
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
483
460
}
484
461
if (err == FSP_SUCCESS) {
485
462
if (m_read != nullptr ) {
463
+ bus_status = WIRE_STATUS_UNSET;
486
464
err = m_read (&m_i2c_ctrl,data,length,!sendStop);
487
465
}
488
466
}
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
+
493
470
}
494
471
}
495
472
@@ -511,13 +488,13 @@ uint8_t TwoWire::write_to(uint8_t address, uint8_t* data, uint8_t length, unsign
511
488
}
512
489
if (err == FSP_SUCCESS) {
513
490
if (m_write != nullptr ) {
491
+ bus_status = WIRE_STATUS_UNSET;
514
492
err = m_write (&m_i2c_ctrl,data,length,!sendStop);
515
493
}
516
494
}
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
+
521
498
}
522
499
523
500
if (err != FSP_SUCCESS) {
@@ -544,7 +521,7 @@ uint8_t TwoWire::write_to(uint8_t address, uint8_t* data, uint8_t length, unsign
544
521
}
545
522
546
523
/* -------------------------------------------------------------------------- */
547
- void TwoWire::setClock (uint32_t ws ) {
524
+ void TwoWire::setClock (uint32_t freq ) {
548
525
/* -------------------------------------------------------------------------- */
549
526
if (init_ok && is_master) {
550
527
if (m_close != nullptr ) {
@@ -553,16 +530,48 @@ void TwoWire::setClock(uint32_t ws) {
553
530
}
554
531
555
532
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;
565
534
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
+ }
566
575
}
567
576
568
577
if (init_ok) {
0 commit comments