Browse Source

Implement digipots for MKS SBASE (#9927)

Fix #9477
Scott Lahteine 7 years ago
parent
commit
beeed580b8
No account linked to committer's email address

Marlin/src/HAL/HAL_LPC1768/digipot_mcp4451_I2C_routines.c → Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.c View File

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

+ 57
- 0
Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.h View File

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
+#ifndef _DIGIPOT_MCP4451_I2C_ROUTINES_H_
27
+#define _DIGIPOT_MCP4451_I2C_ROUTINES_H_
28
+
29
+#define USEDI2CDEV_M  1  // use I2C1 controller
30
+
31
+#if USEDI2CDEV_M == 0
32
+  #define I2CDEV_M LPC_I2C0
33
+#elif USEDI2CDEV_M == 1
34
+  #define I2CDEV_M LPC_I2C1
35
+#elif USEDI2CDEV_M == 2
36
+  #define I2CDEV_M LPC_I2C2
37
+#else
38
+  #error "Master I2C device not defined!"
39
+#endif
40
+
41
+#ifdef __cplusplus
42
+  extern "C" {
43
+#endif
44
+
45
+#include <lpc17xx_i2c.h>
46
+#include <lpc17xx_pinsel.h>
47
+#include <lpc17xx_libcfg_default.h>
48
+
49
+uint8_t digipot_mcp4451_start(uint8_t sla);
50
+void digipot_mcp4451_init(void);
51
+uint8_t digipot_mcp4451_send_byte(uint8_t data);
52
+
53
+#ifdef __cplusplus
54
+  }
55
+#endif
56
+
57
+#endif // _DIGIPOT_MCP4451_I2C_ROUTINES_H_

+ 20
- 6
Marlin/src/feature/digipot/digipot_mcp4451.cpp View File

27
 #include "Stream.h"
27
 #include "Stream.h"
28
 #include <Wire.h>
28
 #include <Wire.h>
29
 
29
 
30
+#if MB(MKS_SBASE)
31
+  #include "digipot_mcp4451_I2C_routines.h"
32
+#endif
33
+
30
 // Settings for the I2C based DIGIPOT (MCP4451) on Azteeg X3 Pro
34
 // Settings for the I2C based DIGIPOT (MCP4451) on Azteeg X3 Pro
31
 #if MB(5DPRINT)
35
 #if MB(5DPRINT)
32
   #define DIGIPOT_I2C_FACTOR 117.96
36
   #define DIGIPOT_I2C_FACTOR 117.96
41
 }
45
 }
42
 
46
 
43
 static void i2c_send(const byte addr, const byte a, const byte b) {
47
 static void i2c_send(const byte addr, const byte a, const byte b) {
44
-  Wire.beginTransmission(addr);
45
-  Wire.write(a);
46
-  Wire.write(b);
47
-  Wire.endTransmission();
48
+  #if MB(MKS_SBASE)
49
+    digipot_mcp4451_start(addr);
50
+    digipot_mcp4451_send_byte(a);
51
+    digipot_mcp4451_send_byte(b);
52
+  #else
53
+    Wire.beginTransmission(addr);
54
+    Wire.write(a);
55
+    Wire.write(b);
56
+    Wire.endTransmission();
57
+  #endif
48
 }
58
 }
49
 
59
 
50
 // This is for the MCP4451 I2C based digipot
60
 // This is for the MCP4451 I2C based digipot
63
 }
73
 }
64
 
74
 
65
 void digipot_i2c_init() {
75
 void digipot_i2c_init() {
66
-  static const float digipot_motor_current[] PROGMEM = DIGIPOT_I2C_MOTOR_CURRENTS;
67
-  Wire.begin();
76
+  #if MB(MKS_SBASE)
77
+    digipot_mcp4451_init();
78
+  #else
79
+    Wire.begin();
80
+  #endif
68
   // setup initial currents as defined in Configuration_adv.h
81
   // setup initial currents as defined in Configuration_adv.h
82
+  static const float digipot_motor_current[] PROGMEM = DIGIPOT_I2C_MOTOR_CURRENTS;
69
   for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++)
83
   for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++)
70
     digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
84
     digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
71
 }
85
 }

Loading…
Cancel
Save