Browse Source

Implemented M155 and M156, a generic TWI/I2C interface for Marlin

jbrazio 8 years ago
parent
commit
dd3a06a95a

+ 32
- 0
Marlin/Configuration_adv.h View File

@@ -653,6 +653,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
653 653
 
654 654
 #endif
655 655
 
656
+/**
657
+ * TWI/I2C BUS
658
+ *
659
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
660
+ * machines. Enabling this will allow you to send and receive I2C data from slave
661
+ * devices on the bus.
662
+ *
663
+ * ; Example #1
664
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
665
+ * ; It uses multiple M155 commands with one B<base 10> arg
666
+ * M155 A63  ; Target slave address
667
+ * M155 B77  ; M
668
+ * M155 B97  ; a
669
+ * M155 B114 ; r
670
+ * M155 B108 ; l
671
+ * M155 B105 ; i
672
+ * M155 B110 ; n
673
+ * M155 S1   ; Send the current buffer
674
+ *
675
+ * ; Example #2
676
+ * ; Request 6 bytes from slave device with address 0x63
677
+ * M156 A63 B5
678
+ *
679
+ * ; Example #3
680
+ * ; Example serial output of a M156 request
681
+ * echo:i2c-reply: from:63 bytes:5 data:hello
682
+ */
683
+
684
+// @section i2cbus
685
+
686
+//#define EXPERIMENTAL_I2CBUS
687
+
656 688
 #include "Conditionals.h"
657 689
 #include "SanityCheck.h"
658 690
 

+ 71
- 0
Marlin/Marlin_main.cpp View File

@@ -77,6 +77,10 @@
77 77
   #include "stepper_dac.h"
78 78
 #endif
79 79
 
