Explorar el Código

Implement digipots for MKS SBASE (#9927)

Fix #9477
Scott Lahteine hace 7 años
padre
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 Ver fichero

@@ -23,22 +23,20 @@
23 23
 // adapted from  I2C/master/master.c example
24 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 32
 #ifdef __cplusplus
29 33
   extern "C" {
30 34
 #endif
31 35
 
32
-#include <lpc17xx_i2c.h>
33
-#include <lpc17xx_pinsel.h>
34
-#include <lpc17xx_libcfg_default.h>
35
-
36
-//////////////////////////////////////////////////////////////////////////////////////
37
-
38 36
 // These two routines are exact copies of the lpc17xx_i2c.c routines.  Couldn't link to
39 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 40
   // Reset STA, STO, SI
43 41
   I2Cx->I2CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;
44 42
 
@@ -51,8 +49,8 @@ static uint32_t _I2C_Start (LPC_I2C_TypeDef *I2Cx) {
51 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 54
   if (I2Cx->I2CONSET & I2C_I2CONSET_STA)
57 55
     I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
58 56
 
@@ -60,34 +58,16 @@ static void _I2C_Stop (LPC_I2C_TypeDef *I2Cx) {
60 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 61
 PINSEL_CFG_Type PinCfg;
81 62
 I2C_M_SETUP_Type transferMCfg;
82 63
 
83 64
 #define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK)
84 65
 
85
-
86 66
 uint8_t digipot_mcp4451_start(uint8_t sla) {  // send slave address and write bit
87 67
   // Sometimes TX data ACK or NAK status is returned.  That mean the start state didn't
88 68
   // happen which means only the value of the slave address was send.  Keep looping until
89 69
   // the slave address and write bit are actually sent.
90
-  do{
70
+  do {
91 71
     _I2C_Stop(I2CDEV_M); // output stop state on I2C bus
92 72
     _I2C_Start(I2CDEV_M); // output start state on I2C bus
93 73
     while ((I2C_status != I2C_I2STAT_M_TX_START)
@@ -96,42 +76,38 @@ uint8_t digipot_mcp4451_start(uint8_t sla) {  // send slave address and write bi
96 76
         && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK));  //wait for start to be asserted
97 77
 
98 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 80
     LPC_I2C1->I2CONSET = I2C_I2CONSET_AA;
101 81
     LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC;
102 82
     while ((I2C_status != I2C_I2STAT_M_TX_SLAW_ACK)
103 83
         && (I2C_status != I2C_I2STAT_M_TX_SLAW_NACK)
104 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 87
   return 1;
108 88
 }
109 89
 
110
-
111 90
 void digipot_mcp4451_init(void) {
112
-
113 91
   /**
114 92
    * Init I2C pin connect
115 93
    */
116 94
   PinCfg.OpenDrain = 0;
117 95
   PinCfg.Pinmode = 0;
118
-  #if ((USEDI2CDEV_M == 0))
96
+  #if USEDI2CDEV_M == 0
119 97
     PinCfg.Funcnum = 1;
120 98
     PinCfg.Pinnum = 27;
121 99
     PinCfg.Portnum = 0;
122 100
     PINSEL_ConfigPin(&PinCfg); // SDA0 / D57  AUX-1
123 101
     PinCfg.Pinnum = 28;
124 102
     PINSEL_ConfigPin(&PinCfg); // SCL0 / D58  AUX-1
125
-  #endif
126
-  #if ((USEDI2CDEV_M == 1))
103
+  #elif USEDI2CDEV_M == 1
127 104
     PinCfg.Funcnum = 3;
128 105
     PinCfg.Pinnum = 0;
129 106
     PinCfg.Portnum = 0;
130 107
     PINSEL_ConfigPin(&PinCfg);  // SDA1 / D20 SCA
131 108
     PinCfg.Pinnum = 1;
132 109
     PINSEL_ConfigPin(&PinCfg);  // SCL1 / D21 SCL
133
-  #endif
134
-  #if ((USEDI2CDEV_M == 2))
110
+  #elif USEDI2CDEV_M == 2
135 111
     PinCfg.Funcnum = 2;
136 112
     PinCfg.Pinnum = 10;
137 113
     PinCfg.Portnum = 0;
@@ -142,16 +118,15 @@ void digipot_mcp4451_init(void) {
142 118
   // Initialize I2C peripheral
143 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 122
   I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
147 123
 }
148 124
 
149
-
150 125
 uint8_t digipot_mcp4451_send_byte(uint8_t data) {
151 126
   LPC_I2C1->I2DAT = data & I2C_I2DAT_BITMASK; // transmit data
152 127
   LPC_I2C1->I2CONSET = I2C_I2CONSET_AA;
153 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 130
   return 1;
156 131
 }
157 132
 
@@ -159,4 +134,4 @@ uint8_t digipot_mcp4451_send_byte(uint8_t data) {
159 134
   }
160 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 Ver fichero

@@ -0,0 +1,57 @@
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 Ver fichero

@@ -27,6 +27,10 @@
27 27
 #include "Stream.h"
28 28
 #include <Wire.h>
29 29
 
30
+#if MB(MKS_SBASE)
31
+  #include "digipot_mcp4451_I2C_routines.h"
32
+#endif
33
+
30 34
 // Settings for the I2C based DIGIPOT (MCP4451) on Azteeg X3 Pro
31 35
 #if MB(5DPRINT)
32 36
   #define DIGIPOT_I2C_FACTOR 117.96
@@ -41,10 +45,16 @@ static byte current_to_wiper(const float current) {
41 45
 }
42 46
 
43 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 60
 // This is for the MCP4451 I2C based digipot
@@ -63,9 +73,13 @@ void digipot_i2c_set_current(const uint8_t channel, const float current) {
63 73
 }
64 74
 
65 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 81
   // setup initial currents as defined in Configuration_adv.h
82
+  static const float digipot_motor_current[] PROGMEM = DIGIPOT_I2C_MOTOR_CURRENTS;
69 83
   for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++)
70 84
     digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
71 85
 }

Loading…
Cancelar
Guardar