Browse Source

Master-only TwoWire class for LPC1768

Thomas Moore 7 years ago
parent
commit
6856eccc77

+ 238
- 0
Marlin/src/HAL/HAL_LPC1768/Wire.cpp View File

@@ -0,0 +1,238 @@
1
+/*
2
+  TwoWire.cpp - TWI/I2C library for Wiring & Arduino
3
+  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#ifdef TARGET_LPC1768
21
+
22
+extern "C" {
23
+  #include <stdlib.h>
24
+  #include <string.h>
25
+  #include <inttypes.h>
26
+  #include <lpc17xx_i2c.h>
27
+  #include <lpc17xx_pinsel.h>
28
+  #include <lpc17xx_libcfg_default.h>
29
+}
30
+
31
+#include "Wire.h"
32
+
33
+#define USEDI2CDEV_M 1
34
+
35
+#if (USEDI2CDEV_M == 0)
36
+  #define I2CDEV_M LPC_I2C0
37
+#elif (USEDI2CDEV_M == 1)
38
+  #define I2CDEV_M LPC_I2C1
39
+#elif (USEDI2CDEV_M == 2)
40
+  #define I2CDEV_M LPC_I2C2
41
+#else
42
+  #error "Master I2C device not defined!"
43
+#endif
44
+
45
+// Initialize Class Variables //////////////////////////////////////////////////
46
+
47
+uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
48
+uint8_t TwoWire::rxBufferIndex = 0;
49
+uint8_t TwoWire::rxBufferLength = 0;
50
+
51
+uint8_t TwoWire::txAddress = 0;
52
+uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
53
+uint8_t TwoWire::txBufferIndex = 0;
54
+uint8_t TwoWire::txBufferLength = 0;
55
+
56
+uint8_t TwoWire::transmitting = 0;
57
+
58
+// Constructors ////////////////////////////////////////////////////////////////
59
+
60
+TwoWire::TwoWire() {
61
+}
62
+
63
+// Public Methods //////////////////////////////////////////////////////////////
64
+
65
+void TwoWire::begin(void) {
66
+  rxBufferIndex = 0;
67
+  rxBufferLength = 0;
68
+
69
+  txBufferIndex = 0;
70
+  txBufferLength = 0;
71
+
72
+  /*
73
+   * Init I2C pin connect
74
+   */
75
+  PINSEL_CFG_Type PinCfg;
76
+  PinCfg.OpenDrain = 0;
77
+  PinCfg.Pinmode = 0;
78
+  #if ((USEDI2CDEV_M == 0))
79
+    PinCfg.Funcnum = 1;
80
+    PinCfg.Pinnum = 27;
81
+    PinCfg.Portnum = 0;
82
+    PINSEL_ConfigPin(&PinCfg); // SDA0 / D57  AUX-1
83
+    PinCfg.Pinnum = 28;
84
+    PINSEL_ConfigPin(&PinCfg); // SCL0 / D58  AUX-1
85
+  #endif
86
+  #if ((USEDI2CDEV_M == 1))
87
+    PinCfg.Funcnum = 3;
88
+    PinCfg.Pinnum = 0;
89
+    PinCfg.Portnum = 0;
90
+    PINSEL_ConfigPin(&PinCfg);  // SDA1 / D20 SCA
91
+    PinCfg.Pinnum = 1;
92
+    PINSEL_ConfigPin(&PinCfg);  // SCL1 / D21 SCL
93
+  #endif
94
+  #if ((USEDI2CDEV_M == 2))
95
+    PinCfg.Funcnum = 2;
96
+    PinCfg.Pinnum = 10;
97
+    PinCfg.Portnum = 0;
98
+    PINSEL_ConfigPin(&PinCfg); // SDA2 / D38  X_ENABLE_PIN
99
+    PinCfg.Pinnum = 11;
100
+    PINSEL_ConfigPin(&PinCfg); // SCL2 / D55  X_DIR_PIN
101
+  #endif
102
+
103
+  // Initialize I2C peripheral
104
+  I2C_Init(I2CDEV_M, 100000);
105
+  
106
+  // Enable Master I2C operation
107
+  I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
108
+}
109
+
110
+uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) {
111
+  // clamp to buffer length
112
+  if(quantity > BUFFER_LENGTH){
113
+    quantity = BUFFER_LENGTH;
114
+  }
115
+  
116
+  // perform blocking read into buffer
117
+  I2C_M_SETUP_Type transferMCfg;
118
+  transferMCfg.sl_addr7bit = address >> 1; // not sure about the right shift
119
+	transferMCfg.tx_data = NULL;
120
+	transferMCfg.tx_length = 0;
121
+	transferMCfg.rx_data = rxBuffer;
122
+	transferMCfg.rx_length = quantity;
123
+	transferMCfg.retransmissions_max = 3;
124
+	I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
125
+
126
+  // set rx buffer iterator vars
127
+  rxBufferIndex = 0;
128
+  rxBufferLength = transferMCfg.rx_count;
129
+
130
+  return transferMCfg.rx_count;
131
+}
132
+
133
+uint8_t TwoWire::requestFrom(int address, int quantity) {
134
+  return requestFrom((uint8_t)address, (uint8_t)quantity);
135
+}
136
+
137
+void TwoWire::beginTransmission(uint8_t address) {
138
+  // indicate that we are transmitting
139
+  transmitting = 1;
140
+  // set address of targeted slave
141
+  txAddress = address;
142
+  // reset tx buffer iterator vars
143
+  txBufferIndex = 0;
144
+  txBufferLength = 0;
145
+}
146
+
147
+void TwoWire::beginTransmission(int address) {
148
+  beginTransmission((uint8_t)address);
149
+}
150
+
151
+uint8_t TwoWire::endTransmission(void) {
152
+  // transmit buffer (blocking)
153
+  I2C_M_SETUP_Type transferMCfg;
154
+  transferMCfg.sl_addr7bit = txAddress >> 1; // not sure about the right shift
155
+	transferMCfg.tx_data = txBuffer;
156
+	transferMCfg.tx_length = txBufferLength;
157
+	transferMCfg.rx_data = NULL;
158
+	transferMCfg.rx_length = 0;
159
+	transferMCfg.retransmissions_max = 3;
160
+  Status status = I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
161
+  
162
+  // reset tx buffer iterator vars
163
+  txBufferIndex = 0;
164
+  txBufferLength = 0;
165
+
166
+  // indicate that we are done transmitting
167
+  transmitting = 0;
168
+
169
+  if (status == SUCCESS)
170
+    return 0; // success
171
+  else
172
+    return 4; // other error
173
+}
174
+
175
+// must be called after beginTransmission(address)
176
+size_t TwoWire::write(uint8_t data) {
177
+  if (transmitting) {
178
+    // don't bother if buffer is full
179
+    if (txBufferLength >= BUFFER_LENGTH) {
180
+      return 0;
181
+    }
182
+
183
+    // put byte in tx buffer
184
+    txBuffer[txBufferIndex] = data;
185
+    ++txBufferIndex;
186
+
187
+    // update amount in buffer   
188
+    txBufferLength = txBufferIndex;
189
+  }
190
+
191
+  return 1;
192
+}
193
+
194
+// must be called after beginTransmission(address)
195
+size_t TwoWire::write(const uint8_t *data, size_t quantity) {
196
+  size_t sent = 0;
197
+  if (transmitting)
198
+    for(sent = 0; sent < quantity; ++sent)
199
+      if (!write(data[sent]))
200
+        break;
201
+
202
+  return sent;
203
+}
204
+
205
+// must be called after requestFrom(address, numBytes)
206
+int TwoWire::available(void) {
207
+  return rxBufferLength - rxBufferIndex;
208
+}
209
+
210
+// must be called after requestFrom(address, numBytes)
211
+int TwoWire::read(void) {
212
+  int value = -1;
213
+  
214
+  // get each successive byte on each call
215
+  if(rxBufferIndex < rxBufferLength) {
216
+    value = rxBuffer[rxBufferIndex];
217
+    ++rxBufferIndex;
218
+  }
219
+
220
+  return value;
221
+}
222
+
223
+// must be called after requestFrom(address, numBytes)
224
+int TwoWire::peek(void) {
225
+  int value = -1;
226
+  
227
+  if(rxBufferIndex < rxBufferLength){
228
+    value = rxBuffer[rxBufferIndex];
229
+  }
230
+
231
+  return value;
232
+}
233
+
234
+// Preinstantiate Objects //////////////////////////////////////////////////////
235
+
236
+TwoWire Wire = TwoWire();
237
+
238
+#endif // TARGET_LPC1768