80
+#if ENABLED(EXPERIMENTAL_I2CBUS)
81
+  #include "twibus.h"
82
+#endif
83
+
80 84
 /**
81 85
  * Look here for descriptions of G-codes:
82 86
  *  - http://linuxcnc.org/handbook/gcode/g-code.html
@@ -248,6 +252,10 @@
248 252
   CardReader card;
249 253
 #endif
250 254
 
255
+#if ENABLED(EXPERIMENTAL_I2CBUS)
256
+  TWIBus i2c;
257
+#endif
258
+
251 259
 bool Running = true;
252 260
 
253 261
 uint8_t marlin_debug_flags = DEBUG_NONE;
@@ -4771,6 +4779,57 @@ inline void gcode_M121() { enable_endstops_globally(false); }
4771 4779
 
4772 4780
 #endif // BLINKM
4773 4781
 
4782
+#if ENABLED(EXPERIMENTAL_I2CBUS)
4783
+
4784
+  /**
4785
+   * M155: Send data to a I2C slave device
4786
+   *
4787
+   * This is a PoC, the formating and arguments for the GCODE will
4788
+   * change to be more compatible, the current proposal is:
4789
+   *
4790
+   *  M155 A<slave device address base 10> ; Sets the I2C slave address the data will be sent to
4791
+   *
4792
+   *  M155 B<byte-1 value in base 10>
4793
+   *  M155 B<byte-2 value in base 10>
4794
+   *  M155 B<byte-3 value in base 10>
4795
+   *
4796
+   *  M155 S1 ; Send the buffered data and reset the buffer
4797
+   *  M155 R1 ; Reset the buffer without sending data
4798
+   *
4799
+   */
4800
+  inline void gcode_M155() {
4801
+    // Set the target address
4802
+    if (code_seen('A'))
4803
+      i2c.address((uint8_t) code_value_short());
4804
+
4805
+    // Add a new byte to the buffer
4806
+    else if (code_seen('B'))
4807
+      i2c.addbyte((int) code_value_short());
4808
+
4809
+    // Flush the buffer to the bus
4810
+    else if (code_seen('S')) i2c.send();
4811
+
4812
+    // Reset and rewind the buffer
4813
+    else if (code_seen('R')) i2c.reset();
4814
+  }
4815
+
4816
+  /**
4817
+   * M156: Request X bytes from I2C slave device
4818
+   *
4819
+   * Usage: M156 A<slave device address base 10> B<number of bytes>
4820
+   */
4821
+  inline void gcode_M156() {
4822
+    uint8_t addr = code_seen('A') ? code_value_short() : 0;
4823
+    int bytes    = code_seen('B') ? code_value_short() : 0;
4824
+
4825
+    if (addr && bytes) {
4826
+      i2c.address(addr);
4827
+      i2c.reqbytes(bytes);
4828
+    }
4829
+  }
4830
+
4831
+#endif //EXPERIMENTAL_I2CBUS
4832
+
4774 4833
 /**
4775 4834
  * M200: Set filament diameter and set E axis units to cubic millimeters
4776 4835
  *
@@ -6439,6 +6498,18 @@ void process_next_command() {
6439 6498
 
6440 6499
       #endif //BLINKM
6441 6500
 
6501
+      #if ENABLED(EXPERIMENTAL_I2CBUS)
6502
+
6503
+        case 155:
6504
+          gcode_M155();
6505
+          break;
6506
+
6507
+        case 156:
6508
+          gcode_M156();
6509
+          break;
6510
+
6511
+      #endif //EXPERIMENTAL_I2CBUS
6512
+
6442 6513
       case 200: // M200 D<millimeters> set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters).
6443 6514
         gcode_M200();
6444 6515
         break;

+ 32
- 0
Marlin/example_configurations/Felix/Configuration_adv.h View File

@@ -653,6 +653,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
653 653
 
654 654
 #endif
655 655
 
656
+/**
657
+ * TWI/I2C BUS
658
+ *
659
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
660
+ * machines. Enabling this will allow you to send and receive I2C data from slave
661
+ * devices on the bus.
662
+ *
663
+ * ; Example #1
664
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
665
+ * ; It uses multiple M155 commands with one B<base 10> arg
666
+ * M155 A63  ; Target slave address
667
+ * M155 B77  ; M
668
+ * M155 B97  ; a
669
+ * M155 B114 ; r
670
+ * M155 B108 ; l
671
+ * M155 B105 ; i
672
+ * M155 B110 ; n
673
+ * M155 S1   ; Send the current buffer
674
+ *
675
+ * ; Example #2
676
+ * ; Request 6 bytes from slave device with address 0x63
677
+ * M156 A63 B5
678
+ *
679
+ * ; Example #3
680
+ * ; Example serial output of a M156 request
681
+ * echo:i2c-reply: from:63 bytes:5 data:hello
682
+ */
683
+
684
+// @section i2cbus
685
+
686
+//#define EXPERIMENTAL_I2CBUS
687
+
656 688
 #include "Conditionals.h"
657 689
 #include "SanityCheck.h"
658 690
 

+ 32
- 0
Marlin/example_configurations/Hephestos/Configuration_adv.h View File

@@ -653,6 +653,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
653 653
 
654 654
 #endif
655 655
 
656
+/**
657
+ * TWI/I2C BUS
658
+ *
659
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
660
+ * machines. Enabling this will allow you to send and receive I2C data from slave
661
+ * devices on the bus.
662
+ *
663
+ * ; Example #1
664
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
665
+ * ; It uses multiple M155 commands with one B<base 10> arg
666
+ * M155 A63  ; Target slave address
667
+ * M155 B77  ; M
668
+ * M155 B97  ; a
669
+ * M155 B114 ; r
670
+ * M155 B108 ; l
671
+ * M155 B105 ; i
672
+ * M155 B110 ; n
673
+ * M155 S1   ; Send the current buffer
674
+ *
675
+ * ; Example #2
676
+ * ; Request 6 bytes from slave device with address 0x63
677
+ * M156 A63 B5
678
+ *
679
+ * ; Example #3
680
+ * ; Example serial output of a M156 request
681
+ * echo:i2c-reply: from:63 bytes:5 data:hello
682
+ */
683
+
684
+// @section i2cbus
685
+
686
+//#define EXPERIMENTAL_I2CBUS
687
+
656 688
 #include "Conditionals.h"
657 689
 #include "SanityCheck.h"
658 690
 

+ 32
- 0
Marlin/example_configurations/Hephestos_2/Configuration_adv.h View File

@@ -653,6 +653,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
653 653
 
654 654
 #endif
