Browse Source

🎨 Combine common LPC1768 I2C code

Scott Lahteine 2 years ago
parent
commit
6015ee2531

+ 2
- 31
Marlin/src/HAL/LPC1768/include/digipot_mcp4451_I2C_routines.c View File

29
 
29
 
30
 #include "../../../inc/MarlinConfigPre.h"
30
 #include "../../../inc/MarlinConfigPre.h"
31
 
31
 
32
-#if MB(MKS_SBASE)
32
+#if ENABLED(DIGIPOT_MCP4451) && MB(MKS_SBASE)
33
 
33
 
34
 #ifdef __cplusplus
34
 #ifdef __cplusplus
35
   extern "C" {
35
   extern "C" {
37
 
37
 
38
 #include "digipot_mcp4451_I2C_routines.h"
38
 #include "digipot_mcp4451_I2C_routines.h"
39
 
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
-  // Reset STA, STO, SI
45
-  I2Cx->I2CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;
46
-
47
-  // Enter to Master Transmitter mode
48
-  I2Cx->I2CONSET = I2C_I2CONSET_STA;
49
-
50
-  // Wait for complete
51
-  while (!(I2Cx->I2CONSET & I2C_I2CONSET_SI));
52
-  I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
53
-  return (I2Cx->I2STAT & I2C_STAT_CODE_BITMASK);
54
-}
55
-
56
-static void _I2C_Stop(LPC_I2C_TypeDef *I2Cx) {
57
-  // Make sure start bit is not active
58
-  if (I2Cx->I2CONSET & I2C_I2CONSET_STA)
59
-    I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
60
-
61
-  I2Cx->I2CONSET = I2C_I2CONSET_STO|I2C_I2CONSET_AA;
62
-  I2Cx->I2CONCLR = I2C_I2CONCLR_SIC;
63
-}
64
-
65
-I2C_M_SETUP_Type transferMCfg;
66
-
67
-#define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK)
68
-
69
 uint8_t digipot_mcp4451_start(uint8_t sla) {  // send slave address and write bit
40
 uint8_t digipot_mcp4451_start(uint8_t sla) {  // send slave address and write bit
70
   // Sometimes TX data ACK or NAK status is returned.  That mean the start state didn't
41
   // Sometimes TX data ACK or NAK status is returned.  That mean the start state didn't
71
   // happen which means only the value of the slave address was send.  Keep looping until
42
   // happen which means only the value of the slave address was send.  Keep looping until
102
   }
73
   }
103
 #endif
74
 #endif
104
 
75
 
105
-#endif // MB(MKS_SBASE)
76
+#endif // DIGIPOT_MCP4451 && MKS_SBASE
106
 #endif // TARGET_LPC1768
77
 #endif // TARGET_LPC1768

+ 26
- 0
Marlin/src/HAL/LPC1768/include/i2c_util.c View File

63
   I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
63
   I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
64
 }
64
 }
65
 
65
 
66
+//////////////////////////////////////////////////////////////////////////////////////
67
+// These two routines are exact copies of the lpc17xx_i2c.c routines.  Couldn't link to
68
+// to the lpc17xx_i2c.c routines so had to copy them into this file & rename them.
69
+
70
+uint32_t _I2C_Start(LPC_I2C_TypeDef *I2Cx) {
71
+  // Reset STA, STO, SI
72
+  I2Cx->I2CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;
73
+
74
+  // Enter to Master Transmitter mode
75
+  I2Cx->I2CONSET = I2C_I2CONSET_STA;
76
+
77
+  // Wait for complete
78
+  while (!(I2Cx->I2CONSET & I2C_I2CONSET_SI));
79
+  I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
80
+  return (I2Cx->I2STAT & I2C_STAT_CODE_BITMASK);
81
+}
82
+
83
+void _I2C_Stop(LPC_I2C_TypeDef *I2Cx) {
84
+  /* Make sure start bit is not active */
85
+  if (I2Cx->I2CONSET & I2C_I2CONSET_STA)
86
+    I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
87
+
88
+  I2Cx->I2CONSET = I2C_I2CONSET_STO|I2C_I2CONSET_AA;
89
+  I2Cx->I2CONCLR = I2C_I2CONCLR_SIC;
90
+}
91
+
66
 #ifdef __cplusplus
92
 #ifdef __cplusplus
67
   }
93
   }
68
 #endif
94
 #endif

+ 5
- 0
Marlin/src/HAL/LPC1768/include/i2c_util.h View File

51
 
51
 
52
 void configure_i2c(const uint8_t clock_option);
52
 void configure_i2c(const uint8_t clock_option);
53
 
53
 
54
+uint32_t _I2C_Start(LPC_I2C_TypeDef *I2Cx);
55
+void _I2C_Stop(LPC_I2C_TypeDef *I2Cx);
56
+
57
+#define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK)
58
+
54
 #ifdef __cplusplus
59
 #ifdef __cplusplus
55
   }
60
   }
56
 #endif
61
 #endif

+ 1
- 35
Marlin/src/HAL/LPC1768/u8g/LCD_I2C_routines.cpp View File

36
 
36
 
37
 //////////////////////////////////////////////////////////////////////////////////////
37
 //////////////////////////////////////////////////////////////////////////////////////
38
 
38
 
39
-// These two routines are exact copies of the lpc17xx_i2c.c routines.  Couldn't link to
40
-// to the lpc17xx_i2c.c routines so had to copy them into this file & rename them.
41
-
42
-static uint32_t _I2C_Start(LPC_I2C_TypeDef *I2Cx) {
43
-  // Reset STA, STO, SI
44
-  I2Cx->I2CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;
45
-
46
-  // Enter to Master Transmitter mode
47
-  I2Cx->I2CONSET = I2C_I2CONSET_STA;
48
-
49
-  // Wait for complete
50
-  while (!(I2Cx->I2CONSET & I2C_I2CONSET_SI));
51
-  I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
52
-  return (I2Cx->I2STAT & I2C_STAT_CODE_BITMASK);
53
-}
54
-
55
-static void _I2C_Stop (LPC_I2C_TypeDef *I2Cx) {
56
-  /* Make sure start bit is not active */
57
-  if (I2Cx->I2CONSET & I2C_I2CONSET_STA)
58
-    I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
59
-
60
-  I2Cx->I2CONSET = I2C_I2CONSET_STO|I2C_I2CONSET_AA;
61
-  I2Cx->I2CONCLR = I2C_I2CONCLR_SIC;
62
-}
63
-
64
-//////////////////////////////////////////////////////////////////////////////////////
65
-
66
-#define I2CDEV_S_ADDR   0x78  // from SSD1306  //actual address is 0x3C - shift left 1 with LSB set to 0 to indicate write
67
-
68
-#define BUFFER_SIZE                     0x1  // only do single byte transfers with LCDs
69
-
70
-I2C_M_SETUP_Type transferMCfg;
71
-
72
-#define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK)
39
+#define I2CDEV_S_ADDR   0x78  // From SSD1306 (actual address is 0x3C - shift left 1 with LSB set to 0 to indicate write)
73
 
40
 
74
 // Send slave address and write bit
41
 // Send slave address and write bit
75
 uint8_t u8g_i2c_start(const uint8_t sla) {
42
 uint8_t u8g_i2c_start(const uint8_t sla) {
115
 void u8g_i2c_stop() {
82
 void u8g_i2c_stop() {
116
 }
83
 }
117
 
84
 
118
-
119
 #ifdef __cplusplus
85
 #ifdef __cplusplus
120
   }
86
   }
121
 #endif
87
 #endif

Loading…
Cancel
Save