Browse Source

Further refinements of TWIBus

Scott Lahteine 8 years ago
parent
commit
97e0aed304
3 changed files with 126 additions and 57 deletions
  1. 1
    6
      Marlin/Marlin_main.cpp
  2. 72
    27
      Marlin/twibus.cpp
  3. 53
    24
      Marlin/twibus.h

+ 1
- 6
Marlin/Marlin_main.cpp View File

@@ -842,12 +842,7 @@ void servo_init() {
842 842
   }
843 843
 
844 844
   void i2c_on_request() {          // just send dummy data for now
845
-    static const char *msg = "Hello World!\n";
846
-    const char *adr = msg;
847
-    char c;
848
-    i2c.reset();
849
-    while (c = *adr++) i2c.addbyte(c);
850
-    i2c.send();
845
+    i2c.reply("Hello World!\n");
851 846
   }
852 847
 
853 848
 #endif

+ 72
- 27
Marlin/twibus.cpp View File

@@ -42,38 +42,60 @@ void TWIBus::reset() {
42 42
   this->buffer[0] = 0x00;
43 43
 }
44 44
 
45
-void TWIBus::address(const uint8_t addr) {
46
-  this->addr = addr;
45
+void TWIBus::address(const uint8_t adr) {
46
+  this->addr = adr;
47 47
 
48 48
   #if ENABLED(DEBUG_TWIBUS)
49
-    debug(PSTR("address"), this->addr);
49
+    debug(PSTR("address"), adr);
50 50
   #endif
51 51
 }
52 52
 
53 53
 void TWIBus::addbyte(const char c) {
54
-  if (buffer_s >= sizeof(this->buffer)) return;
54
+  if (this->buffer_s >= COUNT(this->buffer)) return;
55 55
   this->buffer[this->buffer_s++] = c;
56
+  #if ENABLED(DEBUG_TWIBUS)
57
+    debug(PSTR("addbyte"), c);
58
+  #endif
59
+}
60
+
61
+void TWIBus::addbytes(char src[], uint8_t bytes) {
62
+  #if ENABLED(DEBUG_TWIBUS)
63
+    debug(PSTR("addbytes"), bytes);
64
+  #endif
65
+  while (bytes--) this->addbyte(*src++);
66
+}
56 67
 
68
+void TWIBus::addstring(char str[]) {
57 69
   #if ENABLED(DEBUG_TWIBUS)
58
-    debug(PSTR("addbyte"), this->buffer[this->buffer_s - 1]);
70
+    debug(PSTR("addstring"), str);
59 71
   #endif
72
+  while (char c = *str++) this->addbyte(c);
60 73
 }
61 74
 
62 75
 void TWIBus::send() {
63 76
   if (!this->addr) return;
64 77
 
65 78
   #if ENABLED(DEBUG_TWIBUS)
66
-    debug(PSTR("send()"));
79
+    debug(PSTR("send"), this->addr);
67 80
   #endif
68 81
 
69 82
   Wire.beginTransmission(this->addr);
70 83
   Wire.write(this->buffer, this->buffer_s);
71 84
   Wire.endTransmission();
72 85
 
73
-  // Reset the buffer after sending the data
74 86
   this->reset();
75 87
 }
76 88
 
89
+void TWIBus::echodata(uint8_t bytes, const char prefix[], uint8_t adr) {
90
+  SERIAL_ECHO_START;
91
+  serialprintPGM(prefix);
92
+  SERIAL_ECHOPAIR(": from:", adr);
93
+  SERIAL_ECHOPAIR(" bytes:", bytes);
94
+  SERIAL_ECHOPGM (" data:");
95
+  while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
96
+  SERIAL_EOL;
97
+}
98
+
77 99
 void TWIBus::reqbytes(const uint8_t bytes) {
78 100
   if (!this->addr) return;
79 101
 
@@ -82,34 +104,57 @@ void TWIBus::reqbytes(const uint8_t bytes) {
82 104
   #endif
83 105
 
84 106
   // requestFrom() is a blocking function
85
-  millis_t t = millis() + this->timeout;
86 107
   Wire.requestFrom(this->addr, bytes);
87
-  while (Wire.available() < bytes && PENDING(millis(), t)) { /*nada*/ }
88 108
 
89
-  this->relaydata(bytes);
109
+  // Wait until all bytes arrive, or timeout
110
+  millis_t t = millis() + this->timeout;
111
+  while (Wire.available() < bytes && PENDING(millis(), t)) { /*nada*/ }
90 112
 
91
-  // Reset the buffer after sending the data
92
-  this->reset();
113
+  // Simply echo the data to the bus
114
+  this->echodata(bytes, PSTR("i2c-reply"), this->addr);
93 115
 }
94 116
 
95
-void TWIBus::relaydata(uint8_t bytes) {
96
-  SERIAL_ECHO_START;
97
-  SERIAL_ECHOPAIR("i2c-reply: from:", this->addr);
98
-  SERIAL_ECHOPAIR(" bytes:", bytes);
99
-  SERIAL_ECHOPGM (" data:");
100
-  while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
101
-  SERIAL_EOL;
102
-}
117
+#if I2C_SLAVE_ADDRESS > 0
103 118
 
104
-#if ENABLED(DEBUG_TWIBUS)
119
+  void TWIBus::receive(uint8_t bytes) {
120
+    #if ENABLED(DEBUG_TWIBUS)
121
+      debug(PSTR("receive"), bytes);
122
+    #endif
123
+    this->echodata(bytes, PSTR("i2c-receive"), 0);
124
+  }
125
+
126
+  void TWIBus::reply(char str[]/*=NULL*/) {
127
+    #if ENABLED(DEBUG_TWIBUS)
128
+      debug(PSTR("reply"), str);
129
+    #endif
105 130
 
106
-  void TWIBus::debug(const char func[], int32_t val/*=-1*/) {
107
-    if (DEBUGGING(INFO)) {
108
-      SERIAL_ECHOPGM("TWIBus::");
109
-      serialprintPGM(func);
110
-      if (val >= 0) SERIAL_ECHOPAIR(": ", val);
111
-      SERIAL_EOL;
131
+    if (str) {
132
+      this->reset();
133
+      this->addstring(str);
112 134
     }
135
+
136
+    Wire.write(this->buffer, this->buffer_s);
137
+
138
+    this->reset();
139
+  }
140
+
141
+#endif
142
+
143
+#if ENABLED(DEBUG_TWIBUS)
144
+
145
+  void TWIBus::prefix(const char func[]) {
146
+    SERIAL_ECHOPGM("TWIBus::");
147
+    serialprintPGM(func);
148
+    SERIAL_ECHOPGM(": ");
149
+  }
150
+  void TWIBus::debug(const char func[], uint32_t adr) {
151
+    if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(adr); }
152
+  }
153
+  void TWIBus::debug(const char func[], char c) {
154
+    if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(c); }
155
+  }
156
+  void TWIBus::debug(const char func[], char str[]) {
157
+    if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(str); }
113 158
   }
114 159
 
115 160
 #endif

+ 53
- 24
Marlin/twibus.h View File

@@ -62,7 +62,7 @@ class TWIBus {
62 62
 
63 63
     /**
64 64
      * @brief Number of bytes on buffer
65
-     * @description Number of bytes in the buffer waiting to be flushed to the bus.
65
+     * @description Number of bytes in the buffer waiting to be flushed to the bus
66 66
      */
67 67
     uint8_t buffer_s = 0;
68 68
 
@@ -94,7 +94,7 @@ class TWIBus {
94 94
 
95 95
     /**
96 96
      * @brief Send the buffer data to the bus
97
-     * @details Flush the buffer to the bus at the target address.
97
+     * @details Flush the buffer to the target address
98 98
      */
99 99
     void send();
100 100
 
@@ -108,44 +108,56 @@ class TWIBus {
108 108
     void addbyte(const char c);
109 109
 
110 110
     /**
111
+     * @brief Add some bytes to the buffer
112
+     * @details Add bytes to the end of the buffer.
113
+     *          Concatenates at the buffer size.
114
+     *
115
+     * @param src source data address
116
+     * @param bytes the number of bytes to add
117
+     */
118
+    void addbytes(char src[], uint8_t bytes);
119
+
120
+    /**
121
+     * @brief Add a null-terminated string to the buffer
122
+     * @details Add bytes to the end of the buffer up to a nul.
123
+     *          Concatenates at the buffer size.
124
+     *
125
+     * @param str source string address
126
+     */
127
+    void addstring(char str[]);
128
+
129
+    /**
111 130
      * @brief Set the target slave address
112
-     * @details The target slave address for sending the full packet.
131
+     * @details The target slave address for sending the full packet
113 132
      *
114
-     * @param addr 7-bit integer address
133
+     * @param adr 7-bit integer address
115 134
      */
116
-    void address(const uint8_t addr);
135
+    void address(const uint8_t adr);
117 136
 
118 137
     /**
119
-     * @brief Request data from the slave device
120
-     * @details Request a number of bytes from a slave device.
121
-     *          This implementation simply sends the data to serial
122
-     *          in a parser-friendly format.
138
+     * @brief Echo data on the bus to serial
139
+     * @details Echo some number of bytes from the bus
140
+     *          to serial in a parser-friendly format.
123 141
      *
124 142
      * @param bytes the number of bytes to request
125 143
      */
126
-    void reqbytes(const uint8_t bytes);
144
+    static void echodata(uint8_t bytes, const char prefix[], uint8_t adr);
127 145
 
128 146
     /**
129
-     * @brief Relay data from the slave device to serial
130
-     * @details Relay a number of bytes from the bus to
131
-     *          serial in a parser-friendly format.
147
+     * @brief Request data from the slave device
148
+     * @details Request a number of bytes from a slave device.
149
+     *          This implementation simply sends the data to serial
150
+     *          in a parser-friendly format.
132 151
      *
133 152
      * @param bytes the number of bytes to request
134 153
      */
135
-    void relaydata(uint8_t bytes);
154
+    void reqbytes(const uint8_t bytes);
136 155
 
137 156
     #if I2C_SLAVE_ADDRESS > 0
138 157
 
139 158
       /**
140
-       * @brief Receive bytes (passively)
141
-       * @details Receive bytes sent to our slave address.
142
-       *          and simply echo them to serial.
143
-       */
144
-      inline void receive(uint8_t bytes) { relaydata(bytes); }
145
-
146
-      /**
147 159
        * @brief Register a slave receive handler
148
-       * @details Set a handler to receive data addressed to us.
160
+       * @details Set a handler to receive data addressed to us
149 161
        *
150 162
        * @param handler A function to handle receiving bytes
151 163
        */
@@ -153,12 +165,25 @@ class TWIBus {
153 165
 
154 166
       /**
155 167
        * @brief Register a slave request handler
156
-       * @details Set a handler to send data requested from us.
168
+       * @details Set a handler to send data requested from us
157 169
        *
158 170
        * @param handler A function to handle receiving bytes
159 171
        */
160 172
       inline void onRequest(const twiRequestFunc_t handler) { Wire.onRequest(handler); }
161 173
 
174
+      /**
175
+       * @brief Default handler to receive
176
+       * @details Receive bytes sent to our slave address
177
+       *          and simply echo them to serial.
178
+       */
179
+      void receive(uint8_t bytes);
180
+
181
+      /**
182
+       * @brief Send a reply to the bus
183
+       * @details Send the buffer and clear it.
184
+       */
185
+      void reply(char str[]=NULL);
186
+
162 187
     #endif
163 188
 
164 189
     #if ENABLED(DEBUG_TWIBUS)
@@ -167,7 +192,11 @@ class TWIBus {
167 192
        * @brief Prints a debug message
168 193
        * @details Prints a simple debug message "TWIBus::function: value"
169 194
        */
170
-      static void debug(const char func[], int32_t val = -1);
195
+      static void prefix(const char func[]);
196
+      static void debug(const char func[], uint32_t adr);
197
+      static void debug(const char func[], char c);
198
+      static void debug(const char func[], char adr[]);
199
+      static inline void debug(const char func[], uint8_t v) { debug(func, (uint32_t)v); }
171 200
 
172 201
     #endif
173 202
 };

Loading…
Cancel
Save