655 655
 
656
+/**
657
+ * TWI/I2C BUS
658
+ *
659
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
660
+ * machines. Enabling this will allow you to send and receive I2C data from slave
661
+ * devices on the bus.
662
+ *
663
+ * ; Example #1
664
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
665
+ * ; It uses multiple M155 commands with one B<base 10> arg
666
+ * M155 A63  ; Target slave address
667
+ * M155 B77  ; M
668
+ * M155 B97  ; a
669
+ * M155 B114 ; r
670
+ * M155 B108 ; l
671
+ * M155 B105 ; i
672
+ * M155 B110 ; n
673
+ * M155 S1   ; Send the current buffer
674
+ *
675
+ * ; Example #2
676
+ * ; Request 6 bytes from slave device with address 0x63
677
+ * M156 A63 B5
678
+ *
679
+ * ; Example #3
680
+ * ; Example serial output of a M156 request
681
+ * echo:i2c-reply: from:63 bytes:5 data:hello
682
+ */
683
+
684
+// @section i2cbus
685
+
686
+//#define EXPERIMENTAL_I2CBUS
687
+
656 688
 #include "Conditionals.h"
657 689
 #include "SanityCheck.h"
658 690
 

+ 32
- 0
Marlin/example_configurations/K8200/Configuration_adv.h View File

@@ -659,6 +659,38 @@ const unsigned int dropsegments = 2; //everything with less than this number of
659 659
 
660 660
 #endif
661 661
 
662
+/**
663
+ * TWI/I2C BUS
664
+ *
665
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
666
+ * machines. Enabling this will allow you to send and receive I2C data from slave
667
+ * devices on the bus.
668
+ *
669
+ * ; Example #1
670
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
671
+ * ; It uses multiple M155 commands with one B<base 10> arg
672
+ * M155 A63  ; Target slave address
673
+ * M155 B77  ; M
674
+ * M155 B97  ; a
675
+ * M155 B114 ; r
676
+ * M155 B108 ; l
677
+ * M155 B105 ; i
678
+ * M155 B110 ; n
679
+ * M155 S1   ; Send the current buffer
680
+ *
681
+ * ; Example #2
682
+ * ; Request 6 bytes from slave device with address 0x63
683
+ * M156 A63 B5
684
+ *
685
+ * ; Example #3
686
+ * ; Example serial output of a M156 request
687
+ * echo:i2c-reply: from:63 bytes:5 data:hello
688
+ */
689
+
690
+// @section i2cbus
691
+
692
+//#define EXPERIMENTAL_I2CBUS
693
+
662 694
 #include "Conditionals.h"
663 695
 #include "SanityCheck.h"
664 696
 

+ 32
- 0
Marlin/example_configurations/RigidBot/Configuration_adv.h View File

@@ -653,6 +653,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
653 653
 
654 654
 #endif
655 655
 
656
+/**
657
+ * TWI/I2C BUS
658
+ *
659
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
660
+ * machines. Enabling this will allow you to send and receive I2C data from slave
661
+ * devices on the bus.
662
+ *
663
+ * ; Example #1
664
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
665
+ * ; It uses multiple M155 commands with one B<base 10> arg
666
+ * M155 A63  ; Target slave address
667
+ * M155 B77  ; M
668
+ * M155 B97  ; a
669
+ * M155 B114 ; r
670
+ * M155 B108 ; l
671
+ * M155 B105 ; i
672
+ * M155 B110 ; n
673
+ * M155 S1   ; Send the current buffer
674
+ *
675
+ * ; Example #2
676
+ * ; Request 6 bytes from slave device with address 0x63
677
+ * M156 A63 B5
678
+ *
679
+ * ; Example #3
680
+ * ; Example serial output of a M156 request
681
+ * echo:i2c-reply: from:63 bytes:5 data:hello
682
+ */
683
+
684
+// @section i2cbus
685
+
686
+//#define EXPERIMENTAL_I2CBUS
687
+
656 688
 #include "Conditionals.h"
657 689
 #include "SanityCheck.h"
658 690
 

+ 32
- 0
Marlin/example_configurations/SCARA/Configuration_adv.h View File

@@ -653,6 +653,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
653 653
 
