|
@@ -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)
|