Browse Source

🎨 Combine common LPC1768 I2C code

FB4S_WIFI
Scott Lahteine 3 years ago
committed by Scott Lahteine
parent
commit
89d5ed0c9e
  1. 33
      Marlin/src/HAL/LPC1768/include/digipot_mcp4451_I2C_routines.c
  2. 26
      Marlin/src/HAL/LPC1768/include/i2c_util.c
  3. 5
      Marlin/src/HAL/LPC1768/include/i2c_util.h
  4. 36
      Marlin/src/HAL/LPC1768/u8g/LCD_I2C_routines.cpp

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

@ -29,7 +29,7 @@
#include "../../../inc/MarlinConfigPre.h" #include "../../../inc/MarlinConfigPre.h"
#if MB(MKS_SBASE) #if ENABLED(DIGIPOT_MCP4451) && MB(MKS_SBASE)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -37,35 +37,6 @@
#include "digipot_mcp4451_I2C_routines.h" #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 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 // 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 // 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) {
} }
#endif #endif
#endif // MB(MKS_SBASE) #endif // DIGIPOT_MCP4451 && MKS_SBASE
#endif // TARGET_LPC1768 #endif // TARGET_LPC1768

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

@ -63,6 +63,32 @@ void configure_i2c(const uint8_t clock_option) {
I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE); 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 #ifdef __cplusplus
} }
#endif #endif

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

@ -51,6 +51,11 @@
void configure_i2c(const uint8_t clock_option); 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 #ifdef __cplusplus
} }
#endif #endif

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

@ -36,40 +36,7 @@ extern int millis();
////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////
// These two routines are exact copies of the lpc17xx_i2c.c routines. Couldn't link to #define I2CDEV_S_ADDR 0x78 // From SSD1306 (actual address is 0x3C - shift left 1 with LSB set to 0 to indicate write)
// 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)
// Send slave address and write bit // Send slave address and write bit
uint8_t u8g_i2c_start(const uint8_t sla) { uint8_t u8g_i2c_start(const uint8_t sla) {
@ -115,7 +82,6 @@ uint8_t u8g_i2c_send_byte(uint8_t data) {
void u8g_i2c_stop() { void u8g_i2c_stop() {
} }
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

Loading…
Cancel
Save