Browse Source

Merge pull request #7935 from Bob-the-Kuhn/Digipot-MCP4451

2.0.x - MPC4451 I2C support
Bob-the-Kuhn 6 years ago
parent
commit
e9cf9ad3f3

+ 58
- 0
Marlin/src/HAL/HAL_LPC1768/include/Wire.h View File

@@ -0,0 +1,58 @@
1
+/*
2
+  TwoWire.h - TWI/I2C library for Arduino & Wiring
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
+  Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
20
+*/
21
+
22
+// Modified for use with the mcp4451 digipot routine
23
+
24
+#if defined(TARGET_LPC1768)
25
+
26
+#ifndef TwoWire_h
27
+#define TwoWire_h
28
+
29
+#include <inttypes.h>
30
+
31
+class TwoWire
32
+{
33
+
34
+  public:
35
+//    TwoWire();
36
+    void begin();
37
+    void beginTransmission(uint8_t);
38
+    uint8_t endTransmission(void);
39
+    size_t write(uint8_t);
40
+};
41
+
42
+//extern TwoWire Wire;//
43
+
44
+TwoWire Wire;
45
+
46
+  ////////////////////////////////////////////////////////////////////////////////////////
47
+  extern "C" uint8_t digipot_mcp4451_start(uint8_t sla);
48
+  extern "C" void digipot_mcp4451_init(void);
49
+  extern "C" uint8_t digipot_mcp4451_send_byte(uint8_t data);
50
+
51
+
52
+  void TwoWire::beginTransmission(uint8_t sla) { digipot_mcp4451_start(sla);}
53
+  void TwoWire::begin(void) {digipot_mcp4451_init();}
54
+  size_t TwoWire::write(uint8_t data) {return digipot_mcp4451_send_byte(data);}
55
+  uint8_t TwoWire::endTransmission(void) {return 1;}
56
+
57
+#endif
58
+#endif  // TARGET_LPC1768

+ 171
- 0
Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.c View File