654 654
 #endif
655 655
 
656
+/**
657
+ * TWI/I2C BUS
658
+ *
659
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
660
+ * machines. Enabling this will allow you to send and receive I2C data from slave
661
+ * devices on the bus.
662
+ *
663
+ * ; Example #1
664
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
665
+ * ; It uses multiple M155 commands with one B<base 10> arg
666
+ * M155 A63  ; Target slave address
667
+ * M155 B77  ; M
668
+ * M155 B97  ; a
669
+ * M155 B114 ; r
670
+ * M155 B108 ; l
671
+ * M155 B105 ; i
672
+ * M155 B110 ; n
673
+ * M155 S1   ; Send the current buffer
674
+ *
675
+ * ; Example #2
676
+ * ; Request 6 bytes from slave device with address 0x63
677
+ * M156 A63 B5
678
+ *
679
+ * ; Example #3
680
+ * ; Example serial output of a M156 request
681
+ * echo:i2c-reply: from:63 bytes:5 data:hello
682
+ */
683
+
684
+// @section i2cbus
685
+
686
+//#define EXPERIMENTAL_I2CBUS
687
+
656 688
 #include "Conditionals.h"
657 689
 #include "SanityCheck.h"
658 690
 

+ 32
- 0
Marlin/example_configurations/TAZ4/Configuration_adv.h View File

@@ -661,6 +661,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
661 661
 
662 662
 #endif
663 663
 
664
+/**
665
+ * TWI/I2C BUS
666
+ *
667
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
668
+ * machines. Enabling this will allow you to send and receive I2C data from slave
669
+ * devices on the bus.
670
+ *
671
+ * ; Example #1
672
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
673
+ * ; It uses multiple M155 commands with one B<base 10> arg
674
+ * M155 A63  ; Target slave address
675
+ * M155 B77  ; M
676
+ * M155 B97  ; a
677
+ * M155 B114 ; r
678
+ * M155 B108 ; l
679
+ * M155 B105 ; i
680
+ * M155 B110 ; n
681
+ * M155 S1   ; Send the current buffer
682
+ *
683
+ * ; Example #2
684
+ * ; Request 6 bytes from slave device with address 0x63
685
+ * M156 A63 B5
686
+ *
687
+ * ; Example #3
688
+ * ; Example serial output of a M156 request
689
+ * echo:i2c-reply: from:63 bytes:5 data:hello
690
+ */
691
+
692
+// @section i2cbus
693
+
694
+//#define EXPERIMENTAL_I2CBUS
695
+
664 696
 #include "Conditionals.h"
665 697
 #include "SanityCheck.h"
666 698
 

+ 32
- 0
Marlin/example_configurations/WITBOX/Configuration_adv.h View File

@@ -653,6 +653,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
653 653
 
654 654
 #endif
655 655
 
656
+/**
657
+ * TWI/I2C BUS
658
+ *
659
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
660
+ * machines. Enabling this will allow you to send and receive I2C data from slave
661
+ * devices on the bus.
662
+ *
663
+ * ; Example #1
664
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
665
+ * ; It uses multiple M155 commands with one B<base 10> arg
666
+ * M155 A63  ; Target slave address
667
+ * M155 B77  ; M
668
+ * M155 B97  ; a
669
+ * M155 B114 ; r
670
+ * M155 B108 ; l
671
+ * M155 B105 ; i
672
+ * M155 B110 ; n
673
+ * M155 S1   ; Send the current buffer
674
+ *
675
+ * ; Example #2
676
+ * ; Request 6 bytes from slave device with address 0x63
677
+ * M156 A63 B5
678
+ *
679
+ * ; Example #3
680
+ * ; Example serial output of a M156 request
681
+ * echo:i2c-reply: from:63 bytes:5 data:hello
682
+ */
683
+
684
+// @section i2cbus
685
+
686
+//#define EXPERIMENTAL_I2CBUS
687
+
656 688
 #include "Conditionals.h"
657 689
 #include "SanityCheck.h"
658 690
 

+ 32
- 0
Marlin/example_configurations/delta/biv2.5/Configuration_adv.h View File

@@ -655,6 +655,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
655 655
 