+ 35
- 25
Marlin/src/HAL/HAL_LPC1768/include/Wire.h View File

@@ -1,4 +1,4 @@
1
-/*
1
+/**
2 2
  * TwoWire.h - TWI/I2C library for Arduino & Wiring
3 3
  * Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
4 4
  *
@@ -19,39 +19,49 @@
19 19
  * Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
20 20
  */
21 21
 
22
-// Modified for use with the mcp4451 digipot routine
23
-
24
-#ifdef TARGET_LPC1768
25
-
26
-#ifndef TwoWire_h
27
-#define TwoWire_h
22
+#ifndef _TWOWIRE_H_
23
+#define _TWOWIRE_H_
28 24
 
29 25
 #include <inttypes.h>
30 26
 
27
+#define BUFFER_LENGTH 32
28
+
31 29
 class TwoWire {
30
+  private:
31
+    static uint8_t rxBuffer[];
32
+    static uint8_t rxBufferIndex;
33
+    static uint8_t rxBufferLength;
34
+
35
+    static uint8_t txAddress;
36
+    static uint8_t txBuffer[];
37
+    static uint8_t txBufferIndex;
38
+    static uint8_t txBufferLength;
39
+
40
+    static uint8_t transmitting;
41
+
32 42
   public:
33
-    //TwoWire();
43
+    TwoWire();
34 44
     void begin();
35 45
     void beginTransmission(uint8_t);
46
+    void beginTransmission(int);
36 47
     uint8_t endTransmission(void);
37
-    size_t write(uint8_t);
38
-};
39
-
40
-//extern TwoWire Wire;
41
-
42
-TwoWire Wire;
48
+    uint8_t endTransmission(uint8_t);
43 49
 