@@ -0,0 +1,171 @@
1
+/**
2
+  * Marlin 3D Printer Firmware
3
+  * Copyright (C) 2016, 2017 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
+// adapted from  I2C/master/master.c example
24
+//   https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html
25
+
26
+
27
+
28
+#if defined(TARGET_LPC1768)
29
+
30
+  #ifdef __cplusplus
31
+    extern "C" {
32
+  #endif
33
+
34
+  #include <lpc17xx_i2c.h>
35
+  #include <lpc17xx_pinsel.h>
36
+  #include <lpc17xx_libcfg_default.h>
37
+
38
+  //////////////////////////////////////////////////////////////////////////////////////
39
+
40
+  // These two routines are exact copies of the lpc17xx_i2c.c routines.  Couldn't link to
41
+  // to the lpc17xx_i2c.c routines so had to copy them into this file & rename them.
42
+
43
+  static uint32_t _I2C_Start (LPC_I2C_TypeDef *I2Cx)
44
+  {
45
+    // Reset STA, STO, SI
46
+    I2Cx->I2CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;
47
+
48
+    // Enter to Master Transmitter mode
49
+    I2Cx->I2CONSET = I2C_I2CONSET_STA;
50
+
51
+    // Wait for complete
52
+    while (!(I2Cx->I2CONSET & I2C_I2CONSET_SI));
53
+    I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
54
+    return (I2Cx->I2STAT & I2C_STAT_CODE_BITMASK);
55
+  }
56
+
57
+  static void _I2C_Stop (LPC_I2C_TypeDef *I2Cx)
58
+  {
59
+
60
+    /* Make sure start bit is not active */
61
+    if (I2Cx->I2CONSET & I2C_I2CONSET_STA)
62
+    {
63
+      I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
64
+    }
65
+
66
+    I2Cx->I2CONSET = I2C_I2CONSET_STO|I2C_I2CONSET_AA;
67
+
68
+    I2Cx->I2CONCLR = I2C_I2CONCLR_SIC;
69
+  }
70
+
71
+
72
+  //////////////////////////////////////////////////////////////////////////////////////
73
+
74
+
75
+  #define USEDI2CDEV_M  1  // use I2C1 controller
76
+
77
+  #if (USEDI2CDEV_M == 0)
78
+    #define I2CDEV_M LPC_I2C0
79
+  #elif (USEDI2CDEV_M == 1)
80
+    #define I2CDEV_M LPC_I2C1
81
+  #elif (USEDI2CDEV_M == 2)
82
+    #define I2CDEV_M LPC_I2C2
83
+  #else
84
+    #error "Master I2C device not defined!"
85
+  #endif
86
+
87
+
88
+  PINSEL_CFG_Type PinCfg;
89
+  I2C_M_SETUP_Type transferMCfg;
90
+
91
+  #define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK)
92
+
93
+
94
+  uint8_t digipot_mcp4451_start(uint8_t sla) {  // send slave address and write bit
95
+    // Sometimes TX data ACK or NAK status is returned.  That mean the start state didn't
96
+    // happen which means only the value of the slave address was send.  Keep looping until
97
+    // the slave address and write bit are actually sent.
98
+    do{
99
+      _I2C_Stop(I2CDEV_M); // output stop state on I2C bus
100
+      _I2C_Start(I2CDEV_M); // output start state on I2C bus
101
+      while ((I2C_status != I2C_I2STAT_M_TX_START)
102
+          && (I2C_status != I2C_I2STAT_M_TX_RESTART)
103
+          && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK)
104
+          && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK));  //wait for start to be asserted
105
+
106
+      LPC_I2C1->I2CONCLR = I2C_I2CONCLR_STAC; // clear start state before tansmitting slave address
107
+      LPC_I2C1->I2DAT = (sla <<1) & I2C_I2DAT_BITMASK; // transmit slave address & write bit
108
+      LPC_I2C1->I2CONSET = I2C_I2CONSET_AA;
109
+      LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC;
110
+      while ((I2C_status != I2C_I2STAT_M_TX_SLAW_ACK)
111
+          && (I2C_status != I2C_I2STAT_M_TX_SLAW_NACK)
112
+          && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK)
113
+          && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK));  //wait for slaw to finish
114
+    }while ( (I2C_status == I2C_I2STAT_M_TX_DAT_ACK) ||  (I2C_status == I2C_I2STAT_M_TX_DAT_NACK));
115
+    return 1;
116
+  }
117
+
118
+
119
+  void digipot_mcp4451_init(void) {
120
+
121
+    /*
122
+      * Init I2C pin connect
123
+    */
124
+    PinCfg.OpenDrain = 0;
125
+    PinCfg.Pinmode = 0;
126
+    #if ((USEDI2CDEV_M == 0))
127
+      PinCfg.Funcnum = 1;
128
+      PinCfg.Pinnum = 27;
129
+      PinCfg.Portnum = 0;
130
+      PINSEL_ConfigPin(&PinCfg); // SDA0 / D57  AUX-1
131
+      PinCfg.Pinnum = 28;
132
+      PINSEL_ConfigPin(&PinCfg); // SCL0 / D58  AUX-1
133
+    #endif
134
+    #if ((USEDI2CDEV_M == 1))
135
+      PinCfg.Funcnum = 3;
136
+      PinCfg.Pinnum = 0;
137
+      PinCfg.Portnum = 0;
138
+      PINSEL_ConfigPin(&PinCfg);  // SDA1 / D20 SCA
139
+      PinCfg.Pinnum = 1;
140
+      PINSEL_ConfigPin(&PinCfg);  // SCL1 / D21 SCL
141
+    #endif
142
+    #if ((USEDI2CDEV_M == 2))
143
+      PinCfg.Funcnum = 2;
144
+      PinCfg.Pinnum = 10;
145
+      PinCfg.Portnum = 0;
146
+      PINSEL_ConfigPin(&PinCfg); // SDA2 / D38  X_ENABLE_PIN
147
+      PinCfg.Pinnum = 11;
148
+      PINSEL_ConfigPin(&PinCfg); // SCL2 / D55  X_DIR_PIN
149
+    #endif
150
+    // Initialize I2C peripheral
151
+    I2C_Init(I2CDEV_M, 400000);  // hardwired to 400KHz bit rate, 100KHz is the other option
152
+
153
+    /* Enable Master I2C operation */
154
+    I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
155
+  }
156
+
157
+
158
+  uint8_t digipot_mcp4451_send_byte(uint8_t data) {
159
+
160
+    LPC_I2C1->I2DAT = data & I2C_I2DAT_BITMASK; // transmit data
161
+    LPC_I2C1->I2CONSET = I2C_I2CONSET_AA;
162
+    LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC;
163
+    while ((I2C_status != I2C_I2STAT_M_TX_DAT_ACK) && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK));  // wait for xmit to finish
164
+    return 1;
165
+  }
166
+
167
+
168
+  #ifdef __cplusplus
169
+    }
170
+  #endif
171
+#endif

+ 4
- 0
Marlin/src/HAL/utility/twi.h View File

@@ -0,0 +1,4 @@
1
+// Modified for use with the mcp4451 digipot routine
2
+#if defined(TARGET_LPC1768)
3
+
4
+#endif

+ 1
- 1
Marlin/src/module/stepper.h View File

@@ -297,7 +297,7 @@ class Stepper {
297 297
 
298 298
       #ifdef CPU_32_BIT
299 299
         // In case of high-performance processor, it is able to calculate in real-time
300
-        constexpr uint32_t MIN_TIME_PER_STEP = (HAL_STEPPER_TIMER_RATE) / ((STEP_DOUBLER_FREQUENCY) * 2);
300
+        const uint32_t MIN_TIME_PER_STEP = (HAL_STEPPER_TIMER_RATE) / ((STEP_DOUBLER_FREQUENCY) * 2);
301 301
         timer = uint32_t(HAL_STEPPER_TIMER_RATE) / step_rate;
302 302
         NOLESS(timer, MIN_TIME_PER_STEP); // (STEP_DOUBLER_FREQUENCY * 2 kHz - this should never happen)
303 303
       #else

Loading…
Cancel
Save