656 656
 #endif
657 657
 
658
+/**
659
+ * TWI/I2C BUS
660
+ *
661
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
662
+ * machines. Enabling this will allow you to send and receive I2C data from slave
663
+ * devices on the bus.
664
+ *
665
+ * ; Example #1
666
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
667
+ * ; It uses multiple M155 commands with one B<base 10> arg
668
+ * M155 A63  ; Target slave address
669
+ * M155 B77  ; M
670
+ * M155 B97  ; a
671
+ * M155 B114 ; r
672
+ * M155 B108 ; l
673
+ * M155 B105 ; i
674
+ * M155 B110 ; n
675
+ * M155 S1   ; Send the current buffer
676
+ *
677
+ * ; Example #2
678
+ * ; Request 6 bytes from slave device with address 0x63
679
+ * M156 A63 B5
680
+ *
681
+ * ; Example #3
682
+ * ; Example serial output of a M156 request
683
+ * echo:i2c-reply: from:63 bytes:5 data:hello
684
+ */
685
+
686
+// @section i2cbus
687
+
688
+//#define EXPERIMENTAL_I2CBUS
689
+
658 690
 #include "Conditionals.h"
659 691
 #include "SanityCheck.h"
660 692
 

+ 32
- 0
Marlin/example_configurations/delta/generic/Configuration_adv.h View File

@@ -655,6 +655,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
655 655
 
656 656
 #endif
657 657
 
658
+/**
659
+ * TWI/I2C BUS
660
+ *
661
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
662
+ * machines. Enabling this will allow you to send and receive I2C data from slave
663
+ * devices on the bus.
664
+ *
665
+ * ; Example #1
666
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
667
+ * ; It uses multiple M155 commands with one B<base 10> arg
668
+ * M155 A63  ; Target slave address
669
+ * M155 B77  ; M
670
+ * M155 B97  ; a
671
+ * M155 B114 ; r
672
+ * M155 B108 ; l
673
+ * M155 B105 ; i
674
+ * M155 B110 ; n
675
+ * M155 S1   ; Send the current buffer
676
+ *
677
+ * ; Example #2
678
+ * ; Request 6 bytes from slave device with address 0x63
679
+ * M156 A63 B5
680
+ *
681
+ * ; Example #3
682
+ * ; Example serial output of a M156 request
683
+ * echo:i2c-reply: from:63 bytes:5 data:hello
684
+ */
685
+
686
+// @section i2cbus
687
+
688
+//#define EXPERIMENTAL_I2CBUS
689
+
658 690
 #include "Conditionals.h"
659 691
 #include "SanityCheck.h"
660 692
 

+ 32
- 0
Marlin/example_configurations/delta/kossel_mini/Configuration_adv.h View File

@@ -654,6 +654,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
654 654
 
655 655
 #endif
656 656
 
657
+/**
658
+ * TWI/I2C BUS
659
+ *
660
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
661
+ * machines. Enabling this will allow you to send and receive I2C data from slave
662
+ * devices on the bus.
663
+ *
664
+ * ; Example #1
665
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
666
+ * ; It uses multiple M155 commands with one B<base 10> arg
667
+ * M155 A63  ; Target slave address
668
+ * M155 B77  ; M
669
+ * M155 B97  ; a
670
+ * M155 B114 ; r
671
+ * M155 B108 ; l
672
+ * M155 B105 ; i
673
+ * M155 B110 ; n
674
+ * M155 S1   ; Send the current buffer
675
+ *
676
+ * ; Example #2
677
+ * ; Request 6 bytes from slave device with address 0x63
678
+ * M156 A63 B5
679
+ *
680
+ * ; Example #3
681
+ * ; Example serial output of a M156 request
682
+ * echo:i2c-reply: from:63 bytes:5 data:hello
683
+ */
684
+
685
+// @section i2cbus
686
+
687
+//#define EXPERIMENTAL_I2CBUS
688
+
657 689
 #include "Conditionals.h"
658 690
 #include "SanityCheck.h"
659 691
 

+ 32
- 0
Marlin/example_configurations/delta/kossel_pro/Configuration_adv.h View File

@@ -659,6 +659,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
659 659
 
