Skip to content

Commit

Permalink
🎨 Combine common LPC1768 I2C code
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Mar 29, 2022
1 parent 0752082 commit 6015ee2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 66 deletions.
33 changes: 2 additions & 31 deletions Marlin/src/HAL/LPC1768/include/digipot_mcp4451_I2C_routines.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,14 @@

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

#if MB(MKS_SBASE)
#if ENABLED(DIGIPOT_MCP4451) && MB(MKS_SBASE)

#ifdef __cplusplus
extern "C" {
#endif

#include "digipot_mcp4451_I2C_routines.h"

// These two routines are exact copies of the lpc17xx_i2c.c routines. Couldn't link to
// to the lpc17xx_i2c.c routines so had to copy them into this file & rename them.

static uint32_t _I2C_Start(LPC_I2C_TypeDef *I2Cx) {
// Reset STA, STO, SI
I2Cx->I2CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;

// Enter to Master Transmitter mode
I2Cx->I2CONSET = I2C_I2CONSET_STA;

// Wait for complete
while (!(I2Cx->I2CONSET & I2C_I2CONSET_SI));
I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
return (I2Cx->I2STAT & I2C_STAT_CODE_BITMASK);
}

static void _I2C_Stop(LPC_I2C_TypeDef *I2Cx) {
// Make sure start bit is not active
if (I2Cx->I2CONSET & I2C_I2CONSET_STA)
I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;

I2Cx->I2CONSET = I2C_I2CONSET_STO|I2C_I2CONSET_AA;
I2Cx->I2CONCLR = I2C_I2CONCLR_SIC;
}

I2C_M_SETUP_Type transferMCfg;

#define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK)

uint8_t digipot_mcp4451_start(uint8_t sla) { // send slave address and write bit
// Sometimes TX data ACK or NAK status is returned. That mean the start state didn't
// happen which means only the value of the slave address was send. Keep looping until
Expand Down Expand Up @@ -102,5 +73,5 @@ uint8_t digipot_mcp4451_send_byte(uint8_t data) {
}
#endif

#endif // MB(MKS_SBASE)
#endif // DIGIPOT_MCP4451 && MKS_SBASE
#endif // TARGET_LPC1768
26 changes: 26 additions & 0 deletions Marlin/src/HAL/LPC1768/include/i2c_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ void configure_i2c(const uint8_t clock_option) {
I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
}

//////////////////////////////////////////////////////////////////////////////////////
// These two routines are exact copies of the lpc17xx_i2c.c routines. Couldn't link to
// to the lpc17xx_i2c.c routines so had to copy them into this file & rename them.

uint32_t _I2C_Start(LPC_I2C_TypeDef *I2Cx) {
// Reset STA, STO, SI
I2Cx->I2CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;

// Enter to Master Transmitter mode
I2Cx->I2CONSET = I2C_I2CONSET_STA;

// Wait for complete
while (!(I2Cx->I2CONSET & I2C_I2CONSET_SI));
I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
return (I2Cx->I2STAT & I2C_STAT_CODE_BITMASK);
}

void _I2C_Stop(LPC_I2C_TypeDef *I2Cx) {
/* Make sure start bit is not active */
if (I2Cx->I2CONSET & I2C_I2CONSET_STA)
I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;

I2Cx->I2CONSET = I2C_I2CONSET_STO|I2C_I2CONSET_AA;
I2Cx->I2CONCLR = I2C_I2CONCLR_SIC;
}

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 5 additions & 0 deletions Marlin/src/HAL/LPC1768/include/i2c_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@

void configure_i2c(const uint8_t clock_option);

uint32_t _I2C_Start(LPC_I2C_TypeDef *I2Cx);
void _I2C_Stop(LPC_I2C_TypeDef *I2Cx);

#define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK)

#ifdef __cplusplus
}
#endif
36 changes: 1 addition & 35 deletions Marlin/src/HAL/LPC1768/u8g/LCD_I2C_routines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,7 @@ extern int millis();

//////////////////////////////////////////////////////////////////////////////////////

// These two routines are exact copies of the lpc17xx_i2c.c routines. Couldn't link to
// to the lpc17xx_i2c.c routines so had to copy them into this file & rename them.

static uint32_t _I2C_Start(LPC_I2C_TypeDef *I2Cx) {
// Reset STA, STO, SI
I2Cx->I2CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;

// Enter to Master Transmitter mode
I2Cx->I2CONSET = I2C_I2CONSET_STA;

// Wait for complete
while (!(I2Cx->I2CONSET & I2C_I2CONSET_SI));
I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
return (I2Cx->I2STAT & I2C_STAT_CODE_BITMASK);
}

static void _I2C_Stop (LPC_I2C_TypeDef *I2Cx) {
/* Make sure start bit is not active */
if (I2Cx->I2CONSET & I2C_I2CONSET_STA)
I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;

I2Cx->I2CONSET = I2C_I2CONSET_STO|I2C_I2CONSET_AA;
I2Cx->I2CONCLR = I2C_I2CONCLR_SIC;
}

//////////////////////////////////////////////////////////////////////////////////////

#define I2CDEV_S_ADDR 0x78 // from SSD1306 //actual address is 0x3C - shift left 1 with LSB set to 0 to indicate write

#define BUFFER_SIZE 0x1 // only do single byte transfers with LCDs

I2C_M_SETUP_Type transferMCfg;

#define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK)
#define I2CDEV_S_ADDR 0x78 // From SSD1306 (actual address is 0x3C - shift left 1 with LSB set to 0 to indicate write)

// Send slave address and write bit
uint8_t u8g_i2c_start(const uint8_t sla) {
Expand Down Expand Up @@ -115,7 +82,6 @@ uint8_t u8g_i2c_send_byte(uint8_t data) {
void u8g_i2c_stop() {
}


#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 6015ee2

Please sign in to comment.