44
-////////////////////////////////////////////////////////////////////////////////////////
45
-
46
-extern "C" uint8_t digipot_mcp4451_start(uint8_t sla);
47
-extern "C" void digipot_mcp4451_init(void);
48
-extern "C" uint8_t digipot_mcp4451_send_byte(uint8_t data);
50
+    uint8_t requestFrom(uint8_t, uint8_t);
51
+    uint8_t requestFrom(int, int);
52
+    
53
+    virtual size_t write(uint8_t);
54
+    virtual size_t write(const uint8_t *, size_t);
55
+    virtual int available(void);
56
+    virtual int read(void);
57
+    virtual int peek(void);
49 58
 
59
+    inline size_t write(unsigned long n) { return write((uint8_t)n); }
60
+    inline size_t write(long n) { return write((uint8_t)n); }
61
+    inline size_t write(unsigned int n) { return write((uint8_t)n); }
62
+    inline size_t write(int n) { return write((uint8_t)n); }
63
+};
50 64
 
51
-void TwoWire::beginTransmission(uint8_t sla) { digipot_mcp4451_start(sla);}
52
-void TwoWire::begin(void) {digipot_mcp4451_init();}
53
-size_t TwoWire::write(uint8_t data) {return digipot_mcp4451_send_byte(data);}
54
-uint8_t TwoWire::endTransmission(void) {return 1;}
65
+extern TwoWire Wire;
55 66
 
56
-#endif
57
-#endif  // TARGET_LPC1768
67
+#endif // _TWOWIRE_H_

+ 1
- 0
Marlin/src/feature/I2CPositionEncoder.cpp View File

@@ -37,6 +37,7 @@
37 37
 #include "../module/temperature.h"
38 38
 #include "../module/stepper.h"
39 39
 #include "../gcode/parser.h"
40
+#include "binary.h"
40 41
 
41 42
 #include <Wire.h>
42 43
 

Loading…
Cancel
Save