660 660
 #endif
661 661
 
662
+/**
663
+ * TWI/I2C BUS
664
+ *
665
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
666
+ * machines. Enabling this will allow you to send and receive I2C data from slave
667
+ * devices on the bus.
668
+ *
669
+ * ; Example #1
670
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
671
+ * ; It uses multiple M155 commands with one B<base 10> arg
672
+ * M155 A63  ; Target slave address
673
+ * M155 B77  ; M
674
+ * M155 B97  ; a
675
+ * M155 B114 ; r
676
+ * M155 B108 ; l
677
+ * M155 B105 ; i
678
+ * M155 B110 ; n
679
+ * M155 S1   ; Send the current buffer
680
+ *
681
+ * ; Example #2
682
+ * ; Request 6 bytes from slave device with address 0x63
683
+ * M156 A63 B5
684
+ *
685
+ * ; Example #3
686
+ * ; Example serial output of a M156 request
687
+ * echo:i2c-reply: from:63 bytes:5 data:hello
688
+ */
689
+
690
+// @section i2cbus
691
+
692
+//#define EXPERIMENTAL_I2CBUS
693
+
662 694
 #include "Conditionals.h"
663 695
 #include "SanityCheck.h"
664 696
 

+ 32
- 0
Marlin/example_configurations/delta/kossel_xl/Configuration_adv.h View File

@@ -653,6 +653,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
653 653
 
654 654
 #endif
655 655
 
656
+/**
657
+ * TWI/I2C BUS
658
+ *
659
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
660
+ * machines. Enabling this will allow you to send and receive I2C data from slave
661
+ * devices on the bus.
662
+ *
663
+ * ; Example #1
664
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
665
+ * ; It uses multiple M155 commands with one B<base 10> arg
666
+ * M155 A63  ; Target slave address
667
+ * M155 B77  ; M
668
+ * M155 B97  ; a
669
+ * M155 B114 ; r
670
+ * M155 B108 ; l
671
+ * M155 B105 ; i
672
+ * M155 B110 ; n
673
+ * M155 S1   ; Send the current buffer
674
+ *
675
+ * ; Example #2
676
+ * ; Request 6 bytes from slave device with address 0x63
677
+ * M156 A63 B5
678
+ *
679
+ * ; Example #3
680
+ * ; Example serial output of a M156 request
681
+ * echo:i2c-reply: from:63 bytes:5 data:hello
682
+ */
683
+
684
+// @section i2cbus
685
+
686
+//#define EXPERIMENTAL_I2CBUS
687
+
656 688
 #include "Conditionals.h"
657 689
 #include "SanityCheck.h"
658 690
 

+ 32
- 0
Marlin/example_configurations/makibox/Configuration_adv.h View File

@@ -653,6 +653,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
653 653
 
654 654
 #endif
655 655
 
656
+/**
657
+ * TWI/I2C BUS
658
+ *
659
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
660
+ * machines. Enabling this will allow you to send and receive I2C data from slave
661
+ * devices on the bus.
662
+ *
663
+ * ; Example #1
664
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
665
+ * ; It uses multiple M155 commands with one B<base 10> arg
666
+ * M155 A63  ; Target slave address
667
+ * M155 B77  ; M
668
+ * M155 B97  ; a
669
+ * M155 B114 ; r
670
+ * M155 B108 ; l
671
+ * M155 B105 ; i
672
+ * M155 B110 ; n
673
+ * M155 S1   ; Send the current buffer
674
+ *
675
+ * ; Example #2
676
+ * ; Request 6 bytes from slave device with address 0x63
677
+ * M156 A63 B5
678
+ *
679
+ * ; Example #3
680
+ * ; Example serial output of a M156 request
681
+ * echo:i2c-reply: from:63 bytes:5 data:hello
682
+ */
683
+
684
+// @section i2cbus
685
+
686
+//#define EXPERIMENTAL_I2CBUS
687
+
656 688
 #include "Conditionals.h"
657 689
 #include "SanityCheck.h"
658 690
 

+ 32
- 0
Marlin/example_configurations/tvrrug/Round2/Configuration_adv.h View File

@@ -653,6 +653,38 @@ const unsigned int dropsegments = 5; //everything with less than this number of
653 653
 
