1
1
#include " SERCOM.h"
2
-
3
- // Constants for Clock multiplexers
4
- #define GENERIC_CLOCK_SERCOM0 (0x14ul )
5
- #define GENERIC_CLOCK_SERCOM1 (0x15ul )
6
- #define GENERIC_CLOCK_SERCOM2 (0x16ul )
7
- #define GENERIC_CLOCK_SERCOM3 (0x17ul )
8
- #define GENERIC_CLOCK_SERCOM4 (0x18ul )
9
- #define GENERIC_CLOCK_SERCOM5 (0x19ul )
2
+ #include " variant.h"
10
3
11
4
SERCOM::SERCOM (Sercom* s)
12
5
{
@@ -319,7 +312,7 @@ void SERCOM::resetWIRE()
319
312
// I2CM OR I2CS, no matter SWRST is the same bit.
320
313
321
314
// Setting the Software bit to 1
322
- sercom->I2CM .CTRLA .bit .SWRST = 0x1ul ;
315
+ sercom->I2CM .CTRLA .bit .SWRST = 1 ;
323
316
324
317
// Wait both bits Software Reset from CTRLA and SYNCBUSY are equal to 0
325
318
while (sercom->I2CM .CTRLA .bit .SWRST || sercom->I2CM .SYNCBUSY .bit .SWRST );
@@ -330,22 +323,30 @@ void SERCOM::enableWIRE()
330
323
// I2C Master and Slave modes share the ENABLE bit function.
331
324
332
325
// Enable the I²C master mode
333
- sercom->I2CM .CTRLA .bit .ENABLE = 0x1ul ;
326
+ sercom->I2CM .CTRLA .bit .ENABLE = 1 ;
334
327
335
- while ( sercom->I2CM .SYNCBUSY .bit .ENABLE != 0x0ul )
328
+ while ( sercom->I2CM .SYNCBUSY .bit .ENABLE != 0 )
336
329
{
337
330
// Waiting the enable bit from SYNCBUSY is equal to 0;
338
331
}
332
+
333
+ // Setting bus idle mode
334
+ sercom->I2CM .STATUS .bit .BUSSTATE = 1 ;
335
+
336
+ while ( sercom->I2CM .SYNCBUSY .bit .SYSOP != 0 )
337
+ {
338
+ // Wait the SYSOP bit from SYNCBUSY coming back to 0
339
+ }
339
340
}
340
341
341
342
void SERCOM::disableWIRE ()
342
343
{
343
344
// I2C Master and Slave modes share the ENABLE bit function.
344
345
345
346
// Enable the I²C master mode
346
- sercom->I2CM .CTRLA .bit .ENABLE = 0x0ul ;
347
+ sercom->I2CM .CTRLA .bit .ENABLE = 0 ;
347
348
348
- while ( sercom->I2CM .SYNCBUSY .bit .ENABLE != 0x0ul )
349
+ while ( sercom->I2CM .SYNCBUSY .bit .ENABLE != 0 )
349
350
{
350
351
// Waiting the enable bit from SYNCBUSY is equal to 0;
351
352
}
@@ -384,43 +385,35 @@ void SERCOM::initMasterWIRE( uint32_t baudrate )
384
385
resetWIRE () ;
385
386
386
387
// Set master mode and enable SCL Clock Stretch mode (stretch after ACK bit)
387
- sercom->I2CM .CTRLA .reg = SERCOM_I2CM_CTRLA_MODE ( I2C_MASTER_OPERATION ) |
388
- SERCOM_I2CM_CTRLA_SCLSM |
389
- SERCOM_I2CM_CTRLA_INACTOUT ( 3 ) ; // 205µs of Inactive TimeOut
388
+ sercom->I2CM .CTRLA .reg = SERCOM_I2CM_CTRLA_MODE ( I2C_MASTER_OPERATION )/* |
389
+ SERCOM_I2CM_CTRLA_SCLSM*/ ;
390
390
391
391
// Enable Smart mode and Quick Command
392
- sercom->I2CM .CTRLB .reg = SERCOM_I2CM_CTRLB_SMEN | SERCOM_I2CM_CTRLB_QCEN ;
392
+ // sercom->I2CM.CTRLB.reg = SERCOM_I2CM_CTRLB_SMEN /* | SERCOM_I2CM_CTRLB_QCEN*/ ;
393
393
394
- // Setting bus idle mode
395
- sercom->I2CM .STATUS .bit .BUSSTATE = WIRE_IDLE_STATE ;
396
-
397
- while ( sercom->I2CM .SYNCBUSY .bit .SYSOP != 0 )
398
- {
399
- // Wait the SYSOP bit from SYNCBUSY coming back to 0
400
- }
401
394
402
395
// Enable all interrupts
403
- // sercom->I2CM.INTENSET.reg = SERCOM_I2CM_INTENSET_MB | SERCOM_I2CM_INTENSET_SB | SERCOM_I2CM_INTENSET_ERROR ;
396
+ // sercom->I2CM.INTENSET.reg = SERCOM_I2CM_INTENSET_MB | SERCOM_I2CM_INTENSET_SB | SERCOM_I2CM_INTENSET_ERROR ;
404
397
405
398
// Synchronous arithmetic baudrate
406
399
sercom->I2CM .BAUD .bit .BAUD = SystemCoreClock / ( 2 * baudrate) - 1 ;
407
400
}
408
401
409
- void SERCOM::sendNackBitWIRE ( void )
402
+ void SERCOM::prepareNackBitWIRE ( void )
410
403
{
411
404
// Send a NACK
412
- sercom->I2CM .CTRLB .bit .ACKACT = 0x1ul ;
405
+ sercom->I2CM .CTRLB .bit .ACKACT = 1 ;
413
406
}
414
407
415
- void SERCOM::sendAckBitWIRE ( void )
408
+ void SERCOM::prepareAckBitWIRE ( void )
416
409
{
417
410
// Send an ACK
418
- sercom->I2CM .CTRLB .bit .ACKACT = 0x0ul ;
411
+ sercom->I2CM .CTRLB .bit .ACKACT = 0 ;
419
412
}
420
413
421
- void SERCOM::sendStopBitWIRE ( void )
414
+ void SERCOM::prepareCommandBitsWire (SercomMasterCommandWire cmd )
422
415
{
423
- sercom->I2CM .CTRLB .bit .CMD = WIRE_MASTER_ACT_STOP ;
416
+ sercom->I2CM .CTRLB .bit .CMD = cmd ;
424
417
425
418
while (sercom->I2CM .SYNCBUSY .bit .SYSOP )
426
419
{
@@ -557,15 +550,12 @@ uint8_t SERCOM::readDataWIRE( void )
557
550
{
558
551
if (isMasterWIRE ())
559
552
{
560
- while ( sercom->I2CM .INTFLAG .bit .SB == 0x0ul || sercom->I2CM .SYNCBUSY .bit .SYSOP == 0x1ul )
553
+ while ( sercom->I2CM .INTFLAG .bit .SB == 0 || sercom->I2CM .STATUS .bit .CLKHOLD == 0 )
561
554
{
562
- // Waiting complete receive & synchronization finished
555
+ // Waiting complete receive, Clock holding & synchronization finished
563
556
}
564
557
565
- // Clear the Slave on Bus flag
566
- sercom->I2CM .INTFLAG .bit .SB = 0x1 ;
567
-
568
- return sercom->I2CM .DATA .reg ;
558
+ return sercom->I2CM .DATA .bit .DATA ;
569
559
}
570
560
else
571
561
{
@@ -581,32 +571,32 @@ void SERCOM::initClockNVIC( void )
581
571
582
572
if (sercom == SERCOM0)
583
573
{
584
- clockId = GENERIC_CLOCK_SERCOM0 ;
574
+ clockId = GCM_SERCOM0_CORE ;
585
575
IdNvic = SERCOM0_IRQn;
586
576
}
587
577
else if (sercom == SERCOM1)
588
578
{
589
- clockId = GENERIC_CLOCK_SERCOM1 ;
579
+ clockId = GCM_SERCOM1_CORE ;
590
580
IdNvic = SERCOM1_IRQn;
591
581
}
592
582
else if (sercom == SERCOM2)
593
583
{
594
- clockId = GENERIC_CLOCK_SERCOM2 ;
584
+ clockId = GCM_SERCOM2_CORE ;
595
585
IdNvic = SERCOM2_IRQn;
596
586
}
597
587
else if (sercom == SERCOM3)
598
588
{
599
- clockId = GENERIC_CLOCK_SERCOM3 ;
589
+ clockId = GCM_SERCOM3_CORE ;
600
590
IdNvic = SERCOM3_IRQn;
601
591
}
602
592
else if (sercom == SERCOM4)
603
593
{
604
- clockId = GENERIC_CLOCK_SERCOM4 ;
594
+ clockId = GCM_SERCOM4_CORE ;
605
595
IdNvic = SERCOM4_IRQn;
606
596
}
607
597
else if (sercom == SERCOM5)
608
598
{
609
- clockId = GENERIC_CLOCK_SERCOM5 ;
599
+ clockId = GCM_SERCOM5_CORE ;
610
600
IdNvic = SERCOM5_IRQn;
611
601
}
612
602
0 commit comments