Skip to content

Commit 6015ee2

Browse files
committed
🎨 Combine common LPC1768 I2C code
1 parent 0752082 commit 6015ee2

File tree

4 files changed

+34
-66
lines changed

4 files changed

+34
-66
lines changed

Marlin/src/HAL/LPC1768/include/digipot_mcp4451_I2C_routines.c

+2-31
Original file line numberDiff line numberDiff line change
@@ -29,43 +29,14 @@
2929

3030
#include "../../../inc/MarlinConfigPre.h"
3131

32-
#if MB(MKS_SBASE)
32+
#if ENABLED(DIGIPOT_MCP4451) && MB(MKS_SBASE)
3333

3434
#ifdef __cplusplus
3535
extern "C" {
3636
#endif
3737

3838
#include "digipot_mcp4451_I2C_routines.h"
3939

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-
6940
uint8_t digipot_mcp4451_start(uint8_t sla) { // send slave address and write bit
7041
// Sometimes TX data ACK or NAK status is returned. That mean the start state didn't
7142
// happen which means only the value of the slave address was send. Keep looping until
@@ -102,5 +73,5 @@ uint8_t digipot_mcp4451_send_byte(uint8_t data) {
10273
}
10374
#endif
10475

105-
#endif // MB(MKS_SBASE)
76+
#endif // DIGIPOT_MCP4451 && MKS_SBASE
10677
#endif // TARGET_LPC1768

Marlin/src/HAL/LPC1768/include/i2c_util.c

+26
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,32 @@ void configure_i2c(const uint8_t clock_option) {
6363
I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
6464
}
6565

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+
6692
#ifdef __cplusplus
6793
}
6894
#endif

Marlin/src/HAL/LPC1768/include/i2c_util.h

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151

5252
void configure_i2c(const uint8_t clock_option);
5353

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+
5459
#ifdef __cplusplus
5560
}
5661
#endif

Marlin/src/HAL/LPC1768/u8g/LCD_I2C_routines.cpp

+1-35
Original file line numberDiff line numberDiff line change
@@ -36,40 +36,7 @@ extern int millis();
3636

3737
//////////////////////////////////////////////////////////////////////////////////////
3838

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

7441
// Send slave address and write bit
7542
uint8_t u8g_i2c_start(const uint8_t sla) {
@@ -115,7 +82,6 @@ uint8_t u8g_i2c_send_byte(uint8_t data) {
11582
void u8g_i2c_stop() {
11683
}
11784

118-
11985
#ifdef __cplusplus
12086
}
12187
#endif

0 commit comments

Comments
 (0)