654 654
 #endif
655 655
 
656
+/**
657
+ * TWI/I2C BUS
658
+ *
659
+ * This feature is an EXPERIMENTAL feature so it shall not be used on production
660
+ * machines. Enabling this will allow you to send and receive I2C data from slave
661
+ * devices on the bus.
662
+ *
663
+ * ; Example #1
664
+ * ; This macro send the string "Marlin" to the slave device with address 0x63
665
+ * ; It uses multiple M155 commands with one B<base 10> arg
666
+ * M155 A63  ; Target slave address
667
+ * M155 B77  ; M
668
+ * M155 B97  ; a
669
+ * M155 B114 ; r
670
+ * M155 B108 ; l
671
+ * M155 B105 ; i
672
+ * M155 B110 ; n
673
+ * M155 S1   ; Send the current buffer
674
+ *
675
+ * ; Example #2
676
+ * ; Request 6 bytes from slave device with address 0x63
677
+ * M156 A63 B5
678
+ *
679
+ * ; Example #3
680
+ * ; Example serial output of a M156 request
681
+ * echo:i2c-reply: from:63 bytes:5 data:hello
682
+ */
683
+
684
+// @section i2cbus
685
+
686
+//#define EXPERIMENTAL_I2CBUS
687
+
656 688
 #include "Conditionals.h"
657 689
 #include "SanityCheck.h"
658 690
 

+ 104
- 0
Marlin/twibus.cpp View File

@@ -0,0 +1,104 @@
1
+/*
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+#include "Marlin.h"
24
+
25
+#if ENABLED(EXPERIMENTAL_I2CBUS)
26
+
27
+#include "twibus.h"
28
+
29
+#include <Wire.h>
30
+
31
+TWIBus::twibus() {
32
+  Wire.begin(); // We use no address so we will join the BUS as the master
33
+  this->reset();
34
+}
35
+
36
+void TWIBus::reset() {
37
+  this->addr = 0;
38
+  this->buffer_s = 0;
39
+  this->buffer[0] = 0x00;
40
+}
41
+
42
+void TWIBus::address(uint8_t addr) {
43
+  this->addr = addr;
44
+
45
+  if (DEBUGGING(INFO)) {
46
+    SERIAL_ECHOPAIR("TWIBus::sendto: ", this->addr);
47
+    SERIAL_EOL;
48
+  }
49
+}
50
+
51
+void TWIBus::addbyte(char c) {
52
+  if (buffer_s >= sizeof(this->buffer)) return;
53
+  this->buffer[this->buffer_s++] = c;
54
+
55
+  if (DEBUGGING(INFO)) {
56
+    SERIAL_ECHOPAIR("TWIBus::addbyte: ", this->buffer[this->buffer_s -1]);
57
+    SERIAL_EOL;
58
+  }
59
+}
60
+
61
+void TWIBus::send() {
62
+  if (!this->addr) return;
63
+  if (DEBUGGING(INFO)) SERIAL_ECHOLNPGM("TWIBus::send()");
64
+
65
+  Wire.beginTransmission(this->addr);
66
+  Wire.write(this->buffer, this->buffer_s);
67
+  Wire.endTransmission();
68
+
69
+    // Reset the buffer after sending the data
70
+  this->reset();
71
+}
72
+
73
+void TWIBus::reqbytes(uint8_t bytes) {
74
+  if (!this->addr) return;
75
+  if (DEBUGGING(INFO)) {
76
+    SERIAL_ECHOPAIR("TWIBus::reqbytes(): ", bytes);
77
+    SERIAL_EOL;
78
+  }
79
+
80
+  millis_t t = millis();
81
+  Wire.requestFrom(this->addr, bytes);
82
+
83
+    // requestFrom() is a blocking function
84
+  while (Wire.available() < bytes) {
85
+    if (millis() - t >= this->timeout) break;
86
+    else continue;
87
+  }
88
+
89
+  SERIAL_ECHO_START;
90
+  SERIAL_ECHOPAIR("i2c-reply: from:", this->addr);
91
+  SERIAL_ECHOPAIR(" bytes:", Wire.available());
92
+  SERIAL_ECHOPGM (" data:");
93
+
94
+    // Protect against buffer overflows if the number of received bytes
95
+    // is less than the number of requested bytes
96
+  uint8_t wba = Wire.available();
97
+  for (int i = 0; i < wba; i++) SERIAL_CHAR(Wire.read());
98
+  SERIAL_EOL;
99
+
100
+  // Reset the buffer after sending the data
101
+  this->reset();
102
+}
103
+
104
+#endif //EXPERIMENTAL_I2CBUS

+ 122
- 0
Marlin/twibus.h View File

@@ -0,0 +1,122 @@
1
+/*
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+#ifndef TWIBUS_H
24
+#define TWIBUS_H
25
+
26
+/**
27
+ * TWIBUS class
28
+ *
29
+ * This class implements a wrapper around the two wire (I2C) bus, it allows
30
+ * Marlin to send and request data from any slave device on the bus. This is
31
+ * an experimental feature and it's inner workings as well as public facing
32
+ * interface are prune to change in the future.
33
+ *
34
+ * The two main consumers of this class are M155 and M156, where M155 allows
35
+ * Marlin to send a I2C packet to a device (please be aware that no repeated
36
+ * starts are possible), this can be done in caching method by calling multiple
37
+ * times M155 B<byte-1 value in base 10> or a one liner M155, have a look at
38
+ * the gcode_M155() function for more information. M156 allows Marlin to
39
+ * request data from a device, the received data is then relayed into the serial
40
+ * line for host interpretation.
41
+ *
42
+ */
43
+class TWIBus {
44
+  private:
45
+    /**
46
+     * @brief Timeout value in milliseconds
47
+     * @details For blocking operations this constant value will set the max
48
+     * amount of time Marlin will keep waiting for a reply. Useful is something
49
+     * goes wrong on the bus and the SDA/SCL lines are held up by another device.
50
+     */
51
+    const int timeout = 5;
52
+
53
+    /**
54
+     * @brief Target device address
55
+     * @description This stores, until the buffer is flushed, the target device
56
+     * address, take not we do follow Arduino 7bit addressing.
57
+     */
58
+    uint8_t addr = 0;
59
+
60
+    /**
61
+     * @brief Number of bytes on buffer
62
+     * @description This var holds the total number of bytes on our buffer
63
+     * waiting to be flushed to the bus.
64
+     */
65
+    uint8_t buffer_s = 0;
66
+
67
+    /**
68
+     * @brief Internal buffer
69
+     * @details This is a fixed buffer, TWI command cannot be longer than this
70
+     */
71
+    char buffer[30];
72
+
73
+
74
+  public:
75
+    /**
76
+     * @brief Class constructor
77
+     * @details Initialized the TWI bus and clears the buffer
78
+     */
79
+    TWIBus();
80
+
81
+    /**
82
+     * @brief Reset the buffer
83
+     * @details Brings the internal buffer to a known-empty state
84
+     */
85
+    void reset();
86
+
87
+    /**
88
+     * @brief Send the buffer data to the bus
89
+     * @details Flushed the buffer into the bus targeting the cached slave device
90
+     * address.
91
+     */
92
+    void send();
93
+
94
+    /**
95
+     * @brief Add one byte to the buffer
96
+     * @details Adds the byte to the buffer in a sequential way, if buffer is full
97
+     * the request is silently ignored.
98
+     *
99
+     * @param c a data byte
100
+     */
101
+    void addbyte(char c);
102
+
103
+    /**
104
+     * @brief Sets the target slave address
105
+     * @details The target slave address is stored so it can be later used when
106
+     * the complete packet needs to be sent over the bus.
107
+     *
108
+     * @param addr 7-bit integer address
109
+     */
110
+    void address(uint8_t addr);
111
+
112
+    /**
113
+     * @brief Request data from slave device
114
+     * @details Requests data from a slave device, when the data is received it will
115
+     * be relayed to the serial line using a parser-friendly formatting.
116
+     *
117
+     * @param bytes the number of bytes to request
118
+     */
119
+    void reqbytes(uint8_t bytes);
120
+};
121
+
122
+#endif //TWIBUS_H

Loading…
Cancel
Save